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_DIBITMAP_H_
8 #define CORE_FXGE_DIB_CFX_DIBITMAP_H_
9 
10 #include "core/fxcrt/fx_coordinates.h"
11 #include "core/fxcrt/fx_memory_wrappers.h"
12 #include "core/fxcrt/maybe_owned.h"
13 #include "core/fxcrt/retain_ptr.h"
14 #include "core/fxge/dib/cfx_dibbase.h"
15 #include "core/fxge/fx_dib.h"
16 
17 class CFX_DIBitmap : public CFX_DIBBase {
18  public:
19   template <typename T, typename... Args>
20   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
21 
22   bool Create(int width, int height, FXDIB_Format format);
23 
24   bool Create(int width,
25               int height,
26               FXDIB_Format format,
27               uint8_t* pBuffer,
28               uint32_t pitch);
29 
30   bool Copy(const RetainPtr<CFX_DIBBase>& pSrc);
31 
32   // CFX_DIBBase
33   uint8_t* GetBuffer() const override;
34   const uint8_t* GetScanline(int line) const override;
35   void DownSampleScanline(int line,
36                           uint8_t* dest_scan,
37                           int dest_bpp,
38                           int dest_width,
39                           bool bFlipX,
40                           int clip_left,
41                           int clip_width) const override;
42 
43   void TakeOver(RetainPtr<CFX_DIBitmap>&& pSrcBitmap);
44   bool ConvertFormat(FXDIB_Format format);
45   void Clear(uint32_t color);
46 
47   uint32_t GetPixel(int x, int y) const;
48   void SetPixel(int x, int y, uint32_t color);
49 
50   bool LoadChannelFromAlpha(FXDIB_Channel destChannel,
51                             const RetainPtr<CFX_DIBBase>& pSrcBitmap);
52   bool LoadChannel(FXDIB_Channel destChannel, int value);
53 
54   bool MultiplyAlpha(int alpha);
55   bool MultiplyAlpha(const RetainPtr<CFX_DIBBase>& pSrcBitmap);
56 
57   bool TransferBitmap(int dest_left,
58                       int dest_top,
59                       int width,
60                       int height,
61                       const RetainPtr<CFX_DIBBase>& pSrcBitmap,
62                       int src_left,
63                       int src_top);
64 
65   bool CompositeBitmap(int dest_left,
66                        int dest_top,
67                        int width,
68                        int height,
69                        const RetainPtr<CFX_DIBBase>& pSrcBitmap,
70                        int src_left,
71                        int src_top,
72                        BlendMode blend_type,
73                        const CFX_ClipRgn* pClipRgn,
74                        bool bRgbByteOrder);
75 
76   bool CompositeMask(int dest_left,
77                      int dest_top,
78                      int width,
79                      int height,
80                      const RetainPtr<CFX_DIBBase>& pMask,
81                      uint32_t color,
82                      int src_left,
83                      int src_top,
84                      BlendMode blend_type,
85                      const CFX_ClipRgn* pClipRgn,
86                      bool bRgbByteOrder);
87 
88   bool CompositeRect(int dest_left,
89                      int dest_top,
90                      int width,
91                      int height,
92                      uint32_t color,
93                      int alpha_flag);
94 
95   bool ConvertColorScale(uint32_t forecolor, uint32_t backcolor);
96 
97   static bool CalculatePitchAndSize(int height,
98                                     int width,
99                                     FXDIB_Format format,
100                                     uint32_t* pitch,
101                                     uint32_t* size);
102 
103 #if defined _SKIA_SUPPORT_ || _SKIA_SUPPORT_PATHS_
104   void PreMultiply();
105 #endif
106 #if defined _SKIA_SUPPORT_PATHS_
107   void UnPreMultiply();
108 #endif
109 
110  protected:
111   CFX_DIBitmap();
112   CFX_DIBitmap(const CFX_DIBitmap& src);
113   ~CFX_DIBitmap() override;
114 
115 #if defined _SKIA_SUPPORT_PATHS_
116   enum class Format { kCleared, kPreMultiplied, kUnPreMultiplied };
117 #endif
118 
119   MaybeOwned<uint8_t, FxFreeDeleter> m_pBuffer;
120 #if defined _SKIA_SUPPORT_PATHS_
121   Format m_nFormat;
122 #endif
123 
124  private:
125   void ConvertBGRColorScale(uint32_t forecolor, uint32_t backcolor);
126   void ConvertCMYKColorScale(uint32_t forecolor, uint32_t backcolor);
127   bool TransferWithUnequalFormats(FXDIB_Format dest_format,
128                                   int dest_left,
129                                   int dest_top,
130                                   int width,
131                                   int height,
132                                   const RetainPtr<CFX_DIBBase>& pSrcBitmap,
133                                   int src_left,
134                                   int src_top);
135   void TransferWithMultipleBPP(int dest_left,
136                                int dest_top,
137                                int width,
138                                int height,
139                                const RetainPtr<CFX_DIBBase>& pSrcBitmap,
140                                int src_left,
141                                int src_top);
142   void TransferEqualFormatsOneBPP(int dest_left,
143                                   int dest_top,
144                                   int width,
145                                   int height,
146                                   const RetainPtr<CFX_DIBBase>& pSrcBitmap,
147                                   int src_left,
148                                   int src_top);
149 };
150 
151 #endif  // CORE_FXGE_DIB_CFX_DIBITMAP_H_
152