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