1 /*
2  * Copyright 2018 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 "../third_party/skcms/skcms.h"
9 #include "SkColorSpace.h"
10 #include "SkColorSpaceXformSteps.h"
11 #include "Test.h"
12 
DEF_TEST(SkColorSpaceXformSteps_vs_skcms,r)13 DEF_TEST(SkColorSpaceXformSteps_vs_skcms, r) {
14     auto srgb = SkColorSpace::MakeSRGB();
15     auto dp3  = SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
16 
17     skcms_ICCProfile srgb_profile;
18     srgb->toProfile(&srgb_profile);
19     skcms_ICCProfile dp3_profile;
20     dp3->toProfile(&dp3_profile);
21 
22     // These colors provide a good spread of interesting test cases.
23     SkColor colors[] = {
24         0xffff0000, 0x7fff0000, 0x7f7f0000,
25         0xff00ff00, 0x7f00ff00, 0x7f007f00,
26     };
27 
28     for (auto color : colors) {
29         auto bgra = skcms_PixelFormat_BGRA_8888,
30               f32 = skcms_PixelFormat_RGBA_ffff;
31         auto unpremul = skcms_AlphaFormat_Unpremul,
32                premul = skcms_AlphaFormat_PremulAsEncoded;
33 
34         float via_skcms[4];
35         skcms_Transform(&color,     bgra, unpremul, &srgb_profile,
36                         &via_skcms,  f32,   premul,  &dp3_profile,
37                         1);
38 
39         SkColorSpaceXformSteps steps(srgb.get(), kUnpremul_SkAlphaType,
40                                       dp3.get(),   kPremul_SkAlphaType);
41         float via_steps[4] = {
42             SkColorGetR(color) * (1 / 255.0f),
43             SkColorGetG(color) * (1 / 255.0f),
44             SkColorGetB(color) * (1 / 255.0f),
45             SkColorGetA(color) * (1 / 255.0f),
46         };
47         steps.apply(via_steps);
48 
49         REPORTER_ASSERT(r, fabsf(via_skcms[0] - via_steps[0]) <= 0.005f);
50         REPORTER_ASSERT(r, fabsf(via_skcms[1] - via_steps[1]) <= 0.005f);
51         REPORTER_ASSERT(r, fabsf(via_skcms[2] - via_steps[2]) <= 0.005f);
52         REPORTER_ASSERT(r, fabsf(via_skcms[3] - via_steps[3]) <= 0.0f);
53 
54         // Now go back using the other method's inverse transform
55         float steps_to_skcms[4];
56         skcms_Transform(via_steps,      f32, premul, &dp3_profile,
57                         steps_to_skcms, f32, premul, &srgb_profile,
58                         1);
59 
60         float skcms_to_steps[4] = { via_skcms[0], via_skcms[1], via_skcms[2], via_skcms[3] };
61         SkColorSpaceXformSteps inv_steps(dp3.get(), kPremul_SkAlphaType,
62                                         srgb.get(), kPremul_SkAlphaType);
63         inv_steps.apply(skcms_to_steps);
64 
65         REPORTER_ASSERT(r, fabsf(skcms_to_steps[0] - steps_to_skcms[0]) <= 0.005f);
66         REPORTER_ASSERT(r, fabsf(skcms_to_steps[1] - steps_to_skcms[1]) <= 0.005f);
67         REPORTER_ASSERT(r, fabsf(skcms_to_steps[2] - steps_to_skcms[2]) <= 0.005f);
68         REPORTER_ASSERT(r, fabsf(skcms_to_steps[3] - steps_to_skcms[3]) <= 0.0f);
69     }
70 }
71