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 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/>.
34 #include "imgoutput.h"
37 static int verbose = 0;
38 static char *outname = "out";
39 static int usepalette = 1;
41 static void printusage(void)
43 puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]"
45 puts(" [-F format] [frameno ...]");
48 static void printhelp(void)
51 puts("For now, see the man page for detailed help.");
60 static const struct img_format {
61 img_output_func *output;
65 { img_output_png, "png", 1 },
66 { img_output_pam, "pam", 1 },
67 { img_output_ppm, "ppm", 1 },
68 { img_output_pbm, "pbm", 1 },
71 static int lookup_format(const char *fmt)
73 for (size_t i = 0; i < sizeof formats / sizeof formats[0]; i++) {
74 assert(!formats[i].name[sizeof formats[i].name - 1]);
76 if (!fmt && formats[i].enabled)
79 if (strcmp(formats[i].name, fmt))
82 if (!formats[i].enabled) {
83 tool_err(-1, "%s support disabled at build time", fmt);
90 tool_err(-1, "unknown format %s", fmt);
94 bool img_is_masked(unsigned char **mask, unsigned width, unsigned height)
98 for (x = y = 0; y < height; ++x < width || (x = 0, y++)) {
106 int parserange(unsigned frames, char *str, unsigned char *bits)
108 unsigned long start, end;
112 start = strtoul(str, &endptr, 0);
113 if (start >= frames) {
114 tool_err(-1, "frame %lu out of range.", start);
119 tool_err(-1, "invalid frame range: %s.", str);
128 end = strtoul(endptr+1, &endptr, 0);
130 tool_err(-1, "frame %lu out of range.", end);
138 tool_err(-1, "invalid frame range: %s.", str);
143 tool_err(-1, "invalid frame range: %s.", str);
147 for (i = start; i <= end; i++) {
148 bits[i / CHAR_BIT] |= 1 << (i % CHAR_BIT);
154 int output(unsigned int frameno, const struct img_format *fmt,
155 unsigned char **framedata, unsigned char **mask,
156 unsigned int width, unsigned int height,
157 struct lbx_colour palette[static 256])
159 char name[strlen(outname) + sizeof ".65535.png"];
163 assert(fmt->output != NULL);
164 assert(frameno < 65536);
165 snprintf(name, sizeof name, "%s.%03d.%s", outname, frameno, fmt->name);
167 of = fopen(name, "wb");
169 tool_err(0, "failed to open %s", name);
173 rc = fmt->output(of, name, width, height, framedata, mask, palette);
179 if (fclose(of) == EOF) {
180 tool_err(0, "error writing %s", name);
185 printf("wrote %s\n", name);
190 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
192 struct lbx_image *overimg = lbx_img_open(f, &lbx_default_fops, NULL);
193 struct lbx_imginfo info;
196 tool_err(-1, "failed to open override image: %s", lbx_errmsg());
199 lbx_img_getinfo(overimg, &info);
201 if (!info.palettesz) {
202 tool_err(-1, "override image has no palette.");
203 lbx_img_close(overimg);
207 if (lbx_img_getpalette(overimg, palette) == -1) {
208 tool_err(-1, "error reading override palette: %s", lbx_errmsg());
209 lbx_img_close(overimg);
213 lbx_img_close(overimg);
217 static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info,
218 FILE *palf, FILE *override,
219 struct lbx_colour palette[static 256])
224 if (!palf && !info->palettesz && !override) {
225 tool_err(-1, "no palette available.");
229 /* Default the palette to a wonderful pink. */
230 for (i = 0; i < 256; i++) {
231 palette[i] = (struct lbx_colour){0x3f, 0x00, 0x3f};
234 /* Read the external palette, if any. */
235 if (palf && lbx_img_loadpalette(palf, &lbx_default_fops, palette) != 0) {
236 tool_err(-1, "error reading external palette: %s", lbx_errmsg());
240 /* Read the embedded palette, if any. */
241 if (info->palettesz && lbx_img_getpalette(img, palette) == -1) {
242 tool_err(-1, "error reading embedded palette: %s", lbx_errmsg());
246 /* Read the override palette, if any. */
247 if (override && loadoverride(override, palette) == -1) {
255 decode(struct lbx_image *img, FILE *palf, FILE *override, int fmt, char **argv)
257 unsigned char *framebits;
258 struct lbx_colour palette[256];
259 struct lbx_imginfo info;
263 assert(fmt >= 0 && fmt < sizeof formats / sizeof formats[0]);
265 lbx_img_getinfo(img, &info);
267 framebits = calloc(1, img->frames / CHAR_BIT + 1);
272 /* Figure out what images we're extracting. */
274 /* extract all images by default. */
275 memset(framebits, -1, img->frames / CHAR_BIT + 1);
277 for (i = 0; argv[i]; i++) {
278 parserange(img->frames, argv[i], framebits);
283 if (loadpalette(img, &info, palf, override, palette) == -1) {
288 /* Extract the images, in order. */
289 for (i = 0; i < img->frames; i++) {
290 unsigned char **data;
291 unsigned char **mask;
293 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
296 data = lbx_img_getframe(img, i);
298 tool_err(-1, "error in frame %u: %s", i, lbx_errmsg());
302 mask = lbx_img_getmask(img);
304 if (!output(i, &formats[fmt], data, mask,
305 img->width, img->height,
306 usepalette ? palette : NULL)) {
312 tool_err(-1, "no frames extracted.");
323 int main(int argc, char **argv)
325 int mode = MODE_NONE, fmt, opt, rc = EXIT_FAILURE;
326 struct lbx_pipe_state stdin_handle = { .f = stdin };
327 const char *file = NULL, *fmtstring = NULL;
328 FILE *palf = NULL, *overf = NULL;
329 struct lbx_image *img;
331 static const char sopts[] = "idnvF:f:p:O:VH";
332 static const struct option lopts[] = {
333 { "ident", 0, NULL, 'i' },
334 { "decode", 0, NULL, 'd' },
335 { "verbose", 0, NULL, 'v' },
336 { "file", 1, NULL, 'f' },
337 { "format", 1, NULL, 'F' },
338 { "palette", 1, NULL, 'p' },
339 { "override", 1, NULL, 'p' },
341 { "version", 0, NULL, 'V' },
342 { "usage", 0, NULL, 'U' },
343 { "help", 0, NULL, 'H' },
345 { "no-palette", 0, NULL, 'n' },
350 tool_init("lbximg", argc, argv);
351 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
372 palf = fopen(optarg, "rb");
374 tool_err(0, "failed to open %s", optarg);
380 overf = fopen(optarg, "rb");
382 tool_err(0, "failed to open %s", optarg);
400 if (mode == MODE_NONE) {
401 tool_err(-1, "you must specify a mode.");
405 fmt = lookup_format(fmtstring);
410 img = lbx_img_fopen(file);
412 img = lbx_img_open(&stdin_handle, &lbx_pipe_fops, NULL);
415 tool_err(-1, "failed to open image: %s.", lbx_errmsg());
419 if (verbose || mode == MODE_IDENT) {
420 struct lbx_imginfo info;
425 lbx_img_getinfo(img, &info);
426 printf("%s is %hux%hu LBX image, %hhu frame(s)%s%s%s\n",
427 file, img->width, img->height, img->frames,
428 info.palettesz ? ", embedded palette" : "",
429 img->chunk ? ", chunked" : "",
430 info.looping ? ", loops" : "");
435 rc = decode(img, palf, overf, fmt, &argv[optind]);