2005-04-06 05:54:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2007-01-27 04:26:49 +03:00
|
|
|
#ifndef GFX_COLOR_H
|
|
|
|
#define GFX_COLOR_H
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2012-09-06 09:31:29 +04:00
|
|
|
#include "mozilla/Attributes.h" // for MOZ_ALWAYS_INLINE
|
2013-04-24 11:40:33 +04:00
|
|
|
#include "mozilla/Endian.h" // for mozilla::NativeEndian::swapToBigEndian
|
2012-09-06 09:31:29 +04:00
|
|
|
|
2008-01-30 09:22:23 +03:00
|
|
|
/**
|
|
|
|
* GFX_BLOCK_RGB_TO_FRGB(from,to)
|
|
|
|
* sizeof(*from) == sizeof(char)
|
2012-08-22 19:56:38 +04:00
|
|
|
* sizeof(*to) == sizeof(uint32_t)
|
2008-01-30 09:22:23 +03:00
|
|
|
*
|
|
|
|
* Copy 4 pixels at a time, reading blocks of 12 bytes (RGB x4)
|
|
|
|
* and writing blocks of 16 bytes (FRGB x4)
|
|
|
|
*/
|
|
|
|
#define GFX_BLOCK_RGB_TO_FRGB(from,to) \
|
|
|
|
PR_BEGIN_MACRO \
|
2015-09-25 05:24:16 +03:00
|
|
|
uint32_t m0 = ((uint32_t*)from)[0], \
|
|
|
|
m1 = ((uint32_t*)from)[1], \
|
|
|
|
m2 = ((uint32_t*)from)[2], \
|
2013-04-24 11:40:33 +04:00
|
|
|
rgbr = mozilla::NativeEndian::swapToBigEndian(m0), \
|
|
|
|
gbrg = mozilla::NativeEndian::swapToBigEndian(m1), \
|
|
|
|
brgb = mozilla::NativeEndian::swapToBigEndian(m2), \
|
2008-01-30 09:22:23 +03:00
|
|
|
p0, p1, p2, p3; \
|
|
|
|
p0 = 0xFF000000 | ((rgbr) >> 8); \
|
|
|
|
p1 = 0xFF000000 | ((rgbr) << 16) | ((gbrg) >> 16); \
|
|
|
|
p2 = 0xFF000000 | ((gbrg) << 8) | ((brgb) >> 24); \
|
|
|
|
p3 = 0xFF000000 | (brgb); \
|
|
|
|
to[0] = p0; to[1] = p1; to[2] = p2; to[3] = p3; \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
2007-02-01 00:09:20 +03:00
|
|
|
/**
|
|
|
|
* Fast approximate division by 255. It has the property that
|
2009-06-24 13:18:17 +04:00
|
|
|
* for all 0 <= n <= 255*255, GFX_DIVIDE_BY_255(n) == n/255.
|
|
|
|
* But it only uses two adds and two shifts instead of an
|
2007-02-01 00:09:20 +03:00
|
|
|
* integer division (which is expensive on many processors).
|
|
|
|
*
|
|
|
|
* equivalent to ((v)/255)
|
|
|
|
*/
|
|
|
|
#define GFX_DIVIDE_BY_255(v) \
|
|
|
|
(((((unsigned)(v)) << 8) + ((unsigned)(v)) + 255) >> 16)
|
|
|
|
|
|
|
|
/**
|
2012-09-06 09:31:29 +04:00
|
|
|
* Fast premultiply
|
2007-02-01 00:09:20 +03:00
|
|
|
*
|
|
|
|
* equivalent to (((c)*(a))/255)
|
|
|
|
*/
|
2012-09-08 03:07:32 +04:00
|
|
|
uint8_t MOZ_ALWAYS_INLINE gfxPreMultiply(uint8_t c, uint8_t a) {
|
2012-09-06 09:31:29 +04:00
|
|
|
return GFX_DIVIDE_BY_255((c)*(a));
|
|
|
|
}
|
2007-02-01 00:09:20 +03:00
|
|
|
|
2012-09-06 09:31:29 +04:00
|
|
|
/**
|
|
|
|
* Pack the 4 8-bit channels (A,R,G,B)
|
2011-01-13 04:45:13 +03:00
|
|
|
* into a 32-bit packed NON-premultiplied pixel.
|
|
|
|
*/
|
2012-09-08 03:07:32 +04:00
|
|
|
uint32_t MOZ_ALWAYS_INLINE
|
|
|
|
gfxPackedPixelNoPreMultiply(uint8_t a, uint8_t r, uint8_t g, uint8_t b) {
|
2012-09-06 09:31:29 +04:00
|
|
|
return (((a) << 24) | ((r) << 16) | ((g) << 8) | (b));
|
|
|
|
}
|
2011-01-13 04:45:13 +03:00
|
|
|
|
2012-09-06 09:31:29 +04:00
|
|
|
/**
|
|
|
|
* Pack the 4 8-bit channels (A,R,G,B)
|
|
|
|
* into a 32-bit packed premultiplied pixel.
|
|
|
|
*/
|
2012-09-08 03:07:32 +04:00
|
|
|
uint32_t MOZ_ALWAYS_INLINE
|
|
|
|
gfxPackedPixel(uint8_t a, uint8_t r, uint8_t g, uint8_t b) {
|
2012-09-06 09:31:29 +04:00
|
|
|
if (a == 0x00)
|
|
|
|
return 0x00000000;
|
|
|
|
else if (a == 0xFF) {
|
|
|
|
return gfxPackedPixelNoPreMultiply(a, r, g, b);
|
|
|
|
} else {
|
|
|
|
return ((a) << 24) |
|
|
|
|
(gfxPreMultiply(r,a) << 16) |
|
|
|
|
(gfxPreMultiply(g,a) << 8) |
|
|
|
|
(gfxPreMultiply(b,a));
|
|
|
|
}
|
|
|
|
}
|
2007-02-01 00:09:20 +03:00
|
|
|
|
2005-04-08 09:44:32 +04:00
|
|
|
#endif /* _GFX_COLOR_H */
|