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 errmsg("frame %lu out of range.\n", start);
72 errmsg("invalid frame range: %s.\n", str);
81 end = strtoul(endptr+1, &endptr, 0);
83 errmsg("frame %lu out of range.\n", end);
91 errmsg("invalid frame range: %s.\n", str);
96 errmsg("invalid frame range: %s.\n", 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 errmsg("failed to allocate row buffer: %s\n", strerror(errno));
141 of = fopen(name, "wb");
143 errmsg("failed to open %s: %s.\n", name, strerror(errno));
148 png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
150 errmsg("failed to init libpng.\n", 0);
154 info = png_create_info_struct(png);
156 errmsg("failed to init libpng.\n", 0);
157 png_destroy_write_struct(&png, NULL);
161 if (setjmp(png_jmpbuf(png))) {
162 png_destroy_write_struct(&png, &info);
166 png_init_io(png, of);
168 if (!ismasked(mask, width, height)) {
170 * This case is easy; we can just feed the palette and pixel
171 * data to libpng and let it do its magic.
174 png_color png_palette[256];
175 for (unsigned i = 0; i < 256; i++) {
176 png_palette[i].red = palette[i].red;
177 png_palette[i].green = palette[i].green;
178 png_palette[i].blue = palette[i].blue;
181 png_set_IHDR(png, info, width, height, 8,
182 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
183 PNG_COMPRESSION_TYPE_DEFAULT,
184 PNG_FILTER_TYPE_DEFAULT);
186 png_set_PLTE(png, info, png_palette, 256);
187 png_set_rows(png, info, framedata);
188 png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
191 * Unfortunately, LBX doesn't translate nicely to PNG here.
192 * LBX has a 256 colour palette _plus_ transparency.
193 * We'll form an RGBA PNG to deal with this.
196 png_set_IHDR(png, info, width, height, 8,
197 PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
198 PNG_COMPRESSION_TYPE_DEFAULT,
199 PNG_FILTER_TYPE_DEFAULT);
201 png_write_info(png, info);
203 for (y = 0; y < height; y++) {
204 for (x = 0; x < width; x++) {
205 row[4*x+0] = palette[framedata[y][x]].red;
206 row[4*x+1] = palette[framedata[y][x]].green;
207 row[4*x+2] = palette[framedata[y][x]].blue;
208 row[4*x+3] = (mask[y][x]) ? -1 : 0;
211 png_write_row(png, row);
214 png_write_end(png, NULL);
217 png_destroy_write_struct(&png, &info);
222 printf("wrote %s\n", name);
231 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
233 struct lbx_image *overimg = lbx_img_open(f, &lbx_default_fops, NULL);
234 struct lbx_imginfo info;
237 errmsg("failed to open override image: %s\n", lbx_errmsg());
240 lbx_img_getinfo(overimg, &info);
242 if (!info.palettesz) {
243 errmsg("override image has no palette.\n", 0);
244 lbx_img_close(overimg);
248 if (lbx_img_getpalette(overimg, palette) == -1) {
249 errmsg("error reading override palette: %s\n", lbx_errmsg());
250 lbx_img_close(overimg);
254 lbx_img_close(overimg);
258 static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info,
259 FILE *palf, FILE *override,
260 struct lbx_colour palette[static 256])
264 /* In no-palette mode, use palette indices for colour. */
266 for (i = 0; i < 256; i++) {
267 palette[i] = (struct lbx_colour){i,i,i};
274 if (!palf && !info->palettesz && !override) {
275 errmsg("no palette available.\n", 0);
279 /* Default the palette to a wonderful pink. */
280 for (i = 0; i < 256; i++) {
281 palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
284 /* Read the external palette, if any. */
285 if (palf && lbx_img_loadpalette(palf, &lbx_default_fops, palette) != 0) {
286 errmsg("error reading external palette: %s\n", lbx_errmsg());
290 /* Read the embedded palette, if any. */
291 if (info->palettesz && lbx_img_getpalette(img, palette) == -1) {
292 errmsg("error reading embedded palette: %s\n", lbx_errmsg());
296 /* Read the override palette, if any. */
297 if (override && loadoverride(override, palette) == -1) {
304 int decode(struct lbx_image *img, FILE *palf, FILE *override, char **argv)
306 unsigned char *framebits;
307 struct lbx_colour palette[256];
308 struct lbx_imginfo info;
312 lbx_img_getinfo(img, &info);
314 framebits = calloc(1, img->frames / CHAR_BIT + 1);
319 /* Figure out what images we're extracting. */
321 /* extract all images by default. */
322 memset(framebits, -1, img->frames / CHAR_BIT + 1);
324 for (i = 0; argv[i]; i++) {
325 parserange(img->frames, argv[i], framebits);
329 if (loadpalette(img, &info, palf, override, palette) == -1) {
333 /* Extract the images, in order. */
334 for (i = 0; i < img->frames; i++) {
335 unsigned char **data;
336 unsigned char **mask;
338 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
341 data = lbx_img_getframe(img, i);
343 errmsg("error in frame %u: %s\n", i, lbx_errmsg());
347 mask = lbx_img_getmask(img);
349 if (!outpng(i, data, mask, img->width, img->height, palette)) {
355 errmsg("no frames extracted.\n", 0);
366 int main(int argc, char **argv)
368 int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
369 struct lbx_pipe_state stdin_handle = { .f = stdin };
370 FILE *palf = NULL, *overf = NULL;
371 const char *file = NULL;
372 struct lbx_image *img;
374 static const char *sopts = "idnvf:p:O:V";
375 static const struct option lopts[] = {
376 { "ident", 0, NULL, 'i' },
377 { "decode", 0, NULL, 'd' },
378 { "verbose", 0, NULL, 'v' },
379 { "file", 1, NULL, 'f' },
380 { "palette", 1, NULL, 'p' },
381 { "override", 1, NULL, 'p' },
383 { "version", 0, NULL, 'V' },
384 { "usage", 0, NULL, 'U' },
385 { "help", 0, NULL, 'H' },
387 { "no-palette", 0, NULL, 'n' },
392 tool_init("lbximg", argc, argv);
393 while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
411 palf = fopen(optarg, "rb");
413 errmsg("failed to open %s: %m\n", optarg);
419 overf = fopen(optarg, "rb");
421 errmsg("failed to open %s: %m\n", optarg);
440 if (mode == MODE_NONE) {
441 errmsg("you must specify a mode.\n", 0);
446 img = lbx_img_fopen(file);
448 img = lbx_img_open(&stdin_handle, &lbx_pipe_fops, NULL);
451 errmsg("failed to open image: %s.\n", lbx_errmsg());
455 if (verbose || mode == MODE_IDENT) {
456 struct lbx_imginfo info;
461 lbx_img_getinfo(img, &info);
462 printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
463 file, img->width, img->height, img->frames,
464 info.palettesz ? ", embedded palette" : "",
465 img->chunk ? ", chunked" : "",
466 info.looping ? ", loops" : "");
471 rc = decode(img, palf, overf, &argv[optind]);