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