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 #ifndef SkColorSpacePriv_DEFINED
8 #define SkColorSpacePriv_DEFINED
9 
10 #include <math.h>
11 
12 #include "include/core/SkColorSpace.h"
13 #include "include/private/SkFixed.h"
14 
15 #define SkColorSpacePrintf(...)
16 
17 // A gamut narrower than sRGB, useful for testing.
18 static constexpr skcms_Matrix3x3 gNarrow_toXYZD50 = {{
19     { 0.190974f,  0.404865f,  0.368380f },
20     { 0.114746f,  0.582937f,  0.302318f },
21     { 0.032925f,  0.153615f,  0.638669f },
22 }};
23 
color_space_almost_equal(float a,float b)24 static inline bool color_space_almost_equal(float a, float b) {
25     return SkTAbs(a - b) < 0.01f;
26 }
27 
28 // Let's use a stricter version for transfer functions.  Worst case, these are encoded
29 // in ICC format, which offers 16-bits of fractional precision.
transfer_fn_almost_equal(float a,float b)30 static inline bool transfer_fn_almost_equal(float a, float b) {
31     return SkTAbs(a - b) < 0.001f;
32 }
33 
34 // NOTE: All of this logic is copied from skcms.cc, and needs to be kept in sync.
35 
36 // Most transfer functions we work with are sRGBish.
37 // For exotic HDR transfer functions, we encode them using a tf.g that makes no sense,
38 // and repurpose the other fields to hold the parameters of the HDR functions.
39 enum TFKind { Bad_TF, sRGBish_TF, PQish_TF, HLGish_TF, HLGinvish_TF };
40 
classify_transfer_fn(const skcms_TransferFunction & tf)41 static inline TFKind classify_transfer_fn(const skcms_TransferFunction& tf) {
42     if (tf.g < 0 && (int)tf.g == tf.g) {
43         // TODO: sanity checks for PQ/HLG like we do for sRGBish.
44         switch (-(int)tf.g) {
45             case PQish_TF:     return PQish_TF;
46             case HLGish_TF:    return HLGish_TF;
47             case HLGinvish_TF: return HLGinvish_TF;
48         }
49         return Bad_TF;
50     }
51 
52     // Basic sanity checks for sRGBish transfer functions.
53     if (sk_float_isfinite(tf.a + tf.b + tf.c + tf.d + tf.e + tf.f + tf.g)
54             // a,c,d,g should be non-negative to make any sense.
55             && tf.a >= 0
56             && tf.c >= 0
57             && tf.d >= 0
58             && tf.g >= 0
59             // Raising a negative value to a fractional tf->g produces complex numbers.
60             && tf.a * tf.d + tf.b >= 0) {
61         return sRGBish_TF;
62     }
63 
64     return Bad_TF;
65 }
66 
is_almost_srgb(const skcms_TransferFunction & coeffs)67 static inline bool is_almost_srgb(const skcms_TransferFunction& coeffs) {
68     return transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.a, coeffs.a) &&
69            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.b, coeffs.b) &&
70            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.c, coeffs.c) &&
71            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.d, coeffs.d) &&
72            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.e, coeffs.e) &&
73            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.f, coeffs.f) &&
74            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.g, coeffs.g);
75 }
76 
is_almost_2dot2(const skcms_TransferFunction & coeffs)77 static inline bool is_almost_2dot2(const skcms_TransferFunction& coeffs) {
78     return transfer_fn_almost_equal(1.0f, coeffs.a) &&
79            transfer_fn_almost_equal(0.0f, coeffs.b) &&
80            transfer_fn_almost_equal(0.0f, coeffs.e) &&
81            transfer_fn_almost_equal(2.2f, coeffs.g) &&
82            coeffs.d <= 0.0f;
83 }
84 
is_almost_linear(const skcms_TransferFunction & coeffs)85 static inline bool is_almost_linear(const skcms_TransferFunction& coeffs) {
86     // OutputVal = InputVal ^ 1.0f
87     const bool linearExp =
88             transfer_fn_almost_equal(1.0f, coeffs.a) &&
89             transfer_fn_almost_equal(0.0f, coeffs.b) &&
90             transfer_fn_almost_equal(0.0f, coeffs.e) &&
91             transfer_fn_almost_equal(1.0f, coeffs.g) &&
92             coeffs.d <= 0.0f;
93 
94     // OutputVal = 1.0f * InputVal
95     const bool linearFn =
96             transfer_fn_almost_equal(1.0f, coeffs.c) &&
97             transfer_fn_almost_equal(0.0f, coeffs.f) &&
98             coeffs.d >= 1.0f;
99 
100     return linearExp || linearFn;
101 }
102 
103 // Return raw pointers to commonly used SkColorSpaces.
104 // No need to ref/unref these, but if you do, do it in pairs.
105 SkColorSpace* sk_srgb_singleton();
106 SkColorSpace* sk_srgb_linear_singleton();
107 
108 #endif  // SkColorSpacePriv_DEFINED
109