• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 #include "core/fxcrt/fx_2d_size.h"
12 #include "core/fxcrt/span_util.h"
13 #include "core/fxcrt/stl_util.h"
14 
15 namespace fxcodec {
16 
17 namespace {
18 
Decode(Jbig2Context * pJbig2Context,bool decode_success)19 FXCODEC_STATUS Decode(Jbig2Context* pJbig2Context, bool decode_success) {
20   FXCODEC_STATUS status = pJbig2Context->m_pContext->GetProcessingStatus();
21   if (status != FXCODEC_STATUS::kDecodeFinished) {
22     return status;
23   }
24   pJbig2Context->m_pContext.reset();
25   if (!decode_success) {
26     return FXCODEC_STATUS::kError;
27   }
28   uint32_t byte_size = pJbig2Context->m_height * pJbig2Context->m_dest_pitch;
29   pdfium::span<uint8_t> nonraw_span(pJbig2Context->m_dest_buf.first(byte_size));
30   auto dword_span = fxcrt::reinterpret_span<uint32_t>(nonraw_span);
31   for (auto& pix : dword_span) {
32     pix = ~pix;
33   }
34   return FXCODEC_STATUS::kDecodeFinished;
35 }
36 
37 }  // namespace
38 
39 Jbig2Context::Jbig2Context() = default;
40 
41 Jbig2Context::~Jbig2Context() = default;
42 
43 // static
StartDecode(Jbig2Context * pJbig2Context,JBig2_DocumentContext * pJBig2DocumentContext,uint32_t width,uint32_t height,pdfium::span<const uint8_t> src_span,uint64_t src_key,pdfium::span<const uint8_t> global_span,uint64_t global_key,pdfium::span<uint8_t> dest_buf,uint32_t dest_pitch,PauseIndicatorIface * pPause)44 FXCODEC_STATUS Jbig2Decoder::StartDecode(
45     Jbig2Context* pJbig2Context,
46     JBig2_DocumentContext* pJBig2DocumentContext,
47     uint32_t width,
48     uint32_t height,
49     pdfium::span<const uint8_t> src_span,
50     uint64_t src_key,
51     pdfium::span<const uint8_t> global_span,
52     uint64_t global_key,
53     pdfium::span<uint8_t> dest_buf,
54     uint32_t dest_pitch,
55     PauseIndicatorIface* pPause) {
56   pJbig2Context->m_width = width;
57   pJbig2Context->m_height = height;
58   pJbig2Context->m_pSrcSpan = src_span;
59   pJbig2Context->m_nSrcKey = src_key;
60   pJbig2Context->m_pGlobalSpan = global_span;
61   pJbig2Context->m_nGlobalKey = global_key;
62   pJbig2Context->m_dest_buf = dest_buf;
63   pJbig2Context->m_dest_pitch = dest_pitch;
64   fxcrt::Fill(dest_buf.first(Fx2DSizeOrDie(height, dest_pitch)), 0);
65   pJbig2Context->m_pContext =
66       CJBig2_Context::Create(global_span, global_key, src_span, src_key,
67                              pJBig2DocumentContext->GetSymbolDictCache());
68   bool succeeded = pJbig2Context->m_pContext->GetFirstPage(
69       dest_buf, width, height, dest_pitch, pPause);
70   return Decode(pJbig2Context, succeeded);
71 }
72 
73 // static
ContinueDecode(Jbig2Context * pJbig2Context,PauseIndicatorIface * pPause)74 FXCODEC_STATUS Jbig2Decoder::ContinueDecode(Jbig2Context* pJbig2Context,
75                                             PauseIndicatorIface* pPause) {
76   bool succeeded = pJbig2Context->m_pContext->Continue(pPause);
77   return Decode(pJbig2Context, succeeded);
78 }
79 
80 }  // namespace fxcodec
81