Move SourcePictClassify into pixman-image.c
[profile/ivi/pixman.git] / pixman / pixman-image.c
1 /*
2  * Copyright © 2000 SuSE, Inc.
3  * Copyright © 2007 Red Hat, Inc.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation, and that the name of SuSE not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  SuSE makes no representations about the
12  * suitability of this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  *
15  * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
17  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "pixman-private.h"
32
33 #define Alpha(x) ((x) >> 24)
34
35 static source_pict_class_t
36 SourcePictureClassify (pixman_image_t *image,
37                        int             x,
38                        int             y,
39                        int             width,
40                        int             height)
41 {
42     source_image_t *pict = &image->source;
43     
44     if (pict->class != SOURCE_IMAGE_CLASS_UNKNOWN)
45         return pict->class;
46
47     pict->class = SOURCE_IMAGE_CLASS_NEITHER;
48     
49     if (pict->common.type == SOLID)
50     {
51         pict->class = SOURCE_IMAGE_CLASS_HORIZONTAL;
52     }
53     else if (pict->common.type == LINEAR)
54     {
55         linear_gradient_t *linear = (linear_gradient_t *)pict;
56         pixman_vector_t   v;
57         pixman_fixed_32_32_t l;
58         pixman_fixed_48_16_t dx, dy, a, b, off;
59         pixman_fixed_48_16_t factors[4];
60         int          i;
61
62         dx = linear->p2.x - linear->p1.x;
63         dy = linear->p2.y - linear->p1.y;
64         l = dx * dx + dy * dy;
65         if (l)
66         {
67             a = (dx << 32) / l;
68             b = (dy << 32) / l;
69         }
70         else
71         {
72             a = b = 0;
73         }
74
75         off = (-a * linear->p1.x
76                -b * linear->p1.y) >> 16;
77
78         for (i = 0; i < 3; i++)
79         {
80             v.vector[0] = pixman_int_to_fixed ((i % 2) * (width  - 1) + x);
81             v.vector[1] = pixman_int_to_fixed ((i / 2) * (height - 1) + y);
82             v.vector[2] = pixman_fixed_1;
83
84             if (pict->common.transform)
85             {
86                 if (!pixman_transform_point_3d (pict->common.transform, &v))
87                     return SOURCE_IMAGE_CLASS_UNKNOWN;
88             }
89
90             factors[i] = ((a * v.vector[0] + b * v.vector[1]) >> 16) + off;
91         }
92
93         if (factors[2] == factors[0])
94             pict->class = SOURCE_IMAGE_CLASS_HORIZONTAL;
95         else if (factors[1] == factors[0])
96             pict->class = SOURCE_IMAGE_CLASS_VERTICAL;
97     }
98
99     return pict->class;
100 }
101
102 static void
103 init_source_image (source_image_t *image)
104 {
105     image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
106     image->common.classify = SourcePictureClassify;
107 }
108
109 static pixman_bool_t
110 init_gradient (gradient_t     *gradient,
111                const pixman_gradient_stop_t *stops,
112                int             n_stops)
113 {
114     return_val_if_fail (n_stops > 0, FALSE);
115
116     init_source_image (&gradient->common);
117
118     gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
119     if (!gradient->stops)
120         return FALSE;
121
122     memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
123
124     gradient->n_stops = n_stops;
125
126     gradient->stop_range = 0xffff;
127     gradient->color_table = NULL;
128     gradient->color_table_size = 0;
129
130     return TRUE;
131 }
132
133 static uint32_t
134 color_to_uint32 (const pixman_color_t *color)
135 {
136     return
137         (color->alpha >> 8 << 24) |
138         (color->red >> 8 << 16) |
139         (color->green & 0xff00) |
140         (color->blue >> 8);
141 }
142
143 static pixman_image_t *
144 allocate_image (void)
145 {
146     pixman_image_t *image = malloc (sizeof (pixman_image_t));
147
148     if (image)
149     {
150         image_common_t *common = &image->common;
151
152         pixman_region32_init (&common->full_region);
153         pixman_region32_init (&common->clip_region);
154         common->src_clip = &common->full_region;
155         common->has_client_clip = FALSE;
156         common->transform = NULL;
157         common->repeat = PIXMAN_REPEAT_NONE;
158         common->filter = PIXMAN_FILTER_NEAREST;
159         common->filter_params = NULL;
160         common->n_filter_params = 0;
161         common->alpha_map = NULL;
162         common->component_alpha = FALSE;
163         common->ref_count = 1;
164         common->read_func = NULL;
165         common->write_func = NULL;
166     }
167
168     return image;
169 }
170
171 source_pict_class_t
172 _pixman_image_classify (pixman_image_t *image,
173                         int             x,
174                         int             y,
175                         int             width,
176                         int             height)
177 {
178     if (image->common.classify)
179         return image->common.classify (image, x, y, width, height);
180     else
181         return SOURCE_IMAGE_CLASS_NEITHER;
182 }
183
184 /* Ref Counting */
185 PIXMAN_EXPORT pixman_image_t *
186 pixman_image_ref (pixman_image_t *image)
187 {
188     image->common.ref_count++;
189
190     return image;
191 }
192
193 /* returns TRUE when the image is freed */
194 PIXMAN_EXPORT pixman_bool_t
195 pixman_image_unref (pixman_image_t *image)
196 {
197     image_common_t *common = (image_common_t *)image;
198
199     common->ref_count--;
200
201     if (common->ref_count == 0)
202     {
203         pixman_region32_fini (&common->clip_region);
204         pixman_region32_fini (&common->full_region);
205
206         if (common->transform)
207             free (common->transform);
208
209         if (common->filter_params)
210             free (common->filter_params);
211
212         if (common->alpha_map)
213             pixman_image_unref ((pixman_image_t *)common->alpha_map);
214
215 #if 0
216         if (image->type == BITS && image->bits.indexed)
217             free (image->bits.indexed);
218 #endif
219
220 #if 0
221         memset (image, 0xaa, sizeof (pixman_image_t));
222 #endif
223         if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
224         {
225             if (image->gradient.stops)
226                 free (image->gradient.stops);
227         }
228
229
230         if (image->type == BITS && image->bits.free_me)
231             free (image->bits.free_me);
232
233         free (image);
234
235         return TRUE;
236     }
237
238     return FALSE;
239 }
240
241 /* Constructors */
242 PIXMAN_EXPORT pixman_image_t *
243 pixman_image_create_solid_fill (pixman_color_t *color)
244 {
245     pixman_image_t *img = allocate_image();
246     if (!img)
247         return NULL;
248
249     init_source_image (&img->solid.common);
250
251     img->type = SOLID;
252     img->solid.color = color_to_uint32 (color);
253
254     return img;
255 }
256
257 PIXMAN_EXPORT pixman_image_t *
258 pixman_image_create_linear_gradient (pixman_point_fixed_t         *p1,
259                                      pixman_point_fixed_t         *p2,
260                                      const pixman_gradient_stop_t *stops,
261                                      int                           n_stops)
262 {
263     pixman_image_t *image;
264     linear_gradient_t *linear;
265
266     return_val_if_fail (n_stops >= 2, NULL);
267
268     image = allocate_image();
269
270     if (!image)
271         return NULL;
272
273     linear = &image->linear;
274
275     if (!init_gradient (&linear->common, stops, n_stops))
276     {
277         free (image);
278         return NULL;
279     }
280
281     linear->p1 = *p1;
282     linear->p2 = *p2;
283
284     image->type = LINEAR;
285
286     return image;
287 }
288
289
290 PIXMAN_EXPORT pixman_image_t *
291 pixman_image_create_radial_gradient (pixman_point_fixed_t         *inner,
292                                      pixman_point_fixed_t         *outer,
293                                      pixman_fixed_t                inner_radius,
294                                      pixman_fixed_t                outer_radius,
295                                      const pixman_gradient_stop_t *stops,
296                                      int                           n_stops)
297 {
298     pixman_image_t *image;
299     radial_gradient_t *radial;
300
301     return_val_if_fail (n_stops >= 2, NULL);
302
303     image = allocate_image();
304
305     if (!image)
306         return NULL;
307
308     radial = &image->radial;
309
310     if (!init_gradient (&radial->common, stops, n_stops))
311     {
312         free (image);
313         return NULL;
314     }
315
316     image->type = RADIAL;
317
318     radial->c1.x = inner->x;
319     radial->c1.y = inner->y;
320     radial->c1.radius = inner_radius;
321     radial->c2.x = outer->x;
322     radial->c2.y = outer->y;
323     radial->c2.radius = outer_radius;
324     radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
325     radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
326     radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
327     radial->A = (radial->cdx * radial->cdx
328                  + radial->cdy * radial->cdy
329                  - radial->dr  * radial->dr);
330
331     return image;
332 }
333
334 PIXMAN_EXPORT pixman_image_t *
335 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
336                                       pixman_fixed_t angle,
337                                       const pixman_gradient_stop_t *stops,
338                                       int n_stops)
339 {
340     pixman_image_t *image = allocate_image();
341     conical_gradient_t *conical;
342
343     if (!image)
344         return NULL;
345
346     conical = &image->conical;
347
348     if (!init_gradient (&conical->common, stops, n_stops))
349     {
350         free (image);
351         return NULL;
352     }
353
354     image->type = CONICAL;
355     conical->center = *center;
356     conical->angle = angle;
357
358     return image;
359 }
360
361 static uint32_t *
362 create_bits (pixman_format_code_t format,
363              int                  width,
364              int                  height,
365              int                 *rowstride_bytes)
366 {
367     int stride;
368     int buf_size;
369     int bpp;
370
371     /* what follows is a long-winded way, avoiding any possibility of integer
372      * overflows, of saying:
373      * stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
374      */
375
376     bpp = PIXMAN_FORMAT_BPP (format);
377     if (pixman_multiply_overflows_int (width, bpp))
378         return NULL;
379
380     stride = width * bpp;
381     if (pixman_addition_overflows_int (stride, FB_MASK))
382         return NULL;
383
384     stride += FB_MASK;
385     stride >>= FB_SHIFT;
386
387 #if FB_SHIFT < 2
388     if (pixman_multiply_overflows_int (stride, sizeof (uint32_t)))
389         return NULL;
390 #endif
391     stride *= sizeof (uint32_t);
392
393     if (pixman_multiply_overflows_int (height, stride))
394         return NULL;
395
396     buf_size = height * stride;
397
398     if (rowstride_bytes)
399         *rowstride_bytes = stride;
400
401     return calloc (buf_size, 1);
402 }
403
404 static void
405 reset_clip_region (pixman_image_t *image)
406 {
407     pixman_region32_fini (&image->common.clip_region);
408
409     if (image->type == BITS)
410     {
411         pixman_region32_init_rect (&image->common.clip_region, 0, 0,
412                                    image->bits.width, image->bits.height);
413     }
414     else
415     {
416         pixman_region32_init (&image->common.clip_region);
417     }
418 }
419
420 PIXMAN_EXPORT pixman_image_t *
421 pixman_image_create_bits (pixman_format_code_t  format,
422                           int                   width,
423                           int                   height,
424                           uint32_t             *bits,
425                           int                   rowstride_bytes)
426 {
427     pixman_image_t *image;
428     uint32_t *free_me = NULL;
429
430     /* must be a whole number of uint32_t's
431      */
432     return_val_if_fail (bits == NULL ||
433                         (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
434
435     if (!bits && width && height)
436     {
437         free_me = bits = create_bits (format, width, height, &rowstride_bytes);
438         if (!bits)
439             return NULL;
440     }
441
442     image = allocate_image();
443
444     if (!image) {
445         if (free_me)
446             free (free_me);
447         return NULL;
448     }
449     
450     image->type = BITS;
451     image->bits.format = format;
452     image->bits.width = width;
453     image->bits.height = height;
454     image->bits.bits = bits;
455     image->bits.free_me = free_me;
456
457     image->bits.rowstride = rowstride_bytes / (int) sizeof (uint32_t); /* we store it in number
458                                                                   * of uint32_t's
459                                                                   */
460     image->bits.indexed = NULL;
461
462     pixman_region32_fini (&image->common.full_region);
463     pixman_region32_init_rect (&image->common.full_region, 0, 0,
464                                image->bits.width, image->bits.height);
465
466     reset_clip_region (image);
467     return image;
468 }
469
470 PIXMAN_EXPORT pixman_bool_t
471 pixman_image_set_clip_region32 (pixman_image_t *image,
472                                 pixman_region32_t *region)
473 {
474     image_common_t *common = (image_common_t *)image;
475
476     if (region)
477     {
478         return pixman_region32_copy (&common->clip_region, region);
479     }
480     else
481     {
482         reset_clip_region (image);
483
484         return TRUE;
485     }
486 }
487
488
489 PIXMAN_EXPORT pixman_bool_t
490 pixman_image_set_clip_region (pixman_image_t    *image,
491                               pixman_region16_t *region)
492 {
493     image_common_t *common = (image_common_t *)image;
494
495     if (region)
496     {
497         return pixman_region32_copy_from_region16 (&common->clip_region, region);
498     }
499     else
500     {
501         reset_clip_region (image);
502
503         return TRUE;
504     }
505 }
506
507 /* Sets whether the clip region includes a clip region set by the client
508  */
509 PIXMAN_EXPORT void
510 pixman_image_set_has_client_clip (pixman_image_t *image,
511                                   pixman_bool_t   client_clip)
512 {
513     image->common.has_client_clip = client_clip;
514 }
515
516 PIXMAN_EXPORT pixman_bool_t
517 pixman_image_set_transform (pixman_image_t           *image,
518                             const pixman_transform_t *transform)
519 {
520     static const pixman_transform_t id =
521     {
522         { { pixman_fixed_1, 0, 0 },
523           { 0, pixman_fixed_1, 0 },
524           { 0, 0, pixman_fixed_1 }
525         }
526     };
527
528     image_common_t *common = (image_common_t *)image;
529
530     if (common->transform == transform)
531         return TRUE;
532
533     if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
534     {
535         free(common->transform);
536         common->transform = NULL;
537         return TRUE;
538     }
539
540     if (common->transform == NULL)
541         common->transform = malloc (sizeof (pixman_transform_t));
542     if (common->transform == NULL)
543         return FALSE;
544
545     memcpy(common->transform, transform, sizeof(pixman_transform_t));
546
547     return TRUE;
548 }
549
550 PIXMAN_EXPORT void
551 pixman_image_set_repeat (pixman_image_t  *image,
552                          pixman_repeat_t  repeat)
553 {
554     image->common.repeat = repeat;
555 }
556
557 PIXMAN_EXPORT pixman_bool_t
558 pixman_image_set_filter (pixman_image_t       *image,
559                          pixman_filter_t       filter,
560                          const pixman_fixed_t *params,
561                          int                   n_params)
562 {
563     image_common_t *common = (image_common_t *)image;
564     pixman_fixed_t *new_params;
565
566     if (params == common->filter_params && filter == common->filter)
567         return TRUE;
568
569     new_params = NULL;
570     if (params)
571     {
572         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
573         if (!new_params)
574             return FALSE;
575
576         memcpy (new_params,
577                 params, n_params * sizeof (pixman_fixed_t));
578     }
579
580     common->filter = filter;
581
582     if (common->filter_params)
583         free (common->filter_params);
584
585     common->filter_params = new_params;
586     common->n_filter_params = n_params;
587     return TRUE;
588 }
589
590 PIXMAN_EXPORT void
591 pixman_image_set_source_clipping (pixman_image_t  *image,
592                                   pixman_bool_t    source_clipping)
593 {
594     image_common_t *common = &image->common;
595
596     if (source_clipping)
597         common->src_clip = &common->clip_region;
598     else
599         common->src_clip = &common->full_region;
600 }
601
602 /* Unlike all the other property setters, this function does not
603  * copy the content of indexed. Doing this copying is simply
604  * way, way too expensive.
605  */
606 PIXMAN_EXPORT void
607 pixman_image_set_indexed (pixman_image_t         *image,
608                           const pixman_indexed_t *indexed)
609 {
610     bits_image_t *bits = (bits_image_t *)image;
611
612     bits->indexed = indexed;
613 }
614
615 PIXMAN_EXPORT void
616 pixman_image_set_alpha_map (pixman_image_t *image,
617                             pixman_image_t *alpha_map,
618                             int16_t         x,
619                             int16_t         y)
620 {
621     image_common_t *common = (image_common_t *)image;
622
623     return_if_fail (!alpha_map || alpha_map->type == BITS);
624
625     if (common->alpha_map != (bits_image_t *)alpha_map)
626     {
627         if (common->alpha_map)
628             pixman_image_unref ((pixman_image_t *)common->alpha_map);
629
630         if (alpha_map)
631             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
632         else
633             common->alpha_map = NULL;
634     }
635
636     common->alpha_origin.x = x;
637     common->alpha_origin.y = y;
638 }
639
640 PIXMAN_EXPORT void
641 pixman_image_set_component_alpha   (pixman_image_t       *image,
642                                     pixman_bool_t         component_alpha)
643 {
644     image->common.component_alpha = component_alpha;
645 }
646
647
648 PIXMAN_EXPORT void
649 pixman_image_set_accessors (pixman_image_t             *image,
650                             pixman_read_memory_func_t   read_func,
651                             pixman_write_memory_func_t  write_func)
652 {
653     return_if_fail (image != NULL);
654
655     image->common.read_func = read_func;
656     image->common.write_func = write_func;
657 }
658
659 PIXMAN_EXPORT uint32_t *
660 pixman_image_get_data (pixman_image_t *image)
661 {
662     if (image->type == BITS)
663         return image->bits.bits;
664
665     return NULL;
666 }
667
668 PIXMAN_EXPORT int
669 pixman_image_get_width (pixman_image_t *image)
670 {
671     if (image->type == BITS)
672         return image->bits.width;
673
674     return 0;
675 }
676
677 PIXMAN_EXPORT int
678 pixman_image_get_height (pixman_image_t *image)
679 {
680     if (image->type == BITS)
681         return image->bits.height;
682
683     return 0;
684 }
685
686 PIXMAN_EXPORT int
687 pixman_image_get_stride (pixman_image_t *image)
688 {
689     if (image->type == BITS)
690         return image->bits.rowstride * (int) sizeof (uint32_t);
691
692     return 0;
693 }
694
695 PIXMAN_EXPORT int
696 pixman_image_get_depth (pixman_image_t *image)
697 {
698     if (image->type == BITS)
699         return PIXMAN_FORMAT_DEPTH (image->bits.format);
700
701     return 0;
702 }
703
704 static pixman_bool_t
705 color_to_pixel (pixman_color_t *color,
706                 uint32_t       *pixel,
707                 pixman_format_code_t format)
708 {
709     uint32_t c = color_to_uint32 (color);
710
711     if (!(format == PIXMAN_a8r8g8b8     ||
712           format == PIXMAN_x8r8g8b8     ||
713           format == PIXMAN_a8b8g8r8     ||
714           format == PIXMAN_x8b8g8r8     ||
715           format == PIXMAN_b8g8r8a8     ||
716           format == PIXMAN_b8g8r8x8     ||
717           format == PIXMAN_r5g6b5       ||
718           format == PIXMAN_b5g6r5       ||
719           format == PIXMAN_a8))
720     {
721         return FALSE;
722     }
723
724     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
725     {
726         c = ((c & 0xff000000) >>  0) |
727             ((c & 0x00ff0000) >> 16) |
728             ((c & 0x0000ff00) >>  0) |
729             ((c & 0x000000ff) << 16);
730     }
731     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
732     {
733         c = ((c & 0xff000000) >> 24) |
734             ((c & 0x00ff0000) >>  8) |
735             ((c & 0x0000ff00) <<  8) |
736             ((c & 0x000000ff) << 24);
737     }
738
739     if (format == PIXMAN_a8)
740         c = c >> 24;
741     else if (format == PIXMAN_r5g6b5 ||
742              format == PIXMAN_b5g6r5)
743         c = cvt8888to0565 (c);
744
745 #if 0
746     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
747     printf ("pixel: %x\n", c);
748 #endif
749
750     *pixel = c;
751     return TRUE;
752 }
753
754 PIXMAN_EXPORT pixman_bool_t
755 pixman_image_fill_rectangles (pixman_op_t                   op,
756                               pixman_image_t               *dest,
757                               pixman_color_t               *color,
758                               int                           n_rects,
759                               const pixman_rectangle16_t   *rects)
760 {
761     pixman_image_t *solid;
762     pixman_color_t c;
763     int i;
764
765     if (color->alpha == 0xffff)
766     {
767         if (op == PIXMAN_OP_OVER)
768             op = PIXMAN_OP_SRC;
769     }
770
771     if (op == PIXMAN_OP_CLEAR)
772     {
773         c.red = 0;
774         c.green = 0;
775         c.blue = 0;
776         c.alpha = 0;
777
778         color = &c;
779
780         op = PIXMAN_OP_SRC;
781     }
782
783     if (op == PIXMAN_OP_SRC)
784     {
785         uint32_t pixel;
786
787         if (color_to_pixel (color, &pixel, dest->bits.format))
788         {
789             for (i = 0; i < n_rects; ++i)
790             {
791                 pixman_region32_t fill_region;
792                 int n_boxes, j;
793                 pixman_box32_t *boxes;
794
795                 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
796                 if (!pixman_region32_intersect (&fill_region,
797                                                 &fill_region,
798                                                 &dest->common.clip_region))
799                     return FALSE;
800
801
802                 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
803                 for (j = 0; j < n_boxes; ++j)
804                 {
805                     const pixman_box32_t *box = &(boxes[j]);
806                     pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
807                                  box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
808                                  pixel);
809                 }
810
811                 pixman_region32_fini (&fill_region);
812             }
813             return TRUE;
814         }
815     }
816
817     solid = pixman_image_create_solid_fill (color);
818     if (!solid)
819         return FALSE;
820
821     for (i = 0; i < n_rects; ++i)
822     {
823         const pixman_rectangle16_t *rect = &(rects[i]);
824
825         pixman_image_composite (op, solid, NULL, dest,
826                                 0, 0, 0, 0,
827                                 rect->x, rect->y,
828                                 rect->width, rect->height);
829     }
830
831     pixman_image_unref (solid);
832
833     return TRUE;
834 }
835
836 pixman_bool_t
837 pixman_image_can_get_solid (pixman_image_t *image)
838 {
839     if (image->type == SOLID)
840         return TRUE;
841
842     if (image->type != BITS     ||
843         image->bits.width != 1  ||
844         image->bits.height != 1)
845     {
846         return FALSE;
847     }
848
849     if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
850         return FALSE;
851
852     switch (image->bits.format)
853     {
854     case PIXMAN_a8r8g8b8:
855     case PIXMAN_x8r8g8b8:
856     case PIXMAN_a8b8g8r8:
857     case PIXMAN_x8b8g8r8:
858     case PIXMAN_b8g8r8a8:
859     case PIXMAN_b8g8r8x8:
860     case PIXMAN_r8g8b8:
861     case PIXMAN_b8g8r8:
862     case PIXMAN_r5g6b5:
863     case PIXMAN_b5g6r5:
864         return TRUE;
865     default:
866         return FALSE;
867     }
868 }
869
870 pixman_bool_t
871 pixman_image_is_opaque(pixman_image_t *image)
872 {
873     int i = 0;
874     int gradientNumberOfColors = 0;
875
876     if(image->common.alpha_map)
877         return FALSE;
878
879     switch(image->type)
880     {
881     case BITS:
882         if(PIXMAN_FORMAT_A(image->bits.format))
883             return FALSE;
884         break;
885
886     case LINEAR:
887     case CONICAL:
888     case RADIAL:
889         gradientNumberOfColors = image->gradient.n_stops;
890         i=0;
891         while(i<gradientNumberOfColors)
892         {
893             if(image->gradient.stops[i].color.alpha != 0xffff)
894                 return FALSE;
895             i++;
896         }
897         break;
898
899     case SOLID:
900          if(Alpha(image->solid.color) != 0xff)
901             return FALSE;
902         break;
903     }
904
905     /* Convolution filters can introduce translucency if the sum of the weights
906        is lower than 1. */
907     if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
908          return FALSE;
909
910     if (image->common.repeat == PIXMAN_REPEAT_NONE)
911     {
912         if (image->common.filter != PIXMAN_FILTER_NEAREST)
913             return FALSE;
914
915         if (image->common.transform)
916             return FALSE;
917
918         /* Gradients do not necessarily cover the entire compositing area */
919         if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)
920             return FALSE;
921     }
922
923      return TRUE;
924 }