Fix some warnings
[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 void
235 _pixman_image_reset_clip_region (pixman_image_t *image)
236 {
237     image->common.have_clip_region = FALSE;
238 }
239
240 void
241 _pixman_image_validate (pixman_image_t *image)
242 {
243     if (image->common.dirty)
244     {
245         image->common.property_changed (image);
246         image->common.dirty = FALSE;
247     }
248
249     if (image->common.alpha_map)
250         _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
251 }
252
253 PIXMAN_EXPORT pixman_bool_t
254 pixman_image_set_clip_region32 (pixman_image_t *   image,
255                                 pixman_region32_t *region)
256 {
257     image_common_t *common = (image_common_t *)image;
258     pixman_bool_t result;
259
260     if (region)
261     {
262         if ((result = pixman_region32_copy (&common->clip_region, region)))
263             image->common.have_clip_region = TRUE;
264     }
265     else
266     {
267         _pixman_image_reset_clip_region (image);
268
269         result = TRUE;
270     }
271
272     image_property_changed (image);
273
274     return result;
275 }
276
277 PIXMAN_EXPORT pixman_bool_t
278 pixman_image_set_clip_region (pixman_image_t *   image,
279                               pixman_region16_t *region)
280 {
281     image_common_t *common = (image_common_t *)image;
282     pixman_bool_t result;
283
284     if (region)
285     {
286         if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
287             image->common.have_clip_region = TRUE;
288     }
289     else
290     {
291         _pixman_image_reset_clip_region (image);
292
293         result = TRUE;
294     }
295
296     image_property_changed (image);
297
298     return result;
299 }
300
301 PIXMAN_EXPORT void
302 pixman_image_set_has_client_clip (pixman_image_t *image,
303                                   pixman_bool_t   client_clip)
304 {
305     image->common.client_clip = client_clip;
306 }
307
308 PIXMAN_EXPORT pixman_bool_t
309 pixman_image_set_transform (pixman_image_t *          image,
310                             const pixman_transform_t *transform)
311 {
312     static const pixman_transform_t id =
313     {
314         { { pixman_fixed_1, 0, 0 },
315           { 0, pixman_fixed_1, 0 },
316           { 0, 0, pixman_fixed_1 } }
317     };
318
319     image_common_t *common = (image_common_t *)image;
320     pixman_bool_t result;
321
322     if (common->transform == transform)
323         return TRUE;
324
325     if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
326     {
327         free (common->transform);
328         common->transform = NULL;
329         result = TRUE;
330
331         goto out;
332     }
333
334     if (common->transform == NULL)
335         common->transform = malloc (sizeof (pixman_transform_t));
336
337     if (common->transform == NULL)
338     {
339         result = FALSE;
340
341         goto out;
342     }
343
344     memcpy (common->transform, transform, sizeof(pixman_transform_t));
345
346     result = TRUE;
347
348 out:
349     image_property_changed (image);
350
351     return result;
352 }
353
354 PIXMAN_EXPORT void
355 pixman_image_set_repeat (pixman_image_t *image,
356                          pixman_repeat_t repeat)
357 {
358     image->common.repeat = repeat;
359
360     image_property_changed (image);
361 }
362
363 PIXMAN_EXPORT pixman_bool_t
364 pixman_image_set_filter (pixman_image_t *      image,
365                          pixman_filter_t       filter,
366                          const pixman_fixed_t *params,
367                          int                   n_params)
368 {
369     image_common_t *common = (image_common_t *)image;
370     pixman_fixed_t *new_params;
371
372     if (params == common->filter_params && filter == common->filter)
373         return TRUE;
374
375     new_params = NULL;
376     if (params)
377     {
378         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
379         if (!new_params)
380             return FALSE;
381
382         memcpy (new_params,
383                 params, n_params * sizeof (pixman_fixed_t));
384     }
385
386     common->filter = filter;
387
388     if (common->filter_params)
389         free (common->filter_params);
390
391     common->filter_params = new_params;
392     common->n_filter_params = n_params;
393
394     image_property_changed (image);
395     return TRUE;
396 }
397
398 PIXMAN_EXPORT void
399 pixman_image_set_source_clipping (pixman_image_t *image,
400                                   pixman_bool_t   clip_sources)
401 {
402     image->common.clip_sources = clip_sources;
403
404     image_property_changed (image);
405 }
406
407 /* Unlike all the other property setters, this function does not
408  * copy the content of indexed. Doing this copying is simply
409  * way, way too expensive.
410  */
411 PIXMAN_EXPORT void
412 pixman_image_set_indexed (pixman_image_t *        image,
413                           const pixman_indexed_t *indexed)
414 {
415     bits_image_t *bits = (bits_image_t *)image;
416
417     bits->indexed = indexed;
418
419     image_property_changed (image);
420 }
421
422 PIXMAN_EXPORT void
423 pixman_image_set_alpha_map (pixman_image_t *image,
424                             pixman_image_t *alpha_map,
425                             int16_t         x,
426                             int16_t         y)
427 {
428     image_common_t *common = (image_common_t *)image;
429
430     return_if_fail (!alpha_map || alpha_map->type == BITS);
431
432     if (common->alpha_map != (bits_image_t *)alpha_map)
433     {
434         if (common->alpha_map)
435             pixman_image_unref ((pixman_image_t *)common->alpha_map);
436
437         if (alpha_map)
438             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
439         else
440             common->alpha_map = NULL;
441     }
442
443     common->alpha_origin_x = x;
444     common->alpha_origin_y = y;
445
446     image_property_changed (image);
447 }
448
449 PIXMAN_EXPORT void
450 pixman_image_set_component_alpha   (pixman_image_t *image,
451                                     pixman_bool_t   component_alpha)
452 {
453     image->common.component_alpha = component_alpha;
454
455     image_property_changed (image);
456 }
457
458 PIXMAN_EXPORT void
459 pixman_image_set_accessors (pixman_image_t *           image,
460                             pixman_read_memory_func_t  read_func,
461                             pixman_write_memory_func_t write_func)
462 {
463     return_if_fail (image != NULL);
464
465     if (image->type == BITS)
466     {
467         image->bits.read_func = read_func;
468         image->bits.write_func = write_func;
469
470         image_property_changed (image);
471     }
472 }
473
474 PIXMAN_EXPORT uint32_t *
475 pixman_image_get_data (pixman_image_t *image)
476 {
477     if (image->type == BITS)
478         return image->bits.bits;
479
480     return NULL;
481 }
482
483 PIXMAN_EXPORT int
484 pixman_image_get_width (pixman_image_t *image)
485 {
486     if (image->type == BITS)
487         return image->bits.width;
488
489     return 0;
490 }
491
492 PIXMAN_EXPORT int
493 pixman_image_get_height (pixman_image_t *image)
494 {
495     if (image->type == BITS)
496         return image->bits.height;
497
498     return 0;
499 }
500
501 PIXMAN_EXPORT int
502 pixman_image_get_stride (pixman_image_t *image)
503 {
504     if (image->type == BITS)
505         return image->bits.rowstride * (int) sizeof (uint32_t);
506
507     return 0;
508 }
509
510 PIXMAN_EXPORT int
511 pixman_image_get_depth (pixman_image_t *image)
512 {
513     if (image->type == BITS)
514         return PIXMAN_FORMAT_DEPTH (image->bits.format);
515
516     return 0;
517 }
518
519 pixman_bool_t
520 _pixman_image_is_solid (pixman_image_t *image)
521 {
522     if (image->type == SOLID)
523         return TRUE;
524
525     if (image->type != BITS     ||
526         image->bits.width != 1  ||
527         image->bits.height != 1)
528     {
529         return FALSE;
530     }
531
532     if (image->common.repeat == PIXMAN_REPEAT_NONE)
533         return FALSE;
534
535     return TRUE;
536 }
537
538 uint32_t
539 _pixman_image_get_solid (pixman_image_t *     image,
540                          pixman_format_code_t format)
541 {
542     uint32_t result;
543
544     _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
545
546     /* If necessary, convert RGB <--> BGR. */
547     if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
548     {
549         result = (((result & 0xff000000) >>  0) |
550                   ((result & 0x00ff0000) >> 16) |
551                   ((result & 0x0000ff00) >>  0) |
552                   ((result & 0x000000ff) << 16));
553     }
554
555     return result;
556 }
557
558 pixman_bool_t
559 _pixman_image_is_opaque (pixman_image_t *image)
560 {
561     int i;
562
563     if (image->common.alpha_map)
564         return FALSE;
565
566     switch (image->type)
567     {
568     case BITS:
569         if (image->common.repeat == PIXMAN_REPEAT_NONE)
570             return FALSE;
571
572         if (PIXMAN_FORMAT_A (image->bits.format))
573             return FALSE;
574         break;
575
576     case LINEAR:
577     case RADIAL:
578         if (image->common.repeat == PIXMAN_REPEAT_NONE)
579             return FALSE;
580
581         for (i = 0; i < image->gradient.n_stops; ++i)
582         {
583             if (image->gradient.stops[i].color.alpha != 0xffff)
584                 return FALSE;
585         }
586         break;
587
588     case CONICAL:
589         /* Conical gradients always have a transparent border */
590         return FALSE;
591         break;
592
593     case SOLID:
594         if (ALPHA_8 (image->solid.color) != 0xff)
595             return FALSE;
596         break;
597
598     default:
599         return FALSE;
600         break;
601     }
602
603     /* Convolution filters can introduce translucency if the sum of the
604      * weights is lower than 1.
605      */
606     if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
607         return FALSE;
608
609     return TRUE;
610 }
611