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 © 2006-2011, 2013 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.");
59 int parserange(unsigned frames, char *str, unsigned char *bits)
61 unsigned long start, end;
65 start = strtoul(str, &endptr, 0);
66 if (start >= frames) {
67 tool_err(-1, "frame %lu out of range.", start);
72 tool_err(-1, "invalid frame range: %s.", str);
81 end = strtoul(endptr+1, &endptr, 0);
83 tool_err(-1, "frame %lu out of range.", end);
91 tool_err(-1, "invalid frame range: %s.", str);
96 tool_err(-1, "invalid frame range: %s.", str);
100 for (i = start; i <= end; i++) {
101 bits[i / CHAR_BIT] |= 1 << (i % CHAR_BIT);
107 static int ismasked(unsigned char **mask, unsigned width, unsigned height)
110 for (y = 0; y < height; y++) {
111 for (x = 0; x < width; x++) {
112 if (mask[y][x] == 0) return 1;
119 int outpng(unsigned int frameno,
120 unsigned char **framedata, unsigned char **mask,
121 unsigned int width, unsigned int height,
122 struct lbx_colour palette[static 256])
124 char name[strlen(outname) + sizeof ".65535.png"];
132 assert(frameno < 65536);
133 snprintf(name, sizeof name, "%s.%03d.png", outname, frameno);
135 row = malloc(4 * width);
137 tool_err(0, "failed to allocate row buffer");
141 of = fopen(name, "wb");
143 tool_err(0, "failed to open %s", name);
147 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
149 tool_err(-1, "failed to init libpng.");
153 info = png_create_info_struct(png);
155 tool_err(-1, "failed to init libpng.");
156 png_destroy_write_struct(&png, NULL);
160 if (setjmp(png_jmpbuf(png))) {
161 png_destroy_write_struct(&png, &info);
165 png_init_io(png, of);
167 if (!ismasked(mask, width, height)) {
169 * This case is easy; we can just feed the palette and pixel
170 * data to libpng and let it do its magic.
173 png_color png_palette[256];
174 for (unsigned i = 0; i < 256; i++) {
175 png_palette[i].red = palette[i].red;
176 png_palette[i].green = palette[i].green;
177 png_palette[i].blue = palette[i].blue;
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_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);
221 printf("wrote %s\n", name);
230 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
232 struct lbx_image *overimg = lbx_img_open(f, &lbx_default_fops, NULL);
233 struct lbx_imginfo info;
236 tool_err(-1, "failed to open override image: %s", lbx_errmsg());
239 lbx_img_getinfo(overimg, &info);
241 if (!info.palettesz) {
242 tool_err(-1, "override image has no palette.");
243 lbx_img_close(overimg);
247 if (lbx_img_getpalette(overimg, palette) == -1) {
248 tool_err(-1, "error reading override palette: %s", lbx_errmsg());
249 lbx_img_close(overimg);
253 lbx_img_close(overimg);
257 static int loadpalette(struct lbx_image *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 tool_err(-1, "no palette available.");
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 && lbx_img_loadpalette(palf, &lbx_default_fops, palette) != 0) {
285 tool_err(-1, "error reading external palette: %s", lbx_errmsg());
289 /* Read the embedded palette, if any. */
290 if (info->palettesz && lbx_img_getpalette(img, palette) == -1) {
291 tool_err(-1, "error reading embedded palette: %s", lbx_errmsg());
295 /* Read the override palette, if any. */
296 if (override && loadoverride(override, palette) == -1) {
303 int decode(struct lbx_image *img, FILE *palf, FILE *override, char **argv)
305 unsigned char *framebits;
306 struct lbx_colour palette[256];
307 struct lbx_imginfo info;
311 lbx_img_getinfo(img, &info);
313 framebits = calloc(1, img->frames / CHAR_BIT + 1);
318 /* Figure out what images we're extracting. */
320 /* extract all images by default. */
321 memset(framebits, -1, img->frames / CHAR_BIT + 1);
323 for (i = 0; argv[i]; i++) {
324 parserange(img->frames, argv[i], framebits);
328 if (loadpalette(img, &info, palf, override, palette) == -1) {
332 /* Extract the images, in order. */
333 for (i = 0; i < img->frames; i++) {
334 unsigned char **data;
335 unsigned char **mask;
337 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
340 data = lbx_img_getframe(img, i);
342 tool_err(-1, "error in frame %u: %s", i, lbx_errmsg());
346 mask = lbx_img_getmask(img);
348 if (!outpng(i, data, mask, img->width, img->height, palette)) {
354 tool_err(-1, "no frames extracted.");
365 int main(int argc, char **argv)
367 int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
368 struct lbx_pipe_state stdin_handle = { .f = stdin };
369 FILE *palf = NULL, *overf = NULL;
370 const char *file = NULL;
371 struct lbx_image *img;
373 static const char *sopts = "idnvf: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 { "no-palette", 0, NULL, 'n' },
391 tool_init("lbximg", argc, argv);
392 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
410 palf = fopen(optarg, "rb");
412 tool_err(0, "failed to open %s", optarg);
418 overf = fopen(optarg, "rb");
420 tool_err(0, "failed to open %s", optarg);
439 if (mode == MODE_NONE) {
440 tool_err(-1, "you must specify a mode.");
445 img = lbx_img_fopen(file);
447 img = lbx_img_open(&stdin_handle, &lbx_pipe_fops, NULL);
450 tool_err(-1, "failed to open image: %s.", lbx_errmsg());
454 if (verbose || mode == MODE_IDENT) {
455 struct lbx_imginfo info;
460 lbx_img_getinfo(img, &info);
461 printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
462 file, img->width, img->height, img->frames,
463 info.palettesz ? ", embedded palette" : "",
464 img->chunk ? ", chunked" : "",
465 info.looping ? ", loops" : "");
470 rc = decode(img, palf, overf, &argv[optind]);