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