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/fxcodec/fx_codec_def.h" 16 #include "core/fxcodec/jbig2/JBig2_Page.h" 17 #include "core/fxcodec/jbig2/JBig2_Segment.h" 18 #include "core/fxcrt/fx_safe_types.h" 19 #include "third_party/base/span.h" 20 21 class CJBig2_ArithDecoder; 22 class CJBig2_GRDProc; 23 class CPDF_StreamAcc; 24 class PauseIndicatorIface; 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_MIN_SEGMENT_SIZE 11 32 33 enum class JBig2_Result { kSuccess, kFailure, kEndReached }; 34 35 class CJBig2_Context { 36 public: 37 static std::unique_ptr<CJBig2_Context> Create( 38 pdfium::span<const uint8_t> pGlobalSpan, 39 uint32_t dwGlobalObjNum, 40 pdfium::span<const uint8_t> pSrcSpan, 41 uint32_t dwSrcObjNum, 42 std::list<CJBig2_CachePair>* pSymbolDictCache); 43 44 ~CJBig2_Context(); 45 46 static bool HuffmanAssignCode(JBig2HuffmanCode* SBSYMCODES, uint32_t NTEMP); 47 48 bool GetFirstPage(uint8_t* pBuf, 49 int32_t width, 50 int32_t height, 51 int32_t stride, 52 PauseIndicatorIface* pPause); 53 54 bool Continue(PauseIndicatorIface* pPause); GetProcessingStatus()55 FXCODEC_STATUS GetProcessingStatus() const { return m_ProcessingStatus; } 56 57 private: 58 CJBig2_Context(pdfium::span<const uint8_t> pSrcSpan, 59 uint32_t dwObjNum, 60 std::list<CJBig2_CachePair>* pSymbolDictCache, 61 bool bIsGlobal); 62 63 JBig2_Result DecodeSequential(PauseIndicatorIface* pPause); 64 65 CJBig2_Segment* FindSegmentByNumber(uint32_t dwNumber); 66 CJBig2_Segment* FindReferredTableSegmentByIndex(CJBig2_Segment* pSegment, 67 int32_t nIndex); 68 69 JBig2_Result ParseSegmentHeader(CJBig2_Segment* pSegment); 70 JBig2_Result ParseSegmentData(CJBig2_Segment* pSegment, 71 PauseIndicatorIface* pPause); 72 JBig2_Result ProcessingParseSegmentData(CJBig2_Segment* pSegment, 73 PauseIndicatorIface* pPause); 74 JBig2_Result ParseSymbolDict(CJBig2_Segment* pSegment); 75 JBig2_Result ParseTextRegion(CJBig2_Segment* pSegment); 76 JBig2_Result ParsePatternDict(CJBig2_Segment* pSegment, 77 PauseIndicatorIface* pPause); 78 JBig2_Result ParseHalftoneRegion(CJBig2_Segment* pSegment, 79 PauseIndicatorIface* pPause); 80 JBig2_Result ParseGenericRegion(CJBig2_Segment* pSegment, 81 PauseIndicatorIface* pPause); 82 JBig2_Result ParseGenericRefinementRegion(CJBig2_Segment* pSegment); 83 JBig2_Result ParseTable(CJBig2_Segment* pSegment); 84 JBig2_Result ParseRegionInfo(JBig2RegionInfo* pRI); 85 86 std::vector<JBig2HuffmanCode> DecodeSymbolIDHuffmanTable(uint32_t SBNUMSYMS); 87 const CJBig2_HuffmanTable* GetHuffmanTable(size_t idx); 88 89 std::unique_ptr<CJBig2_Context> m_pGlobalContext; 90 std::unique_ptr<CJBig2_BitStream> m_pStream; 91 std::vector<std::unique_ptr<CJBig2_Segment>> m_SegmentList; 92 std::vector<std::unique_ptr<JBig2PageInfo>> m_PageInfoList; 93 std::unique_ptr<CJBig2_Image> m_pPage; 94 std::vector<std::unique_ptr<CJBig2_HuffmanTable>> m_HuffmanTables; 95 const bool m_bIsGlobal; 96 bool m_bInPage = false; 97 bool m_bBufSpecified = false; 98 int32_t m_PauseStep = 10; 99 FXCODEC_STATUS m_ProcessingStatus = FXCODEC_STATUS_FRAME_READY; 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 = 0; 105 JBig2RegionInfo m_ri; 106 std::list<CJBig2_CachePair>* const m_pSymbolDictCache; 107 }; 108 109 #endif // CORE_FXCODEC_JBIG2_JBIG2_CONTEXT_H_ 110