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;
172 _pixman_image_classify (pixman_image_t *image,
178 if (image->common.classify)
179 return image->common.classify (image, x, y, width, height);
181 return SOURCE_IMAGE_CLASS_NEITHER;
185 PIXMAN_EXPORT pixman_image_t *
186 pixman_image_ref (pixman_image_t *image)
188 image->common.ref_count++;
193 /* returns TRUE when the image is freed */
194 PIXMAN_EXPORT pixman_bool_t
195 pixman_image_unref (pixman_image_t *image)
197 image_common_t *common = (image_common_t *)image;
201 if (common->ref_count == 0)
203 pixman_region32_fini (&common->clip_region);
204 pixman_region32_fini (&common->full_region);
206 if (common->transform)
207 free (common->transform);
209 if (common->filter_params)
210 free (common->filter_params);
212 if (common->alpha_map)
213 pixman_image_unref ((pixman_image_t *)common->alpha_map);
216 if (image->type == BITS && image->bits.indexed)
217 free (image->bits.indexed);
221 memset (image, 0xaa, sizeof (pixman_image_t));
223 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
225 if (image->gradient.stops)
226 free (image->gradient.stops);
230 if (image->type == BITS && image->bits.free_me)
231 free (image->bits.free_me);
242 PIXMAN_EXPORT pixman_image_t *
243 pixman_image_create_solid_fill (pixman_color_t *color)
245 pixman_image_t *img = allocate_image();
249 init_source_image (&img->solid.common);
252 img->solid.color = color_to_uint32 (color);
257 PIXMAN_EXPORT pixman_image_t *
258 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
259 pixman_point_fixed_t *p2,
260 const pixman_gradient_stop_t *stops,
263 pixman_image_t *image;
264 linear_gradient_t *linear;
266 return_val_if_fail (n_stops >= 2, NULL);
268 image = allocate_image();
273 linear = &image->linear;
275 if (!init_gradient (&linear->common, stops, n_stops))
284 image->type = LINEAR;
290 PIXMAN_EXPORT pixman_image_t *
291 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
292 pixman_point_fixed_t *outer,
293 pixman_fixed_t inner_radius,
294 pixman_fixed_t outer_radius,
295 const pixman_gradient_stop_t *stops,
298 pixman_image_t *image;
299 radial_gradient_t *radial;
301 return_val_if_fail (n_stops >= 2, NULL);
303 image = allocate_image();
308 radial = &image->radial;
310 if (!init_gradient (&radial->common, stops, n_stops))
316 image->type = RADIAL;
318 radial->c1.x = inner->x;
319 radial->c1.y = inner->y;
320 radial->c1.radius = inner_radius;
321 radial->c2.x = outer->x;
322 radial->c2.y = outer->y;
323 radial->c2.radius = outer_radius;
324 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
325 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
326 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
327 radial->A = (radial->cdx * radial->cdx
328 + radial->cdy * radial->cdy
329 - radial->dr * radial->dr);
334 PIXMAN_EXPORT pixman_image_t *
335 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
336 pixman_fixed_t angle,
337 const pixman_gradient_stop_t *stops,
340 pixman_image_t *image = allocate_image();
341 conical_gradient_t *conical;
346 conical = &image->conical;
348 if (!init_gradient (&conical->common, stops, n_stops))
354 image->type = CONICAL;
355 conical->center = *center;
356 conical->angle = angle;
362 create_bits (pixman_format_code_t format,
365 int *rowstride_bytes)
371 /* what follows is a long-winded way, avoiding any possibility of integer
372 * overflows, of saying:
373 * stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
376 bpp = PIXMAN_FORMAT_BPP (format);
377 if (pixman_multiply_overflows_int (width, bpp))
380 stride = width * bpp;
381 if (pixman_addition_overflows_int (stride, FB_MASK))
388 if (pixman_multiply_overflows_int (stride, sizeof (uint32_t)))
391 stride *= sizeof (uint32_t);
393 if (pixman_multiply_overflows_int (height, stride))
396 buf_size = height * stride;
399 *rowstride_bytes = stride;
401 return calloc (buf_size, 1);
405 reset_clip_region (pixman_image_t *image)
407 pixman_region32_fini (&image->common.clip_region);
409 if (image->type == BITS)
411 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
412 image->bits.width, image->bits.height);
416 pixman_region32_init (&image->common.clip_region);
420 PIXMAN_EXPORT pixman_image_t *
421 pixman_image_create_bits (pixman_format_code_t format,
427 pixman_image_t *image;
428 uint32_t *free_me = NULL;
430 /* must be a whole number of uint32_t's
432 return_val_if_fail (bits == NULL ||
433 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
435 if (!bits && width && height)
437 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
442 image = allocate_image();
451 image->bits.format = format;
452 image->bits.width = width;
453 image->bits.height = height;
454 image->bits.bits = bits;
455 image->bits.free_me = free_me;
457 image->bits.rowstride = rowstride_bytes / (int) sizeof (uint32_t); /* we store it in number
460 image->bits.indexed = NULL;
462 pixman_region32_fini (&image->common.full_region);
463 pixman_region32_init_rect (&image->common.full_region, 0, 0,
464 image->bits.width, image->bits.height);
466 reset_clip_region (image);
470 PIXMAN_EXPORT pixman_bool_t
471 pixman_image_set_clip_region32 (pixman_image_t *image,
472 pixman_region32_t *region)
474 image_common_t *common = (image_common_t *)image;
478 return pixman_region32_copy (&common->clip_region, region);
482 reset_clip_region (image);
489 PIXMAN_EXPORT pixman_bool_t
490 pixman_image_set_clip_region (pixman_image_t *image,
491 pixman_region16_t *region)
493 image_common_t *common = (image_common_t *)image;
497 return pixman_region32_copy_from_region16 (&common->clip_region, region);
501 reset_clip_region (image);
507 /* Sets whether the clip region includes a clip region set by the client
510 pixman_image_set_has_client_clip (pixman_image_t *image,
511 pixman_bool_t client_clip)
513 image->common.has_client_clip = client_clip;
516 PIXMAN_EXPORT pixman_bool_t
517 pixman_image_set_transform (pixman_image_t *image,
518 const pixman_transform_t *transform)
520 static const pixman_transform_t id =
522 { { pixman_fixed_1, 0, 0 },
523 { 0, pixman_fixed_1, 0 },
524 { 0, 0, pixman_fixed_1 }
528 image_common_t *common = (image_common_t *)image;
530 if (common->transform == transform)
533 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
535 free(common->transform);
536 common->transform = NULL;
540 if (common->transform == NULL)
541 common->transform = malloc (sizeof (pixman_transform_t));
542 if (common->transform == NULL)
545 memcpy(common->transform, transform, sizeof(pixman_transform_t));
551 pixman_image_set_repeat (pixman_image_t *image,
552 pixman_repeat_t repeat)
554 image->common.repeat = repeat;
557 PIXMAN_EXPORT pixman_bool_t
558 pixman_image_set_filter (pixman_image_t *image,
559 pixman_filter_t filter,
560 const pixman_fixed_t *params,
563 image_common_t *common = (image_common_t *)image;
564 pixman_fixed_t *new_params;
566 if (params == common->filter_params && filter == common->filter)
572 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
577 params, n_params * sizeof (pixman_fixed_t));
580 common->filter = filter;
582 if (common->filter_params)
583 free (common->filter_params);
585 common->filter_params = new_params;
586 common->n_filter_params = n_params;
591 pixman_image_set_source_clipping (pixman_image_t *image,
592 pixman_bool_t source_clipping)
594 image_common_t *common = &image->common;
597 common->src_clip = &common->clip_region;
599 common->src_clip = &common->full_region;
602 /* Unlike all the other property setters, this function does not
603 * copy the content of indexed. Doing this copying is simply
604 * way, way too expensive.
607 pixman_image_set_indexed (pixman_image_t *image,
608 const pixman_indexed_t *indexed)
610 bits_image_t *bits = (bits_image_t *)image;
612 bits->indexed = indexed;
616 pixman_image_set_alpha_map (pixman_image_t *image,
617 pixman_image_t *alpha_map,
621 image_common_t *common = (image_common_t *)image;
623 return_if_fail (!alpha_map || alpha_map->type == BITS);
625 if (common->alpha_map != (bits_image_t *)alpha_map)
627 if (common->alpha_map)
628 pixman_image_unref ((pixman_image_t *)common->alpha_map);
631 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
633 common->alpha_map = NULL;
636 common->alpha_origin.x = x;
637 common->alpha_origin.y = y;
641 pixman_image_set_component_alpha (pixman_image_t *image,
642 pixman_bool_t component_alpha)
644 image->common.component_alpha = component_alpha;
649 pixman_image_set_accessors (pixman_image_t *image,
650 pixman_read_memory_func_t read_func,
651 pixman_write_memory_func_t write_func)
653 return_if_fail (image != NULL);
655 image->common.read_func = read_func;
656 image->common.write_func = write_func;
659 PIXMAN_EXPORT uint32_t *
660 pixman_image_get_data (pixman_image_t *image)
662 if (image->type == BITS)
663 return image->bits.bits;
669 pixman_image_get_width (pixman_image_t *image)
671 if (image->type == BITS)
672 return image->bits.width;
678 pixman_image_get_height (pixman_image_t *image)
680 if (image->type == BITS)
681 return image->bits.height;
687 pixman_image_get_stride (pixman_image_t *image)
689 if (image->type == BITS)
690 return image->bits.rowstride * (int) sizeof (uint32_t);
696 pixman_image_get_depth (pixman_image_t *image)
698 if (image->type == BITS)
699 return PIXMAN_FORMAT_DEPTH (image->bits.format);
705 color_to_pixel (pixman_color_t *color,
707 pixman_format_code_t format)
709 uint32_t c = color_to_uint32 (color);
711 if (!(format == PIXMAN_a8r8g8b8 ||
712 format == PIXMAN_x8r8g8b8 ||
713 format == PIXMAN_a8b8g8r8 ||
714 format == PIXMAN_x8b8g8r8 ||
715 format == PIXMAN_b8g8r8a8 ||
716 format == PIXMAN_b8g8r8x8 ||
717 format == PIXMAN_r5g6b5 ||
718 format == PIXMAN_b5g6r5 ||
719 format == PIXMAN_a8))
724 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
726 c = ((c & 0xff000000) >> 0) |
727 ((c & 0x00ff0000) >> 16) |
728 ((c & 0x0000ff00) >> 0) |
729 ((c & 0x000000ff) << 16);
731 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
733 c = ((c & 0xff000000) >> 24) |
734 ((c & 0x00ff0000) >> 8) |
735 ((c & 0x0000ff00) << 8) |
736 ((c & 0x000000ff) << 24);
739 if (format == PIXMAN_a8)
741 else if (format == PIXMAN_r5g6b5 ||
742 format == PIXMAN_b5g6r5)
743 c = cvt8888to0565 (c);
746 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
747 printf ("pixel: %x\n", c);
754 PIXMAN_EXPORT pixman_bool_t
755 pixman_image_fill_rectangles (pixman_op_t op,
756 pixman_image_t *dest,
757 pixman_color_t *color,
759 const pixman_rectangle16_t *rects)
761 pixman_image_t *solid;
765 if (color->alpha == 0xffff)
767 if (op == PIXMAN_OP_OVER)
771 if (op == PIXMAN_OP_CLEAR)
783 if (op == PIXMAN_OP_SRC)
787 if (color_to_pixel (color, &pixel, dest->bits.format))
789 for (i = 0; i < n_rects; ++i)
791 pixman_region32_t fill_region;
793 pixman_box32_t *boxes;
795 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
796 if (!pixman_region32_intersect (&fill_region,
798 &dest->common.clip_region))
802 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
803 for (j = 0; j < n_boxes; ++j)
805 const pixman_box32_t *box = &(boxes[j]);
806 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
807 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
811 pixman_region32_fini (&fill_region);
817 solid = pixman_image_create_solid_fill (color);
821 for (i = 0; i < n_rects; ++i)
823 const pixman_rectangle16_t *rect = &(rects[i]);
825 pixman_image_composite (op, solid, NULL, dest,
828 rect->width, rect->height);
831 pixman_image_unref (solid);
837 pixman_image_can_get_solid (pixman_image_t *image)
839 if (image->type == SOLID)
842 if (image->type != BITS ||
843 image->bits.width != 1 ||
844 image->bits.height != 1)
849 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
852 switch (image->bits.format)
854 case PIXMAN_a8r8g8b8:
855 case PIXMAN_x8r8g8b8:
856 case PIXMAN_a8b8g8r8:
857 case PIXMAN_x8b8g8r8:
858 case PIXMAN_b8g8r8a8:
859 case PIXMAN_b8g8r8x8:
871 pixman_image_is_opaque(pixman_image_t *image)
874 int gradientNumberOfColors = 0;
876 if(image->common.alpha_map)
882 if(PIXMAN_FORMAT_A(image->bits.format))
889 gradientNumberOfColors = image->gradient.n_stops;
891 while(i<gradientNumberOfColors)
893 if(image->gradient.stops[i].color.alpha != 0xffff)
900 if(Alpha(image->solid.color) != 0xff)
905 /* Convolution filters can introduce translucency if the sum of the weights
907 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
910 if (image->common.repeat == PIXMAN_REPEAT_NONE)
912 if (image->common.filter != PIXMAN_FILTER_NEAREST)
915 if (image->common.transform)
918 /* Gradients do not necessarily cover the entire compositing area */
919 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)