]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf.c
add a 'paned' mode to the function to let it draw the seven dots, instead
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf.c
1 /* GdkPixbuf library - Basic memory management
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Authors: Mark Crichton <crichton@gimp.org>
6  *          Miguel de Icaza <miguel@gnu.org>
7  *          Federico Mena-Quintero <federico@gimp.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  */
24
25 #include <config.h>
26 #include <math.h>
27 #include <libart_lgpl/art_misc.h>
28 #include <libart_lgpl/art_affine.h>
29 #include <libart_lgpl/art_pixbuf.h>
30 #include <libart_lgpl/art_rgb_pixbuf_affine.h>
31 #include <libart_lgpl/art_alphagamma.h>
32 #include "gdk-pixbuf.h"
33
34 \f
35
36 /* Reference counting */
37
38 /**
39  * gdk_pixbuf_ref:
40  * @pixbuf: A pixbuf.
41  *
42  * Adds a reference to a pixbuf.  It must be released afterwards using
43  * gdk_pixbuf_unref().
44  *
45  * Return value: The same as the @pixbuf argument.
46  **/
47 GdkPixbuf *
48 gdk_pixbuf_ref (GdkPixbuf *pixbuf)
49 {
50         g_return_val_if_fail (pixbuf != NULL, NULL);
51         g_return_val_if_fail (pixbuf->ref_count > 0, NULL);
52
53         pixbuf->ref_count++;
54         return pixbuf;
55 }
56
57 /**
58  * gdk_pixbuf_unref:
59  * @pixbuf: A pixbuf.
60  *
61  * Removes a reference from a pixbuf.  It will be destroyed when the reference
62  * count drops to zero.
63  **/
64 void
65 gdk_pixbuf_unref (GdkPixbuf *pixbuf)
66 {
67         g_return_if_fail (pixbuf != NULL);
68         g_return_if_fail (pixbuf->ref_count > 0);
69
70         pixbuf->ref_count--;
71
72         if (pixbuf->ref_count == 0) {
73                 art_pixbuf_free (pixbuf->art_pixbuf);
74                 pixbuf->art_pixbuf = NULL;
75                 g_free (pixbuf);
76         }
77 }
78
79 \f
80
81 /* Wrap a libart pixbuf */
82
83 /**
84  * gdk_pixbuf_new_from_art_pixbuf:
85  * @art_pixbuf: A libart pixbuf.
86  *
87  * Creates a #GdkPixbuf by wrapping a libart pixbuf.
88  *
89  * Return value: A newly-created #GdkPixbuf structure with a reference count of
90  * 1.
91  **/
92 GdkPixbuf *
93 gdk_pixbuf_new_from_art_pixbuf (ArtPixBuf *art_pixbuf)
94 {
95         GdkPixbuf *pixbuf;
96
97         g_return_val_if_fail (art_pixbuf != NULL, NULL);
98
99         pixbuf = g_new (GdkPixbuf, 1);
100         pixbuf->ref_count = 1;
101         pixbuf->art_pixbuf = art_pixbuf;
102
103         return pixbuf;
104 }
105
106 /* Destroy notification function for gdk_pixbuf_new() */
107 static void
108 free_buffer (gpointer user_data, gpointer data)
109 {
110         free (data);
111 }
112
113 \f
114
115 /* Create an empty pixbuf */
116
117 /**
118  * gdk_pixbuf_new:
119  * @format: Image format.
120  * @has_alpha: Whether the image should have transparency information.
121  * @bits_per_sample: Number of bits per color sample.
122  * @width: Width of image in pixels.
123  * @height: Height of image in pixels.
124  *
125  * Creates a new #GdkPixbuf structure and allocates a buffer for it.  The buffer
126  * has an optimal rowstride.  Note that the buffer is not cleared; you will have
127  * to fill it completely.
128  *
129  * Return value: A newly-created #GdkPixbuf with a reference count of 1, or NULL
130  * if not enough memory could be allocated for the image buffer.
131  **/
132 GdkPixbuf *
133 gdk_pixbuf_new (ArtPixFormat format, gboolean has_alpha, int bits_per_sample,
134                 int width, int height)
135 {
136         guchar *buf;
137         int channels;
138         int rowstride;
139
140         g_return_val_if_fail (format == ART_PIX_RGB, NULL);
141         g_return_val_if_fail (bits_per_sample == 8, NULL);
142         g_return_val_if_fail (width > 0, NULL);
143         g_return_val_if_fail (height > 0, NULL);
144
145         /* Always align rows to 32-bit boundaries */
146
147         channels = has_alpha ? 4 : 3;
148         rowstride = 4 * ((channels * width + 3) / 4);
149
150         buf = malloc (height * rowstride);
151         if (!buf)
152                 return NULL;
153
154         return gdk_pixbuf_new_from_data (buf, format, has_alpha, width, height, rowstride,
155                                          free_buffer, NULL);
156 }
157
158 \f
159
160 /* Convenience functions */
161
162 /**
163  * gdk_pixbuf_get_format:
164  * @pixbuf: A pixbuf.
165  *
166  * Queries the image format (color model) of a pixbuf.
167  *
168  * Return value: Image format.
169  **/
170 ArtPixFormat
171 gdk_pixbuf_get_format (GdkPixbuf *pixbuf)
172 {
173         g_return_val_if_fail (pixbuf != NULL, ART_PIX_RGB);
174         g_assert (pixbuf->art_pixbuf != NULL);
175
176         return pixbuf->art_pixbuf->format;
177 }
178
179 /**
180  * gdk_pixbuf_get_n_channels:
181  * @pixbuf: A pixbuf.
182  *
183  * Queries the number of channels of a pixbuf.
184  *
185  * Return value: Number of channels.
186  **/
187 int
188 gdk_pixbuf_get_n_channels (GdkPixbuf *pixbuf)
189 {
190         g_return_val_if_fail (pixbuf != NULL, -1);
191         g_assert (pixbuf->art_pixbuf != NULL);
192
193         return pixbuf->art_pixbuf->n_channels;
194 }
195
196 /**
197  * gdk_pixbuf_get_has_alpha:
198  * @pixbuf: A pixbuf.
199  *
200  * Queries whether a pixbuf has an alpha channel (opacity information).
201  *
202  * Return value: TRUE if it has an alpha channel, FALSE otherwise.
203  **/
204 int
205 gdk_pixbuf_get_has_alpha (GdkPixbuf *pixbuf)
206 {
207         g_return_val_if_fail (pixbuf != NULL, -1);
208         g_assert (pixbuf->art_pixbuf != NULL);
209
210         return pixbuf->art_pixbuf->has_alpha;
211 }
212
213 /**
214  * gdk_pixbuf_get_bits_per_sample:
215  * @pixbuf: A pixbuf.
216  *
217  * Queries the number of bits per color sample in a pixbuf.
218  *
219  * Return value: Number of bits per color sample.
220  **/
221 int
222 gdk_pixbuf_get_bits_per_sample (GdkPixbuf *pixbuf)
223 {
224         g_return_val_if_fail (pixbuf != NULL, -1);
225         g_assert (pixbuf->art_pixbuf != NULL);
226
227         return pixbuf->art_pixbuf->bits_per_sample;
228 }
229
230 /**
231  * gdk_pixbuf_get_pixels:
232  * @pixbuf: A pixbuf.
233  *
234  * Queries a pointer to the pixel data of a pixbuf.
235  *
236  * Return value: A pointer to the pixbuf's pixel data.
237  **/
238 guchar *
239 gdk_pixbuf_get_pixels (GdkPixbuf *pixbuf)
240 {
241         g_return_val_if_fail (pixbuf != NULL, NULL);
242         g_assert (pixbuf->art_pixbuf != NULL);
243
244         return pixbuf->art_pixbuf->pixels;
245 }
246
247 /**
248  * gdk_pixbuf_get_width:
249  * @pixbuf: A pixbuf.
250  *
251  * Queries the width of a pixbuf.
252  *
253  * Return value: Width in pixels.
254  **/
255 int
256 gdk_pixbuf_get_width (GdkPixbuf *pixbuf)
257 {
258         g_return_val_if_fail (pixbuf != NULL, -1);
259         g_assert (pixbuf->art_pixbuf != NULL);
260
261         return pixbuf->art_pixbuf->width;
262 }
263
264 /**
265  * gdk_pixbuf_get_height:
266  * @pixbuf: A pixbuf.
267  *
268  * Queries the height of a pixbuf.
269  *
270  * Return value: Height in pixels.
271  **/
272 int
273 gdk_pixbuf_get_height (GdkPixbuf *pixbuf)
274 {
275         g_return_val_if_fail (pixbuf != NULL, -1);
276         g_assert (pixbuf->art_pixbuf != NULL);
277
278         return pixbuf->art_pixbuf->height;
279 }
280
281 /**
282  * gdk_pixbuf_get_rowstride:
283  * @pixbuf: A pixbuf.
284  *
285  * Queries the rowstride of a pixbuf, or the number of bytes between rows.
286  *
287  * Return value: Number of bytes between rows.
288  **/
289 int
290 gdk_pixbuf_get_rowstride (GdkPixbuf *pixbuf)
291 {
292         g_return_val_if_fail (pixbuf != NULL, -1);
293         g_assert (pixbuf->art_pixbuf != NULL);
294
295         return pixbuf->art_pixbuf->rowstride;
296 }
297
298 /* General initialization hooks */
299 const guint gdk_pixbuf_major_version=GDK_PIXBUF_MAJOR,
300   gdk_pixbuf_minor_version=GDK_PIXBUF_MINOR,
301   gdk_pixbuf_micro_version=GDK_PIXBUF_MICRO;
302
303 const char *gdk_pixbuf_version = GDK_PIXBUF_VERSION;
304
305 void
306 gdk_pixbuf_preinit(gpointer app, gpointer modinfo)
307 {
308 }
309
310 void
311 gdk_pixbuf_postinit(gpointer app, gpointer modinfo)
312 {
313 }