blob: f96d9045673a7b6313e0082e611452d3933f5576 [file] [log] [blame]
reed3601f282016-02-05 11:18:39 -08001/*
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 "Test.h"
9#include "SkColor.h"
10#include "SkHalf.h"
mtkleina525cb12016-02-09 08:18:10 -080011#include "SkOpts.h"
reed3601f282016-02-05 11:18:39 -080012#include "SkPixmap.h"
mtkleinfff055c2016-02-11 06:30:03 -080013#include "SkRandom.h"
reed3601f282016-02-05 11:18:39 -080014
15static bool eq_within_half_float(float a, float b) {
16 const float kTolerance = 1.0f / (1 << (8 + 10));
17
18 SkHalf ha = SkFloatToHalf(a);
19 SkHalf hb = SkFloatToHalf(b);
20 float a2 = SkHalfToFloat(ha);
21 float b2 = SkHalfToFloat(hb);
22 return fabsf(a2 - b2) <= kTolerance;
23}
24
25static bool eq_within_half_float(const SkPM4f& a, const SkPM4f& b) {
26 for (int i = 0; i < 4; ++i) {
27 if (!eq_within_half_float(a.fVec[i], b.fVec[i])) {
28 return false;
29 }
30 }
31 return true;
32}
33
34DEF_TEST(color_half_float, reporter) {
35 const int w = 100;
36 const int h = 100;
37
38 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
39
40 SkAutoPixmapStorage pm;
41 pm.alloc(info);
42 REPORTER_ASSERT(reporter, pm.getSafeSize() == SkToSizeT(w * h * sizeof(uint64_t)));
43
44 SkColor4f c4 { 0.5f, 1, 0.5f, 0.25f };
45 pm.erase(c4);
46
47 SkPM4f origpm4 = c4.premul();
48 for (int y = 0; y < pm.height(); ++y) {
49 for (int x = 0; x < pm.width(); ++x) {
50 SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y));
51 REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4));
52 }
53 }
54}
mtkleina525cb12016-02-09 08:18:10 -080055
56DEF_TEST(float_to_half, reporter) {
57 const float fs[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
58 const uint16_t hs[] = { 0x3c00, 0x4000, 0x4200, 0x4400, 0x4500, 0x4600, 0x4700 };
59
60 uint16_t hscratch[7];
61 SkOpts::float_to_half(hscratch, fs, 7);
62 REPORTER_ASSERT(reporter, 0 == memcmp(hscratch, hs, sizeof(hs)));
63
64 float fscratch[7];
65 SkOpts::half_to_float(fscratch, hs, 7);
66 REPORTER_ASSERT(reporter, 0 == memcmp(fscratch, fs, sizeof(fs)));
67}
mtkleinfff055c2016-02-11 06:30:03 -080068
69DEF_TEST(HalfToFloat_01, r) {
70 for (uint16_t h = 0; h < 0x8000; h++) {
71 float f = SkHalfToFloat(h);
72 if (f >= 0 && f <= 1) {
73 REPORTER_ASSERT(r, SkHalfToFloat_01(h)[0] == f);
74 REPORTER_ASSERT(r, SkFloatToHalf_01(SkHalfToFloat_01(h)) == h);
75 }
76 }
77}
78
79DEF_TEST(FloatToHalf_01, r) {
80#if 0
81 for (uint32_t bits = 0; bits < 0x80000000; bits++) {
82#else
83 SkRandom rand;
84 for (int i = 0; i < 1000000; i++) {
85 uint32_t bits = rand.nextU();
86#endif
87 float f;
88 memcpy(&f, &bits, 4);
89 if (f >= 0 && f <= 1) {
90 uint16_t h1 = (uint16_t)SkFloatToHalf_01(Sk4f(f,0,0,0)),
91 h2 = SkFloatToHalf(f);
92 bool ok = (h1 == h2 || h1 == h2-1);
93 REPORTER_ASSERT(r, ok);
94 if (!ok) {
95 SkDebugf("%08x (%d) -> %04x (%d), want %04x (%d)\n",
96 bits, bits>>23, h1, h1>>10, h2, h2>>10);
97 break;
98 }
99 }
100 }
101}