aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkMaskGamma.h
blob: 7d8b9e310b51ac01dbd1c32c455f3ee8a68d2df3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkMaskGamma_DEFINED
#define SkMaskGamma_DEFINED

#include "SkTypes.h"
#include "SkColor.h"
#include "SkColorPriv.h"
#include "SkRefCnt.h"

/**
 * SkColorSpaceLuminance is used to convert luminances to and from linear and
 * perceptual color spaces.
 *
 * Luma is used to specify a linear luminance value [0.0, 1.0].
 * Luminance is used to specify a luminance value in an arbitrary color space [0.0, 1.0].
 */
class SkColorSpaceLuminance : SkNoncopyable {
public:
    virtual ~SkColorSpaceLuminance() {};

    /** Converts a color component luminance in the color space to a linear luma. */
    virtual SkScalar toLuma(SkScalar luminance) const = 0;
    /** Converts a linear luma to a color component luminance in the color space. */
    virtual SkScalar fromLuma(SkScalar luma) const = 0;

    /** Converts a color to a luminance value. */
    U8CPU computeLuminance(SkColor c) const {
        SkScalar r = toLuma(SkIntToScalar(SkColorGetR(c)) / 255);
        SkScalar g = toLuma(SkIntToScalar(SkColorGetG(c)) / 255);
        SkScalar b = toLuma(SkIntToScalar(SkColorGetB(c)) / 255);
        SkScalar luma = r * SkFloatToScalar(SK_LUM_COEFF_R) +
                        g * SkFloatToScalar(SK_LUM_COEFF_G) +
                        b * SkFloatToScalar(SK_LUM_COEFF_B);
        SkASSERT(luma <= SK_Scalar1);
        return SkScalarRoundToInt(fromLuma(luma) * 255);
    }
};

class SkSRGBLuminance : public SkColorSpaceLuminance {
public:
    SkScalar toLuma(SkScalar luminance) const SK_OVERRIDE;
    SkScalar fromLuma(SkScalar luma) const SK_OVERRIDE;
};

class SkGammaLuminance : public SkColorSpaceLuminance {
public:
    SkGammaLuminance(SkScalar gamma);
    SkScalar toLuma(SkScalar luminance) const SK_OVERRIDE;
    SkScalar fromLuma(SkScalar luma) const SK_OVERRIDE;
private:
    SkScalar fGamma;
    SkScalar fGammaInverse;
};

///@{
/**
 * Scales base <= 2^N-1 to 2^8-1
 * @param N [1, 8] the number of bits used by base.
 * @param base the number to be scaled to [0, 255].
 */
template<U8CPU N> static inline U8CPU sk_t_scale255(U8CPU base) {
    base <<= (8 - N);
    U8CPU lum = base;
    for (unsigned int i = N; i < 8; i += N) {
        lum |= base >> i;
    }
    return lum;
}
template<> /*static*/ inline U8CPU sk_t_scale255<1>(U8CPU base) {
    return base * 0xFF;
}
template<> /*static*/ inline U8CPU sk_t_scale255<2>(U8CPU base) {
    return base * 0x55;
}
template<> /*static*/ inline U8CPU sk_t_scale255<4>(U8CPU base) {
    return base * 0x11;
}
template<> /*static*/ inline U8CPU sk_t_scale255<8>(U8CPU base) {
    return base;
}
///@}

template <int R_LUM_BITS, int G_LUM_BITS, int B_LUM_BITS> class SkTMaskPreBlend;

void SkTMaskGamma_build_correcting_lut(uint8_t table[256], U8CPU srcI, SkScalar contrast,
                                       const SkColorSpaceLuminance& srcConvert,
                                       const SkColorSpaceLuminance& dstConvert);

/**
 * A regular mask contains linear alpha values. A gamma correcting mask
 * contains non-linear alpha values in an attempt to create gamma correct blits
 * in the presence of a gamma incorrect (linear) blend in the blitter.
 *
 * SkMaskGamma creates and maintains tables which convert linear alpha values
 * to gamma correcting alpha values.
 * @param R The number of luminance bits to use [1, 8] from the red channel.
 * @param G The number of luminance bits to use [1, 8] from the green channel.
 * @param B The number of luminance bits to use [1, 8] from the blue channel.
 */
template <int R_LUM_BITS, int G_LUM_BITS, int B_LUM_BITS> class SkTMaskGamma : public SkRefCnt {
public:
    SK_DECLARE_INST_COUNT_TEMPLATE(SkTMaskGamma)

    /**
     * Creates tables to convert linear alpha values to gamma correcting alpha
     * values.
     *
     * @param contrast A value in the range [0.0, 1.0] which indicates the
     *                 amount of artificial contrast to add.
     * @param paint The color space in which the paint color was chosen.
     * @param device The color space of the target device.
     */
    SkTMaskGamma(SkScalar contrast,
                 const SkColorSpaceLuminance& paint,
                 const SkColorSpaceLuminance& device) {
        for (U8CPU i = 0; i < (1 << kLuminanceBits_Max); ++i) {
            U8CPU lum = sk_t_scale255<kLuminanceBits_Max>(i);
            SkTMaskGamma_build_correcting_lut(fGammaTables[i], lum, contrast, paint, device);
        }
    }

    /** Given a color, returns the closest cannonical color. */
    SkColor cannonicalColor(SkColor color) {
        return SkColorSetRGB(
                   sk_t_scale255<kLuminanceBits_R>(SkColorGetR(color) >> (8 - kLuminanceBits_R)),
                   sk_t_scale255<kLuminanceBits_G>(SkColorGetG(color) >> (8 - kLuminanceBits_G)),
                   sk_t_scale255<kLuminanceBits_B>(SkColorGetB(color) >> (8 - kLuminanceBits_B)));
    }

    /** The type of the mask pre-blend which will be returned from preBlend(SkColor). */
    typedef SkTMaskPreBlend<R_LUM_BITS, G_LUM_BITS, B_LUM_BITS> PreBlend;

    /**
     * Provides access to the tables appropriate for converting linear alpha
     * values into gamma correcting alpha values when drawing the given color
     * through the mask. The destination color will be approximated.
     */
    PreBlend preBlend(SkColor color);

private:
    enum LuminanceBits {
        kLuminanceBits_R = R_LUM_BITS,
        kLuminanceBits_G = G_LUM_BITS,
        kLuminanceBits_B = B_LUM_BITS,
        kLuminanceBits_Max = B_LUM_BITS > (R_LUM_BITS > G_LUM_BITS ? R_LUM_BITS : G_LUM_BITS)
                           ? B_LUM_BITS
                           : (R_LUM_BITS > G_LUM_BITS ? R_LUM_BITS : G_LUM_BITS)
    };
    uint8_t fGammaTables[1 << kLuminanceBits_Max][256];

    typedef SkRefCnt INHERITED;
};


#define MacroComma ,
SK_DEFINE_INST_COUNT_TEMPLATE(
    template <int R_LUM_BITS MacroComma int G_LUM_BITS MacroComma int B_LUM_BITS>, 
    SkTMaskGamma<R_LUM_BITS MacroComma G_LUM_BITS MacroComma B_LUM_BITS>);

/**
 * SkTMaskPreBlend is a tear-off of SkTMaskGamma. It provides the tables to
 * convert a linear alpha value for a given channel to a gamma correcting alpha
 * value for that channel. This class is immutable.
 */
template <int R_LUM_BITS, int G_LUM_BITS, int B_LUM_BITS> class SkTMaskPreBlend {
private:
    SkTMaskPreBlend(SkTMaskGamma<R_LUM_BITS, G_LUM_BITS, B_LUM_BITS>* parent,
                    const uint8_t* r,
                    const uint8_t* g,
                    const uint8_t* b)
    : fParent(parent), fR(r), fG(g), fB(b) {
        parent->ref();
    }
    SkAutoTUnref<SkTMaskGamma<R_LUM_BITS, G_LUM_BITS, B_LUM_BITS> > fParent;
    friend class SkTMaskGamma<R_LUM_BITS, G_LUM_BITS, B_LUM_BITS>;
public:
    /**
     * This copy contructor exists for correctness, but should never be called
     * when return value optimization is enabled.
     */
    SkTMaskPreBlend(const SkTMaskPreBlend<R_LUM_BITS, G_LUM_BITS, B_LUM_BITS>& that)
    : fParent(that.fParent.get()), fR(that.fR), fG(that.fG), fB(that.fB) {
        fParent.get()->ref();
    }
    ~SkTMaskPreBlend() { }
    const uint8_t* fR;
    const uint8_t* fG;
    const uint8_t* fB;
};

template <int R_LUM_BITS, int G_LUM_BITS, int B_LUM_BITS>
SkTMaskPreBlend<R_LUM_BITS, G_LUM_BITS, B_LUM_BITS>
SkTMaskGamma<R_LUM_BITS, G_LUM_BITS, B_LUM_BITS>::preBlend(SkColor color) {
    return SkTMaskPreBlend<R_LUM_BITS, G_LUM_BITS, B_LUM_BITS>(
                          this,
                          fGammaTables[SkColorGetR(color) >> (8 - kLuminanceBits_Max)],
                          fGammaTables[SkColorGetG(color) >> (8 - kLuminanceBits_Max)],
                          fGammaTables[SkColorGetB(color) >> (8 - kLuminanceBits_Max)]);
}

///@{
/**
 *  If APPLY_LUT is false, returns component unchanged.
 *  If APPLY_LUT is true, returns lut[component].
 *  @param APPLY_LUT whether or not the look-up table should be applied to component.
 *  @component the initial component.
 *  @lut a look-up table which transforms the component.
 */
template<bool APPLY_LUT> static inline U8CPU sk_apply_lut_if(U8CPU component, const uint8_t*) {
    return component;
}
template<> /*static*/ inline U8CPU sk_apply_lut_if<true>(U8CPU component, const uint8_t* lut) {
    return lut[component];
}
///@}

#endif