Delete unused WRITE_ACCESS() macro
[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 static void
168 image_property_changed (pixman_image_t *image)
169 {
170     
171     
172     image->common.property_changed (image);
173 }
174
175 /* Ref Counting */
176 PIXMAN_EXPORT pixman_image_t *
177 pixman_image_ref (pixman_image_t *image)
178 {
179     image->common.ref_count++;
180
181     return image;
182 }
183
184 /* returns TRUE when the image is freed */
185 PIXMAN_EXPORT pixman_bool_t
186 pixman_image_unref (pixman_image_t *image)
187 {
188     image_common_t *common = (image_common_t *)image;
189
190     common->ref_count--;
191
192     if (common->ref_count == 0)
193     {
194         if (image->common.destroy_func)
195             image->common.destroy_func (image, image->common.destroy_data);
196         
197         pixman_region32_fini (&common->clip_region);
198
199         if (common->transform)
200             free (common->transform);
201
202         if (common->filter_params)
203             free (common->filter_params);
204
205         if (common->alpha_map)
206             pixman_image_unref ((pixman_image_t *)common->alpha_map);
207
208 #if 0
209         if (image->type == BITS && image->bits.indexed)
210             free (image->bits.indexed);
211 #endif
212
213 #if 0
214         memset (image, 0xaa, sizeof (pixman_image_t));
215 #endif
216         if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
217         {
218             if (image->gradient.stops)
219                 free (image->gradient.stops);
220         }
221
222
223         if (image->type == BITS && image->bits.free_me)
224             free (image->bits.free_me);
225
226         free (image);
227
228         return TRUE;
229     }
230
231     return FALSE;
232 }
233
234 PIXMAN_EXPORT void
235 pixman_image_set_destroy_function (pixman_image_t *image,
236                                    pixman_image_destroy_func_t func,
237                                    void *data)
238 {
239     image->common.destroy_func = func;
240     image->common.destroy_data = data;
241 }
242                                
243
244 /* Constructors */
245
246 void
247 _pixman_image_reset_clip_region (pixman_image_t *image)
248 {
249     image->common.have_clip_region = FALSE;
250 }
251
252 PIXMAN_EXPORT pixman_bool_t
253 pixman_image_set_clip_region32 (pixman_image_t *image,
254                                 pixman_region32_t *region)
255 {
256     image_common_t *common = (image_common_t *)image;
257     pixman_bool_t result;
258
259     if (region)
260     {
261         if ((result = pixman_region32_copy (&common->clip_region, region)))
262             image->common.have_clip_region = TRUE;
263     }
264     else
265     {
266         _pixman_image_reset_clip_region (image);
267
268         result = TRUE;
269     }
270
271     image_property_changed (image);
272
273     return result;
274 }
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
320     image_common_t *common = (image_common_t *)image;
321     pixman_bool_t result;
322
323     if (common->transform == transform)
324         return TRUE;
325
326     if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
327     {
328         free(common->transform);
329         common->transform = NULL;
330         result = TRUE;
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         goto out;
341     }
342
343     memcpy(common->transform, transform, sizeof(pixman_transform_t));
344
345 out:
346     image_property_changed (image);
347     
348     return TRUE;
349 }
350
351 PIXMAN_EXPORT void
352 pixman_image_set_repeat (pixman_image_t  *image,
353                          pixman_repeat_t  repeat)
354 {
355     image->common.repeat = repeat;
356
357     image_property_changed (image);
358 }
359
360 PIXMAN_EXPORT pixman_bool_t
361 pixman_image_set_filter (pixman_image_t       *image,
362                          pixman_filter_t       filter,
363                          const pixman_fixed_t *params,
364                          int                   n_params)
365 {
366     image_common_t *common = (image_common_t *)image;
367     pixman_fixed_t *new_params;
368
369     if (params == common->filter_params && filter == common->filter)
370         return TRUE;
371
372     new_params = NULL;
373     if (params)
374     {
375         new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
376         if (!new_params)
377             return FALSE;
378
379         memcpy (new_params,
380                 params, n_params * sizeof (pixman_fixed_t));
381     }
382
383     common->filter = filter;
384
385     if (common->filter_params)
386         free (common->filter_params);
387
388     common->filter_params = new_params;
389     common->n_filter_params = n_params;
390
391     image_property_changed (image);
392     return TRUE;
393 }
394
395 PIXMAN_EXPORT void
396 pixman_image_set_source_clipping (pixman_image_t  *image,
397                                   pixman_bool_t    clip_sources)
398 {
399     image->common.clip_sources = clip_sources;
400 }
401
402 /* Unlike all the other property setters, this function does not
403  * copy the content of indexed. Doing this copying is simply
404  * way, way too expensive.
405  */
406 PIXMAN_EXPORT void
407 pixman_image_set_indexed (pixman_image_t         *image,
408                           const pixman_indexed_t *indexed)
409 {
410     bits_image_t *bits = (bits_image_t *)image;
411
412     bits->indexed = indexed;
413
414     image_property_changed (image);
415 }
416
417 PIXMAN_EXPORT void
418 pixman_image_set_alpha_map (pixman_image_t *image,
419                             pixman_image_t *alpha_map,
420                             int16_t         x,
421                             int16_t         y)
422 {
423     image_common_t *common = (image_common_t *)image;
424
425     return_if_fail (!alpha_map || alpha_map->type == BITS);
426
427     if (common->alpha_map != (bits_image_t *)alpha_map)
428     {
429         if (common->alpha_map)
430             pixman_image_unref ((pixman_image_t *)common->alpha_map);
431
432         if (alpha_map)
433             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
434         else
435             common->alpha_map = NULL;
436     }
437
438     common->alpha_origin.x = x;
439     common->alpha_origin.y = y;
440
441     image_property_changed (image);
442 }
443
444 PIXMAN_EXPORT void
445 pixman_image_set_component_alpha   (pixman_image_t       *image,
446                                     pixman_bool_t         component_alpha)
447 {
448     image->common.component_alpha = component_alpha;
449
450     image_property_changed (image);
451 }
452
453
454 PIXMAN_EXPORT void
455 pixman_image_set_accessors (pixman_image_t             *image,
456                             pixman_read_memory_func_t   read_func,
457                             pixman_write_memory_func_t  write_func)
458 {
459     return_if_fail (image != NULL);
460
461     image->common.read_func = read_func;
462     image->common.write_func = write_func;
463
464     image_property_changed (image);
465 }
466
467 PIXMAN_EXPORT uint32_t *
468 pixman_image_get_data (pixman_image_t *image)
469 {
470     if (image->type == BITS)
471         return image->bits.bits;
472
473     return NULL;
474 }
475
476 PIXMAN_EXPORT int
477 pixman_image_get_width (pixman_image_t *image)
478 {
479     if (image->type == BITS)
480         return image->bits.width;
481
482     return 0;
483 }
484
485 PIXMAN_EXPORT int
486 pixman_image_get_height (pixman_image_t *image)
487 {
488     if (image->type == BITS)
489         return image->bits.height;
490
491     return 0;
492 }
493
494 PIXMAN_EXPORT int
495 pixman_image_get_stride (pixman_image_t *image)
496 {
497     if (image->type == BITS)
498         return image->bits.rowstride * (int) sizeof (uint32_t);
499
500     return 0;
501 }
502
503 PIXMAN_EXPORT int
504 pixman_image_get_depth (pixman_image_t *image)
505 {
506     if (image->type == BITS)
507         return PIXMAN_FORMAT_DEPTH (image->bits.format);
508
509     return 0;
510 }
511
512 pixman_bool_t
513 pixman_image_can_get_solid (pixman_image_t *image)
514 {
515     if (image->type == SOLID)
516         return TRUE;
517
518     if (image->type != BITS     ||
519         image->bits.width != 1  ||
520         image->bits.height != 1)
521     {
522         return FALSE;
523     }
524
525     if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
526         return FALSE;
527
528     switch (image->bits.format)
529     {
530     case PIXMAN_a8r8g8b8:
531     case PIXMAN_x8r8g8b8:
532     case PIXMAN_a8b8g8r8:
533     case PIXMAN_x8b8g8r8:
534     case PIXMAN_b8g8r8a8:
535     case PIXMAN_b8g8r8x8:
536     case PIXMAN_r8g8b8:
537     case PIXMAN_b8g8r8:
538     case PIXMAN_r5g6b5:
539     case PIXMAN_b5g6r5:
540         return TRUE;
541     default:
542         return FALSE;
543     }
544 }
545
546 pixman_bool_t
547 pixman_image_is_opaque (pixman_image_t *image)
548 {
549     int i;
550
551     if (image->common.alpha_map)
552         return FALSE;
553
554     switch (image->type)
555     {
556     case BITS:
557         if (image->common.repeat == PIXMAN_REPEAT_NONE)
558             return FALSE;
559         
560         if (PIXMAN_FORMAT_A (image->bits.format))
561             return FALSE;
562         break;
563
564     case LINEAR:
565     case RADIAL:
566         if (image->common.repeat == PIXMAN_REPEAT_NONE)
567             return FALSE;
568         
569         for (i = 0; i < image->gradient.n_stops; ++i)
570         {
571             if (image->gradient.stops[i].color.alpha != 0xffff)
572                 return FALSE;
573         }
574         break;
575
576     case CONICAL:
577         /* Conical gradients always have a transparent border */
578         return FALSE;
579         break;
580         
581     case SOLID:
582          if (Alpha (image->solid.color) != 0xff)
583             return FALSE;
584         break;
585     }
586
587     /* Convolution filters can introduce translucency if the sum of the
588      * weights is lower than 1.
589      */
590     if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
591          return FALSE;
592
593      return TRUE;
594 }