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