Ravi Mistry | 113d05f | 2016-12-17 01:31:03 +0000 | [diff] [blame] | 1 | /* |
| 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 "SkColorSpace.h" |
Matt Sarett | 1bfcf88 | 2016-12-19 18:37:34 -0500 | [diff] [blame^] | 10 | #include "SkColorSpacePriv.h" |
Ravi Mistry | 113d05f | 2016-12-17 01:31:03 +0000 | [diff] [blame] | 11 | #include "SkData.h" |
| 12 | #include "SkICC.h" |
| 13 | #include "SkMatrix44.h" |
Matt Sarett | 1bfcf88 | 2016-12-19 18:37:34 -0500 | [diff] [blame^] | 14 | #include "SkStream.h" |
Ravi Mistry | 113d05f | 2016-12-17 01:31:03 +0000 | [diff] [blame] | 15 | #include "Test.h" |
| 16 | |
| 17 | static bool almost_equal(float a, float b) { |
| 18 | return SkTAbs(a - b) < 0.001f; |
| 19 | } |
| 20 | |
| 21 | static inline void test_to_xyz_d50(skiatest::Reporter* r, SkICC* icc, bool shouldSucceed, |
| 22 | const float* reference) { |
Matt Sarett | d4b9c35 | 2016-12-18 13:49:43 -0500 | [diff] [blame] | 23 | SkMatrix44 result(SkMatrix44::kUninitialized_Constructor); |
Ravi Mistry | 113d05f | 2016-12-17 01:31:03 +0000 | [diff] [blame] | 24 | REPORTER_ASSERT(r, shouldSucceed == icc->toXYZD50(&result)); |
| 25 | if (shouldSucceed) { |
| 26 | float resultVals[16]; |
| 27 | result.asColMajorf(resultVals); |
| 28 | for (int i = 0; i < 16; i++) { |
| 29 | REPORTER_ASSERT(r, almost_equal(resultVals[i], reference[i])); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | DEF_TEST(ICC_ToXYZD50, r) { |
| 35 | const float z30Reference[16] = { |
| 36 | 0.59825f, 0.27103f, 0.00603f, 0.0f, 0.22243f, 0.67447f, 0.07368f, 0.0f, 0.14352f, 0.05449f, |
| 37 | 0.74519f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 38 | }; |
| 39 | |
| 40 | sk_sp<SkData> data = SkData::MakeFromFileName( |
| 41 | GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str()); |
| 42 | sk_sp<SkICC> z30 = SkICC::Make(data->data(), data->size()); |
| 43 | test_to_xyz_d50(r, z30.get(), true, z30Reference); |
| 44 | |
| 45 | const float z32Reference[16] = { |
| 46 | 0.61583f, 0.28789f, 0.00513f, 0.0f, 0.20428f, 0.66972f, 0.06609f, 0.0f, 0.14409f, 0.04237f, |
| 47 | 0.75368f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 48 | }; |
| 49 | |
| 50 | data = SkData::MakeFromFileName( GetResourcePath("icc_profiles/HP_Z32x.icc").c_str()); |
| 51 | sk_sp<SkICC> z32 = SkICC::Make(data->data(), data->size()); |
| 52 | test_to_xyz_d50(r, z32.get(), true, z32Reference); |
| 53 | |
| 54 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperLeft.icc").c_str()); |
| 55 | sk_sp<SkICC> upperLeft = SkICC::Make(data->data(), data->size()); |
| 56 | test_to_xyz_d50(r, upperLeft.get(), false, z32Reference); |
| 57 | |
| 58 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperRight.icc").c_str()); |
| 59 | sk_sp<SkICC> upperRight = SkICC::Make(data->data(), data->size()); |
| 60 | test_to_xyz_d50(r, upperRight.get(), false, z32Reference); |
| 61 | } |
| 62 | |
| 63 | static inline void test_is_numerical_transfer_fn(skiatest::Reporter* r, SkICC* icc, |
| 64 | bool shouldSucceed, |
| 65 | const SkColorSpaceTransferFn& reference) { |
| 66 | SkColorSpaceTransferFn result; |
| 67 | REPORTER_ASSERT(r, shouldSucceed == icc->isNumericalTransferFn(&result)); |
| 68 | if (shouldSucceed) { |
| 69 | REPORTER_ASSERT(r, 0 == memcmp(&result, &reference, sizeof(SkColorSpaceTransferFn))); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | DEF_TEST(ICC_IsNumericalTransferFn, r) { |
| 74 | SkColorSpaceTransferFn referenceFn; |
| 75 | referenceFn.fA = 1.0f; |
| 76 | referenceFn.fB = 0.0f; |
| 77 | referenceFn.fC = 0.0f; |
| 78 | referenceFn.fD = 0.0f; |
| 79 | referenceFn.fE = 0.0f; |
| 80 | referenceFn.fF = 0.0f; |
| 81 | referenceFn.fG = 2.2f; |
| 82 | |
| 83 | sk_sp<SkData> data = SkData::MakeFromFileName( |
| 84 | GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str()); |
| 85 | sk_sp<SkICC> z30 = SkICC::Make(data->data(), data->size()); |
| 86 | test_is_numerical_transfer_fn(r, z30.get(), true, referenceFn); |
| 87 | |
| 88 | data = SkData::MakeFromFileName( GetResourcePath("icc_profiles/HP_Z32x.icc").c_str()); |
| 89 | sk_sp<SkICC> z32 = SkICC::Make(data->data(), data->size()); |
| 90 | test_is_numerical_transfer_fn(r, z32.get(), true, referenceFn); |
| 91 | |
| 92 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperLeft.icc").c_str()); |
| 93 | sk_sp<SkICC> upperLeft = SkICC::Make(data->data(), data->size()); |
| 94 | test_is_numerical_transfer_fn(r, upperLeft.get(), false, referenceFn); |
| 95 | |
| 96 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperRight.icc").c_str()); |
| 97 | sk_sp<SkICC> upperRight = SkICC::Make(data->data(), data->size()); |
| 98 | test_is_numerical_transfer_fn(r, upperRight.get(), false, referenceFn); |
| 99 | } |
Matt Sarett | 1bfcf88 | 2016-12-19 18:37:34 -0500 | [diff] [blame^] | 100 | |
| 101 | static inline void test_write_icc(skiatest::Reporter* r, const SkColorSpaceTransferFn& fn, |
| 102 | const SkMatrix44& toXYZD50, SkColorSpace* reference, |
| 103 | bool writeToFile) { |
| 104 | sk_sp<SkData> profile = SkICC::WriteToICC(fn, toXYZD50); |
| 105 | if (writeToFile) { |
| 106 | SkFILEWStream stream("out.icc"); |
| 107 | stream.write(profile->data(), profile->size()); |
| 108 | } |
| 109 | |
| 110 | sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(profile->data(), profile->size()); |
| 111 | REPORTER_ASSERT(r, SkColorSpace::Equals(reference, colorSpace.get())); |
| 112 | } |
| 113 | |
| 114 | DEF_TEST(ICC_WriteICC, r) { |
| 115 | SkColorSpaceTransferFn adobeFn; |
| 116 | adobeFn.fA = 1.0f; |
| 117 | adobeFn.fB = 0.0f; |
| 118 | adobeFn.fC = 0.0f; |
| 119 | adobeFn.fD = 0.0f; |
| 120 | adobeFn.fE = 0.0f; |
| 121 | adobeFn.fF = 0.0f; |
| 122 | adobeFn.fG = 2.2f; |
| 123 | SkMatrix44 adobeMatrix(SkMatrix44::kUninitialized_Constructor); |
| 124 | adobeMatrix.set3x3RowMajorf(gAdobeRGB_toXYZD50); |
| 125 | test_write_icc(r, adobeFn, adobeMatrix, |
| 126 | SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named).get(), false); |
| 127 | |
| 128 | SkColorSpaceTransferFn srgbFn; |
| 129 | srgbFn.fA = 1.0f / 1.055f; |
| 130 | srgbFn.fB = 0.055f / 1.055f; |
| 131 | srgbFn.fC = 1.0f / 12.92f; |
| 132 | srgbFn.fD = 0.04045f; |
| 133 | srgbFn.fE = 0.0f; |
| 134 | srgbFn.fF = 0.0f; |
| 135 | srgbFn.fG = 2.4f; |
| 136 | SkMatrix44 srgbMatrix(SkMatrix44::kUninitialized_Constructor); |
| 137 | srgbMatrix.set3x3RowMajorf(gSRGB_toXYZD50); |
| 138 | test_write_icc(r, srgbFn, srgbMatrix, SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named).get(), |
| 139 | false); |
| 140 | } |