blob: 2eea2ceeabc90ec27ccd65e2884d53a28b37e52e [file] [log] [blame]
mtkleinac41bac2016-07-08 06:33:16 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
9#include "src/core/SkColorSpaceXformSteps.h"
10#include "src/core/SkRasterPipeline.h"
11#include "tests/Test.h"
mtkleinac41bac2016-07-08 06:33:16 -070012
Hal Canary8a001442018-09-19 11:31:27 -040013#include <math.h>
14
Mike Klein06b802f2018-10-26 11:13:04 -040015DEF_TEST(srgb_roundtrip, r) {
Mike Kleinec8e0bf2020-05-22 11:42:38 -050016 uint32_t reds[256];
17 for (int i = 0; i < 256; i++) {
18 reds[i] = i;
19 }
Mike Kleinca2194b2017-05-15 18:03:52 -040020
Mike Kleinec8e0bf2020-05-22 11:42:38 -050021 SkRasterPipeline_MemoryCtx ptr = { reds, 0 };
Mike Kleinca2194b2017-05-15 18:03:52 -040022
Mike Kleinec8e0bf2020-05-22 11:42:38 -050023 sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(),
24 linear = sRGB->makeLinearGamma();
25 const SkAlphaType upm = kUnpremul_SkAlphaType;
Mike Klein06b802f2018-10-26 11:13:04 -040026
Mike Kleinec8e0bf2020-05-22 11:42:38 -050027 SkColorSpaceXformSteps linearize{ sRGB.get(),upm, linear.get(),upm},
28 reencode {linear.get(),upm, sRGB.get(),upm};
Mike Klein06b802f2018-10-26 11:13:04 -040029
Mike Kleinec8e0bf2020-05-22 11:42:38 -050030 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);
Mike Kleinca2194b2017-05-15 18:03:52 -040035
Mike Kleinec8e0bf2020-05-22 11:42:38 -050036 p.run(0,0,256,1);
Mike Kleinca2194b2017-05-15 18:03:52 -040037
Mike Kleinec8e0bf2020-05-22 11:42:38 -050038 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]);
Mike Kleinca2194b2017-05-15 18:03:52 -040041 }
42 }
43}
Mike Kleinc998f732017-05-24 19:00:47 -040044
Mike Klein06b802f2018-10-26 11:13:04 -040045DEF_TEST(srgb_edge_cases, r) {
Mike Kleinc998f732017-05-24 19:00:47 -040046 // We need to run at least 4 pixels to make sure we hit all specializations.
Brian Osman1cf418b2018-09-20 13:10:49 -040047 float colors[4][4] = { {0,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
Mike Kleinc998f732017-05-24 19:00:47 -040048 auto& color = colors[0];
Mike Klein45c16fa2017-07-18 18:15:13 -040049
Mike Kleinb11ab572018-10-24 06:42:14 -040050 SkRasterPipeline_MemoryCtx dst = { &color, 0 };
Mike Kleinc998f732017-05-24 19:00:47 -040051
Mike Klein06b802f2018-10-26 11:13:04 -040052 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
Mike Klein16776df2017-08-03 10:22:42 -040058 SkSTArenaAlloc<256> alloc;
59 SkRasterPipeline p(&alloc);
60 p.append_constant_color(&alloc, color);
Mike Kleinec8e0bf2020-05-22 11:42:38 -050061 steps.apply(&p);
Mike Kleinc998f732017-05-24 19:00:47 -040062 p.append(SkRasterPipeline::store_f32, &dst);
Mike Klein45c16fa2017-07-18 18:15:13 -040063 p.run(0,0,4,1);
Mike Kleinc998f732017-05-24 19:00:47 -040064
Brian Osman1cf418b2018-09-20 13:10:49 -040065 if (color[0] != 0.0f) {
66 ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color[0]);
Mike Kleinc998f732017-05-24 19:00:47 -040067 }
Brian Osman1cf418b2018-09-20 13:10:49 -040068 if (color[1] != 1.0f) {
69 float f = color[1];
Mike Kleinc998f732017-05-24 19:00:47 -040070 uint32_t x;
71 memcpy(&x, &f, 4);
Brian Osman1cf418b2018-09-20 13:10:49 -040072 ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color[1], x);
Mike Kleinc998f732017-05-24 19:00:47 -040073 }
74}
Mike Klein06b802f2018-10-26 11:13:04 -040075
76// Linearize and then re-encode pixel values, testing that the output is close to the input.
Mike Kleinec8e0bf2020-05-22 11:42:38 -050077DEF_TEST(srgb_roundtrip_extended, r) {
Mike Klein06b802f2018-10-26 11:13:04 -040078 static const int kSteps = 128;
79 SkColor4f rgba[kSteps];
80
81 auto expected = [=](int i) {
Mike Kleinec8e0bf2020-05-22 11:42:38 -050082 float scale = 10000.0f / (3*kSteps);
Mike Klein06b802f2018-10-26 11:13:04 -040083 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
Mike Kleinec8e0bf2020-05-22 11:42:38 -050097 sk_sp<SkColorSpace> cs = SkColorSpace::MakeSRGB();
Mike Klein06b802f2018-10-26 11:13:04 -040098 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);
Mike Kleinec8e0bf2020-05-22 11:42:38 -0500106 linearize.apply(&p);
107 reencode .apply(&p);
Mike Klein06b802f2018-10-26 11:13:04 -0400108 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
Mike Kleinec8e0bf2020-05-22 11:42:38 -0500113 || (x/y < 1.001f && y/x < 1.001f);
Mike Klein06b802f2018-10-26 11:13:04 -0400114 };
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}