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;
129 _pixman_image_classify (pixman_image_t *image,
135 if (image->common.classify)
136 return image->common.classify (image, x, y, width, height);
138 return SOURCE_IMAGE_CLASS_UNKNOWN;
142 _pixman_image_get_scanline_32 (pixman_image_t *image,
147 const uint32_t *mask,
150 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
153 /* Even thought the type of buffer is uint32_t *, the function actually expects
154 * a uint64_t *buffer.
157 _pixman_image_get_scanline_64 (pixman_image_t *image,
162 const uint32_t *unused,
165 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
169 image_property_changed (pixman_image_t *image)
171 image->common.property_changed (image);
175 PIXMAN_EXPORT pixman_image_t *
176 pixman_image_ref (pixman_image_t *image)
178 image->common.ref_count++;
183 /* returns TRUE when the image is freed */
184 PIXMAN_EXPORT pixman_bool_t
185 pixman_image_unref (pixman_image_t *image)
187 image_common_t *common = (image_common_t *)image;
191 if (common->ref_count == 0)
193 if (image->common.destroy_func)
194 image->common.destroy_func (image, image->common.destroy_data);
196 pixman_region32_fini (&common->clip_region);
198 if (common->transform)
199 free (common->transform);
201 if (common->filter_params)
202 free (common->filter_params);
204 if (common->alpha_map)
205 pixman_image_unref ((pixman_image_t *)common->alpha_map);
207 if (image->type == LINEAR ||
208 image->type == RADIAL ||
209 image->type == CONICAL)
211 if (image->gradient.stops)
212 free (image->gradient.stops);
215 if (image->type == BITS && image->bits.free_me)
216 free (image->bits.free_me);
227 pixman_image_set_destroy_function (pixman_image_t * image,
228 pixman_image_destroy_func_t func,
231 image->common.destroy_func = func;
232 image->common.destroy_data = data;
236 _pixman_image_reset_clip_region (pixman_image_t *image)
238 image->common.have_clip_region = FALSE;
241 PIXMAN_EXPORT pixman_bool_t
242 pixman_image_set_clip_region32 (pixman_image_t * image,
243 pixman_region32_t *region)
245 image_common_t *common = (image_common_t *)image;
246 pixman_bool_t result;
250 if ((result = pixman_region32_copy (&common->clip_region, region)))
251 image->common.have_clip_region = TRUE;
255 _pixman_image_reset_clip_region (image);
260 image_property_changed (image);
265 PIXMAN_EXPORT pixman_bool_t
266 pixman_image_set_clip_region (pixman_image_t * image,
267 pixman_region16_t *region)
269 image_common_t *common = (image_common_t *)image;
270 pixman_bool_t result;
274 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
275 image->common.have_clip_region = TRUE;
279 _pixman_image_reset_clip_region (image);
284 image_property_changed (image);
290 pixman_image_set_has_client_clip (pixman_image_t *image,
291 pixman_bool_t client_clip)
293 image->common.client_clip = client_clip;
296 PIXMAN_EXPORT pixman_bool_t
297 pixman_image_set_transform (pixman_image_t * image,
298 const pixman_transform_t *transform)
300 static const pixman_transform_t id =
302 { { pixman_fixed_1, 0, 0 },
303 { 0, pixman_fixed_1, 0 },
304 { 0, 0, pixman_fixed_1 }}
307 image_common_t *common = (image_common_t *)image;
308 pixman_bool_t result;
310 if (common->transform == transform)
313 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
315 free (common->transform);
316 common->transform = NULL;
322 if (common->transform == NULL)
323 common->transform = malloc (sizeof (pixman_transform_t));
325 if (common->transform == NULL)
332 memcpy (common->transform, transform, sizeof(pixman_transform_t));
335 image_property_changed (image);
341 pixman_image_set_repeat (pixman_image_t *image,
342 pixman_repeat_t repeat)
344 image->common.repeat = repeat;
346 image_property_changed (image);
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,
355 image_common_t *common = (image_common_t *)image;
356 pixman_fixed_t *new_params;
358 if (params == common->filter_params && filter == common->filter)
364 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
369 params, n_params * sizeof (pixman_fixed_t));
372 common->filter = filter;
374 if (common->filter_params)
375 free (common->filter_params);
377 common->filter_params = new_params;
378 common->n_filter_params = n_params;
380 image_property_changed (image);
385 pixman_image_set_source_clipping (pixman_image_t *image,
386 pixman_bool_t clip_sources)
388 image->common.clip_sources = clip_sources;
390 image_property_changed (image);
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.
398 pixman_image_set_indexed (pixman_image_t * image,
399 const pixman_indexed_t *indexed)
401 bits_image_t *bits = (bits_image_t *)image;
403 bits->indexed = indexed;
405 image_property_changed (image);
409 pixman_image_set_alpha_map (pixman_image_t *image,
410 pixman_image_t *alpha_map,
414 image_common_t *common = (image_common_t *)image;
416 return_if_fail (!alpha_map || alpha_map->type == BITS);
418 if (common->alpha_map != (bits_image_t *)alpha_map)
420 if (common->alpha_map)
421 pixman_image_unref ((pixman_image_t *)common->alpha_map);
424 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
426 common->alpha_map = NULL;
429 common->alpha_origin_x = x;
430 common->alpha_origin_y = y;
432 image_property_changed (image);
436 pixman_image_set_component_alpha (pixman_image_t *image,
437 pixman_bool_t component_alpha)
439 image->common.component_alpha = component_alpha;
441 image_property_changed (image);
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)
449 return_if_fail (image != NULL);
451 if (image->type == BITS)
453 image->bits.read_func = read_func;
454 image->bits.write_func = write_func;
456 image_property_changed (image);
460 PIXMAN_EXPORT uint32_t *
461 pixman_image_get_data (pixman_image_t *image)
463 if (image->type == BITS)
464 return image->bits.bits;
470 pixman_image_get_width (pixman_image_t *image)
472 if (image->type == BITS)
473 return image->bits.width;
479 pixman_image_get_height (pixman_image_t *image)
481 if (image->type == BITS)
482 return image->bits.height;
488 pixman_image_get_stride (pixman_image_t *image)
490 if (image->type == BITS)
491 return image->bits.rowstride * (int) sizeof (uint32_t);
497 pixman_image_get_depth (pixman_image_t *image)
499 if (image->type == BITS)
500 return PIXMAN_FORMAT_DEPTH (image->bits.format);
506 _pixman_image_is_solid (pixman_image_t *image)
508 if (image->type == SOLID)
511 if (image->type != BITS ||
512 image->bits.width != 1 ||
513 image->bits.height != 1)
518 if (image->common.repeat == PIXMAN_REPEAT_NONE)
525 _pixman_image_get_solid (pixman_image_t * image,
526 pixman_format_code_t format)
530 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
532 /* If necessary, convert RGB <--> BGR. */
533 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
535 result = (((result & 0xff000000) >> 0) |
536 ((result & 0x00ff0000) >> 16) |
537 ((result & 0x0000ff00) >> 0) |
538 ((result & 0x000000ff) << 16));
545 _pixman_image_is_opaque (pixman_image_t *image)
549 if (image->common.alpha_map)
555 if (image->common.repeat == PIXMAN_REPEAT_NONE)
558 if (PIXMAN_FORMAT_A (image->bits.format))
564 if (image->common.repeat == PIXMAN_REPEAT_NONE)
567 for (i = 0; i < image->gradient.n_stops; ++i)
569 if (image->gradient.stops[i].color.alpha != 0xffff)
575 /* Conical gradients always have a transparent border */
580 if (ALPHA_8 (image->solid.color) != 0xff)
585 /* Convolution filters can introduce translucency if the sum of the
586 * weights is lower than 1.
588 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)