Debian Codegraph @

lib/make-imglib.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <dirent.h>
•8 #include <ctype.h>
9
10 #include <png.h>
11
12 static int verbose = 0;
13 static char *basename = "images";
14 static char *imagedir = ".";
15
16 /*****************************************************************************/
17
18 char *
19 concat (char *a, char *b)
20 {
21 char *buf = 0;
22 int i = strlen(a) + strlen(b) + 2;
23 buf = (char *)malloc(i);
24 sprintf(buf, "%s/%s", a, b);
25 return buf;
26 }
27
28 /*****************************************************************************/
29
30 typedef struct subimage_struct {
31 struct subimage_struct *next;
32 char *filename;
33 int w, h, max;
34 int index;
35 } subimage_struct;
36
37 typedef struct image_struct {
38 struct image_struct *next;
39 char *name;
40 int a, d;
41 subimage_struct *sub[3];
42 int index;
43 } image_struct;
44
45 image_struct *image_list = 0;
46 int current_index = 0;
47
48 void
49 scan_image_directory ()
50 {
51 DIR *dir;
52 struct dirent *de;
53 image_struct *img;
54 subimage_struct *sub;
55
56 dir = opendir(imagedir);
57 if (!dir) {
58 fprintf(stderr, "Unable to scan directory %s for images.\n", imagedir);
59 perror("The error was");
60 exit(1);
61 }
62 while (de = readdir(dir)) {
63 FILE *f;
64 char *dot;
65 png_structp png_ptr;
66 png_infop info_ptr;
67 png_uint_32 width, height;
68 int bit_depth, color_type, interlace_type, type;
69
70 dot = strrchr(de->d_name, '.');
71 if (!dot)
72 continue;
73 if (strcmp(dot, ".png"))
74 continue;
75
76 dot = strchr(de->d_name, '.');
77 *dot = 0;
78 for (img=image_list; img; img=img->next)
79 if (strcmp(img->name, de->d_name) == 0)
80 break;
81 if (!img)
82 continue;
83 *dot = '.';
84
85 f = fopen(concat(imagedir, de->d_name), "rb");
86
87 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
88 info_ptr = png_create_info_struct (png_ptr);
89
90 if (setjmp (png_jmpbuf (png_ptr))) {
91 fclose (f);
92 continue;
93 }
94
95 png_init_io (png_ptr, f);
96
97 png_read_info (png_ptr, info_ptr);
98
99 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth,
100 &color_type, &interlace_type, 0, 0);
101
102 if (bit_depth == 1)
103 type = 0;
104 else if (color_type & PNG_COLOR_MASK_COLOR)
105 type = 2;
106 else
107 type = 1;
108
109 sub = (subimage_struct *)malloc(sizeof(subimage_struct));
110 sub->next = img->sub[type];
111 img->sub[type] = sub;
112 sub->filename = strdup (concat (imagedir, de->d_name));
113 sub->w = width;
114 sub->h = height;
115 sub->index = current_index++;
116
117 fclose (f);
118 }
119
120 closedir(dir);
121 }
122
123 static int print_col = 0;
124 static int print_com = 1;
125 static void
126 print_int(FILE *f, int val)
127 {
128 char buf[20];
129 sprintf(buf, "%d", val);
130 if (print_com) {
131 fputc(',', f);
132 print_col ++;
133 } else {
134 fprintf(f, " ");
135 print_col = 2;
136 }
137 if (print_col + strlen(buf) > 70) {
138 fprintf(f, "\n ");
139 print_col = 2;
140 }
141 fputs(buf, f);
142 print_col += strlen(buf);
143 print_com = 1;
144 }
145
146 void
147 dump_images(FILE *o)
148 {
149 image_struct *img;
150 subimage_struct *sub;
151 int i;
152 static char *type_names[] = {"mono", "grey", "color"};
153
154 for (img=image_list; img; img=img->next)
155 for (i=0; i<3; i++)
156 for (sub=img->sub[i]; sub; sub=sub->next)
157 {
158 FILE *f = fopen(sub->filename, "rb");
159 int byte;
160 fprintf(o, "/* %s */\n", sub->filename);
161 fprintf(o, "static const unsigned char data_%d[] = {\n", sub->index);
162 print_com = 0;
163 while ((byte = fgetc (f)) != EOF)
164 print_int(o, byte);
165 fprintf(o, "};\n\n");
166 fclose (f);
167 }
168
169 for (img=image_list; img; img=img->next)
170 for (i=0; i<3; i++)
171 if (img->sub[i])
172 {
173 fprintf(o, "static image sub_%d_%s[] = {\n", img->index, type_names[i]);
174 for (sub=img->sub[i]; sub; sub=sub->next)
175 fprintf(o, " { %d, %d, data_%d },\n", sub->w, sub->h, sub->index);
176 fprintf(o, " { 0, 0, 0 }\n};\n\n");
177 }
178
179 fprintf(o, "image_list %s_imagelib[] = {\n", basename);
180 for (img=image_list; img; img=img->next)
181 {
182 char *c = "";
183 fprintf(o, " { \"%s\", %d, %d, { ", img->name, img->a, img->d);
184 for (i=0; i<3; i++)
185 {
186 if (img->sub[i])
187 fprintf(o, "%ssub_%d_%s", c, img->index, type_names[i]);
188 else
189 fprintf(o, "%s0", c);
190 c = ", ";
191 }
192 fprintf(o, " } },\n");
193 }
194 fprintf(o, " { 0, 0, 0, { 0, 0, 0 } }\n};\n\n");
195 }
196
197 /*****************************************************************************/
198
199 char *
200 tokenize(char *string)
201 {
202 static char *next;
203 char *rv;
204 if (string) {
205 next = string;
206 return NULL;
207 }
208 while (*next && !isgraph(*next)) next++;
209 if (!*next) return NULL;
210 rv = next;
211 while (*next && isgraph(*next)) next++;
212 if (*next)
213 *next++ = 0;
214 return rv;
215 }
216
217 void
218 give_help()
219 {
220 fprintf(stderr, "make-imgdir [options] infile outfile [pngs]\n");
221 fprintf(stderr, " -v verbose\n");
222 fprintf(stderr, " -h print this help message\n");
223 fprintf(stderr, " -n basename specify basename for symbols\n");
224 fprintf(stderr, " -i imagedir location of images\n");
225 fprintf(stderr, " -d dependencies write dependencies to this file\n");
226 fprintf(stderr, " infile format: <image> [across down]\n");
227 fprintf(stderr, " if infile is `-' image names are taken from the command line.\n");
228 exit(1);
229 }
230
231 /*****************************************************************************/
232
233 int
234 main(int argc, char **argv)
235 {
236 struct stat s;
237 int x, y, o;
238 FILE *inf, *outf, *depfile;
239 char *inbuf, *outfname, *tok;
240 char *imagename;
241 int across, down;
242 image_struct *img;
243
244 while (1)
245 switch (getopt(argc, argv, "vhn:i:d:m:")) {
246 case '?':
247 case 'h':
248 give_help();
249 case 'v':
250 verbose++;
251 break;
252 case 'n':
253 basename = optarg;
254 break;
255 case 'i':
256 imagedir = optarg;
257 break;
258 case 'd':
259 depfile = fopen(optarg, "w");
260 if (!depfile) {
261 fprintf(stderr, "Unable to open dependency file %s for writing\n", optarg);
262 perror("The error was");
263 exit(1);
264 }
265 break;
266 case -1:
267 goto done_args;
268 }
269 done_args:
270
271 if (optind > argc-1)
272 give_help();
273
274 if (strcmp (argv[optind], "-") == 0)
275 {
276 int i;
277 char *dot;
278 for (i=optind+2; i<argc; i++)
279 {
280 printf("image %s\n", argv[i]);
281 dot = strrchr(argv[i], '.');
282 if (dot) *dot = 0;
283
284 img = (image_struct *)malloc(sizeof(image_struct));
285 memset(img, 0, sizeof(*img));
286 img->name = strdup (argv[i]);
287 img->a = img->d = 1;
288 img->index = current_index++;
289
290 img->next = image_list;
291 image_list = img;
292 }
293 }
294 else
295 {
296 inf = fopen(argv[optind], "r");
297 if (!inf) {
298 fprintf(stderr, "Unable to open %s for reading\n", argv[optind]);
299 perror("The error was");
300 exit(1);
301 }
302 fstat(fileno(inf), &s);
303 inbuf = (char *)malloc(s.st_size+1);
304
305 while (fgets(inbuf, s.st_size, inf)) {
306
307 inbuf[strlen(inbuf)] = 0;
308 tokenize(inbuf);
309
310 imagename = tokenize(0);
311 if (!imagename) continue;
312
313 img = (image_struct *)malloc(sizeof(image_struct));
314 memset(img, 0, sizeof(*img));
315 img->name = strdup (imagename);
316 img->a = img->d = 1;
317 img->index = current_index++;
318
319 tok = tokenize(0);
320 if (tok)
321 img->a = atoi(tok);
322 tok = tokenize(0);
323 if (tok)
324 img->d = atoi(tok);
325
326 img->next = image_list;
327 image_list = img;
328 }
329 }
330
331 scan_image_directory();
332
333 if (optind > argc-2)
334 outfname = 0;
335 else
336 outfname = argv[optind+1];
337
338 if (outfname)
339 outf = fopen(outfname, "w");
340 else
341 outf = stdout;
342 if (!outf) {
343 fprintf(stderr, "Unable to open %s for writing\n", outfname);
344 perror("The error was");
345 exit(1);
346 }
347
348 fprintf(outf, "/* Automatically generated, do not edit */\n");
349 fprintf(outf, "#include \"imagelib.h\"\n\n");
350
351 dump_images(outf);
352
353 if (outfname) fclose(outf);
354
355 exit(0);
356 }