2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published
4 * by the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 // Convert input files to .h files consisting of hex arrays
26 int main(int argc, char *argv[])
30 fprintf(stderr, "Usage: %s <file.png>\n", argv[0]);
31 fprintf(stderr, " Convert 'file.png' file to C table 'file_png.h'.\n");
35 for(argc--; argc > 0; argc--)
39 char variable[1024], header_fn[1024], output_fn[1024], *suffix, *prefix;
41 int bytes_per_row = 16;
42 char row[1024], byte[1024];
46 in = fopen(argv[argc], "rb");
49 stat(argv[argc], &st);
50 total_bytes = (long)st.st_size;
52 // Replace . with _png and append .h to filename
53 strcpy(output_fn, argv[argc]);
54 suffix = strrchr(output_fn, '.');
55 if(suffix) *suffix = '_';
56 strcat(output_fn, ".h");
58 // Strip leading directories for variable and header
59 prefix = strrchr(output_fn, '/');
65 out = fopen(prefix, "w");
69 fprintf(stderr, "error: unable to write to %s: %s\n",
70 prefix, strerror(errno));
75 strcpy(header_fn, prefix);
76 for(i = 0; i < strlen(header_fn); i++)
78 // Replace leading digits
79 if(i == 0 && isdigit(header_fn[i]))
82 for(k = strlen(header_fn); k >= 0; k--)
84 header_fn[k + 1] = header_fn[k];
89 // Replace . with _ for header
90 if(header_fn[i] == '.')
93 header_fn[i] = toupper(header_fn[i]);
96 // Strip .h for variable
97 strcpy(variable, prefix);
98 suffix = strrchr(variable, '.');
99 if(suffix) *suffix = 0;
101 // Replace leading digits
102 if(isdigit(variable[0]))
105 for(k = strlen(variable); k >= 0; k--)
107 variable[k + 1] = variable[k];
113 fprintf(out, "#ifndef %s\n"
116 "static unsigned char %s[] =\n{\n",
121 // Print the size of the file
122 fprintf(out, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x,\n",
123 (int)(total_bytes >> 24) & 0xff,
124 (int)(total_bytes >> 16) & 0xff,
125 (int)(total_bytes >> 8) & 0xff,
126 (int)(total_bytes & 0xff));
128 while(total_bytes > 0)
131 for(i = 0; i < bytes_per_row && total_bytes > 0; i++)
134 sprintf(byte, "0x%02x", fgetc(in));
136 sprintf(byte, ", 0x%02x", fgetc(in));
141 sprintf(byte, ",\n");
145 fprintf(out, "%s%s", row, byte);
148 fprintf(out, "};\n\n#endif\n");