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 "SkPM4f.h"
9 #include "SkRasterPipeline.h"
10 #include "SkSRGB.h"
11 #include "SkTypes.h"
12 #include "Test.h"
13 #include <math.h>
14 #include "../src/jumper/SkJumper.h"
15
linear_to_srgb(float l)16 static uint8_t linear_to_srgb(float l) {
17 return (uint8_t)sk_linear_to_srgb(Sk4f{l})[0];
18 }
19
DEF_TEST(sk_linear_to_srgb,r)20 DEF_TEST(sk_linear_to_srgb, r) {
21 // 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 }
28
29 // Should be monotonic between 0 and 1.
30 uint8_t prev = 0;
31 for (float f = FLT_MIN; f <= 1.0f; ) { // We don't bother checking denorm values.
32 uint8_t srgb = linear_to_srgb(f);
33
34 REPORTER_ASSERT(r, srgb >= prev);
35 prev = srgb;
36
37 union { float flt; uint32_t bits; } pun = { f };
38 pun.bits++;
39 SkDEBUGCODE(pun.bits += 127);
40 f = pun.flt;
41 }
42 }
43
DEF_TEST(sk_pipeline_srgb_roundtrip,r)44 DEF_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
50 SkJumper_MemoryCtx ptr = { reds, 0 };
51
52 SkRasterPipeline_<256> p;
53 p.append(SkRasterPipeline::load_8888, &ptr);
54 p.append(SkRasterPipeline::from_srgb);
55 p.append(SkRasterPipeline::to_srgb);
56 p.append(SkRasterPipeline::store_8888, &ptr);
57
58 p.run(0,0,256,1);
59
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 }
66
DEF_TEST(sk_pipeline_srgb_edge_cases,r)67 DEF_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];
71
72 SkJumper_MemoryCtx dst = { &color, 0 };
73
74 SkSTArenaAlloc<256> alloc;
75 SkRasterPipeline p(&alloc);
76 p.append_constant_color(&alloc, color);
77 p.append(SkRasterPipeline::to_srgb);
78 p.append(SkRasterPipeline::store_f32, &dst);
79 p.run(0,0,4,1);
80
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 }
91