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/>.
33 #include "imgoutput.h"
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 int outpng(unsigned int frameno,
108 unsigned char **framedata, unsigned char **mask,
109 unsigned int width, unsigned int height,
110 struct lbx_colour palette[static 256])
112 char name[strlen(outname) + sizeof ".65535.png"];
116 assert(frameno < 65536);
117 snprintf(name, sizeof name, "%s.%03d.png", outname, frameno);
119 of = fopen(name, "wb");
121 tool_err(0, "failed to open %s", name);
125 rc = img_output_png(of, name, width, height, framedata, mask, palette);
131 if (fclose(of) == EOF) {
132 tool_err(0, "error writing %s", name);
137 printf("wrote %s\n", name);
142 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
144 struct lbx_image *overimg = lbx_img_open(f, &lbx_default_fops, NULL);
145 struct lbx_imginfo info;
148 tool_err(-1, "failed to open override image: %s", lbx_errmsg());
151 lbx_img_getinfo(overimg, &info);
153 if (!info.palettesz) {
154 tool_err(-1, "override image has no palette.");
155 lbx_img_close(overimg);
159 if (lbx_img_getpalette(overimg, palette) == -1) {
160 tool_err(-1, "error reading override palette: %s", lbx_errmsg());
161 lbx_img_close(overimg);
165 lbx_img_close(overimg);
169 static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info,
170 FILE *palf, FILE *override,
171 struct lbx_colour palette[static 256])
176 if (!palf && !info->palettesz && !override) {
177 tool_err(-1, "no palette available.");
181 /* Default the palette to a wonderful pink. */
182 for (i = 0; i < 256; i++) {
183 palette[i] = (struct lbx_colour){0x3f, 0x00, 0x3f};
186 /* Read the external palette, if any. */
187 if (palf && lbx_img_loadpalette(palf, &lbx_default_fops, palette) != 0) {
188 tool_err(-1, "error reading external palette: %s", lbx_errmsg());
192 /* Read the embedded palette, if any. */
193 if (info->palettesz && lbx_img_getpalette(img, palette) == -1) {
194 tool_err(-1, "error reading embedded palette: %s", lbx_errmsg());
198 /* Read the override palette, if any. */
199 if (override && loadoverride(override, palette) == -1) {
206 int decode(struct lbx_image *img, FILE *palf, FILE *override, char **argv)
208 unsigned char *framebits;
209 struct lbx_colour palette[256];
210 struct lbx_imginfo info;
214 lbx_img_getinfo(img, &info);
216 framebits = calloc(1, img->frames / CHAR_BIT + 1);
221 /* Figure out what images we're extracting. */
223 /* extract all images by default. */
224 memset(framebits, -1, img->frames / CHAR_BIT + 1);
226 for (i = 0; argv[i]; i++) {
227 parserange(img->frames, argv[i], framebits);
232 if (loadpalette(img, &info, palf, override, palette) == -1) {
237 /* Extract the images, in order. */
238 for (i = 0; i < img->frames; i++) {
239 unsigned char **data;
240 unsigned char **mask;
242 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
245 data = lbx_img_getframe(img, i);
247 tool_err(-1, "error in frame %u: %s", i, lbx_errmsg());
251 mask = lbx_img_getmask(img);
253 if (!outpng(i, data, mask, img->width, img->height,
254 usepalette ? palette : NULL)) {
260 tool_err(-1, "no frames extracted.");
271 int main(int argc, char **argv)
273 int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
274 struct lbx_pipe_state stdin_handle = { .f = stdin };
275 FILE *palf = NULL, *overf = NULL;
276 const char *file = NULL;
277 struct lbx_image *img;
279 static const char *sopts = "idnvf:p:O:V";
280 static const struct option lopts[] = {
281 { "ident", 0, NULL, 'i' },
282 { "decode", 0, NULL, 'd' },
283 { "verbose", 0, NULL, 'v' },
284 { "file", 1, NULL, 'f' },
285 { "palette", 1, NULL, 'p' },
286 { "override", 1, NULL, 'p' },
288 { "version", 0, NULL, 'V' },
289 { "usage", 0, NULL, 'U' },
290 { "help", 0, NULL, 'H' },
292 { "no-palette", 0, NULL, 'n' },
297 tool_init("lbximg", argc, argv);
298 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
316 palf = fopen(optarg, "rb");
318 tool_err(0, "failed to open %s", optarg);
324 overf = fopen(optarg, "rb");
326 tool_err(0, "failed to open %s", optarg);
345 if (mode == MODE_NONE) {
346 tool_err(-1, "you must specify a mode.");
351 img = lbx_img_fopen(file);
353 img = lbx_img_open(&stdin_handle, &lbx_pipe_fops, NULL);
356 tool_err(-1, "failed to open image: %s.", lbx_errmsg());
360 if (verbose || mode == MODE_IDENT) {
361 struct lbx_imginfo info;
366 lbx_img_getinfo(img, &info);
367 printf("%s is %hux%hu LBX image, %hhu frame(s)%s%s%s\n",
368 file, img->width, img->height, img->frames,
369 info.palettesz ? ", embedded palette" : "",
370 img->chunk ? ", chunked" : "",
371 info.looping ? ", loops" : "");
376 rc = decode(img, palf, overf, &argv[optind]);