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.
30 #include "pixman-private.h"
33 init_source_image (source_image_t *image)
35 image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
39 init_gradient (gradient_t *gradient,
40 const pixman_gradient_stop_t *stops,
43 return_val_if_fail (n_stops > 0, FALSE);
45 init_source_image (&gradient->common);
47 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
51 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
53 gradient->n_stops = n_stops;
55 gradient->stop_range = 0xffff;
56 gradient->color_table = NULL;
57 gradient->color_table_size = 0;
63 color_to_uint32 (const pixman_color_t *color)
66 (color->alpha >> 8 << 24) |
67 (color->red >> 8 << 16) |
68 (color->green & 0xff00) |
72 static pixman_image_t *
75 pixman_image_t *image = malloc (sizeof (pixman_image_t));
79 image_common_t *common = &image->common;
81 pixman_region_init (&common->full_region);
82 pixman_region_init (&common->clip_region);
83 common->src_clip = &common->full_region;
84 common->has_client_clip = FALSE;
85 common->transform = NULL;
86 common->repeat = PIXMAN_REPEAT_NONE;
87 common->filter = PIXMAN_FILTER_NEAREST;
88 common->filter_params = NULL;
89 common->n_filter_params = 0;
90 common->alpha_map = NULL;
91 common->component_alpha = FALSE;
92 common->ref_count = 1;
93 common->read_func = NULL;
94 common->write_func = NULL;
102 pixman_image_ref (pixman_image_t *image)
104 image->common.ref_count++;
109 /* returns TRUE when the image is freed */
111 pixman_image_unref (pixman_image_t *image)
113 image_common_t *common = (image_common_t *)image;
117 if (common->ref_count == 0)
119 pixman_region_fini (&common->clip_region);
120 pixman_region_fini (&common->full_region);
122 if (common->transform)
123 free (common->transform);
125 if (common->filter_params)
126 free (common->filter_params);
128 if (common->alpha_map)
129 pixman_image_unref ((pixman_image_t *)common->alpha_map);
132 if (image->type == BITS && image->bits.indexed)
133 free (image->bits.indexed);
137 memset (image, 0xaa, sizeof (pixman_image_t));
139 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
141 if (image->gradient.stops)
142 free (image->gradient.stops);
146 if (image->type == BITS && image->bits.free_me)
147 free (image->bits.free_me);
159 pixman_image_create_solid_fill (pixman_color_t *color)
161 pixman_image_t *img = allocate_image();
165 init_source_image (&img->solid.common);
168 img->solid.color = color_to_uint32 (color);
174 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
175 pixman_point_fixed_t *p2,
176 const pixman_gradient_stop_t *stops,
179 pixman_image_t *image;
180 linear_gradient_t *linear;
182 return_val_if_fail (n_stops >= 2, NULL);
184 image = allocate_image();
189 linear = &image->linear;
191 if (!init_gradient (&linear->common, stops, n_stops))
200 image->type = LINEAR;
207 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
208 pixman_point_fixed_t *outer,
209 pixman_fixed_t inner_radius,
210 pixman_fixed_t outer_radius,
211 const pixman_gradient_stop_t *stops,
214 pixman_image_t *image;
215 radial_gradient_t *radial;
217 return_val_if_fail (n_stops >= 2, NULL);
219 image = allocate_image();
224 radial = &image->radial;
226 if (!init_gradient (&radial->common, stops, n_stops))
232 image->type = RADIAL;
234 radial->c1.x = inner->x;
235 radial->c1.y = inner->y;
236 radial->c1.radius = inner_radius;
237 radial->c2.x = outer->x;
238 radial->c2.y = outer->y;
239 radial->c2.radius = outer_radius;
240 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
241 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
242 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
243 radial->A = (radial->cdx * radial->cdx
244 + radial->cdy * radial->cdy
245 - radial->dr * radial->dr);
251 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
252 pixman_fixed_t angle,
253 const pixman_gradient_stop_t *stops,
256 pixman_image_t *image = allocate_image();
257 conical_gradient_t *conical;
262 conical = &image->conical;
264 if (!init_gradient (&conical->common, stops, n_stops))
270 image->type = CONICAL;
271 conical->center = *center;
272 conical->angle = angle;
278 create_bits (pixman_format_code_t format,
281 int *rowstride_bytes)
287 bpp = PIXMAN_FORMAT_BPP (format);
288 stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
289 buf_size = height * stride;
292 *rowstride_bytes = stride;
294 return calloc (buf_size, 1);
298 reset_clip_region (pixman_image_t *image)
300 pixman_region_fini (&image->common.clip_region);
302 if (image->type == BITS)
304 pixman_region_init_rect (&image->common.clip_region, 0, 0,
305 image->bits.width, image->bits.height);
309 pixman_region_init (&image->common.clip_region);
314 pixman_image_create_bits (pixman_format_code_t format,
320 pixman_image_t *image;
321 uint32_t *free_me = NULL;
323 /* must be a whole number of uint32_t's
325 return_val_if_fail (bits == NULL ||
326 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
330 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
335 image = allocate_image();
341 image->bits.format = format;
342 image->bits.width = width;
343 image->bits.height = height;
344 image->bits.bits = bits;
345 image->bits.free_me = free_me;
347 image->bits.rowstride = rowstride_bytes / sizeof (uint32_t); /* we store it in number
350 image->bits.indexed = NULL;
352 pixman_region_fini (&image->common.full_region);
353 pixman_region_init_rect (&image->common.full_region, 0, 0,
354 image->bits.width, image->bits.height);
356 reset_clip_region (image);
361 pixman_image_set_clip_region (pixman_image_t *image,
362 pixman_region16_t *region)
364 image_common_t *common = (image_common_t *)image;
368 return pixman_region_copy (&common->clip_region, region);
372 reset_clip_region (image);
378 /* Sets whether the clip region includes a clip region set by the client
381 pixman_image_set_has_client_clip (pixman_image_t *image,
382 pixman_bool_t client_clip)
384 image->common.has_client_clip = client_clip;
388 pixman_image_set_transform (pixman_image_t *image,
389 const pixman_transform_t *transform)
391 static const pixman_transform_t id =
393 { { pixman_fixed_1, 0, 0 },
394 { 0, pixman_fixed_1, 0 },
395 { 0, 0, pixman_fixed_1 }
399 image_common_t *common = (image_common_t *)image;
401 if (common->transform == transform)
404 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
406 free(common->transform);
407 common->transform = NULL;
411 if (common->transform == NULL)
412 common->transform = malloc (sizeof (pixman_transform_t));
413 if (common->transform == NULL)
416 memcpy(common->transform, transform, sizeof(pixman_transform_t));
422 pixman_image_set_repeat (pixman_image_t *image,
423 pixman_repeat_t repeat)
425 image->common.repeat = repeat;
429 pixman_image_set_filter (pixman_image_t *image,
430 pixman_filter_t filter,
431 const pixman_fixed_t *params,
434 image_common_t *common = (image_common_t *)image;
435 pixman_fixed_t *new_params;
437 if (params == common->filter_params && filter == common->filter)
443 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
448 params, n_params * sizeof (pixman_fixed_t));
451 common->filter = filter;
453 if (common->filter_params)
454 free (common->filter_params);
456 common->filter_params = new_params;
457 common->n_filter_params = n_params;
462 pixman_image_set_source_clipping (pixman_image_t *image,
463 pixman_bool_t source_clipping)
465 image_common_t *common = &image->common;
468 common->src_clip = &common->clip_region;
470 common->src_clip = &common->full_region;
473 /* Unlike all the other property setters, this function does not
474 * copy the content of indexed. Doing this copying is simply
475 * way, way too expensive.
478 pixman_image_set_indexed (pixman_image_t *image,
479 const pixman_indexed_t *indexed)
481 bits_image_t *bits = (bits_image_t *)image;
483 bits->indexed = indexed;
487 pixman_image_set_alpha_map (pixman_image_t *image,
488 pixman_image_t *alpha_map,
492 image_common_t *common = (image_common_t *)image;
494 return_if_fail (!alpha_map || alpha_map->type == BITS);
496 if (common->alpha_map != (bits_image_t *)alpha_map)
498 if (common->alpha_map)
499 pixman_image_unref ((pixman_image_t *)common->alpha_map);
502 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
504 common->alpha_map = NULL;
507 common->alpha_origin.x = x;
508 common->alpha_origin.y = y;
512 pixman_image_set_component_alpha (pixman_image_t *image,
513 pixman_bool_t component_alpha)
515 image->common.component_alpha = component_alpha;
520 pixman_image_set_accessors (pixman_image_t *image,
521 pixman_read_memory_func_t read_func,
522 pixman_write_memory_func_t write_func)
524 return_if_fail (image != NULL);
526 image->common.read_func = read_func;
527 image->common.write_func = write_func;
531 pixman_image_get_data (pixman_image_t *image)
533 if (image->type == BITS)
534 return image->bits.bits;
540 pixman_image_get_width (pixman_image_t *image)
542 if (image->type == BITS)
543 return image->bits.width;
549 pixman_image_get_height (pixman_image_t *image)
551 if (image->type == BITS)
552 return image->bits.height;
558 pixman_image_get_stride (pixman_image_t *image)
560 if (image->type == BITS)
561 return image->bits.rowstride * sizeof (uint32_t);
567 pixman_image_get_depth (pixman_image_t *image)
569 if (image->type == BITS)
570 return PIXMAN_FORMAT_DEPTH (image->bits.format);
576 color_to_pixel (pixman_color_t *color,
578 pixman_format_code_t format)
580 uint32_t c = color_to_uint32 (color);
582 if (!(format == PIXMAN_a8r8g8b8 ||
583 format == PIXMAN_x8r8g8b8 ||
584 format == PIXMAN_a8b8g8r8 ||
585 format == PIXMAN_x8b8g8r8 ||
586 format == PIXMAN_r5g6b5 ||
587 format == PIXMAN_b5g6r5 ||
588 format == PIXMAN_a8))
593 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
595 c = ((c & 0xff000000) >> 0) |
596 ((c & 0x00ff0000) >> 16) |
597 ((c & 0x0000ff00) >> 0) |
598 ((c & 0x000000ff) << 16);
601 if (format == PIXMAN_a8)
603 else if (format == PIXMAN_r5g6b5 ||
604 format == PIXMAN_b5g6r5)
605 c = cvt8888to0565 (c);
608 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
609 printf ("pixel: %x\n", c);
617 pixman_image_fill_rectangles (pixman_op_t op,
618 pixman_image_t *dest,
619 pixman_color_t *color,
621 const pixman_rectangle16_t *rects)
623 pixman_image_t *solid;
627 if (color->alpha == 0xffff)
629 if (op == PIXMAN_OP_OVER)
633 if (op == PIXMAN_OP_CLEAR)
645 if (op == PIXMAN_OP_SRC)
649 if (color_to_pixel (color, &pixel, dest->bits.format))
651 for (i = 0; i < n_rects; ++i)
653 pixman_region16_t fill_region;
655 pixman_box16_t *boxes;
657 pixman_region_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
658 pixman_region_intersect (&fill_region, &fill_region, &dest->common.clip_region);
660 boxes = pixman_region_rectangles (&fill_region, &n_boxes);
661 for (j = 0; j < n_boxes; ++j)
663 const pixman_box16_t *box = &(boxes[j]);
664 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
665 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
669 pixman_region_fini (&fill_region);
675 solid = pixman_image_create_solid_fill (color);
679 for (i = 0; i < n_rects; ++i)
681 const pixman_rectangle16_t *rect = &(rects[i]);
683 pixman_image_composite (op, solid, NULL, dest,
686 rect->width, rect->height);
689 pixman_image_unref (solid);