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 | |
| 8 | #include "SkSRGB.h" |
| 9 | #include "SkTypes.h" |
| 10 | #include "Test.h" |
| 11 | #include <math.h> |
| 12 | |
| 13 | static uint8_t linear_to_srgb(float l) { |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 14 | return (uint8_t)sk_linear_to_srgb(Sk4f{l})[0]; |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | DEF_TEST(sk_linear_to_srgb, r) { |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 18 | // All bytes should round trip. |
| 19 | for (int i = 0; i < 256; i++) { |
| 20 | int actual = linear_to_srgb(sk_linear_from_srgb[i]); |
| 21 | if (i != actual) { |
| 22 | ERRORF(r, "%d -> %d\n", i, actual); |
| 23 | } |
| 24 | } |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 25 | |
| 26 | // Should be monotonic between 0 and 1. |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 27 | uint8_t prev = 0; |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 28 | 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] | 29 | uint8_t srgb = linear_to_srgb(f); |
| 30 | |
mtklein | 566ea9b | 2016-07-20 12:10:11 -0700 | [diff] [blame] | 31 | REPORTER_ASSERT(r, srgb >= prev); |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 32 | prev = srgb; |
| 33 | |
| 34 | union { float flt; uint32_t bits; } pun = { f }; |
| 35 | pun.bits++; |
mtklein | af06420 | 2016-08-08 05:52:55 -0700 | [diff] [blame] | 36 | SkDEBUGCODE(pun.bits += 127); |
mtklein | ac41bac | 2016-07-08 06:33:16 -0700 | [diff] [blame] | 37 | f = pun.flt; |
| 38 | } |
| 39 | } |