Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: tests/ColorSpaceTest.cpp

Issue 1943833002: return 4x4 matrix from SkColorSpace (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: fix redirect header Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/utils/SkMatrix44.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "Resources.h" 8 #include "Resources.h"
9 #include "SkCodec.h" 9 #include "SkCodec.h"
10 #include "SkColorSpace.h" 10 #include "SkColorSpace.h"
11 #include "Test.h" 11 #include "Test.h"
12 12
13 #include "png.h" 13 #include "png.h"
14 14
15 static bool almost_equal(float a, float b) {
16 return SkTAbs(a - b) < 0.001f;
17 }
18
19 static 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
44 DEF_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
15 static SkStreamAsset* resource(const char path[]) { 54 static SkStreamAsset* resource(const char path[]) {
16 SkString fullPath = GetResourcePath(path); 55 SkString fullPath = GetResourcePath(path);
17 return SkStream::NewFromFile(fullPath.c_str()); 56 return SkStream::NewFromFile(fullPath.c_str());
18 } 57 }
19 58
20 static bool almost_equal(float a, float b) {
21 return SkTAbs(a - b) < 0.001f;
22 }
23
24 DEF_TEST(ColorSpaceParsePngICCProfile, r) { 59 DEF_TEST(ColorSpaceParsePngICCProfile, r) {
25 SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png")); 60 SkAutoTDelete<SkStream> stream(resource("color_wheel_with_profile.png"));
26 REPORTER_ASSERT(r, nullptr != stream); 61 REPORTER_ASSERT(r, nullptr != stream);
27 if (!stream) { 62 if (!stream) {
28 return; 63 return;
29 } 64 }
30 65
31 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); 66 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
32 REPORTER_ASSERT(r, nullptr != codec); 67 REPORTER_ASSERT(r, nullptr != codec);
33 68
34 #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_M INOR >= 6) 69 #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_M INOR >= 6)
35 SkColorSpace* colorSpace = codec->getColorSpace(); 70 SkColorSpace* colorSpace = codec->getColorSpace();
36 REPORTER_ASSERT(r, nullptr != colorSpace); 71 REPORTER_ASSERT(r, nullptr != colorSpace);
37 72
38 // The color profile that we have extracted has represents gamma with a look up table. 73 const float red[] = { 0.436066f, 0.222488f, 0.013916f };
39 // So we expect the gamma value to be zero. 74 const float green[] = { 0.385147f, 0.716873f, 0.0970764f };
40 #ifdef SK_DEBUG 75 const float blue[] = { 0.143066f, 0.0606079f, 0.714096f };
41 const SkColorSpace::SkGammas& gammas = colorSpace->gammas(); 76 const float gamma[] = { 0, 0, 0 }; // table-based gamma returns 0 from this its float-getter
42 REPORTER_ASSERT(r, 0.0f == gammas.red()); 77 test_space(r, colorSpace, red, green, blue, gamma);
43 REPORTER_ASSERT(r, 0.0f == gammas.green());
44 REPORTER_ASSERT(r, 0.0f == gammas.blue());
45 #endif
46
47 // These nine values were extracted from the color profile in isolation (bef ore
48 // we embedded it in the png). Here we check that we still extract the same values.
49 SkFloat3x3 xyz = colorSpace->xyz();
50 REPORTER_ASSERT(r, almost_equal(0.436066f, xyz.fMat[0]));
51 REPORTER_ASSERT(r, almost_equal(0.222488f, xyz.fMat[1]));
52 REPORTER_ASSERT(r, almost_equal(0.013916f, xyz.fMat[2]));
53 REPORTER_ASSERT(r, almost_equal(0.385147f, xyz.fMat[3]));
54 REPORTER_ASSERT(r, almost_equal(0.716873f, xyz.fMat[4]));
55 REPORTER_ASSERT(r, almost_equal(0.0970764f, xyz.fMat[5]));
56 REPORTER_ASSERT(r, almost_equal(0.143066f, xyz.fMat[6]));
57 REPORTER_ASSERT(r, almost_equal(0.0606079f, xyz.fMat[7]));
58 REPORTER_ASSERT(r, almost_equal(0.714096f, xyz.fMat[8]));
59 #endif 78 #endif
60 } 79 }
61 80
62 DEF_TEST(ColorSpaceParseJpegICCProfile, r) { 81 DEF_TEST(ColorSpaceParseJpegICCProfile, r) {
63 SkAutoTDelete<SkStream> stream(resource("icc-v2-gbr.jpg")); 82 SkAutoTDelete<SkStream> stream(resource("icc-v2-gbr.jpg"));
64 REPORTER_ASSERT(r, nullptr != stream); 83 REPORTER_ASSERT(r, nullptr != stream);
65 if (!stream) { 84 if (!stream) {
66 return; 85 return;
67 } 86 }
68 87
69 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release())); 88 SkAutoTDelete<SkCodec> codec(SkCodec::NewFromStream(stream.release()));
70 REPORTER_ASSERT(r, nullptr != codec); 89 REPORTER_ASSERT(r, nullptr != codec);
71 if (!codec) { 90 if (!codec) {
72 return; 91 return;
73 } 92 }
74 93
75 SkColorSpace* colorSpace = codec->getColorSpace(); 94 SkColorSpace* colorSpace = codec->getColorSpace();
76 REPORTER_ASSERT(r, nullptr != colorSpace); 95 REPORTER_ASSERT(r, nullptr != colorSpace);
77 96
78 // It's important to use almost equal here. This profile sets gamma as 97 const float red[] = { 0.385117f, 0.716904f, 0.0970612f };
79 // 563 / 256, which actually comes out to about 2.19922. 98 const float green[] = { 0.143051f, 0.0606079f, 0.713913f };
80 #ifdef SK_DEBUG 99 const float blue[] = { 0.436035f, 0.222488f, 0.013916f };
81 const SkColorSpace::SkGammas& gammas = colorSpace->gammas(); 100 const float gamma[] = { 2.2f, 2.2f, 2.2f };
82 REPORTER_ASSERT(r, almost_equal(2.2f, gammas.red())); 101 test_space(r, colorSpace, red, green, blue, gamma);
83 REPORTER_ASSERT(r, almost_equal(2.2f, gammas.green()));
84 REPORTER_ASSERT(r, almost_equal(2.2f, gammas.blue()));
85 #endif
86
87 // These nine values were extracted from the color profile. Until we know a ny
88 // better, we'll assume these are the right values and test that we continue
89 // to extract them properly.
90 SkFloat3x3 xyz = colorSpace->xyz();
91 REPORTER_ASSERT(r, almost_equal(0.385117f, xyz.fMat[0]));
92 REPORTER_ASSERT(r, almost_equal(0.716904f, xyz.fMat[1]));
93 REPORTER_ASSERT(r, almost_equal(0.0970612f, xyz.fMat[2]));
94 REPORTER_ASSERT(r, almost_equal(0.143051f, xyz.fMat[3]));
95 REPORTER_ASSERT(r, almost_equal(0.0606079f, xyz.fMat[4]));
96 REPORTER_ASSERT(r, almost_equal(0.713913f, xyz.fMat[5]));
97 REPORTER_ASSERT(r, almost_equal(0.436035f, xyz.fMat[6]));
98 REPORTER_ASSERT(r, almost_equal(0.222488f, xyz.fMat[7]));
99 REPORTER_ASSERT(r, almost_equal(0.013916f, xyz.fMat[8]));
100 } 102 }
OLDNEW
« no previous file with comments | « src/utils/SkMatrix44.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698