Add new fast path flag FAST_PATH_BITS_IMAGE
[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             flags |= FAST_PATH_BITS_IMAGE;
379         }
380
381         if (!PIXMAN_FORMAT_A (image->bits.format)                               &&
382             PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY         &&
383             PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
384         {
385             flags |= FAST_PATH_SAMPLES_OPAQUE;
386
387             if (image->common.repeat != PIXMAN_REPEAT_NONE)
388                 flags |= FAST_PATH_IS_OPAQUE;
389         }
390
391         if (image->bits.read_func || image->bits.write_func)
392             flags &= ~FAST_PATH_NO_ACCESSORS;
393
394         if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
395             flags &= ~FAST_PATH_NARROW_FORMAT;
396         break;
397
398     case RADIAL:
399         code = PIXMAN_unknown;
400
401         /*
402          * As explained in pixman-radial-gradient.c, every point of
403          * the plane has a valid associated radius (and thus will be
404          * colored) if and only if a is negative (i.e. one of the two
405          * circles contains the other one).
406          */
407
408         if (image->radial.a >= 0)
409             break;
410
411         /* Fall through */
412
413     case CONICAL:
414     case LINEAR:
415         code = PIXMAN_unknown;
416
417         if (image->common.repeat != PIXMAN_REPEAT_NONE)
418         {
419             int i;
420
421             flags |= FAST_PATH_IS_OPAQUE;
422             for (i = 0; i < image->gradient.n_stops; ++i)
423             {
424                 if (image->gradient.stops[i].color.alpha != 0xffff)
425                 {
426                     flags &= ~FAST_PATH_IS_OPAQUE;
427                     break;
428                 }
429             }
430         }
431         break;
432
433     default:
434         code = PIXMAN_unknown;
435         break;
436     }
437
438     /* Alpha map */
439     if (!image->common.alpha_map)
440     {
441         flags |= FAST_PATH_NO_ALPHA_MAP;
442     }
443     else
444     {
445         if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
446             flags &= ~FAST_PATH_NARROW_FORMAT;
447     }
448
449     /* Both alpha maps and convolution filters can introduce
450      * non-opaqueness in otherwise opaque images. Also
451      * an image with component alpha turned on is only opaque
452      * if all channels are opaque, so we simply turn it off
453      * unconditionally for those images.
454      */
455     if (image->common.alpha_map                                 ||
456         image->common.filter == PIXMAN_FILTER_CONVOLUTION       ||
457         image->common.component_alpha)
458     {
459         flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
460     }
461
462     image->common.flags = flags;
463     image->common.extended_format_code = code;
464 }
465
466 void
467 _pixman_image_validate (pixman_image_t *image)
468 {
469     if (image->common.dirty)
470     {
471         compute_image_info (image);
472
473         /* It is important that property_changed is
474          * called *after* compute_image_info() because
475          * property_changed() can make use of the flags
476          * to set up accessors etc.
477          */
478         if (image->common.property_changed)
479             image->common.property_changed (image);
480
481         image->common.dirty = FALSE;
482     }
483
484     if (image->common.alpha_map)
485         _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
486 }
487
488 PIXMAN_EXPORT pixman_bool_t
489 pixman_image_set_clip_region32 (pixman_image_t *   image,
490                                 pixman_region32_t *region)
491 {
492     image_common_t *common = (image_common_t *)image;
493     pixman_bool_t result;
494
495     if (region)
496     {
497         if ((result = pixman_region32_copy (&common->clip_region, region)))
498             image->common.have_clip_region = TRUE;
499     }
500     else
501     {
502         _pixman_image_reset_clip_region (image);
503
504         result = TRUE;
505     }
506
507     image_property_changed (image);
508
509     return result;
510 }
511
512 PIXMAN_EXPORT pixman_bool_t
513 pixman_image_set_clip_region (pixman_image_t *   image,
514                               pixman_region16_t *region)
515 {
516     image_common_t *common = (image_common_t *)image;
517     pixman_bool_t result;
518
519     if (region)
520     {
521         if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
522             image->common.have_clip_region = TRUE;
523     }
524     else
525     {
526         _pixman_image_reset_clip_region (image);
527
528         result = TRUE;
529     }
530
531     image_property_changed (image);
532
533     return result;
534 }
535
536 PIXMAN_EXPORT void
537 pixman_image_set_has_client_clip (pixman_image_t *image,
538                                   pixman_bool_t   client_clip)
539 {
540     image->common.client_clip = client_clip;
541 }
542
543 PIXMAN_EXPORT pixman_bool_t
544 pixman_image_set_transform (pixman_image_t *          image,
545                             const pixman_transform_t *transform)
546 {
547     static const pixman_transform_t id =
548     {
549         { { pixman_fixed_1, 0, 0 },
550           { 0, pixman_fixed_1, 0 },
551           { 0, 0, pixman_fixed_1 } }
552     };
553
554     image_common_t *common = (image_common_t *)image;
555     pixman_bool_t result;
556
557     if (common->transform == transform)
558         return TRUE;
559
560     if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
561     {
562         free (common->transform);
563         common->transform = NULL;
564         result = TRUE;
565
566         goto out;
567     }
568
569     if (common->transform &&
570         memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
571     {
572         return TRUE;
573     }
574
575     if (common->transform == NULL)
576         common->transform = malloc (sizeof (pixman_transform_t));
577
578     if (common->transform == NULL)
579     {
580         result = FALSE;
581
582         goto out;
583     }
584
585     memcpy (common->transform, transform, sizeof(pixman_transform_t));
586
587     result = TRUE;
588
589 out:
590     image_property_changed (image);
591
592     return result;
593 }
594
595 PIXMAN_EXPORT void
596 pixman_image_set_repeat (pixman_image_t *image,
597                          pixman_repeat_t repeat)
598 {
599     if (image->common.repeat == repeat)
600         return;
601
602     image->common.repeat = repeat;
603
604     image_property_changed (image);
605 }
606
607 PIXMAN_EXPORT pixman_bool_t
608 pixman_image_set_filter (pixman_image_t *      image,
609                          pixman_filter_t       filter,
610                          const pixman_fixed_t *params,
611                          int                   n_params)
612 {
613     image_common_t *common = (image_common_t *)image;
614     pixman_fixed_t *new_params;
615
616     if (params == common->filter_params && filter == common->filter)
617         return TRUE;
618
619     new_params = NULL;
620     if (params)
621     {
622         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
623         if (!new_params)
624             return FALSE;
625
626         memcpy (new_params,
627                 params, n_params * sizeof (pixman_fixed_t));
628     }
629
630     common->filter = filter;
631
632     if (common->filter_params)
633         free (common->filter_params);
634
635     common->filter_params = new_params;
636     common->n_filter_params = n_params;
637
638     image_property_changed (image);
639     return TRUE;
640 }
641
642 PIXMAN_EXPORT void
643 pixman_image_set_source_clipping (pixman_image_t *image,
644                                   pixman_bool_t   clip_sources)
645 {
646     if (image->common.clip_sources == clip_sources)
647         return;
648
649     image->common.clip_sources = clip_sources;
650
651     image_property_changed (image);
652 }
653
654 /* Unlike all the other property setters, this function does not
655  * copy the content of indexed. Doing this copying is simply
656  * way, way too expensive.
657  */
658 PIXMAN_EXPORT void
659 pixman_image_set_indexed (pixman_image_t *        image,
660                           const pixman_indexed_t *indexed)
661 {
662     bits_image_t *bits = (bits_image_t *)image;
663
664     if (bits->indexed == indexed)
665         return;
666
667     bits->indexed = indexed;
668
669     image_property_changed (image);
670 }
671
672 PIXMAN_EXPORT void
673 pixman_image_set_alpha_map (pixman_image_t *image,
674                             pixman_image_t *alpha_map,
675                             int16_t         x,
676                             int16_t         y)
677 {
678     image_common_t *common = (image_common_t *)image;
679
680     return_if_fail (!alpha_map || alpha_map->type == BITS);
681
682     if (alpha_map && common->alpha_count > 0)
683     {
684         /* If this image is being used as an alpha map itself,
685          * then you can't give it an alpha map of its own.
686          */
687         return;
688     }
689
690     if (alpha_map && alpha_map->common.alpha_map)
691     {
692         /* If the image has an alpha map of its own,
693          * then it can't be used as an alpha map itself
694          */
695         return;
696     }
697
698     if (common->alpha_map != (bits_image_t *)alpha_map)
699     {
700         if (common->alpha_map)
701         {
702             common->alpha_map->common.alpha_count--;
703
704             pixman_image_unref ((pixman_image_t *)common->alpha_map);
705         }
706
707         if (alpha_map)
708         {
709             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
710
711             common->alpha_map->common.alpha_count++;
712         }
713         else
714         {
715             common->alpha_map = NULL;
716         }
717     }
718
719     common->alpha_origin_x = x;
720     common->alpha_origin_y = y;
721
722     image_property_changed (image);
723 }
724
725 PIXMAN_EXPORT void
726 pixman_image_set_component_alpha   (pixman_image_t *image,
727                                     pixman_bool_t   component_alpha)
728 {
729     if (image->common.component_alpha == component_alpha)
730         return;
731
732     image->common.component_alpha = component_alpha;
733
734     image_property_changed (image);
735 }
736
737 PIXMAN_EXPORT pixman_bool_t
738 pixman_image_get_component_alpha   (pixman_image_t       *image)
739 {
740     return image->common.component_alpha;
741 }
742
743 PIXMAN_EXPORT void
744 pixman_image_set_accessors (pixman_image_t *           image,
745                             pixman_read_memory_func_t  read_func,
746                             pixman_write_memory_func_t write_func)
747 {
748     return_if_fail (image != NULL);
749
750     if (image->type == BITS)
751     {
752         image->bits.read_func = read_func;
753         image->bits.write_func = write_func;
754
755         image_property_changed (image);
756     }
757 }
758
759 PIXMAN_EXPORT uint32_t *
760 pixman_image_get_data (pixman_image_t *image)
761 {
762     if (image->type == BITS)
763         return image->bits.bits;
764
765     return NULL;
766 }
767
768 PIXMAN_EXPORT int
769 pixman_image_get_width (pixman_image_t *image)
770 {
771     if (image->type == BITS)
772         return image->bits.width;
773
774     return 0;
775 }
776
777 PIXMAN_EXPORT int
778 pixman_image_get_height (pixman_image_t *image)
779 {
780     if (image->type == BITS)
781         return image->bits.height;
782
783     return 0;
784 }
785
786 PIXMAN_EXPORT int
787 pixman_image_get_stride (pixman_image_t *image)
788 {
789     if (image->type == BITS)
790         return image->bits.rowstride * (int) sizeof (uint32_t);
791
792     return 0;
793 }
794
795 PIXMAN_EXPORT int
796 pixman_image_get_depth (pixman_image_t *image)
797 {
798     if (image->type == BITS)
799         return PIXMAN_FORMAT_DEPTH (image->bits.format);
800
801     return 0;
802 }
803
804 PIXMAN_EXPORT pixman_format_code_t
805 pixman_image_get_format (pixman_image_t *image)
806 {
807     if (image->type == BITS)
808         return image->bits.format;
809
810     return 0;
811 }
812
813 uint32_t
814 _pixman_image_get_solid (pixman_implementation_t *imp,
815                          pixman_image_t *         image,
816                          pixman_format_code_t     format)
817 {
818     uint32_t result;
819     pixman_iter_t iter;
820
821     _pixman_implementation_src_iter_init (
822         imp, &iter, image, 0, 0, 1, 1,
823         (uint8_t *)&result, ITER_NARROW);
824
825     result = *iter.get_scanline (&iter, NULL);
826
827     /* If necessary, convert RGB <--> BGR. */
828     if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
829     {
830         result = (((result & 0xff000000) >>  0) |
831                   ((result & 0x00ff0000) >> 16) |
832                   ((result & 0x0000ff00) >>  0) |
833                   ((result & 0x000000ff) << 16));
834     }
835
836     return result;
837 }