img2gba

view src/imgconv.c @ 2:5f314b692d9e

fixed missing semi-colons in generated source
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Jun 2014 06:34:58 +0300
parents eb9334da7c80
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <imago2.h>
7 struct imgfile {
8 char *fname;
9 struct imgfile *next;
10 };
12 struct varnames {
13 char *arrayname;
14 char *widthname, *heightname;
15 char *bppname;
16 };
18 int save_image15(unsigned int *img, int x, int y, const char *fname, struct varnames *vars);
19 int parse_args(int argc, char **argv);
21 struct imgfile *flist, *ftail;
22 const char *outname_fmt = "%s.img.c";
23 const char *arrayname_fmt = "%s_pixels";
24 const char *widthname_fmt = "%s_width";
25 const char *heightname_fmt = "%s_height";
26 const char *bppname_fmt = "%s_bpp";
27 const char *hdrname = "data.h";
29 FILE *hdrfile;
31 int main(int argc, char **argv)
32 {
33 struct varnames vars;
34 char *outname;
36 if(parse_args(argc, argv) == -1) {
37 return 1;
38 }
40 if(!flist) {
41 fprintf(stderr, "you didn't specify any files to convert\n");
42 return 1;
43 }
45 if(!(hdrfile = fopen(hdrname, "w"))) {
46 fprintf(stderr, "failed to write header file: %s: %s\n", hdrname, strerror(errno));
47 return 1;
48 }
49 fprintf(hdrfile, "#ifndef IMG2GBA_DATAFILE_DECLARATIONS_H_\n");
50 fprintf(hdrfile, "#define IMG2GBA_DATAFILE_DECLARATIONS_H_\n\n");
52 while(flist) {
53 char *suffix;
54 int x, y, namelen;
55 unsigned int *img;
57 struct imgfile *imgfile = flist;
58 flist = flist->next;
60 printf("converting: %s ...\n", imgfile->fname);
62 if(!(img = img_load_pixels(imgfile->fname, &x, &y, IMG_FMT_RGBA32))) {
63 continue;
64 }
66 if((suffix = strrchr(imgfile->fname, '.'))) {
67 *suffix = 0;
68 }
69 namelen = strlen(imgfile->fname);
71 outname = malloc(namelen + strlen(outname_fmt) + 1);
72 vars.arrayname = malloc(namelen + strlen(arrayname_fmt) + 1);
73 vars.widthname = malloc(namelen + strlen(widthname_fmt) + 1);
74 vars.heightname = malloc(namelen + strlen(heightname_fmt) + 1);
75 vars.bppname = malloc(namelen + strlen(bppname_fmt) + 1);
77 sprintf(outname, outname_fmt, imgfile->fname);
78 sprintf(vars.arrayname, arrayname_fmt, imgfile->fname);
79 sprintf(vars.widthname, widthname_fmt, imgfile->fname);
80 sprintf(vars.heightname, heightname_fmt, imgfile->fname);
81 sprintf(vars.bppname, bppname_fmt, imgfile->fname);
83 if(save_image15(img, x, y, outname, &vars) == -1) {
84 fprintf(stderr, "could not save as %s\n", outname);
85 }
87 free(outname);
88 free(vars.arrayname);
89 free(vars.widthname);
90 free(vars.heightname);
91 free(vars.bppname);
93 free(imgfile);
95 img_free_pixels(img);
96 }
98 fprintf(hdrfile, "\n#endif\n");
99 fclose(hdrfile);
100 return 0;
101 }
103 #define GET_R(pixel) (((pixel) >> 16) & 0xff)
104 #define GET_G(pixel) (((pixel) >> 8) & 0xff)
105 #define GET_B(pixel) (((pixel) >> 0) & 0xff)
107 #define PACK_COLOR15(r, g, b) ((((r) & 0x1f) << 10) | (((g) & 0x1f) << 5) | ((b) & 0x1f))
109 int save_image15(unsigned int *img, int x, int y, const char *fname, struct varnames *vars)
110 {
111 int i, j;
112 FILE *fp;
114 fprintf(hdrfile, "extern const short %s;\n", vars->widthname);
115 fprintf(hdrfile, "extern const short %s;\n", vars->heightname);
116 fprintf(hdrfile, "extern const short %s;\n", vars->bppname);
117 fprintf(hdrfile, "extern const unsigned short %s[%d * %d];\n", vars->arrayname, x, y);
119 if(!(fp = fopen(fname, "w"))) {
120 fprintf(stderr, "failed to write file: %s: %s\n", fname, strerror(errno));
121 return -1;
122 }
124 fprintf(fp, "\nconst short %s = %d;\n", vars->widthname, x);
125 fprintf(fp, "\nconst short %s = %d;\n", vars->heightname, y);
126 fprintf(fp, "\nconst short %s = 16;\n", vars->bppname);
128 fprintf(fp, "\nconst unsigned short %s[] = {\n", vars->arrayname);
130 for(j=0; j<y; j++) {
131 fprintf(fp, "\t");
132 for(i=0; i<x; i++) {
133 unsigned short r = GET_R(*img) >> 3;
134 unsigned short g = GET_G(*img) >> 3;
135 unsigned short b = GET_B(*img) >> 3;
136 unsigned short pixel15 = PACK_COLOR15(r, g, b);
137 fprintf(fp, "%u", pixel15);
138 if(i < x-1 || j < y-1) {
139 fprintf(fp, ", ");
140 }
141 img++;
142 }
143 fprintf(fp, "\n");
144 }
146 fprintf(fp, "};\n");
147 fclose(fp);
148 return 0;
149 }
151 int parse_args(int argc, char **argv)
152 {
153 int i;
155 for(i=1; i<argc; i++) {
156 if(argv[i][0] == '-') {
157 if(argv[i][2] != 0) {
158 fprintf(stderr, "invalid option: %s\n", argv[i]);
159 goto usage;
160 }
161 switch(argv[i][1]) {
162 case 'o':
163 outname_fmt = argv[++i];
164 break;
166 case 'n':
167 arrayname_fmt = argv[++i];
168 break;
170 case 'x':
171 widthname_fmt = argv[++i];
172 break;
174 case 'y':
175 heightname_fmt = argv[++i];
176 break;
178 case 'b':
179 bppname_fmt = argv[++i];
180 break;
182 case 'h':
183 hdrname = argv[++i];
184 break;
186 default:
187 goto usage;
188 }
189 } else {
190 struct imgfile *node;
192 if(!(node = malloc(sizeof *node))) {
193 perror("failed to allocate memory");
194 return -1;
195 }
196 if(!(node->fname = malloc(strlen(argv[i]) + 1))) {
197 perror("failed to allocate filename");
198 free(node);
199 return -1;
200 }
201 strcpy(node->fname, argv[i]);
202 node->next = 0;
204 if(flist) {
205 ftail->next = node;
206 ftail = node;
207 } else {
208 flist = ftail = node;
209 }
210 }
211 }
212 return 0;
214 usage:
215 printf("usage: %s [options] <filenames>\n", argv[0]);
216 printf("options:\n");
217 printf(" -o\toutput filename (def: %%s.img.c)\n");
218 printf(" -n\tpixel array name (def: %%s_pixels)\n");
219 printf(" -x\twidth var name (def: %%s_width)\n");
220 printf(" -y\theight var name (def: %%s_height)\n");
221 printf(" -b\tbpp var name (def: %%s_bpp)\n");
222 printf(" -h\theader file to append declarations (def: data.h)\n");
223 return -1;
224 }