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