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 <stddef.h> 11 #include <stdint.h> 12 13 #include "build/build_config.h" 14 #include "core/fxcrt/data_vector.h" 15 #include "core/fxcrt/retain_ptr.h" 16 #include "core/fxcrt/span.h" 17 #include "core/fxcrt/span_util.h" 18 #include "core/fxge/dib/fx_dib.h" 19 20 #if defined(PDF_USE_SKIA) 21 #include "third_party/skia/include/core/SkRefCnt.h" // nogncheck 22 #endif 23 24 class CFX_AggClipRgn; 25 class CFX_DIBitmap; 26 class CFX_Matrix; 27 class PauseIndicatorIface; 28 struct FX_RECT; 29 30 #if defined(PDF_USE_SKIA) 31 class SkImage; 32 #endif // defined(PDF_USE_SKIA) 33 34 // Base class for all Device-Independent Bitmaps. 35 class CFX_DIBBase : public Retainable { 36 public: 37 #if BUILDFLAG(IS_APPLE) 38 // Matches Apple's kCGBitmapByteOrder32Little in fx_quartz_device.cpp. 39 static constexpr FXDIB_Format kPlatformRGBFormat = FXDIB_Format::kBgrx; 40 using kPlatformRGBStruct = FX_BGRA_STRUCT<uint8_t>; 41 #else // BUILDFLAG(IS_APPLE) 42 static constexpr FXDIB_Format kPlatformRGBFormat = FXDIB_Format::kBgr; 43 using kPlatformRGBStruct = FX_BGR_STRUCT<uint8_t>; 44 #endif // BUILDFLAG(IS_APPLE) 45 46 static constexpr uint32_t kPaletteSize = 256; 47 48 // Note that the returned scanline includes unused space at the end, if any. 49 virtual pdfium::span<const uint8_t> GetScanline(int line) const = 0; 50 virtual bool SkipToScanline(int line, PauseIndicatorIface* pPause) const; 51 virtual size_t GetEstimatedImageMemoryBurden() const; 52 #if BUILDFLAG(IS_WIN) || defined(PDF_USE_SKIA) 53 // Calls Realize() if needed. Otherwise, return `this`. 54 virtual RetainPtr<const CFX_DIBitmap> RealizeIfNeeded() const; 55 #endif 56 57 // Note that the returned scanline does not include unused space at the end, 58 // if any. 59 template <typename T> GetScanlineAs(int line)60 pdfium::span<const T> GetScanlineAs(int line) const { 61 return fxcrt::reinterpret_span<const T>(GetScanline(line)) 62 .first(GetWidth()); 63 } 64 GetWidth()65 int GetWidth() const { return width_; } GetHeight()66 int GetHeight() const { return height_; } GetPitch()67 uint32_t GetPitch() const { return pitch_; } 68 GetFormat()69 FXDIB_Format GetFormat() const { return format_; } 70 71 // Bits per pixel, not bytes. GetBPP()72 int GetBPP() const { return GetBppFromFormat(GetFormat()); } 73 IsMaskFormat()74 bool IsMaskFormat() const { return GetIsMaskFromFormat(GetFormat()); } IsAlphaFormat()75 bool IsAlphaFormat() const { return GetIsAlphaFromFormat(GetFormat()); } IsOpaqueImage()76 bool IsOpaqueImage() const { return !IsMaskFormat() && !IsAlphaFormat(); } 77 HasPalette()78 bool HasPalette() const { return !palette_.empty(); } GetPaletteSpan()79 pdfium::span<const uint32_t> GetPaletteSpan() const { return palette_; } 80 size_t GetRequiredPaletteSize() const; 81 uint32_t GetPaletteArgb(int index) const; 82 void SetPaletteArgb(int index, uint32_t color); 83 84 // Copies into internally-owned palette. 85 void SetPalette(pdfium::span<const uint32_t> src_palette); 86 87 // Moves palette into internally-owned palette. 88 void TakePalette(DataVector<uint32_t> src_palette); 89 90 RetainPtr<CFX_DIBitmap> Realize() const; 91 RetainPtr<CFX_DIBitmap> ClipTo(const FX_RECT& rect) const; 92 // `format` must be different from the existing format. 93 // Only supports `FXDIB_Format::kBgr` and `FXDIB_Format::k8bppRgb`. 94 RetainPtr<CFX_DIBitmap> ConvertTo(FXDIB_Format format) const; 95 RetainPtr<CFX_DIBitmap> StretchTo(int dest_width, 96 int dest_height, 97 const FXDIB_ResampleOptions& options, 98 const FX_RECT* pClip) const; 99 RetainPtr<CFX_DIBitmap> TransformTo(const CFX_Matrix& mtDest, 100 int* left, 101 int* top) const; 102 RetainPtr<CFX_DIBitmap> SwapXY(bool bXFlip, bool bYFlip) const; 103 RetainPtr<CFX_DIBitmap> FlipImage(bool bXFlip, bool bYFlip) const; 104 105 RetainPtr<CFX_DIBitmap> CloneAlphaMask() const; 106 107 bool GetOverlapRect(int& dest_left, 108 int& dest_top, 109 int& width, 110 int& height, 111 int src_width, 112 int src_height, 113 int& src_left, 114 int& src_top, 115 const CFX_AggClipRgn* pClipRgn) const; 116 IsPremultiplied()117 bool IsPremultiplied() const { 118 #if defined(PDF_USE_SKIA) 119 return GetFormat() == FXDIB_Format::kBgraPremul; 120 #else 121 return false; 122 #endif 123 } 124 125 #if defined(PDF_USE_SKIA) 126 // Realizes an `SkImage` from this DIB. 127 // 128 // This may share the underlying pixels, in which case, this DIB should not be 129 // modified during the lifetime of the `SkImage`. 130 virtual sk_sp<SkImage> RealizeSkImage() const; 131 #endif // defined(PDF_USE_SKIA) 132 133 protected: 134 CFX_DIBBase(); 135 ~CFX_DIBBase() override; 136 137 // Returns the color palette, or an empty vector if there is no palette. 138 static DataVector<uint32_t> ConvertBuffer( 139 FXDIB_Format dest_format, 140 pdfium::span<uint8_t> dest_buf, 141 int dest_pitch, 142 int width, 143 int height, 144 const RetainPtr<const CFX_DIBBase>& pSrcBitmap, 145 int src_left, 146 int src_top); 147 148 RetainPtr<CFX_DIBitmap> ClipToInternal(const FX_RECT* pClip) const; 149 void BuildPalette(); 150 int FindPalette(uint32_t color) const; 151 SetFormat(FXDIB_Format format)152 void SetFormat(FXDIB_Format format) { format_ = format; } SetWidth(int width)153 void SetWidth(int width) { width_ = width; } SetHeight(int height)154 void SetHeight(int height) { height_ = height; } SetPitch(uint32_t pitch)155 void SetPitch(uint32_t pitch) { pitch_ = pitch; } 156 157 DataVector<uint32_t> palette_; 158 159 private: 160 FXDIB_Format format_ = FXDIB_Format::kInvalid; 161 int width_ = 0; 162 int height_ = 0; 163 uint32_t pitch_ = 0; 164 }; 165 166 #endif // CORE_FXGE_DIB_CFX_DIBBASE_H_ 167