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 | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 8 | #include "SkPM4f.h" |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 9 | #include "SkRasterPipeline.h" |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 10 | #include "SkSRGB.h" |
| 11 | #include "SkTypes.h" |
| 12 | #include "Test.h" |
| 13 | #include <math.h> |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 14 | #include "../src/jumper/SkJumper.h" |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 15 | |
| 16 | static uint8_t linear_to_srgb(float l) { |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 17 | return (uint8_t)sk_linear_to_srgb(Sk4f{l})[0]; |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | DEF_TEST(sk_linear_to_srgb, r) { |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 21 | // All bytes should round trip. |
| 22 | for (int i = 0; i < 256; i++) { |
| 23 | int actual = linear_to_srgb(sk_linear_from_srgb[i]); |
| 24 | if (i != actual) { |
| 25 | ERRORF(r, "%d -> %d\n", i, actual); |
| 26 | } |
| 27 | } |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 28 | |
| 29 | // Should be monotonic between 0 and 1. |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 30 | uint8_t prev = 0; |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 31 | for (float f = FLT_MIN; f <= 1.0f; ) { // We don't bother checking denorm values. |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 32 | uint8_t srgb = linear_to_srgb(f); |
| 33 | |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 34 | REPORTER_ASSERT(r, srgb >= prev); |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 35 | prev = srgb; |
| 36 | |
| 37 | union { float flt; uint32_t bits; } pun = { f }; |
| 38 | pun.bits++; |
mtklein | af06420 | 2016-08-08 05:52:55 -0700 | [diff] [blame] | 39 | SkDEBUGCODE(pun.bits += 127); |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 40 | f = pun.flt; |
| 41 | } |
| 42 | } |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 43 | |
| 44 | DEF_TEST(sk_pipeline_srgb_roundtrip, r) { |
| 45 | uint32_t reds[256]; |
| 46 | for (int i = 0; i < 256; i++) { |
| 47 | reds[i] = i; |
| 48 | } |
| 49 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 50 | SkJumper_MemoryCtx ptr = { reds, 0 }; |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 51 | |
Mike Klein | b24704d | 2017-05-24 07:53:00 -0400 | [diff] [blame] | 52 | SkRasterPipeline_<256> p; |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 53 | p.append(SkRasterPipeline::load_8888, &ptr); |
| 54 | p.append_from_srgb(kUnpremul_SkAlphaType); |
| 55 | p.append(SkRasterPipeline::to_srgb); |
| 56 | p.append(SkRasterPipeline::store_8888, &ptr); |
| 57 | |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 58 | p.run(0,0,256,1); |
Mike Klein | ca2194b | 2017-05-15 18:03:52 -0400 | [diff] [blame] | 59 | |
| 60 | for (int i = 0; i < 256; i++) { |
| 61 | if (reds[i] != (uint32_t)i) { |
| 62 | ERRORF(r, "%d doesn't round trip, %d", i, reds[i]); |
| 63 | } |
| 64 | } |
| 65 | } |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 66 | |
| 67 | DEF_TEST(sk_pipeline_srgb_edge_cases, r) { |
| 68 | // We need to run at least 4 pixels to make sure we hit all specializations. |
| 69 | SkPM4f colors[4] = { {{0,1,1,1}}, {{0,0,0,0}}, {{0,0,0,0}}, {{0,0,0,0}} }; |
| 70 | auto& color = colors[0]; |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 71 | |
| 72 | SkJumper_MemoryCtx dst = { &color, 0 }; |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 73 | |
| 74 | SkRasterPipeline_<256> p; |
Mike Reed | c91e387 | 2017-07-05 14:12:37 -0400 | [diff] [blame] | 75 | p.append(SkRasterPipeline::uniform_color, &color); |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 76 | p.append(SkRasterPipeline::to_srgb); |
| 77 | p.append(SkRasterPipeline::store_f32, &dst); |
Mike Klein | 45c16fa | 2017-07-18 18:15:13 -0400 | [diff] [blame] | 78 | p.run(0,0,4,1); |
Mike Klein | c998f73 | 2017-05-24 19:00:47 -0400 | [diff] [blame] | 79 | |
| 80 | if (color.r() != 0.0f) { |
| 81 | ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color.r()); |
| 82 | } |
| 83 | if (color.g() != 1.0f) { |
| 84 | float f = color.g(); |
| 85 | uint32_t x; |
| 86 | memcpy(&x, &f, 4); |
| 87 | ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color.g(), x); |
| 88 | } |
| 89 | } |