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" |
Matt Sarett | 6f67fc2 | 2017-01-26 13:10:49 -0500 | [diff] [blame^] | 11 | #include "SkColorSpace_XYZ.h" |
Ravi Mistry | 113d05f | 2016-12-17 01:31:03 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
| 13 | #include "SkICC.h" |
| 14 | #include "SkMatrix44.h" |
Matt Sarett | 1bfcf88 | 2016-12-19 18:37:34 -0500 | [diff] [blame] | 15 | #include "SkStream.h" |
Ravi Mistry | 113d05f | 2016-12-17 01:31:03 +0000 | [diff] [blame] | 16 | #include "Test.h" |
| 17 | |
| 18 | static bool almost_equal(float a, float b) { |
| 19 | return SkTAbs(a - b) < 0.001f; |
| 20 | } |
| 21 | |
| 22 | static inline void test_to_xyz_d50(skiatest::Reporter* r, SkICC* icc, bool shouldSucceed, |
| 23 | const float* reference) { |
Matt Sarett | d4b9c35 | 2016-12-18 13:49:43 -0500 | [diff] [blame] | 24 | SkMatrix44 result(SkMatrix44::kUninitialized_Constructor); |
Ravi Mistry | 113d05f | 2016-12-17 01:31:03 +0000 | [diff] [blame] | 25 | REPORTER_ASSERT(r, shouldSucceed == icc->toXYZD50(&result)); |
| 26 | if (shouldSucceed) { |
| 27 | float resultVals[16]; |
| 28 | result.asColMajorf(resultVals); |
| 29 | for (int i = 0; i < 16; i++) { |
| 30 | REPORTER_ASSERT(r, almost_equal(resultVals[i], reference[i])); |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | DEF_TEST(ICC_ToXYZD50, r) { |
| 36 | const float z30Reference[16] = { |
| 37 | 0.59825f, 0.27103f, 0.00603f, 0.0f, 0.22243f, 0.67447f, 0.07368f, 0.0f, 0.14352f, 0.05449f, |
| 38 | 0.74519f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 39 | }; |
| 40 | |
| 41 | sk_sp<SkData> data = SkData::MakeFromFileName( |
| 42 | GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str()); |
| 43 | sk_sp<SkICC> z30 = SkICC::Make(data->data(), data->size()); |
| 44 | test_to_xyz_d50(r, z30.get(), true, z30Reference); |
| 45 | |
| 46 | const float z32Reference[16] = { |
| 47 | 0.61583f, 0.28789f, 0.00513f, 0.0f, 0.20428f, 0.66972f, 0.06609f, 0.0f, 0.14409f, 0.04237f, |
| 48 | 0.75368f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 49 | }; |
| 50 | |
| 51 | data = SkData::MakeFromFileName( GetResourcePath("icc_profiles/HP_Z32x.icc").c_str()); |
| 52 | sk_sp<SkICC> z32 = SkICC::Make(data->data(), data->size()); |
| 53 | test_to_xyz_d50(r, z32.get(), true, z32Reference); |
| 54 | |
| 55 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperLeft.icc").c_str()); |
| 56 | sk_sp<SkICC> upperLeft = SkICC::Make(data->data(), data->size()); |
| 57 | test_to_xyz_d50(r, upperLeft.get(), false, z32Reference); |
| 58 | |
| 59 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperRight.icc").c_str()); |
| 60 | sk_sp<SkICC> upperRight = SkICC::Make(data->data(), data->size()); |
| 61 | test_to_xyz_d50(r, upperRight.get(), false, z32Reference); |
| 62 | } |
| 63 | |
| 64 | static inline void test_is_numerical_transfer_fn(skiatest::Reporter* r, SkICC* icc, |
| 65 | bool shouldSucceed, |
| 66 | const SkColorSpaceTransferFn& reference) { |
| 67 | SkColorSpaceTransferFn result; |
| 68 | REPORTER_ASSERT(r, shouldSucceed == icc->isNumericalTransferFn(&result)); |
| 69 | if (shouldSucceed) { |
| 70 | REPORTER_ASSERT(r, 0 == memcmp(&result, &reference, sizeof(SkColorSpaceTransferFn))); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | DEF_TEST(ICC_IsNumericalTransferFn, r) { |
| 75 | SkColorSpaceTransferFn referenceFn; |
| 76 | referenceFn.fA = 1.0f; |
| 77 | referenceFn.fB = 0.0f; |
| 78 | referenceFn.fC = 0.0f; |
| 79 | referenceFn.fD = 0.0f; |
| 80 | referenceFn.fE = 0.0f; |
| 81 | referenceFn.fF = 0.0f; |
| 82 | referenceFn.fG = 2.2f; |
| 83 | |
| 84 | sk_sp<SkData> data = SkData::MakeFromFileName( |
| 85 | GetResourcePath("icc_profiles/HP_ZR30w.icc").c_str()); |
| 86 | sk_sp<SkICC> z30 = SkICC::Make(data->data(), data->size()); |
| 87 | test_is_numerical_transfer_fn(r, z30.get(), true, referenceFn); |
| 88 | |
| 89 | data = SkData::MakeFromFileName( GetResourcePath("icc_profiles/HP_Z32x.icc").c_str()); |
| 90 | sk_sp<SkICC> z32 = SkICC::Make(data->data(), data->size()); |
| 91 | test_is_numerical_transfer_fn(r, z32.get(), true, referenceFn); |
| 92 | |
| 93 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperLeft.icc").c_str()); |
| 94 | sk_sp<SkICC> upperLeft = SkICC::Make(data->data(), data->size()); |
| 95 | test_is_numerical_transfer_fn(r, upperLeft.get(), false, referenceFn); |
| 96 | |
| 97 | data = SkData::MakeFromFileName(GetResourcePath("icc_profiles/upperRight.icc").c_str()); |
| 98 | sk_sp<SkICC> upperRight = SkICC::Make(data->data(), data->size()); |
| 99 | test_is_numerical_transfer_fn(r, upperRight.get(), false, referenceFn); |
| 100 | } |
Matt Sarett | 1bfcf88 | 2016-12-19 18:37:34 -0500 | [diff] [blame] | 101 | |
| 102 | static inline void test_write_icc(skiatest::Reporter* r, const SkColorSpaceTransferFn& fn, |
| 103 | const SkMatrix44& toXYZD50, SkColorSpace* reference, |
| 104 | bool writeToFile) { |
| 105 | sk_sp<SkData> profile = SkICC::WriteToICC(fn, toXYZD50); |
| 106 | if (writeToFile) { |
| 107 | SkFILEWStream stream("out.icc"); |
| 108 | stream.write(profile->data(), profile->size()); |
| 109 | } |
| 110 | |
| 111 | sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(profile->data(), profile->size()); |
| 112 | REPORTER_ASSERT(r, SkColorSpace::Equals(reference, colorSpace.get())); |
| 113 | } |
| 114 | |
| 115 | DEF_TEST(ICC_WriteICC, r) { |
| 116 | SkColorSpaceTransferFn adobeFn; |
| 117 | adobeFn.fA = 1.0f; |
| 118 | adobeFn.fB = 0.0f; |
| 119 | adobeFn.fC = 0.0f; |
| 120 | adobeFn.fD = 0.0f; |
| 121 | adobeFn.fE = 0.0f; |
| 122 | adobeFn.fF = 0.0f; |
| 123 | adobeFn.fG = 2.2f; |
| 124 | SkMatrix44 adobeMatrix(SkMatrix44::kUninitialized_Constructor); |
| 125 | adobeMatrix.set3x3RowMajorf(gAdobeRGB_toXYZD50); |
| 126 | test_write_icc(r, adobeFn, adobeMatrix, |
| 127 | SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named).get(), false); |
| 128 | |
| 129 | SkColorSpaceTransferFn srgbFn; |
| 130 | srgbFn.fA = 1.0f / 1.055f; |
| 131 | srgbFn.fB = 0.055f / 1.055f; |
| 132 | srgbFn.fC = 1.0f / 12.92f; |
| 133 | srgbFn.fD = 0.04045f; |
| 134 | srgbFn.fE = 0.0f; |
| 135 | srgbFn.fF = 0.0f; |
| 136 | srgbFn.fG = 2.4f; |
| 137 | SkMatrix44 srgbMatrix(SkMatrix44::kUninitialized_Constructor); |
| 138 | srgbMatrix.set3x3RowMajorf(gSRGB_toXYZD50); |
| 139 | test_write_icc(r, srgbFn, srgbMatrix, SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named).get(), |
| 140 | false); |
| 141 | } |
Matt Sarett | 6f67fc2 | 2017-01-26 13:10:49 -0500 | [diff] [blame^] | 142 | |
| 143 | static inline void test_raw_transfer_fn(skiatest::Reporter* r, SkICC* icc) { |
| 144 | SkICC::Tables tables; |
| 145 | bool result = icc->rawTransferFnData(&tables); |
| 146 | REPORTER_ASSERT(r, result); |
| 147 | |
| 148 | REPORTER_ASSERT(r, 0.0f == tables.red()[0]); |
| 149 | REPORTER_ASSERT(r, 0.0f == tables.green()[0]); |
| 150 | REPORTER_ASSERT(r, 0.0f == tables.blue()[0]); |
| 151 | REPORTER_ASSERT(r, 1.0f == tables.red()[tables.fRed.fCount - 1]); |
| 152 | REPORTER_ASSERT(r, 1.0f == tables.green()[tables.fGreen.fCount - 1]); |
| 153 | REPORTER_ASSERT(r, 1.0f == tables.blue()[tables.fBlue.fCount - 1]); |
| 154 | } |
| 155 | |
| 156 | class ICCTest { |
| 157 | public: |
| 158 | static sk_sp<SkICC> MakeICC(sk_sp<SkColorSpace> space) { |
| 159 | return sk_sp<SkICC>(new SkICC(std::move(space))); |
| 160 | } |
| 161 | static sk_sp<SkICC> MakeICC(sk_sp<SkGammas> gammas) { |
| 162 | return MakeICC(sk_sp<SkColorSpace>(new SkColorSpace_XYZ( |
| 163 | kNonStandard_SkGammaNamed, std::move(gammas), |
| 164 | SkMatrix44(SkMatrix44::kIdentity_Constructor), nullptr))); |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | DEF_TEST(ICC_RawTransferFns, r) { |
| 169 | sk_sp<SkICC> srgb = ICCTest::MakeICC(SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named)); |
| 170 | test_raw_transfer_fn(r, srgb.get()); |
| 171 | |
| 172 | sk_sp<SkICC> adobe = |
| 173 | ICCTest::MakeICC(SkColorSpace::MakeNamed(SkColorSpace::kAdobeRGB_Named)); |
| 174 | test_raw_transfer_fn(r, adobe.get()); |
| 175 | |
| 176 | // Lookup-table based gamma curves |
| 177 | constexpr size_t tableSize = 10; |
| 178 | void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize); |
| 179 | sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas(3)); |
| 180 | for (int i = 0; i < 3; ++i) { |
| 181 | gammas->fType[i] = SkGammas::Type::kTable_Type; |
| 182 | gammas->fData[i].fTable.fSize = tableSize; |
| 183 | gammas->fData[i].fTable.fOffset = 0; |
| 184 | } |
| 185 | |
| 186 | float* table = SkTAddOffset<float>(memory, sizeof(SkGammas)); |
| 187 | table[0] = 0.00f; |
| 188 | table[1] = 0.05f; |
| 189 | table[2] = 0.10f; |
| 190 | table[3] = 0.15f; |
| 191 | table[4] = 0.25f; |
| 192 | table[5] = 0.35f; |
| 193 | table[6] = 0.45f; |
| 194 | table[7] = 0.60f; |
| 195 | table[8] = 0.75f; |
| 196 | table[9] = 1.00f; |
| 197 | sk_sp<SkICC> tbl = ICCTest::MakeICC(gammas); |
| 198 | test_raw_transfer_fn(r, tbl.get()); |
| 199 | |
| 200 | // Parametric gamma curves |
| 201 | memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkColorSpaceTransferFn)); |
| 202 | gammas = sk_sp<SkGammas>(new (memory) SkGammas(3)); |
| 203 | for (int i = 0; i < 3; ++i) { |
| 204 | gammas->fType[i] = SkGammas::Type::kParam_Type; |
| 205 | gammas->fData[i].fParamOffset = 0; |
| 206 | } |
| 207 | |
| 208 | SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn> |
| 209 | (memory, sizeof(SkGammas)); |
| 210 | |
| 211 | // Interval. |
| 212 | params->fD = 0.04045f; |
| 213 | |
| 214 | // First equation: |
| 215 | params->fC = 1.0f / 12.92f; |
| 216 | params->fF = 0.0f; |
| 217 | |
| 218 | // Second equation: |
| 219 | // Note that the function is continuous (it's actually sRGB). |
| 220 | params->fA = 1.0f / 1.055f; |
| 221 | params->fB = 0.055f / 1.055f; |
| 222 | params->fE = 0.0f; |
| 223 | params->fG = 2.4f; |
| 224 | sk_sp<SkICC> param = ICCTest::MakeICC(gammas); |
| 225 | test_raw_transfer_fn(r, param.get()); |
| 226 | |
| 227 | // Exponential gamma curves |
| 228 | gammas = sk_sp<SkGammas>(new SkGammas(3)); |
| 229 | for (int i = 0; i < 3; ++i) { |
| 230 | gammas->fType[i] = SkGammas::Type::kValue_Type; |
| 231 | gammas->fData[i].fValue = 1.4f; |
| 232 | } |
| 233 | sk_sp<SkICC> exp = ICCTest::MakeICC(gammas); |
| 234 | test_raw_transfer_fn(r, exp.get()); |
| 235 | |
| 236 | gammas = sk_sp<SkGammas>(new SkGammas(3)); |
| 237 | gammas->fType[0] = gammas->fType[1] = gammas->fType[2] = SkGammas::Type::kNamed_Type; |
| 238 | gammas->fData[0].fNamed = kSRGB_SkGammaNamed; |
| 239 | gammas->fData[1].fNamed = k2Dot2Curve_SkGammaNamed; |
| 240 | gammas->fData[2].fNamed = kLinear_SkGammaNamed; |
| 241 | sk_sp<SkICC> named = ICCTest::MakeICC(gammas); |
| 242 | test_raw_transfer_fn(r, named.get()); |
| 243 | |
| 244 | memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize + |
| 245 | sizeof(SkColorSpaceTransferFn)); |
| 246 | gammas = sk_sp<SkGammas>(new (memory) SkGammas(3)); |
| 247 | |
| 248 | table = SkTAddOffset<float>(memory, sizeof(SkGammas)); |
| 249 | table[0] = 0.00f; |
| 250 | table[1] = 0.15f; |
| 251 | table[2] = 0.20f; |
| 252 | table[3] = 0.25f; |
| 253 | table[4] = 0.35f; |
| 254 | table[5] = 0.45f; |
| 255 | table[6] = 0.55f; |
| 256 | table[7] = 0.70f; |
| 257 | table[8] = 0.85f; |
| 258 | table[9] = 1.00f; |
| 259 | |
| 260 | params = SkTAddOffset<SkColorSpaceTransferFn>(memory, |
| 261 | sizeof(SkGammas) + sizeof(float) * tableSize); |
| 262 | params->fA = 1.0f / 1.055f; |
| 263 | params->fB = 0.055f / 1.055f; |
| 264 | params->fC = 1.0f / 12.92f; |
| 265 | params->fD = 0.04045f; |
| 266 | params->fE = 0.0f; |
| 267 | params->fF = 0.0f; |
| 268 | params->fG = 2.4f; |
| 269 | |
| 270 | gammas->fType[0] = SkGammas::Type::kValue_Type; |
| 271 | gammas->fData[0].fValue = 1.2f; |
| 272 | |
| 273 | gammas->fType[1] = SkGammas::Type::kTable_Type; |
| 274 | gammas->fData[1].fTable.fSize = tableSize; |
| 275 | gammas->fData[1].fTable.fOffset = 0; |
| 276 | |
| 277 | gammas->fType[2] = SkGammas::Type::kParam_Type; |
| 278 | gammas->fData[2].fParamOffset = sizeof(float) * tableSize; |
| 279 | sk_sp<SkICC> nonstd = ICCTest::MakeICC(gammas); |
| 280 | test_raw_transfer_fn(r, nonstd.get()); |
| 281 | |
| 282 | // Reverse order of table and exponent |
| 283 | gammas->fType[1] = SkGammas::Type::kValue_Type; |
| 284 | gammas->fData[1].fValue = 1.2f; |
| 285 | |
| 286 | gammas->fType[0] = SkGammas::Type::kTable_Type; |
| 287 | gammas->fData[0].fTable.fSize = tableSize; |
| 288 | gammas->fData[0].fTable.fOffset = 0; |
| 289 | sk_sp<SkICC> nonstd2 = ICCTest::MakeICC(gammas); |
| 290 | test_raw_transfer_fn(r, nonstd2.get()); |
| 291 | } |