2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef GFX_COLOR_H
|
|
|
|
#define GFX_COLOR_H
|
|
|
|
|
|
|
|
#include "gfxTypes.h"
|
|
|
|
|
2008-02-18 23:07:01 -08:00
|
|
|
#include "prbit.h" // for PR_ROTATE_(LEFT,RIGHT)32
|
|
|
|
#include "prio.h" // for ntohl
|
|
|
|
|
2012-09-05 22:31:29 -07:00
|
|
|
#include "mozilla/Attributes.h" // for MOZ_ALWAYS_INLINE
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
#define GFX_UINT32_FROM_BPTR(pbptr,i) (((uint32_t*)(pbptr))[i])
|
2008-01-29 22:22:23 -08:00
|
|
|
|
|
|
|
#if defined(IS_BIG_ENDIAN)
|
|
|
|
#define GFX_NTOHL(x) (x)
|
|
|
|
#define GFX_HAVE_CHEAP_NTOHL
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
#if (_MSC_VER >= 1300) // also excludes MinGW
|
|
|
|
#include <stdlib.h>
|
|
|
|
#pragma intrinsic(_byteswap_ulong)
|
|
|
|
#define GFX_NTOHL(x) _byteswap_ulong(x)
|
|
|
|
#define GFX_HAVE_CHEAP_NTOHL
|
|
|
|
#else
|
|
|
|
// A reasonably fast generic little-endian implementation.
|
|
|
|
#define GFX_NTOHL(x) \
|
|
|
|
( (PR_ROTATE_RIGHT32((x),8) & 0xFF00FF00) | \
|
|
|
|
(PR_ROTATE_LEFT32((x),8) & 0x00FF00FF) )
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define GFX_NTOHL(x) ntohl(x)
|
|
|
|
#define GFX_HAVE_CHEAP_NTOHL
|
|
|
|
#endif
|
|
|
|
|
2007-12-21 02:26:31 -08:00
|
|
|
/**
|
2008-01-29 22:22:23 -08:00
|
|
|
* GFX_0XFF_PPIXEL_FROM_BPTR(x)
|
|
|
|
*
|
2008-01-16 20:06:40 -08: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 02:26:31 -08:00
|
|
|
*/
|
2008-01-29 22:22:23 -08:00
|
|
|
#if defined(GFX_HAVE_CHEAP_NTOHL)
|
|
|
|
#define GFX_0XFF_PPIXEL_FROM_UINT32(x) \
|
|
|
|
( (GFX_NTOHL(x) >> 8) | (0xFF << 24) )
|
2007-12-21 02:26:31 -08:00
|
|
|
#else
|
2008-01-29 22:22:23 -08:00
|
|
|
// A reasonably fast generic little-endian implementation.
|
|
|
|
#define GFX_0XFF_PPIXEL_FROM_UINT32(x) \
|
|
|
|
( (PR_ROTATE_LEFT32((x),16) | 0xFF00FF00) & ((x) | 0xFFFF00FF) )
|
2007-12-21 02:26:31 -08:00
|
|
|
#endif
|
|
|
|
|
2008-01-29 22:22:23 -08: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 08:56:38 -07:00
|
|
|
* sizeof(*to) == sizeof(uint32_t)
|
2008-01-29 22:22:23 -08: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 08:56:38 -07:00
|
|
|
uint32_t m0 = GFX_UINT32_FROM_BPTR(from,0), \
|
2008-01-29 22:22:23 -08:00
|
|
|
m1 = GFX_UINT32_FROM_BPTR(from,1), \
|
|
|
|
m2 = GFX_UINT32_FROM_BPTR(from,2), \
|
|
|
|
rgbr = GFX_NTOHL(m0), \
|
|
|
|
gbrg = GFX_NTOHL(m1), \
|
|
|
|
brgb = GFX_NTOHL(m2), \
|
|
|
|
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-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Fast approximate division by 255. It has the property that
|
2009-06-24 02:18:17 -07: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-03-22 10:30:00 -07: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-05 22:31:29 -07:00
|
|
|
* Fast premultiply
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* equivalent to (((c)*(a))/255)
|
|
|
|
*/
|
2012-09-07 16:07:32 -07:00
|
|
|
uint8_t MOZ_ALWAYS_INLINE gfxPreMultiply(uint8_t c, uint8_t a) {
|
2012-09-05 22:31:29 -07:00
|
|
|
return GFX_DIVIDE_BY_255((c)*(a));
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-09-05 22:31:29 -07:00
|
|
|
/**
|
|
|
|
* Pack the 4 8-bit channels (A,R,G,B)
|
2011-01-12 17:45:13 -08:00
|
|
|
* into a 32-bit packed NON-premultiplied pixel.
|
|
|
|
*/
|
2012-09-07 16:07:32 -07:00
|
|
|
uint32_t MOZ_ALWAYS_INLINE
|
|
|
|
gfxPackedPixelNoPreMultiply(uint8_t a, uint8_t r, uint8_t g, uint8_t b) {
|
2012-09-05 22:31:29 -07:00
|
|
|
return (((a) << 24) | ((r) << 16) | ((g) << 8) | (b));
|
|
|
|
}
|
2011-01-12 17:45:13 -08:00
|
|
|
|
2012-09-05 22:31:29 -07:00
|
|
|
/**
|
|
|
|
* Pack the 4 8-bit channels (A,R,G,B)
|
|
|
|
* into a 32-bit packed premultiplied pixel.
|
|
|
|
*/
|
2012-09-07 16:07:32 -07:00
|
|
|
uint32_t MOZ_ALWAYS_INLINE
|
|
|
|
gfxPackedPixel(uint8_t a, uint8_t r, uint8_t g, uint8_t b) {
|
2012-09-05 22:31:29 -07: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-03-22 10:30:00 -07: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?
|
|
|
|
*/
|
|
|
|
struct THEBES_API gfxRGBA {
|
|
|
|
gfxFloat r, g, b, a;
|
|
|
|
|
|
|
|
enum PackedColorType {
|
|
|
|
PACKED_ABGR,
|
|
|
|
PACKED_ABGR_PREMULTIPLIED,
|
|
|
|
|
|
|
|
PACKED_ARGB,
|
|
|
|
PACKED_ARGB_PREMULTIPLIED,
|
|
|
|
|
|
|
|
PACKED_XRGB
|
|
|
|
};
|
|
|
|
|
|
|
|
gfxRGBA() { }
|
|
|
|
/**
|
|
|
|
* Intialize this color using explicit red, green, blue and alpha
|
|
|
|
* values.
|
|
|
|
*/
|
|
|
|
gfxRGBA(gfxFloat _r, gfxFloat _g, gfxFloat _b, gfxFloat _a=1.0) : r(_r), g(_g), b(_b), a(_a) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize this color from a packed 32-bit color.
|
|
|
|
* The color value is interpreted based on colorType;
|
|
|
|
* all values use the native platform endianness.
|
|
|
|
*
|
2012-03-23 15:10:50 -07:00
|
|
|
* Resulting gfxRGBA stores non-premultiplied data.
|
|
|
|
*
|
2007-03-22 10:30:00 -07:00
|
|
|
* @see gfxRGBA::Packed
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxRGBA(uint32_t c, PackedColorType colorType = PACKED_ABGR) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (colorType == PACKED_ABGR ||
|
|
|
|
colorType == PACKED_ABGR_PREMULTIPLIED)
|
|
|
|
{
|
2009-09-08 13:14:39 -07: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);
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (colorType == PACKED_ARGB ||
|
|
|
|
colorType == PACKED_XRGB ||
|
|
|
|
colorType == PACKED_ARGB_PREMULTIPLIED)
|
|
|
|
{
|
2009-09-08 13:14:39 -07: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);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (colorType == PACKED_ABGR_PREMULTIPLIED ||
|
|
|
|
colorType == PACKED_ARGB_PREMULTIPLIED)
|
|
|
|
{
|
|
|
|
if (a > 0.0) {
|
|
|
|
r /= a;
|
|
|
|
g /= a;
|
|
|
|
b /= a;
|
|
|
|
}
|
2011-08-25 01:46:01 -07:00
|
|
|
} else if (colorType == PACKED_XRGB) {
|
2007-03-22 10:30:00 -07:00
|
|
|
a = 1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-21 13:35:49 -07:00
|
|
|
bool operator==(const gfxRGBA& other) const
|
|
|
|
{
|
|
|
|
return r == other.r && g == other.g && b == other.b && a == other.a;
|
|
|
|
}
|
2011-02-16 14:43:30 -08:00
|
|
|
bool operator!=(const gfxRGBA& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2010-06-21 13:35:49 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Returns this color value as a packed 32-bit integer. This reconstructs
|
|
|
|
* the int32 based on the given colorType, always in the native byte order.
|
2008-09-11 17:49:58 -07:00
|
|
|
*
|
|
|
|
* Note: gcc 4.2.3 on at least Ubuntu (x86) does something strange with
|
2012-08-22 08:56:38 -07:00
|
|
|
* (uint8_t)(c * 255.0) << x, where the result is different than
|
|
|
|
* double d = c * 255.0; v = ((uint8_t) d) << x.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t Packed(PackedColorType colorType = PACKED_ABGR) const {
|
2008-09-11 17:49:58 -07:00
|
|
|
gfxFloat rb = (r * 255.0);
|
|
|
|
gfxFloat gb = (g * 255.0);
|
|
|
|
gfxFloat bb = (b * 255.0);
|
|
|
|
gfxFloat ab = (a * 255.0);
|
|
|
|
|
2011-08-25 01:46:01 -07:00
|
|
|
if (colorType == PACKED_ABGR) {
|
2012-08-22 08:56:38 -07:00
|
|
|
return (uint8_t(ab) << 24) |
|
|
|
|
(uint8_t(bb) << 16) |
|
|
|
|
(uint8_t(gb) << 8) |
|
|
|
|
(uint8_t(rb) << 0);
|
2011-08-25 01:46:01 -07:00
|
|
|
}
|
|
|
|
if (colorType == PACKED_ARGB || colorType == PACKED_XRGB) {
|
2012-08-22 08:56:38 -07:00
|
|
|
return (uint8_t(ab) << 24) |
|
|
|
|
(uint8_t(rb) << 16) |
|
|
|
|
(uint8_t(gb) << 8) |
|
|
|
|
(uint8_t(bb) << 0);
|
2008-09-11 17:49:58 -07:00
|
|
|
}
|
|
|
|
|
2011-08-25 01:46:01 -07:00
|
|
|
rb *= a;
|
|
|
|
gb *= a;
|
|
|
|
bb *= a;
|
2008-09-11 17:49:58 -07:00
|
|
|
|
|
|
|
if (colorType == PACKED_ABGR_PREMULTIPLIED) {
|
2012-08-22 08:56:38 -07:00
|
|
|
return (((uint8_t)(ab) << 24) |
|
|
|
|
((uint8_t)(bb) << 16) |
|
|
|
|
((uint8_t)(gb) << 8) |
|
|
|
|
((uint8_t)(rb) << 0));
|
2011-08-25 01:46:01 -07:00
|
|
|
}
|
|
|
|
if (colorType == PACKED_ARGB_PREMULTIPLIED) {
|
2012-08-22 08:56:38 -07:00
|
|
|
return (((uint8_t)(ab) << 24) |
|
|
|
|
((uint8_t)(rb) << 16) |
|
|
|
|
((uint8_t)(gb) << 8) |
|
|
|
|
((uint8_t)(bb) << 0));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-09-11 17:49:58 -07:00
|
|
|
|
|
|
|
return 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _GFX_COLOR_H */
|