Fix shift bug in fetch_scanline/pixel_a2b2g2r2()
[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) >> 4;
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) >> 3))))
853 #ifdef WORDS_BIGENDIAN
854 #define FETCH_4(img,l,o)                                                \
855     (((4 * (o)) & 4) ? (FETCH_8 (img,l, 4 * (o)) & 0xf) : (FETCH_8 (img,l,(4 * (o))) >> 4))
856 #else
857 #define FETCH_4(img,l,o)                                                \
858     (((4 * (o)) & 4) ? (FETCH_8 (img, l, 4 * (o)) >> 4) : (FETCH_8 (img, l, (4 * (o))) & 0xf))
859 #endif
860
861 static void
862 fetch_scanline_a4 (pixman_image_t *image,
863                    int             x,
864                    int             y,
865                    int             width,
866                    uint32_t *      buffer,
867                    const uint32_t *mask,
868                    uint32_t        mask_bits)
869 {
870     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
871     int i;
872
873     for (i = 0; i < width; ++i)
874     {
875         uint32_t p = FETCH_4 (image, bits, i + x);
876
877         p |= p << 4;
878
879         *buffer++ = p << 24;
880     }
881 }
882
883 static void
884 fetch_scanline_r1g2b1 (pixman_image_t *image,
885                        int             x,
886                        int             y,
887                        int             width,
888                        uint32_t *      buffer,
889                        const uint32_t *mask,
890                        uint32_t        mask_bits)
891 {
892     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
893     int i;
894     
895     for (i = 0; i < width; ++i)
896     {
897         uint32_t p = FETCH_4 (image, bits, i + x);
898         uint32_t r, g, b;
899         
900         r = ((p & 0x8) * 0xff) << 13;
901         g = ((p & 0x6) * 0x55) << 7;
902         b = ((p & 0x1) * 0xff);
903         
904         *buffer++ = 0xff000000 | r | g | b;
905     }
906 }
907
908 static void
909 fetch_scanline_b1g2r1 (pixman_image_t *image,
910                        int             x,
911                        int             y,
912                        int             width,
913                        uint32_t *      buffer,
914                        const uint32_t *mask,
915                        uint32_t        mask_bits)
916 {
917     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
918     int i;
919     
920     for (i = 0; i < width; ++i)
921     {
922         uint32_t p = FETCH_4 (image, bits, i + x);
923         uint32_t r, g, b;
924         
925         b = ((p & 0x8) * 0xff) >> 3;
926         g = ((p & 0x6) * 0x55) << 7;
927         r = ((p & 0x1) * 0xff) << 16;
928
929         *buffer++ = 0xff000000 | r | g | b;
930     }
931 }
932
933 static void
934 fetch_scanline_a1r1g1b1 (pixman_image_t *image,
935                          int             x,
936                          int             y,
937                          int             width,
938                          uint32_t *      buffer,
939                          const uint32_t *mask,
940                          uint32_t        mask_bits)
941 {
942     uint32_t a, r, g, b;
943     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
944     int i;
945     
946     for (i = 0; i < width; ++i)
947     {
948         uint32_t p = FETCH_4 (image, bits, i + x);
949         
950         a = ((p & 0x8) * 0xff) << 21;
951         r = ((p & 0x4) * 0xff) << 14;
952         g = ((p & 0x2) * 0xff) << 7;
953         b = ((p & 0x1) * 0xff);
954         
955         *buffer++ = a | r | g | b;
956     }
957 }
958
959 static void
960 fetch_scanline_a1b1g1r1 (pixman_image_t *image,
961                          int             x,
962                          int             y,
963                          int             width,
964                          uint32_t *      buffer,
965                          const uint32_t *mask,
966                          uint32_t        mask_bits)
967 {
968     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
969     int i;
970     
971     for (i = 0; i < width; ++i)
972     {
973         uint32_t p = FETCH_4 (image, bits, i + x);
974         uint32_t a, r, g, b;
975         
976         a = ((p & 0x8) * 0xff) << 21;
977         r = ((p & 0x4) * 0xff) >> 3;
978         g = ((p & 0x2) * 0xff) << 7;
979         b = ((p & 0x1) * 0xff) << 16;
980         
981         *buffer++ = a | r | g | b;
982     }
983 }
984
985 static void
986 fetch_scanline_c4 (pixman_image_t *image,
987                    int             x,
988                    int             y,
989                    int             width,
990                    uint32_t *      buffer,
991                    const uint32_t *mask,
992                    uint32_t        mask_bits)
993 {
994     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
995     const pixman_indexed_t * indexed = image->bits.indexed;
996     int i;
997     
998     for (i = 0; i < width; ++i)
999     {
1000         uint32_t p = FETCH_4 (image, bits, i + x);
1001         
1002         *buffer++ = indexed->rgba[p];
1003     }
1004 }
1005
1006 static void
1007 fetch_scanline_a1 (pixman_image_t *image,
1008                    int             x,
1009                    int             y,
1010                    int             width,
1011                    uint32_t *      buffer,
1012                    const uint32_t *mask,
1013                    uint32_t        mask_bits)
1014 {
1015     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
1016     int i;
1017     
1018     for (i = 0; i < width; ++i)
1019     {
1020         uint32_t p = READ (image, bits + ((i + x) >> 5));
1021         uint32_t a;
1022         
1023 #ifdef WORDS_BIGENDIAN
1024         a = p >> (0x1f - ((i + x) & 0x1f));
1025 #else
1026         a = p >> ((i + x) & 0x1f);
1027 #endif
1028         a = a & 1;
1029         a |= a << 1;
1030         a |= a << 2;
1031         a |= a << 4;
1032         
1033         *buffer++ = a << 24;
1034     }
1035 }
1036
1037 static void
1038 fetch_scanline_g1 (pixman_image_t *image,
1039                    int             x,
1040                    int             y,
1041                    int             width,
1042                    uint32_t *      buffer,
1043                    const uint32_t *mask,
1044                    uint32_t        mask_bits)
1045 {
1046     const uint32_t *bits = image->bits.bits + y * image->bits.rowstride;
1047     const pixman_indexed_t * indexed = image->bits.indexed;
1048     int i;
1049     
1050     for (i = 0; i < width; ++i)
1051     {
1052         uint32_t p = READ (image, bits + ((i + x) >> 5));
1053         uint32_t a;
1054         
1055 #ifdef WORDS_BIGENDIAN
1056         a = p >> (0x1f - ((i + x) & 0x1f));
1057 #else
1058         a = p >> ((i + x) & 0x1f);
1059 #endif
1060         a = a & 1;
1061         
1062         *buffer++ = indexed->rgba[a];
1063     }
1064 }
1065
1066 static void
1067 fetch_scanline_yuy2 (pixman_image_t *image,
1068                      int             x,
1069                      int             line,
1070                      int             width,
1071                      uint32_t *      buffer,
1072                      const uint32_t *mask,
1073                      uint32_t        mask_bits)
1074 {
1075     const uint32_t *bits = image->bits.bits + image->bits.rowstride * line;
1076     int i;
1077     
1078     for (i = 0; i < width; i++)
1079     {
1080         int16_t y, u, v;
1081         int32_t r, g, b;
1082         
1083         y = ((uint8_t *) bits)[(x + i) << 1] - 16;
1084         u = ((uint8_t *) bits)[(((x + i) << 1) & - 4) + 1] - 128;
1085         v = ((uint8_t *) bits)[(((x + i) << 1) & - 4) + 3] - 128;
1086         
1087         /* R = 1.164(Y - 16) + 1.596(V - 128) */
1088         r = 0x012b27 * y + 0x019a2e * v;
1089         /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
1090         g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
1091         /* B = 1.164(Y - 16) + 2.018(U - 128) */
1092         b = 0x012b27 * y + 0x0206a2 * u;
1093         
1094         *buffer++ = 0xff000000 |
1095             (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
1096             (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
1097             (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
1098     }
1099 }
1100
1101 static void
1102 fetch_scanline_yv12 (pixman_image_t *image,
1103                      int             x,
1104                      int             line,
1105                      int             width,
1106                      uint32_t *      buffer,
1107                      const uint32_t *mask,
1108                      uint32_t        mask_bits)
1109 {
1110     YV12_SETUP (image);
1111     uint8_t *y_line = YV12_Y (line);
1112     uint8_t *u_line = YV12_U (line);
1113     uint8_t *v_line = YV12_V (line);
1114     int i;
1115     
1116     for (i = 0; i < width; i++)
1117     {
1118         int16_t y, u, v;
1119         int32_t r, g, b;
1120
1121         y = y_line[x + i] - 16;
1122         u = u_line[(x + i) >> 1] - 128;
1123         v = v_line[(x + i) >> 1] - 128;
1124
1125         /* R = 1.164(Y - 16) + 1.596(V - 128) */
1126         r = 0x012b27 * y + 0x019a2e * v;
1127         /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
1128         g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
1129         /* B = 1.164(Y - 16) + 2.018(U - 128) */
1130         b = 0x012b27 * y + 0x0206a2 * u;
1131
1132         *buffer++ = 0xff000000 |
1133             (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
1134             (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
1135             (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
1136     }
1137 }
1138
1139 /**************************** Pixel wise fetching *****************************/
1140
1141 /* Despite the type, expects a uint64_t buffer */
1142 static uint64_t
1143 fetch_pixel_a2r10g10b10 (bits_image_t *image,
1144                          int              offset,
1145                          int           line)
1146 {
1147     uint32_t *bits = image->bits + line * image->rowstride;
1148     uint32_t p = READ (image, bits + offset);
1149     uint64_t a = p >> 30;
1150     uint64_t r = (p >> 20) & 0x3ff;
1151     uint64_t g = (p >> 10) & 0x3ff;
1152     uint64_t b = p & 0x3ff;
1153
1154     r = r << 6 | r >> 4;
1155     g = g << 6 | g >> 4;
1156     b = b << 6 | b >> 4;
1157
1158     a <<= 14;
1159     a |= a >> 2;
1160     a |= a >> 4;
1161     a |= a >> 8;
1162
1163     return a << 48 | r << 32 | g << 16 | b;
1164 }
1165
1166 /* Despite the type, this function expects a uint64_t buffer */
1167 static uint64_t
1168 fetch_pixel_x2r10g10b10 (bits_image_t *image,
1169                          int       offset,
1170                          int           line)
1171 {
1172     uint32_t *bits = image->bits + line * image->rowstride;
1173     uint32_t p = READ (image, bits + offset);
1174     uint64_t r = (p >> 20) & 0x3ff;
1175     uint64_t g = (p >> 10) & 0x3ff;
1176     uint64_t b = p & 0x3ff;
1177     
1178     r = r << 6 | r >> 4;
1179     g = g << 6 | g >> 4;
1180     b = b << 6 | b >> 4;
1181     
1182     return 0xffffULL << 48 | r << 32 | g << 16 | b;
1183 }
1184
1185 /* Despite the type, expects a uint64_t buffer */
1186 static uint64_t
1187 fetch_pixel_a2b10g10r10 (bits_image_t *image,
1188                          int           offset,
1189                          int           line)
1190 {
1191     uint32_t *bits = image->bits + line * image->rowstride;
1192     uint32_t p = READ (image, bits + offset);
1193     uint64_t a = p >> 30;
1194     uint64_t b = (p >> 20) & 0x3ff;
1195     uint64_t g = (p >> 10) & 0x3ff;
1196     uint64_t r = p & 0x3ff;
1197     
1198     r = r << 6 | r >> 4;
1199     g = g << 6 | g >> 4;
1200     b = b << 6 | b >> 4;
1201     
1202     a <<= 14;
1203     a |= a >> 2;
1204     a |= a >> 4;
1205     a |= a >> 8;
1206     
1207     return a << 48 | r << 32 | g << 16 | b;
1208 }
1209
1210 /* Despite the type, this function expects a uint64_t buffer */
1211 static uint64_t
1212 fetch_pixel_x2b10g10r10 (bits_image_t *image,
1213                          int           offset,
1214                          int           line)
1215 {
1216     uint32_t *bits = image->bits + line * image->rowstride;
1217     uint32_t p = READ (image, bits + offset);
1218     uint64_t b = (p >> 20) & 0x3ff;
1219     uint64_t g = (p >> 10) & 0x3ff;
1220     uint64_t r = p & 0x3ff;
1221     
1222     r = r << 6 | r >> 4;
1223     g = g << 6 | g >> 4;
1224     b = b << 6 | b >> 4;
1225     
1226     return 0xffffULL << 48 | r << 32 | g << 16 | b;
1227 }
1228
1229 static uint32_t
1230 fetch_pixel_a8r8g8b8 (bits_image_t *image,
1231                       int           offset,
1232                       int           line)
1233 {
1234     uint32_t *bits = image->bits + line * image->rowstride;
1235     return READ (image, (uint32_t *)bits + offset);
1236 }
1237
1238 static uint32_t
1239 fetch_pixel_x8r8g8b8 (bits_image_t *image,
1240                       int           offset,
1241                       int           line)
1242 {
1243     uint32_t *bits = image->bits + line * image->rowstride;
1244
1245     return READ (image, (uint32_t *)bits + offset) | 0xff000000;
1246 }
1247
1248 static uint32_t
1249 fetch_pixel_a8b8g8r8 (bits_image_t *image,
1250                       int           offset,
1251                       int           line)
1252 {
1253     uint32_t *bits = image->bits + line * image->rowstride;
1254     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
1255     
1256     return ((pixel & 0xff000000) |
1257             ((pixel >> 16) & 0xff) |
1258             (pixel & 0x0000ff00) |
1259             ((pixel & 0xff) << 16));
1260 }
1261
1262 static uint32_t
1263 fetch_pixel_x8b8g8r8 (bits_image_t *image,
1264                       int           offset,
1265                       int           line)
1266 {
1267     uint32_t *bits = image->bits + line * image->rowstride;
1268     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
1269     
1270     return ((0xff000000) |
1271             ((pixel >> 16) & 0xff) |
1272             (pixel & 0x0000ff00) |
1273             ((pixel & 0xff) << 16));
1274 }
1275
1276 static uint32_t
1277 fetch_pixel_b8g8r8a8 (bits_image_t *image,
1278                       int           offset,
1279                       int           line)
1280 {
1281     uint32_t *bits = image->bits + line * image->rowstride;
1282     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
1283     
1284     return ((pixel & 0xff000000) >> 24 |
1285             (pixel & 0x00ff0000) >> 8 |
1286             (pixel & 0x0000ff00) << 8 |
1287             (pixel & 0x000000ff) << 24);
1288 }
1289
1290 static uint32_t
1291 fetch_pixel_b8g8r8x8 (bits_image_t *image,
1292                       int           offset,
1293                       int           line)
1294 {
1295     uint32_t *bits = image->bits + line * image->rowstride;
1296     uint32_t pixel = READ (image, (uint32_t *)bits + offset);
1297     
1298     return ((0xff000000) |
1299             (pixel & 0xff000000) >> 24 |
1300             (pixel & 0x00ff0000) >> 8 |
1301             (pixel & 0x0000ff00) << 8);
1302 }
1303
1304 static uint32_t
1305 fetch_pixel_r8g8b8 (bits_image_t *image,
1306                     int           offset,
1307                     int           line)
1308 {
1309     uint32_t *bits = image->bits + line * image->rowstride;
1310     uint8_t   *pixel = ((uint8_t *) bits) + (offset * 3);
1311     
1312 #ifdef WORDS_BIGENDIAN
1313     return (0xff000000 |
1314             (READ (image, pixel + 0) << 16) |
1315             (READ (image, pixel + 1) << 8) |
1316             (READ (image, pixel + 2)));
1317 #else
1318     return (0xff000000 |
1319             (READ (image, pixel + 2) << 16) |
1320             (READ (image, pixel + 1) << 8) |
1321             (READ (image, pixel + 0)));
1322 #endif
1323 }
1324
1325 static uint32_t
1326 fetch_pixel_b8g8r8 (bits_image_t *image,
1327                     int           offset,
1328                     int           line)
1329 {
1330     uint32_t *bits = image->bits + line * image->rowstride;
1331     uint8_t   *pixel = ((uint8_t *) bits) + (offset * 3);
1332 #ifdef WORDS_BIGENDIAN
1333     return (0xff000000 |
1334             (READ (image, pixel + 2) << 16) |
1335             (READ (image, pixel + 1) << 8) |
1336             (READ (image, pixel + 0)));
1337 #else
1338     return (0xff000000 |
1339             (READ (image, pixel + 0) << 16) |
1340             (READ (image, pixel + 1) << 8) |
1341             (READ (image, pixel + 2)));
1342 #endif
1343 }
1344
1345 static uint32_t
1346 fetch_pixel_r5g6b5 (bits_image_t *image,
1347                     int           offset,
1348                     int           line)
1349 {
1350     uint32_t *bits = image->bits + line * image->rowstride;
1351     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1352     uint32_t r, g, b;
1353     
1354     r = ((pixel & 0xf800) | ((pixel & 0xe000) >> 5)) << 8;
1355     g = ((pixel & 0x07e0) | ((pixel & 0x0600) >> 6)) << 5;
1356     b = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) >> 2;
1357     
1358     return (0xff000000 | r | g | b);
1359 }
1360
1361 static uint32_t
1362 fetch_pixel_b5g6r5 (bits_image_t *image,
1363                     int           offset,
1364                     int           line)
1365 {
1366     uint32_t r, g, b;
1367     uint32_t *bits = image->bits + line * image->rowstride;
1368     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1369     
1370     b = ((pixel & 0xf800) | ((pixel & 0xe000) >> 5)) >> 8;
1371     g = ((pixel & 0x07e0) | ((pixel & 0x0600) >> 6)) << 5;
1372     r = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) << 14;
1373     
1374     return (0xff000000 | r | g | b);
1375 }
1376
1377 static uint32_t
1378 fetch_pixel_a1r5g5b5 (bits_image_t *image,
1379                       int           offset,
1380                       int           line)
1381 {
1382     uint32_t *bits = image->bits + line * image->rowstride;
1383     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1384     uint32_t a, r, g, b;
1385     
1386     a = (uint32_t) ((uint8_t) (0 - ((pixel & 0x8000) >> 15))) << 24;
1387     r = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) << 9;
1388     g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
1389     b = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) >> 2;
1390     
1391     return (a | r | g | b);
1392 }
1393
1394 static uint32_t
1395 fetch_pixel_x1r5g5b5 (bits_image_t *image,
1396                       int           offset,
1397                       int           line)
1398 {
1399     uint32_t *bits = image->bits + line * image->rowstride;
1400     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1401     uint32_t r, g, b;
1402     
1403     r = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) << 9;
1404     g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
1405     b = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) >> 2;
1406     
1407     return (0xff000000 | r | g | b);
1408 }
1409
1410 static uint32_t
1411 fetch_pixel_a1b5g5r5 (bits_image_t *image,
1412                       int           offset,
1413                       int           line)
1414 {
1415     uint32_t *bits = image->bits + line * image->rowstride;
1416     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1417     uint32_t a, r, g, b;
1418     
1419     a = (uint32_t) ((uint8_t) (0 - ((pixel & 0x8000) >> 15))) << 24;
1420     b = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) >> 7;
1421     g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
1422     r = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) << 14;
1423     
1424     return (a | r | g | b);
1425 }
1426
1427 static uint32_t
1428 fetch_pixel_x1b5g5r5 (bits_image_t *image,
1429                       int           offset,
1430                       int           line)
1431 {
1432     uint32_t *bits = image->bits + line * image->rowstride;
1433     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1434     uint32_t r, g, b;
1435     
1436     b = ((pixel & 0x7c00) | ((pixel & 0x7000) >> 5)) >> 7;
1437     g = ((pixel & 0x03e0) | ((pixel & 0x0380) >> 5)) << 6;
1438     r = ((pixel & 0x001c) | ((pixel & 0x001f) << 5)) << 14;
1439     
1440     return (0xff000000 | r | g | b);
1441 }
1442
1443 static uint32_t
1444 fetch_pixel_a4r4g4b4 (bits_image_t *image,
1445                       int           offset,
1446                       int           line)
1447 {
1448     uint32_t *bits = image->bits + line * image->rowstride;
1449     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1450     uint32_t a, r, g, b;
1451     
1452     a = ((pixel & 0xf000) | ((pixel & 0xf000) >> 4)) << 16;
1453     r = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) << 12;
1454     g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
1455     b = ((pixel & 0x000f) | ((pixel & 0x000f) << 4));
1456     
1457     return (a | r | g | b);
1458 }
1459
1460 static uint32_t
1461 fetch_pixel_x4r4g4b4 (bits_image_t *image,
1462                       int           offset,
1463                       int           line)
1464 {
1465     uint32_t *bits = image->bits + line * image->rowstride;
1466     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1467     uint32_t r, g, b;
1468     
1469     r = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) << 12;
1470     g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
1471     b = ((pixel & 0x000f) | ((pixel & 0x000f) << 4));
1472     
1473     return (0xff000000 | r | g | b);
1474 }
1475
1476 static uint32_t
1477 fetch_pixel_a4b4g4r4 (bits_image_t *image,
1478                       int           offset,
1479                       int           line)
1480 {
1481     uint32_t *bits = image->bits + line * image->rowstride;
1482     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1483     uint32_t a, r, g, b;
1484     
1485     a = ((pixel & 0xf000) | ((pixel & 0xf000) >> 4)) << 16;
1486     b = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) >> 4;
1487     g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
1488     r = ((pixel & 0x000f) | ((pixel & 0x000f) << 4)) << 16;
1489     
1490     return (a | r | g | b);
1491 }
1492
1493 static uint32_t
1494 fetch_pixel_x4b4g4r4 (bits_image_t *image,
1495                       int           offset,
1496                       int           line)
1497 {
1498     uint32_t *bits = image->bits + line * image->rowstride;
1499     uint32_t pixel = READ (image, (uint16_t *) bits + offset);
1500     uint32_t r, g, b;
1501     
1502     b = ((pixel & 0x0f00) | ((pixel & 0x0f00) >> 4)) >> 4;
1503     g = ((pixel & 0x00f0) | ((pixel & 0x00f0) >> 4)) << 8;
1504     r = ((pixel & 0x000f) | ((pixel & 0x000f) << 4)) << 16;
1505     
1506     return (0xff000000 | r | g | b);
1507 }
1508
1509 static uint32_t
1510 fetch_pixel_a8 (bits_image_t *image,
1511                 int           offset,
1512                 int           line)
1513 {
1514     uint32_t *bits = image->bits + line * image->rowstride;
1515     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
1516     
1517     return pixel << 24;
1518 }
1519
1520 static uint32_t
1521 fetch_pixel_r3g3b2 (bits_image_t *image,
1522                     int           offset,
1523                     int           line)
1524 {
1525     uint32_t *bits = image->bits + line * image->rowstride;
1526     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
1527     uint32_t r, g, b;
1528     
1529     r = ((pixel & 0xe0) |
1530          ((pixel & 0xe0) >> 3) |
1531          ((pixel & 0xc0) >> 6)) << 16;
1532     
1533     g = ((pixel & 0x1c) |
1534          ((pixel & 0x18) >> 3) |
1535          ((pixel & 0x1c) << 3)) << 8;
1536     
1537     b = (((pixel & 0x03)     ) |
1538          ((pixel & 0x03) << 2) |
1539          ((pixel & 0x03) << 4) |
1540          ((pixel & 0x03) << 6));
1541     
1542     return (0xff000000 | r | g | b);
1543 }
1544
1545 static uint32_t
1546 fetch_pixel_b2g3r3 (bits_image_t *image,
1547                     int           offset,
1548                     int           line)
1549 {
1550     uint32_t *bits = image->bits + line * image->rowstride;
1551     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
1552     uint32_t r, g, b;
1553     
1554     b = ((pixel & 0xc0)         |
1555          ((pixel & 0xc0) >> 2)  |
1556          ((pixel & 0xc0) >> 4)  |
1557          ((pixel & 0xc0) >> 6));
1558     
1559     g = ((pixel & 0x38)         |
1560          ((pixel & 0x38) >> 3)  |
1561          ((pixel & 0x30) << 2)) << 8;
1562     
1563     r = ((pixel & 0x07)         |
1564          ((pixel & 0x07) << 3)  |
1565          ((pixel & 0x06) << 6)) << 16;
1566     
1567     return (0xff000000 | r | g | b);
1568 }
1569
1570 static uint32_t
1571 fetch_pixel_a2r2g2b2 (bits_image_t *image,
1572                       int           offset,
1573                       int           line)
1574 {
1575     uint32_t *bits = image->bits + line * image->rowstride;
1576     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
1577     uint32_t a, r, g, b;
1578     
1579     a = ((pixel & 0xc0) * 0x55) << 18;
1580     r = ((pixel & 0x30) * 0x55) << 12;
1581     g = ((pixel & 0x0c) * 0x55) << 6;
1582     b = ((pixel & 0x03) * 0x55);
1583     
1584     return a | r | g | b;
1585 }
1586
1587 static uint32_t
1588 fetch_pixel_a2b2g2r2 (bits_image_t *image,
1589                       int           offset,
1590                       int           line)
1591 {
1592     uint32_t *bits = image->bits + line * image->rowstride;
1593     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
1594     uint32_t a, r, g, b;
1595     
1596     a = ((pixel & 0xc0) * 0x55) << 18;
1597     b = ((pixel & 0x30) * 0x55) >> 4;
1598     g = ((pixel & 0x0c) * 0x55) << 6;
1599     r = ((pixel & 0x03) * 0x55) << 16;
1600     
1601     return a | r | g | b;
1602 }
1603
1604 static uint32_t
1605 fetch_pixel_c8 (bits_image_t *image,
1606                 int           offset,
1607                 int           line)
1608 {
1609     uint32_t *bits = image->bits + line * image->rowstride;
1610     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
1611     const pixman_indexed_t * indexed = image->indexed;
1612     
1613     return indexed->rgba[pixel];
1614 }
1615
1616 static uint32_t
1617 fetch_pixel_x4a4 (bits_image_t *image,
1618                   int           offset,
1619                   int           line)
1620 {
1621     uint32_t *bits = image->bits + line * image->rowstride;
1622     uint32_t pixel = READ (image, (uint8_t *) bits + offset);
1623     
1624     return ((pixel & 0xf) | ((pixel & 0xf) << 4)) << 24;
1625 }
1626
1627 static uint32_t
1628 fetch_pixel_a4 (bits_image_t *image,
1629                 int           offset,
1630                 int           line)
1631 {
1632     uint32_t *bits = image->bits + line * image->rowstride;
1633     uint32_t pixel = FETCH_4 (image, bits, offset);
1634     
1635     pixel |= pixel << 4;
1636     return pixel << 24;
1637 }
1638
1639 static uint32_t
1640 fetch_pixel_r1g2b1 (bits_image_t *image,
1641                     int           offset,
1642                     int           line)
1643 {
1644     uint32_t *bits = image->bits + line * image->rowstride;
1645     uint32_t pixel = FETCH_4 (image, bits, offset);
1646     uint32_t r, g, b;
1647     
1648     r = ((pixel & 0x8) * 0xff) << 13;
1649     g = ((pixel & 0x6) * 0x55) << 7;
1650     b = ((pixel & 0x1) * 0xff);
1651     
1652     return 0xff000000 | r | g | b;
1653 }
1654
1655 static uint32_t
1656 fetch_pixel_b1g2r1 (bits_image_t *image,
1657                     int           offset,
1658                     int           line)
1659 {
1660     uint32_t *bits = image->bits + line * image->rowstride;
1661     uint32_t pixel = FETCH_4 (image, bits, offset);
1662     uint32_t r, g, b;
1663     
1664     b = ((pixel & 0x8) * 0xff) >> 3;
1665     g = ((pixel & 0x6) * 0x55) << 7;
1666     r = ((pixel & 0x1) * 0xff) << 16;
1667     
1668     return 0xff000000 | r | g | b;
1669 }
1670
1671 static uint32_t
1672 fetch_pixel_a1r1g1b1 (bits_image_t *image,
1673                       int           offset,
1674                       int           line)
1675 {
1676     uint32_t *bits = image->bits + line * image->rowstride;
1677     uint32_t pixel = FETCH_4 (image, bits, offset);
1678     uint32_t a, r, g, b;
1679     
1680     a = ((pixel & 0x8) * 0xff) << 21;
1681     r = ((pixel & 0x4) * 0xff) << 14;
1682     g = ((pixel & 0x2) * 0xff) << 7;
1683     b = ((pixel & 0x1) * 0xff);
1684     
1685     return a | r | g | b;
1686 }
1687
1688 static uint32_t
1689 fetch_pixel_a1b1g1r1 (bits_image_t *image,
1690                       int           offset,
1691                       int           line)
1692 {
1693     uint32_t *bits = image->bits + line * image->rowstride;
1694     uint32_t pixel = FETCH_4 (image, bits, offset);
1695     uint32_t a, r, g, b;
1696     
1697     a = ((pixel & 0x8) * 0xff) << 21;
1698     r = ((pixel & 0x4) * 0xff) >> 3;
1699     g = ((pixel & 0x2) * 0xff) << 7;
1700     b = ((pixel & 0x1) * 0xff) << 16;
1701     
1702     return a | r | g | b;
1703 }
1704
1705 static uint32_t
1706 fetch_pixel_c4 (bits_image_t *image,
1707                 int           offset,
1708                 int           line)
1709 {
1710     uint32_t *bits = image->bits + line * image->rowstride;
1711     uint32_t pixel = FETCH_4 (image, bits, offset);
1712     const pixman_indexed_t * indexed = image->indexed;
1713     
1714     return indexed->rgba[pixel];
1715 }
1716
1717 static uint32_t
1718 fetch_pixel_a1 (bits_image_t *image,
1719                 int           offset,
1720                 int           line)
1721 {
1722     uint32_t *bits = image->bits + line * image->rowstride;
1723     uint32_t pixel = READ (image, bits + (offset >> 5));
1724     uint32_t a;
1725     
1726 #ifdef WORDS_BIGENDIAN
1727     a = pixel >> (0x1f - (offset & 0x1f));
1728 #else
1729     a = pixel >> (offset & 0x1f);
1730 #endif
1731     a = a & 1;
1732     a |= a << 1;
1733     a |= a << 2;
1734     a |= a << 4;
1735     
1736     return a << 24;
1737 }
1738
1739 static uint32_t
1740 fetch_pixel_g1 (bits_image_t *image,
1741                 int           offset,
1742                 int           line)
1743 {
1744     uint32_t *bits = image->bits + line * image->rowstride;
1745     uint32_t pixel = READ (image, bits + (offset >> 5));
1746     const pixman_indexed_t * indexed = image->indexed;
1747     uint32_t a;
1748     
1749 #ifdef WORDS_BIGENDIAN
1750     a = pixel >> (0x1f - (offset & 0x1f));
1751 #else
1752     a = pixel >> (offset & 0x1f);
1753 #endif
1754     a = a & 1;
1755     
1756     return indexed->rgba[a];
1757 }
1758
1759 static uint32_t
1760 fetch_pixel_yuy2 (bits_image_t *image,
1761                   int           offset,
1762                   int           line)
1763 {
1764     const uint32_t *bits = image->bits + image->rowstride * line;
1765     
1766     int16_t y, u, v;
1767     int32_t r, g, b;
1768     
1769     y = ((uint8_t *) bits)[offset << 1] - 16;
1770     u = ((uint8_t *) bits)[((offset << 1) & - 4) + 1] - 128;
1771     v = ((uint8_t *) bits)[((offset << 1) & - 4) + 3] - 128;
1772     
1773     /* R = 1.164(Y - 16) + 1.596(V - 128) */
1774     r = 0x012b27 * y + 0x019a2e * v;
1775     
1776     /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
1777     g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
1778     
1779     /* B = 1.164(Y - 16) + 2.018(U - 128) */
1780     b = 0x012b27 * y + 0x0206a2 * u;
1781     
1782     return 0xff000000 |
1783         (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
1784         (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
1785         (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
1786 }
1787
1788 static uint32_t
1789 fetch_pixel_yv12 (bits_image_t *image,
1790                   int           offset,
1791                   int           line)
1792 {
1793     YV12_SETUP (image);
1794     int16_t y = YV12_Y (line)[offset] - 16;
1795     int16_t u = YV12_U (line)[offset >> 1] - 128;
1796     int16_t v = YV12_V (line)[offset >> 1] - 128;
1797     int32_t r, g, b;
1798     
1799     /* R = 1.164(Y - 16) + 1.596(V - 128) */
1800     r = 0x012b27 * y + 0x019a2e * v;
1801     
1802     /* G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128) */
1803     g = 0x012b27 * y - 0x00d0f2 * v - 0x00647e * u;
1804     
1805     /* B = 1.164(Y - 16) + 2.018(U - 128) */
1806     b = 0x012b27 * y + 0x0206a2 * u;
1807     
1808     return 0xff000000 |
1809         (r >= 0 ? r < 0x1000000 ? r         & 0xff0000 : 0xff0000 : 0) |
1810         (g >= 0 ? g < 0x1000000 ? (g >> 8)  & 0x00ff00 : 0x00ff00 : 0) |
1811         (b >= 0 ? b < 0x1000000 ? (b >> 16) & 0x0000ff : 0x0000ff : 0);
1812 }
1813
1814 /*********************************** Store ************************************/
1815
1816 #define SPLIT_A(v)              \
1817     uint32_t a = ((v) >> 24),   \
1818         r = ((v) >> 16) & 0xff, \
1819         g = ((v) >> 8) & 0xff,  \
1820         b = (v) & 0xff
1821
1822 #define SPLIT(v)                     \
1823     uint32_t r = ((v) >> 16) & 0xff, \
1824         g = ((v) >> 8) & 0xff,       \
1825         b = (v) & 0xff
1826
1827 static void
1828 store_scanline_a2r10g10b10 (bits_image_t *  image,
1829                             int             x,
1830                             int             y,
1831                             int             width,
1832                             const uint32_t *v)
1833 {
1834     uint32_t *bits = image->bits + image->rowstride * y;
1835     uint32_t *pixel = bits + x;
1836     uint64_t *values = (uint64_t *)v;
1837     int i;
1838     
1839     for (i = 0; i < width; ++i)
1840     {
1841         WRITE (image, pixel++,
1842                ((values[i] >> 32) & 0xc0000000) |
1843                ((values[i] >> 18) & 0x3ff00000) |
1844                ((values[i] >> 12) & 0xffc00) | 
1845                ((values[i] >> 6) & 0x3ff));    
1846     }
1847 }
1848
1849 static void
1850 store_scanline_x2r10g10b10 (bits_image_t *  image,
1851                             int             x,
1852                             int             y,
1853                             int             width,
1854                             const uint32_t *v)
1855 {
1856     uint32_t *bits = image->bits + image->rowstride * y;
1857     uint64_t *values = (uint64_t *)v;
1858     uint32_t *pixel = bits + x;
1859     int i;
1860     
1861     for (i = 0; i < width; ++i)
1862     {
1863         WRITE (image, pixel++,
1864                ((values[i] >> 18) & 0x3ff00000) | 
1865                ((values[i] >> 12) & 0xffc00) |
1866                ((values[i] >> 6) & 0x3ff));
1867     }
1868 }
1869
1870 static void
1871 store_scanline_a2b10g10r10 (bits_image_t *  image,
1872                             int             x,
1873                             int             y,
1874                             int             width,
1875                             const uint32_t *v)
1876 {
1877     uint32_t *bits = image->bits + image->rowstride * y;
1878     uint32_t *pixel = bits + x;
1879     uint64_t *values = (uint64_t *)v;
1880     int i;
1881     
1882     for (i = 0; i < width; ++i)
1883     {
1884         WRITE (image, pixel++,
1885                ((values[i] >> 32) & 0xc0000000) |
1886                ((values[i] >> 38) & 0x3ff) |
1887                ((values[i] >> 12) & 0xffc00) |
1888                ((values[i] << 14) & 0x3ff00000));
1889     }
1890 }
1891
1892 static void
1893 store_scanline_x2b10g10r10 (bits_image_t *  image,
1894                             int             x,
1895                             int             y,
1896                             int             width,
1897                             const uint32_t *v)
1898 {
1899     uint32_t *bits = image->bits + image->rowstride * y;
1900     uint64_t *values = (uint64_t *)v;
1901     uint32_t *pixel = bits + x;
1902     int i;
1903     
1904     for (i = 0; i < width; ++i)
1905     {
1906         WRITE (image, pixel++,
1907                ((values[i] >> 38) & 0x3ff) |
1908                ((values[i] >> 12) & 0xffc00) |
1909                ((values[i] << 14) & 0x3ff00000));
1910     }
1911 }
1912
1913 static void
1914 store_scanline_a8r8g8b8 (bits_image_t *  image,
1915                          int             x,
1916                          int             y,
1917                          int             width,
1918                          const uint32_t *values)
1919 {
1920     uint32_t *bits = image->bits + image->rowstride * y;
1921     
1922     MEMCPY_WRAPPED (image, ((uint32_t *)bits) + x, values,
1923                     width * sizeof(uint32_t));
1924 }
1925
1926 static void
1927 store_scanline_x8r8g8b8 (bits_image_t *  image,
1928                          int             x,
1929                          int             y,
1930                          int             width,
1931                          const uint32_t *values)
1932 {
1933     uint32_t *bits = image->bits + image->rowstride * y;
1934     uint32_t *pixel = (uint32_t *)bits + x;
1935     int i;
1936     
1937     for (i = 0; i < width; ++i)
1938         WRITE (image, pixel++, values[i] & 0xffffff);
1939 }
1940
1941 static void
1942 store_scanline_a8b8g8r8 (bits_image_t *  image,
1943                          int             x,
1944                          int             y,
1945                          int             width,
1946                          const uint32_t *values)
1947 {
1948     uint32_t *bits = image->bits + image->rowstride * y;
1949     uint32_t *pixel = (uint32_t *)bits + x;
1950     int i;
1951     
1952     for (i = 0; i < width; ++i)
1953     {
1954         WRITE (image, pixel++,
1955                (values[i] & 0xff00ff00)         |
1956                ((values[i] >> 16) & 0xff)       |
1957                ((values[i] & 0xff) << 16));
1958     }
1959 }
1960
1961 static void
1962 store_scanline_x8b8g8r8 (bits_image_t *  image,
1963                          int             x,
1964                          int             y,
1965                          int             width,
1966                          const uint32_t *values)
1967 {
1968     uint32_t *bits = image->bits + image->rowstride * y;
1969     uint32_t *pixel = (uint32_t *)bits + x;
1970     int i;
1971     
1972     for (i = 0; i < width; ++i)
1973     {
1974         WRITE (image, pixel++,
1975                (values[i] & 0x0000ff00)         |
1976                ((values[i] >> 16) & 0xff)       |
1977                ((values[i] & 0xff) << 16));
1978     }
1979 }
1980
1981 static void
1982 store_scanline_b8g8r8a8 (bits_image_t *  image,
1983                          int             x,
1984                          int             y,
1985                          int             width,
1986                          const uint32_t *values)
1987 {
1988     uint32_t *bits = image->bits + image->rowstride * y;
1989     uint32_t *pixel = (uint32_t *)bits + x;
1990     int i;
1991     
1992     for (i = 0; i < width; ++i)
1993     {
1994         WRITE (image, pixel++,
1995                ((values[i] >> 24) & 0x000000ff) |
1996                ((values[i] >>  8) & 0x0000ff00) |
1997                ((values[i] <<  8) & 0x00ff0000) |
1998                ((values[i] << 24) & 0xff000000));
1999     }
2000 }
2001
2002 static void
2003 store_scanline_b8g8r8x8 (bits_image_t *  image,
2004                          int             x,
2005                          int             y,
2006                          int             width,
2007                          const uint32_t *values)
2008 {
2009     uint32_t *bits = image->bits + image->rowstride * y;
2010     uint32_t *pixel = (uint32_t *)bits + x;
2011     int i;
2012     
2013     for (i = 0; i < width; ++i)
2014     {
2015         WRITE (image, pixel++,
2016                ((values[i] >>  8) & 0x0000ff00) |
2017                ((values[i] <<  8) & 0x00ff0000) |
2018                ((values[i] << 24) & 0xff000000));
2019     }
2020 }
2021
2022 static void
2023 store_scanline_r8g8b8 (bits_image_t *  image,
2024                        int             x,
2025                        int             y,
2026                        int             width,
2027                        const uint32_t *values)
2028 {
2029     uint32_t *bits = image->bits + image->rowstride * y;
2030     uint8_t *pixel = ((uint8_t *) bits) + 3 * x;
2031     int i;
2032     
2033     for (i = 0; i < width; ++i)
2034     {
2035         uint32_t val = values[i];
2036         
2037 #ifdef WORDS_BIGENDIAN
2038         WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
2039         WRITE (image, pixel++, (val & 0x0000ff00) >>  8);
2040         WRITE (image, pixel++, (val & 0x000000ff) >>  0);
2041 #else
2042         WRITE (image, pixel++, (val & 0x000000ff) >>  0);
2043         WRITE (image, pixel++, (val & 0x0000ff00) >>  8);
2044         WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
2045 #endif
2046     }
2047 }
2048
2049 static void
2050 store_scanline_b8g8r8 (bits_image_t *  image,
2051                        int             x,
2052                        int             y,
2053                        int             width,
2054                        const uint32_t *values)
2055 {
2056     uint32_t *bits = image->bits + image->rowstride * y;
2057     uint8_t *pixel = ((uint8_t *) bits) + 3 * x;
2058     int i;
2059     
2060     for (i = 0; i < width; ++i)
2061     {
2062         uint32_t val = values[i];
2063         
2064 #ifdef WORDS_BIGENDIAN
2065         WRITE (image, pixel++, (val & 0x000000ff) >>  0);
2066         WRITE (image, pixel++, (val & 0x0000ff00) >>  8);
2067         WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
2068 #else
2069         WRITE (image, pixel++, (val & 0x00ff0000) >> 16);
2070         WRITE (image, pixel++, (val & 0x0000ff00) >>  8);
2071         WRITE (image, pixel++, (val & 0x000000ff) >>  0);
2072 #endif
2073     }
2074 }
2075
2076 static void
2077 store_scanline_r5g6b5 (bits_image_t *  image,
2078                        int             x,
2079                        int             y,
2080                        int             width,
2081                        const uint32_t *values)
2082 {
2083     uint32_t *bits = image->bits + image->rowstride * y;
2084     uint16_t *pixel = ((uint16_t *) bits) + x;
2085     int i;
2086     
2087     for (i = 0; i < width; ++i)
2088     {
2089         uint32_t s = values[i];
2090         
2091         WRITE (image, pixel++,
2092                ((s >> 3) & 0x001f) |
2093                ((s >> 5) & 0x07e0) |
2094                ((s >> 8) & 0xf800));
2095     }
2096 }
2097
2098 static void
2099 store_scanline_b5g6r5 (bits_image_t *  image,
2100                        int             x,
2101                        int             y,
2102                        int             width,
2103                        const uint32_t *values)
2104 {
2105     uint32_t *bits = image->bits + image->rowstride * y;
2106     uint16_t  *pixel = ((uint16_t *) bits) + x;
2107     int i;
2108     
2109     for (i = 0; i < width; ++i)
2110     {
2111         SPLIT (values[i]);
2112         
2113         WRITE (image, pixel++,
2114                ((b << 8) & 0xf800) |
2115                ((g << 3) & 0x07e0) |
2116                ((r >> 3)         ));
2117     }
2118 }
2119
2120 static void
2121 store_scanline_a1r5g5b5 (bits_image_t *  image,
2122                          int             x,
2123                          int             y,
2124                          int             width,
2125                          const uint32_t *values)
2126 {
2127     uint32_t *bits = image->bits + image->rowstride * y;
2128     uint16_t  *pixel = ((uint16_t *) bits) + x;
2129     int i;
2130     
2131     for (i = 0; i < width; ++i)
2132     {
2133         SPLIT_A (values[i]);
2134         
2135         WRITE (image, pixel++,
2136                ((a << 8) & 0x8000) |
2137                ((r << 7) & 0x7c00) |
2138                ((g << 2) & 0x03e0) |
2139                ((b >> 3)         ));
2140     }
2141 }
2142
2143 static void
2144 store_scanline_x1r5g5b5 (bits_image_t *  image,
2145                          int             x,
2146                          int             y,
2147                          int             width,
2148                          const uint32_t *values)
2149 {
2150     uint32_t *bits = image->bits + image->rowstride * y;
2151     uint16_t  *pixel = ((uint16_t *) bits) + x;
2152     int i;
2153     
2154     for (i = 0; i < width; ++i)
2155     {
2156         SPLIT (values[i]);
2157         
2158         WRITE (image, pixel++,
2159                ((r << 7) & 0x7c00) |
2160                ((g << 2) & 0x03e0) |
2161                ((b >> 3)         ));
2162     }
2163 }
2164
2165 static void
2166 store_scanline_a1b5g5r5 (bits_image_t *  image,
2167                          int             x,
2168                          int             y,
2169                          int             width,
2170                          const uint32_t *values)
2171 {
2172     uint32_t *bits = image->bits + image->rowstride * y;
2173     uint16_t  *pixel = ((uint16_t *) bits) + x;
2174     int i;
2175     
2176     for (i = 0; i < width; ++i)
2177     {
2178         SPLIT_A (values[i]);
2179         
2180         WRITE (image, pixel++,
2181                ((a << 8) & 0x8000) |
2182                ((b << 7) & 0x7c00) |
2183                ((g << 2) & 0x03e0) |
2184                ((r >> 3)         ));
2185     }
2186 }
2187
2188 static void
2189 store_scanline_x1b5g5r5 (bits_image_t *  image,
2190                          int             x,
2191                          int             y,
2192                          int             width,
2193                          const uint32_t *values)
2194 {
2195     uint32_t *bits = image->bits + image->rowstride * y;
2196     uint16_t  *pixel = ((uint16_t *) bits) + x;
2197     int i;
2198     
2199     for (i = 0; i < width; ++i)
2200     {
2201         SPLIT (values[i]);
2202         
2203         WRITE (image, pixel++, ((b << 7) & 0x7c00) |
2204                ((g << 2) & 0x03e0) |
2205                ((r >> 3)         ));
2206     }
2207 }
2208
2209 static void
2210 store_scanline_a4r4g4b4 (bits_image_t *  image,
2211                          int             x,
2212                          int             y,
2213                          int             width,
2214                          const uint32_t *values)
2215 {
2216     uint32_t *bits = image->bits + image->rowstride * y;
2217     uint16_t  *pixel = ((uint16_t *) bits) + x;
2218     int i;
2219     
2220     for (i = 0; i < width; ++i)
2221     {
2222         SPLIT_A (values[i]);
2223         
2224         WRITE (image, pixel++,
2225                ((a << 8) & 0xf000) |
2226                ((r << 4) & 0x0f00) |
2227                ((g     ) & 0x00f0) |
2228                ((b >> 4)         ));
2229     }
2230 }
2231
2232 static void
2233 store_scanline_x4r4g4b4 (bits_image_t *  image,
2234                          int             x,
2235                          int             y,
2236                          int             width,
2237                          const uint32_t *values)
2238 {
2239     uint32_t *bits = image->bits + image->rowstride * y;
2240     uint16_t  *pixel = ((uint16_t *) bits) + x;
2241     int i;
2242     
2243     for (i = 0; i < width; ++i)
2244     {
2245         SPLIT (values[i]);
2246         
2247         WRITE (image, pixel++,
2248                ((r << 4) & 0x0f00) |
2249                ((g     ) & 0x00f0) |
2250                ((b >> 4)         ));
2251     }
2252 }
2253
2254 static void
2255 store_scanline_a4b4g4r4 (bits_image_t *  image,
2256                          int             x,
2257                          int             y,
2258                          int             width,
2259                          const uint32_t *values)
2260 {
2261     uint32_t *bits = image->bits + image->rowstride * y;
2262     uint16_t  *pixel = ((uint16_t *) bits) + x;
2263     int i;
2264     
2265     for (i = 0; i < width; ++i)
2266     {
2267         SPLIT_A (values[i]);
2268         WRITE (image, pixel++, ((a << 8) & 0xf000) |
2269                ((b << 4) & 0x0f00) |
2270                ((g     ) & 0x00f0) |
2271                ((r >> 4)         ));
2272     }
2273 }
2274
2275 static void
2276 store_scanline_x4b4g4r4 (bits_image_t *  image,
2277                          int             x,
2278                          int             y,
2279                          int             width,
2280                          const uint32_t *values)
2281 {
2282     uint32_t *bits = image->bits + image->rowstride * y;
2283     uint16_t  *pixel = ((uint16_t *) bits) + x;
2284     int i;
2285     
2286     for (i = 0; i < width; ++i)
2287     {
2288         SPLIT (values[i]);
2289         
2290         WRITE (image, pixel++,
2291                ((b << 4) & 0x0f00) |
2292                ((g     ) & 0x00f0) |
2293                ((r >> 4)         ));
2294     }
2295 }
2296
2297 static void
2298 store_scanline_a8 (bits_image_t *  image,
2299                    int             x,
2300                    int             y,
2301                    int             width,
2302                    const uint32_t *values)
2303 {
2304     uint32_t *bits = image->bits + image->rowstride * y;
2305     uint8_t   *pixel = ((uint8_t *) bits) + x;
2306     int i;
2307     
2308     for (i = 0; i < width; ++i)
2309     {
2310         WRITE (image, pixel++, values[i] >> 24);
2311     }
2312 }
2313
2314 static void
2315 store_scanline_r3g3b2 (bits_image_t *  image,
2316                        int             x,
2317                        int             y,
2318                        int             width,
2319                        const uint32_t *values)
2320 {
2321     uint32_t *bits = image->bits + image->rowstride * y;
2322     uint8_t   *pixel = ((uint8_t *) bits) + x;
2323     int i;
2324     
2325     for (i = 0; i < width; ++i)
2326     {
2327         SPLIT (values[i]);
2328         
2329         WRITE (image, pixel++,
2330                ((r     ) & 0xe0) |
2331                ((g >> 3) & 0x1c) |
2332                ((b >> 6)       ));
2333     }
2334 }
2335
2336 static void
2337 store_scanline_b2g3r3 (bits_image_t *  image,
2338                        int             x,
2339                        int             y,
2340                        int             width,
2341                        const uint32_t *values)
2342 {
2343     uint32_t *bits = image->bits + image->rowstride * y;
2344     uint8_t   *pixel = ((uint8_t *) bits) + x;
2345     int i;
2346     
2347     for (i = 0; i < width; ++i)
2348     {
2349         SPLIT (values[i]);
2350         
2351         WRITE (image, pixel++,
2352                ((b     ) & 0xc0) |
2353                ((g >> 2) & 0x38) |
2354                ((r >> 5)       ));
2355     }
2356 }
2357
2358 static void
2359 store_scanline_a2r2g2b2 (bits_image_t *  image,
2360                          int             x,
2361                          int             y,
2362                          int             width,
2363                          const uint32_t *values)
2364 {
2365     uint32_t *bits = image->bits + image->rowstride * y;
2366     uint8_t   *pixel = ((uint8_t *) bits) + x;
2367     int i;
2368     
2369     for (i = 0; i < width; ++i)
2370     {
2371         SPLIT_A (values[i]);
2372         
2373         WRITE (image, pixel++,
2374                ((a     ) & 0xc0) |
2375                ((r >> 2) & 0x30) |
2376                ((g >> 4) & 0x0c) |
2377                ((b >> 6)       ));
2378     }
2379 }
2380
2381 static void
2382 store_scanline_a2b2g2r2 (bits_image_t *  image,
2383                          int             x,
2384                          int             y,
2385                          int             width,
2386                          const uint32_t *values)
2387 {
2388     uint32_t *bits = image->bits + image->rowstride * y;
2389     uint8_t   *pixel = ((uint8_t *) bits) + x;
2390     int i;
2391     
2392     for (i = 0; i < width; ++i)
2393     {
2394         SPLIT_A (values[i]);
2395         
2396         *(pixel++) =
2397             ((a     ) & 0xc0) |
2398             ((b >> 2) & 0x30) |
2399             ((g >> 4) & 0x0c) |
2400             ((r >> 6)       );
2401     }
2402 }
2403
2404 static void
2405 store_scanline_c8 (bits_image_t *  image,
2406                    int             x,
2407                    int             y,
2408                    int             width,
2409                    const uint32_t *values)
2410 {
2411     uint32_t *bits = image->bits + image->rowstride * y;
2412     uint8_t *pixel = ((uint8_t *) bits) + x;
2413     const pixman_indexed_t *indexed = image->indexed;
2414     int i;
2415     
2416     for (i = 0; i < width; ++i)
2417         WRITE (image, pixel++, RGB24_TO_ENTRY (indexed,values[i]));
2418 }
2419
2420 static void
2421 store_scanline_x4a4 (bits_image_t *  image,
2422                      int             x,
2423                      int             y,
2424                      int             width,
2425                      const uint32_t *values)
2426 {
2427     uint32_t *bits = image->bits + image->rowstride * y;
2428     uint8_t   *pixel = ((uint8_t *) bits) + x;
2429     int i;
2430
2431     for (i = 0; i < width; ++i)
2432         WRITE (image, pixel++, values[i] >> 28);
2433 }
2434
2435 #define STORE_8(img,l,o,v)  (WRITE (img, (uint8_t *)(l) + ((o) >> 3), (v)))
2436 #ifdef WORDS_BIGENDIAN
2437
2438 #define STORE_4(img,l,o,v)                                              \
2439     do                                                                  \
2440     {                                                                   \
2441         int bo = 4 * (o);                                               \
2442         STORE_8 (img, l, bo, (bo & 4 ?                                  \
2443                               (FETCH_8 (img, l, bo) & 0xf0) | (v) :     \
2444                               (FETCH_8 (img, l, bo) & 0x0f) | ((v) << 4))); \
2445     } while (0)
2446 #else
2447
2448 #define STORE_4(img,l,o,v)                                              \
2449     do                                                                  \
2450     {                                                                   \
2451         int bo = 4 * (o);                                               \
2452         STORE_8 (img, l, bo, (bo & 4 ?                                  \
2453                               (FETCH_8 (img, l, bo) & 0x0f) | ((v) << 4) : \
2454                               (FETCH_8 (img, l, bo) & 0xf0) | (v)));    \
2455     } while (0)
2456 #endif
2457
2458 static void
2459 store_scanline_a4 (bits_image_t *  image,
2460                    int             x,
2461                    int             y,
2462                    int             width,
2463                    const uint32_t *values)
2464 {
2465     uint32_t *bits = image->bits + image->rowstride * y;
2466     int i;
2467
2468     for (i = 0; i < width; ++i)
2469         STORE_4 (image, bits, i + x, values[i] >> 28);
2470 }
2471
2472 static void
2473 store_scanline_r1g2b1 (bits_image_t *  image,
2474                        int             x,
2475                        int             y,
2476                        int             width,
2477                        const uint32_t *values)
2478 {
2479     uint32_t *bits = image->bits + image->rowstride * y;
2480     int i;
2481     
2482     for (i = 0; i < width; ++i)
2483     {
2484         uint32_t pixel;
2485         
2486         SPLIT (values[i]);
2487         pixel = (((r >> 4) & 0x8) |
2488                  ((g >> 5) & 0x6) |
2489                  ((b >> 7)      ));
2490         STORE_4 (image, bits, i + x, pixel);
2491     }
2492 }
2493
2494 static void
2495 store_scanline_b1g2r1 (bits_image_t *  image,
2496                        int             x,
2497                        int             y,
2498                        int             width,
2499                        const uint32_t *values)
2500 {
2501     uint32_t *bits = image->bits + image->rowstride * y;
2502     int i;
2503
2504     for (i = 0; i < width; ++i)
2505     {
2506         uint32_t pixel;
2507
2508         SPLIT (values[i]);
2509         pixel = (((b >> 4) & 0x8) |
2510                  ((g >> 5) & 0x6) |
2511                  ((r >> 7)      ));
2512         STORE_4 (image, bits, i + x, pixel);
2513     }
2514 }
2515
2516 static void
2517 store_scanline_a1r1g1b1 (bits_image_t *  image,
2518                          int             x,
2519                          int             y,
2520                          int             width,
2521                          const uint32_t *values)
2522 {
2523     uint32_t *bits = image->bits + image->rowstride * y;
2524     int i;
2525
2526     for (i = 0; i < width; ++i)
2527     {
2528         uint32_t pixel;
2529
2530         SPLIT_A (values[i]);
2531         pixel = (((a >> 4) & 0x8) |
2532                  ((r >> 5) & 0x4) |
2533                  ((g >> 6) & 0x2) |
2534                  ((b >> 7)      ));
2535
2536         STORE_4 (image, bits, i + x, pixel);
2537     }
2538 }
2539
2540 static void
2541 store_scanline_a1b1g1r1 (bits_image_t *  image,
2542                          int             x,
2543                          int             y,
2544                          int             width,
2545                          const uint32_t *values)
2546 {
2547     uint32_t *bits = image->bits + image->rowstride * y;
2548     int i;
2549
2550     for (i = 0; i < width; ++i)
2551     {
2552         uint32_t pixel;
2553
2554         SPLIT_A (values[i]);
2555         pixel = (((a >> 4) & 0x8) |
2556                  ((b >> 5) & 0x4) |
2557                  ((g >> 6) & 0x2) |
2558                  ((r >> 7)      ));
2559
2560         STORE_4 (image, bits, i + x, pixel);
2561     }
2562 }
2563
2564 static void
2565 store_scanline_c4 (bits_image_t *  image,
2566                    int             x,
2567                    int             y,
2568                    int             width,
2569                    const uint32_t *values)
2570 {
2571     uint32_t *bits = image->bits + image->rowstride * y;
2572     const pixman_indexed_t *indexed = image->indexed;
2573     int i;
2574     
2575     for (i = 0; i < width; ++i)
2576     {
2577         uint32_t pixel;
2578         
2579         pixel = RGB24_TO_ENTRY (indexed, values[i]);
2580         STORE_4 (image, bits, i + x, pixel);
2581     }
2582 }
2583
2584 static void
2585 store_scanline_a1 (bits_image_t *  image,
2586                    int             x,
2587                    int             y,
2588                    int             width,
2589                    const uint32_t *values)
2590 {
2591     uint32_t *bits = image->bits + image->rowstride * y;
2592     int i;
2593     
2594     for (i = 0; i < width; ++i)
2595     {
2596         uint32_t  *pixel = ((uint32_t *) bits) + ((i + x) >> 5);
2597         uint32_t mask, v;
2598         
2599 #ifdef WORDS_BIGENDIAN
2600         mask = 1 << (0x1f - ((i + x) & 0x1f));
2601 #else
2602         mask = 1 << ((i + x) & 0x1f);
2603 #endif
2604         v = values[i] & 0x80000000 ? mask : 0;
2605         
2606         WRITE (image, pixel, (READ (image, pixel) & ~mask) | v);
2607     }
2608 }
2609
2610 static void
2611 store_scanline_g1 (bits_image_t *  image,
2612                    int             x,
2613                    int             y,
2614                    int             width,
2615                    const uint32_t *values)
2616 {
2617     uint32_t *bits = image->bits + image->rowstride * y;
2618     const pixman_indexed_t *indexed = image->indexed;
2619     int i;
2620     
2621     for (i = 0; i < width; ++i)
2622     {
2623         uint32_t  *pixel = ((uint32_t *) bits) + ((i + x) >> 5);
2624         uint32_t mask, v;
2625         
2626 #ifdef WORDS_BIGENDIAN
2627         mask = 1 << (0x1f - ((i + x) & 0x1f));
2628 #else
2629         mask = 1 << ((i + x) & 0x1f);
2630 #endif
2631         v = RGB24_TO_ENTRY_Y (indexed, values[i]) ? mask : 0;
2632         
2633         WRITE (image, pixel, (READ (image, pixel) & ~mask) | v);
2634     }
2635 }
2636
2637 /*
2638  * Contracts a 64bpp image to 32bpp and then stores it using a regular 32-bit
2639  * store proc. Despite the type, this function expects a uint64_t buffer.
2640  */
2641 static void
2642 store_scanline_generic_64 (bits_image_t *  image,
2643                            int             x,
2644                            int             y,
2645                            int             width,
2646                            const uint32_t *values)
2647 {
2648     uint32_t *argb8_pixels;
2649     
2650     assert (image->common.type == BITS);
2651     
2652     argb8_pixels = pixman_malloc_ab (width, sizeof(uint32_t));
2653     if (!argb8_pixels)
2654         return;
2655     
2656     /* Contract the scanline.  We could do this in place if values weren't
2657      * const.
2658      */
2659     pixman_contract (argb8_pixels, (uint64_t *)values, width);
2660     
2661     image->store_scanline_raw_32 (image, x, y, width, argb8_pixels);
2662     
2663     free (argb8_pixels);
2664 }
2665
2666 /* Despite the type, this function expects both buffer
2667  * and mask to be uint64_t
2668  */
2669 static void
2670 fetch_scanline_generic_64 (pixman_image_t *image,
2671                            int             x,
2672                            int             y,
2673                            int             width,
2674                            uint32_t *      buffer,
2675                            const uint32_t *mask,
2676                            uint32_t        mask_bits)
2677 {
2678     /* Fetch the pixels into the first half of buffer and then expand them in
2679      * place.
2680      */
2681     image->bits.fetch_scanline_raw_32 (image, x, y, width, buffer, NULL, 0);
2682     
2683     pixman_expand ((uint64_t *)buffer, buffer, image->bits.format, width);
2684 }
2685
2686 /* Despite the type, this function expects a uint64_t *buffer */
2687 static uint64_t
2688 fetch_pixel_generic_64 (bits_image_t *image,
2689                         int           offset,
2690                         int           line)
2691 {
2692     uint32_t pixel32 = image->fetch_pixel_raw_32 (image, offset, line);
2693     uint64_t result;
2694     
2695     pixman_expand ((uint64_t *)&result, &pixel32, image->format, 1);
2696
2697     return result;
2698 }
2699
2700 /*
2701  * XXX: The transformed fetch path only works at 32-bpp so far.  When all
2702  * paths have wide versions, this can be removed.
2703  *
2704  * WARNING: This function loses precision!
2705  */
2706 static uint32_t
2707 fetch_pixel_generic_lossy_32 (bits_image_t *image,
2708                               int           offset,
2709                               int           line)
2710 {
2711     uint64_t pixel64 = image->fetch_pixel_raw_64 (image, offset, line);
2712     uint32_t result;
2713     
2714     pixman_contract (&result, &pixel64, 1);
2715
2716     return result;
2717 }
2718
2719 typedef struct
2720 {
2721     pixman_format_code_t        format;
2722     fetch_scanline_t            fetch_scanline_raw_32;
2723     fetch_scanline_t            fetch_scanline_raw_64;
2724     fetch_pixel_32_t            fetch_pixel_raw_32;
2725     fetch_pixel_64_t            fetch_pixel_raw_64;
2726     store_scanline_t            store_scanline_raw_32;
2727     store_scanline_t            store_scanline_raw_64;
2728 } format_info_t;
2729
2730 #define FORMAT_INFO(format)                                             \
2731     {                                                                   \
2732         PIXMAN_ ## format,                                              \
2733             fetch_scanline_ ## format,                                  \
2734             fetch_scanline_generic_64,                                  \
2735             fetch_pixel_ ## format, fetch_pixel_generic_64,             \
2736             store_scanline_ ## format, store_scanline_generic_64        \
2737     }
2738
2739 static const format_info_t accessors[] =
2740 {
2741 /* 32 bpp formats */
2742     FORMAT_INFO (a8r8g8b8),
2743     FORMAT_INFO (x8r8g8b8),
2744     FORMAT_INFO (a8b8g8r8),
2745     FORMAT_INFO (x8b8g8r8),
2746     FORMAT_INFO (b8g8r8a8),
2747     FORMAT_INFO (b8g8r8x8),
2748     
2749 /* 24bpp formats */
2750     FORMAT_INFO (r8g8b8),
2751     FORMAT_INFO (b8g8r8),
2752     
2753 /* 16bpp formats */
2754     FORMAT_INFO (r5g6b5),
2755     FORMAT_INFO (b5g6r5),
2756     
2757     FORMAT_INFO (a1r5g5b5),
2758     FORMAT_INFO (x1r5g5b5),
2759     FORMAT_INFO (a1b5g5r5),
2760     FORMAT_INFO (x1b5g5r5),
2761     FORMAT_INFO (a4r4g4b4),
2762     FORMAT_INFO (x4r4g4b4),
2763     FORMAT_INFO (a4b4g4r4),
2764     FORMAT_INFO (x4b4g4r4),
2765     
2766 /* 8bpp formats */
2767     FORMAT_INFO (a8),
2768     FORMAT_INFO (r3g3b2),
2769     FORMAT_INFO (b2g3r3),
2770     FORMAT_INFO (a2r2g2b2),
2771     FORMAT_INFO (a2b2g2r2),
2772     
2773     FORMAT_INFO (c8),
2774     
2775 #define fetch_scanline_g8 fetch_scanline_c8
2776 #define fetch_pixel_g8 fetch_pixel_c8
2777 #define store_scanline_g8 store_scanline_c8
2778     FORMAT_INFO (g8),
2779     
2780 #define fetch_scanline_x4c4 fetch_scanline_c8
2781 #define fetch_pixel_x4c4 fetch_pixel_c8
2782 #define store_scanline_x4c4 store_scanline_c8
2783     FORMAT_INFO (x4c4),
2784     
2785 #define fetch_scanline_x4g4 fetch_scanline_c8
2786 #define fetch_pixel_x4g4 fetch_pixel_c8
2787 #define store_scanline_x4g4 store_scanline_c8
2788     FORMAT_INFO (x4g4),
2789     
2790     FORMAT_INFO (x4a4),
2791     
2792 /* 4bpp formats */
2793     FORMAT_INFO (a4),
2794     FORMAT_INFO (r1g2b1),
2795     FORMAT_INFO (b1g2r1),
2796     FORMAT_INFO (a1r1g1b1),
2797     FORMAT_INFO (a1b1g1r1),
2798     
2799     FORMAT_INFO (c4),
2800     
2801 #define fetch_scanline_g4 fetch_scanline_c4
2802 #define fetch_pixel_g4 fetch_pixel_c4
2803 #define store_scanline_g4 store_scanline_c4
2804     FORMAT_INFO (g4),
2805     
2806 /* 1bpp formats */
2807     FORMAT_INFO (a1),
2808     FORMAT_INFO (g1),
2809     
2810 /* Wide formats */
2811     
2812     { PIXMAN_a2r10g10b10,
2813       NULL, fetch_scanline_a2r10g10b10,
2814       fetch_pixel_generic_lossy_32, fetch_pixel_a2r10g10b10,
2815       NULL, store_scanline_a2r10g10b10 },
2816     
2817     { PIXMAN_x2r10g10b10,
2818       NULL, fetch_scanline_x2r10g10b10,
2819       fetch_pixel_generic_lossy_32, fetch_pixel_x2r10g10b10,
2820       NULL, store_scanline_x2r10g10b10 },
2821     
2822     { PIXMAN_a2b10g10r10,
2823       NULL, fetch_scanline_a2b10g10r10,
2824       fetch_pixel_generic_lossy_32, fetch_pixel_a2b10g10r10,
2825       NULL, store_scanline_a2b10g10r10 },
2826     
2827     { PIXMAN_x2b10g10r10,
2828       NULL, fetch_scanline_x2b10g10r10,
2829       fetch_pixel_generic_lossy_32, fetch_pixel_x2b10g10r10,
2830       NULL, store_scanline_x2b10g10r10 },
2831     
2832 /* YUV formats */
2833     { PIXMAN_yuy2,
2834       fetch_scanline_yuy2, fetch_scanline_generic_64,
2835       fetch_pixel_yuy2, fetch_pixel_generic_64,
2836       NULL, NULL },
2837     
2838     { PIXMAN_yv12,
2839       fetch_scanline_yv12, fetch_scanline_generic_64,
2840       fetch_pixel_yv12, fetch_pixel_generic_64,
2841       NULL, NULL },
2842     
2843     { PIXMAN_null },
2844 };
2845
2846 static void
2847 setup_accessors (bits_image_t *image)
2848 {
2849     const format_info_t *info = accessors;
2850     
2851     while (info->format != PIXMAN_null)
2852     {
2853         if (info->format == image->format)
2854         {
2855             image->fetch_scanline_raw_32 = info->fetch_scanline_raw_32;
2856             image->fetch_scanline_raw_64 = info->fetch_scanline_raw_64;
2857             image->fetch_pixel_raw_32 = info->fetch_pixel_raw_32;
2858             image->fetch_pixel_raw_64 = info->fetch_pixel_raw_64;
2859             image->store_scanline_raw_32 = info->store_scanline_raw_32;
2860             image->store_scanline_raw_64 = info->store_scanline_raw_64;
2861             
2862             return;
2863         }
2864         
2865         info++;
2866     }
2867 }
2868
2869 #ifndef PIXMAN_FB_ACCESSORS
2870 void
2871 _pixman_bits_image_setup_raw_accessors_accessors (bits_image_t *image);
2872
2873 void
2874 _pixman_bits_image_setup_raw_accessors (bits_image_t *image)
2875 {
2876     if (image->read_func || image->write_func)
2877         _pixman_bits_image_setup_raw_accessors_accessors (image);
2878     else
2879         setup_accessors (image);
2880 }
2881
2882 #else
2883
2884 void
2885 _pixman_bits_image_setup_raw_accessors_accessors (bits_image_t *image)
2886 {
2887     setup_accessors (image);
2888 }
2889
2890 #endif