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