1 /* Copyright 2006 Google Inc. All Rights Reserved.
2 * amcrae@google.com (Andrew McRae)
3 * Converted from cairo-lut.c
4 * Author: yangzh@google.com (Zhonghao Yang)
6 * Inlines and defines for pre-multiplied alpha color conversion.
7 * The naming convention is:
8 * rgb - separate values for red, green, blue
9 * argb - separate values for alpha, red, green, blue
10 * pixel - 32 bit combined value, no premultiplication
11 * rgbpixel - 24 bit RGB, no alpha.
12 * cairo - pixel that has alpha premultiplication
15 /* cairo - a vector graphics library with display and print output
17 * Copyright © 2003 University of Southern California
19 * This library is free software; you can redistribute it and/or
20 * modify it either under the terms of the GNU Lesser General Public
21 * License version 2.1 as published by the Free Software Foundation
22 * (the "LGPL") or, at your option, under the terms of the Mozilla
23 * Public License Version 1.1 (the "MPL"). If you do not alter this
24 * notice, a recipient may use your version of this file under either
25 * the MPL or the LGPL.
27 * You should have received a copy of the LGPL along with this library
28 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * You should have received a copy of the MPL along with this library
31 * in the file COPYING-MPL-1.1
33 * The contents of this file are subject to the Mozilla Public License
34 * Version 1.1 (the "License"); you may not use this file except in
35 * compliance with the License. You may obtain a copy of the License at
36 * http://www.mozilla.org/MPL/
38 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
39 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
40 * the specific language governing rights and limitations.
42 * The Original Code is the cairo graphics library.
44 * The Initial Developer of the Original Code is University of Southern
48 * Carl D. Worth <cworth@cworth.org>
49 * Kristian Høgsberg <krh@redhat.com>
52 #ifndef CAIRO_LUT_PRIVATE_H
53 #define CAIRO_LUT_PRIVATE_H
55 extern const uint8_t color_lut
[256][256];
56 extern const uint8_t reverse_color_lut
[256][256];
58 static inline uint8_t _get_alpha (uint32_t pixel
)
60 return (pixel
>> 24) & 0xFF;
63 static inline uint8_t _get_red (uint32_t pixel
)
65 return (pixel
>> 16) & 0xFF;
68 static inline uint8_t _get_green (uint32_t pixel
)
70 return (pixel
>> 8) & 0xFF;
73 static inline uint8_t _get_blue (uint32_t pixel
)
78 static inline uint32_t
79 rgb_to_pixel (uint8_t r
, uint8_t g
, uint8_t b
)
81 return ((r
<< 16) + (g
<< 8) + b
);
84 static inline uint32_t
85 argb_to_pixel (uint8_t alpha
, uint8_t r
, uint8_t g
, uint8_t b
)
87 return ((alpha
<< 24) + rgb_to_pixel(r
, g
, b
));
91 pixel_to_rgb (uint32_t pixel
, uint8_t *r
, uint8_t *g
, uint8_t *b
)
94 *g
= _get_green(pixel
);
95 *b
= _get_blue(pixel
);
100 * @al: The alpha value to apply to the pixel
101 * @pixel: the regular RGB pixel value
103 * return regular R/G/B channel. (non-pre-multiplied)
107 alpha_cairo_to_rgb (uint8_t al
, uint32_t pixel
,
108 uint8_t *r
, uint8_t *g
, uint8_t *b
)
116 *r
= color_lut
[al
][_get_red(pixel
)];
117 *g
= color_lut
[al
][_get_green(pixel
)];
118 *b
= color_lut
[al
][_get_blue(pixel
)];
121 pixel_to_rgb(pixel
, r
, g
, b
);
127 * @pixel: the regular ARGB pixel value
129 * return regular R/G/B channel. (non-pre-multiplied)
133 cairo_to_rgb (uint32_t pixel
, uint8_t *r
, uint8_t *g
, uint8_t *b
)
135 alpha_cairo_to_rgb(_get_alpha(pixel
), pixel
, r
, g
, b
);
140 * @pixel: the premultiplied ARGB pixel value
142 * return regular Alpha/R/G/B channel. (non-pre-multiplied)
145 static inline uint32_t
146 cairo_to_pixel (uint32_t pixel
)
148 uint8_t alpha
, r
, g
, b
;
149 alpha
= _get_alpha (pixel
);
155 r
= color_lut
[alpha
][_get_red(pixel
)];
156 g
= color_lut
[alpha
][_get_green(pixel
)];
157 b
= color_lut
[alpha
][_get_blue(pixel
)];
158 pixel
= argb_to_pixel(alpha
, r
, g
, b
);
165 * @pixel: the premultiplied ARGB pixel value
167 * return R/G/B channel.
170 static inline uint32_t
171 cairo_to_rgbpixel (uint32_t pixel
)
173 return(cairo_to_pixel(pixel
) & 0xFFFFFF);
178 * @a: the alpha value
180 * @g: the green value
183 * given a regular ARGB, return corresponding pre-multipled pixel value.
184 * this is the reverse function for cairo_to_rgb.
186 * Return value: internal pre-multiplied pixel value.
189 static inline uint32_t
190 argb_to_cairo (uint8_t a
, uint8_t r
, uint8_t g
, uint8_t b
)
193 r
= reverse_color_lut
[a
][r
];
194 g
= reverse_color_lut
[a
][g
];
195 b
= reverse_color_lut
[a
][b
];
197 return argb_to_pixel (a
, r
, g
, b
);
202 * @pixel_r: the regular ARGB pixel value.
204 * given a regular ARGB, return corresponding pre-multipled pixel value.
205 * this is the reverse function for cairo_to_pixel.
207 * Return value: internal pre-multiplied pixel value.
210 static inline uint32_t
211 pixel_to_cairo (uint32_t pixel_r
)
215 alpha
= _get_alpha (pixel_r
);
219 return argb_to_cairo (_get_alpha(pixel_r
),
225 #endif /* CAIRO_LUT_PRIVATE_H */