img2gba

view src/imgconv.c @ 0:c359fcbd2422

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 25 Jun 2014 18:19:52 +0300
parents
children eb9334da7c80
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <imago2.h>
6 int save_image15(unsigned int *img, int x, int y, const char *fname, const char *array_name);
8 int main(int argc, char **argv)
9 {
10 int i;
12 if(argc < 2) {
13 fprintf(stderr, "not enough arguments\n");
14 return -1;
15 }
17 for(i=1; i<argc; i++) {
18 char fname[512], *cptr;
19 int x, y;
20 unsigned int *img = img_load_pixels(argv[i], &x, &y, IMG_FMT_RGBA32);
21 if(!img) continue;
23 strcpy(fname, argv[i]);
24 cptr = strrchr(fname, '.');
25 if(cptr) {
26 strcpy(cptr, ".c");
27 } else {
28 cptr = fname + strlen(fname);
29 strcpy(cptr, ".c");
30 }
32 if(save_image15(img, x, y, fname, "img") == -1) {
33 fprintf(stderr, "could not save as %s\n", fname);
34 }
36 img_free_pixels(img);
37 }
38 return 0;
39 }
41 #define GET_R(pixel) (((pixel) >> 16) & 0xff)
42 #define GET_G(pixel) (((pixel) >> 8) & 0xff)
43 #define GET_B(pixel) (((pixel) >> 0) & 0xff)
45 #define PACK_COLOR15(r, g, b) ((((r) & 0x1f) << 10) | (((g) & 0x1f) << 5) | ((b) & 0x1f))
47 int save_image15(unsigned int *img, int x, int y, const char *fname, const char *array_name)
48 {
49 int i, j;
50 FILE *fp;
52 if(!(fp = fopen(fname, "w"))) {
53 return -1;
54 }
56 fprintf(fp, "\nconst unsigned short %s[] = {\n", array_name);
58 for(j=0; j<y; j++) {
59 fprintf(fp, "\t");
60 for(i=0; i<x; i++) {
61 unsigned short r = GET_R(*img) >> 3;
62 unsigned short g = GET_G(*img) >> 3;
63 unsigned short b = GET_B(*img) >> 3;
64 unsigned short pixel15 = PACK_COLOR15(r, g, b);
65 fprintf(fp, "%u", pixel15);
66 if(i < x-1 || j < y-1) {
67 fprintf(fp, ", ");
68 }
69 img++;
70 }
71 fprintf(fp, "\n");
72 }
74 fprintf(fp, "};\n");
76 fclose(fp);
77 return 0;
78 }