mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
| 9 | #include "src/core/SkColorSpaceXformSteps.h" |
| 10 | #include "src/core/SkRasterPipeline.h" |
| 11 | #include "tests/Test.h" |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 12 | |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 13 | #include <math.h> |
| 14 | |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 15 | DEF_TEST(srgb_roundtrip, r) { |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 16 | uint32_t reds[256]; |
| 17 | for (int i = 0; i < 256; i++) { |
| 18 | reds[i] = i; |
| 19 | } |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 20 | |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 21 | SkRasterPipeline_MemoryCtx ptr = { reds, 0 }; |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 22 | |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 23 | sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(), |
| 24 | linear = sRGB->makeLinearGamma(); |
| 25 | const SkAlphaType upm = kUnpremul_SkAlphaType; |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 26 | |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 27 | SkColorSpaceXformSteps linearize{ sRGB.get(),upm, linear.get(),upm}, |
| 28 | reencode {linear.get(),upm, sRGB.get(),upm}; |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 29 | |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 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); |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 35 | |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 36 | p.run(0,0,256,1); |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 37 | |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 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]); |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | } |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 44 | |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 45 | DEF_TEST(srgb_edge_cases, r) { |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 46 | // We need to run at least 4 pixels to make sure we hit all specializations. |
Brian Osman | 1cf418b | 2018-09-20 13:10:49 -0400 | [diff] [blame] | 47 | float colors[4][4] = { {0,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} }; |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 48 | auto& color = colors[0]; |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 49 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 50 | SkRasterPipeline_MemoryCtx dst = { &color, 0 }; |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 51 | |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 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 | |
Mike Klein | 16776df | 2017-08-03 10:22:42 -0400 | [diff] [blame] | 58 | SkSTArenaAlloc<256> alloc; |
| 59 | SkRasterPipeline p(&alloc); |
| 60 | p.append_constant_color(&alloc, color); |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 61 | steps.apply(&p); |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 62 | p.append(SkRasterPipeline::store_f32, &dst); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 63 | p.run(0,0,4,1); |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 64 | |
Brian Osman | 1cf418b | 2018-09-20 13:10:49 -0400 | [diff] [blame] | 65 | if (color[0] != 0.0f) { |
| 66 | ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color[0]); |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 67 | } |
Brian Osman | 1cf418b | 2018-09-20 13:10:49 -0400 | [diff] [blame] | 68 | if (color[1] != 1.0f) { |
| 69 | float f = color[1]; |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 70 | uint32_t x; |
| 71 | memcpy(&x, &f, 4); |
Brian Osman | 1cf418b | 2018-09-20 13:10:49 -0400 | [diff] [blame] | 72 | ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color[1], x); |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 73 | } |
| 74 | } |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 75 | |
| 76 | // Linearize and then re-encode pixel values, testing that the output is close to the input. |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 77 | DEF_TEST(srgb_roundtrip_extended, r) { |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 78 | static const int kSteps = 128; |
| 79 | SkColor4f rgba[kSteps]; |
| 80 | |
| 81 | auto expected = [=](int i) { |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 82 | float scale = 10000.0f / (3*kSteps); |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 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 | |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 97 | sk_sp<SkColorSpace> cs = SkColorSpace::MakeSRGB(); |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 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); |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 106 | linearize.apply(&p); |
| 107 | reencode .apply(&p); |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 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 |
Mike Klein | ec8e0bf | 2020-05-22 11:42:38 -0500 | [diff] [blame] | 113 | || (x/y < 1.001f && y/x < 1.001f); |
Mike Klein | 06b802f | 2018-10-26 11:13:04 -0400 | [diff] [blame] | 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 | } |