msarett | 6a73821 | 2016-03-04 13:27:35 -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 "Resources.h" |
| 9 | #include "SkCodec.h" |
| 10 | #include "SkColorSpace.h" |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame^] | 11 | #include "SkColorSpacePriv.h" |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 12 | #include "Test.h" |
| 13 | |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame] | 14 | #include "png.h" |
| 15 | |
reed | 50d3b57 | 2016-05-03 12:13:21 -0700 | [diff] [blame] | 16 | static bool almost_equal(float a, float b) { |
| 17 | return SkTAbs(a - b) < 0.001f; |
| 18 | } |
| 19 | |
| 20 | static void test_space(skiatest::Reporter* r, SkColorSpace* space, |
| 21 | const float red[], const float green[], const float blue[], |
| 22 | const float expectedGammas[]) { |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame^] | 23 | |
| 24 | SkGammas* gammas = space->gammas(); |
| 25 | REPORTER_ASSERT(r, almost_equal(expectedGammas[0], gammas->fRed.fValue)); |
| 26 | REPORTER_ASSERT(r, almost_equal(expectedGammas[1], gammas->fGreen.fValue)); |
| 27 | REPORTER_ASSERT(r, almost_equal(expectedGammas[2], gammas->fBlue.fValue)); |
| 28 | |
reed | 50d3b57 | 2016-05-03 12:13:21 -0700 | [diff] [blame] | 29 | |
| 30 | SkMatrix44 mat = space->xyz(); |
| 31 | const float src[] = { |
| 32 | 1, 0, 0, 1, |
| 33 | 0, 1, 0, 1, |
| 34 | 0, 0, 1, 1, |
| 35 | }; |
| 36 | float dst[4]; |
| 37 | for (int i = 0; i < 3; ++i) { |
| 38 | mat.mapScalars(&src[i*4], dst); |
| 39 | REPORTER_ASSERT(r, almost_equal(red[i], dst[0])); |
| 40 | REPORTER_ASSERT(r, almost_equal(green[i], dst[1])); |
| 41 | REPORTER_ASSERT(r, almost_equal(blue[i], dst[2])); |
| 42 | } |
| 43 | } |
| 44 | |
msarett | 02125d1 | 2016-05-03 14:24:47 -0700 | [diff] [blame] | 45 | const float g_sRGB_XYZ[] = { 0.4358f, 0.2224f, 0.0139f, // R |
| 46 | 0.3853f, 0.7170f, 0.0971f, // G |
| 47 | 0.1430f, 0.0606f, 0.7139f }; // B |
| 48 | |
reed | 50d3b57 | 2016-05-03 12:13:21 -0700 | [diff] [blame] | 49 | DEF_TEST(ColorSpace_sRGB, r) { |
reed | 50d3b57 | 2016-05-03 12:13:21 -0700 | [diff] [blame] | 50 | const float srgb_gamma[] = { 2.2f, 2.2f, 2.2f }; |
| 51 | test_space(r, SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named).get(), |
msarett | 02125d1 | 2016-05-03 14:24:47 -0700 | [diff] [blame] | 52 | g_sRGB_XYZ, &g_sRGB_XYZ[3], &g_sRGB_XYZ[6], srgb_gamma); |
reed | 50d3b57 | 2016-05-03 12:13:21 -0700 | [diff] [blame] | 53 | |
| 54 | } |
| 55 | |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 56 | static SkStreamAsset* resource(const char path[]) { |
| 57 | SkString fullPath = GetResourcePath(path); |
| 58 | return SkStream::NewFromFile(fullPath.c_str()); |
| 59 | } |
| 60 | |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 61 | DEF_TEST(ColorSpaceParsePngICCProfile, r) { |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 62 | SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png")); |
| 63 | REPORTER_ASSERT(r, nullptr != stream); |
Brian Salomon | 92271fc | 2016-03-25 21:38:09 -0400 | [diff] [blame] | 64 | if (!stream) { |
| 65 | return; |
| 66 | } |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 67 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 68 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 69 | REPORTER_ASSERT(r, nullptr != codec); |
| 70 | |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame] | 71 | #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6) |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 72 | SkColorSpace* colorSpace = codec->getColorSpace(); |
| 73 | REPORTER_ASSERT(r, nullptr != colorSpace); |
| 74 | |
reed | 50d3b57 | 2016-05-03 12:13:21 -0700 | [diff] [blame] | 75 | const float red[] = { 0.436066f, 0.222488f, 0.013916f }; |
| 76 | const float green[] = { 0.385147f, 0.716873f, 0.0970764f }; |
| 77 | const float blue[] = { 0.143066f, 0.0606079f, 0.714096f }; |
msarett | 2c3eca3 | 2016-05-09 14:27:48 -0700 | [diff] [blame] | 78 | const float gamma[] = { 2.2f, 2.2f, 2.2f }; |
reed | 50d3b57 | 2016-05-03 12:13:21 -0700 | [diff] [blame] | 79 | test_space(r, colorSpace, red, green, blue, gamma); |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame] | 80 | #endif |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 81 | } |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 82 | |
| 83 | DEF_TEST(ColorSpaceParseJpegICCProfile, r) { |
| 84 | SkAutoTDelete<SkStream> stream(resource("icc-v2-gbr.jpg")); |
| 85 | REPORTER_ASSERT(r, nullptr != stream); |
Brian Salomon | d2100f2 | 2016-03-25 17:02:20 -0400 | [diff] [blame] | 86 | if (!stream) { |
| 87 | return; |
| 88 | } |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 89 | |
| 90 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
| 91 | REPORTER_ASSERT(r, nullptr != codec); |
Brian Salomon | d2100f2 | 2016-03-25 17:02:20 -0400 | [diff] [blame] | 92 | if (!codec) { |
| 93 | return; |
| 94 | } |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 95 | |
| 96 | SkColorSpace* colorSpace = codec->getColorSpace(); |
| 97 | REPORTER_ASSERT(r, nullptr != colorSpace); |
| 98 | |
reed | 50d3b57 | 2016-05-03 12:13:21 -0700 | [diff] [blame] | 99 | const float red[] = { 0.385117f, 0.716904f, 0.0970612f }; |
| 100 | const float green[] = { 0.143051f, 0.0606079f, 0.713913f }; |
| 101 | const float blue[] = { 0.436035f, 0.222488f, 0.013916f }; |
| 102 | const float gamma[] = { 2.2f, 2.2f, 2.2f }; |
| 103 | test_space(r, colorSpace, red, green, blue, gamma); |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame] | 104 | } |
msarett | 02125d1 | 2016-05-03 14:24:47 -0700 | [diff] [blame] | 105 | |
| 106 | DEF_TEST(ColorSpaceSRGBCompare, r) { |
| 107 | // Create an sRGB color space by name |
| 108 | sk_sp<SkColorSpace> namedColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
| 109 | |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame^] | 110 | |
msarett | 02125d1 | 2016-05-03 14:24:47 -0700 | [diff] [blame] | 111 | // Create an sRGB color space by value |
| 112 | SkMatrix44 srgbToxyzD50(SkMatrix44::kUninitialized_Constructor); |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame^] | 113 | float sRGBGammas[3] = { 2.2f, 2.2f, 2.2f }; |
msarett | 02125d1 | 2016-05-03 14:24:47 -0700 | [diff] [blame] | 114 | srgbToxyzD50.set3x3ColMajorf(g_sRGB_XYZ); |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame^] | 115 | sk_sp<SkColorSpace> rgbColorSpace = SkColorSpace::NewRGB(sRGBGammas, srgbToxyzD50); |
msarett | 02125d1 | 2016-05-03 14:24:47 -0700 | [diff] [blame] | 116 | REPORTER_ASSERT(r, namedColorSpace == namedColorSpace); |
| 117 | |
| 118 | // Change a single value from the sRGB matrix |
| 119 | srgbToxyzD50.set(2, 2, 0.5f); |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame^] | 120 | sk_sp<SkColorSpace> strangeColorSpace = SkColorSpace::NewRGB(sRGBGammas, srgbToxyzD50); |
msarett | 02125d1 | 2016-05-03 14:24:47 -0700 | [diff] [blame] | 121 | REPORTER_ASSERT(r, strangeColorSpace != namedColorSpace); |
| 122 | } |