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 | |
| 20 | static bool almost_equal(float a, float b) { |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame^] | 21 | return SkTAbs(a - b) < 0.001f; |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 22 | } |
| 23 | |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame^] | 24 | DEF_TEST(ColorSpaceParsePngICCProfile, r) { |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 25 | SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png")); |
| 26 | REPORTER_ASSERT(r, nullptr != stream); |
| 27 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 28 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 29 | REPORTER_ASSERT(r, nullptr != codec); |
| 30 | |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame] | 31 | #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] | 32 | SkColorSpace* colorSpace = codec->getColorSpace(); |
| 33 | REPORTER_ASSERT(r, nullptr != colorSpace); |
| 34 | |
| 35 | // No need to use almost equal here. The color profile that we have extracted |
| 36 | // actually has a table of gammas. And our current implementation guesses 2.2f. |
| 37 | SkFloat3 gammas = colorSpace->gamma(); |
| 38 | REPORTER_ASSERT(r, 2.2f == gammas.fVec[0]); |
| 39 | REPORTER_ASSERT(r, 2.2f == gammas.fVec[1]); |
| 40 | REPORTER_ASSERT(r, 2.2f == gammas.fVec[2]); |
| 41 | |
| 42 | // These nine values were extracted from the color profile in isolation (before |
| 43 | // we embedded it in the png). Here we check that we still extract the same values. |
| 44 | SkFloat3x3 xyz = colorSpace->xyz(); |
| 45 | REPORTER_ASSERT(r, almost_equal(0.436066f, xyz.fMat[0])); |
| 46 | REPORTER_ASSERT(r, almost_equal(0.222488f, xyz.fMat[1])); |
| 47 | REPORTER_ASSERT(r, almost_equal(0.013916f, xyz.fMat[2])); |
| 48 | REPORTER_ASSERT(r, almost_equal(0.385147f, xyz.fMat[3])); |
| 49 | REPORTER_ASSERT(r, almost_equal(0.716873f, xyz.fMat[4])); |
| 50 | REPORTER_ASSERT(r, almost_equal(0.0970764f, xyz.fMat[5])); |
| 51 | REPORTER_ASSERT(r, almost_equal(0.143066f, xyz.fMat[6])); |
| 52 | REPORTER_ASSERT(r, almost_equal(0.0606079f, xyz.fMat[7])); |
| 53 | REPORTER_ASSERT(r, almost_equal(0.714096f, xyz.fMat[8])); |
msarett | 53add95 | 2016-03-07 17:25:12 -0800 | [diff] [blame] | 54 | #endif |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 55 | } |
msarett | 0e6274f | 2016-03-21 08:04:40 -0700 | [diff] [blame^] | 56 | |
| 57 | DEF_TEST(ColorSpaceParseJpegICCProfile, r) { |
| 58 | SkAutoTDelete<SkStream> stream(resource("icc-v2-gbr.jpg")); |
| 59 | REPORTER_ASSERT(r, nullptr != stream); |
| 60 | |
| 61 | SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); |
| 62 | REPORTER_ASSERT(r, nullptr != codec); |
| 63 | |
| 64 | SkColorSpace* colorSpace = codec->getColorSpace(); |
| 65 | REPORTER_ASSERT(r, nullptr != colorSpace); |
| 66 | |
| 67 | // It's important to use almost equal here. This profile sets gamma as |
| 68 | // 563 / 256, which actually comes out to about 2.19922. |
| 69 | SkFloat3 gammas = colorSpace->gamma(); |
| 70 | REPORTER_ASSERT(r, almost_equal(2.2f, gammas.fVec[0])); |
| 71 | REPORTER_ASSERT(r, almost_equal(2.2f, gammas.fVec[1])); |
| 72 | REPORTER_ASSERT(r, almost_equal(2.2f, gammas.fVec[2])); |
| 73 | |
| 74 | // These nine values were extracted from the color profile. Until we know any |
| 75 | // better, we'll assume these are the right values and test that we continue |
| 76 | // to extract them properly. |
| 77 | SkFloat3x3 xyz = colorSpace->xyz(); |
| 78 | REPORTER_ASSERT(r, almost_equal(0.385117f, xyz.fMat[0])); |
| 79 | REPORTER_ASSERT(r, almost_equal(0.716904f, xyz.fMat[1])); |
| 80 | REPORTER_ASSERT(r, almost_equal(0.0970612f, xyz.fMat[2])); |
| 81 | REPORTER_ASSERT(r, almost_equal(0.143051f, xyz.fMat[3])); |
| 82 | REPORTER_ASSERT(r, almost_equal(0.0606079f, xyz.fMat[4])); |
| 83 | REPORTER_ASSERT(r, almost_equal(0.713913f, xyz.fMat[5])); |
| 84 | REPORTER_ASSERT(r, almost_equal(0.436035f, xyz.fMat[6])); |
| 85 | REPORTER_ASSERT(r, almost_equal(0.222488f, xyz.fMat[7])); |
| 86 | REPORTER_ASSERT(r, almost_equal(0.013916f, xyz.fMat[8])); |
| 87 | } |