annotate libvisual/lv_bmp.c @ 25:66610fc7de2d trunk

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