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