-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwfc.h
1379 lines (1161 loc) · 39.3 KB
/
wfc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// wfc
//
// Single-file Wave Function Collapse library
//
// Author: Krystian Samp (samp.krystian at gmail.com)
// License: MIT
// Version: 0.7
//
// This is an early version that supports the overlapping WFC method.
// All feedback is very welcome and best sent by email. Thanks.
//
//
// HOW TO USE
// =============================================================================
//
// One file in your project should include wfc.h like this:
//
// #define WFC_IMPLEMENTATION
// #include "wfc.h"
//
// Other files can also include and use wfc.h but they shouldn't define
// WFC_IMPLEMENTATION macro.
//
// Usage:
//
// struct wfc *wfc = wfc_overlapping(
// 128, // Output image width in pixels
// 128, // Output image height in pixels
// input_image, // Input image that will be cut into tiles
// 3, // Tile width in pixels
// 3, // Tile height in pixels
// 1, // Expand input image on the right and bottom
// 1, // Add horizontal flips of all tiles
// 1, // Add vertical flips of all tiles
// 1 // Add n*90deg rotations of all tiles
// );
//
// wfc_run(wfc, -1); // Run Wave Function Collapse
// // -1 means no limit on iterations
// struct wfc_image *output_image = wfc_output_image(wfc);
// wfc_destroy(wfc);
// // use output_image->data
// // wfc_img_destroy(output_image);
//
// By default you work with struct wfc_image for inputs and outputs:
//
// struct wfc_image {
// unsigned char *data;
// int component_cnt;
// int width;
// int height;
// }
//
// Data is tightly packed without padding. Each pixel consists of
// component_cnt components (e.g., four components for rgba format).
// The output image will have the same number of components as the input
// image.
//
// wfc_run returns 0 if it cannot find a solution. You can try again like so:
//
// wfc_init(wfc);
// wfc_run(wfc, -1);
//
//
// Working with image files
// ----------------------------------------
//
// wfc can optionally use stb_image.h and stb_write.h to provide
// convenience functions for working directly with image files.
//
// You will normally place stb_image.h and stb_write.h in the same
// directory as wfc.h and include their implementations in one of the
// project files:
//
// #define STB_IMAGE_IMPLEMENTATION
// #define STB_IMAGE_WRITE_IMPLEMENTATION
// #include "stb_image.h"
// #include "stb_image_write.h"
//
// Further, you will instruct wfc.h to use stb:
//
// #define WFC_IMPLEMENTATION
// #define WFC_USE_STB
// #include "wfc.h"
//
// Usage:
//
// struct wfc_image *input_image = wfc_img_load("input.png");
// struct wfc *wfc = wfc_overlapping(
// ...
// input_image,
// ...
// );
//
// wfc_run(wfc, -1); // Run Wave Function Collapse
// // -1 means no restriction on number of iterations
// wfc_export(wfc, "output.png");
// wfc_img_destroy(input_image);
// wfc_destroy(wfc);
//
//
// Extra functions enabled by the inclusion of stb:
//
// struct wfc_image *image = wfc_img_load("image.png")
// wfc_img_save(image, "image.png")
// wfc_export(wfc, "output.png")
// wfc_export_tiles(wfc, "directory")
// // don't forget to wfc_img_destroy(image) loaded images
//
//
// THANKS
// =============================================================================
//
// Thanks for using wfc. If you find any bugs, have questions, or miss
// a feature please let me know. Also, if you'd like to share your works
// it's very appreciated. Please use my email at the top of the file.
//
#ifndef WFC_H
#define WFC_H
#ifdef __cplusplus
extern "C" {
#endif
struct wfc;
struct wfc_image {
unsigned char *data;
int component_cnt;
int width;
int height;
};
struct wfc *wfc_overlapping(int output_width, // Output width in pixels
int output_height, // Output height in pixels
struct wfc_image *image, // Input image to be cut into tiles
int tile_width, // Tile width in pixels
int tile_height, // Tile height in pixels
int expand_input, // Wrap input image on right and bottom
int xflip_tiles, // Add xflips of all tiles
int yflip_tiles, // Add yflips of all tiles
int rotate_tiles); // Add n*90deg rotations of all tiles
void wfc_init(struct wfc *wfc); // Resets wfc generation, wfc_run can be called again
int wfc_run(struct wfc *wfc, int max_collapse_cnt);
int wfc_export(struct wfc *wfc, const char *filename);
void wfc_destroy(struct wfc *wfc);
#ifdef __cplusplus
}
#endif
#endif // WFC_H
#if defined(WFC_USE_STB)
#undef STB_IMAGE_IMPLEMENTATION
#undef STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image.h"
#include "stb_image_write.h"
#endif
#ifdef WFC_IMPLEMENTATION
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>
#include <float.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <assert.h>
#define WFC_MAX_PROP_CNT 1000
#ifndef WFC_USE_STB
#define wfc_img_save(...) wfc__nofunc_int("wfc_img_save", "requires stb", __VA_ARGS__)
#define wfc_img_load(...) wfc__nofunc_ptr("wfc_img_load", "requires stb", __VA_ARGS__)
#define wfc_export(...) wfc__nofunc_int("wfc_export", "requires_stb", __VA_ARGS__)
#define wfc_export_tiles(...) wfc__nofunc_int("wfc_export_tiles", "requires_stb", __VA_ARGS__)
#endif
#if defined(WFC_DEBUG) || defined(WFC_TOOL)
#define print_progress(int_progress) \
do { \
printf("\rcells collapsed: %d", int_progress); \
fflush(stdout); \
} while(0)
#define print_endprogress() printf("\n")
#define wfcassert(test) assert(test)
#define p(...) printf(__VA_ARGS__)
#else
#define print_progress(int_progress)
#define print_endprogress()
#define wfcassert(test)
#define p(...)
#endif
enum wfc__direction {WFC_UP,WFC_DOWN,WFC_LEFT,WFC_RIGHT};
int directions[4] = {WFC_UP, WFC_DOWN, WFC_LEFT, WFC_RIGHT};
enum wfc__method {WFC_METHOD_OVERLAPPING, WFC_METHOD_TILED};
// Rules are stored in tiles
struct wfc__tile {
struct wfc_image *image;
int freq; // Relative frequency of the tile. Typically a
// count of tile occurrences in the input image.
// It affects the probability of the tile being
// selected when collapsing a cell.
};
struct wfc__cell {
int *tiles; // Possible tiles in the cell (initially all)
int tile_cnt;
int sum_freqs; // Sum of tile frequencies used to calculate
// entropy and randomly pick a tile when
// collapsing a tile.
double entropy; // Shannon entropy. Cell with the smallest entropy
// is picked to be collapsed next.
};
struct wfc__prop {
int src_cell_idx;
int dst_cell_idx;
enum wfc__direction direction;
};
// One structure for overlapping and tiled models
struct wfc {
enum wfc__method method; // overlapping or tiled?
unsigned int seed;
/* tiles */
struct wfc_image *image; // Input image, not always required
int tile_width; // Tile width in pixels
int tile_height; // Tile height in pixels
int expand_input;
int xflip_tiles;
int yflip_tiles;
int rotate_tiles;
struct wfc__tile *tiles; // All available tiles
int tile_cnt;
int sum_freqs;
/* output */
int output_width; // Output width in pixels
int output_height; // Output height in pixels
struct wfc__cell *cells; // One per output pixel
int cell_cnt; // width * height
/* in-use */
struct wfc__prop *props; // Propagation updates
int prop_cnt;
int prop_idx; // Current index into props, used to scan
// props forward to check whether a
// a candidate prop is already there
int collapsed_cell_cnt;
// These are the rules. A matrix lookup of allowed tile pairs in each
// direction. allowed_tiles[d][src_idx*tile_cnt + dst_idx] is 1 or 0
// depending on whether dst_idx tile can be placed next to the src_idx
// tile in the direction d.
//
// In the overlapping method tiles are allowed next to each other if
// their content overlaps, excluding the edges.
int *allowed_tiles[4];
};
#ifdef WFC_DEBUG
static const char *wfc__direction_strings[4] = {"up","down","left","right"};
static void wfc__print_prop(struct wfc__prop *p, const char *prefix)
{
printf("%s%d -> %s -> %d\n", prefix, p->src_cell_idx, direction_strings[p->direction], p->dst_cell_idx);
}
static void wfc__print_props(struct wfc__prop *p, int prop_cnt, const char *prefix)
{
for (int i=0; i<prop_cnt; i++) {
print_prop(&(p[i]), prefix);
}
}
#endif // WFC_DEBUG
////////////////////////////////////////////////////////////////////////////////
//
// Img helpers
//
////////////////////////////////////////////////////////////////////////////////
#ifdef WFC_USE_STB
// Return 0 on failure, non-0 on success
int wfc_img_save(struct wfc_image *image, const char *filename)
{
int len = strlen(filename);
char extension[5];
if (len < 4)
goto UNKNOWNFORMAT;
strcpy(extension, filename + len - 4);
for (int i=0; i<4; i++)
extension[i] = tolower(extension[i]);
if (strcmp(extension, ".png")==0)
return stbi_write_png(filename, image->width, image->height, image->component_cnt, image->data, image->width * image->component_cnt);
else if (strcmp(extension, ".bmp")==0)
return stbi_write_bmp(filename, image->width, image->height, image->component_cnt, image->data);
else if (strcmp(extension, ".tga")==0)
return stbi_write_tga(filename, image->width, image->height, image->component_cnt, image->data);
else if (strcmp(extension, ".jpg")==0)
return stbi_write_jpg(filename, image->width, image->height, image->component_cnt, image->data, 100);
UNKNOWNFORMAT:
printf("error: wfc_imgsave - unknown format (%s)\n", filename);
return 0;
}
// Return NULL on error
struct wfc_image *wfc_img_load(const char *filename)
{
struct wfc_image *image = malloc(sizeof(*image));
if (image == NULL) {
p("wfc_img_load: error\n");
return NULL;
}
image->data = stbi_load(filename, &image->width, &image->height, &image->component_cnt, 0);
if (image->data == NULL) {
p("wfc_img_load: error\n");
free(image);
return NULL;
}
return image;
}
#endif // WFC_USE_STB
// Return NULL on error
struct wfc_image *wfc_img_copy(struct wfc_image *image)
{
struct wfc_image *copy = malloc(sizeof(*copy));
if (copy == NULL) {
p("wfc_img_copy: error\n");
return NULL;
}
copy->width = image->width;
copy->height = image->height;
copy->component_cnt = image->component_cnt;
copy->data = malloc(sizeof(*copy->data) * copy->width*copy->height*copy->component_cnt);
if (copy->data == NULL) {
p("wfc_img_copy: error\n");
free(copy);
return NULL;
}
memcpy(copy->data, image->data, image->width*image->height*image->component_cnt);
return copy;
}
// Return NULL on error
struct wfc_image *wfc_img_create(int width, int height, int component_cnt)
{
struct wfc_image *image = malloc(sizeof(*image));
if (image == NULL) {
p("wfc_img_create: error\n");
return NULL;
}
image->width = width;
image->height = height;
image->component_cnt = component_cnt;
image->data = malloc(sizeof(*image->data) * width * height * component_cnt);
if (image->data == NULL) {
p("wfc_img_create: error\n");
free(image->data);
return NULL;
}
return image;
}
void wfc_img_destroy(struct wfc_image *image)
{
free(image != NULL ? image->data : NULL);
free(image);
}
// Return NULL on error
static struct wfc_image *wfc__img_expand(struct wfc_image *image, int xexp, int yexp)
{
struct wfc_image *exp_image = wfc_img_create(image->width + xexp, image->height + yexp, image->component_cnt);
if (exp_image == NULL) {
p("wfc__img_expand: error\n");
return NULL;
}
for (int y=0; y<exp_image->height; y++) {
memcpy(&exp_image->data[y * exp_image->width * image->component_cnt],
&image->data[(y % image->height) * image->width * image->component_cnt],
image->width * image->component_cnt);
memcpy(&exp_image->data[y * exp_image->width * image->component_cnt + image->width * image->component_cnt],
&image->data[(y % image->height) * image->width * image->component_cnt],
xexp * image->component_cnt);
}
return exp_image;
}
// Return 1 if the two images overlap perfectly except the edges in the given direction, 0 otherwise.
static int wfc__img_cmpoverlap(struct wfc_image *a, struct wfc_image *b, enum wfc__direction direction)
{
int a_offx, a_offy, b_offx, b_offy, width, height;
switch (direction) {
case WFC_UP:
a_offx = 0; a_offy = 0;
b_offx = 0; b_offy = 1;
width = a->width;
height = a->height-1;
break;
case WFC_DOWN:
a_offx = 0; a_offy = 1;
b_offx = 0; b_offy = 0;
width = a->width;
height = a->height-1;
break;
case WFC_LEFT:
a_offx = 0; a_offy = 0;
b_offx = 1; b_offy = 0;
width = a->width-1;
height = a->height;
break;
case WFC_RIGHT:
a_offx = 1; a_offy = 0;
b_offx = 0; b_offy = 0;
width = a->width-1;
height = a->height;
break;
default:
printf("wfc error: wrong direction (%d)\n", direction);
return 0;
};
for (int y=0; y<height; y++) {
int a_y = a_offy + y;
int b_y = b_offy + y;
if (memcmp(&(a->data[(y+a_offy)*a->width*a->component_cnt + a_offx*a->component_cnt]),
&(b->data[(y+b_offy)*a->width*a->component_cnt + b_offx*a->component_cnt]),
width*a->component_cnt)) {
return 0;
}
}
return 1;
}
// Return NULL on error
static struct wfc_image *wfc__img_flip_horizontally(struct wfc_image *image)
{
struct wfc_image *copy = wfc_img_copy(image);
if (copy == NULL) {
p("wfc__img_flip_horizontally: error\n");
return NULL;
}
for (int y=0; y<image->height; y++) {
for (int x=0; x<image->width/2; x++) {
unsigned char *src = &( image->data[y*image->width*image->component_cnt + x*image->component_cnt] );
unsigned char *dst = &( copy->data[y*image->width*image->component_cnt + (image->width-1-x)*image->component_cnt] );
memcpy(dst, src, image->component_cnt);
src = &( image->data[y*image->width*image->component_cnt + (image->width-1-x)*image->component_cnt] );
dst = &( copy->data[y*image->width*image->component_cnt + x*image->component_cnt] );
memcpy(dst, src, image->component_cnt);
}
}
return copy;
}
// Return NULL on error
static struct wfc_image *wfc__img_flip_vertically(struct wfc_image *image)
{
struct wfc_image *copy = wfc_img_copy(image);
if (copy == NULL) {
p("wfc__img_flip_vertically: error\n");
return NULL;
}
for (int y=0; y<image->height/2; y++) {
unsigned char *src = &( image->data[y*image->width*image->component_cnt] );
unsigned char *dst = &( copy->data[(image->height-1-y)*image->width*image->component_cnt] );
memcpy(dst, src, image->width*image->component_cnt);
src = &( image->data[(image->height-1-y)*image->width*image->component_cnt] );
dst = &( copy->data[y*image->width*image->component_cnt] );
memcpy(dst, src, image->width*image->component_cnt);
}
return copy;
}
static struct wfc_image *wfc__img_rotate90(struct wfc_image *image, int n) {
wfcassert(n>0);
n %= 4;
struct wfc_image *copy;
if (n%2) {
copy = wfc_img_create(image->height, image->width, image->component_cnt);
} else {
copy = wfc_img_create(image->width, image->height, image->component_cnt);
}
if (copy == NULL) {
p("wfc__img_rotate90: error\n");
return NULL;
}
for (int y=0; y<image->height; y++) {
for (int x=0; x<image->width; x++) {
unsigned char components[4];
memcpy(components, &(image->data[y * image->width * image->component_cnt + x * image->component_cnt]), image->component_cnt);
if (n==1) {
memcpy(&(copy->data[x * copy->width * copy->component_cnt + (copy->width - y - 1) * copy->component_cnt]),
components,
image->component_cnt);
} else if (n==2) {
memcpy(&(copy->data[(copy->height - y - 1) * copy->width * copy->component_cnt + (copy->width - x - 1) * copy->component_cnt]),
components,
image->component_cnt);
} else if (n==3) {
memcpy(&(copy->data[(copy->height - x - 1) * copy->width * copy->component_cnt + y * copy->component_cnt]),
components,
image->component_cnt);
} else {
printf("error: wfc__img_rotate90, n=%d\n", n);
exit(1);
}
}
}
return copy;
}
// Return 1 if the two images are the same, 0 otherwise.
static int wfc__img_cmp(struct wfc_image *a, struct wfc_image *b)
{
if (a->width!=b->width || a->height!=b->height || a->component_cnt!=b->component_cnt) {
return 0;
}
return memcmp(a->data, b->data, a->width*a->height*a->component_cnt) == 0;
}
////////////////////////////////////////////////////////////////////////////////
//
// WFC: Utils (Method-independent)
//
////////////////////////////////////////////////////////////////////////////////
static int wfc__nofunc_int(const char *func_name, const char *msg, ...)
{
printf("%s: %s\n", func_name, msg);
return -1;
}
static void *wfc__nofunc_ptr(const char *func_name, const char *msg, ...)
{
printf("%s: %s\n", func_name, msg);
return NULL;
}
static void wfc_destroy_cells(int *cells)
{
free(cells);
}
// Return NULL on error
static int *wfc_cells(struct wfc *wfc)
{
int *cells = malloc(sizeof(*cells) * wfc->cell_cnt);
if (cells == NULL)
return NULL;
for (int i=0; i<wfc->cell_cnt; i++)
cells[i] = wfc->cells[i].tiles[0];
return cells;
}
struct wfc_image *wfc_output_image(struct wfc *wfc)
{
struct wfc_image *image = wfc_img_create(wfc->output_width, wfc->output_height, wfc->image->component_cnt);
if (image == NULL) {
p("wfc_export: error\n");
return 0;
}
for (int y=0; y<wfc->output_height; y++) {
for (int x=0; x<wfc->output_width; x++) {
struct wfc__cell *cell = &( wfc->cells[y * wfc->output_width + x] );
double components[4] = {0, 0, 0, 0};
for (int i=0; i<cell->tile_cnt; i++) {
struct wfc__tile *tile = &( wfc->tiles[ cell->tiles[i] ] );
for (int j=0; j<wfc->image->component_cnt; j++) {
components[j] += tile->image->data[j];
}
}
for (int i=0; i<wfc->image->component_cnt; i++) {
image->data[y * wfc->output_width * wfc->image->component_cnt + x * wfc->image->component_cnt + i] = (unsigned char)(components[i] / cell->tile_cnt);
}
}
}
return image;
}
#ifdef WFC_USE_STB
// Return 0 on error, non-0 on success
// dep: stb
int wfc_export(struct wfc *wfc, const char *filename)
{
struct wfc_image *image = wfc_output_image(wfc);
int rv = wfc_img_save(image, filename);
wfc_img_destroy(image);
return rv;
}
// Return 0 on error, non-0 on success
// dep: stb
int wfc_export_tiles(struct wfc *wfc, const char *path)
{
char filename[128];
for (int i=0; i<wfc->tile_cnt; i++) {
sprintf(filename, "%s/%d.png", path, i);
struct wfc__tile *tile = &wfc->tiles[i];
if (wfc_img_save(tile->image, filename) == 0) {
p("wfc_export_tiles: error\n");
return 0;
}
}
return 1;
}
#endif // WFC_USE_STB
// Return NULL on error
// Assumes the tile fits in the image
static struct wfc_image *wfc__create_tile_image(struct wfc_image *image, int x, int y, int tile_width, int tile_height)
{
struct wfc_image *tile_image = wfc_img_create(tile_width, tile_height, image->component_cnt);
if (tile_image == NULL) {
p("wfc__create_tile_image: error\n");
return NULL;
}
tile_image->width = tile_width;
tile_image->height = tile_height;
tile_image->component_cnt = image->component_cnt;
for (int i=0; i<tile_height; i++) {
memcpy(&tile_image->data[i * tile_width * image->component_cnt],
&image->data[(y+i) * image->width * image->component_cnt + x * image->component_cnt],
tile_width * image->component_cnt);
}
return tile_image;
}
// Return 0 on error
static int wfc__add_overlapping_images(struct wfc__tile *tiles, struct wfc_image *image, int xcnt, int ycnt, int tile_width, int tile_height)
{
int tile_cnt = xcnt * ycnt;
for (int y=0; y<ycnt; y++)
for (int x=0; x<xcnt; x++) {
struct wfc__tile *tile = &( tiles[y*xcnt + x] );
tile->freq = 1;
tile->image = wfc__create_tile_image(image, x, y, tile_width, tile_height);
if (tile->image == NULL)
goto CLEANUP;
}
return 1;
CLEANUP:
for (int i=0; i<tile_cnt; i++)
wfc_img_destroy(tiles[i].image);
return 0;
}
// Return 0 on error, non-0 on success
// flip_direction: 0 - horizontal, 1 vertical
static int wfc__add_flipped_images(struct wfc__tile *tiles, int tile_idx, int flip_direction)
{
for (int i=0; i<tile_idx; i++) {
struct wfc__tile *src = &tiles[i];
struct wfc__tile *dst = &tiles[tile_idx + i];
dst->freq = 1;
if (flip_direction == 0)
dst->image = wfc__img_flip_horizontally(src->image);
else
dst->image = wfc__img_flip_vertically(src->image);
if (dst->image == NULL)
goto CLEANUP;
}
return 1;
CLEANUP:
p("wfc__add_flipped_tiles: error\n");
for (int i=0; i<tile_idx; i++)
wfc_img_destroy(tiles[tile_idx + i].image);
return 0;
}
// Return 0 on error, non-0 on success
static int wfc__add_rotated_images(struct wfc__tile *tiles, int tile_idx)
{
for (int i=0; i<tile_idx; i++) {
for (int j=0; j<3; j++) {
struct wfc__tile *src = &tiles[i];
struct wfc__tile *dst = &tiles[tile_idx + i*3 + j];
dst->freq = 1;
dst->image = wfc__img_rotate90(src->image, j+1);
if (dst->image == NULL)
goto CLEANUP;
}
}
return 1;
CLEANUP:
p("wfc__add_rotated_tiles: error\n");
for (int i=tile_idx; i<tile_idx*4; i++)
wfc_img_destroy(tiles[i].image);
return 0;
}
// Return unique tiles with frequencies
// Return 0 on error, non-0 on success
static int wfc__remove_duplicate_tiles(struct wfc__tile **tiles, int *tile_cnt)
{
int unique_cnt = 1;
for (int j=1; j<*tile_cnt; j++) {
int unique = 1;
for (int k=0; k<unique_cnt; k++) {
if (wfc__img_cmp((*tiles)[j].image, (*tiles)[k].image)) {
unique = 0;
(*tiles)[k].freq++;
break;
}
}
if (unique) {
if (unique_cnt != j) {
struct wfc__tile tmp = (*tiles)[unique_cnt];
(*tiles)[unique_cnt] = (*tiles)[j];
(*tiles)[j] = tmp;
}
unique_cnt++;
}
}
for (int i=unique_cnt; i<*tile_cnt; i++)
wfc_img_destroy((*tiles)[i].image);
struct wfc__tile *unique_tiles = realloc(*tiles, sizeof(**tiles) * unique_cnt);
if (unique_tiles == NULL) {
p("wfc__remove_duplicate_tiles: error\n");
return 0;
}
*tiles = unique_tiles;
*tile_cnt = unique_cnt;
return 1;
}
////////////////////////////////////////////////////////////////////////////////
//
// WFC: Solve (Method-independent)
//
////////////////////////////////////////////////////////////////////////////////
static void wfc__destroy_props(struct wfc__prop *props)
{
free(props);
}
static struct wfc__prop *wfc__create_props(int cell_cnt)
{
struct wfc__prop *props = malloc(sizeof(*props) * cell_cnt * WFC_MAX_PROP_CNT);
return props;
}
static void wfc__destroy_cells(struct wfc__cell *cells, int cell_cnt)
{
free(cells[0].tiles);
free(cells);
}
// Return NULL on error
static struct wfc__cell *wfc__create_cells(int cell_cnt, int tile_cnt)
{
struct wfc__cell *cells = malloc(sizeof(*cells) * cell_cnt);
if (cells == NULL)
goto CLEANUP;
for (int i=0; i<cell_cnt; i++)
cells[i].tiles = NULL;
cells[0].tiles = malloc(sizeof(*(cells[0].tiles)) * tile_cnt * cell_cnt);
for (int i=1; i<cell_cnt; i++) {
cells[i].tiles = cells[0].tiles + i * tile_cnt;
if (cells[i].tiles == NULL)
goto CLEANUP;
}
return cells;
CLEANUP:
p("wfc__create_cells: error\n");
wfc__destroy_cells(cells, cell_cnt);
return NULL;
}
static void wfc__destroy_tiles(struct wfc__tile *tiles, int tile_cnt)
{
if (tiles == NULL)
return;
for (int i=0; i<tile_cnt; i++) {
struct wfc__tile *t = &tiles[i];
wfc_img_destroy(t->image);
}
free(tiles);
}
// Return 0 on error
static struct wfc__tile *wfc__create_tiles(int tile_cnt)
{
struct wfc__tile *tiles = malloc(sizeof(*tiles) * tile_cnt);
if (tiles == NULL)
goto CLEANUP;
for (int i=0; i<tile_cnt; i++) {
tiles[i].image = NULL;
tiles[i].freq = 0;
}
return tiles;
CLEANUP:
p("wfc__create_tiles: error\n");
wfc__destroy_tiles(tiles, tile_cnt);
return NULL;
}
static void wfc__destroy_allowed_tiles(int *allowed_tiles[4])
{
free(allowed_tiles[0]);
}
// Return 0 on error
static int wfc__create_allowed_tiles(int *allowed_tiles[4], int tile_cnt)
{
allowed_tiles[0] = malloc(sizeof(*allowed_tiles[0]) * tile_cnt * tile_cnt * 4);
for (int i=1; i<4; i++)
allowed_tiles[i] = allowed_tiles[0] + i * tile_cnt * tile_cnt;
return 1;
CLEANUP:
p("wfc__create_allowed_tiles: error\n");
wfc__destroy_allowed_tiles(allowed_tiles);
return 0;
}
static void wfc__add_prop(struct wfc *wfc, int src_cell_idx, int dst_cell_idx, enum wfc__direction direction)
{
// TODO: check for wfc->prop_cnt == WFC_MAX_PROP_CNT
struct wfc__prop *p = &( wfc->props[wfc->prop_cnt] );
(wfc->prop_cnt)++;
p->src_cell_idx = src_cell_idx;
p->dst_cell_idx = dst_cell_idx;
p->direction = direction;
}
// add prop to update cell above the cell_idx
static void wfc__add_prop_up(struct wfc *wfc, int src_cell_idx)
{
if (src_cell_idx - wfc->output_width >= 0) {
wfc__add_prop(wfc, src_cell_idx, src_cell_idx - wfc->output_width, WFC_UP);
}
}
static void wfc__add_prop_down(struct wfc *wfc, int src_cell_idx)
{
if (src_cell_idx + wfc->output_width < wfc->cell_cnt) {
wfc__add_prop(wfc, src_cell_idx, src_cell_idx + wfc->output_width, WFC_DOWN);
}
}
static void wfc__add_prop_left(struct wfc *wfc, int src_cell_idx)
{
if (src_cell_idx % wfc->output_width != 0) {
wfc__add_prop(wfc, src_cell_idx, src_cell_idx - 1, WFC_LEFT);
}
}
static void wfc__add_prop_right(struct wfc *wfc, int src_cell_idx)
{
if (src_cell_idx % wfc->output_width != wfc->output_width - 1) {
wfc__add_prop(wfc, src_cell_idx, src_cell_idx + 1, WFC_RIGHT);
}
}
// Does the cell enable tile in the direction?
static int wfc__tile_enabled(struct wfc *wfc, int tile_idx, int cell_idx, enum wfc__direction d)
{
struct wfc__cell *cell = &( wfc->cells[cell_idx] );
int *tiles = cell->tiles;
// Tile is enabled if any of the cell's tiles allowes it in
// the specified diretion
int tile_cnt = wfc->tile_cnt;
for (int i=0, cnt=cell->tile_cnt; i<cnt; i++) {
if (wfc->allowed_tiles[d][tiles[i] * tile_cnt + tile_idx]) {
return 1;
}
}
return 0;
}
// Checks whether particular prop is already added and pending, in which
// case there is no point of adding the same prop again.
//
// 1 - prop is added, 0 - prop is not added
static int wfc__is_prop_pending(struct wfc *wfc, int cell_idx, enum wfc__direction d) {
for (int i=wfc->prop_idx+1; i<wfc->prop_cnt; i++) {
struct wfc__prop *p = &( wfc->props[i] );
if (p->src_cell_idx == cell_idx && p->direction == d) {
return 1;
}
}
return 0;
}
// Updates tiles in the destination cell to those that are allowed by the source cell
// and propagate updates
//
// Return 0 on error
static int wfc__propagate_prop(struct wfc *wfc, struct wfc__prop *p)
{
int new_cnt = 0;
struct wfc__cell *dst_cell = &( wfc->cells[ p->dst_cell_idx ] );
// Go through all destination tiles and check whether they are enabled by the source cell
for (int i=0, cnt=dst_cell->tile_cnt; i<cnt; i++) {
int possible_dst_tile_idx = dst_cell->tiles[i];
// If a destination tile is enabled by the source cell, keep it
if (wfc__tile_enabled(wfc, possible_dst_tile_idx, p->src_cell_idx, p->direction)) {
dst_cell->tiles[new_cnt] = possible_dst_tile_idx;
new_cnt++;
} else {
int freq = wfc->tiles[possible_dst_tile_idx].freq;
double p = ((double)freq) / wfc->sum_freqs;
dst_cell->entropy += p*log(p);
dst_cell->sum_freqs -= freq;