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