23
|
1 /* Libvisual - The audio visualisation framework.
|
|
2 *
|
|
3 * Copyright (C) 2004, 2005 Dennis Smit <ds@nerds-incorporated.org>
|
|
4 *
|
|
5 * Authors: Dennis Smit <ds@nerds-incorporated.org>
|
|
6 *
|
|
7 * $Id:
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU Lesser General Public License as
|
|
11 * published by the Free Software Foundation; either version 2.1
|
|
12 * of the License, or (at your option) any later version.
|
|
13 *
|
|
14 * This program 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
|
|
17 * GNU Lesser General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU Lesser General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 */
|
|
23
|
|
24 #include <unistd.h>
|
|
25 #include <stdlib.h>
|
|
26 #include <string.h>
|
|
27 #include <math.h>
|
|
28
|
|
29 #include <sys/stat.h>
|
|
30 #include <fcntl.h>
|
|
31
|
|
32 #include "lv_common.h"
|
|
33 #include "lv_log.h"
|
|
34 #include "lv_endianess.h"
|
|
35 #include "lv_bmp.h"
|
|
36
|
|
37 #define BI_RGB 0
|
|
38 #define BI_RLE8 1
|
|
39 #define BI_RLE 2
|
|
40
|
|
41 /**
|
|
42 * @defgroup VisBitmap VisBitmap
|
|
43 * @{
|
|
44 */
|
|
45
|
|
46 /**
|
|
47 * Loads a BMP file into a VisVideo. The buffer will be located
|
|
48 * for the VisVideo.
|
|
49 *
|
|
50 * The loader currently only supports uncompressed bitmaps in the form
|
|
51 * of either 8 bits indexed or 24 bits RGB.
|
|
52 *
|
|
53 * Keep in mind that you need to free the palette by hand.
|
|
54 *
|
|
55 * @param video Destination video where the bitmap should be loaded in.
|
|
56 * @param filename The filename of the bitmap to be loaded.
|
|
57 *
|
|
58 * @return VISUAL_OK on succes, -VISUAL_ERROR_VIDEO_NULL, -VISUAL_ERROR_BMP_NOT_FOUND,
|
|
59 * -VISUAL_ERROR_BMP_NO_BMP, -VISUAL_ERROR_BMP_NOT_SUPPORTED or -VISUAL_ERROR_BMP_CORRUPTED
|
|
60 * on failure.
|
|
61 */
|
|
62 int visual_bitmap_load (VisVideo *video, const char *filename)
|
|
63 {
|
|
64 /* The win32 BMP header */
|
|
65 char magic[2];
|
|
66 uint32_t bf_size = 0;
|
|
67 uint32_t bf_bits = 0;
|
|
68
|
|
69 /* The win32 BITMAPINFOHEADER */
|
|
70 int32_t bi_size = 0;
|
|
71 int32_t bi_width = 0;
|
|
72 int32_t bi_height = 0;
|
|
73 int16_t bi_bitcount = 0;
|
|
74 uint32_t bi_compression;
|
|
75 uint32_t bi_clrused;
|
|
76
|
|
77 /* File read vars */
|
|
78 int fd;
|
|
79
|
|
80 /* Worker vars */
|
|
81 uint8_t *data;
|
|
82 int pad;
|
|
83 int i;
|
|
84
|
|
85 visual_log_return_val_if_fail (video != NULL, -VISUAL_ERROR_VIDEO_NULL);
|
|
86
|
|
87 fd = open (filename, O_RDONLY);
|
|
88
|
|
89 if (fd < 0) {
|
|
90 visual_log (VISUAL_LOG_WARNING, "Bitmap file not found: %s", filename);
|
|
91
|
|
92 return -VISUAL_ERROR_BMP_NOT_FOUND;
|
|
93 }
|
|
94
|
|
95 /* Read the magic string */
|
|
96 read (fd, magic, 2);
|
|
97
|
|
98 if (strncmp (magic, "BM", 2) != 0) {
|
|
99 visual_log (VISUAL_LOG_WARNING, "Not a bitmap file");
|
|
100
|
|
101 return -VISUAL_ERROR_BMP_NO_BMP;
|
|
102 }
|
|
103
|
|
104 /* Read the file size */
|
|
105 read (fd, &bf_size, 4);
|
|
106 bf_size = VISUAL_ENDIAN_LEI32 (bf_size);
|
|
107
|
|
108 /* Skip past the reserved bits */
|
|
109 lseek (fd, 4, SEEK_CUR);
|
|
110
|
|
111 /* Read the offset bits */
|
|
112 read (fd, &bf_bits, 4);
|
|
113 bf_bits = VISUAL_ENDIAN_LEI32 (bf_bits);
|
|
114
|
|
115 /* Read the info structure size */
|
|
116 read (fd, &bi_size, 4);
|
|
117 bi_size = VISUAL_ENDIAN_LEI32 (bi_size);
|
|
118
|
|
119 if (bi_size == 12) {
|
|
120 /* And read the width, height */
|
|
121 read (fd, &bi_width, 2);
|
|
122 read (fd, &bi_height, 2);
|
|
123 bi_width = VISUAL_ENDIAN_LEI16 (bi_width);
|
|
124 bi_height = VISUAL_ENDIAN_LEI16 (bi_height);
|
|
125
|
|
126 /* Skip over the planet */
|
|
127 lseek (fd, 2, SEEK_CUR);
|
|
128
|
|
129 /* Read the bits per pixel */
|
|
130 read (fd, &bi_bitcount, 2);
|
|
131 bi_bitcount = VISUAL_ENDIAN_LEI16 (bi_bitcount);
|
|
132
|
|
133 bi_compression = BI_RGB;
|
|
134 } else {
|
|
135 /* And read the width, height */
|
|
136 read (fd, &bi_width, 4);
|
|
137 read (fd, &bi_height, 4);
|
|
138 bi_width = VISUAL_ENDIAN_LEI16 (bi_width);
|
|
139 bi_height = VISUAL_ENDIAN_LEI16 (bi_height);
|
|
140
|
|
141 /* Skip over the planet */
|
|
142 lseek (fd, 2, SEEK_CUR);
|
|
143
|
|
144 /* Read the bits per pixel */
|
|
145 read (fd, &bi_bitcount, 2);
|
|
146 bi_bitcount = VISUAL_ENDIAN_LEI16 (bi_bitcount);
|
|
147
|
|
148 /* Read the compression flag */
|
|
149 read (fd, &bi_compression, 4);
|
|
150 bi_compression = VISUAL_ENDIAN_LEI32 (bi_compression);
|
|
151
|
|
152 /* Skip over the nonsense we don't want to know */
|
|
153 lseek (fd, 12, SEEK_CUR);
|
|
154
|
|
155 /* Number of colors in palette */
|
|
156 read (fd, &bi_clrused, 4);
|
|
157 bi_clrused = VISUAL_ENDIAN_LEI32 (bi_clrused);
|
|
158
|
|
159 /* Skip over the other nonsense */
|
|
160 lseek (fd, 4, SEEK_CUR);
|
|
161 }
|
|
162
|
|
163 /* Check if we can handle it */
|
|
164 if (bi_bitcount != 8 && bi_bitcount != 24) {
|
|
165 visual_log (VISUAL_LOG_CRITICAL, "Only bitmaps with 8 bits or 24 bits per pixel are supported");
|
|
166
|
|
167 return -VISUAL_ERROR_BMP_NOT_SUPPORTED;
|
|
168 }
|
|
169
|
|
170 /* We only handle uncompressed bitmaps */
|
|
171 if (bi_compression != BI_RGB) {
|
|
172 visual_log (VISUAL_LOG_CRITICAL, "Only uncompressed bitmaps are supported");
|
|
173
|
|
174 return -VISUAL_ERROR_BMP_NOT_SUPPORTED;
|
|
175 }
|
|
176
|
|
177 /* Load the palette */
|
|
178 if (bi_bitcount == 8) {
|
|
179 if (bi_clrused == 0)
|
|
180 bi_clrused = 256;
|
|
181
|
|
182 if (video->pal == NULL)
|
|
183 visual_object_unref (VISUAL_OBJECT (video->pal));
|
|
184
|
|
185 video->pal = visual_palette_new (bi_clrused);
|
|
186
|
|
187 if (bi_size == 12) {
|
|
188 for (i = 0; i < bi_clrused; i++) {
|
|
189 read (fd, &video->pal->colors[i].b, 1);
|
|
190 read (fd, &video->pal->colors[i].g, 1);
|
|
191 read (fd, &video->pal->colors[i].r, 1);
|
|
192 }
|
|
193 } else {
|
|
194 for (i = 0; i < bi_clrused; i++) {
|
|
195 read (fd, &video->pal->colors[i].b, 1);
|
|
196 read (fd, &video->pal->colors[i].g, 1);
|
|
197 read (fd, &video->pal->colors[i].r, 1);
|
|
198 lseek (fd, 1, SEEK_CUR);
|
|
199 }
|
|
200 }
|
|
201 }
|
|
202
|
|
203 /* Make the target VisVideo ready for use */
|
|
204 visual_video_set_depth (video, visual_video_depth_enum_from_value (bi_bitcount));
|
|
205 visual_video_set_dimension (video, bi_width, bi_height);
|
|
206 visual_video_allocate_buffer (video);
|
|
207
|
|
208 /* Set to the beginning of image data, note that MickeySoft likes stuff upside down .. */
|
|
209 lseek (fd, bf_bits, SEEK_SET);
|
|
210
|
|
211 pad = ((video->pitch % 4) ? (4 - (video->pitch % 4)) : 0);
|
|
212
|
|
213 data = video->pixels + (video->height * video->pitch);
|
|
214 while (data > (uint8_t *) video->pixels) {
|
|
215 data -= video->pitch;
|
|
216
|
|
217 if (read (fd, data, video->pitch) != video->pitch) {
|
|
218 visual_log (VISUAL_LOG_CRITICAL, "Bitmap data is not complete");
|
|
219
|
|
220 visual_video_free_buffer (video);
|
|
221
|
|
222 return -VISUAL_ERROR_BMP_CORRUPTED;
|
|
223 }
|
|
224
|
|
225 #if !VISUAL_LITTLE_ENDIAN
|
|
226 switch (bi_bitcount) {
|
|
227 case 24: {
|
|
228 int i, p;
|
|
229 for (p=0, i=0; i<bi_width; i++){
|
|
230 # if VISUAL_BIG_ENDIAN
|
|
231 uint8_t c[2];
|
|
232
|
|
233 c[0] = data[p];
|
|
234 c[1] = data[p+2];
|
|
235
|
|
236 data[p] = c[1];
|
|
237 data[p+2] = c[0];
|
|
238
|
|
239 p+=3;
|
|
240 # else
|
|
241 # error todo
|
|
242 # endif
|
|
243 }
|
|
244 break;
|
|
245 }
|
|
246 default:
|
|
247 visual_log (VISUAL_LOG_CRITICAL, "Internal error.");
|
|
248 }
|
|
249 #endif
|
|
250
|
|
251 if (pad != 0) {
|
|
252 lseek (fd, 4, pad);
|
|
253 }
|
|
254 }
|
|
255
|
|
256 close (fd);
|
|
257
|
|
258 return VISUAL_OK;
|
|
259 }
|
|
260
|
|
261 /**
|
|
262 * Loads a bitmap into a VisVideo and return this, so it's not needed to
|
|
263 * allocate a VisVideo before by hand.
|
|
264 *
|
|
265 * @see visual_bitmap_load
|
|
266 *
|
|
267 * @param filename The filename of the bitmap to be loaded.
|
|
268 *
|
|
269 * @return The VisVideo containing the bitmap or NULL on failure.
|
|
270 */
|
|
271 VisVideo *visual_bitmap_load_new_video (const char *filename)
|
|
272 {
|
|
273 VisVideo *video;
|
|
274
|
|
275 video = visual_video_new ();
|
|
276
|
|
277 if (visual_bitmap_load (video, filename) < 0) {
|
|
278 visual_object_unref (VISUAL_OBJECT (video));
|
|
279
|
|
280 return NULL;
|
|
281 }
|
|
282
|
|
283 return video;
|
|
284 }
|
|
285
|
|
286 /**
|
|
287 * @}
|
|
288 */
|
|
289
|