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 _pixman_init_gradient (gradient_t *gradient,
37 const pixman_gradient_stop_t *stops,
40 return_val_if_fail (n_stops > 0, FALSE);
42 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
46 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
48 gradient->n_stops = n_stops;
50 gradient->stop_range = 0xffff;
51 gradient->color_table = NULL;
52 gradient->color_table_size = 0;
53 gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
59 _pixman_image_allocate (void)
61 pixman_image_t *image = malloc (sizeof (pixman_image_t));
65 image_common_t *common = &image->common;
67 pixman_region32_init (&common->full_region);
68 pixman_region32_init (&common->clip_region);
69 common->src_clip = &common->full_region;
70 common->has_client_clip = FALSE;
71 common->transform = NULL;
72 common->repeat = PIXMAN_REPEAT_NONE;
73 common->filter = PIXMAN_FILTER_NEAREST;
74 common->filter_params = NULL;
75 common->n_filter_params = 0;
76 common->alpha_map = NULL;
77 common->component_alpha = FALSE;
78 common->ref_count = 1;
79 common->read_func = NULL;
80 common->write_func = NULL;
81 common->classify = NULL;
88 _pixman_image_classify (pixman_image_t *image,
94 if (image->common.classify)
95 return image->common.classify (image, x, y, width, height);
97 return SOURCE_IMAGE_CLASS_UNKNOWN;
100 #define READ_ACCESS(f) ((image->common.read_func)? f##_accessors : f)
102 static void fbFetchSolid(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
106 fetchPixelProc32 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture32)(image);
108 color = fetch(image, 0, 0);
110 end = buffer + width;
115 static void fbFetchSolid64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
119 fetchPixelProc64 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture64)(image);
121 color = fetch(image, 0, 0);
123 end = buffer + width;
128 static void fbFetch(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
130 fetchProc32 fetch = READ_ACCESS(pixman_fetchProcForPicture32)(image);
132 fetch(image, x, y, width, buffer);
135 static void fbFetch64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
137 fetchProc64 fetch = READ_ACCESS(pixman_fetchProcForPicture64)(image);
139 fetch(image, x, y, width, buffer);
143 _pixman_image_get_fetcher (pixman_image_t *image,
146 if (IS_SOURCE_IMAGE (image))
149 return (scanFetchProc)pixmanFetchSourcePict64;
151 return (scanFetchProc)pixmanFetchSourcePict;
155 bits_image_t *bits = (bits_image_t *)image;
157 if (bits->common.alpha_map)
160 return (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha64);
162 return (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha);
164 else if ((bits->common.repeat != PIXMAN_REPEAT_NONE) &&
169 return (scanFetchProc)fbFetchSolid64;
171 return (scanFetchProc)fbFetchSolid;
173 else if (!bits->common.transform && bits->common.filter != PIXMAN_FILTER_CONVOLUTION
174 && bits->common.repeat != PIXMAN_REPEAT_PAD && bits->common.repeat != PIXMAN_REPEAT_REFLECT)
177 return (scanFetchProc)fbFetch64;
179 return (scanFetchProc)fbFetch;
184 return (scanFetchProc)READ_ACCESS(fbFetchTransformed64);
186 return (scanFetchProc)READ_ACCESS(fbFetchTransformed);
193 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
196 fbStore(bits_image_t * image, int x, int y, int width, uint32_t *buffer)
200 storeProc32 store = WRITE_ACCESS(pixman_storeProcForPicture32)(image);
201 const pixman_indexed_t * indexed = image->indexed;
204 stride = image->rowstride;
206 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
210 fbStore64(bits_image_t * image, int x, int y, int width, uint64_t *buffer)
214 storeProc64 store = WRITE_ACCESS(pixman_storeProcForPicture64)(image);
215 const pixman_indexed_t * indexed = image->indexed;
218 stride = image->rowstride;
220 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
224 _pixman_image_get_storer (pixman_image_t *image,
227 if (image->common.alpha_map)
230 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha64);
232 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha);
237 return (scanStoreProc)fbStore64;
239 return (scanStoreProc)fbStore;
244 PIXMAN_EXPORT pixman_image_t *
245 pixman_image_ref (pixman_image_t *image)
247 image->common.ref_count++;
252 /* returns TRUE when the image is freed */
253 PIXMAN_EXPORT pixman_bool_t
254 pixman_image_unref (pixman_image_t *image)
256 image_common_t *common = (image_common_t *)image;
260 if (common->ref_count == 0)
262 pixman_region32_fini (&common->clip_region);
263 pixman_region32_fini (&common->full_region);
265 if (common->transform)
266 free (common->transform);
268 if (common->filter_params)
269 free (common->filter_params);
271 if (common->alpha_map)
272 pixman_image_unref ((pixman_image_t *)common->alpha_map);
275 if (image->type == BITS && image->bits.indexed)
276 free (image->bits.indexed);
280 memset (image, 0xaa, sizeof (pixman_image_t));
282 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
284 if (image->gradient.stops)
285 free (image->gradient.stops);
289 if (image->type == BITS && image->bits.free_me)
290 free (image->bits.free_me);
303 _pixman_image_reset_clip_region (pixman_image_t *image)
305 pixman_region32_fini (&image->common.clip_region);
307 if (image->type == BITS)
309 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
310 image->bits.width, image->bits.height);
314 pixman_region32_init (&image->common.clip_region);
318 PIXMAN_EXPORT pixman_bool_t
319 pixman_image_set_clip_region32 (pixman_image_t *image,
320 pixman_region32_t *region)
322 image_common_t *common = (image_common_t *)image;
326 return pixman_region32_copy (&common->clip_region, region);
330 _pixman_image_reset_clip_region (image);
337 PIXMAN_EXPORT pixman_bool_t
338 pixman_image_set_clip_region (pixman_image_t *image,
339 pixman_region16_t *region)
341 image_common_t *common = (image_common_t *)image;
345 return pixman_region32_copy_from_region16 (&common->clip_region, region);
349 _pixman_image_reset_clip_region (image);
355 /* Sets whether the clip region includes a clip region set by the client
358 pixman_image_set_has_client_clip (pixman_image_t *image,
359 pixman_bool_t client_clip)
361 image->common.has_client_clip = client_clip;
364 PIXMAN_EXPORT pixman_bool_t
365 pixman_image_set_transform (pixman_image_t *image,
366 const pixman_transform_t *transform)
368 static const pixman_transform_t id =
370 { { pixman_fixed_1, 0, 0 },
371 { 0, pixman_fixed_1, 0 },
372 { 0, 0, pixman_fixed_1 }
376 image_common_t *common = (image_common_t *)image;
378 if (common->transform == transform)
381 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
383 free(common->transform);
384 common->transform = NULL;
388 if (common->transform == NULL)
389 common->transform = malloc (sizeof (pixman_transform_t));
390 if (common->transform == NULL)
393 memcpy(common->transform, transform, sizeof(pixman_transform_t));
399 pixman_image_set_repeat (pixman_image_t *image,
400 pixman_repeat_t repeat)
402 image->common.repeat = repeat;
405 PIXMAN_EXPORT pixman_bool_t
406 pixman_image_set_filter (pixman_image_t *image,
407 pixman_filter_t filter,
408 const pixman_fixed_t *params,
411 image_common_t *common = (image_common_t *)image;
412 pixman_fixed_t *new_params;
414 if (params == common->filter_params && filter == common->filter)
420 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
425 params, n_params * sizeof (pixman_fixed_t));
428 common->filter = filter;
430 if (common->filter_params)
431 free (common->filter_params);
433 common->filter_params = new_params;
434 common->n_filter_params = n_params;
439 pixman_image_set_source_clipping (pixman_image_t *image,
440 pixman_bool_t source_clipping)
442 image_common_t *common = &image->common;
445 common->src_clip = &common->clip_region;
447 common->src_clip = &common->full_region;
450 /* Unlike all the other property setters, this function does not
451 * copy the content of indexed. Doing this copying is simply
452 * way, way too expensive.
455 pixman_image_set_indexed (pixman_image_t *image,
456 const pixman_indexed_t *indexed)
458 bits_image_t *bits = (bits_image_t *)image;
460 bits->indexed = indexed;
464 pixman_image_set_alpha_map (pixman_image_t *image,
465 pixman_image_t *alpha_map,
469 image_common_t *common = (image_common_t *)image;
471 return_if_fail (!alpha_map || alpha_map->type == BITS);
473 if (common->alpha_map != (bits_image_t *)alpha_map)
475 if (common->alpha_map)
476 pixman_image_unref ((pixman_image_t *)common->alpha_map);
479 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
481 common->alpha_map = NULL;
484 common->alpha_origin.x = x;
485 common->alpha_origin.y = y;
489 pixman_image_set_component_alpha (pixman_image_t *image,
490 pixman_bool_t component_alpha)
492 image->common.component_alpha = component_alpha;
497 pixman_image_set_accessors (pixman_image_t *image,
498 pixman_read_memory_func_t read_func,
499 pixman_write_memory_func_t write_func)
501 return_if_fail (image != NULL);
503 image->common.read_func = read_func;
504 image->common.write_func = write_func;
507 PIXMAN_EXPORT uint32_t *
508 pixman_image_get_data (pixman_image_t *image)
510 if (image->type == BITS)
511 return image->bits.bits;
517 pixman_image_get_width (pixman_image_t *image)
519 if (image->type == BITS)
520 return image->bits.width;
526 pixman_image_get_height (pixman_image_t *image)
528 if (image->type == BITS)
529 return image->bits.height;
535 pixman_image_get_stride (pixman_image_t *image)
537 if (image->type == BITS)
538 return image->bits.rowstride * (int) sizeof (uint32_t);
544 pixman_image_get_depth (pixman_image_t *image)
546 if (image->type == BITS)
547 return PIXMAN_FORMAT_DEPTH (image->bits.format);
553 color_to_uint32 (const pixman_color_t *color)
556 (color->alpha >> 8 << 24) |
557 (color->red >> 8 << 16) |
558 (color->green & 0xff00) |
563 color_to_pixel (pixman_color_t *color,
565 pixman_format_code_t format)
567 uint32_t c = color_to_uint32 (color);
569 if (!(format == PIXMAN_a8r8g8b8 ||
570 format == PIXMAN_x8r8g8b8 ||
571 format == PIXMAN_a8b8g8r8 ||
572 format == PIXMAN_x8b8g8r8 ||
573 format == PIXMAN_b8g8r8a8 ||
574 format == PIXMAN_b8g8r8x8 ||
575 format == PIXMAN_r5g6b5 ||
576 format == PIXMAN_b5g6r5 ||
577 format == PIXMAN_a8))
582 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
584 c = ((c & 0xff000000) >> 0) |
585 ((c & 0x00ff0000) >> 16) |
586 ((c & 0x0000ff00) >> 0) |
587 ((c & 0x000000ff) << 16);
589 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
591 c = ((c & 0xff000000) >> 24) |
592 ((c & 0x00ff0000) >> 8) |
593 ((c & 0x0000ff00) << 8) |
594 ((c & 0x000000ff) << 24);
597 if (format == PIXMAN_a8)
599 else if (format == PIXMAN_r5g6b5 ||
600 format == PIXMAN_b5g6r5)
601 c = cvt8888to0565 (c);
604 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
605 printf ("pixel: %x\n", c);
612 PIXMAN_EXPORT pixman_bool_t
613 pixman_image_fill_rectangles (pixman_op_t op,
614 pixman_image_t *dest,
615 pixman_color_t *color,
617 const pixman_rectangle16_t *rects)
619 pixman_image_t *solid;
623 if (color->alpha == 0xffff)
625 if (op == PIXMAN_OP_OVER)
629 if (op == PIXMAN_OP_CLEAR)
641 if (op == PIXMAN_OP_SRC)
645 if (color_to_pixel (color, &pixel, dest->bits.format))
647 for (i = 0; i < n_rects; ++i)
649 pixman_region32_t fill_region;
651 pixman_box32_t *boxes;
653 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
654 if (!pixman_region32_intersect (&fill_region,
656 &dest->common.clip_region))
660 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
661 for (j = 0; j < n_boxes; ++j)
663 const pixman_box32_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_region32_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);
695 pixman_image_can_get_solid (pixman_image_t *image)
697 if (image->type == SOLID)
700 if (image->type != BITS ||
701 image->bits.width != 1 ||
702 image->bits.height != 1)
707 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
710 switch (image->bits.format)
712 case PIXMAN_a8r8g8b8:
713 case PIXMAN_x8r8g8b8:
714 case PIXMAN_a8b8g8r8:
715 case PIXMAN_x8b8g8r8:
716 case PIXMAN_b8g8r8a8:
717 case PIXMAN_b8g8r8x8:
729 pixman_image_is_opaque(pixman_image_t *image)
732 int gradientNumberOfColors = 0;
734 if(image->common.alpha_map)
740 if(PIXMAN_FORMAT_A(image->bits.format))
747 gradientNumberOfColors = image->gradient.n_stops;
749 while(i<gradientNumberOfColors)
751 if(image->gradient.stops[i].color.alpha != 0xffff)
758 if(Alpha(image->solid.color) != 0xff)
763 /* Convolution filters can introduce translucency if the sum of the weights
765 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
768 if (image->common.repeat == PIXMAN_REPEAT_NONE)
770 if (image->common.filter != PIXMAN_FILTER_NEAREST)
773 if (image->common.transform)
776 /* Gradients do not necessarily cover the entire compositing area */
777 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)