add gang points to mask_gui, add missing projector xform to centroid marker, fix...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / pngtoh.c
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.
11  *
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
16  */
17
18 #include <errno.h>
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/stat.h>
23
24 // Convert input files to .h files consisting of hex arrays
25
26 int main(int argc, char *argv[])
27 {
28         if(argc < 2)
29         {
30                 fprintf(stderr, "Usage: %s <file.png>\n", argv[0]);
31                 fprintf(stderr, "  Convert 'file.png' file to C table 'file_png.h'.\n");
32                 return 1;
33         }
34
35         for(argc--; argc > 0; argc--)
36         {
37                 FILE *in;
38                 FILE *out;
39                 char variable[1024], header_fn[1024], output_fn[1024], *suffix, *prefix;
40                 int i;
41                 int bytes_per_row = 16;
42                 char row[1024], byte[1024];
43                 struct stat st;
44                 long total_bytes;
45
46                 in = fopen(argv[argc], "rb");
47                 if(!in) continue;
48
49                 stat(argv[argc], &st);
50                 total_bytes = (long)st.st_size;
51
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");
57
58 // Strip leading directories for variable and header
59                 prefix = strrchr(output_fn, '/');
60                 if(!prefix)
61                         prefix = output_fn;
62                 else
63                         prefix++;
64
65                 out = fopen(prefix, "w");
66                 if(!out)
67                 {
68                         fclose(in);
69                         fprintf(stderr, "error: unable to write to %s: %s\n",
70                                 prefix, strerror(errno));
71                         continue;
72                 }
73
74
75                 strcpy(header_fn, prefix);
76                 for(i = 0; i < strlen(header_fn); i++)
77                 {
78 // Replace leading digits
79                         if(i == 0 && isdigit(header_fn[i]))
80                         {
81                                 int k;
82                                 for(k = strlen(header_fn); k >= 0; k--)
83                                 {
84                                         header_fn[k + 1] = header_fn[k];
85                                 }
86                                 header_fn[0] = '_';
87                         }
88
89 // Replace . with _ for header
90                         if(header_fn[i] == '.')
91                                 header_fn[i] = '_';
92                         else
93                                 header_fn[i] = toupper(header_fn[i]);
94                 }
95
96 // Strip .h for variable
97                 strcpy(variable, prefix);
98                 suffix = strrchr(variable, '.');
99                 if(suffix) *suffix = 0;
100
101 // Replace leading digits
102                 if(isdigit(variable[0]))
103                 {
104                         int k;
105                         for(k = strlen(variable); k >= 0; k--)
106                         {
107                                 variable[k + 1] = variable[k];
108                         }
109                         variable[0] = '_';
110                 }
111
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);
120
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));
127
128                 while(total_bytes > 0)
129                 {
130                         sprintf(row, "\t");
131                         for(i = 0; i < bytes_per_row && total_bytes > 0; i++)
132                         {
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--;
139                         }
140                         if(total_bytes > 0)
141                                 sprintf(byte, ",\n");
142                         else
143                                 sprintf(byte, "\n");
144
145                         fprintf(out, "%s%s", row, byte);
146                 }
147
148                 fprintf(out, "};\n\n#endif\n");
149
150                 fclose(out);
151                 fclose(in);
152         }
153 }