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/codec/ccodec_jbig2module.h"
8
9 #include <list>
10 #include <memory>
11
12 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
13 #include "core/fxcodec/JBig2_DocumentContext.h"
14 #include "core/fxcodec/jbig2/JBig2_Context.h"
15 #include "core/fxcodec/jbig2/JBig2_Image.h"
16 #include "core/fxcrt/fx_memory.h"
17 #include "third_party/base/ptr_util.h"
18
JBig2_DocumentContext()19 JBig2_DocumentContext::JBig2_DocumentContext() {}
20
~JBig2_DocumentContext()21 JBig2_DocumentContext::~JBig2_DocumentContext() {}
22
GetJBig2DocumentContext(std::unique_ptr<JBig2_DocumentContext> * pContextHolder)23 JBig2_DocumentContext* GetJBig2DocumentContext(
24 std::unique_ptr<JBig2_DocumentContext>* pContextHolder) {
25 if (!pContextHolder->get())
26 *pContextHolder = pdfium::MakeUnique<JBig2_DocumentContext>();
27 return pContextHolder->get();
28 }
29
CCodec_Jbig2Context()30 CCodec_Jbig2Context::CCodec_Jbig2Context()
31 : m_width(0),
32 m_height(0),
33 m_pGlobalStream(nullptr),
34 m_pSrcStream(nullptr),
35 m_dest_buf(0),
36 m_dest_pitch(0),
37 m_pPause(nullptr) {}
38
~CCodec_Jbig2Context()39 CCodec_Jbig2Context::~CCodec_Jbig2Context() {}
40
~CCodec_Jbig2Module()41 CCodec_Jbig2Module::~CCodec_Jbig2Module() {}
42
StartDecode(CCodec_Jbig2Context * pJbig2Context,std::unique_ptr<JBig2_DocumentContext> * pContextHolder,uint32_t width,uint32_t height,CPDF_StreamAcc * src_stream,CPDF_StreamAcc * global_stream,uint8_t * dest_buf,uint32_t dest_pitch,IFX_Pause * pPause)43 FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(
44 CCodec_Jbig2Context* pJbig2Context,
45 std::unique_ptr<JBig2_DocumentContext>* pContextHolder,
46 uint32_t width,
47 uint32_t height,
48 CPDF_StreamAcc* src_stream,
49 CPDF_StreamAcc* global_stream,
50 uint8_t* dest_buf,
51 uint32_t dest_pitch,
52 IFX_Pause* pPause) {
53 if (!pJbig2Context)
54 return FXCODEC_STATUS_ERR_PARAMS;
55
56 JBig2_DocumentContext* pJBig2DocumentContext =
57 GetJBig2DocumentContext(pContextHolder);
58 pJbig2Context->m_width = width;
59 pJbig2Context->m_height = height;
60 pJbig2Context->m_pSrcStream = src_stream;
61 pJbig2Context->m_pGlobalStream = global_stream;
62 pJbig2Context->m_dest_buf = dest_buf;
63 pJbig2Context->m_dest_pitch = dest_pitch;
64 pJbig2Context->m_pPause = pPause;
65 FXSYS_memset(dest_buf, 0, height * dest_pitch);
66 pJbig2Context->m_pContext = pdfium::MakeUnique<CJBig2_Context>(
67 global_stream, src_stream, pJBig2DocumentContext->GetSymbolDictCache(),
68 pPause, false);
69 if (!pJbig2Context->m_pContext)
70 return FXCODEC_STATUS_ERROR;
71
72 int ret = pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height,
73 dest_pitch, pPause);
74 if (pJbig2Context->m_pContext->GetProcessingStatus() ==
75 FXCODEC_STATUS_DECODE_FINISH) {
76 pJbig2Context->m_pContext.reset();
77 if (ret != JBIG2_SUCCESS)
78 return FXCODEC_STATUS_ERROR;
79
80 int dword_size = height * dest_pitch / 4;
81 uint32_t* dword_buf = (uint32_t*)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 return pJbig2Context->m_pContext->GetProcessingStatus();
87 }
88
ContinueDecode(CCodec_Jbig2Context * pJbig2Context,IFX_Pause * pPause)89 FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(
90 CCodec_Jbig2Context* pJbig2Context,
91 IFX_Pause* pPause) {
92 int ret = pJbig2Context->m_pContext->Continue(pPause);
93 if (pJbig2Context->m_pContext->GetProcessingStatus() !=
94 FXCODEC_STATUS_DECODE_FINISH) {
95 return pJbig2Context->m_pContext->GetProcessingStatus();
96 }
97 pJbig2Context->m_pContext.reset();
98 if (ret != JBIG2_SUCCESS)
99 return FXCODEC_STATUS_ERROR;
100
101 int dword_size = pJbig2Context->m_height * pJbig2Context->m_dest_pitch / 4;
102 uint32_t* dword_buf = (uint32_t*)pJbig2Context->m_dest_buf;
103 for (int i = 0; i < dword_size; i++)
104 dword_buf[i] = ~dword_buf[i];
105 return FXCODEC_STATUS_DECODE_FINISH;
106 }
107