init/fini functions for pixman_image_t
[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 #include <assert.h>
31
32 #include "pixman-private.h"
33
34 pixman_bool_t
35 _pixman_init_gradient (gradient_t *                  gradient,
36                        const pixman_gradient_stop_t *stops,
37                        int                           n_stops)
38 {
39     return_val_if_fail (n_stops > 0, FALSE);
40
41     gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
42     if (!gradient->stops)
43         return FALSE;
44
45     memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
46
47     gradient->n_stops = n_stops;
48
49     return TRUE;
50 }
51
52 void
53 _pixman_image_init (pixman_image_t *image)
54 {
55     image_common_t *common = &image->common;
56
57     pixman_region32_init (&common->clip_region);
58
59     common->alpha_count = 0;
60     common->have_clip_region = FALSE;
61     common->clip_sources = FALSE;
62     common->transform = NULL;
63     common->repeat = PIXMAN_REPEAT_NONE;
64     common->filter = PIXMAN_FILTER_NEAREST;
65     common->filter_params = NULL;
66     common->n_filter_params = 0;
67     common->alpha_map = NULL;
68     common->component_alpha = FALSE;
69     common->ref_count = 1;
70     common->property_changed = NULL;
71     common->client_clip = FALSE;
72     common->destroy_func = NULL;
73     common->destroy_data = NULL;
74     common->dirty = TRUE;
75 }
76
77 pixman_bool_t
78 _pixman_image_fini (pixman_image_t *image)
79 {
80     image_common_t *common = (image_common_t *)image;
81
82     common->ref_count--;
83
84     if (common->ref_count == 0)
85     {
86         if (image->common.destroy_func)
87             image->common.destroy_func (image, image->common.destroy_data);
88
89         pixman_region32_fini (&common->clip_region);
90
91         if (common->transform)
92             free (common->transform);
93
94         if (common->filter_params)
95             free (common->filter_params);
96
97         if (common->alpha_map)
98             pixman_image_unref ((pixman_image_t *)common->alpha_map);
99
100         if (image->type == LINEAR ||
101             image->type == RADIAL ||
102             image->type == CONICAL)
103         {
104             if (image->gradient.stops)
105                 free (image->gradient.stops);
106         }
107
108         if (image->type == BITS && image->bits.free_me)
109             free (image->bits.free_me);
110
111         return TRUE;
112     }
113
114     return FALSE;
115 }
116
117 pixman_image_t *
118 _pixman_image_allocate (void)
119 {
120     pixman_image_t *image = malloc (sizeof (pixman_image_t));
121
122     if (image)
123         _pixman_image_init (image);
124
125     return image;
126 }
127
128 static void
129 image_property_changed (pixman_image_t *image)
130 {
131     image->common.dirty = TRUE;
132 }
133
134 /* Ref Counting */
135 PIXMAN_EXPORT pixman_image_t *
136 pixman_image_ref (pixman_image_t *image)
137 {
138     image->common.ref_count++;
139
140     return image;
141 }
142
143 /* returns TRUE when the image is freed */
144 PIXMAN_EXPORT pixman_bool_t
145 pixman_image_unref (pixman_image_t *image)
146 {
147     if (_pixman_image_fini (image))
148     {
149         free (image);
150         return TRUE;
151     }
152
153     return FALSE;
154 }
155
156 PIXMAN_EXPORT void
157 pixman_image_set_destroy_function (pixman_image_t *            image,
158                                    pixman_image_destroy_func_t func,
159                                    void *                      data)
160 {
161     image->common.destroy_func = func;
162     image->common.destroy_data = data;
163 }
164
165 PIXMAN_EXPORT void *
166 pixman_image_get_destroy_data (pixman_image_t *image)
167 {
168   return image->common.destroy_data;
169 }
170
171 void
172 _pixman_image_reset_clip_region (pixman_image_t *image)
173 {
174     image->common.have_clip_region = FALSE;
175 }
176
177 /* Executive Summary: This function is a no-op that only exists
178  * for historical reasons.
179  *
180  * There used to be a bug in the X server where it would rely on
181  * out-of-bounds accesses when it was asked to composite with a
182  * window as the source. It would create a pixman image pointing
183  * to some bogus position in memory, but then set a clip region
184  * to the position where the actual bits were.
185  *
186  * Due to a bug in old versions of pixman, where it would not clip
187  * against the image bounds when a clip region was set, this would
188  * actually work. So when the pixman bug was fixed, a workaround was
189  * added to allow certain out-of-bound accesses. This function disabled
190  * those workarounds.
191  *
192  * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
193  * this function is a no-op.
194  */
195 PIXMAN_EXPORT void
196 pixman_disable_out_of_bounds_workaround (void)
197 {
198 }
199
200 static void
201 compute_image_info (pixman_image_t *image)
202 {
203     pixman_format_code_t code;
204     uint32_t flags = 0;
205
206     /* Transform */
207     if (!image->common.transform)
208     {
209         flags |= (FAST_PATH_ID_TRANSFORM        |
210                   FAST_PATH_X_UNIT_POSITIVE     |
211                   FAST_PATH_Y_UNIT_ZERO         |
212                   FAST_PATH_AFFINE_TRANSFORM);
213     }
214     else
215     {
216         flags |= FAST_PATH_HAS_TRANSFORM;
217
218         if (image->common.transform->matrix[2][0] == 0                  &&
219             image->common.transform->matrix[2][1] == 0                  &&
220             image->common.transform->matrix[2][2] == pixman_fixed_1)
221         {
222             flags |= FAST_PATH_AFFINE_TRANSFORM;
223
224             if (image->common.transform->matrix[0][1] == 0 &&
225                 image->common.transform->matrix[1][0] == 0)
226             {
227                 if (image->common.transform->matrix[0][0] == -pixman_fixed_1 &&
228                     image->common.transform->matrix[1][1] == -pixman_fixed_1)
229                 {
230                     flags |= FAST_PATH_ROTATE_180_TRANSFORM;
231                 }
232                 flags |= FAST_PATH_SCALE_TRANSFORM;
233             }
234             else if (image->common.transform->matrix[0][0] == 0 &&
235                      image->common.transform->matrix[1][1] == 0)
236             {
237                 pixman_fixed_t m01 = image->common.transform->matrix[0][1];
238                 if (m01 == -image->common.transform->matrix[1][0])
239                 {
240                         if (m01 == -pixman_fixed_1)
241                             flags |= FAST_PATH_ROTATE_90_TRANSFORM;
242                         else if (m01 == pixman_fixed_1)
243                             flags |= FAST_PATH_ROTATE_270_TRANSFORM;
244                 }
245             }
246         }
247
248         if (image->common.transform->matrix[0][0] > 0)
249             flags |= FAST_PATH_X_UNIT_POSITIVE;
250
251         if (image->common.transform->matrix[1][0] == 0)
252             flags |= FAST_PATH_Y_UNIT_ZERO;
253     }
254
255     /* Filter */
256     switch (image->common.filter)
257     {
258     case PIXMAN_FILTER_NEAREST:
259     case PIXMAN_FILTER_FAST:
260         flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
261         break;
262
263     case PIXMAN_FILTER_BILINEAR:
264     case PIXMAN_FILTER_GOOD:
265     case PIXMAN_FILTER_BEST:
266         flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
267
268         /* Here we have a chance to optimize BILINEAR filter to NEAREST if
269          * they are equivalent for the currently used transformation matrix.
270          */
271         if (flags & FAST_PATH_ID_TRANSFORM)
272         {
273             flags |= FAST_PATH_NEAREST_FILTER;
274         }
275         else if (
276             /* affine and integer translation components in matrix ... */
277             ((flags & FAST_PATH_AFFINE_TRANSFORM) &&
278              !pixman_fixed_frac (image->common.transform->matrix[0][2] |
279                                  image->common.transform->matrix[1][2])) &&
280             (
281                 /* ... combined with a simple rotation */
282                 (flags & (FAST_PATH_ROTATE_90_TRANSFORM |
283                           FAST_PATH_ROTATE_180_TRANSFORM |
284                           FAST_PATH_ROTATE_270_TRANSFORM)) ||
285                 /* ... or combined with a simple non-rotated translation */
286                 (image->common.transform->matrix[0][0] == pixman_fixed_1 &&
287                  image->common.transform->matrix[1][1] == pixman_fixed_1 &&
288                  image->common.transform->matrix[0][1] == 0 &&
289                  image->common.transform->matrix[1][0] == 0)
290                 )
291             )
292         {
293             /* FIXME: there are some affine-test failures, showing that
294              * handling of BILINEAR and NEAREST filter is not quite
295              * equivalent when getting close to 32K for the translation
296              * components of the matrix. That's likely some bug, but for
297              * now just skip BILINEAR->NEAREST optimization in this case.
298              */
299             pixman_fixed_t magic_limit = pixman_int_to_fixed (30000);
300             if (image->common.transform->matrix[0][2] <= magic_limit  &&
301                 image->common.transform->matrix[1][2] <= magic_limit  &&
302                 image->common.transform->matrix[0][2] >= -magic_limit &&
303                 image->common.transform->matrix[1][2] >= -magic_limit)
304             {
305                 flags |= FAST_PATH_NEAREST_FILTER;
306             }
307         }
308         break;
309
310     case PIXMAN_FILTER_CONVOLUTION:
311         break;
312
313     default:
314         flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
315         break;
316     }
317
318     /* Repeat mode */
319     switch (image->common.repeat)
320     {
321     case PIXMAN_REPEAT_NONE:
322         flags |=
323             FAST_PATH_NO_REFLECT_REPEAT         |
324             FAST_PATH_NO_PAD_REPEAT             |
325             FAST_PATH_NO_NORMAL_REPEAT;
326         break;
327
328     case PIXMAN_REPEAT_REFLECT:
329         flags |=
330             FAST_PATH_NO_PAD_REPEAT             |
331             FAST_PATH_NO_NONE_REPEAT            |
332             FAST_PATH_NO_NORMAL_REPEAT;
333         break;
334
335     case PIXMAN_REPEAT_PAD:
336         flags |=
337             FAST_PATH_NO_REFLECT_REPEAT         |
338             FAST_PATH_NO_NONE_REPEAT            |
339             FAST_PATH_NO_NORMAL_REPEAT;
340         break;
341
342     default:
343         flags |=
344             FAST_PATH_NO_REFLECT_REPEAT         |
345             FAST_PATH_NO_PAD_REPEAT             |
346             FAST_PATH_NO_NONE_REPEAT;
347         break;
348     }
349
350     /* Component alpha */
351     if (image->common.component_alpha)
352         flags |= FAST_PATH_COMPONENT_ALPHA;
353     else
354         flags |= FAST_PATH_UNIFIED_ALPHA;
355
356     flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
357
358     /* Type specific checks */
359     switch (image->type)
360     {
361     case SOLID:
362         code = PIXMAN_solid;
363
364         if (image->solid.color.alpha == 0xffff)
365             flags |= FAST_PATH_IS_OPAQUE;
366         break;
367
368     case BITS:
369         if (image->bits.width == 1      &&
370             image->bits.height == 1     &&
371             image->common.repeat != PIXMAN_REPEAT_NONE)
372         {
373             code = PIXMAN_solid;
374         }
375         else
376         {
377             code = image->bits.format;
378         }
379
380         if (!PIXMAN_FORMAT_A (image->bits.format)                               &&
381             PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY         &&
382             PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
383         {
384             flags |= FAST_PATH_SAMPLES_OPAQUE;
385
386             if (image->common.repeat != PIXMAN_REPEAT_NONE)
387                 flags |= FAST_PATH_IS_OPAQUE;
388         }
389
390         if (image->bits.read_func || image->bits.write_func)
391             flags &= ~FAST_PATH_NO_ACCESSORS;
392
393         if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
394             flags &= ~FAST_PATH_NARROW_FORMAT;
395         break;
396
397     case RADIAL:
398         code = PIXMAN_unknown;
399
400         /*
401          * As explained in pixman-radial-gradient.c, every point of
402          * the plane has a valid associated radius (and thus will be
403          * colored) if and only if a is negative (i.e. one of the two
404          * circles contains the other one).
405          */
406
407         if (image->radial.a >= 0)
408             break;
409
410         /* Fall through */
411
412     case CONICAL:
413     case LINEAR:
414         code = PIXMAN_unknown;
415
416         if (image->common.repeat != PIXMAN_REPEAT_NONE)
417         {
418             int i;
419
420             flags |= FAST_PATH_IS_OPAQUE;
421             for (i = 0; i < image->gradient.n_stops; ++i)
422             {
423                 if (image->gradient.stops[i].color.alpha != 0xffff)
424                 {
425                     flags &= ~FAST_PATH_IS_OPAQUE;
426                     break;
427                 }
428             }
429         }
430         break;
431
432     default:
433         code = PIXMAN_unknown;
434         break;
435     }
436
437     /* Alpha map */
438     if (!image->common.alpha_map)
439     {
440         flags |= FAST_PATH_NO_ALPHA_MAP;
441     }
442     else
443     {
444         if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
445             flags &= ~FAST_PATH_NARROW_FORMAT;
446     }
447
448     /* Both alpha maps and convolution filters can introduce
449      * non-opaqueness in otherwise opaque images. Also
450      * an image with component alpha turned on is only opaque
451      * if all channels are opaque, so we simply turn it off
452      * unconditionally for those images.
453      */
454     if (image->common.alpha_map                                 ||
455         image->common.filter == PIXMAN_FILTER_CONVOLUTION       ||
456         image->common.component_alpha)
457     {
458         flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
459     }
460
461     image->common.flags = flags;
462     image->common.extended_format_code = code;
463 }
464
465 void
466 _pixman_image_validate (pixman_image_t *image)
467 {
468     if (image->common.dirty)
469     {
470         compute_image_info (image);
471
472         /* It is important that property_changed is
473          * called *after* compute_image_info() because
474          * property_changed() can make use of the flags
475          * to set up accessors etc.
476          */
477         if (image->common.property_changed)
478             image->common.property_changed (image);
479
480         image->common.dirty = FALSE;
481     }
482
483     if (image->common.alpha_map)
484         _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
485 }
486
487 PIXMAN_EXPORT pixman_bool_t
488 pixman_image_set_clip_region32 (pixman_image_t *   image,
489                                 pixman_region32_t *region)
490 {
491     image_common_t *common = (image_common_t *)image;
492     pixman_bool_t result;
493
494     if (region)
495     {
496         if ((result = pixman_region32_copy (&common->clip_region, region)))
497             image->common.have_clip_region = TRUE;
498     }
499     else
500     {
501         _pixman_image_reset_clip_region (image);
502
503         result = TRUE;
504     }
505
506     image_property_changed (image);
507
508     return result;
509 }
510
511 PIXMAN_EXPORT pixman_bool_t
512 pixman_image_set_clip_region (pixman_image_t *   image,
513                               pixman_region16_t *region)
514 {
515     image_common_t *common = (image_common_t *)image;
516     pixman_bool_t result;
517
518     if (region)
519     {
520         if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
521             image->common.have_clip_region = TRUE;
522     }
523     else
524     {
525         _pixman_image_reset_clip_region (image);
526
527         result = TRUE;
528     }
529
530     image_property_changed (image);
531
532     return result;
533 }
534
535 PIXMAN_EXPORT void
536 pixman_image_set_has_client_clip (pixman_image_t *image,
537                                   pixman_bool_t   client_clip)
538 {
539     image->common.client_clip = client_clip;
540 }
541
542 PIXMAN_EXPORT pixman_bool_t
543 pixman_image_set_transform (pixman_image_t *          image,
544                             const pixman_transform_t *transform)
545 {
546     static const pixman_transform_t id =
547     {
548         { { pixman_fixed_1, 0, 0 },
549           { 0, pixman_fixed_1, 0 },
550           { 0, 0, pixman_fixed_1 } }
551     };
552
553     image_common_t *common = (image_common_t *)image;
554     pixman_bool_t result;
555
556     if (common->transform == transform)
557         return TRUE;
558
559     if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
560     {
561         free (common->transform);
562         common->transform = NULL;
563         result = TRUE;
564
565         goto out;
566     }
567
568     if (common->transform &&
569         memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
570     {
571         return TRUE;
572     }
573
574     if (common->transform == NULL)
575         common->transform = malloc (sizeof (pixman_transform_t));
576
577     if (common->transform == NULL)
578     {
579         result = FALSE;
580
581         goto out;
582     }
583
584     memcpy (common->transform, transform, sizeof(pixman_transform_t));
585
586     result = TRUE;
587
588 out:
589     image_property_changed (image);
590
591     return result;
592 }
593
594 PIXMAN_EXPORT void
595 pixman_image_set_repeat (pixman_image_t *image,
596                          pixman_repeat_t repeat)
597 {
598     if (image->common.repeat == repeat)
599         return;
600
601     image->common.repeat = repeat;
602
603     image_property_changed (image);
604 }
605
606 PIXMAN_EXPORT pixman_bool_t
607 pixman_image_set_filter (pixman_image_t *      image,
608                          pixman_filter_t       filter,
609                          const pixman_fixed_t *params,
610                          int                   n_params)
611 {
612     image_common_t *common = (image_common_t *)image;
613     pixman_fixed_t *new_params;
614
615     if (params == common->filter_params && filter == common->filter)
616         return TRUE;
617
618     new_params = NULL;
619     if (params)
620     {
621         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
622         if (!new_params)
623             return FALSE;
624
625         memcpy (new_params,
626                 params, n_params * sizeof (pixman_fixed_t));
627     }
628
629     common->filter = filter;
630
631     if (common->filter_params)
632         free (common->filter_params);
633
634     common->filter_params = new_params;
635     common->n_filter_params = n_params;
636
637     image_property_changed (image);
638     return TRUE;
639 }
640
641 PIXMAN_EXPORT void
642 pixman_image_set_source_clipping (pixman_image_t *image,
643                                   pixman_bool_t   clip_sources)
644 {
645     if (image->common.clip_sources == clip_sources)
646         return;
647
648     image->common.clip_sources = clip_sources;
649
650     image_property_changed (image);
651 }
652
653 /* Unlike all the other property setters, this function does not
654  * copy the content of indexed. Doing this copying is simply
655  * way, way too expensive.
656  */
657 PIXMAN_EXPORT void
658 pixman_image_set_indexed (pixman_image_t *        image,
659                           const pixman_indexed_t *indexed)
660 {
661     bits_image_t *bits = (bits_image_t *)image;
662
663     if (bits->indexed == indexed)
664         return;
665
666     bits->indexed = indexed;
667
668     image_property_changed (image);
669 }
670
671 PIXMAN_EXPORT void
672 pixman_image_set_alpha_map (pixman_image_t *image,
673                             pixman_image_t *alpha_map,
674                             int16_t         x,
675                             int16_t         y)
676 {
677     image_common_t *common = (image_common_t *)image;
678
679     return_if_fail (!alpha_map || alpha_map->type == BITS);
680
681     if (alpha_map && common->alpha_count > 0)
682     {
683         /* If this image is being used as an alpha map itself,
684          * then you can't give it an alpha map of its own.
685          */
686         return;
687     }
688
689     if (alpha_map && alpha_map->common.alpha_map)
690     {
691         /* If the image has an alpha map of its own,
692          * then it can't be used as an alpha map itself
693          */
694         return;
695     }
696
697     if (common->alpha_map != (bits_image_t *)alpha_map)
698     {
699         if (common->alpha_map)
700         {
701             common->alpha_map->common.alpha_count--;
702
703             pixman_image_unref ((pixman_image_t *)common->alpha_map);
704         }
705
706         if (alpha_map)
707         {
708             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
709
710             common->alpha_map->common.alpha_count++;
711         }
712         else
713         {
714             common->alpha_map = NULL;
715         }
716     }
717
718     common->alpha_origin_x = x;
719     common->alpha_origin_y = y;
720
721     image_property_changed (image);
722 }
723
724 PIXMAN_EXPORT void
725 pixman_image_set_component_alpha   (pixman_image_t *image,
726                                     pixman_bool_t   component_alpha)
727 {
728     if (image->common.component_alpha == component_alpha)
729         return;
730
731     image->common.component_alpha = component_alpha;
732
733     image_property_changed (image);
734 }
735
736 PIXMAN_EXPORT pixman_bool_t
737 pixman_image_get_component_alpha   (pixman_image_t       *image)
738 {
739     return image->common.component_alpha;
740 }
741
742 PIXMAN_EXPORT void
743 pixman_image_set_accessors (pixman_image_t *           image,
744                             pixman_read_memory_func_t  read_func,
745                             pixman_write_memory_func_t write_func)
746 {
747     return_if_fail (image != NULL);
748
749     if (image->type == BITS)
750     {
751         image->bits.read_func = read_func;
752         image->bits.write_func = write_func;
753
754         image_property_changed (image);
755     }
756 }
757
758 PIXMAN_EXPORT uint32_t *
759 pixman_image_get_data (pixman_image_t *image)
760 {
761     if (image->type == BITS)
762         return image->bits.bits;
763
764     return NULL;
765 }
766
767 PIXMAN_EXPORT int
768 pixman_image_get_width (pixman_image_t *image)
769 {
770     if (image->type == BITS)
771         return image->bits.width;
772
773     return 0;
774 }
775
776 PIXMAN_EXPORT int
777 pixman_image_get_height (pixman_image_t *image)
778 {
779     if (image->type == BITS)
780         return image->bits.height;
781
782     return 0;
783 }
784
785 PIXMAN_EXPORT int
786 pixman_image_get_stride (pixman_image_t *image)
787 {
788     if (image->type == BITS)
789         return image->bits.rowstride * (int) sizeof (uint32_t);
790
791     return 0;
792 }
793
794 PIXMAN_EXPORT int
795 pixman_image_get_depth (pixman_image_t *image)
796 {
797     if (image->type == BITS)
798         return PIXMAN_FORMAT_DEPTH (image->bits.format);
799
800     return 0;
801 }
802
803 PIXMAN_EXPORT pixman_format_code_t
804 pixman_image_get_format (pixman_image_t *image)
805 {
806     if (image->type == BITS)
807         return image->bits.format;
808
809     return 0;
810 }
811
812 uint32_t
813 _pixman_image_get_solid (pixman_implementation_t *imp,
814                          pixman_image_t *         image,
815                          pixman_format_code_t     format)
816 {
817     uint32_t result;
818     pixman_iter_t iter;
819
820     _pixman_implementation_src_iter_init (
821         imp, &iter, image, 0, 0, 1, 1,
822         (uint8_t *)&result, ITER_NARROW);
823
824     result = *iter.get_scanline (&iter, NULL);
825
826     /* If necessary, convert RGB <--> BGR. */
827     if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
828     {
829         result = (((result & 0xff000000) >>  0) |
830                   ((result & 0x00ff0000) >> 16) |
831                   ((result & 0x0000ff00) >>  0) |
832                   ((result & 0x000000ff) << 16));
833     }
834
835     return result;
836 }