2 * Copyright © 2000 SuSE, Inc.
3 * Copyright © 2007 Red Hat, Inc.
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.
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.
32 #include "pixman-private.h"
34 #define Alpha(x) ((x) >> 24)
37 _pixman_init_gradient (gradient_t *gradient,
38 const pixman_gradient_stop_t *stops,
41 return_val_if_fail (n_stops > 0, FALSE);
43 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
47 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
49 gradient->n_stops = n_stops;
51 gradient->stop_range = 0xffff;
52 gradient->color_table = NULL;
53 gradient->color_table_size = 0;
54 gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
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
63 * depth, but that's a project for the future.
66 _pixman_image_get_scanline_generic_64 (pixman_image_t * pict, int x, int y,
67 int width, uint32_t *buffer,
68 const uint32_t *mask, uint32_t maskBits)
70 uint32_t *mask8 = NULL;
72 // Contract the mask image, if one exists, so that the 32-bit fetch
73 // function can use it.
75 mask8 = pixman_malloc_ab(width, sizeof(uint32_t));
79 pixman_contract (mask8, (uint64_t *)mask, width);
82 // Fetch the source image into the first half of buffer.
83 _pixman_image_get_scanline_32 (pict, x, y, width, (uint32_t*)buffer, mask8,
86 // Expand from 32bpp to 64bpp in place.
87 pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, width);
93 _pixman_image_allocate (void)
95 pixman_image_t *image = malloc (sizeof (pixman_image_t));
99 image_common_t *common = &image->common;
101 pixman_region32_init (&common->clip_region);
103 common->have_clip_region = FALSE;
104 common->clip_sources = FALSE;
105 common->transform = NULL;
106 common->repeat = PIXMAN_REPEAT_NONE;
107 common->filter = PIXMAN_FILTER_NEAREST;
108 common->filter_params = NULL;
109 common->n_filter_params = 0;
110 common->alpha_map = NULL;
111 common->component_alpha = FALSE;
112 common->ref_count = 1;
113 common->read_func = NULL;
114 common->write_func = NULL;
115 common->classify = NULL;
116 common->client_clip = FALSE;
117 common->destroy_func = NULL;
118 common->destroy_data = NULL;
125 _pixman_image_classify (pixman_image_t *image,
131 if (image->common.classify)
132 return image->common.classify (image, x, y, width, height);
134 return SOURCE_IMAGE_CLASS_UNKNOWN;
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)
141 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
144 /* Even thought the type of buffer is uint32_t *, the function actually expects
145 * a uint64_t *buffer.
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)
151 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
155 image_property_changed (pixman_image_t *image)
157 image->common.property_changed (image);
161 PIXMAN_EXPORT pixman_image_t *
162 pixman_image_ref (pixman_image_t *image)
164 image->common.ref_count++;
169 /* returns TRUE when the image is freed */
170 PIXMAN_EXPORT pixman_bool_t
171 pixman_image_unref (pixman_image_t *image)
173 image_common_t *common = (image_common_t *)image;
177 if (common->ref_count == 0)
179 if (image->common.destroy_func)
180 image->common.destroy_func (image, image->common.destroy_data);
182 pixman_region32_fini (&common->clip_region);
184 if (common->transform)
185 free (common->transform);
187 if (common->filter_params)
188 free (common->filter_params);
190 if (common->alpha_map)
191 pixman_image_unref ((pixman_image_t *)common->alpha_map);
194 if (image->type == BITS && image->bits.indexed)
195 free (image->bits.indexed);
199 memset (image, 0xaa, sizeof (pixman_image_t));
201 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
203 if (image->gradient.stops)
204 free (image->gradient.stops);
208 if (image->type == BITS && image->bits.free_me)
209 free (image->bits.free_me);
220 pixman_image_set_destroy_function (pixman_image_t *image,
221 pixman_image_destroy_func_t func,
224 image->common.destroy_func = func;
225 image->common.destroy_data = data;
232 _pixman_image_reset_clip_region (pixman_image_t *image)
234 image->common.have_clip_region = FALSE;
237 PIXMAN_EXPORT pixman_bool_t
238 pixman_image_set_clip_region32 (pixman_image_t *image,
239 pixman_region32_t *region)
241 image_common_t *common = (image_common_t *)image;
242 pixman_bool_t result;
246 if ((result = pixman_region32_copy (&common->clip_region, region)))
247 image->common.have_clip_region = TRUE;
251 _pixman_image_reset_clip_region (image);
256 image_property_changed (image);
262 PIXMAN_EXPORT pixman_bool_t
263 pixman_image_set_clip_region (pixman_image_t *image,
264 pixman_region16_t *region)
266 image_common_t *common = (image_common_t *)image;
267 pixman_bool_t result;
271 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
272 image->common.have_clip_region = TRUE;
276 _pixman_image_reset_clip_region (image);
281 image_property_changed (image);
287 pixman_image_set_has_client_clip (pixman_image_t *image,
288 pixman_bool_t client_clip)
290 image->common.client_clip = client_clip;
293 PIXMAN_EXPORT pixman_bool_t
294 pixman_image_set_transform (pixman_image_t *image,
295 const pixman_transform_t *transform)
297 static const pixman_transform_t id =
299 { { pixman_fixed_1, 0, 0 },
300 { 0, pixman_fixed_1, 0 },
301 { 0, 0, pixman_fixed_1 }
305 image_common_t *common = (image_common_t *)image;
306 pixman_bool_t result;
308 if (common->transform == transform)
311 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
313 free(common->transform);
314 common->transform = NULL;
319 if (common->transform == NULL)
320 common->transform = malloc (sizeof (pixman_transform_t));
322 if (common->transform == NULL)
328 memcpy(common->transform, transform, sizeof(pixman_transform_t));
331 image_property_changed (image);
337 pixman_image_set_repeat (pixman_image_t *image,
338 pixman_repeat_t repeat)
340 image->common.repeat = repeat;
342 image_property_changed (image);
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,
351 image_common_t *common = (image_common_t *)image;
352 pixman_fixed_t *new_params;
354 if (params == common->filter_params && filter == common->filter)
360 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
365 params, n_params * sizeof (pixman_fixed_t));
368 common->filter = filter;
370 if (common->filter_params)
371 free (common->filter_params);
373 common->filter_params = new_params;
374 common->n_filter_params = n_params;
376 image_property_changed (image);
381 pixman_image_set_source_clipping (pixman_image_t *image,
382 pixman_bool_t clip_sources)
384 image->common.clip_sources = clip_sources;
387 /* Unlike all the other property setters, this function does not
388 * copy the content of indexed. Doing this copying is simply
389 * way, way too expensive.
392 pixman_image_set_indexed (pixman_image_t *image,
393 const pixman_indexed_t *indexed)
395 bits_image_t *bits = (bits_image_t *)image;
397 bits->indexed = indexed;
399 image_property_changed (image);
403 pixman_image_set_alpha_map (pixman_image_t *image,
404 pixman_image_t *alpha_map,
408 image_common_t *common = (image_common_t *)image;
410 return_if_fail (!alpha_map || alpha_map->type == BITS);
412 if (common->alpha_map != (bits_image_t *)alpha_map)
414 if (common->alpha_map)
415 pixman_image_unref ((pixman_image_t *)common->alpha_map);
418 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
420 common->alpha_map = NULL;
423 common->alpha_origin_x = x;
424 common->alpha_origin_y = y;
426 image_property_changed (image);
430 pixman_image_set_component_alpha (pixman_image_t *image,
431 pixman_bool_t component_alpha)
433 image->common.component_alpha = component_alpha;
435 image_property_changed (image);
440 pixman_image_set_accessors (pixman_image_t *image,
441 pixman_read_memory_func_t read_func,
442 pixman_write_memory_func_t write_func)
444 return_if_fail (image != NULL);
446 image->common.read_func = read_func;
447 image->common.write_func = write_func;
449 image_property_changed (image);
452 PIXMAN_EXPORT uint32_t *
453 pixman_image_get_data (pixman_image_t *image)
455 if (image->type == BITS)
456 return image->bits.bits;
462 pixman_image_get_width (pixman_image_t *image)
464 if (image->type == BITS)
465 return image->bits.width;
471 pixman_image_get_height (pixman_image_t *image)
473 if (image->type == BITS)
474 return image->bits.height;
480 pixman_image_get_stride (pixman_image_t *image)
482 if (image->type == BITS)
483 return image->bits.rowstride * (int) sizeof (uint32_t);
489 pixman_image_get_depth (pixman_image_t *image)
491 if (image->type == BITS)
492 return PIXMAN_FORMAT_DEPTH (image->bits.format);
498 _pixman_image_is_solid (pixman_image_t *image)
500 if (image->type == SOLID)
503 if (image->type != BITS ||
504 image->bits.width != 1 ||
505 image->bits.height != 1)
510 if (image->common.repeat == PIXMAN_REPEAT_NONE)
517 _pixman_image_get_solid (pixman_image_t *image, pixman_format_code_t format)
521 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
523 /* If necessary, convert RGB <--> BGR. */
524 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
526 result = (((result & 0xff000000) >> 0) |
527 ((result & 0x00ff0000) >> 16) |
528 ((result & 0x0000ff00) >> 0) |
529 ((result & 0x000000ff) << 16));
536 _pixman_image_is_opaque (pixman_image_t *image)
540 if (image->common.alpha_map)
546 if (image->common.repeat == PIXMAN_REPEAT_NONE)
549 if (PIXMAN_FORMAT_A (image->bits.format))
555 if (image->common.repeat == PIXMAN_REPEAT_NONE)
558 for (i = 0; i < image->gradient.n_stops; ++i)
560 if (image->gradient.stops[i].color.alpha != 0xffff)
566 /* Conical gradients always have a transparent border */
571 if (Alpha (image->solid.color) != 0xff)
576 /* Convolution filters can introduce translucency if the sum of the
577 * weights is lower than 1.
579 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)