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 _pixman_image_allocate (void)
62 pixman_image_t *image = malloc (sizeof (pixman_image_t));
66 image_common_t *common = &image->common;
68 pixman_region32_init (&common->full_region);
69 pixman_region32_init (&common->clip_region);
70 common->src_clip = &common->full_region;
71 common->has_client_clip = FALSE;
72 common->transform = NULL;
73 common->repeat = PIXMAN_REPEAT_NONE;
74 common->filter = PIXMAN_FILTER_NEAREST;
75 common->filter_params = NULL;
76 common->n_filter_params = 0;
77 common->alpha_map = NULL;
78 common->component_alpha = FALSE;
79 common->ref_count = 1;
80 common->read_func = NULL;
81 common->write_func = NULL;
82 common->classify = NULL;
89 _pixman_image_classify (pixman_image_t *image,
95 if (image->common.classify)
96 return image->common.classify (image, x, y, width, height);
98 return SOURCE_IMAGE_CLASS_UNKNOWN;
102 _pixman_image_get_scanline_32 (pixman_image_t *image, int x, int y, int width,
103 uint32_t *buffer, uint32_t *mask, uint32_t mask_bits)
105 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
109 _pixman_image_get_scanline_64 (pixman_image_t *image, int x, int y, int width,
110 uint32_t *buffer, uint32_t *unused, uint32_t unused2)
112 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
115 /* Even thought the type of buffer is uint32_t *, the function actually expects
116 * a uint64_t *buffer.
120 _pixman_image_get_fetcher (pixman_image_t *image,
123 assert (image->common.get_scanline_64);
124 assert (image->common.get_scanline_32);
127 return image->common.get_scanline_64;
129 return image->common.get_scanline_32;
132 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
135 fbStore(bits_image_t * image, int x, int y, int width, uint32_t *buffer)
139 storeProc32 store = WRITE_ACCESS(pixman_storeProcForPicture32)(image);
140 const pixman_indexed_t * indexed = image->indexed;
143 stride = image->rowstride;
145 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
149 fbStore64(bits_image_t * image, int x, int y, int width, uint64_t *buffer)
153 storeProc64 store = WRITE_ACCESS(pixman_storeProcForPicture64)(image);
154 const pixman_indexed_t * indexed = image->indexed;
157 stride = image->rowstride;
159 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
163 _pixman_image_get_storer (pixman_image_t *image,
166 if (image->common.alpha_map)
169 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha64);
171 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha);
176 return (scanStoreProc)fbStore64;
178 return (scanStoreProc)fbStore;
183 image_property_changed (pixman_image_t *image)
187 image->common.property_changed (image);
191 PIXMAN_EXPORT pixman_image_t *
192 pixman_image_ref (pixman_image_t *image)
194 image->common.ref_count++;
199 /* returns TRUE when the image is freed */
200 PIXMAN_EXPORT pixman_bool_t
201 pixman_image_unref (pixman_image_t *image)
203 image_common_t *common = (image_common_t *)image;
207 if (common->ref_count == 0)
209 pixman_region32_fini (&common->clip_region);
210 pixman_region32_fini (&common->full_region);
212 if (common->transform)
213 free (common->transform);
215 if (common->filter_params)
216 free (common->filter_params);
218 if (common->alpha_map)
219 pixman_image_unref ((pixman_image_t *)common->alpha_map);
222 if (image->type == BITS && image->bits.indexed)
223 free (image->bits.indexed);
227 memset (image, 0xaa, sizeof (pixman_image_t));
229 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
231 if (image->gradient.stops)
232 free (image->gradient.stops);
236 if (image->type == BITS && image->bits.free_me)
237 free (image->bits.free_me);
250 _pixman_image_reset_clip_region (pixman_image_t *image)
252 pixman_region32_fini (&image->common.clip_region);
254 if (image->type == BITS)
256 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
257 image->bits.width, image->bits.height);
261 pixman_region32_init (&image->common.clip_region);
265 PIXMAN_EXPORT pixman_bool_t
266 pixman_image_set_clip_region32 (pixman_image_t *image,
267 pixman_region32_t *region)
269 image_common_t *common = (image_common_t *)image;
270 pixman_bool_t result;
274 result = pixman_region32_copy (&common->clip_region, region);
278 _pixman_image_reset_clip_region (image);
283 image_property_changed (image);
289 PIXMAN_EXPORT pixman_bool_t
290 pixman_image_set_clip_region (pixman_image_t *image,
291 pixman_region16_t *region)
293 image_common_t *common = (image_common_t *)image;
294 pixman_bool_t result;
298 result = pixman_region32_copy_from_region16 (&common->clip_region, region);
302 _pixman_image_reset_clip_region (image);
307 image_property_changed (image);
312 /* Sets whether the clip region includes a clip region set by the client
315 pixman_image_set_has_client_clip (pixman_image_t *image,
316 pixman_bool_t client_clip)
318 image->common.has_client_clip = client_clip;
320 image_property_changed (image);
323 PIXMAN_EXPORT pixman_bool_t
324 pixman_image_set_transform (pixman_image_t *image,
325 const pixman_transform_t *transform)
327 static const pixman_transform_t id =
329 { { pixman_fixed_1, 0, 0 },
330 { 0, pixman_fixed_1, 0 },
331 { 0, 0, pixman_fixed_1 }
335 image_common_t *common = (image_common_t *)image;
336 pixman_bool_t result;
338 if (common->transform == transform)
341 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
343 free(common->transform);
344 common->transform = NULL;
349 if (common->transform == NULL)
350 common->transform = malloc (sizeof (pixman_transform_t));
352 if (common->transform == NULL)
358 memcpy(common->transform, transform, sizeof(pixman_transform_t));
361 image_property_changed (image);
367 pixman_image_set_repeat (pixman_image_t *image,
368 pixman_repeat_t repeat)
370 image->common.repeat = repeat;
372 image_property_changed (image);
375 PIXMAN_EXPORT pixman_bool_t
376 pixman_image_set_filter (pixman_image_t *image,
377 pixman_filter_t filter,
378 const pixman_fixed_t *params,
381 image_common_t *common = (image_common_t *)image;
382 pixman_fixed_t *new_params;
384 if (params == common->filter_params && filter == common->filter)
390 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
395 params, n_params * sizeof (pixman_fixed_t));
398 common->filter = filter;
400 if (common->filter_params)
401 free (common->filter_params);
403 common->filter_params = new_params;
404 common->n_filter_params = n_params;
406 image_property_changed (image);
411 pixman_image_set_source_clipping (pixman_image_t *image,
412 pixman_bool_t source_clipping)
414 image_common_t *common = &image->common;
417 common->src_clip = &common->clip_region;
419 common->src_clip = &common->full_region;
421 image_property_changed (image);
424 /* Unlike all the other property setters, this function does not
425 * copy the content of indexed. Doing this copying is simply
426 * way, way too expensive.
429 pixman_image_set_indexed (pixman_image_t *image,
430 const pixman_indexed_t *indexed)
432 bits_image_t *bits = (bits_image_t *)image;
434 bits->indexed = indexed;
436 image_property_changed (image);
440 pixman_image_set_alpha_map (pixman_image_t *image,
441 pixman_image_t *alpha_map,
445 image_common_t *common = (image_common_t *)image;
447 return_if_fail (!alpha_map || alpha_map->type == BITS);
449 if (common->alpha_map != (bits_image_t *)alpha_map)
451 if (common->alpha_map)
452 pixman_image_unref ((pixman_image_t *)common->alpha_map);
455 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
457 common->alpha_map = NULL;
460 common->alpha_origin.x = x;
461 common->alpha_origin.y = y;
463 image_property_changed (image);
467 pixman_image_set_component_alpha (pixman_image_t *image,
468 pixman_bool_t component_alpha)
470 image->common.component_alpha = component_alpha;
472 image_property_changed (image);
477 pixman_image_set_accessors (pixman_image_t *image,
478 pixman_read_memory_func_t read_func,
479 pixman_write_memory_func_t write_func)
481 return_if_fail (image != NULL);
483 image->common.read_func = read_func;
484 image->common.write_func = write_func;
486 image_property_changed (image);
489 PIXMAN_EXPORT uint32_t *
490 pixman_image_get_data (pixman_image_t *image)
492 if (image->type == BITS)
493 return image->bits.bits;
499 pixman_image_get_width (pixman_image_t *image)
501 if (image->type == BITS)
502 return image->bits.width;
508 pixman_image_get_height (pixman_image_t *image)
510 if (image->type == BITS)
511 return image->bits.height;
517 pixman_image_get_stride (pixman_image_t *image)
519 if (image->type == BITS)
520 return image->bits.rowstride * (int) sizeof (uint32_t);
526 pixman_image_get_depth (pixman_image_t *image)
528 if (image->type == BITS)
529 return PIXMAN_FORMAT_DEPTH (image->bits.format);
535 color_to_uint32 (const pixman_color_t *color)
538 (color->alpha >> 8 << 24) |
539 (color->red >> 8 << 16) |
540 (color->green & 0xff00) |
545 color_to_pixel (pixman_color_t *color,
547 pixman_format_code_t format)
549 uint32_t c = color_to_uint32 (color);
551 if (!(format == PIXMAN_a8r8g8b8 ||
552 format == PIXMAN_x8r8g8b8 ||
553 format == PIXMAN_a8b8g8r8 ||
554 format == PIXMAN_x8b8g8r8 ||
555 format == PIXMAN_b8g8r8a8 ||
556 format == PIXMAN_b8g8r8x8 ||
557 format == PIXMAN_r5g6b5 ||
558 format == PIXMAN_b5g6r5 ||
559 format == PIXMAN_a8))
564 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
566 c = ((c & 0xff000000) >> 0) |
567 ((c & 0x00ff0000) >> 16) |
568 ((c & 0x0000ff00) >> 0) |
569 ((c & 0x000000ff) << 16);
571 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
573 c = ((c & 0xff000000) >> 24) |
574 ((c & 0x00ff0000) >> 8) |
575 ((c & 0x0000ff00) << 8) |
576 ((c & 0x000000ff) << 24);
579 if (format == PIXMAN_a8)
581 else if (format == PIXMAN_r5g6b5 ||
582 format == PIXMAN_b5g6r5)
583 c = cvt8888to0565 (c);
586 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
587 printf ("pixel: %x\n", c);
594 PIXMAN_EXPORT pixman_bool_t
595 pixman_image_fill_rectangles (pixman_op_t op,
596 pixman_image_t *dest,
597 pixman_color_t *color,
599 const pixman_rectangle16_t *rects)
601 pixman_image_t *solid;
605 if (color->alpha == 0xffff)
607 if (op == PIXMAN_OP_OVER)
611 if (op == PIXMAN_OP_CLEAR)
623 if (op == PIXMAN_OP_SRC)
627 if (color_to_pixel (color, &pixel, dest->bits.format))
629 for (i = 0; i < n_rects; ++i)
631 pixman_region32_t fill_region;
633 pixman_box32_t *boxes;
635 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
636 if (!pixman_region32_intersect (&fill_region,
638 &dest->common.clip_region))
642 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
643 for (j = 0; j < n_boxes; ++j)
645 const pixman_box32_t *box = &(boxes[j]);
646 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
647 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
651 pixman_region32_fini (&fill_region);
657 solid = pixman_image_create_solid_fill (color);
661 for (i = 0; i < n_rects; ++i)
663 const pixman_rectangle16_t *rect = &(rects[i]);
665 pixman_image_composite (op, solid, NULL, dest,
668 rect->width, rect->height);
671 pixman_image_unref (solid);
677 pixman_image_can_get_solid (pixman_image_t *image)
679 if (image->type == SOLID)
682 if (image->type != BITS ||
683 image->bits.width != 1 ||
684 image->bits.height != 1)
689 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
692 switch (image->bits.format)
694 case PIXMAN_a8r8g8b8:
695 case PIXMAN_x8r8g8b8:
696 case PIXMAN_a8b8g8r8:
697 case PIXMAN_x8b8g8r8:
698 case PIXMAN_b8g8r8a8:
699 case PIXMAN_b8g8r8x8:
711 pixman_image_is_opaque(pixman_image_t *image)
714 int gradientNumberOfColors = 0;
716 if(image->common.alpha_map)
722 if(PIXMAN_FORMAT_A(image->bits.format))
729 gradientNumberOfColors = image->gradient.n_stops;
731 while(i<gradientNumberOfColors)
733 if(image->gradient.stops[i].color.alpha != 0xffff)
740 if(Alpha(image->solid.color) != 0xff)
745 /* Convolution filters can introduce translucency if the sum of the weights
747 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
750 if (image->common.repeat == PIXMAN_REPEAT_NONE)
752 if (image->common.filter != PIXMAN_FILTER_NEAREST)
755 if (image->common.transform)
758 /* Gradients do not necessarily cover the entire compositing area */
759 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)