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