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 #ifndef CORE_FXCODEC_JBIG2_JBIG2_CONTEXT_H_ 8 #define CORE_FXCODEC_JBIG2_JBIG2_CONTEXT_H_ 9 10 #include <list> 11 #include <memory> 12 #include <utility> 13 #include <vector> 14 15 #include "core/fpdfapi/parser/cpdf_object.h" 16 #include "core/fxcodec/fx_codec_def.h" 17 #include "core/fxcodec/jbig2/JBig2_Page.h" 18 #include "core/fxcodec/jbig2/JBig2_Segment.h" 19 #include "core/fxcrt/fx_safe_types.h" 20 21 class CJBig2_ArithDecoder; 22 class CJBig2_GRDProc; 23 class CPDF_StreamAcc; 24 class IFX_PauseIndicator; 25 26 // Cache is keyed by the ObjNum of a stream and an index within the stream. 27 using CJBig2_CacheKey = std::pair<uint32_t, uint32_t>; 28 using CJBig2_CachePair = 29 std::pair<CJBig2_CacheKey, std::unique_ptr<CJBig2_SymbolDict>>; 30 31 #define JBIG2_SUCCESS 0 32 #define JBIG2_FAILED -1 33 #define JBIG2_ERROR_TOO_SHORT -2 34 #define JBIG2_ERROR_FATAL -3 35 #define JBIG2_END_OF_PAGE 2 36 #define JBIG2_END_OF_FILE 3 37 #define JBIG2_ERROR_LIMIT -6 38 #define JBIG2_MIN_SEGMENT_SIZE 11 39 40 class CJBig2_Context { 41 public: 42 CJBig2_Context(const RetainPtr<CPDF_StreamAcc>& pGlobalStream, 43 const RetainPtr<CPDF_StreamAcc>& pSrcStream, 44 std::list<CJBig2_CachePair>* pSymbolDictCache, 45 bool bIsGlobal); 46 ~CJBig2_Context(); 47 48 int32_t getFirstPage(uint8_t* pBuf, 49 int32_t width, 50 int32_t height, 51 int32_t stride, 52 IFX_PauseIndicator* pPause); 53 54 int32_t Continue(IFX_PauseIndicator* pPause); GetProcessingStatus()55 FXCODEC_STATUS GetProcessingStatus() const { return m_ProcessingStatus; } 56 57 private: 58 int32_t decodeSequential(IFX_PauseIndicator* pPause); 59 int32_t decodeRandomFirstPage(IFX_PauseIndicator* pPause); 60 int32_t decodeRandom(IFX_PauseIndicator* pPause); 61 62 CJBig2_Segment* findSegmentByNumber(uint32_t dwNumber); 63 CJBig2_Segment* findReferredSegmentByTypeAndIndex(CJBig2_Segment* pSegment, 64 uint8_t cType, 65 int32_t nIndex); 66 67 int32_t parseSegmentHeader(CJBig2_Segment* pSegment); 68 int32_t parseSegmentData(CJBig2_Segment* pSegment, 69 IFX_PauseIndicator* pPause); 70 int32_t ProcessingParseSegmentData(CJBig2_Segment* pSegment, 71 IFX_PauseIndicator* pPause); 72 int32_t parseSymbolDict(CJBig2_Segment* pSegment); 73 int32_t parseTextRegion(CJBig2_Segment* pSegment); 74 int32_t parsePatternDict(CJBig2_Segment* pSegment, 75 IFX_PauseIndicator* pPause); 76 int32_t parseHalftoneRegion(CJBig2_Segment* pSegment, 77 IFX_PauseIndicator* pPause); 78 int32_t parseGenericRegion(CJBig2_Segment* pSegment, 79 IFX_PauseIndicator* pPause); 80 int32_t parseGenericRefinementRegion(CJBig2_Segment* pSegment); 81 int32_t parseTable(CJBig2_Segment* pSegment); 82 int32_t parseRegionInfo(JBig2RegionInfo* pRI); 83 84 std::vector<JBig2HuffmanCode> decodeSymbolIDHuffmanTable( 85 CJBig2_BitStream* pStream, 86 uint32_t SBNUMSYMS); 87 88 void huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, int NTEMP); 89 90 std::unique_ptr<CJBig2_Context> m_pGlobalContext; 91 std::unique_ptr<CJBig2_BitStream> m_pStream; 92 std::vector<std::unique_ptr<CJBig2_Segment>> m_SegmentList; 93 std::vector<std::unique_ptr<JBig2PageInfo>> m_PageInfoList; 94 std::unique_ptr<CJBig2_Image> m_pPage; 95 size_t m_nSegmentDecoded; 96 bool m_bInPage; 97 bool m_bBufSpecified; 98 int32_t m_PauseStep; 99 FXCODEC_STATUS m_ProcessingStatus; 100 std::vector<JBig2ArithCtx> m_gbContext; 101 std::unique_ptr<CJBig2_ArithDecoder> m_pArithDecoder; 102 std::unique_ptr<CJBig2_GRDProc> m_pGRD; 103 std::unique_ptr<CJBig2_Segment> m_pSegment; 104 FX_SAFE_UINT32 m_dwOffset; 105 JBig2RegionInfo m_ri; 106 std::list<CJBig2_CachePair>* const m_pSymbolDictCache; 107 bool m_bIsGlobal; 108 }; 109 110 #endif // CORE_FXCODEC_JBIG2_JBIG2_CONTEXT_H_ 111