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_64_generic (pixman_image_t * pict,
67 int x, int y, int width,
69 uint32_t *mask, uint32_t maskBits)
71 uint32_t *mask8 = NULL;
73 // Contract the mask image, if one exists, so that the 32-bit fetch
74 // function can use it.
76 mask8 = pixman_malloc_ab(width, sizeof(uint32_t));
80 pixman_contract (mask8, (uint64_t *)mask, width);
83 // Fetch the source image into the first half of buffer.
84 _pixman_image_get_scanline_32 (pict, x, y, width, (uint32_t*)buffer, mask8,
87 // Expand from 32bpp to 64bpp in place.
88 pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, width);
94 _pixman_image_allocate (void)
96 pixman_image_t *image = malloc (sizeof (pixman_image_t));
100 image_common_t *common = &image->common;
102 pixman_region32_init (&common->clip_region);
104 common->have_clip_region = FALSE;
105 common->clip_sources = FALSE;
106 common->transform = NULL;
107 common->repeat = PIXMAN_REPEAT_NONE;
108 common->filter = PIXMAN_FILTER_NEAREST;
109 common->filter_params = NULL;
110 common->n_filter_params = 0;
111 common->alpha_map = NULL;
112 common->component_alpha = FALSE;
113 common->ref_count = 1;
114 common->read_func = NULL;
115 common->write_func = NULL;
116 common->classify = NULL;
117 common->client_clip = FALSE;
118 common->destroy_func = NULL;
119 common->destroy_data = NULL;
126 _pixman_image_classify (pixman_image_t *image,
132 if (image->common.classify)
133 return image->common.classify (image, x, y, width, height);
135 return SOURCE_IMAGE_CLASS_UNKNOWN;
139 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
140 uint32_t *buffer, uint32_t *mask, uint32_t mask_bits)
142 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
145 /* Even thought the type of buffer is uint32_t *, the function actually expects
146 * a uint64_t *buffer.
149 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
150 uint32_t *buffer, uint32_t *unused, uint32_t unused2)
152 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
156 image_property_changed (pixman_image_t *image)
158 image->common.property_changed (image);
162 PIXMAN_EXPORT pixman_image_t *
163 pixman_image_ref (pixman_image_t *image)
165 image->common.ref_count++;
170 /* returns TRUE when the image is freed */
171 PIXMAN_EXPORT pixman_bool_t
172 pixman_image_unref (pixman_image_t *image)
174 image_common_t *common = (image_common_t *)image;
178 if (common->ref_count == 0)
180 if (image->common.destroy_func)
181 image->common.destroy_func (image, image->common.destroy_data);
183 pixman_region32_fini (&common->clip_region);
185 if (common->transform)
186 free (common->transform);
188 if (common->filter_params)
189 free (common->filter_params);
191 if (common->alpha_map)
192 pixman_image_unref ((pixman_image_t *)common->alpha_map);
195 if (image->type == BITS && image->bits.indexed)
196 free (image->bits.indexed);
200 memset (image, 0xaa, sizeof (pixman_image_t));
202 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
204 if (image->gradient.stops)
205 free (image->gradient.stops);
209 if (image->type == BITS && image->bits.free_me)
210 free (image->bits.free_me);
221 pixman_image_set_destroy_function (pixman_image_t *image,
222 pixman_image_destroy_func_t func,
225 image->common.destroy_func = func;
226 image->common.destroy_data = data;
233 _pixman_image_reset_clip_region (pixman_image_t *image)
235 image->common.have_clip_region = FALSE;
238 PIXMAN_EXPORT pixman_bool_t
239 pixman_image_set_clip_region32 (pixman_image_t *image,
240 pixman_region32_t *region)
242 image_common_t *common = (image_common_t *)image;
243 pixman_bool_t result;
247 if ((result = pixman_region32_copy (&common->clip_region, region)))
248 image->common.have_clip_region = TRUE;
252 _pixman_image_reset_clip_region (image);
257 image_property_changed (image);
263 PIXMAN_EXPORT pixman_bool_t
264 pixman_image_set_clip_region (pixman_image_t *image,
265 pixman_region16_t *region)
267 image_common_t *common = (image_common_t *)image;
268 pixman_bool_t result;
272 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
273 image->common.have_clip_region = TRUE;
277 _pixman_image_reset_clip_region (image);
282 image_property_changed (image);
288 pixman_image_set_has_client_clip (pixman_image_t *image,
289 pixman_bool_t client_clip)
291 image->common.client_clip = client_clip;
294 PIXMAN_EXPORT pixman_bool_t
295 pixman_image_set_transform (pixman_image_t *image,
296 const pixman_transform_t *transform)
298 static const pixman_transform_t id =
300 { { pixman_fixed_1, 0, 0 },
301 { 0, pixman_fixed_1, 0 },
302 { 0, 0, pixman_fixed_1 }
306 image_common_t *common = (image_common_t *)image;
307 pixman_bool_t result;
309 if (common->transform == transform)
312 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
314 free(common->transform);
315 common->transform = NULL;
320 if (common->transform == NULL)
321 common->transform = malloc (sizeof (pixman_transform_t));
323 if (common->transform == NULL)
329 memcpy(common->transform, transform, sizeof(pixman_transform_t));
332 image_property_changed (image);
338 pixman_image_set_repeat (pixman_image_t *image,
339 pixman_repeat_t repeat)
341 image->common.repeat = repeat;
343 image_property_changed (image);
346 PIXMAN_EXPORT pixman_bool_t
347 pixman_image_set_filter (pixman_image_t *image,
348 pixman_filter_t filter,
349 const pixman_fixed_t *params,
352 image_common_t *common = (image_common_t *)image;
353 pixman_fixed_t *new_params;
355 if (params == common->filter_params && filter == common->filter)
361 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
366 params, n_params * sizeof (pixman_fixed_t));
369 common->filter = filter;
371 if (common->filter_params)
372 free (common->filter_params);
374 common->filter_params = new_params;
375 common->n_filter_params = n_params;
377 image_property_changed (image);
382 pixman_image_set_source_clipping (pixman_image_t *image,
383 pixman_bool_t clip_sources)
385 image->common.clip_sources = clip_sources;
388 /* Unlike all the other property setters, this function does not
389 * copy the content of indexed. Doing this copying is simply
390 * way, way too expensive.
393 pixman_image_set_indexed (pixman_image_t *image,
394 const pixman_indexed_t *indexed)
396 bits_image_t *bits = (bits_image_t *)image;
398 bits->indexed = indexed;
400 image_property_changed (image);
404 pixman_image_set_alpha_map (pixman_image_t *image,
405 pixman_image_t *alpha_map,
409 image_common_t *common = (image_common_t *)image;
411 return_if_fail (!alpha_map || alpha_map->type == BITS);
413 if (common->alpha_map != (bits_image_t *)alpha_map)
415 if (common->alpha_map)
416 pixman_image_unref ((pixman_image_t *)common->alpha_map);
419 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
421 common->alpha_map = NULL;
424 common->alpha_origin_x = x;
425 common->alpha_origin_y = y;
427 image_property_changed (image);
431 pixman_image_set_component_alpha (pixman_image_t *image,
432 pixman_bool_t component_alpha)
434 image->common.component_alpha = component_alpha;
436 image_property_changed (image);
441 pixman_image_set_accessors (pixman_image_t *image,
442 pixman_read_memory_func_t read_func,
443 pixman_write_memory_func_t write_func)
445 return_if_fail (image != NULL);
447 image->common.read_func = read_func;
448 image->common.write_func = write_func;
450 image_property_changed (image);
453 PIXMAN_EXPORT uint32_t *
454 pixman_image_get_data (pixman_image_t *image)
456 if (image->type == BITS)
457 return image->bits.bits;
463 pixman_image_get_width (pixman_image_t *image)
465 if (image->type == BITS)
466 return image->bits.width;
472 pixman_image_get_height (pixman_image_t *image)
474 if (image->type == BITS)
475 return image->bits.height;
481 pixman_image_get_stride (pixman_image_t *image)
483 if (image->type == BITS)
484 return image->bits.rowstride * (int) sizeof (uint32_t);
490 pixman_image_get_depth (pixman_image_t *image)
492 if (image->type == BITS)
493 return PIXMAN_FORMAT_DEPTH (image->bits.format);
499 _pixman_image_is_solid (pixman_image_t *image)
501 if (image->type == SOLID)
504 if (image->type != BITS ||
505 image->bits.width != 1 ||
506 image->bits.height != 1)
511 if (image->common.repeat == PIXMAN_REPEAT_NONE)
518 _pixman_image_get_solid (pixman_image_t *image, pixman_format_code_t format)
522 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
524 /* If necessary, convert RGB <--> BGR. */
525 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
527 result = (((result & 0xff000000) >> 0) |
528 ((result & 0x00ff0000) >> 16) |
529 ((result & 0x0000ff00) >> 0) |
530 ((result & 0x000000ff) << 16));
537 _pixman_image_is_opaque (pixman_image_t *image)
541 if (image->common.alpha_map)
547 if (image->common.repeat == PIXMAN_REPEAT_NONE)
550 if (PIXMAN_FORMAT_A (image->bits.format))
556 if (image->common.repeat == PIXMAN_REPEAT_NONE)
559 for (i = 0; i < image->gradient.n_stops; ++i)
561 if (image->gradient.stops[i].color.alpha != 0xffff)
567 /* Conical gradients always have a transparent border */
572 if (Alpha (image->solid.color) != 0xff)
577 /* Convolution filters can introduce translucency if the sum of the
578 * weights is lower than 1.
580 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)