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;
167 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
170 image_property_changed (pixman_image_t *image)
174 image->common.property_changed (image);
178 PIXMAN_EXPORT pixman_image_t *
179 pixman_image_ref (pixman_image_t *image)
181 image->common.ref_count++;
186 /* returns TRUE when the image is freed */
187 PIXMAN_EXPORT pixman_bool_t
188 pixman_image_unref (pixman_image_t *image)
190 image_common_t *common = (image_common_t *)image;
194 if (common->ref_count == 0)
196 if (image->common.destroy_func)
197 image->common.destroy_func (image, image->common.destroy_data);
199 pixman_region32_fini (&common->clip_region);
201 if (common->transform)
202 free (common->transform);
204 if (common->filter_params)
205 free (common->filter_params);
207 if (common->alpha_map)
208 pixman_image_unref ((pixman_image_t *)common->alpha_map);
211 if (image->type == BITS && image->bits.indexed)
212 free (image->bits.indexed);
216 memset (image, 0xaa, sizeof (pixman_image_t));
218 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
220 if (image->gradient.stops)
221 free (image->gradient.stops);
225 if (image->type == BITS && image->bits.free_me)
226 free (image->bits.free_me);
237 pixman_image_set_destroy_function (pixman_image_t *image,
238 pixman_image_destroy_func_t func,
241 image->common.destroy_func = func;
242 image->common.destroy_data = data;
249 _pixman_image_reset_clip_region (pixman_image_t *image)
251 image->common.have_clip_region = FALSE;
254 PIXMAN_EXPORT pixman_bool_t
255 pixman_image_set_clip_region32 (pixman_image_t *image,
256 pixman_region32_t *region)
258 image_common_t *common = (image_common_t *)image;
259 pixman_bool_t result;
263 if ((result = pixman_region32_copy (&common->clip_region, region)))
264 image->common.have_clip_region = TRUE;
268 _pixman_image_reset_clip_region (image);
273 image_property_changed (image);
279 PIXMAN_EXPORT pixman_bool_t
280 pixman_image_set_clip_region (pixman_image_t *image,
281 pixman_region16_t *region)
283 image_common_t *common = (image_common_t *)image;
284 pixman_bool_t result;
288 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
289 image->common.have_clip_region = TRUE;
293 _pixman_image_reset_clip_region (image);
298 image_property_changed (image);
304 pixman_image_set_has_client_clip (pixman_image_t *image,
305 pixman_bool_t client_clip)
307 image->common.client_clip = client_clip;
310 PIXMAN_EXPORT pixman_bool_t
311 pixman_image_set_transform (pixman_image_t *image,
312 const pixman_transform_t *transform)
314 static const pixman_transform_t id =
316 { { pixman_fixed_1, 0, 0 },
317 { 0, pixman_fixed_1, 0 },
318 { 0, 0, pixman_fixed_1 }
322 image_common_t *common = (image_common_t *)image;
323 pixman_bool_t result;
325 if (common->transform == transform)
328 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
330 free(common->transform);
331 common->transform = NULL;
336 if (common->transform == NULL)
337 common->transform = malloc (sizeof (pixman_transform_t));
339 if (common->transform == NULL)
345 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;
404 /* Unlike all the other property setters, this function does not
405 * copy the content of indexed. Doing this copying is simply
406 * way, way too expensive.
409 pixman_image_set_indexed (pixman_image_t *image,
410 const pixman_indexed_t *indexed)
412 bits_image_t *bits = (bits_image_t *)image;
414 bits->indexed = indexed;
416 image_property_changed (image);
420 pixman_image_set_alpha_map (pixman_image_t *image,
421 pixman_image_t *alpha_map,
425 image_common_t *common = (image_common_t *)image;
427 return_if_fail (!alpha_map || alpha_map->type == BITS);
429 if (common->alpha_map != (bits_image_t *)alpha_map)
431 if (common->alpha_map)
432 pixman_image_unref ((pixman_image_t *)common->alpha_map);
435 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
437 common->alpha_map = NULL;
440 common->alpha_origin.x = x;
441 common->alpha_origin.y = y;
443 image_property_changed (image);
447 pixman_image_set_component_alpha (pixman_image_t *image,
448 pixman_bool_t component_alpha)
450 image->common.component_alpha = component_alpha;
452 image_property_changed (image);
457 pixman_image_set_accessors (pixman_image_t *image,
458 pixman_read_memory_func_t read_func,
459 pixman_write_memory_func_t write_func)
461 return_if_fail (image != NULL);
463 image->common.read_func = read_func;
464 image->common.write_func = write_func;
466 image_property_changed (image);
469 PIXMAN_EXPORT uint32_t *
470 pixman_image_get_data (pixman_image_t *image)
472 if (image->type == BITS)
473 return image->bits.bits;
479 pixman_image_get_width (pixman_image_t *image)
481 if (image->type == BITS)
482 return image->bits.width;
488 pixman_image_get_height (pixman_image_t *image)
490 if (image->type == BITS)
491 return image->bits.height;
497 pixman_image_get_stride (pixman_image_t *image)
499 if (image->type == BITS)
500 return image->bits.rowstride * (int) sizeof (uint32_t);
506 pixman_image_get_depth (pixman_image_t *image)
508 if (image->type == BITS)
509 return PIXMAN_FORMAT_DEPTH (image->bits.format);
515 color_to_uint32 (const pixman_color_t *color)
518 (color->alpha >> 8 << 24) |
519 (color->red >> 8 << 16) |
520 (color->green & 0xff00) |
525 color_to_pixel (pixman_color_t *color,
527 pixman_format_code_t format)
529 uint32_t c = color_to_uint32 (color);
531 if (!(format == PIXMAN_a8r8g8b8 ||
532 format == PIXMAN_x8r8g8b8 ||
533 format == PIXMAN_a8b8g8r8 ||
534 format == PIXMAN_x8b8g8r8 ||
535 format == PIXMAN_b8g8r8a8 ||
536 format == PIXMAN_b8g8r8x8 ||
537 format == PIXMAN_r5g6b5 ||
538 format == PIXMAN_b5g6r5 ||
539 format == PIXMAN_a8))
544 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
546 c = ((c & 0xff000000) >> 0) |
547 ((c & 0x00ff0000) >> 16) |
548 ((c & 0x0000ff00) >> 0) |
549 ((c & 0x000000ff) << 16);
551 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
553 c = ((c & 0xff000000) >> 24) |
554 ((c & 0x00ff0000) >> 8) |
555 ((c & 0x0000ff00) << 8) |
556 ((c & 0x000000ff) << 24);
559 if (format == PIXMAN_a8)
561 else if (format == PIXMAN_r5g6b5 ||
562 format == PIXMAN_b5g6r5)
563 c = cvt8888to0565 (c);
566 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
567 printf ("pixel: %x\n", c);
574 PIXMAN_EXPORT pixman_bool_t
575 pixman_image_fill_rectangles (pixman_op_t op,
576 pixman_image_t *dest,
577 pixman_color_t *color,
579 const pixman_rectangle16_t *rects)
581 pixman_image_t *solid;
585 if (color->alpha == 0xffff)
587 if (op == PIXMAN_OP_OVER)
591 if (op == PIXMAN_OP_CLEAR)
603 if (op == PIXMAN_OP_SRC)
607 if (color_to_pixel (color, &pixel, dest->bits.format))
609 for (i = 0; i < n_rects; ++i)
611 pixman_region32_t fill_region;
613 pixman_box32_t *boxes;
615 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
617 if (dest->common.have_clip_region)
619 if (!pixman_region32_intersect (&fill_region,
621 &dest->common.clip_region))
625 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
626 for (j = 0; j < n_boxes; ++j)
628 const pixman_box32_t *box = &(boxes[j]);
629 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
630 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
634 pixman_region32_fini (&fill_region);
640 solid = pixman_image_create_solid_fill (color);
644 for (i = 0; i < n_rects; ++i)
646 const pixman_rectangle16_t *rect = &(rects[i]);
648 pixman_image_composite (op, solid, NULL, dest,
651 rect->width, rect->height);
654 pixman_image_unref (solid);
660 pixman_image_can_get_solid (pixman_image_t *image)
662 if (image->type == SOLID)
665 if (image->type != BITS ||
666 image->bits.width != 1 ||
667 image->bits.height != 1)
672 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
675 switch (image->bits.format)
677 case PIXMAN_a8r8g8b8:
678 case PIXMAN_x8r8g8b8:
679 case PIXMAN_a8b8g8r8:
680 case PIXMAN_x8b8g8r8:
681 case PIXMAN_b8g8r8a8:
682 case PIXMAN_b8g8r8x8:
694 pixman_image_is_opaque (pixman_image_t *image)
698 if (image->common.alpha_map)
704 if (image->common.repeat == PIXMAN_REPEAT_NONE)
707 if (PIXMAN_FORMAT_A (image->bits.format))
713 if (image->common.repeat == PIXMAN_REPEAT_NONE)
716 for (i = 0; i < image->gradient.n_stops; ++i)
718 if (image->gradient.stops[i].color.alpha != 0xffff)
724 /* Conical gradients always have a transparent border */
729 if (Alpha (image->solid.color) != 0xff)
734 /* Convolution filters can introduce translucency if the sum of the
735 * weights is lower than 1.
737 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)