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 skcms_TransferFunction tf;
118 memcpy(&tf, &srcTF, 7*sizeof(float));
119
120 rgba[0] = skcms_TransferFunction_eval(&tf, rgba[0]);
121 rgba[1] = skcms_TransferFunction_eval(&tf, rgba[1]);
122 rgba[2] = skcms_TransferFunction_eval(&tf, rgba[2]);
123 }
124 if (flags.gamut_transform) {
125 float temp[3] = { rgba[0], rgba[1], rgba[2] };
126 for (int i = 0; i < 3; ++i) {
127 rgba[i] = src_to_dst_matrix[ i] * temp[0] +
128 src_to_dst_matrix[3 + i] * temp[1] +
129 src_to_dst_matrix[6 + i] * temp[2];
130 }
131 }
132 if (flags.encode) {
133 skcms_TransferFunction tf;
134 memcpy(&tf, &dstTFInv, 7*sizeof(float));
135
136 rgba[0] = skcms_TransferFunction_eval(&tf, rgba[0]);
137 rgba[1] = skcms_TransferFunction_eval(&tf, rgba[1]);
138 rgba[2] = skcms_TransferFunction_eval(&tf, rgba[2]);
139 }
140 if (flags.premul) {
141 rgba[0] *= rgba[3];
142 rgba[1] *= rgba[3];
143 rgba[2] *= rgba[3];
144 }
145 }
146
apply(SkRasterPipeline * p,bool src_is_normalized) const147 void SkColorSpaceXformSteps::apply(SkRasterPipeline* p, bool src_is_normalized) const {
148 #if defined(SK_LEGACY_SRGB_STAGE_CHOICE)
149 src_is_normalized = true;
150 #endif
151 if (flags.unpremul) { p->append(SkRasterPipeline::unpremul); }
152 if (flags.linearize) {
153 if (src_is_normalized && srcTF_is_sRGB) {
154 p->append(SkRasterPipeline::from_srgb);
155 } else if (srcTF.a == 1 &&
156 srcTF.b == 0 &&
157 srcTF.c == 0 &&
158 srcTF.d == 0 &&
159 srcTF.e == 0 &&
160 srcTF.f == 0) {
161 p->append(SkRasterPipeline::gamma_, &srcTF.g);
162 } else {
163 p->append(SkRasterPipeline::parametric, &srcTF);
164 }
165 }
166 if (flags.gamut_transform) {
167 p->append(SkRasterPipeline::matrix_3x3, &src_to_dst_matrix);
168 }
169 if (flags.encode) {
170 if (src_is_normalized && dstTF_is_sRGB) {
171 p->append(SkRasterPipeline::to_srgb);
172 } else if (dstTFInv.a == 1 &&
173 dstTFInv.b == 0 &&
174 dstTFInv.c == 0 &&
175 dstTFInv.d == 0 &&
176 dstTFInv.e == 0 &&
177 dstTFInv.f == 0) {
178 p->append(SkRasterPipeline::gamma_, &dstTFInv.g);
179 } else {
180 p->append(SkRasterPipeline::parametric, &dstTFInv);
181 }
182 }
183 if (flags.premul) { p->append(SkRasterPipeline::premul); }
184 }
185
186