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 <string.h>
10
11 #include "core/fxcodec/jbig2/JBig2_Context.h"
12 #include "core/fxcodec/jbig2/JBig2_DocumentContext.h"
13 #include "core/fxcrt/span_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 int dword_size = pJbig2Context->m_height * pJbig2Context->m_dest_pitch / 4;
29 uint32_t* dword_buf = reinterpret_cast<uint32_t*>(pJbig2Context->m_dest_buf);
30 for (int i = 0; i < dword_size; i++)
31 dword_buf[i] = ~dword_buf[i];
32 return FXCODEC_STATUS::kDecodeFinished;
33 }
34
35 } // namespace
36
37 Jbig2Context::Jbig2Context() = default;
38
39 Jbig2Context::~Jbig2Context() = default;
40
41 // 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)42 FXCODEC_STATUS Jbig2Decoder::StartDecode(
43 Jbig2Context* pJbig2Context,
44 JBig2_DocumentContext* pJBig2DocumentContext,
45 uint32_t width,
46 uint32_t height,
47 pdfium::span<const uint8_t> src_span,
48 uint64_t src_key,
49 pdfium::span<const uint8_t> global_span,
50 uint64_t global_key,
51 pdfium::span<uint8_t> dest_buf,
52 uint32_t dest_pitch,
53 PauseIndicatorIface* pPause) {
54 pJbig2Context->m_width = width;
55 pJbig2Context->m_height = height;
56 pJbig2Context->m_pSrcSpan = src_span;
57 pJbig2Context->m_nSrcKey = src_key;
58 pJbig2Context->m_pGlobalSpan = global_span;
59 pJbig2Context->m_nGlobalKey = global_key;
60 pJbig2Context->m_dest_buf = dest_buf.data();
61 pJbig2Context->m_dest_pitch = dest_pitch;
62 fxcrt::spanset(dest_buf.first(height * dest_pitch), 0);
63 pJbig2Context->m_pContext =
64 CJBig2_Context::Create(global_span, global_key, src_span, src_key,
65 pJBig2DocumentContext->GetSymbolDictCache());
66 bool succeeded = pJbig2Context->m_pContext->GetFirstPage(
67 dest_buf, width, height, dest_pitch, pPause);
68 return Decode(pJbig2Context, succeeded);
69 }
70
71 // static
ContinueDecode(Jbig2Context * pJbig2Context,PauseIndicatorIface * pPause)72 FXCODEC_STATUS Jbig2Decoder::ContinueDecode(Jbig2Context* pJbig2Context,
73 PauseIndicatorIface* pPause) {
74 bool succeeded = pJbig2Context->m_pContext->Continue(pPause);
75 return Decode(pJbig2Context, succeeded);
76 }
77
78 } // namespace fxcodec
79