| 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; | 17 * - for linear -> sRGB, call sk_linear_to_srgb() for R,G,B; |
| 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() will run a little faster than usual when compiled with S
SE4.1+. | 20 * sk_linear_to_srgb() will run a little faster than usual when compiled with S
SE4.1+. |
| 21 */ | 21 */ |
| 22 | 22 |
| 23 extern const float sk_linear_from_srgb[256]; | 23 extern const float sk_linear_from_srgb[256]; |
| 24 extern const uint8_t sk_linear_to_srgb_table[8192]; |
| 24 | 25 |
| 25 static inline Sk4f sk_clamp_0_255(const Sk4f& x) { | 26 static inline Sk4f sk_clamp_0_255(const Sk4f& x) { |
| 26 // The order of the arguments is important here. We want to make sure that
NaN | 27 // The order of the arguments is important here. We want to make sure that
NaN |
| 27 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. | 28 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. |
| 28 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); | 29 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f); |
| 29 } | 30 } |
| 30 | 31 |
| 31 // This should probably only be called from sk_linear_to_srgb() or sk_linear_to_
srgb_noclamp(). | 32 // This should probably only be called from sk_linear_to_srgb() or sk_linear_to_
srgb_noclamp(). |
| 32 // It generally doesn't make sense to work with sRGB floats. | 33 // It generally doesn't make sense to work with sRGB floats. |
| 33 static inline Sk4f sk_linear_to_srgb_needs_trunc(const Sk4f& x) { | 34 static inline Sk4f sk_linear_to_srgb_needs_trunc(const Sk4f& x) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 56 | 57 |
| 57 static inline Sk4i sk_linear_to_srgb_noclamp(const Sk4f& x) { | 58 static inline Sk4i sk_linear_to_srgb_noclamp(const Sk4f& x) { |
| 58 Sk4f f = sk_linear_to_srgb_needs_trunc(x); | 59 Sk4f f = sk_linear_to_srgb_needs_trunc(x); |
| 59 for (int i = 0; i < 4; i++) { | 60 for (int i = 0; i < 4; i++) { |
| 60 SkASSERTF(0.0f <= f[i] && f[i] < 256.0f, "f[%d] was %g, outside [0,256)\
n", i, f[i]); | 61 SkASSERTF(0.0f <= f[i] && f[i] < 256.0f, "f[%d] was %g, outside [0,256)\
n", i, f[i]); |
| 61 } | 62 } |
| 62 return SkNx_cast<int>(f); | 63 return SkNx_cast<int>(f); |
| 63 } | 64 } |
| 64 | 65 |
| 65 #endif//SkSRGB_DEFINED | 66 #endif//SkSRGB_DEFINED |
| OLD | NEW |