Sfoglia il codice sorgente

first pass at stb-izing complete, now need to debug

Sean Barrett 11 anni fa
parent
commit
4b590c0a62
2 ha cambiato i file con 121 aggiunte e 115 eliminazioni
  1. 41 114
      stb_herringbone_wang_tile.h
  2. 80 1
      tests/herringbone_generator.c

+ 41 - 114
stb_herringbone_wang_tile.h

@@ -232,7 +232,7 @@ typedef struct
 STBHW_EXTERN void stbhw_get_template_size(stbhw_config *c, int *w, int *h);
 STBHW_EXTERN void stbhw_get_template_size(stbhw_config *c, int *w, int *h);
 
 
 // generates a template image, assuming data is 3*w*h bytes long, RGB format
 // generates a template image, assuming data is 3*w*h bytes long, RGB format
-STBHW_EXTERN void stbhw_make_template(stbhw_config *c, unsigned char *data, int w, int h);
+STBHW_EXTERN int stbhw_make_template(stbhw_config *c, unsigned char *data, int w, int h, int stride_in_bytes);
 
 
 #endif//INCLUDE_STB_HWANG_H
 #endif//INCLUDE_STB_HWANG_H
 
 
@@ -340,7 +340,9 @@ static signed char h_color[STB_HBWANG_MAX_Y+5][STB_HBWANG_MAX_X+6];
 static char *stbhw_error;
 static char *stbhw_error;
 STBHW_EXTERN char *stbhw_get_last_error(void)
 STBHW_EXTERN char *stbhw_get_last_error(void)
 {
 {
-   return stbhw_error;
+   char *temp = stbhw_error;
+   stbhw_error = 0;
+   return temp;
 }
 }
 
 
 
 
@@ -470,7 +472,7 @@ STBHW_EXTERN void stbhw_get_template_size(stbhw_config *c, int *w, int *h)
    stbhw__get_template_info(c, w, h, NULL, NULL);
    stbhw__get_template_info(c, w, h, NULL, NULL);
 }
 }
 
 
-static int stbhw__process_image(stbhw__process *p)
+static int stbhw__process_template(stbhw__process *p)
 {
 {
    int i,j,k,q, ypos;
    int i,j,k,q, ypos;
    int size_x, size_y;
    int size_x, size_y;
@@ -896,6 +898,8 @@ STBHW_EXTERN int stbhw_build_tileset_from_image(stbhw_tileset *ts, unsigned char
       return 0;
       return 0;
    if (c.short_side_len == 0)
    if (c.short_side_len == 0)
       return 0;
       return 0;
+   if (c.num_color[0] > 32 || c.num_color[1] > 32 || c.num_color[2] > 32 || c.num_color[3] > 32)
+      return 0;
 
 
    stbhw__get_template_info(&c, NULL, NULL, &h_count, &v_count);
    stbhw__get_template_info(&c, NULL, NULL, &h_count, &v_count);
 
 
@@ -920,7 +924,7 @@ STBHW_EXTERN int stbhw_build_tileset_from_image(stbhw_tileset *ts, unsigned char
    p.h = h;
    p.h = h;
 
 
    // load all the tiles out of the image
    // load all the tiles out of the image
-   return stbhw__process_image(&p);
+   return stbhw__process_template(&p);
 }
 }
 
 
 STBHW_EXTERN void stbhw_free_tileset(stbhw_tileset *ts)
 STBHW_EXTERN void stbhw_free_tileset(stbhw_tileset *ts)
@@ -1158,126 +1162,49 @@ static void stbhw__corner_process_v_rect(stbhw__process *p, int xpos, int ypos,
 
 
 #endif // STB_HBWANG_IMPLEMENTATION
 #endif // STB_HBWANG_IMPLEMENTATION
 
 
-#ifndef INCLUDE_STB_IMAGE_WRITE_H
-#define STB_IMAGE_WRITE_IMPLEMENTATION
-#include "stb_image_write.h"
-#endif
-
-int corner_color_count[4] = { 2,2,2,2 };
-
-// whether each color for each type of corner should get the corner template
-int corner_type_color_template[4][4] =
-{
-   { 0,0,0,0 },
-   { 0,0,0,0 },
-   { 0,0,0,0 },
-   { 0,0,0,0 },
-};
-
-// number of duplicates with the exact same edge colors
-int num_variants_x = 1;
-int num_variants_y = 1;
-
-// total number of items in complete set:
-//
-// horizontal items:
-//   ec[1] * ec[2] * ec[3] * ec[0] * ec[1] * ec[2] * num_variants_x * num_variants_y
-//
-// vertical items:
-//   ec[0] * ce[1] * ce[3] * ce[0] * ec[2] * ec[3] * num_variants_x * num_variants_y
-
-// number of tiles along the short side
-int short_side = 9;
-
-int my_main(int argc, char **argv)
+// generates a template image, assuming data is 3*w*h bytes long, RGB format
+STBHW_EXTERN int stbhw_make_template(stbhw_config *c, unsigned char *data, int w, int h, int stride_in_bytes)
 {
 {
-   stbhw_config c;
    stbhw__process p;
    stbhw__process p;
    int i;
    int i;
 
 
-   if (argc != 2) {
-      fprintf(stderr, "Usage: gentemplate {filename}\n"); 
-      return 1;
-   }
-   p.data = NULL;
-   p.h = p.w = p.stride = 0;
-   p.process_h_rect = stbhw__corner_process_h_rect;
-   p.process_v_rect = stbhw__corner_process_v_rect;
-   //p.ts = 0;
-   p.c = & c;
-
-   c.is_corner = 1;
-   c.short_side_len = short_side;
-   c.num_vary_x = num_variants_x;
-   c.num_vary_y = num_variants_y;
-   memcpy(c.num_color, corner_color_count, sizeof(corner_color_count));
-
-   stbhw__process_image(&p);
-
-   // write out binary information in first line of image
-   for (i=0; i < 4; ++i)
-      p.data[p.w*3-1-i] = c.num_color[i];
-   p.data[p.w*3-1-i] = num_variants_x;
-   p.data[p.w*3-2-i] = num_variants_y;
-   p.data[p.w*3-3-i] = short_side;
-   p.data[p.w*3-4-i] = 0xc0;
-
-   // make it more obvious it encodes actual data
-   for (i=0; i < 9; ++i)
-      p.data[p.w*3 - 1 - i] ^= i*55;
-
-   stbi_write_png(argv[1], p.w, p.h, 3, p.data, p.w*3);
-   return 0;
-}
-
-
-/*
-// number of colors for each edge based on above diagram:
-int edge_colors[6] = { 1,1,1,1,1,1, };
-
-// number of duplicates with the exact same edge colors
-int num_variants_x = 4;
-int num_variants_y = 4;
-
-// total number of items in complete set:
-//
-// horizontal items:
-//   ec[0] * ec[1] * ec[2] * ec[3] * ec[4] * ec[2] * num_variants_x * num_variants_y
-//
-// vertical items:
-//   ec[0] * ce[1] * ce[5] * ce[3] * ec[4] * ec[5] * num_variants_x * num_variants_y
-
-// number of tiles along the short side
-int short_side = 12;
-
-int main(int argc, char **argv)
-{
-   int w,h,i;
-   unsigned char *data;
+   p.data = data;
+   p.w = w;
+   p.h = h;
+   p.stride = stride_in_bytes;
+   p.ts = 0;
+   p.c = c;
 
 
-   if (argc != 2) {
-      fprintf(stderr, "Usage: gentemplate {filename}\n"); 
-      return 1;
+   if (c->is_corner) {
+      p.process_h_rect = stbhw__corner_process_h_rect;
+      p.process_v_rect = stbhw__corner_process_v_rect;
+   } else {
+      p.process_h_rect = stbhw__edge_process_h_rect;
+      p.process_v_rect = stbhw__edge_process_v_rect;
    }
    }
 
 
-   memcpy(ec, edge_colors, sizeof(edge_colors));
-   num_vx = num_variants_x;
-   num_vy = num_variants_y;
-
-   data = process_image(NULL, &w, &h, short_side);
+   if (!stbhw__process_template(&p))
+      return 0;
 
 
-   // write out binary information in first line of image
-   for (i=0; i < 6; ++i)
-      data[w*3-1-i] = ec[i];
-   data[w*3-1-i] = num_variants_x;
-   data[w*3-2-i] = num_variants_y;
-   data[w*3-3-i] = short_side;
+   if (c->is_corner) {
+      // write out binary information in first line of image
+      for (i=0; i < 4; ++i)
+         data[w*3-1-i] = c->num_color[i];
+      data[w*3-1-i] = c->num_vary_x;
+      data[w*3-2-i] = c->num_vary_y;
+      data[w*3-3-i] = c->short_side_len;
+      data[w*3-4-i] = 0xc0;
+   } else {
+      for (i=0; i < 6; ++i)
+         data[w*3-1-i] = c->num_color[i];
+      data[w*3-1-i] = c->num_vary_x;
+      data[w*3-2-i] = c->num_vary_y;
+      data[w*3-3-i] = c->short_side_len;
+   }
 
 
    // make it more obvious it encodes actual data
    // make it more obvious it encodes actual data
    for (i=0; i < 9; ++i)
    for (i=0; i < 9; ++i)
-      data[w*3 - 1 - i] ^= i*55;
+      p.data[p.w*3 - 1 - i] ^= i*55;
 
 
-   stbi_write_png(argv[1], w, h, 3, data, w*3);
-   return 0;
+   return 1;
 }
 }
-*/

+ 80 - 1
tests/herringbone_generator.c

@@ -1,8 +1,87 @@
 #define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION
 #define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION
 #include "stb_herringbone_wang_tile.h"
 #include "stb_herringbone_wang_tile.h"
 
 
+#define STB_IMAGE_WRITE_IMPLEMENTATION
+#include "stb_image_write.h"
+
+//  e 12 1 1 1 1 1 1 4 4
+
 int main(int argc, char **argv)
 int main(int argc, char **argv)
 {
 {
+   stbhw_config c = { 0 };
+   int w,h, num_colors,i;
+   unsigned char *data;
+
+   if (argc == 1)  goto usage;
+   if (argc  < 3)  goto error;
+
+   switch (argv[2][0]) {
+      case 'c':
+         if (argc <  8 || argc > 10)
+            goto error;
+         num_colors = 4;
+         c.is_corner = 1;
+         break;
+
+      case 'e':
+         if (argc < 10 || argc > 12)
+            goto error;
+         num_colors = 6;
+         c.is_corner = 0;
+         break;
+
+      default:
+         goto error;
+   }
+
+   c.short_side_len = atoi(argv[3]);
+   for (i=0; i < num_colors; ++i)
+      c.num_color[i] = atoi(argv[4+i]);
+
+   c.num_vary_x = 1;
+   c.num_vary_y = 1;
+
+   if (argc > 4+i)
+      c.num_vary_x = atoi(argv[4+i]);
+   if (argc > 5+i)
+      c.num_vary_y = atoi(argv[5+i]);
+
+   stbhw_get_template_size(&c, &w, &h);
+
+   data = (unsigned char *) malloc(w*h*3);
+
+   if (stbhw_make_template(&c, data, w, h, w*3))
+      stbi_write_png(argv[1], w, h, 3, data, w*3);
+   else
+      fprintf(stderr, "Error: %s\n", stbhw_get_last_error());
    return 0;
    return 0;
-}
 
 
+ error:
+   fputs("Invalid command-line arguments\n\n", stderr);
+ usage:
+   fputs("Usage (see source for corner & edge type definitions):\n\n", stderr);
+   fputs("herringbone_generator {outfile} c {sidelen} {c0} {c1} {c2} {c3} [{vx} {vy}]\n"
+         "     {outfile}  -- filename that template will be written to as PNG\n"
+         "     {sidelen}  -- length of short side of rectangle in pixels\n"
+         "     {c0}       -- number of colors for corner type 0\n"
+         "     {c1}       -- number of colors for corner type 1\n"
+         "     {c2}       -- number of colors for corner type 2\n"
+         "     {c3}       -- number of colors for corner type 3\n"
+         "     {vx}       -- number of color-duplicating variations horizontally in template\n"
+         "     {vy}       -- number of color-duplicating variations vertically in template\n"
+         "\n"
+         , stderr);
+   fputs("herringbone_generator {outfile} e {sidelen} {e0} {e1} {e2} {e3} {e4} {e5} [{vx} {vy}]\n"
+         "     {outfile}  -- filename that template will be written to as PNG\n"
+         "     {sidelen}  -- length of short side of rectangle in pixels\n"
+         "     {e0}       -- number of colors for edge type 0\n"
+         "     {e1}       -- number of colors for edge type 1\n"
+         "     {e2}       -- number of colors for edge type 2\n"
+         "     {e3}       -- number of colors for edge type 3\n"
+         "     {e4}       -- number of colors for edge type 4\n"
+         "     {e5}       -- number of colors for edge type 5\n"
+         "     {vx}       -- number of color-duplicating variations horizontally in template\n"
+         "     {vy}       -- number of color-duplicating variations vertically in template\n"
+         , stderr);
+   return 1;
+}