reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 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 "Test.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkHalf.h" |
mtklein | a525cb1 | 2016-02-09 08:18:10 -0800 | [diff] [blame] | 11 | #include "SkOpts.h" |
reed | 3601f28 | 2016-02-05 11:18:39 -0800 | [diff] [blame] | 12 | #include "SkPixmap.h" |
| 13 | |
| 14 | static 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 | |
| 24 | static 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 | |
| 33 | DEF_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 | } |
mtklein | a525cb1 | 2016-02-09 08:18:10 -0800 | [diff] [blame] | 54 | |
| 55 | DEF_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 | } |