removed leftover debug print
[img2tiles] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <errno.h>
6 #include "image.h"
7 #include "dynarr.h"
8
9 void print_usage(const char *argv0);
10 int proc_image(const char *fname);
11 void wrtiles_carr(FILE *fp, const char *name, struct image *timg, int ntiles);
12 void wrtilemap_carr(FILE *fp, const char *name, int *tmap, int xtiles, int ytiles);
13 void wrpalette_carr(FILE *fp, const char *name, struct cmapent *cmap, int ncol);
14
15 enum { OUT_IMG, OUT_C, OUT_ASM } outmode;
16 static const char *otype_suffix[] = {".png", ".c", ".s"};
17
18 int tile_xsz = 8, tile_ysz = 8;
19 const char *output_filename;
20
21 int main(int argc, char **argv)
22 {
23         int i;
24
25         for(i=1; i<argc; i++) {
26                 if(argv[i][0] == '-') {
27                         if(argv[i][2] == 0) {
28                                 switch(argv[i][1]) {
29                                 case 'o':
30                                         output_filename = argv[++i];
31                                         break;
32
33                                 case 't':
34                                         if(sscanf(argv[++i], "%dx%d", &tile_xsz, &tile_ysz) != 2) {
35                                                 fprintf(stderr, "-t must be followed by the tile size (WxH)\n");
36                                                 return 1;
37                                         }
38                                         break;
39
40                                 case 'c':
41                                         outmode = OUT_C;
42                                         break;
43
44                                 case 's':
45                                         outmode = OUT_ASM;
46                                         break;
47
48                                 case 'h':
49                                         print_usage(argv[0]);
50                                         return 0;
51
52                                 default:
53                                         fprintf(stderr, "invalid option: %s\n", argv[i]);
54                                         print_usage(argv[0]);
55                                         return 1;
56                                 }
57                         } else {
58                                 fprintf(stderr, "invalid option: %s\n", argv[i]);
59                                 print_usage(argv[0]);
60                                 return 1;
61                         }
62
63                 } else {
64                         if(proc_image(argv[i]) == -1) {
65                                 return 1;
66                         }
67                 }
68         }
69
70         return 0;
71 }
72
73 void print_usage(const char *argv0)
74 {
75         printf("Usage: %s [options] <img1> [<img2> ... <imgN>]\n", argv0);
76         printf("Options:\n");
77         printf(" -o <file>: output file\n");
78         printf(" -t WxH: tile size (default 8x8)\n");
79         printf(" -c: output C array\n");
80         printf(" -s: output GNU assembler data\n");
81         printf(" -h: print usage and exit\n\n");
82 }
83
84 int find_tile(struct image *tile, struct image *tiles)
85 {
86         int i, count;
87         count = dynarr_size(tiles);
88         for(i=0; i<count; i++) {
89                 if(cmp_image(tile, tiles + i) == 0) {
90                         return i;
91                 }
92         }
93         return -1;
94 }
95
96 int proc_image(const char *fname)
97 {
98         int i, j, k, idx, xtiles, ytiles, ntiles, result = -1;
99         FILE *fp;
100         struct image img, tile;
101         struct image *tiles = 0;
102         unsigned char *tiles_pixels, *tptr;
103         unsigned char *sptr;
104         int *tilemap = 0, *mapptr;
105         char *basename, *suffix, *outfile;
106
107         if(load_image(&img, fname) == -1) {
108                 fprintf(stderr, "failed to load image: %s\n", fname);
109                 return -1;
110         }
111         basename = alloca(strlen(fname) + 1);
112         strcpy(basename, fname);
113         if((suffix = strrchr(basename, '/'))) {
114                 basename = suffix + 1;
115         }
116         if((suffix = strchr(basename, '.'))) {
117                 *suffix = 0;
118         }
119
120         xtiles = img.width / tile_xsz;
121         ytiles = img.height / tile_ysz;
122         if(img.width % tile_xsz != 0 || img.height % tile_ysz != 0) {
123                 fprintf(stderr, "image size (%dx%d) not evenly divisible into %dx%d tiles\n",
124                                 img.width, img.height, tile_xsz, tile_ysz);
125                 goto err;
126         }
127
128         if(!(tilemap = malloc(xtiles * ytiles * sizeof *tilemap))) {
129                 fprintf(stderr, "failed to allocate %dx%d tilemap\n", xtiles, ytiles);
130                 goto err;
131         }
132         mapptr = tilemap;
133
134         if(!(tiles = dynarr_alloc(0, sizeof *tiles))) {
135                 fprintf(stderr, "failed to allocate tile array\n");
136                 goto err;
137         }
138         /* alloc a contiguous buffer for the full tileset pixels, to make it easier to write it
139          * out as a single image in the end
140          */
141         if(!(tiles_pixels = dynarr_alloc(0, tile_xsz * tile_ysz * img.bpp / 8))) {
142                 fprintf(stderr, "failed to allocate tile pixel buffer\n");
143                 goto err;
144         }
145
146         tile = img;
147         tile.width = tile_xsz;
148         tile.height = tile_ysz;
149         tile.scansz = tile_xsz * tile.bpp / 8;
150
151         sptr = img.pixels;
152         for(i=0; i<ytiles; i++) {
153                 for(j=0; j<xtiles; j++) {
154                         tile.pixels = sptr;
155
156                         if((idx = find_tile(&tile, tiles)) == -1) {
157                                 /* we don't have a duplicate of this tile */
158                                 idx = dynarr_size(tiles);
159
160                                 if(!(tiles = dynarr_push(tiles, 0))) {
161                                         goto err;
162                                 }
163                                 if(!(tptr = dynarr_push(tiles_pixels, 0))) {
164                                         goto err;
165                                 }
166
167                                 tiles[idx] = tile;
168                                 tiles[idx].scansz = tiles[idx].pitch = tile.scansz;
169
170                                 /* did the array get relocated? */
171                                 if(tptr != tiles_pixels) {
172                                         tiles_pixels = tptr;
173                                         /* make each tile's pixels pointer point to the right place in the large pixelbuffer */
174                                         for(k=0; k<idx+1; k++) {
175                                                 tiles[k].pixels = tptr;
176                                                 tptr += tile_ysz * tiles[idx].pitch;
177                                         }
178                                 } else {
179                                         /* otherwise just set the new one */
180                                         tiles[idx].pixels = tiles_pixels + idx * tile_ysz * tiles[idx].pitch;
181                                 }
182
183                                 blit(&tile, 0, 0, tile_xsz, tile_ysz, tiles + idx, 0, 0);
184                         }
185
186                         *mapptr++ = idx;
187
188                         sptr += tile.scansz;
189                 }
190                 sptr += (tile_ysz - 1) * tile.pitch;
191         }
192
193         ntiles = dynarr_size(tiles);
194         fprintf(stderr, "%s (%dx%d) -> %d tiles, %d unique\n", fname, img.width, img.height,
195                         xtiles * ytiles, ntiles);
196
197         assert(ntiles > 0);
198
199         /* make a big image out of the tiles and write it out */
200         tile = tiles[0];
201         tile.height = ntiles * tile_ysz;
202
203         if(output_filename) {
204                 outfile = (char*)output_filename;
205         } else {
206                 outfile = alloca(strlen(basename) + 5);
207                 sprintf(outfile, "%s%s", basename, otype_suffix[outmode]);
208         }
209
210         switch(outmode) {
211         case OUT_IMG:
212                 if(save_image(&tile, outfile) == -1) {
213                         fprintf(stderr, "failed to write output image\n");
214                         goto err;
215                 }
216                 break;
217
218         case OUT_C:
219                 if(!(fp = fopen(outfile, "w"))) {
220                         fprintf(stderr, "failed to open output file: %s: %s\n", outfile, strerror(errno));
221                         goto err;
222                 }
223                 wrtiles_carr(fp, basename, &tile, ntiles);
224                 wrtilemap_carr(fp, basename, tilemap, xtiles, ytiles);
225                 wrpalette_carr(fp, basename, tile.cmap, tile.cmap_ncolors);
226                 break;
227
228         case OUT_ASM:
229                 /* TODO */
230                 break;
231         }
232
233         result = 0;
234
235 err:
236         dynarr_free(tiles_pixels);
237         dynarr_free(tiles);
238         free(tilemap);
239         free(img.pixels);
240         return result;
241 }
242
243 void wrtiles_carr(FILE *fp, const char *name, struct image *timg, int ntiles)
244 {
245         int i, j, curx = 0;
246         unsigned char *ptr = timg->pixels;
247
248         fprintf(fp, "\nint %s_num_tiles = %d;\n", name, ntiles);
249         fprintf(fp, "int %s_tiles_width = %d;\n", name, timg->width);
250         fprintf(fp, "int %s_tiles_height = %d;\n", name, timg->height);
251         fprintf(fp, "int %s_tiles_bpp = %d;\n", name, timg->bpp);
252         fprintf(fp, "unsigned char %s_tiles[] = {\n", name);
253
254         for(i=0; i<timg->height; i++) {
255                 for(j=0; j<timg->scansz; j++) {
256                         if(curx == 0) {
257                                 curx = 3 + fprintf(fp, "\t%u", (unsigned int)*ptr++);
258                         } else {
259                                 curx += fprintf(fp, ", %u", (unsigned int)*ptr++);
260                         }
261                         if(curx >= 80) {
262                                 fprintf(fp, ",\n");
263                                 curx = 0;
264                         }
265                 }
266                 ptr += timg->pitch - timg->scansz;
267         }
268
269         fprintf(fp, "\n};\n\n");
270 }
271
272 void wrtilemap_carr(FILE *fp, const char *name, int *tmap, int xtiles, int ytiles)
273 {
274         int i, sz, curx = 0;
275
276         fprintf(fp, "\nint %s_tilemap_cols = %d;\n", name, xtiles);
277         fprintf(fp, "int %s_tilemap_rows = %d;\n", name, ytiles);
278         fprintf(fp, "unsigned int %s_tilemap[] = {\n", name);
279
280         sz = xtiles * ytiles;
281         for(i=0; i<sz; i++) {
282                 if(curx == 0) {
283                         curx = 3 + fprintf(fp, "\t%u", (unsigned int)tmap[i]);
284                 } else {
285                         curx += fprintf(fp, ", %u", (unsigned int)tmap[i]);
286                 }
287                 if(curx >= 80) {
288                         fprintf(fp, ",\n");
289                         curx = 0;
290                 }
291         }
292
293         fprintf(fp, "\n};\n\n");
294 }
295
296 void wrpalette_carr(FILE *fp, const char *name, struct cmapent *cmap, int ncol)
297 {
298         int i;
299
300         fprintf(fp, "\nint %s_cmap_colors = %d;\n", name, ncol);
301         fprintf(fp, "unsigned char %s_cmap[][3] = {\n", name);
302         for(i=0; i<ncol; i++) {
303                 fprintf(fp, "\t{%u, %u, %u}", cmap[i].r, cmap[i].g, cmap[i].b);
304                 if(i < ncol - 1) {
305                         fputs(",\n", fp);
306                 } else {
307                         fputc('\n', fp);
308                 }
309         }
310         fprintf(fp, "};\n\n");
311 }