98f70c5856f90bc24e599459ffb0fee397a6ccd6
[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
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
61
62 /**
63  * SECTION:cairo-image
64  * @Title: Image Surfaces
65  * @Short_Description: Rendering to memory buffers
66  * @See_Also: #cairo_surface_t
67  *
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.
71  **/
72
73 /**
74  * CAIRO_HAS_IMAGE_SURFACE:
75  *
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.
79  *
80  * Since: 1.8
81  **/
82
83 static cairo_bool_t
84 _cairo_image_surface_is_size_valid (int width, int height)
85 {
86     return 0 <= width  &&  width <= MAX_IMAGE_SIZE &&
87            0 <= height && height <= MAX_IMAGE_SIZE;
88 }
89
90 cairo_format_t
91 _cairo_format_from_pixman_format (pixman_format_code_t pixman_format)
92 {
93     switch (pixman_format) {
94     case PIXMAN_a8r8g8b8:
95         return CAIRO_FORMAT_ARGB32;
96     case PIXMAN_x2r10g10b10:
97         return CAIRO_FORMAT_RGB30;
98     case PIXMAN_x8r8g8b8:
99         return CAIRO_FORMAT_RGB24;
100     case PIXMAN_a8:
101         return CAIRO_FORMAT_A8;
102     case PIXMAN_a1:
103         return CAIRO_FORMAT_A1;
104     case PIXMAN_r5g6b5:
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:
124     default:
125         return CAIRO_FORMAT_INVALID;
126     }
127
128     return CAIRO_FORMAT_INVALID;
129 }
130
131 cairo_content_t
132 _cairo_content_from_pixman_format (pixman_format_code_t pixman_format)
133 {
134     cairo_content_t content;
135
136     content = 0;
137     if (PIXMAN_FORMAT_RGB (pixman_format))
138         content |= CAIRO_CONTENT_COLOR;
139     if (PIXMAN_FORMAT_A (pixman_format))
140         content |= CAIRO_CONTENT_ALPHA;
141
142     return content;
143 }
144
145 void
146 _cairo_image_surface_init (cairo_image_surface_t *surface,
147                            pixman_image_t       *pixman_image,
148                            pixman_format_code_t  pixman_format)
149 {
150     surface->parent = NULL;
151     surface->pixman_image = pixman_image;
152
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;
159
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);
164
165     surface->base.is_clear = surface->width == 0 || surface->height == 0;
166
167     surface->compositor = _cairo_image_spans_compositor_get ();
168 }
169
170 cairo_surface_t *
171 _cairo_image_surface_create_for_pixman_image (pixman_image_t            *pixman_image,
172                                               pixman_format_code_t       pixman_format)
173 {
174     cairo_image_surface_t *surface;
175
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));
179
180     _cairo_surface_init (&surface->base,
181                          &_cairo_image_surface_backend,
182                          NULL, /* device */
183                          _cairo_content_from_pixman_format (pixman_format));
184
185     _cairo_image_surface_init (surface, pixman_image, pixman_format);
186
187     return &surface->base;
188 }
189
190 cairo_bool_t
191 _pixman_format_from_masks (cairo_format_masks_t *masks,
192                            pixman_format_code_t *format_ret)
193 {
194     pixman_format_code_t format;
195     int format_type;
196     int a, r, g, b;
197     cairo_format_masks_t format_masks;
198
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);
203
204     if (masks->red_mask) {
205         if (masks->red_mask > masks->blue_mask)
206             format_type = PIXMAN_TYPE_ARGB;
207         else
208             format_type = PIXMAN_TYPE_ABGR;
209     } else if (masks->alpha_mask) {
210         format_type = PIXMAN_TYPE_A;
211     } else {
212         return FALSE;
213     }
214
215     format = PIXMAN_FORMAT (masks->bpp, format_type, a, r, g, b);
216
217     if (! pixman_format_supported_destination (format))
218         return FALSE;
219
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,
223      * or whatever. */
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)
229      {
230          return FALSE;
231      }
232
233     *format_ret = format;
234     return TRUE;
235 }
236
237 /* A mask consisting of N bits set to 1. */
238 #define MASK(N) ((1UL << (N))-1)
239
240 cairo_bool_t
241 _pixman_format_to_masks (pixman_format_code_t    format,
242                          cairo_format_masks_t   *masks)
243 {
244     int a, r, g, b;
245
246     masks->bpp = PIXMAN_FORMAT_BPP (format);
247
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);
253
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);
260         return TRUE;
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);
266         return TRUE;
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);
273         return TRUE;
274 #endif
275     case PIXMAN_TYPE_A:
276         masks->alpha_mask = MASK (a);
277         masks->red_mask   = 0;
278         masks->green_mask = 0;
279         masks->blue_mask  = 0;
280         return TRUE;
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:
286     default:
287         masks->alpha_mask = 0;
288         masks->red_mask   = 0;
289         masks->green_mask = 0;
290         masks->blue_mask  = 0;
291         return FALSE;
292     }
293 }
294
295 pixman_format_code_t
296 _cairo_format_to_pixman_format_code (cairo_format_t format)
297 {
298     pixman_format_code_t ret;
299     switch (format) {
300     case CAIRO_FORMAT_A1:
301         ret = PIXMAN_a1;
302         break;
303     case CAIRO_FORMAT_A8:
304         ret = PIXMAN_a8;
305         break;
306     case CAIRO_FORMAT_RGB24:
307         ret = PIXMAN_x8r8g8b8;
308         break;
309     case CAIRO_FORMAT_RGB30:
310         ret = PIXMAN_x2r10g10b10;
311         break;
312     case CAIRO_FORMAT_RGB16_565:
313         ret = PIXMAN_r5g6b5;
314         break;
315     case CAIRO_FORMAT_ARGB32:
316     case CAIRO_FORMAT_INVALID:
317     default:
318         ret = PIXMAN_a8r8g8b8;
319         break;
320     }
321     return ret;
322 }
323
324 cairo_surface_t *
325 _cairo_image_surface_create_with_pixman_format (unsigned char           *data,
326                                                 pixman_format_code_t     pixman_format,
327                                                 int                      width,
328                                                 int                      height,
329                                                 int                      stride)
330 {
331     cairo_surface_t *surface;
332     pixman_image_t *pixman_image;
333
334     if (! _cairo_image_surface_is_size_valid (width, height))
335     {
336         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
337     }
338
339     pixman_image = pixman_image_create_bits (pixman_format, width, height,
340                                              (uint32_t *) data, stride);
341
342     if (unlikely (pixman_image == NULL))
343         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
344
345     surface = _cairo_image_surface_create_for_pixman_image (pixman_image,
346                                                             pixman_format);
347     if (unlikely (surface->status)) {
348         pixman_image_unref (pixman_image);
349         return surface;
350     }
351
352     /* we can not make any assumptions about the initial state of user data */
353     surface->is_clear = data == NULL;
354     return surface;
355 }
356
357 /**
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
362  *
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).
368  *
369  * Return value: a pointer to the newly created surface. The caller
370  * owns the surface and should call cairo_surface_destroy() when done
371  * with it.
372  *
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.
376  *
377  * Since: 1.0
378  **/
379 cairo_surface_t *
380 cairo_image_surface_create (cairo_format_t      format,
381                             int                 width,
382                             int                 height)
383 {
384     pixman_format_code_t pixman_format;
385
386     if (! CAIRO_FORMAT_VALID (format))
387         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
388
389     pixman_format = _cairo_format_to_pixman_format_code (format);
390
391     return _cairo_image_surface_create_with_pixman_format (NULL, pixman_format,
392                                                            width, height, -1);
393 }
394 slim_hidden_def (cairo_image_surface_create);
395
396     cairo_surface_t *
397 _cairo_image_surface_create_with_content (cairo_content_t       content,
398                                           int                   width,
399                                           int                   height)
400 {
401     return cairo_image_surface_create (_cairo_format_from_content (content),
402                                        width, height);
403 }
404
405 /**
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.
409  *
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:
413  *
414  * <informalexample><programlisting>
415  * int stride;
416  * unsigned char *data;
417  * #cairo_surface_t *surface;
418  *
419  * stride = cairo_format_stride_for_width (format, width);
420  * data = malloc (stride * height);
421  * surface = cairo_image_surface_create_for_data (data, format,
422  *                                                width, height,
423  *                                                stride);
424  * </programlisting></informalexample>
425  *
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
428  * too large.
429  *
430  * Since: 1.6
431  **/
432     int
433 cairo_format_stride_for_width (cairo_format_t   format,
434                                int              width)
435 {
436     int bpp;
437
438     if (! CAIRO_FORMAT_VALID (format)) {
439         _cairo_error_throw (CAIRO_STATUS_INVALID_FORMAT);
440         return -1;
441     }
442
443     bpp = _cairo_format_bits_per_pixel (format);
444     if ((unsigned) (width) >= (INT32_MAX - 7) / (unsigned) (bpp))
445         return -1;
446
447     return CAIRO_STRIDE_FOR_WIDTH_BPP (width, bpp);
448 }
449 slim_hidden_def (cairo_format_stride_for_width);
450
451 /**
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
462  *     buffer.
463  *
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.
470  *
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.
479  *
480  * Return value: a pointer to the newly created surface. The caller
481  * owns the surface and should call cairo_surface_destroy() when done
482  * with it.
483  *
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.
490  *
491  * See cairo_surface_set_user_data() for a means of attaching a
492  * destroy-notification fallback to the surface if necessary.
493  *
494  * Since: 1.0
495  **/
496     cairo_surface_t *
497 cairo_image_surface_create_for_data (unsigned char     *data,
498                                      cairo_format_t     format,
499                                      int                width,
500                                      int                height,
501                                      int                stride)
502 {
503     pixman_format_code_t pixman_format;
504     int minstride;
505
506     if (! CAIRO_FORMAT_VALID (format))
507         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_FORMAT));
508
509     if ((stride & (CAIRO_STRIDE_ALIGNMENT-1)) != 0)
510         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
511
512     if (! _cairo_image_surface_is_size_valid (width, height))
513         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
514
515     minstride = cairo_format_stride_for_width (format, width);
516     if (stride < 0) {
517         if (stride > -minstride) {
518             return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
519         }
520     } else {
521         if (stride < minstride) {
522             return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_STRIDE));
523         }
524     }
525
526     pixman_format = _cairo_format_to_pixman_format_code (format);
527     return _cairo_image_surface_create_with_pixman_format (data,
528                                                            pixman_format,
529                                                            width, height,
530                                                            stride);
531 }
532 slim_hidden_def (cairo_image_surface_create_for_data);
533
534 /**
535  * cairo_image_surface_get_data:
536  * @surface: a #cairo_image_surface_t
537  *
538  * Get a pointer to the data of the image surface, for direct
539  * inspection or modification.
540  *
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.
545  *
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()
548  * has been called.
549  *
550  * Since: 1.2
551  **/
552 unsigned char *
553 cairo_image_surface_get_data (cairo_surface_t *surface)
554 {
555     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
556
557     if (! _cairo_surface_is_image (surface)) {
558         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
559         return NULL;
560     }
561
562     return image_surface->data;
563 }
564 slim_hidden_def (cairo_image_surface_get_data);
565
566 /**
567  * cairo_image_surface_get_format:
568  * @surface: a #cairo_image_surface_t
569  *
570  * Get the format of the surface.
571  *
572  * Return value: the format of the surface
573  *
574  * Since: 1.2
575  **/
576 cairo_format_t
577 cairo_image_surface_get_format (cairo_surface_t *surface)
578 {
579     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
580
581     if (! _cairo_surface_is_image (surface)) {
582         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
583         return CAIRO_FORMAT_INVALID;
584     }
585
586     return image_surface->format;
587 }
588 slim_hidden_def (cairo_image_surface_get_format);
589
590 /**
591  * cairo_image_surface_get_width:
592  * @surface: a #cairo_image_surface_t
593  *
594  * Get the width of the image surface in pixels.
595  *
596  * Return value: the width of the surface in pixels.
597  *
598  * Since: 1.0
599  **/
600 int
601 cairo_image_surface_get_width (cairo_surface_t *surface)
602 {
603     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
604
605     if (! _cairo_surface_is_image (surface)) {
606         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
607         return 0;
608     }
609
610     return image_surface->width;
611 }
612 slim_hidden_def (cairo_image_surface_get_width);
613
614 /**
615  * cairo_image_surface_get_height:
616  * @surface: a #cairo_image_surface_t
617  *
618  * Get the height of the image surface in pixels.
619  *
620  * Return value: the height of the surface in pixels.
621  *
622  * Since: 1.0
623  **/
624 int
625 cairo_image_surface_get_height (cairo_surface_t *surface)
626 {
627     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
628
629     if (! _cairo_surface_is_image (surface)) {
630         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
631         return 0;
632     }
633
634     return image_surface->height;
635 }
636 slim_hidden_def (cairo_image_surface_get_height);
637
638 /**
639  * cairo_image_surface_get_stride:
640  * @surface: a #cairo_image_surface_t
641  *
642  * Get the stride of the image surface in bytes
643  *
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.
648  *
649  * Since: 1.2
650  **/
651 int
652 cairo_image_surface_get_stride (cairo_surface_t *surface)
653 {
654
655     cairo_image_surface_t *image_surface = (cairo_image_surface_t *) surface;
656
657     if (! _cairo_surface_is_image (surface)) {
658         _cairo_error_throw (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
659         return 0;
660     }
661
662     return image_surface->stride;
663 }
664 slim_hidden_def (cairo_image_surface_get_stride);
665
666     cairo_format_t
667 _cairo_format_from_content (cairo_content_t content)
668 {
669     switch (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;
676     }
677
678     ASSERT_NOT_REACHED;
679     return CAIRO_FORMAT_INVALID;
680 }
681
682     cairo_content_t
683 _cairo_content_from_format (cairo_format_t format)
684 {
685     switch (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:
698         break;
699     }
700
701     ASSERT_NOT_REACHED;
702     return CAIRO_CONTENT_COLOR_ALPHA;
703 }
704
705     int
706 _cairo_format_bits_per_pixel (cairo_format_t format)
707 {
708     switch (format) {
709     case CAIRO_FORMAT_ARGB32:
710     case CAIRO_FORMAT_RGB30:
711     case CAIRO_FORMAT_RGB24:
712         return 32;
713     case CAIRO_FORMAT_RGB16_565:
714         return 16;
715     case CAIRO_FORMAT_A8:
716         return 8;
717     case CAIRO_FORMAT_A1:
718         return 1;
719     case CAIRO_FORMAT_INVALID:
720     default:
721         ASSERT_NOT_REACHED;
722         return 0;
723     }
724 }
725
726 static cairo_surface_t *
727 _cairo_image_surface_create_similar (void              *abstract_other,
728                                      cairo_content_t    content,
729                                      int                width,
730                                      int                height)
731 {
732     cairo_image_surface_t *other = abstract_other;
733
734     TRACE ((stderr, "%s (other=%u)\n", __FUNCTION__, other->base.unique_id));
735
736     if (! _cairo_image_surface_is_size_valid (width, height))
737         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_SIZE));
738
739     if (content == other->base.content) {
740         return _cairo_image_surface_create_with_pixman_format (NULL,
741                                                                other->pixman_format,
742                                                                width, height,
743                                                                0);
744     }
745
746     return _cairo_image_surface_create_with_content (content,
747                                                      width, height);
748 }
749
750 cairo_surface_t *
751 _cairo_image_surface_snapshot (void *abstract_surface)
752 {
753     cairo_image_surface_t *image = abstract_surface;
754     cairo_image_surface_t *clone;
755
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))
762             return &clone->base;
763
764         image->pixman_image = NULL;
765         image->owns_data = FALSE;
766
767         clone->transparency = image->transparency;
768         clone->color = image->color;
769
770         clone->owns_data = FALSE;
771         return &clone->base;
772     }
773
774     clone = (cairo_image_surface_t *)
775         _cairo_image_surface_create_with_pixman_format (NULL,
776                                                         image->pixman_format,
777                                                         image->width,
778                                                         image->height,
779                                                         0);
780     if (unlikely (clone->base.status))
781         return &clone->base;
782
783     if (clone->stride == image->stride) {
784         memcpy (clone->data, image->data, clone->stride * clone->height);
785     } else {
786         pixman_image_composite32 (PIXMAN_OP_SRC,
787                                   image->pixman_image, NULL, clone->pixman_image,
788                                   0, 0,
789                                   0, 0,
790                                   0, 0,
791                                   image->width, image->height);
792     }
793     clone->base.is_clear = FALSE;
794     return &clone->base;
795 }
796
797 cairo_surface_t *
798 _cairo_image_surface_map_to_image (void *abstract_other,
799                                    const cairo_rectangle_int_t *extents)
800 {
801     cairo_image_surface_t *other = abstract_other;
802     cairo_surface_t *surface;
803     uint8_t *data;
804
805     data = other->data;
806     data += extents->y * other->stride;
807     data += extents->x * PIXMAN_FORMAT_BPP (other->pixman_format)/ 8;
808
809     surface =
810         _cairo_image_surface_create_with_pixman_format (data,
811                                                         other->pixman_format,
812                                                         extents->width,
813                                                         extents->height,
814                                                         other->stride);
815
816     cairo_surface_set_device_offset (surface, -extents->x, -extents->y);
817     return surface;
818 }
819
820 cairo_int_status_t
821 _cairo_image_surface_unmap_image (void *abstract_surface,
822                                   cairo_image_surface_t *image)
823 {
824     return CAIRO_INT_STATUS_SUCCESS;
825 }
826
827 cairo_status_t
828 _cairo_image_surface_finish (void *abstract_surface)
829 {
830     cairo_image_surface_t *surface = abstract_surface;
831
832     if (surface->pixman_image) {
833         pixman_image_unref (surface->pixman_image);
834         surface->pixman_image = NULL;
835     }
836
837     if (surface->owns_data) {
838         free (surface->data);
839         surface->data = NULL;
840     }
841
842     if (surface->parent) {
843         cairo_surface_destroy (surface->parent);
844         surface->parent = NULL;
845     }
846
847     return CAIRO_STATUS_SUCCESS;
848 }
849
850 void
851 _cairo_image_surface_assume_ownership_of_data (cairo_image_surface_t *surface)
852 {
853     surface->owns_data = TRUE;
854 }
855
856 cairo_surface_t *
857 _cairo_image_surface_source (void                       *abstract_surface,
858                              cairo_rectangle_int_t      *extents)
859 {
860     cairo_image_surface_t *surface = abstract_surface;
861
862     if (extents) {
863         extents->x = extents->y = 0;
864         extents->width = surface->width;
865         extents->height = surface->height;
866     }
867
868     return &surface->base;
869 }
870
871 cairo_status_t
872 _cairo_image_surface_acquire_source_image (void                    *abstract_surface,
873                                            cairo_image_surface_t  **image_out,
874                                            void                   **image_extra)
875 {
876     *image_out = abstract_surface;
877     *image_extra = NULL;
878
879     return CAIRO_STATUS_SUCCESS;
880 }
881
882 void
883 _cairo_image_surface_release_source_image (void                   *abstract_surface,
884                                            cairo_image_surface_t  *image,
885                                            void                   *image_extra)
886 {
887 }
888
889 /* high level image interface */
890 cairo_bool_t
891 _cairo_image_surface_get_extents (void                    *abstract_surface,
892                                   cairo_rectangle_int_t   *rectangle)
893 {
894     cairo_image_surface_t *surface = abstract_surface;
895
896     rectangle->x = 0;
897     rectangle->y = 0;
898     rectangle->width  = surface->width;
899     rectangle->height = surface->height;
900
901     return TRUE;
902 }
903
904 cairo_int_status_t
905 _cairo_image_surface_paint (void                        *abstract_surface,
906                             cairo_operator_t             op,
907                             const cairo_pattern_t       *source,
908                             const cairo_clip_t          *clip)
909 {
910     cairo_image_surface_t *surface = abstract_surface;
911
912     TRACE ((stderr, "%s (surface=%d)\n",
913             __FUNCTION__, surface->base.unique_id));
914
915     return _cairo_compositor_paint (surface->compositor,
916                                     &surface->base, op, source, clip);
917 }
918
919 cairo_int_status_t
920 _cairo_image_surface_mask (void                         *abstract_surface,
921                            cairo_operator_t              op,
922                            const cairo_pattern_t        *source,
923                            const cairo_pattern_t        *mask,
924                            const cairo_clip_t           *clip)
925 {
926     cairo_image_surface_t *surface = abstract_surface;
927
928     TRACE ((stderr, "%s (surface=%d)\n",
929             __FUNCTION__, surface->base.unique_id));
930
931     return _cairo_compositor_mask (surface->compositor,
932                                    &surface->base, op, source, mask, clip);
933 }
934
935 cairo_int_status_t
936 _cairo_image_surface_stroke (void                       *abstract_surface,
937                              cairo_operator_t            op,
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,
943                              double                      tolerance,
944                              cairo_antialias_t           antialias,
945                              const cairo_clip_t         *clip)
946 {
947     cairo_image_surface_t *surface = abstract_surface;
948
949     TRACE ((stderr, "%s (surface=%d)\n",
950             __FUNCTION__, surface->base.unique_id));
951
952     return _cairo_compositor_stroke (surface->compositor, &surface->base,
953                                      op, source, path,
954                                      style, ctm, ctm_inverse,
955                                      tolerance, antialias, clip);
956 }
957
958 cairo_int_status_t
959 _cairo_image_surface_fill (void                         *abstract_surface,
960                            cairo_operator_t              op,
961                            const cairo_pattern_t        *source,
962                            const cairo_path_fixed_t     *path,
963                            cairo_fill_rule_t             fill_rule,
964                            double                        tolerance,
965                            cairo_antialias_t             antialias,
966                            const cairo_clip_t           *clip)
967 {
968     cairo_image_surface_t *surface = abstract_surface;
969
970     TRACE ((stderr, "%s (surface=%d)\n",
971             __FUNCTION__, surface->base.unique_id));
972
973     return _cairo_compositor_fill (surface->compositor, &surface->base,
974                                    op, source, path,
975                                    fill_rule, tolerance, antialias,
976                                    clip);
977 }
978
979 cairo_int_status_t
980 _cairo_image_surface_glyphs (void                       *abstract_surface,
981                              cairo_operator_t            op,
982                              const cairo_pattern_t      *source,
983                              cairo_glyph_t              *glyphs,
984                              int                         num_glyphs,
985                              cairo_scaled_font_t        *scaled_font,
986                              const cairo_clip_t         *clip)
987 {
988     cairo_image_surface_t *surface = abstract_surface;
989
990     TRACE ((stderr, "%s (surface=%d)\n",
991             __FUNCTION__, surface->base.unique_id));
992
993     return _cairo_compositor_glyphs (surface->compositor, &surface->base,
994                                      op, source,
995                                      glyphs, num_glyphs, scaled_font,
996                                      clip);
997 }
998
999 void
1000 _cairo_image_surface_get_font_options (void                  *abstract_surface,
1001                                        cairo_font_options_t  *options)
1002 {
1003     _cairo_font_options_init_default (options);
1004
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);
1007 }
1008
1009 const cairo_surface_backend_t _cairo_image_surface_backend = {
1010     CAIRO_SURFACE_TYPE_IMAGE,
1011     _cairo_image_surface_finish,
1012
1013     _cairo_default_context_create,
1014
1015     _cairo_image_surface_create_similar,
1016     NULL, /* create similar image */
1017     _cairo_image_surface_map_to_image,
1018     _cairo_image_surface_unmap_image,
1019
1020     _cairo_image_surface_source,
1021     _cairo_image_surface_acquire_source_image,
1022     _cairo_image_surface_release_source_image,
1023     _cairo_image_surface_snapshot,
1024
1025     NULL, /* copy_page */
1026     NULL, /* show_page */
1027
1028     _cairo_image_surface_get_extents,
1029     _cairo_image_surface_get_font_options,
1030
1031     NULL, /* flush */
1032     NULL,
1033
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,
1040 };
1041
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)
1046 {
1047     return _cairo_image_surface_coerce_to_format (surface,
1048                                                   _cairo_format_from_content (surface->base.content));
1049 }
1050
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)
1056 {
1057     cairo_image_surface_t *clone;
1058     cairo_status_t status;
1059
1060     status = surface->base.status;
1061     if (unlikely (status))
1062         return (cairo_image_surface_t *)_cairo_surface_create_in_error (status);
1063
1064     if (surface->format == format)
1065         return (cairo_image_surface_t *)cairo_surface_reference(&surface->base);
1066
1067     clone = (cairo_image_surface_t *)
1068         cairo_image_surface_create (format, surface->width, surface->height);
1069     if (unlikely (clone->base.status))
1070         return clone;
1071
1072     pixman_image_composite32 (PIXMAN_OP_SRC,
1073                               surface->pixman_image, NULL, clone->pixman_image,
1074                               0, 0,
1075                               0, 0,
1076                               0, 0,
1077                               surface->width, surface->height);
1078     clone->base.is_clear = FALSE;
1079
1080     clone->base.device_transform =
1081         surface->base.device_transform;
1082     clone->base.device_transform_inverse =
1083         surface->base.device_transform_inverse;
1084
1085     return clone;
1086 }
1087
1088 cairo_image_transparency_t
1089 _cairo_image_analyze_transparency (cairo_image_surface_t *image)
1090 {
1091     int x, y;
1092
1093     if (image->transparency != CAIRO_IMAGE_UNKNOWN)
1094         return image->transparency;
1095
1096     if ((image->base.content & CAIRO_CONTENT_ALPHA) == 0)
1097         return image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1098
1099     if (image->base.is_clear)
1100         return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1101
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);
1108
1109                 for (x = 0; x < image->width; x++, alpha++) {
1110                     if (*alpha > 0 && *alpha < 255)
1111                         return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1112                 }
1113             }
1114             return image->transparency = CAIRO_IMAGE_HAS_BILEVEL_ALPHA;
1115         } else {
1116             return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1117         }
1118     }
1119
1120     if (image->format == CAIRO_FORMAT_RGB16_565) {
1121         image->transparency = CAIRO_IMAGE_IS_OPAQUE;
1122         return CAIRO_IMAGE_IS_OPAQUE;
1123     }
1124
1125     if (image->format != CAIRO_FORMAT_ARGB32)
1126         return image->transparency = CAIRO_IMAGE_HAS_ALPHA;
1127
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);
1131
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;
1138             }
1139         }
1140     }
1141
1142     return image->transparency;
1143 }
1144
1145 cairo_image_color_t
1146 _cairo_image_analyze_color (cairo_image_surface_t      *image)
1147 {
1148     int x, y;
1149
1150     if (image->color != CAIRO_IMAGE_UNKNOWN_COLOR)
1151         return image->color;
1152
1153     if (image->format == CAIRO_FORMAT_A1)
1154         return image->color = CAIRO_IMAGE_IS_MONOCHROME;
1155
1156     if (image->format == CAIRO_FORMAT_A8)
1157         return image->color = CAIRO_IMAGE_IS_GRAYSCALE;
1158
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);
1163
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);
1169                 if (a == 0) {
1170                     r = g = b = 0;
1171                 } else {
1172                     r = (r * 255 + a / 2) / a;
1173                     g = (g * 255 + a / 2) / a;
1174                     b = (b * 255 + a / 2) / a;
1175                 }
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;
1180             }
1181         }
1182         return image->color;
1183     }
1184
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);
1189
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;
1198             }
1199         }
1200         return image->color;
1201     }
1202
1203     return image->color = CAIRO_IMAGE_IS_COLOR;
1204 }