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-2008 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))) {
167 png_destroy_write_struct(&png, &info);
171 png_init_io(png, of);
173 if (!ismasked(mask, width, height)) {
175 * This case is easy; we can just feed the palette and pixel
176 * data to libpng and let it do its magic.
179 png_set_IHDR(png, info, width, height, 8,
180 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
181 PNG_COMPRESSION_TYPE_DEFAULT,
182 PNG_FILTER_TYPE_DEFAULT);
184 png_set_PLTE(png, info, (png_colorp)palette, 256);
185 png_set_rows(png, info, framedata);
186 png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
189 * Unfortunately, LBX doesn't translate nicely to PNG here.
190 * LBX has a 256 colour palette _plus_ transparency.
191 * We'll form an RGBA PNG to deal with this.
194 png_set_IHDR(png, info, width, height, 8,
195 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
196 PNG_COMPRESSION_TYPE_DEFAULT,
197 PNG_FILTER_TYPE_DEFAULT);
199 png_write_info(png, info);
201 for (y = 0; y < height; y++) {
202 for (x = 0; x < width; x++) {
203 row[4*x+0] = palette[framedata[y][x]].red;
204 row[4*x+1] = palette[framedata[y][x]].green;
205 row[4*x+2] = palette[framedata[y][x]].blue;
206 row[4*x+3] = (mask[y][x]) ? -1 : 0;
209 png_write_row(png, row);
212 png_write_end(png, NULL);
215 png_destroy_write_struct(&png, &info);
219 printf("wrote %s\n", name);
230 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
232 LBX_IMG *overimg = lbximg_fopen(f);
233 struct lbx_imginfo info;
236 errmsg("failed to open override image: %s\n", lbx_strerror());
239 lbximg_getinfo(overimg, &info);
241 if (!info.palettesz) {
242 errmsg("override image has no palette.\n", 0);
243 lbximg_close(overimg);
247 if (lbximg_getpalette(overimg, palette) == -1) {
248 errmsg("error reading override palette: %s\n", lbx_strerror());
249 lbximg_close(overimg);
253 lbximg_close(overimg);
257 static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
258 FILE *palf, FILE *override,
259 struct lbx_colour palette[static 256])
263 /* In no-palette mode, use palette indices for colour. */
265 for (i = 0; i < 256; i++) {
266 palette[i] = (struct lbx_colour){i,i,i};
273 if (!palf && !info->palettesz && !override) {
274 errmsg("no palette available.\n", 0);
278 /* Default the palette to a wonderful pink. */
279 for (i = 0; i < 256; i++) {
280 palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
283 /* Read the external palette, if any. */
284 if (palf && lbximg_loadpalette(palf, palette) == -1) {
285 errmsg("error reading external palette: %s\n", lbx_strerror());
289 /* Read the embedded palette, if any. */
290 if (info->palettesz && lbximg_getpalette(img, palette) == -1) {
291 errmsg("error reading embedded palette: %s\n", lbx_strerror());
295 /* Read the override palette, if any. */
296 if (override && loadoverride(override, palette) == -1) {
303 int decode(LBX_IMG *img, FILE *palf, FILE *override, char **argv)
305 unsigned char *framebits;
306 struct lbx_colour palette[256];
307 struct lbx_imginfo info;
311 lbximg_getinfo(img, &info);
313 framebits = malloc(info.nframes / CHAR_BIT + 1);
318 /* Figure out what images we're extracting. */
320 /* extract all images by default. */
321 memset(framebits, -1, info.nframes / CHAR_BIT + 1);
323 for (i = 0; argv[i]; i++) {
324 parserange(&info, argv[i], framebits);
328 if (loadpalette(img, &info, palf, override, palette) == -1) {
332 /* Extract the images, in order. */
333 for (i = 0; i < info.nframes; i++) {
334 unsigned char **data;
335 unsigned char **mask;
337 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
340 data = lbximg_getframe(img, i);
342 errmsg("error in frame %u: %s\n", i, lbx_strerror());
346 mask = lbximg_getmask(img);
348 if (!outpng(i, data, mask, info.width, info.height, palette)) {
354 errmsg("no frames extracted.\n", 0);
365 int main(int argc, char **argv)
367 int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
368 struct lbx_pipe_state state = { .f = stdin };
369 FILE *palf = NULL, *overf = NULL;
370 const char *name = "stdin";
373 static const char *sopts = "idvf:p:O:V";
374 static const struct option lopts[] = {
375 { "ident", 0, NULL, 'i' },
376 { "decode", 0, NULL, 'd' },
377 { "verbose", 0, NULL, 'v' },
378 { "file", 1, NULL, 'f' },
379 { "palette", 1, NULL, 'p' },
380 { "override", 1, NULL, 'p' },
382 { "version", 0, NULL, 'V' },
383 { "usage", 0, NULL, 'U' },
384 { "help", 0, NULL, 'H' },
386 { "nopalette", 0, &usepalette, 0 },
391 progname = "lbximg"; /* argv[0]; */
392 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
404 name = strrchr(optarg, '/');
405 name = name ? name+1 : optarg;
407 if (!freopen(optarg, "rb", state.f)) {
408 errmsg("failed to open %s: %m\n", optarg);
413 palf = fopen(optarg, "rb");
415 errmsg("failed to open %s: %m\n", optarg);
421 overf = fopen(optarg, "rb");
423 errmsg("failed to open %s: %m\n", optarg);
428 puts(VERSION_BOILERPLATE("lbximg"));
442 if (mode == MODE_NONE) {
443 errmsg("you must specify a mode.\n", 0);
447 if (fseek(state.f, 0, SEEK_CUR) == 0)
448 img = lbximg_open(state.f, &lbx_default_fops, NULL);
450 img = lbximg_open(&state, &lbx_pipe_fops, NULL);
453 errmsg("failed to open image: %s.\n", lbx_strerror());
457 if (verbose || mode == MODE_IDENT) {
458 struct lbx_imginfo info;
459 lbximg_getinfo(img, &info);
461 printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
462 name, info.width, info.height, info.nframes,
463 info.palettesz ? ", embedded palette" : "",
464 info.looping ? ", loops" : "");
469 rc = decode(img, palf, overf, &argv[optind]);