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