Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(361)

Side by Side Diff: src/core/SkSRGB.h

Issue 2173803002: Add clamp to sk_linear_to_srgb, reorder instructions (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Add a very important set of parentheses Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/opts/SkColorXform_opts.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 24
25 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 // 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 }
30
25 static inline Sk4i sk_linear_to_srgb(const Sk4f& x) { 31 static inline Sk4i sk_linear_to_srgb(const Sk4f& x) {
26 // 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).
27 // 33 //
28 // Tuned by brute force to minimize the number of bytes that fail to round t rip, 34 // Tuned by brute force to minimize the number of bytes that fail to round t rip,
29 // here 0 (of 256), and then to minimize the number of points halfway betwee n bytes 35 // here 0 (of 256), and then to minimize the number of points halfway betwee n bytes
30 // (in linear space) that fail to hit the right byte, here 131 (of 255), and to 36 // (in linear space) that fail to hit the right byte, here 131 (of 255), and to
31 // minimize the number of monotonicity regressions over the range [0,1], her e 0. 37 // minimize the number of monotonicity regressions over the range [0,1], her e 0.
32 38
33 auto rsqrt = x.rsqrt(), 39 auto rsqrt = x.rsqrt(),
34 sqrt = rsqrt.invert(), 40 sqrt = rsqrt.invert(),
35 ftrt = rsqrt.rsqrt(); 41 ftrt = rsqrt.rsqrt();
36 42
37 auto lo = (13.0471f * 255.0f) * x; 43 auto lo = (13.0471f * 255.0f) * x;
38 44
39 auto hi = (-0.0974983f * 255.0f) 45 auto hi = (-0.0974983f * 255.0f)
40 + (+0.687999f * 255.0f) * sqrt 46 + (+0.687999f * 255.0f) * sqrt
41 + (+0.412999f * 255.0f) * ftrt; 47 + (+0.412999f * 255.0f) * ftrt;
42 48
43 return SkNx_cast<int>( (x < 0.0048f).thenElse(lo, hi) ); 49 return SkNx_cast<int>(sk_clamp_0_255((x < 0.0048f).thenElse(lo, hi)));
44 } 50 }
45 51
46 #endif//SkSRGB_DEFINED 52 #endif//SkSRGB_DEFINED
OLDNEW
« no previous file with comments | « no previous file | src/opts/SkColorXform_opts.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698