Pass the full image flags to iterators
[profile/ivi/pixman.git] / pixman / pixman-bits-image.c
1 /*
2  * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
3  *             2005 Lars Knoll & Zack Rusin, Trolltech
4  *             2008 Aaron Plattner, NVIDIA Corporation
5  * Copyright © 2000 SuSE, Inc.
6  * Copyright © 2007, 2009 Red Hat, Inc.
7  * Copyright © 2008 André Tupinambá <andrelrt@gmail.com>
8  *
9  * Permission to use, copy, modify, distribute, and sell this software and its
10  * documentation for any purpose is hereby granted without fee, provided that
11  * the above copyright notice appear in all copies and that both that
12  * copyright notice and this permission notice appear in supporting
13  * documentation, and that the name of Keith Packard not be used in
14  * advertising or publicity pertaining to distribution of the software without
15  * specific, written prior permission.  Keith Packard makes no
16  * representations about the suitability of this software for any purpose.  It
17  * is provided "as is" without express or implied warranty.
18  *
19  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
20  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
24  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
25  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
26  * SOFTWARE.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "pixman-private.h"
36 #include "pixman-combine32.h"
37 #include "pixman-inlines.h"
38
39 /*
40  * By default, just evaluate the image at 32bpp and expand.  Individual image
41  * types can plug in a better scanline getter if they want to. For example
42  * we  could produce smoother gradients by evaluating them at higher color
43  * depth, but that's a project for the future.
44  */
45 static void
46 _pixman_image_get_scanline_generic_64 (pixman_image_t * image,
47                                        int              x,
48                                        int              y,
49                                        int              width,
50                                        uint32_t *       buffer,
51                                        const uint32_t * mask)
52 {
53     uint32_t *mask8 = NULL;
54
55     /* Contract the mask image, if one exists, so that the 32-bit fetch
56      * function can use it.
57      */
58     if (mask)
59     {
60         mask8 = pixman_malloc_ab (width, sizeof(uint32_t));
61         if (!mask8)
62             return;
63
64         pixman_contract (mask8, (uint64_t *)mask, width);
65     }
66
67     /* Fetch the source image into the first half of buffer. */
68     image->bits.get_scanline_32 (image, x, y, width, (uint32_t*)buffer, mask8);
69
70     /* Expand from 32bpp to 64bpp in place. */
71     pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, width);
72
73     free (mask8);
74 }
75
76 /* Fetch functions */
77
78 static force_inline uint32_t
79 fetch_pixel_no_alpha (bits_image_t *image,
80                       int x, int y, pixman_bool_t check_bounds)
81 {
82     if (check_bounds &&
83         (x < 0 || x >= image->width || y < 0 || y >= image->height))
84     {
85         return 0;
86     }
87
88     return image->fetch_pixel_32 (image, x, y);
89 }
90
91 typedef uint32_t (* get_pixel_t) (bits_image_t *image,
92                                   int x, int y, pixman_bool_t check_bounds);
93
94 static force_inline uint32_t
95 bits_image_fetch_pixel_nearest (bits_image_t   *image,
96                                 pixman_fixed_t  x,
97                                 pixman_fixed_t  y,
98                                 get_pixel_t     get_pixel)
99 {
100     int x0 = pixman_fixed_to_int (x - pixman_fixed_e);
101     int y0 = pixman_fixed_to_int (y - pixman_fixed_e);
102
103     if (image->common.repeat != PIXMAN_REPEAT_NONE)
104     {
105         repeat (image->common.repeat, &x0, image->width);
106         repeat (image->common.repeat, &y0, image->height);
107
108         return get_pixel (image, x0, y0, FALSE);
109     }
110     else
111     {
112         return get_pixel (image, x0, y0, TRUE);
113     }
114 }
115
116 static force_inline uint32_t
117 bits_image_fetch_pixel_bilinear (bits_image_t   *image,
118                                  pixman_fixed_t  x,
119                                  pixman_fixed_t  y,
120                                  get_pixel_t     get_pixel)
121 {
122     pixman_repeat_t repeat_mode = image->common.repeat;
123     int width = image->width;
124     int height = image->height;
125     int x1, y1, x2, y2;
126     uint32_t tl, tr, bl, br;
127     int32_t distx, disty;
128
129     x1 = x - pixman_fixed_1 / 2;
130     y1 = y - pixman_fixed_1 / 2;
131
132     distx = (x1 >> 8) & 0xff;
133     disty = (y1 >> 8) & 0xff;
134
135     x1 = pixman_fixed_to_int (x1);
136     y1 = pixman_fixed_to_int (y1);
137     x2 = x1 + 1;
138     y2 = y1 + 1;
139
140     if (repeat_mode != PIXMAN_REPEAT_NONE)
141     {
142         repeat (repeat_mode, &x1, width);
143         repeat (repeat_mode, &y1, height);
144         repeat (repeat_mode, &x2, width);
145         repeat (repeat_mode, &y2, height);
146
147         tl = get_pixel (image, x1, y1, FALSE);
148         bl = get_pixel (image, x1, y2, FALSE);
149         tr = get_pixel (image, x2, y1, FALSE);
150         br = get_pixel (image, x2, y2, FALSE);
151     }
152     else
153     {
154         tl = get_pixel (image, x1, y1, TRUE);
155         tr = get_pixel (image, x2, y1, TRUE);
156         bl = get_pixel (image, x1, y2, TRUE);
157         br = get_pixel (image, x2, y2, TRUE);
158     }
159
160     return bilinear_interpolation (tl, tr, bl, br, distx, disty);
161 }
162
163 static void
164 bits_image_fetch_bilinear_no_repeat_8888 (pixman_image_t * ima,
165                                           int              offset,
166                                           int              line,
167                                           int              width,
168                                           uint32_t *       buffer,
169                                           const uint32_t * mask)
170 {
171     bits_image_t *bits = &ima->bits;
172     pixman_fixed_t x_top, x_bottom, x;
173     pixman_fixed_t ux_top, ux_bottom, ux;
174     pixman_vector_t v;
175     uint32_t top_mask, bottom_mask;
176     uint32_t *top_row;
177     uint32_t *bottom_row;
178     uint32_t *end;
179     uint32_t zero[2] = { 0, 0 };
180     uint32_t one = 1;
181     int y, y1, y2;
182     int disty;
183     int mask_inc;
184     int w;
185
186     /* reference point is the center of the pixel */
187     v.vector[0] = pixman_int_to_fixed (offset) + pixman_fixed_1 / 2;
188     v.vector[1] = pixman_int_to_fixed (line) + pixman_fixed_1 / 2;
189     v.vector[2] = pixman_fixed_1;
190
191     if (!pixman_transform_point_3d (bits->common.transform, &v))
192         return;
193
194     ux = ux_top = ux_bottom = bits->common.transform->matrix[0][0];
195     x = x_top = x_bottom = v.vector[0] - pixman_fixed_1/2;
196
197     y = v.vector[1] - pixman_fixed_1/2;
198     disty = (y >> 8) & 0xff;
199
200     /* Load the pointers to the first and second lines from the source
201      * image that bilinear code must read.
202      *
203      * The main trick in this code is about the check if any line are
204      * outside of the image;
205      *
206      * When I realize that a line (any one) is outside, I change
207      * the pointer to a dummy area with zeros. Once I change this, I
208      * must be sure the pointer will not change, so I set the
209      * variables to each pointer increments inside the loop.
210      */
211     y1 = pixman_fixed_to_int (y);
212     y2 = y1 + 1;
213
214     if (y1 < 0 || y1 >= bits->height)
215     {
216         top_row = zero;
217         x_top = 0;
218         ux_top = 0;
219     }
220     else
221     {
222         top_row = bits->bits + y1 * bits->rowstride;
223         x_top = x;
224         ux_top = ux;
225     }
226
227     if (y2 < 0 || y2 >= bits->height)
228     {
229         bottom_row = zero;
230         x_bottom = 0;
231         ux_bottom = 0;
232     }
233     else
234     {
235         bottom_row = bits->bits + y2 * bits->rowstride;
236         x_bottom = x;
237         ux_bottom = ux;
238     }
239
240     /* Instead of checking whether the operation uses the mast in
241      * each loop iteration, verify this only once and prepare the
242      * variables to make the code smaller inside the loop.
243      */
244     if (!mask)
245     {
246         mask_inc = 0;
247         mask = &one;
248     }
249     else
250     {
251         /* If have a mask, prepare the variables to check it */
252         mask_inc = 1;
253     }
254
255     /* If both are zero, then the whole thing is zero */
256     if (top_row == zero && bottom_row == zero)
257     {
258         memset (buffer, 0, width * sizeof (uint32_t));
259         return;
260     }
261     else if (bits->format == PIXMAN_x8r8g8b8)
262     {
263         if (top_row == zero)
264         {
265             top_mask = 0;
266             bottom_mask = 0xff000000;
267         }
268         else if (bottom_row == zero)
269         {
270             top_mask = 0xff000000;
271             bottom_mask = 0;
272         }
273         else
274         {
275             top_mask = 0xff000000;
276             bottom_mask = 0xff000000;
277         }
278     }
279     else
280     {
281         top_mask = 0;
282         bottom_mask = 0;
283     }
284
285     end = buffer + width;
286
287     /* Zero fill to the left of the image */
288     while (buffer < end && x < pixman_fixed_minus_1)
289     {
290         *buffer++ = 0;
291         x += ux;
292         x_top += ux_top;
293         x_bottom += ux_bottom;
294         mask += mask_inc;
295     }
296
297     /* Left edge
298      */
299     while (buffer < end && x < 0)
300     {
301         uint32_t tr, br;
302         int32_t distx;
303
304         tr = top_row[pixman_fixed_to_int (x_top) + 1] | top_mask;
305         br = bottom_row[pixman_fixed_to_int (x_bottom) + 1] | bottom_mask;
306
307         distx = (x >> 8) & 0xff;
308
309         *buffer++ = bilinear_interpolation (0, tr, 0, br, distx, disty);
310
311         x += ux;
312         x_top += ux_top;
313         x_bottom += ux_bottom;
314         mask += mask_inc;
315     }
316
317     /* Main part */
318     w = pixman_int_to_fixed (bits->width - 1);
319
320     while (buffer < end  &&  x < w)
321     {
322         if (*mask)
323         {
324             uint32_t tl, tr, bl, br;
325             int32_t distx;
326
327             tl = top_row [pixman_fixed_to_int (x_top)] | top_mask;
328             tr = top_row [pixman_fixed_to_int (x_top) + 1] | top_mask;
329             bl = bottom_row [pixman_fixed_to_int (x_bottom)] | bottom_mask;
330             br = bottom_row [pixman_fixed_to_int (x_bottom) + 1] | bottom_mask;
331
332             distx = (x >> 8) & 0xff;
333
334             *buffer = bilinear_interpolation (tl, tr, bl, br, distx, disty);
335         }
336
337         buffer++;
338         x += ux;
339         x_top += ux_top;
340         x_bottom += ux_bottom;
341         mask += mask_inc;
342     }
343
344     /* Right Edge */
345     w = pixman_int_to_fixed (bits->width);
346     while (buffer < end  &&  x < w)
347     {
348         if (*mask)
349         {
350             uint32_t tl, bl;
351             int32_t distx;
352
353             tl = top_row [pixman_fixed_to_int (x_top)] | top_mask;
354             bl = bottom_row [pixman_fixed_to_int (x_bottom)] | bottom_mask;
355
356             distx = (x >> 8) & 0xff;
357
358             *buffer = bilinear_interpolation (tl, 0, bl, 0, distx, disty);
359         }
360
361         buffer++;
362         x += ux;
363         x_top += ux_top;
364         x_bottom += ux_bottom;
365         mask += mask_inc;
366     }
367
368     /* Zero fill to the left of the image */
369     while (buffer < end)
370         *buffer++ = 0;
371 }
372
373 static force_inline uint32_t
374 bits_image_fetch_pixel_convolution (bits_image_t   *image,
375                                     pixman_fixed_t  x,
376                                     pixman_fixed_t  y,
377                                     get_pixel_t     get_pixel)
378 {
379     pixman_fixed_t *params = image->common.filter_params;
380     int x_off = (params[0] - pixman_fixed_1) >> 1;
381     int y_off = (params[1] - pixman_fixed_1) >> 1;
382     int32_t cwidth = pixman_fixed_to_int (params[0]);
383     int32_t cheight = pixman_fixed_to_int (params[1]);
384     int32_t i, j, x1, x2, y1, y2;
385     pixman_repeat_t repeat_mode = image->common.repeat;
386     int width = image->width;
387     int height = image->height;
388     int srtot, sgtot, sbtot, satot;
389
390     params += 2;
391
392     x1 = pixman_fixed_to_int (x - pixman_fixed_e - x_off);
393     y1 = pixman_fixed_to_int (y - pixman_fixed_e - y_off);
394     x2 = x1 + cwidth;
395     y2 = y1 + cheight;
396
397     srtot = sgtot = sbtot = satot = 0;
398
399     for (i = y1; i < y2; ++i)
400     {
401         for (j = x1; j < x2; ++j)
402         {
403             int rx = j;
404             int ry = i;
405
406             pixman_fixed_t f = *params;
407
408             if (f)
409             {
410                 uint32_t pixel;
411
412                 if (repeat_mode != PIXMAN_REPEAT_NONE)
413                 {
414                     repeat (repeat_mode, &rx, width);
415                     repeat (repeat_mode, &ry, height);
416
417                     pixel = get_pixel (image, rx, ry, FALSE);
418                 }
419                 else
420                 {
421                     pixel = get_pixel (image, rx, ry, TRUE);
422                 }
423
424                 srtot += (int)RED_8 (pixel) * f;
425                 sgtot += (int)GREEN_8 (pixel) * f;
426                 sbtot += (int)BLUE_8 (pixel) * f;
427                 satot += (int)ALPHA_8 (pixel) * f;
428             }
429
430             params++;
431         }
432     }
433
434     satot >>= 16;
435     srtot >>= 16;
436     sgtot >>= 16;
437     sbtot >>= 16;
438
439     satot = CLIP (satot, 0, 0xff);
440     srtot = CLIP (srtot, 0, 0xff);
441     sgtot = CLIP (sgtot, 0, 0xff);
442     sbtot = CLIP (sbtot, 0, 0xff);
443
444     return ((satot << 24) | (srtot << 16) | (sgtot <<  8) | (sbtot));
445 }
446
447 static force_inline uint32_t
448 bits_image_fetch_pixel_filtered (bits_image_t *image,
449                                  pixman_fixed_t x,
450                                  pixman_fixed_t y,
451                                  get_pixel_t    get_pixel)
452 {
453     switch (image->common.filter)
454     {
455     case PIXMAN_FILTER_NEAREST:
456     case PIXMAN_FILTER_FAST:
457         return bits_image_fetch_pixel_nearest (image, x, y, get_pixel);
458         break;
459
460     case PIXMAN_FILTER_BILINEAR:
461     case PIXMAN_FILTER_GOOD:
462     case PIXMAN_FILTER_BEST:
463         return bits_image_fetch_pixel_bilinear (image, x, y, get_pixel);
464         break;
465
466     case PIXMAN_FILTER_CONVOLUTION:
467         return bits_image_fetch_pixel_convolution (image, x, y, get_pixel);
468         break;
469
470     default:
471         break;
472     }
473
474     return 0;
475 }
476
477 static void
478 bits_image_fetch_affine_no_alpha (pixman_image_t * image,
479                                   int              offset,
480                                   int              line,
481                                   int              width,
482                                   uint32_t *       buffer,
483                                   const uint32_t * mask)
484 {
485     pixman_fixed_t x, y;
486     pixman_fixed_t ux, uy;
487     pixman_vector_t v;
488     int i;
489
490     /* reference point is the center of the pixel */
491     v.vector[0] = pixman_int_to_fixed (offset) + pixman_fixed_1 / 2;
492     v.vector[1] = pixman_int_to_fixed (line) + pixman_fixed_1 / 2;
493     v.vector[2] = pixman_fixed_1;
494
495     if (image->common.transform)
496     {
497         if (!pixman_transform_point_3d (image->common.transform, &v))
498             return;
499
500         ux = image->common.transform->matrix[0][0];
501         uy = image->common.transform->matrix[1][0];
502     }
503     else
504     {
505         ux = pixman_fixed_1;
506         uy = 0;
507     }
508
509     x = v.vector[0];
510     y = v.vector[1];
511
512     for (i = 0; i < width; ++i)
513     {
514         if (!mask || mask[i])
515         {
516             buffer[i] = bits_image_fetch_pixel_filtered (
517                 &image->bits, x, y, fetch_pixel_no_alpha);
518         }
519
520         x += ux;
521         y += uy;
522     }
523 }
524
525 /* General fetcher */
526 static force_inline uint32_t
527 fetch_pixel_general (bits_image_t *image, int x, int y, pixman_bool_t check_bounds)
528 {
529     uint32_t pixel;
530
531     if (check_bounds &&
532         (x < 0 || x >= image->width || y < 0 || y >= image->height))
533     {
534         return 0;
535     }
536
537     pixel = image->fetch_pixel_32 (image, x, y);
538
539     if (image->common.alpha_map)
540     {
541         uint32_t pixel_a;
542
543         x -= image->common.alpha_origin_x;
544         y -= image->common.alpha_origin_y;
545
546         if (x < 0 || x >= image->common.alpha_map->width ||
547             y < 0 || y >= image->common.alpha_map->height)
548         {
549             pixel_a = 0;
550         }
551         else
552         {
553             pixel_a = image->common.alpha_map->fetch_pixel_32 (
554                 image->common.alpha_map, x, y);
555
556             pixel_a = ALPHA_8 (pixel_a);
557         }
558
559         pixel &= 0x00ffffff;
560         pixel |= (pixel_a << 24);
561     }
562
563     return pixel;
564 }
565
566 static void
567 bits_image_fetch_general (pixman_image_t * image,
568                           int              offset,
569                           int              line,
570                           int              width,
571                           uint32_t *       buffer,
572                           const uint32_t * mask)
573 {
574     pixman_fixed_t x, y, w;
575     pixman_fixed_t ux, uy, uw;
576     pixman_vector_t v;
577     int i;
578
579     /* reference point is the center of the pixel */
580     v.vector[0] = pixman_int_to_fixed (offset) + pixman_fixed_1 / 2;
581     v.vector[1] = pixman_int_to_fixed (line) + pixman_fixed_1 / 2;
582     v.vector[2] = pixman_fixed_1;
583
584     if (image->common.transform)
585     {
586         if (!pixman_transform_point_3d (image->common.transform, &v))
587             return;
588
589         ux = image->common.transform->matrix[0][0];
590         uy = image->common.transform->matrix[1][0];
591         uw = image->common.transform->matrix[2][0];
592     }
593     else
594     {
595         ux = pixman_fixed_1;
596         uy = 0;
597         uw = 0;
598     }
599
600     x = v.vector[0];
601     y = v.vector[1];
602     w = v.vector[2];
603
604     for (i = 0; i < width; ++i)
605     {
606         pixman_fixed_t x0, y0;
607
608         if (!mask || mask[i])
609         {
610             if (w != 0)
611             {
612                 x0 = ((pixman_fixed_48_16_t)x << 16) / w;
613                 y0 = ((pixman_fixed_48_16_t)y << 16) / w;
614             }
615             else
616             {
617                 x0 = 0;
618                 y0 = 0;
619             }
620
621             buffer[i] = bits_image_fetch_pixel_filtered (
622                 &image->bits, x0, y0, fetch_pixel_general);
623         }
624
625         x += ux;
626         y += uy;
627         w += uw;
628     }
629 }
630
631 static const uint8_t zero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
632
633 typedef uint32_t (* convert_pixel_t) (const uint8_t *row, int x);
634
635 static force_inline void
636 bits_image_fetch_bilinear_affine (pixman_image_t * image,
637                                   int              offset,
638                                   int              line,
639                                   int              width,
640                                   uint32_t *       buffer,
641                                   const uint32_t * mask,
642
643                                   convert_pixel_t       convert_pixel,
644                                   pixman_format_code_t  format,
645                                   pixman_repeat_t       repeat_mode)
646 {
647     pixman_fixed_t x, y;
648     pixman_fixed_t ux, uy;
649     pixman_vector_t v;
650     bits_image_t *bits = &image->bits;
651     int i;
652
653     /* reference point is the center of the pixel */
654     v.vector[0] = pixman_int_to_fixed (offset) + pixman_fixed_1 / 2;
655     v.vector[1] = pixman_int_to_fixed (line) + pixman_fixed_1 / 2;
656     v.vector[2] = pixman_fixed_1;
657
658     if (!pixman_transform_point_3d (image->common.transform, &v))
659         return;
660
661     ux = image->common.transform->matrix[0][0];
662     uy = image->common.transform->matrix[1][0];
663
664     x = v.vector[0];
665     y = v.vector[1];
666
667     for (i = 0; i < width; ++i)
668     {
669         int x1, y1, x2, y2;
670         uint32_t tl, tr, bl, br;
671         int32_t distx, disty;
672         int width = image->bits.width;
673         int height = image->bits.height;
674         const uint8_t *row1;
675         const uint8_t *row2;
676
677         if (mask && !mask[i])
678             goto next;
679
680         x1 = x - pixman_fixed_1 / 2;
681         y1 = y - pixman_fixed_1 / 2;
682
683         distx = (x1 >> 8) & 0xff;
684         disty = (y1 >> 8) & 0xff;
685
686         y1 = pixman_fixed_to_int (y1);
687         y2 = y1 + 1;
688         x1 = pixman_fixed_to_int (x1);
689         x2 = x1 + 1;
690
691         if (repeat_mode != PIXMAN_REPEAT_NONE)
692         {
693             uint32_t mask;
694
695             mask = PIXMAN_FORMAT_A (format)? 0 : 0xff000000;
696
697             repeat (repeat_mode, &x1, width);
698             repeat (repeat_mode, &y1, height);
699             repeat (repeat_mode, &x2, width);
700             repeat (repeat_mode, &y2, height);
701
702             row1 = (uint8_t *)bits->bits + bits->rowstride * 4 * y1;
703             row2 = (uint8_t *)bits->bits + bits->rowstride * 4 * y2;
704
705             tl = convert_pixel (row1, x1) | mask;
706             tr = convert_pixel (row1, x2) | mask;
707             bl = convert_pixel (row2, x1) | mask;
708             br = convert_pixel (row2, x2) | mask;
709         }
710         else
711         {
712             uint32_t mask1, mask2;
713             int bpp;
714
715             /* Note: PIXMAN_FORMAT_BPP() returns an unsigned value,
716              * which means if you use it in expressions, those
717              * expressions become unsigned themselves. Since
718              * the variables below can be negative in some cases,
719              * that will lead to crashes on 64 bit architectures.
720              *
721              * So this line makes sure bpp is signed
722              */
723             bpp = PIXMAN_FORMAT_BPP (format);
724
725             if (x1 >= width || x2 < 0 || y1 >= height || y2 < 0)
726             {
727                 buffer[i] = 0;
728                 goto next;
729             }
730
731             if (y2 == 0)
732             {
733                 row1 = zero;
734                 mask1 = 0;
735             }
736             else
737             {
738                 row1 = (uint8_t *)bits->bits + bits->rowstride * 4 * y1;
739                 row1 += bpp / 8 * x1;
740
741                 mask1 = PIXMAN_FORMAT_A (format)? 0 : 0xff000000;
742             }
743
744             if (y1 == height - 1)
745             {
746                 row2 = zero;
747                 mask2 = 0;
748             }
749             else
750             {
751                 row2 = (uint8_t *)bits->bits + bits->rowstride * 4 * y2;
752                 row2 += bpp / 8 * x1;
753
754                 mask2 = PIXMAN_FORMAT_A (format)? 0 : 0xff000000;
755             }
756
757             if (x2 == 0)
758             {
759                 tl = 0;
760                 bl = 0;
761             }
762             else
763             {
764                 tl = convert_pixel (row1, 0) | mask1;
765                 bl = convert_pixel (row2, 0) | mask2;
766             }
767
768             if (x1 == width - 1)
769             {
770                 tr = 0;
771                 br = 0;
772             }
773             else
774             {
775                 tr = convert_pixel (row1, 1) | mask1;
776                 br = convert_pixel (row2, 1) | mask2;
777             }
778         }
779
780         buffer[i] = bilinear_interpolation (
781             tl, tr, bl, br, distx, disty);
782
783     next:
784         x += ux;
785         y += uy;
786     }
787 }
788
789 static force_inline void
790 bits_image_fetch_nearest_affine (pixman_image_t * image,
791                                  int              offset,
792                                  int              line,
793                                  int              width,
794                                  uint32_t *       buffer,
795                                  const uint32_t * mask,
796                                  
797                                  convert_pixel_t        convert_pixel,
798                                  pixman_format_code_t   format,
799                                  pixman_repeat_t        repeat_mode)
800 {
801     pixman_fixed_t x, y;
802     pixman_fixed_t ux, uy;
803     pixman_vector_t v;
804     bits_image_t *bits = &image->bits;
805     int i;
806
807     /* reference point is the center of the pixel */
808     v.vector[0] = pixman_int_to_fixed (offset) + pixman_fixed_1 / 2;
809     v.vector[1] = pixman_int_to_fixed (line) + pixman_fixed_1 / 2;
810     v.vector[2] = pixman_fixed_1;
811
812     if (!pixman_transform_point_3d (image->common.transform, &v))
813         return;
814
815     ux = image->common.transform->matrix[0][0];
816     uy = image->common.transform->matrix[1][0];
817
818     x = v.vector[0];
819     y = v.vector[1];
820
821     for (i = 0; i < width; ++i)
822     {
823         int width, height, x0, y0;
824         const uint8_t *row;
825
826         if (mask && !mask[i])
827             goto next;
828         
829         width = image->bits.width;
830         height = image->bits.height;
831         x0 = pixman_fixed_to_int (x - pixman_fixed_e);
832         y0 = pixman_fixed_to_int (y - pixman_fixed_e);
833
834         if (repeat_mode == PIXMAN_REPEAT_NONE &&
835             (y0 < 0 || y0 >= height || x0 < 0 || x0 >= width))
836         {
837             buffer[i] = 0;
838         }
839         else
840         {
841             uint32_t mask = PIXMAN_FORMAT_A (format)? 0 : 0xff000000;
842
843             if (repeat_mode != PIXMAN_REPEAT_NONE)
844             {
845                 repeat (repeat_mode, &x0, width);
846                 repeat (repeat_mode, &y0, height);
847             }
848
849             row = (uint8_t *)bits->bits + bits->rowstride * 4 * y0;
850
851             buffer[i] = convert_pixel (row, x0) | mask;
852         }
853
854     next:
855         x += ux;
856         y += uy;
857     }
858 }
859
860 static force_inline uint32_t
861 convert_a8r8g8b8 (const uint8_t *row, int x)
862 {
863     return *(((uint32_t *)row) + x);
864 }
865
866 static force_inline uint32_t
867 convert_x8r8g8b8 (const uint8_t *row, int x)
868 {
869     return *(((uint32_t *)row) + x);
870 }
871
872 static force_inline uint32_t
873 convert_a8 (const uint8_t *row, int x)
874 {
875     return *(row + x) << 24;
876 }
877
878 static force_inline uint32_t
879 convert_r5g6b5 (const uint8_t *row, int x)
880 {
881     return CONVERT_0565_TO_0888 (*((uint16_t *)row + x));
882 }
883
884 #define MAKE_BILINEAR_FETCHER(name, format, repeat_mode)                \
885     static void                                                         \
886     bits_image_fetch_bilinear_affine_ ## name (pixman_image_t *image,   \
887                                                int              offset, \
888                                                int              line,   \
889                                                int              width,  \
890                                                uint32_t *       buffer, \
891                                                const uint32_t * mask)   \
892     {                                                                   \
893         bits_image_fetch_bilinear_affine (image, offset, line,          \
894                                           width, buffer, mask,          \
895                                           convert_ ## format,           \
896                                           PIXMAN_ ## format,            \
897                                           repeat_mode);                 \
898     }
899
900 #define MAKE_NEAREST_FETCHER(name, format, repeat_mode)                 \
901     static void                                                         \
902     bits_image_fetch_nearest_affine_ ## name (pixman_image_t *image,    \
903                                               int              offset,  \
904                                               int              line,    \
905                                               int              width,   \
906                                               uint32_t *       buffer,  \
907                                               const uint32_t * mask)    \
908     {                                                                   \
909         bits_image_fetch_nearest_affine (image, offset, line,           \
910                                          width, buffer, mask,           \
911                                          convert_ ## format,            \
912                                          PIXMAN_ ## format,             \
913                                          repeat_mode);                  \
914     }
915
916 #define MAKE_FETCHERS(name, format, repeat_mode)                        \
917     MAKE_NEAREST_FETCHER (name, format, repeat_mode)                    \
918     MAKE_BILINEAR_FETCHER (name, format, repeat_mode)
919
920 MAKE_FETCHERS (pad_a8r8g8b8,     a8r8g8b8, PIXMAN_REPEAT_PAD)
921 MAKE_FETCHERS (none_a8r8g8b8,    a8r8g8b8, PIXMAN_REPEAT_NONE)
922 MAKE_FETCHERS (reflect_a8r8g8b8, a8r8g8b8, PIXMAN_REPEAT_REFLECT)
923 MAKE_FETCHERS (normal_a8r8g8b8,  a8r8g8b8, PIXMAN_REPEAT_NORMAL)
924 MAKE_FETCHERS (pad_x8r8g8b8,     x8r8g8b8, PIXMAN_REPEAT_PAD)
925 MAKE_FETCHERS (none_x8r8g8b8,    x8r8g8b8, PIXMAN_REPEAT_NONE)
926 MAKE_FETCHERS (reflect_x8r8g8b8, x8r8g8b8, PIXMAN_REPEAT_REFLECT)
927 MAKE_FETCHERS (normal_x8r8g8b8,  x8r8g8b8, PIXMAN_REPEAT_NORMAL)
928 MAKE_FETCHERS (pad_a8,           a8,       PIXMAN_REPEAT_PAD)
929 MAKE_FETCHERS (none_a8,          a8,       PIXMAN_REPEAT_NONE)
930 MAKE_FETCHERS (reflect_a8,       a8,       PIXMAN_REPEAT_REFLECT)
931 MAKE_FETCHERS (normal_a8,        a8,       PIXMAN_REPEAT_NORMAL)
932 MAKE_FETCHERS (pad_r5g6b5,       r5g6b5,   PIXMAN_REPEAT_PAD)
933 MAKE_FETCHERS (none_r5g6b5,      r5g6b5,   PIXMAN_REPEAT_NONE)
934 MAKE_FETCHERS (reflect_r5g6b5,   r5g6b5,   PIXMAN_REPEAT_REFLECT)
935 MAKE_FETCHERS (normal_r5g6b5,    r5g6b5,   PIXMAN_REPEAT_NORMAL)
936
937 static void
938 replicate_pixel_32 (bits_image_t *   bits,
939                     int              x,
940                     int              y,
941                     int              width,
942                     uint32_t *       buffer)
943 {
944     uint32_t color;
945     uint32_t *end;
946
947     color = bits->fetch_pixel_32 (bits, x, y);
948
949     end = buffer + width;
950     while (buffer < end)
951         *(buffer++) = color;
952 }
953
954 static void
955 replicate_pixel_64 (bits_image_t *   bits,
956                     int              x,
957                     int              y,
958                     int              width,
959                     uint32_t *       b)
960 {
961     uint64_t color;
962     uint64_t *buffer = (uint64_t *)b;
963     uint64_t *end;
964
965     color = bits->fetch_pixel_64 (bits, x, y);
966
967     end = buffer + width;
968     while (buffer < end)
969         *(buffer++) = color;
970 }
971
972 static void
973 bits_image_fetch_untransformed_repeat_none (bits_image_t *image,
974                                             pixman_bool_t wide,
975                                             int           x,
976                                             int           y,
977                                             int           width,
978                                             uint32_t *    buffer)
979 {
980     uint32_t w;
981
982     if (y < 0 || y >= image->height)
983     {
984         memset (buffer, 0, width * (wide? 8 : 4));
985         return;
986     }
987
988     if (x < 0)
989     {
990         w = MIN (width, -x);
991
992         memset (buffer, 0, w * (wide ? 8 : 4));
993
994         width -= w;
995         buffer += w * (wide? 2 : 1);
996         x += w;
997     }
998
999     if (x < image->width)
1000     {
1001         w = MIN (width, image->width - x);
1002
1003         if (wide)
1004             image->fetch_scanline_64 ((pixman_image_t *)image, x, y, w, buffer, NULL);
1005         else
1006             image->fetch_scanline_32 ((pixman_image_t *)image, x, y, w, buffer, NULL);
1007
1008         width -= w;
1009         buffer += w * (wide? 2 : 1);
1010         x += w;
1011     }
1012
1013     memset (buffer, 0, width * (wide ? 8 : 4));
1014 }
1015
1016 static void
1017 bits_image_fetch_untransformed_repeat_normal (bits_image_t *image,
1018                                               pixman_bool_t wide,
1019                                               int           x,
1020                                               int           y,
1021                                               int           width,
1022                                               uint32_t *    buffer)
1023 {
1024     uint32_t w;
1025
1026     while (y < 0)
1027         y += image->height;
1028
1029     while (y >= image->height)
1030         y -= image->height;
1031
1032     if (image->width == 1)
1033     {
1034         if (wide)
1035             replicate_pixel_64 (image, 0, y, width, buffer);
1036         else
1037             replicate_pixel_32 (image, 0, y, width, buffer);
1038
1039         return;
1040     }
1041
1042     while (width)
1043     {
1044         while (x < 0)
1045             x += image->width;
1046         while (x >= image->width)
1047             x -= image->width;
1048
1049         w = MIN (width, image->width - x);
1050
1051         if (wide)
1052             image->fetch_scanline_64 ((pixman_image_t *)image, x, y, w, buffer, NULL);
1053         else
1054             image->fetch_scanline_32 ((pixman_image_t *)image, x, y, w, buffer, NULL);
1055
1056         buffer += w * (wide? 2 : 1);
1057         x += w;
1058         width -= w;
1059     }
1060 }
1061
1062 static void
1063 bits_image_fetch_untransformed_32 (pixman_image_t * image,
1064                                    int              x,
1065                                    int              y,
1066                                    int              width,
1067                                    uint32_t *       buffer,
1068                                    const uint32_t * mask)
1069 {
1070     if (image->common.repeat == PIXMAN_REPEAT_NONE)
1071     {
1072         bits_image_fetch_untransformed_repeat_none (
1073             &image->bits, FALSE, x, y, width, buffer);
1074     }
1075     else
1076     {
1077         bits_image_fetch_untransformed_repeat_normal (
1078             &image->bits, FALSE, x, y, width, buffer);
1079     }
1080 }
1081
1082 static void
1083 bits_image_fetch_untransformed_64 (pixman_image_t * image,
1084                                    int              x,
1085                                    int              y,
1086                                    int              width,
1087                                    uint32_t *       buffer,
1088                                    const uint32_t * unused)
1089 {
1090     if (image->common.repeat == PIXMAN_REPEAT_NONE)
1091     {
1092         bits_image_fetch_untransformed_repeat_none (
1093             &image->bits, TRUE, x, y, width, buffer);
1094     }
1095     else
1096     {
1097         bits_image_fetch_untransformed_repeat_normal (
1098             &image->bits, TRUE, x, y, width, buffer);
1099     }
1100 }
1101
1102 typedef struct
1103 {
1104     pixman_format_code_t        format;
1105     uint32_t                    flags;
1106     fetch_scanline_t            fetch_32;
1107     fetch_scanline_t            fetch_64;
1108 } fetcher_info_t;
1109
1110 static const fetcher_info_t fetcher_info[] =
1111 {
1112     { PIXMAN_any,
1113       (FAST_PATH_NO_ALPHA_MAP                   |
1114        FAST_PATH_ID_TRANSFORM                   |
1115        FAST_PATH_NO_CONVOLUTION_FILTER          |
1116        FAST_PATH_NO_PAD_REPEAT                  |
1117        FAST_PATH_NO_REFLECT_REPEAT),
1118       bits_image_fetch_untransformed_32,
1119       bits_image_fetch_untransformed_64
1120     },
1121
1122 #define FAST_BILINEAR_FLAGS                                             \
1123     (FAST_PATH_NO_ALPHA_MAP             |                               \
1124      FAST_PATH_NO_ACCESSORS             |                               \
1125      FAST_PATH_HAS_TRANSFORM            |                               \
1126      FAST_PATH_AFFINE_TRANSFORM         |                               \
1127      FAST_PATH_X_UNIT_POSITIVE          |                               \
1128      FAST_PATH_Y_UNIT_ZERO              |                               \
1129      FAST_PATH_NONE_REPEAT              |                               \
1130      FAST_PATH_BILINEAR_FILTER)
1131
1132     { PIXMAN_a8r8g8b8,
1133       FAST_BILINEAR_FLAGS,
1134       bits_image_fetch_bilinear_no_repeat_8888,
1135       _pixman_image_get_scanline_generic_64
1136     },
1137
1138     { PIXMAN_x8r8g8b8,
1139       FAST_BILINEAR_FLAGS,
1140       bits_image_fetch_bilinear_no_repeat_8888,
1141       _pixman_image_get_scanline_generic_64
1142     },
1143
1144 #define GENERAL_BILINEAR_FLAGS                                          \
1145     (FAST_PATH_NO_ALPHA_MAP             |                               \
1146      FAST_PATH_NO_ACCESSORS             |                               \
1147      FAST_PATH_HAS_TRANSFORM            |                               \
1148      FAST_PATH_AFFINE_TRANSFORM         |                               \
1149      FAST_PATH_BILINEAR_FILTER)
1150
1151 #define GENERAL_NEAREST_FLAGS                                           \
1152     (FAST_PATH_NO_ALPHA_MAP             |                               \
1153      FAST_PATH_NO_ACCESSORS             |                               \
1154      FAST_PATH_HAS_TRANSFORM            |                               \
1155      FAST_PATH_AFFINE_TRANSFORM         |                               \
1156      FAST_PATH_NEAREST_FILTER)
1157
1158 #define BILINEAR_AFFINE_FAST_PATH(name, format, repeat)                 \
1159     { PIXMAN_ ## format,                                                \
1160       GENERAL_BILINEAR_FLAGS | FAST_PATH_ ## repeat ## _REPEAT,         \
1161       bits_image_fetch_bilinear_affine_ ## name,                        \
1162       _pixman_image_get_scanline_generic_64                             \
1163     },
1164
1165 #define NEAREST_AFFINE_FAST_PATH(name, format, repeat)                  \
1166     { PIXMAN_ ## format,                                                \
1167       GENERAL_NEAREST_FLAGS | FAST_PATH_ ## repeat ## _REPEAT,          \
1168       bits_image_fetch_nearest_affine_ ## name,                 \
1169       _pixman_image_get_scanline_generic_64                             \
1170     },
1171
1172 #define AFFINE_FAST_PATHS(name, format, repeat)                         \
1173     BILINEAR_AFFINE_FAST_PATH(name, format, repeat)                     \
1174     NEAREST_AFFINE_FAST_PATH(name, format, repeat)
1175     
1176     AFFINE_FAST_PATHS (pad_a8r8g8b8, a8r8g8b8, PAD)
1177     AFFINE_FAST_PATHS (none_a8r8g8b8, a8r8g8b8, NONE)
1178     AFFINE_FAST_PATHS (reflect_a8r8g8b8, a8r8g8b8, REFLECT)
1179     AFFINE_FAST_PATHS (normal_a8r8g8b8, a8r8g8b8, NORMAL)
1180     AFFINE_FAST_PATHS (pad_x8r8g8b8, x8r8g8b8, PAD)
1181     AFFINE_FAST_PATHS (none_x8r8g8b8, x8r8g8b8, NONE)
1182     AFFINE_FAST_PATHS (reflect_x8r8g8b8, x8r8g8b8, REFLECT)
1183     AFFINE_FAST_PATHS (normal_x8r8g8b8, x8r8g8b8, NORMAL)
1184     AFFINE_FAST_PATHS (pad_a8, a8, PAD)
1185     AFFINE_FAST_PATHS (none_a8, a8, NONE)
1186     AFFINE_FAST_PATHS (reflect_a8, a8, REFLECT)
1187     AFFINE_FAST_PATHS (normal_a8, a8, NORMAL)
1188     AFFINE_FAST_PATHS (pad_r5g6b5, r5g6b5, PAD)
1189     AFFINE_FAST_PATHS (none_r5g6b5, r5g6b5, NONE)
1190     AFFINE_FAST_PATHS (reflect_r5g6b5, r5g6b5, REFLECT)
1191     AFFINE_FAST_PATHS (normal_r5g6b5, r5g6b5, NORMAL)
1192
1193     /* Affine, no alpha */
1194     { PIXMAN_any,
1195       (FAST_PATH_NO_ALPHA_MAP | FAST_PATH_HAS_TRANSFORM | FAST_PATH_AFFINE_TRANSFORM),
1196       bits_image_fetch_affine_no_alpha,
1197       _pixman_image_get_scanline_generic_64
1198     },
1199
1200     /* General */
1201     { PIXMAN_any, 0, bits_image_fetch_general, _pixman_image_get_scanline_generic_64 },
1202
1203     { PIXMAN_null },
1204 };
1205
1206 static void
1207 bits_image_property_changed (pixman_image_t *image)
1208 {
1209     uint32_t flags = image->common.flags;
1210     pixman_format_code_t format = image->common.extended_format_code;
1211     const fetcher_info_t *info;
1212
1213     _pixman_bits_image_setup_accessors (&image->bits);
1214
1215     info = fetcher_info;
1216     while (info->format != PIXMAN_null)
1217     {
1218         if ((info->format == format || info->format == PIXMAN_any)      &&
1219             (info->flags & flags) == info->flags)
1220         {
1221             image->bits.get_scanline_32 = info->fetch_32;
1222             image->bits.get_scanline_64 = info->fetch_64;
1223             break;
1224         }
1225
1226         info++;
1227     }
1228 }
1229
1230 static uint32_t *
1231 src_get_scanline_narrow (pixman_iter_t *iter, const uint32_t *mask)
1232 {
1233     iter->image->bits.get_scanline_32 (
1234         iter->image, iter->x, iter->y++, iter->width, iter->buffer, mask);
1235
1236     return iter->buffer;
1237 }
1238
1239 static uint32_t *
1240 src_get_scanline_wide (pixman_iter_t *iter, const uint32_t *mask)
1241 {
1242     iter->image->bits.get_scanline_64 (
1243         iter->image, iter->x, iter->y++, iter->width, iter->buffer, mask);
1244
1245     return iter->buffer;
1246 }
1247
1248 void
1249 _pixman_bits_image_src_iter_init (pixman_image_t *image, pixman_iter_t *iter)
1250 {
1251     if (iter->iter_flags & ITER_NARROW)
1252         iter->get_scanline = src_get_scanline_narrow;
1253     else
1254         iter->get_scanline = src_get_scanline_wide;
1255 }
1256
1257 static uint32_t *
1258 dest_get_scanline_narrow (pixman_iter_t *iter, const uint32_t *mask)
1259 {
1260     pixman_image_t *image  = iter->image;
1261     int             x      = iter->x;
1262     int             y      = iter->y;
1263     int             width  = iter->width;
1264     uint32_t *      buffer = iter->buffer;
1265
1266     image->bits.fetch_scanline_32 (image, x, y, width, buffer, mask);
1267     if (image->common.alpha_map)
1268     {
1269         uint32_t *alpha;
1270
1271         if ((alpha = malloc (width * sizeof (uint32_t))))
1272         {
1273             int i;
1274
1275             x -= image->common.alpha_origin_x;
1276             y -= image->common.alpha_origin_y;
1277
1278             image->common.alpha_map->fetch_scanline_32 (
1279                 (pixman_image_t *)image->common.alpha_map,
1280                 x, y, width, alpha, mask);
1281
1282             for (i = 0; i < width; ++i)
1283             {
1284                 buffer[i] &= ~0xff000000;
1285                 buffer[i] |= (alpha[i] & 0xff000000);
1286             }
1287
1288             free (alpha);
1289         }
1290     }
1291
1292     return iter->buffer;
1293 }
1294
1295 static uint32_t *
1296 dest_get_scanline_wide (pixman_iter_t *iter, const uint32_t *mask)
1297 {
1298     bits_image_t *  image  = &iter->image->bits;
1299     int             x      = iter->x;
1300     int             y      = iter->y;
1301     int             width  = iter->width;
1302     uint64_t *      buffer = (uint64_t *)iter->buffer;
1303
1304     image->fetch_scanline_64 (
1305         (pixman_image_t *)image, x, y, width, (uint32_t *)buffer, mask);
1306     if (image->common.alpha_map)
1307     {
1308         uint64_t *alpha;
1309
1310         if ((alpha = malloc (width * sizeof (uint64_t))))
1311         {
1312             int i;
1313
1314             x -= image->common.alpha_origin_x;
1315             y -= image->common.alpha_origin_y;
1316
1317             image->common.alpha_map->fetch_scanline_64 (
1318                 (pixman_image_t *)image->common.alpha_map,
1319                 x, y, width, (uint32_t *)alpha, mask);
1320
1321             for (i = 0; i < width; ++i)
1322             {
1323                 buffer[i] &= ~0xffff000000000000ULL;
1324                 buffer[i] |= (alpha[i] & 0xffff000000000000ULL);
1325             }
1326
1327             free (alpha);
1328         }
1329     }
1330
1331     return iter->buffer;
1332 }
1333
1334 static void
1335 dest_write_back_narrow (pixman_iter_t *iter)
1336 {
1337     bits_image_t *  image  = &iter->image->bits;
1338     int             x      = iter->x;
1339     int             y      = iter->y;
1340     int             width  = iter->width;
1341     const uint32_t *buffer = iter->buffer;
1342
1343     image->store_scanline_32 (image, x, y, width, buffer);
1344
1345     if (image->common.alpha_map)
1346     {
1347         x -= image->common.alpha_origin_x;
1348         y -= image->common.alpha_origin_y;
1349
1350         image->common.alpha_map->store_scanline_32 (
1351             image->common.alpha_map, x, y, width, buffer);
1352     }
1353
1354     iter->y++;
1355 }
1356
1357 static void
1358 dest_write_back_wide (pixman_iter_t *iter)
1359 {
1360     bits_image_t *  image  = &iter->image->bits;
1361     int             x      = iter->x;
1362     int             y      = iter->y;
1363     int             width  = iter->width;
1364     const uint32_t *buffer = iter->buffer;
1365
1366     image->store_scanline_64 (image, x, y, width, buffer);
1367
1368     if (image->common.alpha_map)
1369     {
1370         x -= image->common.alpha_origin_x;
1371         y -= image->common.alpha_origin_y;
1372
1373         image->common.alpha_map->store_scanline_64 (
1374             image->common.alpha_map, x, y, width, buffer);
1375     }
1376
1377     iter->y++;
1378 }
1379
1380 void
1381 _pixman_bits_image_dest_iter_init (pixman_image_t *image, pixman_iter_t *iter)
1382 {
1383     if (iter->iter_flags & ITER_NARROW)
1384     {
1385         if ((iter->iter_flags & (ITER_IGNORE_RGB | ITER_IGNORE_ALPHA)) ==
1386             (ITER_IGNORE_RGB | ITER_IGNORE_ALPHA))
1387         {
1388             iter->get_scanline = _pixman_iter_get_scanline_noop;
1389         }
1390         else
1391         {
1392             iter->get_scanline = dest_get_scanline_narrow;
1393         }
1394         
1395         iter->write_back = dest_write_back_narrow;
1396     }
1397     else
1398     {
1399         iter->get_scanline = dest_get_scanline_wide;
1400         iter->write_back = dest_write_back_wide;
1401     }
1402 }
1403
1404 static uint32_t *
1405 create_bits (pixman_format_code_t format,
1406              int                  width,
1407              int                  height,
1408              int *                rowstride_bytes)
1409 {
1410     int stride;
1411     size_t buf_size;
1412     int bpp;
1413
1414     /* what follows is a long-winded way, avoiding any possibility of integer
1415      * overflows, of saying:
1416      * stride = ((width * bpp + 0x1f) >> 5) * sizeof (uint32_t);
1417      */
1418
1419     bpp = PIXMAN_FORMAT_BPP (format);
1420     if (_pixman_multiply_overflows_int (width, bpp))
1421         return NULL;
1422
1423     stride = width * bpp;
1424     if (_pixman_addition_overflows_int (stride, 0x1f))
1425         return NULL;
1426
1427     stride += 0x1f;
1428     stride >>= 5;
1429
1430     stride *= sizeof (uint32_t);
1431
1432     if (_pixman_multiply_overflows_size (height, stride))
1433         return NULL;
1434
1435     buf_size = height * stride;
1436
1437     if (rowstride_bytes)
1438         *rowstride_bytes = stride;
1439
1440     return calloc (buf_size, 1);
1441 }
1442
1443 pixman_bool_t
1444 _pixman_bits_image_init (pixman_image_t *     image,
1445                          pixman_format_code_t format,
1446                          int                  width,
1447                          int                  height,
1448                          uint32_t *           bits,
1449                          int                  rowstride)
1450 {
1451     uint32_t *free_me = NULL;
1452
1453     if (!bits && width && height)
1454     {
1455         int rowstride_bytes;
1456
1457         free_me = bits = create_bits (format, width, height, &rowstride_bytes);
1458
1459         if (!bits)
1460             return FALSE;
1461
1462         rowstride = rowstride_bytes / (int) sizeof (uint32_t);
1463     }
1464
1465     _pixman_image_init (image);
1466
1467     image->type = BITS;
1468     image->bits.format = format;
1469     image->bits.width = width;
1470     image->bits.height = height;
1471     image->bits.bits = bits;
1472     image->bits.free_me = free_me;
1473     image->bits.read_func = NULL;
1474     image->bits.write_func = NULL;
1475     image->bits.rowstride = rowstride;
1476     image->bits.indexed = NULL;
1477
1478     image->common.property_changed = bits_image_property_changed;
1479
1480     _pixman_image_reset_clip_region (image);
1481
1482     return TRUE;
1483 }
1484
1485 PIXMAN_EXPORT pixman_image_t *
1486 pixman_image_create_bits (pixman_format_code_t format,
1487                           int                  width,
1488                           int                  height,
1489                           uint32_t *           bits,
1490                           int                  rowstride_bytes)
1491 {
1492     pixman_image_t *image;
1493
1494     /* must be a whole number of uint32_t's
1495      */
1496     return_val_if_fail (
1497         bits == NULL || (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
1498
1499     return_val_if_fail (PIXMAN_FORMAT_BPP (format) >= PIXMAN_FORMAT_DEPTH (format), NULL);
1500
1501     image = _pixman_image_allocate ();
1502
1503     if (!image)
1504         return NULL;
1505
1506     if (!_pixman_bits_image_init (image, format, width, height, bits,
1507                                   rowstride_bytes / (int) sizeof (uint32_t)))
1508     {
1509         free (image);
1510         return NULL;
1511     }
1512
1513     return image;
1514 }