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

Side by Side Diff: src/opts/SkColorXform_opts.h

Issue 2162063003: Tune linear->sRGB constants to round-trip all bytes. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Perfect 131 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 | « src/core/SkSRGB.h ('k') | tests/SRGBTest.cpp » ('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 SkColorXform_opts_DEFINED 8 #ifndef SkColorXform_opts_DEFINED
9 #define SkColorXform_opts_DEFINED 9 #define SkColorXform_opts_DEFINED
10 10
11 #include "SkNx.h" 11 #include "SkNx.h"
12 #include "SkColorPriv.h" 12 #include "SkColorPriv.h"
13 #include "SkHalf.h" 13 #include "SkHalf.h"
14 #include "SkSRGB.h" 14 #include "SkSRGB.h"
15 #include "SkTemplates.h" 15 #include "SkTemplates.h"
16 16
17 namespace SK_OPTS_NS { 17 namespace SK_OPTS_NS {
18 18
19 static Sk4f linear_to_2dot2(const Sk4f& x) { 19 static Sk4f clamp_0_1(const Sk4f& x) {
20 // The order of the arguments is important here. We want to make sure that NaN
21 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN.
22 return Sk4f::Min(Sk4f::Max(x, 0.0f), 1.0f);
23 }
24
25 static Sk4i linear_to_2dot2(const Sk4f& x) {
20 // x^(29/64) is a very good approximation of the true value, x^(1/2.2). 26 // x^(29/64) is a very good approximation of the true value, x^(1/2.2).
21 auto x2 = x.rsqrt(), // x^(-1/2) 27 auto x2 = x.rsqrt(), // x^(-1/2)
22 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32) 28 x32 = x2.rsqrt().rsqrt().rsqrt().rsqrt(), // x^(-1/32)
23 x64 = x32.rsqrt(); // x^(+1/64) 29 x64 = x32.rsqrt(); // x^(+1/64)
24 30
25 // 29 = 32 - 2 - 1 31 // 29 = 32 - 2 - 1
26 return 255.0f * x2.invert() * x32 * x64.invert(); 32 return Sk4f_round(255.0f * x2.invert() * x32 * x64.invert());
27 }
28
29 static Sk4f clamp_0_to_255(const Sk4f& x) {
30 // The order of the arguments is important here. We want to make sure that NaN
31 // clamps to zero. Note that max(NaN, 0) = 0, while max(0, NaN) = NaN.
32 return Sk4f::Min(Sk4f::Max(x, 0.0f), 255.0f);
33 } 33 }
34 34
35 enum DstGamma { 35 enum DstGamma {
36 // 8888 36 // 8888
37 kSRGB_DstGamma, 37 kSRGB_DstGamma,
38 k2Dot2_DstGamma, 38 k2Dot2_DstGamma,
39 kTable_DstGamma, 39 kTable_DstGamma,
40 40
41 // F16 41 // F16
42 kLinear_DstGamma, 42 kLinear_DstGamma,
(...skipping 29 matching lines...) Expand all
72 Sk4f dstReds, dstGreens, dstBlues; 72 Sk4f dstReds, dstGreens, dstBlues;
73 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl ues, &rXgXbX, 73 auto transform_4 = [&reds, &greens, &blues, &dstReds, &dstGreens, &dstBl ues, &rXgXbX,
74 &rYgYbY, &rZgZbZ] { 74 &rYgYbY, &rZgZbZ] {
75 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues; 75 dstReds = rXgXbX[0]*reds + rYgYbY[0]*greens + rZgZbZ[0]*blues;
76 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues; 76 dstGreens = rXgXbX[1]*reds + rYgYbY[1]*greens + rZgZbZ[1]*blues;
77 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues; 77 dstBlues = rXgXbX[2]*reds + rYgYbY[2]*greens + rZgZbZ[2]*blues;
78 }; 78 };
79 79
80 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables] { 80 auto store_4 = [&dstReds, &dstGreens, &dstBlues, &dst, &dstTables] {
81 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { 81 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) {
82 Sk4f (*linear_to_curve)(const Sk4f&) = 82 Sk4i (*linear_to_curve)(const Sk4f&) =
83 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : line ar_to_2dot2; 83 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : line ar_to_2dot2;
84 84
85 dstReds = linear_to_curve(dstReds); 85 auto reds = linear_to_curve(clamp_0_1(dstReds));
86 dstGreens = linear_to_curve(dstGreens); 86 auto greens = linear_to_curve(clamp_0_1(dstGreens));
87 dstBlues = linear_to_curve(dstBlues); 87 auto blues = linear_to_curve(clamp_0_1(dstBlues));
88 88
89 dstReds = clamp_0_to_255(dstReds);
90 dstGreens = clamp_0_to_255(dstGreens);
91 dstBlues = clamp_0_to_255(dstBlues);
92 89
93 auto rgba = (Sk4f_round(dstReds) << SK_R32_SHIFT) 90 auto rgba = (reds << SK_R32_SHIFT)
94 | (Sk4f_round(dstGreens) << SK_G32_SHIFT) 91 | (greens << SK_G32_SHIFT)
95 | (Sk4f_round(dstBlues) << SK_B32_SHIFT) 92 | (blues << SK_B32_SHIFT)
96 | (Sk4i{ 0xFF << SK_A32_SHIFT}); 93 | (Sk4i{0xFF} << SK_A32_SHIFT);
97 rgba.store((uint32_t*) dst); 94 rgba.store((uint32_t*) dst);
98 95
99 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t)); 96 dst = SkTAddOffset<void>(dst, 4 * sizeof(uint32_t));
100 } else if (kTable_DstGamma == kDstGamma) { 97 } else if (kTable_DstGamma == kDstGamma) {
101 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0 f), 1023.0f); 98 Sk4f scaledReds = Sk4f::Min(Sk4f::Max(1023.0f * dstReds, 0.0 f), 1023.0f);
102 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0 f), 1023.0f); 99 Sk4f scaledGreens = Sk4f::Min(Sk4f::Max(1023.0f * dstGreens, 0.0 f), 1023.0f);
103 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0 f), 1023.0f); 100 Sk4f scaledBlues = Sk4f::Min(Sk4f::Max(1023.0f * dstBlues, 0.0 f), 1023.0f);
104 101
105 Sk4i indicesReds = Sk4f_round(scaledReds); 102 Sk4i indicesReds = Sk4f_round(scaledReds);
106 Sk4i indicesGreens = Sk4f_round(scaledGreens); 103 Sk4i indicesGreens = Sk4f_round(scaledGreens);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 145
149 while (len > 0) { 146 while (len > 0) {
150 // Splat r,g,b across a register each. 147 // Splat r,g,b across a register each.
151 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]}, 148 auto r = Sk4f{srcTables[0][(*src >> 0) & 0xFF]},
152 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]}, 149 g = Sk4f{srcTables[1][(*src >> 8) & 0xFF]},
153 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]}; 150 b = Sk4f{srcTables[2][(*src >> 16) & 0xFF]};
154 151
155 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b; 152 auto dstPixel = rXgXbX*r + rYgYbY*g + rZgZbZ*b;
156 153
157 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) { 154 if (kSRGB_DstGamma == kDstGamma || k2Dot2_DstGamma == kDstGamma) {
158 Sk4f (*linear_to_curve)(const Sk4f&) = 155 Sk4i (*linear_to_curve)(const Sk4f&) =
159 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_t o_2dot2; 156 (kSRGB_DstGamma == kDstGamma) ? sk_linear_to_srgb : linear_t o_2dot2;
160 157
161 dstPixel = linear_to_curve(dstPixel); 158 auto pixel = linear_to_curve(clamp_0_1(dstPixel));
162
163 dstPixel = clamp_0_to_255(dstPixel);
164 159
165 uint32_t rgba; 160 uint32_t rgba;
166 SkNx_cast<uint8_t>(Sk4f_round(dstPixel)).store(&rgba); 161 SkNx_cast<uint8_t>(pixel).store(&rgba);
167 rgba |= 0xFF000000; 162 rgba |= 0xFF000000;
168 *((uint32_t*) dst) = SkSwizzle_RGBA_to_PMColor(rgba); 163 *((uint32_t*) dst) = SkSwizzle_RGBA_to_PMColor(rgba);
169 dst = SkTAddOffset<void>(dst, sizeof(uint32_t)); 164 dst = SkTAddOffset<void>(dst, sizeof(uint32_t));
170 } else if (kTable_DstGamma == kDstGamma) { 165 } else if (kTable_DstGamma == kDstGamma) {
171 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10 23.0f); 166 Sk4f scaledPixel = Sk4f::Min(Sk4f::Max(1023.0f * dstPixel, 0.0f), 10 23.0f);
172 167
173 Sk4i indices = Sk4f_round(scaledPixel); 168 Sk4i indices = Sk4f_round(scaledPixel);
174 169
175 *((uint32_t*) dst) = dstTables[0][indices[0]] << SK_R32_SHIFT 170 *((uint32_t*) dst) = dstTables[0][indices[0]] << SK_R32_SHIFT
176 | dstTables[1][indices[1]] << SK_G32_SHIFT 171 | dstTables[1][indices[1]] << SK_G32_SHIFT
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 } 203 }
209 204
210 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l en, 205 static void color_xform_RGB1_to_linear(uint64_t* dst, const uint32_t* src, int l en,
211 const float* const srcTables[3], const fl oat matrix[12]) { 206 const float* const srcTables[3], const fl oat matrix[12]) {
212 color_xform_RGB1<kLinear_DstGamma>(dst, src, len, srcTables, matrix, nullptr ); 207 color_xform_RGB1<kLinear_DstGamma>(dst, src, len, srcTables, matrix, nullptr );
213 } 208 }
214 209
215 } // namespace SK_OPTS_NS 210 } // namespace SK_OPTS_NS
216 211
217 #endif // SkColorXform_opts_DEFINED 212 #endif // SkColorXform_opts_DEFINED
OLDNEW
« no previous file with comments | « src/core/SkSRGB.h ('k') | tests/SRGBTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698