| 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 COMPRESScxx . . $ `bitmap/iff.cpp:65:9`! 1 // do the RLE or not? (for debugging mostly) |
| 66 | #define WRITE_TINYcxx . . $ `bitmap/iff.cpp:66:9`! 0 // should we write a TINY chunk? |
| 67 | |
| 68 | #define MIN_COMPRESS_WIDTHcxx . . $ `bitmap/iff.cpp:68:9`! 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"cxx . . $ `<file>/mem/mem.h`/ |
| 75 | #include "iff.h"cxx . . $ `<file>/bitmap/iff.h`/ |
| 76 | #include "byteswap.h"cxx . . $ `<file>/lib/byteswap.h`/ |
| 77 | #include "cfile.h"cxx . . $ `<file>/cfile/cfile.h`/ |
| 78 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 79 | #include "pstypes.h"cxx . . $ `<file>/lib/pstypes.h`/ |
| 80 | #include "bitmap.h"cxx . . $ `<file>/lib/bitmap.h`/ |
| 81 | #include "mono.h"cxx . . $ `<file>/ddebug/mono.h`/ |
| 82 | #include "grdefs.h"cxx . . $ `<file>/lib/grdefs.h`/ |
| 83 | |
| 84 | // Internal constants and structures for this library |
| 85 | |
| 86 | // Compression types |
| 87 | #define cmpNonecxx . . $ `bitmap/iff.cpp:87:9`! 0 |
| 88 | #define cmpByteRun1cxx . . $ `bitmap/iff.cpp:88:9`! 1 |
| 89 | |
| 90 | // Masking types |
| 91 | #define mskNonecxx . . $ `bitmap/iff.cpp:91:9`! 0 |
| 92 | #define mskHasMaskcxx . . $ `bitmap/iff.cpp:92:9`! 1 |
| 93 | #define mskHasTransparentColorcxx . . $ `bitmap/iff.cpp:93:9`! 2 |
| 94 | |
| 95 | // Palette entry structure |
| 96 | struct pal_entrycxx . . $ pal_entry# { |
| 97 | uint8_tcxx . . $ uint8_t# rcxx . . $ pal_entry#r., gcxx . . $ pal_entry#g., bcxx . . $ pal_entry#b.; |
| 98 | }; |
| 99 | |
| 100 | // structure of the header in the file |
| 101 | struct iff_bitmap_headercxx . . $ iff_bitmap_header# { |
| 102 | int16_t wcxx . . $ iff_bitmap_header#w., hcxx . . $ iff_bitmap_header#h.; // width and height of this bitmap |
| 103 | int16_t xcxx . . $ iff_bitmap_header#x., ycxx . . $ iff_bitmap_header#y.; // generally unused |
| 104 | int16_t typecxx . . $ iff_bitmap_header#type.; // see types above |
| 105 | int16_t transparentcolorcxx . . $ iff_bitmap_header#transparentcolor.; // which color is transparent (if any) |
| 106 | int16_t pagewidthcxx . . $ iff_bitmap_header#pagewidth., pageheightcxx . . $ iff_bitmap_header#pageheight.; // width & height of source screen |
| 107 | uint8_tcxx . . $ uint8_t# nplanescxx . . $ iff_bitmap_header#nplanes.; // number of planes (8 for 256 color image) |
| 108 | uint8_tcxx . . $ uint8_t# maskingcxx . . $ iff_bitmap_header#masking., compressioncxx . . $ iff_bitmap_header#compression.; // see constants above |
| 109 | uint8_tcxx . . $ uint8_t# xaspectcxx . . $ iff_bitmap_header#xaspect., yaspectcxx . . $ iff_bitmap_header#yaspect.; // aspect ratio (usually 5/6) |
| 110 | pal_entrycxx . . $ pal_entry# palettecxx . . $ iff_bitmap_header#palette.[256]; // the palette for this bitmap |
| 111 | uint8_tcxx . . $ uint8_t# *raw_datacxx . . $ iff_bitmap_header#raw_data.; // ptr to array of data |
| 112 | int16_t row_sizecxx . . $ iff_bitmap_header#row_size.; // offset to next row |
| 113 | }; |
| 114 | |
| 115 | int16_t iff_transparent_colorcxx . . $ iff_transparent_color.; |
| 116 | int16_t iff_has_transparencycxx . . $ iff_has_transparency.; // 0=no transparency, 1=iff_transparent_color is valid |
| 117 | |
| 118 | #define MINcxx . . $ `bitmap/iff.cpp:118:9`!(a, b) ((a < b) ? a : b) |
| 119 | |
| 120 | #define MAKE_SIGcxx . . $ `bitmap/iff.cpp:120:9`!(a, b, c, d) (((int32_t)(a) << 24) + ((int32_t)(b) << 16) + ((c) << 8) + (d)) |
| 121 | |
| 122 | #define IFF_SIG_FORMcxx . . $ `bitmap/iff.cpp:122:9`! 1 |
| 123 | #define IFF_SIG_ILBMcxx . . $ `bitmap/iff.cpp:123:9`! 2 |
| 124 | #define IFF_SIG_BODYcxx . . $ `bitmap/iff.cpp:124:9`! 3 |
| 125 | #define IFF_SIG_BMHDcxx . . $ `bitmap/iff.cpp:125:9`! 4 |
| 126 | #define IFF_SIG_CMAPcxx . . $ `bitmap/iff.cpp:126:9`! 5 |
| 127 | #define IFF_SIG_UNKNOWNcxx . . $ `bitmap/iff.cpp:127:9`! 6 |
| 128 | #define IFF_SIG_PBMcxx . . $ `bitmap/iff.cpp:128:9`! 7 |
| 129 | #define IFF_SIG_ANIMcxx . . $ `bitmap/iff.cpp:129:9`! 8 |
| 130 | #define IFF_SIG_DELTAcxx . . $ `bitmap/iff.cpp:130:9`! 9 |
| 131 | #define IFF_SIG_ANHDcxx . . $ `bitmap/iff.cpp:131:9`! 10 |
| 132 | |
| 133 | static int bm_iff_get_sigcxx . . $ bm_iff_get_sig(413716a422df0036).(CFILEcxx . . $ CFILE# *f); |
| 134 | static int bm_iff_parse_bmhdcxx . . $ bm_iff_parse_bmhd(d91d2a6dd8adbea5).(CFILEcxx . . $ CFILE# *ifile, uint32_tcxx . . $ uint32_t# len, iff_bitmap_headercxx . . $ 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_bodycxx . . $ bm_iff_parse_body(44d0e32310802e0c).(CFILEcxx . . $ CFILE# *ifile, int len, iff_bitmap_headercxx . . $ 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_chunkcxx . . $ bm_iff_skip_chunk(f7aa3f82fed783cb).(CFILEcxx . . $ CFILE# *ifile, uint32_tcxx . . $ uint32_t# len); |
| 141 | |
| 142 | /// modify passed bitmap |
| 143 | static int bm_iff_parse_deltacxx . . $ bm_iff_parse_delta(44d0e32310802e0c).(CFILEcxx . . $ CFILE# *ifile, int len, iff_bitmap_headercxx . . $ iff_bitmap_header# *bmheader); |
| 144 | |
| 145 | static int bm_iff_parse_filecxx . . $ bm_iff_parse_file(c863debcf0cac729).(CFILEcxx . . $ CFILE# *ifile, iff_bitmap_headercxx . . $ iff_bitmap_header# *bmheader, iff_bitmap_headercxx . . $ iff_bitmap_header# *prev_bm); |
| 146 | static void bm_iff_convert_8_to_16cxx . . $ bm_iff_convert_8_to_16(f7d20204e5dee4a4).(int dest_bm, iff_bitmap_headercxx . . $ iff_bitmap_header# *iffbm); |
| 147 | |
| 148 | int bm_iff_get_sigcxx . . $ bm_iff_get_sig(413716a422df0036).(CFILEcxx . . $ CFILE# *f) { |
| 149 | char s[4]; |
| 150 | int i; |
| 151 | |
| 152 | for (i = 0; i < 4; i++) |
| 153 | s[i] = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(f); |
| 154 | |
| 155 | if (!strncmp("ILBM", s, 4)) |
| 156 | return IFF_SIG_ILBMcxx . . $ `bitmap/iff.cpp:123:9`!; |
| 157 | if (!strncmp("BODY", s, 4)) |
| 158 | return IFF_SIG_BODYcxx . . $ `bitmap/iff.cpp:124:9`!; |
| 159 | if (!strncmp("CMAP", s, 4)) |
| 160 | return IFF_SIG_CMAPcxx . . $ `bitmap/iff.cpp:126:9`!; |
| 161 | if (!strncmp("BMHD", s, 4)) |
| 162 | return IFF_SIG_BMHDcxx . . $ `bitmap/iff.cpp:125:9`!; |
| 163 | if (!strncmp("FORM", s, 4)) |
| 164 | return IFF_SIG_FORMcxx . . $ `bitmap/iff.cpp:122:9`!; |
| 165 | if (!strncmp("PBM ", s, 4)) |
| 166 | return IFF_SIG_PBMcxx . . $ `bitmap/iff.cpp:128:9`!; |
| 167 | if (!strncmp("ANIM", s, 4)) |
| 168 | return IFF_SIG_ANIMcxx . . $ `bitmap/iff.cpp:129:9`!; |
| 169 | if (!strncmp("DLTA", s, 4)) |
| 170 | return IFF_SIG_DELTAcxx . . $ `bitmap/iff.cpp:130:9`!; |
| 171 | if (!strncmp("ANHD", s, 4)) |
| 172 | return IFF_SIG_ANHDcxx . . $ `bitmap/iff.cpp:131:9`!; |
| 173 | |
| 174 | return (IFF_SIG_UNKNOWNcxx . . $ `bitmap/iff.cpp:127:9`!); |
| 175 | } |
| 176 | int bm_iff_parse_bmhdcxx . . $ bm_iff_parse_bmhd(d91d2a6dd8adbea5).(CFILEcxx . . $ CFILE# *ifile, uint32_tcxx . . $ uint32_t# len, iff_bitmap_headercxx . . $ iff_bitmap_header# *bmheader) { |
| 177 | len = len; |
| 178 | |
| 179 | bmheader->wcxx . . $ iff_bitmap_header#w. = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(ifile, false); |
| 180 | bmheader->hcxx . . $ iff_bitmap_header#h. = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(ifile, false); |
| 181 | bmheader->xcxx . . $ iff_bitmap_header#x. = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(ifile, false); |
| 182 | bmheader->ycxx . . $ iff_bitmap_header#y. = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(ifile, false); |
| 183 | |
| 184 | bmheader->nplanescxx . . $ iff_bitmap_header#nplanes. = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 185 | bmheader->maskingcxx . . $ iff_bitmap_header#masking. = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 186 | bmheader->compressioncxx . . $ iff_bitmap_header#compression. = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 187 | cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); /* skip pad */ |
| 188 | |
| 189 | bmheader->transparentcolorcxx . . $ iff_bitmap_header#transparentcolor. = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(ifile, false); |
| 190 | bmheader->xaspectcxx . . $ iff_bitmap_header#xaspect. = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 191 | bmheader->yaspectcxx . . $ iff_bitmap_header#yaspect. = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 192 | |
| 193 | bmheader->pagewidthcxx . . $ iff_bitmap_header#pagewidth. = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(ifile, false); |
| 194 | bmheader->pageheightcxx . . $ iff_bitmap_header#pageheight. = cf_ReadShortcxx . . $ cf_ReadShort(30b00389b433b379).(ifile, false); |
| 195 | |
| 196 | iff_transparent_colorcxx . . $ iff_transparent_color. = bmheader->transparentcolorcxx . . $ iff_bitmap_header#transparentcolor.; |
| 197 | |
| 198 | iff_has_transparencycxx . . $ iff_has_transparency. = 0; |
| 199 | |
| 200 | if (bmheader->maskingcxx . . $ iff_bitmap_header#masking. == mskHasTransparentColorcxx . . $ `bitmap/iff.cpp:93:9`!) |
| 201 | iff_has_transparencycxx . . $ iff_has_transparency. = 1; |
| 202 | |
| 203 | else if (bmheader->maskingcxx . . $ iff_bitmap_header#masking. != mskNonecxx . . $ `bitmap/iff.cpp:91:9`! && bmheader->maskingcxx . . $ iff_bitmap_header#masking. != mskHasMaskcxx . . $ `bitmap/iff.cpp:92:9`!) |
| 204 | return IFF_UNKNOWN_MASKcxx . . $ `bitmap/iff.h:36:9`!; |
| 205 | |
| 206 | return IFF_NO_ERRORcxx . . $ `bitmap/iff.h:26:9`!; |
| 207 | } |
| 208 | |
| 209 | // the buffer pointed to by raw_data is stuffed with a pointer to decompressed pixel data |
| 210 | int bm_iff_parse_bodycxx . . $ bm_iff_parse_body(44d0e32310802e0c).(CFILEcxx . . $ CFILE# *ifile, int len, iff_bitmap_headercxx . . $ iff_bitmap_header# *bmheader) { |
| 211 | uint8_tcxx . . $ uint8_t# *p = bmheader->raw_datacxx . . $ iff_bitmap_header#raw_data.; |
| 212 | int width = 0, depth = 0, done = 0; |
| 213 | |
| 214 | if (bmheader->typecxx . . $ iff_bitmap_header#type. == TYPE_PBMcxx . . $ `bitmap/iff.h:41:9`!) { |
| 215 | width = bmheader->wcxx . . $ iff_bitmap_header#w.; |
| 216 | depth = 1; |
| 217 | } else if (bmheader->typecxx . . $ iff_bitmap_header#type. == TYPE_ILBMcxx . . $ `bitmap/iff.h:42:9`!) { |
| 218 | width = (bmheader->wcxx . . $ iff_bitmap_header#w. + 7) / 8; |
| 219 | depth = bmheader->nplanescxx . . $ iff_bitmap_header#nplanes.; |
| 220 | } |
| 221 | |
| 222 | if (bmheader->compressioncxx . . $ iff_bitmap_header#compression. == cmpNonecxx . . $ `bitmap/iff.cpp:87:9`!) // no compression |
| 223 | { |
| 224 | for (int y = 0; y < bmheader->hcxx . . $ iff_bitmap_header#h.; y++) { |
| 225 | int x; |
| 226 | |
| 227 | for (x = 0; x < width * depth; x++) |
| 228 | *p++ = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 229 | |
| 230 | if (bmheader->maskingcxx . . $ iff_bitmap_header#masking. == mskHasMaskcxx . . $ `bitmap/iff.cpp:92:9`!) { |
| 231 | for (int i = 0; i < width; i++) |
| 232 | cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); // skip mask! |
| 233 | } |
| 234 | |
| 235 | if (bmheader->wcxx . . $ iff_bitmap_header#w. & 1) |
| 236 | cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 237 | } |
| 238 | |
| 239 | } else if (bmheader->compressioncxx . . $ iff_bitmap_header#compression. == cmpByteRun1cxx . . $ `bitmap/iff.cpp:88:9`!) // compression |
| 240 | { |
| 241 | uint8_tcxx . . $ uint8_t# *data_end = p + (bmheader->hcxx . . $ iff_bitmap_header#h. * depth * width); |
| 242 | uint8_tcxx . . $ uint8_t# mask = (bmheader->maskingcxx . . $ iff_bitmap_header#masking. == mskHasMaskcxx . . $ `bitmap/iff.cpp:92:9`!); |
| 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_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 266 | if (command >= 0 && command <= 127) { |
| 267 | if (!skip_mask) { |
| 268 | for (int i = 0; i < command + 1; i++) |
| 269 | *p++ = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 270 | } else { |
| 271 | for (int i = 0; i < (command + 1); i++) |
| 272 | cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(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_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(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_ERRORcxx . . $ `bitmap/iff.h:26:9`!; |
| 291 | } |
| 292 | |
| 293 | // the buffer pointed to by raw_data is stuffed with a pointer to bitplane pixel data |
| 294 | void bm_iff_skip_chunkcxx . . $ bm_iff_skip_chunk(f7aa3f82fed783cb).(CFILEcxx . . $ CFILE# *ifile, uint32_tcxx . . $ uint32_t# len) { |
| 295 | uint32_tcxx . . $ uint32_t# i; |
| 296 | |
| 297 | for (i = 0; i < len; i++) |
| 298 | cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 299 | } |
| 300 | |
| 301 | // modify passed bitmap |
| 302 | int bm_iff_parse_deltacxx . . $ bm_iff_parse_delta(44d0e32310802e0c).(CFILEcxx . . $ CFILE# *ifile, int len, iff_bitmap_headercxx . . $ iff_bitmap_header# *bmheader) { |
| 303 | uint8_tcxx . . $ uint8_t# *p = bmheader->raw_datacxx . . $ iff_bitmap_header#raw_data.; |
| 304 | int y; |
| 305 | int32_t chunk_end = cftellcxx . . $ cftell(65b457210a6528a1).(ifile) + len; |
| 306 | |
| 307 | cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(ifile); // longword, seems to be equal to 4. Don't know what it is |
| 308 | |
| 309 | for (y = 0; y < bmheader->hcxx . . $ iff_bitmap_header#h.; y++) { |
| 310 | uint8_tcxx . . $ uint8_t# n_items; |
| 311 | int cnt = bmheader->wcxx . . $ iff_bitmap_header#w.; |
| 312 | uint8_tcxx . . $ uint8_t# code; |
| 313 | |
| 314 | n_items = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 315 | |
| 316 | while (n_items--) { |
| 317 | code = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 318 | |
| 319 | if (code == 0) { |
| 320 | uint8_tcxx . . $ uint8_t# rep, val; |
| 321 | |
| 322 | rep = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 323 | val = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(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_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 342 | |
| 343 | if (cnt == -1) |
| 344 | cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (cnt == -1) { |
| 349 | if (!bmheader->wcxx . . $ iff_bitmap_header#w. & 1) |
| 350 | return IFF_CORRUPTcxx . . $ `bitmap/iff.h:32:9`!; |
| 351 | } else if (cnt) |
| 352 | return IFF_CORRUPTcxx . . $ `bitmap/iff.h:32:9`!; |
| 353 | } |
| 354 | |
| 355 | if (cftellcxx . . $ cftell(65b457210a6528a1).(ifile) == chunk_end - 1) // pad |
| 356 | cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 357 | |
| 358 | if (cftellcxx . . $ cftell(65b457210a6528a1).(ifile) != chunk_end) { |
| 359 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 360 | return IFF_CORRUPTcxx . . $ `bitmap/iff.h:32:9`!; |
| 361 | } |
| 362 | |
| 363 | else |
| 364 | return IFF_NO_ERRORcxx . . $ `bitmap/iff.h:26:9`!; |
| 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_filecxx . . $ bm_iff_parse_file(c863debcf0cac729).(CFILEcxx . . $ CFILE# *ifile, iff_bitmap_headercxx . . $ iff_bitmap_header# *bmheader, iff_bitmap_headercxx . . $ iff_bitmap_header# *prev_bm) { |
| 370 | uint32_tcxx . . $ uint32_t# sig, len; |
| 371 | int done = 0; |
| 372 | |
| 373 | while (!done) { |
| 374 | if (cfeofcxx . . $ cfeof(413716a422df0036).(ifile)) { |
| 375 | done = 1; |
| 376 | continue; |
| 377 | } |
| 378 | |
| 379 | sig = bm_iff_get_sigcxx . . $ bm_iff_get_sig(413716a422df0036).(ifile); |
| 380 | |
| 381 | len = cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(ifile, false); |
| 382 | |
| 383 | switch (sig) { |
| 384 | case IFF_SIG_FORMcxx . . $ `bitmap/iff.cpp:122:9`!: { |
| 385 | int newsig = bm_iff_get_sigcxx . . $ bm_iff_get_sig(413716a422df0036).(ifile); |
| 386 | bmheader->typecxx . . $ iff_bitmap_header#type. = TYPE_PBMcxx . . $ `bitmap/iff.h:41:9`!; |
| 387 | break; |
| 388 | } |
| 389 | case IFF_SIG_BMHDcxx . . $ `bitmap/iff.cpp:125:9`!: { |
| 390 | int ret; |
| 391 | |
| 392 | ret = bm_iff_parse_bmhdcxx . . $ bm_iff_parse_bmhd(d91d2a6dd8adbea5).(ifile, len, bmheader); |
| 393 | if (ret != IFF_NO_ERRORcxx . . $ `bitmap/iff.h:26:9`!) |
| 394 | return ret; |
| 395 | else { |
| 396 | |
| 397 | bmheader->raw_datacxx . . $ iff_bitmap_header#raw_data. = (uint8_tcxx . . $ uint8_t# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(bmheader->w * bmheader->h); |
| 398 | if (!bmheader->raw_datacxx . . $ iff_bitmap_header#raw_data.) |
| 399 | return IFF_NO_MEMcxx . . $ `bitmap/iff.h:27:9`!; |
| 400 | } |
| 401 | |
| 402 | } break; |
| 403 | case IFF_SIG_ANHDcxx . . $ `bitmap/iff.cpp:131:9`!: { |
| 404 | |
| 405 | if (!prev_bm) { |
| 406 | Int3cxx . . $ `misc/pserror.h:247:9`!(); |
| 407 | return IFF_CORRUPTcxx . . $ `bitmap/iff.h:32:9`!; |
| 408 | } |
| 409 | |
| 410 | bmheader->wcxx . . $ iff_bitmap_header#w. = prev_bm->wcxx . . $ iff_bitmap_header#w.; |
| 411 | bmheader->hcxx . . $ iff_bitmap_header#h. = prev_bm->hcxx . . $ iff_bitmap_header#h.; |
| 412 | bmheader->typecxx . . $ iff_bitmap_header#type. = prev_bm->typecxx . . $ iff_bitmap_header#type.; |
| 413 | bmheader->raw_datacxx . . $ iff_bitmap_header#raw_data. = (uint8_tcxx . . $ uint8_t# *)mem_malloccxx . . $ `mem/mem.h:79:9`!(bmheader->w * bmheader->h); |
| 414 | |
| 415 | if (!bmheader->raw_datacxx . . $ iff_bitmap_header#raw_data.) |
| 416 | return IFF_NO_MEMcxx . . $ `bitmap/iff.h:27:9`!; |
| 417 | |
| 418 | memcpy(bmheader->raw_datacxx . . $ iff_bitmap_header#raw_data., prev_bm->raw_datacxx . . $ iff_bitmap_header#raw_data., bmheader->wcxx . . $ iff_bitmap_header#w. * bmheader->hcxx . . $ iff_bitmap_header#h.); |
| 419 | |
| 420 | if (len & 1) |
| 421 | len++; |
| 422 | bm_iff_skip_chunkcxx . . $ bm_iff_skip_chunk(f7aa3f82fed783cb).(ifile, len); |
| 423 | |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | case IFF_SIG_CMAPcxx . . $ `bitmap/iff.cpp:126:9`!: { |
| 428 | int ncolors = (int)(len / 3), cnum; |
| 429 | uint8_tcxx . . $ uint8_t# r, g, b; |
| 430 | |
| 431 | for (cnum = 0; cnum < ncolors; cnum++) { |
| 432 | r = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 433 | g = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 434 | b = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 435 | r >>= 2; |
| 436 | bmheader->palettecxx . . $ iff_bitmap_header#palette.[cnum].rcxx . . $ pal_entry#r. = r; |
| 437 | g >>= 2; |
| 438 | bmheader->palettecxx . . $ iff_bitmap_header#palette.[cnum].gcxx . . $ pal_entry#g. = g; |
| 439 | b >>= 2; |
| 440 | bmheader->palettecxx . . $ iff_bitmap_header#palette.[cnum].bcxx . . $ pal_entry#b. = b; |
| 441 | } |
| 442 | if (len & 1) |
| 443 | cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 444 | |
| 445 | } break; |
| 446 | |
| 447 | case IFF_SIG_BODYcxx . . $ `bitmap/iff.cpp:124:9`!: { |
| 448 | int r; |
| 449 | if ((r = bm_iff_parse_bodycxx . . $ bm_iff_parse_body(44d0e32310802e0c).(ifile, len, bmheader)) != IFF_NO_ERRORcxx . . $ `bitmap/iff.h:26:9`!) |
| 450 | return r; |
| 451 | done = 1; |
| 452 | break; |
| 453 | } |
| 454 | case IFF_SIG_DELTAcxx . . $ `bitmap/iff.cpp:130:9`!: { |
| 455 | int r; |
| 456 | if ((r = bm_iff_parse_deltacxx . . $ bm_iff_parse_delta(44d0e32310802e0c).(ifile, len, bmheader)) != IFF_NO_ERRORcxx . . $ `bitmap/iff.h:26:9`!) |
| 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_chunkcxx . . $ bm_iff_skip_chunk(f7aa3f82fed783cb).(ifile, len); |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return IFF_NO_ERRORcxx . . $ `bitmap/iff.h:26:9`!; /* ok! */ |
| 472 | } |
| 473 | |
| 474 | void bm_iff_convert_8_to_16cxx . . $ bm_iff_convert_8_to_16(f7d20204e5dee4a4).(int dest_bm, iff_bitmap_headercxx . . $ iff_bitmap_header# *iffbm) { |
| 475 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(bm_w(dest_bm, 0) == iffbm->w); |
| 476 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(bm_h(dest_bm, 0) == iffbm->h); |
| 477 | |
| 478 | uint16_tcxx . . $ uint16_t# *data; |
| 479 | |
| 480 | data = (uint16_tcxx . . $ uint16_t# *)bm_datacxx . . $ bm_data(501e1bc9f482c6bd).(dest_bm, 0); |
| 481 | |
| 482 | for (int i = 0; i < iffbm->hcxx . . $ iff_bitmap_header#h.; i++) |
| 483 | for (int t = 0; t < iffbm->wcxx . . $ iff_bitmap_header#w.; t++) { |
| 484 | uint16_tcxx . . $ uint16_t# pixel; |
| 485 | uint8_tcxx . . $ uint8_t# c = iffbm->raw_datacxx . . $ iff_bitmap_header#raw_data.[i * iffbm->wcxx . . $ iff_bitmap_header#w. + t]; |
| 486 | |
| 487 | int r = iffbm->palettecxx . . $ iff_bitmap_header#palette.[c].rcxx . . $ pal_entry#r. >> 1; |
| 488 | int g = iffbm->palettecxx . . $ iff_bitmap_header#palette.[c].gcxx . . $ pal_entry#g. >> 1; |
| 489 | int b = iffbm->palettecxx . . $ iff_bitmap_header#palette.[c].bcxx . . $ pal_entry#b. >> 1; |
| 490 | |
| 491 | pixel = OPAQUE_FLAGcxx . . $ `lib/grdefs.h:72:9`! | (r << 10) | (g << 5) | b; |
| 492 | if (c == iffbm->transparentcolorcxx . . $ iff_bitmap_header#transparentcolor.) |
| 493 | pixel = NEW_TRANSPARENT_COLORcxx . . $ `lib/grdefs.h:71:9`!; |
| 494 | |
| 495 | data[i * bm_wcxx . . $ bm_w(9b79fb6aee4c0440).(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_filecxx . . $ bm_iff_alloc_file(413716a422df0036).(CFILEcxx . . $ CFILE# *ifile) { |
| 503 | int ret; // return code |
| 504 | iff_bitmap_headercxx . . $ iff_bitmap_header# bmheader; |
| 505 | int src_bm; |
| 506 | char cur_sig[4]; |
| 507 | |
| 508 | // Ignore FORM and form length |
| 509 | cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(ifile); |
| 510 | cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(ifile); |
| 511 | |
| 512 | // check if this an ILBM |
| 513 | |
| 514 | for (int i = 0; i < 4; i++) |
| 515 | cur_sig[i] = cf_ReadBytecxx . . $ cf_ReadByte(dcbbf0b0e13758ec).(ifile); |
| 516 | |
| 517 | if (strncmp("PBM ", cur_sig, 4)) { |
| 518 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "IFF file isn't a PBM...aborting.\n"); |
| 519 | return -1; |
| 520 | } |
| 521 | if (!strncmp("PBM ", cur_sig, 4)) |
| 522 | bmheader.typecxx . . $ iff_bitmap_header#type. = TYPE_PBMcxx . . $ `bitmap/iff.h:41:9`!; |
| 523 | else |
| 524 | Int3cxx . . $ `misc/pserror.h:247:9`!(); // Huh? Get Jason! |
| 525 | |
| 526 | ret = bm_iff_parse_filecxx . . $ bm_iff_parse_file(c863debcf0cac729).(ifile, &bmheader, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 527 | |
| 528 | if (ret != IFF_NO_ERRORcxx . . $ `bitmap/iff.h:26:9`!) { |
| 529 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Couldn't load IFF file.\n"); |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | // Alloc our bitmap |
| 534 | src_bm = bm_AllocBitmapcxx . . $ bm_AllocBitmap(2326284602b0bb2d).(bmheader.wcxx . . $ iff_bitmap_header#w., bmheader.hcxx . . $ iff_bitmap_header#h., 0); |
| 535 | if (src_bm < 0) { |
| 536 | mem_freecxx . . $ `mem/mem.h:80:9`!(bmheader.raw_data); |
| 537 | return -1; |
| 538 | } |
| 539 | |
| 540 | // Convert our 8 bit bitmap to 16bit |
| 541 | bm_iff_convert_8_to_16cxx . . $ bm_iff_convert_8_to_16(f7d20204e5dee4a4).(src_bm, &bmheader); |
| 542 | freecxx . . $ free(bd21765a0afc8e3c).(bmheader.raw_datacxx . . $ iff_bitmap_header#raw_data.); |
| 543 | |
| 544 | return src_bm; |
| 545 | } |
| 546 | |
| 547 | // returns number of bitmaps or -1 on error |
| 548 | int bm_iff_read_animbrushcxx . . $ bm_iff_read_animbrush(7436bf38bddd805d).(const char *ifilename, int *bm_list) { |
| 549 | CFILEcxx . . $ CFILE# *ifile; |
| 550 | iff_bitmap_headercxx . . $ iff_bitmap_header# bm_headers[40]; |
| 551 | iff_bitmap_headercxx . . $ 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_datacxx . . $ iff_bitmap_header#raw_data. = 0; |
| 559 | |
| 560 | ifile = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(ifilenamecxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d)., "rb"); |
| 561 | if (!ifile) |
| 562 | return -1; |
| 563 | |
| 564 | sig = bm_iff_get_sigcxx . . $ bm_iff_get_sig(413716a422df0036).(ifile); |
| 565 | form_len = cf_ReadIntcxx . . $ cf_ReadInt(d60d6f5e30f0c96a).(ifile, false); |
| 566 | |
| 567 | if (sig != IFF_SIG_FORMcxx . . $ `bitmap/iff.cpp:122:9`!) { |
| 568 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Not a valid IFF file.\n"); |
| 569 | cfclosecxx . . $ cfclose(3639e799710acb68).(ifile); |
| 570 | return -1; |
| 571 | } |
| 572 | |
| 573 | form_type = bm_iff_get_sigcxx . . $ bm_iff_get_sig(413716a422df0036).(ifile); |
| 574 | |
| 575 | if (form_type == IFF_SIG_ANIMcxx . . $ `bitmap/iff.cpp:129:9`!) { |
| 576 | while (!cfeofcxx . . $ cfeof(413716a422df0036).(ifile) && num_bitmaps < 40) { |
| 577 | ret = bm_iff_parse_filecxx . . $ bm_iff_parse_file(c863debcf0cac729).(ifile, &bm_headers[num_bitmaps], num_bitmaps > 0 ? &bm_headers[num_bitmaps - 1] : NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!); |
| 578 | |
| 579 | if (ret != IFF_NO_ERRORcxx . . $ `bitmap/iff.h:26:9`!) |
| 580 | goto done; |
| 581 | |
| 582 | num_bitmaps++; |
| 583 | } |
| 584 | |
| 585 | } else { |
| 586 | cfclosecxx . . $ cfclose(3639e799710acb68).(ifile); |
| 587 | return -1; |
| 588 | } |
| 589 | |
| 590 | done: |
| 591 | cfclosecxx . . $ cfclose(3639e799710acb68).(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->palettecxx . . $ iff_bitmap_header#palette., bm_headers[0].palettecxx . . $ iff_bitmap_header#palette., sizeof(pal_entrycxx . . $ pal_entry#) * 256); |
| 598 | temp_bm_head->transparentcolorcxx . . $ iff_bitmap_header#transparentcolor. = bm_headers[0].transparentcolorcxx . . $ iff_bitmap_header#transparentcolor.; |
| 599 | |
| 600 | src_bm = bm_AllocBitmapcxx . . $ bm_AllocBitmap(2326284602b0bb2d).(temp_bm_head->wcxx . . $ iff_bitmap_header#w., temp_bm_head->hcxx . . $ iff_bitmap_header#h., 0); |
| 601 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(src_bm > 0); |
| 602 | |
| 603 | bm_iff_convert_8_to_16cxx . . $ bm_iff_convert_8_to_16(f7d20204e5dee4a4).(src_bm, temp_bm_head); |
| 604 | bm_list[i] = src_bm; |
| 605 | } |
| 606 | |
| 607 | for (i = 0; i < num_bitmaps; i++) |
| 608 | mem_freecxx . . $ `mem/mem.h:80:9`!(bm_headers[i].raw_data); |
| 609 | |
| 610 | return num_bitmaps; |
| 611 | } |