blob: 77c1fb75dedbc4d61378e21340fae4f06c2b31bd [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
8#include "SkSRGB.h"
9#include "SkTypes.h"
10#include "Test.h"
11#include <math.h>
12
13static uint8_t linear_to_srgb(float l) {
mtklein566ea9b2016-07-20 12:10:11 -070014 return (uint8_t)sk_linear_to_srgb(Sk4f{l})[0];
mtkleinac41bac2016-07-08 06:33:16 -070015}
16
17DEF_TEST(sk_linear_to_srgb, r) {
mtklein566ea9b2016-07-20 12:10:11 -070018 // 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 }
mtkleinac41bac2016-07-08 06:33:16 -070025
26 // Should be monotonic between 0 and 1.
mtkleinac41bac2016-07-08 06:33:16 -070027 uint8_t prev = 0;
mtklein566ea9b2016-07-20 12:10:11 -070028 for (float f = FLT_MIN; f <= 1.0f; ) { // We don't bother checking denorm values.
mtkleinac41bac2016-07-08 06:33:16 -070029 uint8_t srgb = linear_to_srgb(f);
30
mtklein566ea9b2016-07-20 12:10:11 -070031 REPORTER_ASSERT(r, srgb >= prev);
mtkleinac41bac2016-07-08 06:33:16 -070032 prev = srgb;
33
34 union { float flt; uint32_t bits; } pun = { f };
35 pun.bits++;
mtkleinaf064202016-08-08 05:52:55 -070036 SkDEBUGCODE(pun.bits += 127);
mtkleinac41bac2016-07-08 06:33:16 -070037 f = pun.flt;
38 }
39}