summaryrefslogtreecommitdiff
path: root/doc/public/xml/cairo-png.xml
blob: 9b76a3983e9704862e9346933c347d179403f8e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?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" id="cairo-PNG-Support.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>
</refnamediv>

<refsynopsisdiv id="cairo-PNG-Support.synopsis" 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"><returnvalue>cairo_surface_t</returnvalue></link> *   <link linkend="cairo-image-surface-create-from-png">cairo_image_surface_create_from_png</link> (<parameter>const <link linkend="char"><type>char</type></link> *filename</parameter>);
<link linkend="cairo-status-t"><returnvalue>cairo_status_t</returnvalue></link>      (<link linkend="cairo-read-func-t">*cairo_read_func_t</link>)                (<parameter><link linkend="void"><type>void</type></link> *closure</parameter>,
                                                         <parameter>unsigned <link linkend="char"><type>char</type></link> *data</parameter>,
                                                         <parameter>unsigned <link linkend="int"><type>int</type></link> length</parameter>);
<link linkend="cairo-surface-t"><returnvalue>cairo_surface_t</returnvalue></link> *   <link linkend="cairo-image-surface-create-from-png-stream">cairo_image_surface_create_from_png_stream</link>
                                                        (<parameter><link linkend="cairo-read-func-t"><type>cairo_read_func_t</type></link> read_func</parameter>,
                                                         <parameter><link linkend="void"><type>void</type></link> *closure</parameter>);
<link linkend="cairo-status-t"><returnvalue>cairo_status_t</returnvalue></link>      <link linkend="cairo-surface-write-to-png">cairo_surface_write_to_png</link>          (<parameter><link linkend="cairo-surface-t"><type>cairo_surface_t</type></link> *surface</parameter>,
                                                         <parameter>const <link linkend="char"><type>char</type></link> *filename</parameter>);
<link linkend="cairo-status-t"><returnvalue>cairo_status_t</returnvalue></link>      (<link linkend="cairo-write-func-t">*cairo_write_func_t</link>)               (<parameter><link linkend="void"><type>void</type></link> *closure</parameter>,
                                                         <parameter>unsigned <link linkend="char"><type>char</type></link> *data</parameter>,
                                                         <parameter>unsigned <link linkend="int"><type>int</type></link> length</parameter>);
<link linkend="cairo-status-t"><returnvalue>cairo_status_t</returnvalue></link>      <link linkend="cairo-surface-write-to-png-stream">cairo_surface_write_to_png_stream</link>   (<parameter><link linkend="cairo-surface-t"><type>cairo_surface_t</type></link> *surface</parameter>,
                                                         <parameter><link linkend="cairo-write-func-t"><type>cairo_write_func_t</type></link> write_func</parameter>,
                                                         <parameter><link linkend="void"><type>void</type></link> *closure</parameter>);
</synopsis>
</refsynopsisdiv>

<refsect1 id="cairo-PNG-Support.description" 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>
<para>
It is a toy API. It only offers very simple support for reading and
writing PNG files, which is sufficient for testing and
demonstration purposes. Applications which need more control over
the generated PNG file should access the pixel data directly, using
<link linkend="cairo-image-surface-get-data"><function>cairo_image_surface_get_data()</function></link> or a backend-specific access
function, and process it with another library, e.g. gdk-pixbuf or
libpng.
</para>
</refsect1>
<refsect1 id="cairo-PNG-Support.details" role="details">
<title role="details.title">Details</title>
<refsect2 id="CAIRO-HAS-PNG-FUNCTIONS:CAPS" role="macro">
<title>CAIRO_HAS_PNG_FUNCTIONS</title>
<indexterm zone="CAIRO-HAS-PNG-FUNCTIONS:CAPS"><primary sortas="HAS_PNG_FUNCTIONS">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 id="cairo-image-surface-create-from-png" role="function">
<title>cairo_image_surface_create_from_png ()</title>
<indexterm zone="cairo-image-surface-create-from-png"><primary sortas="image_surface_create_from_png">cairo_image_surface_create_from_png</primary></indexterm>
<programlisting><link linkend="cairo-surface-t"><returnvalue>cairo_surface_t</returnvalue></link> *   cairo_image_surface_create_from_png (<parameter>const <link linkend="char"><type>char</type></link> *filename</parameter>);</programlisting>
<para>
Creates a new image surface and initializes the contents to the
given PNG file.
</para><variablelist role="params">
<varlistentry><term><parameter>filename</parameter>&#160;:</term>
<listitem><simpara>name of PNG file to load
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</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:

<link linkend="CAIRO-STATUS-NO-MEMORY:CAPS"><literal>CAIRO_STATUS_NO_MEMORY</literal></link>
<link linkend="CAIRO-STATUS-FILE-NOT-FOUND:CAPS"><literal>CAIRO_STATUS_FILE_NOT_FOUND</literal></link>
<link linkend="CAIRO-STATUS-READ-ERROR:CAPS"><literal>CAIRO_STATUS_READ_ERROR</literal></link>

Alternatively, you can allow errors to propagate through the drawing
operations and check the status on the context upon completion
using <link linkend="cairo-status"><function>cairo_status()</function></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="cairo-read-func-t" role="function">
<title>cairo_read_func_t ()</title>
<indexterm zone="cairo-read-func-t"><primary sortas="read_func_t">cairo_read_func_t</primary></indexterm>
<programlisting><link linkend="cairo-status-t"><returnvalue>cairo_status_t</returnvalue></link>      (*cairo_read_func_t)                (<parameter><link linkend="void"><type>void</type></link> *closure</parameter>,
                                                         <parameter>unsigned <link linkend="char"><type>char</type></link> *data</parameter>,
                                                         <parameter>unsigned <link linkend="int"><type>int</type></link> length</parameter>);</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
<link linkend="CAIRO-STATUS-SUCCESS:CAPS"><literal>CAIRO_STATUS_SUCCESS</literal></link> if all the data was successfully read,
<link linkend="CAIRO-STATUS-READ-ERROR:CAPS"><literal>CAIRO_STATUS_READ_ERROR</literal></link> otherwise.
</para><variablelist role="params">
<varlistentry><term><parameter>closure</parameter>&#160;:</term>
<listitem><simpara>the input closure
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>the buffer into which to read the data
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>length</parameter>&#160;:</term>
<listitem><simpara>the amount of data to read
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the status code of the read operation
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="cairo-image-surface-create-from-png-stream" role="function">
<title>cairo_image_surface_create_from_png_stream ()</title>
<indexterm zone="cairo-image-surface-create-from-png-stream"><primary sortas="image_surface_create_from_png_stream">cairo_image_surface_create_from_png_stream</primary></indexterm>
<programlisting><link linkend="cairo-surface-t"><returnvalue>cairo_surface_t</returnvalue></link> *   cairo_image_surface_create_from_png_stream
                                                        (<parameter><link linkend="cairo-read-func-t"><type>cairo_read_func_t</type></link> read_func</parameter>,
                                                         <parameter><link linkend="void"><type>void</type></link> *closure</parameter>);</programlisting>
<para>
Creates a new image surface from PNG data read incrementally
via the <parameter>read_func</parameter> function.
</para><variablelist role="params">
<varlistentry><term><parameter>read_func</parameter>&#160;:</term>
<listitem><simpara>function called to read the data of the file
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>closure</parameter>&#160;:</term>
<listitem><simpara>data to pass to <parameter>read_func</parameter>.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</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 the data read is not a valid PNG image
or memory could not be allocated for the operation.  A nil
surface can be checked for with cairo_surface_status(surface) which
may return one of the following values:

<link linkend="CAIRO-STATUS-NO-MEMORY:CAPS"><literal>CAIRO_STATUS_NO_MEMORY</literal></link>
<link linkend="CAIRO-STATUS-READ-ERROR:CAPS"><literal>CAIRO_STATUS_READ_ERROR</literal></link>

Alternatively, you can allow errors to propagate through the drawing
operations and check the status on the context upon completion
using <link linkend="cairo-status"><function>cairo_status()</function></link>.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="cairo-surface-write-to-png" role="function">
<title>cairo_surface_write_to_png ()</title>
<indexterm zone="cairo-surface-write-to-png"><primary sortas="surface_write_to_png">cairo_surface_write_to_png</primary></indexterm>
<programlisting><link linkend="cairo-status-t"><returnvalue>cairo_status_t</returnvalue></link>      cairo_surface_write_to_png          (<parameter><link linkend="cairo-surface-t"><type>cairo_surface_t</type></link> *surface</parameter>,
                                                         <parameter>const <link linkend="char"><type>char</type></link> *filename</parameter>);</programlisting>
<para>
Writes the contents of <parameter>surface</parameter> to a new file <parameter>filename</parameter> as a PNG
image.
</para><variablelist role="params">
<varlistentry><term><parameter>surface</parameter>&#160;:</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>&#160;:</term>
<listitem><simpara>the name of a file to write to
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</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
<link linkend="CAIRO-STATUS-SURFACE-TYPE-MISMATCH:CAPS"><literal>CAIRO_STATUS_SURFACE_TYPE_MISMATCH</literal></link> 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 id="cairo-write-func-t" role="function">
<title>cairo_write_func_t ()</title>
<indexterm zone="cairo-write-func-t"><primary sortas="write_func_t">cairo_write_func_t</primary></indexterm>
<programlisting><link linkend="cairo-status-t"><returnvalue>cairo_status_t</returnvalue></link>      (*cairo_write_func_t)               (<parameter><link linkend="void"><type>void</type></link> *closure</parameter>,
                                                         <parameter>unsigned <link linkend="char"><type>char</type></link> *data</parameter>,
                                                         <parameter>unsigned <link linkend="int"><type>int</type></link> length</parameter>);</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
<link linkend="CAIRO-STATUS-SUCCESS:CAPS"><literal>CAIRO_STATUS_SUCCESS</literal></link> if all the data was successfully written,
<link linkend="CAIRO-STATUS-WRITE-ERROR:CAPS"><literal>CAIRO_STATUS_WRITE_ERROR</literal></link> otherwise.
</para><variablelist role="params">
<varlistentry><term><parameter>closure</parameter>&#160;:</term>
<listitem><simpara>the output closure
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&#160;:</term>
<listitem><simpara>the buffer containing the data to write
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>length</parameter>&#160;:</term>
<listitem><simpara>the amount of data to write
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</term><listitem><simpara> the status code of the write operation
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2 id="cairo-surface-write-to-png-stream" role="function">
<title>cairo_surface_write_to_png_stream ()</title>
<indexterm zone="cairo-surface-write-to-png-stream"><primary sortas="surface_write_to_png_stream">cairo_surface_write_to_png_stream</primary></indexterm>
<programlisting><link linkend="cairo-status-t"><returnvalue>cairo_status_t</returnvalue></link>      cairo_surface_write_to_png_stream   (<parameter><link linkend="cairo-surface-t"><type>cairo_surface_t</type></link> *surface</parameter>,
                                                         <parameter><link linkend="cairo-write-func-t"><type>cairo_write_func_t</type></link> write_func</parameter>,
                                                         <parameter><link linkend="void"><type>void</type></link> *closure</parameter>);</programlisting>
<para>
Writes the image surface to the write function.
</para><variablelist role="params">
<varlistentry><term><parameter>surface</parameter>&#160;:</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>&#160;:</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>&#160;:</term>
<listitem><simpara>closure data for the write function
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis>&#160;:</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,
<link linkend="CAIRO-STATUS-SURFACE-TYPE-MISMATCH:CAPS"><literal>CAIRO_STATUS_SURFACE_TYPE_MISMATCH</literal></link> if the surface does not have
pixel contents.
</simpara></listitem></varlistentry>
</variablelist></refsect2>

</refsect1>
<refsect1 id="cairo-PNG-Support.see-also">
<title>See Also</title>
<link linkend="cairo-surface-t"><type>cairo_surface_t</type></link>
</refsect1>

</refentry>