| 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 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 static Sk4f linear_to_2dot2(const Sk4f& x) { | 150 static Sk4f linear_to_2dot2(const Sk4f& x) { |
| 151 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). | 151 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). |
| 152 auto x2 = x.rsqrt(), // x^(-1/2) | 152 auto x2 = x.rsqrt(), // x^(-1/2) |
| 153 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) | 153 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) |
| 154 x64 = x32.rsqrt(); // x^(+1/64) | 154 x64 = x32.rsqrt(); // x^(+1/64) |
| 155 | 155 |
| 156 // 29 = 32 - 2 - 1 | 156 // 29 = 32 - 2 - 1 |
| 157 return 255.0f * x2.invert() * x32 * x64.invert(); | 157 return 255.0f * x2.invert() * x32 * x64.invert(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 static Sk4f linear_to_srgb(const Sk4f& x) { |
| 161 // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixe
ls). |
| 162 // For 0.00000f <= x < 0.00349f, 12.92 * x |
| 163 // For 0.00349f <= x <= 1.00000f, 0.679*(x.^0.5) + 0.423*x.^(0.25) - 0.10
1 |
| 164 // Note that 0.00349 was selected because it is a point where both functions
produce the |
| 165 // same pixel value when rounded. |
| 166 auto rsqrt = x.rsqrt(), |
| 167 sqrt = rsqrt.invert(), |
| 168 ftrt = rsqrt.rsqrt(); |
| 169 |
| 170 auto hi = (-0.101115084998961f * 255.0f) + |
| 171 (+0.678513029959381f * 255.0f) * sqrt + |
| 172 (+0.422602055039580f * 255.0f) * ftrt; |
| 173 |
| 174 auto lo = (12.92f * 255.0f) * x; |
| 175 |
| 176 auto mask = (x < 0.00349f); |
| 177 return mask.thenElse(lo, hi); |
| 178 } |
| 179 |
| 160 static Sk4f clamp_0_to_255(const Sk4f& x) { | 180 static Sk4f clamp_0_to_255(const Sk4f& x) { |
| 161 // The order of the arguments is important here. We want to make sure that
NaN | 181 // The order of the arguments is important here. We want to make sure that
NaN |
| 162 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. | 182 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. |
| 163 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); | 183 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); |
| 164 } | 184 } |
| 165 | 185 |
| 166 template <const float (&linear_from_curve)[256]> | 186 template <const float (&linear_from_curve)[256], Sk4f (*linear_to_curve)(const S
k4f&)> |
| 167 static void color_xform_RGB1(uint32_t* dst, const uint32_t* src, int len, | 187 static void color_xform_RGB1(uint32_t* dst, const uint32_t* src, int len, |
| 168 const float matrix[16]) { | 188 const float matrix[16]) { |
| 169 // Load transformation matrix. | 189 // Load transformation matrix. |
| 170 auto rXgXbX = Sk4f::Load(matrix + 0), | 190 auto rXgXbX = Sk4f::Load(matrix + 0), |
| 171 rYgYbY = Sk4f::Load(matrix + 4), | 191 rYgYbY = Sk4f::Load(matrix + 4), |
| 172 rZgZbZ = Sk4f::Load(matrix + 8); | 192 rZgZbZ = Sk4f::Load(matrix + 8); |
| 173 | 193 |
| 174 while (len >= 4) { | 194 while (len >= 4) { |
| 175 // Convert to linear. The look-up table has perfect accuracy. | 195 // Convert to linear. The look-up table has perfect accuracy. |
| 176 auto reds = Sk4f{linear_from_curve[(src[0] >> 0) & 0xFF], | 196 auto reds = Sk4f{linear_from_curve[(src[0] >> 0) & 0xFF], |
| 177 linear_from_curve[(src[1] >> 0) & 0xFF], | 197 linear_from_curve[(src[1] >> 0) & 0xFF], |
| 178 linear_from_curve[(src[2] >> 0) & 0xFF], | 198 linear_from_curve[(src[2] >> 0) & 0xFF], |
| 179 linear_from_curve[(src[3] >> 0) & 0xFF]}; | 199 linear_from_curve[(src[3] >> 0) & 0xFF]}; |
| 180 auto greens = Sk4f{linear_from_curve[(src[0] >> 8) & 0xFF], | 200 auto greens = Sk4f{linear_from_curve[(src[0] >> 8) & 0xFF], |
| 181 linear_from_curve[(src[1] >> 8) & 0xFF], | 201 linear_from_curve[(src[1] >> 8) & 0xFF], |
| 182 linear_from_curve[(src[2] >> 8) & 0xFF], | 202 linear_from_curve[(src[2] >> 8) & 0xFF], |
| 183 linear_from_curve[(src[3] >> 8) & 0xFF]}; | 203 linear_from_curve[(src[3] >> 8) & 0xFF]}; |
| 184 auto blues = Sk4f{linear_from_curve[(src[0] >> 16) & 0xFF], | 204 auto blues = Sk4f{linear_from_curve[(src[0] >> 16) & 0xFF], |
| 185 linear_from_curve[(src[1] >> 16) & 0xFF], | 205 linear_from_curve[(src[1] >> 16) & 0xFF], |
| 186 linear_from_curve[(src[2] >> 16) & 0xFF], | 206 linear_from_curve[(src[2] >> 16) & 0xFF], |
| 187 linear_from_curve[(src[3] >> 16) & 0xFF]}; | 207 linear_from_curve[(src[3] >> 16) & 0xFF]}; |
| 188 | 208 |
| 189 // Apply the transformation matrix to dst gamut. | 209 // Apply the transformation matrix to dst gamut. |
| 190 auto dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues, | 210 auto dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues, |
| 191 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues, | 211 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues, |
| 192 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues; | 212 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues; |
| 193 | 213 |
| 194 // Convert to dst gamma. | 214 // Convert to dst gamma. |
| 195 dstReds = linear_to_2dot2(dstReds); | 215 dstReds = linear_to_curve(dstReds); |
| 196 dstGreens = linear_to_2dot2(dstGreens); | 216 dstGreens = linear_to_curve(dstGreens); |
| 197 dstBlues = linear_to_2dot2(dstBlues); | 217 dstBlues = linear_to_curve(dstBlues); |
| 198 | 218 |
| 199 // Clamp floats to byte range. | 219 // Clamp floats to byte range. |
| 200 dstReds = clamp_0_to_255(dstReds); | 220 dstReds = clamp_0_to_255(dstReds); |
| 201 dstGreens = clamp_0_to_255(dstGreens); | 221 dstGreens = clamp_0_to_255(dstGreens); |
| 202 dstBlues = clamp_0_to_255(dstBlues); | 222 dstBlues = clamp_0_to_255(dstBlues); |
| 203 | 223 |
| 204 // Convert to bytes and store to memory. | 224 // Convert to bytes and store to memory. |
| 205 auto rgba = (Sk4i{(int)0xFF000000} ) | 225 auto rgba = (Sk4i{(int)0xFF000000} ) |
| 206 | (SkNx_cast<int>(dstReds) ) | 226 | (SkNx_cast<int>(dstReds) ) |
| 207 | (SkNx_cast<int>(dstGreens) << 8) | 227 | (SkNx_cast<int>(dstGreens) << 8) |
| 208 | (SkNx_cast<int>(dstBlues) << 16); | 228 | (SkNx_cast<int>(dstBlues) << 16); |
| 209 rgba.store(dst); | 229 rgba.store(dst); |
| 210 | 230 |
| 211 dst += 4; | 231 dst += 4; |
| 212 src += 4; | 232 src += 4; |
| 213 len -= 4; | 233 len -= 4; |
| 214 } | 234 } |
| 215 | 235 |
| 216 while (len > 0) { | 236 while (len > 0) { |
| 217 // Splat r,g,b across a register each. | 237 // Splat r,g,b across a register each. |
| 218 auto r = Sk4f{linear_from_curve[(*src >> 0) & 0xFF]}, | 238 auto r = Sk4f{linear_from_curve[(*src >> 0) & 0xFF]}, |
| 219 g = Sk4f{linear_from_curve[(*src >> 8) & 0xFF]}, | 239 g = Sk4f{linear_from_curve[(*src >> 8) & 0xFF]}, |
| 220 b = Sk4f{linear_from_curve[(*src >> 16) & 0xFF]}; | 240 b = Sk4f{linear_from_curve[(*src >> 16) & 0xFF]}; |
| 221 | 241 |
| 222 // Apply transformation matrix to dst gamut. | 242 // Apply transformation matrix to dst gamut. |
| 223 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; | 243 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; |
| 224 | 244 |
| 225 // Convert to dst gamma. | 245 // Convert to dst gamma. |
| 226 dstPixel = linear_to_2dot2(dstPixel); | 246 dstPixel = linear_to_curve(dstPixel); |
| 227 | 247 |
| 228 // Clamp floats to byte range. | 248 // Clamp floats to byte range. |
| 229 dstPixel = clamp_0_to_255(dstPixel); | 249 dstPixel = clamp_0_to_255(dstPixel); |
| 230 | 250 |
| 231 // Convert to bytes and store to memory. | 251 // Convert to bytes and store to memory. |
| 232 uint32_t rgba; | 252 uint32_t rgba; |
| 233 SkNx_cast<uint8_t>(dstPixel).store(&rgba); | 253 SkNx_cast<uint8_t>(dstPixel).store(&rgba); |
| 234 rgba |= 0xFF000000; | 254 rgba |= 0xFF000000; |
| 235 *dst = rgba; | 255 *dst = rgba; |
| 236 | 256 |
| 237 dst += 1; | 257 dst += 1; |
| 238 src += 1; | 258 src += 1; |
| 239 len -= 1; | 259 len -= 1; |
| 240 } | 260 } |
| 241 } | 261 } |
| 242 | 262 |
| 243 static void color_xform_RGB1_srgb_to_2dot2(uint32_t* dst, const uint32_t* src, i
nt len, | 263 static void color_xform_RGB1_srgb_to_2dot2(uint32_t* dst, const uint32_t* src, i
nt len, |
| 244 const float matrix[16]) { | 264 const float matrix[16]) { |
| 245 color_xform_RGB1<linear_from_srgb>(dst, src, len, matrix); | 265 color_xform_RGB1<linear_from_srgb, linear_to_2dot2>(dst, src, len, matrix); |
| 246 } | 266 } |
| 247 | 267 |
| 248 static void color_xform_RGB1_2dot2_to_2dot2(uint32_t* dst, const uint32_t* src,
int len, | 268 static void color_xform_RGB1_2dot2_to_2dot2(uint32_t* dst, const uint32_t* src,
int len, |
| 249 const float matrix[16]) { | 269 const float matrix[16]) { |
| 250 color_xform_RGB1<linear_from_2dot2>(dst, src, len, matrix); | 270 color_xform_RGB1<linear_from_2dot2, linear_to_2dot2>(dst, src, len, matrix); |
| 271 } |
| 272 |
| 273 static void color_xform_RGB1_srgb_to_srgb(uint32_t* dst, const uint32_t* src, in
t len, |
| 274 const float matrix[16]) { |
| 275 color_xform_RGB1<linear_from_srgb, linear_to_srgb>(dst, src, len, matrix); |
| 276 } |
| 277 |
| 278 static void color_xform_RGB1_2dot2_to_srgb(uint32_t* dst, const uint32_t* src, i
nt len, |
| 279 const float matrix[16]) { |
| 280 color_xform_RGB1<linear_from_2dot2, linear_to_srgb>(dst, src, len, matrix); |
| 251 } | 281 } |
| 252 | 282 |
| 253 } // namespace SK_OPTS_NS | 283 } // namespace SK_OPTS_NS |
| 254 | 284 |
| 255 #endif // SkColorXform_opts_DEFINED | 285 #endif // SkColorXform_opts_DEFINED |
| OLD | NEW |