fix crashes reported by Debian Cylab Mayhem Team
[swftools.git] / lib / art / art_rgba.c
blobcde183cc52c2b03c227b1c7c6493bd23d3b2af9d
1 /*
2 * art_rgba.c: Functions for manipulating RGBA pixel data.
4 * Libart_LGPL - library of basic graphic primitives
5 * Copyright (C) 2000 Raph Levien
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
23 #include "config.h"
24 #include "art_rgba.h"
26 #define ART_OPTIMIZE_SPACE
28 #ifndef ART_OPTIMIZE_SPACE
29 #include "art_rgba_table.c"
30 #endif
32 /**
33 * art_rgba_rgba_composite: Composite RGBA image over RGBA buffer.
34 * @dst: Destination RGBA buffer.
35 * @src: Source RGBA buffer.
36 * @n: Number of RGBA pixels to composite.
38 * Composites the RGBA pixels in @dst over the @src buffer.
39 **/
40 void
41 art_rgba_rgba_composite (art_u8 *dst, const art_u8 *src, int n)
43 int i;
44 #ifdef WORDS_BIGENDIAN
45 art_u32 src_rgba, dst_rgba;
46 #else
47 art_u32 src_abgr, dst_abgr;
48 #endif
49 art_u8 src_alpha, dst_alpha;
51 for (i = 0; i < n; i++)
53 #ifdef WORDS_BIGENDIAN
54 src_rgba = ((art_u32 *)src)[i];
55 src_alpha = src_rgba & 0xff;
56 #else
57 src_abgr = ((art_u32 *)src)[i];
58 src_alpha = (src_abgr >> 24) & 0xff;
59 #endif
60 if (src_alpha)
62 if (src_alpha == 0xff ||
64 #ifdef WORDS_BIGENDIAN
65 dst_rgba = ((art_u32 *)dst)[i],
66 dst_alpha = dst_rgba & 0xff,
67 #else
68 dst_abgr = ((art_u32 *)dst)[i],
69 dst_alpha = (dst_abgr >> 24),
70 #endif
71 dst_alpha == 0))
72 #ifdef WORDS_BIGENDIAN
73 ((art_u32 *)dst)[i] = src_rgba;
74 #else
75 ((art_u32 *)dst)[i] = src_abgr;
76 #endif
77 else
79 int r, g, b, a;
80 int src_r, src_g, src_b;
81 int dst_r, dst_g, dst_b;
82 int tmp;
83 int c;
85 #ifdef ART_OPTIMIZE_SPACE
86 tmp = (255 - src_alpha) * (255 - dst_alpha) + 0x80;
87 a = 255 - ((tmp + (tmp >> 8)) >> 8);
88 c = ((src_alpha << 16) + (a >> 1)) / a;
89 #else
90 tmp = art_rgba_composite_table[(src_alpha << 8) + dst_alpha];
91 c = tmp & 0x1ffff;
92 a = tmp >> 24;
93 #endif
94 #ifdef WORDS_BIGENDIAN
95 src_r = (src_rgba >> 24) & 0xff;
96 src_g = (src_rgba >> 16) & 0xff;
97 src_b = (src_rgba >> 8) & 0xff;
98 dst_r = (dst_rgba >> 24) & 0xff;
99 dst_g = (dst_rgba >> 16) & 0xff;
100 dst_b = (dst_rgba >> 8) & 0xff;
101 #else
102 src_r = src_abgr & 0xff;
103 src_g = (src_abgr >> 8) & 0xff;
104 src_b = (src_abgr >> 16) & 0xff;
105 dst_r = dst_abgr & 0xff;
106 dst_g = (dst_abgr >> 8) & 0xff;
107 dst_b = (dst_abgr >> 16) & 0xff;
108 #endif
109 r = dst_r + (((src_r - dst_r) * c + 0x8000) >> 16);
110 g = dst_g + (((src_g - dst_g) * c + 0x8000) >> 16);
111 b = dst_b + (((src_b - dst_b) * c + 0x8000) >> 16);
112 #ifdef WORDS_BIGENDIAN
113 ((art_u32 *)dst)[i] = (r << 24) | (g << 16) | (b << 8) | a;
114 #else
115 ((art_u32 *)dst)[i] = (a << 24) | (b << 16) | (g << 8) | r;
116 #endif
119 #if 0
120 /* it's not clear to me this optimization really wins */
121 else
123 /* skip over run of transparent pixels */
124 for (; i < n - 1; i++)
126 #ifdef WORDS_BIGENDIAN
127 src_rgba = ((art_u32 *)src)[i + 1];
128 if (src_rgba & 0xff)
129 break;
130 #else
131 src_abgr = ((art_u32 *)src)[i + 1];
132 if (src_abgr & 0xff000000)
133 break;
134 #endif
137 #endif
142 * art_rgba_fill_run: fill an RGBA buffer a solid RGB color.
143 * @buf: Buffer to fill.
144 * @r: Red, range 0..255.
145 * @g: Green, range 0..255.
146 * @b: Blue, range 0..255.
147 * @n: Number of RGB triples to fill.
149 * Fills a buffer with @n copies of the (@r, @g, @b) triple, solid
150 * alpha. Thus, locations @buf (inclusive) through @buf + 4 * @n
151 * (exclusive) are written.
153 void
154 art_rgba_fill_run (art_u8 *buf, art_u8 r, art_u8 g, art_u8 b, int n)
156 int i;
157 #ifdef WORDS_BIGENDIAN
158 art_u32 src_rgba;
159 #else
160 art_u32 src_abgr;
161 #endif
163 #ifdef WORDS_BIGENDIAN
164 src_rgba = (r << 24) | (g << 16) | (b << 8) | 255;
165 #else
166 src_abgr = (255 << 24) | (b << 16) | (g << 8) | r;
167 #endif
168 for (i = 0; i < n; i++)
170 #ifdef WORDS_BIGENDIAN
171 ((art_u32 *)buf)[i] = src_rgba;
172 #else
173 ((art_u32 *)buf)[i] = src_abgr;
174 #endif
179 * art_rgba_run_alpha: Render semitransparent color over RGBA buffer.
180 * @buf: Buffer for rendering.
181 * @r: Red, range 0..255.
182 * @g: Green, range 0..255.
183 * @b: Blue, range 0..255.
184 * @alpha: Alpha, range 0..255.
185 * @n: Number of RGB triples to render.
187 * Renders a sequential run of solid (@r, @g, @b) color over @buf with
188 * opacity @alpha. Note that the range of @alpha is 0..255, in contrast
189 * to art_rgb_run_alpha, which has a range of 0..256.
191 void
192 art_rgba_run_alpha (art_u8 *buf, art_u8 r, art_u8 g, art_u8 b, int alpha, int n)
194 int i;
195 #ifdef WORDS_BIGENDIAN
196 art_u32 src_rgba, dst_rgba;
197 #else
198 art_u32 src_abgr, dst_abgr;
199 #endif
200 art_u8 dst_alpha;
201 int a;
202 int dst_r, dst_g, dst_b;
203 int tmp;
204 int c;
206 #ifdef WORDS_BIGENDIAN
207 src_rgba = (r << 24) | (g << 16) | (b << 8) | alpha;
208 #else
209 src_abgr = (alpha << 24) | (b << 16) | (g << 8) | r;
210 #endif
211 for (i = 0; i < n; i++)
213 #ifdef WORDS_BIGENDIAN
214 dst_rgba = ((art_u32 *)buf)[i];
215 dst_alpha = dst_rgba & 0xff;
216 #else
217 dst_abgr = ((art_u32 *)buf)[i];
218 dst_alpha = (dst_abgr >> 24) & 0xff;
219 #endif
220 if (dst_alpha)
222 #ifdef ART_OPTIMIZE_SPACE
223 tmp = (255 - alpha) * (255 - dst_alpha) + 0x80;
224 a = 255 - ((tmp + (tmp >> 8)) >> 8);
225 c = ((alpha << 16) + (a >> 1)) / a;
226 #else
227 tmp = art_rgba_composite_table[(alpha << 8) + dst_alpha];
228 c = tmp & 0x1ffff;
229 a = tmp >> 24;
230 #endif
231 #ifdef WORDS_BIGENDIAN
232 dst_r = (dst_rgba >> 24) & 0xff;
233 dst_g = (dst_rgba >> 16) & 0xff;
234 dst_b = (dst_rgba >> 8) & 0xff;
235 #else
236 dst_r = dst_abgr & 0xff;
237 dst_g = (dst_abgr >> 8) & 0xff;
238 dst_b = (dst_abgr >> 16) & 0xff;
239 #endif
240 dst_r += (((r - dst_r) * c + 0x8000) >> 16);
241 dst_g += (((g - dst_g) * c + 0x8000) >> 16);
242 dst_b += (((b - dst_b) * c + 0x8000) >> 16);
243 #ifdef WORDS_BIGENDIAN
244 ((art_u32 *)buf)[i] = (dst_r << 24) | (dst_g << 16) | (dst_b << 8) | a;
245 #else
246 ((art_u32 *)buf)[i] = (a << 24) | (dst_b << 16) | (dst_g << 8) | dst_r;
247 #endif
249 else
251 #ifdef WORDS_BIGENDIAN
252 ((art_u32 *)buf)[i] = src_rgba;
253 #else
254 ((art_u32 *)buf)[i] = src_abgr;
255 #endif