Add API to set a function to be called when the image is destroyed.
[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 #define Alpha(x) ((x) >> 24)
35
36 pixman_bool_t
37 _pixman_init_gradient (gradient_t     *gradient,
38                        const pixman_gradient_stop_t *stops,
39                        int             n_stops)
40 {
41     return_val_if_fail (n_stops > 0, FALSE);
42
43     gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
44     if (!gradient->stops)
45         return FALSE;
46
47     memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
48
49     gradient->n_stops = n_stops;
50
51     gradient->stop_range = 0xffff;
52     gradient->color_table = NULL;
53     gradient->color_table_size = 0;
54     gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
55
56     return TRUE;
57 }
58
59 /*
60  * By default, just evaluate the image at 32bpp and expand.  Individual image
61  * types can plug in a better scanline getter if they want to. For example
62  * we  could produce smoother gradients by evaluating them at higher color depth, but
63  * that's a project for the future.
64  */
65 void
66 _pixman_image_get_scanline_64_generic (pixman_image_t * pict, int x, int y, int width,
67                                        uint64_t *buffer, uint64_t *mask, uint32_t maskBits)
68 {
69     uint32_t *mask8 = NULL;
70
71     // Contract the mask image, if one exists, so that the 32-bit fetch function
72     // can use it.
73     if (mask) {
74         mask8 = pixman_malloc_ab(width, sizeof(uint32_t));
75         if (!mask8)
76             return;
77         
78         pixman_contract(mask8, mask, width);
79     }
80
81     // Fetch the source image into the first half of buffer.
82     _pixman_image_get_scanline_32 (pict, x, y, width, (uint32_t*)buffer, mask8,
83                                    maskBits);
84
85     // Expand from 32bpp to 64bpp in place.
86     pixman_expand(buffer, (uint32_t*)buffer, PIXMAN_a8r8g8b8, width);
87
88     free(mask8);
89 }
90
91 pixman_image_t *
92 _pixman_image_allocate (void)
93 {
94     pixman_image_t *image = malloc (sizeof (pixman_image_t));
95
96     if (image)
97     {
98         image_common_t *common = &image->common;
99
100         pixman_region32_init (&common->clip_region);
101
102         common->have_clip_region = FALSE;
103         common->clip_sources = FALSE;
104         common->transform = NULL;
105         common->repeat = PIXMAN_REPEAT_NONE;
106         common->filter = PIXMAN_FILTER_NEAREST;
107         common->filter_params = NULL;
108         common->n_filter_params = 0;
109         common->alpha_map = NULL;
110         common->component_alpha = FALSE;
111         common->ref_count = 1;
112         common->read_func = NULL;
113         common->write_func = NULL;
114         common->classify = NULL;
115         common->client_clip = FALSE;
116         common->destroy_func = NULL;
117         common->destroy_data = NULL;
118     }
119
120     return image;
121 }
122
123 source_pict_class_t
124 _pixman_image_classify (pixman_image_t *image,
125                         int             x,
126                         int             y,
127                         int             width,
128                         int             height)
129 {
130     if (image->common.classify)
131         return image->common.classify (image, x, y, width, height);
132     else
133         return SOURCE_IMAGE_CLASS_UNKNOWN;
134 }
135
136 void
137 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
138                                uint32_t *buffer, uint32_t *mask, uint32_t mask_bits)
139 {
140     image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
141 }
142
143 void
144 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
145                                uint32_t *buffer, uint32_t *unused, uint32_t unused2)
146 {
147     image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
148 }
149
150 /* Even thought the type of buffer is uint32_t *, the function actually expects
151  * a uint64_t *buffer.
152  */
153
154 scanFetchProc
155 _pixman_image_get_fetcher (pixman_image_t *image,
156                            int             wide)
157 {
158     assert (image->common.get_scanline_64);
159     assert (image->common.get_scanline_32);
160     
161     if (wide)
162         return image->common.get_scanline_64;
163     else
164         return image->common.get_scanline_32;
165 }
166
167 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
168
169 static void
170 image_property_changed (pixman_image_t *image)
171 {
172     
173     
174     image->common.property_changed (image);
175 }
176
177 /* Ref Counting */
178 PIXMAN_EXPORT pixman_image_t *
179 pixman_image_ref (pixman_image_t *image)
180 {
181     image->common.ref_count++;
182
183     return image;
184 }
185
186 /* returns TRUE when the image is freed */
187 PIXMAN_EXPORT pixman_bool_t
188 pixman_image_unref (pixman_image_t *image)
189 {
190     image_common_t *common = (image_common_t *)image;
191
192     common->ref_count--;
193
194     if (common->ref_count == 0)
195     {
196         if (image->common.destroy_func)
197             image->common.destroy_func (image, image->common.destroy_data);
198         
199         pixman_region32_fini (&common->clip_region);
200
201         if (common->transform)
202             free (common->transform);
203
204         if (common->filter_params)
205             free (common->filter_params);
206
207         if (common->alpha_map)
208             pixman_image_unref ((pixman_image_t *)common->alpha_map);
209
210 #if 0
211         if (image->type == BITS && image->bits.indexed)
212             free (image->bits.indexed);
213 #endif
214
215 #if 0
216         memset (image, 0xaa, sizeof (pixman_image_t));
217 #endif
218         if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
219         {
220             if (image->gradient.stops)
221                 free (image->gradient.stops);
222         }
223
224
225         if (image->type == BITS && image->bits.free_me)
226             free (image->bits.free_me);
227
228         free (image);
229
230         return TRUE;
231     }
232
233     return FALSE;
234 }
235
236 PIXMAN_EXPORT void
237 pixman_image_set_destroy_function (pixman_image_t *image,
238                                    pixman_image_destroy_func_t func,
239                                    void *data)
240 {
241     image->common.destroy_func = func;
242     image->common.destroy_data = data;
243 }
244                                
245
246 /* Constructors */
247
248 void
249 _pixman_image_reset_clip_region (pixman_image_t *image)
250 {
251     image->common.have_clip_region = FALSE;
252 }
253
254 PIXMAN_EXPORT pixman_bool_t
255 pixman_image_set_clip_region32 (pixman_image_t *image,
256                                 pixman_region32_t *region)
257 {
258     image_common_t *common = (image_common_t *)image;
259     pixman_bool_t result;
260
261     if (region)
262     {
263         if ((result = pixman_region32_copy (&common->clip_region, region)))
264             image->common.have_clip_region = TRUE;
265     }
266     else
267     {
268         _pixman_image_reset_clip_region (image);
269
270         result = TRUE;
271     }
272
273     image_property_changed (image);
274
275     return result;
276 }
277
278
279 PIXMAN_EXPORT pixman_bool_t
280 pixman_image_set_clip_region (pixman_image_t    *image,
281                               pixman_region16_t *region)
282 {
283     image_common_t *common = (image_common_t *)image;
284     pixman_bool_t result;
285
286     if (region)
287     {
288         if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
289             image->common.have_clip_region = TRUE;
290     }
291     else
292     {
293         _pixman_image_reset_clip_region (image);
294
295         result = TRUE;
296     }
297
298     image_property_changed (image);
299
300     return result;
301 }
302
303 PIXMAN_EXPORT void
304 pixman_image_set_has_client_clip (pixman_image_t *image,
305                                   pixman_bool_t   client_clip)
306 {
307     image->common.client_clip = client_clip;
308 }
309
310 PIXMAN_EXPORT pixman_bool_t
311 pixman_image_set_transform (pixman_image_t           *image,
312                             const pixman_transform_t *transform)
313 {
314     static const pixman_transform_t id =
315     {
316         { { pixman_fixed_1, 0, 0 },
317           { 0, pixman_fixed_1, 0 },
318           { 0, 0, pixman_fixed_1 }
319         }
320     };
321
322     image_common_t *common = (image_common_t *)image;
323     pixman_bool_t result;
324
325     if (common->transform == transform)
326         return TRUE;
327
328     if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
329     {
330         free(common->transform);
331         common->transform = NULL;
332         result = TRUE;
333         goto out;
334     }
335
336     if (common->transform == NULL)
337         common->transform = malloc (sizeof (pixman_transform_t));
338
339     if (common->transform == NULL)
340     {
341         result = FALSE;
342         goto out;
343     }
344
345     memcpy(common->transform, transform, sizeof(pixman_transform_t));
346
347 out:
348     image_property_changed (image);
349     
350     return TRUE;
351 }
352
353 PIXMAN_EXPORT void
354 pixman_image_set_repeat (pixman_image_t  *image,
355                          pixman_repeat_t  repeat)
356 {
357     image->common.repeat = repeat;
358
359     image_property_changed (image);
360 }
361
362 PIXMAN_EXPORT pixman_bool_t
363 pixman_image_set_filter (pixman_image_t       *image,
364                          pixman_filter_t       filter,
365                          const pixman_fixed_t *params,
366                          int                   n_params)
367 {
368     image_common_t *common = (image_common_t *)image;
369     pixman_fixed_t *new_params;
370
371     if (params == common->filter_params && filter == common->filter)
372         return TRUE;
373
374     new_params = NULL;
375     if (params)
376     {
377         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
378         if (!new_params)
379             return FALSE;
380
381         memcpy (new_params,
382                 params, n_params * sizeof (pixman_fixed_t));
383     }
384
385     common->filter = filter;
386
387     if (common->filter_params)
388         free (common->filter_params);
389
390     common->filter_params = new_params;
391     common->n_filter_params = n_params;
392
393     image_property_changed (image);
394     return TRUE;
395 }
396
397 PIXMAN_EXPORT void
398 pixman_image_set_source_clipping (pixman_image_t  *image,
399                                   pixman_bool_t    clip_sources)
400 {
401     image->common.clip_sources = clip_sources;
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
456 PIXMAN_EXPORT void
457 pixman_image_set_accessors (pixman_image_t             *image,
458                             pixman_read_memory_func_t   read_func,
459                             pixman_write_memory_func_t  write_func)
460 {
461     return_if_fail (image != NULL);
462
463     image->common.read_func = read_func;
464     image->common.write_func = write_func;
465
466     image_property_changed (image);
467 }
468
469 PIXMAN_EXPORT uint32_t *
470 pixman_image_get_data (pixman_image_t *image)
471 {
472     if (image->type == BITS)
473         return image->bits.bits;
474
475     return NULL;
476 }
477
478 PIXMAN_EXPORT int
479 pixman_image_get_width (pixman_image_t *image)
480 {
481     if (image->type == BITS)
482         return image->bits.width;
483
484     return 0;
485 }
486
487 PIXMAN_EXPORT int
488 pixman_image_get_height (pixman_image_t *image)
489 {
490     if (image->type == BITS)
491         return image->bits.height;
492
493     return 0;
494 }
495
496 PIXMAN_EXPORT int
497 pixman_image_get_stride (pixman_image_t *image)
498 {
499     if (image->type == BITS)
500         return image->bits.rowstride * (int) sizeof (uint32_t);
501
502     return 0;
503 }
504
505 PIXMAN_EXPORT int
506 pixman_image_get_depth (pixman_image_t *image)
507 {
508     if (image->type == BITS)
509         return PIXMAN_FORMAT_DEPTH (image->bits.format);
510
511     return 0;
512 }
513
514 static uint32_t
515 color_to_uint32 (const pixman_color_t *color)
516 {
517     return
518         (color->alpha >> 8 << 24) |
519         (color->red >> 8 << 16) |
520         (color->green & 0xff00) |
521         (color->blue >> 8);
522 }
523
524 static pixman_bool_t
525 color_to_pixel (pixman_color_t *color,
526                 uint32_t       *pixel,
527                 pixman_format_code_t format)
528 {
529     uint32_t c = color_to_uint32 (color);
530
531     if (!(format == PIXMAN_a8r8g8b8     ||
532           format == PIXMAN_x8r8g8b8     ||
533           format == PIXMAN_a8b8g8r8     ||
534           format == PIXMAN_x8b8g8r8     ||
535           format == PIXMAN_b8g8r8a8     ||
536           format == PIXMAN_b8g8r8x8     ||
537           format == PIXMAN_r5g6b5       ||
538           format == PIXMAN_b5g6r5       ||
539           format == PIXMAN_a8))
540     {
541         return FALSE;
542     }
543
544     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
545     {
546         c = ((c & 0xff000000) >>  0) |
547             ((c & 0x00ff0000) >> 16) |
548             ((c & 0x0000ff00) >>  0) |
549             ((c & 0x000000ff) << 16);
550     }
551     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
552     {
553         c = ((c & 0xff000000) >> 24) |
554             ((c & 0x00ff0000) >>  8) |
555             ((c & 0x0000ff00) <<  8) |
556             ((c & 0x000000ff) << 24);
557     }
558
559     if (format == PIXMAN_a8)
560         c = c >> 24;
561     else if (format == PIXMAN_r5g6b5 ||
562              format == PIXMAN_b5g6r5)
563         c = cvt8888to0565 (c);
564
565 #if 0
566     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
567     printf ("pixel: %x\n", c);
568 #endif
569
570     *pixel = c;
571     return TRUE;
572 }
573
574 PIXMAN_EXPORT pixman_bool_t
575 pixman_image_fill_rectangles (pixman_op_t                   op,
576                               pixman_image_t               *dest,
577                               pixman_color_t               *color,
578                               int                           n_rects,
579                               const pixman_rectangle16_t   *rects)
580 {
581     pixman_image_t *solid;
582     pixman_color_t c;
583     int i;
584
585     if (color->alpha == 0xffff)
586     {
587         if (op == PIXMAN_OP_OVER)
588             op = PIXMAN_OP_SRC;
589     }
590
591     if (op == PIXMAN_OP_CLEAR)
592     {
593         c.red = 0;
594         c.green = 0;
595         c.blue = 0;
596         c.alpha = 0;
597
598         color = &c;
599
600         op = PIXMAN_OP_SRC;
601     }
602
603     if (op == PIXMAN_OP_SRC)
604     {
605         uint32_t pixel;
606
607         if (color_to_pixel (color, &pixel, dest->bits.format))
608         {
609             for (i = 0; i < n_rects; ++i)
610             {
611                 pixman_region32_t fill_region;
612                 int n_boxes, j;
613                 pixman_box32_t *boxes;
614
615                 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
616
617                 if (dest->common.have_clip_region)
618                 {
619                     if (!pixman_region32_intersect (&fill_region,
620                                                     &fill_region,
621                                                     &dest->common.clip_region))
622                         return FALSE;
623                 }
624
625                 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
626                 for (j = 0; j < n_boxes; ++j)
627                 {
628                     const pixman_box32_t *box = &(boxes[j]);
629                     pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
630                                  box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
631                                  pixel);
632                 }
633
634                 pixman_region32_fini (&fill_region);
635             }
636             return TRUE;
637         }
638     }
639
640     solid = pixman_image_create_solid_fill (color);
641     if (!solid)
642         return FALSE;
643
644     for (i = 0; i < n_rects; ++i)
645     {
646         const pixman_rectangle16_t *rect = &(rects[i]);
647
648         pixman_image_composite (op, solid, NULL, dest,
649                                 0, 0, 0, 0,
650                                 rect->x, rect->y,
651                                 rect->width, rect->height);
652     }
653
654     pixman_image_unref (solid);
655
656     return TRUE;
657 }
658
659 pixman_bool_t
660 pixman_image_can_get_solid (pixman_image_t *image)
661 {
662     if (image->type == SOLID)
663         return TRUE;
664
665     if (image->type != BITS     ||
666         image->bits.width != 1  ||
667         image->bits.height != 1)
668     {
669         return FALSE;
670     }
671
672     if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
673         return FALSE;
674
675     switch (image->bits.format)
676     {
677     case PIXMAN_a8r8g8b8:
678     case PIXMAN_x8r8g8b8:
679     case PIXMAN_a8b8g8r8:
680     case PIXMAN_x8b8g8r8:
681     case PIXMAN_b8g8r8a8:
682     case PIXMAN_b8g8r8x8:
683     case PIXMAN_r8g8b8:
684     case PIXMAN_b8g8r8:
685     case PIXMAN_r5g6b5:
686     case PIXMAN_b5g6r5:
687         return TRUE;
688     default:
689         return FALSE;
690     }
691 }
692
693 pixman_bool_t
694 pixman_image_is_opaque (pixman_image_t *image)
695 {
696     int i;
697
698     if (image->common.alpha_map)
699         return FALSE;
700
701     switch (image->type)
702     {
703     case BITS:
704         if (image->common.repeat == PIXMAN_REPEAT_NONE)
705             return FALSE;
706         
707         if (PIXMAN_FORMAT_A (image->bits.format))
708             return FALSE;
709         break;
710
711     case LINEAR:
712     case RADIAL:
713         if (image->common.repeat == PIXMAN_REPEAT_NONE)
714             return FALSE;
715         
716         for (i = 0; i < image->gradient.n_stops; ++i)
717         {
718             if (image->gradient.stops[i].color.alpha != 0xffff)
719                 return FALSE;
720         }
721         break;
722
723     case CONICAL:
724         /* Conical gradients always have a transparent border */
725         return FALSE;
726         break;
727         
728     case SOLID:
729          if (Alpha (image->solid.color) != 0xff)
730             return FALSE;
731         break;
732     }
733
734     /* Convolution filters can introduce translucency if the sum of the
735      * weights is lower than 1.
736      */
737     if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
738          return FALSE;
739
740      return TRUE;
741 }