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-inline.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->parent = NULL;
151 surface->pixman_image = pixman_image;
153 surface->pixman_format = pixman_format;
154 surface->format = _cairo_format_from_pixman_format (pixman_format);
155 surface->data = (uint8_t *) pixman_image_get_data (pixman_image);
156 surface->owns_data = FALSE;
157 surface->transparency = CAIRO_IMAGE_UNKNOWN;
158 surface->color = CAIRO_IMAGE_UNKNOWN_COLOR;
160 surface->width = pixman_image_get_width (pixman_image);
161 surface->height = pixman_image_get_height (pixman_image);
162 surface->stride = pixman_image_get_stride (pixman_image);
163 surface->depth = pixman_image_get_depth (pixman_image);
165 surface->base.is_clear = surface->width == 0 || surface->height == 0;
167 surface->compositor = _cairo_image_spans_compositor_get ();
171 _cairo_image_surface_create_for_pixman_image (pixman_image_t *pixman_image,
172 pixman_format_code_t pixman_format)
174 cairo_image_surface_t *surface;
176 surface = malloc (sizeof (cairo_image_surface_t));
177 if (unlikely (surface == NULL))
178 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
180 _cairo_surface_init (&surface->base,
181 &_cairo_image_surface_backend,
183 _cairo_content_from_pixman_format (pixman_format));
185 _cairo_image_surface_init (surface, pixman_image, pixman_format);
187 return &surface->base;
191 _pixman_format_from_masks (cairo_format_masks_t *masks,
192 pixman_format_code_t *format_ret)
194 pixman_format_code_t format;
197 cairo_format_masks_t format_masks;
199 a = _cairo_popcount (masks->alpha_mask);
200 r = _cairo_popcount (masks->red_mask);
201 g = _cairo_popcount (masks->green_mask);
202 b = _cairo_popcount (masks->blue_mask);
204 if (masks->red_mask) {
205 if (masks->red_mask > masks->blue_mask)
206 format_type = PIXMAN_TYPE_ARGB;
208 format_type = PIXMAN_TYPE_ABGR;
209 } else if (masks->alpha_mask) {
210 format_type = PIXMAN_TYPE_A;
215 format = PIXMAN_FORMAT (masks->bpp, format_type, a, r, g, b);
217 if (! pixman_format_supported_destination (format))
220 /* Sanity check that we got out of PIXMAN_FORMAT exactly what we
221 * expected. This avoid any problems from something bizarre like
222 * alpha in the least-significant bits, or insane channel order,
224 if (!_pixman_format_to_masks (format, &format_masks) ||
225 masks->bpp != format_masks.bpp ||
226 masks->red_mask != format_masks.red_mask ||
227 masks->green_mask != format_masks.green_mask ||
228 masks->blue_mask != format_masks.blue_mask)
233 *format_ret = format;
237 /* A mask consisting of N bits set to 1. */
238 #define MASK(N) ((1UL << (N))-1)
241 _pixman_format_to_masks (pixman_format_code_t format,
242 cairo_format_masks_t *masks)
246 masks->bpp = PIXMAN_FORMAT_BPP (format);
248 /* Number of bits in each channel */
249 a = PIXMAN_FORMAT_A (format);
250 r = PIXMAN_FORMAT_R (format);
251 g = PIXMAN_FORMAT_G (format);
252 b = PIXMAN_FORMAT_B (format);
254 switch (PIXMAN_FORMAT_TYPE (format)) {
255 case PIXMAN_TYPE_ARGB:
256 masks->alpha_mask = MASK (a) << (r + g + b);
257 masks->red_mask = MASK (r) << (g + b);
258 masks->green_mask = MASK (g) << (b);
259 masks->blue_mask = MASK (b);
261 case PIXMAN_TYPE_ABGR:
262 masks->alpha_mask = MASK (a) << (b + g + r);
263 masks->blue_mask = MASK (b) << (g + r);
264 masks->green_mask = MASK (g) << (r);
265 masks->red_mask = MASK (r);
267 #ifdef PIXMAN_TYPE_BGRA
268 case PIXMAN_TYPE_BGRA:
269 masks->blue_mask = MASK (b) << (masks->bpp - b);
270 masks->green_mask = MASK (g) << (masks->bpp - b - g);
271 masks->red_mask = MASK (r) << (masks->bpp - b - g - r);
272 masks->alpha_mask = MASK (a);
276 masks->alpha_mask = MASK (a);
278 masks->green_mask = 0;
279 masks->blue_mask = 0;
281 case PIXMAN_TYPE_OTHER:
282 case PIXMAN_TYPE_COLOR:
283 case PIXMAN_TYPE_GRAY:
284 case PIXMAN_TYPE_YUY2:
285 case PIXMAN_TYPE_YV12:
287 masks->alpha_mask = 0;
289 masks->green_mask = 0;
290 masks->blue_mask = 0;
296 _cairo_format_to_pixman_format_code (cairo_format_t format)
298 pixman_format_code_t ret;
300 case CAIRO_FORMAT_A1:
303 case CAIRO_FORMAT_A8:
306 case CAIRO_FORMAT_RGB24:
307 ret = PIXMAN_x8r8g8b8;
309 case CAIRO_FORMAT_RGB30:
310 ret = PIXMAN_x2r10g10b10;
312 case CAIRO_FORMAT_RGB16_565:
315 case CAIRO_FORMAT_ARGB32:
316 case CAIRO_FORMAT_INVALID:
318 ret = PIXMAN_a8r8g8b8;
325 _cairo_image_surface_create_with_pixman_format (unsigned char *data,
326 pixman_format_code_t pixman_format,
331 cairo_surface_t *surface;
332 pixman_image_t *pixman_image;
334 if (! _cairo_image_surface_is_size_valid (width, height))
336 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
339 pixman_image = pixman_image_create_bits (pixman_format, width, height,
340 (uint32_t *) data, stride);
342 if (unlikely (pixman_image == NULL))
343 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
345 surface = _cairo_image_surface_create_for_pixman_image (pixman_image,
347 if (unlikely (surface->status)) {
348 pixman_image_unref (pixman_image);
352 /* we can not make any assumptions about the initial state of user data */
353 surface->is_clear = data == NULL;
358 * cairo_image_surface_create:
359 * @format: format of pixels in the surface to create
360 * @width: width of the surface, in pixels
361 * @height: height of the surface, in pixels
363 * Creates an image surface of the specified format and
364 * dimensions. Initially the surface contents are all
365 * 0. (Specifically, within each pixel, each color or alpha channel
366 * belonging to format will be 0. The contents of bits within a pixel,
367 * but not belonging to the given format are undefined).
369 * Return value: a pointer to the newly created surface. The caller
370 * owns the surface and should call cairo_surface_destroy() when done
373 * This function always returns a valid pointer, but it will return a
374 * pointer to a "nil" surface if an error such as out of memory
375 * occurs. You can use cairo_surface_status() to check for this.
380 cairo_image_surface_create (cairo_format_t format,
384 pixman_format_code_t pixman_format;
386 if (! CAIRO_FORMAT_VALID (format))
387 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
389 pixman_format = _cairo_format_to_pixman_format_code (format);
391 return _cairo_image_surface_create_with_pixman_format (NULL, pixman_format,
394 slim_hidden_def (cairo_image_surface_create);
397 _cairo_image_surface_create_with_content (cairo_content_t content,
401 return cairo_image_surface_create (_cairo_format_from_content (content),
406 * cairo_format_stride_for_width:
407 * @format: A #cairo_format_t value
408 * @width: The desired width of an image surface to be created.
410 * This function provides a stride value that will respect all
411 * alignment requirements of the accelerated image-rendering code
412 * within cairo. Typical usage will be of the form:
414 * <informalexample><programlisting>
416 * unsigned char *data;
417 * #cairo_surface_t *surface;
419 * stride = cairo_format_stride_for_width (format, width);
420 * data = malloc (stride * height);
421 * surface = cairo_image_surface_create_for_data (data, format,
424 * </programlisting></informalexample>
426 * Return value: the appropriate stride to use given the desired
427 * format and width, or -1 if either the format is invalid or the width
433 cairo_format_stride_for_width (cairo_format_t format,
438 if (! CAIRO_FORMAT_VALID (format)) {
439 _cairo_error_throw (CAIRO_STATUS_INVALID_FORMAT);
443 bpp = _cairo_format_bits_per_pixel (format);
444 if ((unsigned) (width) >= (INT32_MAX - 7) / (unsigned) (bpp))
447 return CAIRO_STRIDE_FOR_WIDTH_BPP (width, bpp);
449 slim_hidden_def (cairo_format_stride_for_width);
452 * cairo_image_surface_create_for_data:
453 * @data: a pointer to a buffer supplied by the application in which
454 * to write contents. This pointer must be suitably aligned for any
455 * kind of variable, (for example, a pointer returned by malloc).
456 * @format: the format of pixels in the buffer
457 * @width: the width of the image to be stored in the buffer
458 * @height: the height of the image to be stored in the buffer
459 * @stride: the number of bytes between the start of rows in the
460 * buffer as allocated. This value should always be computed by
461 * cairo_format_stride_for_width() before allocating the data
464 * Creates an image surface for the provided pixel data. The output
465 * buffer must be kept around until the #cairo_surface_t is destroyed
466 * or cairo_surface_finish() is called on the surface. The initial
467 * contents of @data will be used as the initial image contents; you
468 * must explicitly clear the buffer, using, for example,
469 * cairo_rectangle() and cairo_fill() if you want it cleared.
471 * Note that the stride may be larger than
472 * width*bytes_per_pixel to provide proper alignment for each pixel
473 * and row. This alignment is required to allow high-performance rendering
474 * within cairo. The correct way to obtain a legal stride value is to
475 * call cairo_format_stride_for_width() with the desired format and
476 * maximum image width value, and then use the resulting stride value
477 * to allocate the data and to create the image surface. See
478 * cairo_format_stride_for_width() for example code.
480 * Return value: a pointer to the newly created surface. The caller
481 * owns the surface and should call cairo_surface_destroy() when done
484 * This function always returns a valid pointer, but it will return a
485 * pointer to a "nil" surface in the case of an error such as out of
486 * memory or an invalid stride value. In case of invalid stride value
487 * the error status of the returned surface will be
488 * %CAIRO_STATUS_INVALID_STRIDE. You can use
489 * cairo_surface_status() to check for this.
491 * See cairo_surface_set_user_data() for a means of attaching a
492 * destroy-notification fallback to the surface if necessary.
497 cairo_image_surface_create_for_data (unsigned char *data,
498 cairo_format_t format,
503 pixman_format_code_t pixman_format;
506 if (! CAIRO_FORMAT_VALID (format))
507 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
509 if ((stride & (CAIRO_STRIDE_ALIGNMENT-1)) != 0)
510 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
512 if (! _cairo_image_surface_is_size_valid (width, height))
513 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
515 minstride = cairo_format_stride_for_width (format, width);
517 if (stride > -minstride) {
518 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
521 if (stride < minstride) {
522 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
526 pixman_format = _cairo_format_to_pixman_format_code (format);
527 return _cairo_image_surface_create_with_pixman_format (data,
532 slim_hidden_def (cairo_image_surface_create_for_data);
535 * cairo_image_surface_get_data:
536 * @surface: a #cairo_image_surface_t
538 * Get a pointer to the data of the image surface, for direct
539 * inspection or modification.
541 * A call to cairo_surface_flush() is required before accessing the
542 * pixel data to ensure that all pending drawing operations are
543 * finished. A call to cairo_surface_mark_dirty() is required after
544 * the data is modified.
546 * Return value: a pointer to the image data of this surface or %NULL
547 * if @surface is not an image surface, or if cairo_surface_finish()
553 cairo_image_surface_get_data (cairo_surface_t *surface)
555 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
557 if (! _cairo_surface_is_image (surface)) {
558 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
562 return image_surface->data;
564 slim_hidden_def (cairo_image_surface_get_data);
567 * cairo_image_surface_get_format:
568 * @surface: a #cairo_image_surface_t
570 * Get the format of the surface.
572 * Return value: the format of the surface
577 cairo_image_surface_get_format (cairo_surface_t *surface)
579 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
581 if (! _cairo_surface_is_image (surface)) {
582 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
583 return CAIRO_FORMAT_INVALID;
586 return image_surface->format;
588 slim_hidden_def (cairo_image_surface_get_format);
591 * cairo_image_surface_get_width:
592 * @surface: a #cairo_image_surface_t
594 * Get the width of the image surface in pixels.
596 * Return value: the width of the surface in pixels.
601 cairo_image_surface_get_width (cairo_surface_t *surface)
603 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
605 if (! _cairo_surface_is_image (surface)) {
606 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
610 return image_surface->width;
612 slim_hidden_def (cairo_image_surface_get_width);
615 * cairo_image_surface_get_height:
616 * @surface: a #cairo_image_surface_t
618 * Get the height of the image surface in pixels.
620 * Return value: the height of the surface in pixels.
625 cairo_image_surface_get_height (cairo_surface_t *surface)
627 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
629 if (! _cairo_surface_is_image (surface)) {
630 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
634 return image_surface->height;
636 slim_hidden_def (cairo_image_surface_get_height);
639 * cairo_image_surface_get_stride:
640 * @surface: a #cairo_image_surface_t
642 * Get the stride of the image surface in bytes
644 * Return value: the stride of the image surface in bytes (or 0 if
645 * @surface is not an image surface). The stride is the distance in
646 * bytes from the beginning of one row of the image data to the
647 * beginning of the next row.
652 cairo_image_surface_get_stride (cairo_surface_t *surface)
655 cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
657 if (! _cairo_surface_is_image (surface)) {
658 _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
662 return image_surface->stride;
664 slim_hidden_def (cairo_image_surface_get_stride);
667 _cairo_format_from_content (cairo_content_t content)
670 case CAIRO_CONTENT_COLOR:
671 return CAIRO_FORMAT_RGB24;
672 case CAIRO_CONTENT_ALPHA:
673 return CAIRO_FORMAT_A8;
674 case CAIRO_CONTENT_COLOR_ALPHA:
675 return CAIRO_FORMAT_ARGB32;
679 return CAIRO_FORMAT_INVALID;
683 _cairo_content_from_format (cairo_format_t format)
686 case CAIRO_FORMAT_ARGB32:
687 return CAIRO_CONTENT_COLOR_ALPHA;
688 case CAIRO_FORMAT_RGB30:
689 return CAIRO_CONTENT_COLOR;
690 case CAIRO_FORMAT_RGB24:
691 return CAIRO_CONTENT_COLOR;
692 case CAIRO_FORMAT_RGB16_565:
693 return CAIRO_CONTENT_COLOR;
694 case CAIRO_FORMAT_A8:
695 case CAIRO_FORMAT_A1:
696 return CAIRO_CONTENT_ALPHA;
697 case CAIRO_FORMAT_INVALID:
702 return CAIRO_CONTENT_COLOR_ALPHA;
706 _cairo_format_bits_per_pixel (cairo_format_t format)
709 case CAIRO_FORMAT_ARGB32:
710 case CAIRO_FORMAT_RGB30:
711 case CAIRO_FORMAT_RGB24:
713 case CAIRO_FORMAT_RGB16_565:
715 case CAIRO_FORMAT_A8:
717 case CAIRO_FORMAT_A1:
719 case CAIRO_FORMAT_INVALID:
726 static cairo_surface_t *
727 _cairo_image_surface_create_similar (void *abstract_other,
728 cairo_content_t content,
732 cairo_image_surface_t *other = abstract_other;
734 TRACE ((stderr, "%s (other=%u)\n", __FUNCTION__, other->base.unique_id));
736 if (! _cairo_image_surface_is_size_valid (width, height))
737 return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
739 if (content == other->base.content) {
740 return _cairo_image_surface_create_with_pixman_format (NULL,
741 other->pixman_format,
746 return _cairo_image_surface_create_with_content (content,
751 _cairo_image_surface_snapshot (void *abstract_surface)
753 cairo_image_surface_t *image = abstract_surface;
754 cairo_image_surface_t *clone;
756 /* If we own the image, we can simply steal the memory for the snapshot */
757 if (image->owns_data && image->base._finishing) {
758 clone = (cairo_image_surface_t *)
759 _cairo_image_surface_create_for_pixman_image (image->pixman_image,
760 image->pixman_format);
761 if (unlikely (clone->base.status))
764 image->pixman_image = NULL;
765 image->owns_data = FALSE;
767 clone->transparency = image->transparency;
768 clone->color = image->color;
770 clone->owns_data = FALSE;
774 clone = (cairo_image_surface_t *)
775 _cairo_image_surface_create_with_pixman_format (NULL,
776 image->pixman_format,
780 if (unlikely (clone->base.status))
783 if (clone->stride == image->stride) {
784 memcpy (clone->data, image->data, clone->stride * clone->height);
786 pixman_image_composite32 (PIXMAN_OP_SRC,
787 image->pixman_image, NULL, clone->pixman_image,
791 image->width, image->height);
793 clone->base.is_clear = FALSE;
798 _cairo_image_surface_map_to_image (void *abstract_other,
799 const cairo_rectangle_int_t *extents)
801 cairo_image_surface_t *other = abstract_other;
802 cairo_surface_t *surface;
806 data += extents->y * other->stride;
807 data += extents->x * PIXMAN_FORMAT_BPP (other->pixman_format)/ 8;
810 _cairo_image_surface_create_with_pixman_format (data,
811 other->pixman_format,
816 cairo_surface_set_device_offset (surface, -extents->x, -extents->y);
821 _cairo_image_surface_unmap_image (void *abstract_surface,
822 cairo_image_surface_t *image)
824 return CAIRO_INT_STATUS_SUCCESS;
828 _cairo_image_surface_finish (void *abstract_surface)
830 cairo_image_surface_t *surface = abstract_surface;
832 if (surface->pixman_image) {
833 pixman_image_unref (surface->pixman_image);
834 surface->pixman_image = NULL;
837 if (surface->owns_data) {
838 free (surface->data);
839 surface->data = NULL;
842 if (surface->parent) {
843 cairo_surface_destroy (surface->parent);
844 surface->parent = NULL;
847 return CAIRO_STATUS_SUCCESS;
851 _cairo_image_surface_assume_ownership_of_data (cairo_image_surface_t *surface)
853 surface->owns_data = TRUE;
857 _cairo_image_surface_source (void *abstract_surface,
858 cairo_rectangle_int_t *extents)
860 cairo_image_surface_t *surface = abstract_surface;
863 extents->x = extents->y = 0;
864 extents->width = surface->width;
865 extents->height = surface->height;
868 return &surface->base;
872 _cairo_image_surface_acquire_source_image (void *abstract_surface,
873 cairo_image_surface_t **image_out,
876 *image_out = abstract_surface;
879 return CAIRO_STATUS_SUCCESS;
883 _cairo_image_surface_release_source_image (void *abstract_surface,
884 cairo_image_surface_t *image,
889 /* high level image interface */
891 _cairo_image_surface_get_extents (void *abstract_surface,
892 cairo_rectangle_int_t *rectangle)
894 cairo_image_surface_t *surface = abstract_surface;
898 rectangle->width = surface->width;
899 rectangle->height = surface->height;
905 _cairo_image_surface_paint (void *abstract_surface,
907 const cairo_pattern_t *source,
908 const cairo_clip_t *clip)
910 cairo_image_surface_t *surface = abstract_surface;
912 TRACE ((stderr, "%s (surface=%d)\n",
913 __FUNCTION__, surface->base.unique_id));
915 return _cairo_compositor_paint (surface->compositor,
916 &surface->base, op, source, clip);
920 _cairo_image_surface_mask (void *abstract_surface,
922 const cairo_pattern_t *source,
923 const cairo_pattern_t *mask,
924 const cairo_clip_t *clip)
926 cairo_image_surface_t *surface = abstract_surface;
928 TRACE ((stderr, "%s (surface=%d)\n",
929 __FUNCTION__, surface->base.unique_id));
931 return _cairo_compositor_mask (surface->compositor,
932 &surface->base, op, source, mask, clip);
936 _cairo_image_surface_stroke (void *abstract_surface,
938 const cairo_pattern_t *source,
939 const cairo_path_fixed_t *path,
940 const cairo_stroke_style_t *style,
941 const cairo_matrix_t *ctm,
942 const cairo_matrix_t *ctm_inverse,
944 cairo_antialias_t antialias,
945 const cairo_clip_t *clip)
947 cairo_image_surface_t *surface = abstract_surface;
949 TRACE ((stderr, "%s (surface=%d)\n",
950 __FUNCTION__, surface->base.unique_id));
952 return _cairo_compositor_stroke (surface->compositor, &surface->base,
954 style, ctm, ctm_inverse,
955 tolerance, antialias, clip);
959 _cairo_image_surface_fill (void *abstract_surface,
961 const cairo_pattern_t *source,
962 const cairo_path_fixed_t *path,
963 cairo_fill_rule_t fill_rule,
965 cairo_antialias_t antialias,
966 const cairo_clip_t *clip)
968 cairo_image_surface_t *surface = abstract_surface;
970 TRACE ((stderr, "%s (surface=%d)\n",
971 __FUNCTION__, surface->base.unique_id));
973 return _cairo_compositor_fill (surface->compositor, &surface->base,
975 fill_rule, tolerance, antialias,
980 _cairo_image_surface_glyphs (void *abstract_surface,
982 const cairo_pattern_t *source,
983 cairo_glyph_t *glyphs,
985 cairo_scaled_font_t *scaled_font,
986 const cairo_clip_t *clip)
988 cairo_image_surface_t *surface = abstract_surface;
990 TRACE ((stderr, "%s (surface=%d)\n",
991 __FUNCTION__, surface->base.unique_id));
993 return _cairo_compositor_glyphs (surface->compositor, &surface->base,
995 glyphs, num_glyphs, scaled_font,
1000 _cairo_image_surface_get_font_options (void *abstract_surface,
1001 cairo_font_options_t *options)
1003 _cairo_font_options_init_default (options);
1005 cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
1006 _cairo_font_options_set_round_glyph_positions (options, CAIRO_ROUND_GLYPH_POS_ON);
1009 const cairo_surface_backend_t _cairo_image_surface_backend = {
1010 CAIRO_SURFACE_TYPE_IMAGE,
1011 _cairo_image_surface_finish,
1013 _cairo_default_context_create,
1015 _cairo_image_surface_create_similar,
1016 NULL, /* create similar image */
1017 _cairo_image_surface_map_to_image,
1018 _cairo_image_surface_unmap_image,
1020 _cairo_image_surface_source,
1021 _cairo_image_surface_acquire_source_image,
1022 _cairo_image_surface_release_source_image,
1023 _cairo_image_surface_snapshot,
1025 NULL, /* copy_page */
1026 NULL, /* show_page */
1028 _cairo_image_surface_get_extents,
1029 _cairo_image_surface_get_font_options,
1034 _cairo_image_surface_paint,
1035 _cairo_image_surface_mask,
1036 _cairo_image_surface_stroke,
1037 _cairo_image_surface_fill,
1038 NULL, /* fill-stroke */
1039 _cairo_image_surface_glyphs,
1042 /* A convenience function for when one needs to coerce an image
1043 * surface to an alternate format. */
1044 cairo_image_surface_t *
1045 _cairo_image_surface_coerce (cairo_image_surface_t *surface)
1047 return _cairo_image_surface_coerce_to_format (surface,
1048 _cairo_format_from_content (surface->base.content));
1051 /* A convenience function for when one needs to coerce an image
1052 * surface to an alternate format. */
1053 cairo_image_surface_t *
1054 _cairo_image_surface_coerce_to_format (cairo_image_surface_t *surface,
1055 cairo_format_t format)
1057 cairo_image_surface_t *clone;
1058 cairo_status_t status;
1060 status = surface->base.status;
1061 if (unlikely (status))
1062 return (cairo_image_surface_t *)_cairo_surface_create_in_error (status);
1064 if (surface->format == format)
1065 return (cairo_image_surface_t *)cairo_surface_reference(&surface->base);
1067 clone = (cairo_image_surface_t *)
1068 cairo_image_surface_create (format, surface->width, surface->height);
1069 if (unlikely (clone->base.status))
1072 pixman_image_composite32 (PIXMAN_OP_SRC,
1073 surface->pixman_image, NULL, clone->pixman_image,
1077 surface->width, surface->height);
1078 clone->base.is_clear = FALSE;
1080 clone->base.device_transform =
1081 surface->base.device_transform;
1082 clone->base.device_transform_inverse =
1083 surface->base.device_transform_inverse;
1088 cairo_image_transparency_t
1089 _cairo_image_analyze_transparency (cairo_image_surface_t *image)
1093 if (image->transparency != CAIRO_IMAGE_UNKNOWN)
1094 return image->transparency;
1096 if ((image->base.content & CAIRO_CONTENT_ALPHA) == 0)
1097 return image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1099 if (image->base.is_clear)
1100 return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1102 if ((image->base.content & CAIRO_CONTENT_COLOR) == 0) {
1103 if (image->format == CAIRO_FORMAT_A1) {
1104 return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1105 } else if (image->format == CAIRO_FORMAT_A8) {
1106 for (y = 0; y < image->height; y++) {
1107 uint8_t *alpha = (uint8_t *) (image->data + y * image->stride);
1109 for (x = 0; x < image->width; x++, alpha++) {
1110 if (*alpha > 0 && *alpha < 255)
1111 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1114 return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1116 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1120 if (image->format == CAIRO_FORMAT_RGB16_565) {
1121 image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1122 return CAIRO_IMAGE_IS_OPAQUE;
1125 if (image->format != CAIRO_FORMAT_ARGB32)
1126 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1128 image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1129 for (y = 0; y < image->height; y++) {
1130 uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1132 for (x = 0; x < image->width; x++, pixel++) {
1133 int a = (*pixel & 0xff000000) >> 24;
1134 if (a > 0 && a < 255) {
1135 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1136 } else if (a == 0) {
1137 image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1142 return image->transparency;
1146 _cairo_image_analyze_color (cairo_image_surface_t *image)
1150 if (image->color != CAIRO_IMAGE_UNKNOWN_COLOR)
1151 return image->color;
1153 if (image->format == CAIRO_FORMAT_A1)
1154 return image->color = CAIRO_IMAGE_IS_MONOCHROME;
1156 if (image->format == CAIRO_FORMAT_A8)
1157 return image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1159 if (image->format == CAIRO_FORMAT_ARGB32) {
1160 image->color = CAIRO_IMAGE_IS_MONOCHROME;
1161 for (y = 0; y < image->height; y++) {
1162 uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1164 for (x = 0; x < image->width; x++, pixel++) {
1165 int a = (*pixel & 0xff000000) >> 24;
1166 int r = (*pixel & 0x00ff0000) >> 16;
1167 int g = (*pixel & 0x0000ff00) >> 8;
1168 int b = (*pixel & 0x000000ff);
1172 r = (r * 255 + a / 2) / a;
1173 g = (g * 255 + a / 2) / a;
1174 b = (b * 255 + a / 2) / a;
1176 if (!(r == g && g == b))
1177 return image->color = CAIRO_IMAGE_IS_COLOR;
1178 else if (r > 0 && r < 255)
1179 image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1182 return image->color;
1185 if (image->format == CAIRO_FORMAT_RGB24) {
1186 image->color = CAIRO_IMAGE_IS_MONOCHROME;
1187 for (y = 0; y < image->height; y++) {
1188 uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1190 for (x = 0; x < image->width; x++, pixel++) {
1191 int r = (*pixel & 0x00ff0000) >> 16;
1192 int g = (*pixel & 0x0000ff00) >> 8;
1193 int b = (*pixel & 0x000000ff);
1194 if (!(r == g && g == b))
1195 return image->color = CAIRO_IMAGE_IS_COLOR;
1196 else if (r > 0 && r < 255)
1197 image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1200 return image->color;
1203 return image->color = CAIRO_IMAGE_IS_COLOR;