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