• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
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 SkColorData_DEFINED
9 #define SkColorData_DEFINED
10 
11 #include "include/core/SkAlphaType.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkTypes.h"
14 #include "include/private/base/SkCPUTypes.h"
15 #include "include/private/base/SkFloatingPoint.h"
16 #include "include/private/base/SkTo.h"
17 #include "src/core/SkColorPriv.h"
18 
19 #include <cstdint>
20 
21 ////////////////////////////////////////////////////////////////////////////////////////////
22 // Convert a 16bit pixel to a 32bit pixel
23 
24 #define SK_R16_BITS     5
25 #define SK_G16_BITS     6
26 #define SK_B16_BITS     5
27 
28 #define SK_R16_SHIFT    (SK_B16_BITS + SK_G16_BITS)
29 #define SK_G16_SHIFT    (SK_B16_BITS)
30 #define SK_B16_SHIFT    0
31 
32 #define SK_R16_MASK     ((1 << SK_R16_BITS) - 1)
33 #define SK_G16_MASK     ((1 << SK_G16_BITS) - 1)
34 #define SK_B16_MASK     ((1 << SK_B16_BITS) - 1)
35 
36 #define SkGetPackedR16(color)   (((unsigned)(color) >> SK_R16_SHIFT) & SK_R16_MASK)
37 #define SkGetPackedG16(color)   (((unsigned)(color) >> SK_G16_SHIFT) & SK_G16_MASK)
38 #define SkGetPackedB16(color)   (((unsigned)(color) >> SK_B16_SHIFT) & SK_B16_MASK)
39 
SkR16ToR32(unsigned r)40 static inline unsigned SkR16ToR32(unsigned r) {
41     return (r << (8 - SK_R16_BITS)) | (r >> (2 * SK_R16_BITS - 8));
42 }
43 
SkG16ToG32(unsigned g)44 static inline unsigned SkG16ToG32(unsigned g) {
45     return (g << (8 - SK_G16_BITS)) | (g >> (2 * SK_G16_BITS - 8));
46 }
47 
SkB16ToB32(unsigned b)48 static inline unsigned SkB16ToB32(unsigned b) {
49     return (b << (8 - SK_B16_BITS)) | (b >> (2 * SK_B16_BITS - 8));
50 }
51 
52 #define SkPacked16ToR32(c)      SkR16ToR32(SkGetPackedR16(c))
53 #define SkPacked16ToG32(c)      SkG16ToG32(SkGetPackedG16(c))
54 #define SkPacked16ToB32(c)      SkB16ToB32(SkGetPackedB16(c))
55 
56 //////////////////////////////////////////////////////////////////////////////
57 
58 #define SkASSERT_IS_BYTE(x)     SkASSERT(0 == ((x) & ~0xFFu))
59 
60 // Reverse the bytes coorsponding to RED and BLUE in a packed pixels. Note the
61 // pair of them are in the same 2 slots in both RGBA and BGRA, thus there is
62 // no need to pass in the colortype to this function.
SkSwizzle_RB(uint32_t c)63 static inline uint32_t SkSwizzle_RB(uint32_t c) {
64     static const uint32_t kRBMask = (0xFF << SK_R32_SHIFT) | (0xFF << SK_B32_SHIFT);
65 
66     unsigned c0 = (c >> SK_R32_SHIFT) & 0xFF;
67     unsigned c1 = (c >> SK_B32_SHIFT) & 0xFF;
68     return (c & ~kRBMask) | (c0 << SK_B32_SHIFT) | (c1 << SK_R32_SHIFT);
69 }
70 
SkPackARGB_as_RGBA(U8CPU a,U8CPU r,U8CPU g,U8CPU b)71 static inline uint32_t SkPackARGB_as_RGBA(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
72     SkASSERT_IS_BYTE(a);
73     SkASSERT_IS_BYTE(r);
74     SkASSERT_IS_BYTE(g);
75     SkASSERT_IS_BYTE(b);
76     return (a << SK_RGBA_A32_SHIFT) | (r << SK_RGBA_R32_SHIFT) |
77            (g << SK_RGBA_G32_SHIFT) | (b << SK_RGBA_B32_SHIFT);
78 }
79 
SkPackARGB_as_BGRA(U8CPU a,U8CPU r,U8CPU g,U8CPU b)80 static inline uint32_t SkPackARGB_as_BGRA(U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
81     SkASSERT_IS_BYTE(a);
82     SkASSERT_IS_BYTE(r);
83     SkASSERT_IS_BYTE(g);
84     SkASSERT_IS_BYTE(b);
85     return (a << SK_BGRA_A32_SHIFT) | (r << SK_BGRA_R32_SHIFT) |
86            (g << SK_BGRA_G32_SHIFT) | (b << SK_BGRA_B32_SHIFT);
87 }
88 
SkSwizzle_RGBA_to_PMColor(uint32_t c)89 static inline SkPMColor SkSwizzle_RGBA_to_PMColor(uint32_t c) {
90 #ifdef SK_PMCOLOR_IS_RGBA
91     return c;
92 #else
93     return SkSwizzle_RB(c);
94 #endif
95 }
96 
SkSwizzle_BGRA_to_PMColor(uint32_t c)97 static inline SkPMColor SkSwizzle_BGRA_to_PMColor(uint32_t c) {
98 #ifdef SK_PMCOLOR_IS_BGRA
99     return c;
100 #else
101     return SkSwizzle_RB(c);
102 #endif
103 }
104 
105 //////////////////////////////////////////////////////////////////////////////
106 
107 ///@{
108 /** See ITU-R Recommendation BT.709 at http://www.itu.int/rec/R-REC-BT.709/ .*/
109 #define SK_ITU_BT709_LUM_COEFF_R (0.2126f)
110 #define SK_ITU_BT709_LUM_COEFF_G (0.7152f)
111 #define SK_ITU_BT709_LUM_COEFF_B (0.0722f)
112 ///@}
113 
114 ///@{
115 /** A float value which specifies this channel's contribution to luminance. */
116 #define SK_LUM_COEFF_R SK_ITU_BT709_LUM_COEFF_R
117 #define SK_LUM_COEFF_G SK_ITU_BT709_LUM_COEFF_G
118 #define SK_LUM_COEFF_B SK_ITU_BT709_LUM_COEFF_B
119 ///@}
120 
121 /** Computes the luminance from the given r, g, and b in accordance with
122     SK_LUM_COEFF_X. For correct results, r, g, and b should be in linear space.
123 */
SkComputeLuminance(U8CPU r,U8CPU g,U8CPU b)124 static inline U8CPU SkComputeLuminance(U8CPU r, U8CPU g, U8CPU b) {
125     //The following is
126     //r * SK_LUM_COEFF_R + g * SK_LUM_COEFF_G + b * SK_LUM_COEFF_B
127     //with SK_LUM_COEFF_X in 1.8 fixed point (rounding adjusted to sum to 256).
128     return (r * 54 + g * 183 + b * 19) >> 8;
129 }
130 
131 /** Calculates 256 - (value * alpha256) / 255 in range [0,256],
132  *  for [0,255] value and [0,256] alpha256.
133  */
SkAlphaMulInv256(U16CPU value,U16CPU alpha256)134 static inline U16CPU SkAlphaMulInv256(U16CPU value, U16CPU alpha256) {
135     unsigned prod = 0xFFFF - value * alpha256;
136     return (prod + (prod >> 8)) >> 8;
137 }
138 
139 //  The caller may want negative values, so keep all params signed (int)
140 //  so we don't accidentally slip into unsigned math and lose the sign
141 //  extension when we shift (in SkAlphaMul)
SkAlphaBlend(int src,int dst,int scale256)142 static inline int SkAlphaBlend(int src, int dst, int scale256) {
143     SkASSERT((unsigned)scale256 <= 256);
144     return dst + SkAlphaMul(src - dst, scale256);
145 }
146 
SkPackRGB16(unsigned r,unsigned g,unsigned b)147 static inline uint16_t SkPackRGB16(unsigned r, unsigned g, unsigned b) {
148     SkASSERT(r <= SK_R16_MASK);
149     SkASSERT(g <= SK_G16_MASK);
150     SkASSERT(b <= SK_B16_MASK);
151 
152     return SkToU16((r << SK_R16_SHIFT) | (g << SK_G16_SHIFT) | (b << SK_B16_SHIFT));
153 }
154 
155 #define SK_R16_MASK_IN_PLACE        (SK_R16_MASK << SK_R16_SHIFT)
156 #define SK_G16_MASK_IN_PLACE        (SK_G16_MASK << SK_G16_SHIFT)
157 #define SK_B16_MASK_IN_PLACE        (SK_B16_MASK << SK_B16_SHIFT)
158 
159 ///////////////////////////////////////////////////////////////////////////////
160 
161 /**
162  * Abstract 4-byte interpolation, implemented on top of SkPMColor
163  * utility functions. Third parameter controls blending of the first two:
164  *   (src, dst, 0) returns dst
165  *   (src, dst, 0xFF) returns src
166  *   scale is [0..256], unlike SkFourByteInterp which takes [0..255]
167  */
SkFourByteInterp256(SkPMColor src,SkPMColor dst,int scale)168 static inline SkPMColor SkFourByteInterp256(SkPMColor src, SkPMColor dst, int scale) {
169     unsigned a = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedA32(src), SkGetPackedA32(dst), scale));
170     unsigned r = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedR32(src), SkGetPackedR32(dst), scale));
171     unsigned g = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedG32(src), SkGetPackedG32(dst), scale));
172     unsigned b = SkTo<uint8_t>(SkAlphaBlend(SkGetPackedB32(src), SkGetPackedB32(dst), scale));
173 
174     return SkPackARGB32(a, r, g, b);
175 }
176 
177 /**
178  * Abstract 4-byte interpolation, implemented on top of SkPMColor
179  * utility functions. Third parameter controls blending of the first two:
180  *   (src, dst, 0) returns dst
181  *   (src, dst, 0xFF) returns src
182  */
SkFourByteInterp(SkPMColor src,SkPMColor dst,U8CPU srcWeight)183 static inline SkPMColor SkFourByteInterp(SkPMColor src, SkPMColor dst, U8CPU srcWeight) {
184     int scale = (int)SkAlpha255To256(srcWeight);
185     return SkFourByteInterp256(src, dst, scale);
186 }
187 
188 /**
189  * 0xAARRGGBB -> 0x00AA00GG, 0x00RR00BB
190  */
SkSplay(uint32_t color,uint32_t * ag,uint32_t * rb)191 static inline void SkSplay(uint32_t color, uint32_t* ag, uint32_t* rb) {
192     static constexpr uint32_t kMask = 0x00FF00FF;
193     *ag = (color >> 8) & kMask;
194     *rb = color & kMask;
195 }
196 
197 /**
198  * 0xAARRGGBB -> 0x00AA00GG00RR00BB
199  * (note, ARGB -> AGRB)
200  */
SkSplay(uint32_t color)201 static inline uint64_t SkSplay(uint32_t color) {
202     static constexpr uint32_t kMask = 0x00FF00FF;
203     uint64_t agrb = (color >> 8) & kMask;  // 0x0000000000AA00GG
204     agrb <<= 32;                           // 0x00AA00GG00000000
205     agrb |= color & kMask;                 // 0x00AA00GG00RR00BB
206     return agrb;
207 }
208 
209 /**
210  * 0xAAxxGGxx, 0xRRxxBBxx-> 0xAARRGGBB
211  */
SkUnsplay(uint32_t ag,uint32_t rb)212 static inline uint32_t SkUnsplay(uint32_t ag, uint32_t rb) {
213     static constexpr uint32_t kMask = 0xFF00FF00;
214     return (ag & kMask) | ((rb & kMask) >> 8);
215 }
216 
217 /**
218  * 0xAAxxGGxxRRxxBBxx -> 0xAARRGGBB
219  * (note, AGRB -> ARGB)
220  */
SkUnsplay(uint64_t agrb)221 static inline uint32_t SkUnsplay(uint64_t agrb) {
222     static constexpr uint32_t kMask = 0xFF00FF00;
223     return SkPMColor(
224         ((agrb & kMask) >> 8) |   // 0x00RR00BB
225         ((agrb >> 32) & kMask));  // 0xAARRGGBB
226 }
227 
SkFastFourByteInterp256_32(SkPMColor src,SkPMColor dst,unsigned scale)228 static inline SkPMColor SkFastFourByteInterp256_32(SkPMColor src, SkPMColor dst, unsigned scale) {
229     SkASSERT(scale <= 256);
230 
231     // Two 8-bit blends per two 32-bit registers, with space to make sure the math doesn't collide.
232     uint32_t src_ag, src_rb, dst_ag, dst_rb;
233     SkSplay(src, &src_ag, &src_rb);
234     SkSplay(dst, &dst_ag, &dst_rb);
235 
236     const uint32_t ret_ag = src_ag * scale + (256 - scale) * dst_ag;
237     const uint32_t ret_rb = src_rb * scale + (256 - scale) * dst_rb;
238 
239     return SkUnsplay(ret_ag, ret_rb);
240 }
241 
SkFastFourByteInterp256_64(SkPMColor src,SkPMColor dst,unsigned scale)242 static inline SkPMColor SkFastFourByteInterp256_64(SkPMColor src, SkPMColor dst, unsigned scale) {
243     SkASSERT(scale <= 256);
244     // Four 8-bit blends in one 64-bit register, with space to make sure the math doesn't collide.
245     return SkUnsplay(SkSplay(src) * scale + (256-scale) * SkSplay(dst));
246 }
247 
248 // TODO(mtklein): Replace slow versions with fast versions, using scale + (scale>>7) everywhere.
249 
250 /**
251  * Same as SkFourByteInterp256, but faster.
252  */
SkFastFourByteInterp256(SkPMColor src,SkPMColor dst,unsigned scale)253 static inline SkPMColor SkFastFourByteInterp256(SkPMColor src, SkPMColor dst, unsigned scale) {
254     // On a 64-bit machine, _64 is about 10% faster than _32, but ~40% slower on a 32-bit machine.
255     if (sizeof(void*) == 4) {
256         return SkFastFourByteInterp256_32(src, dst, scale);
257     } else {
258         return SkFastFourByteInterp256_64(src, dst, scale);
259     }
260 }
261 
262 /**
263  * Nearly the same as SkFourByteInterp, but faster and a touch more accurate, due to better
264  * srcWeight scaling to [0, 256].
265  */
SkFastFourByteInterp(SkPMColor src,SkPMColor dst,U8CPU srcWeight)266 static inline SkPMColor SkFastFourByteInterp(SkPMColor src, SkPMColor dst, U8CPU srcWeight) {
267     SkASSERT(srcWeight <= 255);
268     // scale = srcWeight + (srcWeight >> 7) is more accurate than
269     // scale = srcWeight + 1, but 7% slower
270     return SkFastFourByteInterp256(src, dst, srcWeight + (srcWeight >> 7));
271 }
272 
273 /**
274  * Interpolates between colors src and dst using [0,256] scale.
275  */
SkPMLerp(SkPMColor src,SkPMColor dst,unsigned scale)276 static inline SkPMColor SkPMLerp(SkPMColor src, SkPMColor dst, unsigned scale) {
277     return SkFastFourByteInterp256(src, dst, scale);
278 }
279 
SkBlendARGB32(SkPMColor src,SkPMColor dst,U8CPU aa)280 static inline SkPMColor SkBlendARGB32(SkPMColor src, SkPMColor dst, U8CPU aa) {
281     SkASSERT((unsigned)aa <= 255);
282 
283     unsigned src_scale = SkAlpha255To256(aa);
284     unsigned dst_scale = SkAlphaMulInv256(SkGetPackedA32(src), src_scale);
285 
286     static constexpr uint32_t kMask = 0x00FF00FF;
287 
288     uint32_t src_rb = (src & kMask) * src_scale;
289     uint32_t src_ag = ((src >> 8) & kMask) * src_scale;
290 
291     uint32_t dst_rb = (dst & kMask) * dst_scale;
292     uint32_t dst_ag = ((dst >> 8) & kMask) * dst_scale;
293 
294     return (((src_rb + dst_rb) >> 8) & kMask) | ((src_ag + dst_ag) & ~kMask);
295 }
296 
297 ////////////////////////////////////////////////////////////////////////////////////////////
298 // Convert a 32bit pixel to a 16bit pixel (no dither)
299 
300 #define SkR32ToR16_MACRO(r)   ((unsigned)(r) >> (SK_R32_BITS - SK_R16_BITS))
301 #define SkG32ToG16_MACRO(g)   ((unsigned)(g) >> (SK_G32_BITS - SK_G16_BITS))
302 #define SkB32ToB16_MACRO(b)   ((unsigned)(b) >> (SK_B32_BITS - SK_B16_BITS))
303 
304 #ifdef SK_DEBUG
SkR32ToR16(unsigned r)305     static inline unsigned SkR32ToR16(unsigned r) {
306         SkR32Assert(r);
307         return SkR32ToR16_MACRO(r);
308     }
SkG32ToG16(unsigned g)309     static inline unsigned SkG32ToG16(unsigned g) {
310         SkG32Assert(g);
311         return SkG32ToG16_MACRO(g);
312     }
SkB32ToB16(unsigned b)313     static inline unsigned SkB32ToB16(unsigned b) {
314         SkB32Assert(b);
315         return SkB32ToB16_MACRO(b);
316     }
317 #else
318     #define SkR32ToR16(r)   SkR32ToR16_MACRO(r)
319     #define SkG32ToG16(g)   SkG32ToG16_MACRO(g)
320     #define SkB32ToB16(b)   SkB32ToB16_MACRO(b)
321 #endif
322 
SkPixel32ToPixel16(SkPMColor c)323 static inline U16CPU SkPixel32ToPixel16(SkPMColor c) {
324     unsigned r = ((c >> (SK_R32_SHIFT + (8 - SK_R16_BITS))) & SK_R16_MASK) << SK_R16_SHIFT;
325     unsigned g = ((c >> (SK_G32_SHIFT + (8 - SK_G16_BITS))) & SK_G16_MASK) << SK_G16_SHIFT;
326     unsigned b = ((c >> (SK_B32_SHIFT + (8 - SK_B16_BITS))) & SK_B16_MASK) << SK_B16_SHIFT;
327     return r | g | b;
328 }
329 
SkPack888ToRGB16(U8CPU r,U8CPU g,U8CPU b)330 static inline U16CPU SkPack888ToRGB16(U8CPU r, U8CPU g, U8CPU b) {
331     return  (SkR32ToR16(r) << SK_R16_SHIFT) |
332             (SkG32ToG16(g) << SK_G16_SHIFT) |
333             (SkB32ToB16(b) << SK_B16_SHIFT);
334 }
335 
336 /////////////////////////////////////////////////////////////////////////////////////////
337 
SkPixel16ToColor(U16CPU src)338 static inline SkColor SkPixel16ToColor(U16CPU src) {
339     SkASSERT(src == SkToU16(src));
340 
341     unsigned    r = SkPacked16ToR32(src);
342     unsigned    g = SkPacked16ToG32(src);
343     unsigned    b = SkPacked16ToB32(src);
344 
345     SkASSERT((r >> (8 - SK_R16_BITS)) == SkGetPackedR16(src));
346     SkASSERT((g >> (8 - SK_G16_BITS)) == SkGetPackedG16(src));
347     SkASSERT((b >> (8 - SK_B16_BITS)) == SkGetPackedB16(src));
348 
349     return SkColorSetRGB(r, g, b);
350 }
351 
352 ///////////////////////////////////////////////////////////////////////////////
353 
354 typedef uint16_t SkPMColor16;
355 
356 // Put in OpenGL order (r g b a)
357 #define SK_A4444_SHIFT    0
358 #define SK_R4444_SHIFT    12
359 #define SK_G4444_SHIFT    8
360 #define SK_B4444_SHIFT    4
361 
SkReplicateNibble(unsigned nib)362 static inline U8CPU SkReplicateNibble(unsigned nib) {
363     SkASSERT(nib <= 0xF);
364     return (nib << 4) | nib;
365 }
366 
367 #define SkGetPackedA4444(c)     (((unsigned)(c) >> SK_A4444_SHIFT) & 0xF)
368 #define SkGetPackedR4444(c)     (((unsigned)(c) >> SK_R4444_SHIFT) & 0xF)
369 #define SkGetPackedG4444(c)     (((unsigned)(c) >> SK_G4444_SHIFT) & 0xF)
370 #define SkGetPackedB4444(c)     (((unsigned)(c) >> SK_B4444_SHIFT) & 0xF)
371 
372 #define SkPacked4444ToA32(c)    SkReplicateNibble(SkGetPackedA4444(c))
373 
SkPixel4444ToPixel32(U16CPU c)374 static inline SkPMColor SkPixel4444ToPixel32(U16CPU c) {
375     uint32_t d = (SkGetPackedA4444(c) << SK_A32_SHIFT) |
376                  (SkGetPackedR4444(c) << SK_R32_SHIFT) |
377                  (SkGetPackedG4444(c) << SK_G32_SHIFT) |
378                  (SkGetPackedB4444(c) << SK_B32_SHIFT);
379     return d | (d << 4);
380 }
381 
382 using SkPMColor4f = SkRGBA4f<kPremul_SkAlphaType>;
383 
384 constexpr SkPMColor4f SK_PMColor4fTRANSPARENT = { 0, 0, 0, 0 };
385 constexpr SkPMColor4f SK_PMColor4fBLACK = { 0, 0, 0, 1 };
386 constexpr SkPMColor4f SK_PMColor4fWHITE = { 1, 1, 1, 1 };
387 constexpr SkPMColor4f SK_PMColor4fILLEGAL = { SK_FloatNegativeInfinity,
388                                               SK_FloatNegativeInfinity,
389                                               SK_FloatNegativeInfinity,
390                                               SK_FloatNegativeInfinity };
391 #endif  // SkColorData_DEFINED
392