• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 class CFX_DIBitmap final : public CFX_DIBBase {
18  public:
19   struct PitchAndSize {
20     uint32_t pitch;
21     uint32_t size;
22   };
23 
24   CONSTRUCT_VIA_MAKE_RETAIN;
25 
26   bool Create(int width, int height, FXDIB_Format format);
27   bool Create(int width,
28               int height,
29               FXDIB_Format format,
30               uint8_t* pBuffer,
31               uint32_t pitch);
32 
33   bool Copy(const RetainPtr<CFX_DIBBase>& pSrc);
34 
35   // CFX_DIBBase
36   pdfium::span<uint8_t> GetBuffer() const override;
37   pdfium::span<const uint8_t> GetScanline(int line) const override;
38   size_t GetEstimatedImageMemoryBurden() const override;
39 
40   void TakeOver(RetainPtr<CFX_DIBitmap>&& pSrcBitmap);
41   bool ConvertFormat(FXDIB_Format format);
42   void Clear(uint32_t color);
43 
44 #if defined(_SKIA_SUPPORT_)
45   uint32_t GetPixel(int x, int y) const;
46   void SetPixel(int x, int y, uint32_t color);
47 #endif  // defined(_SKIA_SUPPORT_)
48 
49   bool SetRedFromBitmap(const RetainPtr<CFX_DIBBase>& pSrcBitmap);
50   bool SetAlphaFromBitmap(const RetainPtr<CFX_DIBBase>& pSrcBitmap);
51   bool SetUniformOpaqueAlpha();
52 
53   // TODO(crbug.com/pdfium/2007): Migrate callers to `CFX_RenderDevice`.
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   void CompositeOneBPPMask(int dest_left,
89                            int dest_top,
90                            int width,
91                            int height,
92                            const RetainPtr<CFX_DIBBase>& pSrcBitmap,
93                            int src_left,
94                            int src_top);
95 
96   bool CompositeRect(int dest_left,
97                      int dest_top,
98                      int width,
99                      int height,
100                      uint32_t color);
101 
102   bool ConvertColorScale(uint32_t forecolor, uint32_t backcolor);
103 
104   // |width| and |height| must be greater than 0.
105   // |format| must have a valid bits per pixel count.
106   // If |pitch| is zero, then the actual pitch will be calculated based on
107   // |width| and |format|.
108   // If |pitch| is non-zero, then that be used as the actual pitch.
109   // The actual pitch will be used to calculate the size.
110   // Returns the calculated pitch and size on success, or nullopt on failure.
111   static absl::optional<PitchAndSize> CalculatePitchAndSize(int width,
112                                                             int height,
113                                                             FXDIB_Format format,
114                                                             uint32_t pitch);
115 
116 #if defined(_SKIA_SUPPORT_)
117   // Converts to pre-multiplied alpha if necessary.
118   void PreMultiply();
119 
120   // Converts to un-pre-multiplied alpha if necessary.
121   void UnPreMultiply();
122 
123   // Forces pre-multiplied alpha without conversion.
124   // TODO(crbug.com/pdfium/2011): Remove the need for this.
125   void ForcePreMultiply();
126 
127   // Forces un-pre-multiplied alpha without conversion.
128   // TODO(crbug.com/pdfium/2011): Remove the need for this.
129   void ForceUnPreMultiply();
130 #endif
131 
132  private:
133   enum class Channel : uint8_t { kRed, kAlpha };
134 
135 #if defined(_SKIA_SUPPORT_)
136   enum class Format { kCleared, kPreMultiplied, kUnPreMultiplied };
137 #endif
138 
139   CFX_DIBitmap();
140   CFX_DIBitmap(const CFX_DIBitmap& src);
141   ~CFX_DIBitmap() override;
142 
143   bool SetChannelFromBitmap(Channel destChannel,
144                             const RetainPtr<CFX_DIBBase>& pSrcBitmap);
145   void ConvertBGRColorScale(uint32_t forecolor, uint32_t backcolor);
146   bool TransferWithUnequalFormats(FXDIB_Format dest_format,
147                                   int dest_left,
148                                   int dest_top,
149                                   int width,
150                                   int height,
151                                   const RetainPtr<CFX_DIBBase>& pSrcBitmap,
152                                   int src_left,
153                                   int src_top);
154   void TransferWithMultipleBPP(int dest_left,
155                                int dest_top,
156                                int width,
157                                int height,
158                                const RetainPtr<CFX_DIBBase>& pSrcBitmap,
159                                int src_left,
160                                int src_top);
161   void TransferEqualFormatsOneBPP(int dest_left,
162                                   int dest_top,
163                                   int width,
164                                   int height,
165                                   const RetainPtr<CFX_DIBBase>& pSrcBitmap,
166                                   int src_left,
167                                   int src_top);
168 
169   MaybeOwned<uint8_t, FxFreeDeleter> m_pBuffer;
170 #if defined(_SKIA_SUPPORT_)
171   Format m_nFormat = Format::kCleared;
172 #endif
173 };
174 
175 #endif  // CORE_FXGE_DIB_CFX_DIBITMAP_H_
176