1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fxcodec/jbig2/jbig2module.h"
8
9 #include "core/fxcodec/jbig2/JBig2_Context.h"
10 #include "core/fxcodec/jbig2/JBig2_DocumentContext.h"
11 #include "third_party/base/ptr_util.h"
12
13 namespace fxcodec {
14
GetJBig2DocumentContext(std::unique_ptr<JBig2_DocumentContext> * pContextHolder)15 JBig2_DocumentContext* GetJBig2DocumentContext(
16 std::unique_ptr<JBig2_DocumentContext>* pContextHolder) {
17 if (!*pContextHolder)
18 *pContextHolder = pdfium::MakeUnique<JBig2_DocumentContext>();
19 return pContextHolder->get();
20 }
21
22 Jbig2Context::Jbig2Context() = default;
23
24 Jbig2Context::~Jbig2Context() = default;
25
26 Jbig2Module::Jbig2Module() = default;
27
28 Jbig2Module::~Jbig2Module() = default;
29
StartDecode(Jbig2Context * pJbig2Context,std::unique_ptr<JBig2_DocumentContext> * pContextHolder,uint32_t width,uint32_t height,pdfium::span<const uint8_t> src_span,uint32_t src_objnum,pdfium::span<const uint8_t> global_span,uint32_t global_objnum,uint8_t * dest_buf,uint32_t dest_pitch,PauseIndicatorIface * pPause)30 FXCODEC_STATUS Jbig2Module::StartDecode(
31 Jbig2Context* pJbig2Context,
32 std::unique_ptr<JBig2_DocumentContext>* pContextHolder,
33 uint32_t width,
34 uint32_t height,
35 pdfium::span<const uint8_t> src_span,
36 uint32_t src_objnum,
37 pdfium::span<const uint8_t> global_span,
38 uint32_t global_objnum,
39 uint8_t* dest_buf,
40 uint32_t dest_pitch,
41 PauseIndicatorIface* pPause) {
42 if (!pJbig2Context)
43 return FXCODEC_STATUS_ERR_PARAMS;
44
45 JBig2_DocumentContext* pJBig2DocumentContext =
46 GetJBig2DocumentContext(pContextHolder);
47 pJbig2Context->m_width = width;
48 pJbig2Context->m_height = height;
49 pJbig2Context->m_pSrcSpan = src_span;
50 pJbig2Context->m_nSrcObjNum = src_objnum;
51 pJbig2Context->m_pGlobalSpan = global_span;
52 pJbig2Context->m_nGlobalObjNum = global_objnum;
53 pJbig2Context->m_dest_buf = dest_buf;
54 pJbig2Context->m_dest_pitch = dest_pitch;
55 memset(dest_buf, 0, height * dest_pitch);
56 pJbig2Context->m_pContext =
57 CJBig2_Context::Create(global_span, global_objnum, src_span, src_objnum,
58 pJBig2DocumentContext->GetSymbolDictCache());
59 bool succeeded = pJbig2Context->m_pContext->GetFirstPage(
60 dest_buf, width, height, dest_pitch, pPause);
61 return Decode(pJbig2Context, succeeded);
62 }
63
ContinueDecode(Jbig2Context * pJbig2Context,PauseIndicatorIface * pPause)64 FXCODEC_STATUS Jbig2Module::ContinueDecode(Jbig2Context* pJbig2Context,
65 PauseIndicatorIface* pPause) {
66 bool succeeded = pJbig2Context->m_pContext->Continue(pPause);
67 return Decode(pJbig2Context, succeeded);
68 }
69
Decode(Jbig2Context * pJbig2Context,bool decode_success)70 FXCODEC_STATUS Jbig2Module::Decode(Jbig2Context* pJbig2Context,
71 bool decode_success) {
72 FXCODEC_STATUS status = pJbig2Context->m_pContext->GetProcessingStatus();
73 if (status != FXCODEC_STATUS_DECODE_FINISH)
74 return status;
75
76 pJbig2Context->m_pContext.reset();
77 if (!decode_success)
78 return FXCODEC_STATUS_ERROR;
79
80 int dword_size = pJbig2Context->m_height * pJbig2Context->m_dest_pitch / 4;
81 uint32_t* dword_buf = reinterpret_cast<uint32_t*>(pJbig2Context->m_dest_buf);
82 for (int i = 0; i < dword_size; i++)
83 dword_buf[i] = ~dword_buf[i];
84 return FXCODEC_STATUS_DECODE_FINISH;
85 }
86
87 } // namespace fxcodec
88