Home | History | Annotate | Download | only in jbig2
      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_SRC_FXCODEC_JBIG2_JBIG2_CONTEXT_H_
      8 #define CORE_SRC_FXCODEC_JBIG2_JBIG2_CONTEXT_H_
      9 
     10 #include <list>
     11 #include <memory>
     12 #include <utility>
     13 
     14 #include "JBig2_List.h"
     15 #include "JBig2_Page.h"
     16 #include "JBig2_Segment.h"
     17 #include "core/include/fpdfapi/fpdf_objects.h"
     18 #include "core/include/fxcodec/fx_codec_def.h"
     19 
     20 class CJBig2_ArithDecoder;
     21 class CJBig2_GRDProc;
     22 class IFX_Pause;
     23 
     24 // Cache is keyed by the ObjNum of a stream and an index within the stream.
     25 using CJBig2_CacheKey = std::pair<FX_DWORD, FX_DWORD>;
     26 // NB: CJBig2_SymbolDict* is owned.
     27 using CJBig2_CachePair = std::pair<CJBig2_CacheKey, CJBig2_SymbolDict*>;
     28 
     29 #define JBIG2_SUCCESS 0
     30 #define JBIG2_FAILED -1
     31 #define JBIG2_ERROR_TOO_SHORT -2
     32 #define JBIG2_ERROR_FATAL -3
     33 #define JBIG2_END_OF_PAGE 2
     34 #define JBIG2_END_OF_FILE 3
     35 #define JBIG2_ERROR_FILE_FORMAT -4
     36 #define JBIG2_ERROR_STREAM_TYPE -5
     37 #define JBIG2_ERROR_LIMIT -6
     38 #define JBIG2_MIN_SEGMENT_SIZE 11
     39 
     40 class CJBig2_Context {
     41  public:
     42   static CJBig2_Context* CreateContext(
     43       CPDF_StreamAcc* pGlobalStream,
     44       CPDF_StreamAcc* pSrcStream,
     45       std::list<CJBig2_CachePair>* pSymbolDictCache,
     46       IFX_Pause* pPause = NULL);
     47 
     48   static void DestroyContext(CJBig2_Context* pContext);
     49 
     50   int32_t getFirstPage(uint8_t* pBuf,
     51                        int32_t width,
     52                        int32_t height,
     53                        int32_t stride,
     54                        IFX_Pause* pPause);
     55 
     56   int32_t Continue(IFX_Pause* pPause);
     57   FXCODEC_STATUS GetProcessingStatus() { return m_ProcessingStatus; }
     58 
     59  private:
     60   CJBig2_Context(CPDF_StreamAcc* pGlobalStream,
     61                  CPDF_StreamAcc* pSrcStream,
     62                  std::list<CJBig2_CachePair>* pSymbolDictCache,
     63                  IFX_Pause* pPause,
     64                  bool bIsGlobal);
     65 
     66   ~CJBig2_Context();
     67 
     68   int32_t decode_SquentialOrgnazation(IFX_Pause* pPause);
     69 
     70   int32_t decode_EmbedOrgnazation(IFX_Pause* pPause);
     71 
     72   int32_t decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
     73 
     74   int32_t decode_RandomOrgnazation(IFX_Pause* pPause);
     75 
     76   CJBig2_Segment* findSegmentByNumber(FX_DWORD dwNumber);
     77 
     78   CJBig2_Segment* findReferredSegmentByTypeAndIndex(CJBig2_Segment* pSegment,
     79                                                     uint8_t cType,
     80                                                     int32_t nIndex);
     81 
     82   int32_t parseSegmentHeader(CJBig2_Segment* pSegment);
     83 
     84   int32_t parseSegmentData(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     85   int32_t ProcessingParseSegmentData(CJBig2_Segment* pSegment,
     86                                      IFX_Pause* pPause);
     87 
     88   int32_t parseSymbolDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     89 
     90   int32_t parseTextRegion(CJBig2_Segment* pSegment);
     91 
     92   int32_t parsePatternDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     93 
     94   int32_t parseHalftoneRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     95 
     96   int32_t parseGenericRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     97 
     98   int32_t parseGenericRefinementRegion(CJBig2_Segment* pSegment);
     99 
    100   int32_t parseTable(CJBig2_Segment* pSegment);
    101 
    102   int32_t parseRegionInfo(JBig2RegionInfo* pRI);
    103 
    104   JBig2HuffmanCode* decodeSymbolIDHuffmanTable(CJBig2_BitStream* pStream,
    105                                                FX_DWORD SBNUMSYMS);
    106 
    107   void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);
    108 
    109   void huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, int NTEMP);
    110 
    111  private:
    112   CJBig2_Context* m_pGlobalContext;
    113   std::unique_ptr<CJBig2_BitStream> m_pStream;
    114   CJBig2_List<CJBig2_Segment> m_SegmentList;
    115   CJBig2_List<JBig2PageInfo> m_PageInfoList;
    116   std::unique_ptr<CJBig2_Image> m_pPage;
    117   size_t m_nSegmentDecoded;
    118   bool m_bInPage;
    119   bool m_bBufSpecified;
    120   int32_t m_PauseStep;
    121   IFX_Pause* m_pPause;
    122   FXCODEC_STATUS m_ProcessingStatus;
    123   std::unique_ptr<CJBig2_ArithDecoder> m_pArithDecoder;
    124   std::unique_ptr<CJBig2_GRDProc> m_pGRD;
    125   JBig2ArithCtx* m_gbContext;
    126   std::unique_ptr<CJBig2_Segment> m_pSegment;
    127   FX_DWORD m_dwOffset;
    128   JBig2RegionInfo m_ri;
    129   std::list<CJBig2_CachePair>* const m_pSymbolDictCache;
    130   bool m_bIsGlobal;
    131 };
    132 
    133 #endif  // CORE_SRC_FXCODEC_JBIG2_JBIG2_CONTEXT_H_
    134