stb_herringbone_wang_tile.h 42 KB

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