Debian Codegraph @

bitmap/iff.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/bitmap/iff.cpp $
21 * $Revision: 8 $
22 * $Date: 10/21/99 9:28p $
23 * $Author: Jeff $
24 *
25 * Code to read IFF files
26 *
27 * $Log: /DescentIII/Main/bitmap/iff.cpp $
28 *
29 * 8 10/21/99 9:28p Jeff
30 * B.A. Macintosh code merge
31 *
32 * 7 4/14/99 1:07a Jeff
33 * fixed case mismatched #includes
34 *
35 * 6 10/08/98 4:23p Kevin
36 * Changed code to comply with memory library usage. Always use mem_malloc
37 * , mem_free and mem_strdup
38 *
39 * 5 4/23/98 6:38p Jason
40 * made bitmaps use 1555 format
41 *
42 * 4 3/19/98 3:18p Samir
43 * enforce constant char* arguments when needed. done in CFILE and bitmap
44 * libraries as well as ddio.
45 *
46 * 3 12/22/97 7:34p Samir
47 * Removed instances of gr.h include. Replaced with grdefs.h
48 *
49 * 2 10/15/97 5:20p Jason
50 * did a HUGE overhaul of the bitmap system
51 *
52 * 11 3/07/97 4:02p Samir
53 * Took out conio.h and malloc.h for ANSI compliance.
54 *
55 * 10 3/07/97 1:02p Jason
56 * decreased size of bitmap headers array to compensate for stupid mac
57 * problem
58 *
59 * 9 3/03/97 6:20p Matt
60 * Changed cfile functions to use D3 naming convention
61 *
62 * $NoKeywords: $
63 */
64
65 #define COMPRESS 1 // do the RLE or not? (for debugging mostly)
66 #define WRITE_TINY 0 // should we write a TINY chunk?
67
68 #define MIN_COMPRESS_WIDTH 65 // don't compress if less than this wide
69
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73
74 #include "mem.h"
75 #include "iff.h"
76 #include "byteswap.h"
77 #include "cfile.h"
78 #include "pserror.h"
79 #include "pstypes.h"
80 #include "bitmap.h"
81 #include "mono.h"
82 #include "grdefs.h"
83
84 // Internal constants and structures for this library
85
86 // Compression types
87 #define cmpNone 0
88 #define cmpByteRun1 1
89
90 // Masking types
91 #define mskNone 0
92 #define mskHasMask 1
93 #define mskHasTransparentColor 2
94
95 // Palette entry structure
96 struct pal_entry {
97 uint8_t r, g, b;
98 };
99
100 // structure of the header in the file
101 struct iff_bitmap_header {
102 int16_t w, h; // width and height of this bitmap
103 int16_t x, y; // generally unused
104 int16_t type; // see types above
105 int16_t transparentcolor; // which color is transparent (if any)
106 int16_t pagewidth, pageheight; // width & height of source screen
107 uint8_t nplanes; // number of planes (8 for 256 color image)
108 uint8_t masking, compression; // see constants above
109 uint8_t xaspect, yaspect; // aspect ratio (usually 5/6)
110 pal_entry palette[256]; // the palette for this bitmap
111 uint8_t *raw_data; // ptr to array of data
112 int16_t row_size; // offset to next row
113 };
114
115 int16_t iff_transparent_color;
116 int16_t iff_has_transparency; // 0=no transparency, 1=iff_transparent_color is valid
117
118 #define MIN(a, b) ((a < b) ? a : b)
119
120 #define MAKE_SIG(a, b, c, d) (((int32_t)(a) << 24) + ((int32_t)(b) << 16) + ((c) << 8) + (d))
121
122 #define IFF_SIG_FORM 1
123 #define IFF_SIG_ILBM 2
124 #define IFF_SIG_BODY 3
125 #define IFF_SIG_BMHD 4
126 #define IFF_SIG_CMAP 5
127 #define IFF_SIG_UNKNOWN 6
128 #define IFF_SIG_PBM 7
129 #define IFF_SIG_ANIM 8
130 #define IFF_SIG_DELTA 9
131 #define IFF_SIG_ANHD 10
132
133 static int bm_iff_get_sig(CFILE *f);
134 static int bm_iff_parse_bmhd(CFILE *ifile, uint32_t len, iff_bitmap_header *bmheader);
135
136 /// the buffer pointed to by raw_data is stuffed with a pointer to decompressed pixel data
137 static int bm_iff_parse_body(CFILE *ifile, int len, iff_bitmap_header *bmheader);
138
139 /// the buffer pointed to by raw_data is stuffed with a pointer to bitplane pixel data
140 static void bm_iff_skip_chunk(CFILE *ifile, uint32_t len);
141
142 /// modify passed bitmap
143 static int bm_iff_parse_delta(CFILE *ifile, int len, iff_bitmap_header *bmheader);
144
145 static int bm_iff_parse_file(CFILE *ifile, iff_bitmap_header *bmheader, iff_bitmap_header *prev_bm);
146 static void bm_iff_convert_8_to_16(int dest_bm, iff_bitmap_header *iffbm);
147
148 int bm_iff_get_sig(CFILE *f) {
149 char s[4];
150 int i;
151
152 for (i = 0; i < 4; i++)
153 s[i] = cf_ReadByte(f);
154
155 if (!strncmp("ILBM", s, 4))
156 return IFF_SIG_ILBM;
157 if (!strncmp("BODY", s, 4))
158 return IFF_SIG_BODY;
159 if (!strncmp("CMAP", s, 4))
160 return IFF_SIG_CMAP;
161 if (!strncmp("BMHD", s, 4))
162 return IFF_SIG_BMHD;
163 if (!strncmp("FORM", s, 4))
164 return IFF_SIG_FORM;
165 if (!strncmp("PBM ", s, 4))
166 return IFF_SIG_PBM;
167 if (!strncmp("ANIM", s, 4))
168 return IFF_SIG_ANIM;
169 if (!strncmp("DLTA", s, 4))
170 return IFF_SIG_DELTA;
171 if (!strncmp("ANHD", s, 4))
172 return IFF_SIG_ANHD;
173
174 return (IFF_SIG_UNKNOWN);
175 }
176 int bm_iff_parse_bmhd(CFILE *ifile, uint32_t len, iff_bitmap_header *bmheader) {
177 len = len;
178
179 bmheader->w = cf_ReadShort(ifile, false);
180 bmheader->h = cf_ReadShort(ifile, false);
181 bmheader->x = cf_ReadShort(ifile, false);
182 bmheader->y = cf_ReadShort(ifile, false);
183
184 bmheader->nplanes = cf_ReadByte(ifile);
185 bmheader->masking = cf_ReadByte(ifile);
186 bmheader->compression = cf_ReadByte(ifile);
187 cf_ReadByte(ifile); /* skip pad */
188
189 bmheader->transparentcolor = cf_ReadShort(ifile, false);
190 bmheader->xaspect = cf_ReadByte(ifile);
191 bmheader->yaspect = cf_ReadByte(ifile);
192
193 bmheader->pagewidth = cf_ReadShort(ifile, false);
194 bmheader->pageheight = cf_ReadShort(ifile, false);
195
196 iff_transparent_color = bmheader->transparentcolor;
197
198 iff_has_transparency = 0;
199
200 if (bmheader->masking == mskHasTransparentColor)
201 iff_has_transparency = 1;
202
203 else if (bmheader->masking != mskNone && bmheader->masking != mskHasMask)
204 return IFF_UNKNOWN_MASK;
205
206 return IFF_NO_ERROR;
207 }
208
209 // the buffer pointed to by raw_data is stuffed with a pointer to decompressed pixel data
210 int bm_iff_parse_body(CFILE *ifile, int len, iff_bitmap_header *bmheader) {
211 uint8_t *p = bmheader->raw_data;
212 int width = 0, depth = 0, done = 0;
213
214 if (bmheader->type == TYPE_PBM) {
215 width = bmheader->w;
216 depth = 1;
217 } else if (bmheader->type == TYPE_ILBM) {
218 width = (bmheader->w + 7) / 8;
219 depth = bmheader->nplanes;
220 }
221
222 if (bmheader->compression == cmpNone) // no compression
223 {
224 for (int y = 0; y < bmheader->h; y++) {
225 int x;
226
227 for (x = 0; x < width * depth; x++)
228 *p++ = cf_ReadByte(ifile);
229
230 if (bmheader->masking == mskHasMask) {
231 for (int i = 0; i < width; i++)
232 cf_ReadByte(ifile); // skip mask!
233 }
234
235 if (bmheader->w & 1)
236 cf_ReadByte(ifile);
237 }
238
239 } else if (bmheader->compression == cmpByteRun1) // compression
240 {
241 uint8_t *data_end = p + (bmheader->h * depth * width);
242 uint8_t mask = (bmheader->masking == mskHasMask);
243 int cur_width = 0, skip_mask = 0;
244 int command;
245 int plane = 0;
246
247 while (!done) {
248 if (p >= data_end) {
249 done = 1;
250 continue;
251 }
252 if (cur_width == width) {
253 plane++;
254 if ((plane == depth && !mask) || (plane == depth + 1 && mask)) {
255 skip_mask = 0;
256 plane = 0;
257 }
258
259 if (mask && plane == depth)
260 skip_mask = 1;
261
262 cur_width = 0;
263 }
264
265 command = cf_ReadByte(ifile);
266 if (command >= 0 && command <= 127) {
267 if (!skip_mask) {
268 for (int i = 0; i < command + 1; i++)
269 *p++ = cf_ReadByte(ifile);
270 } else {
271 for (int i = 0; i < (command + 1); i++)
272 cf_ReadByte(ifile);
273 }
274
275 cur_width += (command + 1);
276 } else if (command >= -127 && command < 0) {
277 int run = (-command) + 1;
278 int repeat_byte = cf_ReadByte(ifile);
279
280 if (!skip_mask) {
281 for (int i = 0; i < run; i++)
282 *p++ = repeat_byte;
283 }
284
285 cur_width += run;
286 }
287 }
288 }
289
290 return IFF_NO_ERROR;
291 }
292
293 // the buffer pointed to by raw_data is stuffed with a pointer to bitplane pixel data
294 void bm_iff_skip_chunk(CFILE *ifile, uint32_t len) {
295 uint32_t i;
296
297 for (i = 0; i < len; i++)
298 cf_ReadByte(ifile);
299 }
300
301 // modify passed bitmap
302 int bm_iff_parse_delta(CFILE *ifile, int len, iff_bitmap_header *bmheader) {
303 uint8_t *p = bmheader->raw_data;
304 int y;
305 int32_t chunk_end = cftell(ifile) + len;
306
307 cf_ReadInt(ifile); // longword, seems to be equal to 4. Don't know what it is
308
309 for (y = 0; y < bmheader->h; y++) {
310 uint8_t n_items;
311 int cnt = bmheader->w;
312 uint8_t code;
313
314 n_items = cf_ReadByte(ifile);
315
316 while (n_items--) {
317 code = cf_ReadByte(ifile);
318
319 if (code == 0) {
320 uint8_t rep, val;
321
322 rep = cf_ReadByte(ifile);
323 val = cf_ReadByte(ifile);
324
325 cnt -= rep;
326 if (cnt == -1)
327 rep--;
328 while (rep--)
329 *p++ = val;
330 } else if (code > 0x80) { // skip
331 cnt -= (code - 0x80);
332 p += (code - 0x80);
333 if (cnt == -1)
334 p--;
335 } else { // literal
336 cnt -= code;
337 if (cnt == -1)
338 code--;
339
340 while (code--)
341 *p++ = cf_ReadByte(ifile);
342
343 if (cnt == -1)
344 cf_ReadByte(ifile);
345 }
346 }
347
348 if (cnt == -1) {
349 if (!bmheader->w & 1)
350 return IFF_CORRUPT;
351 } else if (cnt)
352 return IFF_CORRUPT;
353 }
354
355 if (cftell(ifile) == chunk_end - 1) // pad
356 cf_ReadByte(ifile);
357
358 if (cftell(ifile) != chunk_end) {
359 Int3();
360 return IFF_CORRUPT;
361 }
362
363 else
364 return IFF_NO_ERROR;
365 }
366
367 // read an PBM
368 // Pass pointer to opened file, and to empty bitmap_header structure, and form length
369 int bm_iff_parse_file(CFILE *ifile, iff_bitmap_header *bmheader, iff_bitmap_header *prev_bm) {
370 uint32_t sig, len;
371 int done = 0;
372
373 while (!done) {
374 if (cfeof(ifile)) {
375 done = 1;
376 continue;
377 }
378
379 sig = bm_iff_get_sig(ifile);
380
381 len = cf_ReadInt(ifile, false);
382
383 switch (sig) {
384 case IFF_SIG_FORM: {
385 int newsig = bm_iff_get_sig(ifile);
386 bmheader->type = TYPE_PBM;
387 break;
388 }
389 case IFF_SIG_BMHD: {
390 int ret;
391
392 ret = bm_iff_parse_bmhd(ifile, len, bmheader);
393 if (ret != IFF_NO_ERROR)
394 return ret;
395 else {
396
397 bmheader->raw_data = (uint8_t *)mem_malloc(bmheader->w * bmheader->h);
398 if (!bmheader->raw_data)
399 return IFF_NO_MEM;
400 }
401
402 } break;
403 case IFF_SIG_ANHD: {
404
405 if (!prev_bm) {
406 Int3();
407 return IFF_CORRUPT;
408 }
409
410 bmheader->w = prev_bm->w;
411 bmheader->h = prev_bm->h;
412 bmheader->type = prev_bm->type;
413 bmheader->raw_data = (uint8_t *)mem_malloc(bmheader->w * bmheader->h);
414
415 if (!bmheader->raw_data)
416 return IFF_NO_MEM;
417
418 memcpy(bmheader->raw_data, prev_bm->raw_data, bmheader->w * bmheader->h);
419
420 if (len & 1)
421 len++;
422 bm_iff_skip_chunk(ifile, len);
423
424 break;
425 }
426
427 case IFF_SIG_CMAP: {
428 int ncolors = (int)(len / 3), cnum;
429 uint8_t r, g, b;
430
431 for (cnum = 0; cnum < ncolors; cnum++) {
432 r = cf_ReadByte(ifile);
433 g = cf_ReadByte(ifile);
434 b = cf_ReadByte(ifile);
435 r >>= 2;
436 bmheader->palette[cnum].r = r;
437 g >>= 2;
438 bmheader->palette[cnum].g = g;
439 b >>= 2;
440 bmheader->palette[cnum].b = b;
441 }
442 if (len & 1)
443 cf_ReadByte(ifile);
444
445 } break;
446
447 case IFF_SIG_BODY: {
448 int r;
449 if ((r = bm_iff_parse_body(ifile, len, bmheader)) != IFF_NO_ERROR)
450 return r;
451 done = 1;
452 break;
453 }
454 case IFF_SIG_DELTA: {
455 int r;
456 if ((r = bm_iff_parse_delta(ifile, len, bmheader)) != IFF_NO_ERROR)
457 return r;
458 done = 1;
459 break;
460 }
461
462 default:
463 // Don't know this chunk
464 if (len & 1)
465 len++;
466 bm_iff_skip_chunk(ifile, len);
467 break;
468 }
469 }
470
471 return IFF_NO_ERROR; /* ok! */
472 }
473
474 void bm_iff_convert_8_to_16(int dest_bm, iff_bitmap_header *iffbm) {
475 ASSERT(bm_w(dest_bm, 0) == iffbm->w);
476 ASSERT(bm_h(dest_bm, 0) == iffbm->h);
477
478 uint16_t *data;
479
480 data = (uint16_t *)bm_data(dest_bm, 0);
481
482 for (int i = 0; i < iffbm->h; i++)
483 for (int t = 0; t < iffbm->w; t++) {
484 uint16_t pixel;
485 uint8_t c = iffbm->raw_data[i * iffbm->w + t];
486
487 int r = iffbm->palette[c].r >> 1;
488 int g = iffbm->palette[c].g >> 1;
489 int b = iffbm->palette[c].b >> 1;
490
491 pixel = OPAQUE_FLAG | (r << 10) | (g << 5) | b;
492 if (c == iffbm->transparentcolor)
493 pixel = NEW_TRANSPARENT_COLOR;
494
495 data[i * bm_w(dest_bm, 0) + t] = pixel;
496 }
497 }
498
499 // Loads an iff into a structure, allocs bitmap memory and converts 8 bit iff file into
500 // 16bit bitmap
501 // Returns bitmap handle on success, or -1 if failed
502 int bm_iff_alloc_file(CFILE *ifile) {
503 int ret; // return code
504 iff_bitmap_header bmheader;
505 int src_bm;
506 char cur_sig[4];
507
508 // Ignore FORM and form length
509 cf_ReadInt(ifile);
510 cf_ReadInt(ifile);
511
512 // check if this an ILBM
513
514 for (int i = 0; i < 4; i++)
515 cur_sig[i] = cf_ReadByte(ifile);
516
517 if (strncmp("PBM ", cur_sig, 4)) {
518 mprintf(0, "IFF file isn't a PBM...aborting.\n");
519 return -1;
520 }
521 if (!strncmp("PBM ", cur_sig, 4))
522 bmheader.type = TYPE_PBM;
523 else
524 Int3(); // Huh? Get Jason!
525
526 ret = bm_iff_parse_file(ifile, &bmheader, NULL);
527
528 if (ret != IFF_NO_ERROR) {
529 mprintf(0, "Couldn't load IFF file.\n");
530 return -1;
531 }
532
533 // Alloc our bitmap
534 src_bm = bm_AllocBitmap(bmheader.w, bmheader.h, 0);
535 if (src_bm < 0) {
536 mem_free(bmheader.raw_data);
537 return -1;
538 }
539
540 // Convert our 8 bit bitmap to 16bit
541 bm_iff_convert_8_to_16(src_bm, &bmheader);
542 free(bmheader.raw_data);
543
544 return src_bm;
545 }
546
547 // returns number of bitmaps or -1 on error
548 int bm_iff_read_animbrush(const char *ifilename, int *bm_list) {
549 CFILE *ifile;
550 iff_bitmap_header bm_headers[40];
551 iff_bitmap_header *temp_bm_head;
552 int32_t sig, form_len;
553 int32_t form_type;
554 int num_bitmaps = 0;
555 int ret, i;
556
557 for (int t = 0; t < 40; t++)
558 bm_headers[t].raw_data = 0;
559
560 ifile = cfopen(ifilename, "rb");
561 if (!ifile)
562 return -1;
563
564 sig = bm_iff_get_sig(ifile);
565 form_len = cf_ReadInt(ifile, false);
566
567 if (sig != IFF_SIG_FORM) {
568 mprintf(0, "Not a valid IFF file.\n");
569 cfclose(ifile);
570 return -1;
571 }
572
573 form_type = bm_iff_get_sig(ifile);
574
575 if (form_type == IFF_SIG_ANIM) {
576 while (!cfeof(ifile) && num_bitmaps < 40) {
577 ret = bm_iff_parse_file(ifile, &bm_headers[num_bitmaps], num_bitmaps > 0 ? &bm_headers[num_bitmaps - 1] : NULL);
578
579 if (ret != IFF_NO_ERROR)
580 goto done;
581
582 num_bitmaps++;
583 }
584
585 } else {
586 cfclose(ifile);
587 return -1;
588 }
589
590 done:
591 cfclose(ifile);
592
593 for (i = 0; i < num_bitmaps; i++) {
594 int src_bm;
595 temp_bm_head = &bm_headers[i];
596
597 memcpy(temp_bm_head->palette, bm_headers[0].palette, sizeof(pal_entry) * 256);
598 temp_bm_head->transparentcolor = bm_headers[0].transparentcolor;
599
600 src_bm = bm_AllocBitmap(temp_bm_head->w, temp_bm_head->h, 0);
601 ASSERT(src_bm > 0);
602
603 bm_iff_convert_8_to_16(src_bm, temp_bm_head);
604 bm_list[i] = src_bm;
605 }
606
607 for (i = 0; i < num_bitmaps; i++)
608 mem_free(bm_headers[i].raw_data);
609
610 return num_bitmaps;
611 }