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"
34 init_source_image (source_image_t *image)
36 image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
40 init_gradient (gradient_t *gradient,
41 const pixman_gradient_stop_t *stops,
44 return_val_if_fail (n_stops > 0, FALSE);
46 init_source_image (&gradient->common);
48 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
52 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
54 gradient->n_stops = n_stops;
56 gradient->stop_range = 0xffff;
57 gradient->color_table = NULL;
58 gradient->color_table_size = 0;
64 color_to_uint32 (const pixman_color_t *color)
67 (color->alpha >> 8 << 24) |
68 (color->red >> 8 << 16) |
69 (color->green & 0xff00) |
73 static pixman_image_t *
76 pixman_image_t *image = malloc (sizeof (pixman_image_t));
80 image_common_t *common = &image->common;
82 pixman_region_init (&common->full_region);
83 pixman_region_init (&common->clip_region);
84 common->src_clip = &common->full_region;
85 common->has_client_clip = FALSE;
86 common->transform = NULL;
87 common->repeat = PIXMAN_REPEAT_NONE;
88 common->filter = PIXMAN_FILTER_NEAREST;
89 common->filter_params = NULL;
90 common->n_filter_params = 0;
91 common->alpha_map = NULL;
92 common->component_alpha = FALSE;
93 common->ref_count = 1;
94 common->read_func = NULL;
95 common->write_func = NULL;
103 pixman_image_ref (pixman_image_t *image)
105 image->common.ref_count++;
110 /* returns TRUE when the image is freed */
112 pixman_image_unref (pixman_image_t *image)
114 image_common_t *common = (image_common_t *)image;
118 if (common->ref_count == 0)
120 pixman_region_fini (&common->clip_region);
121 pixman_region_fini (&common->full_region);
123 if (common->transform)
124 free (common->transform);
126 if (common->filter_params)
127 free (common->filter_params);
129 if (common->alpha_map)
130 pixman_image_unref ((pixman_image_t *)common->alpha_map);
133 if (image->type == BITS && image->bits.indexed)
134 free (image->bits.indexed);
138 memset (image, 0xaa, sizeof (pixman_image_t));
140 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
142 if (image->gradient.stops)
143 free (image->gradient.stops);
147 if (image->type == BITS && image->bits.free_me)
148 free (image->bits.free_me);
160 pixman_image_create_solid_fill (pixman_color_t *color)
162 pixman_image_t *img = allocate_image();
166 init_source_image (&img->solid.common);
169 img->solid.color = color_to_uint32 (color);
175 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
176 pixman_point_fixed_t *p2,
177 const pixman_gradient_stop_t *stops,
180 pixman_image_t *image;
181 linear_gradient_t *linear;
183 return_val_if_fail (n_stops >= 2, NULL);
185 image = allocate_image();
190 linear = &image->linear;
192 if (!init_gradient (&linear->common, stops, n_stops))
201 image->type = LINEAR;
208 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
209 pixman_point_fixed_t *outer,
210 pixman_fixed_t inner_radius,
211 pixman_fixed_t outer_radius,
212 const pixman_gradient_stop_t *stops,
215 pixman_image_t *image;
216 radial_gradient_t *radial;
218 return_val_if_fail (n_stops >= 2, NULL);
220 image = allocate_image();
225 radial = &image->radial;
227 if (!init_gradient (&radial->common, stops, n_stops))
233 image->type = RADIAL;
235 radial->c1.x = inner->x;
236 radial->c1.y = inner->y;
237 radial->c1.radius = inner_radius;
238 radial->c2.x = outer->x;
239 radial->c2.y = outer->y;
240 radial->c2.radius = outer_radius;
241 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
242 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
243 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
244 radial->A = (radial->cdx * radial->cdx
245 + radial->cdy * radial->cdy
246 - radial->dr * radial->dr);
252 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
253 pixman_fixed_t angle,
254 const pixman_gradient_stop_t *stops,
257 pixman_image_t *image = allocate_image();
258 conical_gradient_t *conical;
263 conical = &image->conical;
265 if (!init_gradient (&conical->common, stops, n_stops))
271 image->type = CONICAL;
272 conical->center = *center;
273 conical->angle = angle;
279 create_bits (pixman_format_code_t format,
282 int *rowstride_bytes)
288 /* what follows is a long-winded way, avoiding any possibility of integer
289 * overflows, of saying:
290 * stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
293 bpp = PIXMAN_FORMAT_BPP (format);
294 if (pixman_multiply_overflows_int (width, bpp))
297 stride = width * bpp;
298 if (pixman_addition_overflows_int (stride, FB_MASK))
305 if (pixman_multiply_overflows_int (stride, sizeof (uint32_t)))
308 stride *= sizeof (uint32_t);
310 if (pixman_multiply_overflows_int (height, stride))
313 buf_size = height * stride;
316 *rowstride_bytes = stride;
318 return calloc (buf_size, 1);
322 reset_clip_region (pixman_image_t *image)
324 pixman_region_fini (&image->common.clip_region);
326 if (image->type == BITS)
328 pixman_region_init_rect (&image->common.clip_region, 0, 0,
329 image->bits.width, image->bits.height);
333 pixman_region_init (&image->common.clip_region);
338 pixman_image_create_bits (pixman_format_code_t format,
344 pixman_image_t *image;
345 uint32_t *free_me = NULL;
347 /* must be a whole number of uint32_t's
349 return_val_if_fail (bits == NULL ||
350 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
352 if (!bits && width && height)
354 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
359 image = allocate_image();
368 image->bits.format = format;
369 image->bits.width = width;
370 image->bits.height = height;
371 image->bits.bits = bits;
372 image->bits.free_me = free_me;
374 image->bits.rowstride = rowstride_bytes / (int) sizeof (uint32_t); /* we store it in number
377 image->bits.indexed = NULL;
379 pixman_region_fini (&image->common.full_region);
380 pixman_region_init_rect (&image->common.full_region, 0, 0,
381 image->bits.width, image->bits.height);
383 reset_clip_region (image);
388 pixman_image_set_clip_region (pixman_image_t *image,
389 pixman_region16_t *region)
391 image_common_t *common = (image_common_t *)image;
395 return pixman_region_copy (&common->clip_region, region);
399 reset_clip_region (image);
405 /* Sets whether the clip region includes a clip region set by the client
408 pixman_image_set_has_client_clip (pixman_image_t *image,
409 pixman_bool_t client_clip)
411 image->common.has_client_clip = client_clip;
415 pixman_image_set_transform (pixman_image_t *image,
416 const pixman_transform_t *transform)
418 static const pixman_transform_t id =
420 { { pixman_fixed_1, 0, 0 },
421 { 0, pixman_fixed_1, 0 },
422 { 0, 0, pixman_fixed_1 }
426 image_common_t *common = (image_common_t *)image;
428 if (common->transform == transform)
431 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
433 free(common->transform);
434 common->transform = NULL;
438 if (common->transform == NULL)
439 common->transform = malloc (sizeof (pixman_transform_t));
440 if (common->transform == NULL)
443 memcpy(common->transform, transform, sizeof(pixman_transform_t));
449 pixman_image_set_repeat (pixman_image_t *image,
450 pixman_repeat_t repeat)
452 image->common.repeat = repeat;
456 pixman_image_set_filter (pixman_image_t *image,
457 pixman_filter_t filter,
458 const pixman_fixed_t *params,
461 image_common_t *common = (image_common_t *)image;
462 pixman_fixed_t *new_params;
464 if (params == common->filter_params && filter == common->filter)
470 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
475 params, n_params * sizeof (pixman_fixed_t));
478 common->filter = filter;
480 if (common->filter_params)
481 free (common->filter_params);
483 common->filter_params = new_params;
484 common->n_filter_params = n_params;
489 pixman_image_set_source_clipping (pixman_image_t *image,
490 pixman_bool_t source_clipping)
492 image_common_t *common = &image->common;
495 common->src_clip = &common->clip_region;
497 common->src_clip = &common->full_region;
500 /* Unlike all the other property setters, this function does not
501 * copy the content of indexed. Doing this copying is simply
502 * way, way too expensive.
505 pixman_image_set_indexed (pixman_image_t *image,
506 const pixman_indexed_t *indexed)
508 bits_image_t *bits = (bits_image_t *)image;
510 bits->indexed = indexed;
514 pixman_image_set_alpha_map (pixman_image_t *image,
515 pixman_image_t *alpha_map,
519 image_common_t *common = (image_common_t *)image;
521 return_if_fail (!alpha_map || alpha_map->type == BITS);
523 if (common->alpha_map != (bits_image_t *)alpha_map)
525 if (common->alpha_map)
526 pixman_image_unref ((pixman_image_t *)common->alpha_map);
529 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
531 common->alpha_map = NULL;
534 common->alpha_origin.x = x;
535 common->alpha_origin.y = y;
539 pixman_image_set_component_alpha (pixman_image_t *image,
540 pixman_bool_t component_alpha)
542 image->common.component_alpha = component_alpha;
547 pixman_image_set_accessors (pixman_image_t *image,
548 pixman_read_memory_func_t read_func,
549 pixman_write_memory_func_t write_func)
551 return_if_fail (image != NULL);
553 image->common.read_func = read_func;
554 image->common.write_func = write_func;
558 pixman_image_get_data (pixman_image_t *image)
560 if (image->type == BITS)
561 return image->bits.bits;
567 pixman_image_get_width (pixman_image_t *image)
569 if (image->type == BITS)
570 return image->bits.width;
576 pixman_image_get_height (pixman_image_t *image)
578 if (image->type == BITS)
579 return image->bits.height;
585 pixman_image_get_stride (pixman_image_t *image)
587 if (image->type == BITS)
588 return image->bits.rowstride * (int) sizeof (uint32_t);
594 pixman_image_get_depth (pixman_image_t *image)
596 if (image->type == BITS)
597 return PIXMAN_FORMAT_DEPTH (image->bits.format);
603 color_to_pixel (pixman_color_t *color,
605 pixman_format_code_t format)
607 uint32_t c = color_to_uint32 (color);
609 if (!(format == PIXMAN_a8r8g8b8 ||
610 format == PIXMAN_x8r8g8b8 ||
611 format == PIXMAN_a8b8g8r8 ||
612 format == PIXMAN_x8b8g8r8 ||
613 format == PIXMAN_r5g6b5 ||
614 format == PIXMAN_b5g6r5 ||
615 format == PIXMAN_a8))
620 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
622 c = ((c & 0xff000000) >> 0) |
623 ((c & 0x00ff0000) >> 16) |
624 ((c & 0x0000ff00) >> 0) |
625 ((c & 0x000000ff) << 16);
628 if (format == PIXMAN_a8)
630 else if (format == PIXMAN_r5g6b5 ||
631 format == PIXMAN_b5g6r5)
632 c = cvt8888to0565 (c);
635 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
636 printf ("pixel: %x\n", c);
644 pixman_image_fill_rectangles (pixman_op_t op,
645 pixman_image_t *dest,
646 pixman_color_t *color,
648 const pixman_rectangle16_t *rects)
650 pixman_image_t *solid;
654 if (color->alpha == 0xffff)
656 if (op == PIXMAN_OP_OVER)
660 if (op == PIXMAN_OP_CLEAR)
672 if (op == PIXMAN_OP_SRC)
676 if (color_to_pixel (color, &pixel, dest->bits.format))
678 for (i = 0; i < n_rects; ++i)
680 pixman_region16_t fill_region;
682 pixman_box16_t *boxes;
684 pixman_region_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
685 pixman_region_intersect (&fill_region, &fill_region, &dest->common.clip_region);
687 boxes = pixman_region_rectangles (&fill_region, &n_boxes);
688 for (j = 0; j < n_boxes; ++j)
690 const pixman_box16_t *box = &(boxes[j]);
691 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
692 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
696 pixman_region_fini (&fill_region);
702 solid = pixman_image_create_solid_fill (color);
706 for (i = 0; i < n_rects; ++i)
708 const pixman_rectangle16_t *rect = &(rects[i]);
710 pixman_image_composite (op, solid, NULL, dest,
713 rect->width, rect->height);
716 pixman_image_unref (solid);