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