| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Routines for reading and writing IFF files |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#define COMPRESS 1 //do the RLE or not? (for debugging mostly) |
| 27 |
#define WRITE_TINY 0 //should we write a TINY chunk? |
| 28 |
|
| 29 |
#define MIN_COMPRESS_WIDTH 65 //don't compress if less than this wide |
| 30 |
|
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
|
| 35 |
#include "u_mem.h" |
| 36 |
#include "iff.h" |
| 37 |
#include "dxxerror.h" |
| 38 |
#include "makesig.h" |
| 39 |
#include "physfsx.h" |
| 40 |
#include "gr.h" |
| 41 |
|
| 42 |
#include "dxxsconf.h" |
| 43 |
#include "d_underlying_value.h" |
| 44 |
#include "compiler-range_for.h" |
| 45 |
#include "partial_range.h" |
| 46 |
#include <memory> |
| 47 |
|
| 48 |
//Internal constants and structures for this library |
| 49 |
|
| 50 |
//Type values for bitmaps |
| 51 |
#define TYPE_PBM bm_mode::linear |
| 52 |
#define TYPE_ILBM bm_mode::ilbm |
| 53 |
|
| 54 |
//Compression types |
| 55 |
#define cmpNone 0 |
| 56 |
#define cmpByteRun1 1 |
| 57 |
|
| 58 |
//Masking types |
| 59 |
#define mskNone 0 |
| 60 |
#define mskHasMask 1 |
| 61 |
#define mskHasTransparentColor 2 |
| 62 |
|
| 63 |
namespace { |
| 64 |
|
| 65 |
//structure of the header in the file |
| 66 |
struct iff_bitmap_header : prohibit_void_ptr<iff_bitmap_header> |
| 67 |
{ |
| 68 |
short w,h; //width and height of this bitmap |
| 69 |
short x,y; //generally unused |
| 70 |
short transparentcolor; //which color is transparent (if any) |
| 71 |
short pagewidth,pageheight; //width & height of source screen |
| 72 |
bm_mode type; //see types above |
| 73 |
sbyte nplanes; //number of planes (8 for 256 color image) |
| 74 |
sbyte masking,compression; //see constants above |
| 75 |
sbyte xaspect,yaspect; //aspect ratio (usually 5/6) |
| 76 |
palette_array_t palette; //the palette for this bitmap |
| 77 |
short row_size; //offset to next row |
| 78 |
RAIIdmem<uint8_t[]> raw_data; //ptr to array of data |
| 79 |
}; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
ubyte iff_transparent_color; |
| 84 |
ubyte iff_has_transparency; // 0=no transparency, 1=iff_transparent_color is valid |
| 85 |
|
| 86 |
#define form_sig MAKE_SIG('F','O','R','M') |
| 87 |
#define ilbm_sig MAKE_SIG('I','L','B','M') |
| 88 |
#define body_sig MAKE_SIG('B','O','D','Y') |
| 89 |
#define pbm_sig MAKE_SIG('P','B','M',' ') |
| 90 |
#define bmhd_sig MAKE_SIG('B','M','H','D') |
| 91 |
#define anhd_sig MAKE_SIG('A','N','H','D') |
| 92 |
#define cmap_sig MAKE_SIG('C','M','A','P') |
| 93 |
#define tiny_sig MAKE_SIG('T','I','N','Y') |
| 94 |
#define anim_sig MAKE_SIG('A','N','I','M') |
| 95 |
#define dlta_sig MAKE_SIG('D','L','T','A') |
| 96 |
|
| 97 |
namespace { |
| 98 |
|
| 99 |
static int32_t get_sig(PHYSFS_File *f) |
| 100 |
{ |
| 101 |
int s; |
| 102 |
PHYSFS_readSBE32(f, &s); |
| 103 |
return s; |
| 104 |
} |
| 105 |
|
| 106 |
#define put_sig(sig, f) PHYSFS_writeSBE32(f, sig) |
| 107 |
|
| 108 |
[[nodiscard]] |
| 109 |
static iff_status_code parse_bmhd(const NamedPHYSFS_File ifile, iff_bitmap_header *const bmheader) |
| 110 |
{ |
| 111 |
PHYSFS_readSBE16(ifile, &bmheader->w); |
| 112 |
PHYSFS_readSBE16(ifile, &bmheader->h); |
| 113 |
PHYSFS_readSBE16(ifile, &bmheader->x); |
| 114 |
PHYSFS_readSBE16(ifile, &bmheader->y); |
| 115 |
|
| 116 |
bmheader->nplanes = PHYSFSX_readByte(ifile); |
| 117 |
bmheader->masking = PHYSFSX_readByte(ifile); |
| 118 |
bmheader->compression = PHYSFSX_readByte(ifile); |
| 119 |
PHYSFSX_skipBytes<1>(ifile); /* skip pad */ |
| 120 |
|
| 121 |
PHYSFS_readSBE16(ifile, &bmheader->transparentcolor); |
| 122 |
bmheader->xaspect = PHYSFSX_readByte(ifile); |
| 123 |
bmheader->yaspect = PHYSFSX_readByte(ifile); |
| 124 |
|
| 125 |
PHYSFS_readSBE16(ifile, &bmheader->pagewidth); |
| 126 |
PHYSFS_readSBE16(ifile, &bmheader->pageheight); |
| 127 |
|
| 128 |
iff_transparent_color = bmheader->transparentcolor; |
| 129 |
|
| 130 |
iff_has_transparency = 0; |
| 131 |
|
| 132 |
if (bmheader->masking == mskHasTransparentColor) |
| 133 |
iff_has_transparency = 1; |
| 134 |
|
| 135 |
else if (bmheader->masking != mskNone && bmheader->masking != mskHasMask) |
| 136 |
return iff_status_code::unknown_mask; |
| 137 |
|
| 138 |
// debug("w,h=%d,%d x,y=%d,%d\n",w,h,x,y); |
| 139 |
// debug("nplanes=%d, masking=%d ,compression=%d, transcolor=%d\n",nplanes,masking,compression,transparentcolor); |
| 140 |
|
| 141 |
return iff_status_code::no_error; |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
// the buffer pointed to by raw_data is stuffed with a pointer to decompressed pixel data |
| 147 |
namespace dsx { |
| 148 |
|
| 149 |
namespace { |
| 150 |
|
| 151 |
[[nodiscard]] |
| 152 |
static iff_status_code parse_body(PHYSFS_File *ifile,long len,iff_bitmap_header *bmheader) |
| 153 |
{ |
| 154 |
auto p = bmheader->raw_data.get(); |
| 155 |
int width,depth; |
| 156 |
signed char n; |
| 157 |
int nn,wid_cnt,end_cnt,plane; |
| 158 |
unsigned char *data_end; |
| 159 |
int end_pos; |
| 160 |
#ifndef NDEBUG |
| 161 |
int row_count{0}; |
| 162 |
#endif |
| 163 |
|
| 164 |
width=0; |
| 165 |
depth=0; |
| 166 |
|
| 167 |
end_pos = PHYSFS_tell(ifile) + len; |
| 168 |
if (len&1) |
| 169 |
end_pos++; |
| 170 |
|
| 171 |
if (bmheader->type == TYPE_PBM) { |
| 172 |
width=bmheader->w; |
| 173 |
depth=1; |
| 174 |
} else if (bmheader->type == TYPE_ILBM) { |
| 175 |
width = (bmheader->w+7)/8; |
| 176 |
depth=bmheader->nplanes; |
| 177 |
} |
| 178 |
|
| 179 |
end_cnt = (width&1)?-1:0; |
| 180 |
|
| 181 |
data_end = p + width*bmheader->h*depth; |
| 182 |
|
| 183 |
if (bmheader->compression == cmpNone) { /* no compression */ |
| 184 |
int y; |
| 185 |
|
| 186 |
for (y=bmheader->h;y;y--) { |
| 187 |
PHYSFSX_readBytes(ifile, p, width * depth); |
| 188 |
p += bmheader->w; |
| 189 |
|
| 190 |
if (bmheader->masking == mskHasMask) |
| 191 |
PHYSFSX_fseek(ifile, width, SEEK_CUR); //skip mask! |
| 192 |
|
| 193 |
if (bmheader->w & 1) PHYSFSX_fgetc(ifile); |
| 194 |
} |
| 195 |
|
| 196 |
//cnt = len - bmheader->h * ((bmheader->w+1)&~1); |
| 197 |
|
| 198 |
} |
| 199 |
else if (bmheader->compression == cmpByteRun1) |
| 200 |
for (wid_cnt=width,plane=0; PHYSFS_tell(ifile) < end_pos && p<data_end;) { |
| 201 |
unsigned char c; |
| 202 |
|
| 203 |
if (wid_cnt == end_cnt) { |
| 204 |
wid_cnt = width; |
| 205 |
plane++; |
| 206 |
if ((bmheader->masking == mskHasMask && plane==depth+1) || |
| 207 |
(bmheader->masking != mskHasMask && plane==depth)) |
| 208 |
plane=0; |
| 209 |
} |
| 210 |
|
| 211 |
Assert(wid_cnt > end_cnt); |
| 212 |
|
| 213 |
n=PHYSFSX_fgetc(ifile); |
| 214 |
|
| 215 |
if (n >= 0) { // copy next n+1 bytes from source, they are not compressed |
| 216 |
nn = static_cast<int>(n)+1; |
| 217 |
wid_cnt -= nn; |
| 218 |
if (wid_cnt==-1) {--nn; Assert(width&1);} |
| 219 |
if (plane==depth) //masking row |
| 220 |
PHYSFSX_fseek(ifile, nn, SEEK_CUR); |
| 221 |
else |
| 222 |
{ |
| 223 |
PHYSFSX_readBytes(ifile, p, nn); |
| 224 |
p += nn; |
| 225 |
} |
| 226 |
if (wid_cnt==-1) PHYSFSX_fseek(ifile, 1, SEEK_CUR); |
| 227 |
} |
| 228 |
else if (n>=-127) { // next -n + 1 bytes are following byte |
| 229 |
c=PHYSFSX_fgetc(ifile); |
| 230 |
const int negative_n = -n; |
| 231 |
nn = negative_n + 1; |
| 232 |
wid_cnt -= nn; |
| 233 |
if (wid_cnt==-1) {--nn; Assert(width&1);} |
| 234 |
if (plane!=depth) //not masking row |
| 235 |
{memset(p,c,nn); p+=nn;} |
| 236 |
} |
| 237 |
|
| 238 |
#ifndef NDEBUG |
| 239 |
if ((p - bmheader->raw_data.get()) % width == 0) |
| 240 |
row_count++; |
| 241 |
|
| 242 |
Assert((p - bmheader->raw_data.get()) - (width*row_count) < width); |
| 243 |
#endif |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
#if DXX_BUILD_DESCENT == 1 |
| 248 |
if (bmheader->masking==mskHasMask && p==data_end && PHYSFS_tell(ifile)==end_pos-2) //I don't know why... |
| 249 |
PHYSFSX_fseek(ifile, 1, SEEK_CUR); //...but if I do this it works |
| 250 |
|
| 251 |
if (p==data_end && PHYSFS_tell(ifile)==end_pos-1) //must be a pad byte |
| 252 |
//ignore = PHYSFSX_fgetc(ifile); //get pad byte |
| 253 |
PHYSFSX_fseek(ifile, 1, SEEK_CUR); |
| 254 |
else |
| 255 |
if (PHYSFS_tell(ifile)!=end_pos || p!=data_end) { |
| 256 |
// debug("IFF Error: p=%x, data_end=%x, cnt=%d\n",p,data_end,cnt); |
| 257 |
return iff_status_code::corrupt; |
| 258 |
} |
| 259 |
#elif DXX_BUILD_DESCENT == 2 |
| 260 |
if (p!=data_end) //if we don't have the whole bitmap... |
| 261 |
return iff_status_code::corrupt; //...the give an error |
| 262 |
|
| 263 |
//Pretend we read the whole chuck, because if we didn't, it's because |
| 264 |
//we didn't read the last mask like or the last rle record for padding |
| 265 |
//or whatever and it's not important, because we check to make sure |
| 266 |
//we got the while bitmap, and that's what really counts. |
| 267 |
#endif |
| 268 |
|
| 269 |
return iff_status_code::no_error; |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
namespace { |
| 275 |
//modify passed bitmap |
| 276 |
|
| 277 |
[[nodiscard]] |
| 278 |
static iff_status_code parse_delta(const NamedPHYSFS_File ifile, long len, iff_bitmap_header *const bmheader) |
| 279 |
{ |
| 280 |
auto p = bmheader->raw_data.get(); |
| 281 |
long chunk_end = PHYSFS_tell(ifile) + len; |
| 282 |
|
| 283 |
PHYSFSX_fseek(ifile, 4, SEEK_CUR); //longword, seems to be equal to 4. Don't know what it is |
| 284 |
|
| 285 |
for (int y=0;y<bmheader->h;y++) { |
| 286 |
ubyte n_items; |
| 287 |
int cnt = bmheader->w; |
| 288 |
ubyte code; |
| 289 |
|
| 290 |
n_items = PHYSFSX_readByte(ifile); |
| 291 |
|
| 292 |
while (n_items--) { |
| 293 |
|
| 294 |
code = PHYSFSX_readByte(ifile); |
| 295 |
|
| 296 |
if (code==0) { //repeat |
| 297 |
ubyte rep,val; |
| 298 |
|
| 299 |
rep = PHYSFSX_readByte(ifile); |
| 300 |
val = PHYSFSX_readByte(ifile); |
| 301 |
|
| 302 |
cnt -= rep; |
| 303 |
if (cnt==-1) |
| 304 |
rep--; |
| 305 |
while (rep--) |
| 306 |
*p++ = val; |
| 307 |
} |
| 308 |
else if (code > 0x80) { //skip |
| 309 |
cnt -= (code-0x80); |
| 310 |
p += (code-0x80); |
| 311 |
if (cnt==-1) |
| 312 |
p--; |
| 313 |
} |
| 314 |
else { //literal |
| 315 |
cnt -= code; |
| 316 |
if (cnt==-1) |
| 317 |
code--; |
| 318 |
|
| 319 |
while (code--) |
| 320 |
*p++ = PHYSFSX_readByte(ifile); |
| 321 |
|
| 322 |
if (cnt==-1) |
| 323 |
PHYSFSX_skipBytes<1>(ifile); |
| 324 |
} |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
if (cnt == -1) { |
| 329 |
if (!bmheader->w) |
| 330 |
return iff_status_code::corrupt; |
| 331 |
} |
| 332 |
else if (cnt) |
| 333 |
return iff_status_code::corrupt; |
| 334 |
} |
| 335 |
|
| 336 |
if (PHYSFS_tell(ifile) == chunk_end-1) //pad |
| 337 |
PHYSFSX_fseek(ifile, 1, SEEK_CUR); |
| 338 |
|
| 339 |
if (PHYSFS_tell(ifile) != chunk_end) |
| 340 |
return iff_status_code::corrupt; |
| 341 |
else |
| 342 |
return iff_status_code::no_error; |
| 343 |
} |
| 344 |
|
| 345 |
// the buffer pointed to by raw_data is stuffed with a pointer to bitplane pixel data |
| 346 |
static void skip_chunk(PHYSFS_File *ifile,long len) |
| 347 |
{ |
| 348 |
int ilen; |
| 349 |
ilen = (len+1) & ~1; |
| 350 |
|
| 351 |
PHYSFSX_fseek(ifile,ilen,SEEK_CUR); |
| 352 |
} |
| 353 |
|
| 354 |
//read an ILBM or PBM file |
| 355 |
// Pass pointer to opened file, and to empty bitmap_header structure, and form length |
| 356 |
[[nodiscard]] |
| 357 |
static iff_status_code iff_parse_ilbm_pbm(const NamedPHYSFS_File ifile, long form_type, iff_bitmap_header *bmheader, int form_len, grs_bitmap *prev_bm) |
| 358 |
{ |
| 359 |
int sig,len; |
| 360 |
long start_pos,end_pos; |
| 361 |
|
| 362 |
start_pos = PHYSFS_tell(ifile); |
| 363 |
end_pos = start_pos-4+form_len; |
| 364 |
|
| 365 |
if (form_type == pbm_sig) |
| 366 |
bmheader->type = TYPE_PBM; |
| 367 |
else |
| 368 |
bmheader->type = TYPE_ILBM; |
| 369 |
|
| 370 |
while ((PHYSFS_tell(ifile) < end_pos) && (sig=get_sig(ifile)) != EOF) { |
| 371 |
|
| 372 |
PHYSFS_readSBE32(ifile, &len); |
| 373 |
|
| 374 |
switch (sig) { |
| 375 |
|
| 376 |
case bmhd_sig: { |
| 377 |
int save_w=bmheader->w,save_h=bmheader->h; |
| 378 |
|
| 379 |
if (const auto ret{parse_bmhd(ifile, bmheader)}; ret != iff_status_code::no_error) |
| 380 |
return ret; |
| 381 |
|
| 382 |
if (bmheader->raw_data) { |
| 383 |
|
| 384 |
if (save_w != bmheader->w || save_h != bmheader->h) |
| 385 |
return iff_status_code::bm_mismatch; |
| 386 |
|
| 387 |
} |
| 388 |
else { |
| 389 |
MALLOC(bmheader->raw_data, uint8_t[], bmheader->w * bmheader->h); |
| 390 |
if (!bmheader->raw_data) |
| 391 |
return iff_status_code::no_mem; |
| 392 |
} |
| 393 |
|
| 394 |
break; |
| 395 |
|
| 396 |
} |
| 397 |
|
| 398 |
case anhd_sig: |
| 399 |
|
| 400 |
if (!prev_bm) |
| 401 |
return iff_status_code::corrupt; |
| 402 |
|
| 403 |
bmheader->w = prev_bm->bm_w; |
| 404 |
bmheader->h = prev_bm->bm_h; |
| 405 |
bmheader->type = prev_bm->get_type(); |
| 406 |
|
| 407 |
MALLOC(bmheader->raw_data, uint8_t[], bmheader->w * bmheader->h); |
| 408 |
|
| 409 |
memcpy(bmheader->raw_data.get(), prev_bm->bm_data, bmheader->w * bmheader->h); |
| 410 |
skip_chunk(ifile,len); |
| 411 |
|
| 412 |
break; |
| 413 |
|
| 414 |
case cmap_sig: |
| 415 |
{ |
| 416 |
unsigned ncolors=(len/3); |
| 417 |
|
| 418 |
range_for (auto &p, partial_range(bmheader->palette, ncolors)) |
| 419 |
{ |
| 420 |
p.r = static_cast<unsigned char>(PHYSFSX_fgetc(ifile)) >> 2; |
| 421 |
p.g = static_cast<unsigned char>(PHYSFSX_fgetc(ifile)) >> 2; |
| 422 |
p.b = static_cast<unsigned char>(PHYSFSX_fgetc(ifile)) >> 2; |
| 423 |
} |
| 424 |
if (len & 1) PHYSFSX_fgetc(ifile); |
| 425 |
|
| 426 |
break; |
| 427 |
} |
| 428 |
|
| 429 |
case body_sig: |
| 430 |
{ |
| 431 |
if (const auto r{parse_body(ifile,len,bmheader)}; r != iff_status_code::no_error) |
| 432 |
return r; |
| 433 |
break; |
| 434 |
} |
| 435 |
case dlta_sig: |
| 436 |
{ |
| 437 |
if (const auto r{parse_delta(ifile,len,bmheader)}; r != iff_status_code::no_error) |
| 438 |
return r; |
| 439 |
break; |
| 440 |
} |
| 441 |
default: |
| 442 |
skip_chunk(ifile,len); |
| 443 |
break; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if (PHYSFS_tell(ifile) != start_pos-4+form_len) |
| 448 |
return iff_status_code::corrupt; |
| 449 |
|
| 450 |
return iff_status_code::no_error; /* ok! */ |
| 451 |
} |
| 452 |
|
| 453 |
//convert an ILBM file to a PBM file |
| 454 |
[[nodiscard]] |
| 455 |
static iff_status_code convert_ilbm_to_pbm(iff_bitmap_header *bmheader) |
| 456 |
{ |
| 457 |
int x,p; |
| 458 |
int bytes_per_row,byteofs; |
| 459 |
ubyte checkmask,newbyte,setbit; |
| 460 |
|
| 461 |
RAIIdmem<uint8_t[]> new_data; |
| 462 |
MALLOC(new_data, uint8_t[], bmheader->w * bmheader->h); |
| 463 |
if (new_data == nullptr) |
| 464 |
return iff_status_code::no_mem; |
| 465 |
|
| 466 |
auto destptr = new_data.get(); |
| 467 |
|
| 468 |
bytes_per_row = 2*((bmheader->w+15)/16); |
| 469 |
|
| 470 |
for (int y=0;y<bmheader->h;y++) { |
| 471 |
|
| 472 |
const auto rowptr = reinterpret_cast<int8_t *>(&bmheader->raw_data[y * bytes_per_row * bmheader->nplanes]); |
| 473 |
|
| 474 |
for (x=0,checkmask=0x80;x<bmheader->w;x++) { |
| 475 |
|
| 476 |
byteofs = x >> 3; |
| 477 |
|
| 478 |
for (p=newbyte=0,setbit=1;p<bmheader->nplanes;p++) { |
| 479 |
|
| 480 |
if (rowptr[bytes_per_row * p + byteofs] & checkmask) |
| 481 |
newbyte |= setbit; |
| 482 |
|
| 483 |
setbit <<= 1; |
| 484 |
} |
| 485 |
|
| 486 |
*destptr++ = newbyte; |
| 487 |
|
| 488 |
if ((checkmask >>= 1) == 0) checkmask=0x80; |
| 489 |
} |
| 490 |
|
| 491 |
} |
| 492 |
|
| 493 |
bmheader->raw_data = std::move(new_data); |
| 494 |
|
| 495 |
bmheader->type = TYPE_PBM; |
| 496 |
return iff_status_code::no_error; |
| 497 |
} |
| 498 |
|
| 499 |
} |
| 500 |
|
| 501 |
#define INDEX_TO_15BPP(i) (static_cast<short>((((palptr[(i)].r/2)&31)<<10)+(((palptr[(i)].g/2)&31)<<5)+((palptr[(i)].b/2 )&31))) |
| 502 |
|
| 503 |
namespace dsx { |
| 504 |
|
| 505 |
namespace { |
| 506 |
|
| 507 |
static iff_status_code convert_rgb15(grs_bitmap &bm,iff_bitmap_header &bmheader) |
| 508 |
{ |
| 509 |
palette_array_t::iterator palptr = begin(bmheader.palette); |
| 510 |
|
| 511 |
#if DXX_BUILD_DESCENT == 1 |
| 512 |
for (int y=0; y < bm.bm_h; y++) { |
| 513 |
for (int x=0; x < bmheader.w; x++) |
| 514 |
gr_bm_pixel(*grd_curcanv, bm, x, y, INDEX_TO_15BPP(bm.get_bitmap_data()[y * bmheader.w + x])); |
| 515 |
} |
| 516 |
#elif DXX_BUILD_DESCENT == 2 |
| 517 |
uint16_t *new_data; |
| 518 |
MALLOC(new_data, ushort, bm.bm_w * bm.bm_h * 2); |
| 519 |
if (new_data == nullptr) |
| 520 |
return iff_status_code::no_mem; |
| 521 |
|
| 522 |
unsigned newptr{0}; |
| 523 |
for (int y=0; y < bm.bm_h; y++) { |
| 524 |
|
| 525 |
for (int x=0; x < bmheader.w; x++) |
| 526 |
new_data[newptr++] = INDEX_TO_15BPP(bm.get_bitmap_data()[y * bmheader.w + x]); |
| 527 |
|
| 528 |
} |
| 529 |
|
| 530 |
d_free(bm.bm_mdata); //get rid of old-style data |
| 531 |
bm.bm_mdata = reinterpret_cast<uint8_t *>(new_data); //..and point to new data |
| 532 |
|
| 533 |
bm.bm_rowsize *= 2; //two bytes per row |
| 534 |
#endif |
| 535 |
return iff_status_code::no_error; |
| 536 |
|
| 537 |
} |
| 538 |
|
| 539 |
} |
| 540 |
|
| 541 |
} |
| 542 |
|
| 543 |
namespace { |
| 544 |
|
| 545 |
//copy an iff header structure to a grs_bitmap structure |
| 546 |
static void copy_iff_to_grs(grs_bitmap &bm,iff_bitmap_header &bmheader) |
| 547 |
{ |
| 548 |
gr_init_bitmap(bm, bmheader.type, 0, 0, bmheader.w, bmheader.h, bmheader.w, bmheader.raw_data.release()); |
| 549 |
} |
| 550 |
|
| 551 |
//if bm->bm_data is set, use it (making sure w & h are correct), else |
| 552 |
//allocate the memory |
| 553 |
[[nodiscard]] |
| 554 |
static iff_status_code iff_parse_bitmap(const NamedPHYSFS_File ifile, grs_bitmap &bm, const bm_mode bitmap_type, palette_array_t *const palette, grs_bitmap *const prev_bm) |
| 555 |
{ |
| 556 |
iff_bitmap_header bmheader; |
| 557 |
int sig,form_len; |
| 558 |
long form_type; |
| 559 |
|
| 560 |
bmheader.w=bmheader.h=0; |
| 561 |
|
| 562 |
sig=get_sig(ifile); |
| 563 |
|
| 564 |
if (sig != form_sig) { |
| 565 |
return iff_status_code::not_iff; |
| 566 |
} |
| 567 |
|
| 568 |
PHYSFS_readSBE32(ifile, &form_len); |
| 569 |
|
| 570 |
form_type = get_sig(ifile); |
| 571 |
|
| 572 |
if (form_type == anim_sig) |
| 573 |
return iff_status_code::form_anim; |
| 574 |
else if ((form_type == pbm_sig) || (form_type == ilbm_sig)) |
| 575 |
{ |
| 576 |
if (const auto ret{iff_parse_ilbm_pbm(ifile, form_type, &bmheader, form_len, prev_bm)}; ret != iff_status_code::no_error) |
| 577 |
{ //got an error parsing |
| 578 |
return ret; |
| 579 |
} |
| 580 |
} |
| 581 |
else |
| 582 |
return iff_status_code::unknown_form; |
| 583 |
|
| 584 |
//If IFF file is ILBM, convert to PPB |
| 585 |
if (bmheader.type == TYPE_ILBM) { |
| 586 |
if (const auto ret{convert_ilbm_to_pbm(&bmheader)}; ret != iff_status_code::no_error) |
| 587 |
return ret; |
| 588 |
} |
| 589 |
|
| 590 |
//Copy data from iff_bitmap_header structure into grs_bitmap structure |
| 591 |
|
| 592 |
copy_iff_to_grs(bm,bmheader); |
| 593 |
|
| 594 |
if (palette) |
| 595 |
*palette = bmheader.palette; |
| 596 |
|
| 597 |
//Now do post-process if required |
| 598 |
|
| 599 |
if (bitmap_type == bm_mode::rgb15) { |
| 600 |
return convert_rgb15(bm, bmheader); |
| 601 |
} |
| 602 |
return iff_status_code::no_error; |
| 603 |
} |
| 604 |
|
| 605 |
} |
| 606 |
|
| 607 |
//returns error codes - see IFF.H. see GR.H for bitmap_type |
| 608 |
iff_status_code iff_read_bitmap(const char *const ifilename, grs_bitmap &bm, palette_array_t *const palette) |
| 609 |
{ |
| 610 |
auto ifile = PHYSFSX_openReadBuffered(ifilename).first; |
| 611 |
if (!ifile) |
| 612 |
return iff_status_code::no_file; |
| 613 |
|
| 614 |
assert(bm.bm_data == nullptr); |
| 615 |
return iff_parse_bitmap(ifile, bm, bm_mode::linear, palette, nullptr); |
| 616 |
} |
| 617 |
|
| 618 |
#define BMHD_SIZE 20 |
| 619 |
|
| 620 |
#if 0 |
| 621 |
static int write_bmhd(PHYSFS_File *ofile,iff_bitmap_header *bitmap_header) |
| 622 |
{ |
| 623 |
put_sig(bmhd_sig,ofile); |
| 624 |
PHYSFS_writeSBE32(ofile, BMHD_SIZE); |
| 625 |
|
| 626 |
PHYSFS_writeSBE16(ofile, bitmap_header->w); |
| 627 |
PHYSFS_writeSBE16(ofile, bitmap_header->h); |
| 628 |
PHYSFS_writeSBE16(ofile, bitmap_header->x); |
| 629 |
PHYSFS_writeSBE16(ofile, bitmap_header->y); |
| 630 |
|
| 631 |
PHYSFSX_writeU8(ofile, bitmap_header->nplanes); |
| 632 |
PHYSFSX_writeU8(ofile, bitmap_header->masking); |
| 633 |
PHYSFSX_writeU8(ofile, bitmap_header->compression); |
| 634 |
PHYSFSX_writeU8(ofile, 0); /* pad */ |
| 635 |
|
| 636 |
PHYSFS_writeSBE16(ofile, bitmap_header->transparentcolor); |
| 637 |
PHYSFSX_writeU8(ofile, bitmap_header->xaspect); |
| 638 |
PHYSFSX_writeU8(ofile, bitmap_header->yaspect); |
| 639 |
|
| 640 |
PHYSFS_writeSBE16(ofile, bitmap_header->pagewidth); |
| 641 |
PHYSFS_writeSBE16(ofile, bitmap_header->pageheight); |
| 642 |
|
| 643 |
return IFF_NO_ERROR; |
| 644 |
|
| 645 |
} |
| 646 |
|
| 647 |
static int write_pal(PHYSFS_File *ofile,iff_bitmap_header *bitmap_header) |
| 648 |
{ |
| 649 |
int n_colors = 1<<bitmap_header->nplanes; |
| 650 |
|
| 651 |
put_sig(cmap_sig,ofile); |
| 652 |
// PHYSFS_writeSBE32(sizeof(pal_entry) * n_colors,ofile); |
| 653 |
PHYSFS_writeSBE32(ofile, 3 * n_colors); |
| 654 |
|
| 655 |
range_for (auto &c, bitmap_header->palette) |
| 656 |
{ |
| 657 |
unsigned char r,g,b; |
| 658 |
r = c.r * 4 + (c.r?3:0); |
| 659 |
g = c.g * 4 + (c.g?3:0); |
| 660 |
b = c.b * 4 + (c.b?3:0); |
| 661 |
PHYSFSX_writeU8(ofile, r); |
| 662 |
PHYSFSX_writeU8(ofile, g); |
| 663 |
PHYSFSX_writeU8(ofile, b); |
| 664 |
} |
| 665 |
|
| 666 |
return IFF_NO_ERROR; |
| 667 |
} |
| 668 |
|
| 669 |
static int rle_span(ubyte *dest,ubyte *src,int len) |
| 670 |
{ |
| 671 |
int lit_cnt,rep_cnt; |
| 672 |
ubyte last,*cnt_ptr,*dptr; |
| 673 |
|
| 674 |
cnt_ptr=0; |
| 675 |
|
| 676 |
dptr = dest; |
| 677 |
|
| 678 |
last=src[0]; lit_cnt=1; |
| 679 |
|
| 680 |
for (int n=1;n<len;n++) { |
| 681 |
|
| 682 |
if (src[n] == last) { |
| 683 |
|
| 684 |
rep_cnt = 2; |
| 685 |
|
| 686 |
n++; |
| 687 |
while (n<len && rep_cnt<128 && src[n]==last) {n++; rep_cnt++;} |
| 688 |
|
| 689 |
if (rep_cnt > 2 || lit_cnt < 2) { |
| 690 |
|
| 691 |
if (lit_cnt > 1) {*cnt_ptr = lit_cnt-2; --dptr;} //save old lit count |
| 692 |
*dptr++ = -(rep_cnt-1); |
| 693 |
*dptr++ = last; |
| 694 |
last = src[n]; |
| 695 |
lit_cnt = (n<len)?1:0; |
| 696 |
|
| 697 |
continue; //go to next char |
| 698 |
} else n--; |
| 699 |
|
| 700 |
} |
| 701 |
|
| 702 |
{ |
| 703 |
|
| 704 |
if (lit_cnt==1) { |
| 705 |
cnt_ptr = dptr++; //save place for count |
| 706 |
*dptr++=last; //store first char |
| 707 |
} |
| 708 |
|
| 709 |
*dptr++ = last = src[n]; |
| 710 |
|
| 711 |
if (lit_cnt == 127) { |
| 712 |
*cnt_ptr = lit_cnt; |
| 713 |
//cnt_ptr = dptr++; |
| 714 |
lit_cnt = 1; |
| 715 |
last = src[++n]; |
| 716 |
} |
| 717 |
else lit_cnt++; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
} |
| 722 |
|
| 723 |
if (lit_cnt==1) { |
| 724 |
*dptr++ = 0; |
| 725 |
*dptr++=last; //store first char |
| 726 |
} |
| 727 |
else if (lit_cnt > 1) |
| 728 |
*cnt_ptr = lit_cnt-1; |
| 729 |
|
| 730 |
return dptr-dest; |
| 731 |
} |
| 732 |
|
| 733 |
#define EVEN(a) ((a+1)&0xfffffffel) |
| 734 |
|
| 735 |
//returns length of chunk |
| 736 |
static int write_body(PHYSFS_File *ofile,iff_bitmap_header *bitmap_header,int compression_on) |
| 737 |
{ |
| 738 |
int w=bitmap_header->w,h=bitmap_header->h; |
| 739 |
int y,odd=w&1; |
| 740 |
long len = EVEN(w) * h,newlen,total_len=0; |
| 741 |
uint8_t *p=bitmap_header->raw_data; |
| 742 |
long save_pos; |
| 743 |
|
| 744 |
put_sig(body_sig,ofile); |
| 745 |
save_pos = PHYSFS_tell(ofile); |
| 746 |
PHYSFS_writeSBE32(ofile, len); |
| 747 |
|
| 748 |
const auto new_span = compression_on ? std::make_unique<uint8_t[]>(bitmap_header->w + (bitmap_header->w / 128 + 2) * 2) : {}; |
| 749 |
for (y=bitmap_header->h;y--;) { |
| 750 |
|
| 751 |
if (compression_on) { |
| 752 |
total_len += newlen = rle_span(new_span,p,bitmap_header->w+odd); |
| 753 |
PHYSFSX_writeBytes(ofile, new_span, newlen); |
| 754 |
} |
| 755 |
else |
| 756 |
PHYSFSX_writeBytes(ofile, p, bitmap_header->w + odd); |
| 757 |
|
| 758 |
p+=bitmap_header->row_size; //bitmap_header->w; |
| 759 |
} |
| 760 |
|
| 761 |
if (compression_on) { //write actual data length |
| 762 |
assert(PHYSFS_seek(ofile, save_pos) != 0); |
| 763 |
(void)save_pos; |
| 764 |
PHYSFS_writeSBE32(ofile, total_len); |
| 765 |
Assert(PHYSFSX_fseek(ofile,total_len,SEEK_CUR)==0); |
| 766 |
if (total_len&1) PHYSFSX_writeU8(ofile, 0); //pad to even |
| 767 |
} |
| 768 |
return ((compression_on) ? (EVEN(total_len)+8) : (len+8)); |
| 769 |
|
| 770 |
} |
| 771 |
|
| 772 |
#if WRITE_TINY |
| 773 |
//write a small representation of a bitmap. returns size |
| 774 |
int write_tiny(PHYSFS_File *ofile,iff_bitmap_header *bitmap_header,int compression_on) |
| 775 |
{ |
| 776 |
int skip; |
| 777 |
int new_w,new_h; |
| 778 |
int len,total_len=0,newlen; |
| 779 |
int x,xofs,odd; |
| 780 |
uint8_t *p = bitmap_header->raw_data; |
| 781 |
ubyte tspan[80],new_span[80*2]; |
| 782 |
long save_pos; |
| 783 |
|
| 784 |
skip = max((bitmap_header->w+79)/80,(bitmap_header->h+63)/64); |
| 785 |
|
| 786 |
new_w = bitmap_header->w / skip; |
| 787 |
new_h = bitmap_header->h / skip; |
| 788 |
|
| 789 |
odd = new_w & 1; |
| 790 |
|
| 791 |
len = new_w * new_h + 4; |
| 792 |
|
| 793 |
put_sig(tiny_sig,ofile); |
| 794 |
save_pos = PHYSFS_tell(ofile); |
| 795 |
PHYSFS_writeSBE32(ofile, EVEN(len)); |
| 796 |
|
| 797 |
PHYSFS_writeSBE16(ofile, new_w); |
| 798 |
PHYSFS_writeSBE16(ofile, new_h); |
| 799 |
|
| 800 |
for (int y=0;y<new_h;y++) { |
| 801 |
for (x=xofs=0;x<new_w;x++,xofs+=skip) |
| 802 |
tspan[x] = p[xofs]; |
| 803 |
|
| 804 |
if (compression_on) { |
| 805 |
total_len += newlen = rle_span(new_span,tspan,new_w+odd); |
| 806 |
PHYSFSX_writeBytes(ofile, new_span, newlen); |
| 807 |
} |
| 808 |
else |
| 809 |
PHYSFSX_writeBytes(ofile, p, new_w + odd); |
| 810 |
|
| 811 |
p += skip * bitmap_header->row_size; //bitmap_header->w; |
| 812 |
|
| 813 |
} |
| 814 |
|
| 815 |
if (compression_on) { |
| 816 |
assert(PHYSFS_seek(ofile, save_pos) != 0); |
| 817 |
(void)save_pos; |
| 818 |
PHYSFS_writeSBE32(ofile, 4+total_len); |
| 819 |
Assert(PHYSFSX_fseek(ofile,4+total_len,SEEK_CUR)==0); |
| 820 |
if (total_len&1) PHYSFSX_writeU8(ofile, 0); //pad to even |
| 821 |
} |
| 822 |
|
| 823 |
return ((compression_on) ? (EVEN(total_len)+8+4) : (len+8)); |
| 824 |
} |
| 825 |
#endif |
| 826 |
|
| 827 |
static int write_pbm(PHYSFS_File *ofile,iff_bitmap_header *bitmap_header,int compression_on) /* writes a pbm iff file */ |
| 828 |
{ |
| 829 |
int ret; |
| 830 |
long raw_size = EVEN(bitmap_header->w) * bitmap_header->h; |
| 831 |
long body_size,tiny_size,pbm_size = 4 + BMHD_SIZE + 8 + EVEN(raw_size) + sizeof(rgb_t)*(1<<bitmap_header->nplanes)+8; |
| 832 |
long save_pos; |
| 833 |
|
| 834 |
put_sig(form_sig,ofile); |
| 835 |
save_pos = PHYSFS_tell(ofile); |
| 836 |
PHYSFS_writeSBE32(ofile, pbm_size+8); |
| 837 |
put_sig(pbm_sig,ofile); |
| 838 |
|
| 839 |
ret = write_bmhd(ofile,bitmap_header); |
| 840 |
if (ret != IFF_NO_ERROR) return ret; |
| 841 |
|
| 842 |
ret = write_pal(ofile,bitmap_header); |
| 843 |
if (ret != IFF_NO_ERROR) return ret; |
| 844 |
|
| 845 |
#if WRITE_TINY |
| 846 |
tiny_size = write_tiny(ofile,bitmap_header,compression_on); |
| 847 |
#else |
| 848 |
tiny_size = 0; |
| 849 |
#endif |
| 850 |
|
| 851 |
body_size = write_body(ofile,bitmap_header,compression_on); |
| 852 |
|
| 853 |
pbm_size = 4 + BMHD_SIZE + body_size + tiny_size + sizeof(rgb_t)*(1<<bitmap_header->nplanes)+8; |
| 854 |
|
| 855 |
assert(PHYSFS_seek(ofile, save_pos) != 0); |
| 856 |
(void)save_pos; |
| 857 |
PHYSFS_writeSBE32(ofile, pbm_size+8); |
| 858 |
Assert(PHYSFSX_fseek(ofile,pbm_size+8,SEEK_CUR)==0); |
| 859 |
|
| 860 |
return ret; |
| 861 |
|
| 862 |
} |
| 863 |
|
| 864 |
//writes an IFF file from a grs_bitmap structure. writes palette if not null |
| 865 |
//returns error codes - see IFF.H. |
| 866 |
int iff_write_bitmap(const char *ofilename,grs_bitmap *bm,palette_array_t *palette) |
| 867 |
{ |
| 868 |
iff_bitmap_header bmheader; |
| 869 |
int ret; |
| 870 |
int compression_on; |
| 871 |
|
| 872 |
if (bm->bm_type == bm_mode::rgb15) return IFF_BAD_BM_TYPE; |
| 873 |
|
| 874 |
#if COMPRESS |
| 875 |
compression_on = (bm->bm_w>=MIN_COMPRESS_WIDTH); |
| 876 |
#else |
| 877 |
compression_on = 0; |
| 878 |
#endif |
| 879 |
|
| 880 |
//fill in values in bmheader |
| 881 |
|
| 882 |
bmheader.x = bmheader.y = 0; |
| 883 |
bmheader.w = bm->bm_w; |
| 884 |
bmheader.h = bm->bm_h; |
| 885 |
bmheader.type = TYPE_PBM; |
| 886 |
bmheader.transparentcolor = iff_transparent_color; |
| 887 |
bmheader.pagewidth = bm->bm_w; //I don't think it matters what I write |
| 888 |
bmheader.pageheight = bm->bm_h; |
| 889 |
bmheader.nplanes = 8; |
| 890 |
bmheader.masking = mskNone; |
| 891 |
if (iff_has_transparency) { |
| 892 |
bmheader.masking |= mskHasTransparentColor; |
| 893 |
} |
| 894 |
bmheader.compression = (compression_on?cmpByteRun1:cmpNone); |
| 895 |
|
| 896 |
bmheader.xaspect = bmheader.yaspect = 1; //I don't think it matters what I write |
| 897 |
bmheader.raw_data = bm->get_bitmap_data(); |
| 898 |
bmheader.row_size = bm->bm_rowsize; |
| 899 |
|
| 900 |
if (palette) |
| 901 |
bmheader.palette = *palette; |
| 902 |
|
| 903 |
//open file and write |
| 904 |
|
| 905 |
RAIIPHYSFS_File ofile{PHYSFS_openWrite(ofilename)}; |
| 906 |
if (!ofile) |
| 907 |
return IFF_NO_FILE; |
| 908 |
|
| 909 |
ret = write_pbm(ofile,&bmheader,compression_on); |
| 910 |
return ret; |
| 911 |
} |
| 912 |
#endif |
| 913 |
|
| 914 |
//read in many brushes. fills in array of pointers, and n_bitmaps. |
| 915 |
//returns iff error codes |
| 916 |
iff_read_result iff_read_animbrush(const char *ifilename) |
| 917 |
{ |
| 918 |
iff_read_result result; |
| 919 |
int sig,form_len; |
| 920 |
long form_type; |
| 921 |
|
| 922 |
auto ifile = PHYSFSX_openReadBuffered(ifilename).first; |
| 923 |
if (!ifile) |
| 924 |
{ |
| 925 |
result.status = iff_status_code::no_file; |
| 926 |
return result; |
| 927 |
} |
| 928 |
|
| 929 |
sig=get_sig(ifile); |
| 930 |
PHYSFS_readSBE32(ifile, &form_len); |
| 931 |
|
| 932 |
if (sig != form_sig) { |
| 933 |
result.status = iff_status_code::not_iff; |
| 934 |
return result; |
| 935 |
} |
| 936 |
|
| 937 |
form_type = get_sig(ifile); |
| 938 |
|
| 939 |
if ((form_type == pbm_sig) || (form_type == ilbm_sig)) |
| 940 |
result.status = iff_status_code::form_bitmap; |
| 941 |
else if (form_type == anim_sig) { |
| 942 |
int anim_end = PHYSFS_tell(ifile) + form_len - 4; |
| 943 |
|
| 944 |
while (PHYSFS_tell(ifile) < anim_end && result.n_bitmaps < result.bm.size()) { |
| 945 |
const auto n_bitmaps = result.n_bitmaps; |
| 946 |
auto &n = result.bm[n_bitmaps]; |
| 947 |
n = std::make_unique<grs_main_bitmap>(); |
| 948 |
grs_bitmap *const prev_bm = n_bitmaps > 0 ? result.bm[n_bitmaps - 1].get() : nullptr; |
| 949 |
|
| 950 |
/* iff_parse_bitmap needs a bm_mode, but only to test |
| 951 |
* whether to do RGB conversion. Historically, anim files |
| 952 |
* do not require RGB conversion, so pass a mode that skips |
| 953 |
* the conversion. |
| 954 |
*/ |
| 955 |
if (const auto ret{iff_parse_bitmap(ifile, *n.get(), bm_mode::linear, n_bitmaps > 0 ? nullptr : &result.palette, prev_bm)}; ret != iff_status_code::no_error) |
| 956 |
{ |
| 957 |
result.status = ret; |
| 958 |
return result; |
| 959 |
} |
| 960 |
++result.n_bitmaps; |
| 961 |
} |
| 962 |
|
| 963 |
if (PHYSFS_tell(ifile) < anim_end) //ran out of room |
| 964 |
result.status = iff_status_code::too_many_bms; |
| 965 |
} |
| 966 |
else |
| 967 |
result.status = iff_status_code::unknown_form; |
| 968 |
return result; |
| 969 |
} |
| 970 |
|
| 971 |
//text for error messges |
| 972 |
constexpr char error_messages[] = { |
| 973 |
"No error.\0" |
| 974 |
"Not enough mem for loading or processing bitmap.\0" |
| 975 |
"IFF file has unknown FORM type.\0" |
| 976 |
"Not an IFF file.\0" |
| 977 |
"Cannot open file.\0" |
| 978 |
"Tried to save invalid type, like bm_mode::rgb15.\0" |
| 979 |
"Bad data in file.\0" |
| 980 |
"ANIM file cannot be loaded with normal bitmap loader.\0" |
| 981 |
"Normal bitmap file cannot be loaded with anim loader.\0" |
| 982 |
"Array not big enough on anim brush read.\0" |
| 983 |
"Unknown mask type in bitmap header.\0" |
| 984 |
"Error reading file.\0" |
| 985 |
}; |
| 986 |
|
| 987 |
|
| 988 |
//function to return pointer to error message |
| 989 |
const char *iff_errormsg(const iff_status_code status) |
| 990 |
{ |
| 991 |
auto error_number{underlying_value(status)}; |
| 992 |
const char *p{error_messages}; |
| 993 |
while (error_number--) { |
| 994 |
if (!p) return NULL; |
| 995 |
p += strlen(p)+1; |
| 996 |
} |
| 997 |
return p; |
| 998 |
|
| 999 |
} |
| 1000 |
|
| 1001 |
|