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.
31 #include "pixman-private.h"
33 #define Alpha(x) ((x) >> 24)
35 static source_pict_class_t
36 SourcePictureClassify (pixman_image_t *image,
42 source_image_t *pict = &image->source;
44 pict->class = SOURCE_IMAGE_CLASS_UNKNOWN;
46 if (pict->common.type == SOLID)
48 pict->class = SOURCE_IMAGE_CLASS_HORIZONTAL;
50 else if (pict->common.type == LINEAR)
52 linear_gradient_t *linear = (linear_gradient_t *)pict;
54 pixman_fixed_32_32_t l;
55 pixman_fixed_48_16_t dx, dy, a, b, off;
56 pixman_fixed_48_16_t factors[4];
59 dx = linear->p2.x - linear->p1.x;
60 dy = linear->p2.y - linear->p1.y;
61 l = dx * dx + dy * dy;
72 off = (-a * linear->p1.x
73 -b * linear->p1.y) >> 16;
75 for (i = 0; i < 3; i++)
77 v.vector[0] = pixman_int_to_fixed ((i % 2) * (width - 1) + x);
78 v.vector[1] = pixman_int_to_fixed ((i / 2) * (height - 1) + y);
79 v.vector[2] = pixman_fixed_1;
81 if (pict->common.transform)
83 if (!pixman_transform_point_3d (pict->common.transform, &v))
85 pict->class = SOURCE_IMAGE_CLASS_UNKNOWN;
90 factors[i] = ((a * v.vector[0] + b * v.vector[1]) >> 16) + off;
93 if (factors[2] == factors[0])
94 pict->class = SOURCE_IMAGE_CLASS_HORIZONTAL;
95 else if (factors[1] == factors[0])
96 pict->class = SOURCE_IMAGE_CLASS_VERTICAL;
104 init_source_image (source_image_t *image)
106 image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
107 image->common.classify = SourcePictureClassify;
111 init_gradient (gradient_t *gradient,
112 const pixman_gradient_stop_t *stops,
115 return_val_if_fail (n_stops > 0, FALSE);
117 init_source_image (&gradient->common);
119 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
120 if (!gradient->stops)
123 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
125 gradient->n_stops = n_stops;
127 gradient->stop_range = 0xffff;
128 gradient->color_table = NULL;
129 gradient->color_table_size = 0;
135 color_to_uint32 (const pixman_color_t *color)
138 (color->alpha >> 8 << 24) |
139 (color->red >> 8 << 16) |
140 (color->green & 0xff00) |
144 static pixman_image_t *
145 allocate_image (void)
147 pixman_image_t *image = malloc (sizeof (pixman_image_t));
151 image_common_t *common = &image->common;
153 pixman_region32_init (&common->full_region);
154 pixman_region32_init (&common->clip_region);
155 common->src_clip = &common->full_region;
156 common->has_client_clip = FALSE;
157 common->transform = NULL;
158 common->repeat = PIXMAN_REPEAT_NONE;
159 common->filter = PIXMAN_FILTER_NEAREST;
160 common->filter_params = NULL;
161 common->n_filter_params = 0;
162 common->alpha_map = NULL;
163 common->component_alpha = FALSE;
164 common->ref_count = 1;
165 common->read_func = NULL;
166 common->write_func = NULL;
167 common->classify = NULL;
174 _pixman_image_classify (pixman_image_t *image,
180 if (image->common.classify)
181 return image->common.classify (image, x, y, width, height);
183 return SOURCE_IMAGE_CLASS_UNKNOWN;
187 PIXMAN_EXPORT pixman_image_t *
188 pixman_image_ref (pixman_image_t *image)
190 image->common.ref_count++;
195 /* returns TRUE when the image is freed */
196 PIXMAN_EXPORT pixman_bool_t
197 pixman_image_unref (pixman_image_t *image)
199 image_common_t *common = (image_common_t *)image;
203 if (common->ref_count == 0)
205 pixman_region32_fini (&common->clip_region);
206 pixman_region32_fini (&common->full_region);
208 if (common->transform)
209 free (common->transform);
211 if (common->filter_params)
212 free (common->filter_params);
214 if (common->alpha_map)
215 pixman_image_unref ((pixman_image_t *)common->alpha_map);
218 if (image->type == BITS && image->bits.indexed)
219 free (image->bits.indexed);
223 memset (image, 0xaa, sizeof (pixman_image_t));
225 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
227 if (image->gradient.stops)
228 free (image->gradient.stops);
232 if (image->type == BITS && image->bits.free_me)
233 free (image->bits.free_me);
244 PIXMAN_EXPORT pixman_image_t *
245 pixman_image_create_solid_fill (pixman_color_t *color)
247 pixman_image_t *img = allocate_image();
251 init_source_image (&img->solid.common);
254 img->solid.color = color_to_uint32 (color);
259 PIXMAN_EXPORT pixman_image_t *
260 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
261 pixman_point_fixed_t *p2,
262 const pixman_gradient_stop_t *stops,
265 pixman_image_t *image;
266 linear_gradient_t *linear;
268 return_val_if_fail (n_stops >= 2, NULL);
270 image = allocate_image();
275 linear = &image->linear;
277 if (!init_gradient (&linear->common, stops, n_stops))
286 image->type = LINEAR;
292 PIXMAN_EXPORT pixman_image_t *
293 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
294 pixman_point_fixed_t *outer,
295 pixman_fixed_t inner_radius,
296 pixman_fixed_t outer_radius,
297 const pixman_gradient_stop_t *stops,
300 pixman_image_t *image;
301 radial_gradient_t *radial;
303 return_val_if_fail (n_stops >= 2, NULL);
305 image = allocate_image();
310 radial = &image->radial;
312 if (!init_gradient (&radial->common, stops, n_stops))
318 image->type = RADIAL;
320 radial->c1.x = inner->x;
321 radial->c1.y = inner->y;
322 radial->c1.radius = inner_radius;
323 radial->c2.x = outer->x;
324 radial->c2.y = outer->y;
325 radial->c2.radius = outer_radius;
326 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
327 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
328 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
329 radial->A = (radial->cdx * radial->cdx
330 + radial->cdy * radial->cdy
331 - radial->dr * radial->dr);
336 PIXMAN_EXPORT pixman_image_t *
337 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
338 pixman_fixed_t angle,
339 const pixman_gradient_stop_t *stops,
342 pixman_image_t *image = allocate_image();
343 conical_gradient_t *conical;
348 conical = &image->conical;
350 if (!init_gradient (&conical->common, stops, n_stops))
356 image->type = CONICAL;
357 conical->center = *center;
358 conical->angle = angle;
364 create_bits (pixman_format_code_t format,
367 int *rowstride_bytes)
373 /* what follows is a long-winded way, avoiding any possibility of integer
374 * overflows, of saying:
375 * stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
378 bpp = PIXMAN_FORMAT_BPP (format);
379 if (pixman_multiply_overflows_int (width, bpp))
382 stride = width * bpp;
383 if (pixman_addition_overflows_int (stride, FB_MASK))
390 if (pixman_multiply_overflows_int (stride, sizeof (uint32_t)))
393 stride *= sizeof (uint32_t);
395 if (pixman_multiply_overflows_int (height, stride))
398 buf_size = height * stride;
401 *rowstride_bytes = stride;
403 return calloc (buf_size, 1);
407 reset_clip_region (pixman_image_t *image)
409 pixman_region32_fini (&image->common.clip_region);
411 if (image->type == BITS)
413 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
414 image->bits.width, image->bits.height);
418 pixman_region32_init (&image->common.clip_region);
422 PIXMAN_EXPORT pixman_image_t *
423 pixman_image_create_bits (pixman_format_code_t format,
429 pixman_image_t *image;
430 uint32_t *free_me = NULL;
432 /* must be a whole number of uint32_t's
434 return_val_if_fail (bits == NULL ||
435 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
437 if (!bits && width && height)
439 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
444 image = allocate_image();
453 image->bits.format = format;
454 image->bits.width = width;
455 image->bits.height = height;
456 image->bits.bits = bits;
457 image->bits.free_me = free_me;
459 image->bits.rowstride = rowstride_bytes / (int) sizeof (uint32_t); /* we store it in number
462 image->bits.indexed = NULL;
464 pixman_region32_fini (&image->common.full_region);
465 pixman_region32_init_rect (&image->common.full_region, 0, 0,
466 image->bits.width, image->bits.height);
468 reset_clip_region (image);
472 PIXMAN_EXPORT pixman_bool_t
473 pixman_image_set_clip_region32 (pixman_image_t *image,
474 pixman_region32_t *region)
476 image_common_t *common = (image_common_t *)image;
480 return pixman_region32_copy (&common->clip_region, region);
484 reset_clip_region (image);
491 PIXMAN_EXPORT pixman_bool_t
492 pixman_image_set_clip_region (pixman_image_t *image,
493 pixman_region16_t *region)
495 image_common_t *common = (image_common_t *)image;
499 return pixman_region32_copy_from_region16 (&common->clip_region, region);
503 reset_clip_region (image);
509 /* Sets whether the clip region includes a clip region set by the client
512 pixman_image_set_has_client_clip (pixman_image_t *image,
513 pixman_bool_t client_clip)
515 image->common.has_client_clip = client_clip;
518 PIXMAN_EXPORT pixman_bool_t
519 pixman_image_set_transform (pixman_image_t *image,
520 const pixman_transform_t *transform)
522 static const pixman_transform_t id =
524 { { pixman_fixed_1, 0, 0 },
525 { 0, pixman_fixed_1, 0 },
526 { 0, 0, pixman_fixed_1 }
530 image_common_t *common = (image_common_t *)image;
532 if (common->transform == transform)
535 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
537 free(common->transform);
538 common->transform = NULL;
542 if (common->transform == NULL)
543 common->transform = malloc (sizeof (pixman_transform_t));
544 if (common->transform == NULL)
547 memcpy(common->transform, transform, sizeof(pixman_transform_t));
553 pixman_image_set_repeat (pixman_image_t *image,
554 pixman_repeat_t repeat)
556 image->common.repeat = repeat;
559 PIXMAN_EXPORT pixman_bool_t
560 pixman_image_set_filter (pixman_image_t *image,
561 pixman_filter_t filter,
562 const pixman_fixed_t *params,
565 image_common_t *common = (image_common_t *)image;
566 pixman_fixed_t *new_params;
568 if (params == common->filter_params && filter == common->filter)
574 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
579 params, n_params * sizeof (pixman_fixed_t));
582 common->filter = filter;
584 if (common->filter_params)
585 free (common->filter_params);
587 common->filter_params = new_params;
588 common->n_filter_params = n_params;
593 pixman_image_set_source_clipping (pixman_image_t *image,
594 pixman_bool_t source_clipping)
596 image_common_t *common = &image->common;
599 common->src_clip = &common->clip_region;
601 common->src_clip = &common->full_region;
604 /* Unlike all the other property setters, this function does not
605 * copy the content of indexed. Doing this copying is simply
606 * way, way too expensive.
609 pixman_image_set_indexed (pixman_image_t *image,
610 const pixman_indexed_t *indexed)
612 bits_image_t *bits = (bits_image_t *)image;
614 bits->indexed = indexed;
618 pixman_image_set_alpha_map (pixman_image_t *image,
619 pixman_image_t *alpha_map,
623 image_common_t *common = (image_common_t *)image;
625 return_if_fail (!alpha_map || alpha_map->type == BITS);
627 if (common->alpha_map != (bits_image_t *)alpha_map)
629 if (common->alpha_map)
630 pixman_image_unref ((pixman_image_t *)common->alpha_map);
633 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
635 common->alpha_map = NULL;
638 common->alpha_origin.x = x;
639 common->alpha_origin.y = y;
643 pixman_image_set_component_alpha (pixman_image_t *image,
644 pixman_bool_t component_alpha)
646 image->common.component_alpha = component_alpha;
651 pixman_image_set_accessors (pixman_image_t *image,
652 pixman_read_memory_func_t read_func,
653 pixman_write_memory_func_t write_func)
655 return_if_fail (image != NULL);
657 image->common.read_func = read_func;
658 image->common.write_func = write_func;
661 PIXMAN_EXPORT uint32_t *
662 pixman_image_get_data (pixman_image_t *image)
664 if (image->type == BITS)
665 return image->bits.bits;
671 pixman_image_get_width (pixman_image_t *image)
673 if (image->type == BITS)
674 return image->bits.width;
680 pixman_image_get_height (pixman_image_t *image)
682 if (image->type == BITS)
683 return image->bits.height;
689 pixman_image_get_stride (pixman_image_t *image)
691 if (image->type == BITS)
692 return image->bits.rowstride * (int) sizeof (uint32_t);
698 pixman_image_get_depth (pixman_image_t *image)
700 if (image->type == BITS)
701 return PIXMAN_FORMAT_DEPTH (image->bits.format);
707 color_to_pixel (pixman_color_t *color,
709 pixman_format_code_t format)
711 uint32_t c = color_to_uint32 (color);
713 if (!(format == PIXMAN_a8r8g8b8 ||
714 format == PIXMAN_x8r8g8b8 ||
715 format == PIXMAN_a8b8g8r8 ||
716 format == PIXMAN_x8b8g8r8 ||
717 format == PIXMAN_b8g8r8a8 ||
718 format == PIXMAN_b8g8r8x8 ||
719 format == PIXMAN_r5g6b5 ||
720 format == PIXMAN_b5g6r5 ||
721 format == PIXMAN_a8))
726 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
728 c = ((c & 0xff000000) >> 0) |
729 ((c & 0x00ff0000) >> 16) |
730 ((c & 0x0000ff00) >> 0) |
731 ((c & 0x000000ff) << 16);
733 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
735 c = ((c & 0xff000000) >> 24) |
736 ((c & 0x00ff0000) >> 8) |
737 ((c & 0x0000ff00) << 8) |
738 ((c & 0x000000ff) << 24);
741 if (format == PIXMAN_a8)
743 else if (format == PIXMAN_r5g6b5 ||
744 format == PIXMAN_b5g6r5)
745 c = cvt8888to0565 (c);
748 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
749 printf ("pixel: %x\n", c);
756 PIXMAN_EXPORT pixman_bool_t
757 pixman_image_fill_rectangles (pixman_op_t op,
758 pixman_image_t *dest,
759 pixman_color_t *color,
761 const pixman_rectangle16_t *rects)
763 pixman_image_t *solid;
767 if (color->alpha == 0xffff)
769 if (op == PIXMAN_OP_OVER)
773 if (op == PIXMAN_OP_CLEAR)
785 if (op == PIXMAN_OP_SRC)
789 if (color_to_pixel (color, &pixel, dest->bits.format))
791 for (i = 0; i < n_rects; ++i)
793 pixman_region32_t fill_region;
795 pixman_box32_t *boxes;
797 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
798 if (!pixman_region32_intersect (&fill_region,
800 &dest->common.clip_region))
804 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
805 for (j = 0; j < n_boxes; ++j)
807 const pixman_box32_t *box = &(boxes[j]);
808 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
809 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
813 pixman_region32_fini (&fill_region);
819 solid = pixman_image_create_solid_fill (color);
823 for (i = 0; i < n_rects; ++i)
825 const pixman_rectangle16_t *rect = &(rects[i]);
827 pixman_image_composite (op, solid, NULL, dest,
830 rect->width, rect->height);
833 pixman_image_unref (solid);
839 pixman_image_can_get_solid (pixman_image_t *image)
841 if (image->type == SOLID)
844 if (image->type != BITS ||
845 image->bits.width != 1 ||
846 image->bits.height != 1)
851 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
854 switch (image->bits.format)
856 case PIXMAN_a8r8g8b8:
857 case PIXMAN_x8r8g8b8:
858 case PIXMAN_a8b8g8r8:
859 case PIXMAN_x8b8g8r8:
860 case PIXMAN_b8g8r8a8:
861 case PIXMAN_b8g8r8x8:
873 pixman_image_is_opaque(pixman_image_t *image)
876 int gradientNumberOfColors = 0;
878 if(image->common.alpha_map)
884 if(PIXMAN_FORMAT_A(image->bits.format))
891 gradientNumberOfColors = image->gradient.n_stops;
893 while(i<gradientNumberOfColors)
895 if(image->gradient.stops[i].color.alpha != 0xffff)
902 if(Alpha(image->solid.color) != 0xff)
907 /* Convolution filters can introduce translucency if the sum of the weights
909 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
912 if (image->common.repeat == PIXMAN_REPEAT_NONE)
914 if (image->common.filter != PIXMAN_FILTER_NEAREST)
917 if (image->common.transform)
920 /* Gradients do not necessarily cover the entire compositing area */
921 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)