xref: /aosp_15_r20/external/pdfium/core/fxge/dib/cfx_dibitmap.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 The PDFium Authors
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_memory_wrappers.h"
11 #include "core/fxcrt/maybe_owned.h"
12 #include "core/fxcrt/retain_ptr.h"
13 #include "core/fxge/dib/cfx_dibbase.h"
14 #include "core/fxge/dib/fx_dib.h"
15 #include "third_party/abseil-cpp/absl/types/optional.h"
16 #include "third_party/base/containers/span.h"
17 
18 class CFX_DIBitmap final : public CFX_DIBBase {
19  public:
20   struct PitchAndSize {
21     uint32_t pitch;
22     uint32_t size;
23   };
24 
25   CONSTRUCT_VIA_MAKE_RETAIN;
26 
27   bool Create(int width, int height, FXDIB_Format format);
28   bool Create(int width,
29               int height,
30               FXDIB_Format format,
31               uint8_t* pBuffer,
32               uint32_t pitch);
33 
34   bool Copy(const RetainPtr<CFX_DIBBase>& pSrc);
35 
36   // CFX_DIBBase
37   pdfium::span<const uint8_t> GetBuffer() const override;
38   pdfium::span<const uint8_t> GetScanline(int line) const override;
39   size_t GetEstimatedImageMemoryBurden() const override;
40 
GetWritableBuffer()41   pdfium::span<uint8_t> GetWritableBuffer() {
42     pdfium::span<const uint8_t> src = GetBuffer();
43     return {const_cast<uint8_t*>(src.data()), src.size()};
44   }
45 
GetWritableScanline(int line)46   pdfium::span<uint8_t> GetWritableScanline(int line) {
47     pdfium::span<const uint8_t> src = GetScanline(line);
48     return {const_cast<uint8_t*>(src.data()), src.size()};
49   }
50 
51   void TakeOver(RetainPtr<CFX_DIBitmap>&& pSrcBitmap);
52   bool ConvertFormat(FXDIB_Format format);
53   void Clear(uint32_t color);
54 
55 #if defined(_SKIA_SUPPORT_)
56   uint32_t GetPixel(int x, int y) const;
57   void SetPixel(int x, int y, uint32_t color);
58 #endif  // defined(_SKIA_SUPPORT_)
59 
60   bool SetRedFromBitmap(const RetainPtr<CFX_DIBBase>& pSrcBitmap);
61   bool SetAlphaFromBitmap(const RetainPtr<CFX_DIBBase>& pSrcBitmap);
62   bool SetUniformOpaqueAlpha();
63 
64   // TODO(crbug.com/pdfium/2007): Migrate callers to `CFX_RenderDevice`.
65   bool MultiplyAlpha(int alpha);
66   bool MultiplyAlpha(const RetainPtr<CFX_DIBBase>& pSrcBitmap);
67 
68   bool TransferBitmap(int dest_left,
69                       int dest_top,
70                       int width,
71                       int height,
72                       const RetainPtr<CFX_DIBBase>& pSrcBitmap,
73                       int src_left,
74                       int src_top);
75 
76   bool CompositeBitmap(int dest_left,
77                        int dest_top,
78                        int width,
79                        int height,
80                        const RetainPtr<CFX_DIBBase>& pSrcBitmap,
81                        int src_left,
82                        int src_top,
83                        BlendMode blend_type,
84                        const CFX_ClipRgn* pClipRgn,
85                        bool bRgbByteOrder);
86 
87   bool CompositeMask(int dest_left,
88                      int dest_top,
89                      int width,
90                      int height,
91                      const RetainPtr<CFX_DIBBase>& pMask,
92                      uint32_t color,
93                      int src_left,
94                      int src_top,
95                      BlendMode blend_type,
96                      const CFX_ClipRgn* pClipRgn,
97                      bool bRgbByteOrder);
98 
99   void CompositeOneBPPMask(int dest_left,
100                            int dest_top,
101                            int width,
102                            int height,
103                            const RetainPtr<CFX_DIBBase>& pSrcBitmap,
104                            int src_left,
105                            int src_top);
106 
107   bool CompositeRect(int dest_left,
108                      int dest_top,
109                      int width,
110                      int height,
111                      uint32_t color);
112 
113   bool ConvertColorScale(uint32_t forecolor, uint32_t backcolor);
114 
115   // |width| and |height| must be greater than 0.
116   // |format| must have a valid bits per pixel count.
117   // If |pitch| is zero, then the actual pitch will be calculated based on
118   // |width| and |format|.
119   // If |pitch| is non-zero, then that be used as the actual pitch.
120   // The actual pitch will be used to calculate the size.
121   // Returns the calculated pitch and size on success, or nullopt on failure.
122   static absl::optional<PitchAndSize> CalculatePitchAndSize(int width,
123                                                             int height,
124                                                             FXDIB_Format format,
125                                                             uint32_t pitch);
126 
127 #if defined(_SKIA_SUPPORT_)
128   // Converts to un-pre-multiplied alpha if necessary.
129   void UnPreMultiply();
130 
131   // Forces pre-multiplied alpha without conversion.
132   // TODO(crbug.com/pdfium/2011): Remove the need for this.
133   void ForcePreMultiply();
134 #endif
135 
136  protected:
137 #if defined(_SKIA_SUPPORT_)
138   bool IsPremultiplied() const override;
139 #endif  // defined(_SKIA_SUPPORT_)
140 
141  private:
142   enum class Channel : uint8_t { kRed, kAlpha };
143 
144 #if defined(_SKIA_SUPPORT_)
145   enum class Format { kCleared, kPreMultiplied, kUnPreMultiplied };
146 #endif
147 
148   CFX_DIBitmap();
149   CFX_DIBitmap(const CFX_DIBitmap& src);
150   ~CFX_DIBitmap() override;
151 
152   bool SetChannelFromBitmap(Channel destChannel,
153                             const RetainPtr<CFX_DIBBase>& pSrcBitmap);
154   void ConvertBGRColorScale(uint32_t forecolor, uint32_t backcolor);
155   bool TransferWithUnequalFormats(FXDIB_Format dest_format,
156                                   int dest_left,
157                                   int dest_top,
158                                   int width,
159                                   int height,
160                                   const RetainPtr<CFX_DIBBase>& pSrcBitmap,
161                                   int src_left,
162                                   int src_top);
163   void TransferWithMultipleBPP(int dest_left,
164                                int dest_top,
165                                int width,
166                                int height,
167                                const RetainPtr<CFX_DIBBase>& pSrcBitmap,
168                                int src_left,
169                                int src_top);
170   void TransferEqualFormatsOneBPP(int dest_left,
171                                   int dest_top,
172                                   int width,
173                                   int height,
174                                   const RetainPtr<CFX_DIBBase>& pSrcBitmap,
175                                   int src_left,
176                                   int src_top);
177 
178   MaybeOwned<uint8_t, FxFreeDeleter> m_pBuffer;
179 #if defined(_SKIA_SUPPORT_)
180   Format m_nFormat = Format::kCleared;
181 #endif
182 };
183 
184 #endif  // CORE_FXGE_DIB_CFX_DIBITMAP_H_
185