# HG changeset patch # User John Tsiombikas # Date 1403753640 -10800 # Node ID eb9334da7c80f675f709cec2bb3559cbc3e07e48 # Parent c359fcbd2422e4361a1d4063d26a7e50296fc9c7 vastly improved img2gba diff -r c359fcbd2422 -r eb9334da7c80 src/imgconv.c --- a/src/imgconv.c Wed Jun 25 18:19:52 2014 +0300 +++ b/src/imgconv.c Thu Jun 26 06:34:00 2014 +0300 @@ -1,40 +1,102 @@ #include #include #include +#include #include -int save_image15(unsigned int *img, int x, int y, const char *fname, const char *array_name); +struct imgfile { + char *fname; + struct imgfile *next; +}; + +struct varnames { + char *arrayname; + char *widthname, *heightname; + char *bppname; +}; + +int save_image15(unsigned int *img, int x, int y, const char *fname, struct varnames *vars); +int parse_args(int argc, char **argv); + +struct imgfile *flist, *ftail; +const char *outname_fmt = "%s.img.c"; +const char *arrayname_fmt = "%s_pixels"; +const char *widthname_fmt = "%s_width"; +const char *heightname_fmt = "%s_height"; +const char *bppname_fmt = "%s_bpp"; +const char *hdrname = "data.h"; + +FILE *hdrfile; int main(int argc, char **argv) { - int i; + struct varnames vars; + char *outname; - if(argc < 2) { - fprintf(stderr, "not enough arguments\n"); - return -1; + if(parse_args(argc, argv) == -1) { + return 1; } - for(i=1; inext; + + printf("converting: %s ...\n", imgfile->fname); + + if(!(img = img_load_pixels(imgfile->fname, &x, &y, IMG_FMT_RGBA32))) { + continue; } - if(save_image15(img, x, y, fname, "img") == -1) { - fprintf(stderr, "could not save as %s\n", fname); + if((suffix = strrchr(imgfile->fname, '.'))) { + *suffix = 0; } + namelen = strlen(imgfile->fname); + + outname = malloc(namelen + strlen(outname_fmt) + 1); + vars.arrayname = malloc(namelen + strlen(arrayname_fmt) + 1); + vars.widthname = malloc(namelen + strlen(widthname_fmt) + 1); + vars.heightname = malloc(namelen + strlen(heightname_fmt) + 1); + vars.bppname = malloc(namelen + strlen(bppname_fmt) + 1); + + sprintf(outname, outname_fmt, imgfile->fname); + sprintf(vars.arrayname, arrayname_fmt, imgfile->fname); + sprintf(vars.widthname, widthname_fmt, imgfile->fname); + sprintf(vars.heightname, heightname_fmt, imgfile->fname); + sprintf(vars.bppname, bppname_fmt, imgfile->fname); + + if(save_image15(img, x, y, outname, &vars) == -1) { + fprintf(stderr, "could not save as %s\n", outname); + } + + free(outname); + free(vars.arrayname); + free(vars.widthname); + free(vars.heightname); + free(vars.bppname); + + free(imgfile); img_free_pixels(img); } + + fprintf(hdrfile, "\n#endif\n"); + fclose(hdrfile); return 0; } @@ -44,16 +106,26 @@ #define PACK_COLOR15(r, g, b) ((((r) & 0x1f) << 10) | (((g) & 0x1f) << 5) | ((b) & 0x1f)) -int save_image15(unsigned int *img, int x, int y, const char *fname, const char *array_name) +int save_image15(unsigned int *img, int x, int y, const char *fname, struct varnames *vars) { int i, j; FILE *fp; + fprintf(hdrfile, "extern const short %s;\n", vars->widthname); + fprintf(hdrfile, "extern const short %s;\n", vars->heightname); + fprintf(hdrfile, "extern const short %s;\n", vars->bppname); + fprintf(hdrfile, "extern const unsigned short %s[%d * %d];\n", vars->arrayname, x, y); + if(!(fp = fopen(fname, "w"))) { + fprintf(stderr, "failed to write file: %s: %s\n", fname, strerror(errno)); return -1; } - fprintf(fp, "\nconst unsigned short %s[] = {\n", array_name); + fprintf(fp, "\nconst short %s = %d\n", vars->widthname, x); + fprintf(fp, "\nconst short %s = %d\n", vars->heightname, y); + fprintf(fp, "\nconst short %s = 16\n", vars->bppname); + + fprintf(fp, "\nconst unsigned short %s[] = {\n", vars->arrayname); for(j=0; jfname = malloc(strlen(argv[i]) + 1))) { + perror("failed to allocate filename"); + free(node); + return -1; + } + strcpy(node->fname, argv[i]); + node->next = 0; + + if(flist) { + ftail->next = node; + ftail = node; + } else { + flist = ftail = node; + } + } + } + return 0; + +usage: + printf("usage: %s [options] \n", argv[0]); + printf("options:\n"); + printf(" -o\toutput filename (def: %%s.img.c)\n"); + printf(" -n\tpixel array name (def: %%s_pixels)\n"); + printf(" -x\twidth var name (def: %%s_width)\n"); + printf(" -y\theight var name (def: %%s_height)\n"); + printf(" -b\tbpp var name (def: %%s_bpp)\n"); + printf(" -h\theader file to append declarations (def: data.h)\n"); + return -1; +}