1 // Copyright 2017 PDFium Authors. All rights reserved. 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_DIBSOURCE_H_ 8 #define CORE_FXGE_DIB_CFX_DIBSOURCE_H_ 9 10 #include <memory> 11 12 #include "core/fxcrt/fx_coordinates.h" 13 #include "core/fxcrt/fx_memory.h" 14 #include "core/fxcrt/retain_ptr.h" 15 #include "core/fxge/fx_dib.h" 16 17 enum FXDIB_Channel { 18 FXDIB_Red = 1, 19 FXDIB_Green, 20 FXDIB_Blue, 21 FXDIB_Cyan, 22 FXDIB_Magenta, 23 FXDIB_Yellow, 24 FXDIB_Black, 25 FXDIB_Alpha 26 }; 27 28 class CFX_ClipRgn; 29 class CFX_DIBitmap; 30 class IFX_PauseIndicator; 31 32 class CFX_DIBSource : public Retainable { 33 public: 34 ~CFX_DIBSource() override; 35 36 virtual uint8_t* GetBuffer() const; 37 virtual const uint8_t* GetScanline(int line) const = 0; 38 virtual bool SkipToScanline(int line, IFX_PauseIndicator* pPause) const; 39 virtual void DownSampleScanline(int line, 40 uint8_t* dest_scan, 41 int dest_bpp, 42 int dest_width, 43 bool bFlipX, 44 int clip_left, 45 int clip_width) const = 0; 46 GetWidth()47 int GetWidth() const { return m_Width; } GetHeight()48 int GetHeight() const { return m_Height; } 49 GetFormat()50 FXDIB_Format GetFormat() const { 51 return static_cast<FXDIB_Format>(m_AlphaFlag * 0x100 + m_bpp); 52 } GetPitch()53 uint32_t GetPitch() const { return m_Pitch; } GetPalette()54 uint32_t* GetPalette() const { return m_pPalette.get(); } GetBPP()55 int GetBPP() const { return m_bpp; } 56 57 // TODO(thestig): Investigate this. Given the possible values of FXDIB_Format, 58 // it feels as though this should be implemented as !!(m_AlphaFlag & 1) and 59 // IsOpaqueImage() below should never be able to return true. IsAlphaMask()60 bool IsAlphaMask() const { return m_AlphaFlag == 1; } HasAlpha()61 bool HasAlpha() const { return !!(m_AlphaFlag & 2); } IsOpaqueImage()62 bool IsOpaqueImage() const { return !(m_AlphaFlag & 3); } IsCmykImage()63 bool IsCmykImage() const { return !!(m_AlphaFlag & 4); } 64 GetPaletteSize()65 int GetPaletteSize() const { 66 return IsAlphaMask() ? 0 : (m_bpp == 1 ? 2 : (m_bpp == 8 ? 256 : 0)); 67 } 68 69 uint32_t GetPaletteArgb(int index) const; 70 void SetPaletteArgb(int index, uint32_t color); 71 72 // Copies into internally-owned palette. 73 void SetPalette(const uint32_t* pSrcPal); 74 75 RetainPtr<CFX_DIBitmap> Clone(const FX_RECT* pClip) const; 76 RetainPtr<CFX_DIBitmap> CloneConvert(FXDIB_Format format); 77 RetainPtr<CFX_DIBitmap> StretchTo(int dest_width, 78 int dest_height, 79 uint32_t flags, 80 const FX_RECT* pClip); 81 RetainPtr<CFX_DIBitmap> TransformTo(const CFX_Matrix* pMatrix, 82 int* left, 83 int* top); 84 RetainPtr<CFX_DIBitmap> SwapXY(bool bXFlip, bool bYFlip) const; 85 RetainPtr<CFX_DIBitmap> FlipImage(bool bXFlip, bool bYFlip) const; 86 87 RetainPtr<CFX_DIBitmap> CloneAlphaMask() const; 88 89 // Copies into internally-owned mask. 90 bool SetAlphaMask(const RetainPtr<CFX_DIBSource>& pAlphaMask, 91 const FX_RECT* pClip); 92 93 void GetOverlapRect(int& dest_left, 94 int& dest_top, 95 int& width, 96 int& height, 97 int src_width, 98 int src_height, 99 int& src_left, 100 int& src_top, 101 const CFX_ClipRgn* pClipRgn); 102 103 #if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_ 104 void DebugVerifyBitmapIsPreMultiplied(void* buffer) const; 105 #endif 106 107 RetainPtr<CFX_DIBitmap> m_pAlphaMask; 108 109 protected: 110 CFX_DIBSource(); 111 112 static bool ConvertBuffer(FXDIB_Format dest_format, 113 uint8_t* dest_buf, 114 int dest_pitch, 115 int width, 116 int height, 117 const RetainPtr<CFX_DIBSource>& pSrcBitmap, 118 int src_left, 119 int src_top, 120 std::unique_ptr<uint32_t, FxFreeDeleter>* pal); 121 122 void BuildPalette(); 123 bool BuildAlphaMask(); 124 int FindPalette(uint32_t color) const; 125 void GetPalette(uint32_t* pal, int alpha) const; 126 127 int m_Width; 128 int m_Height; 129 int m_bpp; 130 uint32_t m_AlphaFlag; 131 uint32_t m_Pitch; 132 // TODO(weili): Use std::vector for this. 133 std::unique_ptr<uint32_t, FxFreeDeleter> m_pPalette; 134 }; 135 136 #endif // CORE_FXGE_DIB_CFX_DIBSOURCE_H_ 137