1 /* 2ooM: The Master of Orion II Reverse Engineering Project
2 * Simple command-line tool to convert an LBX image to a set of PNGs.
3 * Copyright (C) 2006-2008 Nick Bowler
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 static int verbose = 0;
35 static char *outname = "out";
36 static int usepalette = 1;
38 static const char *progname;
39 #define errmsg(fmt, ...) (\
40 fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
49 int parserange(struct lbx_imginfo *info, char *str, unsigned char *bits)
51 unsigned long start, end;
55 start = strtoul(str, &endptr, 0);
56 if (start >= info->nframes) {
57 errmsg("frame %lu out of range.\n", start);
62 errmsg("invalid frame range: %s.\n", str);
71 end = strtoul(endptr+1, &endptr, 0);
72 if (end >= info->nframes) {
73 errmsg("frame %lu out of range.\n", end);
78 end = info->nframes - 1;
81 errmsg("invalid frame range: %s.\n", str);
86 errmsg("invalid frame range: %s.\n", str);
90 for (i = start; i <= end; i++) {
91 bits[i / CHAR_BIT] |= 1 << (i % CHAR_BIT);
97 static int ismasked(unsigned char **mask, unsigned width, unsigned height)
100 for (y = 0; y < height; y++) {
101 for (x = 0; x < width; x++) {
102 if (mask[y][x] == 0) return 1;
109 int outpng(unsigned int frameno,
110 unsigned char **framedata, unsigned char **mask,
111 unsigned int width, unsigned int height,
112 struct lbx_colour palette[static 256])
114 char name[strlen(outname) + sizeof ".65535.png"];
122 assert(frameno < 65536);
123 snprintf(name, sizeof name, "%s.%03d.png", outname, frameno);
125 row = malloc(4 * width);
127 errmsg("failed to allocate row buffer: %s\n", strerror(errno));
131 of = fopen(name, "wb");
133 errmsg("failed to open %s: %s.\n", name, strerror(errno));
138 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
140 errmsg("failed to init libpng.\n", 0);
144 info = png_create_info_struct(png);
146 errmsg("failed to init libpng.\n", 0);
147 png_destroy_write_struct(&png, NULL);
151 if (setjmp(png_jmpbuf(png))) {
153 png_destroy_write_struct(&png, &info);
157 png_init_io(png, of);
159 if (!ismasked(mask, width, height)) {
161 * This case is easy; we can just feed the palette and pixel
162 * data to libpng and let it do its magic.
165 png_set_IHDR(png, info, width, height, 8,
166 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
167 PNG_COMPRESSION_TYPE_DEFAULT,
168 PNG_FILTER_TYPE_DEFAULT);
170 png_set_PLTE(png, info, (png_colorp)palette, 256);
171 png_set_rows(png, info, framedata);
172 png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
175 * Unfortunately, LBX doesn't translate nicely to PNG here.
176 * LBX has a 256 colour palette _plus_ transparency.
177 * We'll form an RGBA PNG to deal with this.
180 png_set_IHDR(png, info, width, height, 8,
181 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
182 PNG_COMPRESSION_TYPE_DEFAULT,
183 PNG_FILTER_TYPE_DEFAULT);
185 png_write_info(png, info);
187 for (y = 0; y < height; y++) {
188 for (x = 0; x < width; x++) {
189 row[4*x+0] = palette[framedata[y][x]].red;
190 row[4*x+1] = palette[framedata[y][x]].green;
191 row[4*x+2] = palette[framedata[y][x]].blue;
192 row[4*x+3] = (mask[y][x]) ? -1 : 0;
195 png_write_row(png, row);
198 png_write_end(png, NULL);
201 png_destroy_write_struct(&png, &info);
205 printf("wrote %s\n", name);
216 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
218 LBX_IMG *overimg = lbximg_fopen(f);
219 struct lbx_imginfo info;
222 errmsg("failed to open override image: %s\n", lbx_strerror());
225 lbximg_getinfo(overimg, &info);
227 if (!info.palettesz) {
228 errmsg("override image has no palette.\n", 0);
229 lbximg_close(overimg);
233 if (lbximg_getpalette(overimg, palette) == -1) {
234 errmsg("error reading override palette: %s\n", lbx_strerror());
235 lbximg_close(overimg);
239 lbximg_close(overimg);
243 static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
244 FILE *palf, FILE *override,
245 struct lbx_colour palette[static 256])
249 /* In no-palette mode, use palette indices for colour. */
251 for (i = 0; i < 256; i++) {
252 palette[i] = (struct lbx_colour){i,i,i};
259 if (!palf && !info->palettesz && !override) {
260 errmsg("no palette available.\n", 0);
264 /* Default the palette to a wonderful pink. */
265 for (i = 0; i < 256; i++) {
266 palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
269 /* Read the external palette, if any. */
270 if (palf && lbximg_loadpalette(palf, palette) == -1) {
271 errmsg("error reading external palette: %s\n", lbx_strerror());
275 /* Read the embedded palette, if any. */
276 if (info->palettesz && lbximg_getpalette(img, palette) == -1) {
277 errmsg("error reading embedded palette: %s\n", lbx_strerror());
281 /* Read the override palette, if any. */
282 if (override && loadoverride(override, palette) == -1) {
289 int decode(LBX_IMG *img, FILE *palf, FILE *override, char **argv)
291 unsigned char *framebits;
292 struct lbx_colour palette[256];
293 struct lbx_imginfo info;
297 lbximg_getinfo(img, &info);
299 framebits = malloc(info.nframes / CHAR_BIT + 1);
304 /* Figure out what images we're extracting. */
306 /* extract all images by default. */
307 memset(framebits, -1, info.nframes / CHAR_BIT + 1);
309 for (i = 0; argv[i]; i++) {
310 parserange(&info, argv[i], framebits);
314 if (loadpalette(img, &info, palf, override, palette) == -1) {
318 /* Extract the images, in order. */
319 for (i = 0; i < info.nframes; i++) {
320 unsigned char **data;
321 unsigned char **mask;
323 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
326 data = lbximg_getframe(img, i);
328 errmsg("error in frame %u: %s\n", i, lbx_strerror());
332 mask = lbximg_getmask(img);
334 if (!outpng(i, data, mask, info.width, info.height, palette)) {
340 errmsg("no frames extracted.\n", 0);
351 int main(int argc, char **argv)
353 int mode = MODE_NONE;
354 FILE *inf = stdin, *palf = NULL, *overf = NULL;
355 const char *name = "stdin";
359 static const char *sopts = "idvf:p:O:";
360 static const struct option lopts[] = {
361 { "ident", 0, NULL, 'i' },
362 { "decode", 0, NULL, 'd' },
363 { "verbose", 0, NULL, 'v' },
364 { "file", 1, NULL, 'f' },
365 { "palette", 1, NULL, 'p' },
366 { "override", 1, NULL, 'p' },
368 { "nopalette", 0, &usepalette, 0 },
373 progname = "lbximg"; /* argv[0]; */
374 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
386 if (strcmp(optarg, "-") == 0)
389 name = strrchr(optarg, '/');
390 name = name ? name+1 : optarg;
392 inf = fopen(optarg, "rb");
394 errmsg("failed to open %s: %m\n", optarg);
399 palf = fopen(optarg, "rb");
401 errmsg("failed to open %s: %m\n", optarg);
407 overf = fopen(optarg, "rb");
409 errmsg("failed to open %s: %m\n", optarg);
419 if (mode == MODE_NONE) {
420 errmsg("you must specify a mode.\n", 0);
424 img = lbximg_fopen(inf);
426 errmsg("failed to open image: %s.\n", lbx_strerror());
430 if (verbose || mode == MODE_IDENT) {
431 struct lbx_imginfo info;
432 lbximg_getinfo(img, &info);
434 printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
435 name, info.width, info.height, info.nframes,
436 info.palettesz ? ", embedded palette" : "",
437 info.looping ? ", loops" : "");
442 if (decode(img, palf, overf, &argv[optind])) {