blob: d8f5a35fdbb06048ed8c5c1cff3b2a34429e5a9c [file] [log] [blame]
Ravi Mistry113d05f2016-12-17 01:31:03 +00001/*
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
Mike Klein5b58b7c2018-05-22 13:47:52 +00008#include "SkTypes.h"
Ravi Mistry113d05f2016-12-17 01:31:03 +00009
Brian Osman8dc68c62018-05-30 12:57:45 -040010#include "Resources.h"
11#include "SkColorSpacePriv.h"
12#include "SkICC.h"
13#include "SkString.h"
14#include "Test.h"
15#include "../third_party/skcms/skcms.h"
Ravi Mistry113d05f2016-12-17 01:31:03 +000016
Brian Osman8dc68c62018-05-30 12:57:45 -040017DEF_TEST(WriteICCProfile, r) {
18 auto adobeRGB = SkColorSpace::MakeRGB(g2Dot2_TransferFn, SkColorSpace::kAdobeRGB_Gamut);
Mike Klein5b58b7c2018-05-22 13:47:52 +000019
Brian Osman8dc68c62018-05-30 12:57:45 -040020 struct {
21 SkColorSpaceTransferFn fn;
22 const float* toXYZD50;
23 const char* desc;
24 sk_sp<SkColorSpace> want;
25 } tests[] = {
26 {g2Dot2_TransferFn, gAdobeRGB_toXYZD50, "AdobeRGB", adobeRGB},
27 { gSRGB_TransferFn, gSRGB_toXYZD50, "sRGB", SkColorSpace::MakeSRGB()},
28 };
Mike Klein5b58b7c2018-05-22 13:47:52 +000029
Brian Osman8dc68c62018-05-30 12:57:45 -040030 for (auto test : tests) {
31 sk_sp<SkData> profile = SkWriteICCProfile(test.fn, test.toXYZD50);
32 REPORTER_ASSERT(r, profile);
Mike Klein5b58b7c2018-05-22 13:47:52 +000033
Brian Osman8dc68c62018-05-30 12:57:45 -040034 skcms_ICCProfile parsed;
35 REPORTER_ASSERT(r, skcms_Parse(profile->data(), profile->size(), &parsed));
Mike Klein5b58b7c2018-05-22 13:47:52 +000036
Brian Osman8dc68c62018-05-30 12:57:45 -040037 sk_sp<SkColorSpace> got = SkColorSpace::Make(parsed);
38 REPORTER_ASSERT(r, got);
39 REPORTER_ASSERT(r, SkColorSpace::Equals(got.get(), test.want.get()));
Mike Klein5b58b7c2018-05-22 13:47:52 +000040
Brian Osman8dc68c62018-05-30 12:57:45 -040041 skcms_ICCTag desc;
42 REPORTER_ASSERT(r, skcms_GetTagBySignature(&parsed,
43 SkSetFourByteTag('d','e','s','c'),
44 &desc));
Mike Klein5b58b7c2018-05-22 13:47:52 +000045
Brian Osman8dc68c62018-05-30 12:57:45 -040046 // Rather than really carefully break down the 'desc' tag,
47 // just check our expected description is somewhere in there (as big-endian UTF-16).
48 uint8_t big_endian_utf16[16];
49 for (size_t i = 0; i < strlen(test.desc); i++) {
50 big_endian_utf16[2*i+0] = 0;
51 big_endian_utf16[2*i+1] = test.desc[i];
Ravi Mistry113d05f2016-12-17 01:31:03 +000052 }
Brian Osman8dc68c62018-05-30 12:57:45 -040053
54 SkString haystack((const char*)desc.buf, desc.size),
55 needle ((const char*)big_endian_utf16, 2*strlen(test.desc));
56 REPORTER_ASSERT(r, haystack.contains(needle.c_str()));
Ravi Mistry113d05f2016-12-17 01:31:03 +000057 }
Brian Osman8dc68c62018-05-30 12:57:45 -040058}
Ravi Mistry113d05f2016-12-17 01:31:03 +000059
Brian Osman8dc68c62018-05-30 12:57:45 -040060DEF_TEST(AdobeRGB, r) {
61 if (sk_sp<SkData> profile = GetResourceAsData("icc_profiles/AdobeRGB1998.icc")) {
62 skcms_ICCProfile parsed;
63 REPORTER_ASSERT(r, skcms_Parse(profile->data(), profile->size(), &parsed));
Ravi Mistry113d05f2016-12-17 01:31:03 +000064
Brian Osman8dc68c62018-05-30 12:57:45 -040065 auto got = SkColorSpace::Make(parsed);
66 auto want = SkColorSpace::MakeRGB(g2Dot2_TransferFn, SkColorSpace::kAdobeRGB_Gamut);
67 REPORTER_ASSERT(r, SkColorSpace::Equals(got.get(), want.get()));
Matt Sarett1bfcf882016-12-19 18:37:34 -050068 }
Brian Osman8dc68c62018-05-30 12:57:45 -040069}