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