hl-pak0-gen/tools/bmpfix/bmpfix.c

144 lines
4.5 KiB
C
Raw Permalink Normal View History

/* this program is dedicated to all the cyberpunks out there */
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
typedef unsigned char byte;
/* the format is too annoying for me to give a damn */
static byte bmp_header[54];
static byte bmp_palette[1024];
static byte bmp_buffer[32800];
typedef union {
int rgba:32;
struct {
byte b:8;
byte g:8;
byte r:8;
byte a:8;
} u;
} pixel_t;
pixel_t bmp_pal[256] = {
0xffffff, 0xdfffbb, 0xcbefa3, 0xb7df8b, 0xa7cf77, 0x97bf63, 0x87b353,
0x77a343, 0x679333, 0x5b8327, 0x4f771b, 0x3f6713, 0x33570b, 0x2b4707,
0x1f3700, 0x172b00, 0xfff77f, 0xefeb77, 0xe3df6f, 0xd7cf67, 0xc7c35f,
0xbbb75b, 0xafab53, 0x9f9f4b, 0x939347, 0x87833f, 0x7b7737, 0x6b6b33,
0x5f5f2b, 0x534f23, 0x43431f, 0x373717, 0x2b2b13, 0x27270f, 0x27270f,
0x23230f, 0x23230f, 0x1f230f, 0x1b1f0b, 0x1b1f0b, 0x1b1b0b, 0x171b0b,
0x171b0b, 0x131707, 0x131707, 0x0f1307, 0x0f1307, 0x0f1307, 0x9baf87,
0x8f9f77, 0x83936b, 0x77875f, 0x6f7753, 0x636b47, 0x575f3b, 0x4f5333,
0x434b2b, 0x3b4727, 0x334323, 0x2b3f1f, 0x233717, 0x1b3313, 0x132f13,
0x0f2b0f, 0xefcfaf, 0xe7c7a7, 0xdfbf9f, 0xdbb79b, 0xd3b393, 0xcfab8b,
0xc7a387, 0xbf9b7f, 0xbb977b, 0xbf9373, 0xc38f6b, 0xc78b63, 0xcb875b,
0xcf8353, 0xd37f4b, 0xdb7b43, 0xd36f3f, 0xcb673b, 0xc35f37, 0xbb5733,
0xb3532f, 0xab4b2b, 0xa3432b, 0x9f3b27, 0x973723, 0x8f2f1f, 0x872b1f,
0x7f231b, 0x771f17, 0x6f1b17, 0x671713, 0x631313, 0xb7876f, 0xab7b63,
0xa36f57, 0x9b634b, 0x93573f, 0x874f37, 0x7f472f, 0x773b23, 0x6b331f,
0x632b17, 0x5b230f, 0x531f0b, 0x471707, 0x3f1300, 0x370f00, 0x2f0b00,
0xd3d7bb, 0xc7cbaf, 0xbbbfa3, 0xafb397, 0xa3a78f, 0x979b83, 0x8f8f77,
0x83836f, 0x777763, 0x6b6b57, 0x5f5f4f, 0x535343, 0x47473b, 0x3b3b2f,
0x333327, 0x27271f, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff,
0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x70527a,
0x6a4d73, 0x62486b, 0x5c4263, 0x503a57, 0x49354f, 0x423047, 0x3b2a40,
0x332538, 0x2a1e2e, 0x231926, 0x1a121c, 0x150f17, 0x0b080c, 0x070507,
0x000000, 0x70527a, 0x6a4d73, 0x62486b, 0x5b4263, 0x4f3a57, 0x4a354f,
0x423047, 0x3a2a40, 0x342538, 0x2a1e2e, 0x231926, 0x1b071f, 0x130717,
0x0b000f, 0x070007, 0x000000, 0x52737a, 0x4d6773, 0x48646b, 0x415a61,
0x3c5359, 0x354a4f, 0x2e4045, 0x29393d, 0x223033, 0x1e2b2e, 0x182123,
0x141c1e, 0x0f1517, 0x0a0e0f, 0x050707, 0x000000, 0x52727a, 0x4d6a73,
0x48636b, 0x425d63, 0x3a5257, 0x354a4f, 0x304247, 0x2a3c40, 0x253438,
0x1e2b2e, 0x192326, 0x141d1e, 0x0f1517, 0x0a0e0f, 0x050707, 0x000000,
0xffffff, 0xf7f7f7, 0xefefef, 0xe7e7e7, 0xdfdfdf, 0xd7d7d7, 0xcfcfcf,
0xc7c7c7, 0xbfbfbf, 0xb7b7b7, 0xafafaf, 0xa7a7a7, 0x9f9f9f, 0x979797,
0x8f8f8f, 0x878787, 0x7f7f7f, 0x777777, 0x6f6f6f, 0x676767, 0x5f5f5f,
0x575757, 0x4f4f4f, 0x474747, 0x3f3f3f, 0x373737, 0x2f2f2f, 0x272727,
0x1f1f1f, 0x171717, 0x0f0f0f, 0x040404
};
byte
find_in_pal(byte r, byte g, byte b)
{
for (int i = 0; i < 256; i++) {
if (bmp_pal[i].u.r == r && bmp_pal[i].u.g == g && bmp_pal[i].u.b == b)
return (byte)i;
}
return 0;
}
void
process_bmp(char *filename)
{
FILE *fp;
struct stat bmp_st;
if ((fp = fopen(filename, "rb")) == NULL) {
exit(0);
}
2023-05-06 15:38:06 -07:00
/* this is a 164x200 256-color BMP household */
stat(filename, &bmp_st);
if (bmp_st.st_size != 33878) {
fprintf(stderr, "%s has invalid player bitmap size, skipping\n", filename);
return;
}
/* lazily read the clown format in */
fread(bmp_header, 1, 54, fp);
fread(bmp_palette, 1, 1024, fp);
fread(bmp_buffer, 1, 32800, fp);
fclose(fp);
2023-05-06 15:38:06 -07:00
/* convert from 8-bit to 24-bit and back */
for (int i = 0; i < 32800; i += 1) {
int palId = bmp_buffer[i] * 4;
/* get 24-bit values */
byte r = bmp_palette[palId + 2];
byte g = bmp_palette[palId + 1];
byte b = bmp_palette[palId + 0];
/* assign the index from the good palette */
bmp_buffer[i] = find_in_pal(r, g, b);
}
/* shove the old palette into the new place */
for (int i = 0; i < 256; i += 1) {
int id = i * 4;
bmp_palette[id+2] = bmp_pal[i].u.r;
bmp_palette[id+1] = bmp_pal[i].u.g;
bmp_palette[id+0] = bmp_pal[i].u.b;
}
if ((fp = fopen(filename, "wb")) == NULL) {
exit(0);
}
fwrite(bmp_header, 1, 54, fp);
fwrite(bmp_palette, 1, 1024, fp);
fwrite(bmp_buffer, 1, 32800, fp);
fclose(fp);
}
int
main(int argc, char *argv[])
{
int c;
short p;
FILE *fPAL;
if (argc <= 1) {
fprintf(stderr, "usage: bmpfix [file ...]\n");
return 1;
}
for (c = 1; c < argc; c++)
process_bmp(argv[c]);
return 0;
}