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 = malloc (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++;
110 pixman_image_unref (pixman_image_t *image)
112 image_common_t *common = (image_common_t *)image;
116 if (common->ref_count == 0)
118 pixman_region_fini (&common->clip_region);
119 pixman_region_fini (&common->full_region);
121 if (common->transform)
122 free (common->transform);
124 if (common->filter_params)
125 free (common->filter_params);
127 if (common->alpha_map)
128 pixman_image_unref ((pixman_image_t *)common->alpha_map);
131 if (image->type == BITS && image->bits.indexed)
132 free (image->bits.indexed);
136 memset (image, 0xaa, sizeof (pixman_image_t));
138 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
140 if (image->gradient.stops)
141 free (image->gradient.stops);
145 if (image->type == BITS && image->bits.free_me)
146 free (image->bits.free_me);
154 pixman_image_create_solid_fill (pixman_color_t *color)
156 pixman_image_t *img = allocate_image();
160 init_source_image (&img->solid.common);
163 img->solid.color = color_to_uint32 (color);
169 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
170 pixman_point_fixed_t *p2,
171 const pixman_gradient_stop_t *stops,
174 pixman_image_t *image;
175 linear_gradient_t *linear;
177 return_val_if_fail (n_stops >= 2, NULL);
179 image = allocate_image();
184 linear = &image->linear;
186 if (!init_gradient (&linear->common, stops, n_stops))
195 image->type = LINEAR;
202 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
203 pixman_point_fixed_t *outer,
204 pixman_fixed_t inner_radius,
205 pixman_fixed_t outer_radius,
206 const pixman_gradient_stop_t *stops,
209 pixman_image_t *image;
210 radial_gradient_t *radial;
212 return_val_if_fail (n_stops >= 2, NULL);
214 image = allocate_image();
219 radial = &image->radial;
221 if (!init_gradient (&radial->common, stops, n_stops))
227 image->type = RADIAL;
229 radial->c1.x = inner->x;
230 radial->c1.y = inner->y;
231 radial->c1.radius = inner_radius;
232 radial->c2.x = outer->x;
233 radial->c2.y = outer->y;
234 radial->c2.radius = outer_radius;
235 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
236 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
237 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
238 radial->A = (radial->cdx * radial->cdx
239 + radial->cdy * radial->cdy
240 - radial->dr * radial->dr);
246 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
247 pixman_fixed_t angle,
248 const pixman_gradient_stop_t *stops,
251 pixman_image_t *image = allocate_image();
252 conical_gradient_t *conical;
257 conical = &image->conical;
259 if (!init_gradient (&conical->common, stops, n_stops))
265 image->type = CONICAL;
266 conical->center = *center;
267 conical->angle = angle;
273 create_bits (pixman_format_code_t format,
276 int *rowstride_bytes)
282 bpp = PIXMAN_FORMAT_BPP (format);
283 stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
284 buf_size = height * stride;
287 *rowstride_bytes = stride;
289 return calloc (buf_size, 1);
293 reset_clip_region (pixman_image_t *image)
295 pixman_region_fini (&image->common.clip_region);
297 if (image->type == BITS)
299 pixman_region_init_rect (&image->common.clip_region, 0, 0,
300 image->bits.width, image->bits.height);
304 pixman_region_init (&image->common.clip_region);
309 pixman_image_create_bits (pixman_format_code_t format,
315 pixman_image_t *image;
316 uint32_t *free_me = NULL;
318 /* must be a whole number of uint32_t's
320 return_val_if_fail (bits == NULL ||
321 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
325 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
330 image = allocate_image();
336 image->bits.format = format;
337 image->bits.width = width;
338 image->bits.height = height;
339 image->bits.bits = bits;
340 image->bits.free_me = free_me;
342 image->bits.rowstride = rowstride_bytes / sizeof (uint32_t); /* we store it in number
345 image->bits.indexed = NULL;
347 pixman_region_fini (&image->common.full_region);
348 pixman_region_init_rect (&image->common.full_region, 0, 0,
349 image->bits.width, image->bits.height);
351 reset_clip_region (image);
356 pixman_image_set_clip_region (pixman_image_t *image,
357 pixman_region16_t *region)
359 image_common_t *common = (image_common_t *)image;
363 return pixman_region_copy (&common->clip_region, region);
367 reset_clip_region (image);
373 /* Sets whether the clip region includes a clip region set by the client
376 pixman_image_set_has_client_clip (pixman_image_t *image,
377 pixman_bool_t client_clip)
379 image->common.has_client_clip = client_clip;
383 pixman_image_set_transform (pixman_image_t *image,
384 const pixman_transform_t *transform)
386 static const pixman_transform_t id =
388 { { pixman_fixed_1, 0, 0 },
389 { 0, pixman_fixed_1, 0 },
390 { 0, 0, pixman_fixed_1 }
394 image_common_t *common = (image_common_t *)image;
396 if (common->transform == transform)
399 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
405 if (common->transform)
406 free (common->transform);
410 common->transform = malloc (sizeof (pixman_transform_t));
411 if (!common->transform)
414 *common->transform = *transform;
418 common->transform = NULL;
425 pixman_image_set_repeat (pixman_image_t *image,
426 pixman_repeat_t repeat)
428 image->common.repeat = repeat;
432 pixman_image_set_filter (pixman_image_t *image,
433 pixman_filter_t filter,
434 const pixman_fixed_t *params,
437 image_common_t *common = (image_common_t *)image;
438 pixman_fixed_t *new_params;
440 if (params == common->filter_params && filter == common->filter)
446 new_params = malloc (n_params * sizeof (pixman_fixed_t));
451 params, n_params * sizeof (pixman_fixed_t));
454 common->filter = filter;
456 if (common->filter_params)
457 free (common->filter_params);
459 common->filter_params = new_params;
460 common->n_filter_params = n_params;
464 /* Unlike all the other property setters, this function does not
465 * copy the content of indexed. Doing this copying is simply
466 * way, way too expensive.
469 pixman_image_set_indexed (pixman_image_t *image,
470 const pixman_indexed_t *indexed)
472 bits_image_t *bits = (bits_image_t *)image;
474 bits->indexed = indexed;
478 pixman_image_set_alpha_map (pixman_image_t *image,
479 pixman_image_t *alpha_map,
483 image_common_t *common = (image_common_t *)image;
485 return_if_fail (!alpha_map || alpha_map->type == BITS);
487 if (common->alpha_map != (bits_image_t *)alpha_map)
489 if (common->alpha_map)
490 pixman_image_unref ((pixman_image_t *)common->alpha_map);
493 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
495 common->alpha_map = NULL;
498 common->alpha_origin.x = x;
499 common->alpha_origin.y = y;
503 pixman_image_set_component_alpha (pixman_image_t *image,
504 pixman_bool_t component_alpha)
506 image->common.component_alpha = component_alpha;
511 pixman_image_set_accessors (pixman_image_t *image,
512 pixman_read_memory_func_t read_func,
513 pixman_write_memory_func_t write_func)
515 return_if_fail (image != NULL);
517 image->common.read_func = read_func;
518 image->common.write_func = write_func;
522 pixman_image_get_data (pixman_image_t *image)
524 if (image->type == BITS)
525 return image->bits.bits;
531 pixman_image_get_width (pixman_image_t *image)
533 if (image->type == BITS)
534 return image->bits.width;
540 pixman_image_get_height (pixman_image_t *image)
542 if (image->type == BITS)
543 return image->bits.height;
549 pixman_image_get_stride (pixman_image_t *image)
551 if (image->type == BITS)
552 return image->bits.rowstride * sizeof (uint32_t);
558 pixman_image_get_depth (pixman_image_t *image)
560 if (image->type == BITS)
561 return PIXMAN_FORMAT_DEPTH (image->bits.format);
567 pixman_image_fill_rectangles (pixman_op_t op,
568 pixman_image_t *dest,
569 pixman_color_t *color,
571 const pixman_rectangle16_t *rects)
573 pixman_image_t *solid;
577 if (color->alpha == 0xffff)
579 if (op == PIXMAN_OP_OVER)
583 if (op == PIXMAN_OP_CLEAR)
595 solid = pixman_image_create_solid_fill (color);
599 for (i = 0; i < n_rects; ++i)
601 const pixman_rectangle16_t *rect = &(rects[i]);
603 pixman_image_composite (op, solid, NULL, dest,
606 rect->width, rect->height);
609 pixman_image_unref (solid);