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" |
| 11 | #include "Test.h" |
| 12 | |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame^] | 13 | #include "png.h" |
| 14 | |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 15 | static SkStreamAsset* resource(const char path[]) { |
| 16 | SkString fullPath = GetResourcePath(path); |
| 17 | return SkStream::NewFromFile(fullPath.c_str()); |
| 18 | } |
| 19 | |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame^] | 20 | #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] | 21 | static bool almost_equal(float a, float b) { |
| 22 | return SkTAbs(a - b) < 0.0001f; |
| 23 | } |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame^] | 24 | #endif |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 25 | |
| 26 | DEF_TEST(ColorSpaceParseICCProfile, r) { |
| 27 | SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png")); |
| 28 | REPORTER_ASSERT(r, nullptr != stream); |
| 29 | |
| 30 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.detach())); |
| 31 | REPORTER_ASSERT(r, nullptr != codec); |
| 32 | |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame^] | 33 | #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] | 34 | SkColorSpace* colorSpace = codec->getColorSpace(); |
| 35 | REPORTER_ASSERT(r, nullptr != colorSpace); |
| 36 | |
| 37 | // No need to use almost equal here. The color profile that we have extracted |
| 38 | // actually has a table of gammas. And our current implementation guesses 2.2f. |
| 39 | SkFloat3 gammas = colorSpace->gamma(); |
| 40 | REPORTER_ASSERT(r, 2.2f == gammas.fVec[0]); |
| 41 | REPORTER_ASSERT(r, 2.2f == gammas.fVec[1]); |
| 42 | REPORTER_ASSERT(r, 2.2f == gammas.fVec[2]); |
| 43 | |
| 44 | // These nine values were extracted from the color profile in isolation (before |
| 45 | // we embedded it in the png). Here we check that we still extract the same values. |
| 46 | SkFloat3x3 xyz = colorSpace->xyz(); |
| 47 | REPORTER_ASSERT(r, almost_equal(0.436066f, xyz.fMat[0])); |
| 48 | REPORTER_ASSERT(r, almost_equal(0.222488f, xyz.fMat[1])); |
| 49 | REPORTER_ASSERT(r, almost_equal(0.013916f, xyz.fMat[2])); |
| 50 | REPORTER_ASSERT(r, almost_equal(0.385147f, xyz.fMat[3])); |
| 51 | REPORTER_ASSERT(r, almost_equal(0.716873f, xyz.fMat[4])); |
| 52 | REPORTER_ASSERT(r, almost_equal(0.0970764f, xyz.fMat[5])); |
| 53 | REPORTER_ASSERT(r, almost_equal(0.143066f, xyz.fMat[6])); |
| 54 | REPORTER_ASSERT(r, almost_equal(0.0606079f, xyz.fMat[7])); |
| 55 | REPORTER_ASSERT(r, almost_equal(0.714096f, xyz.fMat[8])); |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame^] | 56 | #endif |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 57 | } |