blob: 5ae5a648a0cc20c81af505cd4a27096fe147685b [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
reed50d3b572016-05-03 12:13:21 -070015static bool almost_equal(float a, float b) {
16 return SkTAbs(a - b) < 0.001f;
17}
18
19static void test_space(skiatest::Reporter* r, SkColorSpace* space,
20 const float red[], const float green[], const float blue[],
21 const float expectedGammas[]) {
22#ifdef SK_DEBUG
23 const SkColorSpace::SkGammas& gammas = space->gammas();
24 REPORTER_ASSERT(r, almost_equal(expectedGammas[0], gammas.red()));
25 REPORTER_ASSERT(r, almost_equal(expectedGammas[1], gammas.green()));
26 REPORTER_ASSERT(r, almost_equal(expectedGammas[2], gammas.blue()));
27#endif
28
29 SkMatrix44 mat = space->xyz();
30 const float src[] = {
31 1, 0, 0, 1,
32 0, 1, 0, 1,
33 0, 0, 1, 1,
34 };
35 float dst[4];
36 for (int i = 0; i < 3; ++i) {
37 mat.mapScalars(&src[i*4], dst);
38 REPORTER_ASSERT(r, almost_equal(red[i], dst[0]));
39 REPORTER_ASSERT(r, almost_equal(green[i], dst[1]));
40 REPORTER_ASSERT(r, almost_equal(blue[i], dst[2]));
41 }
42}
43
44DEF_TEST(ColorSpace_sRGB, r) {
45 const float srgb_r[] = { 0.4358f, 0.2224f, 0.0139f };
46 const float srgb_g[] = { 0.3853f, 0.7170f, 0.0971f };
47 const float srgb_b[] = { 0.1430f, 0.0606f, 0.7139f };
48 const float srgb_gamma[] = { 2.2f, 2.2f, 2.2f };
49 test_space(r, SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named).get(),
50 srgb_r, srgb_g, srgb_b, srgb_gamma);
51
52}
53
msarett6a738212016-03-04 13:27:35 -080054static SkStreamAsset* resource(const char path[]) {
55 SkString fullPath = GetResourcePath(path);
56 return SkStream::NewFromFile(fullPath.c_str());
57}
58
msarett0e6274f2016-03-21 08:04:40 -070059DEF_TEST(ColorSpaceParsePngICCProfile, r) {
msarett6a738212016-03-04 13:27:35 -080060 SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png"));
61 REPORTER_ASSERT(r, nullptr != stream);
Brian Salomon92271fc2016-03-25 21:38:09 -040062 if (!stream) {
63 return;
64 }
msarett6a738212016-03-04 13:27:35 -080065
mtklein18300a32016-03-16 13:53:35 -070066 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
msarett6a738212016-03-04 13:27:35 -080067 REPORTER_ASSERT(r, nullptr != codec);
68
msarett53add952016-03-07 17:25:12 -080069#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6)
msarett6a738212016-03-04 13:27:35 -080070 SkColorSpace* colorSpace = codec->getColorSpace();
71 REPORTER_ASSERT(r, nullptr != colorSpace);
72
reed50d3b572016-05-03 12:13:21 -070073 const float red[] = { 0.436066f, 0.222488f, 0.013916f };
74 const float green[] = { 0.385147f, 0.716873f, 0.0970764f };
75 const float blue[] = { 0.143066f, 0.0606079f, 0.714096f };
76 const float gamma[] = { 0, 0, 0 }; // table-based gamma returns 0 from this its float-getter
77 test_space(r, colorSpace, red, green, blue, gamma);
msarett53add952016-03-07 17:25:12 -080078#endif
msarett6a738212016-03-04 13:27:35 -080079}
msarett0e6274f2016-03-21 08:04:40 -070080
81DEF_TEST(ColorSpaceParseJpegICCProfile, r) {
82 SkAutoTDelete<SkStream> stream(resource("icc-v2-gbr.jpg"));
83 REPORTER_ASSERT(r, nullptr != stream);
Brian Salomond2100f22016-03-25 17:02:20 -040084 if (!stream) {
85 return;
86 }
msarett0e6274f2016-03-21 08:04:40 -070087
88 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
89 REPORTER_ASSERT(r, nullptr != codec);
Brian Salomond2100f22016-03-25 17:02:20 -040090 if (!codec) {
91 return;
92 }
msarett0e6274f2016-03-21 08:04:40 -070093
94 SkColorSpace* colorSpace = codec->getColorSpace();
95 REPORTER_ASSERT(r, nullptr != colorSpace);
96
reed50d3b572016-05-03 12:13:21 -070097 const float red[] = { 0.385117f, 0.716904f, 0.0970612f };
98 const float green[] = { 0.143051f, 0.0606079f, 0.713913f };
99 const float blue[] = { 0.436035f, 0.222488f, 0.013916f };
100 const float gamma[] = { 2.2f, 2.2f, 2.2f };
101 test_space(r, colorSpace, red, green, blue, gamma);
msarett0e6274f2016-03-21 08:04:40 -0700102}