]> git.draconx.ca Git - liblbx.git/blob - src/lbximg.c
Initial implementation of the lbximg tool.
[liblbx.git] / src / lbximg.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <getopt.h>
6
7 #include "image.h"
8 #include "lbx.h"
9
10 static const char *progname;
11 #define errmsg(fmt, ...) (\
12         fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
13 )
14
15 enum {
16         MODE_NONE,
17         MODE_DECODE,
18         MODE_IDENT,
19 };
20
21 int main(int argc, char **argv)
22 {
23         int mode = MODE_NONE, verbose = 0;
24         FILE *inf = stdin, *palf = NULL;
25         const char *name = "stdin";
26         LBX_IMG *img;
27         int opt;
28
29         static const char *sopts = "idvf:p:";
30         static const struct option lopts[] = {
31                 { "info",    0, NULL, 'i' },
32                 { "decode",  0, NULL, 'd' },
33                 { "verbose", 0, NULL, 'v' },
34                 { "file",    1, NULL, 'f' },
35                 { "palette", 1, NULL, 'p' },
36
37                 { 0 }
38         };
39
40         progname = "lbximg"; /* argv[0]; */
41         while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
42                 switch(opt) {
43                 case 'i':
44                         mode = MODE_IDENT;
45                         break;
46                 case 'd':
47                         mode = MODE_DECODE;
48                         break;
49                 case 'v':
50                         verbose = 1;
51                         break;
52                 case 'f':
53                         if (strcmp(optarg, "-") == 0)
54                                 break;
55
56                         name = strrchr(optarg, '/');
57                         name = name ? name+1 : optarg;
58
59                         inf = fopen(optarg, "rb");
60                         if (!inf) {
61                                 errmsg("failed to open %s: %m\n", optarg);
62                                 return EXIT_FAILURE;
63                         }
64                         break;
65                 case 'p':
66                         palf = fopen(optarg, "rb");
67                         if (!palf) {
68                                 errmsg("failed to open %s: %m\n", optarg);
69                                 return EXIT_FAILURE;
70                         }
71
72                         break;
73                 default:
74                         return EXIT_FAILURE;
75                 }
76         }
77
78         if (mode == MODE_NONE) {
79                 errmsg("you must specify a mode.\n", 0);
80                 return EXIT_FAILURE;
81         }
82
83         img = lbximg_fopen(inf);
84         if (!img) {
85                 errmsg("failed to open image: %s.\n", lbx_strerror());
86                 return EXIT_FAILURE;
87         }
88
89         if (verbose || mode == MODE_IDENT) {
90                 struct lbx_imginfo info;
91                 lbximg_getinfo(img, &info);
92
93                 printf("%s is %ux%u LBX image, %u frames\n",
94                        name, info.width, info.height, info.nframes);
95         }
96
97         switch (mode) {
98         case MODE_DECODE:
99                 errmsg("decode function not yet implemented.\n", 0);
100                 break;
101         }
102
103         lbximg_close(img);
104         return EXIT_SUCCESS;
105 }