Skip to content
Myr

herringbone_map.c

This file

Path

tests/herringbone_map.c

83 lines · 2.4 KiB · C

Last changed by

Not known.

History of this file

Take it

1#include <stdio.h>23#define STB_HBWANG_MAX_X  5004#define STB_HBWANG_MAX_Y  50056#define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION7#include "stb_herringbone_wang_tile.h"89#define STB_IMAGE_IMPLEMENTATION10#include "stb_image.h"1112#define STB_IMAGE_WRITE_IMPLEMENTATION13#include "stb_image_write.h"1415int 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];2526      unsigned char *pixels, *out_pixels;27      stbhw_tileset ts;28      int w,h;2930      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      }3536      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      }4041      free(pixels);4243      #ifdef DEBUG_OUTPUT44      {45         int i,j,k;46         // add blue borders to top-left edges of the tiles47         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      #endif6970      out_pixels = malloc(out_w * out_h * 3);7172      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      }7677      stbi_write_png(argv[4], out_w, out_h, 3, out_pixels, out_w*3);78      free(out_pixels);7980      stbhw_free_tileset(&ts);81      return 0;82   }83}