PNG Support

PNG Support — Reading and writing PNG images

Synopsis




#define             CAIRO_HAS_PNG_FUNCTIONS
cairo_surface_t*    cairo_image_surface_create_from_png (const char *filename);
cairo_status_t      (*cairo_read_func_t)                (void *closure,
                                                         unsigned char *data,
                                                         unsigned int length);
cairo_surface_t*    cairo_image_surface_create_from_png_stream
                                                        (cairo_read_func_t read_func,
                                                         void *closure);
cairo_status_t      cairo_surface_write_to_png          (cairo_surface_t *surface,
                                                         const char *filename);
cairo_status_t      (*cairo_write_func_t)               (void *closure,
                                                         unsigned char *data,
                                                         unsigned int length);
cairo_status_t      cairo_surface_write_to_png_stream   (cairo_surface_t *surface,
                                                         cairo_write_func_t write_func,
                                                         void *closure);

Description

The PNG functions allow reading PNG images into image surfaces, and writing any surface to a PNG file.

Details

CAIRO_HAS_PNG_FUNCTIONS

#define CAIRO_HAS_PNG_FUNCTIONS 1

Defined if the PNG functions are available. This macro can be used to conditionally compile code using the cairo PNG functions.


cairo_image_surface_create_from_png ()

cairo_surface_t*    cairo_image_surface_create_from_png (const char *filename);

Creates a new image surface and initializes the contents to the given PNG file.

filename :

name of PNG file to load

Returns :

a new cairo_surface_t initialized with the contents of the PNG file, or a "nil" surface if any error occurred. A nil surface can be checked for with cairo_surface_status(surface) which may return one of the following values: CAIRO_STATUS_NO_MEMORY CAIRO_STATUS_FILE_NOT_FOUND CAIRO_STATUS_READ_ERROR

cairo_read_func_t ()

cairo_status_t      (*cairo_read_func_t)                (void *closure,
                                                         unsigned char *data,
                                                         unsigned int length);

cairo_read_func_t is the type of function which is called when a backend needs to read data from an input stream. It is passed the closure which was specified by the user at the time the read function was registered, the buffer to read the data into and the length of the data in bytes. The read function should return CAIRO_STATUS_SUCCESS if all the data was successfully read, CAIRO_STATUS_READ_ERROR otherwise.

closure :

the input closure

data :

the buffer into which to read the data

length :

the amount of data to read

Returns :

the status code of the read operation

cairo_image_surface_create_from_png_stream ()

cairo_surface_t*    cairo_image_surface_create_from_png_stream
                                                        (cairo_read_func_t read_func,
                                                         void *closure);

Creates a new image surface from PNG data read incrementally via the read_func function.

read_func :

function called to read the data of the file

closure :

data to pass to read_func.

Returns :

a new cairo_surface_t initialized with the contents of the PNG file or NULL if the data read is not a valid PNG image or memory could not be allocated for the operation.

cairo_surface_write_to_png ()

cairo_status_t      cairo_surface_write_to_png          (cairo_surface_t *surface,
                                                         const char *filename);

Writes the contents of surface to a new file filename as a PNG image.

surface :

a cairo_surface_t with pixel contents

filename :

the name of a file to write to

Returns :

CAIRO_STATUS_SUCCESS if the PNG file was written successfully. Otherwise, CAIRO_STATUS_NO_MEMORY if memory could not be allocated for the operation or CAIRO_STATUS_SURFACE_TYPE_MISMATCH if the surface does not have pixel contents, or CAIRO_STATUS_WRITE_ERROR if an I/O error occurs while attempting to write the file.

cairo_write_func_t ()

cairo_status_t      (*cairo_write_func_t)               (void *closure,
                                                         unsigned char *data,
                                                         unsigned int length);

cairo_write_func_t is the type of function which is called when a backend needs to write data to an output stream. It is passed the closure which was specified by the user at the time the write function was registered, the data to write and the length of the data in bytes. The write function should return CAIRO_STATUS_SUCCESS if all the data was successfully written, CAIRO_STATUS_WRITE_ERROR otherwise.

closure :

the output closure

data :

the buffer containing the data to write

length :

the amount of data to write

Returns :

the status code of the write operation

cairo_surface_write_to_png_stream ()

cairo_status_t      cairo_surface_write_to_png_stream   (cairo_surface_t *surface,
                                                         cairo_write_func_t write_func,
                                                         void *closure);

Writes the image surface to the write function.

surface :

a cairo_surface_t with pixel contents

write_func :

a cairo_write_func_t

closure :

closure data for the write function

Returns :

CAIRO_STATUS_SUCCESS if the PNG file was written successfully. Otherwise, CAIRO_STATUS_NO_MEMORY is returned if memory could not be allocated for the operation, CAIRO_STATUS_SURFACE_TYPE_MISMATCH if the surface does not have pixel contents.

See Also