summaryrefslogtreecommitdiff
path: root/core/fxge/dib/cfx_dibsource.h
blob: 76f70095028fd7bb40207661a61682e86de09f78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#ifndef CORE_FXGE_DIB_CFX_DIBSOURCE_H_
#define CORE_FXGE_DIB_CFX_DIBSOURCE_H_

#include <memory>

#include "core/fxcrt/cfx_retain_ptr.h"
#include "core/fxcrt/fx_basic.h"
#include "core/fxcrt/fx_coordinates.h"

enum FXDIB_Channel {
  FXDIB_Red = 1,
  FXDIB_Green,
  FXDIB_Blue,
  FXDIB_Cyan,
  FXDIB_Magenta,
  FXDIB_Yellow,
  FXDIB_Black,
  FXDIB_Alpha
};

enum FXDIB_Format {
  FXDIB_Invalid = 0,
  FXDIB_1bppMask = 0x101,
  FXDIB_1bppRgb = 0x001,
  FXDIB_1bppCmyk = 0x401,
  FXDIB_8bppMask = 0x108,
  FXDIB_8bppRgb = 0x008,
  FXDIB_8bppRgba = 0x208,
  FXDIB_8bppCmyk = 0x408,
  FXDIB_8bppCmyka = 0x608,
  FXDIB_Rgb = 0x018,
  FXDIB_Rgba = 0x218,
  FXDIB_Rgb32 = 0x020,
  FXDIB_Argb = 0x220,
  FXDIB_Cmyk = 0x420,
  FXDIB_Cmyka = 0x620,
};

#define FXDIB_DOWNSAMPLE 0x04
#define FXDIB_INTERPOL 0x20
#define FXDIB_BICUBIC_INTERPOL 0x80
#define FXDIB_NOSMOOTH 0x100
#define FXDIB_BLEND_NORMAL 0
#define FXDIB_BLEND_MULTIPLY 1
#define FXDIB_BLEND_SCREEN 2
#define FXDIB_BLEND_OVERLAY 3
#define FXDIB_BLEND_DARKEN 4
#define FXDIB_BLEND_LIGHTEN 5

#define FXDIB_BLEND_COLORDODGE 6
#define FXDIB_BLEND_COLORBURN 7
#define FXDIB_BLEND_HARDLIGHT 8
#define FXDIB_BLEND_SOFTLIGHT 9
#define FXDIB_BLEND_DIFFERENCE 10
#define FXDIB_BLEND_EXCLUSION 11
#define FXDIB_BLEND_NONSEPARABLE 21
#define FXDIB_BLEND_HUE 21
#define FXDIB_BLEND_SATURATION 22
#define FXDIB_BLEND_COLOR 23
#define FXDIB_BLEND_LUMINOSITY 24
#define FXDIB_BLEND_UNSUPPORTED -1

class CFX_ClipRgn;
class CFX_DIBitmap;

class CFX_DIBSource : public CFX_Retainable {
 public:
  ~CFX_DIBSource() override;

  virtual uint8_t* GetBuffer() const;
  virtual const uint8_t* GetScanline(int line) const = 0;
  virtual bool SkipToScanline(int line, IFX_Pause* pPause) const;
  virtual void DownSampleScanline(int line,
                                  uint8_t* dest_scan,
                                  int dest_bpp,
                                  int dest_width,
                                  bool bFlipX,
                                  int clip_left,
                                  int clip_width) const = 0;

  int GetWidth() const { return m_Width; }
  int GetHeight() const { return m_Height; }

  FXDIB_Format GetFormat() const {
    return (FXDIB_Format)(m_AlphaFlag * 0x100 + m_bpp);
  }
  uint32_t GetPitch() const { return m_Pitch; }
  uint32_t* GetPalette() const { return m_pPalette.get(); }
  int GetBPP() const { return m_bpp; }

  // TODO(thestig): Investigate this. Given the possible values of FXDIB_Format,
  // it feels as though this should be implemented as !!(m_AlphaFlag & 1) and
  // IsOpaqueImage() below should never be able to return true.
  bool IsAlphaMask() const { return m_AlphaFlag == 1; }
  bool HasAlpha() const { return !!(m_AlphaFlag & 2); }
  bool IsOpaqueImage() const { return !(m_AlphaFlag & 3); }
  bool IsCmykImage() const { return !!(m_AlphaFlag & 4); }

  int GetPaletteSize() const {
    return IsAlphaMask() ? 0 : (m_bpp == 1 ? 2 : (m_bpp == 8 ? 256 : 0));
  }

  uint32_t GetPaletteEntry(int index) const;

  void SetPaletteEntry(int index, uint32_t color);
  uint32_t GetPaletteArgb(int index) const { return GetPaletteEntry(index); }
  void SetPaletteArgb(int index, uint32_t color) {
    SetPaletteEntry(index, color);
  }

  // Copies into internally-owned palette.
  void SetPalette(const uint32_t* pSrcPal);

  CFX_RetainPtr<CFX_DIBitmap> Clone(const FX_RECT* pClip = nullptr) const;
  CFX_RetainPtr<CFX_DIBitmap> CloneConvert(FXDIB_Format format);
  CFX_RetainPtr<CFX_DIBitmap> StretchTo(int dest_width,
                                        int dest_height,
                                        uint32_t flags = 0,
                                        const FX_RECT* pClip = nullptr);
  CFX_RetainPtr<CFX_DIBitmap> TransformTo(const CFX_Matrix* pMatrix,
                                          int& left,
                                          int& top,
                                          uint32_t flags = 0,
                                          const FX_RECT* pClip = nullptr);
  CFX_RetainPtr<CFX_DIBitmap> SwapXY(bool bXFlip,
                                     bool bYFlip,
                                     const FX_RECT* pClip = nullptr) const;
  CFX_RetainPtr<CFX_DIBitmap> FlipImage(bool bXFlip, bool bYFlip) const;

  CFX_RetainPtr<CFX_DIBitmap> CloneAlphaMask(
      const FX_RECT* pClip = nullptr) const;

  // Copies into internally-owned mask.
  bool SetAlphaMask(const CFX_RetainPtr<CFX_DIBSource>& pAlphaMask,
                    const FX_RECT* pClip = nullptr);

  void GetOverlapRect(int& dest_left,
                      int& dest_top,
                      int& width,
                      int& height,
                      int src_width,
                      int src_height,
                      int& src_left,
                      int& src_top,
                      const CFX_ClipRgn* pClipRgn);

#if defined _SKIA_SUPPORT_ || defined _SKIA_SUPPORT_PATHS_
  void DebugVerifyBitmapIsPreMultiplied(void* buffer = nullptr) const;
#endif

  CFX_RetainPtr<CFX_DIBitmap> m_pAlphaMask;

 protected:
  CFX_DIBSource();

  void BuildPalette();
  bool BuildAlphaMask();
  int FindPalette(uint32_t color) const;
  void GetPalette(uint32_t* pal, int alpha) const;

  int m_Width;
  int m_Height;
  int m_bpp;
  uint32_t m_AlphaFlag;
  uint32_t m_Pitch;
  // TODO(weili): Use std::vector for this.
  std::unique_ptr<uint32_t, FxFreeDeleter> m_pPalette;
};

#endif  // CORE_FXGE_DIB_CFX_DIBSOURCE_H_