| 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) |
| 5 | * |
| 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 |
| 13 | */ |
| 14 | |
| 15 | /* cairo - a vector graphics library with display and print output |
| 16 | * |
| 17 | * Copyright © 2003 University of Southern California |
| 18 | * |
| 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. |
| 26 | * |
| 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 |
| 32 | * |
| 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/ |
| 37 | * |
| 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. |
| 41 | * |
| 42 | * The Original Code is the cairo graphics library. |
| 43 | * |
| 44 | * The Initial Developer of the Original Code is University of Southern |
| 45 | * California. |
| 46 | * |
| 47 | * Contributor(s): |
| 48 | * Carl D. Worth <cworth@cworth.org> |
| 49 | * Kristian Høgsberg <krh@redhat.com> |
| 50 | */ |
| 51 | |
| 52 | #ifndef CAIRO_LUT_PRIVATE_H |
| 53 | #define CAIRO_LUT_PRIVATE_H |
| 54 | |
| 55 | extern const uint8_t color_lut[256][256]; |
| 56 | extern const uint8_t reverse_color_lut[256][256]; |
| 57 | |
| 58 | static inline uint8_t _get_alpha (uint32_t pixel) |
| 59 | { |
| 60 | return (pixel >> 24) & 0xFF; |
| 61 | } |
| 62 | |
| 63 | static inline uint8_t _get_red (uint32_t pixel) |
| 64 | { |
| 65 | return (pixel >> 16) & 0xFF; |
| 66 | } |
| 67 | |
| 68 | static inline uint8_t _get_green (uint32_t pixel) |
| 69 | { |
| 70 | return (pixel >> 8) & 0xFF; |
| 71 | } |
| 72 | |
| 73 | static inline uint8_t _get_blue (uint32_t pixel) |
| 74 | { |
| 75 | return pixel & 0xFF; |
| 76 | } |
| 77 | |
| 78 | static inline uint32_t |
| 79 | rgb_to_pixel (uint8_t r, uint8_t g, uint8_t b) |
| 80 | { |
| 81 | return ((r << 16) + (g << 8) + b); |
| 82 | } |
| 83 | |
| 84 | static inline uint32_t |
| 85 | argb_to_pixel (uint8_t alpha, uint8_t r, uint8_t g, uint8_t b) |
| 86 | { |
| 87 | return ((alpha << 24) + rgb_to_pixel(r, g, b)); |
| 88 | } |
| 89 | |
| 90 | static inline void |
| 91 | pixel_to_rgb (uint32_t pixel, uint8_t *r, uint8_t *g, uint8_t *b) |
| 92 | { |
| 93 | *r = _get_red(pixel); |
| 94 | *g = _get_green(pixel); |
| 95 | *b = _get_blue(pixel); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * alpha_cairo_to_rgb: |
| 100 | * @al: The alpha value to apply to the pixel |
| 101 | * @pixel: the regular RGB pixel value |
| 102 | * |
| 103 | * return regular R/G/B channel. (non-pre-multiplied) |
| 104 | * |
| 105 | */ |
| 106 | static inline void |
| 107 | alpha_cairo_to_rgb (uint8_t al, uint32_t pixel, |
| 108 | uint8_t *r, uint8_t *g, uint8_t *b) |
| 109 | { |
| 110 | if (al != 0xFF) { |
| 111 | if (al == 0) { |
| 112 | *r = 0; |
| 113 | *g = 0; |
| 114 | *b = 0; |
| 115 | } else { |
| 116 | *r = color_lut[al][_get_red(pixel)]; |
| 117 | *g = color_lut[al][_get_green(pixel)]; |
| 118 | *b = color_lut[al][_get_blue(pixel)]; |
| 119 | } |
| 120 | } else { |
| 121 | pixel_to_rgb(pixel, r, g, b); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * cairo_to_rgb: |
| 127 | * @pixel: the regular ARGB pixel value |
| 128 | * |
| 129 | * return regular R/G/B channel. (non-pre-multiplied) |
| 130 | * |
| 131 | */ |
| 132 | static inline void |
| 133 | cairo_to_rgb (uint32_t pixel, uint8_t *r, uint8_t *g, uint8_t *b) |
| 134 | { |
| 135 | alpha_cairo_to_rgb(_get_alpha(pixel), pixel, r, g, b); |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * cairo_to_pixel: |
| 140 | * @pixel: the premultiplied ARGB pixel value |
| 141 | * |
| 142 | * return regular Alpha/R/G/B channel. (non-pre-multiplied) |
| 143 | * |
| 144 | */ |
| 145 | static inline uint32_t |
| 146 | cairo_to_pixel (uint32_t pixel) |
| 147 | { |
| 148 | uint8_t alpha, r, g, b; |
| 149 | alpha = _get_alpha (pixel); |
| 150 | |
| 151 | if (alpha != 0xFF) { |
| 152 | if (alpha == 0) { |
| 153 | return(0); |
| 154 | } |
| 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); |
| 159 | } |
| 160 | return(pixel); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * cairo_to_rgbpixel: |
| 165 | * @pixel: the premultiplied ARGB pixel value |
| 166 | * |
| 167 | * return R/G/B channel. |
| 168 | * |
| 169 | */ |
| 170 | static inline uint32_t |
| 171 | cairo_to_rgbpixel (uint32_t pixel) |
| 172 | { |
| 173 | return(cairo_to_pixel(pixel) & 0xFFFFFF); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * argb_to_cairo: |
| 178 | * @a: the alpha value |
| 179 | * @r: the red value |
| 180 | * @g: the green value |
| 181 | * @b: the blue value |
| 182 | * |
| 183 | * given a regular ARGB, return corresponding pre-multipled pixel value. |
| 184 | * this is the reverse function for cairo_to_rgb. |
| 185 | * |
| 186 | * Return value: internal pre-multiplied pixel value. |
| 187 | * |
| 188 | */ |
| 189 | static inline uint32_t |
| 190 | argb_to_cairo (uint8_t a, uint8_t r, uint8_t g, uint8_t b) |
| 191 | { |
| 192 | if (a != 0xFF) { |
| 193 | r = reverse_color_lut[a][r]; |
| 194 | g = reverse_color_lut[a][g]; |
| 195 | b = reverse_color_lut[a][b]; |
| 196 | } |
| 197 | return argb_to_pixel (a, r, g, b); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * pixel_to_cairo: |
| 202 | * @pixel_r: the regular ARGB pixel value. |
| 203 | * |
| 204 | * given a regular ARGB, return corresponding pre-multipled pixel value. |
| 205 | * this is the reverse function for cairo_to_pixel. |
| 206 | * |
| 207 | * Return value: internal pre-multiplied pixel value. |
| 208 | * |
| 209 | */ |
| 210 | static inline uint32_t |
| 211 | pixel_to_cairo (uint32_t pixel_r) |
| 212 | { |
| 213 | uint8_t alpha; |
| 214 | |
| 215 | alpha = _get_alpha (pixel_r); |
| 216 | if (alpha == 0xFF) |
| 217 | return pixel_r; |
| 218 | |
| 219 | return argb_to_cairo (_get_alpha(pixel_r), |
| 220 | _get_red(pixel_r), |
| 221 | _get_green(pixel_r), |
| 222 | _get_blue(pixel_r)); |
| 223 | } |
| 224 | |
| 225 | #endif /* CAIRO_LUT_PRIVATE_H */ |