blob: cda6fc410a8c545bd9d85621badc80c5eacb6988 [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>
Mike Klein45c16fa2017-07-18 18:15:13 -040014#include "../src/jumper/SkJumper.h"
mtkleinac41bac2016-07-08 06:33:16 -070015
16static uint8_t linear_to_srgb(float l) {
mtklein566ea9b2016-07-20 12:10:11 -070017 return (uint8_t)sk_linear_to_srgb(Sk4f{l})[0];
mtkleinac41bac2016-07-08 06:33:16 -070018}
19
20DEF_TEST(sk_linear_to_srgb, r) {
mtklein566ea9b2016-07-20 12:10:11 -070021 // 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 }
mtkleinac41bac2016-07-08 06:33:16 -070028
29 // Should be monotonic between 0 and 1.
mtkleinac41bac2016-07-08 06:33:16 -070030 uint8_t prev = 0;
mtklein566ea9b2016-07-20 12:10:11 -070031 for (float f = FLT_MIN; f <= 1.0f; ) { // We don't bother checking denorm values.
mtkleinac41bac2016-07-08 06:33:16 -070032 uint8_t srgb = linear_to_srgb(f);
33
mtklein566ea9b2016-07-20 12:10:11 -070034 REPORTER_ASSERT(r, srgb >= prev);
mtkleinac41bac2016-07-08 06:33:16 -070035 prev = srgb;
36
37 union { float flt; uint32_t bits; } pun = { f };
38 pun.bits++;
mtkleinaf064202016-08-08 05:52:55 -070039 SkDEBUGCODE(pun.bits += 127);
mtkleinac41bac2016-07-08 06:33:16 -070040 f = pun.flt;
41 }
42}
Mike Kleinca2194b2017-05-15 18:03:52 -040043
44DEF_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 Klein45c16fa2017-07-18 18:15:13 -040050 SkJumper_MemoryCtx ptr = { reds, 0 };
Mike Kleinca2194b2017-05-15 18:03:52 -040051
Mike Kleinb24704d2017-05-24 07:53:00 -040052 SkRasterPipeline_<256> p;
Mike Kleinca2194b2017-05-15 18:03:52 -040053 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 Klein45c16fa2017-07-18 18:15:13 -040058 p.run(0,0,256,1);
Mike Kleinca2194b2017-05-15 18:03:52 -040059
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 Kleinc998f732017-05-24 19:00:47 -040066
67DEF_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 Klein45c16fa2017-07-18 18:15:13 -040071
72 SkJumper_MemoryCtx dst = { &color, 0 };
Mike Kleinc998f732017-05-24 19:00:47 -040073
Mike Klein16776df2017-08-03 10:22:42 -040074 SkSTArenaAlloc<256> alloc;
75 SkRasterPipeline p(&alloc);
76 p.append_constant_color(&alloc, color);
Mike Kleinc998f732017-05-24 19:00:47 -040077 p.append(SkRasterPipeline::to_srgb);
78 p.append(SkRasterPipeline::store_f32, &dst);
Mike Klein45c16fa2017-07-18 18:15:13 -040079 p.run(0,0,4,1);
Mike Kleinc998f732017-05-24 19:00:47 -040080
81 if (color.r() != 0.0f) {
82 ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color.r());
83 }
84 if (color.g() != 1.0f) {
85 float f = color.g();
86 uint32_t x;
87 memcpy(&x, &f, 4);
88 ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color.g(), x);
89 }
90}