xref: /aosp_15_r20/external/pdfium/core/fxge/dib/cfx_dibbase.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_DIBBASE_H_
8 #define CORE_FXGE_DIB_CFX_DIBBASE_H_
9 
10 #include <stdint.h>
11 
12 #include "core/fxcrt/data_vector.h"
13 #include "core/fxcrt/retain_ptr.h"
14 #include "core/fxge/dib/fx_dib.h"
15 #include "third_party/base/containers/span.h"
16 
17 #if defined(_SKIA_SUPPORT_)
18 #include "third_party/skia/include/core/SkRefCnt.h"  // nogncheck
19 #endif
20 
21 class CFX_ClipRgn;
22 class CFX_DIBitmap;
23 class CFX_Matrix;
24 class PauseIndicatorIface;
25 struct FX_RECT;
26 
27 #if defined(_SKIA_SUPPORT_)
28 class SkImage;
29 #endif  // defined(_SKIA_SUPPORT_)
30 
31 // Base class for all Device-Independent Bitmaps.
32 class CFX_DIBBase : public Retainable {
33  public:
34 #if BUILDFLAG(IS_APPLE)
35   // Matches Apple's kCGBitmapByteOrder32Little in fx_quartz_device.cpp.
36   static constexpr FXDIB_Format kPlatformRGBFormat = FXDIB_Format::kRgb32;
37 #else   // BUILDFLAG(IS_APPLE)
38   static constexpr FXDIB_Format kPlatformRGBFormat = FXDIB_Format::kRgb;
39 #endif  // BUILDFLAG(IS_APPLE)
40 
41   static constexpr uint32_t kPaletteSize = 256;
42 
43   virtual pdfium::span<const uint8_t> GetBuffer() const;
44   virtual pdfium::span<const uint8_t> GetScanline(int line) const = 0;
45   virtual bool SkipToScanline(int line, PauseIndicatorIface* pPause) const;
46   virtual size_t GetEstimatedImageMemoryBurden() const;
47 
GetWidth()48   int GetWidth() const { return m_Width; }
GetHeight()49   int GetHeight() const { return m_Height; }
GetPitch()50   uint32_t GetPitch() const { return m_Pitch; }
51 
GetFormat()52   FXDIB_Format GetFormat() const { return m_Format; }
GetBPP()53   int GetBPP() const { return GetBppFromFormat(m_Format); }
IsMaskFormat()54   bool IsMaskFormat() const { return GetIsMaskFromFormat(m_Format); }
IsAlphaFormat()55   bool IsAlphaFormat() const { return m_Format == FXDIB_Format::kArgb; }
IsOpaqueImage()56   bool IsOpaqueImage() const { return !IsMaskFormat() && !IsAlphaFormat(); }
57 
HasPalette()58   bool HasPalette() const { return !m_palette.empty(); }
GetPaletteSpan()59   pdfium::span<const uint32_t> GetPaletteSpan() const { return m_palette; }
60   size_t GetRequiredPaletteSize() const;
61   uint32_t GetPaletteArgb(int index) const;
62   void SetPaletteArgb(int index, uint32_t color);
63 
64   // Copies into internally-owned palette.
65   void SetPalette(pdfium::span<const uint32_t> src_palette);
66 
67   RetainPtr<CFX_DIBitmap> Realize() const;
68   RetainPtr<CFX_DIBitmap> ClipTo(const FX_RECT& rect) const;
69   RetainPtr<CFX_DIBitmap> ConvertTo(FXDIB_Format format) const;
70   RetainPtr<CFX_DIBitmap> StretchTo(int dest_width,
71                                     int dest_height,
72                                     const FXDIB_ResampleOptions& options,
73                                     const FX_RECT* pClip) const;
74   RetainPtr<CFX_DIBitmap> TransformTo(const CFX_Matrix& mtDest,
75                                       int* left,
76                                       int* top) const;
77   RetainPtr<CFX_DIBitmap> SwapXY(bool bXFlip, bool bYFlip) const;
78   RetainPtr<CFX_DIBitmap> FlipImage(bool bXFlip, bool bYFlip) const;
79 
80   RetainPtr<CFX_DIBitmap> CloneAlphaMask() const;
81 
82   bool GetOverlapRect(int& dest_left,
83                       int& dest_top,
84                       int& width,
85                       int& height,
86                       int src_width,
87                       int src_height,
88                       int& src_left,
89                       int& src_top,
90                       const CFX_ClipRgn* pClipRgn) const;
91 
92 #if defined(_SKIA_SUPPORT_)
93   // Realizes an `SkImage` from this DIB.
94   //
95   // This may share the underlying pixels, in which case, this DIB should not be
96   // modified during the lifetime of the `SkImage`.
97   virtual sk_sp<SkImage> RealizeSkImage() const;
98 #endif  // defined(_SKIA_SUPPORT_)
99 
100  protected:
101   CFX_DIBBase();
102   ~CFX_DIBBase() override;
103 
104   static bool ConvertBuffer(FXDIB_Format dest_format,
105                             pdfium::span<uint8_t> dest_buf,
106                             int dest_pitch,
107                             int width,
108                             int height,
109                             const RetainPtr<const CFX_DIBBase>& pSrcBitmap,
110                             int src_left,
111                             int src_top,
112                             DataVector<uint32_t>* pal);
113 
114 #if defined(_SKIA_SUPPORT_)
115   // Whether alpha is premultiplied (if `IsAlphaFormat()`).
116   virtual bool IsPremultiplied() const;
117 #endif  // defined(_SKIA_SUPPORT_)
118 
119   RetainPtr<CFX_DIBitmap> ClipToInternal(const FX_RECT* pClip) const;
120   void BuildPalette();
121   int FindPalette(uint32_t color) const;
122 
123   FXDIB_Format m_Format = FXDIB_Format::kInvalid;
124   int m_Width = 0;
125   int m_Height = 0;
126   uint32_t m_Pitch = 0;
127   DataVector<uint32_t> m_palette;
128 };
129 
130 #endif  // CORE_FXGE_DIB_CFX_DIBBASE_H_
131