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 #include "core/fxge/fx_dib.h"
8
9 #include <tuple>
10 #include <utility>
11
12 #include "build/build_config.h"
13 #include "core/fxcrt/fx_extension.h"
14
15 #if defined(OS_WIN)
16 static_assert(sizeof(FX_COLORREF) == sizeof(COLORREF),
17 "FX_COLORREF vs. COLORREF mismatch");
18 #endif
19
20 const int16_t SDP_Table[513] = {
21 256, 256, 256, 256, 256, 256, 256, 256, 256, 255, 255, 255, 255, 255, 255,
22 254, 254, 254, 254, 253, 253, 253, 252, 252, 252, 251, 251, 251, 250, 250,
23 249, 249, 249, 248, 248, 247, 247, 246, 246, 245, 244, 244, 243, 243, 242,
24 242, 241, 240, 240, 239, 238, 238, 237, 236, 236, 235, 234, 233, 233, 232,
25 231, 230, 230, 229, 228, 227, 226, 226, 225, 224, 223, 222, 221, 220, 219,
26 218, 218, 217, 216, 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205,
27 204, 203, 202, 201, 200, 199, 198, 196, 195, 194, 193, 192, 191, 190, 189,
28 188, 186, 185, 184, 183, 182, 181, 179, 178, 177, 176, 175, 173, 172, 171,
29 170, 169, 167, 166, 165, 164, 162, 161, 160, 159, 157, 156, 155, 154, 152,
30 151, 150, 149, 147, 146, 145, 143, 142, 141, 140, 138, 137, 136, 134, 133,
31 132, 130, 129, 128, 126, 125, 124, 122, 121, 120, 119, 117, 116, 115, 113,
32 112, 111, 109, 108, 107, 105, 104, 103, 101, 100, 99, 97, 96, 95, 93,
33 92, 91, 89, 88, 87, 85, 84, 83, 81, 80, 79, 77, 76, 75, 73,
34 72, 71, 69, 68, 67, 66, 64, 63, 62, 60, 59, 58, 57, 55, 54,
35 53, 52, 50, 49, 48, 47, 45, 44, 43, 42, 40, 39, 38, 37, 36,
36 34, 33, 32, 31, 30, 28, 27, 26, 25, 24, 23, 21, 20, 19, 18,
37 17, 16, 15, 14, 13, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,
38 1, 0, 0, -1, -2, -3, -4, -5, -6, -7, -7, -8, -9, -10, -11,
39 -12, -12, -13, -14, -15, -15, -16, -17, -17, -18, -19, -19, -20, -21, -21,
40 -22, -22, -23, -24, -24, -25, -25, -26, -26, -27, -27, -27, -28, -28, -29,
41 -29, -30, -30, -30, -31, -31, -31, -32, -32, -32, -33, -33, -33, -33, -34,
42 -34, -34, -34, -35, -35, -35, -35, -35, -36, -36, -36, -36, -36, -36, -36,
43 -36, -36, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37,
44 -37, -37, -37, -37, -37, -37, -37, -37, -36, -36, -36, -36, -36, -36, -36,
45 -36, -36, -35, -35, -35, -35, -35, -35, -34, -34, -34, -34, -34, -33, -33,
46 -33, -33, -33, -32, -32, -32, -32, -31, -31, -31, -31, -30, -30, -30, -30,
47 -29, -29, -29, -29, -28, -28, -28, -27, -27, -27, -27, -26, -26, -26, -25,
48 -25, -25, -24, -24, -24, -23, -23, -23, -22, -22, -22, -22, -21, -21, -21,
49 -20, -20, -20, -19, -19, -19, -18, -18, -18, -17, -17, -17, -16, -16, -16,
50 -15, -15, -15, -14, -14, -14, -13, -13, -13, -12, -12, -12, -11, -11, -11,
51 -10, -10, -10, -9, -9, -9, -9, -8, -8, -8, -7, -7, -7, -7, -6,
52 -6, -6, -6, -5, -5, -5, -5, -4, -4, -4, -4, -3, -3, -3, -3,
53 -3, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0,
56 };
57
58 FXDIB_ResampleOptions::FXDIB_ResampleOptions() = default;
59
HasAnyOptions() const60 bool FXDIB_ResampleOptions::HasAnyOptions() const {
61 return bInterpolateBilinear || bInterpolateBicubic || bHalftone ||
62 bNoSmoothing || bLossy;
63 }
64
FXDIB_SwapClipBox(const FX_RECT & clip,int width,int height,bool bFlipX,bool bFlipY)65 FX_RECT FXDIB_SwapClipBox(const FX_RECT& clip,
66 int width,
67 int height,
68 bool bFlipX,
69 bool bFlipY) {
70 FX_RECT rect;
71 if (bFlipY) {
72 rect.left = height - clip.top;
73 rect.right = height - clip.bottom;
74 } else {
75 rect.left = clip.top;
76 rect.right = clip.bottom;
77 }
78 if (bFlipX) {
79 rect.top = width - clip.left;
80 rect.bottom = width - clip.right;
81 } else {
82 rect.top = clip.left;
83 rect.bottom = clip.right;
84 }
85 rect.Normalize();
86 return rect;
87 }
88
ArgbDecode(FX_ARGB argb)89 std::tuple<int, int, int, int> ArgbDecode(FX_ARGB argb) {
90 return std::make_tuple(FXARGB_A(argb), FXARGB_R(argb), FXARGB_G(argb),
91 FXARGB_B(argb));
92 }
93
ArgbToAlphaAndColorRef(FX_ARGB argb)94 std::pair<int, FX_COLORREF> ArgbToAlphaAndColorRef(FX_ARGB argb) {
95 return {FXARGB_A(argb), ArgbToColorRef(argb)};
96 }
97
ArgbToColorRef(FX_ARGB argb)98 FX_COLORREF ArgbToColorRef(FX_ARGB argb) {
99 return FXSYS_BGR(FXARGB_B(argb), FXARGB_G(argb), FXARGB_R(argb));
100 }
101
AlphaAndColorRefToArgb(int a,FX_COLORREF colorref)102 FX_ARGB AlphaAndColorRefToArgb(int a, FX_COLORREF colorref) {
103 return ArgbEncode(a, FXSYS_GetRValue(colorref), FXSYS_GetGValue(colorref),
104 FXSYS_GetBValue(colorref));
105 }
106
StringToFXARGB(WideStringView view)107 FX_ARGB StringToFXARGB(WideStringView view) {
108 static constexpr FX_ARGB kDefaultValue = 0xff000000;
109 if (view.IsEmpty())
110 return kDefaultValue;
111
112 int cc = 0;
113 const wchar_t* str = view.unterminated_c_str();
114 int len = view.GetLength();
115 while (cc < len && FXSYS_iswspace(str[cc]))
116 cc++;
117
118 if (cc >= len)
119 return kDefaultValue;
120
121 uint8_t r = 0;
122 uint8_t g = 0;
123 uint8_t b = 0;
124 while (cc < len) {
125 if (str[cc] == ',' || !FXSYS_IsDecimalDigit(str[cc]))
126 break;
127
128 r = r * 10 + str[cc] - '0';
129 cc++;
130 }
131 if (cc < len && str[cc] == ',') {
132 cc++;
133 while (cc < len && FXSYS_iswspace(str[cc]))
134 cc++;
135
136 while (cc < len) {
137 if (str[cc] == ',' || !FXSYS_IsDecimalDigit(str[cc]))
138 break;
139
140 g = g * 10 + str[cc] - '0';
141 cc++;
142 }
143 if (cc < len && str[cc] == ',') {
144 cc++;
145 while (cc < len && FXSYS_iswspace(str[cc]))
146 cc++;
147
148 while (cc < len) {
149 if (str[cc] == ',' || !FXSYS_IsDecimalDigit(str[cc]))
150 break;
151
152 b = b * 10 + str[cc] - '0';
153 cc++;
154 }
155 }
156 }
157 return (0xffU << 24) | (r << 16) | (g << 8) | b;
158 }
159