1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/Attributes.h" // for MOZ_ALWAYS_INLINE
10 #include "mozilla/gfx/Types.h" // for mozilla::gfx::SurfaceFormatBit
13 * Fast approximate division by 255. It has the property that
14 * for all 0 <= n <= 255*255, GFX_DIVIDE_BY_255(n) == n/255.
15 * But it only uses two adds and two shifts instead of an
16 * integer division (which is expensive on many processors).
18 * equivalent to ((v)/255)
20 #define GFX_DIVIDE_BY_255(v) \
21 (((((unsigned)(v)) << 8) + ((unsigned)(v)) + 255) >> 16)
26 * equivalent to (((c)*(a))/255)
28 uint8_t MOZ_ALWAYS_INLINE
gfxPreMultiply(uint8_t c
, uint8_t a
) {
29 return GFX_DIVIDE_BY_255((c
) * (a
));
33 * Pack the 4 8-bit channels (A,R,G,B)
34 * into a 32-bit packed NON-premultiplied pixel.
36 uint32_t MOZ_ALWAYS_INLINE
gfxPackedPixelNoPreMultiply(uint8_t a
, uint8_t r
,
37 uint8_t g
, uint8_t b
) {
38 return (((a
) << mozilla::gfx::SurfaceFormatBit::OS_A
) |
39 ((r
) << mozilla::gfx::SurfaceFormatBit::OS_R
) |
40 ((g
) << mozilla::gfx::SurfaceFormatBit::OS_G
) |
41 ((b
) << mozilla::gfx::SurfaceFormatBit::OS_B
));
45 * Pack the 4 8-bit channels (A,R,G,B)
46 * into a 32-bit packed premultiplied pixel.
48 uint32_t MOZ_ALWAYS_INLINE
gfxPackedPixel(uint8_t a
, uint8_t r
, uint8_t g
,
53 return gfxPackedPixelNoPreMultiply(a
, r
, g
, b
);
55 return ((a
) << mozilla::gfx::SurfaceFormatBit::OS_A
) |
56 (gfxPreMultiply(r
, a
) << mozilla::gfx::SurfaceFormatBit::OS_R
) |
57 (gfxPreMultiply(g
, a
) << mozilla::gfx::SurfaceFormatBit::OS_G
) |
58 (gfxPreMultiply(b
, a
) << mozilla::gfx::SurfaceFormatBit::OS_B
);
62 #endif /* _GFX_COLOR_H */