blob: 78855a34c8167ee542dcb63756a3a5f428d5dfd4 [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 Kleinca2194b2017-05-15 18:03:52 -04008#include "SkRasterPipeline.h"
mtkleinac41bac2016-07-08 06:33:16 -07009#include "SkSRGB.h"
10#include "SkTypes.h"
11#include "Test.h"
12#include <math.h>
13
14static uint8_t linear_to_srgb(float l) {
mtklein566ea9b2016-07-20 12:10:11 -070015 return (uint8_t)sk_linear_to_srgb(Sk4f{l})[0];
mtkleinac41bac2016-07-08 06:33:16 -070016}
17
18DEF_TEST(sk_linear_to_srgb, r) {
mtklein566ea9b2016-07-20 12:10:11 -070019 // All bytes should round trip.
20 for (int i = 0; i < 256; i++) {
21 int actual = linear_to_srgb(sk_linear_from_srgb[i]);
22 if (i != actual) {
23 ERRORF(r, "%d -> %d\n", i, actual);
24 }
25 }
mtkleinac41bac2016-07-08 06:33:16 -070026
27 // Should be monotonic between 0 and 1.
mtkleinac41bac2016-07-08 06:33:16 -070028 uint8_t prev = 0;
mtklein566ea9b2016-07-20 12:10:11 -070029 for (float f = FLT_MIN; f <= 1.0f; ) { // We don't bother checking denorm values.
mtkleinac41bac2016-07-08 06:33:16 -070030 uint8_t srgb = linear_to_srgb(f);
31
mtklein566ea9b2016-07-20 12:10:11 -070032 REPORTER_ASSERT(r, srgb >= prev);
mtkleinac41bac2016-07-08 06:33:16 -070033 prev = srgb;
34
35 union { float flt; uint32_t bits; } pun = { f };
36 pun.bits++;
mtkleinaf064202016-08-08 05:52:55 -070037 SkDEBUGCODE(pun.bits += 127);
mtkleinac41bac2016-07-08 06:33:16 -070038 f = pun.flt;
39 }
40}
Mike Kleinca2194b2017-05-15 18:03:52 -040041
42DEF_TEST(sk_pipeline_srgb_roundtrip, r) {
43 uint32_t reds[256];
44 for (int i = 0; i < 256; i++) {
45 reds[i] = i;
46 }
47
48 auto ptr = (void*)reds;
49
Mike Kleinb24704d2017-05-24 07:53:00 -040050 SkRasterPipeline_<256> p;
Mike Kleinca2194b2017-05-15 18:03:52 -040051 p.append(SkRasterPipeline::load_8888, &ptr);
52 p.append_from_srgb(kUnpremul_SkAlphaType);
53 p.append(SkRasterPipeline::to_srgb);
54 p.append(SkRasterPipeline::store_8888, &ptr);
55
56 p.run(0,256);
57
58 for (int i = 0; i < 256; i++) {
59 if (reds[i] != (uint32_t)i) {
60 ERRORF(r, "%d doesn't round trip, %d", i, reds[i]);
61 }
62 }
63}