2014-07-07 12:54:52 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define STB_HBWANG_MAX_X 500
|
|
|
|
#define STB_HBWANG_MAX_Y 500
|
|
|
|
|
|
|
|
#define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION
|
|
|
|
#include "stb_herringbone_wang_tile.h"
|
|
|
|
|
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
|
|
#include "stb_image.h"
|
|
|
|
|
|
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
|
|
#include "stb_image_write.h"
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc < 5) {
|
|
|
|
fprintf(stderr, "Usage: herringbone_map {inputfile} {output-width} {output-height} {outputfile}\n");
|
|
|
|
return 1;
|
|
|
|
} else {
|
2014-07-07 15:46:39 +01:00
|
|
|
char *filename = argv[1];
|
2014-07-07 12:54:52 +01:00
|
|
|
int out_w = atoi(argv[2]);
|
2014-07-07 15:46:39 +01:00
|
|
|
int out_h = atoi(argv[3]);
|
2014-07-07 12:54:52 +01:00
|
|
|
char *outfile = argv[4];
|
|
|
|
|
2014-07-07 15:46:39 +01:00
|
|
|
unsigned char *pixels, *out_pixels;
|
|
|
|
stbhw_tileset ts;
|
|
|
|
int w,h;
|
2014-07-07 12:54:52 +01:00
|
|
|
|
2014-07-07 15:46:39 +01:00
|
|
|
pixels = stbi_load(filename, &w, &h, 0, 3);
|
2014-07-07 12:54:52 +01:00
|
|
|
if (pixels == 0) {
|
|
|
|
fprintf(stderr, "Could open input file '%s'\n", filename);
|
|
|
|
}
|
|
|
|
|
2014-07-07 15:46:39 +01:00
|
|
|
if (!stbhw_build_tileset_from_image(&ts, pixels, w*3, w, h)) {
|
|
|
|
fprintf(stderr, "Error: %s\n", stbhw_get_last_error());
|
|
|
|
return 1;
|
|
|
|
}
|
2014-07-07 12:54:52 +01:00
|
|
|
|
2014-07-07 15:46:39 +01:00
|
|
|
free(pixels);
|
2014-07-07 12:54:52 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG_OUTPUT
|
|
|
|
{
|
|
|
|
int i,j,k;
|
|
|
|
// add blue borders to top-left edges of the tiles
|
|
|
|
int hstride = (ts.short_side_len*2)*3;
|
|
|
|
int vstride = (ts.short_side_len )*3;
|
|
|
|
for (i=0; i < ts.num_h_tiles; ++i) {
|
|
|
|
unsigned char *pix = ts.h_tiles[i]->pixels;
|
|
|
|
for (j=0; j < ts.short_side_len*2; ++j)
|
|
|
|
for (k=0; k < 3; ++k)
|
|
|
|
pix[j*3+k] = (pix[j*3+k]*0.5+100+k*75)/1.5;
|
|
|
|
for (j=1; j < ts.short_side_len; ++j)
|
|
|
|
for (k=0; k < 3; ++k)
|
|
|
|
pix[j*hstride+k] = (pix[j*hstride+k]*0.5+100+k*75)/1.5;
|
|
|
|
}
|
|
|
|
for (i=0; i < ts.num_v_tiles; ++i) {
|
|
|
|
unsigned char *pix = ts.v_tiles[i]->pixels;
|
|
|
|
for (j=0; j < ts.short_side_len; ++j)
|
|
|
|
for (k=0; k < 3; ++k)
|
|
|
|
pix[j*3+k] = (pix[j*3+k]*0.5+100+k*75)/1.5;
|
|
|
|
for (j=1; j < ts.short_side_len*2; ++j)
|
|
|
|
for (k=0; k < 3; ++k)
|
|
|
|
pix[j*vstride+k] = (pix[j*vstride+k]*0.5+100+k*75)/1.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-07-07 15:46:39 +01:00
|
|
|
out_pixels = malloc(out_w * out_h * 3);
|
2014-07-07 12:54:52 +01:00
|
|
|
|
2014-07-07 15:46:39 +01:00
|
|
|
if (!stbhw_generate_image(&ts, NULL, out_pixels, out_w*3, out_w, out_h)) {
|
|
|
|
fprintf(stderr, "Error: %s\n", stbhw_get_last_error());
|
|
|
|
return 1;
|
|
|
|
}
|
2014-07-07 12:54:52 +01:00
|
|
|
|
2014-07-07 15:46:39 +01:00
|
|
|
stbi_write_png(argv[4], out_w, out_h, 3, out_pixels, out_w*3);
|
|
|
|
free(out_pixels);
|
2014-07-07 12:54:52 +01:00
|
|
|
|
2014-07-07 15:46:39 +01:00
|
|
|
stbhw_free_tileset(&ts);
|
|
|
|
return 0;
|
2014-07-07 12:54:52 +01:00
|
|
|
}
|
|
|
|
}
|