Remove useless checks for NULL before freeing
[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 srtot, sgtot, sbtot, satot;
385     int32_t i, j, x1, x2, y1, y2;
386     pixman_repeat_t repeat_mode = image->common.repeat;
387     int width = image->width;
388     int height = image->height;
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 += RED_8 (pixel) * f;
425                 sgtot += GREEN_8 (pixel) * f;
426                 sbtot += BLUE_8 (pixel) * f;
427                 satot += 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_solid_32 (pixman_image_t * image,
974                            int              x,
975                            int              y,
976                            int              width,
977                            uint32_t *       buffer,
978                            const uint32_t * mask)
979 {
980     replicate_pixel_32 (&image->bits, 0, 0, width, buffer);
981 }
982
983 static void
984 bits_image_fetch_solid_64 (pixman_image_t * image,
985                            int              x,
986                            int              y,
987                            int              width,
988                            uint32_t *       b,
989                            const uint32_t * unused)
990 {
991     replicate_pixel_64 (&image->bits, 0, 0, width, b);
992 }
993
994 static void
995 bits_image_fetch_untransformed_repeat_none (bits_image_t *image,
996                                             pixman_bool_t wide,
997                                             int           x,
998                                             int           y,
999                                             int           width,
1000                                             uint32_t *    buffer)
1001 {
1002     uint32_t w;
1003
1004     if (y < 0 || y >= image->height)
1005     {
1006         memset (buffer, 0, width * (wide? 8 : 4));
1007         return;
1008     }
1009
1010     if (x < 0)
1011     {
1012         w = MIN (width, -x);
1013
1014         memset (buffer, 0, w * (wide ? 8 : 4));
1015
1016         width -= w;
1017         buffer += w * (wide? 2 : 1);
1018         x += w;
1019     }
1020
1021     if (x < image->width)
1022     {
1023         w = MIN (width, image->width - x);
1024
1025         if (wide)
1026             image->fetch_scanline_64 ((pixman_image_t *)image, x, y, w, buffer, NULL);
1027         else
1028             image->fetch_scanline_32 ((pixman_image_t *)image, x, y, w, buffer, NULL);
1029
1030         width -= w;
1031         buffer += w * (wide? 2 : 1);
1032         x += w;
1033     }
1034
1035     memset (buffer, 0, width * (wide ? 8 : 4));
1036 }
1037
1038 static void
1039 bits_image_fetch_untransformed_repeat_normal (bits_image_t *image,
1040                                               pixman_bool_t wide,
1041                                               int           x,
1042                                               int           y,
1043                                               int           width,
1044                                               uint32_t *    buffer)
1045 {
1046     uint32_t w;
1047
1048     while (y < 0)
1049         y += image->height;
1050
1051     while (y >= image->height)
1052         y -= image->height;
1053
1054     if (image->width == 1)
1055     {
1056         if (wide)
1057             replicate_pixel_64 (image, 0, y, width, buffer);
1058         else
1059             replicate_pixel_32 (image, 0, y, width, buffer);
1060
1061         return;
1062     }
1063
1064     while (width)
1065     {
1066         while (x < 0)
1067             x += image->width;
1068         while (x >= image->width)
1069             x -= image->width;
1070
1071         w = MIN (width, image->width - x);
1072
1073         if (wide)
1074             image->fetch_scanline_64 ((pixman_image_t *)image, x, y, w, buffer, NULL);
1075         else
1076             image->fetch_scanline_32 ((pixman_image_t *)image, x, y, w, buffer, NULL);
1077
1078         buffer += w * (wide? 2 : 1);
1079         x += w;
1080         width -= w;
1081     }
1082 }
1083
1084 static void
1085 bits_image_fetch_untransformed_32 (pixman_image_t * image,
1086                                    int              x,
1087                                    int              y,
1088                                    int              width,
1089                                    uint32_t *       buffer,
1090                                    const uint32_t * mask)
1091 {
1092     if (image->common.repeat == PIXMAN_REPEAT_NONE)
1093     {
1094         bits_image_fetch_untransformed_repeat_none (
1095             &image->bits, FALSE, x, y, width, buffer);
1096     }
1097     else
1098     {
1099         bits_image_fetch_untransformed_repeat_normal (
1100             &image->bits, FALSE, x, y, width, buffer);
1101     }
1102 }
1103
1104 static void
1105 bits_image_fetch_untransformed_64 (pixman_image_t * image,
1106                                    int              x,
1107                                    int              y,
1108                                    int              width,
1109                                    uint32_t *       buffer,
1110                                    const uint32_t * unused)
1111 {
1112     if (image->common.repeat == PIXMAN_REPEAT_NONE)
1113     {
1114         bits_image_fetch_untransformed_repeat_none (
1115             &image->bits, TRUE, x, y, width, buffer);
1116     }
1117     else
1118     {
1119         bits_image_fetch_untransformed_repeat_normal (
1120             &image->bits, TRUE, x, y, width, buffer);
1121     }
1122 }
1123
1124 typedef struct
1125 {
1126     pixman_format_code_t        format;
1127     uint32_t                    flags;
1128     fetch_scanline_t            fetch_32;
1129     fetch_scanline_t            fetch_64;
1130 } fetcher_info_t;
1131
1132 static const fetcher_info_t fetcher_info[] =
1133 {
1134     { PIXMAN_solid,
1135       FAST_PATH_NO_ALPHA_MAP,
1136       bits_image_fetch_solid_32,
1137       bits_image_fetch_solid_64
1138     },
1139
1140     { PIXMAN_any,
1141       (FAST_PATH_NO_ALPHA_MAP                   |
1142        FAST_PATH_ID_TRANSFORM                   |
1143        FAST_PATH_NO_CONVOLUTION_FILTER          |
1144        FAST_PATH_NO_PAD_REPEAT                  |
1145        FAST_PATH_NO_REFLECT_REPEAT),
1146       bits_image_fetch_untransformed_32,
1147       bits_image_fetch_untransformed_64
1148     },
1149
1150 #define FAST_BILINEAR_FLAGS                                             \
1151     (FAST_PATH_NO_ALPHA_MAP             |                               \
1152      FAST_PATH_NO_ACCESSORS             |                               \
1153      FAST_PATH_HAS_TRANSFORM            |                               \
1154      FAST_PATH_AFFINE_TRANSFORM         |                               \
1155      FAST_PATH_X_UNIT_POSITIVE          |                               \
1156      FAST_PATH_Y_UNIT_ZERO              |                               \
1157      FAST_PATH_NONE_REPEAT              |                               \
1158      FAST_PATH_BILINEAR_FILTER)
1159
1160     { PIXMAN_a8r8g8b8,
1161       FAST_BILINEAR_FLAGS,
1162       bits_image_fetch_bilinear_no_repeat_8888,
1163       _pixman_image_get_scanline_generic_64
1164     },
1165
1166     { PIXMAN_x8r8g8b8,
1167       FAST_BILINEAR_FLAGS,
1168       bits_image_fetch_bilinear_no_repeat_8888,
1169       _pixman_image_get_scanline_generic_64
1170     },
1171
1172 #define GENERAL_BILINEAR_FLAGS                                          \
1173     (FAST_PATH_NO_ALPHA_MAP             |                               \
1174      FAST_PATH_NO_ACCESSORS             |                               \
1175      FAST_PATH_HAS_TRANSFORM            |                               \
1176      FAST_PATH_AFFINE_TRANSFORM         |                               \
1177      FAST_PATH_BILINEAR_FILTER)
1178
1179 #define GENERAL_NEAREST_FLAGS                                           \
1180     (FAST_PATH_NO_ALPHA_MAP             |                               \
1181      FAST_PATH_NO_ACCESSORS             |                               \
1182      FAST_PATH_HAS_TRANSFORM            |                               \
1183      FAST_PATH_AFFINE_TRANSFORM         |                               \
1184      FAST_PATH_NEAREST_FILTER)
1185
1186 #define BILINEAR_AFFINE_FAST_PATH(name, format, repeat)                 \
1187     { PIXMAN_ ## format,                                                \
1188       GENERAL_BILINEAR_FLAGS | FAST_PATH_ ## repeat ## _REPEAT,         \
1189       bits_image_fetch_bilinear_affine_ ## name,                        \
1190       _pixman_image_get_scanline_generic_64                             \
1191     },
1192
1193 #define NEAREST_AFFINE_FAST_PATH(name, format, repeat)                  \
1194     { PIXMAN_ ## format,                                                \
1195       GENERAL_NEAREST_FLAGS | FAST_PATH_ ## repeat ## _REPEAT,          \
1196       bits_image_fetch_nearest_affine_ ## name,                 \
1197       _pixman_image_get_scanline_generic_64                             \
1198     },
1199
1200 #define AFFINE_FAST_PATHS(name, format, repeat)                         \
1201     BILINEAR_AFFINE_FAST_PATH(name, format, repeat)                     \
1202     NEAREST_AFFINE_FAST_PATH(name, format, repeat)
1203     
1204     AFFINE_FAST_PATHS (pad_a8r8g8b8, a8r8g8b8, PAD)
1205     AFFINE_FAST_PATHS (none_a8r8g8b8, a8r8g8b8, NONE)
1206     AFFINE_FAST_PATHS (reflect_a8r8g8b8, a8r8g8b8, REFLECT)
1207     AFFINE_FAST_PATHS (normal_a8r8g8b8, a8r8g8b8, NORMAL)
1208     AFFINE_FAST_PATHS (pad_x8r8g8b8, x8r8g8b8, PAD)
1209     AFFINE_FAST_PATHS (none_x8r8g8b8, x8r8g8b8, NONE)
1210     AFFINE_FAST_PATHS (reflect_x8r8g8b8, x8r8g8b8, REFLECT)
1211     AFFINE_FAST_PATHS (normal_x8r8g8b8, x8r8g8b8, NORMAL)
1212     AFFINE_FAST_PATHS (pad_a8, a8, PAD)
1213     AFFINE_FAST_PATHS (none_a8, a8, NONE)
1214     AFFINE_FAST_PATHS (reflect_a8, a8, REFLECT)
1215     AFFINE_FAST_PATHS (normal_a8, a8, NORMAL)
1216     AFFINE_FAST_PATHS (pad_r5g6b5, r5g6b5, PAD)
1217     AFFINE_FAST_PATHS (none_r5g6b5, r5g6b5, NONE)
1218     AFFINE_FAST_PATHS (reflect_r5g6b5, r5g6b5, REFLECT)
1219     AFFINE_FAST_PATHS (normal_r5g6b5, r5g6b5, NORMAL)
1220
1221     /* Affine, no alpha */
1222     { PIXMAN_any,
1223       (FAST_PATH_NO_ALPHA_MAP | FAST_PATH_HAS_TRANSFORM | FAST_PATH_AFFINE_TRANSFORM),
1224       bits_image_fetch_affine_no_alpha,
1225       _pixman_image_get_scanline_generic_64
1226     },
1227
1228     /* General */
1229     { PIXMAN_any, 0, bits_image_fetch_general, _pixman_image_get_scanline_generic_64 },
1230
1231     { PIXMAN_null },
1232 };
1233
1234 static void
1235 bits_image_property_changed (pixman_image_t *image)
1236 {
1237     uint32_t flags = image->common.flags;
1238     pixman_format_code_t format = image->common.extended_format_code;
1239     const fetcher_info_t *info;
1240
1241     _pixman_bits_image_setup_accessors (&image->bits);
1242
1243     info = fetcher_info;
1244     while (info->format != PIXMAN_null)
1245     {
1246         if ((info->format == format || info->format == PIXMAN_any)      &&
1247             (info->flags & flags) == info->flags)
1248         {
1249             image->bits.get_scanline_32 = info->fetch_32;
1250             image->bits.get_scanline_64 = info->fetch_64;
1251             break;
1252         }
1253
1254         info++;
1255     }
1256 }
1257
1258 static uint32_t *
1259 src_get_scanline_narrow (pixman_iter_t *iter, const uint32_t *mask)
1260 {
1261     iter->image->bits.get_scanline_32 (
1262         iter->image, iter->x, iter->y++, iter->width, iter->buffer, mask);
1263
1264     return iter->buffer;
1265 }
1266
1267 static uint32_t *
1268 src_get_scanline_wide (pixman_iter_t *iter, const uint32_t *mask)
1269 {
1270     iter->image->bits.get_scanline_64 (
1271         iter->image, iter->x, iter->y++, iter->width, iter->buffer, mask);
1272
1273     return iter->buffer;
1274 }
1275
1276 void
1277 _pixman_bits_image_src_iter_init (pixman_image_t *image, pixman_iter_t *iter)
1278 {
1279     if (iter->flags & ITER_NARROW)
1280         iter->get_scanline = src_get_scanline_narrow;
1281     else
1282         iter->get_scanline = src_get_scanline_wide;
1283 }
1284
1285 static uint32_t *
1286 dest_get_scanline_narrow (pixman_iter_t *iter, const uint32_t *mask)
1287 {
1288     pixman_image_t *image  = iter->image;
1289     int             x      = iter->x;
1290     int             y      = iter->y;
1291     int             width  = iter->width;
1292     uint32_t *      buffer = iter->buffer;
1293
1294     image->bits.fetch_scanline_32 (image, x, y, width, buffer, mask);
1295     if (image->common.alpha_map)
1296     {
1297         x -= image->common.alpha_origin_x;
1298         y -= image->common.alpha_origin_y;
1299
1300         image->common.alpha_map->fetch_scanline_32 (
1301             (pixman_image_t *)image->common.alpha_map,
1302             x, y, width, buffer, mask);
1303     }
1304
1305     return iter->buffer;
1306 }
1307
1308 static uint32_t *
1309 dest_get_scanline_wide (pixman_iter_t *iter, const uint32_t *mask)
1310 {
1311     bits_image_t *  image  = &iter->image->bits;
1312     int             x      = iter->x;
1313     int             y      = iter->y;
1314     int             width  = iter->width;
1315     uint32_t *      buffer = iter->buffer;
1316
1317     image->fetch_scanline_64 (
1318         (pixman_image_t *)image, x, y, width, buffer, mask);
1319     if (image->common.alpha_map)
1320     {
1321         x -= image->common.alpha_origin_x;
1322         y -= image->common.alpha_origin_y;
1323
1324         image->common.alpha_map->fetch_scanline_64 (
1325             (pixman_image_t *)image->common.alpha_map, x, y, width, buffer, mask);
1326     }
1327
1328     return iter->buffer;
1329 }
1330
1331 static void
1332 dest_write_back_narrow (pixman_iter_t *iter)
1333 {
1334     bits_image_t *  image  = &iter->image->bits;
1335     int             x      = iter->x;
1336     int             y      = iter->y;
1337     int             width  = iter->width;
1338     const uint32_t *buffer = iter->buffer;
1339
1340     image->store_scanline_32 (image, x, y, width, buffer);
1341
1342     if (image->common.alpha_map)
1343     {
1344         x -= image->common.alpha_origin_x;
1345         y -= image->common.alpha_origin_y;
1346
1347         image->common.alpha_map->store_scanline_32 (
1348             image->common.alpha_map, x, y, width, buffer);
1349     }
1350
1351     iter->y++;
1352 }
1353
1354 static void
1355 dest_write_back_wide (pixman_iter_t *iter)
1356 {
1357     bits_image_t *  image  = &iter->image->bits;
1358     int             x      = iter->x;
1359     int             y      = iter->y;
1360     int             width  = iter->width;
1361     const uint32_t *buffer = iter->buffer;
1362
1363     image->store_scanline_64 (image, x, y, width, buffer);
1364
1365     if (image->common.alpha_map)
1366     {
1367         x -= image->common.alpha_origin_x;
1368         y -= image->common.alpha_origin_y;
1369
1370         image->common.alpha_map->store_scanline_64 (
1371             image->common.alpha_map, x, y, width, buffer);
1372     }
1373
1374     iter->y++;
1375 }
1376
1377 void
1378 _pixman_bits_image_dest_iter_init (pixman_image_t *image, pixman_iter_t *iter)
1379 {
1380     if (iter->flags & ITER_NARROW)
1381     {
1382         if ((iter->flags & (ITER_IGNORE_RGB | ITER_IGNORE_ALPHA)) ==
1383             (ITER_IGNORE_RGB | ITER_IGNORE_ALPHA))
1384         {
1385             iter->get_scanline = _pixman_iter_get_scanline_noop;
1386         }
1387         else
1388         {
1389             iter->get_scanline = dest_get_scanline_narrow;
1390         }
1391         
1392         iter->write_back = dest_write_back_narrow;
1393     }
1394     else
1395     {
1396         iter->get_scanline = dest_get_scanline_wide;
1397         iter->write_back = dest_write_back_wide;
1398     }
1399 }
1400
1401 static uint32_t *
1402 create_bits (pixman_format_code_t format,
1403              int                  width,
1404              int                  height,
1405              int *                rowstride_bytes)
1406 {
1407     int stride;
1408     size_t buf_size;
1409     int bpp;
1410
1411     /* what follows is a long-winded way, avoiding any possibility of integer
1412      * overflows, of saying:
1413      * stride = ((width * bpp + 0x1f) >> 5) * sizeof (uint32_t);
1414      */
1415
1416     bpp = PIXMAN_FORMAT_BPP (format);
1417     if (_pixman_multiply_overflows_int (width, bpp))
1418         return NULL;
1419
1420     stride = width * bpp;
1421     if (_pixman_addition_overflows_int (stride, 0x1f))
1422         return NULL;
1423
1424     stride += 0x1f;
1425     stride >>= 5;
1426
1427     stride *= sizeof (uint32_t);
1428
1429     if (_pixman_multiply_overflows_size (height, stride))
1430         return NULL;
1431
1432     buf_size = height * stride;
1433
1434     if (rowstride_bytes)
1435         *rowstride_bytes = stride;
1436
1437     return calloc (buf_size, 1);
1438 }
1439
1440 pixman_bool_t
1441 _pixman_bits_image_init (pixman_image_t *     image,
1442                          pixman_format_code_t format,
1443                          int                  width,
1444                          int                  height,
1445                          uint32_t *           bits,
1446                          int                  rowstride)
1447 {
1448     uint32_t *free_me = NULL;
1449
1450     if (!bits && width && height)
1451     {
1452         int rowstride_bytes;
1453
1454         free_me = bits = create_bits (format, width, height, &rowstride_bytes);
1455
1456         if (!bits)
1457             return FALSE;
1458
1459         rowstride = rowstride_bytes / (int) sizeof (uint32_t);
1460     }
1461
1462     _pixman_image_init (image);
1463
1464     image->type = BITS;
1465     image->bits.format = format;
1466     image->bits.width = width;
1467     image->bits.height = height;
1468     image->bits.bits = bits;
1469     image->bits.free_me = free_me;
1470     image->bits.read_func = NULL;
1471     image->bits.write_func = NULL;
1472     image->bits.rowstride = rowstride;
1473     image->bits.indexed = NULL;
1474
1475     image->common.property_changed = bits_image_property_changed;
1476
1477     _pixman_image_reset_clip_region (image);
1478
1479     return TRUE;
1480 }
1481
1482 PIXMAN_EXPORT pixman_image_t *
1483 pixman_image_create_bits (pixman_format_code_t format,
1484                           int                  width,
1485                           int                  height,
1486                           uint32_t *           bits,
1487                           int                  rowstride_bytes)
1488 {
1489     pixman_image_t *image;
1490
1491     /* must be a whole number of uint32_t's
1492      */
1493     return_val_if_fail (
1494         bits == NULL || (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
1495
1496     return_val_if_fail (PIXMAN_FORMAT_BPP (format) >= PIXMAN_FORMAT_DEPTH (format), NULL);
1497
1498     image = _pixman_image_allocate ();
1499
1500     if (!image)
1501         return NULL;
1502
1503     if (!_pixman_bits_image_init (image, format, width, height, bits,
1504                                   rowstride_bytes / (int) sizeof (uint32_t)))
1505     {
1506         free (image);
1507         return NULL;
1508     }
1509
1510     return image;
1511 }