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_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 
     20 class CJBig2_ArithDecoder;
     21 class CJBig2_GRDProc;
     22 class CPDF_StreamAcc;
     23 class IFX_Pause;
     24 
     25 // Cache is keyed by the ObjNum of a stream and an index within the stream.
     26 using CJBig2_CacheKey = std::pair<uint32_t, uint32_t>;
     27 using CJBig2_CachePair =
     28     std::pair<CJBig2_CacheKey, std::unique_ptr<CJBig2_SymbolDict>>;
     29 
     30 #define JBIG2_SUCCESS 0
     31 #define JBIG2_FAILED -1
     32 #define JBIG2_ERROR_TOO_SHORT -2
     33 #define JBIG2_ERROR_FATAL -3
     34 #define JBIG2_END_OF_PAGE 2
     35 #define JBIG2_END_OF_FILE 3
     36 #define JBIG2_ERROR_FILE_FORMAT -4
     37 #define JBIG2_ERROR_STREAM_TYPE -5
     38 #define JBIG2_ERROR_LIMIT -6
     39 #define JBIG2_MIN_SEGMENT_SIZE 11
     40 
     41 class CJBig2_Context {
     42  public:
     43   CJBig2_Context(CPDF_StreamAcc* pGlobalStream,
     44                  CPDF_StreamAcc* pSrcStream,
     45                  std::list<CJBig2_CachePair>* pSymbolDictCache,
     46                  IFX_Pause* pPause,
     47                  bool bIsGlobal);
     48   ~CJBig2_Context();
     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   int32_t decode_SquentialOrgnazation(IFX_Pause* pPause);
     61   int32_t decode_EmbedOrgnazation(IFX_Pause* pPause);
     62   int32_t decode_RandomOrgnazation_FirstPage(IFX_Pause* pPause);
     63   int32_t decode_RandomOrgnazation(IFX_Pause* pPause);
     64 
     65   CJBig2_Segment* findSegmentByNumber(uint32_t dwNumber);
     66   CJBig2_Segment* findReferredSegmentByTypeAndIndex(CJBig2_Segment* pSegment,
     67                                                     uint8_t cType,
     68                                                     int32_t nIndex);
     69 
     70   int32_t parseSegmentHeader(CJBig2_Segment* pSegment);
     71   int32_t parseSegmentData(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     72   int32_t ProcessingParseSegmentData(CJBig2_Segment* pSegment,
     73                                      IFX_Pause* pPause);
     74   int32_t parseSymbolDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     75   int32_t parseTextRegion(CJBig2_Segment* pSegment);
     76   int32_t parsePatternDict(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     77   int32_t parseHalftoneRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     78   int32_t parseGenericRegion(CJBig2_Segment* pSegment, IFX_Pause* pPause);
     79   int32_t parseGenericRefinementRegion(CJBig2_Segment* pSegment);
     80   int32_t parseTable(CJBig2_Segment* pSegment);
     81   int32_t parseRegionInfo(JBig2RegionInfo* pRI);
     82 
     83   JBig2HuffmanCode* decodeSymbolIDHuffmanTable(CJBig2_BitStream* pStream,
     84                                                uint32_t SBNUMSYMS);
     85 
     86   void huffman_assign_code(int* CODES, int* PREFLEN, int NTEMP);
     87   void huffman_assign_code(JBig2HuffmanCode* SBSYMCODES, int NTEMP);
     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   size_t m_nSegmentDecoded;
     95   bool m_bInPage;
     96   bool m_bBufSpecified;
     97   int32_t m_PauseStep;
     98   IFX_Pause* const m_pPause;
     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   uint32_t 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