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 if (pict->class != SOURCE_IMAGE_CLASS_UNKNOWN)
47 pict->class = SOURCE_IMAGE_CLASS_NEITHER;
49 if (pict->common.type == SOLID)
51 pict->class = SOURCE_IMAGE_CLASS_HORIZONTAL;
53 else if (pict->common.type == LINEAR)
55 linear_gradient_t *linear = (linear_gradient_t *)pict;
57 pixman_fixed_32_32_t l;
58 pixman_fixed_48_16_t dx, dy, a, b, off;
59 pixman_fixed_48_16_t factors[4];
62 dx = linear->p2.x - linear->p1.x;
63 dy = linear->p2.y - linear->p1.y;
64 l = dx * dx + dy * dy;
75 off = (-a * linear->p1.x
76 -b * linear->p1.y) >> 16;
78 for (i = 0; i < 3; i++)
80 v.vector[0] = pixman_int_to_fixed ((i % 2) * (width - 1) + x);
81 v.vector[1] = pixman_int_to_fixed ((i / 2) * (height - 1) + y);
82 v.vector[2] = pixman_fixed_1;
84 if (pict->common.transform)
86 if (!pixman_transform_point_3d (pict->common.transform, &v))
87 return 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;
103 init_source_image (source_image_t *image)
105 image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
106 image->common.classify = SourcePictureClassify;
110 init_gradient (gradient_t *gradient,
111 const pixman_gradient_stop_t *stops,
114 return_val_if_fail (n_stops > 0, FALSE);
116 init_source_image (&gradient->common);
118 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
119 if (!gradient->stops)
122 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
124 gradient->n_stops = n_stops;
126 gradient->stop_range = 0xffff;
127 gradient->color_table = NULL;
128 gradient->color_table_size = 0;
134 color_to_uint32 (const pixman_color_t *color)
137 (color->alpha >> 8 << 24) |
138 (color->red >> 8 << 16) |
139 (color->green & 0xff00) |
143 static pixman_image_t *
144 allocate_image (void)
146 pixman_image_t *image = malloc (sizeof (pixman_image_t));
150 image_common_t *common = &image->common;
152 pixman_region32_init (&common->full_region);
153 pixman_region32_init (&common->clip_region);
154 common->src_clip = &common->full_region;
155 common->has_client_clip = FALSE;
156 common->transform = NULL;
157 common->repeat = PIXMAN_REPEAT_NONE;
158 common->filter = PIXMAN_FILTER_NEAREST;
159 common->filter_params = NULL;
160 common->n_filter_params = 0;
161 common->alpha_map = NULL;
162 common->component_alpha = FALSE;
163 common->ref_count = 1;
164 common->read_func = NULL;
165 common->write_func = NULL;
166 common->classify = NULL;
173 _pixman_image_classify (pixman_image_t *image,
179 if (image->common.classify)
180 return image->common.classify (image, x, y, width, height);
182 return SOURCE_IMAGE_CLASS_NEITHER;
186 PIXMAN_EXPORT pixman_image_t *
187 pixman_image_ref (pixman_image_t *image)
189 image->common.ref_count++;
194 /* returns TRUE when the image is freed */
195 PIXMAN_EXPORT pixman_bool_t
196 pixman_image_unref (pixman_image_t *image)
198 image_common_t *common = (image_common_t *)image;
202 if (common->ref_count == 0)
204 pixman_region32_fini (&common->clip_region);
205 pixman_region32_fini (&common->full_region);
207 if (common->transform)
208 free (common->transform);
210 if (common->filter_params)
211 free (common->filter_params);
213 if (common->alpha_map)
214 pixman_image_unref ((pixman_image_t *)common->alpha_map);
217 if (image->type == BITS && image->bits.indexed)
218 free (image->bits.indexed);
222 memset (image, 0xaa, sizeof (pixman_image_t));
224 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
226 if (image->gradient.stops)
227 free (image->gradient.stops);
231 if (image->type == BITS && image->bits.free_me)
232 free (image->bits.free_me);
243 PIXMAN_EXPORT pixman_image_t *
244 pixman_image_create_solid_fill (pixman_color_t *color)
246 pixman_image_t *img = allocate_image();
250 init_source_image (&img->solid.common);
253 img->solid.color = color_to_uint32 (color);
258 PIXMAN_EXPORT pixman_image_t *
259 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
260 pixman_point_fixed_t *p2,
261 const pixman_gradient_stop_t *stops,
264 pixman_image_t *image;
265 linear_gradient_t *linear;
267 return_val_if_fail (n_stops >= 2, NULL);
269 image = allocate_image();
274 linear = &image->linear;
276 if (!init_gradient (&linear->common, stops, n_stops))
285 image->type = LINEAR;
291 PIXMAN_EXPORT pixman_image_t *
292 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
293 pixman_point_fixed_t *outer,
294 pixman_fixed_t inner_radius,
295 pixman_fixed_t outer_radius,
296 const pixman_gradient_stop_t *stops,
299 pixman_image_t *image;
300 radial_gradient_t *radial;
302 return_val_if_fail (n_stops >= 2, NULL);
304 image = allocate_image();
309 radial = &image->radial;
311 if (!init_gradient (&radial->common, stops, n_stops))
317 image->type = RADIAL;
319 radial->c1.x = inner->x;
320 radial->c1.y = inner->y;
321 radial->c1.radius = inner_radius;
322 radial->c2.x = outer->x;
323 radial->c2.y = outer->y;
324 radial->c2.radius = outer_radius;
325 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
326 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
327 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
328 radial->A = (radial->cdx * radial->cdx
329 + radial->cdy * radial->cdy
330 - radial->dr * radial->dr);
335 PIXMAN_EXPORT pixman_image_t *
336 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
337 pixman_fixed_t angle,
338 const pixman_gradient_stop_t *stops,
341 pixman_image_t *image = allocate_image();
342 conical_gradient_t *conical;
347 conical = &image->conical;
349 if (!init_gradient (&conical->common, stops, n_stops))
355 image->type = CONICAL;
356 conical->center = *center;
357 conical->angle = angle;
363 create_bits (pixman_format_code_t format,
366 int *rowstride_bytes)
372 /* what follows is a long-winded way, avoiding any possibility of integer
373 * overflows, of saying:
374 * stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
377 bpp = PIXMAN_FORMAT_BPP (format);
378 if (pixman_multiply_overflows_int (width, bpp))
381 stride = width * bpp;
382 if (pixman_addition_overflows_int (stride, FB_MASK))
389 if (pixman_multiply_overflows_int (stride, sizeof (uint32_t)))
392 stride *= sizeof (uint32_t);
394 if (pixman_multiply_overflows_int (height, stride))
397 buf_size = height * stride;
400 *rowstride_bytes = stride;
402 return calloc (buf_size, 1);
406 reset_clip_region (pixman_image_t *image)
408 pixman_region32_fini (&image->common.clip_region);
410 if (image->type == BITS)
412 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
413 image->bits.width, image->bits.height);
417 pixman_region32_init (&image->common.clip_region);
421 PIXMAN_EXPORT pixman_image_t *
422 pixman_image_create_bits (pixman_format_code_t format,
428 pixman_image_t *image;
429 uint32_t *free_me = NULL;
431 /* must be a whole number of uint32_t's
433 return_val_if_fail (bits == NULL ||
434 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
436 if (!bits && width && height)
438 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
443 image = allocate_image();
452 image->bits.format = format;
453 image->bits.width = width;
454 image->bits.height = height;
455 image->bits.bits = bits;
456 image->bits.free_me = free_me;
458 image->bits.rowstride = rowstride_bytes / (int) sizeof (uint32_t); /* we store it in number
461 image->bits.indexed = NULL;
463 pixman_region32_fini (&image->common.full_region);
464 pixman_region32_init_rect (&image->common.full_region, 0, 0,
465 image->bits.width, image->bits.height);
467 reset_clip_region (image);
471 PIXMAN_EXPORT pixman_bool_t
472 pixman_image_set_clip_region32 (pixman_image_t *image,
473 pixman_region32_t *region)
475 image_common_t *common = (image_common_t *)image;
479 return pixman_region32_copy (&common->clip_region, region);
483 reset_clip_region (image);
490 PIXMAN_EXPORT pixman_bool_t
491 pixman_image_set_clip_region (pixman_image_t *image,
492 pixman_region16_t *region)
494 image_common_t *common = (image_common_t *)image;
498 return pixman_region32_copy_from_region16 (&common->clip_region, region);
502 reset_clip_region (image);
508 /* Sets whether the clip region includes a clip region set by the client
511 pixman_image_set_has_client_clip (pixman_image_t *image,
512 pixman_bool_t client_clip)
514 image->common.has_client_clip = client_clip;
517 PIXMAN_EXPORT pixman_bool_t
518 pixman_image_set_transform (pixman_image_t *image,
519 const pixman_transform_t *transform)
521 static const pixman_transform_t id =
523 { { pixman_fixed_1, 0, 0 },
524 { 0, pixman_fixed_1, 0 },
525 { 0, 0, pixman_fixed_1 }
529 image_common_t *common = (image_common_t *)image;
531 if (common->transform == transform)
534 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
536 free(common->transform);
537 common->transform = NULL;
541 if (common->transform == NULL)
542 common->transform = malloc (sizeof (pixman_transform_t));
543 if (common->transform == NULL)
546 memcpy(common->transform, transform, sizeof(pixman_transform_t));
552 pixman_image_set_repeat (pixman_image_t *image,
553 pixman_repeat_t repeat)
555 image->common.repeat = repeat;
558 PIXMAN_EXPORT pixman_bool_t
559 pixman_image_set_filter (pixman_image_t *image,
560 pixman_filter_t filter,
561 const pixman_fixed_t *params,
564 image_common_t *common = (image_common_t *)image;
565 pixman_fixed_t *new_params;
567 if (params == common->filter_params && filter == common->filter)
573 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
578 params, n_params * sizeof (pixman_fixed_t));
581 common->filter = filter;
583 if (common->filter_params)
584 free (common->filter_params);
586 common->filter_params = new_params;
587 common->n_filter_params = n_params;
592 pixman_image_set_source_clipping (pixman_image_t *image,
593 pixman_bool_t source_clipping)
595 image_common_t *common = &image->common;
598 common->src_clip = &common->clip_region;
600 common->src_clip = &common->full_region;
603 /* Unlike all the other property setters, this function does not
604 * copy the content of indexed. Doing this copying is simply
605 * way, way too expensive.
608 pixman_image_set_indexed (pixman_image_t *image,
609 const pixman_indexed_t *indexed)
611 bits_image_t *bits = (bits_image_t *)image;
613 bits->indexed = indexed;
617 pixman_image_set_alpha_map (pixman_image_t *image,
618 pixman_image_t *alpha_map,
622 image_common_t *common = (image_common_t *)image;
624 return_if_fail (!alpha_map || alpha_map->type == BITS);
626 if (common->alpha_map != (bits_image_t *)alpha_map)
628 if (common->alpha_map)
629 pixman_image_unref ((pixman_image_t *)common->alpha_map);
632 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
634 common->alpha_map = NULL;
637 common->alpha_origin.x = x;
638 common->alpha_origin.y = y;
642 pixman_image_set_component_alpha (pixman_image_t *image,
643 pixman_bool_t component_alpha)
645 image->common.component_alpha = component_alpha;
650 pixman_image_set_accessors (pixman_image_t *image,
651 pixman_read_memory_func_t read_func,
652 pixman_write_memory_func_t write_func)
654 return_if_fail (image != NULL);
656 image->common.read_func = read_func;
657 image->common.write_func = write_func;
660 PIXMAN_EXPORT uint32_t *
661 pixman_image_get_data (pixman_image_t *image)
663 if (image->type == BITS)
664 return image->bits.bits;
670 pixman_image_get_width (pixman_image_t *image)
672 if (image->type == BITS)
673 return image->bits.width;
679 pixman_image_get_height (pixman_image_t *image)
681 if (image->type == BITS)
682 return image->bits.height;
688 pixman_image_get_stride (pixman_image_t *image)
690 if (image->type == BITS)
691 return image->bits.rowstride * (int) sizeof (uint32_t);
697 pixman_image_get_depth (pixman_image_t *image)
699 if (image->type == BITS)
700 return PIXMAN_FORMAT_DEPTH (image->bits.format);
706 color_to_pixel (pixman_color_t *color,
708 pixman_format_code_t format)
710 uint32_t c = color_to_uint32 (color);
712 if (!(format == PIXMAN_a8r8g8b8 ||
713 format == PIXMAN_x8r8g8b8 ||
714 format == PIXMAN_a8b8g8r8 ||
715 format == PIXMAN_x8b8g8r8 ||
716 format == PIXMAN_b8g8r8a8 ||
717 format == PIXMAN_b8g8r8x8 ||
718 format == PIXMAN_r5g6b5 ||
719 format == PIXMAN_b5g6r5 ||
720 format == PIXMAN_a8))
725 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
727 c = ((c & 0xff000000) >> 0) |
728 ((c & 0x00ff0000) >> 16) |
729 ((c & 0x0000ff00) >> 0) |
730 ((c & 0x000000ff) << 16);
732 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
734 c = ((c & 0xff000000) >> 24) |
735 ((c & 0x00ff0000) >> 8) |
736 ((c & 0x0000ff00) << 8) |
737 ((c & 0x000000ff) << 24);
740 if (format == PIXMAN_a8)
742 else if (format == PIXMAN_r5g6b5 ||
743 format == PIXMAN_b5g6r5)
744 c = cvt8888to0565 (c);
747 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
748 printf ("pixel: %x\n", c);
755 PIXMAN_EXPORT pixman_bool_t
756 pixman_image_fill_rectangles (pixman_op_t op,
757 pixman_image_t *dest,
758 pixman_color_t *color,
760 const pixman_rectangle16_t *rects)
762 pixman_image_t *solid;
766 if (color->alpha == 0xffff)
768 if (op == PIXMAN_OP_OVER)
772 if (op == PIXMAN_OP_CLEAR)
784 if (op == PIXMAN_OP_SRC)
788 if (color_to_pixel (color, &pixel, dest->bits.format))
790 for (i = 0; i < n_rects; ++i)
792 pixman_region32_t fill_region;
794 pixman_box32_t *boxes;
796 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
797 if (!pixman_region32_intersect (&fill_region,
799 &dest->common.clip_region))
803 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
804 for (j = 0; j < n_boxes; ++j)
806 const pixman_box32_t *box = &(boxes[j]);
807 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
808 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
812 pixman_region32_fini (&fill_region);
818 solid = pixman_image_create_solid_fill (color);
822 for (i = 0; i < n_rects; ++i)
824 const pixman_rectangle16_t *rect = &(rects[i]);
826 pixman_image_composite (op, solid, NULL, dest,
829 rect->width, rect->height);
832 pixman_image_unref (solid);
838 pixman_image_can_get_solid (pixman_image_t *image)
840 if (image->type == SOLID)
843 if (image->type != BITS ||
844 image->bits.width != 1 ||
845 image->bits.height != 1)
850 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
853 switch (image->bits.format)
855 case PIXMAN_a8r8g8b8:
856 case PIXMAN_x8r8g8b8:
857 case PIXMAN_a8b8g8r8:
858 case PIXMAN_x8b8g8r8:
859 case PIXMAN_b8g8r8a8:
860 case PIXMAN_b8g8r8x8:
872 pixman_image_is_opaque(pixman_image_t *image)
875 int gradientNumberOfColors = 0;
877 if(image->common.alpha_map)
883 if(PIXMAN_FORMAT_A(image->bits.format))
890 gradientNumberOfColors = image->gradient.n_stops;
892 while(i<gradientNumberOfColors)
894 if(image->gradient.stops[i].color.alpha != 0xffff)
901 if(Alpha(image->solid.color) != 0xff)
906 /* Convolution filters can introduce translucency if the sum of the weights
908 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
911 if (image->common.repeat == PIXMAN_REPEAT_NONE)
913 if (image->common.filter != PIXMAN_FILTER_NEAREST)
916 if (image->common.transform)
919 /* Gradients do not necessarily cover the entire compositing area */
920 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)