2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Simple command-line tool to convert an LBX image to a set of PNGs.
4 * Copyright (C) 2006-2010 Nick Bowler
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 static int verbose = 0;
36 static char *outname = "out";
37 static int usepalette = 1;
39 static void printusage(void)
41 puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]"
43 puts(" [frameno ...]");
46 static void printhelp(void)
49 puts("For now, see the man page for detailed help.");
52 static const char *progname;
53 #define errmsg(fmt, ...) (\
54 fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
63 int parserange(struct lbx_imginfo *info, char *str, unsigned char *bits)
65 unsigned long start, end;
69 start = strtoul(str, &endptr, 0);
70 if (start >= info->nframes) {
71 errmsg("frame %lu out of range.\n", start);
76 errmsg("invalid frame range: %s.\n", str);
85 end = strtoul(endptr+1, &endptr, 0);
86 if (end >= info->nframes) {
87 errmsg("frame %lu out of range.\n", end);
92 end = info->nframes - 1;
95 errmsg("invalid frame range: %s.\n", str);
100 errmsg("invalid frame range: %s.\n", str);
104 for (i = start; i <= end; i++) {
105 bits[i / CHAR_BIT] |= 1 << (i % CHAR_BIT);
111 static int ismasked(unsigned char **mask, unsigned width, unsigned height)
114 for (y = 0; y < height; y++) {
115 for (x = 0; x < width; x++) {
116 if (mask[y][x] == 0) return 1;
123 int outpng(unsigned int frameno,
124 unsigned char **framedata, unsigned char **mask,
125 unsigned int width, unsigned int height,
126 struct lbx_colour palette[static 256])
128 char name[strlen(outname) + sizeof ".65535.png"];
136 assert(frameno < 65536);
137 snprintf(name, sizeof name, "%s.%03d.png", outname, frameno);
139 row = malloc(4 * width);
141 errmsg("failed to allocate row buffer: %s\n", strerror(errno));
145 of = fopen(name, "wb");
147 errmsg("failed to open %s: %s.\n", name, strerror(errno));
152 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
154 errmsg("failed to init libpng.\n", 0);
158 info = png_create_info_struct(png);
160 errmsg("failed to init libpng.\n", 0);
161 png_destroy_write_struct(&png, NULL);
165 if (setjmp(png_jmpbuf(png))) {
166 png_destroy_write_struct(&png, &info);
170 png_init_io(png, of);
172 if (!ismasked(mask, width, height)) {
174 * This case is easy; we can just feed the palette and pixel
175 * data to libpng and let it do its magic.
178 png_color png_palette[256];
179 for (unsigned i = 0; i < 256; i++) {
180 png_palette[i].red = palette[i].red;
181 png_palette[i].green = palette[i].green;
182 png_palette[i].blue = palette[i].blue;
185 png_set_IHDR(png, info, width, height, 8,
186 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
187 PNG_COMPRESSION_TYPE_DEFAULT,
188 PNG_FILTER_TYPE_DEFAULT);
190 png_set_PLTE(png, info, png_palette, 256);
191 png_set_rows(png, info, framedata);
192 png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
195 * Unfortunately, LBX doesn't translate nicely to PNG here.
196 * LBX has a 256 colour palette _plus_ transparency.
197 * We'll form an RGBA PNG to deal with this.
200 png_set_IHDR(png, info, width, height, 8,
201 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
202 PNG_COMPRESSION_TYPE_DEFAULT,
203 PNG_FILTER_TYPE_DEFAULT);
205 png_write_info(png, info);
207 for (y = 0; y < height; y++) {
208 for (x = 0; x < width; x++) {
209 row[4*x+0] = palette[framedata[y][x]].red;
210 row[4*x+1] = palette[framedata[y][x]].green;
211 row[4*x+2] = palette[framedata[y][x]].blue;
212 row[4*x+3] = (mask[y][x]) ? -1 : 0;
215 png_write_row(png, row);
218 png_write_end(png, NULL);
221 png_destroy_write_struct(&png, &info);
226 printf("wrote %s\n", name);
235 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
237 LBX_IMG *overimg = lbximg_open(f, &lbx_default_fops, NULL);
238 struct lbx_imginfo info;
241 errmsg("failed to open override image: %s\n", lbx_strerror());
244 lbximg_getinfo(overimg, &info);
246 if (!info.palettesz) {
247 errmsg("override image has no palette.\n", 0);
248 lbximg_close(overimg);
252 if (lbximg_getpalette(overimg, palette) == -1) {
253 errmsg("error reading override palette: %s\n", lbx_strerror());
254 lbximg_close(overimg);
258 lbximg_close(overimg);
262 static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
263 FILE *palf, FILE *override,
264 struct lbx_colour palette[static 256])
268 /* In no-palette mode, use palette indices for colour. */
270 for (i = 0; i < 256; i++) {
271 palette[i] = (struct lbx_colour){i,i,i};
278 if (!palf && !info->palettesz && !override) {
279 errmsg("no palette available.\n", 0);
283 /* Default the palette to a wonderful pink. */
284 for (i = 0; i < 256; i++) {
285 palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
288 /* Read the external palette, if any. */
289 if (palf && lbximg_loadpalette(palf, &lbx_default_fops, palette) != 0) {
290 errmsg("error reading external palette: %s\n", lbx_strerror());
294 /* Read the embedded palette, if any. */
295 if (info->palettesz && lbximg_getpalette(img, palette) == -1) {
296 errmsg("error reading embedded palette: %s\n", lbx_strerror());
300 /* Read the override palette, if any. */
301 if (override && loadoverride(override, palette) == -1) {
308 int decode(LBX_IMG *img, FILE *palf, FILE *override, char **argv)
310 unsigned char *framebits;
311 struct lbx_colour palette[256];
312 struct lbx_imginfo info;
316 lbximg_getinfo(img, &info);
318 framebits = calloc(1, info.nframes / CHAR_BIT + 1);
323 /* Figure out what images we're extracting. */
325 /* extract all images by default. */
326 memset(framebits, -1, info.nframes / CHAR_BIT + 1);
328 for (i = 0; argv[i]; i++) {
329 parserange(&info, argv[i], framebits);
333 if (loadpalette(img, &info, palf, override, palette) == -1) {
337 /* Extract the images, in order. */
338 for (i = 0; i < info.nframes; i++) {
339 unsigned char **data;
340 unsigned char **mask;
342 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
345 data = lbximg_getframe(img, i);
347 errmsg("error in frame %u: %s\n", i, lbx_strerror());
351 mask = lbximg_getmask(img);
353 if (!outpng(i, data, mask, info.width, info.height, palette)) {
359 errmsg("no frames extracted.\n", 0);
370 int main(int argc, char **argv)
372 int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
373 struct lbx_pipe_state stdin_handle = { .f = stdin };
374 FILE *palf = NULL, *overf = NULL;
375 const char *file = NULL;
378 static const char *sopts = "idnvf:p:O:V";
379 static const struct option lopts[] = {
380 { "ident", 0, NULL, 'i' },
381 { "decode", 0, NULL, 'd' },
382 { "verbose", 0, NULL, 'v' },
383 { "file", 1, NULL, 'f' },
384 { "palette", 1, NULL, 'p' },
385 { "override", 1, NULL, 'p' },
387 { "version", 0, NULL, 'V' },
388 { "usage", 0, NULL, 'U' },
389 { "help", 0, NULL, 'H' },
391 { "no-palette", 0, NULL, 'n' },
396 progname = "lbximg"; /* argv[0]; */
397 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
415 palf = fopen(optarg, "rb");
417 errmsg("failed to open %s: %m\n", optarg);
423 overf = fopen(optarg, "rb");
425 errmsg("failed to open %s: %m\n", optarg);
430 puts(VERSION_BOILERPLATE("lbximg"));
444 if (mode == MODE_NONE) {
445 errmsg("you must specify a mode.\n", 0);
450 img = lbximg_fopen(file);
452 img = lbximg_open(&stdin_handle, &lbx_pipe_fops, NULL);
455 errmsg("failed to open image: %s.\n", lbx_strerror());
459 if (verbose || mode == MODE_IDENT) {
460 struct lbx_imginfo info;
465 lbximg_getinfo(img, &info);
466 printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
467 file, info.width, info.height, info.nframes,
468 info.palettesz ? ", embedded palette" : "",
469 info.chunk ? ", chunked" : "",
470 info.looping ? ", loops" : "");
475 rc = decode(img, palf, overf, &argv[optind]);