aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/image/img_gdkpixbuf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/image/img_gdkpixbuf.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/image/img_gdkpixbuf.c210
1 files changed, 210 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/image/img_gdkpixbuf.c b/src/others/mimesh/libg3d-0.0.8/plugins/image/img_gdkpixbuf.c
new file mode 100644
index 0000000..725981d
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/image/img_gdkpixbuf.c
@@ -0,0 +1,210 @@
1/* $Id$ */
2
3/*
4 libg3d - 3D object loading library
5
6 Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de>
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21*/
22
23#include <g3d/config.h>
24#include <stdio.h>
25#include <string.h>
26#include <errno.h>
27
28#include <gdk/gdk.h>
29#include <gdk-pixbuf/gdk-pixbuf.h>
30
31#include <g3d/types.h>
32#include <g3d/stream.h>
33
34static gboolean gdkpixbuf_init(void);
35static gboolean gdkpixbuf_postprocess(GdkPixbuf *pixbuf, G3DImage *image,
36 const gchar *uri);
37
38#define BUFSIZE 1024
39
40EAPI
41gboolean plugin_load_image_from_stream(G3DContext *context, G3DStream *stream,
42 G3DImage *image, gpointer user_data)
43{
44 GdkPixbuf *pixbuf;
45 GdkPixbufLoader *loader;
46 GError *error = NULL;
47 guint8 buffer[BUFSIZE];
48 gsize n;
49 gboolean retval;
50
51 if(!gdkpixbuf_init())
52 return FALSE;
53
54 loader = gdk_pixbuf_loader_new();
55 if (NULL == loader)
56 {
57 g_warning("gdkpixbuf - plugin_load_image_from_stream(): no loader");
58 return FALSE;
59 }
60 while(!g3d_stream_eof(stream)) {
61 n = g3d_stream_read(stream, buffer, BUFSIZE);
62 if(0 >= n)
63 break;
64 if(!gdk_pixbuf_loader_write(loader, buffer, n, &error)) {
65 g_warning("error loading image data from stream: %s",
66 error->message);
67 g_error_free(error);
68 g_object_unref(loader);
69 return FALSE;
70 }
71 }
72
73 if(!gdk_pixbuf_loader_close(loader, &error)) {
74 g_warning("error loading image data from stream: %s",
75 error->message);
76 g_error_free(error);
77 g_object_unref(loader);
78 return FALSE;
79 }
80
81 pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
82 gdk_pixbuf_ref(pixbuf);
83
84 retval = gdkpixbuf_postprocess(pixbuf, image, stream->uri);
85
86 g_object_unref(loader);
87 return retval;
88}
89
90EAPI
91gchar *plugin_description(G3DContext *context)
92{
93 return g_strdup("Read most image formats using the GdkPixbuf library.");
94}
95
96EAPI
97gchar **plugin_extensions(G3DContext *context)
98{
99 gchar *extensions = g_strdup("");
100 gchar **retval;
101 gchar *tmp, **ext;
102 GSList *fitem;
103 GdkPixbufFormat *format;
104
105 fitem = gdk_pixbuf_get_formats();
106 while(fitem)
107 {
108 gboolean doit = TRUE;
109
110 format = (GdkPixbufFormat *)fitem->data;
111 ext = gdk_pixbuf_format_get_extensions(format);
112
113 if (0 == g_strcmp0("bmp", *ext))
114 doit = FALSE;
115 else if (0 == g_strcmp0("jpg", *ext))
116 doit = FALSE;
117 else if (0 == g_strcmp0("jpeg", *ext))
118 doit = FALSE;
119 else if (0 == g_strcmp0("jfif", *ext))
120 doit = FALSE;
121 else if (0 == g_strcmp0("jif", *ext))
122 doit = FALSE;
123
124 if (doit)
125 {
126 tmp = g_strdup_printf("%s%s%s", extensions, strlen(extensions) ? ":" : "", g_strjoinv(":", ext));
127 g_free(extensions);
128 extensions = tmp;
129 }
130 fitem = fitem->next;
131 }
132
133 retval = g_strsplit(extensions, ":", 0);
134 g_free(extensions);
135 return retval;
136}
137
138/*****************************************************************************/
139
140static gboolean gdkpixbuf_init(void)
141{
142 static gboolean init = TRUE;
143
144 if(init) {
145 /* initialize GDK */
146 /* FIXME: problem if already initialized with gtk_init()? */
147 gint argc = 0;
148 if(!gdk_init_check(&argc, NULL))
149 return FALSE;
150 init = FALSE;
151 }
152 return TRUE;
153}
154
155static gboolean gdkpixbuf_postprocess(GdkPixbuf *pixbuf, G3DImage *image,
156 const gchar *uri)
157{
158 guint32 x, y, nchannels;
159 guchar *p;
160
161 if(gdk_pixbuf_get_colorspace(pixbuf) != GDK_COLORSPACE_RGB) {
162 g_warning("GdkPixbuf: %s: colorspace is not RGB", uri);
163 gdk_pixbuf_unref(pixbuf);
164 return FALSE;
165 }
166
167 nchannels = gdk_pixbuf_get_n_channels(pixbuf);
168 if(nchannels < 3)
169 {
170 g_warning("GdkPixbuf: %s: has only %d channels", uri,
171 gdk_pixbuf_get_n_channels(pixbuf));
172 gdk_pixbuf_unref(pixbuf);
173 return FALSE;
174 }
175
176 image->width = gdk_pixbuf_get_width(pixbuf);
177 image->height = gdk_pixbuf_get_height(pixbuf);
178 image->depth = 32;
179 image->name = g_path_get_basename(uri);
180 image->pixeldata = g_new0(guint8, image->width * image->height * 4);
181
182 for(y = 0; y < image->height; y ++)
183 for(x = 0; x < image->width; x ++)
184 {
185 p = gdk_pixbuf_get_pixels(pixbuf) + y *
186 gdk_pixbuf_get_rowstride(pixbuf) + x * nchannels;
187
188 image->pixeldata[(y * image->width + x) * 4 + 0] = p[0];
189 image->pixeldata[(y * image->width + x) * 4 + 1] = p[1];
190 image->pixeldata[(y * image->width + x) * 4 + 2] = p[2];
191 if(gdk_pixbuf_get_n_channels(pixbuf) >= 4)
192 image->pixeldata[(y * image->width + x) * 4 + 3] = p[3];
193 }
194
195 /* set alpha to 1.0 */
196 if(gdk_pixbuf_get_n_channels(pixbuf) < 4)
197 for(y = 0; y < image->height; y ++)
198 for(x = 0; x < image->width; x ++)
199 image->pixeldata[(y * image->width + x) * 4 + 3] = 0xFF;
200
201 gdk_pixbuf_unref(pixbuf);
202
203#if DEBUG > 0
204 g_print("GdkPixbuf: image '%s' loaded (%dx%d)\n",
205 image->name, image->width, image->height);
206#endif
207
208 return TRUE;
209}
210