mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SkSRGB_DEFINED |
| 9 | #define SkSRGB_DEFINED |
| 10 | |
| 11 | #include "SkNx.h" |
| 12 | |
| 13 | /** Components for building our canonical sRGB -> linear and linear -> sRGB transformations. |
| 14 | * |
| 15 | * Current best practices: |
| 16 | * - for sRGB -> linear, lookup R,G,B in sk_linear_from_srgb; |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 17 | * - for linear -> sRGB, call sk_linear_to_srgb() for R,G,B; |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 18 | * - the alpha channel is linear in both formats, needing at most *(1/255.0f) or *255.0f. |
| 19 | * |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 20 | * sk_linear_to_srgb() will run a little faster than usual when compiled with SSE4.1+. |
| 21 | */ |
| 22 | |
Matt Sarett | 08541e8 | 2017-03-08 16:30:18 -0500 | [diff] [blame] | 23 | extern const float sk_linear_from_srgb[256]; |
| 24 | extern const uint16_t sk_linear12_from_srgb[256]; |
| 25 | extern const uint8_t sk_linear12_to_srgb[4096]; |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 26 | |
Mike Klein | f0429db | 2017-11-01 11:52:10 -0400 | [diff] [blame] | 27 | // [0.0f, 1.0f] -> [0, 255]. |
| 28 | static inline Sk4i sk_linear_to_srgb(const Sk4f& x) { |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 29 | // Approximation of the sRGB gamma curve (within 1 when scaled to 8-bit pixels). |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 30 | // |
mtklein | b5acf6e | 2016-07-25 06:13:47 -0700 | [diff] [blame] | 31 | // Constants tuned by brute force to minimize (in order of importance) after truncation: |
| 32 | // 1) the number of bytes that fail to round trip (0 of 256); |
| 33 | // 2) the number of points in [FLT_MIN, 1.0f] that are non-monotonic (0 of ~1 billion); |
| 34 | // 3) the number of points halfway between bytes that hit the wrong byte (131 of 255). |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 35 | auto rsqrt = x.rsqrt(), |
| 36 | sqrt = rsqrt.invert(), |
| 37 | ftrt = rsqrt.rsqrt(); |
| 38 | |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 39 | auto lo = (13.0471f * 255.0f) * x; |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 40 | |
Mike Klein | f0429db | 2017-11-01 11:52:10 -0400 | [diff] [blame] | 41 | auto hi = SkNx_fma(Sk4f{+0.412999f * 255.0f}, ftrt, |
| 42 | SkNx_fma(Sk4f{+0.687999f * 255.0f}, sqrt, |
| 43 | Sk4f{-0.0974983f * 255.0f})); |
| 44 | auto s = (x < 0.0048f).thenElse(lo, hi); |
| 45 | |
| 46 | // Now clamp and truncate. |
| 47 | // The order of the arguments is important here. We want to make sure that NaN |
| 48 | // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN. |
| 49 | return SkNx_cast<int>(Sk4f::Min(Sk4f::Max(s, 0.0f), 255.0f)); |
mtklein | b5acf6e | 2016-07-25 06:13:47 -0700 | [diff] [blame] | 50 | } |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 51 | |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 52 | #endif//SkSRGB_DEFINED |