1 #include <stdio.h>
2 
3 #define STB_HBWANG_MAX_X  500
4 #define STB_HBWANG_MAX_Y  500
5 
6 #define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION
7 #include "stb_herringbone_wang_tile.h"
8 
9 #define STB_IMAGE_IMPLEMENTATION
10 #include "stb_image.h"
11 
12 #define STB_IMAGE_WRITE_IMPLEMENTATION
13 #include "stb_image_write.h"
14 
main(int argc,char ** argv)15 int main(int argc, char **argv)
16 {
17    if (argc < 5) {
18       fprintf(stderr, "Usage: herringbone_map {inputfile} {output-width} {output-height} {outputfile}\n");
19       return 1;
20    } else {
21       char *filename = argv[1];
22       int out_w = atoi(argv[2]);
23       int out_h = atoi(argv[3]);
24       char *outfile = argv[4];
25 
26       unsigned char *pixels, *out_pixels;
27       stbhw_tileset ts;
28       int w,h;
29 
30       pixels = stbi_load(filename, &w, &h, 0, 3);
31       if (pixels == 0) {
32          fprintf(stderr, "Couldn't open input file '%s'\n", filename);
33 			exit(1);
34       }
35 
36       if (!stbhw_build_tileset_from_image(&ts, pixels, w*3, w, h)) {
37          fprintf(stderr, "Error: %s\n", stbhw_get_last_error());
38          return 1;
39       }
40 
41       free(pixels);
42 
43       #ifdef DEBUG_OUTPUT
44       {
45          int i,j,k;
46          // add blue borders to top-left edges of the tiles
47          int hstride = (ts.short_side_len*2)*3;
48          int vstride = (ts.short_side_len  )*3;
49          for (i=0; i < ts.num_h_tiles; ++i) {
50             unsigned char *pix = ts.h_tiles[i]->pixels;
51             for (j=0; j < ts.short_side_len*2; ++j)
52                for (k=0; k < 3; ++k)
53                   pix[j*3+k] = (pix[j*3+k]*0.5+100+k*75)/1.5;
54             for (j=1; j < ts.short_side_len; ++j)
55                for (k=0; k < 3; ++k)
56                   pix[j*hstride+k] = (pix[j*hstride+k]*0.5+100+k*75)/1.5;
57          }
58          for (i=0; i < ts.num_v_tiles; ++i) {
59             unsigned char *pix = ts.v_tiles[i]->pixels;
60             for (j=0; j < ts.short_side_len; ++j)
61                for (k=0; k < 3; ++k)
62                   pix[j*3+k] = (pix[j*3+k]*0.5+100+k*75)/1.5;
63             for (j=1; j < ts.short_side_len*2; ++j)
64                for (k=0; k < 3; ++k)
65                   pix[j*vstride+k] = (pix[j*vstride+k]*0.5+100+k*75)/1.5;
66          }
67       }
68       #endif
69 
70       out_pixels = malloc(out_w * out_h * 3);
71 
72       if (!stbhw_generate_image(&ts, NULL, out_pixels, out_w*3, out_w, out_h)) {
73          fprintf(stderr, "Error: %s\n", stbhw_get_last_error());
74          return 1;
75       }
76 
77       stbi_write_png(argv[4], out_w, out_h, 3, out_pixels, out_w*3);
78       free(out_pixels);
79 
80       stbhw_free_tileset(&ts);
81       return 0;
82    }
83 }