OLD | NEW |
| (Empty) |
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 <list> | |
8 | |
9 #include "core/include/fxcodec/fx_codec.h" | |
10 #include "core/src/fxcodec/codec/codec_int.h" | |
11 | |
12 // Holds per-document JBig2 related data. | |
13 class JBig2DocumentContext : public CFX_DestructObject { | |
14 public: | |
15 std::list<CJBig2_CachePair>* GetSymbolDictCache() { | |
16 return &m_SymbolDictCache; | |
17 } | |
18 | |
19 ~JBig2DocumentContext() override { | |
20 for (auto it : m_SymbolDictCache) { | |
21 delete it.second; | |
22 } | |
23 } | |
24 | |
25 private: | |
26 std::list<CJBig2_CachePair> m_SymbolDictCache; | |
27 }; | |
28 | |
29 JBig2DocumentContext* GetJBig2DocumentContext(CCodec_Jbig2Module* pModule, | |
30 CFX_PrivateData* pPrivateData) { | |
31 void* pModulePrivateData = pPrivateData->GetPrivateData(pModule); | |
32 if (pModulePrivateData) { | |
33 CFX_DestructObject* pDestructObject = | |
34 reinterpret_cast<CFX_DestructObject*>(pModulePrivateData); | |
35 return static_cast<JBig2DocumentContext*>(pDestructObject); | |
36 } | |
37 JBig2DocumentContext* pJBig2DocumentContext = new JBig2DocumentContext(); | |
38 pPrivateData->SetPrivateObj(pModule, pJBig2DocumentContext); | |
39 return pJBig2DocumentContext; | |
40 } | |
41 | |
42 CCodec_Jbig2Context::CCodec_Jbig2Context() { | |
43 FXSYS_memset(this, 0, sizeof(CCodec_Jbig2Context)); | |
44 } | |
45 | |
46 CCodec_Jbig2Module::~CCodec_Jbig2Module() { | |
47 } | |
48 | |
49 void* CCodec_Jbig2Module::CreateJbig2Context() { | |
50 return new CCodec_Jbig2Context(); | |
51 } | |
52 void CCodec_Jbig2Module::DestroyJbig2Context(void* pJbig2Content) { | |
53 if (pJbig2Content) { | |
54 CJBig2_Context::DestroyContext( | |
55 ((CCodec_Jbig2Context*)pJbig2Content)->m_pContext); | |
56 delete (CCodec_Jbig2Context*)pJbig2Content; | |
57 } | |
58 pJbig2Content = NULL; | |
59 } | |
60 FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(void* pJbig2Context, | |
61 CFX_PrivateData* pPrivateData, | |
62 FX_DWORD width, | |
63 FX_DWORD height, | |
64 CPDF_StreamAcc* src_stream, | |
65 CPDF_StreamAcc* global_stream, | |
66 uint8_t* dest_buf, | |
67 FX_DWORD dest_pitch, | |
68 IFX_Pause* pPause) { | |
69 if (!pJbig2Context) { | |
70 return FXCODEC_STATUS_ERR_PARAMS; | |
71 } | |
72 JBig2DocumentContext* pJBig2DocumentContext = | |
73 GetJBig2DocumentContext(this, pPrivateData); | |
74 CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context; | |
75 m_pJbig2Context->m_width = width; | |
76 m_pJbig2Context->m_height = height; | |
77 m_pJbig2Context->m_pSrcStream = src_stream; | |
78 m_pJbig2Context->m_pGlobalStream = global_stream; | |
79 m_pJbig2Context->m_dest_buf = dest_buf; | |
80 m_pJbig2Context->m_dest_pitch = dest_pitch; | |
81 m_pJbig2Context->m_pPause = pPause; | |
82 FXSYS_memset(dest_buf, 0, height * dest_pitch); | |
83 m_pJbig2Context->m_pContext = CJBig2_Context::CreateContext( | |
84 global_stream, src_stream, pJBig2DocumentContext->GetSymbolDictCache(), | |
85 pPause); | |
86 if (!m_pJbig2Context->m_pContext) { | |
87 return FXCODEC_STATUS_ERROR; | |
88 } | |
89 int ret = m_pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height, | |
90 dest_pitch, pPause); | |
91 if (m_pJbig2Context->m_pContext->GetProcessingStatus() == | |
92 FXCODEC_STATUS_DECODE_FINISH) { | |
93 CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext); | |
94 m_pJbig2Context->m_pContext = NULL; | |
95 if (ret != JBIG2_SUCCESS) { | |
96 return FXCODEC_STATUS_ERROR; | |
97 } | |
98 int dword_size = height * dest_pitch / 4; | |
99 FX_DWORD* dword_buf = (FX_DWORD*)dest_buf; | |
100 for (int i = 0; i < dword_size; i++) { | |
101 dword_buf[i] = ~dword_buf[i]; | |
102 } | |
103 return FXCODEC_STATUS_DECODE_FINISH; | |
104 } | |
105 return m_pJbig2Context->m_pContext->GetProcessingStatus(); | |
106 } | |
107 FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(void* pJbig2Context, | |
108 IFX_Pause* pPause) { | |
109 CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context; | |
110 int ret = m_pJbig2Context->m_pContext->Continue(pPause); | |
111 if (m_pJbig2Context->m_pContext->GetProcessingStatus() != | |
112 FXCODEC_STATUS_DECODE_FINISH) { | |
113 return m_pJbig2Context->m_pContext->GetProcessingStatus(); | |
114 } | |
115 CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext); | |
116 m_pJbig2Context->m_pContext = NULL; | |
117 if (ret != JBIG2_SUCCESS) { | |
118 return FXCODEC_STATUS_ERROR; | |
119 } | |
120 int dword_size = | |
121 m_pJbig2Context->m_height * m_pJbig2Context->m_dest_pitch / 4; | |
122 FX_DWORD* dword_buf = (FX_DWORD*)m_pJbig2Context->m_dest_buf; | |
123 for (int i = 0; i < dword_size; i++) { | |
124 dword_buf[i] = ~dword_buf[i]; | |
125 } | |
126 return FXCODEC_STATUS_DECODE_FINISH; | |
127 } | |
OLD | NEW |