• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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_FPDFAPI_PAGE_CPDF_DIB_H_
8 #define CORE_FPDFAPI_PAGE_CPDF_DIB_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fpdfapi/page/cpdf_clippath.h"
14 #include "core/fpdfapi/page/cpdf_colorspace.h"
15 #include "core/fpdfapi/page/cpdf_graphicstates.h"
16 #include "core/fxcrt/fx_memory_wrappers.h"
17 #include "core/fxcrt/retain_ptr.h"
18 #include "core/fxcrt/unowned_ptr.h"
19 #include "core/fxge/dib/cfx_dibbase.h"
20 #include "third_party/base/span.h"
21 
22 class CPDF_Dictionary;
23 class CPDF_Document;
24 class CPDF_Stream;
25 class CPDF_StreamAcc;
26 
27 struct DIB_COMP_DATA {
28   float m_DecodeMin;
29   float m_DecodeStep;
30   int m_ColorKeyMin;
31   int m_ColorKeyMax;
32 };
33 
34 namespace fxcodec {
35 class Jbig2Context;
36 class ScanlineDecoder;
37 }  // namespace fxcodec
38 
39 constexpr size_t kHugeImageSize = 60000000;
40 
41 class CPDF_DIB final : public CFX_DIBBase {
42  public:
43   enum class LoadState : uint8_t { kFail, kSuccess, kContinue };
44 
45   template <typename T, typename... Args>
46   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
47 
48   bool Load(CPDF_Document* pDoc, const CPDF_Stream* pStream);
49 
50   // CFX_DIBBase:
51   bool SkipToScanline(int line, PauseIndicatorIface* pPause) const override;
52   uint8_t* GetBuffer() const override;
53   const uint8_t* GetScanline(int line) const override;
54   void DownSampleScanline(int line,
55                           uint8_t* dest_scan,
56                           int dest_bpp,
57                           int dest_width,
58                           bool bFlipX,
59                           int clip_left,
60                           int clip_width) const override;
61 
GetColorSpace()62   RetainPtr<CPDF_ColorSpace> GetColorSpace() const { return m_pColorSpace; }
GetMatteColor()63   uint32_t GetMatteColor() const { return m_MatteColor; }
64 
65   LoadState StartLoadDIBBase(CPDF_Document* pDoc,
66                              const CPDF_Stream* pStream,
67                              bool bHasMask,
68                              const CPDF_Dictionary* pFormResources,
69                              CPDF_Dictionary* pPageResources,
70                              bool bStdCS,
71                              uint32_t GroupFamily,
72                              bool bLoadMask);
73   LoadState ContinueLoadDIBBase(PauseIndicatorIface* pPause);
74   RetainPtr<CPDF_DIB> DetachMask();
75 
76   bool IsJBigImage() const;
77 
78  private:
79   CPDF_DIB();
80   ~CPDF_DIB() override;
81 
82   LoadState StartLoadMask();
83   LoadState StartLoadMaskDIB(RetainPtr<const CPDF_Stream> mask);
84   bool ContinueToLoadMask();
85   LoadState ContinueLoadMaskDIB(PauseIndicatorIface* pPause);
86   bool LoadColorInfo(const CPDF_Dictionary* pFormResources,
87                      const CPDF_Dictionary* pPageResources);
88   bool GetDecodeAndMaskArray(bool* bDefaultDecode, bool* bColorKey);
89   RetainPtr<CFX_DIBitmap> LoadJpxBitmap();
90   void LoadPalette();
91   LoadState CreateDecoder();
92   bool CreateDCTDecoder(pdfium::span<const uint8_t> src_span,
93                         const CPDF_Dictionary* pParams);
94   void TranslateScanline24bpp(uint8_t* dest_scan,
95                               const uint8_t* src_scan) const;
96   bool TranslateScanline24bppDefaultDecode(uint8_t* dest_scan,
97                                            const uint8_t* src_scan) const;
98   void ValidateDictParam();
99   void DownSampleScanline1Bit(int orig_Bpp,
100                               int dest_Bpp,
101                               uint32_t src_width,
102                               const uint8_t* pSrcLine,
103                               uint8_t* dest_scan,
104                               int dest_width,
105                               bool bFlipX,
106                               int clip_left,
107                               int clip_width) const;
108   void DownSampleScanline8Bit(int orig_Bpp,
109                               int dest_Bpp,
110                               uint32_t src_width,
111                               const uint8_t* pSrcLine,
112                               uint8_t* dest_scan,
113                               int dest_width,
114                               bool bFlipX,
115                               int clip_left,
116                               int clip_width) const;
117   void DownSampleScanline32Bit(int orig_Bpp,
118                                int dest_Bpp,
119                                uint32_t src_width,
120                                const uint8_t* pSrcLine,
121                                uint8_t* dest_scan,
122                                int dest_width,
123                                bool bFlipX,
124                                int clip_left,
125                                int clip_width) const;
126   bool TransMask() const;
127   void SetMaskProperties();
128 
129   UnownedPtr<CPDF_Document> m_pDocument;
130   RetainPtr<const CPDF_Stream> m_pStream;
131   RetainPtr<const CPDF_Dictionary> m_pDict;
132   RetainPtr<CPDF_StreamAcc> m_pStreamAcc;
133   RetainPtr<CPDF_ColorSpace> m_pColorSpace;
134   uint32_t m_Family = 0;
135   uint32_t m_bpc = 0;
136   uint32_t m_bpc_orig = 0;
137   uint32_t m_nComponents = 0;
138   uint32_t m_GroupFamily = 0;
139   uint32_t m_MatteColor = 0;
140   LoadState m_Status = LoadState::kFail;
141   bool m_bLoadMask = false;
142   bool m_bDefaultDecode = true;
143   bool m_bImageMask = false;
144   bool m_bDoBpcCheck = true;
145   bool m_bColorKey = false;
146   bool m_bHasMask = false;
147   bool m_bStdCS = false;
148   std::vector<DIB_COMP_DATA> m_CompData;
149   std::unique_ptr<uint8_t, FxFreeDeleter> m_pLineBuf;
150   std::unique_ptr<uint8_t, FxFreeDeleter> m_pMaskedLine;
151   RetainPtr<CFX_DIBitmap> m_pCachedBitmap;
152   // Note: Must not create a cycle between CPDF_DIB instances.
153   RetainPtr<CPDF_DIB> m_pMask;
154   RetainPtr<CPDF_StreamAcc> m_pGlobalAcc;
155   std::unique_ptr<fxcodec::ScanlineDecoder> m_pDecoder;
156 
157   // Must come after |m_pCachedBitmap|.
158   std::unique_ptr<fxcodec::Jbig2Context> m_pJbig2Context;
159 };
160 
161 #endif  // CORE_FPDFAPI_PAGE_CPDF_DIB_H_
162