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
use std::ffi;
use std::ptr::NonNull;
use libc;
#[link(name="wfc")]
extern {
fn wfc_init(wfc: *mut libc::c_void);
fn wfc_run(wfc: *mut libc::c_void, max_collapse_cnt: libc::c_int) -> libc::c_int;
fn wfc_export(wfc: *const libc::c_void, filename: *const libc::c_char) -> libc::c_int;
fn wfc_destroy(wfc: *mut libc::c_void);
fn wfc_img_copy(image: *const WfcImage) -> *mut WfcImage;
fn wfc_img_destroy(image: *mut WfcImage);
fn wfc_output_image(wfc: *mut libc::c_void) -> *mut WfcImage;
fn wfc_img_load(filename: *const libc::c_char) -> *mut WfcImage;
fn wfc_img_create(width: libc::c_int, height: libc::c_int, component_cnt: libc::c_int) -> *mut WfcImage;
fn wfc_overlapping(output_width: libc::c_int,
output_height: libc::c_int,
image: *mut WfcImage,
tile_width: libc::c_int,
tile_height: libc::c_int,
expand_input: libc::c_int,
xflip_tiles: libc::c_int,
yflip_tiles: libc::c_int,
rotate_tiles: libc::c_int) -> *mut libc::c_void;
}
#[repr(C)]
pub struct WfcImage {
pub data: *mut i8,
pub component_cnt: libc::c_int,
pub width: libc::c_int,
pub height: libc::c_int,
}
impl WfcImage {
pub fn new(data: *mut i8,
component_cnt: libc::c_int,
width: libc::c_int,
height: libc::c_int) -> WfcImage {
return WfcImage { data, component_cnt, width, height };
}
pub fn from_vec(width: i32, height: i32, component_cnt: i32, data: Vec<u8>) -> Option<NonNull<WfcImage>> {
unsafe {
let image_ptr = wfc_img_create(width, height, component_cnt);
let length = data.len();
std::ptr::copy_nonoverlapping(data.as_ptr() as *mut u8, (*image_ptr).data as *mut u8, length);
return NonNull::new(image_ptr);
}
}
pub fn from_file(filename: &str) -> Option<NonNull<WfcImage>> {
unsafe {
let c_filename = ffi::CString::new(filename).unwrap();
let image: *mut WfcImage = wfc_img_load(c_filename.as_ptr());
return NonNull::new(image);
}
}
pub fn vec(&self) -> Vec<u8> {
unsafe {
let length = self.num_bytes();
let data: *mut u8 = libc::malloc(length) as *mut u8;
std::ptr::copy_nonoverlapping(self.data as *mut u8, data, length);
return Vec::from_raw_parts(data, length, length);
}
}
pub fn num_bytes(&self) -> usize {
return (self.width * self.height * self.component_cnt) as usize;
}
}
pub struct Wfc {
pub wfc: NonNull<libc::c_void>,
pub image: NonNull<WfcImage>,
}
impl Wfc {
pub fn from_raw_parts(wfc: *mut libc::c_void, image: *mut WfcImage) -> Option<Wfc> {
let wfc = NonNull::new(wfc)?;
let image = NonNull::new(image)?;
return Some(Wfc { wfc, image });
}
pub fn overlapping(output_width: i32,
output_height: i32,
mut image: NonNull<WfcImage>,
tile_width: i32,
tile_height: i32,
expand_input: bool,
xflip_tiles: bool,
yflip_tiles: bool,
rotate_tiles: bool) -> Option<Wfc> {
unsafe {
let wfc = wfc_overlapping(output_width,
output_height,
image.as_mut(),
tile_width,
tile_height,
expand_input as i32,
xflip_tiles as i32,
yflip_tiles as i32,
rotate_tiles as i32);
return Wfc::from_raw_parts(wfc, image.as_mut());
}
}
pub fn run(&mut self, max_collapse_cnt: Option<i32>) -> Result<(), &str> {
unsafe {
wfc_init(self.wfc.as_mut());
let max_cnt = max_collapse_cnt.unwrap_or(-1);
let result: libc::c_int = wfc_run(self.wfc.as_mut(), max_cnt);
if result == 0 {
return Err("wfc_run returned an error!");
} else {
return Ok(());
}
}
}
pub fn export(&mut self, filename: &str) -> Result<(), &str> {
unsafe {
let c_filename = ffi::CString::new(filename).unwrap();
let result = wfc_export(self.wfc.as_mut(), c_filename.as_ptr());
if result == 0 {
return Err("wfc_export returned an error!");
} else {
return Ok(());
}
}
}
pub fn output_image(&mut self) -> Option<NonNull<WfcImage>> {
unsafe {
let image = wfc_output_image(self.wfc.as_mut());
return NonNull::new(image);
}
}
pub fn vec(&mut self) -> Vec<u8> {
unsafe {
return self.image.as_ref().vec();
}
}
}
impl Drop for Wfc {
fn drop(&mut self) {
unsafe {
wfc_img_destroy(self.image.as_mut());
}
}
}
#[test]
pub fn test_overlapping() {
let image = WfcImage::from_file("data/cave.png").unwrap();
let maybe_wfc = Wfc::overlapping(32, 32, image, 3, 3, true, true, true, true);
assert!(maybe_wfc.is_some());
}
#[test]
pub fn test_run() {
let image = WfcImage::from_file("data/cave.png").unwrap();
let maybe_wfc = Wfc::overlapping(32, 32, image, 3, 3, true, true, true, true);
assert!(maybe_wfc.is_some());
let mut wfc = maybe_wfc.unwrap();
let result = wfc.run(Some(1));
assert_eq!(Ok(()), result);
}
#[test]
pub fn test_export() {
let image = WfcImage::from_file("data/cave.png").unwrap();
let maybe_wfc = Wfc::overlapping(32, 32, image, 3, 3, true, true, true, true);
assert!(maybe_wfc.is_some());
let mut wfc = maybe_wfc.unwrap();
let result = wfc.run(Some(100));
assert_eq!(Ok(()), result);
wfc.export("output.png").unwrap();
std::fs::remove_file("output.png");
}
#[test]
pub fn test_image() {
let mut image = WfcImage::from_file("data/cave.png").unwrap();
unsafe {
let bytes = image.as_ref().vec();
assert_eq!(image.as_ref().num_bytes(), bytes.len());
}
}