Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1220 lines
42 KiB

  1. /* stbhw - v0.6 - http://nothings.org/gamedev/herringbone
  2. Herringbone Wang Tile Generator - Sean Barrett 2014 - public domain
  3. == LICENSE ==============================
  4. This software is dual-licensed to the public domain and under the following
  5. license: you are granted a perpetual, irrevocable license to copy, modify,
  6. publish, and distribute this file as you see fit.
  7. == WHAT IT IS ===========================
  8. This library is an SDK for Herringbone Wang Tile generation:
  9. http://nothings.org/gamedev/herringbone
  10. The core design is that you use this library offline to generate a
  11. "template" of the tiles you'll create. You then edit those tiles, then
  12. load the created tile image file back into this library and use it at
  13. runtime to generate "maps".
  14. You cannot load arbitrary tile image files with this library; it is
  15. only designed to load image files made from the template it created.
  16. It stores a binary description of the tile sizes & constraints in a
  17. few pixels, and uses those to recover the rules, rather than trying
  18. to parse the tiles themselves.
  19. You *can* use this library to generate from arbitrary tile sets, but
  20. only by loading the tile set and specifying the constraints explicitly
  21. yourself.
  22. == COMPILING ============================
  23. 1. #define STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION before including this
  24. header file in *one* source file to create the implementation
  25. in that source file.
  26. 2. optionally #define STB_HBWANG_RAND() to be a random number
  27. generator. if you don't define it, it will use rand(),
  28. and you need to seed srand() yourself.
  29. 3. optionally #define STB_HBWANG_ASSERT(x), otherwise
  30. it will use assert()
  31. 4. optionally #define STB_HBWANG_STATIC to force all symbols to be
  32. static instead of public, so they are only accesible
  33. in the source file that creates the implementation
  34. 5. optionally #define STB_HBWANG_NO_REPITITION_REDUCTION to disable
  35. the code that tries to reduce having the same tile appear
  36. adjacent to itself in wang-corner-tile mode (e.g. imagine
  37. if you were doing something where 90% of things should be
  38. the same grass tile, you need to disable this system)
  39. 6. optionally define STB_HBWANG_MAX_X and STB_HBWANG_MAX_Y
  40. to be the max dimensions of the generated map in multiples
  41. of the wang tile's short side's length (e.g. if you
  42. have 20x10 wang tiles, so short_side_len=10, and you
  43. have MAX_X is 17, then the largest map you can generate
  44. is 170 pixels wide). The defaults are 100x100. This
  45. is used to define static arrays which affect memory
  46. usage.
  47. == USING ================================
  48. To use the map generator, you need a tileset. You can download
  49. some sample tilesets from http://nothings.org/gamedev/herringbone
  50. Then see the "sample application" below.
  51. You can also use this file to generate templates for
  52. tilesets which you then hand-edit to create the data.
  53. == MEMORY MANAGEMENT ====================
  54. The tileset loader allocates memory with malloc(). The map
  55. generator does no memory allocation, so e.g. you can load
  56. tilesets at startup and never free them and never do any
  57. further allocation.
  58. == SAMPLE APPLICATION ===================
  59. #include <stdlib.h>
  60. #include <stdio.h>
  61. #include <time.h>
  62. #define STB_IMAGE_IMPLEMENTATION
  63. #include "stb_image.h" // http://nothings.org/stb_image.c
  64. #define STB_IMAGE_WRITE_IMPLEMENTATION
  65. #include "stb_image_write.h" // http://nothings.org/stb/stb_image_write.h
  66. #define STB_HBWANG_IMPLEMENTATION
  67. #include "stb_hbwang.h"
  68. int main(int argc, char **argv)
  69. {
  70. unsigned char *data;
  71. int xs,ys, w,h;
  72. stbhw_tileset ts;
  73. if (argc != 4) {
  74. fprintf(stderr, "Usage: mapgen {tile-file} {xsize} {ysize}\n"
  75. "generates file named 'test_map.png'\n");
  76. exit(1);
  77. }
  78. data = stbi_load(argv[1], &w, &h, NULL, 3);
  79. xs = atoi(argv[2]);
  80. ys = atoi(argv[3]);
  81. if (data == NULL) {
  82. fprintf(stderr, "Error opening or parsing '%s' as an image file\n", argv[1]);
  83. exit(1);
  84. }
  85. if (xs < 1 || xs > 1000) {
  86. fprintf(stderr, "xsize invalid or out of range\n");
  87. exit(1);
  88. }
  89. if (ys < 1 || ys > 1000) {
  90. fprintf(stderr, "ysize invalid or out of range\n");
  91. exit(1);
  92. }
  93. stbhw_build_tileset_from_image(&ts, data, w*3, w, h);
  94. free(data);
  95. // allocate a buffer to create the final image to
  96. data = malloc(3 * xs * ys);
  97. srand(time(NULL));
  98. stbhw_generate_image(&ts, NULL, data, xs*3, xs, ys);
  99. stbi_write_png("test_map.png", xs, ys, 3, data, xs*3);
  100. stbhw_free_tileset(&ts);
  101. free(data);
  102. return 0;
  103. }
  104. == VERSION HISTORY ===================
  105. 0.6 2014-08-17 - fix broken map-maker
  106. 0.5 2014-07-07 - initial release
  107. */
  108. //////////////////////////////////////////////////////////////////////////////
  109. // //
  110. // HEADER FILE SECTION //
  111. // //
  112. #ifndef INCLUDE_STB_HWANG_H
  113. #define INCLUDE_STB_HWANG_H
  114. #ifdef STB_HBWANG_STATIC
  115. #define STBHW_EXTERN static
  116. #else
  117. #ifdef __cplusplus
  118. #define STBHW_EXTERN extern "C"
  119. #else
  120. #define STBHW_EXTERN extern
  121. #endif
  122. #endif
  123. typedef struct stbhw_tileset stbhw_tileset;
  124. // returns description of last error produced by any function (not thread-safe)
  125. STBHW_EXTERN char *stbhw_get_last_error(void);
  126. // build a tileset from an image that conforms to a template created by this
  127. // library. (you allocate storage for stbhw_tileset and function fills it out;
  128. // memory for individual tiles are malloc()ed).
  129. // returns non-zero on success, 0 on error
  130. STBHW_EXTERN int stbhw_build_tileset_from_image(stbhw_tileset *ts,
  131. unsigned char *pixels, int stride_in_bytes, int w, int h);
  132. // free a tileset built by stbhw_build_tileset_from_image
  133. STBHW_EXTERN void stbhw_free_tileset(stbhw_tileset *ts);
  134. // generate a map that is w * h pixels (3-bytes each)
  135. // returns non-zero on success, 0 on error
  136. // not thread-safe (uses a global data structure to avoid memory management)
  137. // weighting should be NULL, as non-NULL weighting is currently untested
  138. STBHW_EXTERN int stbhw_generate_image(stbhw_tileset *ts, int **weighting,
  139. unsigned char *pixels, int stride_in_bytes, int w, int h);
  140. //////////////////////////////////////
  141. //
  142. // TILESET DATA STRUCTURE
  143. //
  144. // if you use the image-to-tileset system from this file, you
  145. // don't need to worry about these data structures. but if you
  146. // want to build/load a tileset yourself, you'll need to fill
  147. // these out.
  148. typedef struct
  149. {
  150. // the edge or vertex constraints, according to diagram below
  151. signed char a,b,c,d,e,f;
  152. // The herringbone wang tile data; it is a bitmap which is either
  153. // w=2*short_sidelen,h=short_sidelen, or w=short_sidelen,h=2*short_sidelen.
  154. // it is always RGB, stored row-major, with no padding between rows.
  155. // (allocate stbhw_tile structure to be large enough for the pixel data)
  156. unsigned char pixels[1];
  157. } stbhw_tile;
  158. struct stbhw_tileset
  159. {
  160. int is_corner;
  161. int num_color[6]; // number of colors for each of 6 edge types or 4 corner types
  162. int short_side_len;
  163. stbhw_tile **h_tiles;
  164. stbhw_tile **v_tiles;
  165. int num_h_tiles, max_h_tiles;
  166. int num_v_tiles, max_v_tiles;
  167. };
  168. /////////////// TEMPLATE GENERATOR //////////////////////////
  169. // when requesting a template, you fill out this data
  170. typedef struct
  171. {
  172. int is_corner; // using corner colors or edge colors?
  173. int short_side_len; // rectangles is 2n x n, n = short_side_len
  174. int num_color[6]; // see below diagram for meaning of the index to this;
  175. // 6 values if edge (!is_corner), 4 values if is_corner
  176. // legal numbers: 1..8 if edge, 1..4 if is_corner
  177. int num_vary_x; // additional number of variations along x axis in the template
  178. int num_vary_y; // additional number of variations along y axis in the template
  179. int corner_type_color_template[4][4];
  180. // if corner_type_color_template[s][t] is non-zero, then any
  181. // corner of type s generated as color t will get a little
  182. // corner sample markup in the template image data
  183. } stbhw_config;
  184. // computes the size needed for the template image
  185. STBHW_EXTERN void stbhw_get_template_size(stbhw_config *c, int *w, int *h);
  186. // generates a template image, assuming data is 3*w*h bytes long, RGB format
  187. STBHW_EXTERN int stbhw_make_template(stbhw_config *c, unsigned char *data, int w, int h, int stride_in_bytes);
  188. #endif//INCLUDE_STB_HWANG_H
  189. // TILE CONSTRAINT TYPES
  190. //
  191. // there are 4 "types" of corners and 6 types of edges.
  192. // you can configure the tileset to have different numbers
  193. // of colors for each type of color or edge.
  194. //
  195. // corner types:
  196. //
  197. // 0---*---1---*---2---*---3
  198. // | | |
  199. // * * *
  200. // | | |
  201. // 1---*---2---*---3 0---*---1---*---2
  202. // | | |
  203. // * * *
  204. // | | |
  205. // 0---*---1---*---2---*---3
  206. //
  207. //
  208. // edge types:
  209. //
  210. // *---2---*---3---* *---0---*
  211. // | | | |
  212. // 1 4 5 1
  213. // | | | |
  214. // *---0---*---2---* * *
  215. // | |
  216. // 4 5
  217. // | |
  218. // *---3---*
  219. //
  220. // TILE CONSTRAINTS
  221. //
  222. // each corner/edge has a color; this shows the name
  223. // of the variable containing the color
  224. //
  225. // corner constraints:
  226. //
  227. // a---*---d
  228. // | |
  229. // * *
  230. // | |
  231. // a---*---b---*---c b e
  232. // | | | |
  233. // * * * *
  234. // | | | |
  235. // d---*---e---*---f c---*---f
  236. //
  237. //
  238. // edge constraints:
  239. //
  240. // *---a---*---b---* *---a---*
  241. // | | | |
  242. // c d b c
  243. // | | | |
  244. // *---e---*---f---* * *
  245. // | |
  246. // d e
  247. // | |
  248. // *---f---*
  249. //
  250. //////////////////////////////////////////////////////////////////////////////
  251. // //
  252. // IMPLEMENTATION SECTION //
  253. // //
  254. #ifdef STB_HERRINGBONE_WANG_TILE_IMPLEMENTATION
  255. #include <string.h> // memcpy
  256. #include <stdlib.h> // malloc
  257. #ifndef STB_HBWANG_RAND
  258. #include <stdlib.h>
  259. #define STB_HBWANG_RAND() (rand() >> 4)
  260. #endif
  261. #ifndef STB_HBWANG_ASSERT
  262. #include <assert.h>
  263. #define STB_HBWANG_ASSERT(x) assert(x)
  264. #endif
  265. // map size
  266. #ifndef STB_HBWANG_MAX_X
  267. #define STB_HBWANG_MAX_X 100
  268. #endif
  269. #ifndef STB_HBWANG_MAX_Y
  270. #define STB_HBWANG_MAX_Y 100
  271. #endif
  272. // global variables for color assignments
  273. // @MEMORY change these to just store last two/three rows
  274. // and keep them on the stack
  275. static signed char c_color[STB_HBWANG_MAX_Y+6][STB_HBWANG_MAX_X+6];
  276. static signed char v_color[STB_HBWANG_MAX_Y+6][STB_HBWANG_MAX_X+5];
  277. static signed char h_color[STB_HBWANG_MAX_Y+5][STB_HBWANG_MAX_X+6];
  278. static char *stbhw_error;
  279. STBHW_EXTERN char *stbhw_get_last_error(void)
  280. {
  281. char *temp = stbhw_error;
  282. stbhw_error = 0;
  283. return temp;
  284. }
  285. /////////////////////////////////////////////////////////////
  286. //
  287. // SHARED TEMPLATE-DESCRIPTION CODE
  288. //
  289. // Used by both template generator and tileset parser; by
  290. // using the same code, they are locked in sync and we don't
  291. // need to try to do more sophisticated parsing of edge color
  292. // markup or something.
  293. typedef void stbhw__process_rect(struct stbhw__process *p, int xpos, int ypos,
  294. int a, int b, int c, int d, int e, int f);
  295. typedef struct stbhw__process
  296. {
  297. stbhw_tileset *ts;
  298. stbhw_config *c;
  299. stbhw__process_rect *process_h_rect;
  300. stbhw__process_rect *process_v_rect;
  301. unsigned char *data;
  302. int stride,w,h;
  303. } stbhw__process;
  304. static void stbhw__process_h_row(stbhw__process *p,
  305. int xpos, int ypos,
  306. int a0, int a1,
  307. int b0, int b1,
  308. int c0, int c1,
  309. int d0, int d1,
  310. int e0, int e1,
  311. int f0, int f1,
  312. int variants)
  313. {
  314. int a,b,c,d,e,f,v;
  315. for (v=0; v < variants; ++v)
  316. for (f=f0; f <= f1; ++f)
  317. for (e=e0; e <= e1; ++e)
  318. for (d=d0; d <= d1; ++d)
  319. for (c=c0; c <= c1; ++c)
  320. for (b=b0; b <= b1; ++b)
  321. for (a=a0; a <= a1; ++a) {
  322. p->process_h_rect(p, xpos, ypos, a,b,c,d,e,f);
  323. xpos += 2*p->c->short_side_len + 3;
  324. }
  325. }
  326. static void stbhw__process_v_row(stbhw__process *p,
  327. int xpos, int ypos,
  328. int a0, int a1,
  329. int b0, int b1,
  330. int c0, int c1,
  331. int d0, int d1,
  332. int e0, int e1,
  333. int f0, int f1,
  334. int variants)
  335. {
  336. int a,b,c,d,e,f,v;
  337. for (v=0; v < variants; ++v)
  338. for (f=f0; f <= f1; ++f)
  339. for (e=e0; e <= e1; ++e)
  340. for (d=d0; d <= d1; ++d)
  341. for (c=c0; c <= c1; ++c)
  342. for (b=b0; b <= b1; ++b)
  343. for (a=a0; a <= a1; ++a) {
  344. p->process_v_rect(p, xpos, ypos, a,b,c,d,e,f);
  345. xpos += p->c->short_side_len+3;
  346. }
  347. }
  348. static void stbhw__get_template_info(stbhw_config *c, int *w, int *h, int *h_count, int *v_count)
  349. {
  350. int size_x,size_y;
  351. int horz_count,vert_count;
  352. if (c->is_corner) {
  353. int horz_w = c->num_color[1] * c->num_color[2] * c->num_color[3] * c->num_vary_x;
  354. int horz_h = c->num_color[0] * c->num_color[1] * c->num_color[2] * c->num_vary_y;
  355. int vert_w = c->num_color[0] * c->num_color[3] * c->num_color[2] * c->num_vary_y;
  356. int vert_h = c->num_color[1] * c->num_color[0] * c->num_color[3] * c->num_vary_x;
  357. int horz_x = horz_w * (2*c->short_side_len + 3);
  358. int horz_y = horz_h * ( c->short_side_len + 3);
  359. int vert_x = vert_w * ( c->short_side_len + 3);
  360. int vert_y = vert_h * (2*c->short_side_len + 3);
  361. horz_count = horz_w * horz_h;
  362. vert_count = vert_w * vert_h;
  363. size_x = horz_x > vert_x ? horz_x : vert_x;
  364. size_y = 2 + horz_y + 2 + vert_y;
  365. } else {
  366. int horz_w = c->num_color[0] * c->num_color[1] * c->num_color[2] * c->num_vary_x;
  367. int horz_h = c->num_color[3] * c->num_color[4] * c->num_color[2] * c->num_vary_y;
  368. int vert_w = c->num_color[0] * c->num_color[5] * c->num_color[1] * c->num_vary_y;
  369. int vert_h = c->num_color[3] * c->num_color[4] * c->num_color[5] * c->num_vary_x;
  370. int horz_x = horz_w * (2*c->short_side_len + 3);
  371. int horz_y = horz_h * ( c->short_side_len + 3);
  372. int vert_x = vert_w * ( c->short_side_len + 3);
  373. int vert_y = vert_h * (2*c->short_side_len + 3);
  374. horz_count = horz_w * horz_h;
  375. vert_count = vert_w * vert_h;
  376. size_x = horz_x > vert_x ? horz_x : vert_x;
  377. size_y = 2 + horz_y + 2 + vert_y;
  378. }
  379. if (w) *w = size_x;
  380. if (h) *h = size_y;
  381. if (h_count) *h_count = horz_count;
  382. if (v_count) *v_count = vert_count;
  383. }
  384. STBHW_EXTERN void stbhw_get_template_size(stbhw_config *c, int *w, int *h)
  385. {
  386. stbhw__get_template_info(c, w, h, NULL, NULL);
  387. }
  388. static int stbhw__process_template(stbhw__process *p)
  389. {
  390. int i,j,k,q, ypos;
  391. int size_x, size_y;
  392. stbhw_config *c = p->c;
  393. stbhw__get_template_info(c, &size_x, &size_y, NULL, NULL);
  394. if (p->w < size_x || p->h < size_y) {
  395. stbhw_error = "image too small for configuration";
  396. return 0;
  397. }
  398. if (c->is_corner) {
  399. ypos = 2;
  400. for (k=0; k < c->num_color[2]; ++k) {
  401. for (j=0; j < c->num_color[1]; ++j) {
  402. for (i=0; i < c->num_color[0]; ++i) {
  403. for (q=0; q < c->num_vary_y; ++q) {
  404. stbhw__process_h_row(p, 0,ypos,
  405. 0,c->num_color[1]-1, 0,c->num_color[2]-1, 0,c->num_color[3]-1,
  406. i,i, j,j, k,k,
  407. c->num_vary_x);
  408. ypos += c->short_side_len + 3;
  409. }
  410. }
  411. }
  412. }
  413. ypos += 2;
  414. for (k=0; k < c->num_color[3]; ++k) {
  415. for (j=0; j < c->num_color[0]; ++j) {
  416. for (i=0; i < c->num_color[1]; ++i) {
  417. for (q=0; q < c->num_vary_x; ++q) {
  418. stbhw__process_v_row(p, 0,ypos,
  419. 0,c->num_color[0]-1, 0,c->num_color[3]-1, 0,c->num_color[2]-1,
  420. i,i, j,j, k,k,
  421. c->num_vary_y);
  422. ypos += (c->short_side_len*2) + 3;
  423. }
  424. }
  425. }
  426. }
  427. assert(ypos == size_y);
  428. } else {
  429. ypos = 2;
  430. for (k=0; k < c->num_color[3]; ++k) {
  431. for (j=0; j < c->num_color[4]; ++j) {
  432. for (i=0; i < c->num_color[2]; ++i) {
  433. for (q=0; q < c->num_vary_y; ++q) {
  434. stbhw__process_h_row(p, 0,ypos,
  435. 0,c->num_color[2]-1, k,k,
  436. 0,c->num_color[1]-1, j,j,
  437. 0,c->num_color[0]-1, i,i,
  438. c->num_vary_x);
  439. ypos += c->short_side_len + 3;
  440. }
  441. }
  442. }
  443. }
  444. ypos += 2;
  445. for (k=0; k < c->num_color[3]; ++k) {
  446. for (j=0; j < c->num_color[4]; ++j) {
  447. for (i=0; i < c->num_color[5]; ++i) {
  448. for (q=0; q < c->num_vary_x; ++q) {
  449. stbhw__process_v_row(p, 0,ypos,
  450. 0,c->num_color[0]-1, i,i,
  451. 0,c->num_color[1]-1, j,j,
  452. 0,c->num_color[5]-1, k,k,
  453. c->num_vary_y);
  454. ypos += (c->short_side_len*2) + 3;
  455. }
  456. }
  457. }
  458. }
  459. assert(ypos == size_y);
  460. }
  461. return 1;
  462. }
  463. /////////////////////////////////////////////////////////////
  464. //
  465. // MAP GENERATOR
  466. //
  467. static void stbhw__draw_pixel(unsigned char *output, int stride, int x, int y, unsigned char c[3])
  468. {
  469. memcpy(output + y*stride + x*3, c, 3);
  470. }
  471. static void stbhw__draw_h_tile(unsigned char *output, int stride, int xmax, int ymax, int x, int y, stbhw_tile *h, int sz)
  472. {
  473. int i,j;
  474. for (j=0; j < sz; ++j)
  475. if (y+j >= 0 && y+j < ymax)
  476. for (i=0; i < sz*2; ++i)
  477. if (x+i >= 0 && x+i < xmax)
  478. stbhw__draw_pixel(output,stride, x+i,y+j, &h->pixels[(j*sz*2 + i)*3]);
  479. }
  480. static void stbhw__draw_v_tile(unsigned char *output, int stride, int xmax, int ymax, int x, int y, stbhw_tile *h, int sz)
  481. {
  482. int i,j;
  483. for (j=0; j < sz*2; ++j)
  484. if (y+j >= 0 && y+j < ymax)
  485. for (i=0; i < sz; ++i)
  486. if (x+i >= 0 && x+i < xmax)
  487. stbhw__draw_pixel(output,stride, x+i,y+j, &h->pixels[(j*sz + i)*3]);
  488. }
  489. // randomly choose a tile that fits constraints for a given spot, and update the constraints
  490. static stbhw_tile * stbhw__choose_tile(stbhw_tile **list, int numlist,
  491. signed char *a, signed char *b, signed char *c,
  492. signed char *d, signed char *e, signed char *f,
  493. int **weighting)
  494. {
  495. int i,n,m = 1<<30,pass;
  496. for (pass=0; pass < 2; ++pass) {
  497. n=0;
  498. // pass #1:
  499. // count number of variants that match this partial set of constraints
  500. // pass #2:
  501. // stop on randomly selected match
  502. for (i=0; i < numlist; ++i) {
  503. stbhw_tile *h = list[i];
  504. if ((*a < 0 || *a == h->a) &&
  505. (*b < 0 || *b == h->b) &&
  506. (*c < 0 || *c == h->c) &&
  507. (*d < 0 || *d == h->d) &&
  508. (*e < 0 || *e == h->e) &&
  509. (*f < 0 || *f == h->f)) {
  510. if (weighting)
  511. n += weighting[0][i];
  512. else
  513. n += 1;
  514. if (n > m) {
  515. // use list[i]
  516. // update constraints to reflect what we placed
  517. *a = h->a;
  518. *b = h->b;
  519. *c = h->c;
  520. *d = h->d;
  521. *e = h->e;
  522. *f = h->f;
  523. return h;
  524. }
  525. }
  526. }
  527. if (n == 0) {
  528. stbhw_error = "couldn't find tile matching constraints";
  529. return NULL;
  530. }
  531. m = STB_HBWANG_RAND() % n;
  532. }
  533. STB_HBWANG_ASSERT(0);
  534. return NULL;
  535. }
  536. static int stbhw__match(int x, int y)
  537. {
  538. return c_color[y][x] == c_color[y+1][x+1];
  539. }
  540. static int stbhw__weighted(int num_options, int *weights)
  541. {
  542. int k, total, choice;
  543. total = 0;
  544. for (k=0; k < num_options; ++k)
  545. total += weights[k];
  546. choice = STB_HBWANG_RAND() % total;
  547. total = 0;
  548. for (k=0; k < num_options; ++k) {
  549. total += weights[k];
  550. if (choice < total)
  551. break;
  552. }
  553. STB_HBWANG_ASSERT(k < num_options);
  554. return k;
  555. }
  556. static int stbhw__change_color(int old_color, int num_options, int *weights)
  557. {
  558. if (weights) {
  559. int k, total, choice;
  560. total = 0;
  561. for (k=0; k < num_options; ++k)
  562. if (k != old_color)
  563. total += weights[k];
  564. choice = STB_HBWANG_RAND() % total;
  565. total = 0;
  566. for (k=0; k < num_options; ++k) {
  567. if (k != old_color) {
  568. total += weights[k];
  569. if (choice < total)
  570. break;
  571. }
  572. }
  573. STB_HBWANG_ASSERT(k < num_options);
  574. return k;
  575. } else {
  576. int offset = 1+STB_HBWANG_RAND() % (num_options-1);
  577. return (old_color+offset) % num_options;
  578. }
  579. }
  580. // generate a map that is w * h pixels (3-bytes each)
  581. // returns 1 on success, 0 on error
  582. STBHW_EXTERN int stbhw_generate_image(stbhw_tileset *ts, int **weighting, unsigned char *output, int stride, int w, int h)
  583. {
  584. int sidelen = ts->short_side_len;
  585. int xmax = (w / sidelen) + 6;
  586. int ymax = (h / sidelen) + 6;
  587. if (xmax > STB_HBWANG_MAX_X+6 || ymax > STB_HBWANG_MAX_Y+6) {
  588. stbhw_error = "increase STB_HBWANG_MAX_X/Y";
  589. return 0;
  590. }
  591. if (ts->is_corner) {
  592. int i,j, ypos;
  593. int *cc = ts->num_color;
  594. for (j=0; j < ymax; ++j) {
  595. for (i=0; i < xmax; ++i) {
  596. int p = (i-j+1)&3; // corner type
  597. if (weighting==NULL || weighting[p]==0 || cc[p] == 1)
  598. c_color[j][i] = STB_HBWANG_RAND() % cc[p];
  599. else
  600. c_color[j][i] = stbhw__weighted(cc[p], weighting[p]);
  601. }
  602. }
  603. #ifndef STB_HBWANG_NO_REPITITION_REDUCTION
  604. // now go back through and make sure we don't have adjancent 3x2 vertices that are identical,
  605. // to avoid really obvious repetition (which happens easily with extreme weights)
  606. for (j=0; j < ymax-3; ++j) {
  607. for (i=0; i < xmax-3; ++i) {
  608. int p = (i-j+1) & 3; // corner type
  609. STB_HBWANG_ASSERT(i+3 < STB_HBWANG_MAX_X+6);
  610. STB_HBWANG_ASSERT(j+3 < STB_HBWANG_MAX_Y+6);
  611. if (stbhw__match(i,j) && stbhw__match(i,j+1) && stbhw__match(i,j+2)
  612. && stbhw__match(i+1,j) && stbhw__match(i+1,j+1) && stbhw__match(i+1,j+2)) {
  613. int p = ((i+1)-(j+1)+1) & 3;
  614. if (cc[p] > 1)
  615. c_color[j+1][i+1] = stbhw__change_color(c_color[j+1][i+1], cc[p], weighting ? weighting[p] : NULL);
  616. }
  617. if (stbhw__match(i,j) && stbhw__match(i+1,j) && stbhw__match(i+2,j)
  618. && stbhw__match(i,j+1) && stbhw__match(i+1,j+1) && stbhw__match(i+2,j+1)) {
  619. int p = ((i+2)-(j+1)+1) & 3;
  620. if (cc[p] > 1)
  621. c_color[j+1][i+2] = stbhw__change_color(c_color[j+1][i+2], cc[p], weighting ? weighting[p] : NULL);
  622. }
  623. }
  624. }
  625. #endif
  626. ypos = -1 * sidelen;
  627. for (j = -1; ypos < h; ++j) {
  628. // a general herringbone row consists of:
  629. // horizontal left block, the bottom of a previous vertical, the top of a new vertical
  630. int phase = (j & 3);
  631. // displace horizontally according to pattern
  632. if (phase == 0) {
  633. i = 0;
  634. } else {
  635. i = phase-4;
  636. }
  637. for (i;; i += 4) {
  638. int xpos = i * sidelen;
  639. if (xpos >= w)
  640. break;
  641. // horizontal left-block
  642. if (xpos + sidelen*2 >= 0 && ypos >= 0) {
  643. stbhw_tile *t = stbhw__choose_tile(
  644. ts->h_tiles, ts->num_h_tiles,
  645. &c_color[j+2][i+2], &c_color[j+2][i+3], &c_color[j+2][i+4],
  646. &c_color[j+3][i+2], &c_color[j+3][i+3], &c_color[j+3][i+4],
  647. weighting
  648. );
  649. if (t == NULL)
  650. return 0;
  651. stbhw__draw_h_tile(output,stride,w,h, xpos, ypos, t, sidelen);
  652. }
  653. xpos += sidelen * 2;
  654. // now we're at the end of a previous vertical one
  655. xpos += sidelen;
  656. // now we're at the start of a new vertical one
  657. if (xpos < w) {
  658. stbhw_tile *t = stbhw__choose_tile(
  659. ts->v_tiles, ts->num_v_tiles,
  660. &c_color[j+2][i+5], &c_color[j+3][i+5], &c_color[j+4][i+5],
  661. &c_color[j+2][i+6], &c_color[j+3][i+6], &c_color[j+4][i+6],
  662. weighting
  663. );
  664. if (t == NULL)
  665. return 0;
  666. stbhw__draw_v_tile(output,stride,w,h, xpos, ypos, t, sidelen);
  667. }
  668. }
  669. ypos += sidelen;
  670. }
  671. } else {
  672. // @TODO edge-color repetition reduction
  673. int i,j, ypos;
  674. memset(v_color, -1, sizeof(v_color));
  675. memset(h_color, -1, sizeof(h_color));
  676. ypos = -1 * sidelen;
  677. for (j = -1; ypos<h; ++j) {
  678. // a general herringbone row consists of:
  679. // horizontal left block, the bottom of a previous vertical, the top of a new vertical
  680. int phase = (j & 3);
  681. // displace horizontally according to pattern
  682. if (phase == 0) {
  683. i = 0;
  684. } else {
  685. i = phase-4;
  686. }
  687. for (i;; i += 4) {
  688. int xpos = i * sidelen;
  689. if (xpos >= w)
  690. break;
  691. // horizontal left-block
  692. if (xpos + sidelen*2 >= 0 && ypos >= 0) {
  693. stbhw_tile *t = stbhw__choose_tile(
  694. ts->h_tiles, ts->num_h_tiles,
  695. &h_color[j+2][i+2], &h_color[j+2][i+3],
  696. &v_color[j+2][i+2], &v_color[j+2][i+4],
  697. &h_color[j+3][i+2], &h_color[j+3][i+3],
  698. weighting
  699. );
  700. if (t == NULL) return 0;
  701. stbhw__draw_h_tile(output,stride,w,h, xpos, ypos, t, sidelen);
  702. }
  703. xpos += sidelen * 2;
  704. // now we're at the end of a previous vertical one
  705. xpos += sidelen;
  706. // now we're at the start of a new vertical one
  707. if (xpos < w) {
  708. stbhw_tile *t = stbhw__choose_tile(
  709. ts->v_tiles, ts->num_v_tiles,
  710. &h_color[j+2][i+5],
  711. &v_color[j+2][i+5], &v_color[j+2][i+6],
  712. &v_color[j+3][i+5], &v_color[j+3][i+6],
  713. &h_color[j+4][i+5],
  714. weighting
  715. );
  716. if (t == NULL) return 0;
  717. stbhw__draw_v_tile(output,stride,w,h, xpos, ypos, t, sidelen);
  718. }
  719. }
  720. ypos += sidelen;
  721. }
  722. }
  723. return 1;
  724. }
  725. static void stbhw__parse_h_rect(stbhw__process *p, int xpos, int ypos,
  726. int a, int b, int c, int d, int e, int f)
  727. {
  728. int len = p->c->short_side_len;
  729. stbhw_tile *h = (stbhw_tile *) malloc(sizeof(*h)-1 + 3 * (len*2) * len);
  730. int i,j;
  731. ++xpos;
  732. ++ypos;
  733. h->a = a, h->b = b, h->c = c, h->d = d, h->e = e, h->f = f;
  734. for (j=0; j < len; ++j)
  735. for (i=0; i < len*2; ++i)
  736. memcpy(h->pixels + j*(3*len*2) + i*3, p->data+(ypos+j)*p->stride+(xpos+i)*3, 3);
  737. STB_HBWANG_ASSERT(p->ts->num_h_tiles < p->ts->max_h_tiles);
  738. p->ts->h_tiles[p->ts->num_h_tiles++] = h;
  739. }
  740. static void stbhw__parse_v_rect(stbhw__process *p, int xpos, int ypos,
  741. int a, int b, int c, int d, int e, int f)
  742. {
  743. int len = p->c->short_side_len;
  744. stbhw_tile *h = (stbhw_tile *) malloc(sizeof(*h)-1 + 3 * (len*2) * len);
  745. int i,j;
  746. ++xpos;
  747. ++ypos;
  748. h->a = a, h->b = b, h->c = c, h->d = d, h->e = e, h->f = f;
  749. for (j=0; j < len*2; ++j)
  750. for (i=0; i < len; ++i)
  751. memcpy(h->pixels + j*(3*len) + i*3, p->data+(ypos+j)*p->stride+(xpos+i)*3, 3);
  752. STB_HBWANG_ASSERT(p->ts->num_v_tiles < p->ts->max_v_tiles);
  753. p->ts->v_tiles[p->ts->num_v_tiles++] = h;
  754. }
  755. STBHW_EXTERN int stbhw_build_tileset_from_image(stbhw_tileset *ts, unsigned char *data, int stride, int w, int h)
  756. {
  757. int i, h_count, v_count;
  758. unsigned char header[9];
  759. stbhw_config c = { 0 };
  760. stbhw__process p = { 0 };
  761. // extract binary header
  762. // remove encoding that makes it more visually obvious it encodes actual data
  763. for (i=0; i < 9; ++i)
  764. header[i] = data[w*3 - 1 - i] ^ (i*55);
  765. // extract header info
  766. if (header[7] == 0xc0) {
  767. // corner-type
  768. c.is_corner = 1;
  769. for (i=0; i < 4; ++i)
  770. c.num_color[i] = header[i];
  771. c.num_vary_x = header[4];
  772. c.num_vary_y = header[5];
  773. c.short_side_len = header[6];
  774. } else {
  775. c.is_corner = 0;
  776. // edge-type
  777. for (i=0; i < 6; ++i)
  778. c.num_color[i] = header[i];
  779. c.num_vary_x = header[6];
  780. c.num_vary_y = header[7];
  781. c.short_side_len = header[8];
  782. }
  783. if (c.num_vary_x < 0 || c.num_vary_x > 64 || c.num_vary_y < 0 || c.num_vary_y > 64)
  784. return 0;
  785. if (c.short_side_len == 0)
  786. return 0;
  787. if (c.num_color[0] > 32 || c.num_color[1] > 32 || c.num_color[2] > 32 || c.num_color[3] > 32)
  788. return 0;
  789. stbhw__get_template_info(&c, NULL, NULL, &h_count, &v_count);
  790. ts->is_corner = c.is_corner;
  791. ts->short_side_len = c.short_side_len;
  792. memcpy(ts->num_color, c.num_color, sizeof(ts->num_color));
  793. ts->max_h_tiles = h_count;
  794. ts->max_v_tiles = v_count;
  795. ts->num_h_tiles = ts->num_v_tiles = 0;
  796. ts->h_tiles = (stbhw_tile **) malloc(sizeof(*ts->h_tiles) * h_count);
  797. ts->v_tiles = (stbhw_tile **) malloc(sizeof(*ts->v_tiles) * v_count);
  798. p.ts = ts;
  799. p.data = data;
  800. p.stride = stride;
  801. p.process_h_rect = stbhw__parse_h_rect;
  802. p.process_v_rect = stbhw__parse_v_rect;
  803. p.w = w;
  804. p.h = h;
  805. p.c = &c;
  806. // load all the tiles out of the image
  807. return stbhw__process_template(&p);
  808. }
  809. STBHW_EXTERN void stbhw_free_tileset(stbhw_tileset *ts)
  810. {
  811. int i;
  812. for (i=0; i < ts->num_h_tiles; ++i)
  813. free(ts->h_tiles[i]);
  814. for (i=0; i < ts->num_v_tiles; ++i)
  815. free(ts->v_tiles[i]);
  816. free(ts->h_tiles);
  817. free(ts->v_tiles);
  818. ts->h_tiles = NULL;
  819. ts->v_tiles = NULL;
  820. ts->num_h_tiles = ts->max_h_tiles = 0;
  821. ts->num_v_tiles = ts->max_v_tiles = 0;
  822. }
  823. //////////////////////////////////////////////////////////////////////////////
  824. //
  825. // GENERATOR
  826. //
  827. //
  828. // shared code
  829. static void stbhw__set_pixel(unsigned char *data, int stride, int xpos, int ypos, unsigned char color[3])
  830. {
  831. memcpy(data + ypos*stride + xpos*3, color, 3);
  832. }
  833. static void stbhw__stbhw__set_pixel_whiten(unsigned char *data, int stride, int xpos, int ypos, unsigned char color[3])
  834. {
  835. unsigned char c2[3];
  836. int i;
  837. for (i=0; i < 3; ++i)
  838. c2[i] = (color[i]*2 + 255)/3;
  839. memcpy(data + ypos*stride + xpos*3, c2, 3);
  840. }
  841. static unsigned char stbhw__black[3] = { 0,0,0 };
  842. // each edge set gets its own unique color variants
  843. // used http://phrogz.net/css/distinct-colors.html to generate this set,
  844. // but it's not very good and needs to be revised
  845. static unsigned char stbhw__color[7][8][3] =
  846. {
  847. { {255,51,51} , {143,143,29}, {0,199,199}, {159,119,199}, {0,149,199} , {143, 0,143}, {255,128,0}, {64,255,0}, },
  848. { {235,255,30 }, {255,0,255}, {199,139,119}, {29,143, 57}, {143,0,71} , { 0,143,143}, {0,99,199}, {143,71,0}, },
  849. { {0,149,199} , {143, 0,143}, {255,128,0}, {64,255,0}, {255,191,0} , {51,255,153}, {0,0,143}, {199,119,159},},
  850. { {143,0,71} , { 0,143,143}, {0,99,199}, {143,71,0}, {255,190,153}, { 0,255,255}, {128,0,255}, {255,51,102},},
  851. { {255,191,0} , {51,255,153}, {0,0,143}, {199,119,159}, {255,51,51} , {143,143,29}, {0,199,199}, {159,119,199},},
  852. { {255,190,153}, { 0,255,255}, {128,0,255}, {255,51,102}, {235,255,30 }, {255,0,255}, {199,139,119}, {29,143, 57}, },
  853. { {40,40,40 }, { 90,90,90 }, { 150,150,150 }, { 200,200,200 },
  854. { 255,90,90 }, { 160,160,80}, { 50,150,150 }, { 200,50,200 } },
  855. };
  856. static void stbhw__draw_hline(unsigned char *data, int stride, int xpos, int ypos, int color, int len, int slot)
  857. {
  858. int i;
  859. int j = len * 6 / 16;
  860. int k = len * 10 / 16;
  861. for (i=0; i < len; ++i)
  862. stbhw__set_pixel(data, stride, xpos+i, ypos, stbhw__black);
  863. if (k-j < 2) {
  864. j = len/2 - 1;
  865. k = j+2;
  866. if (len & 1)
  867. ++k;
  868. }
  869. for (i=j; i < k; ++i)
  870. stbhw__stbhw__set_pixel_whiten(data, stride, xpos+i, ypos, stbhw__color[slot][color]);
  871. }
  872. static void stbhw__draw_vline(unsigned char *data, int stride, int xpos, int ypos, int color, int len, int slot)
  873. {
  874. int i;
  875. int j = len * 6 / 16;
  876. int k = len * 10 / 16;
  877. for (i=0; i < len; ++i)
  878. stbhw__set_pixel(data, stride, xpos, ypos+i, stbhw__black);
  879. if (k-j < 2) {
  880. j = len/2 - 1;
  881. k = j+2;
  882. if (len & 1)
  883. ++k;
  884. }
  885. for (i=j; i < k; ++i)
  886. stbhw__stbhw__set_pixel_whiten(data, stride, xpos, ypos+i, stbhw__color[slot][color]);
  887. }
  888. // 0--*--1--*--2--*--3
  889. // | | |
  890. // * * *
  891. // | | |
  892. // 1--*--2--*--3 0--*--1--*--2
  893. // | | |
  894. // * * *
  895. // | | |
  896. // 0--*--1--*--2--*--3
  897. //
  898. // variables while enumerating (no correspondence between corners
  899. // of the types is implied by these variables)
  900. //
  901. // a-----b-----c a-----d
  902. // | | | |
  903. // | | | |
  904. // | | | |
  905. // d-----e-----f b e
  906. // | |
  907. // | |
  908. // | |
  909. // c-----f
  910. //
  911. unsigned char stbhw__corner_colors[4][4][3] =
  912. {
  913. { { 255,0,0 }, { 200,200,200 }, { 100,100,200 }, { 255,200,150 }, },
  914. { { 0,0,255 }, { 255,255,0 }, { 100,200,100 }, { 150,255,200 }, },
  915. { { 255,0,255 }, { 80,80,80 }, { 200,100,100 }, { 200,150,255 }, },
  916. { { 0,255,255 }, { 0,255,0 }, { 200,120,200 }, { 255,200,200 }, },
  917. };
  918. int stbhw__corner_colors_to_edge_color[4][4] =
  919. {
  920. // 0 1 2 3
  921. { 0, 1, 4, 9, }, // 0
  922. { 2, 3, 5, 10, }, // 1
  923. { 6, 7, 8, 11, }, // 2
  924. { 12, 13, 14, 15, }, // 3
  925. };
  926. #define stbhw__c2e stbhw__corner_colors_to_edge_color
  927. static void stbhw__draw_clipped_corner(unsigned char *data, int stride, int xpos, int ypos, int w, int h, int x, int y)
  928. {
  929. static unsigned char template_color[3] = { 167,204,204 };
  930. int i,j;
  931. for (j = -2; j <= 1; ++j) {
  932. for (i = -2; i <= 1; ++i) {
  933. if ((i == -2 || i == 1) && (j == -2 || j == 1))
  934. continue;
  935. else {
  936. if (x+i < 1 || x+i > w) continue;
  937. if (y+j < 1 || y+j > h) continue;
  938. stbhw__set_pixel(data, stride, xpos+x+i, ypos+y+j, template_color);
  939. }
  940. }
  941. }
  942. }
  943. static void stbhw__edge_process_h_rect(stbhw__process *p, int xpos, int ypos,
  944. int a, int b, int c, int d, int e, int f)
  945. {
  946. int len = p->c->short_side_len;
  947. stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos , a, len, 2);
  948. stbhw__draw_hline(p->data, p->stride, xpos+ len+1 , ypos , b, len, 3);
  949. stbhw__draw_vline(p->data, p->stride, xpos , ypos+1 , c, len, 1);
  950. stbhw__draw_vline(p->data, p->stride, xpos+2*len+1 , ypos+1 , d, len, 4);
  951. stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos + len+1, e, len, 0);
  952. stbhw__draw_hline(p->data, p->stride, xpos + len+1 , ypos + len+1, f, len, 2);
  953. }
  954. static void stbhw__edge_process_v_rect(stbhw__process *p, int xpos, int ypos,
  955. int a, int b, int c, int d, int e, int f)
  956. {
  957. int len = p->c->short_side_len;
  958. stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos , a, len, 0);
  959. stbhw__draw_vline(p->data, p->stride, xpos , ypos+1 , b, len, 5);
  960. stbhw__draw_vline(p->data, p->stride, xpos + len+1, ypos+1 , c, len, 1);
  961. stbhw__draw_vline(p->data, p->stride, xpos , ypos + len+1, d, len, 4);
  962. stbhw__draw_vline(p->data, p->stride, xpos + len+1, ypos + len+1, e, len, 5);
  963. stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos + 2*len+1, f, len, 3);
  964. }
  965. static void stbhw__corner_process_h_rect(stbhw__process *p, int xpos, int ypos,
  966. int a, int b, int c, int d, int e, int f)
  967. {
  968. int len = p->c->short_side_len;
  969. stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos , stbhw__c2e[a][b], len, 2);
  970. stbhw__draw_hline(p->data, p->stride, xpos+ len+1 , ypos , stbhw__c2e[b][c], len, 3);
  971. stbhw__draw_vline(p->data, p->stride, xpos , ypos+1 , stbhw__c2e[a][d], len, 1);
  972. stbhw__draw_vline(p->data, p->stride, xpos+2*len+1 , ypos+1 , stbhw__c2e[c][f], len, 4);
  973. stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos + len+1, stbhw__c2e[d][e], len, 0);
  974. stbhw__draw_hline(p->data, p->stride, xpos + len+1 , ypos + len+1, stbhw__c2e[e][f], len, 2);
  975. if (p->c->corner_type_color_template[1][a]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, 1,1);
  976. if (p->c->corner_type_color_template[2][b]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, len+1,1);
  977. if (p->c->corner_type_color_template[3][c]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, len*2+1,1);
  978. if (p->c->corner_type_color_template[0][d]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, 1,len+1);
  979. if (p->c->corner_type_color_template[1][e]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, len+1,len+1);
  980. if (p->c->corner_type_color_template[2][f]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len*2,len, len*2+1,len+1);
  981. stbhw__set_pixel(p->data, p->stride, xpos , ypos, stbhw__corner_colors[1][a]);
  982. stbhw__set_pixel(p->data, p->stride, xpos+len , ypos, stbhw__corner_colors[2][b]);
  983. stbhw__set_pixel(p->data, p->stride, xpos+2*len+1, ypos, stbhw__corner_colors[3][c]);
  984. stbhw__set_pixel(p->data, p->stride, xpos , ypos+len+1, stbhw__corner_colors[0][d]);
  985. stbhw__set_pixel(p->data, p->stride, xpos+len , ypos+len+1, stbhw__corner_colors[1][e]);
  986. stbhw__set_pixel(p->data, p->stride, xpos+2*len+1, ypos+len+1, stbhw__corner_colors[2][f]);
  987. }
  988. static void stbhw__corner_process_v_rect(stbhw__process *p, int xpos, int ypos,
  989. int a, int b, int c, int d, int e, int f)
  990. {
  991. int len = p->c->short_side_len;
  992. stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos , stbhw__c2e[a][d], len, 0);
  993. stbhw__draw_vline(p->data, p->stride, xpos , ypos+1 , stbhw__c2e[a][b], len, 5);
  994. stbhw__draw_vline(p->data, p->stride, xpos + len+1, ypos+1 , stbhw__c2e[d][e], len, 1);
  995. stbhw__draw_vline(p->data, p->stride, xpos , ypos + len+1, stbhw__c2e[b][c], len, 4);
  996. stbhw__draw_vline(p->data, p->stride, xpos + len+1, ypos + len+1, stbhw__c2e[e][f], len, 5);
  997. stbhw__draw_hline(p->data, p->stride, xpos+1 , ypos + 2*len+1, stbhw__c2e[c][f], len, 3);
  998. if (p->c->corner_type_color_template[0][a]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, 1,1);
  999. if (p->c->corner_type_color_template[3][b]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, 1,len+1);
  1000. if (p->c->corner_type_color_template[2][c]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, 1,len*2+1);
  1001. if (p->c->corner_type_color_template[1][d]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, len+1,1);
  1002. if (p->c->corner_type_color_template[0][e]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, len+1,len+1);
  1003. if (p->c->corner_type_color_template[3][f]) stbhw__draw_clipped_corner(p->data,p->stride, xpos,ypos, len,len*2, len+1,len*2+1);
  1004. stbhw__set_pixel(p->data, p->stride, xpos , ypos , stbhw__corner_colors[0][a]);
  1005. stbhw__set_pixel(p->data, p->stride, xpos , ypos+len , stbhw__corner_colors[3][b]);
  1006. stbhw__set_pixel(p->data, p->stride, xpos , ypos+2*len+1, stbhw__corner_colors[2][c]);
  1007. stbhw__set_pixel(p->data, p->stride, xpos+len+1, ypos , stbhw__corner_colors[1][d]);
  1008. stbhw__set_pixel(p->data, p->stride, xpos+len+1, ypos+len , stbhw__corner_colors[0][e]);
  1009. stbhw__set_pixel(p->data, p->stride, xpos+len+1, ypos+2*len+1, stbhw__corner_colors[3][f]);
  1010. }
  1011. // generates a template image, assuming data is 3*w*h bytes long, RGB format
  1012. STBHW_EXTERN int stbhw_make_template(stbhw_config *c, unsigned char *data, int w, int h, int stride_in_bytes)
  1013. {
  1014. stbhw__process p;
  1015. int i;
  1016. p.data = data;
  1017. p.w = w;
  1018. p.h = h;
  1019. p.stride = stride_in_bytes;
  1020. p.ts = 0;
  1021. p.c = c;
  1022. if (c->is_corner) {
  1023. p.process_h_rect = stbhw__corner_process_h_rect;
  1024. p.process_v_rect = stbhw__corner_process_v_rect;
  1025. } else {
  1026. p.process_h_rect = stbhw__edge_process_h_rect;
  1027. p.process_v_rect = stbhw__edge_process_v_rect;
  1028. }
  1029. for (i=0; i < p.h; ++i)
  1030. memset(p.data + i*p.stride, 255, 3*p.w);
  1031. if (!stbhw__process_template(&p))
  1032. return 0;
  1033. if (c->is_corner) {
  1034. // write out binary information in first line of image
  1035. for (i=0; i < 4; ++i)
  1036. data[w*3-1-i] = c->num_color[i];
  1037. data[w*3-1-i] = c->num_vary_x;
  1038. data[w*3-2-i] = c->num_vary_y;
  1039. data[w*3-3-i] = c->short_side_len;
  1040. data[w*3-4-i] = 0xc0;
  1041. } else {
  1042. for (i=0; i < 6; ++i)
  1043. data[w*3-1-i] = c->num_color[i];
  1044. data[w*3-1-i] = c->num_vary_x;
  1045. data[w*3-2-i] = c->num_vary_y;
  1046. data[w*3-3-i] = c->short_side_len;
  1047. }
  1048. // make it more obvious it encodes actual data
  1049. for (i=0; i < 9; ++i)
  1050. p.data[p.w*3 - 1 - i] ^= i*55;
  1051. return 1;
  1052. }
  1053. #endif // STB_HBWANG_IMPLEMENTATION