1 // Copyright 2014 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_FX_DIB_H_
8 #define CORE_FXGE_DIB_FX_DIB_H_
9
10 #include <stdint.h>
11
12 #include <tuple>
13 #include <utility>
14
15 // Encoding:
16 // - Bits-per-pixel: value & 0xFF
17 // - Is mask: value & 0x100
18 // - Has alpha: value & 0x200
19 enum class FXDIB_Format : uint16_t {
20 kInvalid = 0,
21 k1bppRgb = 0x001,
22 k8bppRgb = 0x008,
23 kRgb = 0x018,
24 kRgb32 = 0x020,
25 k1bppMask = 0x101,
26 k8bppMask = 0x108,
27 kArgb = 0x220,
28 };
29
30 using FX_ARGB = uint32_t;
31 using FX_CMYK = uint32_t;
32
33 // FX_COLORREF, like win32 COLORREF, is BGR.
34 using FX_COLORREF = uint32_t;
35
36 struct FXDIB_ResampleOptions {
37 FXDIB_ResampleOptions();
38
39 bool HasAnyOptions() const;
40
41 bool bInterpolateBilinear = false;
42 bool bHalftone = false;
43 bool bNoSmoothing = false;
44 bool bLossy = false;
45 };
46
47 // See PDF 1.7 spec, table 7.2 and 7.3. The enum values need to be in the same
48 // order as listed in the spec.
49 enum class BlendMode {
50 kNormal = 0,
51 kMultiply,
52 kScreen,
53 kOverlay,
54 kDarken,
55 kLighten,
56 kColorDodge,
57 kColorBurn,
58 kHardLight,
59 kSoftLight,
60 kDifference,
61 kExclusion,
62 kHue,
63 kSaturation,
64 kColor,
65 kLuminosity,
66 kLast = kLuminosity,
67 };
68
FXSYS_BGR(uint8_t b,uint8_t g,uint8_t r)69 constexpr uint32_t FXSYS_BGR(uint8_t b, uint8_t g, uint8_t r) {
70 return (b << 16) | (g << 8) | r;
71 }
72
FXSYS_GetRValue(uint32_t bgr)73 constexpr uint8_t FXSYS_GetRValue(uint32_t bgr) {
74 return bgr & 0xff;
75 }
76
FXSYS_GetGValue(uint32_t bgr)77 constexpr uint8_t FXSYS_GetGValue(uint32_t bgr) {
78 return (bgr >> 8) & 0xff;
79 }
80
FXSYS_GetBValue(uint32_t bgr)81 constexpr uint8_t FXSYS_GetBValue(uint32_t bgr) {
82 return (bgr >> 16) & 0xff;
83 }
84
FXSYS_GetUnsignedAlpha(float alpha)85 constexpr unsigned int FXSYS_GetUnsignedAlpha(float alpha) {
86 return static_cast<unsigned int>(alpha * 255.f + 0.5f);
87 }
88
89 // Bits per pixel, not bytes.
GetBppFromFormat(FXDIB_Format format)90 inline int GetBppFromFormat(FXDIB_Format format) {
91 return static_cast<uint16_t>(format) & 0xff;
92 }
93
94 // AKA bytes per pixel, assuming 8-bits per component.
GetCompsFromFormat(FXDIB_Format format)95 inline int GetCompsFromFormat(FXDIB_Format format) {
96 return (static_cast<uint16_t>(format) & 0xff) / 8;
97 }
98
GetIsMaskFromFormat(FXDIB_Format format)99 inline bool GetIsMaskFromFormat(FXDIB_Format format) {
100 return !!(static_cast<uint16_t>(format) & 0x100);
101 }
102
103 FXDIB_Format MakeRGBFormat(int bpp);
104
CmykEncode(uint32_t c,uint32_t m,uint32_t y,uint32_t k)105 constexpr FX_CMYK CmykEncode(uint32_t c, uint32_t m, uint32_t y, uint32_t k) {
106 return (c << 24) | (m << 16) | (y << 8) | k;
107 }
108
109 // Returns (a, r, g, b)
110 std::tuple<int, int, int, int> ArgbDecode(FX_ARGB argb);
111
112 // Returns (a, FX_COLORREF)
113 std::pair<int, FX_COLORREF> ArgbToAlphaAndColorRef(FX_ARGB argb);
114
115 // Returns FX_COLORREF.
116 FX_COLORREF ArgbToColorRef(FX_ARGB argb);
117
ArgbEncode(uint32_t a,uint32_t r,uint32_t g,uint32_t b)118 constexpr FX_ARGB ArgbEncode(uint32_t a, uint32_t r, uint32_t g, uint32_t b) {
119 return (a << 24) | (r << 16) | (g << 8) | b;
120 }
121
122 FX_ARGB AlphaAndColorRefToArgb(int a, FX_COLORREF colorref);
123
124 #define FXARGB_A(argb) ((uint8_t)((argb) >> 24))
125 #define FXARGB_R(argb) ((uint8_t)((argb) >> 16))
126 #define FXARGB_G(argb) ((uint8_t)((argb) >> 8))
127 #define FXARGB_B(argb) ((uint8_t)(argb))
128 #define FXARGB_MUL_ALPHA(argb, alpha) \
129 (((((argb) >> 24) * (alpha) / 255) << 24) | ((argb)&0xffffff))
130
131 #define FXRGB2GRAY(r, g, b) (((b)*11 + (g)*59 + (r)*30) / 100)
132 #define FXDIB_ALPHA_MERGE(backdrop, source, source_alpha) \
133 (((backdrop) * (255 - (source_alpha)) + (source) * (source_alpha)) / 255)
134 #define FXARGB_GETDIB(p) \
135 ((((uint8_t*)(p))[0]) | (((uint8_t*)(p))[1] << 8) | \
136 (((uint8_t*)(p))[2] << 16) | (((uint8_t*)(p))[3] << 24))
137 #define FXARGB_SETDIB(p, argb) \
138 ((uint8_t*)(p))[0] = (uint8_t)(argb), \
139 ((uint8_t*)(p))[1] = (uint8_t)((argb) >> 8), \
140 ((uint8_t*)(p))[2] = (uint8_t)((argb) >> 16), \
141 ((uint8_t*)(p))[3] = (uint8_t)((argb) >> 24)
142 #define FXARGB_SETRGBORDERDIB(p, argb) \
143 ((uint8_t*)(p))[3] = (uint8_t)(argb >> 24), \
144 ((uint8_t*)(p))[0] = (uint8_t)((argb) >> 16), \
145 ((uint8_t*)(p))[1] = (uint8_t)((argb) >> 8), \
146 ((uint8_t*)(p))[2] = (uint8_t)(argb)
147 #define FXCMYK_TODIB(cmyk) \
148 ((uint8_t)((cmyk) >> 24) | ((uint8_t)((cmyk) >> 16)) << 8 | \
149 ((uint8_t)((cmyk) >> 8)) << 16 | ((uint8_t)(cmyk) << 24))
150 #define FXARGB_TOBGRORDERDIB(argb) \
151 ((uint8_t)(argb >> 16) | ((uint8_t)(argb >> 8)) << 8 | \
152 ((uint8_t)(argb)) << 16 | ((uint8_t)(argb >> 24) << 24))
153
ReverseCopy3Bytes(uint8_t * dest,const uint8_t * src)154 inline void ReverseCopy3Bytes(uint8_t* dest, const uint8_t* src) {
155 dest[2] = src[0];
156 dest[1] = src[1];
157 dest[0] = src[2];
158 }
159
160 #endif // CORE_FXGE_DIB_FX_DIB_H_
161