Flutter Engine
The Flutter Engine
SkMaskGamma.h
Go to the documentation of this file.
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#ifndef SkMaskGamma_DEFINED
9#define SkMaskGamma_DEFINED
10
19
20#include <cstdint>
21
22/**
23 * SkColorSpaceLuminance is used to convert luminances to and from linear and
24 * perceptual color spaces.
25 *
26 * Luma is used to specify a linear luminance value [0.0, 1.0].
27 * Luminance is used to specify a luminance value in an arbitrary color space [0.0, 1.0].
28 */
30public:
32
33 /** Converts a color component luminance in the color space to a linear luma. */
34 virtual SkScalar toLuma(SkScalar gamma, SkScalar luminance) const = 0;
35 /** Converts a linear luma to a color component luminance in the color space. */
36 virtual SkScalar fromLuma(SkScalar gamma, SkScalar luma) const = 0;
37
38 /** Converts a color to a luminance value. */
40 const SkColorSpaceLuminance& luminance = Fetch(gamma);
41 SkScalar r = luminance.toLuma(gamma, SkIntToScalar(SkColorGetR(c)) / 255);
42 SkScalar g = luminance.toLuma(gamma, SkIntToScalar(SkColorGetG(c)) / 255);
43 SkScalar b = luminance.toLuma(gamma, SkIntToScalar(SkColorGetB(c)) / 255);
44 SkScalar luma = r * SK_LUM_COEFF_R +
45 g * SK_LUM_COEFF_G +
47 SkASSERT(luma <= SK_Scalar1);
48 return SkScalarRoundToInt(luminance.fromLuma(gamma, luma) * 255);
49 }
50
51 /** Retrieves the SkColorSpaceLuminance for the given gamma. */
52 static const SkColorSpaceLuminance& Fetch(SkScalar gamma);
53};
54
55///@{
56/**
57 * Scales base <= 2^N-1 to 2^8-1
58 * @param N [1, 8] the number of bits used by base.
59 * @param base the number to be scaled to [0, 255].
60 */
61template<U8CPU N> static inline U8CPU sk_t_scale255(U8CPU base) {
62 base <<= (8 - N);
63 U8CPU lum = base;
64 for (unsigned int i = N; i < 8; i += N) {
65 lum |= base >> i;
66 }
67 return lum;
68}
69template<> /*static*/ inline U8CPU sk_t_scale255<1>(U8CPU base) {
70 return base * 0xFF;
71}
72template<> /*static*/ inline U8CPU sk_t_scale255<2>(U8CPU base) {
73 return base * 0x55;
74}
75template<> /*static*/ inline U8CPU sk_t_scale255<4>(U8CPU base) {
76 return base * 0x11;
77}
78template<> /*static*/ inline U8CPU sk_t_scale255<8>(U8CPU base) {
79 return base;
80}
81///@}
82
83template <int R_LUM_BITS, int G_LUM_BITS, int B_LUM_BITS> class SkTMaskPreBlend;
84
85void SkTMaskGamma_build_correcting_lut(uint8_t table[256], U8CPU srcI, SkScalar contrast,
86 const SkColorSpaceLuminance& dstConvert, SkScalar dstGamma);
87
88/**
89 * A regular mask contains linear alpha values. A gamma correcting mask
90 * contains non-linear alpha values in an attempt to create gamma correct blits
91 * in the presence of a gamma incorrect (linear) blend in the blitter.
92 *
93 * SkMaskGamma creates and maintains tables which convert linear alpha values
94 * to gamma correcting alpha values.
95 * @param R The number of luminance bits to use [1, 8] from the red channel.
96 * @param G The number of luminance bits to use [1, 8] from the green channel.
97 * @param B The number of luminance bits to use [1, 8] from the blue channel.
98 */
99template <int R_LUM_BITS, int G_LUM_BITS, int B_LUM_BITS> class SkTMaskGamma : public SkRefCnt {
100
101public:
102
103 /** Creates a linear SkTMaskGamma. */
104 SkTMaskGamma() : fIsLinear(true) { }
105
106 /**
107 * Creates tables to convert linear alpha values to gamma correcting alpha
108 * values.
109 *
110 * @param contrast A value in the range [0.0, 1.0] which indicates the
111 * amount of artificial contrast to add.
112 * @param device The color space of the target device.
113 */
114 SkTMaskGamma(SkScalar contrast, SkScalar deviceGamma) : fIsLinear(false) {
115 const SkColorSpaceLuminance& deviceConvert = SkColorSpaceLuminance::Fetch(deviceGamma);
116 for (U8CPU i = 0; i < (1 << MAX_LUM_BITS); ++i) {
117 U8CPU lum = sk_t_scale255<MAX_LUM_BITS>(i);
118 SkTMaskGamma_build_correcting_lut(fGammaTables[i], lum, contrast,
119 deviceConvert, deviceGamma);
120 }
121 }
122
123 /** Given a color, returns the closest canonical color. */
125 return SkColorSetRGB(
126 sk_t_scale255<R_LUM_BITS>(SkColorGetR(color) >> (8 - R_LUM_BITS)),
127 sk_t_scale255<G_LUM_BITS>(SkColorGetG(color) >> (8 - G_LUM_BITS)),
128 sk_t_scale255<B_LUM_BITS>(SkColorGetB(color) >> (8 - B_LUM_BITS)));
129 }
130
131 /** The type of the mask pre-blend which will be returned from preBlend(SkColor). */
133
134 /**
135 * Provides access to the tables appropriate for converting linear alpha
136 * values into gamma correcting alpha values when drawing the given color
137 * through the mask. The destination color will be approximated.
138 */
140
141 /**
142 * Get dimensions for the full table set, so it can be allocated as a block.
143 */
144 void getGammaTableDimensions(int* tableWidth, int* numTables) const {
145 *tableWidth = 256;
146 *numTables = (1 << MAX_LUM_BITS);
147 }
148
149 /**
150 * Provides direct access to the full table set, so it can be uploaded
151 * into a texture or analyzed in other ways.
152 * Returns nullptr if fGammaTables hasn't been initialized.
153 */
154 const uint8_t* getGammaTables() const {
155 return fIsLinear ? nullptr : (const uint8_t*) fGammaTables;
156 }
157
158private:
159 static const int MAX_LUM_BITS =
160 B_LUM_BITS > (R_LUM_BITS > G_LUM_BITS ? R_LUM_BITS : G_LUM_BITS)
161 ? B_LUM_BITS : (R_LUM_BITS > G_LUM_BITS ? R_LUM_BITS : G_LUM_BITS);
162 uint8_t fGammaTables[1 << MAX_LUM_BITS][256];
163 bool fIsLinear;
164
165 using INHERITED = SkRefCnt;
166};
167
168
169/**
170 * SkTMaskPreBlend is a tear-off of SkTMaskGamma. It provides the tables to
171 * convert a linear alpha value for a given channel to a gamma correcting alpha
172 * value for that channel. This class is immutable.
173 *
174 * If fR, fG, or fB is nullptr, all of them will be. This indicates that no mask
175 * pre blend should be applied. SkTMaskPreBlend::isApplicable() is provided as
176 * a convenience function to test for the absence of this case.
177 */
178template <int R_LUM_BITS, int G_LUM_BITS, int B_LUM_BITS> class SkTMaskPreBlend {
179private:
181 const uint8_t* r, const uint8_t* g, const uint8_t* b)
182 : fParent(std::move(parent)), fR(r), fG(g), fB(b) { }
183
185 friend class SkTMaskGamma<R_LUM_BITS, G_LUM_BITS, B_LUM_BITS>;
186public:
187 /** Creates a non applicable SkTMaskPreBlend. */
188 SkTMaskPreBlend() : fParent(), fR(nullptr), fG(nullptr), fB(nullptr) { }
189
190 /**
191 * This copy contructor exists for correctness, but should never be called
192 * when return value optimization is enabled.
193 */
195 : fParent(that.fParent), fR(that.fR), fG(that.fG), fB(that.fB) { }
196
198
199 /** True if this PreBlend should be applied. When false, fR, fG, and fB are nullptr. */
200 bool isApplicable() const { return SkToBool(this->fG); }
201
202 const uint8_t* fR;
203 const uint8_t* fG;
204 const uint8_t* fB;
205};
206
207template <int R_LUM_BITS, int G_LUM_BITS, int B_LUM_BITS>
212 fGammaTables[SkColorGetR(color) >> (8 - MAX_LUM_BITS)],
213 fGammaTables[SkColorGetG(color) >> (8 - MAX_LUM_BITS)],
214 fGammaTables[SkColorGetB(color) >> (8 - MAX_LUM_BITS)]);
215}
216
217///@{
218/**
219 * If APPLY_LUT is false, returns component unchanged.
220 * If APPLY_LUT is true, returns lut[component].
221 * @param APPLY_LUT whether or not the look-up table should be applied to component.
222 * @component the initial component.
223 * @lut a look-up table which transforms the component.
224 */
225template<bool APPLY_LUT> static inline U8CPU sk_apply_lut_if(U8CPU component, const uint8_t*) {
226 return component;
227}
228template<> /*static*/ inline U8CPU sk_apply_lut_if<true>(U8CPU component, const uint8_t* lut) {
229 return lut[component];
230}
231///@}
232
233#endif
#define SkASSERT(cond)
Definition: SkAssert.h:116
unsigned U8CPU
Definition: SkCPUTypes.h:18
#define SK_LUM_COEFF_B
Definition: SkColorData.h:112
#define SK_LUM_COEFF_R
Definition: SkColorData.h:110
#define SK_LUM_COEFF_G
Definition: SkColorData.h:111
#define SkColorGetR(color)
Definition: SkColor.h:65
#define SkColorGetG(color)
Definition: SkColor.h:69
uint32_t SkColor
Definition: SkColor.h:37
#define SkColorSetRGB(r, g, b)
Definition: SkColor.h:57
#define SkColorGetB(color)
Definition: SkColor.h:73
U8CPU sk_t_scale255< 4 >(U8CPU base)
Definition: SkMaskGamma.h:75
static U8CPU sk_t_scale255(U8CPU base)
Definition: SkMaskGamma.h:61
U8CPU sk_apply_lut_if< true >(U8CPU component, const uint8_t *lut)
Definition: SkMaskGamma.h:228
U8CPU sk_t_scale255< 2 >(U8CPU base)
Definition: SkMaskGamma.h:72
static U8CPU sk_apply_lut_if(U8CPU component, const uint8_t *)
Definition: SkMaskGamma.h:225
U8CPU sk_t_scale255< 1 >(U8CPU base)
Definition: SkMaskGamma.h:69
U8CPU sk_t_scale255< 8 >(U8CPU base)
Definition: SkMaskGamma.h:78
void SkTMaskGamma_build_correcting_lut(uint8_t table[256], U8CPU srcI, SkScalar contrast, const SkColorSpaceLuminance &dstConvert, SkScalar dstGamma)
Definition: SkMaskGamma.cpp:78
sk_sp< T > sk_ref_sp(T *obj)
Definition: SkRefCnt.h:381
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkScalarRoundToInt(x)
Definition: SkScalar.h:37
#define SkIntToScalar(x)
Definition: SkScalar.h:57
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
SI F table(const skcms_Curve *curve, F v)
#define N
Definition: beziers.cpp:19
static U8CPU computeLuminance(SkScalar gamma, SkColor c)
Definition: SkMaskGamma.h:39
virtual SkScalar fromLuma(SkScalar gamma, SkScalar luma) const =0
virtual SkScalar toLuma(SkScalar gamma, SkScalar luminance) const =0
static const SkColorSpaceLuminance & Fetch(SkScalar gamma)
Definition: SkMaskGamma.cpp:60
virtual ~SkColorSpaceLuminance()
Definition: SkMaskGamma.h:31
SkTMaskGamma(SkScalar contrast, SkScalar deviceGamma)
Definition: SkMaskGamma.h:114
void getGammaTableDimensions(int *tableWidth, int *numTables) const
Definition: SkMaskGamma.h:144
PreBlend preBlend(SkColor color) const
Definition: SkMaskGamma.h:209
static SkColor CanonicalColor(SkColor color)
Definition: SkMaskGamma.h:124
const uint8_t * getGammaTables() const
Definition: SkMaskGamma.h:154
SkTMaskPreBlend< R_LUM_BITS, G_LUM_BITS, B_LUM_BITS > PreBlend
Definition: SkMaskGamma.h:132
const uint8_t * fG
Definition: SkMaskGamma.h:203
SkTMaskPreBlend(const SkTMaskPreBlend< R_LUM_BITS, G_LUM_BITS, B_LUM_BITS > &that)
Definition: SkMaskGamma.h:194
const uint8_t * fB
Definition: SkMaskGamma.h:204
bool isApplicable() const
Definition: SkMaskGamma.h:200
const uint8_t * fR
Definition: SkMaskGamma.h:202
DlColor color
float SkScalar
Definition: extension.cpp:12
static bool b
static float lum(float r, float g, float b)
Definition: hsl.cpp:52