| 1 |
/* |
| 2 |
* Copyright 2012 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/SkMaskGamma.h" |
| 9 |
|
| 10 |
#include "include/core/SkTypes.h" |
| 11 |
#include "include/private/base/SkDebug.h" |
| 12 |
#include "include/private/base/SkFloatingPoint.h" |
| 13 |
#include "include/private/base/SkTo.h" |
| 14 |
|
| 15 |
#include <cmath> |
| 16 |
|
| 17 |
class SkLinearColorSpaceLuminance : public SkColorSpaceLuminance { |
| 18 |
SkScalar toLuma(SkScalar SkDEBUGCODE(gamma), SkScalar luminance) const override { |
| 19 |
SkASSERT(SK_Scalar1 == gamma); |
| 20 |
return luminance; |
| 21 |
} |
| 22 |
SkScalar fromLuma(SkScalar SkDEBUGCODE(gamma), SkScalar luma) const override { |
| 23 |
SkASSERT(SK_Scalar1 == gamma); |
| 24 |
return luma; |
| 25 |
} |
| 26 |
}; |
| 27 |
|
| 28 |
class SkGammaColorSpaceLuminance : public SkColorSpaceLuminance { |
| 29 |
SkScalar toLuma(SkScalar gamma, SkScalar luminance) const override { |
| 30 |
return SkScalarPow(luminance, gamma); |
| 31 |
} |
| 32 |
SkScalar fromLuma(SkScalar gamma, SkScalar luma) const override { |
| 33 |
return SkScalarPow(luma, SkScalarInvert(gamma)); |
| 34 |
} |
| 35 |
}; |
| 36 |
|
| 37 |
class SkSRGBColorSpaceLuminance : public SkColorSpaceLuminance { |
| 38 |
SkScalar toLuma(SkScalar SkDEBUGCODE(gamma), SkScalar luminance) const override { |
| 39 |
SkASSERT(0 == gamma); |
| 40 |
//The magic numbers are derived from the sRGB specification. |
| 41 |
//See http://www.color.org/chardata/rgb/srgb.xalter . |
| 42 |
if (luminance <= 0.04045f) { |
| 43 |
return luminance / 12.92f; |
| 44 |
} |
| 45 |
return SkScalarPow((luminance + 0.055f) / 1.055f, |
| 46 |
2.4f); |
| 47 |
} |
| 48 |
SkScalar fromLuma(SkScalar SkDEBUGCODE(gamma), SkScalar luma) const override { |
| 49 |
SkASSERT(0 == gamma); |
| 50 |
//The magic numbers are derived from the sRGB specification. |
| 51 |
//See http://www.color.org/chardata/rgb/srgb.xalter . |
| 52 |
if (luma <= 0.0031308f) { |
| 53 |
return luma * 12.92f; |
| 54 |
} |
| 55 |
return 1.055f * SkScalarPow(luma, SkScalarInvert(2.4f)) |
| 56 |
- 0.055f; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
/*static*/ const SkColorSpaceLuminance& SkColorSpaceLuminance::Fetch(SkScalar gamma) { |
| 61 |
static SkLinearColorSpaceLuminance gSkLinearColorSpaceLuminance; |
| 62 |
static SkGammaColorSpaceLuminance gSkGammaColorSpaceLuminance; |
| 63 |
static SkSRGBColorSpaceLuminance gSkSRGBColorSpaceLuminance; |
| 64 |
|
| 65 |
if (0 == gamma) { |
| 66 |
return gSkSRGBColorSpaceLuminance; |
| 67 |
} else if (SK_Scalar1 == gamma) { |
| 68 |
return gSkLinearColorSpaceLuminance; |
| 69 |
} else { |
| 70 |
return gSkGammaColorSpaceLuminance; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static float apply_contrast(float srca, float contrast) { |
| 75 |
return srca + ((1.0f - srca) * contrast * srca); |
| 76 |
} |
| 77 |
|
| 78 |
void SkTMaskGamma_build_correcting_lut(uint8_t* table, U8CPU srcI, SkScalar contrast, |
| 79 |
const SkColorSpaceLuminance& dstConvert, SkScalar dstGamma) { |
| 80 |
const SkColorSpaceLuminance& srcConvert = dstConvert; |
| 81 |
const SkScalar srcGamma = dstGamma; |
| 82 |
const float src = (float)srcI / 255.0f; |
| 83 |
const float linSrc = srcConvert.toLuma(srcGamma, src); |
| 84 |
//Guess at the dst. The perceptual inverse provides smaller visual |
| 85 |
//discontinuities when slight changes to desaturated colors cause a channel |
| 86 |
//to map to a different correcting lut with neighboring srcI. |
| 87 |
//See https://code.google.com/p/chromium/issues/detail?id=141425#c59 . |
| 88 |
const float dst = 1.0f - src; |
| 89 |
const float linDst = dstConvert.toLuma(dstGamma, dst); |
| 90 |
|
| 91 |
//Contrast value tapers off to 0 as the src luminance becomes white |
| 92 |
const float adjustedContrast = contrast * linDst; |
| 93 |
|
| 94 |
//Remove discontinuity and instability when src is close to dst. |
| 95 |
//The value 1/256 is arbitrary and appears to contain the instability. |
| 96 |
if (fabs(src - dst) < (1.0f / 256.0f)) { |
| 97 |
float ii = 0.0f; |
| 98 |
for (int i = 0; i < 256; ++i, ii += 1.0f) { |
| 99 |
float rawSrca = ii / 255.0f; |
| 100 |
float srca = apply_contrast(rawSrca, adjustedContrast); |
| 101 |
table[i] = SkToU8(sk_float_round2int(255.0f * srca)); |
| 102 |
} |
| 103 |
} else { |
| 104 |
// Avoid slow int to float conversion. |
| 105 |
float ii = 0.0f; |
| 106 |
for (int i = 0; i < 256; ++i, ii += 1.0f) { |
| 107 |
// 'rawSrca += 1.0f / 255.0f' and even |
| 108 |
// 'rawSrca = i * (1.0f / 255.0f)' can add up to more than 1.0f. |
| 109 |
// When this happens the table[255] == 0x0 instead of 0xff. |
| 110 |
// See http://code.google.com/p/chromium/issues/detail?id=146466 |
| 111 |
float rawSrca = ii / 255.0f; |
| 112 |
float srca = apply_contrast(rawSrca, adjustedContrast); |
| 113 |
SkASSERT(srca <= 1.0f); |
| 114 |
float dsta = 1.0f - srca; |
| 115 |
|
| 116 |
//Calculate the output we want. |
| 117 |
float linOut = (linSrc * srca + dsta * linDst); |
| 118 |
SkASSERT(linOut <= 1.0f); |
| 119 |
float out = dstConvert.fromLuma(dstGamma, linOut); |
| 120 |
|
| 121 |
//Undo what the blit blend will do. |
| 122 |
float result = (out - dst) / (src - dst); |
| 123 |
SkASSERT(sk_float_round2int(255.0f * result) <= 255); |
| 124 |
|
| 125 |
table[i] = SkToU8(sk_float_round2int(255.0f * result)); |
| 126 |
} |
| 127 |
} |
| 128 |
} |