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#include "src/core/SkVM_fwd.h"
15
16#define SkColorSpacePrintf(...)
17
18// A gamut narrower than sRGB, useful for testing.
19static 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 },
23}};
24
25static inline bool color_space_almost_equal(float a, float b) {
26 return SkTAbs(a - b) < 0.01f;
27}
28
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.
31static inline bool transfer_fn_almost_equal(float a, float b) {
32 return SkTAbs(a - b) < 0.001f;
33}
34
35// NOTE: All of this logic is copied from skcms.cc, and needs to be kept in sync.
36
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.
40enum TFKind { Bad_TF, sRGBish_TF, PQish_TF, HLGish_TF, HLGinvish_TF };
41
42static inline TFKind classify_transfer_fn(const skcms_TransferFunction& tf) {
43 if (tf.g < 0 && (int)tf.g == tf.g) {
44 // TODO: sanity checks for PQ/HLG like we do for sRGBish.
45 switch ((int)tf.g) {
46 case -PQish_TF: return PQish_TF;
47 case -HLGish_TF: return HLGish_TF;
48 case -HLGinvish_TF: return HLGinvish_TF;
49 }
50 return Bad_TF;
51 }
52
53 // Basic sanity 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.
56 && tf.a >= 0
57 && tf.c >= 0
58 && tf.d >= 0
59 && tf.g >= 0
60 // Raising a negative value to a fractional tf->g produces complex numbers.
61 && tf.a * tf.d + tf.b >= 0) {
62 return sRGBish_TF;
63 }
64
65 return Bad_TF;
66}
67
68static 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);
76}
77
78static 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) &&
83 coeffs.d <= 0.0f;
84}
85
86static 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) &&
93 coeffs.d <= 0.0f;
94
95 // OutputVal = 1.0f * InputVal
96 const bool linearFn =
97 transfer_fn_almost_equal(1.0f, coeffs.c) &&
98 transfer_fn_almost_equal(0.0f, coeffs.f) &&
99 coeffs.d >= 1.0f;
100
101 return linearExp || linearFn;
102}
103
104skvm::Color sk_program_transfer_fn(skvm::Builder*, skvm::Uniforms*,
105 const skcms_TransferFunction&, skvm::Color);
106
107// Return raw pointers to commonly used SkColorSpaces.
108// No need to ref/unref these, but if you do, do it in pairs.
109SkColorSpace* sk_srgb_singleton();
110SkColorSpace* sk_srgb_linear_singleton();
111
112#endif // SkColorSpacePriv_DEFINED
113