Move computation of extended format code to validate.
[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     gradient->stop_range = 0xffff;
51     gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
52
53     return TRUE;
54 }
55
56 /*
57  * By default, just evaluate the image at 32bpp and expand.  Individual image
58  * types can plug in a better scanline getter if they want to. For example
59  * we  could produce smoother gradients by evaluating them at higher color
60  * depth, but that's a project for the future.
61  */
62 void
63 _pixman_image_get_scanline_generic_64 (pixman_image_t * image,
64                                        int              x,
65                                        int              y,
66                                        int              width,
67                                        uint32_t *       buffer,
68                                        const uint32_t * mask,
69                                        uint32_t         mask_bits)
70 {
71     uint32_t *mask8 = NULL;
72
73     /* Contract the mask image, if one exists, so that the 32-bit fetch
74      * function can use it.
75      */
76     if (mask)
77     {
78         mask8 = pixman_malloc_ab (width, sizeof(uint32_t));
79         if (!mask8)
80             return;
81
82         pixman_contract (mask8, (uint64_t *)mask, width);
83     }
84
85     /* Fetch the source image into the first half of buffer. */
86     _pixman_image_get_scanline_32 (image, x, y, width, (uint32_t*)buffer, mask8,
87                                    mask_bits);
88
89     /* Expand from 32bpp to 64bpp in place. */
90     pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, width);
91
92     free (mask8);
93 }
94
95 pixman_image_t *
96 _pixman_image_allocate (void)
97 {
98     pixman_image_t *image = malloc (sizeof (pixman_image_t));
99
100     if (image)
101     {
102         image_common_t *common = &image->common;
103
104         pixman_region32_init (&common->clip_region);
105
106         common->have_clip_region = FALSE;
107         common->clip_sources = FALSE;
108         common->transform = NULL;
109         common->repeat = PIXMAN_REPEAT_NONE;
110         common->filter = PIXMAN_FILTER_NEAREST;
111         common->filter_params = NULL;
112         common->n_filter_params = 0;
113         common->alpha_map = NULL;
114         common->component_alpha = FALSE;
115         common->ref_count = 1;
116         common->classify = NULL;
117         common->client_clip = FALSE;
118         common->destroy_func = NULL;
119         common->destroy_data = NULL;
120         common->need_workaround = FALSE;
121         common->dirty = TRUE;
122     }
123
124     return image;
125 }
126
127 source_image_class_t
128 _pixman_image_classify (pixman_image_t *image,
129                         int             x,
130                         int             y,
131                         int             width,
132                         int             height)
133 {
134     if (image->common.classify)
135         return image->common.classify (image, x, y, width, height);
136     else
137         return SOURCE_IMAGE_CLASS_UNKNOWN;
138 }
139
140 void
141 _pixman_image_get_scanline_32 (pixman_image_t *image,
142                                int             x,
143                                int             y,
144                                int             width,
145                                uint32_t *      buffer,
146                                const uint32_t *mask,
147                                uint32_t        mask_bits)
148 {
149     image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
150 }
151
152 /* Even thought the type of buffer is uint32_t *, the function actually expects
153  * a uint64_t *buffer.
154  */
155 void
156 _pixman_image_get_scanline_64 (pixman_image_t *image,
157                                int             x,
158                                int             y,
159                                int             width,
160                                uint32_t *      buffer,
161                                const uint32_t *unused,
162                                uint32_t        unused2)
163 {
164     image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
165 }
166
167 static void
168 image_property_changed (pixman_image_t *image)
169 {
170     image->common.dirty = TRUE;
171 }
172
173 /* Ref Counting */
174 PIXMAN_EXPORT pixman_image_t *
175 pixman_image_ref (pixman_image_t *image)
176 {
177     image->common.ref_count++;
178
179     return image;
180 }
181
182 /* returns TRUE when the image is freed */
183 PIXMAN_EXPORT pixman_bool_t
184 pixman_image_unref (pixman_image_t *image)
185 {
186     image_common_t *common = (image_common_t *)image;
187
188     common->ref_count--;
189
190     if (common->ref_count == 0)
191     {
192         if (image->common.destroy_func)
193             image->common.destroy_func (image, image->common.destroy_data);
194
195         pixman_region32_fini (&common->clip_region);
196
197         if (common->transform)
198             free (common->transform);
199
200         if (common->filter_params)
201             free (common->filter_params);
202
203         if (common->alpha_map)
204             pixman_image_unref ((pixman_image_t *)common->alpha_map);
205
206         if (image->type == LINEAR ||
207             image->type == RADIAL ||
208             image->type == CONICAL)
209         {
210             if (image->gradient.stops)
211                 free (image->gradient.stops);
212         }
213
214         if (image->type == BITS && image->bits.free_me)
215             free (image->bits.free_me);
216
217         free (image);
218
219         return TRUE;
220     }
221
222     return FALSE;
223 }
224
225 PIXMAN_EXPORT void
226 pixman_image_set_destroy_function (pixman_image_t *            image,
227                                    pixman_image_destroy_func_t func,
228                                    void *                      data)
229 {
230     image->common.destroy_func = func;
231     image->common.destroy_data = data;
232 }
233
234 PIXMAN_EXPORT void *
235 pixman_image_get_destroy_data (pixman_image_t *image)
236 {
237   return image->common.destroy_data;
238 }
239
240 void
241 _pixman_image_reset_clip_region (pixman_image_t *image)
242 {
243     image->common.have_clip_region = FALSE;
244 }
245
246 static void
247 compute_image_info (pixman_image_t *image)
248 {
249     pixman_format_code_t code;
250     uint32_t flags = 0;
251
252     if (!image->common.transform)
253     {
254         flags |= FAST_PATH_ID_TRANSFORM;
255     }
256     else if (image->common.transform &&
257              image->common.transform->matrix[0][1] == 0 &&
258              image->common.transform->matrix[1][0] == 0 &&
259              image->common.transform->matrix[2][0] == 0 &&
260              image->common.transform->matrix[2][1] == 0 &&
261              image->common.transform->matrix[2][2] == pixman_fixed_1)
262     {
263         flags |= FAST_PATH_SCALE_TRANSFORM;
264     }
265
266     if (!image->common.alpha_map)
267         flags |= FAST_PATH_NO_ALPHA_MAP;
268
269     if (image->common.filter != PIXMAN_FILTER_CONVOLUTION)
270     {
271         flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
272
273         if (image->common.filter == PIXMAN_FILTER_NEAREST)
274             flags |= FAST_PATH_NEAREST_FILTER;
275     }
276
277     if (image->common.repeat != PIXMAN_REPEAT_PAD)
278         flags |= FAST_PATH_NO_PAD_REPEAT;
279
280     if (image->common.repeat != PIXMAN_REPEAT_REFLECT)
281         flags |= FAST_PATH_NO_REFLECT_REPEAT;
282
283     flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NO_WIDE_FORMAT);
284     if (image->common.type == BITS)
285     {
286         if (image->bits.read_func || image->bits.write_func)
287             flags &= ~FAST_PATH_NO_ACCESSORS;
288
289         if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
290             flags &= ~FAST_PATH_NO_WIDE_FORMAT;
291     }
292
293     if (image->common.component_alpha)
294         flags |= FAST_PATH_COMPONENT_ALPHA;
295     else
296         flags |= FAST_PATH_UNIFIED_ALPHA;
297
298     if (_pixman_image_is_solid (image))
299     {
300         code = PIXMAN_solid;
301     }
302     else if (image->common.type == BITS)
303     {
304         code = image->bits.format;
305
306         if (!image->common.transform &&
307             image->common.repeat == PIXMAN_REPEAT_NORMAL)
308         {
309             flags |= FAST_PATH_SIMPLE_REPEAT;
310         }
311     }
312     else
313     {
314         code = PIXMAN_unknown;
315     }
316
317     image->common.flags = flags;
318     image->common.extended_format_code = code;
319 }
320
321 void
322 _pixman_image_validate (pixman_image_t *image)
323 {
324     if (image->common.dirty)
325     {
326         image->common.property_changed (image);
327
328         compute_image_info (image);
329
330         image->common.dirty = FALSE;
331     }
332
333     if (image->common.alpha_map)
334         _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
335 }
336
337 PIXMAN_EXPORT pixman_bool_t
338 pixman_image_set_clip_region32 (pixman_image_t *   image,
339                                 pixman_region32_t *region)
340 {
341     image_common_t *common = (image_common_t *)image;
342     pixman_bool_t result;
343
344     if (region)
345     {
346         if ((result = pixman_region32_copy (&common->clip_region, region)))
347             image->common.have_clip_region = TRUE;
348     }
349     else
350     {
351         _pixman_image_reset_clip_region (image);
352
353         result = TRUE;
354     }
355
356     image_property_changed (image);
357
358     return result;
359 }
360
361 PIXMAN_EXPORT pixman_bool_t
362 pixman_image_set_clip_region (pixman_image_t *   image,
363                               pixman_region16_t *region)
364 {
365     image_common_t *common = (image_common_t *)image;
366     pixman_bool_t result;
367
368     if (region)
369     {
370         if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
371             image->common.have_clip_region = TRUE;
372     }
373     else
374     {
375         _pixman_image_reset_clip_region (image);
376
377         result = TRUE;
378     }
379
380     image_property_changed (image);
381
382     return result;
383 }
384
385 PIXMAN_EXPORT void
386 pixman_image_set_has_client_clip (pixman_image_t *image,
387                                   pixman_bool_t   client_clip)
388 {
389     image->common.client_clip = client_clip;
390 }
391
392 PIXMAN_EXPORT pixman_bool_t
393 pixman_image_set_transform (pixman_image_t *          image,
394                             const pixman_transform_t *transform)
395 {
396     static const pixman_transform_t id =
397     {
398         { { pixman_fixed_1, 0, 0 },
399           { 0, pixman_fixed_1, 0 },
400           { 0, 0, pixman_fixed_1 } }
401     };
402
403     image_common_t *common = (image_common_t *)image;
404     pixman_bool_t result;
405
406     if (common->transform == transform)
407         return TRUE;
408
409     if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
410     {
411         free (common->transform);
412         common->transform = NULL;
413         result = TRUE;
414
415         goto out;
416     }
417
418     if (common->transform == NULL)
419         common->transform = malloc (sizeof (pixman_transform_t));
420
421     if (common->transform == NULL)
422     {
423         result = FALSE;
424
425         goto out;
426     }
427
428     memcpy (common->transform, transform, sizeof(pixman_transform_t));
429
430     result = TRUE;
431
432 out:
433     image_property_changed (image);
434
435     return result;
436 }
437
438 PIXMAN_EXPORT void
439 pixman_image_set_repeat (pixman_image_t *image,
440                          pixman_repeat_t repeat)
441 {
442     image->common.repeat = repeat;
443
444     image_property_changed (image);
445 }
446
447 PIXMAN_EXPORT pixman_bool_t
448 pixman_image_set_filter (pixman_image_t *      image,
449                          pixman_filter_t       filter,
450                          const pixman_fixed_t *params,
451                          int                   n_params)
452 {
453     image_common_t *common = (image_common_t *)image;
454     pixman_fixed_t *new_params;
455
456     if (params == common->filter_params && filter == common->filter)
457         return TRUE;
458
459     new_params = NULL;
460     if (params)
461     {
462         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
463         if (!new_params)
464             return FALSE;
465
466         memcpy (new_params,
467                 params, n_params * sizeof (pixman_fixed_t));
468     }
469
470     common->filter = filter;
471
472     if (common->filter_params)
473         free (common->filter_params);
474
475     common->filter_params = new_params;
476     common->n_filter_params = n_params;
477
478     image_property_changed (image);
479     return TRUE;
480 }
481
482 PIXMAN_EXPORT void
483 pixman_image_set_source_clipping (pixman_image_t *image,
484                                   pixman_bool_t   clip_sources)
485 {
486     image->common.clip_sources = clip_sources;
487
488     image_property_changed (image);
489 }
490
491 /* Unlike all the other property setters, this function does not
492  * copy the content of indexed. Doing this copying is simply
493  * way, way too expensive.
494  */
495 PIXMAN_EXPORT void
496 pixman_image_set_indexed (pixman_image_t *        image,
497                           const pixman_indexed_t *indexed)
498 {
499     bits_image_t *bits = (bits_image_t *)image;
500
501     bits->indexed = indexed;
502
503     image_property_changed (image);
504 }
505
506 PIXMAN_EXPORT void
507 pixman_image_set_alpha_map (pixman_image_t *image,
508                             pixman_image_t *alpha_map,
509                             int16_t         x,
510                             int16_t         y)
511 {
512     image_common_t *common = (image_common_t *)image;
513
514     return_if_fail (!alpha_map || alpha_map->type == BITS);
515
516     if (common->alpha_map != (bits_image_t *)alpha_map)
517     {
518         if (common->alpha_map)
519             pixman_image_unref ((pixman_image_t *)common->alpha_map);
520
521         if (alpha_map)
522             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
523         else
524             common->alpha_map = NULL;
525     }
526
527     common->alpha_origin_x = x;
528     common->alpha_origin_y = y;
529
530     image_property_changed (image);
531 }
532
533 PIXMAN_EXPORT void
534 pixman_image_set_component_alpha   (pixman_image_t *image,
535                                     pixman_bool_t   component_alpha)
536 {
537     image->common.component_alpha = component_alpha;
538
539     image_property_changed (image);
540 }
541
542 PIXMAN_EXPORT void
543 pixman_image_set_accessors (pixman_image_t *           image,
544                             pixman_read_memory_func_t  read_func,
545                             pixman_write_memory_func_t write_func)
546 {
547     return_if_fail (image != NULL);
548
549     if (image->type == BITS)
550     {
551         image->bits.read_func = read_func;
552         image->bits.write_func = write_func;
553
554         image_property_changed (image);
555     }
556 }
557
558 PIXMAN_EXPORT uint32_t *
559 pixman_image_get_data (pixman_image_t *image)
560 {
561     if (image->type == BITS)
562         return image->bits.bits;
563
564     return NULL;
565 }
566
567 PIXMAN_EXPORT int
568 pixman_image_get_width (pixman_image_t *image)
569 {
570     if (image->type == BITS)
571         return image->bits.width;
572
573     return 0;
574 }
575
576 PIXMAN_EXPORT int
577 pixman_image_get_height (pixman_image_t *image)
578 {
579     if (image->type == BITS)
580         return image->bits.height;
581
582     return 0;
583 }
584
585 PIXMAN_EXPORT int
586 pixman_image_get_stride (pixman_image_t *image)
587 {
588     if (image->type == BITS)
589         return image->bits.rowstride * (int) sizeof (uint32_t);
590
591     return 0;
592 }
593
594 PIXMAN_EXPORT int
595 pixman_image_get_depth (pixman_image_t *image)
596 {
597     if (image->type == BITS)
598         return PIXMAN_FORMAT_DEPTH (image->bits.format);
599
600     return 0;
601 }
602
603 pixman_bool_t
604 _pixman_image_is_solid (pixman_image_t *image)
605 {
606     if (image->type == SOLID)
607         return TRUE;
608
609     if (image->type != BITS     ||
610         image->bits.width != 1  ||
611         image->bits.height != 1)
612     {
613         return FALSE;
614     }
615
616     if (image->common.repeat == PIXMAN_REPEAT_NONE)
617         return FALSE;
618
619     return TRUE;
620 }
621
622 uint32_t
623 _pixman_image_get_solid (pixman_image_t *     image,
624                          pixman_format_code_t format)
625 {
626     uint32_t result;
627
628     _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
629
630     /* If necessary, convert RGB <--> BGR. */
631     if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
632     {
633         result = (((result & 0xff000000) >>  0) |
634                   ((result & 0x00ff0000) >> 16) |
635                   ((result & 0x0000ff00) >>  0) |
636                   ((result & 0x000000ff) << 16));
637     }
638
639     return result;
640 }
641
642 pixman_bool_t
643 _pixman_image_is_opaque (pixman_image_t *image)
644 {
645     int i;
646
647     if (image->common.alpha_map)
648         return FALSE;
649
650     switch (image->type)
651     {
652     case BITS:
653         if (image->common.repeat == PIXMAN_REPEAT_NONE)
654             return FALSE;
655
656         if (PIXMAN_FORMAT_A (image->bits.format))
657             return FALSE;
658         break;
659
660     case LINEAR:
661     case RADIAL:
662         if (image->common.repeat == PIXMAN_REPEAT_NONE)
663             return FALSE;
664
665         for (i = 0; i < image->gradient.n_stops; ++i)
666         {
667             if (image->gradient.stops[i].color.alpha != 0xffff)
668                 return FALSE;
669         }
670         break;
671
672     case CONICAL:
673         /* Conical gradients always have a transparent border */
674         return FALSE;
675         break;
676
677     case SOLID:
678         if (image->solid.color.alpha != 0xffff)
679             return FALSE;
680         break;
681
682     default:
683         return FALSE;
684         break;
685     }
686
687     /* Convolution filters can introduce translucency if the sum of the
688      * weights is lower than 1.
689      */
690     if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
691         return FALSE;
692
693     return TRUE;
694 }
695