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
use std::ffi;
use libc;
#[link(name="wfc")]
extern {
fn wfc_init(wfc: *mut std::ffi::c_void);
fn wfc_run(wfc: *mut std::ffi::c_void, max_collapse_cnt: libc::c_int) -> libc::c_int;
fn wfc_export(wfc: *const std::ffi::c_void, filename: *const libc::c_char) -> libc::c_int;
fn wfc_destroy(wfc: *mut std::ffi::c_void);
fn wfc_img_copy(image: *const WfcImage) -> *mut WfcImage;
fn wfc_img_destroy(image: *mut WfcImage);
fn wfc_output_image(wfc: *mut std::ffi::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 std::ffi::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>) -> *mut 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 image_ptr;
}
}
pub fn from_file(filename: &str) -> Option<*mut WfcImage> {
unsafe {
let c_filename = ffi::CString::new(filename).ok()?;
let image: *mut WfcImage = wfc_img_load(c_filename.as_ptr());
return Some(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;
}
}
impl Drop for WfcImage {
fn drop(&mut self) {
unsafe {
wfc_img_destroy(self as *mut WfcImage);
}
}
}
pub struct Wfc {
pub wfc: *mut libc::c_void,
pub image: *mut WfcImage,
}
impl Wfc {
pub fn from_raw_parts(wfc: *mut libc::c_void, image: *mut WfcImage) -> Wfc {
return Wfc { wfc, image };
}
pub fn overlapping(output_width: i32,
output_height: i32,
image: *mut WfcImage,
tile_width: i32,
tile_height: i32,
expand_input: bool,
xflip_tiles: bool,
yflip_tiles: bool,
rotate_tiles: bool) -> Option<Wfc> {
unsafe {
if image.is_null() {
return None;
}
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);
if wfc.is_null() {
return None;
}
return Some(Wfc::from_raw_parts(wfc, image.as_mut()?));
}
}
pub fn run(&mut self, max_collapse_cnt: Option<i32>, seed: Option<u32>) -> Result<(), &str> {
unsafe {
let mut wfc_ptr = self.wfc.as_mut().ok_or("Wfc pointer invalid")?;
wfc_init(wfc_ptr);
if let Some(seed) = seed {
libc::srand(seed)
}
let max_cnt = max_collapse_cnt.unwrap_or(-1);
let result: libc::c_int = wfc_run(wfc_ptr, 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).map_err(|_| "Filename to CString error")?;
let mut wfc_ptr = self.wfc.as_mut().ok_or("Wfc pointer invalid")?;
let result = wfc_export(wfc_ptr, c_filename.as_ptr());
if result == 0 {
return Err("wfc_export returned an error!");
} else {
return Ok(());
}
}
}
pub fn output_image(&mut self) -> Option<*mut WfcImage> {
unsafe {
return Some(wfc_output_image(self.wfc.as_mut()?));
};
}
pub fn vec(&mut self) -> Vec<u8> {
unsafe {
return self.image.as_ref().unwrap().vec();
}
}
}
impl Drop for Wfc {
fn drop(&mut self) {
unsafe {
if let Some(wfc_ptr) = self.wfc.as_mut() {
wfc_destroy(wfc_ptr);
}
}
}
}
#[test]
pub fn test_overlapping() {
let image = WfcImage::from_file("data/cave.png").unwrap();
{
let wfc = Wfc::overlapping(32, 32, image, 3, 3, true, true, true, true);
}
{
let wfc = Wfc::overlapping(32, 32, image, 3, 3, true, true, true, true);
}
{
let wfc = Wfc::overlapping(32, 32, image, 3, 3, true, true, true, true);
}
}
#[test]
pub fn test_run() {
let image = WfcImage::from_file("data/cave.png").unwrap();
let mut wfc = Wfc::overlapping(32, 32, image, 3, 3, true, true, true, true).unwrap();
let result = wfc.run(Some(10), Some(1));
assert_eq!(Ok(()), result);
}
#[test]
pub fn test_export() {
let image = WfcImage::from_file("data/cave.png").unwrap();
let mut wfc = Wfc::overlapping(32, 32, image, 3, 3, true, true, true, true).unwrap();
let result = wfc.run(Some(100), Some(1));
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().unwrap().vec();
assert_eq!(image.as_ref().unwrap().num_bytes(), bytes.len());
}
}