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/jbig2_decoder.h"
8 
9 #include "core/fxcodec/jbig2/JBig2_Context.h"
10 #include "core/fxcodec/jbig2/JBig2_DocumentContext.h"
11 
12 namespace fxcodec {
13 
14 namespace {
15 
Decode(Jbig2Context * pJbig2Context,bool decode_success)16 FXCODEC_STATUS Decode(Jbig2Context* pJbig2Context, bool decode_success) {
17   FXCODEC_STATUS status = pJbig2Context->m_pContext->GetProcessingStatus();
18   if (status != FXCODEC_STATUS_DECODE_FINISH)
19     return status;
20 
21   pJbig2Context->m_pContext.reset();
22   if (!decode_success)
23     return FXCODEC_STATUS_ERROR;
24 
25   int dword_size = pJbig2Context->m_height * pJbig2Context->m_dest_pitch / 4;
26   uint32_t* dword_buf = reinterpret_cast<uint32_t*>(pJbig2Context->m_dest_buf);
27   for (int i = 0; i < dword_size; i++)
28     dword_buf[i] = ~dword_buf[i];
29   return FXCODEC_STATUS_DECODE_FINISH;
30 }
31 
32 }  // namespace
33 
GetJBig2DocumentContext(std::unique_ptr<JBig2_DocumentContext> * pContextHolder)34 JBig2_DocumentContext* GetJBig2DocumentContext(
35     std::unique_ptr<JBig2_DocumentContext>* pContextHolder) {
36   if (!*pContextHolder)
37     *pContextHolder = std::make_unique<JBig2_DocumentContext>();
38   return pContextHolder->get();
39 }
40 
41 Jbig2Context::Jbig2Context() = default;
42 
43 Jbig2Context::~Jbig2Context() = default;
44 
45 // static
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)46 FXCODEC_STATUS Jbig2Decoder::StartDecode(
47     Jbig2Context* pJbig2Context,
48     std::unique_ptr<JBig2_DocumentContext>* pContextHolder,
49     uint32_t width,
50     uint32_t height,
51     pdfium::span<const uint8_t> src_span,
52     uint32_t src_objnum,
53     pdfium::span<const uint8_t> global_span,
54     uint32_t global_objnum,
55     uint8_t* dest_buf,
56     uint32_t dest_pitch,
57     PauseIndicatorIface* pPause) {
58   ASSERT(pJbig2Context);
59 
60   JBig2_DocumentContext* pJBig2DocumentContext =
61       GetJBig2DocumentContext(pContextHolder);
62   pJbig2Context->m_width = width;
63   pJbig2Context->m_height = height;
64   pJbig2Context->m_pSrcSpan = src_span;
65   pJbig2Context->m_nSrcObjNum = src_objnum;
66   pJbig2Context->m_pGlobalSpan = global_span;
67   pJbig2Context->m_nGlobalObjNum = global_objnum;
68   pJbig2Context->m_dest_buf = dest_buf;
69   pJbig2Context->m_dest_pitch = dest_pitch;
70   memset(dest_buf, 0, height * dest_pitch);
71   pJbig2Context->m_pContext =
72       CJBig2_Context::Create(global_span, global_objnum, src_span, src_objnum,
73                              pJBig2DocumentContext->GetSymbolDictCache());
74   bool succeeded = pJbig2Context->m_pContext->GetFirstPage(
75       dest_buf, width, height, dest_pitch, pPause);
76   return Decode(pJbig2Context, succeeded);
77 }
78 
79 // static
ContinueDecode(Jbig2Context * pJbig2Context,PauseIndicatorIface * pPause)80 FXCODEC_STATUS Jbig2Decoder::ContinueDecode(Jbig2Context* pJbig2Context,
81                                             PauseIndicatorIface* pPause) {
82   bool succeeded = pJbig2Context->m_pContext->Continue(pPause);
83   return Decode(pJbig2Context, succeeded);
84 }
85 
86 }  // namespace fxcodec
87