/
vxl.c
317 lines (268 loc) · 9.07 KB
/
vxl.c
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
/* Goxel 3D voxels editor
*
* copyright (c) 2016 Guillaume Chereau <guillaume@noctua-software.com>
*
* Goxel is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
* Goxel is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with
* goxel. If not, see <http://www.gnu.org/licenses/>.
*/
// Support for Ace of Spades map files (vxl)
#include "goxel.h"
#include "file_format.h"
#define READ(type, file) \
({ type v; size_t r = fread(&v, sizeof(v), 1, file); (void)r; v;})
#define CHECK(x) do { \
if (!(x)) { \
ret = -1; \
goto end; \
} \
} while(0)
static inline int AT(int x, int y, int z, int d) {
x = 511 - x;
z = d - 1 - z;
return x + y * 512 + z * 512 * 512;
}
static void swap_color(uint32_t v, uint8_t ret[4])
{
uint8_t o[4];
memcpy(o, &v, 4);
ret[0] = o[2];
ret[1] = o[1];
ret[2] = o[0];
ret[3] = o[3];
}
/*
* Get the max height of a vlx file.
*/
static int vxl_get_d(const uint8_t *data, int size)
{
int w = 512, h = 512, d = 64, x, y;
const uint8_t *v;
int number_4byte_chunks;
int top_color_start;
int top_color_end;
int len_bottom;
v = data;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
while (true) {
number_4byte_chunks = v[0];
top_color_start = v[1];
top_color_end = v[2];
d = max(d, top_color_end + 1);
len_bottom = top_color_end - top_color_start + 1;
if (number_4byte_chunks == 0) {
v += 4 * (len_bottom + 1);
break;
}
v += v[0] * 4;
}
}
return d;
}
static int vxl_import(const file_format_t *format, image_t *image,
const char *path)
{
// The algo is based on
// https://silverspaceship.com/aosmap/aos_file_format.html
// From Sean Barrett (the same person that wrote the code used in
// ext_src/stb!).
int ret = 0, size;
int w = 512, h = 512, d = 64, x, y, z;
uint8_t (*cube)[4] = NULL;
uint8_t *data = NULL, *v;
uint32_t *color;
int i;
int number_4byte_chunks;
int top_color_start;
int top_color_end;
int bottom_color_start;
int bottom_color_end; // exclusive
int len_top;
int len_bottom;
if (!path) return -1;
data = (void*)read_file(path, &size);
d = vxl_get_d(data, size);
cube = calloc(w * h * d, sizeof(*cube));
v = data;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
for (z = 0; z < d; z++)
cube[AT(x, y, z, d)][3] = 255;
z = 0;
while (true) {
number_4byte_chunks = v[0];
top_color_start = v[1];
top_color_end = v[2];
for (i = z; i < top_color_start; i++)
cube[AT(x, y, i, d)][3] = 0;
color = (uint32_t*)(v + 4);
for (z = top_color_start; z <= top_color_end; z++) {
CHECK(z >= 0 && z < d);
swap_color(*color++, cube[AT(x, y, z, d)]);
}
len_bottom = top_color_end - top_color_start + 1;
// check for end of data marker
if (number_4byte_chunks == 0) {
// infer ACTUAL number of 4-byte chunks from the length of the
// color data
v += 4 * (len_bottom + 1);
break;
}
// infer the number of bottom colors in next span from chunk length
len_top = (number_4byte_chunks-1) - len_bottom;
// now skip the v pointer past the data to the beginning of the
// next span
v += v[0] * 4;
bottom_color_end = v[3]; // aka air start
bottom_color_start = bottom_color_end - len_top;
for(z = bottom_color_start; z < bottom_color_end; z++)
swap_color(*color++, cube[AT(x, y, z, d)]);
}
}
volume_blit(image->active_layer->volume, (uint8_t*)cube,
-w / 2, -h / 2, -d / 2, w, h, d, NULL);
if (box_is_null(image->box)) {
bbox_from_extents(image->box, vec3_zero, w / 2, h / 2, d / 2);
}
end:
free(cube);
free(data);
return ret;
}
static int is_surface(int x, int y, int z, uint8_t map[512][512][64])
{
if (map[x][y][z]==0) return 0;
if (x == 0 || x == 511) return 1;
if (y == 0 || y == 511) return 1;
if (z == 0 || z == 63) return 1;
if (x > 0 && map[x-1][y][z]==0) return 1;
if (x+1 < 512 && map[x+1][y][z]==0) return 1;
if (y > 0 && map[x][y-1][z]==0) return 1;
if (y+1 < 512 && map[x][y+1][z]==0) return 1;
if (z > 0 && map[x][y][z-1]==0) return 1;
if (z+1 < 64 && map[x][y][z+1]==0) return 1;
return 0;
}
static void write_color(FILE *f, uint32_t color)
{
uint8_t c[4];
memcpy(c, &color, 4);
fputc(c[2], f);
fputc(c[1], f);
fputc(c[0], f);
fputc(c[3], f);
}
#define MAP_Z 64
void write_map(const char *filename,
uint8_t map[512][512][64],
uint32_t color[512][512][64])
{
int i,j,k;
FILE *f = fopen(filename, "wb");
for (j = 0; j < 512; ++j) {
for (i=0; i < 512; ++i) {
k = 0;
while (k < MAP_Z) {
int z;
int air_start;
int top_colors_start;
int top_colors_end; // exclusive
int bottom_colors_start;
int bottom_colors_end; // exclusive
int top_colors_len;
int bottom_colors_len;
int colors;
// find the air region
air_start = k;
while (k < MAP_Z && !map[i][j][k])
++k;
// find the top region
top_colors_start = k;
while (k < MAP_Z && is_surface(i,j,k,map))
++k;
top_colors_end = k;
// now skip past the solid voxels
while (k < MAP_Z && map[i][j][k] && !is_surface(i,j,k,map))
++k;
// at the end of the solid voxels, we have colored voxels.
// in the "normal" case they're bottom colors; but it's
// possible to have air-color-solid-color-solid-color-air,
// which we encode as air-color-solid-0, 0-color-solid-air
// so figure out if we have any bottom colors at this point
bottom_colors_start = k;
z = k;
while (z < MAP_Z && is_surface(i,j,z,map))
++z;
if (z == MAP_Z || 0)
; // in this case, the bottom colors of this span are
// empty, because we'l emit as top colors
else {
// otherwise, these are real bottom colors so we can write
// them
while (is_surface(i,j,k,map))
++k;
}
bottom_colors_end = k;
// now we're ready to write a span
top_colors_len = top_colors_end - top_colors_start;
bottom_colors_len = bottom_colors_end - bottom_colors_start;
colors = top_colors_len + bottom_colors_len;
if (k == MAP_Z)
fputc(0,f); // last span
else
fputc(colors+1, f);
fputc(top_colors_start, f);
fputc(top_colors_end-1, f);
fputc(air_start, f);
for (z=0; z < top_colors_len; ++z)
write_color(f, color[i][j][top_colors_start + z]);
for (z=0; z < bottom_colors_len; ++z)
write_color(f, color[i][j][bottom_colors_start + z]);
}
}
}
fclose(f);
}
static int export_as_vxl(const file_format_t *format, const image_t *image,
const char *path)
{
uint8_t (*map)[512][512][64];
uint32_t (*color)[512][512][64];
const volume_t *volume = goxel_get_layers_volume(image);
volume_iterator_t iter = {0};
uint8_t c[4];
int x, y, z, pos[3];
assert(path);
map = calloc(1, sizeof(*map));
color = calloc(1, sizeof(*color));
for (z = 0; z < 64; z++)
for (y = 0; y < 512; y++)
for (x = 0; x < 512; x++) {
pos[0] = 256 - x;
pos[1] = y - 256;
pos[2] = 31 - z;
volume_get_at(volume, &iter, pos, c);
if (c[3] <= 127) continue;
(*map)[x][y][z] = 1;
memcpy(&((*color)[x][y][z]), c, 4);
}
write_map(path, *map, *color);
free(map);
free(color);
return 0;
}
FILE_FORMAT_REGISTER(vxl,
.name = "vxl",
.exts = {"*.vxl"},
.exts_desc = "vxl",
.import_func = vxl_import,
.export_func = export_as_vxl,
)