18a665784a7382cc3d70c8ce9b6ec36376ad44f5
[pdfium.git] / core / src / fxcodec / codec / fx_codec_jbig.cpp
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 "../../../include/fxcodec/fx_codec.h"
8 #include "codec_int.h"
9
10 CCodec_Jbig2Context::CCodec_Jbig2Context() {
11   FXSYS_memset(this, 0, sizeof(CCodec_Jbig2Context));
12 }
13
14 CCodec_Jbig2Module::~CCodec_Jbig2Module() {
15   for (auto it : m_SymbolDictCache) {
16     delete it.second;
17   }
18 }
19
20 void* CCodec_Jbig2Module::CreateJbig2Context() {
21   return new CCodec_Jbig2Context();
22 }
23 void CCodec_Jbig2Module::DestroyJbig2Context(void* pJbig2Content) {
24   if (pJbig2Content) {
25     CJBig2_Context::DestroyContext(
26         ((CCodec_Jbig2Context*)pJbig2Content)->m_pContext);
27     delete (CCodec_Jbig2Context*)pJbig2Content;
28   }
29   pJbig2Content = NULL;
30 }
31 FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(void* pJbig2Context,
32                                                FX_DWORD width,
33                                                FX_DWORD height,
34                                                const uint8_t* src_buf,
35                                                FX_DWORD src_size,
36                                                const uint8_t* global_data,
37                                                FX_DWORD global_size,
38                                                uint8_t* dest_buf,
39                                                FX_DWORD dest_pitch,
40                                                IFX_Pause* pPause) {
41   if (!pJbig2Context) {
42     return FXCODEC_STATUS_ERR_PARAMS;
43   }
44   CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
45   m_pJbig2Context->m_width = width;
46   m_pJbig2Context->m_height = height;
47   m_pJbig2Context->m_src_buf = (unsigned char*)src_buf;
48   m_pJbig2Context->m_src_size = src_size;
49   m_pJbig2Context->m_global_data = global_data;
50   m_pJbig2Context->m_global_size = global_size;
51   m_pJbig2Context->m_dest_buf = dest_buf;
52   m_pJbig2Context->m_dest_pitch = dest_pitch;
53   m_pJbig2Context->m_pPause = pPause;
54   FXSYS_memset(dest_buf, 0, height * dest_pitch);
55   m_pJbig2Context->m_pContext = CJBig2_Context::CreateContext(
56       global_data, global_size, src_buf, src_size, &m_SymbolDictCache, pPause);
57   if (!m_pJbig2Context->m_pContext) {
58     return FXCODEC_STATUS_ERROR;
59   }
60   int ret = m_pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height,
61                                                       dest_pitch, pPause);
62   if (m_pJbig2Context->m_pContext->GetProcessingStatus() ==
63       FXCODEC_STATUS_DECODE_FINISH) {
64     CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
65     m_pJbig2Context->m_pContext = NULL;
66     if (ret != JBIG2_SUCCESS) {
67       return FXCODEC_STATUS_ERROR;
68     }
69     int dword_size = height * dest_pitch / 4;
70     FX_DWORD* dword_buf = (FX_DWORD*)dest_buf;
71     for (int i = 0; i < dword_size; i++) {
72       dword_buf[i] = ~dword_buf[i];
73     }
74     return FXCODEC_STATUS_DECODE_FINISH;
75   }
76   return m_pJbig2Context->m_pContext->GetProcessingStatus();
77 }
78 FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(void* pJbig2Context,
79                                                   IFX_Pause* pPause) {
80   CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context;
81   int ret = m_pJbig2Context->m_pContext->Continue(pPause);
82   if (m_pJbig2Context->m_pContext->GetProcessingStatus() !=
83       FXCODEC_STATUS_DECODE_FINISH) {
84     return m_pJbig2Context->m_pContext->GetProcessingStatus();
85   }
86   CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext);
87   m_pJbig2Context->m_pContext = NULL;
88   if (ret != JBIG2_SUCCESS) {
89     return FXCODEC_STATUS_ERROR;
90   }
91   int dword_size =
92       m_pJbig2Context->m_height * m_pJbig2Context->m_dest_pitch / 4;
93   FX_DWORD* dword_buf = (FX_DWORD*)m_pJbig2Context->m_dest_buf;
94   for (int i = 0; i < dword_size; i++) {
95     dword_buf[i] = ~dword_buf[i];
96   }
97   return FXCODEC_STATUS_DECODE_FINISH;
98 }