r956: README.BUILD - add more library recommendations
[cinelerra_cv/ct.git] / guicast / pngtoh.c
blobfa1b1d634be42192f6291be10b241fbff41f6923
1 /*
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.
6 *
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
15 * USA
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <sys/stat.h>
31 // Convert input files to .h files consisting of hex arrays
33 int main(int argc, char *argv[])
35 if(argc < 2) return 1;
37 for(argc--; argc > 0; argc--)
39 FILE *in;
40 FILE *out;
41 char variable[1024], header_fn[1024], output_fn[1024], *suffix, *prefix;
42 int i;
43 int bytes_per_row = 16;
44 char row[1024], byte[1024], character;
45 struct stat st;
46 long total_bytes;
48 in = fopen(argv[argc], "rb");
49 if(!in) continue;
51 stat(argv[argc], &st);
52 total_bytes = (long)st.st_size;
54 // Replace . with _png and append .h to filename
55 strcpy(output_fn, argv[argc]);
56 suffix = strrchr(output_fn, '.');
57 if(suffix) *suffix = '_';
58 strcat(output_fn, ".h");
60 out = fopen(output_fn, "w");
61 if(!out)
63 fclose(in);
64 continue;
68 // Strip leading directories for variable and header
69 prefix = strrchr(output_fn, '/');
70 if(!prefix)
71 prefix = output_fn;
72 else
73 prefix++;
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]))
81 int k;
82 for(k = strlen(header_fn); k >= 0; k--)
84 header_fn[k + 1] = header_fn[k];
86 header_fn[0] = '_';
89 // Replace . with _ for header
90 if(header_fn[i] == '.')
91 header_fn[i] = '_';
92 else
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]))
104 int k;
105 for(k = strlen(variable); k >= 0; k--)
107 variable[k + 1] = variable[k];
109 variable[0] = '_';
112 // Print the header
113 fprintf(out, "#ifndef %s\n"
114 "#define %s\n"
115 "\n"
116 "static unsigned char %s[] = \n{\n",
117 header_fn,
118 header_fn,
119 variable);
121 // Print the size of the file
122 fprintf(out, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x, \n",
123 (unsigned long)(total_bytes & 0xff000000) >> 24,
124 (unsigned long)(total_bytes & 0xff0000) >> 16,
125 (unsigned long)(total_bytes & 0xff00) >> 8,
126 (unsigned long)(total_bytes & 0xff));
128 while(total_bytes > 0)
130 sprintf(row, "\t");
131 for(i = 0; i < bytes_per_row && total_bytes > 0; i++)
133 if(i == 0)
134 sprintf(byte, "0x%02x", fgetc(in));
135 else
136 sprintf(byte, ", 0x%02x", fgetc(in));
137 strcat(row, byte);
138 total_bytes--;
140 if(total_bytes > 0)
141 sprintf(byte, ", \n");
142 else
143 sprintf(byte, "\n");
145 fprintf(out, "%s%s", row, byte);
148 fprintf(out, "};\n\n#endif\n");
149 fclose(out);