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->full_region);
101 pixman_region32_init (&common->clip_region);
102 common->src_clip = &common->full_region;
103 common->has_client_clip = 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;
121 _pixman_image_classify (pixman_image_t *image,
127 if (image->common.classify)
128 return image->common.classify (image, x, y, width, height);
130 return SOURCE_IMAGE_CLASS_UNKNOWN;
134 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
135 uint32_t *buffer, uint32_t *mask, uint32_t mask_bits)
137 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
141 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
142 uint32_t *buffer, uint32_t *unused, uint32_t unused2)
144 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
147 /* Even thought the type of buffer is uint32_t *, the function actually expects
148 * a uint64_t *buffer.
152 _pixman_image_get_fetcher (pixman_image_t *image,
155 assert (image->common.get_scanline_64);
156 assert (image->common.get_scanline_32);
159 return image->common.get_scanline_64;
161 return image->common.get_scanline_32;
164 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
167 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 pixman_region32_fini (&common->clip_region);
194 pixman_region32_fini (&common->full_region);
196 if (common->transform)
197 free (common->transform);
199 if (common->filter_params)
200 free (common->filter_params);
202 if (common->alpha_map)
203 pixman_image_unref ((pixman_image_t *)common->alpha_map);
206 if (image->type == BITS && image->bits.indexed)
207 free (image->bits.indexed);
211 memset (image, 0xaa, sizeof (pixman_image_t));
213 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
215 if (image->gradient.stops)
216 free (image->gradient.stops);
220 if (image->type == BITS && image->bits.free_me)
221 free (image->bits.free_me);
234 _pixman_image_reset_clip_region (pixman_image_t *image)
236 pixman_region32_fini (&image->common.clip_region);
238 if (image->type == BITS)
240 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
241 image->bits.width, image->bits.height);
245 pixman_region32_init (&image->common.clip_region);
249 PIXMAN_EXPORT pixman_bool_t
250 pixman_image_set_clip_region32 (pixman_image_t *image,
251 pixman_region32_t *region)
253 image_common_t *common = (image_common_t *)image;
254 pixman_bool_t result;
258 result = pixman_region32_copy (&common->clip_region, region);
262 _pixman_image_reset_clip_region (image);
267 image_property_changed (image);
273 PIXMAN_EXPORT pixman_bool_t
274 pixman_image_set_clip_region (pixman_image_t *image,
275 pixman_region16_t *region)
277 image_common_t *common = (image_common_t *)image;
278 pixman_bool_t result;
282 result = pixman_region32_copy_from_region16 (&common->clip_region, region);
286 _pixman_image_reset_clip_region (image);
291 image_property_changed (image);
296 /* Sets whether the clip region includes a clip region set by the client
299 pixman_image_set_has_client_clip (pixman_image_t *image,
300 pixman_bool_t client_clip)
302 image->common.has_client_clip = client_clip;
304 image_property_changed (image);
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 }
319 image_common_t *common = (image_common_t *)image;
320 pixman_bool_t result;
322 if (common->transform == transform)
325 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
327 free(common->transform);
328 common->transform = NULL;
333 if (common->transform == NULL)
334 common->transform = malloc (sizeof (pixman_transform_t));
336 if (common->transform == NULL)
342 memcpy(common->transform, transform, sizeof(pixman_transform_t));
345 image_property_changed (image);
351 pixman_image_set_repeat (pixman_image_t *image,
352 pixman_repeat_t repeat)
354 image->common.repeat = repeat;
356 image_property_changed (image);
359 PIXMAN_EXPORT pixman_bool_t
360 pixman_image_set_filter (pixman_image_t *image,
361 pixman_filter_t filter,
362 const pixman_fixed_t *params,
365 image_common_t *common = (image_common_t *)image;
366 pixman_fixed_t *new_params;
368 if (params == common->filter_params && filter == common->filter)
374 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
379 params, n_params * sizeof (pixman_fixed_t));
382 common->filter = filter;
384 if (common->filter_params)
385 free (common->filter_params);
387 common->filter_params = new_params;
388 common->n_filter_params = n_params;
390 image_property_changed (image);
395 pixman_image_set_source_clipping (pixman_image_t *image,
396 pixman_bool_t source_clipping)
398 image_common_t *common = &image->common;
401 common->src_clip = &common->clip_region;
403 common->src_clip = &common->full_region;
405 image_property_changed (image);
408 /* Unlike all the other property setters, this function does not
409 * copy the content of indexed. Doing this copying is simply
410 * way, way too expensive.
413 pixman_image_set_indexed (pixman_image_t *image,
414 const pixman_indexed_t *indexed)
416 bits_image_t *bits = (bits_image_t *)image;
418 bits->indexed = indexed;
420 image_property_changed (image);
424 pixman_image_set_alpha_map (pixman_image_t *image,
425 pixman_image_t *alpha_map,
429 image_common_t *common = (image_common_t *)image;
431 return_if_fail (!alpha_map || alpha_map->type == BITS);
433 if (common->alpha_map != (bits_image_t *)alpha_map)
435 if (common->alpha_map)
436 pixman_image_unref ((pixman_image_t *)common->alpha_map);
439 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
441 common->alpha_map = NULL;
444 common->alpha_origin.x = x;
445 common->alpha_origin.y = y;
447 image_property_changed (image);
451 pixman_image_set_component_alpha (pixman_image_t *image,
452 pixman_bool_t component_alpha)
454 image->common.component_alpha = component_alpha;
456 image_property_changed (image);
461 pixman_image_set_accessors (pixman_image_t *image,
462 pixman_read_memory_func_t read_func,
463 pixman_write_memory_func_t write_func)
465 return_if_fail (image != NULL);
467 image->common.read_func = read_func;
468 image->common.write_func = write_func;
470 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 color_to_uint32 (const pixman_color_t *color)
522 (color->alpha >> 8 << 24) |
523 (color->red >> 8 << 16) |
524 (color->green & 0xff00) |
529 color_to_pixel (pixman_color_t *color,
531 pixman_format_code_t format)
533 uint32_t c = color_to_uint32 (color);
535 if (!(format == PIXMAN_a8r8g8b8 ||
536 format == PIXMAN_x8r8g8b8 ||
537 format == PIXMAN_a8b8g8r8 ||
538 format == PIXMAN_x8b8g8r8 ||
539 format == PIXMAN_b8g8r8a8 ||
540 format == PIXMAN_b8g8r8x8 ||
541 format == PIXMAN_r5g6b5 ||
542 format == PIXMAN_b5g6r5 ||
543 format == PIXMAN_a8))
548 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
550 c = ((c & 0xff000000) >> 0) |
551 ((c & 0x00ff0000) >> 16) |
552 ((c & 0x0000ff00) >> 0) |
553 ((c & 0x000000ff) << 16);
555 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
557 c = ((c & 0xff000000) >> 24) |
558 ((c & 0x00ff0000) >> 8) |
559 ((c & 0x0000ff00) << 8) |
560 ((c & 0x000000ff) << 24);
563 if (format == PIXMAN_a8)
565 else if (format == PIXMAN_r5g6b5 ||
566 format == PIXMAN_b5g6r5)
567 c = cvt8888to0565 (c);
570 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
571 printf ("pixel: %x\n", c);
578 PIXMAN_EXPORT pixman_bool_t
579 pixman_image_fill_rectangles (pixman_op_t op,
580 pixman_image_t *dest,
581 pixman_color_t *color,
583 const pixman_rectangle16_t *rects)
585 pixman_image_t *solid;
589 if (color->alpha == 0xffff)
591 if (op == PIXMAN_OP_OVER)
595 if (op == PIXMAN_OP_CLEAR)
607 if (op == PIXMAN_OP_SRC)
611 if (color_to_pixel (color, &pixel, dest->bits.format))
613 for (i = 0; i < n_rects; ++i)
615 pixman_region32_t fill_region;
617 pixman_box32_t *boxes;
619 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
620 if (!pixman_region32_intersect (&fill_region,
622 &dest->common.clip_region))
626 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
627 for (j = 0; j < n_boxes; ++j)
629 const pixman_box32_t *box = &(boxes[j]);
630 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
631 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
635 pixman_region32_fini (&fill_region);
641 solid = pixman_image_create_solid_fill (color);
645 for (i = 0; i < n_rects; ++i)
647 const pixman_rectangle16_t *rect = &(rects[i]);
649 pixman_image_composite (op, solid, NULL, dest,
652 rect->width, rect->height);
655 pixman_image_unref (solid);
661 pixman_image_can_get_solid (pixman_image_t *image)
663 if (image->type == SOLID)
666 if (image->type != BITS ||
667 image->bits.width != 1 ||
668 image->bits.height != 1)
673 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
676 switch (image->bits.format)
678 case PIXMAN_a8r8g8b8:
679 case PIXMAN_x8r8g8b8:
680 case PIXMAN_a8b8g8r8:
681 case PIXMAN_x8b8g8r8:
682 case PIXMAN_b8g8r8a8:
683 case PIXMAN_b8g8r8x8:
695 pixman_image_is_opaque (pixman_image_t *image)
699 if (image->common.alpha_map)
705 if (image->common.repeat == PIXMAN_REPEAT_NONE)
708 if (PIXMAN_FORMAT_A (image->bits.format))
714 if (image->common.repeat == PIXMAN_REPEAT_NONE)
717 for (i = 0; i < image->gradient.n_stops; ++i)
719 if (image->gradient.stops[i].color.alpha != 0xffff)
725 /* Conical gradients always have a transparent border */
730 if (Alpha (image->solid.color) != 0xff)
735 /* Convolution filters can introduce translucency if the sum of the
736 * weights is lower than 1.
738 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)