/
IMG_lbm.c
356 lines (281 loc) · 7.74 KB
1
/*
2
3
SDL_image: An example image loading library for use with SDL
Copyright (C) 1999, 2000, 2001 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
23
/* $Id$ */
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* This is a ILBM image file loading framework
Load IFF pictures, PBM & ILBM packing methods, with or without stencil
Written by Daniel Morais ( Daniel@Morais.com ) in September 2001
*/
#include <stdio.h>
#include <stdlib.h>
#include "SDL_endian.h"
#include "SDL_image.h"
#ifdef LOAD_LBM
#define MAXCOLORS 256
41
/* Structure for an IFF picture ( BMHD = Bitmap Header ) */
42
43
44
typedef struct
{
45
46
47
48
49
50
51
52
Uint16 w, h; /* width & height of the bitmap in pixels */
Sint16 x, y; /* screen coordinates of the bitmap */
Uint8 planes; /* number of planes of the bitmap */
Uint8 mask; /* mask type ( 0 => no mask ) */
Uint8 tcomp; /* compression type */
Uint8 pad1; /* dummy value, for padding */
Uint16 tcolor; /* transparent color */
Uint8 xAspect, /* pixel aspect ratio */
53
yAspect;
54
55
Sint16 Lpage; /* width of the screen in pixels */
Sint16 Hpage; /* height of the screen in pixels */
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
} BMHD;
int IMG_isLBM( SDL_RWops *src )
{
int is_LBM;
Uint8 magic[4+4+4];
is_LBM = 0;
if ( SDL_RWread( src, magic, 4+4+4, 1 ) )
{
if ( !memcmp( magic, "FORM", 4 ) &&
( !memcmp( magic + 8, "PBM ", 4 ) ||
!memcmp( magic + 8, "ILBM", 4 ) ) )
{
is_LBM = 1;
}
}
return( is_LBM );
}
SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
{
SDL_Surface *Image;
Uint8 id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
Uint32 size, bytesloaded, nbcolors;
Uint32 i, j, bytesperline, nbplanes, plane, h;
Uint32 remainingbytes;
int width;
BMHD bmhd;
char *error;
Image = NULL;
error = NULL;
MiniBuf = NULL;
if ( src == NULL ) goto done;
if ( !SDL_RWread( src, id, 4, 1 ) )
{
error="error reading IFF chunk";
goto done;
}
99
100
/* Should be the size of the file minus 4+4 ( 'FORM'+size ) */
if ( !SDL_RWread( src, &size, 4, 1 ) )
101
102
103
104
105
{
error="error reading IFF chunk size";
goto done;
}
106
/* As size is not used here, no need to swap it */
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
if ( memcmp( id, "FORM", 4 ) != 0 )
{
error="not a IFF file";
goto done;
}
if ( !SDL_RWread( src, id, 4, 1 ) )
{
error="error reading IFF chunk";
goto done;
}
pbm = 0;
122
123
/* File format : PBM=Packed Bitmap, ILBM=Interleaved Bitmap */
if ( !memcmp( id, "PBM ", 4 ) ) pbm = 1;
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
else if ( memcmp( id, "ILBM", 4 ) )
{
error="not a IFF picture";
goto done;
}
nbcolors = 0;
memset( &bmhd, 0, sizeof( BMHD ) );
while ( memcmp( id, "BODY", 4 ) != 0 )
{
if ( !SDL_RWread( src, id, 4, 1 ) )
{
error="error reading IFF chunk";
goto done;
}
if ( !SDL_RWread( src, &size, 4, 1 ) )
{
error="error reading IFF chunk size";
goto done;
}
bytesloaded = 0;
size = SDL_SwapBE32( size );
152
if ( !memcmp( id, "BMHD", 4 ) ) /* Bitmap header */
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{
if ( !SDL_RWread( src, &bmhd, sizeof( BMHD ), 1 ) )
{
error="error reading BMHD chunk";
goto done;
}
bytesloaded = sizeof( BMHD );
bmhd.w = SDL_SwapBE16( bmhd.w );
bmhd.h = SDL_SwapBE16( bmhd.h );
bmhd.x = SDL_SwapBE16( bmhd.x );
bmhd.y = SDL_SwapBE16( bmhd.y );
bmhd.tcolor = SDL_SwapBE16( bmhd.tcolor );
bmhd.Lpage = SDL_SwapBE16( bmhd.Lpage );
bmhd.Hpage = SDL_SwapBE16( bmhd.Hpage );
}
171
if ( !memcmp( id, "CMAP", 4 ) ) /* palette ( Color Map ) */
172
173
174
175
176
177
178
179
180
181
182
183
184
{
if ( !SDL_RWread( src, &colormap, size, 1 ) )
{
error="error reading CMAP chunk";
goto done;
}
bytesloaded = size;
nbcolors = size / 3;
}
if ( memcmp( id, "BODY", 4 ) )
{
185
if ( size & 1 ) ++size; /* padding ! */
186
size -= bytesloaded;
187
188
/* skip the remaining bytes of this chunk */
if ( size ) SDL_RWseek( src, size, SEEK_CUR );
189
190
191
}
}
192
/* compute some usefull values, based on the bitmap header */
193
194
width = ( bmhd.w + 15 ) & 0xFFFFFFF0; /* Width in pixels modulo 16 */
195
196
bytesperline = ( ( bmhd.w + 15 ) / 16 ) * 2;
197
198
nbplanes = bmhd.planes;
199
200
if ( pbm ) /* File format : 'Packed Bitmap' */
201
202
203
204
205
{
bytesperline *= 8;
nbplanes = 1;
}
206
if ( bmhd.mask ) ++nbplanes; /* There is a mask ( 'stencil' ) */
207
208
209
/* Allocate memory for a temporary buffer ( used for
decompression/deinterleaving ) */
210
211
212
213
214
215
216
217
218
219
if ( ( MiniBuf = (void *)malloc( bytesperline * nbplanes ) ) == NULL )
{
error="no enough memory for temporary buffer";
goto done;
}
if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, 8, 0, 0, 0, 0 ) ) == NULL )
goto done;
220
/* Update palette informations */
221
222
223
224
225
226
227
228
229
230
231
232
Image->format->palette->ncolors = nbcolors;
ptr = &colormap[0];
for ( i=0; i<nbcolors; i++ )
{
Image->format->palette->colors[i].r = *ptr++;
Image->format->palette->colors[i].g = *ptr++;
Image->format->palette->colors[i].b = *ptr++;
}
233
/* Get the bitmap */
234
235
236
for ( h=0; h < bmhd.h; h++ )
{
237
/* uncompress the datas of each planes */
238
239
240
241
242
243
244
for ( plane=0; plane < nbplanes; plane++ )
{
ptr = MiniBuf + ( plane * bytesperline );
remainingbytes = bytesperline;
245
if ( bmhd.tcomp == 1 ) /* Datas are compressed */
246
247
248
249
250
251
252
253
254
255
256
257
{
do
{
if ( !SDL_RWread( src, &count, 1, 1 ) )
{
error="error reading BODY chunk";
goto done;
}
if ( count & 0x80 )
{
count ^= 0xFF;
258
count += 2; /* now it */
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
if ( !SDL_RWread( src, &color, 1, 1 ) )
{
error="error reading BODY chunk";
goto done;
}
memset( ptr, color, count );
}
else
{
++count;
if ( !SDL_RWread( src, ptr, count, 1 ) )
{
error="error reading BODY chunk";
goto done;
}
}
ptr += count;
remainingbytes -= count;
} while ( remainingbytes > 0 );
}
else
{
if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
{
error="error reading BODY chunk";
goto done;
}
}
}
293
/* One line has been read, store it ! */
294
295
296
297
ptr = Image->pixels;
ptr += h * width;
298
if ( pbm ) /* File format : 'Packed Bitmap' */
299
300
301
{
memcpy( ptr, MiniBuf, width );
}
302
else /* We have to un-interlace the bits ! */
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
{
size = ( width + 7 ) / 8;
for ( i=0; i < size; i++ )
{
memset( ptr, 0, 8 );
for ( plane=0; plane < nbplanes; plane++ )
{
color = *( MiniBuf + i + ( plane * bytesperline ) );
msk = 0x80;
for ( j=0; j<8; j++ )
{
if ( ( plane + j ) <= 7 ) ptr[j] |= (Uint8)( color & msk ) >> ( 7 - plane - j );
else ptr[j] |= (Uint8)( color & msk ) << ( plane + j - 7 );
msk >>= 1;
}
}
ptr += 8;
}
}
}
done:
if ( MiniBuf ) free( MiniBuf );
if ( error )
{
IMG_SetError( error );
SDL_FreeSurface( Image );
Image = NULL;
}
return( Image );
}
#else /* LOAD_LBM */
/* See if an image is contained in a data source */
int IMG_isLBM(SDL_RWops *src)
{
return(0);
}
/* Load an IFF type image from an SDL datasource */
SDL_Surface *IMG_LoadLBM_RW(SDL_RWops *src)
{
return(NULL);
}
#endif /* LOAD_LBM */