11
static int verbose = 0;
12
static char *basename = "images";
13
static char *imagedir = ".";
15
/*****************************************************************************/
18
concat (char *a, char *b)
21
int i = strlen(a) + strlen(b) + 2;
22
buf = (char *)malloc(i);
23
sprintf(buf, "%s/%s", a, b);
27
/*****************************************************************************/
29
typedef struct subimage_struct {
30
struct subimage_struct *next;
36
typedef struct image_struct {
37
struct image_struct *next;
40
subimage_struct *sub[3];
44
image_struct *image_list = 0;
45
int current_index = 0;
48
scan_image_directory ()
55
dir = opendir(imagedir);
57
fprintf(stderr, "Unable to scan directory %s for images.\n", imagedir);
58
perror("The error was");
61
while (de = readdir(dir)) {
66
png_uint_32 width, height;
67
int bit_depth, color_type, interlace_type, type;
69
dot = strrchr(de->d_name, '.');
72
if (strcmp(dot, ".png"))
75
dot = strchr(de->d_name, '.');
77
for (img=image_list; img; img=img->next)
78
if (strcmp(img->name, de->d_name) == 0)
84
f = fopen(concat(imagedir, de->d_name), "rb");
86
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
87
info_ptr = png_create_info_struct (png_ptr);
89
if (setjmp (png_ptr->jmpbuf)) {
94
png_init_io (png_ptr, f);
96
png_read_info (png_ptr, info_ptr);
98
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth,
99
&color_type, &interlace_type, 0, 0);
103
else if (color_type & PNG_COLOR_MASK_COLOR)
108
sub = (subimage_struct *)malloc(sizeof(subimage_struct));
109
sub->next = img->sub[type];
110
img->sub[type] = sub;
111
sub->filename = strdup (concat (imagedir, de->d_name));
114
sub->index = current_index++;
120
static int print_col = 0;
121
static int print_com = 1;
123
print_int(FILE *f, int val)
126
sprintf(buf, "%d", val);
134
if (print_col + strlen(buf) > 70) {
139
print_col += strlen(buf);
147
subimage_struct *sub;
149
static char *type_names[] = {"mono", "grey", "color"};
151
for (img=image_list; img; img=img->next)
153
for (sub=img->sub[i]; sub; sub=sub->next)
155
FILE *f = fopen(sub->filename, "rb");
157
fprintf(o, "/* %s */\n", sub->filename);
158
fprintf(o, "static const unsigned char data_%d[] = {\n", sub->index);
160
while ((byte = fgetc (f)) != EOF)
162
fprintf(o, "};\n\n");
166
for (img=image_list; img; img=img->next)
170
fprintf(o, "static image sub_%d_%s[] = {\n", img->index, type_names[i]);
171
for (sub=img->sub[i]; sub; sub=sub->next)
172
fprintf(o, " { %d, %d, data_%d },\n", sub->w, sub->h, sub->index);
173
fprintf(o, " { 0, 0, 0 }\n};\n\n");
176
fprintf(o, "image_list %s_imagelib[] = {\n", basename);
177
for (img=image_list; img; img=img->next)
180
fprintf(o, " { \"%s\", %d, %d, { ", img->name, img->a, img->d);
184
fprintf(o, "%ssub_%d_%s", c, img->index, type_names[i]);
186
fprintf(o, "%s0", c);
189
fprintf(o, " } },\n");
191
fprintf(o, " { 0, 0, 0, { 0, 0, 0 } }\n};\n\n");
194
/*****************************************************************************/
197
tokenize(char *string)
205
while (*next && !isgraph(*next)) next++;
206
if (!*next) return 0;
208
while (*next && isgraph(*next)) next++;
217
fprintf(stderr, "make-imgdir [options] infile outfile [pngs]\n");
218
fprintf(stderr, " -v verbose\n");
219
fprintf(stderr, " -h print this help message\n");
220
fprintf(stderr, " -n basename specify basename for symbols\n");
221
fprintf(stderr, " -i imagedir location of images\n");
222
fprintf(stderr, " -d dependencies write dependencies to this file\n");
223
fprintf(stderr, " infile format: <image> [across down]\n");
224
fprintf(stderr, " if infile is `-' image names are taken from the command line.\n");
228
/*****************************************************************************/
231
main(int argc, char **argv)
235
FILE *inf, *outf, *depfile;
236
char *inbuf, *outfname, *tok;
242
switch (getopt(argc, argv, "vhn:i:d:m:")) {
256
depfile = fopen(optarg, "w");
258
fprintf(stderr, "Unable to open dependency file %s for writing\n", depfile);
259
perror("The error was");
271
if (strcmp (argv[optind], "-") == 0)
275
for (i=optind+2; i<argc; i++)
277
printf("image %s\n", argv[i]);
278
dot = strrchr(argv[i], '.');
281
img = (image_struct *)malloc(sizeof(image_struct));
282
memset(img, 0, sizeof(*img));
283
img->name = strdup (argv[i]);
285
img->index = current_index++;
287
img->next = image_list;
293
inf = fopen(argv[optind], "r");
295
fprintf(stderr, "Unable to open %s for reading\n", argv[optind]);
296
perror("The error was");
299
fstat(fileno(inf), &s);
300
inbuf = (char *)malloc(s.st_size+1);
302
while (fgets(inbuf, s.st_size, inf)) {
304
inbuf[strlen(inbuf)] = 0;
307
imagename = tokenize(0);
308
if (!imagename) continue;
310
img = (image_struct *)malloc(sizeof(image_struct));
311
memset(img, 0, sizeof(*img));
312
img->name = strdup (imagename);
314
img->index = current_index++;
323
img->next = image_list;
328
scan_image_directory();
333
outfname = argv[optind+1];
336
outf = fopen(outfname, "w");
340
fprintf(stderr, "Unable to open %s for writing\n", outfname);
341
perror("The error was");
345
fprintf(outf, "/* Automatically generated, do not edit */\n");
346
fprintf(outf, "#include \"imagelib.h\"\n\n");
350
if (outfname) fclose(outf);