aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/ICCTest.cpp
diff options
context:
space:
mode:
authorGravatar Mike Klein <mtklein@chromium.org>2018-05-10 17:08:33 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2018-05-10 21:44:56 +0000
commit27fe397bc0cc1b091f9f85863c62b88156239cf0 (patch)
treedeef5933f642e01717f4eacf2ed50c400157f9b8 /tests/ICCTest.cpp
parent959fc245a92af65fb8bb4a78db53aec188c812be (diff)
Revert "strip down SkICC.cpp"
This reverts commit eab50eb9c6117c2a9d0e5648f89cebbb4dbd9d30 and this tiny bit of e61b969a07ba3ebe9e47e61381ad16c5d2c549a2: https://skia-review.googlesource.com/c/skia/+/127122/3/tests/ICCTest.cpp Change-Id: I4306e5118a4e5eb88c05078186a28bd443fd76f7 Reviewed-on: https://skia-review.googlesource.com/127305 Reviewed-by: Mike Klein <mtklein@chromium.org> Commit-Queue: Mike Klein <mtklein@chromium.org>
Diffstat (limited to 'tests/ICCTest.cpp')
-rw-r--r--tests/ICCTest.cpp347
1 files changed, 287 insertions, 60 deletions
diff --git a/tests/ICCTest.cpp b/tests/ICCTest.cpp
index e88255b2cd..34ed52984c 100644
--- a/tests/ICCTest.cpp
+++ b/tests/ICCTest.cpp
@@ -5,69 +5,296 @@
* found in the LICENSE file.
*/
-#include "SkTypes.h"
-
-#if defined(SK_USE_SKCMS)
-
- #include "Resources.h"
- #include "SkColorSpacePriv.h"
- #include "SkICC.h"
- #include "SkString.h"
- #include "Test.h"
- #include "skcms.h"
-
- DEF_TEST(WriteICCProfile, r) {
- auto adobeRGB = SkColorSpace::MakeRGB(g2Dot2_TransferFn, SkColorSpace::kAdobeRGB_Gamut);
-
- struct {
- SkColorSpaceTransferFn fn;
- const float* toXYZD50;
- const char* desc;
- sk_sp<SkColorSpace> want;
- } tests[] = {
- {g2Dot2_TransferFn, gAdobeRGB_toXYZD50, "AdobeRGB", adobeRGB},
- { gSRGB_TransferFn, gSRGB_toXYZD50, "sRGB", SkColorSpace::MakeSRGB()},
- };
-
- for (auto test : tests) {
- sk_sp<SkData> profile = SkWriteICCProfile(test.fn, test.toXYZD50);
- REPORTER_ASSERT(r, profile);
-
- skcms_ICCProfile parsed;
- REPORTER_ASSERT(r, skcms_Parse(profile->data(), profile->size(), &parsed));
-
- sk_sp<SkColorSpace> got = SkColorSpace::Make(parsed);
- REPORTER_ASSERT(r, got);
- REPORTER_ASSERT(r, SkColorSpace::Equals(got.get(), test.want.get()));
-
- skcms_ICCTag desc;
- REPORTER_ASSERT(r, skcms_GetTagBySignature(&parsed,
- SkSetFourByteTag('d','e','s','c'),
- &desc));
-
- // Rather than really carefully break down the 'desc' tag,
- // just check our expected description is somewhere in there (as big-endian UTF-16).
- uint8_t big_endian_utf16[16];
- for (size_t i = 0; i < strlen(test.desc); i++) {
- big_endian_utf16[2*i+0] = 0;
- big_endian_utf16[2*i+1] = test.desc[i];
- }
-
- SkString haystack((const char*)desc.buf, desc.size),
- needle ((const char*)big_endian_utf16, 2*strlen(test.desc));
- REPORTER_ASSERT(r, haystack.contains(needle.c_str()));
+#include "Resources.h"
+#include "SkColorSpace.h"
+#include "SkColorSpacePriv.h"
+#include "SkColorSpace_XYZ.h"
+#include "SkData.h"
+#include "SkICC.h"
+#include "SkICCPriv.h"
+#include "SkMatrix44.h"
+#include "SkStream.h"
+#include "Test.h"
+
+static bool almost_equal(float a, float b) {
+ return SkTAbs(a - b) < 0.001f;
+}
+
+static inline void test_to_xyz_d50(skiatest::Reporter* r, SkICC* icc, bool shouldSucceed,
+ const float* reference) {
+ SkMatrix44 result(SkMatrix44::kUninitialized_Constructor);
+ REPORTER_ASSERT(r, shouldSucceed == icc->toXYZD50(&result));
+ if (shouldSucceed) {
+ float resultVals[16];
+ result.asColMajorf(resultVals);
+ for (int i = 0; i < 16; i++) {
+ REPORTER_ASSERT(r, almost_equal(resultVals[i], reference[i]));
}
}
+}
- DEF_TEST(AdobeRGB, r) {
- if (sk_sp<SkData> profile = GetResourceAsData("icc_profiles/AdobeRGB1998.icc")) {
- skcms_ICCProfile parsed;
- REPORTER_ASSERT(r, skcms_Parse(profile->data(), profile->size(), &parsed));
+DEF_TEST(ICC_ToXYZD50, r) {
+ const float z30Reference[16] = {
+ 0.59825f, 0.27103f, 0.00603f, 0.0f, 0.22243f, 0.67447f, 0.07368f, 0.0f, 0.14352f, 0.05449f,
+ 0.74519f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
+ };
- auto got = SkColorSpace::Make(parsed);
- auto want = SkColorSpace::MakeRGB(g2Dot2_TransferFn, SkColorSpace::kAdobeRGB_Gamut);
- REPORTER_ASSERT(r, SkColorSpace::Equals(got.get(), want.get()));
- }
+ sk_sp<SkData> data = GetResourceAsData("icc_profiles/HP_ZR30w.icc");
+ sk_sp<SkICC> z30 = SkICC::Make(data->data(), data->size());
+ test_to_xyz_d50(r, z30.get(), true, z30Reference);
+
+ const float z32Reference[16] = {
+ 0.61583f, 0.28789f, 0.00513f, 0.0f, 0.20428f, 0.66972f, 0.06609f, 0.0f, 0.14409f, 0.04237f,
+ 0.75368f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
+ };
+
+ data = GetResourceAsData("icc_profiles/HP_Z32x.icc");
+ sk_sp<SkICC> z32 = SkICC::Make(data->data(), data->size());
+ test_to_xyz_d50(r, z32.get(), true, z32Reference);
+
+ data = GetResourceAsData("icc_profiles/upperLeft.icc");
+ sk_sp<SkICC> upperLeft = SkICC::Make(data->data(), data->size());
+ test_to_xyz_d50(r, upperLeft.get(), false, z32Reference);
+
+ data = GetResourceAsData("icc_profiles/upperRight.icc");
+ sk_sp<SkICC> upperRight = SkICC::Make(data->data(), data->size());
+ test_to_xyz_d50(r, upperRight.get(), false, z32Reference);
+}
+
+static inline void test_is_numerical_transfer_fn(skiatest::Reporter* r, SkICC* icc,
+ bool shouldSucceed,
+ const SkColorSpaceTransferFn& reference) {
+ SkColorSpaceTransferFn result;
+ REPORTER_ASSERT(r, shouldSucceed == icc->isNumericalTransferFn(&result));
+ if (shouldSucceed) {
+ REPORTER_ASSERT(r, 0 == memcmp(&result, &reference, sizeof(SkColorSpaceTransferFn)));
}
+}
+
+DEF_TEST(ICC_IsNumericalTransferFn, r) {
+ SkColorSpaceTransferFn referenceFn;
+ referenceFn.fA = 1.0f;
+ referenceFn.fB = 0.0f;
+ referenceFn.fC = 0.0f;
+ referenceFn.fD = 0.0f;
+ referenceFn.fE = 0.0f;
+ referenceFn.fF = 0.0f;
+ referenceFn.fG = 2.2f;
+
+ sk_sp<SkData> data = GetResourceAsData("icc_profiles/HP_ZR30w.icc");
+ sk_sp<SkICC> z30 = SkICC::Make(data->data(), data->size());
+ test_is_numerical_transfer_fn(r, z30.get(), true, referenceFn);
+
+ data = GetResourceAsData("icc_profiles/HP_Z32x.icc");
+ sk_sp<SkICC> z32 = SkICC::Make(data->data(), data->size());
+ test_is_numerical_transfer_fn(r, z32.get(), true, referenceFn);
+
+ data = GetResourceAsData("icc_profiles/upperLeft.icc");
+ sk_sp<SkICC> upperLeft = SkICC::Make(data->data(), data->size());
+ test_is_numerical_transfer_fn(r, upperLeft.get(), false, referenceFn);
+
+ data = GetResourceAsData("icc_profiles/upperRight.icc");
+ sk_sp<SkICC> upperRight = SkICC::Make(data->data(), data->size());
+ test_is_numerical_transfer_fn(r, upperRight.get(), false, referenceFn);
+}
+
+DEF_TEST(ICC_Adobe, r) {
+ // Test that the color spaces produced by our procedural Adobe factory, and the official
+ // Adobe ICC profile match exactly.
+ sk_sp<SkData> data = GetResourceAsData("icc_profiles/AdobeRGB1998.icc");
+ sk_sp<SkColorSpace> fromIcc = SkColorSpace::MakeICC(data->data(), data->size());
+ sk_sp<SkColorSpace> procedural = SkColorSpace::MakeRGB(g2Dot2_TransferFn,
+ SkColorSpace::kAdobeRGB_Gamut);
+ REPORTER_ASSERT(r, SkColorSpace::Equals(fromIcc.get(), procedural.get()));
+}
+
+static inline void test_write_icc(skiatest::Reporter* r, const SkColorSpaceTransferFn& fn,
+ const SkMatrix44& toXYZD50, bool writeToFile) {
+ sk_sp<SkData> profile = SkICC::WriteToICC(fn, toXYZD50);
+ if (writeToFile) {
+ SkFILEWStream stream("out.icc");
+ stream.write(profile->data(), profile->size());
+ }
+
+ sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(profile->data(), profile->size());
+ sk_sp<SkColorSpace> reference = SkColorSpace::MakeRGB(fn, toXYZD50);
+ REPORTER_ASSERT(r, SkColorSpace::Equals(reference.get(), colorSpace.get()));
+}
+
+DEF_TEST(ICC_WriteICC, r) {
+ SkColorSpaceTransferFn adobeFn;
+ adobeFn.fA = 1.0f;
+ adobeFn.fB = 0.0f;
+ adobeFn.fC = 0.0f;
+ adobeFn.fD = 0.0f;
+ adobeFn.fE = 0.0f;
+ adobeFn.fF = 0.0f;
+ adobeFn.fG = 2.2f;
+ SkMatrix44 adobeMatrix(SkMatrix44::kUninitialized_Constructor);
+ adobeMatrix.set3x3RowMajorf(gAdobeRGB_toXYZD50);
+ test_write_icc(r, adobeFn, adobeMatrix, false);
+
+ SkColorSpaceTransferFn srgbFn;
+ srgbFn.fA = 1.0f / 1.055f;
+ srgbFn.fB = 0.055f / 1.055f;
+ srgbFn.fC = 1.0f / 12.92f;
+ srgbFn.fD = 0.04045f;
+ srgbFn.fE = 0.0f;
+ srgbFn.fF = 0.0f;
+ srgbFn.fG = 2.4f;
+ SkMatrix44 srgbMatrix(SkMatrix44::kUninitialized_Constructor);
+ srgbMatrix.set3x3RowMajorf(gSRGB_toXYZD50);
+ test_write_icc(r, srgbFn, srgbMatrix, false);
+
+ SkString adobeTag = SkICCGetColorProfileTag(adobeFn, adobeMatrix);
+ SkString srgbTag = SkICCGetColorProfileTag(srgbFn, srgbMatrix);
+ REPORTER_ASSERT(r, adobeTag != srgbTag);
+ REPORTER_ASSERT(r, srgbTag.equals("sRGB"));
+ REPORTER_ASSERT(r, adobeTag.equals("AdobeRGB"));
+}
+
+static inline void test_raw_transfer_fn(skiatest::Reporter* r, SkICC* icc) {
+ SkICC::Tables tables;
+ bool result = icc->rawTransferFnData(&tables);
+ REPORTER_ASSERT(r, result);
+
+ REPORTER_ASSERT(r, 0.0f == tables.red()[0]);
+ REPORTER_ASSERT(r, 0.0f == tables.green()[0]);
+ REPORTER_ASSERT(r, 0.0f == tables.blue()[0]);
+ REPORTER_ASSERT(r, 1.0f == tables.red()[tables.fRed.fCount - 1]);
+ REPORTER_ASSERT(r, 1.0f == tables.green()[tables.fGreen.fCount - 1]);
+ REPORTER_ASSERT(r, 1.0f == tables.blue()[tables.fBlue.fCount - 1]);
+}
+
+class ICCTest {
+public:
+ static sk_sp<SkICC> MakeICC(sk_sp<SkColorSpace> space) {
+ return sk_sp<SkICC>(new SkICC(std::move(space)));
+ }
+ static sk_sp<SkICC> MakeICC(sk_sp<SkGammas> gammas) {
+ return MakeICC(sk_sp<SkColorSpace>(new SkColorSpace_XYZ(
+ kNonStandard_SkGammaNamed, std::move(gammas),
+ SkMatrix44(SkMatrix44::kIdentity_Constructor), nullptr)));
+ }
+};
+
+DEF_TEST(ICC_RawTransferFns, r) {
+ sk_sp<SkICC> srgb = ICCTest::MakeICC(SkColorSpace::MakeSRGB());
+ test_raw_transfer_fn(r, srgb.get());
+
+ // Lookup-table based gamma curves
+ constexpr size_t tableSize = 10;
+ void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize);
+ sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas(3));
+ for (int i = 0; i < 3; ++i) {
+ gammas->fType[i] = SkGammas::Type::kTable_Type;
+ gammas->fData[i].fTable.fSize = tableSize;
+ gammas->fData[i].fTable.fOffset = 0;
+ }
+
+ float* table = SkTAddOffset<float>(memory, sizeof(SkGammas));
+ table[0] = 0.00f;
+ table[1] = 0.05f;
+ table[2] = 0.10f;
+ table[3] = 0.15f;
+ table[4] = 0.25f;
+ table[5] = 0.35f;
+ table[6] = 0.45f;
+ table[7] = 0.60f;
+ table[8] = 0.75f;
+ table[9] = 1.00f;
+ sk_sp<SkICC> tbl = ICCTest::MakeICC(gammas);
+ test_raw_transfer_fn(r, tbl.get());
+
+ // Parametric gamma curves
+ memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkColorSpaceTransferFn));
+ gammas = sk_sp<SkGammas>(new (memory) SkGammas(3));
+ for (int i = 0; i < 3; ++i) {
+ gammas->fType[i] = SkGammas::Type::kParam_Type;
+ gammas->fData[i].fParamOffset = 0;
+ }
+
+ SkColorSpaceTransferFn* params = SkTAddOffset<SkColorSpaceTransferFn>
+ (memory, sizeof(SkGammas));
+
+ // Interval.
+ params->fD = 0.04045f;
+
+ // First equation:
+ params->fC = 1.0f / 12.92f;
+ params->fF = 0.0f;
+
+ // Second equation:
+ // Note that the function is continuous (it's actually sRGB).
+ params->fA = 1.0f / 1.055f;
+ params->fB = 0.055f / 1.055f;
+ params->fE = 0.0f;
+ params->fG = 2.4f;
+ sk_sp<SkICC> param = ICCTest::MakeICC(gammas);
+ test_raw_transfer_fn(r, param.get());
+
+ // Exponential gamma curves
+ gammas = sk_sp<SkGammas>(new SkGammas(3));
+ for (int i = 0; i < 3; ++i) {
+ gammas->fType[i] = SkGammas::Type::kValue_Type;
+ gammas->fData[i].fValue = 1.4f;
+ }
+ sk_sp<SkICC> exp = ICCTest::MakeICC(gammas);
+ test_raw_transfer_fn(r, exp.get());
+
+ gammas = sk_sp<SkGammas>(new SkGammas(3));
+ gammas->fType[0] = gammas->fType[1] = gammas->fType[2] = SkGammas::Type::kNamed_Type;
+ gammas->fData[0].fNamed = kSRGB_SkGammaNamed;
+ gammas->fData[1].fNamed = k2Dot2Curve_SkGammaNamed;
+ gammas->fData[2].fNamed = kLinear_SkGammaNamed;
+ sk_sp<SkICC> named = ICCTest::MakeICC(gammas);
+ test_raw_transfer_fn(r, named.get());
+
+ memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(float) * tableSize +
+ sizeof(SkColorSpaceTransferFn));
+ gammas = sk_sp<SkGammas>(new (memory) SkGammas(3));
+
+ table = SkTAddOffset<float>(memory, sizeof(SkGammas));
+ table[0] = 0.00f;
+ table[1] = 0.15f;
+ table[2] = 0.20f;
+ table[3] = 0.25f;
+ table[4] = 0.35f;
+ table[5] = 0.45f;
+ table[6] = 0.55f;
+ table[7] = 0.70f;
+ table[8] = 0.85f;
+ table[9] = 1.00f;
+
+ params = SkTAddOffset<SkColorSpaceTransferFn>(memory,
+ sizeof(SkGammas) + sizeof(float) * tableSize);
+ params->fA = 1.0f / 1.055f;
+ params->fB = 0.055f / 1.055f;
+ params->fC = 1.0f / 12.92f;
+ params->fD = 0.04045f;
+ params->fE = 0.0f;
+ params->fF = 0.0f;
+ params->fG = 2.4f;
+
+ gammas->fType[0] = SkGammas::Type::kValue_Type;
+ gammas->fData[0].fValue = 1.2f;
+
+ gammas->fType[1] = SkGammas::Type::kTable_Type;
+ gammas->fData[1].fTable.fSize = tableSize;
+ gammas->fData[1].fTable.fOffset = 0;
+
+ gammas->fType[2] = SkGammas::Type::kParam_Type;
+ gammas->fData[2].fParamOffset = sizeof(float) * tableSize;
+ sk_sp<SkICC> nonstd = ICCTest::MakeICC(gammas);
+ test_raw_transfer_fn(r, nonstd.get());
+
+ // Reverse order of table and exponent
+ gammas->fType[1] = SkGammas::Type::kValue_Type;
+ gammas->fData[1].fValue = 1.2f;
-#endif
+ gammas->fType[0] = SkGammas::Type::kTable_Type;
+ gammas->fData[0].fTable.fSize = tableSize;
+ gammas->fData[0].fTable.fOffset = 0;
+ sk_sp<SkICC> nonstd2 = ICCTest::MakeICC(gammas);
+ test_raw_transfer_fn(r, nonstd2.get());
+}