| 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 SkSRGB_DEFINED | 8 #ifndef SkSRGB_DEFINED |
| 9 #define SkSRGB_DEFINED | 9 #define SkSRGB_DEFINED |
| 10 | 10 |
| 11 #include "SkNx.h" | 11 #include "SkNx.h" |
| 12 | 12 |
| 13 /** Components for building our canonical sRGB -> linear and linear -> sRGB tran
sformations. | 13 /** Components for building our canonical sRGB -> linear and linear -> sRGB tran
sformations. |
| 14 * | 14 * |
| 15 * Current best practices: | 15 * Current best practices: |
| 16 * - for sRGB -> linear, lookup R,G,B in sk_linear_from_srgb; | 16 * - for sRGB -> linear, lookup R,G,B in sk_linear_from_srgb; |
| 17 * - for linear -> sRGB, call sk_linear_to_srgb() for R,G,B, clamp to 255,
and round; | 17 * - for linear -> sRGB, call sk_linear_to_srgb() for R,G,B, and round; |
| 18 * - the alpha channel is linear in both formats, needing at most *(1/255.0
f) or *255.0f. | 18 * - the alpha channel is linear in both formats, needing at most *(1/255.0
f) or *255.0f. |
| 19 * | 19 * |
| 20 * sk_linear_to_srgb()'s output requires rounding; it does not round for you. | 20 * sk_linear_to_srgb()'s output requires rounding; it does not round for you. |
| 21 * | 21 * |
| 22 * Given inputs in [0,1], sk_linear_to_srgb() will not underflow 0 but may over
flow 255. | 22 * Given inputs in [0,1], sk_linear_to_srgb() will not underflow 0 but may over
flow 255. |
| 23 * The overflow is small enough that you can safely either clamp then round or
round then clamp. | 23 * The overflow is small enough to be handled by rounding. |
| 24 * (If you don't trust the inputs are in [0,1], you'd better clamp both sides i
mmediately.) | 24 * (But if you don't trust the inputs are in [0,1], you'd better clamp both sid
es immediately.) |
| 25 * | 25 * |
| 26 * sk_linear_to_srgb() will run a little faster than usual when compiled with S
SE4.1+. | 26 * sk_linear_to_srgb() will run a little faster than usual when compiled with S
SE4.1+. |
| 27 */ | 27 */ |
| 28 | 28 |
| 29 extern const float sk_linear_from_srgb[256]; | 29 extern const float sk_linear_from_srgb[256]; |
| 30 | 30 |
| 31 static inline Sk4f sk_linear_to_srgb(const Sk4f& x) { | 31 static inline Sk4f sk_linear_to_srgb(const Sk4f& x) { |
| 32 // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixe
ls). | 32 // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixe
ls). |
| 33 // For 0.00000f <= x < 0.00349f, 12.92 * x | 33 // For 0.00000f <= x < 0.00349f, 12.92 * x |
| 34 // For 0.00349f <= x <= 1.00000f, 0.679*(x.^0.5) + 0.423*x.^(0.25) - 0.10
1 | 34 // For 0.00349f <= x <= 1.00000f, 0.679*(x.^0.5) + 0.423*x.^(0.25) - 0.10
1 |
| 35 // Note that 0.00349 was selected because it is a point where both functions
produce the | 35 // Note that 0.00349 was selected because it is a point where both functions
produce the |
| 36 // same pixel value when rounded. | 36 // same pixel value when rounded. |
| 37 auto rsqrt = x.rsqrt(), | 37 auto rsqrt = x.rsqrt(), |
| 38 sqrt = rsqrt.invert(), | 38 sqrt = rsqrt.invert(), |
| 39 ftrt = rsqrt.rsqrt(); | 39 ftrt = rsqrt.rsqrt(); |
| 40 | 40 |
| 41 auto lo = (12.92f * 255.0f) * x; | 41 auto lo = (12.92f * 255.0f) * x; |
| 42 | 42 |
| 43 auto hi = (-0.101115084998961f * 255.0f) + | 43 auto hi = (-0.101115084998961f * 255.0f) + |
| 44 (+0.678513029959381f * 255.0f) * sqrt + | 44 (+0.678513029959381f * 255.0f) * sqrt + |
| 45 (+0.422602055039580f * 255.0f) * ftrt; | 45 (+0.422602055039580f * 255.0f) * ftrt; |
| 46 | 46 |
| 47 return (x < 0.00349f).thenElse(lo, hi); | 47 return (x < 0.00349f).thenElse(lo, hi); |
| 48 } | 48 } |
| 49 | 49 |
| 50 #endif//SkSRGB_DEFINED | 50 #endif//SkSRGB_DEFINED |
| OLD | NEW |