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/>.
36 static int verbose = 0;
37 static char *outname = "out";
38 static int usepalette = 1;
40 static void printusage(void)
42 puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]"
44 puts(" [frameno ...]");
47 static void printhelp(void)
50 puts("For now, see the man page for detailed help.");
53 static const char *progname;
54 #define errmsg(fmt, ...) (\
55 fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
64 int parserange(struct lbx_imginfo *info, char *str, unsigned char *bits)
66 unsigned long start, end;
70 start = strtoul(str, &endptr, 0);
71 if (start >= info->nframes) {
72 errmsg("frame %lu out of range.\n", start);
77 errmsg("invalid frame range: %s.\n", str);
86 end = strtoul(endptr+1, &endptr, 0);
87 if (end >= info->nframes) {
88 errmsg("frame %lu out of range.\n", end);
93 end = info->nframes - 1;
96 errmsg("invalid frame range: %s.\n", str);
101 errmsg("invalid frame range: %s.\n", str);
105 for (i = start; i <= end; i++) {
106 bits[i / CHAR_BIT] |= 1 << (i % CHAR_BIT);
112 static int ismasked(unsigned char **mask, unsigned width, unsigned height)
115 for (y = 0; y < height; y++) {
116 for (x = 0; x < width; x++) {
117 if (mask[y][x] == 0) return 1;
124 int outpng(unsigned int frameno,
125 unsigned char **framedata, unsigned char **mask,
126 unsigned int width, unsigned int height,
127 struct lbx_colour palette[static 256])
129 char name[strlen(outname) + sizeof ".65535.png"];
137 assert(frameno < 65536);
138 snprintf(name, sizeof name, "%s.%03d.png", outname, frameno);
140 row = malloc(4 * width);
142 errmsg("failed to allocate row buffer: %s\n", strerror(errno));
146 of = fopen(name, "wb");
148 errmsg("failed to open %s: %s.\n", name, strerror(errno));
153 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
155 errmsg("failed to init libpng.\n", 0);
159 info = png_create_info_struct(png);
161 errmsg("failed to init libpng.\n", 0);
162 png_destroy_write_struct(&png, NULL);
166 if (setjmp(png_jmpbuf(png))) {
168 png_destroy_write_struct(&png, &info);
172 png_init_io(png, of);
174 if (!ismasked(mask, width, height)) {
176 * This case is easy; we can just feed the palette and pixel
177 * data to libpng and let it do its magic.
180 png_set_IHDR(png, info, width, height, 8,
181 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
182 PNG_COMPRESSION_TYPE_DEFAULT,
183 PNG_FILTER_TYPE_DEFAULT);
185 png_set_PLTE(png, info, (png_colorp)palette, 256);
186 png_set_rows(png, info, framedata);
187 png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
190 * Unfortunately, LBX doesn't translate nicely to PNG here.
191 * LBX has a 256 colour palette _plus_ transparency.
192 * We'll form an RGBA PNG to deal with this.
195 png_set_IHDR(png, info, width, height, 8,
196 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
197 PNG_COMPRESSION_TYPE_DEFAULT,
198 PNG_FILTER_TYPE_DEFAULT);
200 png_write_info(png, info);
202 for (y = 0; y < height; y++) {
203 for (x = 0; x < width; x++) {
204 row[4*x+0] = palette[framedata[y][x]].red;
205 row[4*x+1] = palette[framedata[y][x]].green;
206 row[4*x+2] = palette[framedata[y][x]].blue;
207 row[4*x+3] = (mask[y][x]) ? -1 : 0;
210 png_write_row(png, row);
213 png_write_end(png, NULL);
216 png_destroy_write_struct(&png, &info);
220 printf("wrote %s\n", name);
231 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
233 LBX_IMG *overimg = lbximg_fopen(f);
234 struct lbx_imginfo info;
237 errmsg("failed to open override image: %s\n", lbx_strerror());
240 lbximg_getinfo(overimg, &info);
242 if (!info.palettesz) {
243 errmsg("override image has no palette.\n", 0);
244 lbximg_close(overimg);
248 if (lbximg_getpalette(overimg, palette) == -1) {
249 errmsg("error reading override palette: %s\n", lbx_strerror());
250 lbximg_close(overimg);
254 lbximg_close(overimg);
258 static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
259 FILE *palf, FILE *override,
260 struct lbx_colour palette[static 256])
264 /* In no-palette mode, use palette indices for colour. */
266 for (i = 0; i < 256; i++) {
267 palette[i] = (struct lbx_colour){i,i,i};
274 if (!palf && !info->palettesz && !override) {
275 errmsg("no palette available.\n", 0);
279 /* Default the palette to a wonderful pink. */
280 for (i = 0; i < 256; i++) {
281 palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
284 /* Read the external palette, if any. */
285 if (palf && lbximg_loadpalette(palf, palette) == -1) {
286 errmsg("error reading external palette: %s\n", lbx_strerror());
290 /* Read the embedded palette, if any. */
291 if (info->palettesz && lbximg_getpalette(img, palette) == -1) {
292 errmsg("error reading embedded palette: %s\n", lbx_strerror());
296 /* Read the override palette, if any. */
297 if (override && loadoverride(override, palette) == -1) {
304 int decode(LBX_IMG *img, FILE *palf, FILE *override, char **argv)
306 unsigned char *framebits;
307 struct lbx_colour palette[256];
308 struct lbx_imginfo info;
312 lbximg_getinfo(img, &info);
314 framebits = malloc(info.nframes / CHAR_BIT + 1);
319 /* Figure out what images we're extracting. */
321 /* extract all images by default. */
322 memset(framebits, -1, info.nframes / CHAR_BIT + 1);
324 for (i = 0; argv[i]; i++) {
325 parserange(&info, argv[i], framebits);
329 if (loadpalette(img, &info, palf, override, palette) == -1) {
333 /* Extract the images, in order. */
334 for (i = 0; i < info.nframes; i++) {
335 unsigned char **data;
336 unsigned char **mask;
338 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
341 data = lbximg_getframe(img, i);
343 errmsg("error in frame %u: %s\n", i, lbx_strerror());
347 mask = lbximg_getmask(img);
349 if (!outpng(i, data, mask, info.width, info.height, palette)) {
355 errmsg("no frames extracted.\n", 0);
366 int main(int argc, char **argv)
368 int mode = MODE_NONE;
369 FILE *inf = stdin, *palf = NULL, *overf = NULL;
370 const char *name = "stdin";
374 static const char *sopts = "idvf:p:O:V";
375 static const struct option lopts[] = {
376 { "ident", 0, NULL, 'i' },
377 { "decode", 0, NULL, 'd' },
378 { "verbose", 0, NULL, 'v' },
379 { "file", 1, NULL, 'f' },
380 { "palette", 1, NULL, 'p' },
381 { "override", 1, NULL, 'p' },
383 { "version", 0, NULL, 'V' },
384 { "usage", 0, NULL, 'U' },
385 { "help", 0, NULL, 'H' },
387 { "nopalette", 0, &usepalette, 0 },
392 progname = "lbximg"; /* argv[0]; */
393 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
405 if (strcmp(optarg, "-") == 0)
408 name = strrchr(optarg, '/');
409 name = name ? name+1 : optarg;
411 inf = fopen(optarg, "rb");
413 errmsg("failed to open %s: %m\n", optarg);
418 palf = fopen(optarg, "rb");
420 errmsg("failed to open %s: %m\n", optarg);
426 overf = fopen(optarg, "rb");
428 errmsg("failed to open %s: %m\n", optarg);
433 puts(VERSION_BOILERPLATE("lbximg"));
447 if (mode == MODE_NONE) {
448 errmsg("you must specify a mode.\n", 0);
452 img = lbximg_fopen(inf);
454 errmsg("failed to open image: %s.\n", lbx_strerror());
458 if (verbose || mode == MODE_IDENT) {
459 struct lbx_imginfo info;
460 lbximg_getinfo(img, &info);
462 printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
463 name, info.width, info.height, info.nframes,
464 info.palettesz ? ", embedded palette" : "",
465 info.looping ? ", loops" : "");
470 if (decode(img, palf, overf, &argv[optind])) {