|
| 1 | +/* |
| 2 | + * Seven Kingdoms: Ancient Adversaries |
| 3 | + * |
| 4 | + * Copyright 2018-2022 Jesse Allen |
| 5 | + * Copyright 1997,1998 Enlight Software Ltd. |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +/* |
| 23 | + * To compile: |
| 24 | + * gcc -g -I../include icnpack.c -o icnpack |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +#include <stdlib.h> |
| 29 | +#include <stdio.h> |
| 30 | +#include <COLCODE.h> |
| 31 | + |
| 32 | +//------------------------------------------------- |
| 33 | +// |
| 34 | +// Format of the compressed data |
| 35 | +// compressed decompressed |
| 36 | +// FF FF |
| 37 | +// FE (-2) FF FF |
| 38 | +// FD (-3) FF FF FF |
| 39 | +// FC (-4) FF FF FF FF |
| 40 | +// FB (-5) FF FF FF FF FF |
| 41 | +// FA (-6) FF FF FF FF FF FF |
| 42 | +// F9 (-7) FF FF FF FF FF FF FF |
| 43 | +// F8 B FF ... <B times> |
| 44 | +// |
| 45 | +// Header COLCODE.H provides some helpers macros. |
| 46 | +// |
| 47 | +//------------------------------------------------- |
| 48 | +// |
| 49 | +// Format of the bitmap data : |
| 50 | +// |
| 51 | +// <short> width |
| 52 | +// <short> height |
| 53 | +// <char..> bitmap image |
| 54 | +// |
| 55 | +//------------------------------------------------- |
| 56 | + |
| 57 | + |
| 58 | +int main(int argc, char **argv) |
| 59 | +{ |
| 60 | + short w, h; |
| 61 | + unsigned char *pixels; |
| 62 | + FILE *fh; |
| 63 | + |
| 64 | + if( argc<2 ) |
| 65 | + { |
| 66 | + printf("Usage: icnbmp input.icn\n"); |
| 67 | + return 1; |
| 68 | + } |
| 69 | + |
| 70 | + fh = fopen(argv[1], "r"); |
| 71 | + if( !fh ) |
| 72 | + return 1; |
| 73 | + fread(&w, sizeof(short), 1, fh); |
| 74 | + fread(&h, sizeof(short), 1, fh); |
| 75 | + |
| 76 | + pixels = malloc(w*h); |
| 77 | + fread(pixels, 1, w*h, fh); |
| 78 | + fclose(fh); |
| 79 | + |
| 80 | + fh = fopen("OUT.ICN", "w"); |
| 81 | + if( !fh ) |
| 82 | + return 1; |
| 83 | + fwrite(&w, sizeof(short), 1, fh); |
| 84 | + fwrite(&h, sizeof(short), 1, fh); |
| 85 | + |
| 86 | + unsigned char run = 0; |
| 87 | + for( int i = 0; i < w*h; i++ ) |
| 88 | + { |
| 89 | + if( run && pixels[i] != TRANSPARENT_CODE ) |
| 90 | + { |
| 91 | + unsigned char code; |
| 92 | + if( run == 1 ) |
| 93 | + { |
| 94 | + code = TRANSPARENT_CODE; |
| 95 | + } |
| 96 | + if( run <= UNIQUE_REPEAT_CODE_NUM ) |
| 97 | + { |
| 98 | + code = FEW_TRANSPARENT_CODE(run); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + code = MANY_TRANSPARENT_CODE; |
| 103 | + fwrite(&code, sizeof(unsigned char), 1, fh); |
| 104 | + code = run; |
| 105 | + } |
| 106 | + fwrite(&code, sizeof(unsigned char), 1, fh); |
| 107 | + run = 0; |
| 108 | + } |
| 109 | + if( pixels[i] == TRANSPARENT_CODE ) |
| 110 | + { |
| 111 | + unsigned char code; |
| 112 | + if( ++run < TRANSPARENT_CODE ) |
| 113 | + continue; |
| 114 | + code = MANY_TRANSPARENT_CODE; |
| 115 | + fwrite(&code, sizeof(unsigned char), 1, fh); |
| 116 | + code = 0xff; |
| 117 | + fwrite(&code, sizeof(unsigned char), 1, fh); |
| 118 | + run = 0; |
| 119 | + continue; |
| 120 | + } |
| 121 | + fwrite(&pixels[i], sizeof(unsigned char), 1, fh); |
| 122 | + } |
| 123 | + |
| 124 | + fclose(fh); |
| 125 | + return 0; |
| 126 | +} |
0 commit comments