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"
33 #include "pixman-combine32.h"
36 _pixman_init_gradient (gradient_t * gradient,
37 const pixman_gradient_stop_t *stops,
40 return_val_if_fail (n_stops > 0, FALSE);
42 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
46 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
48 gradient->n_stops = n_stops;
50 gradient->stop_range = 0xffff;
51 gradient->color_table = NULL;
52 gradient->color_table_size = 0;
53 gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
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.
65 _pixman_image_get_scanline_generic_64 (pixman_image_t * image,
70 const uint32_t * mask,
73 uint32_t *mask8 = NULL;
75 /* Contract the mask image, if one exists, so that the 32-bit fetch
76 * function can use it.
80 mask8 = pixman_malloc_ab (width, sizeof(uint32_t));
84 pixman_contract (mask8, (uint64_t *)mask, width);
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,
91 /* Expand from 32bpp to 64bpp in place. */
92 pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, width);
98 _pixman_image_allocate (void)
100 pixman_image_t *image = malloc (sizeof (pixman_image_t));
104 image_common_t *common = &image->common;
106 pixman_region32_init (&common->clip_region);
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 common->dirty = TRUE;
130 _pixman_image_classify (pixman_image_t *image,
136 if (image->common.classify)
137 return image->common.classify (image, x, y, width, height);
139 return SOURCE_IMAGE_CLASS_UNKNOWN;
143 _pixman_image_get_scanline_32 (pixman_image_t *image,
148 const uint32_t *mask,
151 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
154 /* Even thought the type of buffer is uint32_t *, the function actually expects
155 * a uint64_t *buffer.
158 _pixman_image_get_scanline_64 (pixman_image_t *image,
163 const uint32_t *unused,
166 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
170 image_property_changed (pixman_image_t *image)
172 image->common.dirty = TRUE;
176 PIXMAN_EXPORT pixman_image_t *
177 pixman_image_ref (pixman_image_t *image)
179 image->common.ref_count++;
184 /* returns TRUE when the image is freed */
185 PIXMAN_EXPORT pixman_bool_t
186 pixman_image_unref (pixman_image_t *image)
188 image_common_t *common = (image_common_t *)image;
192 if (common->ref_count == 0)
194 if (image->common.destroy_func)
195 image->common.destroy_func (image, image->common.destroy_data);
197 pixman_region32_fini (&common->clip_region);
199 if (common->transform)
200 free (common->transform);
202 if (common->filter_params)
203 free (common->filter_params);
205 if (common->alpha_map)
206 pixman_image_unref ((pixman_image_t *)common->alpha_map);
208 if (image->type == LINEAR ||
209 image->type == RADIAL ||
210 image->type == CONICAL)
212 if (image->gradient.stops)
213 free (image->gradient.stops);
216 if (image->type == BITS && image->bits.free_me)
217 free (image->bits.free_me);
228 pixman_image_set_destroy_function (pixman_image_t * image,
229 pixman_image_destroy_func_t func,
232 image->common.destroy_func = func;
233 image->common.destroy_data = data;
237 _pixman_image_reset_clip_region (pixman_image_t *image)
239 image->common.have_clip_region = FALSE;
243 _pixman_image_validate (pixman_image_t *image)
245 if (image->common.dirty)
247 image->common.property_changed (image);
248 image->common.dirty = FALSE;
252 PIXMAN_EXPORT pixman_bool_t
253 pixman_image_set_clip_region32 (pixman_image_t * image,
254 pixman_region32_t *region)
256 image_common_t *common = (image_common_t *)image;
257 pixman_bool_t result;
261 if ((result = pixman_region32_copy (&common->clip_region, region)))
262 image->common.have_clip_region = TRUE;
266 _pixman_image_reset_clip_region (image);
271 image_property_changed (image);
276 PIXMAN_EXPORT pixman_bool_t
277 pixman_image_set_clip_region (pixman_image_t * image,
278 pixman_region16_t *region)
280 image_common_t *common = (image_common_t *)image;
281 pixman_bool_t result;
285 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
286 image->common.have_clip_region = TRUE;
290 _pixman_image_reset_clip_region (image);
295 image_property_changed (image);
301 pixman_image_set_has_client_clip (pixman_image_t *image,
302 pixman_bool_t client_clip)
304 image->common.client_clip = client_clip;
307 PIXMAN_EXPORT pixman_bool_t
308 pixman_image_set_transform (pixman_image_t * image,
309 const pixman_transform_t *transform)
311 static const pixman_transform_t id =
313 { { pixman_fixed_1, 0, 0 },
314 { 0, pixman_fixed_1, 0 },
315 { 0, 0, pixman_fixed_1 } }
318 image_common_t *common = (image_common_t *)image;
319 pixman_bool_t result;
321 if (common->transform == transform)
324 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
326 free (common->transform);
327 common->transform = NULL;
333 if (common->transform == NULL)
334 common->transform = malloc (sizeof (pixman_transform_t));
336 if (common->transform == NULL)
343 memcpy (common->transform, transform, sizeof(pixman_transform_t));
348 image_property_changed (image);
354 pixman_image_set_repeat (pixman_image_t *image,
355 pixman_repeat_t repeat)
357 image->common.repeat = repeat;
359 image_property_changed (image);
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,
368 image_common_t *common = (image_common_t *)image;
369 pixman_fixed_t *new_params;
371 if (params == common->filter_params && filter == common->filter)
377 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
382 params, n_params * sizeof (pixman_fixed_t));
385 common->filter = filter;
387 if (common->filter_params)
388 free (common->filter_params);
390 common->filter_params = new_params;
391 common->n_filter_params = n_params;
393 image_property_changed (image);
398 pixman_image_set_source_clipping (pixman_image_t *image,
399 pixman_bool_t clip_sources)
401 image->common.clip_sources = clip_sources;
403 image_property_changed (image);
406 /* Unlike all the other property setters, this function does not
407 * copy the content of indexed. Doing this copying is simply
408 * way, way too expensive.
411 pixman_image_set_indexed (pixman_image_t * image,
412 const pixman_indexed_t *indexed)
414 bits_image_t *bits = (bits_image_t *)image;
416 bits->indexed = indexed;
418 image_property_changed (image);
422 pixman_image_set_alpha_map (pixman_image_t *image,
423 pixman_image_t *alpha_map,
427 image_common_t *common = (image_common_t *)image;
429 return_if_fail (!alpha_map || alpha_map->type == BITS);
431 if (common->alpha_map != (bits_image_t *)alpha_map)
433 if (common->alpha_map)
434 pixman_image_unref ((pixman_image_t *)common->alpha_map);
437 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
439 common->alpha_map = NULL;
442 common->alpha_origin_x = x;
443 common->alpha_origin_y = y;
445 image_property_changed (image);
449 pixman_image_set_component_alpha (pixman_image_t *image,
450 pixman_bool_t component_alpha)
452 image->common.component_alpha = component_alpha;
454 image_property_changed (image);
458 pixman_image_set_accessors (pixman_image_t * image,
459 pixman_read_memory_func_t read_func,
460 pixman_write_memory_func_t write_func)
462 return_if_fail (image != NULL);
464 if (image->type == BITS)
466 image->bits.read_func = read_func;
467 image->bits.write_func = write_func;
469 image_property_changed (image);
473 PIXMAN_EXPORT uint32_t *
474 pixman_image_get_data (pixman_image_t *image)
476 if (image->type == BITS)
477 return image->bits.bits;
483 pixman_image_get_width (pixman_image_t *image)
485 if (image->type == BITS)
486 return image->bits.width;
492 pixman_image_get_height (pixman_image_t *image)
494 if (image->type == BITS)
495 return image->bits.height;
501 pixman_image_get_stride (pixman_image_t *image)
503 if (image->type == BITS)
504 return image->bits.rowstride * (int) sizeof (uint32_t);
510 pixman_image_get_depth (pixman_image_t *image)
512 if (image->type == BITS)
513 return PIXMAN_FORMAT_DEPTH (image->bits.format);
519 _pixman_image_is_solid (pixman_image_t *image)
521 if (image->type == SOLID)
524 if (image->type != BITS ||
525 image->bits.width != 1 ||
526 image->bits.height != 1)
531 if (image->common.repeat == PIXMAN_REPEAT_NONE)
538 _pixman_image_get_solid (pixman_image_t * image,
539 pixman_format_code_t format)
543 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
545 /* If necessary, convert RGB <--> BGR. */
546 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
548 result = (((result & 0xff000000) >> 0) |
549 ((result & 0x00ff0000) >> 16) |
550 ((result & 0x0000ff00) >> 0) |
551 ((result & 0x000000ff) << 16));
558 _pixman_image_is_opaque (pixman_image_t *image)
562 if (image->common.alpha_map)
568 if (image->common.repeat == PIXMAN_REPEAT_NONE)
571 if (PIXMAN_FORMAT_A (image->bits.format))
577 if (image->common.repeat == PIXMAN_REPEAT_NONE)
580 for (i = 0; i < image->gradient.n_stops; ++i)
582 if (image->gradient.stops[i].color.alpha != 0xffff)
588 /* Conical gradients always have a transparent border */
593 if (ALPHA_8 (image->solid.color) != 0xff)
598 /* Convolution filters can introduce translucency if the sum of the
599 * weights is lower than 1.
601 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)