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)
36 init_source_image (source_image_t *image)
38 image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
42 init_gradient (gradient_t *gradient,
43 const pixman_gradient_stop_t *stops,
46 return_val_if_fail (n_stops > 0, FALSE);
48 init_source_image (&gradient->common);
50 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
54 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
56 gradient->n_stops = n_stops;
58 gradient->stop_range = 0xffff;
59 gradient->color_table = NULL;
60 gradient->color_table_size = 0;
66 color_to_uint32 (const pixman_color_t *color)
69 (color->alpha >> 8 << 24) |
70 (color->red >> 8 << 16) |
71 (color->green & 0xff00) |
75 static pixman_image_t *
78 pixman_image_t *image = malloc (sizeof (pixman_image_t));
82 image_common_t *common = &image->common;
84 pixman_region32_init (&common->full_region);
85 pixman_region32_init (&common->clip_region);
86 common->src_clip = &common->full_region;
87 common->has_client_clip = FALSE;
88 common->transform = NULL;
89 common->repeat = PIXMAN_REPEAT_NONE;
90 common->filter = PIXMAN_FILTER_NEAREST;
91 common->filter_params = NULL;
92 common->n_filter_params = 0;
93 common->alpha_map = NULL;
94 common->component_alpha = FALSE;
95 common->ref_count = 1;
96 common->read_func = NULL;
97 common->write_func = NULL;
104 PIXMAN_EXPORT pixman_image_t *
105 pixman_image_ref (pixman_image_t *image)
107 image->common.ref_count++;
112 /* returns TRUE when the image is freed */
113 PIXMAN_EXPORT pixman_bool_t
114 pixman_image_unref (pixman_image_t *image)
116 image_common_t *common = (image_common_t *)image;
120 if (common->ref_count == 0)
122 pixman_region32_fini (&common->clip_region);
123 pixman_region32_fini (&common->full_region);
125 if (common->transform)
126 free (common->transform);
128 if (common->filter_params)
129 free (common->filter_params);
131 if (common->alpha_map)
132 pixman_image_unref ((pixman_image_t *)common->alpha_map);
135 if (image->type == BITS && image->bits.indexed)
136 free (image->bits.indexed);
140 memset (image, 0xaa, sizeof (pixman_image_t));
142 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
144 if (image->gradient.stops)
145 free (image->gradient.stops);
149 if (image->type == BITS && image->bits.free_me)
150 free (image->bits.free_me);
161 PIXMAN_EXPORT pixman_image_t *
162 pixman_image_create_solid_fill (pixman_color_t *color)
164 pixman_image_t *img = allocate_image();
168 init_source_image (&img->solid.common);
171 img->solid.color = color_to_uint32 (color);
176 PIXMAN_EXPORT pixman_image_t *
177 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
178 pixman_point_fixed_t *p2,
179 const pixman_gradient_stop_t *stops,
182 pixman_image_t *image;
183 linear_gradient_t *linear;
185 return_val_if_fail (n_stops >= 2, NULL);
187 image = allocate_image();
192 linear = &image->linear;
194 if (!init_gradient (&linear->common, stops, n_stops))
203 image->type = LINEAR;
209 PIXMAN_EXPORT pixman_image_t *
210 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
211 pixman_point_fixed_t *outer,
212 pixman_fixed_t inner_radius,
213 pixman_fixed_t outer_radius,
214 const pixman_gradient_stop_t *stops,
217 pixman_image_t *image;
218 radial_gradient_t *radial;
220 return_val_if_fail (n_stops >= 2, NULL);
222 image = allocate_image();
227 radial = &image->radial;
229 if (!init_gradient (&radial->common, stops, n_stops))
235 image->type = RADIAL;
237 radial->c1.x = inner->x;
238 radial->c1.y = inner->y;
239 radial->c1.radius = inner_radius;
240 radial->c2.x = outer->x;
241 radial->c2.y = outer->y;
242 radial->c2.radius = outer_radius;
243 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
244 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
245 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
246 radial->A = (radial->cdx * radial->cdx
247 + radial->cdy * radial->cdy
248 - radial->dr * radial->dr);
253 PIXMAN_EXPORT pixman_image_t *
254 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
255 pixman_fixed_t angle,
256 const pixman_gradient_stop_t *stops,
259 pixman_image_t *image = allocate_image();
260 conical_gradient_t *conical;
265 conical = &image->conical;
267 if (!init_gradient (&conical->common, stops, n_stops))
273 image->type = CONICAL;
274 conical->center = *center;
275 conical->angle = angle;
281 create_bits (pixman_format_code_t format,
284 int *rowstride_bytes)
290 /* what follows is a long-winded way, avoiding any possibility of integer
291 * overflows, of saying:
292 * stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
295 bpp = PIXMAN_FORMAT_BPP (format);
296 if (pixman_multiply_overflows_int (width, bpp))
299 stride = width * bpp;
300 if (pixman_addition_overflows_int (stride, FB_MASK))
307 if (pixman_multiply_overflows_int (stride, sizeof (uint32_t)))
310 stride *= sizeof (uint32_t);
312 if (pixman_multiply_overflows_int (height, stride))
315 buf_size = height * stride;
318 *rowstride_bytes = stride;
320 return calloc (buf_size, 1);
324 reset_clip_region (pixman_image_t *image)
326 pixman_region32_fini (&image->common.clip_region);
328 if (image->type == BITS)
330 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
331 image->bits.width, image->bits.height);
335 pixman_region32_init (&image->common.clip_region);
339 PIXMAN_EXPORT pixman_image_t *
340 pixman_image_create_bits (pixman_format_code_t format,
346 pixman_image_t *image;
347 uint32_t *free_me = NULL;
349 /* must be a whole number of uint32_t's
351 return_val_if_fail (bits == NULL ||
352 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
354 if (!bits && width && height)
356 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
361 image = allocate_image();
370 image->bits.format = format;
371 image->bits.width = width;
372 image->bits.height = height;
373 image->bits.bits = bits;
374 image->bits.free_me = free_me;
376 image->bits.rowstride = rowstride_bytes / (int) sizeof (uint32_t); /* we store it in number
379 image->bits.indexed = NULL;
381 pixman_region32_fini (&image->common.full_region);
382 pixman_region32_init_rect (&image->common.full_region, 0, 0,
383 image->bits.width, image->bits.height);
385 reset_clip_region (image);
389 PIXMAN_EXPORT pixman_bool_t
390 pixman_image_set_clip_region32 (pixman_image_t *image,
391 pixman_region32_t *region)
393 image_common_t *common = (image_common_t *)image;
397 return pixman_region32_copy (&common->clip_region, region);
401 reset_clip_region (image);
408 PIXMAN_EXPORT pixman_bool_t
409 pixman_image_set_clip_region (pixman_image_t *image,
410 pixman_region16_t *region)
412 image_common_t *common = (image_common_t *)image;
416 return pixman_region32_copy_from_region16 (&common->clip_region, region);
420 reset_clip_region (image);
426 /* Sets whether the clip region includes a clip region set by the client
429 pixman_image_set_has_client_clip (pixman_image_t *image,
430 pixman_bool_t client_clip)
432 image->common.has_client_clip = client_clip;
435 PIXMAN_EXPORT pixman_bool_t
436 pixman_image_set_transform (pixman_image_t *image,
437 const pixman_transform_t *transform)
439 static const pixman_transform_t id =
441 { { pixman_fixed_1, 0, 0 },
442 { 0, pixman_fixed_1, 0 },
443 { 0, 0, pixman_fixed_1 }
447 image_common_t *common = (image_common_t *)image;
449 if (common->transform == transform)
452 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
454 free(common->transform);
455 common->transform = NULL;
459 if (common->transform == NULL)
460 common->transform = malloc (sizeof (pixman_transform_t));
461 if (common->transform == NULL)
464 memcpy(common->transform, transform, sizeof(pixman_transform_t));
470 pixman_image_set_repeat (pixman_image_t *image,
471 pixman_repeat_t repeat)
473 image->common.repeat = repeat;
476 PIXMAN_EXPORT pixman_bool_t
477 pixman_image_set_filter (pixman_image_t *image,
478 pixman_filter_t filter,
479 const pixman_fixed_t *params,
482 image_common_t *common = (image_common_t *)image;
483 pixman_fixed_t *new_params;
485 if (params == common->filter_params && filter == common->filter)
491 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
496 params, n_params * sizeof (pixman_fixed_t));
499 common->filter = filter;
501 if (common->filter_params)
502 free (common->filter_params);
504 common->filter_params = new_params;
505 common->n_filter_params = n_params;
510 pixman_image_set_source_clipping (pixman_image_t *image,
511 pixman_bool_t source_clipping)
513 image_common_t *common = &image->common;
516 common->src_clip = &common->clip_region;
518 common->src_clip = &common->full_region;
521 /* Unlike all the other property setters, this function does not
522 * copy the content of indexed. Doing this copying is simply
523 * way, way too expensive.
526 pixman_image_set_indexed (pixman_image_t *image,
527 const pixman_indexed_t *indexed)
529 bits_image_t *bits = (bits_image_t *)image;
531 bits->indexed = indexed;
535 pixman_image_set_alpha_map (pixman_image_t *image,
536 pixman_image_t *alpha_map,
540 image_common_t *common = (image_common_t *)image;
542 return_if_fail (!alpha_map || alpha_map->type == BITS);
544 if (common->alpha_map != (bits_image_t *)alpha_map)
546 if (common->alpha_map)
547 pixman_image_unref ((pixman_image_t *)common->alpha_map);
550 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
552 common->alpha_map = NULL;
555 common->alpha_origin.x = x;
556 common->alpha_origin.y = y;
560 pixman_image_set_component_alpha (pixman_image_t *image,
561 pixman_bool_t component_alpha)
563 image->common.component_alpha = component_alpha;
568 pixman_image_set_accessors (pixman_image_t *image,
569 pixman_read_memory_func_t read_func,
570 pixman_write_memory_func_t write_func)
572 return_if_fail (image != NULL);
574 image->common.read_func = read_func;
575 image->common.write_func = write_func;
578 PIXMAN_EXPORT uint32_t *
579 pixman_image_get_data (pixman_image_t *image)
581 if (image->type == BITS)
582 return image->bits.bits;
588 pixman_image_get_width (pixman_image_t *image)
590 if (image->type == BITS)
591 return image->bits.width;
597 pixman_image_get_height (pixman_image_t *image)
599 if (image->type == BITS)
600 return image->bits.height;
606 pixman_image_get_stride (pixman_image_t *image)
608 if (image->type == BITS)
609 return image->bits.rowstride * (int) sizeof (uint32_t);
615 pixman_image_get_depth (pixman_image_t *image)
617 if (image->type == BITS)
618 return PIXMAN_FORMAT_DEPTH (image->bits.format);
624 color_to_pixel (pixman_color_t *color,
626 pixman_format_code_t format)
628 uint32_t c = color_to_uint32 (color);
630 if (!(format == PIXMAN_a8r8g8b8 ||
631 format == PIXMAN_x8r8g8b8 ||
632 format == PIXMAN_a8b8g8r8 ||
633 format == PIXMAN_x8b8g8r8 ||
634 format == PIXMAN_r5g6b5 ||
635 format == PIXMAN_b5g6r5 ||
636 format == PIXMAN_a8))
641 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
643 c = ((c & 0xff000000) >> 0) |
644 ((c & 0x00ff0000) >> 16) |
645 ((c & 0x0000ff00) >> 0) |
646 ((c & 0x000000ff) << 16);
649 if (format == PIXMAN_a8)
651 else if (format == PIXMAN_r5g6b5 ||
652 format == PIXMAN_b5g6r5)
653 c = cvt8888to0565 (c);
656 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
657 printf ("pixel: %x\n", c);
664 PIXMAN_EXPORT pixman_bool_t
665 pixman_image_fill_rectangles (pixman_op_t op,
666 pixman_image_t *dest,
667 pixman_color_t *color,
669 const pixman_rectangle16_t *rects)
671 pixman_image_t *solid;
675 if (color->alpha == 0xffff)
677 if (op == PIXMAN_OP_OVER)
681 if (op == PIXMAN_OP_CLEAR)
693 if (op == PIXMAN_OP_SRC)
697 if (color_to_pixel (color, &pixel, dest->bits.format))
699 for (i = 0; i < n_rects; ++i)
701 pixman_region32_t fill_region;
703 pixman_box32_t *boxes;
705 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
706 pixman_region32_intersect (&fill_region, &fill_region, &dest->common.clip_region);
708 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
709 for (j = 0; j < n_boxes; ++j)
711 const pixman_box32_t *box = &(boxes[j]);
712 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
713 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
717 pixman_region32_fini (&fill_region);
723 solid = pixman_image_create_solid_fill (color);
727 for (i = 0; i < n_rects; ++i)
729 const pixman_rectangle16_t *rect = &(rects[i]);
731 pixman_image_composite (op, solid, NULL, dest,
734 rect->width, rect->height);
737 pixman_image_unref (solid);
743 pixman_image_can_get_solid (pixman_image_t *image)
745 if (image->type == SOLID)
748 if (image->type != BITS ||
749 image->bits.width != 1 ||
750 image->bits.height != 1)
755 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
758 switch (image->bits.format)
760 case PIXMAN_a8r8g8b8:
761 case PIXMAN_x8r8g8b8:
762 case PIXMAN_a8b8g8r8:
763 case PIXMAN_x8b8g8r8:
775 pixman_image_is_opaque(pixman_image_t *image)
778 int gradientNumberOfColors = 0;
780 if(image->common.alpha_map)
786 if(PIXMAN_FORMAT_A(image->bits.format))
793 gradientNumberOfColors = image->gradient.n_stops;
795 while(i<gradientNumberOfColors)
797 if(image->gradient.stops[i].color.alpha != 0xffff)
804 if(Alpha(image->solid.color) != 0xff)
809 /* Convolution filters can introduce translucency if the sum of the weights
811 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
814 if (image->common.repeat == PIXMAN_REPEAT_NONE)
816 if (image->common.filter != PIXMAN_FILTER_NEAREST)
819 if (image->common.transform)