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;
122 _pixman_image_classify (pixman_image_t *image,
128 if (image->common.classify)
129 return image->common.classify (image, x, y, width, height);
131 return SOURCE_IMAGE_CLASS_UNKNOWN;
135 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
136 uint32_t *buffer, uint32_t *mask, uint32_t mask_bits)
138 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
142 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
143 uint32_t *buffer, uint32_t *unused, uint32_t unused2)
145 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
148 /* Even thought the type of buffer is uint32_t *, the function actually expects
149 * a uint64_t *buffer.
153 _pixman_image_get_fetcher (pixman_image_t *image,
156 assert (image->common.get_scanline_64);
157 assert (image->common.get_scanline_32);
160 return image->common.get_scanline_64;
162 return image->common.get_scanline_32;
165 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
168 image_property_changed (pixman_image_t *image)
172 image->common.property_changed (image);
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 pixman_region32_fini (&common->clip_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 image->common.have_clip_region = FALSE;
239 PIXMAN_EXPORT pixman_bool_t
240 pixman_image_set_clip_region32 (pixman_image_t *image,
241 pixman_region32_t *region)
243 image_common_t *common = (image_common_t *)image;
244 pixman_bool_t result;
248 if ((result = pixman_region32_copy (&common->clip_region, region)))
249 image->common.have_clip_region = TRUE;
253 _pixman_image_reset_clip_region (image);
258 image_property_changed (image);
264 PIXMAN_EXPORT pixman_bool_t
265 pixman_image_set_clip_region (pixman_image_t *image,
266 pixman_region16_t *region)
268 image_common_t *common = (image_common_t *)image;
269 pixman_bool_t result;
273 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
274 image->common.have_clip_region = TRUE;
278 _pixman_image_reset_clip_region (image);
283 image_property_changed (image);
289 pixman_image_set_has_client_clip (pixman_image_t *image,
290 pixman_bool_t client_clip)
292 image->common.client_clip = client_clip;
295 PIXMAN_EXPORT pixman_bool_t
296 pixman_image_set_transform (pixman_image_t *image,
297 const pixman_transform_t *transform)
299 static const pixman_transform_t id =
301 { { pixman_fixed_1, 0, 0 },
302 { 0, pixman_fixed_1, 0 },
303 { 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;
321 if (common->transform == NULL)
322 common->transform = malloc (sizeof (pixman_transform_t));
324 if (common->transform == NULL)
330 memcpy(common->transform, transform, sizeof(pixman_transform_t));
333 image_property_changed (image);
339 pixman_image_set_repeat (pixman_image_t *image,
340 pixman_repeat_t repeat)
342 image->common.repeat = repeat;
344 image_property_changed (image);
347 PIXMAN_EXPORT pixman_bool_t
348 pixman_image_set_filter (pixman_image_t *image,
349 pixman_filter_t filter,
350 const pixman_fixed_t *params,
353 image_common_t *common = (image_common_t *)image;
354 pixman_fixed_t *new_params;
356 if (params == common->filter_params && filter == common->filter)
362 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
367 params, n_params * sizeof (pixman_fixed_t));
370 common->filter = filter;
372 if (common->filter_params)
373 free (common->filter_params);
375 common->filter_params = new_params;
376 common->n_filter_params = n_params;
378 image_property_changed (image);
383 pixman_image_set_source_clipping (pixman_image_t *image,
384 pixman_bool_t clip_sources)
386 image->common.clip_sources = clip_sources;
389 /* Unlike all the other property setters, this function does not
390 * copy the content of indexed. Doing this copying is simply
391 * way, way too expensive.
394 pixman_image_set_indexed (pixman_image_t *image,
395 const pixman_indexed_t *indexed)
397 bits_image_t *bits = (bits_image_t *)image;
399 bits->indexed = indexed;
401 image_property_changed (image);
405 pixman_image_set_alpha_map (pixman_image_t *image,
406 pixman_image_t *alpha_map,
410 image_common_t *common = (image_common_t *)image;
412 return_if_fail (!alpha_map || alpha_map->type == BITS);
414 if (common->alpha_map != (bits_image_t *)alpha_map)
416 if (common->alpha_map)
417 pixman_image_unref ((pixman_image_t *)common->alpha_map);
420 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
422 common->alpha_map = NULL;
425 common->alpha_origin.x = x;
426 common->alpha_origin.y = y;
428 image_property_changed (image);
432 pixman_image_set_component_alpha (pixman_image_t *image,
433 pixman_bool_t component_alpha)
435 image->common.component_alpha = component_alpha;
437 image_property_changed (image);
442 pixman_image_set_accessors (pixman_image_t *image,
443 pixman_read_memory_func_t read_func,
444 pixman_write_memory_func_t write_func)
446 return_if_fail (image != NULL);
448 image->common.read_func = read_func;
449 image->common.write_func = write_func;
451 image_property_changed (image);
454 PIXMAN_EXPORT uint32_t *
455 pixman_image_get_data (pixman_image_t *image)
457 if (image->type == BITS)
458 return image->bits.bits;
464 pixman_image_get_width (pixman_image_t *image)
466 if (image->type == BITS)
467 return image->bits.width;
473 pixman_image_get_height (pixman_image_t *image)
475 if (image->type == BITS)
476 return image->bits.height;
482 pixman_image_get_stride (pixman_image_t *image)
484 if (image->type == BITS)
485 return image->bits.rowstride * (int) sizeof (uint32_t);
491 pixman_image_get_depth (pixman_image_t *image)
493 if (image->type == BITS)
494 return PIXMAN_FORMAT_DEPTH (image->bits.format);
500 color_to_uint32 (const pixman_color_t *color)
503 (color->alpha >> 8 << 24) |
504 (color->red >> 8 << 16) |
505 (color->green & 0xff00) |
510 color_to_pixel (pixman_color_t *color,
512 pixman_format_code_t format)
514 uint32_t c = color_to_uint32 (color);
516 if (!(format == PIXMAN_a8r8g8b8 ||
517 format == PIXMAN_x8r8g8b8 ||
518 format == PIXMAN_a8b8g8r8 ||
519 format == PIXMAN_x8b8g8r8 ||
520 format == PIXMAN_b8g8r8a8 ||
521 format == PIXMAN_b8g8r8x8 ||
522 format == PIXMAN_r5g6b5 ||
523 format == PIXMAN_b5g6r5 ||
524 format == PIXMAN_a8))
529 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
531 c = ((c & 0xff000000) >> 0) |
532 ((c & 0x00ff0000) >> 16) |
533 ((c & 0x0000ff00) >> 0) |
534 ((c & 0x000000ff) << 16);
536 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
538 c = ((c & 0xff000000) >> 24) |
539 ((c & 0x00ff0000) >> 8) |
540 ((c & 0x0000ff00) << 8) |
541 ((c & 0x000000ff) << 24);
544 if (format == PIXMAN_a8)
546 else if (format == PIXMAN_r5g6b5 ||
547 format == PIXMAN_b5g6r5)
548 c = cvt8888to0565 (c);
551 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
552 printf ("pixel: %x\n", c);
559 PIXMAN_EXPORT pixman_bool_t
560 pixman_image_fill_rectangles (pixman_op_t op,
561 pixman_image_t *dest,
562 pixman_color_t *color,
564 const pixman_rectangle16_t *rects)
566 pixman_image_t *solid;
570 if (color->alpha == 0xffff)
572 if (op == PIXMAN_OP_OVER)
576 if (op == PIXMAN_OP_CLEAR)
588 if (op == PIXMAN_OP_SRC)
592 if (color_to_pixel (color, &pixel, dest->bits.format))
594 for (i = 0; i < n_rects; ++i)
596 pixman_region32_t fill_region;
598 pixman_box32_t *boxes;
600 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
602 if (dest->common.have_clip_region)
604 if (!pixman_region32_intersect (&fill_region,
606 &dest->common.clip_region))
610 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
611 for (j = 0; j < n_boxes; ++j)
613 const pixman_box32_t *box = &(boxes[j]);
614 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
615 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
619 pixman_region32_fini (&fill_region);
625 solid = pixman_image_create_solid_fill (color);
629 for (i = 0; i < n_rects; ++i)
631 const pixman_rectangle16_t *rect = &(rects[i]);
633 pixman_image_composite (op, solid, NULL, dest,
636 rect->width, rect->height);
639 pixman_image_unref (solid);
645 pixman_image_can_get_solid (pixman_image_t *image)
647 if (image->type == SOLID)
650 if (image->type != BITS ||
651 image->bits.width != 1 ||
652 image->bits.height != 1)
657 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
660 switch (image->bits.format)
662 case PIXMAN_a8r8g8b8:
663 case PIXMAN_x8r8g8b8:
664 case PIXMAN_a8b8g8r8:
665 case PIXMAN_x8b8g8r8:
666 case PIXMAN_b8g8r8a8:
667 case PIXMAN_b8g8r8x8:
679 pixman_image_is_opaque (pixman_image_t *image)
683 if (image->common.alpha_map)
689 if (image->common.repeat == PIXMAN_REPEAT_NONE)
692 if (PIXMAN_FORMAT_A (image->bits.format))
698 if (image->common.repeat == PIXMAN_REPEAT_NONE)
701 for (i = 0; i < image->gradient.n_stops; ++i)
703 if (image->gradient.stops[i].color.alpha != 0xffff)
709 /* Conical gradients always have a transparent border */
714 if (Alpha (image->solid.color) != 0xff)
719 /* Convolution filters can introduce translucency if the sum of the
720 * weights is lower than 1.
722 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)