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/>.
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))) {
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);
227 printf("wrote %s\n", name);
236 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
238 LBXimg *overimg = lbx_img_open(f, &lbx_default_fops, NULL);
239 struct lbx_imginfo info;
242 errmsg("failed to open override image: %s\n", lbx_errmsg());
245 lbx_img_getinfo(overimg, &info);
247 if (!info.palettesz) {
248 errmsg("override image has no palette.\n", 0);
249 lbx_img_close(overimg);
253 if (lbx_img_getpalette(overimg, palette) == -1) {
254 errmsg("error reading override palette: %s\n", lbx_errmsg());
255 lbx_img_close(overimg);
259 lbx_img_close(overimg);
263 static int loadpalette(LBXimg *img, struct lbx_imginfo *info,
264 FILE *palf, FILE *override,
265 struct lbx_colour palette[static 256])
269 /* In no-palette mode, use palette indices for colour. */
271 for (i = 0; i < 256; i++) {
272 palette[i] = (struct lbx_colour){i,i,i};
279 if (!palf && !info->palettesz && !override) {
280 errmsg("no palette available.\n", 0);
284 /* Default the palette to a wonderful pink. */
285 for (i = 0; i < 256; i++) {
286 palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
289 /* Read the external palette, if any. */
290 if (palf && lbx_img_loadpalette(palf, &lbx_default_fops, palette) != 0) {
291 errmsg("error reading external palette: %s\n", lbx_errmsg());
295 /* Read the embedded palette, if any. */
296 if (info->palettesz && lbx_img_getpalette(img, palette) == -1) {
297 errmsg("error reading embedded palette: %s\n", lbx_errmsg());
301 /* Read the override palette, if any. */
302 if (override && loadoverride(override, palette) == -1) {
309 int decode(LBXimg *img, FILE *palf, FILE *override, char **argv)
311 unsigned char *framebits;
312 struct lbx_colour palette[256];
313 struct lbx_imginfo info;
317 lbx_img_getinfo(img, &info);
319 framebits = calloc(1, info.nframes / CHAR_BIT + 1);
324 /* Figure out what images we're extracting. */
326 /* extract all images by default. */
327 memset(framebits, -1, info.nframes / CHAR_BIT + 1);
329 for (i = 0; argv[i]; i++) {
330 parserange(&info, argv[i], framebits);
334 if (loadpalette(img, &info, palf, override, palette) == -1) {
338 /* Extract the images, in order. */
339 for (i = 0; i < info.nframes; i++) {
340 unsigned char **data;
341 unsigned char **mask;
343 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
346 data = lbx_img_getframe(img, i);
348 errmsg("error in frame %u: %s\n", i, lbx_errmsg());
352 mask = lbx_img_getmask(img);
354 if (!outpng(i, data, mask, info.width, info.height, palette)) {
360 errmsg("no frames extracted.\n", 0);
371 int main(int argc, char **argv)
373 int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
374 struct lbx_pipe_state stdin_handle = { .f = stdin };
375 FILE *palf = NULL, *overf = NULL;
376 const char *file = NULL;
379 static const char *sopts = "idnvf:p:O:V";
380 static const struct option lopts[] = {
381 { "ident", 0, NULL, 'i' },
382 { "decode", 0, NULL, 'd' },
383 { "verbose", 0, NULL, 'v' },
384 { "file", 1, NULL, 'f' },
385 { "palette", 1, NULL, 'p' },
386 { "override", 1, NULL, 'p' },
388 { "version", 0, NULL, 'V' },
389 { "usage", 0, NULL, 'U' },
390 { "help", 0, NULL, 'H' },
392 { "no-palette", 0, NULL, 'n' },
397 progname = "lbximg"; /* argv[0]; */
398 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
416 palf = fopen(optarg, "rb");
418 errmsg("failed to open %s: %m\n", optarg);
424 overf = fopen(optarg, "rb");
426 errmsg("failed to open %s: %m\n", optarg);
431 puts(VERSION_BOILERPLATE("lbximg"));
445 if (mode == MODE_NONE) {
446 errmsg("you must specify a mode.\n", 0);
451 img = lbx_img_fopen(file);
453 img = lbx_img_open(&stdin_handle, &lbx_pipe_fops, NULL);
456 errmsg("failed to open image: %s.\n", lbx_errmsg());
460 if (verbose || mode == MODE_IDENT) {
461 struct lbx_imginfo info;
466 lbx_img_getinfo(img, &info);
467 printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
468 file, info.width, info.height, info.nframes,
469 info.palettesz ? ", embedded palette" : "",
470 info.chunk ? ", chunked" : "",
471 info.looping ? ", loops" : "");
476 rc = decode(img, palf, overf, &argv[optind]);