Fix some more pFoo names
[profile/ivi/pixman.git] / pixman / pixman-access.c
1 /*
2  *
3  * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
4  *             2005 Lars Knoll & Zack Rusin, Trolltech
5  *             2008 Aaron Plattner, NVIDIA Corporation
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of Keith Packard not be used in
12  * advertising or publicity pertaining to distribution of the software without
13  * specific, written prior permission.  Keith Packard makes no
14  * representations about the suitability of this software for any purpose.  It
15  * is provided "as is" without express or implied warranty.
16  *
17  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
18  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
19  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
22  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
23  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
24  * SOFTWARE.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
34
35 #include "pixman-private.h"
36 #include "pixman-accessor.h"
37
38 #define CONVERT_RGB24_TO_Y15(s)       (((((s) >> 16) & 0xff) * 153 + \
39                                   (((s) >>  8) & 0xff) * 301 +          \
40                                   (((s)      ) & 0xff) * 58) >> 2)
41 #define CONVERT_RGB24_TO_RGB15(s) ((((s) >> 3) & 0x001f) |  \
42                             (((s) >> 6) & 0x03e0) |  \
43                             (((s) >> 9) & 0x7c00))
44 #define RGB16_TO_ENTRY(mif,rgb15) ((mif)->ent[rgb15])
45 #define RGB24_TO_ENTRY(mif,rgb24) RGB16_TO_ENTRY(mif,CONVERT_RGB24_TO_RGB15(rgb24))
46
47 #define RGB24_TO_ENTRY_Y(mif,rgb24) ((mif)->ent[CONVERT_RGB24_TO_Y15(rgb24)])
48
49 /*
50  * YV12 setup and access macros
51  */
52
53 #define YV12_SETUP(image)                                               \
54     bits_image_t *__bits_image = (bits_image_t *)image;                 \
55     uint32_t *bits = __bits_image->bits;                                        \
56     int stride = __bits_image->rowstride;                                       \
57     int offset0 = stride < 0 ?                                          \
58         ((-stride) >> 1) * ((__bits_image->height - 1) >> 1) - stride : \
59         stride * __bits_image->height;                                  \
60     int offset1 = stride < 0 ?                                          \
61         offset0 + ((-stride) >> 1) * ((__bits_image->height) >> 1) :    \
62         offset0 + (offset0 >> 2)
63 /* Note no trailing semicolon on the above macro; if it's there, then
64  * the typical usage of YV12_SETUP(pict); will have an extra trailing ;
65  * that some compilers will interpret as a statement -- and then any further
66  * variable declarations will cause an error.
67  */
68
69 #define YV12_Y(line)            \
70     ((uint8_t *) ((bits) + (stride) * (line)))
71
72 #define YV12_U(line)          \
73     ((uint8_t *) ((bits) + offset1 + \
74                 ((stride) >> 1) * ((line) >> 1)))
75
76 #define YV12_V(line)          \
77     ((uint8_t *) ((bits) + offset0 + \
78                 ((stride) >> 1) * ((line) >> 1)))
79
80 /*********************************** Fetch ************************************/
81
82 static void
83 fetch_scanline_a8r8g8b8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
84                   const uint32_t *mask, uint32_t mask_bits)
85 {
86     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
87     MEMCPY_WRAPPED(image,
88                    buffer, (const uint32_t *)bits + x,
89                    width*sizeof(uint32_t));
90 }
91
92 static void
93 fetch_scanline_x8r8g8b8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
94                   const uint32_t *mask, uint32_t mask_bits)
95 {
96     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
97     const uint32_t *pixel = (const uint32_t *)bits + x;
98     const uint32_t *end = pixel + width;
99     while (pixel < end) {
100         *buffer++ = READ(image, pixel++) | 0xff000000;
101     }
102 }
103
104 static void
105 fetch_scanline_a8b8g8r8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
106                   const uint32_t *mask, uint32_t mask_bits)
107 {
108     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
109     const uint32_t *pixel = (uint32_t *)bits + x;
110     const uint32_t *end = pixel + width;
111     while (pixel < end) {
112         uint32_t p = READ(image, pixel++);
113         *buffer++ = (p & 0xff00ff00) |
114                     ((p >> 16) & 0xff) |
115             ((p & 0xff) << 16);
116     }
117 }
118
119 static void
120 fetch_scanline_x8b8g8r8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
121                   const uint32_t *mask, uint32_t mask_bits)
122 {
123     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
124     const uint32_t *pixel = (uint32_t *)bits + x;
125     const uint32_t *end = pixel + width;
126     while (pixel < end) {
127         uint32_t p = READ(image, pixel++);
128         *buffer++ = 0xff000000 |
129             (p & 0x0000ff00) |
130             ((p >> 16) & 0xff) |
131             ((p & 0xff) << 16);
132     }
133 }
134
135 static void
136 fetch_scanline_b8g8r8a8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
137                   const uint32_t *mask, uint32_t mask_bits)
138 {
139     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
140     const uint32_t *pixel = (uint32_t *)bits + x;
141     const uint32_t *end = pixel + width;
142     while (pixel < end) {
143         uint32_t p = READ(image, pixel++);
144         *buffer++ = ((p & 0xff000000) >> 24) |
145             ((p & 0x00ff0000) >> 8) |
146             ((p & 0x0000ff00) << 8) |
147             ((p & 0x000000ff) << 24);
148     }
149 }
150
151 static void
152 fetch_scanline_b8g8r8x8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
153                   const uint32_t *mask, uint32_t mask_bits)
154 {
155     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
156     const uint32_t *pixel = (uint32_t *)bits + x;
157     const uint32_t *end = pixel + width;
158     while (pixel < end) {
159         uint32_t p = READ(image, pixel++);
160         *buffer++ = 0xff000000 |
161             ((p & 0xff000000) >> 24) |
162             ((p & 0x00ff0000) >> 8) |
163             ((p & 0x0000ff00) << 8);
164     }
165 }
166
167 /* Expects a uint64_t buffer */
168 static void
169 fetch_scanline_a2r10g10b10 (pixman_image_t *image, int x, int y, int width, uint32_t *b,
170                      const uint32_t *mask, uint32_t mask_bits)
171 {
172     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
173     const uint32_t *pixel = bits + x;
174     const uint32_t *end = pixel + width;
175     uint64_t *buffer = (uint64_t *)b;
176     
177     while (pixel < end) {
178         uint32_t p = READ(image, pixel++);
179         uint64_t a = p >> 30;
180         uint64_t r = (p >> 20) & 0x3ff;
181         uint64_t g = (p >> 10) & 0x3ff;
182         uint64_t b = p & 0x3ff;
183
184         r = r << 6 | r >> 4;
185         g = g << 6 | g >> 4;
186         b = b << 6 | b >> 4;
187
188         a <<= 62;
189         a |= a >> 2;
190         a |= a >> 4;
191         a |= a >> 8;
192
193         *buffer++ = a << 48 | r << 32 | g << 16 | b;
194     }
195 }
196
197 /* Expects a uint64_t buffer */
198 static void
199 fetch_scanline_x2r10g10b10 (pixman_image_t *image, int x, int y, int width, uint32_t *b,
200                      const uint32_t *mask, uint32_t mask_bits)
201 {
202     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
203     const uint32_t *pixel = (uint32_t *)bits + x;
204     const uint32_t *end = pixel + width;
205     uint64_t *buffer = (uint64_t *)b;
206
207     while (pixel < end) {
208         uint32_t p = READ(image, pixel++);
209         uint64_t r = (p >> 20) & 0x3ff;
210         uint64_t g = (p >> 10) & 0x3ff;
211         uint64_t b = p & 0x3ff;
212
213         r = r << 6 | r >> 4;
214         g = g << 6 | g >> 4;
215         b = b << 6 | b >> 4;
216
217         *buffer++ = 0xffffULL << 48 | r << 32 | g << 16 | b;
218     }
219 }
220
221
222 /* Expects a uint64_t buffer */
223 static void
224 fetch_scanline_a2b10g10r10 (pixman_image_t *image, int x, int y, int width, uint32_t *b,
225                             const uint32_t *mask, uint32_t mask_bits)
226 {
227     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
228     const uint32_t *pixel = bits + x;
229     const uint32_t *end = pixel + width;
230     uint64_t *buffer = (uint64_t *)b;
231
232     while (pixel < end) {
233         uint32_t p = READ(image, pixel++);
234         uint64_t a = p >> 30;
235         uint64_t b = (p >> 20) & 0x3ff;
236         uint64_t g = (p >> 10) & 0x3ff;
237         uint64_t r = p & 0x3ff;
238
239         r = r << 6 | r >> 4;
240         g = g << 6 | g >> 4;
241         b = b << 6 | b >> 4;
242
243         a <<= 62;
244         a |= a >> 2;
245         a |= a >> 4;
246         a |= a >> 8;
247
248         *buffer++ = a << 48 | r << 32 | g << 16 | b;
249     }
250 }
251
252 /* Expects a uint64_t buffer */
253 static void
254 fetch_scanline_x2b10g10r10 (pixman_image_t *image, int x, int y, int width, uint32_t *b,
255                      const uint32_t *mask, uint32_t mask_bits)
256 {
257     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
258     const uint32_t *pixel = (uint32_t *)bits + x;
259     const uint32_t *end = pixel + width;
260     uint64_t *buffer = (uint64_t *)b;
261
262     while (pixel < end) {
263         uint32_t p = READ(image, pixel++);
264         uint64_t b = (p >> 20) & 0x3ff;
265         uint64_t g = (p >> 10) & 0x3ff;
266         uint64_t r = p & 0x3ff;
267
268         r = r << 6 | r >> 4;
269         g = g << 6 | g >> 4;
270         b = b << 6 | b >> 4;
271
272         *buffer++ = 0xffffULL << 48 | r << 32 | g << 16 | b;
273     }
274 }
275
276 static void
277 fetch_scanline_r8g8b8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
278                 const uint32_t *mask, uint32_t mask_bits)
279 {
280     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
281     const uint8_t *pixel = (const uint8_t *)bits + 3*x;
282     const uint8_t *end = pixel + 3*width;
283     while (pixel < end) {
284         uint32_t b = 0xff000000;
285 #ifdef WORDS_BIGENDIAN
286         b |= (READ(image, pixel++) << 16);
287         b |= (READ(image, pixel++) << 8);
288         b |= (READ(image, pixel++));
289 #else
290         b |= (READ(image, pixel++));
291         b |= (READ(image, pixel++) << 8);
292         b |= (READ(image, pixel++) << 16);
293 #endif
294         *buffer++ = b;
295     }
296 }
297
298 static void
299 fetch_scanline_b8g8r8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
300                 const uint32_t *mask, uint32_t mask_bits)
301 {
302     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
303     const uint8_t *pixel = (const uint8_t *)bits + 3*x;
304     const uint8_t *end = pixel + 3*width;
305     while (pixel < end) {
306         uint32_t b = 0xff000000;
307 #ifdef WORDS_BIGENDIAN
308         b |= (READ(image, pixel++));
309         b |= (READ(image, pixel++) << 8);
310         b |= (READ(image, pixel++) << 16);
311 #else
312         b |= (READ(image, pixel++) << 16);
313         b |= (READ(image, pixel++) << 8);
314         b |= (READ(image, pixel++));
315 #endif
316         *buffer++ = b;
317     }
318 }
319
320 static void
321 fetch_scanline_r5g6b5 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
322                 const uint32_t *mask, uint32_t mask_bits)
323 {
324     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
325     const uint16_t *pixel = (const uint16_t *)bits + x;
326     const uint16_t *end = pixel + width;
327     while (pixel < end) {
328         uint32_t p = READ(image, pixel++);
329         uint32_t r = (((p) << 3) & 0xf8) |
330             (((p) << 5) & 0xfc00) |
331             (((p) << 8) & 0xf80000);
332         r |= (r >> 5) & 0x70007;
333         r |= (r >> 6) & 0x300;
334         *buffer++ = 0xff000000 | r;
335     }
336 }
337
338 static void
339 fetch_scanline_b5g6r5 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
340                 const uint32_t *mask, uint32_t mask_bits)
341 {
342     uint32_t  r,g,b;
343     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
344     const uint16_t *pixel = (const uint16_t *)bits + x;
345     const uint16_t *end = pixel + width;
346     while (pixel < end) {
347         uint32_t  p = READ(image, pixel++);
348         b = ((p & 0xf800) | ((p & 0xe000) >> 5)) >> 8;
349         g = ((p & 0x07e0) | ((p & 0x0600) >> 6)) << 5;
350         r = ((p & 0x001c) | ((p & 0x001f) << 5)) << 14;
351         *buffer++ = 0xff000000 | r | g | b;
352     }
353 }
354
355 static void
356 fetch_scanline_a1r5g5b5 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
357                   const uint32_t *mask, uint32_t mask_bits)
358 {
359     uint32_t  r,g,b, a;
360     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
361     const uint16_t *pixel = (const uint16_t *)bits + x;
362     const uint16_t *end = pixel + width;
363     while (pixel < end) {
364         uint32_t  p = READ(image, pixel++);
365
366         a = (uint32_t) ((uint8_t) (0 - ((p & 0x8000) >> 15))) << 24;
367         r = ((p & 0x7c00) | ((p & 0x7000) >> 5)) << 9;
368         g = ((p & 0x03e0) | ((p & 0x0380) >> 5)) << 6;
369         b = ((p & 0x001c) | ((p & 0x001f) << 5)) >> 2;
370         *buffer++ = a | r | g | b;
371     }
372 }
373
374 static void
375 fetch_scanline_x1r5g5b5 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
376                   const uint32_t *mask, uint32_t mask_bits)
377 {
378     uint32_t  r,g,b;
379     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
380     const uint16_t *pixel = (const uint16_t *)bits + x;
381     const uint16_t *end = pixel + width;
382     while (pixel < end) {
383         uint32_t  p = READ(image, pixel++);
384
385         r = ((p & 0x7c00) | ((p & 0x7000) >> 5)) << 9;
386         g = ((p & 0x03e0) | ((p & 0x0380) >> 5)) << 6;
387         b = ((p & 0x001c) | ((p & 0x001f) << 5)) >> 2;
388         *buffer++ = 0xff000000 | r | g | b;
389     }
390 }
391
392 static void
393 fetch_scanline_a1b5g5r5 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
394                   const uint32_t *mask, uint32_t mask_bits)
395 {
396     uint32_t  r,g,b, a;
397     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
398     const uint16_t *pixel = (const uint16_t *)bits + x;
399     const uint16_t *end = pixel + width;
400     while (pixel < end) {
401         uint32_t  p = READ(image, pixel++);
402
403         a = (uint32_t) ((uint8_t) (0 - ((p & 0x8000) >> 15))) << 24;
404         b = ((p & 0x7c00) | ((p & 0x7000) >> 5)) >> 7;
405         g = ((p & 0x03e0) | ((p & 0x0380) >> 5)) << 6;
406         r = ((p & 0x001c) | ((p & 0x001f) << 5)) << 14;
407         *buffer++ = a | r | g | b;
408     }
409 }
410
411 static void
412 fetch_scanline_x1b5g5r5 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
413                   const uint32_t *mask, uint32_t mask_bits)
414 {
415     uint32_t  r,g,b;
416     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
417     const uint16_t *pixel = (const uint16_t *)bits + x;
418     const uint16_t *end = pixel + width;
419     while (pixel < end) {
420         uint32_t  p = READ(image, pixel++);
421
422         b = ((p & 0x7c00) | ((p & 0x7000) >> 5)) >> 7;
423         g = ((p & 0x03e0) | ((p & 0x0380) >> 5)) << 6;
424         r = ((p & 0x001c) | ((p & 0x001f) << 5)) << 14;
425         *buffer++ = 0xff000000 | r | g | b;
426     }
427 }
428
429 static void
430 fetch_scanline_a4r4g4b4 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
431                   const uint32_t *mask, uint32_t mask_bits)
432 {
433     uint32_t  r,g,b, a;
434     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
435     const uint16_t *pixel = (const uint16_t *)bits + x;
436     const uint16_t *end = pixel + width;
437     while (pixel < end) {
438         uint32_t  p = READ(image, pixel++);
439
440         a = ((p & 0xf000) | ((p & 0xf000) >> 4)) << 16;
441         r = ((p & 0x0f00) | ((p & 0x0f00) >> 4)) << 12;
442         g = ((p & 0x00f0) | ((p & 0x00f0) >> 4)) << 8;
443         b = ((p & 0x000f) | ((p & 0x000f) << 4));
444         *buffer++ = a | r | g | b;
445     }
446 }
447
448 static void
449 fetch_scanline_x4r4g4b4 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
450                   const uint32_t *mask, uint32_t mask_bits)
451 {
452     uint32_t  r,g,b;
453     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
454     const uint16_t *pixel = (const uint16_t *)bits + x;
455     const uint16_t *end = pixel + width;
456     while (pixel < end) {
457         uint32_t  p = READ(image, pixel++);
458
459         r = ((p & 0x0f00) | ((p & 0x0f00) >> 4)) << 12;
460         g = ((p & 0x00f0) | ((p & 0x00f0) >> 4)) << 8;
461         b = ((p & 0x000f) | ((p & 0x000f) << 4));
462         *buffer++ = 0xff000000 | r | g | b;
463     }
464 }
465
466 static void
467 fetch_scanline_a4b4g4r4 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
468                   const uint32_t *mask, uint32_t mask_bits)
469 {
470     uint32_t  r,g,b, a;
471     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
472     const uint16_t *pixel = (const uint16_t *)bits + x;
473     const uint16_t *end = pixel + width;
474     while (pixel < end) {
475         uint32_t  p = READ(image, pixel++);
476
477         a = ((p & 0xf000) | ((p & 0xf000) >> 4)) << 16;
478         b = ((p & 0x0f00) | ((p & 0x0f00) >> 4)) >> 4;
479         g = ((p & 0x00f0) | ((p & 0x00f0) >> 4)) << 8;
480         r = ((p & 0x000f) | ((p & 0x000f) << 4)) << 16;
481         *buffer++ = a | r | g | b;
482     }
483 }
484
485 static void
486 fetch_scanline_x4b4g4r4 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
487                   const uint32_t *mask, uint32_t mask_bits)
488 {
489     uint32_t  r,g,b;
490     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
491     const uint16_t *pixel = (const uint16_t *)bits + x;
492     const uint16_t *end = pixel + width;
493     while (pixel < end) {
494         uint32_t  p = READ(image, pixel++);
495
496         b = ((p & 0x0f00) | ((p & 0x0f00) >> 4)) >> 4;
497         g = ((p & 0x00f0) | ((p & 0x00f0) >> 4)) << 8;
498         r = ((p & 0x000f) | ((p & 0x000f) << 4)) << 16;
499         *buffer++ = 0xff000000 | r | g | b;
500     }
501 }
502
503 static void
504 fetch_scanline_a8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
505             const uint32_t *mask, uint32_t mask_bits)
506 {
507     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
508     const uint8_t *pixel = (const uint8_t *)bits + x;
509     const uint8_t *end = pixel + width;
510     while (pixel < end) {
511         *buffer++ = READ(image, pixel++) << 24;
512     }
513 }
514
515 static void
516 fetch_scanline_r3g3b2 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
517                 const uint32_t *mask, uint32_t mask_bits)
518 {
519     uint32_t  r,g,b;
520     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
521     const uint8_t *pixel = (const uint8_t *)bits + x;
522     const uint8_t *end = pixel + width;
523     while (pixel < end) {
524         uint32_t  p = READ(image, pixel++);
525
526         r = ((p & 0xe0) | ((p & 0xe0) >> 3) | ((p & 0xc0) >> 6)) << 16;
527         g = ((p & 0x1c) | ((p & 0x18) >> 3) | ((p & 0x1c) << 3)) << 8;
528         b = (((p & 0x03)     ) |
529              ((p & 0x03) << 2) |
530              ((p & 0x03) << 4) |
531              ((p & 0x03) << 6));
532         *buffer++ = 0xff000000 | r | g | b;
533     }
534 }
535
536 static void
537 fetch_scanline_b2g3r3 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
538                 const uint32_t *mask, uint32_t mask_bits)
539 {
540     uint32_t  r,g,b;
541     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
542     const uint8_t *pixel = (const uint8_t *)bits + x;
543     const uint8_t *end = pixel + width;
544     while (pixel < end) {
545         uint32_t  p = READ(image, pixel++);
546
547         b = (((p & 0xc0)     ) |
548              ((p & 0xc0) >> 2) |
549              ((p & 0xc0) >> 4) |
550              ((p & 0xc0) >> 6));
551         g = ((p & 0x38) | ((p & 0x38) >> 3) | ((p & 0x30) << 2)) << 8;
552         r = (((p & 0x07)     ) |
553              ((p & 0x07) << 3) |
554              ((p & 0x06) << 6)) << 16;
555         *buffer++ = 0xff000000 | r | g | b;
556     }
557 }
558
559 static void
560 fetch_scanline_a2r2g2b2 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
561                   const uint32_t *mask, uint32_t mask_bits)
562 {
563     uint32_t   a,r,g,b;
564     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
565     const uint8_t *pixel = (const uint8_t *)bits + x;
566     const uint8_t *end = pixel + width;
567     while (pixel < end) {
568         uint32_t  p = READ(image, pixel++);
569
570         a = ((p & 0xc0) * 0x55) << 18;
571         r = ((p & 0x30) * 0x55) << 12;
572         g = ((p & 0x0c) * 0x55) << 6;
573         b = ((p & 0x03) * 0x55);
574         *buffer++ = a|r|g|b;
575     }
576 }
577
578 static void
579 fetch_scanline_a2b2g2r2 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
580                   const uint32_t *mask, uint32_t mask_bits)
581 {
582     uint32_t   a,r,g,b;
583     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
584     const uint8_t *pixel = (const uint8_t *)bits + x;
585     const uint8_t *end = pixel + width;
586     while (pixel < end) {
587         uint32_t  p = READ(image, pixel++);
588
589         a = ((p & 0xc0) * 0x55) << 18;
590         b = ((p & 0x30) * 0x55) >> 6;
591         g = ((p & 0x0c) * 0x55) << 6;
592         r = ((p & 0x03) * 0x55) << 16;
593         *buffer++ = a|r|g|b;
594     }
595 }
596
597 static void
598 fetch_scanline_c8 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
599             const uint32_t *mask, uint32_t mask_bits)
600 {
601     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
602     const pixman_indexed_t * indexed = image->bits.indexed;
603     const uint8_t *pixel = (const uint8_t *)bits + x;
604     const uint8_t *end = pixel + width;
605     while (pixel < end) {
606         uint32_t  p = READ(image, pixel++);
607         *buffer++ = indexed->rgba[p];
608     }
609 }
610
611 static void
612 fetch_scanline_x4a4 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
613               const uint32_t *mask, uint32_t mask_bits)
614 {
615     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
616     const uint8_t *pixel = (const uint8_t *)bits + x;
617     const uint8_t *end = pixel + width;
618     while (pixel < end) {
619         uint8_t p = READ(image, pixel++) & 0xf;
620         *buffer++ = (p | (p << 4)) << 24;
621     }
622 }
623
624 #define FETCH_8(img,l,o)    (READ(img, (uint8_t *)(l) + ((o) >> 2)))
625 #ifdef WORDS_BIGENDIAN
626 #define FETCH_4(img,l,o)    ((o) & 2 ? FETCH_8(img,l,o) & 0xf : FETCH_8(img,l,o) >> 4)
627 #else
628 #define FETCH_4(img,l,o)    ((o) & 2 ? FETCH_8(img,l,o) >> 4 : FETCH_8(img,l,o) & 0xf)
629 #endif
630
631 static void
632 fetch_scanline_a4 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
633             const uint32_t *mask, uint32_t mask_bits)
634 {
635     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
636     int i;
637     for (i = 0; i < width; ++i) {
638         uint32_t  p = FETCH_4(image, bits, i + x);
639
640         p |= p << 4;
641         *buffer++ = p << 24;
642     }
643 }
644
645 static void
646 fetch_scanline_r1g2b1 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
647                 const uint32_t *mask, uint32_t mask_bits)
648 {
649     uint32_t  r,g,b;
650     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
651     int i;
652     for (i = 0; i < width; ++i) {
653         uint32_t  p = FETCH_4(image, bits, i + x);
654
655         r = ((p & 0x8) * 0xff) << 13;
656         g = ((p & 0x6) * 0x55) << 7;
657         b = ((p & 0x1) * 0xff);
658         *buffer++ = 0xff000000|r|g|b;
659     }
660 }
661
662 static void
663 fetch_scanline_b1g2r1 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
664                 const uint32_t *mask, uint32_t mask_bits)
665 {
666     uint32_t  r,g,b;
667     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
668     int i;
669     for (i = 0; i < width; ++i) {
670         uint32_t  p = FETCH_4(image, bits, i + x);
671
672         b = ((p & 0x8) * 0xff) >> 3;
673         g = ((p & 0x6) * 0x55) << 7;
674         r = ((p & 0x1) * 0xff) << 16;
675         *buffer++ = 0xff000000|r|g|b;
676     }
677 }
678
679 static void
680 fetch_scanline_a1r1g1b1 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
681                   const uint32_t *mask, uint32_t mask_bits)
682 {
683     uint32_t  a,r,g,b;
684     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
685     int i;
686     for (i = 0; i < width; ++i) {
687         uint32_t  p = FETCH_4(image, bits, i + x);
688
689         a = ((p & 0x8) * 0xff) << 21;
690         r = ((p & 0x4) * 0xff) << 14;
691         g = ((p & 0x2) * 0xff) << 7;
692         b = ((p & 0x1) * 0xff);
693         *buffer++ = a|r|g|b;
694     }
695 }
696
697 static void
698 fetch_scanline_a1b1g1r1 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
699                   const uint32_t *mask, uint32_t mask_bits)
700 {
701     uint32_t  a,r,g,b;
702     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
703     int i;
704     for (i = 0; i < width; ++i) {
705         uint32_t  p = FETCH_4(image, bits, i + x);
706
707         a = ((p & 0x8) * 0xff) << 21;
708         r = ((p & 0x4) * 0xff) >> 3;
709         g = ((p & 0x2) * 0xff) << 7;
710         b = ((p & 0x1) * 0xff) << 16;
711         *buffer++ = a|r|g|b;
712     }
713 }
714
715 static void
716 fetch_scanline_c4 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
717             const uint32_t *mask, uint32_t mask_bits)
718 {
719     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
720     const pixman_indexed_t * indexed = image->bits.indexed;
721     int i;
722     for (i = 0; i < width; ++i) {
723         uint32_t  p = FETCH_4 (image, bits, i + x);
724
725         *buffer++ = indexed->rgba[p];
726     }
727 }
728
729
730 static void
731 fetch_scanline_a1 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
732             const uint32_t *mask, uint32_t mask_bits)
733 {
734     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
735     int i;
736     for (i = 0; i < width; ++i) {
737         uint32_t  p = READ(image, bits + ((i + x) >> 5));
738         uint32_t  a;
739 #ifdef WORDS_BIGENDIAN
740         a = p >> (0x1f - ((i+x) & 0x1f));
741 #else
742         a = p >> ((i+x) & 0x1f);
743 #endif
744         a = a & 1;
745         a |= a << 1;
746         a |= a << 2;
747         a |= a << 4;
748         *buffer++ = a << 24;
749     }
750 }
751
752 static void
753 fetch_scanline_g1 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
754             const uint32_t *mask, uint32_t mask_bits)
755 {
756     const uint32_t *bits = image->bits.bits + y*image->bits.rowstride;
757     const pixman_indexed_t * indexed = image->bits.indexed;
758     int i;
759     for (i = 0; i < width; ++i) {
760         uint32_t p = READ(image, bits + ((i+x) >> 5));
761         uint32_t a;
762 #ifdef WORDS_BIGENDIAN
763         a = p >> (0x1f - ((i+x) & 0x1f));
764 #else
765         a = p >> ((i+x) & 0x1f);
766 #endif
767         a = a & 1;
768         *buffer++ = indexed->rgba[a];
769     }
770 }
771
772 static void
773 fetch_scanline_yuy2 (pixman_image_t *image, int x, int line, int width, uint32_t *buffer,
774               const uint32_t *mask, uint32_t mask_bits)
775 {
776     int16_t y, u, v;
777     int32_t r, g, b;
778     int   i;
779
780     const uint32_t *bits = image->bits.bits + image->bits.rowstride * line;
781
782     for (i = 0; i < width; i++)
783     {
784         y = ((uint8_t *) bits)[(x + i) << 1] - 16;
785         u = ((uint8_t *) bits)[(((x + i) << 1) & -4) + 1] - 128;
786         v = ((uint8_t *) bits)[(((x + i) << 1) & -4) + 3] - 128;
787
788         /* R = 1.164(Y - 16) + 1.596(V - 128) */
789         r = 0x012b27 * y + 0x019a2e * v;
790         /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
791         g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
792         /* B = 1.164(Y - 16) + 2.018(U - 128) */
793         b = 0x012b27 * y + 0x0206a2 * u;
794
795         *buffer++ = 0xff000000 |
796             (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
797             (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
798             (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
799     }
800 }
801
802 static void
803 fetch_scanline_yv12 (pixman_image_t *image, int x, int line, int width, uint32_t *buffer,
804               const uint32_t *mask, uint32_t mask_bits)
805 {
806     YV12_SETUP(image);
807     uint8_t *y_line = YV12_Y (line);
808     uint8_t *u_line = YV12_U (line);
809     uint8_t *v_line = YV12_V (line);
810     int16_t y, u, v;
811     int32_t r, g, b;
812     int   i;
813
814     for (i = 0; i < width; i++)
815     {
816         y = y_line[x + i] - 16;
817         u = u_line[(x + i) >> 1] - 128;
818         v = v_line[(x + i) >> 1] - 128;
819
820         /* R = 1.164(Y - 16) + 1.596(V - 128) */
821         r = 0x012b27 * y + 0x019a2e * v;
822         /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
823         g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
824         /* B = 1.164(Y - 16) + 2.018(U - 128) */
825         b = 0x012b27 * y + 0x0206a2 * u;
826
827         *buffer++ = 0xff000000 |
828             (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
829             (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
830             (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
831     }
832 }
833
834 /**************************** Pixel wise fetching *****************************/
835
836 /* Despite the type, expects a uint64_t buffer */
837 static void
838 fetch_pixels_a2r10g10b10_64 (bits_image_t *pict, uint32_t *b, int n_pixels)
839 {
840     int i;
841     uint64_t *buffer = (uint64_t *)b;
842
843     for (i = 0; i < n_pixels; ++i)
844     {
845         int offset = ((uint32_t *)buffer)[2 * i];
846         int line = ((uint32_t *)buffer)[2 * i + 1];
847
848         if (offset == 0xffffffff || line == 0xffffffff)
849         {
850             buffer[i] = 0;
851         }
852         else
853         {
854             uint32_t *bits = pict->bits + line*pict->rowstride;
855             uint32_t p = READ(pict, bits + offset);
856             uint64_t a = p >> 30;
857             uint64_t r = (p >> 20) & 0x3ff;
858             uint64_t g = (p >> 10) & 0x3ff;
859             uint64_t b = p & 0x3ff;
860
861             r = r << 6 | r >> 4;
862             g = g << 6 | g >> 4;
863             b = b << 6 | b >> 4;
864
865             a <<= 62;
866             a |= a >> 2;
867             a |= a >> 4;
868             a |= a >> 8;
869
870             buffer[i] = a << 48 | r << 32 | g << 16 | b;
871         }
872     }
873 }
874
875 /* Despite the type, this function expects a uint64_t buffer */
876 static void
877 fetch_pixels_x2r10g10b10_64 (bits_image_t *pict, uint32_t *b, int n_pixels)
878 {
879     uint64_t *buffer = (uint64_t *)b;
880     int i;
881
882     for (i = 0; i < n_pixels; ++i)
883     {
884         int offset = ((uint32_t *)buffer)[2 * i];
885         int line = ((uint32_t *)buffer)[2 * i + 1];
886
887         if (offset == 0xffffffff || line == 0xffffffff)
888         {
889             buffer[i] = 0;
890         }
891         else
892         {
893             uint32_t *bits = pict->bits + line*pict->rowstride;
894             uint32_t p = READ(pict, bits + offset);
895             uint64_t r = (p >> 20) & 0x3ff;
896             uint64_t g = (p >> 10) & 0x3ff;
897             uint64_t b = p & 0x3ff;
898
899             r = r << 6 | r >> 4;
900             g = g << 6 | g >> 4;
901             b = b << 6 | b >> 4;
902
903             buffer[i] = 0xffffULL << 48 | r << 32 | g << 16 | b;
904         }
905     }
906 }
907
908 /* Despite the type, expects a uint64_t buffer */
909 static void
910 fetch_pixels_a2b10g10r10_64 (bits_image_t *pict, uint32_t *b, int n_pixels)
911 {
912     int i;
913     uint64_t *buffer = (uint64_t *)b;
914
915     for (i = 0; i < n_pixels; ++i)
916     {
917         int offset = ((uint32_t *)buffer)[2 * i];
918         int line = ((uint32_t *)buffer)[2 * i + 1];
919
920         if (offset == 0xffffffff || line == 0xffffffff)
921         {
922             buffer[i] = 0;
923         }
924         else
925         {
926             uint32_t *bits = pict->bits + line*pict->rowstride;
927             uint32_t p = READ(pict, bits + offset);
928             uint64_t a = p >> 30;
929             uint64_t b = (p >> 20) & 0x3ff;
930             uint64_t g = (p >> 10) & 0x3ff;
931             uint64_t r = p & 0x3ff;
932             
933             r = r << 6 | r >> 4;
934             g = g << 6 | g >> 4;
935             b = b << 6 | b >> 4;
936             
937             a <<= 62;
938             a |= a >> 2;
939             a |= a >> 4;
940             a |= a >> 8;
941
942             buffer[i] = a << 48 | r << 32 | g << 16 | b;
943         }
944     }
945 }
946
947 /* Despite the type, this function expects a uint64_t buffer */
948 static void
949 fetch_pixels_x2b10g10r10_64 (bits_image_t *pict, uint32_t *b, int n_pixels)
950 {
951     uint64_t *buffer = (uint64_t *)b;
952     int i;
953
954     for (i = 0; i < n_pixels; ++i)
955     {
956         int offset = ((uint32_t *)buffer)[2 * i];
957         int line = ((uint32_t *)buffer)[2 * i + 1];
958
959         if (offset == 0xffffffff || line == 0xffffffff)
960         {
961             buffer[i] = 0;
962         }
963         else
964         {
965             uint32_t *bits = pict->bits + line*pict->rowstride;
966             uint32_t p = READ(pict, bits + offset);
967             uint64_t b = (p >> 20) & 0x3ff;
968             uint64_t g = (p >> 10) & 0x3ff;
969             uint64_t r = p & 0x3ff;
970             
971             r = r << 6 | r >> 4;
972             g = g << 6 | g >> 4;
973             b = b << 6 | b >> 4;
974
975             buffer[i] = 0xffffULL << 48 | r << 32 | g << 16 | b;
976         }
977     }
978 }
979
980 static void
981 fetch_pixels_a8r8g8b8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
982 {
983     int i;
984     
985     for (i = 0; i < n_pixels; ++i)
986     {
987         int offset = buffer[2 * i];
988         int line = buffer[2 * i + 1];
989         
990         if (offset == 0xffffffff || line == 0xffffffff)
991         {
992             buffer[i] = 0;
993         }
994         else
995         {
996             uint32_t *bits = pict->bits + line*pict->rowstride;
997             buffer[i] = READ(pict, (uint32_t *)bits + offset);
998         }
999     }
1000 }
1001
1002 static void
1003 fetch_pixels_x8r8g8b8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1004 {
1005     int i;
1006     
1007     for (i = 0; i < n_pixels; ++i)
1008     {
1009         int offset = buffer[2 * i];
1010         int line = buffer[2 * i + 1];
1011         
1012         if (offset == 0xffffffff || line == 0xffffffff)
1013         {
1014             buffer[i] = 0;
1015         }
1016         else
1017         {
1018             uint32_t *bits = pict->bits + line*pict->rowstride;
1019             buffer[i] = READ(pict, (uint32_t *)bits + offset) | 0xff000000;
1020         }
1021     }
1022 }
1023
1024 static void
1025 fetch_pixels_a8b8g8r8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1026 {
1027     int i;
1028     
1029     for (i = 0; i < n_pixels; ++i)
1030     {
1031         int offset = buffer[2 * i];
1032         int line = buffer[2 * i + 1];
1033         
1034         if (offset == 0xffffffff || line == 0xffffffff)
1035         {
1036             buffer[i] = 0;
1037         }
1038         else
1039         {
1040             uint32_t *bits = pict->bits + line*pict->rowstride;
1041             uint32_t  pixel = READ(pict, (uint32_t *)bits + offset);
1042             
1043             buffer[i] = ((pixel & 0xff000000) |
1044                          ((pixel >> 16) & 0xff) |
1045                          (pixel & 0x0000ff00) |
1046                          ((pixel & 0xff) << 16));
1047         }
1048     }
1049 }
1050
1051 static void
1052 fetch_pixels_x8b8g8r8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1053 {
1054     int i;
1055     
1056     for (i = 0; i < n_pixels; ++i)
1057     {
1058         int offset = buffer[2 * i];
1059         int line = buffer[2 * i + 1];
1060         
1061         if (offset == 0xffffffff || line == 0xffffffff)
1062         {
1063             buffer[i] = 0;
1064         }
1065         else
1066         {
1067             uint32_t *bits = pict->bits + line*pict->rowstride;
1068             uint32_t  pixel = READ(pict, (uint32_t *)bits + offset);
1069             
1070             buffer[i] = ((0xff000000) |
1071                          ((pixel >> 16) & 0xff) |
1072                          (pixel & 0x0000ff00) |
1073                          ((pixel & 0xff) << 16));
1074         }
1075     }
1076 }
1077
1078 static void
1079 fetch_pixels_b8g8r8a8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1080 {
1081     int i;
1082
1083     for (i = 0; i < n_pixels; ++i)
1084     {
1085         int offset = buffer[2 * i];
1086         int line = buffer[2 * i + 1];
1087
1088         if (offset == 0xffffffff || line == 0xffffffff)
1089         {
1090             buffer[i] = 0;
1091         }
1092         else
1093         {
1094             uint32_t *bits = pict->bits + line*pict->rowstride;
1095             uint32_t  pixel = READ(pict, (uint32_t *)bits + offset);
1096             
1097             buffer[i] = ((pixel & 0xff000000) >> 24 |
1098                          (pixel & 0x00ff0000) >> 8 |
1099                          (pixel & 0x0000ff00) << 8 |
1100                          (pixel & 0x000000ff) << 24);
1101         }
1102     }
1103 }
1104
1105 static void
1106 fetch_pixels_b8g8r8x8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1107 {
1108     int i;
1109
1110     for (i = 0; i < n_pixels; ++i)
1111     {
1112         int offset = buffer[2 * i];
1113         int line = buffer[2 * i + 1];
1114         
1115         if (offset == 0xffffffff || line == 0xffffffff)
1116         {
1117             buffer[i] = 0;
1118         }
1119         else
1120         {
1121             uint32_t *bits = pict->bits + line*pict->rowstride;
1122             uint32_t  pixel = READ(pict, (uint32_t *)bits + offset);
1123             
1124             buffer[i] = ((0xff000000) |
1125                          (pixel & 0xff000000) >> 24 |
1126                          (pixel & 0x00ff0000) >> 8 |
1127                          (pixel & 0x0000ff00) << 8);
1128         }
1129     }
1130 }
1131
1132 static void
1133 fetch_pixels_r8g8b8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1134 {
1135     int i;
1136
1137     for (i = 0; i < n_pixels; ++i)
1138     {
1139         int offset = buffer[2 * i];
1140         int line = buffer[2 * i + 1];
1141
1142         if (offset == 0xffffffff || line == 0xffffffff)
1143         {
1144             buffer[i] = 0;
1145         }
1146         else
1147         {
1148             uint32_t *bits = pict->bits + line*pict->rowstride;
1149             uint8_t   *pixel = ((uint8_t *) bits) + (offset*3);
1150 #ifdef WORDS_BIGENDIAN
1151             buffer[i] = (0xff000000 |
1152                          (READ(pict, pixel + 0) << 16) |
1153                          (READ(pict, pixel + 1) << 8) |
1154                          (READ(pict, pixel + 2)));
1155 #else
1156             buffer[i] = (0xff000000 |
1157                          (READ(pict, pixel + 2) << 16) |
1158                          (READ(pict, pixel + 1) << 8) |
1159                          (READ(pict, pixel + 0)));
1160 #endif
1161         }
1162     }
1163 }
1164
1165 static void
1166 fetch_pixels_b8g8r8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1167 {
1168     int i;
1169
1170     for (i = 0; i < n_pixels; ++i)
1171     {
1172         int offset = buffer[2 * i];
1173         int line = buffer[2 * i + 1];
1174
1175         if (offset == 0xffffffff || line == 0xffffffff)
1176         {
1177             buffer[i] = 0;
1178         }
1179         else
1180         {
1181             uint32_t *bits = pict->bits + line*pict->rowstride;
1182             uint8_t   *pixel = ((uint8_t *) bits) + (offset*3);
1183 #ifdef WORDS_BIGENDIAN
1184             buffer[i] = (0xff000000 |
1185                          (READ(pict, pixel + 2) << 16) |
1186                          (READ(pict, pixel + 1) << 8) |
1187                          (READ(pict, pixel + 0)));
1188 #else
1189             buffer[i] = (0xff000000 |
1190                          (READ(pict, pixel + 0) << 16) |
1191                          (READ(pict, pixel + 1) << 8) |
1192                          (READ(pict, pixel + 2)));
1193 #endif
1194         }
1195     }
1196 }
1197
1198 static void
1199 fetch_pixels_r5g6b5 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1200 {
1201     int i;
1202
1203     for (i = 0; i < n_pixels; ++i)
1204     {
1205         int offset = buffer[2 * i];
1206         int line = buffer[2 * i + 1];
1207
1208         if (offset == 0xffffffff || line == 0xffffffff)
1209         {
1210             buffer[i] = 0;
1211         }
1212         else
1213         {
1214             uint32_t  r,g,b;
1215             uint32_t *bits = pict->bits + line*pict->rowstride;
1216             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1217             
1218             r = ((pixel & 0xf800) | ((pixel & 0xe000) >> 5)) << 8;
1219             g = ((pixel & 0x07e0) | ((pixel & 0x0600) >> 6)) << 5;
1220             b = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) >> 2;
1221             buffer[i] = (0xff000000 | r | g | b);
1222         }
1223     }
1224 }
1225
1226 static void
1227 fetch_pixels_b5g6r5 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1228 {
1229     int i;
1230
1231     for (i = 0; i < n_pixels; ++i)
1232     {
1233         int offset = buffer[2 * i];
1234         int line = buffer[2 * i + 1];
1235
1236         if (offset == 0xffffffff || line == 0xffffffff)
1237         {
1238             buffer[i] = 0;
1239         }
1240         else
1241         {
1242             uint32_t  r,g,b;
1243             uint32_t *bits = pict->bits + line*pict->rowstride;
1244             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1245             
1246             b = ((pixel & 0xf800) | ((pixel & 0xe000) >> 5)) >> 8;
1247             g = ((pixel & 0x07e0) | ((pixel & 0x0600) >> 6)) << 5;
1248             r = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) << 14;
1249             buffer[i] = (0xff000000 | r | g | b);
1250         }
1251     }
1252 }
1253
1254 static void
1255 fetch_pixels_a1r5g5b5 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1256 {
1257     int i;
1258
1259     for (i = 0; i < n_pixels; ++i)
1260     {
1261         int offset = buffer[2 * i];
1262         int line = buffer[2 * i + 1];
1263
1264         if (offset == 0xffffffff || line == 0xffffffff)
1265         {
1266             buffer[i] = 0;
1267         }
1268         else
1269         {
1270             uint32_t  a,r,g,b;
1271             uint32_t *bits = pict->bits + line*pict->rowstride;
1272             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1273             
1274             a = (uint32_t) ((uint8_t) (0 - ((pixel & 0x8000) >> 15))) << 24;
1275             r = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) << 9;
1276             g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
1277             b = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) >> 2;
1278             buffer[i] = (a | r | g | b);
1279         }
1280     }
1281 }
1282
1283 static void
1284 fetch_pixels_x1r5g5b5 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1285 {
1286     int i;
1287
1288     for (i = 0; i < n_pixels; ++i)
1289     {
1290         int offset = buffer[2 * i];
1291         int line = buffer[2 * i + 1];
1292
1293         if (offset == 0xffffffff || line == 0xffffffff)
1294         {
1295             buffer[i] = 0;
1296         }
1297         else
1298         {
1299             uint32_t  r,g,b;
1300             uint32_t *bits = pict->bits + line*pict->rowstride;
1301             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1302             
1303             r = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) << 9;
1304             g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
1305             b = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) >> 2;
1306             buffer[i] = (0xff000000 | r | g | b);
1307         }
1308     }
1309 }
1310
1311 static void
1312 fetch_pixels_a1b5g5r5 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1313 {
1314     int i;
1315
1316     for (i = 0; i < n_pixels; ++i)
1317     {
1318         int offset = buffer[2 * i];
1319         int line = buffer[2 * i + 1];
1320
1321         if (offset == 0xffffffff || line == 0xffffffff)
1322         {
1323             buffer[i] = 0;
1324         }
1325         else
1326         {
1327             uint32_t  a,r,g,b;
1328             uint32_t *bits = pict->bits + line*pict->rowstride;
1329             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1330             
1331             a = (uint32_t) ((uint8_t) (0 - ((pixel & 0x8000) >> 15))) << 24;
1332             b = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) >> 7;
1333             g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
1334             r = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) << 14;
1335             buffer[i] = (a | r | g | b);
1336         }
1337     }
1338 }
1339
1340 static void
1341 fetch_pixels_x1b5g5r5 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1342 {
1343     int i;
1344
1345     for (i = 0; i < n_pixels; ++i)
1346     {
1347         int offset = buffer[2 * i];
1348         int line = buffer[2 * i + 1];
1349
1350         if (offset == 0xffffffff || line == 0xffffffff)
1351         {
1352             buffer[i] = 0;
1353         }
1354         else
1355         {
1356             uint32_t  r,g,b;
1357             uint32_t *bits = pict->bits + line*pict->rowstride;
1358             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1359             
1360             b = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) >> 7;
1361             g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
1362             r = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) << 14;
1363             buffer[i] = (0xff000000 | r | g | b);
1364         }
1365     }
1366 }
1367
1368 static void
1369 fetch_pixels_a4r4g4b4 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1370 {
1371     int i;
1372
1373     for (i = 0; i < n_pixels; ++i)
1374     {
1375         int offset = buffer[2 * i];
1376         int line = buffer[2 * i + 1];
1377
1378         if (offset == 0xffffffff || line == 0xffffffff)
1379         {
1380             buffer[i] = 0;
1381         }
1382         else
1383         {
1384             uint32_t  a,r,g,b;
1385             uint32_t *bits = pict->bits + line*pict->rowstride;
1386             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1387             
1388             a = ((pixel & 0xf000) | ((pixel & 0xf000) >> 4)) << 16;
1389             r = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) << 12;
1390             g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
1391             b = ((pixel & 0x000f) | ((pixel & 0x000f) << 4));
1392             buffer[i] = (a | r | g | b);
1393         }
1394     }
1395 }
1396
1397 static void
1398 fetch_pixels_x4r4g4b4 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1399 {
1400     int i;
1401
1402     for (i = 0; i < n_pixels; ++i)
1403     {
1404         int offset = buffer[2 * i];
1405         int line = buffer[2 * i + 1];
1406
1407         if (offset == 0xffffffff || line == 0xffffffff)
1408         {
1409             buffer[i] = 0;
1410         }
1411         else
1412         {
1413             uint32_t  r,g,b;
1414             uint32_t *bits = pict->bits + line*pict->rowstride;
1415             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1416             
1417             r = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) << 12;
1418             g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
1419             b = ((pixel & 0x000f) | ((pixel & 0x000f) << 4));
1420             buffer[i] = (0xff000000 | r | g | b);
1421         }
1422     }
1423 }
1424
1425 static void
1426 fetch_pixels_a4b4g4r4 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1427 {
1428     int i;
1429
1430     for (i = 0; i < n_pixels; ++i)
1431     {
1432         int offset = buffer[2 * i];
1433         int line = buffer[2 * i + 1];
1434
1435         if (offset == 0xffffffff || line == 0xffffffff)
1436         {
1437             buffer[i] = 0;
1438         }
1439         else
1440         {
1441             uint32_t  a,r,g,b;
1442             uint32_t *bits = pict->bits + line*pict->rowstride;
1443             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1444             
1445             a = ((pixel & 0xf000) | ((pixel & 0xf000) >> 4)) << 16;
1446             b = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) >> 4;
1447             g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
1448             r = ((pixel & 0x000f) | ((pixel & 0x000f) << 4)) << 16;
1449             buffer[i] = (a | r | g | b);
1450         }
1451     }
1452 }
1453
1454 static void
1455 fetch_pixels_x4b4g4r4 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1456 {
1457     int i;
1458
1459     for (i = 0; i < n_pixels; ++i)
1460     {
1461         int offset = buffer[2 * i];
1462         int line = buffer[2 * i + 1];
1463
1464         if (offset == 0xffffffff || line == 0xffffffff)
1465         {
1466             buffer[i] = 0;
1467         }
1468         else
1469         {
1470             uint32_t  r,g,b;
1471             uint32_t *bits = pict->bits + line*pict->rowstride;
1472             uint32_t  pixel = READ(pict, (uint16_t *) bits + offset);
1473             
1474             b = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) >> 4;
1475             g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
1476             r = ((pixel & 0x000f) | ((pixel & 0x000f) << 4)) << 16;
1477             buffer[i] = (0xff000000 | r | g | b);
1478         }
1479     }
1480 }
1481
1482 static void
1483 fetch_pixels_a8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1484 {
1485     int i;
1486
1487     for (i = 0; i < n_pixels; ++i)
1488     {
1489         int offset = buffer[2 * i];
1490         int line = buffer[2 * i + 1];
1491
1492         if (offset == 0xffffffff || line == 0xffffffff)
1493         {
1494             buffer[i] = 0;
1495         }
1496         else
1497         {
1498             uint32_t *bits = pict->bits + line*pict->rowstride;
1499             uint32_t   pixel = READ(pict, (uint8_t *) bits + offset);
1500             
1501             buffer[i] = pixel << 24;
1502         }
1503     }
1504 }
1505
1506 static void
1507 fetch_pixels_r3g3b2 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1508 {
1509     int i;
1510
1511     for (i = 0; i < n_pixels; ++i)
1512     {
1513         int offset = buffer[2 * i];
1514         int line = buffer[2 * i + 1];
1515
1516         if (offset == 0xffffffff || line == 0xffffffff)
1517         {
1518             buffer[i] = 0;
1519         }
1520         else
1521         {
1522             uint32_t  r,g,b;
1523             uint32_t *bits = pict->bits + line*pict->rowstride;
1524             uint32_t   pixel = READ(pict, (uint8_t *) bits + offset);
1525             
1526             r = ((pixel & 0xe0) | ((pixel & 0xe0) >> 3) | ((pixel & 0xc0) >> 6)) << 16;
1527             g = ((pixel & 0x1c) | ((pixel & 0x18) >> 3) | ((pixel & 0x1c) << 3)) << 8;
1528             b = (((pixel & 0x03)     ) |
1529                  ((pixel & 0x03) << 2) |
1530                  ((pixel & 0x03) << 4) |
1531                  ((pixel & 0x03) << 6));
1532             buffer[i] = (0xff000000 | r | g | b);
1533         }
1534     }
1535 }
1536
1537 static void
1538 fetch_pixels_b2g3r3 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1539 {
1540     int i;
1541
1542     for (i = 0; i < n_pixels; ++i)
1543     {
1544         int offset = buffer[2 * i];
1545         int line = buffer[2 * i + 1];
1546
1547         if (offset == 0xffffffff || line == 0xffffffff)
1548         {
1549             buffer[i] = 0;
1550         }
1551         else
1552         {
1553             uint32_t  r,g,b;
1554             uint32_t *bits = pict->bits + line*pict->rowstride;
1555             uint32_t   pixel = READ(pict, (uint8_t *) bits + offset);
1556             
1557             b = (((pixel & 0xc0)     ) |
1558                  ((pixel & 0xc0) >> 2) |
1559                  ((pixel & 0xc0) >> 4) |
1560                  ((pixel & 0xc0) >> 6));
1561             g = ((pixel & 0x38) | ((pixel & 0x38) >> 3) | ((pixel & 0x30) << 2)) << 8;
1562             r = (((pixel & 0x07)     ) |
1563                  ((pixel & 0x07) << 3) |
1564                  ((pixel & 0x06) << 6)) << 16;
1565             buffer[i] = (0xff000000 | r | g | b);
1566         }
1567     }
1568 }
1569
1570 static void
1571 fetch_pixels_a2r2g2b2 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1572 {
1573     int i;
1574
1575     for (i = 0; i < n_pixels; ++i)
1576     {
1577         int offset = buffer[2 * i];
1578         int line = buffer[2 * i + 1];
1579
1580         if (offset == 0xffffffff || line == 0xffffffff)
1581         {
1582             buffer[i] = 0;
1583         }
1584         else
1585         {
1586             uint32_t   a,r,g,b;
1587             uint32_t *bits = pict->bits + line*pict->rowstride;
1588             uint32_t   pixel = READ(pict, (uint8_t *) bits + offset);
1589             
1590             a = ((pixel & 0xc0) * 0x55) << 18;
1591             r = ((pixel & 0x30) * 0x55) << 12;
1592             g = ((pixel & 0x0c) * 0x55) << 6;
1593             b = ((pixel & 0x03) * 0x55);
1594             buffer[i] = a|r|g|b;
1595         }
1596     }
1597 }
1598
1599 static void
1600 fetch_pixels_a2b2g2r2 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1601 {
1602     int i;
1603
1604     for (i = 0; i < n_pixels; ++i)
1605     {
1606         int offset = buffer[2 * i];
1607         int line = buffer[2 * i + 1];
1608
1609         if (offset == 0xffffffff || line == 0xffffffff)
1610         {
1611             buffer[i] = 0;
1612         }
1613         else
1614         {
1615             uint32_t   a,r,g,b;
1616             uint32_t *bits = pict->bits + line*pict->rowstride;
1617             uint32_t   pixel = READ(pict, (uint8_t *) bits + offset);
1618             
1619             a = ((pixel & 0xc0) * 0x55) << 18;
1620             b = ((pixel & 0x30) * 0x55) >> 6;
1621             g = ((pixel & 0x0c) * 0x55) << 6;
1622             r = ((pixel & 0x03) * 0x55) << 16;
1623             buffer[i] = a|r|g|b;
1624         }
1625     }
1626 }
1627
1628 static void
1629 fetch_pixels_c8 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1630 {
1631     int i;
1632
1633     for (i = 0; i < n_pixels; ++i)
1634     {
1635         int offset = buffer[2 * i];
1636         int line = buffer[2 * i + 1];
1637
1638         if (offset == 0xffffffff || line == 0xffffffff)
1639         {
1640             buffer[i] = 0;
1641         }
1642         else
1643         {
1644             uint32_t *bits = pict->bits + line*pict->rowstride;
1645             uint32_t   pixel = READ(pict, (uint8_t *) bits + offset);
1646             const pixman_indexed_t * indexed = pict->indexed;
1647             buffer[i] = indexed->rgba[pixel];
1648         }
1649     }
1650 }
1651
1652 static void
1653 fetch_pixels_x4a4 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1654 {
1655     int i;
1656
1657     for (i = 0; i < n_pixels; ++i)
1658     {
1659         int offset = buffer[2 * i];
1660         int line = buffer[2 * i + 1];
1661
1662         if (offset == 0xffffffff || line == 0xffffffff)
1663         {
1664             buffer[i] = 0;
1665         }
1666         else
1667         {
1668             uint32_t *bits = pict->bits + line*pict->rowstride;
1669             uint32_t   pixel = READ(pict, (uint8_t *) bits + offset);
1670             
1671             buffer[i] = ((pixel & 0xf) | ((pixel & 0xf) << 4)) << 24;
1672         }
1673     }
1674 }
1675
1676 static void
1677 fetch_pixels_a4 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1678 {
1679     int i;
1680
1681     for (i = 0; i < n_pixels; ++i)
1682     {
1683         int offset = buffer[2 * i];
1684         int line = buffer[2 * i + 1];
1685
1686         if (offset == 0xffffffff || line == 0xffffffff)
1687         {
1688             buffer[i] = 0;
1689         }
1690         else
1691         {
1692             uint32_t *bits = pict->bits + line*pict->rowstride;
1693             uint32_t  pixel = FETCH_4 (pict, bits, offset);
1694             
1695             pixel |= pixel << 4;
1696             buffer[i] = pixel << 24;
1697         }
1698     }
1699 }
1700
1701 static void
1702 fetch_pixels_r1g2b1 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1703 {
1704     int i;
1705
1706     for (i = 0; i < n_pixels; ++i)
1707     {
1708         int offset = buffer[2 * i];
1709         int line = buffer[2 * i + 1];
1710         
1711         if (offset == 0xffffffff || line == 0xffffffff)
1712         {
1713             buffer[i] = 0;
1714         }
1715         else
1716         {
1717             uint32_t  r,g,b;
1718             uint32_t *bits = pict->bits + line*pict->rowstride;
1719             uint32_t  pixel = FETCH_4 (pict, bits, offset);
1720             
1721             r = ((pixel & 0x8) * 0xff) << 13;
1722             g = ((pixel & 0x6) * 0x55) << 7;
1723             b = ((pixel & 0x1) * 0xff);
1724             buffer[i] = 0xff000000|r|g|b;
1725         }
1726     }
1727 }
1728
1729 static void
1730 fetch_pixels_b1g2r1 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1731 {
1732     int i;
1733
1734     for (i = 0; i < n_pixels; ++i)
1735     {
1736         int offset = buffer[2 * i];
1737         int line = buffer[2 * i + 1];
1738
1739         if (offset == 0xffffffff || line == 0xffffffff)
1740         {
1741             buffer[i] = 0;
1742         }
1743         else
1744         {
1745             uint32_t  r,g,b;
1746             uint32_t *bits = pict->bits + line*pict->rowstride;
1747             uint32_t  pixel = FETCH_4 (pict, bits, offset);
1748             
1749             b = ((pixel & 0x8) * 0xff) >> 3;
1750             g = ((pixel & 0x6) * 0x55) << 7;
1751             r = ((pixel & 0x1) * 0xff) << 16;
1752             buffer[i] = 0xff000000|r|g|b;
1753         }
1754     }
1755 }
1756
1757 static void
1758 fetch_pixels_a1r1g1b1 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1759 {
1760     int i;
1761
1762     for (i = 0; i < n_pixels; ++i)
1763     {
1764         int offset = buffer[2 * i];
1765         int line = buffer[2 * i + 1];
1766
1767         if (offset == 0xffffffff || line == 0xffffffff)
1768         {
1769             buffer[i] = 0;
1770         }
1771         else
1772         {
1773             uint32_t  a,r,g,b;
1774             uint32_t *bits = pict->bits + line*pict->rowstride;
1775             uint32_t  pixel = FETCH_4 (pict, bits, offset);
1776             
1777             a = ((pixel & 0x8) * 0xff) << 21;
1778             r = ((pixel & 0x4) * 0xff) << 14;
1779             g = ((pixel & 0x2) * 0xff) << 7;
1780             b = ((pixel & 0x1) * 0xff);
1781             buffer[i] = a|r|g|b;
1782         }
1783     }
1784 }
1785
1786 static void
1787 fetch_pixels_a1b1g1r1 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1788 {
1789     int i;
1790
1791     for (i = 0; i < n_pixels; ++i)
1792     {
1793         int offset = buffer[2 * i];
1794         int line = buffer[2 * i + 1];
1795
1796         if (offset == 0xffffffff || line == 0xffffffff)
1797         {
1798             buffer[i] = 0;
1799         }
1800         else
1801         {
1802             uint32_t  a,r,g,b;
1803             uint32_t *bits = pict->bits + line*pict->rowstride;
1804             uint32_t  pixel = FETCH_4 (pict, bits, offset);
1805             
1806             a = ((pixel & 0x8) * 0xff) << 21;
1807             r = ((pixel & 0x4) * 0xff) >> 3;
1808             g = ((pixel & 0x2) * 0xff) << 7;
1809             b = ((pixel & 0x1) * 0xff) << 16;
1810             buffer[i] = a|r|g|b;
1811         }
1812     }
1813 }
1814
1815 static void
1816 fetch_pixels_c4 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1817 {
1818     int i;
1819
1820     for (i = 0; i < n_pixels; ++i)
1821     {
1822         int offset = buffer[2 * i];
1823         int line = buffer[2 * i + 1];
1824
1825         if (offset == 0xffffffff || line == 0xffffffff)
1826         {
1827             buffer[i] = 0;
1828         }
1829         else
1830         {
1831             uint32_t *bits = pict->bits + line*pict->rowstride;
1832             uint32_t  pixel = FETCH_4 (pict, bits, offset);
1833             const pixman_indexed_t * indexed = pict->indexed;
1834             
1835             buffer[i] = indexed->rgba[pixel];
1836         }
1837     }
1838 }
1839
1840
1841 static void
1842 fetch_pixels_a1 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1843 {
1844     int i;
1845
1846     for (i = 0; i < n_pixels; ++i)
1847     {
1848         int offset = buffer[2 * i];
1849         int line = buffer[2 * i + 1];
1850
1851         if (offset == 0xffffffff || line == 0xffffffff)
1852         {
1853             buffer[i] = 0;
1854         }
1855         else
1856         {
1857             uint32_t *bits = pict->bits + line*pict->rowstride;
1858             uint32_t  pixel = READ(pict, bits + (offset >> 5));
1859             uint32_t  a;
1860 #ifdef WORDS_BIGENDIAN
1861             a = pixel >> (0x1f - (offset & 0x1f));
1862 #else
1863             a = pixel >> (offset & 0x1f);
1864 #endif
1865             a = a & 1;
1866             a |= a << 1;
1867             a |= a << 2;
1868             a |= a << 4;
1869             buffer[i] = a << 24;
1870         }
1871     }
1872 }
1873
1874 static void
1875 fetch_pixels_g1 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1876 {
1877     int i;
1878
1879     for (i = 0; i < n_pixels; ++i)
1880     {
1881         int offset = buffer[2 * i];
1882         int line = buffer[2 * i + 1];
1883
1884         if (offset == 0xffffffff || line == 0xffffffff)
1885         {
1886             buffer[i] = 0;
1887         }
1888         else
1889         {
1890             uint32_t *bits = pict->bits + line*pict->rowstride;
1891             uint32_t pixel = READ(pict, bits + (offset >> 5));
1892             const pixman_indexed_t * indexed = pict->indexed;
1893             uint32_t a;
1894 #ifdef WORDS_BIGENDIAN
1895             a = pixel >> (0x1f - (offset & 0x1f));
1896 #else
1897             a = pixel >> (offset & 0x1f);
1898 #endif
1899             a = a & 1;
1900             buffer[i] = indexed->rgba[a];
1901         }
1902     }
1903 }
1904
1905 static void
1906 fetch_pixels_yuy2 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1907 {
1908     int i;
1909
1910     for (i = 0; i < n_pixels; ++i)
1911     {
1912         int offset = buffer[2 * i];
1913         int line = buffer[2 * i + 1];
1914
1915         if (offset == 0xffffffff || line == 0xffffffff)
1916         {
1917             buffer[i] = 0;
1918         }
1919         else
1920         {
1921             int16_t y, u, v;
1922             int32_t r, g, b;
1923             
1924             const uint32_t *bits = pict->bits + pict->rowstride * line;
1925             
1926             y = ((uint8_t *) bits)[offset << 1] - 16;
1927             u = ((uint8_t *) bits)[((offset << 1) & -4) + 1] - 128;
1928             v = ((uint8_t *) bits)[((offset << 1) & -4) + 3] - 128;
1929             
1930             /* R = 1.164(Y - 16) + 1.596(V - 128) */
1931             r = 0x012b27 * y + 0x019a2e * v;
1932             /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
1933             g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
1934             /* B = 1.164(Y - 16) + 2.018(U - 128) */
1935             b = 0x012b27 * y + 0x0206a2 * u;
1936             
1937             buffer[i] = 0xff000000 |
1938                 (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
1939                 (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
1940                 (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
1941         }
1942     }
1943 }
1944
1945 static void
1946 fetch_pixels_yv12 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
1947 {
1948     int i;
1949
1950     for (i = 0; i < n_pixels; ++i)
1951     {
1952         int offset = buffer[2 * i];
1953         int line = buffer[2 * i + 1];
1954
1955         if (offset == 0xffffffff || line == 0xffffffff)
1956         {
1957             buffer[i] = 0;
1958         }
1959         else
1960         {
1961             YV12_SETUP(pict);
1962             int16_t y = YV12_Y (line)[offset] - 16;
1963             int16_t u = YV12_U (line)[offset >> 1] - 128;
1964             int16_t v = YV12_V (line)[offset >> 1] - 128;
1965             int32_t r, g, b;
1966             
1967             /* R = 1.164(Y - 16) + 1.596(V - 128) */
1968             r = 0x012b27 * y + 0x019a2e * v;
1969             /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
1970             g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
1971             /* B = 1.164(Y - 16) + 2.018(U - 128) */
1972             b = 0x012b27 * y + 0x0206a2 * u;
1973             
1974             buffer[i] = 0xff000000 |
1975                 (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
1976                 (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
1977                 (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
1978         }
1979     }
1980 }
1981
1982 /*********************************** Store ************************************/
1983
1984 #define SPLIT_A(v)      uint32_t        a = ((v) >> 24), r = ((v) >> 16) & 0xff, g = ((v) >> 8) & 0xff, b = (v) & 0xff
1985 #define SPLIT(v)        uint32_t        r = ((v) >> 16) & 0xff, g = ((v) >> 8) & 0xff, b = (v) & 0xff
1986
1987 static void
1988 store_scanline_a2r10g10b10 (bits_image_t *image, int x, int y, int width, const uint32_t *v)
1989 {
1990     uint32_t *bits = image->bits + image->rowstride * y;
1991     uint32_t *pixel = bits + x;
1992     uint64_t *values = (uint64_t *)v;
1993     int i;
1994
1995     for (i = 0; i < width; ++i) {
1996         WRITE(image, pixel++,
1997             ((values[i] >> 32) & 0xc0000000) | // A
1998             ((values[i] >> 18) & 0x3ff00000) | // R
1999             ((values[i] >> 12) & 0xffc00) |    // G
2000             ((values[i] >> 6) & 0x3ff));       // B
2001     }
2002 }
2003
2004 static void
2005 store_scanline_x2r10g10b10 (bits_image_t *image, int x, int y, int width, const uint32_t *v)
2006 {
2007     uint32_t *bits = image->bits + image->rowstride * y;
2008     uint64_t *values = (uint64_t *)v;
2009     uint32_t *pixel = bits + x;
2010     int i;
2011
2012     for (i = 0; i < width; ++i) {
2013         WRITE(image, pixel++,
2014             ((values[i] >> 18) & 0x3ff00000) | // R
2015             ((values[i] >> 12) & 0xffc00) |    // G
2016             ((values[i] >> 6) & 0x3ff));       // B
2017     }
2018 }
2019
2020 static void
2021 store_scanline_a2b10g10r10 (bits_image_t *image, int x, int y, int width, const uint32_t *v)
2022 {
2023     uint32_t *bits = image->bits + image->rowstride * y;
2024     uint32_t *pixel = bits + x;
2025     uint64_t *values = (uint64_t *)v;
2026     int i;
2027     
2028     for (i = 0; i < width; ++i) {
2029         WRITE(image, pixel++,
2030             ((values[i] >> 32) & 0xc0000000) | // A
2031             ((values[i] >> 38) & 0x3ff) |      // R
2032             ((values[i] >> 12) & 0xffc00) |    // G
2033             ((values[i] << 14) & 0x3ff00000)); // B
2034     }
2035 }
2036
2037 static void
2038 store_scanline_x2b10g10r10 (bits_image_t *image, int x, int y, int width, const uint32_t *v)
2039 {
2040     uint32_t *bits = image->bits + image->rowstride * y;
2041     uint64_t *values = (uint64_t *)v;
2042     uint32_t *pixel = bits + x;
2043     int i;
2044     
2045     for (i = 0; i < width; ++i) {
2046         WRITE(image, pixel++,
2047             ((values[i] >> 38) & 0x3ff) |      // R
2048             ((values[i] >> 12) & 0xffc00) |    // G
2049             ((values[i] << 14) & 0x3ff00000)); // B
2050     }
2051 }
2052
2053 static void
2054 store_scanline_a8r8g8b8 (bits_image_t *image,
2055                   int x, int y, int width,
2056                   const uint32_t *values)
2057 {
2058     uint32_t *bits = image->bits + image->rowstride * y;
2059     
2060     MEMCPY_WRAPPED(image, ((uint32_t *)bits) + x, values, width*sizeof(uint32_t));
2061 }
2062
2063 static void
2064 store_scanline_x8r8g8b8 (bits_image_t *image,
2065                   int x, int y, int width,
2066                   const uint32_t *values)
2067 {
2068     uint32_t *bits = image->bits + image->rowstride * y;
2069     uint32_t *pixel = (uint32_t *)bits + x;
2070     int i;
2071     
2072     for (i = 0; i < width; ++i)
2073         WRITE(image, pixel++, values[i] & 0xffffff);
2074 }
2075
2076 static void
2077 store_scanline_a8b8g8r8 (bits_image_t *image,
2078                   int x, int y, int width,
2079                   const uint32_t *values)
2080 {
2081     uint32_t *bits = image->bits + image->rowstride * y;
2082     uint32_t *pixel = (uint32_t *)bits + x;
2083     int i;
2084     
2085     for (i = 0; i < width; ++i)
2086         WRITE(image, pixel++, (values[i] & 0xff00ff00) | ((values[i] >> 16) & 0xff) | ((values[i] & 0xff) << 16));
2087 }
2088
2089 static void
2090 store_scanline_x8b8g8r8 (bits_image_t *image,
2091                   int x, int y, int width,
2092                   const uint32_t *values)
2093 {
2094     uint32_t *bits = image->bits + image->rowstride * y;
2095     uint32_t *pixel = (uint32_t *)bits + x;
2096     int i;
2097     
2098     for (i = 0; i < width; ++i)
2099         WRITE(image, pixel++, (values[i] & 0x0000ff00) | ((values[i] >> 16) & 0xff) | ((values[i] & 0xff) << 16));
2100 }
2101
2102 static void
2103 store_scanline_b8g8r8a8 (bits_image_t *image,
2104                   int x, int y, int width,
2105                   const uint32_t *values)
2106 {
2107     uint32_t *bits = image->bits + image->rowstride * y;
2108     uint32_t *pixel = (uint32_t *)bits + x;
2109     int i;
2110
2111     for (i = 0; i < width; ++i)
2112         WRITE(image, pixel++,
2113             ((values[i] >> 24) & 0x000000ff) |
2114             ((values[i] >>  8) & 0x0000ff00) |
2115             ((values[i] <<  8) & 0x00ff0000) |
2116             ((values[i] << 24) & 0xff000000));
2117 }
2118
2119 static void
2120 store_scanline_b8g8r8x8 (bits_image_t *image,
2121                   int x, int y, int width,
2122                   const uint32_t *values)
2123 {
2124     uint32_t *bits = image->bits + image->rowstride * y;
2125     uint32_t *pixel = (uint32_t *)bits + x;
2126     int i;
2127
2128     for (i = 0; i < width; ++i)
2129         WRITE(image, pixel++,
2130             ((values[i] >>  8) & 0x0000ff00) |
2131             ((values[i] <<  8) & 0x00ff0000) |
2132             ((values[i] << 24) & 0xff000000));
2133 }
2134
2135 static void
2136 store_scanline_r8g8b8 (bits_image_t *image,
2137                 int x, int y, int width,
2138                 const uint32_t *values)
2139 {
2140     uint32_t *bits = image->bits + image->rowstride * y;
2141     uint8_t *pixel = ((uint8_t *) bits) + 3*x;
2142     int i;
2143
2144     for (i = 0; i < width; ++i)
2145     {
2146         uint32_t val = values[i];
2147 #ifdef WORDS_BIGENDIAN
2148         WRITE(image, pixel++, (val & 0x00ff0000) >> 16);
2149         WRITE(image, pixel++, (val & 0x0000ff00) >>  8);
2150         WRITE(image, pixel++, (val & 0x000000ff) >>  0);
2151 #else
2152         WRITE(image, pixel++, (val & 0x000000ff) >>  0);
2153         WRITE(image, pixel++, (val & 0x0000ff00) >>  8);
2154         WRITE(image, pixel++, (val & 0x00ff0000) >> 16);
2155 #endif
2156     }
2157 }
2158
2159 static void
2160 store_scanline_b8g8r8 (bits_image_t *image,
2161                 int x, int y, int width,
2162                 const uint32_t *values)
2163 {
2164     uint32_t *bits = image->bits + image->rowstride * y;
2165     uint8_t *pixel = ((uint8_t *) bits) + 3*x;
2166     int i;
2167
2168     for (i = 0; i < width; ++i)
2169     {
2170         uint32_t val = values[i];
2171 #ifdef WORDS_BIGENDIAN
2172         WRITE(image, pixel++, (val & 0x000000ff) >>  0);
2173         WRITE(image, pixel++, (val & 0x0000ff00) >>  8);
2174         WRITE(image, pixel++, (val & 0x00ff0000) >> 16);
2175 #else
2176         WRITE(image, pixel++, (val & 0x00ff0000) >> 16);
2177         WRITE(image, pixel++, (val & 0x0000ff00) >>  8);
2178         WRITE(image, pixel++, (val & 0x000000ff) >>  0);
2179 #endif
2180     }
2181 }
2182
2183 static void
2184 store_scanline_r5g6b5 (bits_image_t *image,
2185                 int x, int y, int width,
2186                 const uint32_t *values)
2187 {
2188     uint32_t *bits = image->bits + image->rowstride * y;
2189     uint16_t *pixel = ((uint16_t *) bits) + x;
2190     int i;
2191
2192     for (i = 0; i < width; ++i) {
2193         uint32_t s = values[i];
2194         WRITE(image, pixel++, ((s >> 3) & 0x001f) |
2195               ((s >> 5) & 0x07e0) |
2196               ((s >> 8) & 0xf800));
2197     }
2198 }
2199
2200 static void
2201 store_scanline_b5g6r5 (bits_image_t *image,
2202                 int x, int y, int width,
2203                 const uint32_t *values)
2204 {
2205     uint32_t *bits = image->bits + image->rowstride * y;
2206     uint16_t  *pixel = ((uint16_t *) bits) + x;
2207     int i;
2208
2209     for (i = 0; i < width; ++i) {
2210         SPLIT(values[i]);
2211         WRITE(image, pixel++, ((b << 8) & 0xf800) |
2212               ((g << 3) & 0x07e0) |
2213               ((r >> 3)         ));
2214     }
2215 }
2216
2217 static void
2218 store_scanline_a1r5g5b5 (bits_image_t *image,
2219                   int x, int y, int width,
2220                   const uint32_t *values)
2221 {
2222     uint32_t *bits = image->bits + image->rowstride * y;
2223     uint16_t  *pixel = ((uint16_t *) bits) + x;
2224     int i;
2225
2226     for (i = 0; i < width; ++i) {
2227         SPLIT_A(values[i]);
2228         WRITE(image, pixel++, ((a << 8) & 0x8000) |
2229               ((r << 7) & 0x7c00) |
2230               ((g << 2) & 0x03e0) |
2231               ((b >> 3)         ));
2232     }
2233 }
2234
2235 static void
2236 store_scanline_x1r5g5b5 (bits_image_t *image,
2237                   int x, int y, int width,
2238                   const uint32_t *values)
2239 {
2240     uint32_t *bits = image->bits + image->rowstride * y;
2241     uint16_t  *pixel = ((uint16_t *) bits) + x;
2242     int i;
2243
2244     for (i = 0; i < width; ++i) {
2245         SPLIT(values[i]);
2246         WRITE(image, pixel++, ((r << 7) & 0x7c00) |
2247               ((g << 2) & 0x03e0) |
2248               ((b >> 3)         ));
2249     }
2250 }
2251
2252 static void
2253 store_scanline_a1b5g5r5 (bits_image_t *image,
2254                   int x, int y, int width,
2255                   const uint32_t *values)
2256 {
2257     uint32_t *bits = image->bits + image->rowstride * y;
2258     uint16_t  *pixel = ((uint16_t *) bits) + x;
2259     int i;
2260
2261     for (i = 0; i < width; ++i) {
2262         SPLIT_A(values[i]);
2263         WRITE(image, pixel++, ((a << 8) & 0x8000) |
2264               ((b << 7) & 0x7c00) |
2265               ((g << 2) & 0x03e0) |
2266               ((r >> 3)         ));
2267     }
2268 }
2269
2270 static void
2271 store_scanline_x1b5g5r5 (bits_image_t *image,
2272                   int x, int y, int width,
2273                   const uint32_t *values)
2274 {
2275     uint32_t *bits = image->bits + image->rowstride * y;
2276     uint16_t  *pixel = ((uint16_t *) bits) + x;
2277     int i;
2278
2279     for (i = 0; i < width; ++i) {
2280         SPLIT(values[i]);
2281         WRITE(image, pixel++, ((b << 7) & 0x7c00) |
2282               ((g << 2) & 0x03e0) |
2283               ((r >> 3)         ));
2284     }
2285 }
2286
2287 static void
2288 store_scanline_a4r4g4b4 (bits_image_t *image,
2289                   int x, int y, int width,
2290                   const uint32_t *values)
2291 {
2292     uint32_t *bits = image->bits + image->rowstride * y;
2293     uint16_t  *pixel = ((uint16_t *) bits) + x;
2294     int i;
2295
2296     for (i = 0; i < width; ++i) {
2297         SPLIT_A(values[i]);
2298         WRITE(image, pixel++, ((a << 8) & 0xf000) |
2299               ((r << 4) & 0x0f00) |
2300               ((g     ) & 0x00f0) |
2301               ((b >> 4)         ));
2302     }
2303 }
2304
2305 static void
2306 store_scanline_x4r4g4b4 (bits_image_t *image,
2307                   int x, int y, int width,
2308                   const uint32_t *values)
2309 {
2310     uint32_t *bits = image->bits + image->rowstride * y;
2311     uint16_t  *pixel = ((uint16_t *) bits) + x;
2312     int i;
2313
2314     for (i = 0; i < width; ++i) {
2315         SPLIT(values[i]);
2316         WRITE(image, pixel++, ((r << 4) & 0x0f00) |
2317               ((g     ) & 0x00f0) |
2318               ((b >> 4)         ));
2319     }
2320 }
2321
2322 static void
2323 store_scanline_a4b4g4r4 (bits_image_t *image,
2324                   int x, int y, int width,
2325                   const uint32_t *values)
2326 {
2327     uint32_t *bits = image->bits + image->rowstride * y;
2328     uint16_t  *pixel = ((uint16_t *) bits) + x;
2329     int i;
2330
2331     for (i = 0; i < width; ++i) {
2332         SPLIT_A(values[i]);
2333         WRITE(image, pixel++, ((a << 8) & 0xf000) |
2334               ((b << 4) & 0x0f00) |
2335               ((g     ) & 0x00f0) |
2336               ((r >> 4)         ));
2337     }
2338 }
2339
2340 static void
2341 store_scanline_x4b4g4r4 (bits_image_t *image,
2342                   int x, int y, int width,
2343                   const uint32_t *values)
2344 {
2345     uint32_t *bits = image->bits + image->rowstride * y;
2346     uint16_t  *pixel = ((uint16_t *) bits) + x;
2347     int i;
2348
2349     for (i = 0; i < width; ++i) {
2350         SPLIT(values[i]);
2351         WRITE(image, pixel++, ((b << 4) & 0x0f00) |
2352               ((g     ) & 0x00f0) |
2353               ((r >> 4)         ));
2354     }
2355 }
2356
2357 static void
2358 store_scanline_a8 (bits_image_t *image,
2359             int x, int y, int width,
2360             const uint32_t *values)
2361 {
2362     uint32_t *bits = image->bits + image->rowstride * y;
2363     uint8_t   *pixel = ((uint8_t *) bits) + x;
2364     int i;
2365
2366     for (i = 0; i < width; ++i) {
2367         WRITE(image, pixel++, values[i] >> 24);
2368     }
2369 }
2370
2371 static void
2372 store_scanline_r3g3b2 (bits_image_t *image,
2373                 int x, int y, int width,
2374                 const uint32_t *values)
2375 {
2376     uint32_t *bits = image->bits + image->rowstride * y;
2377     uint8_t   *pixel = ((uint8_t *) bits) + x;
2378     int i;
2379
2380     for (i = 0; i < width; ++i) {
2381         SPLIT(values[i]);
2382         WRITE(image, pixel++,
2383               ((r     ) & 0xe0) |
2384               ((g >> 3) & 0x1c) |
2385               ((b >> 6)       ));
2386     }
2387 }
2388
2389 static void
2390 store_scanline_b2g3r3 (bits_image_t *image,
2391                 int x, int y, int width,
2392                 const uint32_t *values)
2393 {
2394     uint32_t *bits = image->bits + image->rowstride * y;
2395     uint8_t   *pixel = ((uint8_t *) bits) + x;
2396     int i;
2397
2398     for (i = 0; i < width; ++i) {
2399         SPLIT(values[i]);
2400         WRITE(image, pixel++,
2401               ((b     ) & 0xc0) |
2402               ((g >> 2) & 0x38) |
2403               ((r >> 5)       ));
2404     }
2405 }
2406
2407 static void
2408 store_scanline_a2r2g2b2 (bits_image_t *image,
2409                   int x, int y, int width,
2410                   const uint32_t *values)
2411 {
2412     uint32_t *bits = image->bits + image->rowstride * y;
2413     uint8_t   *pixel = ((uint8_t *) bits) + x;
2414     int i;
2415
2416     for (i = 0; i < width; ++i) {
2417         SPLIT_A(values[i]);
2418         WRITE(image, pixel++, ((a     ) & 0xc0) |
2419               ((r >> 2) & 0x30) |
2420               ((g >> 4) & 0x0c) |
2421               ((b >> 6)       ));
2422     }
2423 }
2424
2425 static void
2426 store_scanline_a2b2g2r2 (bits_image_t *image,
2427                   int x, int y, int width,
2428                   const uint32_t *values)
2429 {
2430     uint32_t *bits = image->bits + image->rowstride * y;
2431     uint8_t   *pixel = ((uint8_t *) bits) + x;
2432     int i;
2433
2434     for (i = 0; i < width; ++i) {
2435         SPLIT_A(values[i]);
2436         *(pixel++) =  ((a     ) & 0xc0) |
2437             ((b >> 2) & 0x30) |
2438             ((g >> 4) & 0x0c) |
2439             ((r >> 6)       );
2440     }
2441 }
2442
2443 static void
2444 store_scanline_c8 (bits_image_t *image,
2445             int x, int y, int width,
2446             const uint32_t *values)
2447 {
2448     uint32_t *bits = image->bits + image->rowstride * y;
2449     uint8_t *pixel = ((uint8_t *) bits) + x;
2450     const pixman_indexed_t *indexed = image->indexed;
2451     int i;
2452     
2453     for (i = 0; i < width; ++i) {
2454         WRITE(image, pixel++, RGB24_TO_ENTRY(indexed,values[i]));
2455     }
2456 }
2457
2458 static void
2459 store_scanline_x4a4 (bits_image_t *image,
2460               int x, int y, int width,
2461               const uint32_t *values)
2462 {
2463     uint32_t *bits = image->bits + image->rowstride * y;
2464     uint8_t   *pixel = ((uint8_t *) bits) + x;
2465     int i;
2466
2467     for (i = 0; i < width; ++i) {
2468         WRITE(image, pixel++, values[i] >> 28);
2469     }
2470 }
2471
2472 #define STORE_8(img,l,o,v)  (WRITE(img, (uint8_t *)(l) + ((o) >> 3), (v)))
2473 #ifdef WORDS_BIGENDIAN
2474 #define STORE_4(img,l,o,v)  STORE_8(img,l,o,((o) & 4 ?                          \
2475                                    (FETCH_8(img,l,o) & 0xf0) | (v) :            \
2476                                    (FETCH_8(img,l,o) & 0x0f) | ((v) << 4)))
2477 #else
2478 #define STORE_4(img,l,o,v)  STORE_8(img,l,o,((o) & 4 ?                         \
2479                                    (FETCH_8(img,l,o) & 0x0f) | ((v) << 4) : \
2480                                    (FETCH_8(img,l,o) & 0xf0) | (v)))
2481 #endif
2482
2483 static void
2484 store_scanline_a4 (bits_image_t *image,
2485             int x, int y, int width,
2486             const uint32_t *values)
2487 {
2488     uint32_t *bits = image->bits + image->rowstride * y;
2489     int i;
2490     
2491     for (i = 0; i < width; ++i) {
2492         STORE_4(image, bits, i + x, values[i]>>28);
2493     }
2494 }
2495
2496 static void
2497 store_scanline_r1g2b1 (bits_image_t *image,
2498                 int x, int y, int width,
2499                 const uint32_t *values)
2500 {
2501     uint32_t *bits = image->bits + image->rowstride * y;
2502     int i;
2503     
2504     for (i = 0; i < width; ++i) {
2505         uint32_t  pixel;
2506
2507         SPLIT(values[i]);
2508         pixel = (((r >> 4) & 0x8) |
2509                  ((g >> 5) & 0x6) |
2510                  ((b >> 7)      ));
2511         STORE_4(image, bits, i + x, pixel);
2512     }
2513 }
2514
2515 static void
2516 store_scanline_b1g2r1 (bits_image_t *image,
2517                 int x, int y, int width,
2518                 const uint32_t *values)
2519 {
2520     uint32_t *bits = image->bits + image->rowstride * y;
2521     int i;
2522     
2523     for (i = 0; i < width; ++i) {
2524         uint32_t  pixel;
2525
2526         SPLIT(values[i]);
2527         pixel = (((b >> 4) & 0x8) |
2528                  ((g >> 5) & 0x6) |
2529                  ((r >> 7)      ));
2530         STORE_4(image, bits, i + x, pixel);
2531     }
2532 }
2533
2534 static void
2535 store_scanline_a1r1g1b1 (bits_image_t *image,
2536                   int x, int y, int width,
2537                   const uint32_t *values)
2538 {
2539     uint32_t *bits = image->bits + image->rowstride * y;
2540     int i;
2541     
2542     for (i = 0; i < width; ++i) {
2543         uint32_t  pixel;
2544         SPLIT_A(values[i]);
2545         pixel = (((a >> 4) & 0x8) |
2546                  ((r >> 5) & 0x4) |
2547                  ((g >> 6) & 0x2) |
2548                  ((b >> 7)      ));
2549         STORE_4(image, bits, i + x, pixel);
2550     }
2551 }
2552
2553 static void
2554 store_scanline_a1b1g1r1 (bits_image_t *image,
2555                   int x, int y, int width,
2556                   const uint32_t *values)
2557 {
2558     uint32_t *bits = image->bits + image->rowstride * y;
2559     int i;
2560     
2561     for (i = 0; i < width; ++i) {
2562         uint32_t  pixel;
2563         SPLIT_A(values[i]);
2564         pixel = (((a >> 4) & 0x8) |
2565                  ((b >> 5) & 0x4) |
2566                  ((g >> 6) & 0x2) |
2567                  ((r >> 7)      ));
2568         STORE_4(image, bits, i + x, pixel);
2569     }
2570 }
2571
2572 static void
2573 store_scanline_c4 (bits_image_t *image,
2574             int x, int y, int width,
2575             const uint32_t *values)
2576 {
2577     uint32_t *bits = image->bits + image->rowstride * y;
2578     const pixman_indexed_t *indexed = image->indexed;
2579     int i;
2580     
2581     for (i = 0; i < width; ++i) {
2582         uint32_t  pixel;
2583
2584         pixel = RGB24_TO_ENTRY(indexed, values[i]);
2585         STORE_4(image, bits, i + x, pixel);
2586     }
2587 }
2588
2589 static void
2590 store_scanline_a1 (bits_image_t *image,
2591             int x, int y, int width,
2592             const uint32_t *values)
2593 {
2594     uint32_t *bits = image->bits + image->rowstride * y;
2595     int i;
2596     
2597     for (i = 0; i < width; ++i)
2598     {
2599         uint32_t  *pixel = ((uint32_t *) bits) + ((i+x) >> 5);
2600         uint32_t mask, v;
2601 #ifdef WORDS_BIGENDIAN
2602         mask = 1 << (0x1f - ((i+x) & 0x1f));
2603 #else
2604         mask = 1 << ((i+x) & 0x1f);
2605 #endif
2606         v = values[i] & 0x80000000 ? mask : 0;
2607         WRITE(image, pixel, (READ(image, pixel) & ~mask) | v);
2608     }
2609 }
2610
2611 static void
2612 store_scanline_g1 (bits_image_t *image,
2613             int x, int y, int width,
2614             const uint32_t *values)
2615 {
2616     uint32_t *bits = image->bits + image->rowstride * y;
2617     const pixman_indexed_t *indexed = image->indexed;
2618     int i;
2619     
2620     for (i = 0; i < width; ++i)
2621     {
2622         uint32_t  *pixel = ((uint32_t *) bits) + ((i+x) >> 5);
2623         uint32_t  mask, v;
2624 #ifdef WORDS_BIGENDIAN
2625         mask = 1 << (0x1f - ((i+x) & 0x1f));
2626 #else
2627         mask = 1 << ((i + x) & 0x1f);
2628 #endif
2629         v = RGB24_TO_ENTRY_Y (indexed, values[i]) ? mask : 0;
2630         WRITE(image, pixel, (READ(image, pixel) & ~mask) | v);
2631     }
2632 }
2633
2634 /*
2635  * Contracts a 64bpp image to 32bpp and then stores it using a regular 32-bit
2636  * store proc. Despite the type, this function expects a uint64_t buffer.
2637  */
2638 static void
2639 store_scanline_generic_64 (bits_image_t *image, int x, int y, int width, const uint32_t *values)
2640 {
2641     uint32_t *argb8Pixels;
2642
2643     assert(image->common.type == BITS);
2644
2645     argb8Pixels = pixman_malloc_ab (width, sizeof(uint32_t));
2646     if (!argb8Pixels)
2647         return;
2648
2649     /* Contract the scanline.  We could do this in place if values weren't
2650      * const.
2651      */
2652     pixman_contract(argb8Pixels, (uint64_t *)values, width);
2653     
2654     image->store_scanline_raw_32 (image, x, y, width, argb8Pixels);
2655
2656     free(argb8Pixels);
2657 }
2658
2659 /* Despite the type, this function expects both buffer and mask to be uint64_t */
2660 static void
2661 fetch_scanline_generic_64 (pixman_image_t *image, int x, int y, int width, uint32_t *buffer,
2662                    const uint32_t *mask, uint32_t mask_bits)
2663 {
2664     /* Fetch the pixels into the first half of buffer and then expand them in
2665      * place.
2666      */
2667     image->bits.fetch_scanline_raw_32 (image, x, y, width, buffer, NULL, 0);
2668     
2669     pixman_expand ((uint64_t *)buffer, buffer, image->bits.format, width);
2670 }
2671
2672 /* Despite the type, this function expects a uint64_t *buffer */
2673 static void
2674 fetch_pixels_generic_64 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
2675 {
2676     pict->fetch_pixels_raw_32 (pict, buffer, n_pixels);
2677     
2678     pixman_expand ((uint64_t *)buffer, buffer, pict->format, n_pixels);
2679 }
2680
2681 /*
2682  * XXX: The transformed fetch path only works at 32-bpp so far.  When all paths
2683  * have wide versions, this can be removed.
2684  *
2685  * WARNING: This function loses precision!
2686  */
2687 static void
2688 fetch_pixels_generic_lossy_32 (bits_image_t *pict, uint32_t *buffer, int n_pixels)
2689 {
2690     /* Since buffer contains n_pixels coordinate pairs, it also has enough room for
2691      * n_pixels 64 bit pixels.
2692      */
2693     pict->fetch_pixels_raw_64 (pict, buffer, n_pixels);
2694     
2695     pixman_contract (buffer, (uint64_t *)buffer, n_pixels);
2696 }
2697
2698 typedef struct
2699 {
2700     pixman_format_code_t                format;
2701     fetch_scanline_t                            fetch_scanline_raw_32;
2702     fetch_scanline_t                            fetch_scanline_raw_64;
2703     fetch_pixels_t                      fetch_pixels_raw_32;
2704     fetch_pixels_t                      fetch_pixels_raw_64;
2705     store_scanline_t                    store_scanline_raw_32;
2706     store_scanline_t                    store_scanline_raw_64;
2707 } format_info_t;
2708
2709 #define FORMAT_INFO(format)                                             \
2710     {                                                                   \
2711         PIXMAN_##format,                                                \
2712             fetch_scanline_##format, fetch_scanline_generic_64,                 \
2713             fetch_pixels_##format, fetch_pixels_generic_64,             \
2714             store_scanline_##format, store_scanline_generic_64                          \
2715     }
2716
2717 static const format_info_t accessors[] =
2718 {
2719 /* 32 bpp formats */
2720     FORMAT_INFO (a8r8g8b8),
2721     FORMAT_INFO (x8r8g8b8),
2722     FORMAT_INFO (a8b8g8r8),
2723     FORMAT_INFO (x8b8g8r8),
2724     FORMAT_INFO (b8g8r8a8),
2725     FORMAT_INFO (b8g8r8x8),
2726
2727 /* 24bpp formats */
2728     FORMAT_INFO (r8g8b8),
2729     FORMAT_INFO (b8g8r8),
2730     
2731 /* 16bpp formats */
2732     FORMAT_INFO (r5g6b5),
2733     FORMAT_INFO (b5g6r5),
2734     
2735     FORMAT_INFO (a1r5g5b5),
2736     FORMAT_INFO (x1r5g5b5),
2737     FORMAT_INFO (a1b5g5r5),
2738     FORMAT_INFO (x1b5g5r5),
2739     FORMAT_INFO (a4r4g4b4),
2740     FORMAT_INFO (x4r4g4b4),
2741     FORMAT_INFO (a4b4g4r4),
2742     FORMAT_INFO (x4b4g4r4),
2743     
2744 /* 8bpp formats */
2745     FORMAT_INFO (a8),
2746     FORMAT_INFO (r3g3b2),
2747     FORMAT_INFO (b2g3r3),
2748     FORMAT_INFO (a2r2g2b2),
2749     FORMAT_INFO (a2b2g2r2),
2750     
2751     FORMAT_INFO (c8),
2752
2753 #define fetch_scanline_g8 fetch_scanline_c8
2754 #define fetch_pixels_g8 fetch_pixels_c8
2755 #define store_scanline_g8 store_scanline_c8
2756     FORMAT_INFO (g8),
2757 #define fetch_scanline_x4c4 fetch_scanline_c8
2758 #define fetch_pixels_x4c4 fetch_pixels_c8
2759 #define store_scanline_x4c4 store_scanline_c8
2760     FORMAT_INFO (x4c4),
2761 #define fetch_scanline_x4g4 fetch_scanline_c8
2762 #define fetch_pixels_x4g4 fetch_pixels_c8
2763 #define store_scanline_x4g4 store_scanline_c8
2764     FORMAT_INFO (x4g4),
2765     
2766     FORMAT_INFO (x4a4),
2767     
2768 /* 4bpp formats */
2769     FORMAT_INFO (a4),
2770     FORMAT_INFO (r1g2b1),
2771     FORMAT_INFO (b1g2r1),
2772     FORMAT_INFO (a1r1g1b1),
2773     FORMAT_INFO (a1b1g1r1),
2774     
2775     FORMAT_INFO (c4),
2776 #define fetch_scanline_g4 fetch_scanline_c4
2777 #define fetch_pixels_g4 fetch_pixels_c4
2778 #define store_scanline_g4 store_scanline_c4
2779     FORMAT_INFO (g4),
2780     
2781 /* 1bpp formats */
2782     FORMAT_INFO (a1),
2783     FORMAT_INFO (g1),
2784
2785 /* Wide formats */
2786
2787     { PIXMAN_a2r10g10b10,
2788       NULL, fetch_scanline_a2r10g10b10,
2789       fetch_pixels_generic_lossy_32, fetch_pixels_a2r10g10b10_64,
2790       NULL, store_scanline_a2r10g10b10 },
2791
2792     { PIXMAN_x2r10g10b10,
2793       NULL, fetch_scanline_x2r10g10b10,
2794       fetch_pixels_generic_lossy_32, fetch_pixels_x2r10g10b10_64,
2795       NULL, store_scanline_x2r10g10b10 },
2796
2797     { PIXMAN_a2b10g10r10,
2798       NULL, fetch_scanline_a2b10g10r10,
2799       fetch_pixels_generic_lossy_32, fetch_pixels_a2b10g10r10_64,
2800       NULL, store_scanline_a2b10g10r10 },
2801
2802     { PIXMAN_x2b10g10r10,
2803       NULL, fetch_scanline_x2b10g10r10,
2804       fetch_pixels_generic_lossy_32, fetch_pixels_x2b10g10r10_64,
2805       NULL, store_scanline_x2b10g10r10 },
2806
2807 /* YUV formats */
2808     { PIXMAN_yuy2,
2809       fetch_scanline_yuy2, fetch_scanline_generic_64,
2810       fetch_pixels_yuy2, fetch_pixels_generic_64,
2811       NULL, NULL },
2812
2813     { PIXMAN_yv12,
2814       fetch_scanline_yv12, fetch_scanline_generic_64,
2815       fetch_pixels_yv12, fetch_pixels_generic_64,
2816       NULL, NULL },
2817     
2818     { PIXMAN_null },
2819 };
2820
2821 static void
2822 setup_accessors (bits_image_t *image)
2823 {
2824     const format_info_t *info = accessors;
2825
2826     while (info->format != PIXMAN_null)
2827     {
2828         if (info->format == image->format)
2829         {
2830             image->fetch_scanline_raw_32 = info->fetch_scanline_raw_32;
2831             image->fetch_scanline_raw_64 = info->fetch_scanline_raw_64;
2832             image->fetch_pixels_raw_32 = info->fetch_pixels_raw_32;
2833             image->fetch_pixels_raw_64 = info->fetch_pixels_raw_64;
2834             image->store_scanline_raw_32 = info->store_scanline_raw_32;
2835             image->store_scanline_raw_64 = info->store_scanline_raw_64;
2836
2837             return;
2838         }
2839
2840         info++;
2841     }
2842 }
2843
2844 #ifndef PIXMAN_FB_ACCESSORS
2845 void
2846 _pixman_bits_image_setup_raw_accessors_accessors (bits_image_t *image);
2847
2848 void
2849 _pixman_bits_image_setup_raw_accessors (bits_image_t *image)
2850 {
2851     if (image->common.read_func || image->common.write_func)
2852         _pixman_bits_image_setup_raw_accessors_accessors (image);
2853     else
2854         setup_accessors (image);
2855 }
2856
2857 #else
2858
2859 void
2860 _pixman_bits_image_setup_raw_accessors_accessors (bits_image_t *image)
2861 {
2862     setup_accessors (image);
2863 }
2864
2865 #endif