blob: 37785e238b8b6c93938e85a8bea7209b6591e2c1 [file] [log] [blame]
msarett6a738212016-03-04 13:27:35 -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 "Resources.h"
9#include "SkCodec.h"
10#include "SkColorSpace.h"
11#include "Test.h"
12
msarett53add952016-03-07 17:25:12 -080013#include "png.h"
14
msarett6a738212016-03-04 13:27:35 -080015static SkStreamAsset* resource(const char path[]) {
16 SkString fullPath = GetResourcePath(path);
17 return SkStream::NewFromFile(fullPath.c_str());
18}
19
20static bool almost_equal(float a, float b) {
msarett0e6274f2016-03-21 08:04:40 -070021 return SkTAbs(a - b) < 0.001f;
msarett6a738212016-03-04 13:27:35 -080022}
23
msarett0e6274f2016-03-21 08:04:40 -070024DEF_TEST(ColorSpaceParsePngICCProfile, r) {
msarett6a738212016-03-04 13:27:35 -080025 SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png"));
26 REPORTER_ASSERT(r, nullptr != stream);
27
mtklein18300a32016-03-16 13:53:35 -070028 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett6a738212016-03-04 13:27:35 -080029 REPORTER_ASSERT(r, nullptr != codec);
30
msarett53add952016-03-07 17:25:12 -080031#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6)
msarett6a738212016-03-04 13:27:35 -080032 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]));
msarett53add952016-03-07 17:25:12 -080054#endif
msarett6a738212016-03-04 13:27:35 -080055}
msarett0e6274f2016-03-21 08:04:40 -070056
57DEF_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}