ifftools.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* =============================================================================
  2. * PROGRAM: CODE LIBRARY
  3. * FILENAME: ifftools.c
  4. *
  5. * DESCRIPTION:
  6. * This module provides functions to read IFF ILBM files and free the bitmaps
  7. * for ILBM files read.
  8. *
  9. * =============================================================================
  10. * COPYRIGHT:
  11. *
  12. * Copyright (c) 1996, 2004, Julian Olds
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * . Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. *
  22. * . Redistributions in binary form must reproduce the above copyright
  23. * notice, this list of conditions and the following disclaimer in the
  24. * documentation and/or other materials provided with the distribution.
  25. *
  26. * The name of the author may not be used to endorse or promote products
  27. * derived from this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  33. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  39. * THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * =============================================================================
  42. * EXPORTED VARIABLES
  43. *
  44. * None.
  45. *
  46. * =============================================================================
  47. * EXPORTED FUNCTIONS
  48. *
  49. * FreeBitmap : This function frees the bitmap for an ILBM file.
  50. * ReadIff : This function reads an IFF ILBM file and returns the bitmap.
  51. *
  52. * =============================================================================
  53. */
  54. #include <stdio.h>
  55. #include <exec/memory.h>
  56. #include <graphics/gfx.h>
  57. #include <clib/exec_protos.h>
  58. #include "bio.h"
  59. #include "ifftools.h"
  60. /* =======================================================================
  61. ** Definitions
  62. */
  63. #define MAKE_ID(a,b,c,d)\
  64. ( ((long) (a)<<24) + ((long) (b)<<16) + ((long) (c)<<8) + ((long) (d)) )
  65. #define FORM MAKE_ID('F','O','R','M')
  66. #define ILBM MAKE_ID('I','L','B','M')
  67. #define BMHD MAKE_ID('B','M','H','D')
  68. #define CMAP MAKE_ID('C','M','A','P')
  69. #define GRAB MAKE_ID('G','R','A','B')
  70. #define DEST MAKE_ID('D','E','S','T')
  71. #define SPRT MAKE_ID('S','P','R','T')
  72. #define CAMG MAKE_ID('C','A','M','G')
  73. #define CRNG MAKE_ID('C','R','N','G')
  74. #define CCRT MAKE_ID('C','C','R','T')
  75. #define BODY MAKE_ID('B','O','D','Y')
  76. #define mskNone 0
  77. #define mskHasMask 1
  78. #define mskHasTransparentColour 2
  79. #define mskLasso 3
  80. #define cmpNone 0
  81. #define cmpByteRun1 1
  82. typedef struct {
  83. UWORD w,h;
  84. WORD x,y;
  85. UBYTE nPlanes;
  86. UBYTE masking;
  87. UBYTE compression;
  88. UBYTE pad1;
  89. UWORD transparentColour;
  90. UBYTE xAspect,yAspect;
  91. WORD pageWidth,pageHeight;
  92. } BitMapHeader;
  93. typedef struct {
  94. long ckID;
  95. long ckSize;
  96. } ChunkHeader;
  97. /* =============================================================================
  98. * Exported functions
  99. */
  100. /* =============================================================================
  101. * FUNCTION: FreeBitmap
  102. */
  103. void FreeBitmap(struct BitMap *Bitmap)
  104. {
  105. int Plane;
  106. int PlaneSize;
  107. if (Bitmap != NULL)
  108. {
  109. PlaneSize = Bitmap->BytesPerRow * Bitmap->Rows;
  110. for (Plane = 0 ; Plane < Bitmap->Depth ; Plane++)
  111. {
  112. if (Bitmap->Planes[Plane] != NULL)
  113. {
  114. FreeMem(Bitmap->Planes[Plane], PlaneSize);
  115. Bitmap->Planes[Plane] = NULL;
  116. }
  117. }
  118. FreeMem(Bitmap, sizeof(struct BitMap));
  119. }
  120. }
  121. /* =============================================================================
  122. * FUNCTION: ReadIff
  123. */
  124. struct BitMap *ReadIff(char *fname, ULONG *palette)
  125. {
  126. struct BitMap *bitmap; /* pointer to the bitmap to be read */
  127. ChunkHeader ckhd; /* chunk header */
  128. BitMapHeader bmhd; /* bitmap header */
  129. struct BFile *file; /* pointer to buffered file to be read */
  130. PLANEPTR planes[8]; /* quick access to bitplane pointers */
  131. PLANEPTR plane_ptr; /* pointer to the row to be read */
  132. BYTE inbyte1;
  133. BYTE inbyte2;
  134. UBYTE byte3[3]; /* colour map data buffer (rgb triplet */
  135. long byte4; /* 4 byte ID */
  136. short j;
  137. short bytes_per_row; /* number of bytes in each row of the pic */
  138. short bytes_this_row; /* bytes read into the current row so far */
  139. short plane_no; /* bit plane currently being read */
  140. short count; /* number of bytes to place in bitmap */
  141. long size; /* number of bytes read in from file */
  142. long row_offset; /* offset for the start of this row */
  143. long current_row; /* current row being read */
  144. long plane_size; /* bit plane size */
  145. long i;
  146. file = BOpen(fname, MODE_OLDFILE);
  147. if (file == NULL)
  148. {
  149. printf("Can't open file %s.\n",fname);
  150. return(NULL);
  151. }
  152. size = BRead(file, (UBYTE *) &ckhd, (long) sizeof(ChunkHeader));
  153. if (ckhd.ckID != FORM)
  154. {
  155. printf("Not a form!!\n", ckhd.ckID);
  156. BClose(file);
  157. return (NULL);
  158. }
  159. size = BRead(file, (UBYTE *) &byte4, 4L);
  160. if (byte4 != ILBM)
  161. {
  162. printf("Not an ILBM form!!\n");
  163. BClose(file);
  164. return(NULL);
  165. }
  166. bitmap = (struct BitMap *)
  167. AllocMem((long) sizeof(struct BitMap), MEMF_CHIP | MEMF_CLEAR);
  168. while (size != 0L)
  169. {
  170. /* Read in a chunk header */
  171. size = BRead(file, (UBYTE *) &ckhd, (long) sizeof(ChunkHeader));
  172. switch (ckhd.ckID)
  173. {
  174. case BMHD:
  175. {
  176. size = BRead(file, (UBYTE *) &bmhd, (long) sizeof(BitMapHeader));
  177. break;
  178. }
  179. case BODY:
  180. {
  181. bytes_per_row = ((bmhd.w+15)>>4)<<1;
  182. plane_size = ((long) bytes_per_row) * ((long) bmhd.h);
  183. bitmap->BytesPerRow = bytes_per_row;
  184. bitmap->Rows = bmhd.h;
  185. bitmap->Depth = bmhd.nPlanes;
  186. for(j = 0 ; j < bmhd.nPlanes ; j++)
  187. {
  188. bitmap->Planes[j] = (PLANEPTR)
  189. AllocMem(plane_size, MEMF_CHIP | MEMF_CLEAR);
  190. planes[j] = bitmap->Planes[j];
  191. }
  192. row_offset = 0L;
  193. for (current_row = 0L;current_row < ((long) bmhd.h);current_row++)
  194. {
  195. for(plane_no = 0 ; plane_no < bmhd.nPlanes ; plane_no++)
  196. {
  197. plane_ptr = planes[plane_no] + row_offset;
  198. if (bmhd.compression == cmpByteRun1)
  199. {
  200. bytes_this_row = 0;
  201. while (bytes_this_row < bytes_per_row)
  202. {
  203. size = BRead(file, &inbyte1, 1L);
  204. /*
  205. ** If the byte 'N' read is greater not negative then
  206. ** take the next N bytes verbatim.
  207. */
  208. if (inbyte1 >= 0)
  209. {
  210. count = inbyte1+1;
  211. size = BRead(file,
  212. (plane_ptr + bytes_this_row),
  213. (long) count);
  214. bytes_this_row += count;
  215. }
  216. else
  217. {
  218. if (inbyte1 != -128)
  219. {
  220. count = 1-inbyte1;
  221. size = BRead(file, &inbyte2, 1L);
  222. for(j = 0 ; j < count ; j++)
  223. {
  224. plane_ptr[bytes_this_row] = inbyte2;
  225. bytes_this_row++;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. else
  232. {
  233. size = BRead(file, plane_ptr,(long) bytes_per_row);
  234. }
  235. }
  236. row_offset += (long) bytes_per_row;
  237. }
  238. while (size != 0L)
  239. size = BRead(file, &inbyte1, 1L);
  240. break;
  241. }
  242. case CMAP:
  243. {
  244. for (j = 0L, i = 0L ; i < ckhd.ckSize ; j++, i += 3L)
  245. {
  246. size = BRead(file, byte3, 3L);
  247. palette[j] = (byte3[0]<<16) + (byte3[1]<<8) + byte3[2];
  248. }
  249. if ((ckhd.ckSize % 2L) == 1L)
  250. size = BSeek(file, 1L, OFFSET_CURRENT);
  251. break;
  252. }
  253. default :
  254. {
  255. size = BSeek(file, (long) ckhd.ckSize, OFFSET_CURRENT);
  256. break;
  257. }
  258. }
  259. }
  260. BClose(file);
  261. return (bitmap);
  262. }