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/span.h" 16 17 class CFX_ClipRgn; 18 class CFX_DIBitmap; 19 class CFX_Matrix; 20 class PauseIndicatorIface; 21 struct FX_RECT; 22 23 // Base class for all Device-Independent Bitmaps. 24 class CFX_DIBBase : public Retainable { 25 public: 26 #if BUILDFLAG(IS_APPLE) 27 // Matches Apple's kCGBitmapByteOrder32Little in fx_quartz_device.cpp. 28 static constexpr FXDIB_Format kPlatformRGBFormat = FXDIB_Format::kRgb32; 29 #else // BUILDFLAG(IS_APPLE) 30 static constexpr FXDIB_Format kPlatformRGBFormat = FXDIB_Format::kRgb; 31 #endif // BUILDFLAG(IS_APPLE) 32 33 static constexpr uint32_t kPaletteSize = 256; 34 35 ~CFX_DIBBase() override; 36 37 virtual pdfium::span<uint8_t> GetBuffer() const; 38 virtual pdfium::span<const uint8_t> GetScanline(int line) const = 0; 39 virtual bool SkipToScanline(int line, PauseIndicatorIface* pPause) const; 40 virtual size_t GetEstimatedImageMemoryBurden() const; 41 GetWritableScanline(int line)42 pdfium::span<uint8_t> GetWritableScanline(int line) { 43 pdfium::span<const uint8_t> src = GetScanline(line); 44 return {const_cast<uint8_t*>(src.data()), src.size()}; 45 } GetWidth()46 int GetWidth() const { return m_Width; } GetHeight()47 int GetHeight() const { return m_Height; } GetPitch()48 uint32_t GetPitch() const { return m_Pitch; } 49 GetFormat()50 FXDIB_Format GetFormat() const { return m_Format; } GetBPP()51 int GetBPP() const { return GetBppFromFormat(m_Format); } IsMaskFormat()52 bool IsMaskFormat() const { return GetIsMaskFromFormat(m_Format); } IsAlphaFormat()53 bool IsAlphaFormat() const { return m_Format == FXDIB_Format::kArgb; } IsOpaqueImage()54 bool IsOpaqueImage() const { return !IsMaskFormat() && !IsAlphaFormat(); } 55 HasPalette()56 bool HasPalette() const { return !m_palette.empty(); } GetPaletteSpan()57 pdfium::span<const uint32_t> GetPaletteSpan() const { return m_palette; } 58 size_t GetRequiredPaletteSize() const; 59 uint32_t GetPaletteArgb(int index) const; 60 void SetPaletteArgb(int index, uint32_t color); 61 62 // Copies into internally-owned palette. 63 void SetPalette(pdfium::span<const uint32_t> src_palette); 64 65 RetainPtr<CFX_DIBitmap> Realize() const; 66 RetainPtr<CFX_DIBitmap> ClipTo(const FX_RECT& rect) const; 67 RetainPtr<CFX_DIBitmap> ConvertTo(FXDIB_Format format) const; 68 RetainPtr<CFX_DIBitmap> StretchTo(int dest_width, 69 int dest_height, 70 const FXDIB_ResampleOptions& options, 71 const FX_RECT* pClip) const; 72 RetainPtr<CFX_DIBitmap> TransformTo(const CFX_Matrix& mtDest, 73 int* left, 74 int* top) const; 75 RetainPtr<CFX_DIBitmap> SwapXY(bool bXFlip, bool bYFlip) const; 76 RetainPtr<CFX_DIBitmap> FlipImage(bool bXFlip, bool bYFlip) const; 77 78 RetainPtr<CFX_DIBitmap> CloneAlphaMask() const; 79 80 bool GetOverlapRect(int& dest_left, 81 int& dest_top, 82 int& width, 83 int& height, 84 int src_width, 85 int src_height, 86 int& src_left, 87 int& src_top, 88 const CFX_ClipRgn* pClipRgn) const; 89 90 protected: 91 CFX_DIBBase(); 92 93 static bool ConvertBuffer(FXDIB_Format dest_format, 94 pdfium::span<uint8_t> dest_buf, 95 int dest_pitch, 96 int width, 97 int height, 98 const RetainPtr<const CFX_DIBBase>& pSrcBitmap, 99 int src_left, 100 int src_top, 101 DataVector<uint32_t>* pal); 102 103 RetainPtr<CFX_DIBitmap> ClipToInternal(const FX_RECT* pClip) const; 104 void BuildPalette(); 105 int FindPalette(uint32_t color) const; 106 107 FXDIB_Format m_Format = FXDIB_Format::kInvalid; 108 int m_Width = 0; 109 int m_Height = 0; 110 uint32_t m_Pitch = 0; 111 DataVector<uint32_t> m_palette; 112 }; 113 114 #endif // CORE_FXGE_DIB_CFX_DIBBASE_H_ 115