| 1 | /* |
| 2 | * PROPRIETARY INFORMATION. This software is proprietary to POWDER |
| 3 | * Development, and is not to be reproduced, transmitted, or disclosed |
| 4 | * in any way without written permission. |
| 5 | * |
| 6 | * Produced by: Jeff Lait |
| 7 | * |
| 8 | * POWDER Development |
| 9 | * |
| 10 | * NAME: bmp.cpp ( BMP Library, C++ ) |
| 11 | * |
| 12 | * COMMENTS: |
| 13 | */ |
| 14 | |
| 15 | #include "bmp.h" |
| 16 | |
| 17 | #include "gfx/all_bitmaps.h" |
| 18 | #include "mygba.h" |
| 19 | #include "gfxengine.h" |
| 20 | |
| 21 | #ifdef USING_DS |
| 22 | #include <nds.h> |
| 23 | #include <fat.h> |
| 24 | |
| 25 | #define iprintf printf |
| 26 | |
| 27 | #else |
| 28 | #endif |
| 29 | |
| 30 | struct BMPHEAD |
| 31 | { |
| 32 | char id[2]; |
| 33 | int size; |
| 34 | int reserved; |
| 35 | int headersize; |
| 36 | int infosize; |
| 37 | int width; |
| 38 | int depth; |
| 39 | short biplane; |
| 40 | short bits; |
| 41 | int comp; |
| 42 | int imagesize; |
| 43 | int xpelperm; |
| 44 | int ypelperm; |
| 45 | int clrused; |
| 46 | int clrimport; |
| 47 | }; |
| 48 | |
| 49 | struct RGBQUAD |
| 50 | { |
| 51 | u8 blue; |
| 52 | u8 green; |
| 53 | u8 red; |
| 54 | u8 filler; |
| 55 | }; |
| 56 | |
| 57 | void |
| 58 | shorttorgb(u16 cd, u8 &r, u8 &g, u8 &b) |
| 59 | { |
| 60 | r = cd & 31; |
| 61 | r <<= 3; |
| 62 | cd >>= 5; |
| 63 | g = cd & 31; |
| 64 | g <<= 3; |
| 65 | cd >>= 5; |
| 66 | b = cd & 31; |
| 67 | } |
| 68 | |
| 69 | u16 |
| 70 | rgbtoshort(u8 red, u8 green, u8 blue) |
| 71 | { |
| 72 | u16 result; |
| 73 | |
| 74 | // Reduce to 5 bits. |
| 75 | red >>= 3; |
| 76 | green >>= 3; |
| 77 | blue >>= 3; |
| 78 | |
| 79 | result = blue; |
| 80 | result <<= 5; |
| 81 | result |= green; |
| 82 | result <<= 5; |
| 83 | result |= red; |
| 84 | |
| 85 | return result; |
| 86 | } |
| 87 | |
| 88 | static short |
| 89 | gfx_readportableshort(FILE *fp) |
| 90 | { |
| 91 | u8 c[2]; |
| 92 | short result; |
| 93 | |
| 94 | fread(c, 2, 1, fp); |
| 95 | |
| 96 | result = c[0]; |
| 97 | result |= c[1] << 8; |
| 98 | |
| 99 | return result; |
| 100 | } |
| 101 | |
| 102 | static int |
| 103 | gfx_readportableint(FILE *fp) |
| 104 | { |
| 105 | unsigned short s1, s2; |
| 106 | int result; |
| 107 | |
| 108 | s1 = (unsigned short) gfx_readportableshort(fp); |
| 109 | s2 = (unsigned short) gfx_readportableshort(fp); |
| 110 | |
| 111 | result = s1; |
| 112 | result |= s2 << 16; |
| 113 | |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | unsigned short * |
| 119 | bmp_load(const char *name, int &w, int &h, bool quiet) |
| 120 | { |
| 121 | // printf("Load bmp %s\n", name); |
| 122 | |
| 123 | FILE *fp; |
| 124 | BMPHEAD head; |
| 125 | int i, x, y; |
| 126 | |
| 127 | fp = hamfake_fopen(name, "rb"); |
| 128 | if (!fp) |
| 129 | { |
| 130 | if (!quiet) |
| 131 | printf("Failure to open %s\n", name); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | fread(head.id, 2, 1, fp); |
| 136 | |
| 137 | head.size = gfx_readportableint(fp); |
| 138 | head.reserved = gfx_readportableint(fp); |
| 139 | head.headersize = gfx_readportableint(fp); |
| 140 | head.infosize = gfx_readportableint(fp); |
| 141 | head.width = gfx_readportableint(fp); |
| 142 | head.depth = gfx_readportableint(fp); |
| 143 | head.biplane = gfx_readportableshort(fp); |
| 144 | head.bits = gfx_readportableshort(fp); |
| 145 | head.comp = gfx_readportableint(fp); |
| 146 | head.imagesize = gfx_readportableint(fp); |
| 147 | head.xpelperm = gfx_readportableint(fp); |
| 148 | head.ypelperm = gfx_readportableint(fp); |
| 149 | head.clrused = gfx_readportableint(fp); |
| 150 | head.clrimport = gfx_readportableint(fp); |
| 151 | |
| 152 | if (head.id[0] != 'B' && head.id[1] != 'M') |
| 153 | { |
| 154 | printf("%s does not look like a bitmap!\n", name); |
| 155 | fclose(fp); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | if (head.bits != 24 && head.bits != 8) |
| 160 | { |
| 161 | printf("Cannot support %d bit bmp.\n", head.bits); |
| 162 | printf("Only 24 or 8 bit supported.\n"); |
| 163 | fclose(fp); |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | u16 *result; |
| 168 | |
| 169 | h = head.depth; |
| 170 | w = head.width; |
| 171 | |
| 172 | // printf("Desired image is %d by %d\n", h, w); |
| 173 | |
| 174 | result = new u16 [h * w]; |
| 175 | |
| 176 | RGBQUAD palette[256]; |
| 177 | |
| 178 | if (head.bits == 8) |
| 179 | { |
| 180 | int numentries = 256; |
| 181 | |
| 182 | // Clear out palette to start. |
| 183 | memset(palette, 0, 256*sizeof(RGBQUAD)); |
| 184 | |
| 185 | // A non-zero clrused means we only have that number of |
| 186 | // colours in our header. |
| 187 | if (head.clrused) |
| 188 | numentries = head.clrused; |
| 189 | fread(palette, sizeof(RGBQUAD), numentries, fp); |
| 190 | } |
| 191 | |
| 192 | // BMPs are stored bottom up, so we reverse it here. |
| 193 | for (y = head.depth - 1; y >= 0; y--) |
| 194 | { |
| 195 | for (x = 0; x < head.width; x++) |
| 196 | { |
| 197 | u8 red, blue, green, idx; |
| 198 | |
| 199 | if (head.bits == 24) |
| 200 | { |
| 201 | // BMPs are stored BGR. |
| 202 | fread(&blue, 1, 1, fp); |
| 203 | fread(&green, 1, 1, fp); |
| 204 | fread(&red, 1, 1, fp); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | fread(&idx, 1, 1, fp); |
| 209 | blue = palette[idx].blue; |
| 210 | green = palette[idx].green; |
| 211 | red = palette[idx].red; |
| 212 | } |
| 213 | |
| 214 | // Account for endian issues & repack |
| 215 | i = x + y * head.width; |
| 216 | |
| 217 | // Reduce to 5 bits. |
| 218 | red >>= 3; |
| 219 | green >>= 3; |
| 220 | blue >>= 3; |
| 221 | |
| 222 | result[i] = blue; |
| 223 | result[i] <<= 5; |
| 224 | result[i] |= green; |
| 225 | result[i] <<= 5; |
| 226 | result[i] |= red; |
| 227 | } |
| 228 | // NOTE: We need to align our scan lines to 4 byte boundaries. |
| 229 | if ((x * 3) & 3) |
| 230 | { |
| 231 | int extra; |
| 232 | u8 data; |
| 233 | |
| 234 | extra = 4 - (x * 3 & 3); |
| 235 | while (extra--) |
| 236 | fread(&data, 1, 1, fp); |
| 237 | } |
| 238 | } |
| 239 | return result; |
| 240 | } |
| 241 | |
| 242 | static u8 * |
| 243 | bmp_tilify(const u16 *flat, int w, int h, s16 *cd_to_pal, |
| 244 | int tilewidth, int tileheight, int numtilex, int numtiley) |
| 245 | { |
| 246 | int bytesperrow, tileperrow; |
| 247 | int tx, ty, sx, sy, x, y; |
| 248 | u8 *result; |
| 249 | int off = 0; |
| 250 | int th; |
| 251 | int len; |
| 252 | |
| 253 | len = w * h; |
| 254 | result = new u8 [len]; |
| 255 | |
| 256 | tileperrow = w / (tilewidth * numtilex); |
| 257 | bytesperrow = (tilewidth * tileheight * numtilex * numtiley) * tileperrow; |
| 258 | |
| 259 | th = len / bytesperrow; |
| 260 | |
| 261 | for (ty = 0; ty < th; ty++) |
| 262 | { |
| 263 | for (tx = 0; tx < w / (tilewidth * numtilex); tx++) |
| 264 | { |
| 265 | for (sy = 0; sy < numtiley; sy++) |
| 266 | { |
| 267 | for (sx = 0; sx < numtilex; sx++) |
| 268 | { |
| 269 | for (y = 0; y < tileheight; y++) |
| 270 | { |
| 271 | for (x = 0; x < tilewidth; x++) |
| 272 | { |
| 273 | result[off++] = (u8) |
| 274 | cd_to_pal[flat[ |
| 275 | (ty * tileheight * numtiley + sy * tileheight + y) * w |
| 276 | + (tx * numtilex * tilewidth + sx * tilewidth + x) ]]; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return result; |
| 285 | } |
| 286 | |
| 287 | #define NUM_ALPHA 5 |
| 288 | |
| 289 | void |
| 290 | floodColours(s16 *cd_to_pal) |
| 291 | { |
| 292 | bool found = true; |
| 293 | int i = 0, axis; |
| 294 | u8 color[3]; |
| 295 | u16 cd; |
| 296 | s16 *other; |
| 297 | |
| 298 | other = new s16[(1<<15)]; |
| 299 | |
| 300 | int round = 0; |
| 301 | |
| 302 | while (found) |
| 303 | { |
| 304 | found = false; |
| 305 | for (i = 0; i < (1<<15); i++) |
| 306 | { |
| 307 | other[i] = cd_to_pal[i]; |
| 308 | if (cd_to_pal[i] != -1) |
| 309 | { |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | shorttorgb(i, color[0], color[1], color[2]); |
| 314 | |
| 315 | for (axis = 0; axis < 3; axis++) |
| 316 | { |
| 317 | color[axis] >>= 3; |
| 318 | } |
| 319 | for (axis = 0; axis < 3; axis++) |
| 320 | { |
| 321 | if (color[axis]) |
| 322 | { |
| 323 | color[axis]--; |
| 324 | cd = rgbtoshort(color[0]*8, color[1]*8, color[2]*8); |
| 325 | if (cd_to_pal[cd] != -1) |
| 326 | { |
| 327 | other[i] = cd_to_pal[cd]; |
| 328 | found = true; |
| 329 | } |
| 330 | color[axis]++; |
| 331 | } |
| 332 | if (color[axis] < 31) |
| 333 | { |
| 334 | color[axis]++; |
| 335 | cd = rgbtoshort(color[0]*8, color[1]*8, color[2]*8); |
| 336 | if (cd_to_pal[cd] != -1) |
| 337 | { |
| 338 | other[i] = cd_to_pal[cd]; |
| 339 | found = true; |
| 340 | } |
| 341 | color[axis]--; |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | if (found) |
| 346 | { |
| 347 | memcpy(cd_to_pal, other, (1<<15) * sizeof(s16)); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | delete [] other; |
| 352 | } |
| 353 | |
| 354 | bool |
| 355 | bmp_loadExtraTileset() |
| 356 | { |
| 357 | // First, load our alphabet, dungeon, and minimap. We load them |
| 358 | // as 15 bit images. |
| 359 | u16 *a16[NUM_ALPHA], *d16, *m16, *mf16, *sprite16; |
| 360 | int aw, ah, dw, dh, mw, mh, sw, sh; |
| 361 | bool failed = false, alphafail = false; |
| 362 | int anum; |
| 363 | |
| 364 | int tilewidth = 8; |
| 365 | |
| 366 | const char *aname[NUM_ALPHA] = |
| 367 | { |
| 368 | "alphabet_classic", |
| 369 | "alphabet_brass", |
| 370 | "alphabet_shadow", |
| 371 | "alphabet_light", |
| 372 | "alphabet_heavy", |
| 373 | }; |
| 374 | BUF buf; |
| 375 | |
| 376 | for (anum = 0; anum < NUM_ALPHA; anum++) |
| 377 | { |
| 378 | buf.sprintf("gfx/%s.bmp", aname[anum]); |
| 379 | a16[anum] = bmp_load(buf.buffer(), aw, ah, true); |
| 380 | if (!a16[anum]) |
| 381 | alphafail = true; |
| 382 | } |
| 383 | d16 = bmp_load("gfx/dungeon16.bmp", dw, dh, true); |
| 384 | m16 = bmp_load("gfx/mini16.bmp", mw, mh, true); |
| 385 | mf16 = bmp_load("gfx/minif16.bmp", mw, mh, true); |
| 386 | sprite16 = bmp_load("gfx/sprite16.bmp", sw, sh, true); |
| 387 | |
| 388 | // Check if any failed... |
| 389 | if (alphafail || !d16 || !mf16 || !m16 || !sprite16) |
| 390 | { |
| 391 | // IF partial failure, spam. |
| 392 | if (!alphafail || d16 || mf16 || m16 || sprite16) |
| 393 | { |
| 394 | if (alphafail) |
| 395 | printf("Failed to load gfx/alphabet_*.bmp\n"); |
| 396 | if (!d16) |
| 397 | printf("Failed to load gfx/dungeon16.bmp\n"); |
| 398 | if (!m16) |
| 399 | printf("Failed to load gfx/mini16.bmp\n"); |
| 400 | if (!mf16) |
| 401 | printf("Failed to load gfx/minif16.bmp\n"); |
| 402 | if (!sprite16) |
| 403 | printf("Failed to load gfx/sprite16.bmp\n"); |
| 404 | } |
| 405 | |
| 406 | failed = true; |
| 407 | } |
| 408 | |
| 409 | // Calculate our tile width. This is only legal in true SDL |
| 410 | // modes. |
| 411 | #ifdef USING_SDL |
| 412 | tilewidth = aw / 10; |
| 413 | #endif |
| 414 | |
| 415 | // Verify our sizes are accurate. |
| 416 | if (!alphafail && (aw != 10*tilewidth || ah < 88)) |
| 417 | { |
| 418 | printf("gfx/alphabet.bmp incorrect size, must be 80x88\n"); |
| 419 | failed = true; |
| 420 | } |
| 421 | if (d16 && (dw != 48*tilewidth || dh < 192)) |
| 422 | { |
| 423 | printf("gfx/dungeon16.bmp incorrect size, must be 384x192\n"); |
| 424 | failed = true; |
| 425 | } |
| 426 | if (m16 && (mw != 48*tilewidth || mh < 64)) |
| 427 | { |
| 428 | printf("gfx/mini16.bmp incorrect size, must be 384x64\n"); |
| 429 | failed = true; |
| 430 | } |
| 431 | if (sprite16 && (sw != 48*tilewidth || sh < 96)) |
| 432 | { |
| 433 | printf("gfx/sprite16.bmp incorrect size, must be 384x96\n"); |
| 434 | failed = true; |
| 435 | } |
| 436 | |
| 437 | if (failed) |
| 438 | { |
| 439 | for (anum = 0; anum < NUM_ALPHA; anum++) |
| 440 | delete [] a16[anum]; |
| 441 | delete [] d16; |
| 442 | delete [] m16; |
| 443 | delete [] mf16; |
| 444 | delete [] sprite16; |
| 445 | |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | // We now want to build a consolidated palette. |
| 450 | // We can be wasteful of memory here as this is startup and the DS |
| 451 | // has 4Mb anyways. |
| 452 | s16 *cd_to_pal; |
| 453 | u16 *palette; |
| 454 | |
| 455 | // Set everything to unallocated. |
| 456 | cd_to_pal = new s16[(1<<15)]; |
| 457 | memset(cd_to_pal, 0xff, sizeof(s16) * (1<<15)); |
| 458 | |
| 459 | // Palette maps back from palette space to raw space |
| 460 | palette = new u16[256]; |
| 461 | memset(palette, 0, sizeof(u16) * 256); |
| 462 | |
| 463 | int numcol = 0; // Total assigned colours. |
| 464 | u16 cd; |
| 465 | |
| 466 | // We always want black to map to transparent, so assign it first. |
| 467 | cd = 0; |
| 468 | cd_to_pal[cd] = numcol; |
| 469 | palette[numcol] = cd; |
| 470 | numcol++; |
| 471 | |
| 472 | // Likewise, force a bright red, bright green, and bright yellow |
| 473 | // for our selfish text purposes. |
| 474 | cd = rgbtoshort(255, 128, 128); |
| 475 | cd_to_pal[cd] = numcol; |
| 476 | palette[numcol] = cd; |
| 477 | numcol++; |
| 478 | cd = rgbtoshort(255, 255, 128); |
| 479 | cd_to_pal[cd] = numcol; |
| 480 | palette[numcol] = cd; |
| 481 | numcol++; |
| 482 | cd = rgbtoshort(128, 255, 128); |
| 483 | cd_to_pal[cd] = numcol; |
| 484 | palette[numcol] = cd; |
| 485 | numcol++; |
| 486 | |
| 487 | // Now, collect all colours. |
| 488 | int overflow = 0; // Ignored mappings. |
| 489 | int i; |
| 490 | |
| 491 | for (i = 0; i < mw * mh; i++) |
| 492 | { |
| 493 | if (cd_to_pal[m16[i]] == -1) |
| 494 | { |
| 495 | cd_to_pal[m16[i]] = numcol; |
| 496 | palette[numcol] = m16[i]; |
| 497 | numcol++; |
| 498 | if (numcol > 255) { floodColours(cd_to_pal); numcol--; overflow++; } |
| 499 | } |
| 500 | if (cd_to_pal[mf16[i]] == -1) |
| 501 | { |
| 502 | cd_to_pal[mf16[i]] = numcol; |
| 503 | palette[numcol] = mf16[i]; |
| 504 | numcol++; |
| 505 | if (numcol > 255) { floodColours(cd_to_pal); numcol--; overflow++; } |
| 506 | } |
| 507 | } |
| 508 | for (i = 0; i < dw * dh; i++) |
| 509 | { |
| 510 | if (cd_to_pal[d16[i]] == -1) |
| 511 | { |
| 512 | cd_to_pal[d16[i]] = numcol; |
| 513 | palette[numcol] = d16[i]; |
| 514 | numcol++; |
| 515 | if (numcol > 255) { floodColours(cd_to_pal); numcol--; overflow++; } |
| 516 | } |
| 517 | } |
| 518 | for (anum = 0; anum < NUM_ALPHA; anum++) |
| 519 | { |
| 520 | for (i = 0; i < aw * ah; i++) |
| 521 | { |
| 522 | if (cd_to_pal[a16[anum][i]] == -1) |
| 523 | { |
| 524 | cd_to_pal[a16[anum][i]] = numcol; |
| 525 | palette[numcol] = a16[anum][i]; |
| 526 | numcol++; |
| 527 | if (numcol > 255) { floodColours(cd_to_pal); numcol--; overflow++; } |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | // We have now built our look up table. Report any errors in building. |
| 533 | if (overflow) |
| 534 | { |
| 535 | printf("%d colours lost in truncation to 256 palette.\n", overflow); |
| 536 | } |
| 537 | |
| 538 | // Convert to black our magic green. |
| 539 | for (i = 0; i < numcol; i++) |
| 540 | { |
| 541 | if (palette[i] == 0x3e0) |
| 542 | palette[i] = 0; |
| 543 | } |
| 544 | |
| 545 | // Set our tileset's palette. |
| 546 | glb_tilesets[NUM_TILESETS-1].palette = palette; |
| 547 | glb_tilesets[NUM_TILESETS-1].tilewidth = tilewidth; |
| 548 | |
| 549 | // Create tiled versions. |
| 550 | for (anum = 0; anum < NUM_ALPHA; anum++) |
| 551 | { |
| 552 | glb_tilesets[NUM_TILESETS-1].alphabet[anum] = bmp_tilify(a16[anum], |
| 553 | aw, ah, cd_to_pal, |
| 554 | tilewidth, tilewidth, 1, 1); |
| 555 | } |
| 556 | glb_tilesets[NUM_TILESETS-1].dungeon = bmp_tilify(d16, dw, dh, cd_to_pal, |
| 557 | tilewidth, tilewidth, 2, 2); |
| 558 | glb_tilesets[NUM_TILESETS-1].mini = bmp_tilify(m16, mw, mh, cd_to_pal, |
| 559 | tilewidth, tilewidth, 2, 2); |
| 560 | glb_tilesets[NUM_TILESETS-1].minif = bmp_tilify(mf16, mw, mh, cd_to_pal, |
| 561 | tilewidth, tilewidth, 2, 2); |
| 562 | |
| 563 | |
| 564 | // Now repeat the process again, this time for our sprite bitmap. |
| 565 | // Set everything to unallocated. |
| 566 | memset(cd_to_pal, 0xff, sizeof(s16) * (1<<15)); |
| 567 | |
| 568 | // Palette maps back from palette space to raw space |
| 569 | palette = new u16[256]; |
| 570 | memset(palette, 0, sizeof(u16) * 256); |
| 571 | |
| 572 | // We always want black to map to transparent, so assign it first. |
| 573 | cd_to_pal[0] = 0; |
| 574 | palette[0] = 0; |
| 575 | |
| 576 | // Now, collect all colours. |
| 577 | numcol = 1; // Total assigned colours. |
| 578 | overflow = 0; // Ignored mappings. |
| 579 | |
| 580 | for (i = 0; i < sw * sh; i++) |
| 581 | { |
| 582 | if (cd_to_pal[sprite16[i]] == -1) |
| 583 | { |
| 584 | cd_to_pal[sprite16[i]] = numcol; |
| 585 | palette[numcol] = sprite16[i]; |
| 586 | numcol++; |
| 587 | if (numcol > 255) { floodColours(cd_to_pal); numcol--; overflow++; } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | // We have now built our look up table. Report any errors in building. |
| 592 | if (overflow) |
| 593 | { |
| 594 | printf("%d colours lost in truncation to 256 palette in sprites.\n", overflow); |
| 595 | } |
| 596 | |
| 597 | // Convert to black our magic green. |
| 598 | for (i = 0; i < numcol; i++) |
| 599 | { |
| 600 | if (palette[i] == 0x3e0) |
| 601 | palette[i] = 0; |
| 602 | } |
| 603 | |
| 604 | glb_tilesets[NUM_TILESETS-1].spritepalette = palette; |
| 605 | glb_tilesets[NUM_TILESETS-1].sprite = bmp_tilify(sprite16, sw, sh, cd_to_pal, |
| 606 | tilewidth, tilewidth, 2, 2); |
| 607 | // Free unused structures |
| 608 | for (anum = 0; anum < NUM_ALPHA; anum++) |
| 609 | delete [] a16[anum]; |
| 610 | delete [] d16; |
| 611 | delete [] m16; |
| 612 | delete [] mf16; |
| 613 | delete [] sprite16; |
| 614 | delete [] cd_to_pal; |
| 615 | |
| 616 | return true; |
| 617 | } |