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_FXGE_DIB_CFX_DIBBASE_H_
8 #define CORE_FXGE_DIB_CFX_DIBBASE_H_
9 
10 #include <memory>
11 
12 #include "core/fxcrt/fx_coordinates.h"
13 #include "core/fxcrt/fx_memory_wrappers.h"
14 #include "core/fxcrt/retain_ptr.h"
15 #include "core/fxge/fx_dib.h"
16 
17 enum FXDIB_Channel {
18   FXDIB_Red = 1,
19   FXDIB_Green,
20   FXDIB_Blue,
21   FXDIB_Cyan,
22   FXDIB_Magenta,
23   FXDIB_Yellow,
24   FXDIB_Black,
25   FXDIB_Alpha
26 };
27 
28 class CFX_ClipRgn;
29 class CFX_DIBitmap;
30 class PauseIndicatorIface;
31 
32 // Base class for all Device-Independent Bitmaps.
33 class CFX_DIBBase : public Retainable {
34  public:
35   ~CFX_DIBBase() override;
36 
37   virtual uint8_t* GetBuffer() const;
38   virtual const uint8_t* GetScanline(int line) const = 0;
39   virtual bool SkipToScanline(int line, PauseIndicatorIface* pPause) const;
40   virtual void DownSampleScanline(int line,
41                                   uint8_t* dest_scan,
42                                   int dest_bpp,
43                                   int dest_width,
44                                   bool bFlipX,
45                                   int clip_left,
46                                   int clip_width) const = 0;
47 
GetWritableScanline(int line)48   uint8_t* GetWritableScanline(int line) {
49     return const_cast<uint8_t*>(GetScanline(line));
50   }
GetWidth()51   int GetWidth() const { return m_Width; }
GetHeight()52   int GetHeight() const { return m_Height; }
53 
GetFormat()54   FXDIB_Format GetFormat() const {
55     return static_cast<FXDIB_Format>(m_AlphaFlag * 0x100 + m_bpp);
56   }
GetPitch()57   uint32_t GetPitch() const { return m_Pitch; }
GetPalette()58   uint32_t* GetPalette() const { return m_pPalette.get(); }
GetBPP()59   int GetBPP() const { return m_bpp; }
60 
IsAlphaMask()61   bool IsAlphaMask() const { return !!(m_AlphaFlag & 1); }
HasAlpha()62   bool HasAlpha() const { return !!(m_AlphaFlag & 2); }
IsCmykImage()63   bool IsCmykImage() const { return !!(m_AlphaFlag & 4); }
IsOpaqueImage()64   bool IsOpaqueImage() const { return !IsAlphaMask() && !HasAlpha(); }
65 
66   size_t GetPaletteSize() const;
67 
68   uint32_t GetPaletteArgb(int index) const;
69   void SetPaletteArgb(int index, uint32_t color);
70 
71   // Copies into internally-owned palette.
72   void SetPalette(const uint32_t* pSrcPal);
73 
74   RetainPtr<CFX_DIBitmap> Clone(const FX_RECT* pClip) const;
75   RetainPtr<CFX_DIBitmap> CloneConvert(FXDIB_Format format);
76   RetainPtr<CFX_DIBitmap> StretchTo(int dest_width,
77                                     int dest_height,
78                                     const FXDIB_ResampleOptions& options,
79                                     const FX_RECT* pClip);
80   RetainPtr<CFX_DIBitmap> TransformTo(const CFX_Matrix& mtDest,
81                                       int* left,
82                                       int* top);
83   RetainPtr<CFX_DIBitmap> SwapXY(bool bXFlip, bool bYFlip) const;
84   RetainPtr<CFX_DIBitmap> FlipImage(bool bXFlip, bool bYFlip) const;
85 
86   RetainPtr<CFX_DIBitmap> CloneAlphaMask() const;
87 
88   // Copies into internally-owned mask.
89   bool SetAlphaMask(const RetainPtr<CFX_DIBBase>& pAlphaMask,
90                     const FX_RECT* pClip);
91 
92   bool GetOverlapRect(int& dest_left,
93                       int& dest_top,
94                       int& width,
95                       int& height,
96                       int src_width,
97                       int src_height,
98                       int& src_left,
99                       int& src_top,
100                       const CFX_ClipRgn* pClipRgn);
101 
102 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
103   void DebugVerifyBitmapIsPreMultiplied(void* buffer) const;
104 #endif
105 
106   RetainPtr<CFX_DIBitmap> m_pAlphaMask;
107 
108  protected:
109   CFX_DIBBase();
110 
111   static bool ConvertBuffer(FXDIB_Format dest_format,
112                             uint8_t* dest_buf,
113                             int dest_pitch,
114                             int width,
115                             int height,
116                             const RetainPtr<CFX_DIBBase>& pSrcBitmap,
117                             int src_left,
118                             int src_top,
119                             std::unique_ptr<uint32_t, FxFreeDeleter>* pal);
120 
121   void BuildPalette();
122   bool BuildAlphaMask();
123   int FindPalette(uint32_t color) const;
124   void GetPalette(uint32_t* pal, int alpha) const;
125 
126   int m_Width;
127   int m_Height;
128   int m_bpp;
129   uint32_t m_AlphaFlag;
130   uint32_t m_Pitch;
131   // TODO(weili): Use std::vector for this.
132   std::unique_ptr<uint32_t, FxFreeDeleter> m_pPalette;
133 };
134 
135 #endif  // CORE_FXGE_DIB_CFX_DIBBASE_H_
136