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
|
|
|
|
2005-04-08 09:44:32 +04:00
|
|
|
#include "gfxTypes.h"
|
|
|
|
|
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
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
#define GFX_UINT32_FROM_BPTR(pbptr,i) (((uint32_t*)(pbptr))[i])
|
2008-01-30 09:22:23 +03:00
|
|
|
|
2007-12-21 13:26:31 +03:00
|
|
|
/**
|
2008-01-30 09:22:23 +03:00
|
|
|
* GFX_0XFF_PPIXEL_FROM_BPTR(x)
|
|
|
|
*
|
2008-01-17 07:06:40 +03:00
|
|
|
* Avoid tortured construction of 32-bit ARGB pixel from 3 individual bytes
|
|
|
|
* of memory plus constant 0xFF. RGB bytes are already contiguous!
|
|
|
|
* Equivalent to: GFX_PACKED_PIXEL(0xff,r,g,b)
|
|
|
|
*
|
|
|
|
* Attempt to use fast byte-swapping instruction(s), e.g. bswap on x86, in
|
|
|
|
* preference to a sequence of shift/or operations.
|
2007-12-21 13:26:31 +03:00
|
|
|
*/
|
2013-04-24 11:40:33 +04:00
|
|
|
#define GFX_0XFF_PPIXEL_FROM_UINT32(x) \
|
|
|
|
( (mozilla::NativeEndian::swapToBigEndian(uint32_t(x)) >> 8) | (0xFFU << 24) )
|
2007-12-21 13:26:31 +03:00
|
|
|
|
2008-01-30 09:22:23 +03:00
|
|
|
#define GFX_0XFF_PPIXEL_FROM_BPTR(x) \
|
|
|
|
( GFX_0XFF_PPIXEL_FROM_UINT32(GFX_UINT32_FROM_BPTR((x),0)) )
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 \
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t m0 = GFX_UINT32_FROM_BPTR(from,0), \
|
2008-01-30 09:22:23 +03:00
|
|
|
m1 = GFX_UINT32_FROM_BPTR(from,1), \
|
|
|
|
m2 = GFX_UINT32_FROM_BPTR(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-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* A color value, storing red, green, blue and alpha components.
|
|
|
|
* This class does not use premultiplied alpha.
|
|
|
|
*
|
|
|
|
* XXX should this use doubles (instead of gfxFloat), for consistency with
|
|
|
|
* cairo?
|
|
|
|
*/
|
2013-05-30 01:59:24 +04:00
|
|
|
struct gfxRGBA {
|
2005-04-08 09:44:32 +04:00
|
|
|
gfxFloat r, g, b, a;
|
2005-04-06 05:54:26 +04:00
|
|
|
|
2006-11-02 02:13:37 +03:00
|
|
|
enum PackedColorType {
|
|
|
|
PACKED_ABGR,
|
|
|
|
PACKED_ABGR_PREMULTIPLIED,
|
|
|
|
|
|
|
|
PACKED_ARGB,
|
|
|
|
PACKED_ARGB_PREMULTIPLIED,
|
|
|
|
|
|
|
|
PACKED_XRGB
|
|
|
|
};
|
|
|
|
|
2005-04-06 05:54:26 +04:00
|
|
|
gfxRGBA() { }
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Intialize this color using explicit red, green, blue and alpha
|
|
|
|
* values.
|
|
|
|
*/
|
2013-11-22 23:45:50 +04:00
|
|
|
MOZ_CONSTEXPR gfxRGBA(gfxFloat _r, gfxFloat _g, gfxFloat _b, gfxFloat _a=1.0) : r(_r), g(_g), b(_b), a(_a) {}
|
2006-11-02 02:13:37 +03:00
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
|
|
|
* Initialize this color from a packed 32-bit color.
|
2006-11-02 02:13:37 +03:00
|
|
|
* The color value is interpreted based on colorType;
|
|
|
|
* all values use the native platform endianness.
|
2005-08-20 09:36:47 +04:00
|
|
|
*
|
2012-03-24 02:10:50 +04:00
|
|
|
* Resulting gfxRGBA stores non-premultiplied data.
|
|
|
|
*
|
2005-08-20 09:36:47 +04:00
|
|
|
* @see gfxRGBA::Packed
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
gfxRGBA(uint32_t c, PackedColorType colorType = PACKED_ABGR) {
|
2006-11-02 02:13:37 +03:00
|
|
|
if (colorType == PACKED_ABGR ||
|
2006-11-08 04:34:10 +03:00
|
|
|
colorType == PACKED_ABGR_PREMULTIPLIED)
|
2006-11-02 02:13:37 +03:00
|
|
|
{
|
2009-09-09 00:14:39 +04:00
|
|
|
r = ((c >> 0) & 0xff) * (1.0 / 255.0);
|
|
|
|
g = ((c >> 8) & 0xff) * (1.0 / 255.0);
|
|
|
|
b = ((c >> 16) & 0xff) * (1.0 / 255.0);
|
|
|
|
a = ((c >> 24) & 0xff) * (1.0 / 255.0);
|
2006-11-02 02:13:37 +03:00
|
|
|
} else if (colorType == PACKED_ARGB ||
|
2006-11-08 04:34:10 +03:00
|
|
|
colorType == PACKED_XRGB ||
|
|
|
|
colorType == PACKED_ARGB_PREMULTIPLIED)
|
2006-11-02 02:13:37 +03:00
|
|
|
{
|
2009-09-09 00:14:39 +04:00
|
|
|
b = ((c >> 0) & 0xff) * (1.0 / 255.0);
|
|
|
|
g = ((c >> 8) & 0xff) * (1.0 / 255.0);
|
|
|
|
r = ((c >> 16) & 0xff) * (1.0 / 255.0);
|
|
|
|
a = ((c >> 24) & 0xff) * (1.0 / 255.0);
|
2006-11-02 02:13:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (colorType == PACKED_ABGR_PREMULTIPLIED ||
|
|
|
|
colorType == PACKED_ARGB_PREMULTIPLIED)
|
|
|
|
{
|
|
|
|
if (a > 0.0) {
|
|
|
|
r /= a;
|
|
|
|
g /= a;
|
|
|
|
b /= a;
|
|
|
|
}
|
2011-08-25 12:46:01 +04:00
|
|
|
} else if (colorType == PACKED_XRGB) {
|
2006-11-02 02:13:37 +03:00
|
|
|
a = 1.0;
|
|
|
|
}
|
2005-04-06 05:54:26 +04:00
|
|
|
}
|
2006-11-02 02:13:37 +03:00
|
|
|
|
2010-06-22 00:35:49 +04:00
|
|
|
bool operator==(const gfxRGBA& other) const
|
|
|
|
{
|
|
|
|
return r == other.r && g == other.g && b == other.b && a == other.a;
|
|
|
|
}
|
2011-02-17 01:43:30 +03:00
|
|
|
bool operator!=(const gfxRGBA& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2010-06-22 00:35:49 +04:00
|
|
|
|
2005-08-20 09:36:47 +04:00
|
|
|
/**
|
2006-11-02 02:13:37 +03:00
|
|
|
* Returns this color value as a packed 32-bit integer. This reconstructs
|
2012-10-12 03:38:04 +04:00
|
|
|
* the int32_t based on the given colorType, always in the native byte order.
|
2008-09-12 04:49:58 +04:00
|
|
|
*
|
|
|
|
* Note: gcc 4.2.3 on at least Ubuntu (x86) does something strange with
|
2012-08-22 19:56:38 +04:00
|
|
|
* (uint8_t)(c * 255.0) << x, where the result is different than
|
|
|
|
* double d = c * 255.0; v = ((uint8_t) d) << x.
|
2005-08-20 09:36:47 +04:00
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t Packed(PackedColorType colorType = PACKED_ABGR) const {
|
2008-09-12 04:49:58 +04:00
|
|
|
gfxFloat rb = (r * 255.0);
|
|
|
|
gfxFloat gb = (g * 255.0);
|
|
|
|
gfxFloat bb = (b * 255.0);
|
|
|
|
gfxFloat ab = (a * 255.0);
|
|
|
|
|
2011-08-25 12:46:01 +04:00
|
|
|
if (colorType == PACKED_ABGR) {
|
2012-08-22 19:56:38 +04:00
|
|
|
return (uint8_t(ab) << 24) |
|
|
|
|
(uint8_t(bb) << 16) |
|
|
|
|
(uint8_t(gb) << 8) |
|
|
|
|
(uint8_t(rb) << 0);
|
2011-08-25 12:46:01 +04:00
|
|
|
}
|
|
|
|
if (colorType == PACKED_ARGB || colorType == PACKED_XRGB) {
|
2012-08-22 19:56:38 +04:00
|
|
|
return (uint8_t(ab) << 24) |
|
|
|
|
(uint8_t(rb) << 16) |
|
|
|
|
(uint8_t(gb) << 8) |
|
|
|
|
(uint8_t(bb) << 0);
|
2008-09-12 04:49:58 +04:00
|
|
|
}
|
|
|
|
|
2011-08-25 12:46:01 +04:00
|
|
|
rb *= a;
|
|
|
|
gb *= a;
|
|
|
|
bb *= a;
|
2008-09-12 04:49:58 +04:00
|
|
|
|
|
|
|
if (colorType == PACKED_ABGR_PREMULTIPLIED) {
|
2012-08-22 19:56:38 +04:00
|
|
|
return (((uint8_t)(ab) << 24) |
|
|
|
|
((uint8_t)(bb) << 16) |
|
|
|
|
((uint8_t)(gb) << 8) |
|
|
|
|
((uint8_t)(rb) << 0));
|
2011-08-25 12:46:01 +04:00
|
|
|
}
|
|
|
|
if (colorType == PACKED_ARGB_PREMULTIPLIED) {
|
2012-08-22 19:56:38 +04:00
|
|
|
return (((uint8_t)(ab) << 24) |
|
|
|
|
((uint8_t)(rb) << 16) |
|
|
|
|
((uint8_t)(gb) << 8) |
|
|
|
|
((uint8_t)(bb) << 0));
|
2006-11-02 02:13:37 +03:00
|
|
|
}
|
2008-09-12 04:49:58 +04:00
|
|
|
|
|
|
|
return 0;
|
2005-04-06 05:54:26 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2005-04-08 09:44:32 +04:00
|
|
|
#endif /* _GFX_COLOR_H */
|