blob: f437268c79faedacab7f02df01ebd2d33c917cff [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"
13
14static bool eq_within_half_float(float a, float b) {
15 const float kTolerance = 1.0f / (1 << (8 + 10));
16
17 SkHalf ha = SkFloatToHalf(a);
18 SkHalf hb = SkFloatToHalf(b);
19 float a2 = SkHalfToFloat(ha);
20 float b2 = SkHalfToFloat(hb);
21 return fabsf(a2 - b2) <= kTolerance;
22}
23
24static bool eq_within_half_float(const SkPM4f& a, const SkPM4f& b) {
25 for (int i = 0; i < 4; ++i) {
26 if (!eq_within_half_float(a.fVec[i], b.fVec[i])) {
27 return false;
28 }
29 }
30 return true;
31}
32
33DEF_TEST(color_half_float, reporter) {
34 const int w = 100;
35 const int h = 100;
36
37 SkImageInfo info = SkImageInfo::Make(w, h, kRGBA_F16_SkColorType, kPremul_SkAlphaType);
38
39 SkAutoPixmapStorage pm;
40 pm.alloc(info);
41 REPORTER_ASSERT(reporter, pm.getSafeSize() == SkToSizeT(w * h * sizeof(uint64_t)));
42
43 SkColor4f c4 { 0.5f, 1, 0.5f, 0.25f };
44 pm.erase(c4);
45
46 SkPM4f origpm4 = c4.premul();
47 for (int y = 0; y < pm.height(); ++y) {
48 for (int x = 0; x < pm.width(); ++x) {
49 SkPM4f pm4 = SkPM4f::FromF16(pm.addrF16(x, y));
50 REPORTER_ASSERT(reporter, eq_within_half_float(origpm4, pm4));
51 }
52 }
53}
mtkleina525cb12016-02-09 08:18:10 -080054
55DEF_TEST(float_to_half, reporter) {
56 const float fs[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
57 const uint16_t hs[] = { 0x3c00, 0x4000, 0x4200, 0x4400, 0x4500, 0x4600, 0x4700 };
58
59 uint16_t hscratch[7];
60 SkOpts::float_to_half(hscratch, fs, 7);
61 REPORTER_ASSERT(reporter, 0 == memcmp(hscratch, hs, sizeof(hs)));
62
63 float fscratch[7];
64 SkOpts::half_to_float(fscratch, hs, 7);
65 REPORTER_ASSERT(reporter, 0 == memcmp(fscratch, fs, sizeof(fs)));
66}