ifftools.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <clib/exec_protos.h>
  55. #include <exec/memory.h>
  56. #include <graphics/gfx.h>
  57. #include <stdio.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. int Plane;
  105. int PlaneSize;
  106. if (Bitmap != NULL) {
  107. PlaneSize = Bitmap->BytesPerRow * Bitmap->Rows;
  108. for (Plane = 0; Plane < Bitmap->Depth; Plane++) {
  109. if (Bitmap->Planes[Plane] != NULL) {
  110. FreeMem(Bitmap->Planes[Plane], PlaneSize);
  111. Bitmap->Planes[Plane] = NULL;
  112. }
  113. }
  114. FreeMem(Bitmap, sizeof(struct BitMap));
  115. }
  116. }
  117. /* =============================================================================
  118. * FUNCTION: ReadIff
  119. */
  120. struct BitMap *ReadIff(char *fname, ULONG *palette) {
  121. struct BitMap *bitmap; /* pointer to the bitmap to be read */
  122. ChunkHeader ckhd; /* chunk header */
  123. BitMapHeader bmhd; /* bitmap header */
  124. struct BFile *file; /* pointer to buffered file to be read */
  125. PLANEPTR planes[8]; /* quick access to bitplane pointers */
  126. PLANEPTR plane_ptr; /* pointer to the row to be read */
  127. BYTE inbyte1;
  128. BYTE inbyte2;
  129. UBYTE byte3[3]; /* colour map data buffer (rgb triplet */
  130. long byte4; /* 4 byte ID */
  131. short j;
  132. short bytes_per_row; /* number of bytes in each row of the pic */
  133. short bytes_this_row; /* bytes read into the current row so far */
  134. short plane_no; /* bit plane currently being read */
  135. short count; /* number of bytes to place in bitmap */
  136. long size; /* number of bytes read in from file */
  137. long row_offset; /* offset for the start of this row */
  138. long current_row; /* current row being read */
  139. long plane_size; /* bit plane size */
  140. long i;
  141. file = BOpen(fname, MODE_OLDFILE);
  142. if (file == NULL) {
  143. printf("Can't open file %s.\n", fname);
  144. return NULL;
  145. }
  146. size = BRead(file, (UBYTE *)&ckhd, (long)sizeof(ChunkHeader));
  147. if (ckhd.ckID != FORM) {
  148. printf("Not a form!!\n", ckhd.ckID);
  149. BClose(file);
  150. return NULL;
  151. }
  152. size = BRead(file, (UBYTE *)&byte4, 4L);
  153. if (byte4 != ILBM) {
  154. printf("Not an ILBM form!!\n");
  155. BClose(file);
  156. return NULL;
  157. }
  158. bitmap = (struct BitMap *)AllocMem((long)sizeof(struct BitMap),
  159. MEMF_CHIP | MEMF_CLEAR);
  160. while (size != 0L) {
  161. /* Read in a chunk header */
  162. size = BRead(file, (UBYTE *)&ckhd, (long)sizeof(ChunkHeader));
  163. switch (ckhd.ckID) {
  164. case BMHD: {
  165. size = BRead(file, (UBYTE *)&bmhd, (long)sizeof(BitMapHeader));
  166. break;
  167. }
  168. case BODY: {
  169. bytes_per_row = ((bmhd.w + 15) >> 4) << 1;
  170. plane_size = ((long)bytes_per_row) * ((long)bmhd.h);
  171. bitmap->BytesPerRow = bytes_per_row;
  172. bitmap->Rows = bmhd.h;
  173. bitmap->Depth = bmhd.nPlanes;
  174. for (j = 0; j < bmhd.nPlanes; j++) {
  175. bitmap->Planes[j] =
  176. (PLANEPTR)AllocMem(plane_size, MEMF_CHIP | MEMF_CLEAR);
  177. planes[j] = bitmap->Planes[j];
  178. }
  179. row_offset = 0L;
  180. for (current_row = 0L; current_row < ((long)bmhd.h); current_row++) {
  181. for (plane_no = 0; plane_no < bmhd.nPlanes; plane_no++) {
  182. plane_ptr = planes[plane_no] + row_offset;
  183. if (bmhd.compression == cmpByteRun1) {
  184. bytes_this_row = 0;
  185. while (bytes_this_row < bytes_per_row) {
  186. size = BRead(file, &inbyte1, 1L);
  187. /*
  188. ** If the byte 'N' read is greater not negative then
  189. ** take the next N bytes verbatim.
  190. */
  191. if (inbyte1 >= 0) {
  192. count = inbyte1 + 1;
  193. size = BRead(file, (plane_ptr + bytes_this_row), (long)count);
  194. bytes_this_row += count;
  195. } else {
  196. if (inbyte1 != -128) {
  197. count = 1 - inbyte1;
  198. size = BRead(file, &inbyte2, 1L);
  199. for (j = 0; j < count; j++) {
  200. plane_ptr[bytes_this_row] = inbyte2;
  201. bytes_this_row++;
  202. }
  203. }
  204. }
  205. }
  206. } else
  207. size = BRead(file, plane_ptr, (long)bytes_per_row);
  208. }
  209. row_offset += (long)bytes_per_row;
  210. }
  211. while (size != 0L)
  212. size = BRead(file, &inbyte1, 1L);
  213. break;
  214. }
  215. case CMAP: {
  216. for (j = 0L, i = 0L; i < ckhd.ckSize; j++, i += 3L) {
  217. size = BRead(file, byte3, 3L);
  218. palette[j] = (byte3[0] << 16) + (byte3[1] << 8) + byte3[2];
  219. }
  220. if ((ckhd.ckSize % 2L) == 1L)
  221. size = BSeek(file, 1L, OFFSET_CURRENT);
  222. break;
  223. }
  224. default: {
  225. size = BSeek(file, (long)ckhd.ckSize, OFFSET_CURRENT);
  226. break;
  227. }
  228. }
  229. }
  230. BClose(file);
  231. return bitmap;
  232. }