Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "../../../include/fxcodec/fx_codec.h" | 7 #include "../../../include/fxcodec/fx_codec.h" |
| 8 #include "codec_int.h" | 8 #include "codec_int.h" |
| 9 | 9 |
| 10 // Holds per-document JBig2 related data. | |
| 11 class JBig2DocumentContext : public CFX_DestructObject { | |
| 12 public: | |
| 13 std::list<CJBig2_CachePair>* GetSymbolDictCache() { | |
| 14 return &m_SymbolDictCache; | |
| 15 } | |
| 16 | |
| 17 ~JBig2DocumentContext() { | |
| 18 for (auto it : m_SymbolDictCache) { | |
| 19 delete it.second; | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 private: | |
| 24 std::list<CJBig2_CachePair> m_SymbolDictCache; | |
| 25 }; | |
| 26 | |
| 27 JBig2DocumentContext* GetJBig2DocumentContext(CCodec_Jbig2Module* pModule, | |
| 28 CFX_PrivateData* pPrivateData) { | |
| 29 void* pModulePrivateData = pPrivateData->GetPrivateData(pModule); | |
|
Lei Zhang
2015/10/06 22:40:24
It's silly that GetPrvigateData() does not return
David Lattimore
2015/10/07 22:41:53
I think it's only guaranteed to be CFX_DestructObj
| |
| 30 if (pModulePrivateData) { | |
| 31 CFX_DestructObject* pDestructObject = | |
| 32 reinterpret_cast<CFX_DestructObject*>(pModulePrivateData); | |
| 33 return static_cast<JBig2DocumentContext*>(pDestructObject); | |
| 34 } | |
| 35 JBig2DocumentContext* pJBig2DocumentContext = new JBig2DocumentContext(); | |
| 36 pPrivateData->SetPrivateObj(pModule, pJBig2DocumentContext); | |
| 37 return pJBig2DocumentContext; | |
| 38 } | |
| 39 | |
| 10 CCodec_Jbig2Context::CCodec_Jbig2Context() { | 40 CCodec_Jbig2Context::CCodec_Jbig2Context() { |
| 11 FXSYS_memset(this, 0, sizeof(CCodec_Jbig2Context)); | 41 FXSYS_memset(this, 0, sizeof(CCodec_Jbig2Context)); |
| 12 } | 42 } |
| 13 | 43 |
| 14 CCodec_Jbig2Module::~CCodec_Jbig2Module() { | 44 CCodec_Jbig2Module::~CCodec_Jbig2Module() { |
| 15 for (auto it : m_SymbolDictCache) { | |
| 16 delete it.second; | |
| 17 } | |
| 18 } | 45 } |
| 19 | 46 |
| 20 void* CCodec_Jbig2Module::CreateJbig2Context() { | 47 void* CCodec_Jbig2Module::CreateJbig2Context() { |
| 21 return new CCodec_Jbig2Context(); | 48 return new CCodec_Jbig2Context(); |
| 22 } | 49 } |
| 23 void CCodec_Jbig2Module::DestroyJbig2Context(void* pJbig2Content) { | 50 void CCodec_Jbig2Module::DestroyJbig2Context(void* pJbig2Content) { |
| 24 if (pJbig2Content) { | 51 if (pJbig2Content) { |
| 25 CJBig2_Context::DestroyContext( | 52 CJBig2_Context::DestroyContext( |
| 26 ((CCodec_Jbig2Context*)pJbig2Content)->m_pContext); | 53 ((CCodec_Jbig2Context*)pJbig2Content)->m_pContext); |
| 27 delete (CCodec_Jbig2Context*)pJbig2Content; | 54 delete (CCodec_Jbig2Context*)pJbig2Content; |
| 28 } | 55 } |
| 29 pJbig2Content = NULL; | 56 pJbig2Content = NULL; |
| 30 } | 57 } |
| 31 FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(void* pJbig2Context, | 58 FXCODEC_STATUS CCodec_Jbig2Module::StartDecode(void* pJbig2Context, |
| 59 CFX_PrivateData* pPrivateData, | |
| 32 FX_DWORD width, | 60 FX_DWORD width, |
| 33 FX_DWORD height, | 61 FX_DWORD height, |
| 34 const uint8_t* src_buf, | 62 CPDF_StreamAcc* src_stream, |
| 35 FX_DWORD src_size, | 63 CPDF_StreamAcc* global_stream, |
| 36 const uint8_t* global_data, | |
| 37 FX_DWORD global_size, | |
| 38 uint8_t* dest_buf, | 64 uint8_t* dest_buf, |
| 39 FX_DWORD dest_pitch, | 65 FX_DWORD dest_pitch, |
| 40 IFX_Pause* pPause) { | 66 IFX_Pause* pPause) { |
| 41 if (!pJbig2Context) { | 67 if (!pJbig2Context) { |
| 42 return FXCODEC_STATUS_ERR_PARAMS; | 68 return FXCODEC_STATUS_ERR_PARAMS; |
| 43 } | 69 } |
| 70 JBig2DocumentContext* pJBig2DocumentContext = | |
| 71 GetJBig2DocumentContext(this, pPrivateData); | |
| 44 CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context; | 72 CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context; |
| 45 m_pJbig2Context->m_width = width; | 73 m_pJbig2Context->m_width = width; |
| 46 m_pJbig2Context->m_height = height; | 74 m_pJbig2Context->m_height = height; |
| 47 m_pJbig2Context->m_src_buf = (unsigned char*)src_buf; | 75 m_pJbig2Context->m_pSrcStream = src_stream; |
| 48 m_pJbig2Context->m_src_size = src_size; | 76 m_pJbig2Context->m_pGlobalStream = global_stream; |
| 49 m_pJbig2Context->m_global_data = global_data; | |
| 50 m_pJbig2Context->m_global_size = global_size; | |
| 51 m_pJbig2Context->m_dest_buf = dest_buf; | 77 m_pJbig2Context->m_dest_buf = dest_buf; |
| 52 m_pJbig2Context->m_dest_pitch = dest_pitch; | 78 m_pJbig2Context->m_dest_pitch = dest_pitch; |
| 53 m_pJbig2Context->m_pPause = pPause; | 79 m_pJbig2Context->m_pPause = pPause; |
| 54 m_pJbig2Context->m_bFileReader = FALSE; | |
| 55 FXSYS_memset(dest_buf, 0, height * dest_pitch); | 80 FXSYS_memset(dest_buf, 0, height * dest_pitch); |
| 56 m_pJbig2Context->m_pContext = CJBig2_Context::CreateContext( | 81 m_pJbig2Context->m_pContext = CJBig2_Context::CreateContext( |
| 57 global_data, global_size, src_buf, src_size, &m_SymbolDictCache, pPause); | 82 global_stream, src_stream, pJBig2DocumentContext->GetSymbolDictCache(), |
| 83 pPause); | |
| 58 if (!m_pJbig2Context->m_pContext) { | 84 if (!m_pJbig2Context->m_pContext) { |
| 59 return FXCODEC_STATUS_ERROR; | 85 return FXCODEC_STATUS_ERROR; |
| 60 } | 86 } |
| 61 int ret = m_pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height, | 87 int ret = m_pJbig2Context->m_pContext->getFirstPage(dest_buf, width, height, |
| 62 dest_pitch, pPause); | 88 dest_pitch, pPause); |
| 63 if (m_pJbig2Context->m_pContext->GetProcessingStatus() == | 89 if (m_pJbig2Context->m_pContext->GetProcessingStatus() == |
| 64 FXCODEC_STATUS_DECODE_FINISH) { | 90 FXCODEC_STATUS_DECODE_FINISH) { |
| 65 CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext); | 91 CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext); |
| 66 m_pJbig2Context->m_pContext = NULL; | 92 m_pJbig2Context->m_pContext = NULL; |
| 67 if (ret != JBIG2_SUCCESS) { | 93 if (ret != JBIG2_SUCCESS) { |
| 68 return FXCODEC_STATUS_ERROR; | 94 return FXCODEC_STATUS_ERROR; |
| 69 } | 95 } |
| 70 int dword_size = height * dest_pitch / 4; | 96 int dword_size = height * dest_pitch / 4; |
| 71 FX_DWORD* dword_buf = (FX_DWORD*)dest_buf; | 97 FX_DWORD* dword_buf = (FX_DWORD*)dest_buf; |
| 72 for (int i = 0; i < dword_size; i++) { | 98 for (int i = 0; i < dword_size; i++) { |
| 73 dword_buf[i] = ~dword_buf[i]; | 99 dword_buf[i] = ~dword_buf[i]; |
| 74 } | 100 } |
| 75 return FXCODEC_STATUS_DECODE_FINISH; | 101 return FXCODEC_STATUS_DECODE_FINISH; |
| 76 } | 102 } |
| 77 return m_pJbig2Context->m_pContext->GetProcessingStatus(); | 103 return m_pJbig2Context->m_pContext->GetProcessingStatus(); |
| 78 } | 104 } |
| 79 FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(void* pJbig2Context, | 105 FXCODEC_STATUS CCodec_Jbig2Module::ContinueDecode(void* pJbig2Context, |
| 80 IFX_Pause* pPause) { | 106 IFX_Pause* pPause) { |
| 81 CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context; | 107 CCodec_Jbig2Context* m_pJbig2Context = (CCodec_Jbig2Context*)pJbig2Context; |
| 82 int ret = m_pJbig2Context->m_pContext->Continue(pPause); | 108 int ret = m_pJbig2Context->m_pContext->Continue(pPause); |
| 83 if (m_pJbig2Context->m_pContext->GetProcessingStatus() != | 109 if (m_pJbig2Context->m_pContext->GetProcessingStatus() != |
| 84 FXCODEC_STATUS_DECODE_FINISH) { | 110 FXCODEC_STATUS_DECODE_FINISH) { |
| 85 return m_pJbig2Context->m_pContext->GetProcessingStatus(); | 111 return m_pJbig2Context->m_pContext->GetProcessingStatus(); |
| 86 } | 112 } |
| 87 if (m_pJbig2Context->m_bFileReader) { | |
| 88 CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext); | |
| 89 m_pJbig2Context->m_pContext = NULL; | |
| 90 if (ret != JBIG2_SUCCESS) { | |
| 91 FX_Free(m_pJbig2Context->m_src_buf); | |
| 92 m_pJbig2Context->m_src_buf = NULL; | |
| 93 return FXCODEC_STATUS_ERROR; | |
| 94 } | |
| 95 delete m_pJbig2Context->m_dest_image; | |
| 96 FX_Free(m_pJbig2Context->m_src_buf); | |
| 97 return FXCODEC_STATUS_DECODE_FINISH; | |
| 98 } | |
| 99 CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext); | 113 CJBig2_Context::DestroyContext(m_pJbig2Context->m_pContext); |
| 100 m_pJbig2Context->m_pContext = NULL; | 114 m_pJbig2Context->m_pContext = NULL; |
| 101 if (ret != JBIG2_SUCCESS) { | 115 if (ret != JBIG2_SUCCESS) { |
| 102 return FXCODEC_STATUS_ERROR; | 116 return FXCODEC_STATUS_ERROR; |
| 103 } | 117 } |
| 104 int dword_size = | 118 int dword_size = |
| 105 m_pJbig2Context->m_height * m_pJbig2Context->m_dest_pitch / 4; | 119 m_pJbig2Context->m_height * m_pJbig2Context->m_dest_pitch / 4; |
| 106 FX_DWORD* dword_buf = (FX_DWORD*)m_pJbig2Context->m_dest_buf; | 120 FX_DWORD* dword_buf = (FX_DWORD*)m_pJbig2Context->m_dest_buf; |
| 107 for (int i = 0; i < dword_size; i++) { | 121 for (int i = 0; i < dword_size; i++) { |
| 108 dword_buf[i] = ~dword_buf[i]; | 122 dword_buf[i] = ~dword_buf[i]; |
| 109 } | 123 } |
| 110 return FXCODEC_STATUS_DECODE_FINISH; | 124 return FXCODEC_STATUS_DECODE_FINISH; |
| 111 } | 125 } |
| OLD | NEW |