From 9f1b686f41bbee8e299610ffb0cb623c5db99fd5 Mon Sep 17 00:00:00 2001 From: Jesse Allen Date: Tue, 26 Apr 2022 18:52:34 -0700 Subject: [PATCH] Add utility to compress game image files. --- .gitignore | 3 +- tools/icnpack.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 tools/icnpack.c diff --git a/.gitignore b/.gitignore index 993c99b7..b2dcaeac 100644 --- a/.gitignore +++ b/.gitignore @@ -96,5 +96,6 @@ doc/main.fdb_latexmk doc/main.fls doc/main.ilg doc/main.ind -tools/genfont tools/bmpicn +tools/genfont +tools/icnpack diff --git a/tools/icnpack.c b/tools/icnpack.c new file mode 100644 index 00000000..d0c43ec3 --- /dev/null +++ b/tools/icnpack.c @@ -0,0 +1,126 @@ +/* + * Seven Kingdoms: Ancient Adversaries + * + * Copyright 2018-2022 Jesse Allen + * Copyright 1997,1998 Enlight Software Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +/* + * To compile: + * gcc -g -I../include icnpack.c -o icnpack + * + */ + +#include +#include +#include + +//------------------------------------------------- +// +// Format of the compressed data +// compressed decompressed +// FF FF +// FE (-2) FF FF +// FD (-3) FF FF FF +// FC (-4) FF FF FF FF +// FB (-5) FF FF FF FF FF +// FA (-6) FF FF FF FF FF FF +// F9 (-7) FF FF FF FF FF FF FF +// F8 B FF ... +// +// Header COLCODE.H provides some helpers macros. +// +//------------------------------------------------- +// +// Format of the bitmap data : +// +// width +// height +// bitmap image +// +//------------------------------------------------- + + +int main(int argc, char **argv) +{ + short w, h; + unsigned char *pixels; + FILE *fh; + + if( argc<2 ) + { + printf("Usage: icnbmp input.icn\n"); + return 1; + } + + fh = fopen(argv[1], "r"); + if( !fh ) + return 1; + fread(&w, sizeof(short), 1, fh); + fread(&h, sizeof(short), 1, fh); + + pixels = malloc(w*h); + fread(pixels, 1, w*h, fh); + fclose(fh); + + fh = fopen("OUT.ICN", "w"); + if( !fh ) + return 1; + fwrite(&w, sizeof(short), 1, fh); + fwrite(&h, sizeof(short), 1, fh); + + unsigned char run = 0; + for( int i = 0; i < w*h; i++ ) + { + if( run && pixels[i] != TRANSPARENT_CODE ) + { + unsigned char code; + if( run == 1 ) + { + code = TRANSPARENT_CODE; + } + if( run <= UNIQUE_REPEAT_CODE_NUM ) + { + code = FEW_TRANSPARENT_CODE(run); + } + else + { + code = MANY_TRANSPARENT_CODE; + fwrite(&code, sizeof(unsigned char), 1, fh); + code = run; + } + fwrite(&code, sizeof(unsigned char), 1, fh); + run = 0; + } + if( pixels[i] == TRANSPARENT_CODE ) + { + unsigned char code; + if( ++run < TRANSPARENT_CODE ) + continue; + code = MANY_TRANSPARENT_CODE; + fwrite(&code, sizeof(unsigned char), 1, fh); + code = 0xff; + fwrite(&code, sizeof(unsigned char), 1, fh); + run = 0; + continue; + } + fwrite(&pixels[i], sizeof(unsigned char), 1, fh); + } + + fclose(fh); + return 0; +}