1 // Copyright 2014 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_FX_DIB_H_
8 #define CORE_FXGE_FX_DIB_H_
9 
10 #include <tuple>
11 #include <utility>
12 
13 #include "core/fxcrt/fx_coordinates.h"
14 #include "core/fxcrt/widestring.h"
15 
16 enum FXDIB_Format {
17   FXDIB_Invalid = 0,
18   FXDIB_1bppRgb = 0x001,
19   FXDIB_8bppRgb = 0x008,
20   FXDIB_Rgb = 0x018,
21   FXDIB_Rgb32 = 0x020,
22   FXDIB_1bppMask = 0x101,
23   FXDIB_8bppMask = 0x108,
24   FXDIB_8bppRgba = 0x208,
25   FXDIB_Rgba = 0x218,
26   FXDIB_Argb = 0x220,
27   FXDIB_1bppCmyk = 0x401,
28   FXDIB_8bppCmyk = 0x408,
29   FXDIB_Cmyk = 0x420,
30   FXDIB_8bppCmyka = 0x608,
31   FXDIB_Cmyka = 0x620,
32 };
33 
34 struct PixelWeight {
35   int m_SrcStart;
36   int m_SrcEnd;
37   int m_Weights[1];
38 };
39 
40 using FX_ARGB = uint32_t;
41 
42 // FX_COLORREF, like win32 COLORREF, is BGR.
43 using FX_COLORREF = uint32_t;
44 
45 using FX_CMYK = uint32_t;
46 
47 extern const int16_t SDP_Table[513];
48 
49 struct FXDIB_ResampleOptions {
50   FXDIB_ResampleOptions();
51 
52   bool HasAnyOptions() const;
53 
54   bool bInterpolateBilinear = false;
55   bool bInterpolateBicubic = false;
56   bool bHalftone = false;
57   bool bNoSmoothing = false;
58   bool bLossy = false;
59 };
60 
61 // See PDF 1.7 spec, table 7.2 and 7.3. The enum values need to be in the same
62 // order as listed in the spec.
63 enum class BlendMode {
64   kNormal = 0,
65   kMultiply,
66   kScreen,
67   kOverlay,
68   kDarken,
69   kLighten,
70   kColorDodge,
71   kColorBurn,
72   kHardLight,
73   kSoftLight,
74   kDifference,
75   kExclusion,
76   kHue,
77   kSaturation,
78   kColor,
79   kLuminosity,
80   kLast = kLuminosity,
81 };
82 
FXSYS_BGR(uint8_t b,uint8_t g,uint8_t r)83 constexpr uint32_t FXSYS_BGR(uint8_t b, uint8_t g, uint8_t r) {
84   return (b << 16) | (g << 8) | r;
85 }
86 
FXSYS_GetRValue(uint32_t bgr)87 constexpr uint8_t FXSYS_GetRValue(uint32_t bgr) {
88   return bgr & 0xff;
89 }
90 
FXSYS_GetGValue(uint32_t bgr)91 constexpr uint8_t FXSYS_GetGValue(uint32_t bgr) {
92   return (bgr >> 8) & 0xff;
93 }
94 
FXSYS_GetBValue(uint32_t bgr)95 constexpr uint8_t FXSYS_GetBValue(uint32_t bgr) {
96   return (bgr >> 16) & 0xff;
97 }
98 
FXSYS_GetUnsignedAlpha(float alpha)99 constexpr unsigned int FXSYS_GetUnsignedAlpha(float alpha) {
100   return static_cast<unsigned int>(alpha * 255.f + 0.5f);
101 }
102 
103 #define FXSYS_GetCValue(cmyk) ((uint8_t)((cmyk) >> 24) & 0xff)
104 #define FXSYS_GetMValue(cmyk) ((uint8_t)((cmyk) >> 16) & 0xff)
105 #define FXSYS_GetYValue(cmyk) ((uint8_t)((cmyk) >> 8) & 0xff)
106 #define FXSYS_GetKValue(cmyk) ((uint8_t)(cmyk)&0xff)
107 
108 // Bits per pixel, not bytes.
GetBppFromFormat(FXDIB_Format format)109 inline int GetBppFromFormat(FXDIB_Format format) {
110   return format & 0xff;
111 }
112 
113 // AKA bytes per pixel, assuming 8-bits per component.
GetCompsFromFormat(FXDIB_Format format)114 inline int GetCompsFromFormat(FXDIB_Format format) {
115   return (format & 0xff) / 8;
116 }
117 
GetAlphaFlagFromFormat(FXDIB_Format format)118 inline uint32_t GetAlphaFlagFromFormat(FXDIB_Format format) {
119   return (format >> 8) & 0xff;
120 }
121 
GetIsAlphaFromFormat(FXDIB_Format format)122 inline bool GetIsAlphaFromFormat(FXDIB_Format format) {
123   return format & 0x200;
124 }
125 
GetIsCmykFromFormat(FXDIB_Format format)126 inline bool GetIsCmykFromFormat(FXDIB_Format format) {
127   return format & 0x400;
128 }
129 
CmykEncode(int c,int m,int y,int k)130 inline FX_CMYK CmykEncode(int c, int m, int y, int k) {
131   return (c << 24) | (m << 16) | (y << 8) | k;
132 }
133 
134 // Returns (a, r, g, b)
135 std::tuple<int, int, int, int> ArgbDecode(FX_ARGB argb);
136 
137 // Returns (a, FX_COLORREF)
138 std::pair<int, FX_COLORREF> ArgbToAlphaAndColorRef(FX_ARGB argb);
139 
140 // Returns FX_COLORREF.
141 FX_COLORREF ArgbToColorRef(FX_ARGB argb);
142 
ArgbEncode(int a,int r,int g,int b)143 constexpr FX_ARGB ArgbEncode(int a, int r, int g, int b) {
144   return (a << 24) | (r << 16) | (g << 8) | b;
145 }
146 
147 FX_ARGB AlphaAndColorRefToArgb(int a, FX_COLORREF colorref);
148 
149 FX_ARGB StringToFXARGB(WideStringView view);
150 
151 #define FXARGB_A(argb) ((uint8_t)((argb) >> 24))
152 #define FXARGB_R(argb) ((uint8_t)((argb) >> 16))
153 #define FXARGB_G(argb) ((uint8_t)((argb) >> 8))
154 #define FXARGB_B(argb) ((uint8_t)(argb))
155 #define FXARGB_MUL_ALPHA(argb, alpha) \
156   (((((argb) >> 24) * (alpha) / 255) << 24) | ((argb)&0xffffff))
157 
158 #define FXRGB2GRAY(r, g, b) (((b)*11 + (g)*59 + (r)*30) / 100)
159 #define FXDIB_ALPHA_MERGE(backdrop, source, source_alpha) \
160   (((backdrop) * (255 - (source_alpha)) + (source) * (source_alpha)) / 255)
161 #define FXARGB_GETDIB(p)                              \
162   ((((uint8_t*)(p))[0]) | (((uint8_t*)(p))[1] << 8) | \
163    (((uint8_t*)(p))[2] << 16) | (((uint8_t*)(p))[3] << 24))
164 #define FXARGB_SETDIB(p, argb)                  \
165   ((uint8_t*)(p))[0] = (uint8_t)(argb),         \
166   ((uint8_t*)(p))[1] = (uint8_t)((argb) >> 8),  \
167   ((uint8_t*)(p))[2] = (uint8_t)((argb) >> 16), \
168   ((uint8_t*)(p))[3] = (uint8_t)((argb) >> 24)
169 #define FXARGB_SETRGBORDERDIB(p, argb)          \
170   ((uint8_t*)(p))[3] = (uint8_t)(argb >> 24),   \
171   ((uint8_t*)(p))[0] = (uint8_t)((argb) >> 16), \
172   ((uint8_t*)(p))[1] = (uint8_t)((argb) >> 8),  \
173   ((uint8_t*)(p))[2] = (uint8_t)(argb)
174 #define FXARGB_TODIB(argb) (argb)
175 #define FXCMYK_TODIB(cmyk)                                    \
176   ((uint8_t)((cmyk) >> 24) | ((uint8_t)((cmyk) >> 16)) << 8 | \
177    ((uint8_t)((cmyk) >> 8)) << 16 | ((uint8_t)(cmyk) << 24))
178 #define FXARGB_TOBGRORDERDIB(argb)                       \
179   ((uint8_t)(argb >> 16) | ((uint8_t)(argb >> 8)) << 8 | \
180    ((uint8_t)(argb)) << 16 | ((uint8_t)(argb >> 24) << 24))
181 
182 FX_RECT FXDIB_SwapClipBox(const FX_RECT& clip,
183                           int width,
184                           int height,
185                           bool bFlipX,
186                           bool bFlipY);
187 
188 #endif  // CORE_FXGE_FX_DIB_H_
189