blob: 555f41ec6ac5b84fd58d4ec40c12b634baaa323b [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
msarett53add952016-03-07 17:25:12 -080020#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6)
msarett6a738212016-03-04 13:27:35 -080021static bool almost_equal(float a, float b) {
22 return SkTAbs(a - b) < 0.0001f;
23}
msarett53add952016-03-07 17:25:12 -080024#endif
msarett6a738212016-03-04 13:27:35 -080025
26DEF_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
msarett53add952016-03-07 17:25:12 -080033#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6)
msarett6a738212016-03-04 13:27:35 -080034 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]));
msarett53add952016-03-07 17:25:12 -080056#endif
msarett6a738212016-03-04 13:27:35 -080057}