Skip to content

Latest commit

 

History

History
499 lines (422 loc) · 12.5 KB

IMG_lbm.c

File metadata and controls

499 lines (422 loc) · 12.5 KB
 
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Dec 31, 2011
Dec 31, 2011
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
Dec 31, 2011
Dec 31, 2011
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Dec 14, 2001
Dec 14, 2001
21
22
23
/* This is a ILBM image file loading framework
Load IFF pictures, PBM & ILBM packing methods, with or without stencil
Sep 15, 2004
Sep 15, 2004
24
25
26
Written by Daniel Morais ( Daniel AT Morais DOT com ) in September 2001.
24 bits ILBM files support added by Marc Le Douarain (http://www.multimania.com/mavati)
in December 2002.
Jan 4, 2004
Jan 4, 2004
27
EHB and HAM (specific Amiga graphic chip modes) support added by Marc Le Douarain
Sep 15, 2004
Sep 15, 2004
28
29
(http://www.multimania.com/mavati) in December 2003.
Stencil and colorkey fixes by David Raulo (david.raulo AT free DOT fr) in February 2004.
Jan 3, 2008
Jan 3, 2008
30
Buffer overflow fix in RLE decompression by David Raulo in January 2008.
31
32
33
34
*/
#include <stdio.h>
#include <stdlib.h>
Dec 17, 2001
Dec 17, 2001
35
#include <string.h>
36
37
38
39
40
41
42
43
44
#include "SDL_endian.h"
#include "SDL_image.h"
#ifdef LOAD_LBM
#define MAXCOLORS 256
Nov 20, 2001
Nov 20, 2001
45
/* Structure for an IFF picture ( BMHD = Bitmap Header ) */
46
47
48
typedef struct
{
Nov 20, 2001
Nov 20, 2001
49
50
51
52
53
54
55
56
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 */
May 11, 2010
May 11, 2010
57
yAspect;
Nov 20, 2001
Nov 20, 2001
58
59
Sint16 Lpage; /* width of the screen in pixels */
Sint16 Hpage; /* height of the screen in pixels */
60
61
62
63
} BMHD;
int IMG_isLBM( SDL_RWops *src )
{
Feb 3, 2013
Feb 3, 2013
64
Sint64 start;
65
66
67
int is_LBM;
Uint8 magic[4+4+4];
Feb 13, 2007
Feb 13, 2007
68
69
if ( !src )
return 0;
Feb 4, 2006
Feb 4, 2006
70
start = SDL_RWtell(src);
71
is_LBM = 0;
Feb 4, 2006
Feb 4, 2006
72
if ( SDL_RWread( src, magic, sizeof(magic), 1 ) )
Feb 3, 2013
Feb 3, 2013
74
75
76
if ( !SDL_memcmp( magic, "FORM", 4 ) &&
( !SDL_memcmp( magic + 8, "PBM ", 4 ) ||
!SDL_memcmp( magic + 8, "ILBM", 4 ) ) )
77
78
79
80
{
is_LBM = 1;
}
}
Nov 8, 2009
Nov 8, 2009
81
SDL_RWseek(src, start, RW_SEEK_SET);
82
83
84
85
86
return( is_LBM );
}
SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
{
Feb 3, 2013
Feb 3, 2013
87
Sint64 start;
88
89
SDL_Surface *Image;
Uint8 id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
Jan 20, 2003
Jan 20, 2003
90
Uint32 size, bytesloaded, nbcolors;
May 11, 2010
May 11, 2010
91
Uint32 i, j, bytesperline, nbplanes, stencil, plane, h;
92
Uint32 remainingbytes;
Feb 26, 2003
Feb 26, 2003
93
Uint32 width;
94
95
BMHD bmhd;
char *error;
Jan 4, 2004
Jan 4, 2004
96
Uint8 flagHAM,flagEHB;
97
98
99
100
101
Image = NULL;
error = NULL;
MiniBuf = NULL;
Jan 4, 2004
Jan 4, 2004
102
103
104
105
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Feb 4, 2006
Feb 4, 2006
106
107
start = SDL_RWtell(src);
Jan 4, 2004
Jan 4, 2004
108
if ( !SDL_RWread( src, id, 4, 1 ) )
Jan 20, 2003
Jan 20, 2003
110
error="error reading IFF chunk";
111
112
113
goto done;
}
Nov 20, 2001
Nov 20, 2001
114
115
/* Should be the size of the file minus 4+4 ( 'FORM'+size ) */
if ( !SDL_RWread( src, &size, 4, 1 ) )
Jan 20, 2003
Jan 20, 2003
117
error="error reading IFF chunk size";
118
119
120
goto done;
}
Nov 20, 2001
Nov 20, 2001
121
/* As size is not used here, no need to swap it */
Jan 20, 2003
Jan 20, 2003
122
123
if ( memcmp( id, "FORM", 4 ) != 0 )
Jan 20, 2003
Jan 20, 2003
125
error="not a IFF file";
126
127
128
goto done;
}
Jan 20, 2003
Jan 20, 2003
129
if ( !SDL_RWread( src, id, 4, 1 ) )
Jan 20, 2003
Jan 20, 2003
131
error="error reading IFF chunk";
132
133
134
135
136
goto done;
}
pbm = 0;
Nov 20, 2001
Nov 20, 2001
137
/* File format : PBM=Packed Bitmap, ILBM=Interleaved Bitmap */
Feb 3, 2013
Feb 3, 2013
138
139
if ( !SDL_memcmp( id, "PBM ", 4 ) ) pbm = 1;
else if ( SDL_memcmp( id, "ILBM", 4 ) )
Jan 20, 2003
Jan 20, 2003
141
error="not a IFF picture";
142
143
144
145
146
goto done;
}
nbcolors = 0;
Feb 3, 2013
Feb 3, 2013
147
SDL_memset( &bmhd, 0, sizeof( BMHD ) );
Jan 4, 2004
Jan 4, 2004
148
149
flagHAM = 0;
flagEHB = 0;
Feb 3, 2013
Feb 3, 2013
151
while ( SDL_memcmp( id, "BODY", 4 ) != 0 )
152
153
154
{
if ( !SDL_RWread( src, id, 4, 1 ) )
{
Jan 20, 2003
Jan 20, 2003
155
error="error reading IFF chunk";
156
157
158
goto done;
}
Jan 20, 2003
Jan 20, 2003
159
if ( !SDL_RWread( src, &size, 4, 1 ) )
Jan 20, 2003
Jan 20, 2003
161
error="error reading IFF chunk size";
162
163
164
165
166
167
goto done;
}
bytesloaded = 0;
size = SDL_SwapBE32( size );
Jan 20, 2003
Jan 20, 2003
168
Feb 3, 2013
Feb 3, 2013
169
if ( !SDL_memcmp( id, "BMHD", 4 ) ) /* Bitmap header */
170
171
172
{
if ( !SDL_RWread( src, &bmhd, sizeof( BMHD ), 1 ) )
{
Jan 20, 2003
Jan 20, 2003
173
error="error reading BMHD chunk";
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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 );
}
Feb 3, 2013
Feb 3, 2013
188
if ( !SDL_memcmp( id, "CMAP", 4 ) ) /* palette ( Color Map ) */
189
190
191
{
if ( !SDL_RWread( src, &colormap, size, 1 ) )
{
Jan 20, 2003
Jan 20, 2003
192
error="error reading CMAP chunk";
193
194
195
196
197
198
199
goto done;
}
bytesloaded = size;
nbcolors = size / 3;
}
Jan 4, 2004
Jan 4, 2004
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
if ( !memcmp( id, "CAMG", 4 ) ) /* Amiga ViewMode */
{
Uint32 viewmodes;
if ( !SDL_RWread( src, &viewmodes, sizeof(viewmodes), 1 ) )
{
error="error reading CAMG chunk";
goto done;
}
bytesloaded = size;
viewmodes = SDL_SwapBE32( viewmodes );
if ( viewmodes & 0x0800 )
flagHAM = 1;
if ( viewmodes & 0x0080 )
flagEHB = 1;
}
Feb 3, 2013
Feb 3, 2013
217
if ( SDL_memcmp( id, "BODY", 4 ) )
Jan 20, 2003
Jan 20, 2003
219
if ( size & 1 ) ++size; /* padding ! */
220
size -= bytesloaded;
Nov 20, 2001
Nov 20, 2001
221
/* skip the remaining bytes of this chunk */
Nov 8, 2009
Nov 8, 2009
222
if ( size ) SDL_RWseek( src, size, RW_SEEK_CUR );
223
224
225
}
}
Nov 20, 2001
Nov 20, 2001
226
/* compute some usefull values, based on the bitmap header */
Nov 20, 2001
Nov 20, 2001
228
width = ( bmhd.w + 15 ) & 0xFFFFFFF0; /* Width in pixels modulo 16 */
Nov 20, 2001
Nov 20, 2001
230
bytesperline = ( ( bmhd.w + 15 ) / 16 ) * 2;
Nov 20, 2001
Nov 20, 2001
232
nbplanes = bmhd.planes;
Nov 20, 2001
Nov 20, 2001
234
if ( pbm ) /* File format : 'Packed Bitmap' */
Jan 20, 2003
Jan 20, 2003
236
bytesperline *= 8;
Jul 15, 2007
Jul 15, 2007
237
nbplanes = 1;
238
239
}
May 11, 2010
May 11, 2010
240
stencil = (bmhd.mask & 1); /* There is a mask ( 'stencil' ) */
Nov 20, 2001
Nov 20, 2001
242
243
/* Allocate memory for a temporary buffer ( used for
decompression/deinterleaving ) */
Feb 3, 2013
Feb 3, 2013
245
MiniBuf = (Uint8 *)SDL_malloc( bytesperline * (nbplanes + stencil) );
May 11, 2010
May 11, 2010
246
if ( MiniBuf == NULL )
Jan 20, 2003
Jan 20, 2003
248
error="no enough memory for temporary buffer";
249
250
251
goto done;
}
Jan 4, 2004
Jan 4, 2004
252
if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, (bmhd.planes==24 || flagHAM==1)?24:8, 0, 0, 0, 0 ) ) == NULL )
253
254
goto done;
Feb 9, 2004
Feb 9, 2004
255
if ( bmhd.mask & 2 ) /* There is a transparent color */
Jan 23, 2012
Jan 23, 2012
256
SDL_SetColorKey( Image, SDL_TRUE, bmhd.tcolor );
Feb 9, 2004
Feb 9, 2004
257
Nov 20, 2001
Nov 20, 2001
258
/* Update palette informations */
Jan 20, 2003
Jan 20, 2003
260
/* There is no palette in 24 bits ILBM file */
Jan 4, 2004
Jan 4, 2004
261
if ( nbcolors>0 && flagHAM==0 )
Jan 20, 2003
Jan 20, 2003
262
{
May 11, 2010
May 11, 2010
263
264
/* FIXME: Should this include the stencil? See comment below */
int nbrcolorsfinal = 1 << (nbplanes + stencil);
Jan 20, 2003
Jan 20, 2003
265
ptr = &colormap[0];
Jan 20, 2003
Jan 20, 2003
267
268
269
270
271
272
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++;
}
Jan 4, 2004
Jan 4, 2004
273
274
275
276
/* Amiga EHB mode (Extra-Half-Bright) */
/* 6 bitplanes mode with a 32 colors palette */
/* The 32 last colors are the same but divided by 2 */
Feb 9, 2004
Feb 9, 2004
277
/* Some Amiga pictures save 64 colors with 32 last wrong colors, */
Jan 4, 2004
Jan 4, 2004
278
279
280
/* they shouldn't !, and here we overwrite these 32 bad colors. */
if ( (nbcolors==32 || flagEHB ) && (1<<bmhd.planes)==64 )
{
Feb 9, 2004
Feb 9, 2004
281
nbcolors = 64;
Jan 4, 2004
Jan 4, 2004
282
283
284
285
286
287
288
289
ptr = &colormap[0];
for ( i=32; i<64; i++ )
{
Image->format->palette->colors[i].r = (*ptr++)/2;
Image->format->palette->colors[i].g = (*ptr++)/2;
Image->format->palette->colors[i].b = (*ptr++)/2;
}
}
Feb 9, 2004
Feb 9, 2004
290
291
292
/* If nbcolors < 2^nbplanes, repeat the colormap */
/* This happens when pictures have a stencil mask */
Sep 15, 2004
Sep 15, 2004
293
294
295
if ( nbrcolorsfinal > (1<<bmhd.planes) ) {
nbrcolorsfinal = (1<<bmhd.planes);
}
Nov 15, 2004
Nov 15, 2004
296
for ( i=nbcolors; i < (Uint32)nbrcolorsfinal; i++ )
Feb 9, 2004
Feb 9, 2004
297
298
299
300
301
{
Image->format->palette->colors[i].r = Image->format->palette->colors[i%nbcolors].r;
Image->format->palette->colors[i].g = Image->format->palette->colors[i%nbcolors].g;
Image->format->palette->colors[i].b = Image->format->palette->colors[i%nbcolors].b;
}
Jul 20, 2007
Jul 20, 2007
302
303
if ( !pbm )
Image->format->palette->ncolors = nbrcolorsfinal;
304
305
}
Nov 20, 2001
Nov 20, 2001
306
/* Get the bitmap */
307
308
309
for ( h=0; h < bmhd.h; h++ )
{
Jan 20, 2003
Jan 20, 2003
310
311
/* uncompress the datas of each planes */
May 11, 2010
May 11, 2010
312
for ( plane=0; plane < (nbplanes+stencil); plane++ )
Jan 20, 2003
Jan 20, 2003
314
315
ptr = MiniBuf + ( plane * bytesperline );
316
remainingbytes = bytesperline;
Jan 20, 2003
Jan 20, 2003
317
Nov 20, 2001
Nov 20, 2001
318
if ( bmhd.tcomp == 1 ) /* Datas are compressed */
Jan 20, 2003
Jan 20, 2003
320
do
Jan 20, 2003
Jan 20, 2003
322
if ( !SDL_RWread( src, &count, 1, 1 ) )
Jan 20, 2003
Jan 20, 2003
324
error="error reading BODY chunk";
325
326
goto done;
}
Jan 20, 2003
Jan 20, 2003
327
328
329
if ( count & 0x80 )
{
Jan 20, 2003
Jan 20, 2003
330
331
332
count ^= 0xFF;
count += 2; /* now it */
Jan 3, 2008
Jan 3, 2008
333
if ( ( count > remainingbytes ) || !SDL_RWread( src, &color, 1, 1 ) )
May 11, 2010
May 11, 2010
335
error="error reading BODY chunk";
336
337
goto done;
}
Feb 3, 2013
Feb 3, 2013
338
SDL_memset( ptr, color, count );
339
340
341
}
else
{
Jan 20, 2003
Jan 20, 2003
342
++count;
Jan 3, 2008
Jan 3, 2008
344
if ( ( count > remainingbytes ) || !SDL_RWread( src, ptr, count, 1 ) )
345
346
347
348
349
{
error="error reading BODY chunk";
goto done;
}
}
Jan 20, 2003
Jan 20, 2003
350
351
352
ptr += count;
remainingbytes -= count;
Jan 20, 2003
Jan 20, 2003
353
354
355
} while ( remainingbytes > 0 );
}
Jan 20, 2003
Jan 20, 2003
356
else
Jan 20, 2003
Jan 20, 2003
358
if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
Jan 20, 2003
Jan 20, 2003
360
error="error reading BODY chunk";
361
362
363
364
goto done;
}
}
}
Jan 20, 2003
Jan 20, 2003
365
366
/* One line has been read, store it ! */
Feb 3, 2013
Feb 3, 2013
368
ptr = (Uint8 *)Image->pixels;
Jan 4, 2004
Jan 4, 2004
369
if ( nbplanes==24 || flagHAM==1 )
Jan 20, 2003
Jan 20, 2003
370
371
372
373
ptr += h * width * 3;
else
ptr += h * width;
Nov 20, 2001
Nov 20, 2001
374
if ( pbm ) /* File format : 'Packed Bitmap' */
375
376
377
{
memcpy( ptr, MiniBuf, width );
}
Nov 20, 2001
Nov 20, 2001
378
else /* We have to un-interlace the bits ! */
Jan 4, 2004
Jan 4, 2004
380
if ( nbplanes!=24 && flagHAM==0 )
Jan 20, 2003
Jan 20, 2003
381
382
383
384
385
386
387
{
size = ( width + 7 ) / 8;
for ( i=0; i < size; i++ )
{
memset( ptr, 0, 8 );
May 11, 2010
May 11, 2010
388
for ( plane=0; plane < (nbplanes + stencil); plane++ )
Jan 20, 2003
Jan 20, 2003
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
{
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;
}
}
else
Jan 4, 2004
Jan 4, 2004
406
Uint32 finalcolor = 0;
Jan 20, 2003
Jan 20, 2003
407
408
size = ( width + 7 ) / 8;
/* 24 bitplanes ILBM : R0...R7,G0...G7,B0...B7 */
Jan 4, 2004
Jan 4, 2004
409
/* or HAM (6 bitplanes) or HAM8 (8 bitplanes) modes */
Jan 20, 2003
Jan 20, 2003
410
for ( i=0; i<width; i=i+8 )
Jan 20, 2003
Jan 20, 2003
412
Uint8 maskBit = 0x80;
413
414
for ( j=0; j<8; j++ )
{
Jan 4, 2004
Jan 4, 2004
415
416
Uint32 pixelcolor = 0;
Uint32 maskColor = 1;
Jan 20, 2003
Jan 20, 2003
417
418
419
420
421
Uint8 dataBody;
for ( plane=0; plane < nbplanes; plane++ )
{
dataBody = MiniBuf[ plane*size+i/8 ];
if ( dataBody&maskBit )
Jan 4, 2004
Jan 4, 2004
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
pixelcolor = pixelcolor | maskColor;
maskColor = maskColor<<1;
}
/* HAM : 12 bits RGB image (4 bits per color component) */
/* HAM8 : 18 bits RGB image (6 bits per color component) */
if ( flagHAM )
{
switch( pixelcolor>>(nbplanes-2) )
{
case 0: /* take direct color from palette */
finalcolor = colormap[ pixelcolor*3 ] + (colormap[ pixelcolor*3+1 ]<<8) + (colormap[ pixelcolor*3+2 ]<<16);
break;
case 1: /* modify only blue component */
finalcolor = finalcolor&0x00FFFF;
finalcolor = finalcolor | (pixelcolor<<(16+(10-nbplanes)));
break;
case 2: /* modify only red component */
finalcolor = finalcolor&0xFFFF00;
finalcolor = finalcolor | pixelcolor<<(10-nbplanes);
break;
case 3: /* modify only green component */
finalcolor = finalcolor&0xFF00FF;
finalcolor = finalcolor | (pixelcolor<<(8+(10-nbplanes)));
break;
}
}
else
{
finalcolor = pixelcolor;
Jan 20, 2003
Jan 20, 2003
451
}
Feb 3, 2013
Feb 3, 2013
452
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 11, 2006
May 11, 2006
453
454
455
*ptr++ = (Uint8)(finalcolor>>16);
*ptr++ = (Uint8)(finalcolor>>8);
*ptr++ = (Uint8)(finalcolor);
Feb 3, 2013
Feb 3, 2013
456
457
#else
*ptr++ = (Uint8)(finalcolor);
May 11, 2006
May 11, 2006
458
459
*ptr++ = (Uint8)(finalcolor>>8);
*ptr++ = (Uint8)(finalcolor>>16);
Feb 3, 2013
Feb 3, 2013
460
#endif
Jan 20, 2003
Jan 20, 2003
461
maskBit = maskBit>>1;
462
463
464
465
466
467
468
469
}
}
}
}
}
done:
Apr 25, 2013
Apr 25, 2013
470
if ( MiniBuf ) SDL_free( MiniBuf );
Jan 20, 2003
Jan 20, 2003
472
if ( error )
Nov 8, 2009
Nov 8, 2009
474
SDL_RWseek(src, start, RW_SEEK_SET);
Feb 4, 2006
Feb 4, 2006
475
476
477
478
if ( Image ) {
SDL_FreeSurface( Image );
Image = NULL;
}
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
IMG_SetError( error );
}
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 */