Flutter Engine
The Flutter Engine
SRGBTest.cpp
Go to the documentation of this file.
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
18#include "tests/Test.h"
19
20#include <cstdint>
21#include <cstring>
22
23DEF_TEST(srgb_roundtrip, r) {
24 uint32_t reds[256];
25 for (int i = 0; i < 256; i++) {
26 reds[i] = i;
27 }
28
29 SkRasterPipeline_MemoryCtx ptr = { reds, 0 };
30
32 linear = sRGB->makeLinearGamma();
34
35 SkColorSpaceXformSteps linearize{ sRGB.get(),upm, linear.get(),upm},
36 reencode {linear.get(),upm, sRGB.get(),upm};
37
39 p.append(SkRasterPipelineOp::load_8888, &ptr);
40 linearize.apply(&p);
41 reencode .apply(&p);
42 p.append(SkRasterPipelineOp::store_8888, &ptr);
43
44 p.run(0,0,256,1);
45
46 for (int i = 0; i < 256; i++) {
47 if (reds[i] != (uint32_t)i) {
48 ERRORF(r, "%d doesn't round trip, %u", i, reds[i]);
49 }
50 }
51}
52
53DEF_TEST(srgb_edge_cases, r) {
54 // We need to run at least 4 pixels to make sure we hit all specializations.
55 float colors[4][4] = { {0,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
56 auto& color = colors[0];
57
59
61 linear = sRGB->makeLinearGamma();
63
64 SkColorSpaceXformSteps steps {linear.get(),upm, sRGB.get(),upm};
65
67 SkRasterPipeline p(&alloc);
68 p.appendConstantColor(&alloc, color);
69 steps.apply(&p);
70 p.append(SkRasterPipelineOp::store_f32, &dst);
71 p.run(0,0,4,1);
72
73 if (color[0] != 0.0f) {
74 ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color[0]);
75 }
76 if (color[1] != 1.0f) {
77 float f = color[1];
78 uint32_t x;
79 memcpy(&x, &f, 4);
80 ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color[1], x);
81 }
82}
83
84// Linearize and then re-encode pixel values, testing that the output is close to the input.
85DEF_TEST(srgb_roundtrip_extended, r) {
86 static const int kSteps = 128;
87 SkColor4f rgba[kSteps];
88
89 auto expected = [=](int i) {
90 float scale = 10000.0f / (3*kSteps);
91 return SkColor4f{
92 (3*i+0) * scale,
93 (3*i+1) * scale,
94 (3*i+2) * scale,
95 1.0f,
96 };
97 };
98
99 for (int i = 0; i < kSteps; i++) {
100 rgba[i] = expected(i);
101 }
102
103 SkRasterPipeline_MemoryCtx ptr = { rgba, 0 };
104
108
109 SkColorSpaceXformSteps linearize{ cs.get(),upm, linear.get(),upm},
110 reencode {linear.get(),upm, cs.get(),upm};
111
113 p.append(SkRasterPipelineOp::load_f32, &ptr);
114 linearize.apply(&p);
115 reencode .apply(&p);
116 p.append(SkRasterPipelineOp::store_f32, &ptr);
117 p.run(0,0,kSteps,1);
118
119 auto close = [=](float x, float y) {
120 return x == y
121 || (x/y < 1.001f && y/x < 1.001f);
122 };
123
124 for (int i = 0; i < kSteps; i++) {
125 SkColor4f want = expected(i);
126 #if 0
127 SkDebugf("got %g %g %g, want %g %g %g\n",
128 rgba[i].fR, rgba[i].fG, rgba[i].fB,
129 want.fR, want.fG, want.fB);
130 #endif
131 REPORTER_ASSERT(r, close(rgba[i].fR, want.fR));
132 REPORTER_ASSERT(r, close(rgba[i].fG, want.fG));
133 REPORTER_ASSERT(r, close(rgba[i].fB, want.fB));
134 }
135}
kUnpremul_SkAlphaType
static const uint32_t rgba[kNumPixels]
DEF_TEST(srgb_roundtrip, r)
Definition: SRGBTest.cpp:23
SkAlphaType
Definition: SkAlphaType.h:26
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
#define ERRORF(r,...)
Definition: Test.h:293
sk_sp< SkColorSpace > makeLinearGamma() const
static sk_sp< SkColorSpace > MakeSRGB()
T * get() const
Definition: SkRefCnt.h:303
DlColor color
double y
double x
PODArray< SkColor > colors
Definition: SkRecords.h:276
dst
Definition: cp.py:12
const Scalar scale
static sk_sp< SkShader > linear(sk_sp< SkShader > shader)