| 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 "src/core/SkColorSpaceXformSteps.h" |
| 9 |
|
| 10 |
#include "include/core/SkAlphaType.h" |
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/core/SkTypes.h" |
| 14 |
#include "include/private/base/SkFloatingPoint.h" |
| 15 |
#include "modules/skcms/skcms.h" |
| 16 |
#include "src/core/SkColorSpacePriv.h" |
| 17 |
#include "src/core/SkRasterPipeline.h" |
| 18 |
#include "src/core/SkRasterPipelineOpList.h" |
| 19 |
|
| 20 |
#include <cmath> |
| 21 |
#include <cstring> |
| 22 |
|
| 23 |
// See skia.org/docs/user/color (== site/docs/user/color.md). |
| 24 |
|
| 25 |
// Compute the Y vector for the HLG OOTF in the primaries of a specified color space. The value |
| 26 |
// is specified in Rec2020 primaries in ITU-R BT.2100. |
| 27 |
static void set_ootf_Y(const SkColorSpace* cs, float* Y) { |
| 28 |
skcms_Matrix3x3 m; |
| 29 |
cs->gamutTransformTo( |
| 30 |
SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear, SkNamedGamut::kRec2020).get(), |
| 31 |
&m); |
| 32 |
constexpr float Y_rec2020[3] = {0.262700f, 0.678000f, 0.059300f}; |
| 33 |
for (int i = 0; i < 3; ++i) { |
| 34 |
Y[i] = 0.f; |
| 35 |
for (int j = 0; j < 3; ++j) { |
| 36 |
Y[i] += m.vals[j][i] * Y_rec2020[j]; |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
SkColorSpaceXformSteps::SkColorSpaceXformSteps(const SkColorSpace* src, SkAlphaType srcAT, |
| 42 |
const SkColorSpace* dst, SkAlphaType dstAT) { |
| 43 |
// Opaque outputs are treated as the same alpha type as the source input. |
| 44 |
// TODO: we'd really like to have a good way of explaining why we think this is useful. |
| 45 |
if (dstAT == kOpaque_SkAlphaType) { |
| 46 |
dstAT = srcAT; |
| 47 |
} |
| 48 |
|
| 49 |
// We have some options about what to do with null src or dst here. |
| 50 |
// This pair seems to be the most consistent with legacy expectations. |
| 51 |
if (!src) { src = sk_srgb_singleton(); } |
| 52 |
if (!dst) { dst = src; } |
| 53 |
|
| 54 |
if (src->hash() == dst->hash() && srcAT == dstAT) { |
| 55 |
SkASSERT(SkColorSpace::Equals(src,dst)); |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
skcms_TransferFunction srcTrfn; |
| 60 |
src->transferFn(&srcTrfn); |
| 61 |
skcms_TransferFunction dstTrfn; |
| 62 |
dst->transferFn(&dstTrfn); |
| 63 |
|
| 64 |
// The scale factor is the amount that values in linear space will be scaled to accommodate |
| 65 |
// peak luminance and HDR reference white luminance. |
| 66 |
float scaleFactor = 1.f; |
| 67 |
|
| 68 |
// TODO(https://issues.skia.org/issues/420956739): Inline the constants for PQ and HLG transfer |
| 69 |
// functions when the PQish and HLGish transfer functions are no longer in use. |
| 70 |
static constexpr skcms_TransferFunction kPQish = |
| 71 |
{-2.0f, -107/128.0f, 1.0f, 32/2523.0f, 2413/128.0f, -2392/128.0f, 8192/1305.0f }; |
| 72 |
static constexpr skcms_TransferFunction kHLGish = |
| 73 |
{-3.0f, 2.0f, 2.0f, 1/0.17883277f, 0.28466892f, 0.55991073f, 0.0f }; |
| 74 |
skcms_TFType srcTfType = skcms_TransferFunction_getType(&srcTrfn); |
| 75 |
switch (srcTfType) { |
| 76 |
case skcms_TFType_PQ: |
| 77 |
// PQ is always scaled by a peak luminance of 10,000 nits, then divided by the HDR |
| 78 |
// reference white luminance (a). |
| 79 |
scaleFactor *= 10000.f / srcTrfn.a; |
| 80 |
// Use the default PQish transfer function. |
| 81 |
this->fSrcTF = kPQish; |
| 82 |
this->fFlags.linearize = true; |
| 83 |
break; |
| 84 |
case skcms_TFType_HLG: |
| 85 |
// HLG is scaled by the peak luminance (b), then divided by the HDR reference white |
| 86 |
// luminance (a). |
| 87 |
scaleFactor *= srcTrfn.b / srcTrfn.a; |
| 88 |
this->fFlags.linearize = true; |
| 89 |
// Use the HLGish transfer function scaled by 1/12. |
| 90 |
this->fSrcTF = kHLGish; |
| 91 |
this->fSrcTF.f = 1/12.f - 1.f; |
| 92 |
// If the system gamma is not 1.0, then compute the parameters for the OOTF. |
| 93 |
if (srcTrfn.c != 1.f) { |
| 94 |
this->fFlags.src_ootf = true; |
| 95 |
this->fSrcOotf[3] = srcTrfn.c - 1.f; |
| 96 |
set_ootf_Y(src, this->fSrcOotf); |
| 97 |
} |
| 98 |
break; |
| 99 |
default: |
| 100 |
this->fFlags.linearize = memcmp(&srcTrfn, &SkNamedTransferFn::kLinear, sizeof(srcTrfn)) != 0; |
| 101 |
if (this->fFlags.linearize) { |
| 102 |
src->transferFn(&this->fSrcTF); |
| 103 |
} |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
skcms_TFType dstTfType = skcms_TransferFunction_getType(&dstTrfn); |
| 108 |
switch (dstTfType) { |
| 109 |
case skcms_TFType_PQ: |
| 110 |
// This is the inverse of the treatment of source PQ. |
| 111 |
scaleFactor /= 10000.f / dstTrfn.a; |
| 112 |
this->fFlags.encode = true; |
| 113 |
this->fDstTFInv = kPQish; |
| 114 |
skcms_TransferFunction_invert(&this->fDstTFInv, &this->fDstTFInv); |
| 115 |
break; |
| 116 |
case skcms_TFType_HLG: |
| 117 |
// This is the inverse of the treatment of source HLG. |
| 118 |
scaleFactor /= dstTrfn.b / dstTrfn.a; |
| 119 |
this->fFlags.encode = true; |
| 120 |
this->fDstTFInv = kHLGish; |
| 121 |
this->fDstTFInv.f = 1/12.f - 1.f; |
| 122 |
skcms_TransferFunction_invert(&this->fDstTFInv, &this->fDstTFInv); |
| 123 |
if (dstTrfn.c != 1.f) { |
| 124 |
this->fFlags.dst_ootf = true; |
| 125 |
this->fDstOotf[3] = 1/dstTrfn.c - 1.f; |
| 126 |
set_ootf_Y(dst, this->fDstOotf); |
| 127 |
} |
| 128 |
break; |
| 129 |
default: |
| 130 |
this->fFlags.encode = memcmp(&dstTrfn, &SkNamedTransferFn::kLinear, sizeof(dstTrfn)) != 0; |
| 131 |
if (this->fFlags.encode) { |
| 132 |
dst->invTransferFn(&this->fDstTFInv); |
| 133 |
} |
| 134 |
break; |
| 135 |
} |
| 136 |
|
| 137 |
this->fFlags.unpremul = srcAT == kPremul_SkAlphaType; |
| 138 |
this->fFlags.gamut_transform = src->toXYZD50Hash() != dst->toXYZD50Hash() || |
| 139 |
scaleFactor != 1.f; |
| 140 |
this->fFlags.premul = srcAT != kOpaque_SkAlphaType && dstAT == kPremul_SkAlphaType; |
| 141 |
|
| 142 |
if (this->fFlags.gamut_transform) { |
| 143 |
skcms_Matrix3x3 src_to_dst; // TODO: switch fSrcToDstMatrix to row-major |
| 144 |
src->gamutTransformTo(dst, &src_to_dst); |
| 145 |
|
| 146 |
this->fSrcToDstMatrix[0] = src_to_dst.vals[0][0] * scaleFactor; |
| 147 |
this->fSrcToDstMatrix[1] = src_to_dst.vals[1][0] * scaleFactor; |
| 148 |
this->fSrcToDstMatrix[2] = src_to_dst.vals[2][0] * scaleFactor; |
| 149 |
|
| 150 |
this->fSrcToDstMatrix[3] = src_to_dst.vals[0][1] * scaleFactor; |
| 151 |
this->fSrcToDstMatrix[4] = src_to_dst.vals[1][1] * scaleFactor; |
| 152 |
this->fSrcToDstMatrix[5] = src_to_dst.vals[2][1] * scaleFactor; |
| 153 |
|
| 154 |
this->fSrcToDstMatrix[6] = src_to_dst.vals[0][2] * scaleFactor; |
| 155 |
this->fSrcToDstMatrix[7] = src_to_dst.vals[1][2] * scaleFactor; |
| 156 |
this->fSrcToDstMatrix[8] = src_to_dst.vals[2][2] * scaleFactor; |
| 157 |
} else { |
| 158 |
#ifdef SK_DEBUG |
| 159 |
skcms_Matrix3x3 srcM, dstM; |
| 160 |
src->toXYZD50(&srcM); |
| 161 |
dst->toXYZD50(&dstM); |
| 162 |
SkASSERT(0 == memcmp(&srcM, &dstM, 9*sizeof(float)) && "Hash collision"); |
| 163 |
#endif |
| 164 |
} |
| 165 |
|
| 166 |
// If the source and destination OOTFs cancel each other out, skip both. |
| 167 |
if ( this->fFlags.src_ootf && |
| 168 |
!this->fFlags.gamut_transform && |
| 169 |
this->fFlags.dst_ootf) { |
| 170 |
// If there is no gamut transform, then the r,g,b coefficients for the |
| 171 |
// OOTFs must be the same. |
| 172 |
SkASSERT(0 == memcmp(&this->fSrcOotf, &this->fDstOotf, 3*sizeof(float))); |
| 173 |
// If the gammas cancel out, then remove the steps. |
| 174 |
if ((this->fSrcOotf[3] + 1.f) * (this->fDstOotf[3] + 1.f) == 1.f) { |
| 175 |
this->fFlags.src_ootf = false; |
| 176 |
this->fFlags.dst_ootf = false; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// If we linearize then immediately reencode with the same transfer function, skip both. |
| 181 |
if ( this->fFlags.linearize && |
| 182 |
!this->fFlags.src_ootf && |
| 183 |
!this->fFlags.gamut_transform && |
| 184 |
!this->fFlags.dst_ootf && |
| 185 |
this->fFlags.encode && |
| 186 |
src->transferFnHash() == dst->transferFnHash()) |
| 187 |
{ |
| 188 |
#ifdef SK_DEBUG |
| 189 |
// PQ and HLG types use PQish and HLGish for fSrcTF, so this check is not valid for them. |
| 190 |
if (srcTfType != skcms_TFType_PQ && srcTfType != skcms_TFType_HLG) { |
| 191 |
skcms_TransferFunction dstTF; |
| 192 |
dst->transferFn(&dstTF); |
| 193 |
for (int i = 0; i < 7; i++) { |
| 194 |
SkASSERT( (&fSrcTF.g)[i] == (&dstTF.g)[i] && "Hash collision" ); |
| 195 |
} |
| 196 |
} |
| 197 |
#endif |
| 198 |
this->fFlags.linearize = false; |
| 199 |
this->fFlags.encode = false; |
| 200 |
} |
| 201 |
|
| 202 |
// Skip unpremul...premul if there are no non-linear operations between. |
| 203 |
if ( this->fFlags.unpremul && |
| 204 |
!this->fFlags.linearize && |
| 205 |
!this->fFlags.encode && |
| 206 |
this->fFlags.premul) |
| 207 |
{ |
| 208 |
this->fFlags.unpremul = false; |
| 209 |
this->fFlags.premul = false; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
void SkColorSpaceXformSteps::apply(float* rgba) const { |
| 214 |
if (this->fFlags.unpremul) { |
| 215 |
// I don't know why isfinite(x) stopped working on the Chromecast bots... |
| 216 |
auto is_finite = [](float x) { return x*0 == 0; }; |
| 217 |
|
| 218 |
float invA = sk_ieee_float_divide(1.0f, rgba[3]); |
| 219 |
invA = is_finite(invA) ? invA : 0; |
| 220 |
rgba[0] *= invA; |
| 221 |
rgba[1] *= invA; |
| 222 |
rgba[2] *= invA; |
| 223 |
} |
| 224 |
if (this->fFlags.linearize) { |
| 225 |
rgba[0] = skcms_TransferFunction_eval(&fSrcTF, rgba[0]); |
| 226 |
rgba[1] = skcms_TransferFunction_eval(&fSrcTF, rgba[1]); |
| 227 |
rgba[2] = skcms_TransferFunction_eval(&fSrcTF, rgba[2]); |
| 228 |
} |
| 229 |
if (this->fFlags.src_ootf) { |
| 230 |
const float Y = fSrcOotf[0] * rgba[0] + |
| 231 |
fSrcOotf[1] * rgba[1] + |
| 232 |
fSrcOotf[2] * rgba[2]; |
| 233 |
const float Y_to_gamma_minus_1 = std::pow(Y, fSrcOotf[3]); |
| 234 |
rgba[0] *= Y_to_gamma_minus_1; |
| 235 |
rgba[1] *= Y_to_gamma_minus_1; |
| 236 |
rgba[2] *= Y_to_gamma_minus_1; |
| 237 |
} |
| 238 |
if (this->fFlags.gamut_transform) { |
| 239 |
float temp[3] = { rgba[0], rgba[1], rgba[2] }; |
| 240 |
for (int i = 0; i < 3; ++i) { |
| 241 |
rgba[i] = fSrcToDstMatrix[ i] * temp[0] + |
| 242 |
fSrcToDstMatrix[3 + i] * temp[1] + |
| 243 |
fSrcToDstMatrix[6 + i] * temp[2]; |
| 244 |
} |
| 245 |
} |
| 246 |
if (this->fFlags.dst_ootf) { |
| 247 |
const float Y = fDstOotf[0] * rgba[0] + |
| 248 |
fDstOotf[1] * rgba[1] + |
| 249 |
fDstOotf[2] * rgba[2]; |
| 250 |
const float Y_to_gamma_minus_1 = std::pow(Y, fDstOotf[3]); |
| 251 |
rgba[0] *= Y_to_gamma_minus_1; |
| 252 |
rgba[1] *= Y_to_gamma_minus_1; |
| 253 |
rgba[2] *= Y_to_gamma_minus_1; |
| 254 |
} |
| 255 |
if (this->fFlags.encode) { |
| 256 |
rgba[0] = skcms_TransferFunction_eval(&fDstTFInv, rgba[0]); |
| 257 |
rgba[1] = skcms_TransferFunction_eval(&fDstTFInv, rgba[1]); |
| 258 |
rgba[2] = skcms_TransferFunction_eval(&fDstTFInv, rgba[2]); |
| 259 |
} |
| 260 |
if (this->fFlags.premul) { |
| 261 |
rgba[0] *= rgba[3]; |
| 262 |
rgba[1] *= rgba[3]; |
| 263 |
rgba[2] *= rgba[3]; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
void SkColorSpaceXformSteps::apply(SkRasterPipeline* p) const { |
| 268 |
if (this->fFlags.unpremul) { p->append(SkRasterPipelineOp::unpremul); } |
| 269 |
if (this->fFlags.linearize) { p->appendTransferFunction(fSrcTF); } |
| 270 |
if (this->fFlags.src_ootf) { p->append(SkRasterPipelineOp::ootf, fSrcOotf); } |
| 271 |
if (this->fFlags.gamut_transform) { p->append(SkRasterPipelineOp::matrix_3x3, &fSrcToDstMatrix); } |
| 272 |
if (this->fFlags.dst_ootf) { p->append(SkRasterPipelineOp::ootf, fDstOotf); } |
| 273 |
if (this->fFlags.encode) { p->appendTransferFunction(fDstTFInv); } |
| 274 |
if (this->fFlags.premul) { p->append(SkRasterPipelineOp::premul); } |
| 275 |
} |