img2gba
diff src/imgconv.c @ 1:eb9334da7c80
vastly improved img2gba
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 26 Jun 2014 06:34:00 +0300 |
parents | c359fcbd2422 |
children | 5f314b692d9e |
line diff
1.1 --- a/src/imgconv.c Wed Jun 25 18:19:52 2014 +0300 1.2 +++ b/src/imgconv.c Thu Jun 26 06:34:00 2014 +0300 1.3 @@ -1,40 +1,102 @@ 1.4 #include <stdio.h> 1.5 #include <stdlib.h> 1.6 #include <string.h> 1.7 +#include <errno.h> 1.8 #include <imago2.h> 1.9 1.10 -int save_image15(unsigned int *img, int x, int y, const char *fname, const char *array_name); 1.11 +struct imgfile { 1.12 + char *fname; 1.13 + struct imgfile *next; 1.14 +}; 1.15 + 1.16 +struct varnames { 1.17 + char *arrayname; 1.18 + char *widthname, *heightname; 1.19 + char *bppname; 1.20 +}; 1.21 + 1.22 +int save_image15(unsigned int *img, int x, int y, const char *fname, struct varnames *vars); 1.23 +int parse_args(int argc, char **argv); 1.24 + 1.25 +struct imgfile *flist, *ftail; 1.26 +const char *outname_fmt = "%s.img.c"; 1.27 +const char *arrayname_fmt = "%s_pixels"; 1.28 +const char *widthname_fmt = "%s_width"; 1.29 +const char *heightname_fmt = "%s_height"; 1.30 +const char *bppname_fmt = "%s_bpp"; 1.31 +const char *hdrname = "data.h"; 1.32 + 1.33 +FILE *hdrfile; 1.34 1.35 int main(int argc, char **argv) 1.36 { 1.37 - int i; 1.38 + struct varnames vars; 1.39 + char *outname; 1.40 1.41 - if(argc < 2) { 1.42 - fprintf(stderr, "not enough arguments\n"); 1.43 - return -1; 1.44 + if(parse_args(argc, argv) == -1) { 1.45 + return 1; 1.46 } 1.47 1.48 - for(i=1; i<argc; i++) { 1.49 - char fname[512], *cptr; 1.50 - int x, y; 1.51 - unsigned int *img = img_load_pixels(argv[i], &x, &y, IMG_FMT_RGBA32); 1.52 - if(!img) continue; 1.53 + if(!flist) { 1.54 + fprintf(stderr, "you didn't specify any files to convert\n"); 1.55 + return 1; 1.56 + } 1.57 1.58 - strcpy(fname, argv[i]); 1.59 - cptr = strrchr(fname, '.'); 1.60 - if(cptr) { 1.61 - strcpy(cptr, ".c"); 1.62 - } else { 1.63 - cptr = fname + strlen(fname); 1.64 - strcpy(cptr, ".c"); 1.65 + if(!(hdrfile = fopen(hdrname, "w"))) { 1.66 + fprintf(stderr, "failed to write header file: %s: %s\n", hdrname, strerror(errno)); 1.67 + return 1; 1.68 + } 1.69 + fprintf(hdrfile, "#ifndef IMG2GBA_DATAFILE_DECLARATIONS_H_\n"); 1.70 + fprintf(hdrfile, "#define IMG2GBA_DATAFILE_DECLARATIONS_H_\n\n"); 1.71 + 1.72 + while(flist) { 1.73 + char *suffix; 1.74 + int x, y, namelen; 1.75 + unsigned int *img; 1.76 + 1.77 + struct imgfile *imgfile = flist; 1.78 + flist = flist->next; 1.79 + 1.80 + printf("converting: %s ...\n", imgfile->fname); 1.81 + 1.82 + if(!(img = img_load_pixels(imgfile->fname, &x, &y, IMG_FMT_RGBA32))) { 1.83 + continue; 1.84 } 1.85 1.86 - if(save_image15(img, x, y, fname, "img") == -1) { 1.87 - fprintf(stderr, "could not save as %s\n", fname); 1.88 + if((suffix = strrchr(imgfile->fname, '.'))) { 1.89 + *suffix = 0; 1.90 } 1.91 + namelen = strlen(imgfile->fname); 1.92 + 1.93 + outname = malloc(namelen + strlen(outname_fmt) + 1); 1.94 + vars.arrayname = malloc(namelen + strlen(arrayname_fmt) + 1); 1.95 + vars.widthname = malloc(namelen + strlen(widthname_fmt) + 1); 1.96 + vars.heightname = malloc(namelen + strlen(heightname_fmt) + 1); 1.97 + vars.bppname = malloc(namelen + strlen(bppname_fmt) + 1); 1.98 + 1.99 + sprintf(outname, outname_fmt, imgfile->fname); 1.100 + sprintf(vars.arrayname, arrayname_fmt, imgfile->fname); 1.101 + sprintf(vars.widthname, widthname_fmt, imgfile->fname); 1.102 + sprintf(vars.heightname, heightname_fmt, imgfile->fname); 1.103 + sprintf(vars.bppname, bppname_fmt, imgfile->fname); 1.104 + 1.105 + if(save_image15(img, x, y, outname, &vars) == -1) { 1.106 + fprintf(stderr, "could not save as %s\n", outname); 1.107 + } 1.108 + 1.109 + free(outname); 1.110 + free(vars.arrayname); 1.111 + free(vars.widthname); 1.112 + free(vars.heightname); 1.113 + free(vars.bppname); 1.114 + 1.115 + free(imgfile); 1.116 1.117 img_free_pixels(img); 1.118 } 1.119 + 1.120 + fprintf(hdrfile, "\n#endif\n"); 1.121 + fclose(hdrfile); 1.122 return 0; 1.123 } 1.124 1.125 @@ -44,16 +106,26 @@ 1.126 1.127 #define PACK_COLOR15(r, g, b) ((((r) & 0x1f) << 10) | (((g) & 0x1f) << 5) | ((b) & 0x1f)) 1.128 1.129 -int save_image15(unsigned int *img, int x, int y, const char *fname, const char *array_name) 1.130 +int save_image15(unsigned int *img, int x, int y, const char *fname, struct varnames *vars) 1.131 { 1.132 int i, j; 1.133 FILE *fp; 1.134 1.135 + fprintf(hdrfile, "extern const short %s;\n", vars->widthname); 1.136 + fprintf(hdrfile, "extern const short %s;\n", vars->heightname); 1.137 + fprintf(hdrfile, "extern const short %s;\n", vars->bppname); 1.138 + fprintf(hdrfile, "extern const unsigned short %s[%d * %d];\n", vars->arrayname, x, y); 1.139 + 1.140 if(!(fp = fopen(fname, "w"))) { 1.141 + fprintf(stderr, "failed to write file: %s: %s\n", fname, strerror(errno)); 1.142 return -1; 1.143 } 1.144 1.145 - fprintf(fp, "\nconst unsigned short %s[] = {\n", array_name); 1.146 + fprintf(fp, "\nconst short %s = %d\n", vars->widthname, x); 1.147 + fprintf(fp, "\nconst short %s = %d\n", vars->heightname, y); 1.148 + fprintf(fp, "\nconst short %s = 16\n", vars->bppname); 1.149 + 1.150 + fprintf(fp, "\nconst unsigned short %s[] = {\n", vars->arrayname); 1.151 1.152 for(j=0; j<y; j++) { 1.153 fprintf(fp, "\t"); 1.154 @@ -72,7 +144,81 @@ 1.155 } 1.156 1.157 fprintf(fp, "};\n"); 1.158 - 1.159 fclose(fp); 1.160 return 0; 1.161 } 1.162 + 1.163 +int parse_args(int argc, char **argv) 1.164 +{ 1.165 + int i; 1.166 + 1.167 + for(i=1; i<argc; i++) { 1.168 + if(argv[i][0] == '-') { 1.169 + if(argv[i][2] != 0) { 1.170 + fprintf(stderr, "invalid option: %s\n", argv[i]); 1.171 + goto usage; 1.172 + } 1.173 + switch(argv[i][1]) { 1.174 + case 'o': 1.175 + outname_fmt = argv[++i]; 1.176 + break; 1.177 + 1.178 + case 'n': 1.179 + arrayname_fmt = argv[++i]; 1.180 + break; 1.181 + 1.182 + case 'x': 1.183 + widthname_fmt = argv[++i]; 1.184 + break; 1.185 + 1.186 + case 'y': 1.187 + heightname_fmt = argv[++i]; 1.188 + break; 1.189 + 1.190 + case 'b': 1.191 + bppname_fmt = argv[++i]; 1.192 + break; 1.193 + 1.194 + case 'h': 1.195 + hdrname = argv[++i]; 1.196 + break; 1.197 + 1.198 + default: 1.199 + goto usage; 1.200 + } 1.201 + } else { 1.202 + struct imgfile *node; 1.203 + 1.204 + if(!(node = malloc(sizeof *node))) { 1.205 + perror("failed to allocate memory"); 1.206 + return -1; 1.207 + } 1.208 + if(!(node->fname = malloc(strlen(argv[i]) + 1))) { 1.209 + perror("failed to allocate filename"); 1.210 + free(node); 1.211 + return -1; 1.212 + } 1.213 + strcpy(node->fname, argv[i]); 1.214 + node->next = 0; 1.215 + 1.216 + if(flist) { 1.217 + ftail->next = node; 1.218 + ftail = node; 1.219 + } else { 1.220 + flist = ftail = node; 1.221 + } 1.222 + } 1.223 + } 1.224 + return 0; 1.225 + 1.226 +usage: 1.227 + printf("usage: %s [options] <filenames>\n", argv[0]); 1.228 + printf("options:\n"); 1.229 + printf(" -o\toutput filename (def: %%s.img.c)\n"); 1.230 + printf(" -n\tpixel array name (def: %%s_pixels)\n"); 1.231 + printf(" -x\twidth var name (def: %%s_width)\n"); 1.232 + printf(" -y\theight var name (def: %%s_height)\n"); 1.233 + printf(" -b\tbpp var name (def: %%s_bpp)\n"); 1.234 + printf(" -h\theader file to append declarations (def: data.h)\n"); 1.235 + return -1; 1.236 +}