aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/image/img_bmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/image/img_bmp.c')
-rw-r--r--src/others/mimesh/libg3d-0.0.8/plugins/image/img_bmp.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/image/img_bmp.c b/src/others/mimesh/libg3d-0.0.8/plugins/image/img_bmp.c
new file mode 100644
index 0000000..827997a
--- /dev/null
+++ b/src/others/mimesh/libg3d-0.0.8/plugins/image/img_bmp.c
@@ -0,0 +1,129 @@
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 <stdio.h>
24#include <string.h>
25#include <errno.h>
26
27#include <g3d/types.h>
28#include <g3d/stream.h>
29
30EAPI
31gboolean plugin_load_image_from_stream(G3DContext *context, G3DStream *stream,
32 G3DImage *image, gpointer user_data)
33{
34 guint32 filesize, offset, headsize, compression;
35 gint32 x, y, i;
36 guint32 ncolplanes, c;
37
38 /* bitmap file always starts with 'BM' */
39 if(g3d_stream_read_int16_le(stream) != ('B' | ('M' << 8))) {
40 g_warning("bitmap magic not found: image seems to be corrupt\n");
41 return FALSE;
42 }
43
44 image->name = g_strdup(stream->uri);
45
46 filesize = g3d_stream_read_int32_le(stream); /* file size */
47 g3d_stream_read_int32_le(stream); /* 2 x UINT16 reserved */
48 offset = g3d_stream_read_int32_le(stream); /* offset of data */
49 headsize = g3d_stream_read_int32_le(stream); /* size of header */
50 image->width = g3d_stream_read_int32_le(stream); /* width */
51 image->height = g3d_stream_read_int32_le(stream); /* height */
52 ncolplanes = g3d_stream_read_int16_le(stream); /* num of color planes */
53 image->depth = g3d_stream_read_int16_le(stream); /* bits per pixel */
54 compression = g3d_stream_read_int32_le(stream); /* compression */
55 g3d_stream_read_int32_le(stream); /* image size */
56 g3d_stream_read_int32_le(stream); /* v/res (dpi) */
57 g3d_stream_read_int32_le(stream); /* h/res (dpi) */
58
59#if DEBUG > 0
60 g_debug("BMP: %dx%dx%d (%d, 0x%x)", image->width, image->height,
61 image->depth, ncolplanes, compression);
62#endif
63
64 g3d_stream_seek(stream, offset, G_SEEK_SET);
65
66#define ALL32BIT
67#ifndef ALL32BIT /* always 32bit for now.. */
68 image->pixeldata = g_new0(guint8,
69 image->width * image->height * (image->depth / 8));
70#else
71 image->pixeldata = g_new0(guint8, image->width * image->height * 4);
72#endif
73
74 for(y = (image->height - 1); y >= 0; y --) {
75 for(x = 0; x < image->width; x ++) {
76 switch(image->depth) {
77 case 8:
78#ifndef ALL32BIT
79 image->pixeldata[y * image->width + x] =
80 g3d_stream_read_int8(stream);
81#else
82 c = g3d_stream_read_int8(stream);
83 for(i = 0; i < 3; i ++)
84 image->pixeldata[(y * image->width + x) * 4 + i] = c;
85 image->pixeldata[(y * image->width + x) * 4 + 3] = 0xFF;
86#endif
87 break;
88 case 24:
89#if 1
90 /* read BGR */
91 for(i = 2; i >= 0; i --)
92#else
93 for(i = 0; i < 3; i ++)
94#endif
95 image->pixeldata[(y * image->width + x) * 4 + i] =
96 g3d_stream_read_int8(stream);
97 image->pixeldata[(y * image->width + x) * 4 + 3] = 0xFF;
98 break;
99 default:
100 break;
101 }
102 } /* x */
103#if 1
104 /* padding */
105 for(i = x; i < ((image->width + 3) & ~(3)); i ++)
106 g3d_stream_read_int8(stream);
107#endif
108 } /* y */
109 image->depth = 32;
110#if DEBUG > 2
111 g_debug("bitmap successfully loaded");
112#endif
113
114 return TRUE;
115}
116
117EAPI
118gchar *plugin_description(G3DContext *context)
119{
120 return g_strdup("Windows Bitmap images.\n"
121 "Author: Markus Dahms");
122}
123
124EAPI
125gchar **plugin_extensions(G3DContext *context)
126{
127 return g_strsplit("bmp", ":", 0);
128}
129