]> git.draconx.ca Git - liblbx.git/blob - src/lbximg.c
c761c176f6ee308e421cbdf67dfe73bdaaee0b2d
[liblbx.git] / src / lbximg.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <assert.h>
25 #include <getopt.h>
26 #include <errno.h>
27
28 #include "tools.h"
29 #include "image.h"
30 #include "error.h"
31 #include "lbx.h"
32
33 #include "imgoutput.h"
34
35 /* Global flags */
36 static int verbose = 0;
37 static char *outname = "out";
38 static int usepalette = 1;
39
40 static void printusage(void)
41 {
42         puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]"
43                           " [-f path]");
44         puts("              [frameno ...]");
45 }
46
47 static void printhelp(void)
48 {
49         printusage();
50         puts("For now, see the man page for detailed help.");
51 }
52
53 enum {
54         MODE_NONE,
55         MODE_DECODE,
56         MODE_IDENT,
57 };
58
59 int parserange(unsigned frames, char *str, unsigned char *bits)
60 {
61         unsigned long start, end;
62         unsigned int i;
63         char *endptr;
64
65         start = strtoul(str, &endptr, 0);
66         if (start >= frames) {
67                 tool_err(-1, "frame %lu out of range.", start);
68                 return -1;
69         }
70
71         if (endptr == str) {
72                 tool_err(-1, "invalid frame range: %s.", str);
73                 return -1;
74         }
75
76         switch (*endptr) {
77         case '\0':
78                 end = start;
79                 break;
80         case '-':
81                 end = strtoul(endptr+1, &endptr, 0);
82                 if (end >= frames) {
83                         tool_err(-1, "frame %lu out of range.", end);
84                         return -1;
85                 }
86
87                 if (endptr == str)
88                         end = frames - 1;
89                 break;
90         default:
91                 tool_err(-1, "invalid frame range: %s.", str);
92                 return -1;
93         }
94
95         if (end < start) {
96                 tool_err(-1, "invalid frame range: %s.", str);
97                 return -1;
98         }
99
100         for (i = start; i <= end; i++) {
101                 bits[i / CHAR_BIT] |= 1 << (i % CHAR_BIT);
102         }
103
104         return 0;
105 }
106
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])
111 {
112         char name[strlen(outname) + sizeof ".65535.png"];
113         FILE *of;
114         int rc;
115
116         assert(frameno < 65536);
117         snprintf(name, sizeof name, "%s.%03d.png", outname, frameno);
118
119         of = fopen(name, "wb");
120         if (!of) {
121                 tool_err(0, "failed to open %s", name);
122                 return -1;
123         }
124
125         rc = img_output_png(of, name, width, height, framedata, mask, palette);
126         if (rc < 0) {
127                 fclose(of);
128                 return -1;
129         }
130
131         if (fclose(of) == EOF) {
132                 tool_err(0, "error writing %s", name);
133                 return -1;
134         }
135                 
136         if (verbose)
137                 printf("wrote %s\n", name);
138
139         return 0;
140 }
141
142 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
143 {
144         struct lbx_image *overimg = lbx_img_open(f, &lbx_default_fops, NULL);
145         struct lbx_imginfo info;
146
147         if (!overimg) {
148                 tool_err(-1, "failed to open override image: %s", lbx_errmsg());
149                 return -1;
150         }
151         lbx_img_getinfo(overimg, &info);
152
153         if (!info.palettesz) {
154                 tool_err(-1, "override image has no palette.");
155                 lbx_img_close(overimg);
156                 return -1;
157         }
158
159         if (lbx_img_getpalette(overimg, palette) == -1) {
160                 tool_err(-1, "error reading override palette: %s", lbx_errmsg());
161                 lbx_img_close(overimg);
162                 return -1;
163         }
164
165         lbx_img_close(overimg);
166         return 0;
167 }
168
169 static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info,
170                        FILE *palf, FILE *override,
171                        struct lbx_colour palette[static 256])
172 {
173         int i;
174
175         /* In no-palette mode, use palette indices for colour. */
176         if (!usepalette) {
177                 for (i = 0; i < 256; i++) {
178                         palette[i] = (struct lbx_colour){i,i,i};
179                 }
180
181                 return 0;
182         }
183
184         /* For sanity. */
185         if (!palf && !info->palettesz && !override) {
186                 tool_err(-1, "no palette available.");
187                 return -1;
188         }
189
190         /* Default the palette to a wonderful pink. */
191         for (i = 0; i < 256; i++) {
192                 palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
193         }
194
195         /* Read the external palette, if any. */
196         if (palf && lbx_img_loadpalette(palf, &lbx_default_fops, palette) != 0) {
197                 tool_err(-1, "error reading external palette: %s", lbx_errmsg());
198                 return -1;
199         }
200
201         /* Read the embedded palette, if any. */
202         if (info->palettesz && lbx_img_getpalette(img, palette) == -1) {
203                 tool_err(-1, "error reading embedded palette: %s", lbx_errmsg());
204                 return -1;
205         }
206
207         /* Read the override palette, if any. */
208         if (override && loadoverride(override, palette) == -1) {
209                 return -1;
210         }
211
212         return 0;
213 }
214
215 int decode(struct lbx_image *img, FILE *palf, FILE *override, char **argv)
216 {
217         unsigned char *framebits;
218         struct lbx_colour palette[256];
219         struct lbx_imginfo info;
220         int extracted = 0;
221         unsigned int i;
222
223         lbx_img_getinfo(img, &info);
224
225         framebits = calloc(1, img->frames / CHAR_BIT + 1);
226         if (!framebits) {
227                 return EXIT_FAILURE;
228         }
229
230         /* Figure out what images we're extracting. */
231         if (!argv[0]) {
232                 /* extract all images by default. */
233                 memset(framebits, -1, img->frames / CHAR_BIT + 1);
234         } else {
235                 for (i = 0; argv[i]; i++) {
236                         parserange(img->frames, argv[i], framebits);
237                 }
238         }
239
240         if (loadpalette(img, &info, palf, override, palette) == -1) {
241                 goto err;
242         }
243
244         /* Extract the images, in order. */
245         for (i = 0; i < img->frames; i++) {
246                 unsigned char **data;
247                 unsigned char **mask;
248
249                 if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
250                         continue;
251
252                 data = lbx_img_getframe(img, i);
253                 if (!data) {
254                         tool_err(-1, "error in frame %u: %s", i, lbx_errmsg());
255                         continue;
256                 }
257
258                 mask = lbx_img_getmask(img);
259
260                 if (!outpng(i, data, mask, img->width, img->height, palette)) {
261                         extracted = 1;
262                 }
263         }
264
265         if (!extracted) {
266                 tool_err(-1, "no frames extracted.");
267                 goto err;
268         }
269
270         free(framebits);
271         return EXIT_SUCCESS;
272 err:
273         free(framebits);
274         return EXIT_FAILURE;
275 }
276
277 int main(int argc, char **argv)
278 {
279         int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
280         struct lbx_pipe_state stdin_handle = { .f = stdin };
281         FILE *palf = NULL, *overf = NULL;
282         const char *file = NULL;
283         struct lbx_image *img;
284
285         static const char *sopts = "idnvf:p:O:V";
286         static const struct option lopts[] = {
287                 { "ident",      0, NULL, 'i' },
288                 { "decode",     0, NULL, 'd' },
289                 { "verbose",    0, NULL, 'v' },
290                 { "file",       1, NULL, 'f' },
291                 { "palette",    1, NULL, 'p' },
292                 { "override",   1, NULL, 'p' },
293
294                 { "version",    0, NULL, 'V' },
295                 { "usage",      0, NULL, 'U' },
296                 { "help",       0, NULL, 'H' },
297
298                 { "no-palette", 0, NULL, 'n' },
299
300                 { 0 }
301         };
302
303         tool_init("lbximg", argc, argv);
304         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
305                 switch(opt) {
306                 case 'i':
307                         mode = MODE_IDENT;
308                         break;
309                 case 'd':
310                         mode = MODE_DECODE;
311                         break;
312                 case 'v':
313                         verbose = 1;
314                         break;
315                 case 'f':
316                         file = optarg;
317                         break;
318                 case 'n':
319                         usepalette = 0;
320                         break;
321                 case 'p':
322                         palf = fopen(optarg, "rb");
323                         if (!palf) {
324                                 tool_err(0, "failed to open %s", optarg);
325                                 return EXIT_FAILURE;
326                         }
327
328                         break;
329                 case 'O':
330                         overf = fopen(optarg, "rb");
331                         if (!overf) {
332                                 tool_err(0, "failed to open %s", optarg);
333                                 return EXIT_FAILURE;
334                         }
335                         break;
336                 case 'V':
337                         tool_version();
338                         return EXIT_SUCCESS;
339                 case 'U':
340                         printusage();
341                         return EXIT_SUCCESS;
342                 case 'H':
343                         printhelp();
344                         return EXIT_SUCCESS;
345                 case '?':
346                 case ':':
347                         return EXIT_FAILURE;
348                 }
349         }
350
351         if (mode == MODE_NONE) {
352                 tool_err(-1, "you must specify a mode.");
353                 return EXIT_FAILURE;
354         }
355
356         if (file)
357                 img = lbx_img_fopen(file);
358         else
359                 img = lbx_img_open(&stdin_handle, &lbx_pipe_fops, NULL);
360
361         if (!img) {
362                 tool_err(-1, "failed to open image: %s.", lbx_errmsg());
363                 return EXIT_FAILURE;
364         }
365
366         if (verbose || mode == MODE_IDENT) {
367                 struct lbx_imginfo info;
368
369                 if (!file)
370                         file = "stdin";
371
372                 lbx_img_getinfo(img, &info);
373                 printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
374                        file, img->width, img->height, img->frames,
375                        info.palettesz ? ", embedded palette" : "",
376                        img->chunk     ? ", chunked" : "",
377                        info.looping   ? ", loops" : "");
378         }
379
380         switch (mode) {
381         case MODE_DECODE:
382                 rc = decode(img, palf, overf, &argv[optind]);
383                 break;
384         }
385
386         lbx_img_close(img);
387         return rc;
388 }