IMG_lbm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. SDL_image: An example image loading library for use with SDL
  3. Copyright (C) 1999, 2000, 2001 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. */
  18. /* $Id: IMG_lbm.c,v 1.1 2004/07/21 16:24:11 paigoddess Exp $ */
  19. /* This is a ILBM image file loading framework
  20. Load IFF pictures, PBM & ILBM packing methods, with or without stencil
  21. Written by Daniel Morais ( Daniel@Morais.com ) in September 2001.
  22. 24 bits ILBM files support added by Marc Le Douarain (mavati@club-internet.fr)
  23. in December 2002.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "SDL_endian.h"
  29. #include "SDL_image.h"
  30. #define MAXCOLORS 256
  31. /* Structure for an IFF picture ( BMHD = Bitmap Header ) */
  32. typedef struct
  33. {
  34. Uint16 w, h; /* width & height of the bitmap in pixels */
  35. Sint16 x, y; /* screen coordinates of the bitmap */
  36. Uint8 planes; /* number of planes of the bitmap */
  37. Uint8 mask; /* mask type ( 0 => no mask ) */
  38. Uint8 tcomp; /* compression type */
  39. Uint8 pad1; /* dummy value, for padding */
  40. Uint16 tcolor; /* transparent color */
  41. Uint8 xAspect, /* pixel aspect ratio */
  42. yAspect;
  43. Sint16 Lpage; /* width of the screen in pixels */
  44. Sint16 Hpage; /* height of the screen in pixels */
  45. } BMHD;
  46. int IMG_isLBM( SDL_RWops *src )
  47. {
  48. int is_LBM;
  49. Uint8 magic[4+4+4];
  50. is_LBM = 0;
  51. if ( SDL_RWread( src, magic, 4+4+4, 1 ) )
  52. {
  53. if ( !memcmp( magic, "FORM", 4 ) &&
  54. ( !memcmp( magic + 8, "PBM ", 4 ) ||
  55. !memcmp( magic + 8, "ILBM", 4 ) ) )
  56. {
  57. is_LBM = 1;
  58. }
  59. }
  60. return( is_LBM );
  61. }
  62. SDL_Surface *IMG_LoadLBM_RW( SDL_RWops *src )
  63. {
  64. SDL_Surface *Image;
  65. Uint8 id[4], pbm, colormap[MAXCOLORS*3], *MiniBuf, *ptr, count, color, msk;
  66. Uint32 size, bytesloaded, nbcolors;
  67. Uint32 i, j, bytesperline, nbplanes, plane, h;
  68. Uint32 remainingbytes;
  69. Uint32 width;
  70. BMHD bmhd;
  71. char *error;
  72. Image = NULL;
  73. error = NULL;
  74. MiniBuf = NULL;
  75. if ( src == NULL ) goto done;
  76. if ( !SDL_RWread( src, id, 4, 1 ) )
  77. {
  78. error="error reading IFF chunk";
  79. goto done;
  80. }
  81. /* Should be the size of the file minus 4+4 ( 'FORM'+size ) */
  82. if ( !SDL_RWread( src, &size, 4, 1 ) )
  83. {
  84. error="error reading IFF chunk size";
  85. goto done;
  86. }
  87. /* As size is not used here, no need to swap it */
  88. if ( memcmp( id, "FORM", 4 ) != 0 )
  89. {
  90. error="not a IFF file";
  91. goto done;
  92. }
  93. if ( !SDL_RWread( src, id, 4, 1 ) )
  94. {
  95. error="error reading IFF chunk";
  96. goto done;
  97. }
  98. pbm = 0;
  99. /* File format : PBM=Packed Bitmap, ILBM=Interleaved Bitmap */
  100. if ( !memcmp( id, "PBM ", 4 ) ) pbm = 1;
  101. else if ( memcmp( id, "ILBM", 4 ) )
  102. {
  103. error="not a IFF picture";
  104. goto done;
  105. }
  106. nbcolors = 0;
  107. memset( &bmhd, 0, sizeof( BMHD ) );
  108. while ( memcmp( id, "BODY", 4 ) != 0 )
  109. {
  110. if ( !SDL_RWread( src, id, 4, 1 ) )
  111. {
  112. error="error reading IFF chunk";
  113. goto done;
  114. }
  115. if ( !SDL_RWread( src, &size, 4, 1 ) )
  116. {
  117. error="error reading IFF chunk size";
  118. goto done;
  119. }
  120. bytesloaded = 0;
  121. size = SDL_SwapBE32( size );
  122. if ( !memcmp( id, "BMHD", 4 ) ) /* Bitmap header */
  123. {
  124. if ( !SDL_RWread( src, &bmhd, sizeof( BMHD ), 1 ) )
  125. {
  126. error="error reading BMHD chunk";
  127. goto done;
  128. }
  129. bytesloaded = sizeof( BMHD );
  130. bmhd.w = SDL_SwapBE16( bmhd.w );
  131. bmhd.h = SDL_SwapBE16( bmhd.h );
  132. bmhd.x = SDL_SwapBE16( bmhd.x );
  133. bmhd.y = SDL_SwapBE16( bmhd.y );
  134. bmhd.tcolor = SDL_SwapBE16( bmhd.tcolor );
  135. bmhd.Lpage = SDL_SwapBE16( bmhd.Lpage );
  136. bmhd.Hpage = SDL_SwapBE16( bmhd.Hpage );
  137. }
  138. if ( !memcmp( id, "CMAP", 4 ) ) /* palette ( Color Map ) */
  139. {
  140. if ( !SDL_RWread( src, &colormap, size, 1 ) )
  141. {
  142. error="error reading CMAP chunk";
  143. goto done;
  144. }
  145. bytesloaded = size;
  146. nbcolors = size / 3;
  147. }
  148. if ( memcmp( id, "BODY", 4 ) )
  149. {
  150. if ( size & 1 ) ++size; /* padding ! */
  151. size -= bytesloaded;
  152. /* skip the remaining bytes of this chunk */
  153. if ( size ) SDL_RWseek( src, size, SEEK_CUR );
  154. }
  155. }
  156. /* compute some usefull values, based on the bitmap header */
  157. width = ( bmhd.w + 15 ) & 0xFFFFFFF0; /* Width in pixels modulo 16 */
  158. bytesperline = ( ( bmhd.w + 15 ) / 16 ) * 2;
  159. nbplanes = bmhd.planes;
  160. if ( pbm ) /* File format : 'Packed Bitmap' */
  161. {
  162. bytesperline *= 8;
  163. nbplanes = 1;
  164. }
  165. if ( bmhd.mask ) ++nbplanes; /* There is a mask ( 'stencil' ) */
  166. /* Allocate memory for a temporary buffer ( used for
  167. decompression/deinterleaving ) */
  168. if ( ( MiniBuf = (Uint8 *)malloc( bytesperline * nbplanes ) ) == NULL )
  169. {
  170. error="no enough memory for temporary buffer";
  171. goto done;
  172. }
  173. if ( ( Image = SDL_CreateRGBSurface( SDL_SWSURFACE, width, bmhd.h, bmhd.planes==24?24:8, 0, 0, 0, 0 ) ) == NULL )
  174. goto done;
  175. /* Update palette informations */
  176. /* There is no palette in 24 bits ILBM file */
  177. if ( nbcolors>0 )
  178. {
  179. Image->format->palette->ncolors = nbcolors;
  180. ptr = &colormap[0];
  181. for ( i=0; i<nbcolors; i++ )
  182. {
  183. Image->format->palette->colors[i].r = *ptr++;
  184. Image->format->palette->colors[i].g = *ptr++;
  185. Image->format->palette->colors[i].b = *ptr++;
  186. }
  187. }
  188. /* Get the bitmap */
  189. for ( h=0; h < bmhd.h; h++ )
  190. {
  191. /* uncompress the datas of each planes */
  192. for ( plane=0; plane < nbplanes; plane++ )
  193. {
  194. ptr = MiniBuf + ( plane * bytesperline );
  195. remainingbytes = bytesperline;
  196. if ( bmhd.tcomp == 1 ) /* Datas are compressed */
  197. {
  198. do
  199. {
  200. if ( !SDL_RWread( src, &count, 1, 1 ) )
  201. {
  202. error="error reading BODY chunk";
  203. goto done;
  204. }
  205. if ( count & 0x80 )
  206. {
  207. count ^= 0xFF;
  208. count += 2; /* now it */
  209. if ( !SDL_RWread( src, &color, 1, 1 ) )
  210. {
  211. error="error reading BODY chunk";
  212. goto done;
  213. }
  214. memset( ptr, color, count );
  215. }
  216. else
  217. {
  218. ++count;
  219. if ( !SDL_RWread( src, ptr, count, 1 ) )
  220. {
  221. error="error reading BODY chunk";
  222. goto done;
  223. }
  224. }
  225. ptr += count;
  226. remainingbytes -= count;
  227. } while ( remainingbytes > 0 );
  228. }
  229. else
  230. {
  231. if ( !SDL_RWread( src, ptr, bytesperline, 1 ) )
  232. {
  233. error="error reading BODY chunk";
  234. goto done;
  235. }
  236. }
  237. }
  238. /* One line has been read, store it ! */
  239. ptr = (Uint8*)Image->pixels;
  240. if ( nbplanes==24 )
  241. ptr += h * width * 3;
  242. else
  243. ptr += h * width;
  244. if ( pbm ) /* File format : 'Packed Bitmap' */
  245. {
  246. memcpy( ptr, MiniBuf, width );
  247. }
  248. else /* We have to un-interlace the bits ! */
  249. {
  250. if ( nbplanes!=24 )
  251. {
  252. size = ( width + 7 ) / 8;
  253. for ( i=0; i < size; i++ )
  254. {
  255. memset( ptr, 0, 8 );
  256. for ( plane=0; plane < nbplanes; plane++ )
  257. {
  258. color = *( MiniBuf + i + ( plane * bytesperline ) );
  259. msk = 0x80;
  260. for ( j=0; j<8; j++ )
  261. {
  262. if ( ( plane + j ) <= 7 ) ptr[j] |= (Uint8)( color & msk ) >> ( 7 - plane - j );
  263. else ptr[j] |= (Uint8)( color & msk ) << ( plane + j - 7 );
  264. msk >>= 1;
  265. }
  266. }
  267. ptr += 8;
  268. }
  269. }
  270. else
  271. {
  272. size = ( width + 7 ) / 8;
  273. /* 24 bitplanes ILBM : R0...R7,G0...G7,B0...B7 */
  274. for ( i=0; i<width; i=i+8 )
  275. {
  276. Uint8 maskBit = 0x80;
  277. for ( j=0; j<8; j++ )
  278. {
  279. Uint32 color24 = 0;
  280. Uint32 maskColor24 = 1;
  281. Uint8 dataBody;
  282. for ( plane=0; plane < nbplanes; plane++ )
  283. {
  284. dataBody = MiniBuf[ plane*size+i/8 ];
  285. if ( dataBody&maskBit )
  286. color24 = color24 | maskColor24;
  287. maskColor24 = maskColor24<<1;
  288. }
  289. if ( SDL_BYTEORDER == SDL_LIL_ENDIAN )
  290. {
  291. *ptr++ = color24>>16;
  292. *ptr++ = color24>>8;
  293. *ptr++ = color24;
  294. }
  295. else
  296. {
  297. *ptr++ = color24;
  298. *ptr++ = color24>>8;
  299. *ptr++ = color24>>16;
  300. }
  301. maskBit = maskBit>>1;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. done:
  308. if ( MiniBuf ) free( MiniBuf );
  309. if ( error )
  310. {
  311. IMG_SetError( error );
  312. SDL_FreeSurface( Image );
  313. Image = NULL;
  314. }
  315. return( Image );
  316. }