SkRasterPipeline in SkArenaAlloc
[platform/upstream/libSkiaSharp.git] / tests / SRGBTest.cpp
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 "SkRasterPipeline.h"
9 #include "SkSRGB.h"
10 #include "SkTypes.h"
11 #include "Test.h"
12 #include <math.h>
13
14 static uint8_t linear_to_srgb(float l) {
15     return (uint8_t)sk_linear_to_srgb(Sk4f{l})[0];
16 }
17
18 DEF_TEST(sk_linear_to_srgb, r) {
19     // 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     }
26
27     // Should be monotonic between 0 and 1.
28     uint8_t prev = 0;
29     for (float f = FLT_MIN; f <= 1.0f; ) {  // We don't bother checking denorm values.
30         uint8_t srgb = linear_to_srgb(f);
31
32         REPORTER_ASSERT(r, srgb >= prev);
33         prev = srgb;
34
35         union { float flt; uint32_t bits; } pun = { f };
36         pun.bits++;
37         SkDEBUGCODE(pun.bits += 127);
38         f = pun.flt;
39     }
40 }
41
42 DEF_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
50     SkRasterPipeline_<256> p;
51     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 }