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);
15 enum { OUT_IMG, OUT_C, OUT_ASM } outmode;
16 int tile_xsz = 8, tile_ysz = 8;
18 int main(int argc, char **argv)
22 for(i=1; i<argc; i++) {
23 if(argv[i][0] == '-') {
27 if(sscanf(argv[++i], "%dx%d", &tile_xsz, &tile_ysz) != 2) {
28 fprintf(stderr, "-t must be followed by the tile size (WxH)\n");
46 fprintf(stderr, "invalid option: %s\n", argv[i]);
51 fprintf(stderr, "invalid option: %s\n", argv[i]);
57 if(proc_image(argv[i]) == -1) {
66 void print_usage(const char *argv0)
68 printf("Usage: %s [options] <img1> [<img2> ... <imgN>]\n", argv0);
70 printf(" -t WxH: tile size (default 8x8)\n");
71 printf(" -c: output C array\n");
72 printf(" -s: output GNU assembler data\n");
73 printf(" -h: print usage and exit\n\n");
76 int find_tile(struct image *tile, struct image *tiles)
79 count = dynarr_size(tiles);
80 for(i=0; i<count; i++) {
81 if(cmp_image(tile, tiles + i) == 0) {
88 int proc_image(const char *fname)
90 int i, j, k, idx, xtiles, ytiles, ntiles, result = -1;
92 struct image img, tile;
93 struct image *tiles = 0;
94 unsigned char *tiles_pixels, *tptr;
96 int *tilemap = 0, *mapptr;
97 char *basename, *suffix, *outfile;
99 if(load_image(&img, fname) == -1) {
100 fprintf(stderr, "failed to load image: %s\n", fname);
103 basename = alloca(strlen(fname) + 1);
104 strcpy(basename, fname);
105 if((suffix = strchr(basename, '.'))) {
109 xtiles = img.width / tile_xsz;
110 ytiles = img.height / tile_ysz;
111 if(img.width % tile_xsz != 0 || img.height % tile_ysz != 0) {
112 fprintf(stderr, "image size (%dx%d) not evenly divisible into %dx%d tiles\n",
113 img.width, img.height, tile_xsz, tile_ysz);
117 if(!(tilemap = malloc(xtiles * ytiles * sizeof *tilemap))) {
118 fprintf(stderr, "failed to allocate %dx%d tilemap\n", xtiles, ytiles);
123 if(!(tiles = dynarr_alloc(0, sizeof *tiles))) {
124 fprintf(stderr, "failed to allocate tile array\n");
127 /* alloc a contiguous buffer for the full tileset pixels, to make it easier to write it
128 * out as a single image in the end
130 if(!(tiles_pixels = dynarr_alloc(0, tile_xsz * tile_ysz * img.bpp / 8))) {
131 fprintf(stderr, "failed to allocate tile pixel buffer\n");
136 tile.width = tile_xsz;
137 tile.height = tile_ysz;
138 tile.scansz = tile_xsz * tile.bpp / 8;
141 for(i=0; i<ytiles; i++) {
142 for(j=0; j<xtiles; j++) {
145 if((idx = find_tile(&tile, tiles)) == -1) {
146 /* we don't have a duplicate of this tile */
147 idx = dynarr_size(tiles);
149 if(!(tiles = dynarr_push(tiles, 0))) {
152 if(!(tptr = dynarr_push(tiles_pixels, 0))) {
157 tiles[idx].scansz = tiles[idx].pitch = tile.scansz;
159 /* did the array get relocated? */
160 if(tptr != tiles_pixels) {
162 /* make each tile's pixels pointer point to the right place in the large pixelbuffer */
163 for(k=0; k<idx+1; k++) {
164 tiles[k].pixels = tptr;
165 tptr += tile_ysz * tiles[idx].pitch;
168 /* otherwise just set the new one */
169 tiles[idx].pixels = tiles_pixels + idx * tile_ysz * tiles[idx].pitch;
172 blit(&tile, 0, 0, tile_xsz, tile_ysz, tiles + idx, 0, 0);
179 sptr += (tile_ysz - 1) * tile.scansz;
182 ntiles = dynarr_size(tiles);
183 fprintf(stderr, "%s (%dx%d) -> %d tiles, %d unique\n", fname, img.width, img.height,
184 xtiles * ytiles, ntiles);
188 /* make a big image out of the tiles and write it out */
190 tile.height = ntiles * tile_ysz;
192 outfile = alloca(strlen(basename) + 5);
196 sprintf(outfile, "%s.png", basename);
198 if(save_image(&tile, outfile) == -1) {
199 fprintf(stderr, "failed to write output image\n");
205 sprintf(outfile, "%s.c", basename);
207 if(!(fp = fopen(outfile, "w"))) {
208 fprintf(stderr, "failed to open output file: %s: %s\n", outfile, strerror(errno));
211 wrtiles_carr(fp, basename, &tile, ntiles);
212 wrtilemap_carr(fp, basename, tilemap, xtiles, ytiles);
213 wrpalette_carr(fp, basename, tile.cmap, tile.cmap_ncolors);
217 sprintf(outfile, "%s.s", basename);
225 dynarr_free(tiles_pixels);
232 void wrtiles_carr(FILE *fp, const char *name, struct image *timg, int ntiles)
235 unsigned char *ptr = timg->pixels;
237 fprintf(fp, "\nint %s_num_tiles = %d;\n", name, ntiles);
238 fprintf(fp, "int %s_tiles_width = %d;\n", name, timg->width);
239 fprintf(fp, "int %s_tiles_height = %d;\n", name, timg->height);
240 fprintf(fp, "int %s_tiles_bpp = %d;\n", name, timg->bpp);
241 fprintf(fp, "unsigned char %s_tiles[] = {\n", name);
243 for(i=0; i<timg->height; i++) {
244 for(j=0; j<timg->scansz; j++) {
246 curx = 3 + fprintf(fp, "\t%u", (unsigned int)*ptr++);
248 curx += fprintf(fp, ", %u", (unsigned int)*ptr++);
255 ptr += timg->pitch - timg->scansz;
258 fprintf(fp, "\n};\n\n");
261 void wrtilemap_carr(FILE *fp, const char *name, int *tmap, int xtiles, int ytiles)
265 fprintf(fp, "\nint %s_tilemap_cols = %d;\n", name, xtiles);
266 fprintf(fp, "int %s_tilemap_rows = %d;\n", name, ytiles);
267 fprintf(fp, "unsigned int %s_tilemap[] = {\n", name);
269 sz = xtiles * ytiles;
270 for(i=0; i<sz; i++) {
272 curx = 3 + fprintf(fp, "\t%u", (unsigned int)tmap[i]);
274 curx += fprintf(fp, ", %u", (unsigned int)tmap[i]);
282 fprintf(fp, "\n};\n\n");
285 void wrpalette_carr(FILE *fp, const char *name, struct cmapent *cmap, int ncol)
289 fprintf(fp, "\nint %s_cmap_colors = %d;\n", name, ncol);
290 fprintf(fp, "unsigned char %s_cmap[][3] = {\n", name);
291 for(i=0; i<ncol; i++) {
292 fprintf(fp, "\t{%u, %u, %u}", cmap[i].r, cmap[i].g, cmap[i].b);
299 fprintf(fp, "};\n\n");