OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkColorXform_opts_DEFINED | 8 #ifndef SkColorXform_opts_DEFINED |
9 #define SkColorXform_opts_DEFINED | 9 #define SkColorXform_opts_DEFINED |
10 | 10 |
11 #include "SkNx.h" | 11 #include "SkNx.h" |
12 #include "SkColorPriv.h" | 12 #include "SkColorPriv.h" |
13 #include "SkSRGB.h" | 13 #include "SkSRGB.h" |
14 | 14 |
15 extern const float sk_linear_from_2dot2[256]; | |
16 | |
17 namespace SK_OPTS_NS { | 15 namespace SK_OPTS_NS { |
18 | 16 |
19 static Sk4f linear_to_2dot2(const Sk4f& x) { | 17 static Sk4f linear_to_2dot2(const Sk4f& x) { |
20 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). | 18 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). |
21 auto x2 = x.rsqrt(), // x^(-1/2) | 19 auto x2 = x.rsqrt(), // x^(-1/2) |
22 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) | 20 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) |
23 x64 = x32.rsqrt(); // x^(+1/64) | 21 x64 = x32.rsqrt(); // x^(+1/64) |
24 | 22 |
25 // 29 = 32 - 2 - 1 | 23 // 29 = 32 - 2 - 1 |
26 return 255.0f * x2.invert() * x32 * x64.invert(); | 24 return 255.0f * x2.invert() * x32 * x64.invert(); |
27 } | 25 } |
28 | 26 |
29 static Sk4f clamp_0_to_255(const Sk4f& x) { | 27 static Sk4f clamp_0_to_255(const Sk4f& x) { |
30 // The order of the arguments is important here. We want to make sure that
NaN | 28 // The order of the arguments is important here. We want to make sure that
NaN |
31 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. | 29 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. |
32 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); | 30 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); |
33 } | 31 } |
34 | 32 |
35 template <const float (&linear_from_curve)[256], Sk4f (*linear_to_curve)(const S
k4f&)> | 33 enum DstGamma { |
| 34 kSRGB_DstGamma, |
| 35 k2Dot2_DstGamma, |
| 36 kTable_DstGamma, |
| 37 }; |
| 38 |
| 39 template <DstGamma kDstGamma> |
36 static void color_xform_RGB1(uint32_t* dst, const uint32_t* src, int len, | 40 static void color_xform_RGB1(uint32_t* dst, const uint32_t* src, int len, |
37 const float matrix[16]) { | 41 const float* const srcTables[3], const float matrix
[16], |
| 42 const uint8_t* const dstTables[3]) { |
38 Sk4f rXgXbX = Sk4f::Load(matrix + 0), | 43 Sk4f rXgXbX = Sk4f::Load(matrix + 0), |
39 rYgYbY = Sk4f::Load(matrix + 4), | 44 rYgYbY = Sk4f::Load(matrix + 4), |
40 rZgZbZ = Sk4f::Load(matrix + 8); | 45 rZgZbZ = Sk4f::Load(matrix + 8); |
41 | 46 |
42 if (len >= 4) { | 47 if (len >= 4) { |
43 Sk4f reds, greens, blues; | 48 Sk4f reds, greens, blues; |
44 auto load_next_4 = [&reds, &greens, &blues, &src, &len] { | 49 auto load_next_4 = [&reds, &greens, &blues, &src, &len, &srcTables] { |
45 reds = Sk4f{linear_from_curve[(src[0] >> 0) & 0xFF], | 50 reds = Sk4f{srcTables[0][(src[0] >> 0) & 0xFF], |
46 linear_from_curve[(src[1] >> 0) & 0xFF], | 51 srcTables[0][(src[1] >> 0) & 0xFF], |
47 linear_from_curve[(src[2] >> 0) & 0xFF], | 52 srcTables[0][(src[2] >> 0) & 0xFF], |
48 linear_from_curve[(src[3] >> 0) & 0xFF]}; | 53 srcTables[0][(src[3] >> 0) & 0xFF]}; |
49 greens = Sk4f{linear_from_curve[(src[0] >> 8) & 0xFF], | 54 greens = Sk4f{srcTables[1][(src[0] >> 8) & 0xFF], |
50 linear_from_curve[(src[1] >> 8) & 0xFF], | 55 srcTables[1][(src[1] >> 8) & 0xFF], |
51 linear_from_curve[(src[2] >> 8) & 0xFF], | 56 srcTables[1][(src[2] >> 8) & 0xFF], |
52 linear_from_curve[(src[3] >> 8) & 0xFF]}; | 57 srcTables[1][(src[3] >> 8) & 0xFF]}; |
53 blues = Sk4f{linear_from_curve[(src[0] >> 16) & 0xFF], | 58 blues = Sk4f{srcTables[2][(src[0] >> 16) & 0xFF], |
54 linear_from_curve[(src[1] >> 16) & 0xFF], | 59 srcTables[2][(src[1] >> 16) & 0xFF], |
55 linear_from_curve[(src[2] >> 16) & 0xFF], | 60 srcTables[2][(src[2] >> 16) & 0xFF], |
56 linear_from_curve[(src[3] >> 16) & 0xFF]}; | 61 srcTables[2][(src[3] >> 16) & 0xFF]}; |
57 src += 4; | 62 src += 4; |
58 len -= 4; | 63 len -= 4; |
59 }; | 64 }; |
60 | 65 |
61 Sk4f dstReds, dstGreens, dstBlues; | 66 Sk4f dstReds, dstGreens, dstBlues; |
62 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl
ues, &rXgXbX, | 67 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl
ues, &rXgXbX, |
63 &rYgYbY, &rZgZbZ] { | 68 &rYgYbY, &rZgZbZ] { |
64 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues; | 69 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues; |
65 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues; | 70 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues; |
66 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues; | 71 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues; |
67 }; | 72 }; |
68 | 73 |
69 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst] { | 74 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables] { |
70 dstReds = linear_to_curve(dstReds); | 75 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { |
71 dstGreens = linear_to_curve(dstGreens); | 76 Sk4f (*linear_to_curve)(const Sk4f&) = |
72 dstBlues = linear_to_curve(dstBlues); | 77 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : line
ar_to_2dot2; |
73 | 78 |
74 dstReds = clamp_0_to_255(dstReds); | 79 dstReds = linear_to_curve(dstReds); |
75 dstGreens = clamp_0_to_255(dstGreens); | 80 dstGreens = linear_to_curve(dstGreens); |
76 dstBlues = clamp_0_to_255(dstBlues); | 81 dstBlues = linear_to_curve(dstBlues); |
77 | 82 |
78 auto rgba = (Sk4i{(int)0xFF000000} ) | 83 dstReds = clamp_0_to_255(dstReds); |
79 | (SkNx_cast<int>(dstReds) ) | 84 dstGreens = clamp_0_to_255(dstGreens); |
80 | (SkNx_cast<int>(dstGreens) << 8) | 85 dstBlues = clamp_0_to_255(dstBlues); |
81 | (SkNx_cast<int>(dstBlues) << 16); | 86 |
82 rgba.store(dst); | 87 auto rgba = (SkNx_cast<int>(dstReds) ) |
| 88 | (SkNx_cast<int>(dstGreens) << 8) |
| 89 | (SkNx_cast<int>(dstBlues) << 16) |
| 90 | (Sk4i{ 0xFF << 24}); |
| 91 rgba.store(dst); |
| 92 } else { |
| 93 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0
f), 1023.0f); |
| 94 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0
f), 1023.0f); |
| 95 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0
f), 1023.0f); |
| 96 |
| 97 Sk4i indicesReds = SkNx_cast<int>(scaledReds + 0.5f); |
| 98 Sk4i indicesGreens = SkNx_cast<int>(scaledGreens + 0.5f); |
| 99 Sk4i indicesBlues = SkNx_cast<int>(scaledBlues + 0.5f); |
| 100 |
| 101 dst[0] = dstTables[0][indicesReds [0]] |
| 102 | dstTables[1][indicesGreens[0]] << 8 |
| 103 | dstTables[2][indicesBlues [0]] << 16 |
| 104 | 0xFF << 24; |
| 105 dst[1] = dstTables[0][indicesReds [1]] |
| 106 | dstTables[1][indicesGreens[1]] << 8 |
| 107 | dstTables[2][indicesBlues [1]] << 16 |
| 108 | 0xFF << 24; |
| 109 dst[2] = dstTables[0][indicesReds [2]] |
| 110 | dstTables[1][indicesGreens[2]] << 8 |
| 111 | dstTables[2][indicesBlues [2]] << 16 |
| 112 | 0xFF << 24; |
| 113 dst[3] = dstTables[0][indicesReds [3]] |
| 114 | dstTables[1][indicesGreens[3]] << 8 |
| 115 | dstTables[2][indicesBlues [3]] << 16 |
| 116 | 0xFF << 24; |
| 117 } |
| 118 |
83 dst += 4; | 119 dst += 4; |
84 }; | 120 }; |
85 | 121 |
86 load_next_4(); | 122 load_next_4(); |
87 | 123 |
88 while (len >= 4) { | 124 while (len >= 4) { |
89 transform_4(); | 125 transform_4(); |
90 load_next_4(); | 126 load_next_4(); |
91 store_4(); | 127 store_4(); |
92 } | 128 } |
93 | 129 |
94 transform_4(); | 130 transform_4(); |
95 store_4(); | 131 store_4(); |
96 } | 132 } |
97 | 133 |
98 while (len > 0) { | 134 while (len > 0) { |
99 // Splat r,g,b across a register each. | 135 // Splat r,g,b across a register each. |
100 auto r = Sk4f{linear_from_curve[(*src >> 0) & 0xFF]}, | 136 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, |
101 g = Sk4f{linear_from_curve[(*src >> 8) & 0xFF]}, | 137 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, |
102 b = Sk4f{linear_from_curve[(*src >> 16) & 0xFF]}; | 138 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; |
103 | 139 |
104 // Apply transformation matrix to dst gamut. | 140 // Apply transformation matrix to dst gamut. |
105 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; | 141 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; |
106 | 142 |
107 // Convert to dst gamma. | 143 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { |
108 dstPixel = linear_to_curve(dstPixel); | 144 Sk4f (*linear_to_curve)(const Sk4f&) = |
| 145 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_t
o_2dot2; |
109 | 146 |
110 // Clamp floats to byte range. | 147 dstPixel = linear_to_curve(dstPixel); |
111 dstPixel = clamp_0_to_255(dstPixel); | |
112 | 148 |
113 // Convert to bytes and store to memory. | 149 dstPixel = clamp_0_to_255(dstPixel); |
114 uint32_t rgba; | 150 |
115 SkNx_cast<uint8_t>(dstPixel).store(&rgba); | 151 uint32_t rgba; |
116 rgba |= 0xFF000000; | 152 SkNx_cast<uint8_t>(dstPixel).store(&rgba); |
117 *dst = rgba; | 153 rgba |= 0xFF000000; |
| 154 *dst = rgba; |
| 155 } else { |
| 156 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10
23.0f); |
| 157 |
| 158 Sk4i indices = SkNx_cast<int>(scaledPixel + 0.5f); |
| 159 |
| 160 *dst = dstTables[0][indices[0]] |
| 161 | dstTables[1][indices[1]] << 8 |
| 162 | dstTables[2][indices[2]] << 16 |
| 163 | 0xFF << 24; |
| 164 } |
118 | 165 |
119 dst += 1; | 166 dst += 1; |
120 src += 1; | 167 src += 1; |
121 len -= 1; | 168 len -= 1; |
122 } | 169 } |
123 } | 170 } |
124 | 171 |
125 static void color_xform_RGB1_srgb_to_2dot2(uint32_t* dst, const uint32_t* src, i
nt len, | 172 static void color_xform_RGB1_to_2dot2(uint32_t* dst, const uint32_t* src, int le
n, |
126 const float matrix[16]) { | 173 const float* const srcTables[3], const flo
at matrix[16]) { |
127 color_xform_RGB1<sk_linear_from_srgb, linear_to_2dot2>(dst, src, len, matrix
); | 174 color_xform_RGB1<k2Dot2_DstGamma>(dst, src, len, srcTables, matrix, nullptr)
; |
128 } | 175 } |
129 | 176 |
130 static void color_xform_RGB1_2dot2_to_2dot2(uint32_t* dst, const uint32_t* src,
int len, | 177 static void color_xform_RGB1_to_srgb(uint32_t* dst, const uint32_t* src, int len
, |
131 const float matrix[16]) { | 178 const float* const srcTables[3], const floa
t matrix[16]) { |
132 color_xform_RGB1<sk_linear_from_2dot2, linear_to_2dot2>(dst, src, len, matri
x); | 179 color_xform_RGB1<kSRGB_DstGamma>(dst, src, len, srcTables, matrix, nullptr); |
133 } | 180 } |
134 | 181 |
135 static void color_xform_RGB1_srgb_to_srgb(uint32_t* dst, const uint32_t* src, in
t len, | 182 static void color_xform_RGB1_to_table(uint32_t* dst, const uint32_t* src, int le
n, |
136 const float matrix[16]) { | 183 const float* const srcTables[3], const flo
at matrix[16], |
137 color_xform_RGB1<sk_linear_from_srgb, sk_linear_to_srgb>(dst, src, len, matr
ix); | 184 const uint8_t* const dstTables[3]) { |
138 } | 185 color_xform_RGB1<kTable_DstGamma>(dst, src, len, srcTables, matrix, dstTable
s); |
139 | |
140 static void color_xform_RGB1_2dot2_to_srgb(uint32_t* dst, const uint32_t* src, i
nt len, | |
141 const float matrix[16]) { | |
142 color_xform_RGB1<sk_linear_from_2dot2, sk_linear_to_srgb>(dst, src, len, mat
rix); | |
143 } | 186 } |
144 | 187 |
145 } // namespace SK_OPTS_NS | 188 } // namespace SK_OPTS_NS |
146 | 189 |
147 #endif // SkColorXform_opts_DEFINED | 190 #endif // SkColorXform_opts_DEFINED |
OLD | NEW |