1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2003 University of Southern California
5 * Copyright © 2009,2010,2011 Intel Corporation
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
30 * The Original Code is the cairo graphics library.
32 * The Initial Developer of the Original Code is University of Southern
36 * Carl D. Worth <cworth@cworth.org>
37 * Chris Wilson <chris@chris-wilson.co.uk>
42 #include "cairo-boxes-private.h"
43 #include "cairo-clip-private.h"
44 #include "cairo-composite-rectangles-private.h"
45 #include "cairo-compositor-private.h"
46 #include "cairo-default-context-private.h"
47 #include "cairo-error-private.h"
48 #include "cairo-image-surface-private.h"
49 #include "cairo-paginated-private.h"
50 #include "cairo-pattern-private.h"
51 #include "cairo-recording-surface-private.h"
52 #include "cairo-region-private.h"
53 #include "cairo-scaled-font-private.h"
54 #include "cairo-surface-snapshot-private.h"
55 #include "cairo-surface-subsurface-private.h"
57 /* Limit on the width / height of an image surface in pixels. This is
58 * mainly determined by coordinates of things sent to pixman at the
59 * moment being in 16.16 format. */
60 #define MAX_IMAGE_SIZE 32767
64 * @Title: Image Surfaces
65 * @Short_Description: Rendering to memory buffers
66 * @See_Also: #cairo_surface_t
68 * Image surfaces provide the ability to render to memory buffers
69 * either allocated by cairo or by the calling code. The supported
70 * image formats are those defined in #cairo_format_t.
74 * CAIRO_HAS_IMAGE_SURFACE:
76 * Defined if the image surface backend is available.
77 * The image surface backend is always built in.
78 * This macro was added for completeness in cairo 1.8.
84 _cairo_image_surface_is_size_valid (int width, int height)
86 return 0 <= width && width <= MAX_IMAGE_SIZE &&
87 0 <= height && height <= MAX_IMAGE_SIZE;
91 _cairo_format_from_pixman_format (pixman_format_code_t pixman_format)
93 switch (pixman_format) {
95 return CAIRO_FORMAT_ARGB32;
96 case PIXMAN_x2r10g10b10:
97 return CAIRO_FORMAT_RGB30;
99 return CAIRO_FORMAT_RGB24;
101 return CAIRO_FORMAT_A8;
103 return CAIRO_FORMAT_A1;
105 return CAIRO_FORMAT_RGB16_565;
106 case PIXMAN_r8g8b8a8: case PIXMAN_r8g8b8x8:
107 case PIXMAN_a8b8g8r8: case PIXMAN_x8b8g8r8: case PIXMAN_r8g8b8:
108 case PIXMAN_b8g8r8: case PIXMAN_b5g6r5:
109 case PIXMAN_a1r5g5b5: case PIXMAN_x1r5g5b5: case PIXMAN_a1b5g5r5:
110 case PIXMAN_x1b5g5r5: case PIXMAN_a4r4g4b4: case PIXMAN_x4r4g4b4:
111 case PIXMAN_a4b4g4r4: case PIXMAN_x4b4g4r4: case PIXMAN_r3g3b2:
112 case PIXMAN_b2g3r3: case PIXMAN_a2r2g2b2: case PIXMAN_a2b2g2r2:
113 case PIXMAN_c8: case PIXMAN_g8: case PIXMAN_x4a4:
114 case PIXMAN_a4: case PIXMAN_r1g2b1: case PIXMAN_b1g2r1:
115 case PIXMAN_a1r1g1b1: case PIXMAN_a1b1g1r1: case PIXMAN_c4:
116 case PIXMAN_g4: case PIXMAN_g1:
117 case PIXMAN_yuy2: case PIXMAN_yv12:
118 case PIXMAN_b8g8r8x8:
119 case PIXMAN_b8g8r8a8:
120 case PIXMAN_a2b10g10r10:
121 case PIXMAN_x2b10g10r10:
122 case PIXMAN_a2r10g10b10:
123 case PIXMAN_x14r6g6b6:
125 return CAIRO_FORMAT_INVALID;
128 return CAIRO_FORMAT_INVALID;
132 _cairo_content_from_pixman_format (pixman_format_code_t pixman_format)
134 cairo_content_t content;
137 if (PIXMAN_FORMAT_RGB (pixman_format))
138 content |= CAIRO_CONTENT_COLOR;
139 if (PIXMAN_FORMAT_A (pixman_format))
140 content |= CAIRO_CONTENT_ALPHA;
146 _cairo_image_surface_init (cairo_image_surface_t *surface,
147 pixman_image_t *pixman_image,
148 pixman_format_code_t pixman_format)
150 surface->pixman_image = pixman_image;
152 surface->pixman_format = pixman_format;
153 surface->format = _cairo_format_from_pixman_format (pixman_format);
154 surface->data = (uint8_t *) pixman_image_get_data (pixman_image);
155 surface->owns_data = FALSE;
156 surface->transparency = CAIRO_IMAGE_UNKNOWN;
157 surface->color = CAIRO_IMAGE_UNKNOWN_COLOR;
159 surface->width = pixman_image_get_width (pixman_image);
160 surface->height = pixman_image_get_height (pixman_image);
161 surface->stride = pixman_image_get_stride (pixman_image);
162 surface->depth = pixman_image_get_depth (pixman_image);
164 surface->base.is_clear = surface->width == 0 || surface->height == 0;
166 surface->compositor = _cairo_image_spans_compositor_get ();
170 _cairo_image_surface_create_for_pixman_image (pixman_image_t *pixman_image,
171 pixman_format_code_t pixman_format)
173 cairo_image_surface_t *surface;
175 surface = malloc (sizeof (cairo_image_surface_t));
176 if (unlikely (surface == NULL))
177 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
179 _cairo_surface_init (&surface->base,
180 &_cairo_image_surface_backend,
182 _cairo_content_from_pixman_format (pixman_format));
184 _cairo_image_surface_init (surface, pixman_image, pixman_format);
186 return &surface->base;
190 _pixman_format_from_masks (cairo_format_masks_t *masks,
191 pixman_format_code_t *format_ret)
193 pixman_format_code_t format;
196 cairo_format_masks_t format_masks;
198 a = _cairo_popcount (masks->alpha_mask);
199 r = _cairo_popcount (masks->red_mask);
200 g = _cairo_popcount (masks->green_mask);
201 b = _cairo_popcount (masks->blue_mask);
203 if (masks->red_mask) {
204 if (masks->red_mask > masks->blue_mask)
205 format_type = PIXMAN_TYPE_ARGB;
207 format_type = PIXMAN_TYPE_ABGR;
208 } else if (masks->alpha_mask) {
209 format_type = PIXMAN_TYPE_A;
214 format = PIXMAN_FORMAT (masks->bpp, format_type, a, r, g, b);
216 if (! pixman_format_supported_destination (format))
219 /* Sanity check that we got out of PIXMAN_FORMAT exactly what we
220 * expected. This avoid any problems from something bizarre like
221 * alpha in the least-significant bits, or insane channel order,
223 if (!_pixman_format_to_masks (format, &format_masks) ||
224 masks->bpp != format_masks.bpp ||
225 masks->red_mask != format_masks.red_mask ||
226 masks->green_mask != format_masks.green_mask ||
227 masks->blue_mask != format_masks.blue_mask)
232 *format_ret = format;
236 /* A mask consisting of N bits set to 1. */
237 #define MASK(N) ((1UL << (N))-1)
240 _pixman_format_to_masks (pixman_format_code_t format,
241 cairo_format_masks_t *masks)
245 masks->bpp = PIXMAN_FORMAT_BPP (format);
247 /* Number of bits in each channel */
248 a = PIXMAN_FORMAT_A (format);
249 r = PIXMAN_FORMAT_R (format);
250 g = PIXMAN_FORMAT_G (format);
251 b = PIXMAN_FORMAT_B (format);
253 switch (PIXMAN_FORMAT_TYPE (format)) {
254 case PIXMAN_TYPE_ARGB:
255 masks->alpha_mask = MASK (a) << (r + g + b);
256 masks->red_mask = MASK (r) << (g + b);
257 masks->green_mask = MASK (g) << (b);
258 masks->blue_mask = MASK (b);
260 case PIXMAN_TYPE_ABGR:
261 masks->alpha_mask = MASK (a) << (b + g + r);
262 masks->blue_mask = MASK (b) << (g + r);
263 masks->green_mask = MASK (g) << (r);
264 masks->red_mask = MASK (r);
266 #ifdef PIXMAN_TYPE_BGRA
267 case PIXMAN_TYPE_BGRA:
268 masks->blue_mask = MASK (b) << (masks->bpp - b);
269 masks->green_mask = MASK (g) << (masks->bpp - b - g);
270 masks->red_mask = MASK (r) << (masks->bpp - b - g - r);
271 masks->alpha_mask = MASK (a);
275 masks->alpha_mask = MASK (a);
277 masks->green_mask = 0;
278 masks->blue_mask = 0;
280 case PIXMAN_TYPE_OTHER:
281 case PIXMAN_TYPE_COLOR:
282 case PIXMAN_TYPE_GRAY:
283 case PIXMAN_TYPE_YUY2:
284 case PIXMAN_TYPE_YV12:
286 masks->alpha_mask = 0;
288 masks->green_mask = 0;
289 masks->blue_mask = 0;
295 _cairo_format_to_pixman_format_code (cairo_format_t format)
297 pixman_format_code_t ret;
299 case CAIRO_FORMAT_A1:
302 case CAIRO_FORMAT_A8:
305 case CAIRO_FORMAT_RGB24:
306 ret = PIXMAN_x8r8g8b8;
308 case CAIRO_FORMAT_RGB30:
309 ret = PIXMAN_x2r10g10b10;
311 case CAIRO_FORMAT_RGB16_565:
314 case CAIRO_FORMAT_ARGB32:
315 case CAIRO_FORMAT_INVALID:
317 ret = PIXMAN_a8r8g8b8;
324 _cairo_image_surface_create_with_pixman_format (unsigned char *data,
325 pixman_format_code_t pixman_format,
330 cairo_surface_t *surface;
331 pixman_image_t *pixman_image;
333 if (! _cairo_image_surface_is_size_valid (width, height))
335 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
338 pixman_image = pixman_image_create_bits (pixman_format, width, height,
339 (uint32_t *) data, stride);
341 if (unlikely (pixman_image == NULL))
342 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
344 surface = _cairo_image_surface_create_for_pixman_image (pixman_image,
346 if (unlikely (surface->status)) {
347 pixman_image_unref (pixman_image);
351 /* we can not make any assumptions about the initial state of user data */
352 surface->is_clear = data == NULL;
357 * cairo_image_surface_create:
358 * @format: format of pixels in the surface to create
359 * @width: width of the surface, in pixels
360 * @height: height of the surface, in pixels
362 * Creates an image surface of the specified format and
363 * dimensions. Initially the surface contents are all
364 * 0. (Specifically, within each pixel, each color or alpha channel
365 * belonging to format will be 0. The contents of bits within a pixel,
366 * but not belonging to the given format are undefined).
368 * Return value: a pointer to the newly created surface. The caller
369 * owns the surface and should call cairo_surface_destroy() when done
372 * This function always returns a valid pointer, but it will return a
373 * pointer to a "nil" surface if an error such as out of memory
374 * occurs. You can use cairo_surface_status() to check for this.
377 cairo_image_surface_create (cairo_format_t format,
381 pixman_format_code_t pixman_format;
383 if (! CAIRO_FORMAT_VALID (format))
384 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
386 pixman_format = _cairo_format_to_pixman_format_code (format);
388 return _cairo_image_surface_create_with_pixman_format (NULL, pixman_format,
391 slim_hidden_def (cairo_image_surface_create);
394 _cairo_image_surface_create_with_content (cairo_content_t content,
398 return cairo_image_surface_create (_cairo_format_from_content (content),
403 * cairo_format_stride_for_width:
404 * @format: A #cairo_format_t value
405 * @width: The desired width of an image surface to be created.
407 * This function provides a stride value that will respect all
408 * alignment requirements of the accelerated image-rendering code
409 * within cairo. Typical usage will be of the form:
411 * <informalexample><programlisting>
413 * unsigned char *data;
414 * #cairo_surface_t *surface;
416 * stride = cairo_format_stride_for_width (format, width);
417 * data = malloc (stride * height);
418 * surface = cairo_image_surface_create_for_data (data, format,
421 * </programlisting></informalexample>
423 * Return value: the appropriate stride to use given the desired
424 * format and width, or -1 if either the format is invalid or the width
430 cairo_format_stride_for_width (cairo_format_t format,
435 if (! CAIRO_FORMAT_VALID (format)) {
436 _cairo_error_throw (CAIRO_STATUS_INVALID_FORMAT);
440 bpp = _cairo_format_bits_per_pixel (format);
441 if ((unsigned) (width) >= (INT32_MAX - 7) / (unsigned) (bpp))
444 return CAIRO_STRIDE_FOR_WIDTH_BPP (width, bpp);
446 slim_hidden_def (cairo_format_stride_for_width);
449 * cairo_image_surface_create_for_data:
450 * @data: a pointer to a buffer supplied by the application in which
451 * to write contents. This pointer must be suitably aligned for any
452 * kind of variable, (for example, a pointer returned by malloc).
453 * @format: the format of pixels in the buffer
454 * @width: the width of the image to be stored in the buffer
455 * @height: the height of the image to be stored in the buffer
456 * @stride: the number of bytes between the start of rows in the
457 * buffer as allocated. This value should always be computed by
458 * cairo_format_stride_for_width() before allocating the data
461 * Creates an image surface for the provided pixel data. The output
462 * buffer must be kept around until the #cairo_surface_t is destroyed
463 * or cairo_surface_finish() is called on the surface. The initial
464 * contents of @data will be used as the initial image contents; you
465 * must explicitly clear the buffer, using, for example,
466 * cairo_rectangle() and cairo_fill() if you want it cleared.
468 * Note that the stride may be larger than
469 * width*bytes_per_pixel to provide proper alignment for each pixel
470 * and row. This alignment is required to allow high-performance rendering
471 * within cairo. The correct way to obtain a legal stride value is to
472 * call cairo_format_stride_for_width() with the desired format and
473 * maximum image width value, and then use the resulting stride value
474 * to allocate the data and to create the image surface. See
475 * cairo_format_stride_for_width() for example code.
477 * Return value: a pointer to the newly created surface. The caller
478 * owns the surface and should call cairo_surface_destroy() when done
481 * This function always returns a valid pointer, but it will return a
482 * pointer to a "nil" surface in the case of an error such as out of
483 * memory or an invalid stride value. In case of invalid stride value
484 * the error status of the returned surface will be
485 * %CAIRO_STATUS_INVALID_STRIDE. You can use
486 * cairo_surface_status() to check for this.
488 * See cairo_surface_set_user_data() for a means of attaching a
489 * destroy-notification fallback to the surface if necessary.
492 cairo_image_surface_create_for_data (unsigned char *data,
493 cairo_format_t format,
498 pixman_format_code_t pixman_format;
501 if (! CAIRO_FORMAT_VALID (format))
502 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
504 if ((stride & (CAIRO_STRIDE_ALIGNMENT-1)) != 0)
505 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
507 if (! _cairo_image_surface_is_size_valid (width, height))
508 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
510 minstride = cairo_format_stride_for_width (format, width);
512 if (stride > -minstride) {
513 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
516 if (stride < minstride) {
517 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
521 pixman_format = _cairo_format_to_pixman_format_code (format);
522 return _cairo_image_surface_create_with_pixman_format (data,
527 slim_hidden_def (cairo_image_surface_create_for_data);
530 * cairo_image_surface_get_data:
531 * @surface: a #cairo_image_surface_t
533 * Get a pointer to the data of the image surface, for direct
534 * inspection or modification.
536 * A call to cairo_surface_flush() is required before accessing the
537 * pixel data to ensure that all pending drawing operations are
538 * finished. A call to cairo_surface_mark_dirty() is required after
539 * the data is modified.
541 * Return value: a pointer to the image data of this surface or %NULL
542 * if @surface is not an image surface, or if cairo_surface_finish()
548 cairo_image_surface_get_data (cairo_surface_t *surface)
550 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
552 if (! _cairo_surface_is_image (surface)) {
553 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
557 return image_surface->data;
559 slim_hidden_def (cairo_image_surface_get_data);
562 * cairo_image_surface_get_format:
563 * @surface: a #cairo_image_surface_t
565 * Get the format of the surface.
567 * Return value: the format of the surface
572 cairo_image_surface_get_format (cairo_surface_t *surface)
574 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
576 if (! _cairo_surface_is_image (surface)) {
577 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
578 return CAIRO_FORMAT_INVALID;
581 return image_surface->format;
583 slim_hidden_def (cairo_image_surface_get_format);
586 * cairo_image_surface_get_width:
587 * @surface: a #cairo_image_surface_t
589 * Get the width of the image surface in pixels.
591 * Return value: the width of the surface in pixels.
594 cairo_image_surface_get_width (cairo_surface_t *surface)
596 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
598 if (! _cairo_surface_is_image (surface)) {
599 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
603 return image_surface->width;
605 slim_hidden_def (cairo_image_surface_get_width);
608 * cairo_image_surface_get_height:
609 * @surface: a #cairo_image_surface_t
611 * Get the height of the image surface in pixels.
613 * Return value: the height of the surface in pixels.
616 cairo_image_surface_get_height (cairo_surface_t *surface)
618 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
620 if (! _cairo_surface_is_image (surface)) {
621 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
625 return image_surface->height;
627 slim_hidden_def (cairo_image_surface_get_height);
630 * cairo_image_surface_get_stride:
631 * @surface: a #cairo_image_surface_t
633 * Get the stride of the image surface in bytes
635 * Return value: the stride of the image surface in bytes (or 0 if
636 * @surface is not an image surface). The stride is the distance in
637 * bytes from the beginning of one row of the image data to the
638 * beginning of the next row.
643 cairo_image_surface_get_stride (cairo_surface_t *surface)
646 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
648 if (! _cairo_surface_is_image (surface)) {
649 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
653 return image_surface->stride;
655 slim_hidden_def (cairo_image_surface_get_stride);
658 _cairo_format_from_content (cairo_content_t content)
661 case CAIRO_CONTENT_COLOR:
662 return CAIRO_FORMAT_RGB24;
663 case CAIRO_CONTENT_ALPHA:
664 return CAIRO_FORMAT_A8;
665 case CAIRO_CONTENT_COLOR_ALPHA:
666 return CAIRO_FORMAT_ARGB32;
670 return CAIRO_FORMAT_INVALID;
674 _cairo_content_from_format (cairo_format_t format)
677 case CAIRO_FORMAT_ARGB32:
678 return CAIRO_CONTENT_COLOR_ALPHA;
679 case CAIRO_FORMAT_RGB30:
680 return CAIRO_CONTENT_COLOR;
681 case CAIRO_FORMAT_RGB24:
682 return CAIRO_CONTENT_COLOR;
683 case CAIRO_FORMAT_RGB16_565:
684 return CAIRO_CONTENT_COLOR;
685 case CAIRO_FORMAT_A8:
686 case CAIRO_FORMAT_A1:
687 return CAIRO_CONTENT_ALPHA;
688 case CAIRO_FORMAT_INVALID:
693 return CAIRO_CONTENT_COLOR_ALPHA;
697 _cairo_format_bits_per_pixel (cairo_format_t format)
700 case CAIRO_FORMAT_ARGB32:
701 case CAIRO_FORMAT_RGB30:
702 case CAIRO_FORMAT_RGB24:
704 case CAIRO_FORMAT_RGB16_565:
706 case CAIRO_FORMAT_A8:
708 case CAIRO_FORMAT_A1:
710 case CAIRO_FORMAT_INVALID:
717 static cairo_surface_t *
718 _cairo_image_surface_create_similar (void *abstract_other,
719 cairo_content_t content,
723 cairo_image_surface_t *other = abstract_other;
725 if (! _cairo_image_surface_is_size_valid (width, height))
726 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
728 if (content == other->base.content) {
729 return _cairo_image_surface_create_with_pixman_format (NULL,
730 other->pixman_format,
735 return _cairo_image_surface_create_with_content (content,
740 _cairo_image_surface_snapshot (void *abstract_surface)
742 cairo_image_surface_t *image = abstract_surface;
743 cairo_image_surface_t *clone;
745 clone = (cairo_image_surface_t *)
746 _cairo_image_surface_create_with_pixman_format (NULL,
747 image->pixman_format,
751 if (unlikely (clone->base.status))
754 if (clone->stride == image->stride) {
755 memcpy (clone->data, image->data, clone->stride * clone->height);
757 pixman_image_composite32 (PIXMAN_OP_SRC,
758 image->pixman_image, NULL, clone->pixman_image,
762 image->width, image->height);
764 clone->base.is_clear = FALSE;
769 _cairo_image_surface_map_to_image (void *abstract_other,
770 const cairo_rectangle_int_t *extents)
772 cairo_image_surface_t *other = abstract_other;
773 cairo_surface_t *surface;
777 data += extents->y * other->stride;
778 data += extents->x * PIXMAN_FORMAT_BPP (other->pixman_format)/ 8;
781 _cairo_image_surface_create_with_pixman_format (data,
782 other->pixman_format,
787 cairo_surface_set_device_offset (surface, -extents->x, -extents->y);
792 _cairo_image_surface_unmap_image (void *abstract_surface,
793 cairo_image_surface_t *image)
795 return CAIRO_INT_STATUS_SUCCESS;
799 _cairo_image_surface_finish (void *abstract_surface)
801 cairo_image_surface_t *surface = abstract_surface;
803 if (surface->pixman_image) {
804 pixman_image_unref (surface->pixman_image);
805 surface->pixman_image = NULL;
808 if (surface->owns_data) {
809 free (surface->data);
810 surface->data = NULL;
813 return CAIRO_STATUS_SUCCESS;
817 _cairo_image_surface_assume_ownership_of_data (cairo_image_surface_t *surface)
819 surface->owns_data = TRUE;
823 _cairo_image_surface_acquire_source_image (void *abstract_surface,
824 cairo_image_surface_t **image_out,
827 *image_out = abstract_surface;
830 return CAIRO_STATUS_SUCCESS;
834 _cairo_image_surface_release_source_image (void *abstract_surface,
835 cairo_image_surface_t *image,
840 /* high level image interface */
842 _cairo_image_surface_get_extents (void *abstract_surface,
843 cairo_rectangle_int_t *rectangle)
845 cairo_image_surface_t *surface = abstract_surface;
849 rectangle->width = surface->width;
850 rectangle->height = surface->height;
855 static cairo_int_status_t
856 _cairo_image_surface_paint (void *abstract_surface,
858 const cairo_pattern_t *source,
859 const cairo_clip_t *clip)
861 cairo_image_surface_t *surface = abstract_surface;
862 return _cairo_compositor_paint (surface->compositor,
863 &surface->base, op, source, clip);
866 static cairo_int_status_t
867 _cairo_image_surface_mask (void *abstract_surface,
869 const cairo_pattern_t *source,
870 const cairo_pattern_t *mask,
871 const cairo_clip_t *clip)
873 cairo_image_surface_t *surface = abstract_surface;
874 return _cairo_compositor_mask (surface->compositor,
875 &surface->base, op, source, mask, clip);
878 static cairo_int_status_t
879 _cairo_image_surface_stroke (void *abstract_surface,
881 const cairo_pattern_t *source,
882 const cairo_path_fixed_t *path,
883 const cairo_stroke_style_t *style,
884 const cairo_matrix_t *ctm,
885 const cairo_matrix_t *ctm_inverse,
887 cairo_antialias_t antialias,
888 const cairo_clip_t *clip)
890 cairo_image_surface_t *surface = abstract_surface;
891 return _cairo_compositor_stroke (surface->compositor, &surface->base,
893 style, ctm, ctm_inverse,
894 tolerance, antialias, clip);
897 static cairo_int_status_t
898 _cairo_image_surface_fill (void *abstract_surface,
900 const cairo_pattern_t *source,
901 const cairo_path_fixed_t *path,
902 cairo_fill_rule_t fill_rule,
904 cairo_antialias_t antialias,
905 const cairo_clip_t *clip)
907 cairo_image_surface_t *surface = abstract_surface;
908 return _cairo_compositor_fill (surface->compositor, &surface->base,
910 fill_rule, tolerance, antialias,
914 static cairo_int_status_t
915 _cairo_image_surface_glyphs (void *abstract_surface,
917 const cairo_pattern_t *source,
918 cairo_glyph_t *glyphs,
920 cairo_scaled_font_t *scaled_font,
921 const cairo_clip_t *clip)
923 cairo_image_surface_t *surface = abstract_surface;
924 return _cairo_compositor_glyphs (surface->compositor, &surface->base,
926 glyphs, num_glyphs, scaled_font,
931 _cairo_image_surface_get_font_options (void *abstract_surface,
932 cairo_font_options_t *options)
934 _cairo_font_options_init_default (options);
936 cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
937 _cairo_font_options_set_round_glyph_positions (options, CAIRO_ROUND_GLYPH_POS_ON);
940 const cairo_surface_backend_t _cairo_image_surface_backend = {
941 CAIRO_SURFACE_TYPE_IMAGE,
942 _cairo_image_surface_finish,
944 _cairo_default_context_create,
946 _cairo_image_surface_create_similar,
947 NULL, /* create similar image */
948 _cairo_image_surface_map_to_image,
949 _cairo_image_surface_unmap_image,
951 _cairo_image_surface_acquire_source_image,
952 _cairo_image_surface_release_source_image,
953 _cairo_image_surface_snapshot,
955 NULL, /* copy_page */
956 NULL, /* show_page */
958 _cairo_image_surface_get_extents,
959 _cairo_image_surface_get_font_options,
962 NULL, /* mark dirty */
964 _cairo_image_surface_paint,
965 _cairo_image_surface_mask,
966 _cairo_image_surface_stroke,
967 _cairo_image_surface_fill,
968 NULL, /* fill-stroke */
969 _cairo_image_surface_glyphs,
972 /* A convenience function for when one needs to coerce an image
973 * surface to an alternate format. */
974 cairo_image_surface_t *
975 _cairo_image_surface_coerce (cairo_image_surface_t *surface)
977 return _cairo_image_surface_coerce_to_format (surface,
978 _cairo_format_from_content (surface->base.content));
981 /* A convenience function for when one needs to coerce an image
982 * surface to an alternate format. */
983 cairo_image_surface_t *
984 _cairo_image_surface_coerce_to_format (cairo_image_surface_t *surface,
985 cairo_format_t format)
987 cairo_image_surface_t *clone;
988 cairo_status_t status;
990 status = surface->base.status;
991 if (unlikely (status))
992 return (cairo_image_surface_t *)_cairo_surface_create_in_error (status);
994 if (surface->format == format)
995 return (cairo_image_surface_t *)cairo_surface_reference(&surface->base);
997 clone = (cairo_image_surface_t *)
998 cairo_image_surface_create (format, surface->width, surface->height);
999 if (unlikely (clone->base.status))
1002 pixman_image_composite32 (PIXMAN_OP_SRC,
1003 surface->pixman_image, NULL, clone->pixman_image,
1007 surface->width, surface->height);
1008 clone->base.is_clear = FALSE;
1010 clone->base.device_transform =
1011 surface->base.device_transform;
1012 clone->base.device_transform_inverse =
1013 surface->base.device_transform_inverse;
1018 cairo_image_transparency_t
1019 _cairo_image_analyze_transparency (cairo_image_surface_t *image)
1023 if (image->transparency != CAIRO_IMAGE_UNKNOWN)
1024 return image->transparency;
1026 if ((image->base.content & CAIRO_CONTENT_ALPHA) == 0)
1027 return image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1029 if (image->base.is_clear)
1030 return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1032 if ((image->base.content & CAIRO_CONTENT_COLOR) == 0) {
1033 if (image->format == CAIRO_FORMAT_A1) {
1034 return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1035 } else if (image->format == CAIRO_FORMAT_A8) {
1036 for (y = 0; y < image->height; y++) {
1037 uint8_t *alpha = (uint8_t *) (image->data + y * image->stride);
1039 for (x = 0; x < image->width; x++, alpha++) {
1040 if (*alpha > 0 && *alpha < 255)
1041 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1044 return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1046 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1050 if (image->format == CAIRO_FORMAT_RGB16_565) {
1051 image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1052 return CAIRO_IMAGE_IS_OPAQUE;
1055 if (image->format != CAIRO_FORMAT_ARGB32)
1056 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1058 image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1059 for (y = 0; y < image->height; y++) {
1060 uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1062 for (x = 0; x < image->width; x++, pixel++) {
1063 int a = (*pixel & 0xff000000) >> 24;
1064 if (a > 0 && a < 255) {
1065 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1066 } else if (a == 0) {
1067 image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1072 return image->transparency;
1076 _cairo_image_analyze_color (cairo_image_surface_t *image)
1080 if (image->color != CAIRO_IMAGE_UNKNOWN_COLOR)
1081 return image->color;
1083 if (image->format == CAIRO_FORMAT_A1 || image->format == CAIRO_FORMAT_A8)
1084 return image->color = CAIRO_IMAGE_IS_MONOCHROME;
1086 if (image->format == CAIRO_FORMAT_ARGB32) {
1087 image->color = CAIRO_IMAGE_IS_MONOCHROME;
1088 for (y = 0; y < image->height; y++) {
1089 uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1091 for (x = 0; x < image->width; x++, pixel++) {
1092 int a = (*pixel & 0xff000000) >> 24;
1093 int r = (*pixel & 0x00ff0000) >> 16;
1094 int g = (*pixel & 0x0000ff00) >> 8;
1095 int b = (*pixel & 0x000000ff);
1099 r = (r * 255 + a / 2) / a;
1100 g = (g * 255 + a / 2) / a;
1101 b = (b * 255 + a / 2) / a;
1103 if (!(r == g && g == b))
1104 return image->color = CAIRO_IMAGE_IS_COLOR;
1105 else if (r > 0 && r < 255)
1106 image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1109 return image->color;
1112 if (image->format == CAIRO_FORMAT_RGB24) {
1113 image->color = CAIRO_IMAGE_IS_MONOCHROME;
1114 for (y = 0; y < image->height; y++) {
1115 uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1117 for (x = 0; x < image->width; x++, pixel++) {
1118 int r = (*pixel & 0x00ff0000) >> 16;
1119 int g = (*pixel & 0x0000ff00) >> 8;
1120 int b = (*pixel & 0x000000ff);
1121 if (!(r == g && g == b))
1122 return image->color = CAIRO_IMAGE_IS_COLOR;
1123 else if (r > 0 && r < 255)
1124 image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1127 return image->color;
1130 return image->color = CAIRO_IMAGE_IS_COLOR;