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_color png_palette[256];
180 for (unsigned i = 0; i < 256; i++) {
181 png_palette[i].red = palette[i].red;
182 png_palette[i].green = palette[i].green;
183 png_palette[i].blue = palette[i].blue;
186 png_set_IHDR(png, info, width, height, 8,
187 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
188 PNG_COMPRESSION_TYPE_DEFAULT,
189 PNG_FILTER_TYPE_DEFAULT);
191 png_set_PLTE(png, info, png_palette, 256);
192 png_set_rows(png, info, framedata);
193 png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
196 * Unfortunately, LBX doesn't translate nicely to PNG here.
197 * LBX has a 256 colour palette _plus_ transparency.
198 * We'll form an RGBA PNG to deal with this.
201 png_set_IHDR(png, info, width, height, 8,
202 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
203 PNG_COMPRESSION_TYPE_DEFAULT,
204 PNG_FILTER_TYPE_DEFAULT);
206 png_write_info(png, info);
208 for (y = 0; y < height; y++) {
209 for (x = 0; x < width; x++) {
210 row[4*x+0] = palette[framedata[y][x]].red;
211 row[4*x+1] = palette[framedata[y][x]].green;
212 row[4*x+2] = palette[framedata[y][x]].blue;
213 row[4*x+3] = (mask[y][x]) ? -1 : 0;
216 png_write_row(png, row);
219 png_write_end(png, NULL);
222 png_destroy_write_struct(&png, &info);
226 printf("wrote %s\n", name);
237 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
239 LBX_IMG *overimg = lbximg_fopen(f);
240 struct lbx_imginfo info;
243 errmsg("failed to open override image: %s\n", lbx_strerror());
246 lbximg_getinfo(overimg, &info);
248 if (!info.palettesz) {
249 errmsg("override image has no palette.\n", 0);
250 lbximg_close(overimg);
254 if (lbximg_getpalette(overimg, palette) == -1) {
255 errmsg("error reading override palette: %s\n", lbx_strerror());
256 lbximg_close(overimg);
260 lbximg_close(overimg);
264 static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
265 FILE *palf, FILE *override,
266 struct lbx_colour palette[static 256])
270 /* In no-palette mode, use palette indices for colour. */
272 for (i = 0; i < 256; i++) {
273 palette[i] = (struct lbx_colour){i,i,i};
280 if (!palf && !info->palettesz && !override) {
281 errmsg("no palette available.\n", 0);
285 /* Default the palette to a wonderful pink. */
286 for (i = 0; i < 256; i++) {
287 palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
290 /* Read the external palette, if any. */
291 if (palf && lbximg_loadpalette(palf, palette) == -1) {
292 errmsg("error reading external palette: %s\n", lbx_strerror());
296 /* Read the embedded palette, if any. */
297 if (info->palettesz && lbximg_getpalette(img, palette) == -1) {
298 errmsg("error reading embedded palette: %s\n", lbx_strerror());
302 /* Read the override palette, if any. */
303 if (override && loadoverride(override, palette) == -1) {
310 int decode(LBX_IMG *img, FILE *palf, FILE *override, char **argv)
312 unsigned char *framebits;
313 struct lbx_colour palette[256];
314 struct lbx_imginfo info;
318 lbximg_getinfo(img, &info);
320 framebits = malloc(info.nframes / CHAR_BIT + 1);
325 /* Figure out what images we're extracting. */
327 /* extract all images by default. */
328 memset(framebits, -1, info.nframes / CHAR_BIT + 1);
330 for (i = 0; argv[i]; i++) {
331 parserange(&info, argv[i], framebits);
335 if (loadpalette(img, &info, palf, override, palette) == -1) {
339 /* Extract the images, in order. */
340 for (i = 0; i < info.nframes; i++) {
341 unsigned char **data;
342 unsigned char **mask;
344 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
347 data = lbximg_getframe(img, i);
349 errmsg("error in frame %u: %s\n", i, lbx_strerror());
353 mask = lbximg_getmask(img);
355 if (!outpng(i, data, mask, info.width, info.height, palette)) {
361 errmsg("no frames extracted.\n", 0);
372 int main(int argc, char **argv)
374 int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
375 struct lbx_pipe_state state = { .f = stdin };
376 FILE *palf = NULL, *overf = NULL;
377 const char *name = "stdin";
380 static const char *sopts = "idvf:p:O:V";
381 static const struct option lopts[] = {
382 { "ident", 0, NULL, 'i' },
383 { "decode", 0, NULL, 'd' },
384 { "verbose", 0, NULL, 'v' },
385 { "file", 1, NULL, 'f' },
386 { "palette", 1, NULL, 'p' },
387 { "override", 1, NULL, 'p' },
389 { "version", 0, NULL, 'V' },
390 { "usage", 0, NULL, 'U' },
391 { "help", 0, NULL, 'H' },
393 { "nopalette", 0, &usepalette, 0 },
398 progname = "lbximg"; /* argv[0]; */
399 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
411 name = strrchr(optarg, '/');
412 name = name ? name+1 : optarg;
414 if (!freopen(optarg, "rb", state.f)) {
415 errmsg("failed to open %s: %m\n", optarg);
420 palf = fopen(optarg, "rb");
422 errmsg("failed to open %s: %m\n", optarg);
428 overf = fopen(optarg, "rb");
430 errmsg("failed to open %s: %m\n", optarg);
435 puts(VERSION_BOILERPLATE("lbximg"));
449 if (mode == MODE_NONE) {
450 errmsg("you must specify a mode.\n", 0);
454 if (fseek(state.f, 0, SEEK_CUR) == 0)
455 img = lbximg_open(state.f, &lbx_default_fops, NULL);
457 img = lbximg_open(&state, &lbx_pipe_fops, NULL);
460 errmsg("failed to open image: %s.\n", lbx_strerror());
464 if (verbose || mode == MODE_IDENT) {
465 struct lbx_imginfo info;
466 lbximg_getinfo(img, &info);
468 printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
469 name, info.width, info.height, info.nframes,
470 info.palettesz ? ", embedded palette" : "",
471 info.looping ? ", loops" : "");
476 rc = decode(img, palf, overf, &argv[optind]);