diff options
Diffstat (limited to '')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/image/img_sgi.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/image/img_sgi.c b/src/others/mimesh/libg3d-0.0.8/plugins/image/img_sgi.c new file mode 100644 index 0000000..71dd583 --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/image/img_sgi.c | |||
@@ -0,0 +1,181 @@ | |||
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 | |||
30 | #define SGI_STORAGE_VERBATIM 0 | ||
31 | #define SGI_STORAGE_RLE 1 | ||
32 | |||
33 | EAPI | ||
34 | gboolean plugin_load_image_from_stream(G3DContext *context, G3DStream *stream, | ||
35 | G3DImage *image, gpointer user_data) | ||
36 | { | ||
37 | guint32 cmap, planes, x, y, p; | ||
38 | guint16 dims; | ||
39 | guint8 storage, bpc; | ||
40 | gchar name[80]; | ||
41 | |||
42 | if(g3d_stream_read_int16_be(stream) != 474) { | ||
43 | g_warning("file '%s' is not a SGI RGB file", stream->uri); | ||
44 | return FALSE; | ||
45 | } | ||
46 | |||
47 | storage = g3d_stream_read_int8(stream); | ||
48 | bpc = g3d_stream_read_int8(stream); | ||
49 | dims = g3d_stream_read_int16_be(stream); | ||
50 | |||
51 | if(bpc != 1) { | ||
52 | g_warning("SGI: %s: bpc != 1 -- unsupported", stream->uri); | ||
53 | return FALSE; | ||
54 | } | ||
55 | |||
56 | image->width = g3d_stream_read_int16_be(stream); | ||
57 | image->height = g3d_stream_read_int16_be(stream); | ||
58 | |||
59 | planes = g3d_stream_read_int16_be(stream); /* ZSIZE */ | ||
60 | image->depth = 32; | ||
61 | g3d_stream_read_int32_be(stream); /* PIXMIN */ | ||
62 | g3d_stream_read_int32_be(stream); /* PIXMAX */ | ||
63 | g3d_stream_read_int32_be(stream); /* DUMMY */ | ||
64 | g3d_stream_read(stream, name, 80); | ||
65 | |||
66 | if(strlen(name) > 0) { | ||
67 | #if DEBUG > 0 | ||
68 | g_debug("SGI: image name: %s", name); | ||
69 | #endif | ||
70 | image->name = g_strdup(name); | ||
71 | } else { | ||
72 | image->name = g_strdup(stream->uri); | ||
73 | } | ||
74 | |||
75 | cmap = g3d_stream_read_int32_be(stream); /* COLORMAP */ | ||
76 | g3d_stream_skip(stream, 404); | ||
77 | |||
78 | #if DEBUG > 0 | ||
79 | g_debug("SGI: %dx%dx%d, %d bpc, colormap: 0x%02x", | ||
80 | image->width, image->height, planes, bpc, cmap); | ||
81 | #endif | ||
82 | /* end of header */ | ||
83 | |||
84 | image->pixeldata = g_new0(guint8, image->width * image->height * 4); | ||
85 | |||
86 | if(storage == SGI_STORAGE_VERBATIM) { | ||
87 | for(p = 0; p < planes; p ++) { | ||
88 | for(y = 0; y < image->height; y ++) { | ||
89 | for(x = 0; x < image->width; x ++) { | ||
90 | image->pixeldata[(y * image->width + x) * 4 + p] = | ||
91 | g3d_stream_read_int8(stream); | ||
92 | |||
93 | if(planes == 1) { | ||
94 | /* greyscale: g = r; b = r; */ | ||
95 | image->pixeldata[(y * image->width + x) * 4 + 1] = | ||
96 | image->pixeldata[(y * image->width + x) * 4]; | ||
97 | image->pixeldata[(y * image->width + x) * 4 + 2] = | ||
98 | image->pixeldata[(y * image->width + x) * 4]; | ||
99 | } | ||
100 | } /* x */ | ||
101 | } /* y */ | ||
102 | } /* p */ | ||
103 | } /* verbatim */ | ||
104 | else /* RLE */ { | ||
105 | guint32 *starttab, *lengthtab, rleoff, rlelen; | ||
106 | guint8 cnt, pixel; | ||
107 | |||
108 | starttab = g_new0(guint32, image->height * planes); | ||
109 | lengthtab = g_new0(guint32, image->height * planes); | ||
110 | |||
111 | /* read starttab */ | ||
112 | for(p = 0; p < planes; p ++) | ||
113 | for(y = 0; y < image->height; y ++) | ||
114 | starttab[y * planes + p] = g3d_stream_read_int32_be(stream); | ||
115 | /* read lengthtab */ | ||
116 | for(p = 0; p < planes; p ++) | ||
117 | for(y = 0; y < image->height; y ++) | ||
118 | lengthtab[y * planes + p] = g3d_stream_read_int32_be(stream); | ||
119 | |||
120 | /* read image data */ | ||
121 | for(p = 0; p < planes; p ++) | ||
122 | for(y = 0; y < image->height; y ++) | ||
123 | { | ||
124 | rleoff = starttab[y * planes + p]; | ||
125 | rlelen = lengthtab[y * planes + p]; | ||
126 | |||
127 | g3d_stream_seek(stream, rleoff, G_SEEK_SET); | ||
128 | |||
129 | x = 0; | ||
130 | |||
131 | while(1) { | ||
132 | pixel = g3d_stream_read_int8(stream); | ||
133 | cnt = pixel & 0x7F; | ||
134 | |||
135 | if(cnt == 0) | ||
136 | break; | ||
137 | |||
138 | if(pixel & 0x80) { | ||
139 | /* copy n bytes */ | ||
140 | while(cnt --) { | ||
141 | image->pixeldata[(y * image->width + x) * 4 + p] = | ||
142 | g3d_stream_read_int8(stream); | ||
143 | x ++; | ||
144 | } | ||
145 | } else { | ||
146 | /* repeat next byte n times */ | ||
147 | pixel = g3d_stream_read_int8(stream); | ||
148 | while(cnt --) { | ||
149 | image->pixeldata[(y * image->width + x) * 4 + p] = | ||
150 | pixel; | ||
151 | x ++; | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | g_free(starttab); | ||
158 | g_free(lengthtab); | ||
159 | } | ||
160 | |||
161 | /* set alpha to 1.0 */ | ||
162 | if(planes < 4) | ||
163 | for(y = 0; y < image->height; y ++) | ||
164 | for(x = 0; x < image->width; x ++) | ||
165 | image->pixeldata[(y * image->width + x) * 4 + 3] = 0xFF; | ||
166 | |||
167 | return TRUE; | ||
168 | } | ||
169 | |||
170 | EAPI | ||
171 | gchar *plugin_description(G3DContext *context) | ||
172 | { | ||
173 | return g_strdup("SGI RGB images."); | ||
174 | } | ||
175 | |||
176 | EAPI | ||
177 | gchar **plugin_extensions(G3DContext *context) | ||
178 | { | ||
179 | return g_strsplit("rgb:rgba:sgi", ":", 0); | ||
180 | } | ||
181 | |||