## Image Surfaces Image Surfaces — Rendering to memory buffers |

## Functions

int | cairo_format_stride_for_width ()
cairo_surface_t * | cairo_image_surface_create ()
cairo_surface_t * | cairo_image_surface_create_for_data ()
unsigned char * | cairo_image_surface_get_data ()
cairo_format_t | cairo_image_surface_get_format ()
int | cairo_image_surface_get_width ()
int | cairo_image_surface_get_height ()
int | cairo_image_surface_get_stride ()

## Types and Values

#define | CAIRO_HAS_IMAGE_SURFACE
enum | cairo_format_t

## Description

Image surfaces provide the ability to render to memory buffers either allocated by cairo or by the calling code. The supported image formats are those defined in cairo_format_t.

## Functions

### cairo_format_stride_for_width ()

    int
    cairo_format_stride_for_width (cairo_format_t format,
                                   int width);

This function provides a stride value that will respect all alignment requirements of the accelerated image-rendering code within cairo. Typical usage will be of the form:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    int stride;
    unsigned char *data;
    cairo_surface_t *surface;

    stride = cairo_format_stride_for_width (format, width);
    data = malloc (stride * height);
    surface = cairo_image_surface_create_for_data (data, format,
    					  width, height,
    					  stride);

#### Parameters

format | A cairo_format_t value |
width | The desired width of an image surface to be created. |

#### Returns

the appropriate stride to use given the desired format and width, or -1 if either the format is invalid or the width too large.

Since: 1.6

### cairo_image_surface_create ()

    cairo_surface_t *
    cairo_image_surface_create (cairo_format_t format,
                                int width,
                                int height);

Creates an image surface of the specified format and dimensions. Initially the surface contents are set to 0. (Specifically, within each pixel, each color or alpha channel belonging to format will be 0. The contents of bits within a pixel, but not belonging to the given format are undefined).

#### Parameters

format | format of pixels in the surface to create |
width | width of the surface, in pixels |
height | height of the surface, in pixels |

#### Returns

a pointer to the newly created surface. The caller owns the surface and should call `cairo_surface_destroy()` when done with it.

This function always returns a valid pointer, but it will return a pointer to a "nil" surface if an error such as out of memory occurs. You can use `cairo_surface_status()` to check for this.

Since: 1.0

### cairo_image_surface_create_for_data ()

    cairo_surface_t *
    cairo_image_surface_create_for_data (unsigned char *data,
                                         cairo_format_t format,
                                         int width,
                                         int height,
                                         int stride);

Creates an image surface for the provided pixel data. The output buffer must be kept around until the cairo_surface_t is destroyed or `cairo_surface_finish()` is called on the surface. The initial contents of `data` will be used as the initial image contents; you must explicitly clear the buffer, using, for example, `cairo_rectangle()` and `cairo_fill()` if you want it cleared.

Note that the stride may be larger than width*bytes_per_pixel to provide proper alignment for each pixel and row. This alignment is required to allow high-performance rendering within cairo. The correct way to obtain a legal stride value is to call `cairo_format_stride_for_width()` with the desired format and maximum image width value, and then use the resulting stride value to allocate the data and to create the image surface. See `cairo_format_stride_for_width()` for example code.

#### Parameters

data | a pointer to a buffer supplied by the application in which to write contents. This pointer must be suitably aligned for any kind of variable, (for example, a pointer returned by malloc). |
format | the format of pixels in the buffer |
width | the width of the image to be stored in the buffer |
height | the height of the image to be stored in the buffer |
stride | the number of bytes between the start of rows in the buffer as allocated. This value should always be computed by `cairo_format_stride_for_width()` before allocating the data buffer. |

#### Returns

a pointer to the newly created surface. The caller owns the surface and should call `cairo_surface_destroy()` when done with it.

This function always returns a valid pointer, but it will return a pointer to a "nil" surface in the case of an error such as out of memory or an invalid stride value. In case of invalid stride value the error status of the returned surface will be `CAIRO_STATUS_INVALID_STRIDE`. You can use `cairo_surface_status()` to check for this.

See `cairo_surface_set_user_data()` for a means of attaching a destroy-notification fallback to the surface if necessary.

Since: 1.0

### cairo_image_surface_get_data ()

    unsigned char *
    cairo_image_surface_get_data (cairo_surface_t *surface);

Get a pointer to the data of the image surface, for direct inspection or modification.

A call to `cairo_surface_flush()` is required before accessing the pixel data to ensure that all pending drawing operations are finished. A call to `cairo_surface_mark_dirty()` is required after the data is modified.

#### Parameters

surface | a cairo_image_surface_t |

#### Returns

a pointer to the image data of this surface or `NULL` if `surface` is not an image surface, or if `cairo_surface_finish()` has been called.

Since: 1.2

### cairo_image_surface_get_format ()

    cairo_format_t
    cairo_image_surface_get_format (cairo_surface_t *surface);

Get the format of the surface.

#### Parameters

surface | a cairo_image_surface_t |

#### Returns

the format of the surface

Since: 1.2

### cairo_image_surface_get_width ()

    int
    cairo_image_surface_get_width (cairo_surface_t *surface);

Get the width of the image surface in pixels.

#### Parameters

surface | a cairo_image_surface_t |

#### Returns

the width of the surface in pixels.

Since: 1.0

### cairo_image_surface_get_height ()

    int
    cairo_image_surface_get_height (cairo_surface_t *surface);

Get the height of the image surface in pixels.

#### Parameters

surface | a cairo_image_surface_t |

#### Returns

the height of the surface in pixels.

Since: 1.0

### cairo_image_surface_get_stride ()

    int
    cairo_image_surface_get_stride (cairo_surface_t *surface);

Get the stride of the image surface in bytes

#### Parameters

surface | a cairo_image_surface_t |

#### Returns

the stride of the image surface in bytes (or 0 if `surface` is not an image surface). The stride is the distance in bytes from the beginning of one row of the image data to the beginning of the next row.

Since: 1.2

## Types and Values

### CAIRO_HAS_IMAGE_SURFACE

    #define CAIRO_HAS_IMAGE_SURFACE 1

Defined if the image surface backend is available. The image surface backend is always built in. This macro was added for completeness in cairo 1.8.

Since: 1.8

### enum cairo_format_t

cairo_format_t is used to identify the memory format of image data.

New entries may be added in future versions.

#### Members

CAIRO_FORMAT_INVALID | no such format exists or is supported. |
CAIRO_FORMAT_ARGB32 | each pixel is a 32-bit quantity, with alpha in the upper 8 bits, then red, then green, then blue. The 32-bit quantities are stored native-endian. Pre-multiplied alpha is used. (That is, 50% transparent red is 0x80800000, not 0x80ff0000.) (Since 1.0) |
CAIRO_FORMAT_RGB24 | each pixel is a 32-bit quantity, with the upper 8 bits unused. Red, Green, and Blue are stored in the remaining 24 bits in that order. (Since 1.0) |
CAIRO_FORMAT_A8 | each pixel is a 8-bit quantity holding an alpha value. (Since 1.0) |
CAIRO_FORMAT_A1 | each pixel is a 1-bit quantity holding an alpha value. Pixels are packed together into 32-bit quantities. The ordering of the bits matches the endianness of the platform. On a big-endian machine, the first pixel is in the uppermost bit, on a little-endian machine the first pixel is in the least-significant bit. (Since 1.0) |
CAIRO_FORMAT_RGB16_565 | each pixel is a 16-bit quantity with red in the upper 5 bits, then green in the middle 6 bits, and blue in the lower 5 bits. (Since 1.2) |
CAIRO_FORMAT_RGB30 | like RGB24 but with 10bpc. (Since 1.12) |

Since: 1.0

## See Also

cairo_surface_t