• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "include/core/SkColorSpace.h"
11 #include "include/private/base/SkTemplates.h"
12 #include "modules/skcms/skcms.h"
13 
14 namespace skvm {
15 class Builder;
16 struct Color;
17 struct F32;
18 struct Uniforms;
19 }
20 
21 // A gamut narrower than sRGB, useful for testing.
22 static constexpr skcms_Matrix3x3 gNarrow_toXYZD50 = {{
23     { 0.190974f,  0.404865f,  0.368380f },
24     { 0.114746f,  0.582937f,  0.302318f },
25     { 0.032925f,  0.153615f,  0.638669f },
26 }};
27 
color_space_almost_equal(float a,float b)28 static inline bool color_space_almost_equal(float a, float b) {
29     return SkTAbs(a - b) < 0.01f;
30 }
31 
32 // Let's use a stricter version for transfer functions.  Worst case, these are encoded
33 // in ICC format, which offers 16-bits of fractional precision.
transfer_fn_almost_equal(float a,float b)34 static inline bool transfer_fn_almost_equal(float a, float b) {
35     return SkTAbs(a - b) < 0.001f;
36 }
37 
is_almost_srgb(const skcms_TransferFunction & coeffs)38 static inline bool is_almost_srgb(const skcms_TransferFunction& coeffs) {
39     return transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.a, coeffs.a) &&
40            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.b, coeffs.b) &&
41            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.c, coeffs.c) &&
42            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.d, coeffs.d) &&
43            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.e, coeffs.e) &&
44            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.f, coeffs.f) &&
45            transfer_fn_almost_equal(SkNamedTransferFn::kSRGB.g, coeffs.g);
46 }
47 
is_almost_2dot2(const skcms_TransferFunction & coeffs)48 static inline bool is_almost_2dot2(const skcms_TransferFunction& coeffs) {
49     return transfer_fn_almost_equal(1.0f, coeffs.a) &&
50            transfer_fn_almost_equal(0.0f, coeffs.b) &&
51            transfer_fn_almost_equal(0.0f, coeffs.e) &&
52            transfer_fn_almost_equal(2.2f, coeffs.g) &&
53            coeffs.d <= 0.0f;
54 }
55 
is_almost_linear(const skcms_TransferFunction & coeffs)56 static inline bool is_almost_linear(const skcms_TransferFunction& coeffs) {
57     // OutputVal = InputVal ^ 1.0f
58     const bool linearExp =
59             transfer_fn_almost_equal(1.0f, coeffs.a) &&
60             transfer_fn_almost_equal(0.0f, coeffs.b) &&
61             transfer_fn_almost_equal(0.0f, coeffs.e) &&
62             transfer_fn_almost_equal(1.0f, coeffs.g) &&
63             coeffs.d <= 0.0f;
64 
65     // OutputVal = 1.0f * InputVal
66     const bool linearFn =
67             transfer_fn_almost_equal(1.0f, coeffs.c) &&
68             transfer_fn_almost_equal(0.0f, coeffs.f) &&
69             coeffs.d >= 1.0f;
70 
71     return linearExp || linearFn;
72 }
73 
74 skvm::F32 sk_program_transfer_fn(
75     skvm::F32 v, skcms_TFType,
76     skvm::F32 G, skvm::F32 A, skvm::F32 B, skvm::F32 C, skvm::F32 D, skvm::F32 E, skvm::F32 F);
77 
78 skvm::Color sk_program_transfer_fn(skvm::Builder*, skvm::Uniforms*,
79                                    const skcms_TransferFunction&, skvm::Color);
80 
81 // Return raw pointers to commonly used SkColorSpaces.
82 // No need to ref/unref these, but if you do, do it in pairs.
83 SkColorSpace* sk_srgb_singleton();
84 SkColorSpace* sk_srgb_linear_singleton();
85 
86 #endif  // SkColorSpacePriv_DEFINED
87