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 "include/third_party/skcms/skcms.h"
9 #include "src/core/SkColorSpacePriv.h"
10 #include "src/core/SkColorSpaceXformSteps.h"
11 #include "src/core/SkRasterPipeline.h"
12 
13 // TODO(mtklein): explain the logic of this file
14 
Required(SkColorSpace * src,SkColorSpace * dst)15 bool SkColorSpaceXformSteps::Required(SkColorSpace* src, SkColorSpace* dst) {
16     // Any SkAlphaType will work fine here as long as we use the same one.
17     SkAlphaType at = kPremul_SkAlphaType;
18     return 0 != SkColorSpaceXformSteps(src, at,
19                                        dst, at).flags.mask();
20     // TODO(mtklein): quicker impl. that doesn't construct an SkColorSpaceXformSteps?
21 }
22 
SkColorSpaceXformSteps(SkColorSpace * src,SkAlphaType srcAT,SkColorSpace * dst,SkAlphaType dstAT)23 SkColorSpaceXformSteps::SkColorSpaceXformSteps(SkColorSpace* src, SkAlphaType srcAT,
24                                                SkColorSpace* dst, SkAlphaType dstAT) {
25     // Opaque outputs are treated as the same alpha type as the source input.
26     // TODO: we'd really like to have a good way of explaining why we think this is useful.
27     if (dstAT == kOpaque_SkAlphaType) {
28         dstAT =  srcAT;
29     }
30 
31     // We have some options about what to do with null src or dst here.
32     // This pair seems to be the most consistent with legacy expectations.
33     if (!src) { src = sk_srgb_singleton(); }
34     if (!dst) { dst = src; }
35 
36     if (src->hash() == dst->hash() && srcAT == dstAT) {
37         SkASSERT(SkColorSpace::Equals(src,dst));
38         return;
39     }
40 
41     this->flags.unpremul        = srcAT == kPremul_SkAlphaType;
42     this->flags.linearize       = !src->gammaIsLinear();
43     this->flags.gamut_transform = src->toXYZD50Hash() != dst->toXYZD50Hash();
44     this->flags.encode          = !dst->gammaIsLinear();
45     this->flags.premul          = srcAT != kOpaque_SkAlphaType && dstAT == kPremul_SkAlphaType;
46 
47     if (this->flags.gamut_transform) {
48         float row_major[9];  // TODO: switch src_to_dst_matrix to row-major
49         src->gamutTransformTo(dst, row_major);
50 
51         this->src_to_dst_matrix[0] = row_major[0];
52         this->src_to_dst_matrix[1] = row_major[3];
53         this->src_to_dst_matrix[2] = row_major[6];
54 
55         this->src_to_dst_matrix[3] = row_major[1];
56         this->src_to_dst_matrix[4] = row_major[4];
57         this->src_to_dst_matrix[5] = row_major[7];
58 
59         this->src_to_dst_matrix[6] = row_major[2];
60         this->src_to_dst_matrix[7] = row_major[5];
61         this->src_to_dst_matrix[8] = row_major[8];
62     } else {
63     #ifdef SK_DEBUG
64         skcms_Matrix3x3 srcM, dstM;
65         src->toXYZD50(&srcM);
66         dst->toXYZD50(&dstM);
67         SkASSERT(0 == memcmp(&srcM, &dstM, 9*sizeof(float)) && "Hash collision");
68     #endif
69     }
70 
71     // Fill out all the transfer functions we'll use.
72     src->   transferFn(&this->srcTF   .g);
73     dst->invTransferFn(&this->dstTFInv.g);
74 
75     this->srcTF_is_sRGB = src->gammaCloseToSRGB();
76     this->dstTF_is_sRGB = dst->gammaCloseToSRGB();
77 
78     // If we linearize then immediately reencode with the same transfer function, skip both.
79     if ( this->flags.linearize       &&
80         !this->flags.gamut_transform &&
81          this->flags.encode          &&
82          src->transferFnHash() == dst->transferFnHash())
83     {
84     #ifdef SK_DEBUG
85         float dstTF[7];
86         dst->transferFn(dstTF);
87         for (int i = 0; i < 7; i++) {
88             SkASSERT( (&srcTF.g)[i] == dstTF[i] && "Hash collision" );
89         }
90     #endif
91         this->flags.linearize  = false;
92         this->flags.encode     = false;
93     }
94 
95     // Skip unpremul...premul if there are no non-linear operations between.
96     if ( this->flags.unpremul   &&
97         !this->flags.linearize  &&
98         !this->flags.encode     &&
99          this->flags.premul)
100     {
101         this->flags.unpremul = false;
102         this->flags.premul   = false;
103     }
104 }
105 
apply(float * rgba) const106 void SkColorSpaceXformSteps::apply(float* rgba) const {
107     if (flags.unpremul) {
108         // I don't know why isfinite(x) stopped working on the Chromecast bots...
109         auto is_finite = [](float x) { return x*0 == 0; };
110 
111         float invA = is_finite(1.0f / rgba[3]) ? 1.0f / rgba[3] : 0;
112         rgba[0] *= invA;
113         rgba[1] *= invA;
114         rgba[2] *= invA;
115     }
116     if (flags.linearize) {
117         rgba[0] = skcms_TransferFunction_eval(&srcTF, rgba[0]);
118         rgba[1] = skcms_TransferFunction_eval(&srcTF, rgba[1]);
119         rgba[2] = skcms_TransferFunction_eval(&srcTF, rgba[2]);
120     }
121     if (flags.gamut_transform) {
122         float temp[3] = { rgba[0], rgba[1], rgba[2] };
123         for (int i = 0; i < 3; ++i) {
124             rgba[i] = src_to_dst_matrix[    i] * temp[0] +
125                       src_to_dst_matrix[3 + i] * temp[1] +
126                       src_to_dst_matrix[6 + i] * temp[2];
127         }
128     }
129     if (flags.encode) {
130         rgba[0] = skcms_TransferFunction_eval(&dstTFInv, rgba[0]);
131         rgba[1] = skcms_TransferFunction_eval(&dstTFInv, rgba[1]);
132         rgba[2] = skcms_TransferFunction_eval(&dstTFInv, rgba[2]);
133     }
134     if (flags.premul) {
135         rgba[0] *= rgba[3];
136         rgba[1] *= rgba[3];
137         rgba[2] *= rgba[3];
138     }
139 }
140 
apply(SkRasterPipeline * p,bool src_is_normalized) const141 void SkColorSpaceXformSteps::apply(SkRasterPipeline* p, bool src_is_normalized) const {
142 #if defined(SK_LEGACY_SRGB_STAGE_CHOICE)
143     src_is_normalized = true;
144 #endif
145     if (flags.unpremul) { p->append(SkRasterPipeline::unpremul); }
146     if (flags.linearize) {
147         if (src_is_normalized && srcTF_is_sRGB) {
148             p->append(SkRasterPipeline::from_srgb);
149         } else {
150             p->append_transfer_function(srcTF);
151         }
152     }
153     if (flags.gamut_transform) {
154         p->append(SkRasterPipeline::matrix_3x3, &src_to_dst_matrix);
155     }
156     if (flags.encode) {
157         if (src_is_normalized && dstTF_is_sRGB) {
158             p->append(SkRasterPipeline::to_srgb);
159         } else {
160             p->append_transfer_function(dstTFInv);
161         }
162     }
163     if (flags.premul) { p->append(SkRasterPipeline::premul); }
164 }
165 
166