tizen 2.3.1 release
[framework/graphics/cairo.git] / src / cairo-image-surface.c
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
3  *
4  * Copyright © 2003 University of Southern California
5  * Copyright © 2009,2010,2011 Intel Corporation
6  *
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.
14  *
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
20  *
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/
25  *
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.
29  *
30  * The Original Code is the cairo graphics library.
31  *
32  * The Initial Developer of the Original Code is University of Southern
33  * California.
34  *
35  * Contributor(s):
36  *      Carl D. Worth <cworth@cworth.org>
37  *      Chris Wilson <chris@chris-wilson.co.uk>
38  */
39
40 #include "cairoint.h"
41
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"
56 #include "cairo-surface-shadow-private.h"
57 #include "cairo-list-inline.h"
58
59 /* Limit on the width / height of an image surface in pixels.  This is
60  * mainly determined by coordinates of things sent to pixman at the
61  * moment being in 16.16 format. */
62 #define MAX_IMAGE_SIZE 32767
63
64 #define MAX_IMAGE_SHADOW_SIZE 256
65 #define MIN_IMAGE_SHADOW_SIZE 32
66
67 /**
68  * SECTION:cairo-image
69  * @Title: Image Surfaces
70  * @Short_Description: Rendering to memory buffers
71  * @See_Also: #cairo_surface_t
72  *
73  * Image surfaces provide the ability to render to memory buffers
74  * either allocated by cairo or by the calling code.  The supported
75  * image formats are those defined in #cairo_format_t.
76  **/
77
78 /**
79  * CAIRO_HAS_IMAGE_SURFACE:
80  *
81  * Defined if the image surface backend is available.
82  * The image surface backend is always built in.
83  * This macro was added for completeness in cairo 1.8.
84  *
85  * Since: 1.8
86  **/
87
88 static cairo_list_t shadow_caches;
89 static unsigned long shadow_caches_size = 0;
90 static cairo_recursive_mutex_t shadow_caches_mutex;
91 static unsigned shadow_caches_mutex_depth = 0;
92 static cairo_atomic_int_t shadow_caches_ref_count = 0;
93
94 static void
95 _cairo_image_shadow_caches_init (void)
96 {
97     _cairo_atomic_int_inc (&shadow_caches_ref_count);
98
99     if (shadow_caches_ref_count == 1)
100         cairo_list_init (&shadow_caches);
101
102     CAIRO_RECURSIVE_MUTEX_INIT (shadow_caches_mutex);
103 }
104
105 static void
106 _cairo_image_shadow_caches_destroy (void)
107 {
108     assert (shadow_caches_ref_count != 0);
109
110     if (! _cairo_atomic_int_dec_and_test (&shadow_caches_ref_count))
111         return;
112
113     if (shadow_caches_mutex_depth == 0) {
114         CAIRO_MUTEX_FINI (shadow_caches_mutex);
115
116         while (! cairo_list_is_empty (&shadow_caches)) {
117             cairo_shadow_cache_t *shadow;
118
119             shadow = cairo_list_first_entry (&shadow_caches,
120                                              cairo_shadow_cache_t,
121                                              link);
122             cairo_list_del (&shadow->link);
123             cairo_surface_destroy (shadow->surface);
124             free (shadow);
125         }
126         shadow_caches_size = 0;
127     }
128 }
129
130 static cairo_status_t
131 _cairo_image_surface_shadow_cache_acquire (void *abstract_surface)
132 {
133     cairo_image_surface_t *surface = abstract_surface;
134
135     if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
136         return CAIRO_STATUS_SURFACE_TYPE_MISMATCH;
137
138     if (unlikely (surface->base.status))
139         return surface->base.status;
140
141     CAIRO_MUTEX_LOCK (shadow_caches_mutex);
142     shadow_caches_mutex_depth++;
143
144     return CAIRO_STATUS_SUCCESS;
145 }
146
147 static void
148 _cairo_image_surface_shadow_cache_release (void *abstract_surface)
149 {
150     cairo_image_surface_t *surface = abstract_surface;
151
152     if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
153         return;
154
155     if (unlikely (surface->base.status))
156         return;
157
158     assert (shadow_caches_mutex_depth > 0);
159     shadow_caches_mutex_depth--;
160
161     CAIRO_MUTEX_UNLOCK (shadow_caches_mutex);
162 }
163
164 static cairo_list_t *
165 _cairo_image_surface_get_shadow_cache (void *abstract_surface)
166 {
167     cairo_image_surface_t *surface = abstract_surface;
168
169     if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
170         return NULL;
171
172     if (unlikely (surface->base.status))
173         return NULL;
174
175     return &shadow_caches;
176 }
177
178 static unsigned long *
179 _cairo_image_surface_get_shadow_cache_size (void *abstract_surface)
180 {
181     cairo_image_surface_t *surface = abstract_surface;
182
183     if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
184         return NULL;
185
186     if (unlikely (surface->base.status))
187         return NULL;
188
189     return &shadow_caches_size;
190 }
191
192 static cairo_bool_t
193 _cairo_image_surface_has_shadow_cache (void *abstract_surface)
194 {
195     cairo_image_surface_t *surface = abstract_surface;
196
197     if (! surface || surface->base.type != CAIRO_SURFACE_TYPE_IMAGE)
198         return FALSE;
199     return TRUE;
200 }
201
202 static cairo_bool_t
203 _cairo_image_surface_is_size_valid (int width, int height)
204 {
205     return 0 <= width  &&  width <= MAX_IMAGE_SIZE &&
206            0 <= height && height <= MAX_IMAGE_SIZE;
207 }
208
209 cairo_format_t
210 _cairo_format_from_pixman_format (pixman_format_code_t pixman_format)
211 {
212     switch (pixman_format) {
213     case PIXMAN_a8r8g8b8:
214         return CAIRO_FORMAT_ARGB32;
215     case PIXMAN_x2r10g10b10:
216         return CAIRO_FORMAT_RGB30;
217     case PIXMAN_x8r8g8b8:
218         return CAIRO_FORMAT_RGB24;
219     case PIXMAN_a8:
220         return CAIRO_FORMAT_A8;
221     case PIXMAN_a1:
222         return CAIRO_FORMAT_A1;
223     case PIXMAN_r5g6b5:
224         return CAIRO_FORMAT_RGB16_565;
225     case PIXMAN_r8g8b8a8: case PIXMAN_r8g8b8x8:
226     case PIXMAN_a8b8g8r8: case PIXMAN_x8b8g8r8: case PIXMAN_r8g8b8:
227     case PIXMAN_b8g8r8:   case PIXMAN_b5g6r5:
228     case PIXMAN_a1r5g5b5: case PIXMAN_x1r5g5b5: case PIXMAN_a1b5g5r5:
229     case PIXMAN_x1b5g5r5: case PIXMAN_a4r4g4b4: case PIXMAN_x4r4g4b4:
230     case PIXMAN_a4b4g4r4: case PIXMAN_x4b4g4r4: case PIXMAN_r3g3b2:
231     case PIXMAN_b2g3r3:   case PIXMAN_a2r2g2b2: case PIXMAN_a2b2g2r2:
232     case PIXMAN_c8:       case PIXMAN_g8:       case PIXMAN_x4a4:
233     case PIXMAN_a4:       case PIXMAN_r1g2b1:   case PIXMAN_b1g2r1:
234     case PIXMAN_a1r1g1b1: case PIXMAN_a1b1g1r1: case PIXMAN_c4:
235     case PIXMAN_g4:       case PIXMAN_g1:
236     case PIXMAN_yuy2:     case PIXMAN_yv12:
237     case PIXMAN_b8g8r8x8:
238     case PIXMAN_b8g8r8a8:
239     case PIXMAN_a2b10g10r10:
240     case PIXMAN_x2b10g10r10:
241     case PIXMAN_a2r10g10b10:
242     case PIXMAN_x14r6g6b6:
243     default:
244         return CAIRO_FORMAT_INVALID;
245     }
246
247     return CAIRO_FORMAT_INVALID;
248 }
249
250 cairo_content_t
251 _cairo_content_from_pixman_format (pixman_format_code_t pixman_format)
252 {
253     cairo_content_t content;
254
255     content = 0;
256     if (PIXMAN_FORMAT_RGB (pixman_format))
257         content |= CAIRO_CONTENT_COLOR;
258     if (PIXMAN_FORMAT_A (pixman_format))
259         content |= CAIRO_CONTENT_ALPHA;
260
261     return content;
262 }
263
264 void
265 _cairo_image_surface_init (cairo_image_surface_t *surface,
266                            pixman_image_t       *pixman_image,
267                            pixman_format_code_t  pixman_format)
268 {
269     surface->parent = NULL;
270     surface->pixman_image = pixman_image;
271
272     surface->pixman_format = pixman_format;
273     surface->format = _cairo_format_from_pixman_format (pixman_format);
274     surface->data = (uint8_t *) pixman_image_get_data (pixman_image);
275     surface->owns_data = FALSE;
276     surface->transparency = CAIRO_IMAGE_UNKNOWN;
277     surface->color = CAIRO_IMAGE_UNKNOWN_COLOR;
278
279     surface->width = pixman_image_get_width (pixman_image);
280     surface->height = pixman_image_get_height (pixman_image);
281     surface->stride = pixman_image_get_stride (pixman_image);
282     surface->depth = pixman_image_get_depth (pixman_image);
283
284     surface->base.is_clear = surface->width == 0 || surface->height == 0;
285
286     surface->compositor = _cairo_image_spans_compositor_get ();
287
288     _cairo_image_shadow_caches_init ();
289 }
290
291 cairo_surface_t *
292 _cairo_image_surface_create_for_pixman_image (pixman_image_t            *pixman_image,
293                                               pixman_format_code_t       pixman_format)
294 {
295     cairo_image_surface_t *surface;
296
297     surface = malloc (sizeof (cairo_image_surface_t));
298     if (unlikely (surface == NULL))
299         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
300
301     _cairo_surface_init (&surface->base,
302                          &_cairo_image_surface_backend,
303                          NULL, /* device */
304                          _cairo_content_from_pixman_format (pixman_format));
305
306     _cairo_image_surface_init (surface, pixman_image, pixman_format);
307
308     return &surface->base;
309 }
310
311 cairo_bool_t
312 _pixman_format_from_masks (cairo_format_masks_t *masks,
313                            pixman_format_code_t *format_ret)
314 {
315     pixman_format_code_t format;
316     int format_type;
317     int a, r, g, b;
318     cairo_format_masks_t format_masks;
319
320     a = _cairo_popcount (masks->alpha_mask);
321     r = _cairo_popcount (masks->red_mask);
322     g = _cairo_popcount (masks->green_mask);
323     b = _cairo_popcount (masks->blue_mask);
324
325     if (masks->red_mask) {
326         if (masks->red_mask > masks->blue_mask)
327             format_type = PIXMAN_TYPE_ARGB;
328         else
329             format_type = PIXMAN_TYPE_ABGR;
330     } else if (masks->alpha_mask) {
331         format_type = PIXMAN_TYPE_A;
332     } else {
333         return FALSE;
334     }
335
336     format = PIXMAN_FORMAT (masks->bpp, format_type, a, r, g, b);
337
338     if (! pixman_format_supported_destination (format))
339         return FALSE;
340
341     /* Sanity check that we got out of PIXMAN_FORMAT exactly what we
342      * expected. This avoid any problems from something bizarre like
343      * alpha in the least-significant bits, or insane channel order,
344      * or whatever. */
345      if (!_pixman_format_to_masks (format, &format_masks) ||
346          masks->bpp        != format_masks.bpp            ||
347          masks->red_mask   != format_masks.red_mask       ||
348          masks->green_mask != format_masks.green_mask     ||
349          masks->blue_mask  != format_masks.blue_mask)
350      {
351          return FALSE;
352      }
353
354     *format_ret = format;
355     return TRUE;
356 }
357
358 /* A mask consisting of N bits set to 1. */
359 #define MASK(N) ((1UL << (N))-1)
360
361 cairo_bool_t
362 _pixman_format_to_masks (pixman_format_code_t    format,
363                          cairo_format_masks_t   *masks)
364 {
365     int a, r, g, b;
366
367     masks->bpp = PIXMAN_FORMAT_BPP (format);
368
369     /* Number of bits in each channel */
370     a = PIXMAN_FORMAT_A (format);
371     r = PIXMAN_FORMAT_R (format);
372     g = PIXMAN_FORMAT_G (format);
373     b = PIXMAN_FORMAT_B (format);
374
375     switch (PIXMAN_FORMAT_TYPE (format)) {
376     case PIXMAN_TYPE_ARGB:
377         masks->alpha_mask = MASK (a) << (r + g + b);
378         masks->red_mask   = MASK (r) << (g + b);
379         masks->green_mask = MASK (g) << (b);
380         masks->blue_mask  = MASK (b);
381         return TRUE;
382     case PIXMAN_TYPE_ABGR:
383         masks->alpha_mask = MASK (a) << (b + g + r);
384         masks->blue_mask  = MASK (b) << (g + r);
385         masks->green_mask = MASK (g) << (r);
386         masks->red_mask   = MASK (r);
387         return TRUE;
388 #ifdef PIXMAN_TYPE_BGRA
389     case PIXMAN_TYPE_BGRA:
390         masks->blue_mask  = MASK (b) << (masks->bpp - b);
391         masks->green_mask = MASK (g) << (masks->bpp - b - g);
392         masks->red_mask   = MASK (r) << (masks->bpp - b - g - r);
393         masks->alpha_mask = MASK (a);
394         return TRUE;
395 #endif
396     case PIXMAN_TYPE_A:
397         masks->alpha_mask = MASK (a);
398         masks->red_mask   = 0;
399         masks->green_mask = 0;
400         masks->blue_mask  = 0;
401         return TRUE;
402     case PIXMAN_TYPE_OTHER:
403     case PIXMAN_TYPE_COLOR:
404     case PIXMAN_TYPE_GRAY:
405     case PIXMAN_TYPE_YUY2:
406     case PIXMAN_TYPE_YV12:
407     default:
408         masks->alpha_mask = 0;
409         masks->red_mask   = 0;
410         masks->green_mask = 0;
411         masks->blue_mask  = 0;
412         return FALSE;
413     }
414 }
415
416 pixman_format_code_t
417 _cairo_format_to_pixman_format_code (cairo_format_t format)
418 {
419     pixman_format_code_t ret;
420     switch (format) {
421     case CAIRO_FORMAT_A1:
422         ret = PIXMAN_a1;
423         break;
424     case CAIRO_FORMAT_A8:
425         ret = PIXMAN_a8;
426         break;
427     case CAIRO_FORMAT_RGB24:
428         ret = PIXMAN_x8r8g8b8;
429         break;
430     case CAIRO_FORMAT_RGB30:
431         ret = PIXMAN_x2r10g10b10;
432         break;
433     case CAIRO_FORMAT_RGB16_565:
434         ret = PIXMAN_r5g6b5;
435         break;
436     case CAIRO_FORMAT_ARGB32:
437     case CAIRO_FORMAT_INVALID:
438     default:
439         ret = PIXMAN_a8r8g8b8;
440         break;
441     }
442     return ret;
443 }
444
445 cairo_surface_t *
446 _cairo_image_surface_create_with_pixman_format (unsigned char           *data,
447                                                 pixman_format_code_t     pixman_format,
448                                                 int                      width,
449                                                 int                      height,
450                                                 int                      stride)
451 {
452     cairo_surface_t *surface;
453     pixman_image_t *pixman_image;
454
455     if (! _cairo_image_surface_is_size_valid (width, height))
456     {
457         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
458     }
459
460     pixman_image = pixman_image_create_bits (pixman_format, width, height,
461                                              (uint32_t *) data, stride);
462
463     if (unlikely (pixman_image == NULL))
464         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
465
466     surface = _cairo_image_surface_create_for_pixman_image (pixman_image,
467                                                             pixman_format);
468     if (unlikely (surface->status)) {
469         pixman_image_unref (pixman_image);
470         return surface;
471     }
472
473     /* we can not make any assumptions about the initial state of user data */
474     surface->is_clear = data == NULL;
475     return surface;
476 }
477
478 /**
479  * cairo_image_surface_create:
480  * @format: format of pixels in the surface to create
481  * @width: width of the surface, in pixels
482  * @height: height of the surface, in pixels
483  *
484  * Creates an image surface of the specified format and
485  * dimensions. Initially the surface contents are all
486  * 0. (Specifically, within each pixel, each color or alpha channel
487  * belonging to format will be 0. The contents of bits within a pixel,
488  * but not belonging to the given format are undefined).
489  *
490  * Return value: a pointer to the newly created surface. The caller
491  * owns the surface and should call cairo_surface_destroy() when done
492  * with it.
493  *
494  * This function always returns a valid pointer, but it will return a
495  * pointer to a "nil" surface if an error such as out of memory
496  * occurs. You can use cairo_surface_status() to check for this.
497  *
498  * Since: 1.0
499  **/
500 cairo_surface_t *
501 cairo_image_surface_create (cairo_format_t      format,
502                             int                 width,
503                             int                 height)
504 {
505     pixman_format_code_t pixman_format;
506
507     if (! CAIRO_FORMAT_VALID (format))
508         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
509
510     pixman_format = _cairo_format_to_pixman_format_code (format);
511
512     return _cairo_image_surface_create_with_pixman_format (NULL, pixman_format,
513                                                            width, height, -1);
514 }
515 slim_hidden_def (cairo_image_surface_create);
516
517     cairo_surface_t *
518 _cairo_image_surface_create_with_content (cairo_content_t       content,
519                                           int                   width,
520                                           int                   height)
521 {
522     return cairo_image_surface_create (_cairo_format_from_content (content),
523                                        width, height);
524 }
525
526 /**
527  * cairo_format_stride_for_width:
528  * @format: A #cairo_format_t value
529  * @width: The desired width of an image surface to be created.
530  *
531  * This function provides a stride value that will respect all
532  * alignment requirements of the accelerated image-rendering code
533  * within cairo. Typical usage will be of the form:
534  *
535  * <informalexample><programlisting>
536  * int stride;
537  * unsigned char *data;
538  * #cairo_surface_t *surface;
539  *
540  * stride = cairo_format_stride_for_width (format, width);
541  * data = malloc (stride * height);
542  * surface = cairo_image_surface_create_for_data (data, format,
543  *                                                width, height,
544  *                                                stride);
545  * </programlisting></informalexample>
546  *
547  * Return value: the appropriate stride to use given the desired
548  * format and width, or -1 if either the format is invalid or the width
549  * too large.
550  *
551  * Since: 1.6
552  **/
553     int
554 cairo_format_stride_for_width (cairo_format_t   format,
555                                int              width)
556 {
557     int bpp;
558
559     if (! CAIRO_FORMAT_VALID (format)) {
560         _cairo_error_throw (CAIRO_STATUS_INVALID_FORMAT);
561         return -1;
562     }
563
564     bpp = _cairo_format_bits_per_pixel (format);
565     if ((unsigned) (width) >= (INT32_MAX - 7) / (unsigned) (bpp))
566         return -1;
567
568     return CAIRO_STRIDE_FOR_WIDTH_BPP (width, bpp);
569 }
570 slim_hidden_def (cairo_format_stride_for_width);
571
572 /**
573  * cairo_image_surface_create_for_data:
574  * @data: a pointer to a buffer supplied by the application in which
575  *     to write contents. This pointer must be suitably aligned for any
576  *     kind of variable, (for example, a pointer returned by malloc).
577  * @format: the format of pixels in the buffer
578  * @width: the width of the image to be stored in the buffer
579  * @height: the height of the image to be stored in the buffer
580  * @stride: the number of bytes between the start of rows in the
581  *     buffer as allocated. This value should always be computed by
582  *     cairo_format_stride_for_width() before allocating the data
583  *     buffer.
584  *
585  * Creates an image surface for the provided pixel data. The output
586  * buffer must be kept around until the #cairo_surface_t is destroyed
587  * or cairo_surface_finish() is called on the surface.  The initial
588  * contents of @data will be used as the initial image contents; you
589  * must explicitly clear the buffer, using, for example,
590  * cairo_rectangle() and cairo_fill() if you want it cleared.
591  *
592  * Note that the stride may be larger than
593  * width*bytes_per_pixel to provide proper alignment for each pixel
594  * and row. This alignment is required to allow high-performance rendering
595  * within cairo. The correct way to obtain a legal stride value is to
596  * call cairo_format_stride_for_width() with the desired format and
597  * maximum image width value, and then use the resulting stride value
598  * to allocate the data and to create the image surface. See
599  * cairo_format_stride_for_width() for example code.
600  *
601  * Return value: a pointer to the newly created surface. The caller
602  * owns the surface and should call cairo_surface_destroy() when done
603  * with it.
604  *
605  * This function always returns a valid pointer, but it will return a
606  * pointer to a "nil" surface in the case of an error such as out of
607  * memory or an invalid stride value. In case of invalid stride value
608  * the error status of the returned surface will be
609  * %CAIRO_STATUS_INVALID_STRIDE.  You can use
610  * cairo_surface_status() to check for this.
611  *
612  * See cairo_surface_set_user_data() for a means of attaching a
613  * destroy-notification fallback to the surface if necessary.
614  *
615  * Since: 1.0
616  **/
617     cairo_surface_t *
618 cairo_image_surface_create_for_data (unsigned char     *data,
619                                      cairo_format_t     format,
620                                      int                width,
621                                      int                height,
622                                      int                stride)
623 {
624     pixman_format_code_t pixman_format;
625     int minstride;
626
627     if (! CAIRO_FORMAT_VALID (format))
628         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
629
630     if ((stride & (CAIRO_STRIDE_ALIGNMENT-1)) != 0)
631         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
632
633     if (! _cairo_image_surface_is_size_valid (width, height))
634         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
635
636     minstride = cairo_format_stride_for_width (format, width);
637     if (stride < 0) {
638         if (stride > -minstride) {
639             return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
640         }
641     } else {
642         if (stride < minstride) {
643             return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
644         }
645     }
646
647     pixman_format = _cairo_format_to_pixman_format_code (format);
648     return _cairo_image_surface_create_with_pixman_format (data,
649                                                            pixman_format,
650                                                            width, height,
651                                                            stride);
652 }
653 slim_hidden_def (cairo_image_surface_create_for_data);
654
655 /**
656  * cairo_image_surface_get_data:
657  * @surface: a #cairo_image_surface_t
658  *
659  * Get a pointer to the data of the image surface, for direct
660  * inspection or modification.
661  *
662  * A call to cairo_surface_flush() is required before accessing the
663  * pixel data to ensure that all pending drawing operations are
664  * finished. A call to cairo_surface_mark_dirty() is required after
665  * the data is modified.
666  *
667  * Return value: a pointer to the image data of this surface or %NULL
668  * if @surface is not an image surface, or if cairo_surface_finish()
669  * has been called.
670  *
671  * Since: 1.2
672  **/
673 unsigned char *
674 cairo_image_surface_get_data (cairo_surface_t *surface)
675 {
676     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
677
678     if (! _cairo_surface_is_image (surface)) {
679         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
680         return NULL;
681     }
682
683     return image_surface->data;
684 }
685 slim_hidden_def (cairo_image_surface_get_data);
686
687 /**
688  * cairo_image_surface_get_format:
689  * @surface: a #cairo_image_surface_t
690  *
691  * Get the format of the surface.
692  *
693  * Return value: the format of the surface
694  *
695  * Since: 1.2
696  **/
697 cairo_format_t
698 cairo_image_surface_get_format (cairo_surface_t *surface)
699 {
700     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
701
702     if (! _cairo_surface_is_image (surface)) {
703         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
704         return CAIRO_FORMAT_INVALID;
705     }
706
707     return image_surface->format;
708 }
709 slim_hidden_def (cairo_image_surface_get_format);
710
711 /**
712  * cairo_image_surface_get_width:
713  * @surface: a #cairo_image_surface_t
714  *
715  * Get the width of the image surface in pixels.
716  *
717  * Return value: the width of the surface in pixels.
718  *
719  * Since: 1.0
720  **/
721 int
722 cairo_image_surface_get_width (cairo_surface_t *surface)
723 {
724     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
725
726     if (! _cairo_surface_is_image (surface)) {
727         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
728         return 0;
729     }
730
731     return image_surface->width;
732 }
733 slim_hidden_def (cairo_image_surface_get_width);
734
735 /**
736  * cairo_image_surface_get_height:
737  * @surface: a #cairo_image_surface_t
738  *
739  * Get the height of the image surface in pixels.
740  *
741  * Return value: the height of the surface in pixels.
742  *
743  * Since: 1.0
744  **/
745 int
746 cairo_image_surface_get_height (cairo_surface_t *surface)
747 {
748     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
749
750     if (! _cairo_surface_is_image (surface)) {
751         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
752         return 0;
753     }
754
755     return image_surface->height;
756 }
757 slim_hidden_def (cairo_image_surface_get_height);
758
759 /**
760  * cairo_image_surface_get_stride:
761  * @surface: a #cairo_image_surface_t
762  *
763  * Get the stride of the image surface in bytes
764  *
765  * Return value: the stride of the image surface in bytes (or 0 if
766  * @surface is not an image surface). The stride is the distance in
767  * bytes from the beginning of one row of the image data to the
768  * beginning of the next row.
769  *
770  * Since: 1.2
771  **/
772 int
773 cairo_image_surface_get_stride (cairo_surface_t *surface)
774 {
775
776     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
777
778     if (! _cairo_surface_is_image (surface)) {
779         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
780         return 0;
781     }
782
783     return image_surface->stride;
784 }
785 slim_hidden_def (cairo_image_surface_get_stride);
786
787     cairo_format_t
788 _cairo_format_from_content (cairo_content_t content)
789 {
790     switch (content) {
791     case CAIRO_CONTENT_COLOR:
792         return CAIRO_FORMAT_RGB24;
793     case CAIRO_CONTENT_ALPHA:
794         return CAIRO_FORMAT_A8;
795     case CAIRO_CONTENT_COLOR_ALPHA:
796         return CAIRO_FORMAT_ARGB32;
797     }
798
799     ASSERT_NOT_REACHED;
800     return CAIRO_FORMAT_INVALID;
801 }
802
803     cairo_content_t
804 _cairo_content_from_format (cairo_format_t format)
805 {
806     switch (format) {
807     case CAIRO_FORMAT_ARGB32:
808         return CAIRO_CONTENT_COLOR_ALPHA;
809     case CAIRO_FORMAT_RGB30:
810         return CAIRO_CONTENT_COLOR;
811     case CAIRO_FORMAT_RGB24:
812         return CAIRO_CONTENT_COLOR;
813     case CAIRO_FORMAT_RGB16_565:
814         return CAIRO_CONTENT_COLOR;
815     case CAIRO_FORMAT_A8:
816     case CAIRO_FORMAT_A1:
817         return CAIRO_CONTENT_ALPHA;
818     case CAIRO_FORMAT_INVALID:
819         break;
820     }
821
822     ASSERT_NOT_REACHED;
823     return CAIRO_CONTENT_COLOR_ALPHA;
824 }
825
826     int
827 _cairo_format_bits_per_pixel (cairo_format_t format)
828 {
829     switch (format) {
830     case CAIRO_FORMAT_ARGB32:
831     case CAIRO_FORMAT_RGB30:
832     case CAIRO_FORMAT_RGB24:
833         return 32;
834     case CAIRO_FORMAT_RGB16_565:
835         return 16;
836     case CAIRO_FORMAT_A8:
837         return 8;
838     case CAIRO_FORMAT_A1:
839         return 1;
840     case CAIRO_FORMAT_INVALID:
841     default:
842         ASSERT_NOT_REACHED;
843         return 0;
844     }
845 }
846
847 cairo_surface_t *
848 _cairo_image_surface_create_similar (void              *abstract_other,
849                                      cairo_content_t    content,
850                                      int                width,
851                                      int                height)
852 {
853     cairo_image_surface_t *other = abstract_other;
854
855     TRACE ((stderr, "%s (other=%u)\n", __FUNCTION__, other->base.unique_id));
856
857     if (! _cairo_image_surface_is_size_valid (width, height))
858         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
859
860     if (content == other->base.content) {
861         return _cairo_image_surface_create_with_pixman_format (NULL,
862                                                                other->pixman_format,
863                                                                width, height,
864                                                                0);
865     }
866
867     return _cairo_image_surface_create_with_content (content,
868                                                      width, height);
869 }
870
871 cairo_surface_t *
872 _cairo_image_surface_snapshot (void *abstract_surface)
873 {
874     cairo_image_surface_t *image = abstract_surface;
875     cairo_image_surface_t *clone;
876
877     /* If we own the image, we can simply steal the memory for the snapshot */
878     if (image->owns_data && image->base._finishing) {
879         clone = (cairo_image_surface_t *)
880             _cairo_image_surface_create_for_pixman_image (image->pixman_image,
881                                                           image->pixman_format);
882         if (unlikely (clone->base.status))
883             return &clone->base;
884
885         image->pixman_image = NULL;
886
887         clone->transparency = image->transparency;
888         clone->color = image->color;
889
890         clone->owns_data = image->owns_data;
891         image->owns_data = FALSE;
892         return &clone->base;
893     }
894
895     clone = (cairo_image_surface_t *)
896         _cairo_image_surface_create_with_pixman_format (NULL,
897                                                         image->pixman_format,
898                                                         image->width,
899                                                         image->height,
900                                                         0);
901     if (unlikely (clone->base.status))
902         return &clone->base;
903
904     if (clone->stride == image->stride) {
905         memcpy (clone->data, image->data, clone->stride * clone->height);
906     } else {
907         pixman_image_composite32 (PIXMAN_OP_SRC,
908                                   image->pixman_image, NULL, clone->pixman_image,
909                                   0, 0,
910                                   0, 0,
911                                   0, 0,
912                                   image->width, image->height);
913     }
914     clone->base.is_clear = FALSE;
915     return &clone->base;
916 }
917
918 cairo_image_surface_t *
919 _cairo_image_surface_map_to_image (void *abstract_other,
920                                    const cairo_rectangle_int_t *extents)
921 {
922     cairo_image_surface_t *other = abstract_other;
923     cairo_surface_t *surface;
924     uint8_t *data;
925
926     data = other->data;
927     data += extents->y * other->stride;
928     data += extents->x * PIXMAN_FORMAT_BPP (other->pixman_format)/ 8;
929
930     surface =
931         _cairo_image_surface_create_with_pixman_format (data,
932                                                         other->pixman_format,
933                                                         extents->width,
934                                                         extents->height,
935                                                         other->stride);
936
937     cairo_surface_set_device_offset (surface, -extents->x, -extents->y);
938     return (cairo_image_surface_t *) surface;
939 }
940
941 cairo_int_status_t
942 _cairo_image_surface_unmap_image (void *abstract_surface,
943                                   cairo_image_surface_t *image)
944 {
945     cairo_surface_finish (&image->base);
946     cairo_surface_destroy (&image->base);
947
948     return CAIRO_INT_STATUS_SUCCESS;
949 }
950
951 cairo_status_t
952 _cairo_image_surface_finish (void *abstract_surface)
953 {
954     cairo_image_surface_t *surface = abstract_surface;
955
956     if (surface->pixman_image) {
957         pixman_image_unref (surface->pixman_image);
958         surface->pixman_image = NULL;
959     }
960
961     if (surface->owns_data) {
962         free (surface->data);
963         surface->data = NULL;
964     }
965
966     if (surface->parent) {
967         cairo_surface_destroy (surface->parent);
968         surface->parent = NULL;
969     }
970
971     _cairo_image_shadow_caches_destroy ();
972
973     return CAIRO_STATUS_SUCCESS;
974 }
975
976 void
977 _cairo_image_surface_assume_ownership_of_data (cairo_image_surface_t *surface)
978 {
979     surface->owns_data = TRUE;
980 }
981
982 cairo_surface_t *
983 _cairo_image_surface_source (void                       *abstract_surface,
984                              cairo_rectangle_int_t      *extents)
985 {
986     cairo_image_surface_t *surface = abstract_surface;
987
988     if (extents) {
989         extents->x = extents->y = 0;
990         extents->width = surface->width;
991         extents->height = surface->height;
992     }
993
994     return &surface->base;
995 }
996
997 cairo_status_t
998 _cairo_image_surface_acquire_source_image (void                    *abstract_surface,
999                                            cairo_image_surface_t  **image_out,
1000                                            void                   **image_extra)
1001 {
1002     *image_out = abstract_surface;
1003     *image_extra = NULL;
1004
1005     return CAIRO_STATUS_SUCCESS;
1006 }
1007
1008 void
1009 _cairo_image_surface_release_source_image (void                   *abstract_surface,
1010                                            cairo_image_surface_t  *image,
1011                                            void                   *image_extra)
1012 {
1013 }
1014
1015 /* high level image interface */
1016 cairo_bool_t
1017 _cairo_image_surface_get_extents (void                    *abstract_surface,
1018                                   cairo_rectangle_int_t   *rectangle)
1019 {
1020     cairo_image_surface_t *surface = abstract_surface;
1021
1022     rectangle->x = 0;
1023     rectangle->y = 0;
1024     rectangle->width  = surface->width;
1025     rectangle->height = surface->height;
1026
1027     return TRUE;
1028 }
1029
1030 cairo_int_status_t
1031 _cairo_image_surface_paint (void                        *abstract_surface,
1032                             cairo_operator_t             op,
1033                             const cairo_pattern_t       *source,
1034                             const cairo_clip_t          *clip)
1035 {
1036     cairo_image_surface_t *surface = abstract_surface;
1037     cairo_int_status_t status;
1038
1039     TRACE ((stderr, "%s (surface=%d)\n",
1040             __FUNCTION__, surface->base.unique_id));
1041
1042     status = cairo_device_acquire (surface->base.device);
1043     if (unlikely (status))
1044         return status;
1045
1046     status = _cairo_surface_shadow_paint (abstract_surface, op, source,
1047                                           clip, &source->shadow);
1048
1049     if (unlikely (status)) {
1050         cairo_device_release (surface->base.device);
1051         return status;
1052     }
1053
1054     if (source->shadow.draw_shadow_only) {
1055         cairo_device_release (surface->base.device);
1056         return status;
1057     }
1058
1059     status = _cairo_compositor_paint (surface->compositor,
1060                                       &surface->base, op, source, clip);
1061     cairo_device_release (surface->base.device);
1062     return status;
1063 }
1064
1065 cairo_int_status_t
1066 _cairo_image_surface_mask (void                         *abstract_surface,
1067                            cairo_operator_t              op,
1068                            const cairo_pattern_t        *source,
1069                            const cairo_pattern_t        *mask,
1070                            const cairo_clip_t           *clip)
1071 {
1072     cairo_image_surface_t *surface = abstract_surface;
1073     cairo_int_status_t status;
1074
1075     TRACE ((stderr, "%s (surface=%d)\n",
1076             __FUNCTION__, surface->base.unique_id));
1077
1078     status = cairo_device_acquire (surface->base.device);
1079     if (unlikely (status))
1080         return status;
1081
1082     status = _cairo_surface_shadow_mask (abstract_surface, op, source,
1083                                          mask, clip, &source->shadow);
1084
1085     if (unlikely (status)) {
1086         cairo_device_release (surface->base.device);
1087         return status;
1088     }
1089
1090     if (source->shadow.draw_shadow_only) {
1091         cairo_device_release (surface->base.device);
1092         return status;
1093     }
1094
1095     status = _cairo_compositor_mask (surface->compositor,
1096                                      &surface->base, op, source, mask, clip);
1097     cairo_device_release (surface->base.device);
1098     return status;
1099 }
1100
1101 cairo_int_status_t
1102 _cairo_image_surface_stroke (void                       *abstract_surface,
1103                              cairo_operator_t            op,
1104                              const cairo_pattern_t      *source,
1105                              const cairo_path_fixed_t   *path,
1106                              const cairo_stroke_style_t *style,
1107                              const cairo_matrix_t       *ctm,
1108                              const cairo_matrix_t       *ctm_inverse,
1109                              double                      tolerance,
1110                              cairo_antialias_t           antialias,
1111                              const cairo_clip_t         *clip)
1112 {
1113     cairo_int_status_t status;
1114     cairo_image_surface_t *surface = abstract_surface;
1115     cairo_shadow_type_t shadow_type = source->shadow.type;
1116
1117     TRACE ((stderr, "%s (surface=%d)\n",
1118             __FUNCTION__, surface->base.unique_id));
1119
1120     status = cairo_device_acquire (surface->base.device);
1121     if (unlikely (status))
1122         return status;
1123
1124     if (shadow_type != CAIRO_SHADOW_INSET)
1125         status = _cairo_surface_shadow_stroke (abstract_surface, op, source,
1126                                                path, style, ctm, ctm_inverse,
1127                                                tolerance, antialias, clip,
1128                                                &source->shadow);
1129
1130     if (unlikely (status)) {
1131         cairo_device_release (surface->base.device);
1132         return status;
1133     }
1134
1135     if (shadow_type == CAIRO_SHADOW_DROP &&
1136         source->shadow.draw_shadow_only) {
1137         cairo_device_release (surface->base.device);
1138         return status;
1139     }
1140
1141     if (! source->shadow.draw_shadow_only)
1142         status = _cairo_compositor_stroke (surface->compositor, &surface->base,
1143                                            op, source, path,
1144                                            style, ctm, ctm_inverse,
1145                                            tolerance, antialias, clip);
1146
1147     if (unlikely (status)) {
1148         cairo_device_release (surface->base.device);
1149         return status;
1150     }
1151
1152     if (shadow_type == CAIRO_SHADOW_INSET)
1153         status = _cairo_surface_shadow_stroke (abstract_surface, op, source,
1154                                                path, style, ctm, ctm_inverse,
1155                                                tolerance, antialias, clip,
1156                                                &source->shadow);
1157
1158     cairo_device_release (surface->base.device);
1159     return status;
1160 }
1161
1162 cairo_int_status_t
1163 _cairo_image_surface_fill (void                         *abstract_surface,
1164                            cairo_operator_t              op,
1165                            const cairo_pattern_t        *source,
1166                            const cairo_path_fixed_t     *path,
1167                            cairo_fill_rule_t             fill_rule,
1168                            double                        tolerance,
1169                            cairo_antialias_t             antialias,
1170                            const cairo_clip_t           *clip)
1171 {
1172     cairo_int_status_t status;
1173     cairo_image_surface_t *surface = abstract_surface;
1174     cairo_shadow_type_t shadow_type = source->shadow.type;
1175
1176     TRACE ((stderr, "%s (surface=%d)\n",
1177             __FUNCTION__, surface->base.unique_id));
1178
1179     status = cairo_device_acquire (surface->base.device);
1180     if (unlikely (status))
1181         return status;
1182
1183     if (shadow_type != CAIRO_SHADOW_INSET)
1184         status = _cairo_surface_shadow_fill (abstract_surface, op, source,
1185                                              path, fill_rule,
1186                                              tolerance, antialias, clip,
1187                                              &source->shadow);
1188
1189     if (unlikely (status)) {
1190         cairo_device_release (surface->base.device);
1191         return status;
1192     }
1193
1194     if (shadow_type == CAIRO_SHADOW_DROP &&
1195         source->shadow.draw_shadow_only) {
1196         cairo_device_release (surface->base.device);
1197         return status;
1198     }
1199
1200     if (! source->shadow.draw_shadow_only) {
1201         if (! source->shadow.path_is_fill_with_spread ||
1202             source->shadow.type != CAIRO_SHADOW_INSET)
1203             status = _cairo_compositor_fill (surface->compositor,
1204                                              &surface->base,
1205                                              op, source, path,
1206                                              fill_rule, tolerance,
1207                                              antialias,
1208                                              clip);
1209         else
1210             status = _cairo_compositor_paint (surface->compositor,
1211                                               &surface->base,
1212                                               op, source, clip);
1213     }
1214
1215     if (unlikely (status)) {
1216         cairo_device_release (surface->base.device);
1217         return status;
1218     }
1219
1220     if (shadow_type == CAIRO_SHADOW_INSET)
1221         status = _cairo_surface_shadow_fill (abstract_surface, op, source,
1222                                              path, fill_rule,
1223                                              tolerance, antialias, clip,
1224                                              &source->shadow);
1225
1226     cairo_device_release (surface->base.device);
1227     return status;
1228 }
1229
1230 cairo_int_status_t
1231 _cairo_image_surface_glyphs (void                       *abstract_surface,
1232                              cairo_operator_t            op,
1233                              const cairo_pattern_t      *source,
1234                              cairo_glyph_t              *glyphs,
1235                              int                         num_glyphs,
1236                              cairo_scaled_font_t        *scaled_font,
1237                              const cairo_clip_t         *clip)
1238 {
1239     cairo_int_status_t status;
1240     cairo_image_surface_t *surface = abstract_surface;
1241     cairo_shadow_type_t shadow_type = source->shadow.type;
1242
1243     TRACE ((stderr, "%s (surface=%d)\n",
1244             __FUNCTION__, surface->base.unique_id));
1245
1246     status = cairo_device_acquire (surface->base.device);
1247     if (unlikely (status))
1248         return status;
1249
1250     if (shadow_type != CAIRO_SHADOW_INSET)
1251         status = _cairo_surface_shadow_glyphs (abstract_surface, op, source,
1252                                                scaled_font,
1253                                                glyphs, num_glyphs,
1254                                                clip,
1255                                                &source->shadow);
1256
1257     if (unlikely (status)) {
1258         cairo_device_release (surface->base.device);
1259         return status;
1260     }
1261
1262     if (shadow_type == CAIRO_SHADOW_DROP &&
1263         source->shadow.draw_shadow_only) {
1264         cairo_device_release (surface->base.device);
1265         return status;
1266     }
1267
1268     if (! source->shadow.draw_shadow_only)
1269         status = _cairo_compositor_glyphs (surface->compositor, &surface->base,
1270                                            op, source,
1271                                            glyphs, num_glyphs, scaled_font,
1272                                            clip);
1273
1274     if (unlikely (status)) {
1275         cairo_device_release (surface->base.device);
1276         return status;
1277     }
1278
1279     if (shadow_type == CAIRO_SHADOW_INSET)
1280         status = _cairo_surface_shadow_glyphs (abstract_surface, op, source,
1281                                                scaled_font,
1282                                                glyphs, num_glyphs,
1283                                                clip,
1284                                                &source->shadow);
1285
1286     cairo_device_release (surface->base.device);
1287     return status;
1288 }
1289
1290 void
1291 _cairo_image_surface_get_font_options (void                  *abstract_surface,
1292                                        cairo_font_options_t  *options)
1293 {
1294     _cairo_font_options_init_default (options);
1295
1296     cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
1297     _cairo_font_options_set_round_glyph_positions (options, CAIRO_ROUND_GLYPH_POS_ON);
1298 }
1299
1300 static cairo_surface_t *
1301 _cairo_image_surface_shadow_surface (void *surface,
1302                                      cairo_bool_t has_blur,
1303                                      int width, int height,
1304                                      int *width_out, int *height_out)
1305 {
1306     int shadow_width, shadow_height;
1307     cairo_image_surface_t *shadow_surface = NULL;
1308
1309     if (width < MIN_IMAGE_SHADOW_SIZE)
1310         shadow_width = width;
1311     else if (has_blur) {
1312         if (width < MIN_IMAGE_SHADOW_SIZE * 2)
1313             shadow_width = MIN_IMAGE_SHADOW_SIZE;
1314         else if (width > MAX_IMAGE_SHADOW_SIZE * 2)
1315             shadow_width = MAX_IMAGE_SHADOW_SIZE;
1316         else
1317             shadow_width = width * 0.5;
1318     }
1319     else {
1320         if (width > MAX_IMAGE_SHADOW_SIZE)
1321             shadow_width = MAX_IMAGE_SHADOW_SIZE;
1322         else
1323             shadow_width = width;
1324     }
1325
1326     if (height < MIN_IMAGE_SHADOW_SIZE)
1327         shadow_height = height;
1328     else if (has_blur) {
1329         if (height < MIN_IMAGE_SHADOW_SIZE * 2)
1330             shadow_height = MIN_IMAGE_SHADOW_SIZE;
1331         else if (height > MAX_IMAGE_SHADOW_SIZE * 2)
1332             shadow_height = MAX_IMAGE_SHADOW_SIZE;
1333         else
1334             shadow_height = height * 0.5;
1335     }
1336     else {
1337         if (height > MAX_IMAGE_SHADOW_SIZE)
1338             shadow_height = MAX_IMAGE_SHADOW_SIZE;
1339         else
1340             shadow_height = height;
1341     }
1342
1343     shadow_surface = (cairo_image_surface_t *)
1344                 cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1345                                             shadow_width,
1346                                             shadow_height);
1347     if (unlikely (shadow_surface->base.status)) {
1348         cairo_surface_destroy (&shadow_surface->base);
1349         return NULL;
1350     }
1351
1352     *width_out = shadow_width;
1353     *height_out = shadow_height;
1354
1355     return &shadow_surface->base;
1356 }
1357
1358 const cairo_surface_backend_t _cairo_image_surface_backend = {
1359     CAIRO_SURFACE_TYPE_IMAGE,
1360     _cairo_image_surface_finish,
1361
1362     _cairo_default_context_create,
1363
1364     _cairo_image_surface_create_similar,
1365     NULL, /* create similar image */
1366     _cairo_image_surface_map_to_image,
1367     _cairo_image_surface_unmap_image,
1368
1369     _cairo_image_surface_source,
1370     _cairo_image_surface_acquire_source_image,
1371     _cairo_image_surface_release_source_image,
1372     _cairo_image_surface_snapshot,
1373
1374     NULL, /* copy_page */
1375     NULL, /* show_page */
1376
1377     _cairo_image_surface_get_extents,
1378     _cairo_image_surface_get_font_options,
1379
1380     NULL, /* flush */
1381     NULL,
1382
1383     _cairo_image_surface_paint,
1384     _cairo_image_surface_mask,
1385     _cairo_image_surface_stroke,
1386     _cairo_image_surface_fill,
1387     NULL, /* fill-stroke */
1388     _cairo_image_surface_glyphs,
1389     NULL, /* has_text_glyphs */
1390     NULL, /* show_text_glyphs */
1391     NULL, /* get_supported_mime_types */
1392     _cairo_image_surface_shadow_surface,
1393     NULL, /* get_glyph_shadow_surface */
1394     NULL, /* get_shadow_mask_surface */
1395     NULL, /* get_glyph_shadow_mask_surface */
1396     _cairo_image_surface_shadow_cache_acquire,
1397     _cairo_image_surface_shadow_cache_release,
1398     _cairo_image_surface_get_shadow_cache,
1399     _cairo_image_surface_get_shadow_cache_size,
1400     _cairo_image_surface_has_shadow_cache,
1401 };
1402
1403 /* A convenience function for when one needs to coerce an image
1404  * surface to an alternate format. */
1405 cairo_image_surface_t *
1406 _cairo_image_surface_coerce (cairo_image_surface_t *surface)
1407 {
1408     return _cairo_image_surface_coerce_to_format (surface,
1409                                                   _cairo_format_from_content (surface->base.content));
1410 }
1411
1412 /* A convenience function for when one needs to coerce an image
1413  * surface to an alternate format. */
1414 cairo_image_surface_t *
1415 _cairo_image_surface_coerce_to_format (cairo_image_surface_t *surface,
1416                                        cairo_format_t         format)
1417 {
1418     cairo_image_surface_t *clone;
1419     cairo_status_t status;
1420
1421     status = surface->base.status;
1422     if (unlikely (status))
1423         return (cairo_image_surface_t *)_cairo_surface_create_in_error (status);
1424
1425     if (surface->format == format)
1426         return (cairo_image_surface_t *)cairo_surface_reference(&surface->base);
1427
1428     clone = (cairo_image_surface_t *)
1429         cairo_image_surface_create (format, surface->width, surface->height);
1430     if (unlikely (clone->base.status))
1431         return clone;
1432
1433     pixman_image_composite32 (PIXMAN_OP_SRC,
1434                               surface->pixman_image, NULL, clone->pixman_image,
1435                               0, 0,
1436                               0, 0,
1437                               0, 0,
1438                               surface->width, surface->height);
1439     clone->base.is_clear = FALSE;
1440
1441     clone->base.device_transform =
1442         surface->base.device_transform;
1443     clone->base.device_transform_inverse =
1444         surface->base.device_transform_inverse;
1445
1446     return clone;
1447 }
1448
1449 cairo_image_surface_t *
1450 _cairo_image_surface_create_from_image (cairo_image_surface_t *other,
1451                                         pixman_format_code_t format,
1452                                         int x, int y,
1453                                         int width, int height, int stride)
1454 {
1455     cairo_image_surface_t *surface = NULL;
1456     cairo_status_t status;
1457     pixman_image_t *image;
1458     void *mem = NULL;
1459
1460     status = other->base.status;
1461     if (unlikely (status))
1462         goto cleanup;
1463
1464     if (stride) {
1465         mem = _cairo_malloc_ab (height, stride);
1466         if (unlikely (mem == NULL)) {
1467             status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1468             goto cleanup;
1469         }
1470     }
1471
1472     image = pixman_image_create_bits (format, width, height, mem, stride);
1473     if (unlikely (image == NULL)) {
1474         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1475         goto cleanup_mem;
1476     }
1477
1478     surface = (cairo_image_surface_t *)
1479         _cairo_image_surface_create_for_pixman_image (image, format);
1480     if (unlikely (surface->base.status)) {
1481         status = surface->base.status;
1482         goto cleanup_image;
1483     }
1484
1485     pixman_image_composite32 (PIXMAN_OP_SRC,
1486                               other->pixman_image, NULL, image,
1487                               x, y,
1488                               0, 0,
1489                               0, 0,
1490                               width, height);
1491     surface->base.is_clear = FALSE;
1492     surface->owns_data = mem != NULL;
1493
1494     return surface;
1495
1496 cleanup_image:
1497     pixman_image_unref (image);
1498 cleanup_mem:
1499     free (mem);
1500 cleanup:
1501     cairo_surface_destroy (&surface->base);
1502     return (cairo_image_surface_t *) _cairo_surface_create_in_error (status);
1503 }
1504
1505 cairo_image_transparency_t
1506 _cairo_image_analyze_transparency (cairo_image_surface_t *image)
1507 {
1508     int x, y;
1509
1510     if (image->transparency != CAIRO_IMAGE_UNKNOWN)
1511         return image->transparency;
1512
1513     if ((image->base.content & CAIRO_CONTENT_ALPHA) == 0)
1514         return image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1515
1516     if (image->base.is_clear)
1517         return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1518
1519     if ((image->base.content & CAIRO_CONTENT_COLOR) == 0) {
1520         if (image->format == CAIRO_FORMAT_A1) {
1521             return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1522         } else if (image->format == CAIRO_FORMAT_A8) {
1523             for (y = 0; y < image->height; y++) {
1524                 uint8_t *alpha = (uint8_t *) (image->data + y * image->stride);
1525
1526                 for (x = 0; x < image->width; x++, alpha++) {
1527                     if (*alpha > 0 && *alpha < 255)
1528                         return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1529                 }
1530             }
1531             return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1532         } else {
1533             return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1534         }
1535     }
1536
1537     if (image->format == CAIRO_FORMAT_RGB16_565) {
1538         image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1539         return CAIRO_IMAGE_IS_OPAQUE;
1540     }
1541
1542     if (image->format != CAIRO_FORMAT_ARGB32)
1543         return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1544
1545     image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1546     for (y = 0; y < image->height; y++) {
1547         uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1548
1549         for (x = 0; x < image->width; x++, pixel++) {
1550             int a = (*pixel & 0xff000000) >> 24;
1551             if (a > 0 && a < 255) {
1552                 return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1553             } else if (a == 0) {
1554                 image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1555             }
1556         }
1557     }
1558
1559     return image->transparency;
1560 }
1561
1562 cairo_image_color_t
1563 _cairo_image_analyze_color (cairo_image_surface_t      *image)
1564 {
1565     int x, y;
1566
1567     if (image->color != CAIRO_IMAGE_UNKNOWN_COLOR)
1568         return image->color;
1569
1570     if (image->format == CAIRO_FORMAT_A1)
1571         return image->color = CAIRO_IMAGE_IS_MONOCHROME;
1572
1573     if (image->format == CAIRO_FORMAT_A8)
1574         return image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1575
1576     if (image->format == CAIRO_FORMAT_ARGB32) {
1577         image->color = CAIRO_IMAGE_IS_MONOCHROME;
1578         for (y = 0; y < image->height; y++) {
1579             uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1580
1581             for (x = 0; x < image->width; x++, pixel++) {
1582                 int a = (*pixel & 0xff000000) >> 24;
1583                 int r = (*pixel & 0x00ff0000) >> 16;
1584                 int g = (*pixel & 0x0000ff00) >> 8;
1585                 int b = (*pixel & 0x000000ff);
1586                 if (a == 0) {
1587                     r = g = b = 0;
1588                 } else {
1589                     r = (r * 255 + a / 2) / a;
1590                     g = (g * 255 + a / 2) / a;
1591                     b = (b * 255 + a / 2) / a;
1592                 }
1593                 if (!(r == g && g == b))
1594                     return image->color = CAIRO_IMAGE_IS_COLOR;
1595                 else if (r > 0 && r < 255)
1596                     image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1597             }
1598         }
1599         return image->color;
1600     }
1601
1602     if (image->format == CAIRO_FORMAT_RGB24) {
1603         image->color = CAIRO_IMAGE_IS_MONOCHROME;
1604         for (y = 0; y < image->height; y++) {
1605             uint32_t *pixel = (uint32_t *) (image->data + y * image->stride);
1606
1607             for (x = 0; x < image->width; x++, pixel++) {
1608                 int r = (*pixel & 0x00ff0000) >> 16;
1609                 int g = (*pixel & 0x0000ff00) >>  8;
1610                 int b = (*pixel & 0x000000ff);
1611                 if (!(r == g && g == b))
1612                     return image->color = CAIRO_IMAGE_IS_COLOR;
1613                 else if (r > 0 && r < 255)
1614                     image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1615             }
1616         }
1617         return image->color;
1618     }
1619
1620     return image->color = CAIRO_IMAGE_IS_COLOR;
1621 }
1622
1623 cairo_image_surface_t *
1624 _cairo_image_surface_clone_subimage (cairo_surface_t             *surface,
1625                                      const cairo_rectangle_int_t *extents)
1626 {
1627     cairo_surface_t *image;
1628     cairo_surface_pattern_t pattern;
1629     cairo_status_t status;
1630
1631     image = cairo_surface_create_similar_image (surface,
1632                                                 _cairo_format_from_content (surface->content),
1633                                                 extents->width,
1634                                                 extents->height);
1635     if (image->status)
1636         return to_image_surface (image);
1637
1638     /* TODO: check me with non-identity device_transform. Should we
1639      * clone the scaling, too? */
1640     cairo_surface_set_device_offset (image,
1641                                      -extents->x,
1642                                      -extents->y);
1643
1644     _cairo_pattern_init_for_surface (&pattern, surface);
1645     pattern.base.filter = CAIRO_FILTER_NEAREST;
1646
1647     status = _cairo_surface_paint (image,
1648                                    CAIRO_OPERATOR_SOURCE,
1649                                    &pattern.base,
1650                                    NULL);
1651
1652     _cairo_pattern_fini (&pattern.base);
1653
1654     if (unlikely (status))
1655         goto error;
1656
1657     /* We use the parent as a flag during map-to-image/umap-image that the
1658      * resultant image came from a fallback rather than as direct call
1659      * to the backend's map_to_image(). Whilst we use it as a simple flag,
1660      * we need to make sure the parent surface obeys the reference counting
1661      * semantics and is consistent for all callers.
1662      */
1663     _cairo_image_surface_set_parent (to_image_surface (image),
1664                                      cairo_surface_reference (surface));
1665
1666     return to_image_surface (image);
1667
1668 error:
1669     cairo_surface_destroy (image);
1670     return to_image_surface (_cairo_surface_create_in_error (status));
1671 }