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