aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/image/img_gdkpixbuf.c
blob: 725981d2f2215503bd8811053ac1f6a67f89d1b6 (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
/* $Id$ */

/*
    libg3d - 3D object loading library

    Copyright (C) 2005-2009  Markus Dahms <mad@automagically.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <g3d/config.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include <g3d/types.h>
#include <g3d/stream.h>

static gboolean gdkpixbuf_init(void);
static gboolean gdkpixbuf_postprocess(GdkPixbuf *pixbuf, G3DImage *image,
	const gchar *uri);

#define BUFSIZE 1024

EAPI
gboolean plugin_load_image_from_stream(G3DContext *context, G3DStream *stream,
	G3DImage *image, gpointer user_data)
{
	GdkPixbuf *pixbuf;
	GdkPixbufLoader *loader;
	GError *error = NULL;
	guint8 buffer[BUFSIZE];
	gsize n;
	gboolean retval;

	if(!gdkpixbuf_init())
		return FALSE;

	loader = gdk_pixbuf_loader_new();
	if (NULL == loader)
	{
		g_warning("gdkpixbuf - plugin_load_image_from_stream(): no loader");
		return FALSE;
	}
	while(!g3d_stream_eof(stream)) {
		n = g3d_stream_read(stream, buffer, BUFSIZE);
		if(0 >= n)
			break;
		if(!gdk_pixbuf_loader_write(loader, buffer, n, &error)) {
			g_warning("error loading image data from stream: %s",
				error->message);
			g_error_free(error);
			g_object_unref(loader);
			return FALSE;
		}
	}

	if(!gdk_pixbuf_loader_close(loader, &error)) {
		g_warning("error loading image data from stream: %s",
			error->message);
		g_error_free(error);
		g_object_unref(loader);
		return FALSE;
	}

	pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
	gdk_pixbuf_ref(pixbuf);

	retval = gdkpixbuf_postprocess(pixbuf, image, stream->uri);

	g_object_unref(loader);
	return retval;
}

EAPI
gchar *plugin_description(G3DContext *context)
{
	return g_strdup("Read most image formats using the GdkPixbuf library.");
}

EAPI
gchar **plugin_extensions(G3DContext *context)
{
	gchar *extensions = g_strdup("");
	gchar **retval;
	gchar *tmp, **ext;
	GSList *fitem;
	GdkPixbufFormat *format;

	fitem = gdk_pixbuf_get_formats();
	while(fitem)
	{
		gboolean doit = TRUE;

		format = (GdkPixbufFormat *)fitem->data;
		ext = gdk_pixbuf_format_get_extensions(format);

		if (0 == g_strcmp0("bmp", *ext))
		    doit = FALSE;
		else if (0 == g_strcmp0("jpg", *ext))
		    doit = FALSE;
		else if (0 == g_strcmp0("jpeg", *ext))
		    doit = FALSE;
		else if (0 == g_strcmp0("jfif", *ext))
		    doit = FALSE;
		else if (0 == g_strcmp0("jif", *ext))
		    doit = FALSE;

		if (doit)
		{
		    tmp = g_strdup_printf("%s%s%s", extensions, strlen(extensions) ? ":" : "", g_strjoinv(":", ext));
		    g_free(extensions);
		    extensions = tmp;
		}
		fitem = fitem->next;
	}

	retval = g_strsplit(extensions, ":", 0);
	g_free(extensions);
	return retval;
}

/*****************************************************************************/

static gboolean gdkpixbuf_init(void)
{
	static gboolean init = TRUE;

	if(init) {
		/* initialize GDK */
		/* FIXME: problem if already initialized with gtk_init()? */
		gint argc = 0;
		if(!gdk_init_check(&argc, NULL))
			return FALSE;
		init = FALSE;
	}
	return TRUE;
}

static gboolean gdkpixbuf_postprocess(GdkPixbuf *pixbuf, G3DImage *image,
	const gchar *uri)
{
	guint32 x, y, nchannels;
	guchar *p;

	if(gdk_pixbuf_get_colorspace(pixbuf) != GDK_COLORSPACE_RGB) {
		g_warning("GdkPixbuf: %s: colorspace is not RGB", uri);
		gdk_pixbuf_unref(pixbuf);
		return FALSE;
	}

	nchannels = gdk_pixbuf_get_n_channels(pixbuf);
	if(nchannels < 3)
	{
		g_warning("GdkPixbuf: %s: has only %d channels", uri,
			gdk_pixbuf_get_n_channels(pixbuf));
		gdk_pixbuf_unref(pixbuf);
		return FALSE;
	}

	image->width = gdk_pixbuf_get_width(pixbuf);
	image->height = gdk_pixbuf_get_height(pixbuf);
	image->depth = 32;
	image->name = g_path_get_basename(uri);
	image->pixeldata = g_new0(guint8, image->width * image->height * 4);

	for(y = 0; y < image->height; y ++)
		for(x = 0; x < image->width; x ++)
		{
			p = gdk_pixbuf_get_pixels(pixbuf) + y *
				gdk_pixbuf_get_rowstride(pixbuf) + x * nchannels;

			image->pixeldata[(y * image->width + x) * 4 + 0] = p[0];
			image->pixeldata[(y * image->width + x) * 4 + 1] = p[1];
			image->pixeldata[(y * image->width + x) * 4 + 2] = p[2];
			if(gdk_pixbuf_get_n_channels(pixbuf) >= 4)
				image->pixeldata[(y * image->width + x) * 4 + 3] = p[3];
		}

	/* set alpha to 1.0 */
	if(gdk_pixbuf_get_n_channels(pixbuf) < 4)
		for(y = 0; y < image->height; y ++)
			for(x = 0; x < image->width; x ++)
				image->pixeldata[(y * image->width + x) * 4 + 3] = 0xFF;

	gdk_pixbuf_unref(pixbuf);

#if DEBUG > 0
	g_print("GdkPixbuf: image '%s' loaded (%dx%d)\n",
		image->name, image->width, image->height);
#endif

	return TRUE;
}