<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
                "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
<!ENTITY version SYSTEM "version.xml">
]>
<refentry id="cairo-PNG-Support">
<refmeta>
<refentrytitle role="top_of_page">PNG Support</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>CAIRO Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>PNG Support</refname>
<refpurpose>Reading and writing PNG images</refpurpose>
<!--[<xref linkend="desc" endterm="desc.title"/>]-->
</refnamediv>

<refsynopsisdiv role="synopsis">
<title role="synopsis.title">Synopsis</title>

<synopsis>



#define             <link linkend="CAIRO-HAS-PNG-FUNCTIONS:CAPS">CAIRO_HAS_PNG_FUNCTIONS</link>
<link linkend="cairo-surface-t">cairo_surface_t</link>*    <link linkend="cairo-image-surface-create-from-png">cairo_image_surface_create_from_png</link> (const <link linkend="char">char</link> *filename);
<link linkend="cairo-status-t">cairo_status_t</link>      (<link linkend="cairo-read-func-t">*cairo_read_func_t</link>)                (<link linkend="void">void</link> *closure,
                                                         unsigned <link linkend="char">char</link> *data,
                                                         unsigned <link linkend="int">int</link> length);
<link linkend="cairo-surface-t">cairo_surface_t</link>*    <link linkend="cairo-image-surface-create-from-png-stream">cairo_image_surface_create_from_png_stream</link>
                                                        (<link linkend="cairo-read-func-t">cairo_read_func_t</link> read_func,
                                                         <link linkend="void">void</link> *closure);
<link linkend="cairo-status-t">cairo_status_t</link>      <link linkend="cairo-surface-write-to-png">cairo_surface_write_to_png</link>          (<link linkend="cairo-surface-t">cairo_surface_t</link> *surface,
                                                         const <link linkend="char">char</link> *filename);
<link linkend="cairo-status-t">cairo_status_t</link>      (<link linkend="cairo-write-func-t">*cairo_write_func_t</link>)               (<link linkend="void">void</link> *closure,
                                                         unsigned <link linkend="char">char</link> *data,
                                                         unsigned <link linkend="int">int</link> length);
<link linkend="cairo-status-t">cairo_status_t</link>      <link linkend="cairo-surface-write-to-png-stream">cairo_surface_write_to_png_stream</link>   (<link linkend="cairo-surface-t">cairo_surface_t</link> *surface,
                                                         <link linkend="cairo-write-func-t">cairo_write_func_t</link> write_func,
                                                         <link linkend="void">void</link> *closure);
</synopsis>
</refsynopsisdiv>









<refsect1 role="desc">
<title role="desc.title">Description</title>
<para>
The PNG functions allow reading PNG images into image surfaces, and writing
any surface to a PNG file.
</para>
</refsect1>

<refsect1 role="details">
<title role="details.title">Details</title>
<refsect2>
<title><anchor id="CAIRO-HAS-PNG-FUNCTIONS:CAPS" role="macro"/>CAIRO_HAS_PNG_FUNCTIONS</title>
<indexterm><primary>CAIRO_HAS_PNG_FUNCTIONS</primary></indexterm><programlisting>#define CAIRO_HAS_PNG_FUNCTIONS 1
</programlisting>
<para>
Defined if the PNG functions are available.
This macro can be used to conditionally compile code using the cairo
PNG functions.
</para></refsect2>
<refsect2>
<title><anchor id="cairo-image-surface-create-from-png" role="function"/>cairo_image_surface_create_from_png ()</title>
<indexterm><primary>cairo_image_surface_create_from_png</primary></indexterm><programlisting><link linkend="cairo-surface-t">cairo_surface_t</link>*    cairo_image_surface_create_from_png (const <link linkend="char">char</link> *filename);</programlisting>
<para>
Creates a new image surface and initializes the contents to the
given PNG file.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>filename</parameter>&nbsp;:</term>
<listitem><simpara> name of PNG file to load
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> a new <link linkend="cairo-surface-t"><type>cairo_surface_t</type></link> 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
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="cairo-read-func-t" role="function"/>cairo_read_func_t ()</title>
<indexterm><primary>cairo_read_func_t</primary></indexterm><programlisting><link linkend="cairo-status-t">cairo_status_t</link>      (*cairo_read_func_t)                (<link linkend="void">void</link> *closure,
                                                         unsigned <link linkend="char">char</link> *data,
                                                         unsigned <link linkend="int">int</link> length);</programlisting>
<para>
<link linkend="cairo-read-func-t"><type>cairo_read_func_t</type></link> 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.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>closure</parameter>&nbsp;:</term>
<listitem><simpara> the input closure
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara> the buffer into which to read the data
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>length</parameter>&nbsp;:</term>
<listitem><simpara> the amount of data to read
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> the status code of the read operation
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="cairo-image-surface-create-from-png-stream" role="function"/>cairo_image_surface_create_from_png_stream ()</title>
<indexterm><primary>cairo_image_surface_create_from_png_stream</primary></indexterm><programlisting><link linkend="cairo-surface-t">cairo_surface_t</link>*    cairo_image_surface_create_from_png_stream
                                                        (<link linkend="cairo-read-func-t">cairo_read_func_t</link> read_func,
                                                         <link linkend="void">void</link> *closure);</programlisting>
<para>
Creates a new image surface from PNG data read incrementally
via the <parameter>read_func</parameter> function.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>read_func</parameter>&nbsp;:</term>
<listitem><simpara> function called to read the data of the file
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>closure</parameter>&nbsp;:</term>
<listitem><simpara> data to pass to <parameter>read_func</parameter>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> a new <link linkend="cairo-surface-t"><type>cairo_surface_t</type></link> initialized with the contents
of the PNG file or <link linkend="NULL:CAPS"><literal>NULL</literal></link> if the data read is not a valid PNG image or
memory could not be allocated for the operation.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="cairo-surface-write-to-png" role="function"/>cairo_surface_write_to_png ()</title>
<indexterm><primary>cairo_surface_write_to_png</primary></indexterm><programlisting><link linkend="cairo-status-t">cairo_status_t</link>      cairo_surface_write_to_png          (<link linkend="cairo-surface-t">cairo_surface_t</link> *surface,
                                                         const <link linkend="char">char</link> *filename);</programlisting>
<para>
Writes the contents of <parameter>surface</parameter> to a new file <parameter>filename</parameter> as a PNG
image.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>surface</parameter>&nbsp;:</term>
<listitem><simpara> a <link linkend="cairo-surface-t"><type>cairo_surface_t</type></link> with pixel contents
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>filename</parameter>&nbsp;:</term>
<listitem><simpara> the name of a file to write to
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> <link linkend="CAIRO-STATUS-SUCCESS:CAPS"><literal>CAIRO_STATUS_SUCCESS</literal></link> if the PNG file was written
successfully. Otherwise, <link linkend="CAIRO-STATUS-NO-MEMORY:CAPS"><literal>CAIRO_STATUS_NO_MEMORY</literal></link> if memory could not
be allocated for the operation or
CAIRO_STATUS_SURFACE_TYPE_MISMATCH if the surface does not have
pixel contents, or <link linkend="CAIRO-STATUS-WRITE-ERROR:CAPS"><literal>CAIRO_STATUS_WRITE_ERROR</literal></link> if an I/O error occurs
while attempting to write the file.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="cairo-write-func-t" role="function"/>cairo_write_func_t ()</title>
<indexterm><primary>cairo_write_func_t</primary></indexterm><programlisting><link linkend="cairo-status-t">cairo_status_t</link>      (*cairo_write_func_t)               (<link linkend="void">void</link> *closure,
                                                         unsigned <link linkend="char">char</link> *data,
                                                         unsigned <link linkend="int">int</link> length);</programlisting>
<para>
<link linkend="cairo-write-func-t"><type>cairo_write_func_t</type></link> 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.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>closure</parameter>&nbsp;:</term>
<listitem><simpara> the output closure
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara> the buffer containing the data to write
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>length</parameter>&nbsp;:</term>
<listitem><simpara> the amount of data to write
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> the status code of the write operation
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="cairo-surface-write-to-png-stream" role="function"/>cairo_surface_write_to_png_stream ()</title>
<indexterm><primary>cairo_surface_write_to_png_stream</primary></indexterm><programlisting><link linkend="cairo-status-t">cairo_status_t</link>      cairo_surface_write_to_png_stream   (<link linkend="cairo-surface-t">cairo_surface_t</link> *surface,
                                                         <link linkend="cairo-write-func-t">cairo_write_func_t</link> write_func,
                                                         <link linkend="void">void</link> *closure);</programlisting>
<para>
Writes the image surface to the write function.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>surface</parameter>&nbsp;:</term>
<listitem><simpara> a <link linkend="cairo-surface-t"><type>cairo_surface_t</type></link> with pixel contents
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>write_func</parameter>&nbsp;:</term>
<listitem><simpara> a <link linkend="cairo-write-func-t"><type>cairo_write_func_t</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>closure</parameter>&nbsp;:</term>
<listitem><simpara> closure data for the write function
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&nbsp;:</term><listitem><simpara> <link linkend="CAIRO-STATUS-SUCCESS:CAPS"><literal>CAIRO_STATUS_SUCCESS</literal></link> if the PNG file was written
successfully.  Otherwise, <link linkend="CAIRO-STATUS-NO-MEMORY:CAPS"><literal>CAIRO_STATUS_NO_MEMORY</literal></link> is returned if
memory could not be allocated for the operation,
CAIRO_STATUS_SURFACE_TYPE_MISMATCH if the surface does not have
pixel contents.
</simpara></listitem></varlistentry>
</variablelist></refsect2>

</refsect1>



<refsect1>
<title>See Also</title>
<para>
<itemizedlist>
<listitem><link linkend="cairo-surface-t"><type>cairo_surface_t</type></link></listitem>
</itemizedlist>
</para>
</refsect1>


<refsect1><refsect2 /><refsect2 /></refsect1>
</refentry>
