2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #ifndef SkColorSpacePriv_DEFINED
8 #define SkColorSpacePriv_DEFINED
12 #include "include/core/SkColorSpace.h"
13 #include "include/private/SkFixed.h"
14 #include "src/core/SkVM_fwd.h"
16 #define SkColorSpacePrintf(...)
18 // A gamut narrower than sRGB, useful for testing.
19 static constexpr skcms_Matrix3x3 gNarrow_toXYZD50 = {{
20 { 0.190974f, 0.404865f, 0.368380f },
21 { 0.114746f, 0.582937f, 0.302318f },
22 { 0.032925f, 0.153615f, 0.638669f },
25 static inline bool color_space_almost_equal(float a, float b) {
26 return SkTAbs(a - b) < 0.01f;
29 // Let's use a stricter version for transfer functions. Worst case, these are encoded
30 // in ICC format, which offers 16-bits of fractional precision.
31 static inline bool transfer_fn_almost_equal(float a, float b) {
32 return SkTAbs(a - b) < 0.001f;
35 // NOTE: All of this logic is copied from skcms.cc, and needs to be kept in sync.
37 // Most transfer functions we work with are sRGBish.
38 // For exotic HDR transfer functions, we encode them using a tf.g that makes no sense,
39 // and repurpose the other fields to hold the parameters of the HDR functions.
40 enum TFKind { Bad_TF, sRGBish_TF, PQish_TF, HLGish_TF, HLGinvish_TF };
42 static inline TFKind classify_transfer_fn(const skcms_TransferFunction& tf) {
43 if (tf.g < 0 && (int)tf.g == tf.g) {
44 // TODO: safety checks for PQ/HLG like we do for sRGBish.
46 case -PQish_TF: return PQish_TF;
47 case -HLGish_TF: return HLGish_TF;
48 case -HLGinvish_TF: return HLGinvish_TF;
53 // Basic safety checks for sRGBish transfer functions.
54 if (sk_float_isfinite(tf.a + tf.b + tf.c + tf.d + tf.e + tf.f + tf.g)
55 // a,c,d,g should be non-negative to make any sense.
60 // Raising a negative value to a fractional tf->g produces complex numbers.
61 && tf.a * tf.d + tf.b >= 0) {
68 static inline bool is_almost_srgb(const skcms_TransferFunction& coeffs) {
69 return transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.a, coeffs.a) &&
70 transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.b, coeffs.b) &&
71 transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.c, coeffs.c) &&
72 transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.d, coeffs.d) &&
73 transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.e, coeffs.e) &&
74 transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.f, coeffs.f) &&
75 transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.g, coeffs.g);
78 static inline bool is_almost_2dot2(const skcms_TransferFunction& coeffs) {
79 return transfer_fn_almost_equal(1.0f, coeffs.a) &&
80 transfer_fn_almost_equal(0.0f, coeffs.b) &&
81 transfer_fn_almost_equal(0.0f, coeffs.e) &&
82 transfer_fn_almost_equal(2.2f, coeffs.g) &&
86 static inline bool is_almost_linear(const skcms_TransferFunction& coeffs) {
87 // OutputVal = InputVal ^ 1.0f
88 const bool linearExp =
89 transfer_fn_almost_equal(1.0f, coeffs.a) &&
90 transfer_fn_almost_equal(0.0f, coeffs.b) &&
91 transfer_fn_almost_equal(0.0f, coeffs.e) &&
92 transfer_fn_almost_equal(1.0f, coeffs.g) &&
95 // OutputVal = 1.0f * InputVal
97 transfer_fn_almost_equal(1.0f, coeffs.c) &&
98 transfer_fn_almost_equal(0.0f, coeffs.f) &&
101 return linearExp || linearFn;
104 skvm::F32 sk_program_transfer_fn(
106 skvm::F32 G, skvm::F32 A, skvm::F32 B, skvm::F32 C, skvm::F32 D, skvm::F32 E, skvm::F32 F);
108 skvm::Color sk_program_transfer_fn(skvm::Builder*, skvm::Uniforms*,
109 const skcms_TransferFunction&, skvm::Color);
111 // Return raw pointers to commonly used SkColorSpaces.
112 // No need to ref/unref these, but if you do, do it in pairs.
113 SkColorSpace* sk_srgb_singleton();
114 SkColorSpace* sk_srgb_linear_singleton();
116 #endif // SkColorSpacePriv_DEFINED