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