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 depth, but
63 * that's a project for the future.
66 _pixman_image_get_scanline_64_generic (pixman_image_t * pict, int x, int y, int width,
67 uint64_t *buffer, uint64_t *mask, uint32_t maskBits)
69 uint32_t *mask8 = NULL;
71 // Contract the mask image, if one exists, so that the 32-bit fetch function
74 mask8 = pixman_malloc_ab(width, sizeof(uint32_t));
78 pixman_contract(mask8, mask, width);
81 // Fetch the source image into the first half of buffer.
82 _pixman_image_get_scanline_32 (pict, x, y, width, (uint32_t*)buffer, mask8,
85 // Expand from 32bpp to 64bpp in place.
86 pixman_expand(buffer, (uint32_t*)buffer, PIXMAN_a8r8g8b8, width);
92 _pixman_image_allocate (void)
94 pixman_image_t *image = malloc (sizeof (pixman_image_t));
98 image_common_t *common = &image->common;
100 pixman_region32_init (&common->clip_region);
102 common->have_clip_region = FALSE;
103 common->clip_sources = FALSE;
104 common->transform = NULL;
105 common->repeat = PIXMAN_REPEAT_NONE;
106 common->filter = PIXMAN_FILTER_NEAREST;
107 common->filter_params = NULL;
108 common->n_filter_params = 0;
109 common->alpha_map = NULL;
110 common->component_alpha = FALSE;
111 common->ref_count = 1;
112 common->read_func = NULL;
113 common->write_func = NULL;
114 common->classify = NULL;
115 common->client_clip = FALSE;
116 common->destroy_func = NULL;
117 common->destroy_data = NULL;
124 _pixman_image_classify (pixman_image_t *image,
130 if (image->common.classify)
131 return image->common.classify (image, x, y, width, height);
133 return SOURCE_IMAGE_CLASS_UNKNOWN;
137 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
138 uint32_t *buffer, uint32_t *mask, uint32_t mask_bits)
140 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
144 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
145 uint32_t *buffer, uint32_t *unused, uint32_t unused2)
147 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
150 /* Even thought the type of buffer is uint32_t *, the function actually expects
151 * a uint64_t *buffer.
155 _pixman_image_get_fetcher (pixman_image_t *image,
158 assert (image->common.get_scanline_64);
159 assert (image->common.get_scanline_32);
162 return image->common.get_scanline_64;
164 return image->common.get_scanline_32;
168 image_property_changed (pixman_image_t *image)
170 image->common.property_changed (image);
174 PIXMAN_EXPORT pixman_image_t *
175 pixman_image_ref (pixman_image_t *image)
177 image->common.ref_count++;
182 /* returns TRUE when the image is freed */
183 PIXMAN_EXPORT pixman_bool_t
184 pixman_image_unref (pixman_image_t *image)
186 image_common_t *common = (image_common_t *)image;
190 if (common->ref_count == 0)
192 if (image->common.destroy_func)
193 image->common.destroy_func (image, image->common.destroy_data);
195 pixman_region32_fini (&common->clip_region);
197 if (common->transform)
198 free (common->transform);
200 if (common->filter_params)
201 free (common->filter_params);
203 if (common->alpha_map)
204 pixman_image_unref ((pixman_image_t *)common->alpha_map);
207 if (image->type == BITS && image->bits.indexed)
208 free (image->bits.indexed);
212 memset (image, 0xaa, sizeof (pixman_image_t));
214 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
216 if (image->gradient.stops)
217 free (image->gradient.stops);
221 if (image->type == BITS && image->bits.free_me)
222 free (image->bits.free_me);
233 pixman_image_set_destroy_function (pixman_image_t *image,
234 pixman_image_destroy_func_t func,
237 image->common.destroy_func = func;
238 image->common.destroy_data = data;
245 _pixman_image_reset_clip_region (pixman_image_t *image)
247 image->common.have_clip_region = FALSE;
250 PIXMAN_EXPORT pixman_bool_t
251 pixman_image_set_clip_region32 (pixman_image_t *image,
252 pixman_region32_t *region)
254 image_common_t *common = (image_common_t *)image;
255 pixman_bool_t result;
259 if ((result = pixman_region32_copy (&common->clip_region, region)))
260 image->common.have_clip_region = TRUE;
264 _pixman_image_reset_clip_region (image);
269 image_property_changed (image);
275 PIXMAN_EXPORT pixman_bool_t
276 pixman_image_set_clip_region (pixman_image_t *image,
277 pixman_region16_t *region)
279 image_common_t *common = (image_common_t *)image;
280 pixman_bool_t result;
284 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
285 image->common.have_clip_region = TRUE;
289 _pixman_image_reset_clip_region (image);
294 image_property_changed (image);
300 pixman_image_set_has_client_clip (pixman_image_t *image,
301 pixman_bool_t client_clip)
303 image->common.client_clip = client_clip;
306 PIXMAN_EXPORT pixman_bool_t
307 pixman_image_set_transform (pixman_image_t *image,
308 const pixman_transform_t *transform)
310 static const pixman_transform_t id =
312 { { pixman_fixed_1, 0, 0 },
313 { 0, pixman_fixed_1, 0 },
314 { 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;
332 if (common->transform == NULL)
333 common->transform = malloc (sizeof (pixman_transform_t));
335 if (common->transform == NULL)
341 memcpy(common->transform, transform, sizeof(pixman_transform_t));
344 image_property_changed (image);
350 pixman_image_set_repeat (pixman_image_t *image,
351 pixman_repeat_t repeat)
353 image->common.repeat = repeat;
355 image_property_changed (image);
358 PIXMAN_EXPORT pixman_bool_t
359 pixman_image_set_filter (pixman_image_t *image,
360 pixman_filter_t filter,
361 const pixman_fixed_t *params,
364 image_common_t *common = (image_common_t *)image;
365 pixman_fixed_t *new_params;
367 if (params == common->filter_params && filter == common->filter)
373 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
378 params, n_params * sizeof (pixman_fixed_t));
381 common->filter = filter;
383 if (common->filter_params)
384 free (common->filter_params);
386 common->filter_params = new_params;
387 common->n_filter_params = n_params;
389 image_property_changed (image);
394 pixman_image_set_source_clipping (pixman_image_t *image,
395 pixman_bool_t clip_sources)
397 image->common.clip_sources = clip_sources;
400 /* Unlike all the other property setters, this function does not
401 * copy the content of indexed. Doing this copying is simply
402 * way, way too expensive.
405 pixman_image_set_indexed (pixman_image_t *image,
406 const pixman_indexed_t *indexed)
408 bits_image_t *bits = (bits_image_t *)image;
410 bits->indexed = indexed;
412 image_property_changed (image);
416 pixman_image_set_alpha_map (pixman_image_t *image,
417 pixman_image_t *alpha_map,
421 image_common_t *common = (image_common_t *)image;
423 return_if_fail (!alpha_map || alpha_map->type == BITS);
425 if (common->alpha_map != (bits_image_t *)alpha_map)
427 if (common->alpha_map)
428 pixman_image_unref ((pixman_image_t *)common->alpha_map);
431 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
433 common->alpha_map = NULL;
436 common->alpha_origin_x = x;
437 common->alpha_origin_y = y;
439 image_property_changed (image);
443 pixman_image_set_component_alpha (pixman_image_t *image,
444 pixman_bool_t component_alpha)
446 image->common.component_alpha = component_alpha;
448 image_property_changed (image);
453 pixman_image_set_accessors (pixman_image_t *image,
454 pixman_read_memory_func_t read_func,
455 pixman_write_memory_func_t write_func)
457 return_if_fail (image != NULL);
459 image->common.read_func = read_func;
460 image->common.write_func = write_func;
462 image_property_changed (image);
465 PIXMAN_EXPORT uint32_t *
466 pixman_image_get_data (pixman_image_t *image)
468 if (image->type == BITS)
469 return image->bits.bits;
475 pixman_image_get_width (pixman_image_t *image)
477 if (image->type == BITS)
478 return image->bits.width;
484 pixman_image_get_height (pixman_image_t *image)
486 if (image->type == BITS)
487 return image->bits.height;
493 pixman_image_get_stride (pixman_image_t *image)
495 if (image->type == BITS)
496 return image->bits.rowstride * (int) sizeof (uint32_t);
502 pixman_image_get_depth (pixman_image_t *image)
504 if (image->type == BITS)
505 return PIXMAN_FORMAT_DEPTH (image->bits.format);
511 _pixman_image_is_solid (pixman_image_t *image)
513 if (image->type == SOLID)
516 if (image->type != BITS ||
517 image->bits.width != 1 ||
518 image->bits.height != 1)
523 if (image->common.repeat == PIXMAN_REPEAT_NONE)
530 _pixman_image_get_solid (pixman_image_t *image, pixman_format_code_t format)
534 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
536 /* If necessary, convert RGB <--> BGR. */
537 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
539 result = (((result & 0xff000000) >> 0) |
540 ((result & 0x00ff0000) >> 16) |
541 ((result & 0x0000ff00) >> 0) |
542 ((result & 0x000000ff) << 16));
549 _pixman_image_is_opaque (pixman_image_t *image)
553 if (image->common.alpha_map)
559 if (image->common.repeat == PIXMAN_REPEAT_NONE)
562 if (PIXMAN_FORMAT_A (image->bits.format))
568 if (image->common.repeat == PIXMAN_REPEAT_NONE)
571 for (i = 0; i < image->gradient.n_stops; ++i)
573 if (image->gradient.stops[i].color.alpha != 0xffff)
579 /* Conical gradients always have a transparent border */
584 if (Alpha (image->solid.color) != 0xff)
589 /* Convolution filters can introduce translucency if the sum of the
590 * weights is lower than 1.
592 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)