Move workaround logic to 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 #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, int x, int y,
66                                        int width, uint32_t *buffer,
67                                        const uint32_t *mask, uint32_t mask_bits)
68 {
69     uint32_t *mask8 = NULL;
70
71     // Contract the mask image, if one exists, so that the 32-bit fetch
72     // function 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, (uint64_t *)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                                    mask_bits);
84
85     // Expand from 32bpp to 64bpp in place.
86     pixman_expand ((uint64_t *)buffer, 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         common->need_workaround = FALSE;
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     image_property_changed (image);
387 }
388
389 /* Unlike all the other property setters, this function does not
390  * copy the content of indexed. Doing this copying is simply
391  * way, way too expensive.
392  */
393 PIXMAN_EXPORT void
394 pixman_image_set_indexed (pixman_image_t         *image,
395                           const pixman_indexed_t *indexed)
396 {
397     bits_image_t *bits = (bits_image_t *)image;
398
399     bits->indexed = indexed;
400
401     image_property_changed (image);
402 }
403
404 PIXMAN_EXPORT void
405 pixman_image_set_alpha_map (pixman_image_t *image,
406                             pixman_image_t *alpha_map,
407                             int16_t         x,
408                             int16_t         y)
409 {
410     image_common_t *common = (image_common_t *)image;
411
412     return_if_fail (!alpha_map || alpha_map->type == BITS);
413
414     if (common->alpha_map != (bits_image_t *)alpha_map)
415     {
416         if (common->alpha_map)
417             pixman_image_unref ((pixman_image_t *)common->alpha_map);
418
419         if (alpha_map)
420             common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
421         else
422             common->alpha_map = NULL;
423     }
424
425     common->alpha_origin_x = x;
426     common->alpha_origin_y = y;
427
428     image_property_changed (image);
429 }
430
431 PIXMAN_EXPORT void
432 pixman_image_set_component_alpha   (pixman_image_t       *image,
433                                     pixman_bool_t         component_alpha)
434 {
435     image->common.component_alpha = component_alpha;
436
437     image_property_changed (image);
438 }
439
440
441 PIXMAN_EXPORT void
442 pixman_image_set_accessors (pixman_image_t             *image,
443                             pixman_read_memory_func_t   read_func,
444                             pixman_write_memory_func_t  write_func)
445 {
446     return_if_fail (image != NULL);
447
448     image->common.read_func = read_func;
449     image->common.write_func = write_func;
450
451     image_property_changed (image);
452 }
453
454 PIXMAN_EXPORT uint32_t *
455 pixman_image_get_data (pixman_image_t *image)
456 {
457     if (image->type == BITS)
458         return image->bits.bits;
459
460     return NULL;
461 }
462
463 PIXMAN_EXPORT int
464 pixman_image_get_width (pixman_image_t *image)
465 {
466     if (image->type == BITS)
467         return image->bits.width;
468
469     return 0;
470 }
471
472 PIXMAN_EXPORT int
473 pixman_image_get_height (pixman_image_t *image)
474 {
475     if (image->type == BITS)
476         return image->bits.height;
477
478     return 0;
479 }
480
481 PIXMAN_EXPORT int
482 pixman_image_get_stride (pixman_image_t *image)
483 {
484     if (image->type == BITS)
485         return image->bits.rowstride * (int) sizeof (uint32_t);
486
487     return 0;
488 }
489
490 PIXMAN_EXPORT int
491 pixman_image_get_depth (pixman_image_t *image)
492 {
493     if (image->type == BITS)
494         return PIXMAN_FORMAT_DEPTH (image->bits.format);
495
496     return 0;
497 }
498
499 pixman_bool_t
500 _pixman_image_is_solid (pixman_image_t *image)
501 {
502     if (image->type == SOLID)
503         return TRUE;
504
505     if (image->type != BITS     ||
506         image->bits.width != 1  ||
507         image->bits.height != 1)
508     {
509         return FALSE;
510     }
511
512     if (image->common.repeat == PIXMAN_REPEAT_NONE)
513         return FALSE;
514
515     return TRUE;
516 }
517
518 uint32_t
519 _pixman_image_get_solid (pixman_image_t *image, pixman_format_code_t format)
520 {
521     uint32_t result;
522     
523     _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
524     
525     /* If necessary, convert RGB <--> BGR. */
526     if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
527     {
528         result = (((result & 0xff000000) >>  0) |
529                   ((result & 0x00ff0000) >> 16) |
530                   ((result & 0x0000ff00) >>  0) |
531                   ((result & 0x000000ff) << 16));
532     }                                                                   
533     
534     return result;                                                      
535 }
536
537 pixman_bool_t
538 _pixman_image_is_opaque (pixman_image_t *image)
539 {
540     int i;
541
542     if (image->common.alpha_map)
543         return FALSE;
544
545     switch (image->type)
546     {
547     case BITS:
548         if (image->common.repeat == PIXMAN_REPEAT_NONE)
549             return FALSE;
550         
551         if (PIXMAN_FORMAT_A (image->bits.format))
552             return FALSE;
553         break;
554
555     case LINEAR:
556     case RADIAL:
557         if (image->common.repeat == PIXMAN_REPEAT_NONE)
558             return FALSE;
559         
560         for (i = 0; i < image->gradient.n_stops; ++i)
561         {
562             if (image->gradient.stops[i].color.alpha != 0xffff)
563                 return FALSE;
564         }
565         break;
566
567     case CONICAL:
568         /* Conical gradients always have a transparent border */
569         return FALSE;
570         break;
571         
572     case SOLID:
573         if (ALPHA_8 (image->solid.color) != 0xff)
574             return FALSE;
575         break;
576     }
577
578     /* Convolution filters can introduce translucency if the sum of the
579      * weights is lower than 1.
580      */
581     if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
582          return FALSE;
583
584      return TRUE;
585 }