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