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 #include "SkColorSpaceXformSteps.h"
9 #include "SkRasterPipeline.h"
10 #include "SkTypes.h"
11 #include "Test.h"
12
13 #include <math.h>
14
DEF_TEST(srgb_roundtrip,r)15 DEF_TEST(srgb_roundtrip, r) {
16 for (int normalized = 0; normalized < 2; normalized++) {
17 uint32_t reds[256];
18 for (int i = 0; i < 256; i++) {
19 reds[i] = i;
20 }
21
22 SkRasterPipeline_MemoryCtx ptr = { reds, 0 };
23
24 sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(),
25 linear = sRGB->makeLinearGamma();
26 const SkAlphaType upm = kUnpremul_SkAlphaType;
27
28 SkColorSpaceXformSteps linearize{ sRGB.get(),upm, linear.get(),upm},
29 reencode {linear.get(),upm, sRGB.get(),upm};
30
31 SkRasterPipeline_<256> p;
32 p.append(SkRasterPipeline::load_8888, &ptr);
33 linearize.apply(&p, !!normalized);
34 reencode .apply(&p, !!normalized);
35 p.append(SkRasterPipeline::store_8888, &ptr);
36
37 p.run(0,0,256,1);
38
39 for (int i = 0; i < 256; i++) {
40 if (reds[i] != (uint32_t)i) {
41 ERRORF(r, "%d doesn't round trip, %d", i, reds[i]);
42 }
43 }
44 }
45 }
46
DEF_TEST(srgb_edge_cases,r)47 DEF_TEST(srgb_edge_cases, r) {
48 // We need to run at least 4 pixels to make sure we hit all specializations.
49 float colors[4][4] = { {0,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
50 auto& color = colors[0];
51
52 SkRasterPipeline_MemoryCtx dst = { &color, 0 };
53
54 sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(),
55 linear = sRGB->makeLinearGamma();
56 const SkAlphaType upm = kUnpremul_SkAlphaType;
57
58 SkColorSpaceXformSteps steps {linear.get(),upm, sRGB.get(),upm};
59
60 SkSTArenaAlloc<256> alloc;
61 SkRasterPipeline p(&alloc);
62 p.append_constant_color(&alloc, color);
63 steps.apply(&p, true/*inputs are normalized*/);
64 p.append(SkRasterPipeline::store_f32, &dst);
65 p.run(0,0,4,1);
66
67 if (color[0] != 0.0f) {
68 ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color[0]);
69 }
70 if (color[1] != 1.0f) {
71 float f = color[1];
72 uint32_t x;
73 memcpy(&x, &f, 4);
74 ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color[1], x);
75 }
76 }
77
78 // Linearize and then re-encode pixel values, testing that the output is close to the input.
test_roundtripping(skiatest::Reporter * r,sk_sp<SkColorSpace> cs,float range,float tolerance,bool normalized)79 static void test_roundtripping(skiatest::Reporter* r,
80 sk_sp<SkColorSpace> cs,
81 float range,
82 float tolerance,
83 bool normalized) {
84 static const int kSteps = 128;
85 SkColor4f rgba[kSteps];
86
87 auto expected = [=](int i) {
88 float scale = range / (3*kSteps);
89 return SkColor4f{
90 (3*i+0) * scale,
91 (3*i+1) * scale,
92 (3*i+2) * scale,
93 1.0f,
94 };
95 };
96
97 for (int i = 0; i < kSteps; i++) {
98 rgba[i] = expected(i);
99 }
100
101 SkRasterPipeline_MemoryCtx ptr = { rgba, 0 };
102
103 sk_sp<SkColorSpace> linear = cs->makeLinearGamma();
104 const SkAlphaType upm = kUnpremul_SkAlphaType;
105
106 SkColorSpaceXformSteps linearize{ cs.get(),upm, linear.get(),upm},
107 reencode {linear.get(),upm, cs.get(),upm};
108
109 SkRasterPipeline_<256> p;
110 p.append(SkRasterPipeline::load_f32, &ptr);
111 linearize.apply(&p, normalized);
112 reencode .apply(&p, normalized);
113 p.append(SkRasterPipeline::store_f32, &ptr);
114 p.run(0,0,kSteps,1);
115
116 auto close = [=](float x, float y) {
117 return x == y
118 || (x/y < tolerance && y/x < tolerance);
119 };
120
121 for (int i = 0; i < kSteps; i++) {
122 SkColor4f want = expected(i);
123 #if 0
124 SkDebugf("got %g %g %g, want %g %g %g\n",
125 rgba[i].fR, rgba[i].fG, rgba[i].fB,
126 want.fR, want.fG, want.fB);
127 #endif
128 REPORTER_ASSERT(r, close(rgba[i].fR, want.fR));
129 REPORTER_ASSERT(r, close(rgba[i].fG, want.fG));
130 REPORTER_ASSERT(r, close(rgba[i].fB, want.fB));
131 }
132 }
133
DEF_TEST(srgb_roundtrip_extended,r)134 DEF_TEST(srgb_roundtrip_extended, r) {
135 // We're lying when we set normalized=true, but it allows us to test the to_srgb/from_srgb path.
136 test_roundtripping(r, SkColorSpace::MakeSRGB(), 2.0f, 1.025f, true);
137
138 // This normalized=false path should have much better round-tripping properties.
139 test_roundtripping(r, SkColorSpace::MakeSRGB(), 10000.0f, 1.001f, false);
140 }
141