2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Simple command-line tool to convert an LBX image to other formats.
4 * Copyright © 2006-2011, 2013-2014 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 #include "imgoutput.h"
37 #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 static int verbose = 0;
41 static char *outname = "out";
42 static int usepalette = 1;
45 OPT_PREFIX = UCHAR_MAX+1,
48 static void printusage(void)
50 puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]"
52 puts(" [-F format] [frameno ...]");
55 static void printhelp(void)
58 puts("For now, see the man page for detailed help.");
67 static const struct img_format {
68 img_output_func *output;
73 { img_output_png, "png", 1 },
75 { img_output_pam, "pam", 1 },
76 { img_output_ppm, "ppm", 1 },
77 { img_output_pbm, "pbm", 1 },
80 static int lookup_format(const char *fmt)
82 for (size_t i = 0; i < sizeof formats / sizeof formats[0]; i++) {
83 assert(!formats[i].name[sizeof formats[i].name - 1]);
85 if (!fmt && formats[i].enabled)
88 if (strcmp(formats[i].name, fmt))
91 if (!formats[i].enabled) {
92 tool_err(-1, "%s support disabled at build time", fmt);
99 tool_err(-1, "unknown format %s", fmt);
103 bool img_is_masked(unsigned char *mask, unsigned width, unsigned height)
105 unsigned long npixels = (unsigned long) width * height;
106 unsigned long mask_sz = npixels / CHAR_BIT + (npixels % CHAR_BIT != 0);
108 for (unsigned long i = 0; i < mask_sz; i++) {
110 if (mask[i] != (unsigned char)-1)
113 unsigned char test = (1u << npixels % CHAR_BIT) - 1;
115 if ((mask[i] & test) != test)
123 int parserange(unsigned frames, char *str, unsigned char *bits)
125 unsigned long start, end;
129 start = strtoul(str, &endptr, 0);
130 if (start >= frames) {
131 tool_err(-1, "frame %lu out of range.", start);
136 tool_err(-1, "invalid frame range: %s.", str);
145 end = strtoul(endptr+1, &endptr, 0);
147 tool_err(-1, "frame %lu out of range.", end);
155 tool_err(-1, "invalid frame range: %s.", str);
160 tool_err(-1, "invalid frame range: %s.", str);
164 for (i = start; i <= end; i++) {
165 bits[i / CHAR_BIT] |= 1 << (i % CHAR_BIT);
171 int output(unsigned int frameno, const struct img_format *fmt,
172 unsigned char *pixels, unsigned char *pixel_mask,
173 unsigned int width, unsigned int height,
174 struct lbx_colour palette[static 256])
176 char name[strlen(outname) + sizeof ".65535.png"];
180 assert(fmt->output != NULL);
181 assert(frameno < 65536);
182 snprintf(name, sizeof name, "%s.%03d.%s", outname, frameno, fmt->name);
184 of = fopen(name, "wb");
186 tool_err(0, "failed to open %s", name);
190 rc = fmt->output(of, name, width, height, pixels, pixel_mask, palette);
196 if (fclose(of) == EOF) {
197 tool_err(0, "error writing %s", name);
202 printf("wrote %s\n", name);
207 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
209 struct lbx_image *overimg = lbx_img_open(f, &lbx_default_fops, NULL);
210 struct lbx_imginfo info;
213 tool_err(-1, "failed to open override image: %s", lbx_errmsg());
216 lbx_img_getinfo(overimg, &info);
218 if (!info.palettesz) {
219 tool_err(-1, "override image has no palette.");
220 lbx_img_close(overimg);
224 if (lbx_img_getpalette(overimg, palette) == -1) {
225 tool_err(-1, "error reading override palette: %s", lbx_errmsg());
226 lbx_img_close(overimg);
230 lbx_img_close(overimg);
234 static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info,
235 FILE *palf, FILE *override,
236 struct lbx_colour palette[static 256])
241 if (!palf && !info->palettesz && !override) {
242 tool_err(-1, "no palette available.");
246 /* Default the palette to a wonderful pink. */
247 for (i = 0; i < 256; i++) {
248 palette[i] = (struct lbx_colour){0x3f, 0x00, 0x3f};
251 /* Read the external palette, if any. */
252 if (palf && lbx_img_loadpalette(palf, &lbx_default_fops, palette) != 0) {
253 tool_err(-1, "error reading external palette: %s", lbx_errmsg());
257 /* Read the embedded palette, if any. */
258 if (info->palettesz && lbx_img_getpalette(img, palette) == -1) {
259 tool_err(-1, "error reading embedded palette: %s", lbx_errmsg());
263 /* Read the override palette, if any. */
264 if (override && loadoverride(override, palette) == -1) {
271 /* Return true iff a divides b. */
272 static bool divides(unsigned a, unsigned b)
282 /* Set n bits starting from offset in the bitmap. */
284 set_bits(unsigned char *bitmap, unsigned long offset, unsigned long n)
286 if (offset % CHAR_BIT) {
287 bitmap[offset/CHAR_BIT] |= (n >= CHAR_BIT ? -1u : (1u << n) - 1)
290 n -= MIN(n, CHAR_BIT - offset%CHAR_BIT);
291 offset += CHAR_BIT - offset%CHAR_BIT;
295 memset(&bitmap[offset/CHAR_BIT], -1, n/CHAR_BIT);
297 offset += n - n%CHAR_BIT;
302 bitmap[offset/CHAR_BIT] |= (1u << n) - 1;
306 static int decode_frame(struct lbx_image *img, unsigned n,
307 unsigned char *pixels, unsigned char *pixel_mask)
312 rc = lbx_img_seek(img, n);
314 tool_err(-1, "frame %u: invalid frame: %s\n", n, lbx_errmsg());
318 while ((rc = lbx_img_read_row_header(img, &x, &y)) != 0) {
319 unsigned long offset;
322 tool_err(-1, "frame %u: invalid row: %s", n, lbx_errmsg());
326 offset = (unsigned long) y * img->width + x;
327 rc = lbx_img_read_row_data(img, pixels+offset);
329 tool_err(-1, "frame %u: error reading row: %s\n", n, lbx_errmsg());
333 set_bits(pixel_mask, offset, rc);
340 decode(struct lbx_image *img, FILE *palf, FILE *override, int fmt, char **argv)
342 unsigned char *pixels = NULL, *pixel_mask = NULL, *framebits = NULL;
343 struct lbx_colour palette[256];
344 struct lbx_imginfo info;
345 int rc, ret = EXIT_FAILURE;
348 size_t npixels, mask_sz;
350 assert(fmt >= 0 && fmt < sizeof formats / sizeof formats[0]);
352 lbx_img_getinfo(img, &info);
354 npixels = img->width;
355 if (img->height && npixels >= SIZE_MAX / img->height) {
356 tool_err(-1, "image too large");
359 npixels *= img->height;
361 /* Ensure there is at least 1 byte to allocate */
365 framebits = calloc(1, img->frames / CHAR_BIT + 1);
367 tool_err(0, "failed to allocate memory");
371 pixels = calloc(img->width, img->height);
373 tool_err(0, "failed to allocate memory");
377 mask_sz = npixels / CHAR_BIT + (npixels % CHAR_BIT != 0);
378 pixel_mask = malloc(mask_sz);
380 tool_err(0, "failed to allocate memory");
384 /* Figure out what images we're extracting. */
386 /* extract all images by default. */
387 memset(framebits, -1, img->frames / CHAR_BIT + 1);
389 for (i = 0; argv[i]; i++) {
390 parserange(img->frames, argv[i], framebits);
395 if (loadpalette(img, &info, palf, override, palette) == -1) {
401 /* Extract the images, in order. */
403 for (i = 0; i < img->frames; i++) {
404 if (divides(img->chunk, i))
405 memset(pixel_mask, 0, mask_sz);
407 rc = decode_frame(img, i, pixels, pixel_mask);
413 if (framebits[i / CHAR_BIT] & (1u << (i % CHAR_BIT))) {
414 rc = output(i, &formats[fmt], pixels, pixel_mask,
415 img->width, img->height,
416 usepalette ? palette : NULL);
425 tool_err(-1, "no frames extracted.");
435 int main(int argc, char **argv)
437 int mode = MODE_NONE, fmt, opt, rc = EXIT_FAILURE;
438 struct lbx_pipe_state stdin_handle = { .f = stdin };
439 const char *file = NULL, *fmtstring = NULL;
440 const char *ext_palette = NULL, *ovr_palette = NULL;
441 FILE *palf = NULL, *overf = NULL;
442 struct lbx_image *img;
444 static const char sopts[] = "idnvF:f:p:O:VH";
445 static const struct option lopts[] = {
446 { "identify", 0, NULL, 'i' },
447 { "decode", 0, NULL, 'd' },
448 { "verbose", 0, NULL, 'v' },
449 { "file", 1, NULL, 'f' },
450 { "format", 1, NULL, 'F' },
451 { "palette", 1, NULL, 'p' },
452 { "override", 1, NULL, 'O' },
453 { "no-palette", 0, NULL, 'n' },
455 { "output-prefix", 1, NULL, OPT_PREFIX },
457 { "version", 0, NULL, 'V' },
458 { "usage", 0, NULL, 'U' },
459 { "help", 0, NULL, 'H' },
464 tool_init("lbximg", argc, argv);
465 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
486 ext_palette = optarg;
490 ovr_palette = optarg;
509 if (mode == MODE_NONE) {
510 tool_err(-1, "you must specify a mode.");
514 fmt = lookup_format(fmtstring);
519 img = lbx_img_fopen(file);
521 img = lbx_img_open(&stdin_handle, &lbx_pipe_fops, NULL);
524 tool_err(-1, "failed to open image: %s.", lbx_errmsg());
528 if (ext_palette && !(palf = fopen(ext_palette, "rb"))) {
529 tool_err(0, "failed to open %s", optarg);
533 if (ovr_palette && !(overf = fopen(ovr_palette, "rb"))) {
534 tool_err(0, "failed to open %s", optarg);
538 if (verbose || mode == MODE_IDENT) {
539 struct lbx_imginfo info;
544 lbx_img_getinfo(img, &info);
545 printf("%s is %hux%hu LBX image, %hhu frame(s)%s%s%s\n",
546 file, img->width, img->height, img->frames,
547 info.palettesz ? ", embedded palette" : "",
548 img->chunk ? ", chunked" : "",
549 info.looping ? ", loops" : "");
557 rc = decode(img, palf, overf, fmt, &argv[optind]);