blob: b1e4570f507ecd08ec09f1180c81f625f4a0f79c [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 Kleinc998f732017-05-24 19:00:47 -04008#include "SkPM4f.h"
Mike Kleinca2194b2017-05-15 18:03:52 -04009#include "SkRasterPipeline.h"
mtkleinac41bac2016-07-08 06:33:16 -070010#include "SkSRGB.h"
11#include "SkTypes.h"
12#include "Test.h"
13#include <math.h>
14
15static uint8_t linear_to_srgb(float l) {
mtklein566ea9b2016-07-20 12:10:11 -070016 return (uint8_t)sk_linear_to_srgb(Sk4f{l})[0];
mtkleinac41bac2016-07-08 06:33:16 -070017}
18
19DEF_TEST(sk_linear_to_srgb, r) {
mtklein566ea9b2016-07-20 12:10:11 -070020 // All bytes should round trip.
21 for (int i = 0; i < 256; i++) {
22 int actual = linear_to_srgb(sk_linear_from_srgb[i]);
23 if (i != actual) {
24 ERRORF(r, "%d -> %d\n", i, actual);
25 }
26 }
mtkleinac41bac2016-07-08 06:33:16 -070027
28 // Should be monotonic between 0 and 1.
mtkleinac41bac2016-07-08 06:33:16 -070029 uint8_t prev = 0;
mtklein566ea9b2016-07-20 12:10:11 -070030 for (float f = FLT_MIN; f <= 1.0f; ) { // We don't bother checking denorm values.
mtkleinac41bac2016-07-08 06:33:16 -070031 uint8_t srgb = linear_to_srgb(f);
32
mtklein566ea9b2016-07-20 12:10:11 -070033 REPORTER_ASSERT(r, srgb >= prev);
mtkleinac41bac2016-07-08 06:33:16 -070034 prev = srgb;
35
36 union { float flt; uint32_t bits; } pun = { f };
37 pun.bits++;
mtkleinaf064202016-08-08 05:52:55 -070038 SkDEBUGCODE(pun.bits += 127);
mtkleinac41bac2016-07-08 06:33:16 -070039 f = pun.flt;
40 }
41}
Mike Kleinca2194b2017-05-15 18:03:52 -040042
43DEF_TEST(sk_pipeline_srgb_roundtrip, r) {
44 uint32_t reds[256];
45 for (int i = 0; i < 256; i++) {
46 reds[i] = i;
47 }
48
49 auto ptr = (void*)reds;
50
Mike Kleinb24704d2017-05-24 07:53:00 -040051 SkRasterPipeline_<256> p;
Mike Kleinca2194b2017-05-15 18:03:52 -040052 p.append(SkRasterPipeline::load_8888, &ptr);
53 p.append_from_srgb(kUnpremul_SkAlphaType);
54 p.append(SkRasterPipeline::to_srgb);
55 p.append(SkRasterPipeline::store_8888, &ptr);
56
57 p.run(0,256);
58
59 for (int i = 0; i < 256; i++) {
60 if (reds[i] != (uint32_t)i) {
61 ERRORF(r, "%d doesn't round trip, %d", i, reds[i]);
62 }
63 }
64}
Mike Kleinc998f732017-05-24 19:00:47 -040065
66DEF_TEST(sk_pipeline_srgb_edge_cases, r) {
67 // We need to run at least 4 pixels to make sure we hit all specializations.
68 SkPM4f colors[4] = { {{0,1,1,1}}, {{0,0,0,0}}, {{0,0,0,0}}, {{0,0,0,0}} };
69 auto& color = colors[0];
70 void* dst = &color;
71
72 SkRasterPipeline_<256> p;
73 p.append(SkRasterPipeline::constant_color, &color);
74 p.append(SkRasterPipeline::to_srgb);
75 p.append(SkRasterPipeline::store_f32, &dst);
76 p.run(0,4);
77
78 if (color.r() != 0.0f) {
79 ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color.r());
80 }
81 if (color.g() != 1.0f) {
82 float f = color.g();
83 uint32_t x;
84 memcpy(&x, &f, 4);
85 ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color.g(), x);
86 }
87}