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
use std::{
collections::HashSet,
convert::TryInto,
fs::{
File,
OpenOptions,
},
io::{
Read,
Write,
},
path::{
Path,
PathBuf,
},
};
use color_eyre::eyre::{
bail,
Error,
};
use image::io::Reader as ImageReader;
use structopt::StructOpt;
use vox_format::{
chunk::{
read_main_chunk,
Chunk,
ChunkId,
ChunkWriter,
},
default_palette::DEFAULT_PALETTE,
from_file,
types::{
ColorIndex,
Model,
Palette,
},
writer::main_chunk_writer,
};
/// Tools for inspection and manipulation of MagicaVoxel VOX files.
#[derive(Debug, StructOpt)]
enum Args {
/// Strips chunks from the VOX file.
Strip {
/// Selects chunks to strip. If omitted, everything except chunks
/// specified with `--keep` will be stripped.
#[structopt(short = "s", long = "strip")]
strip: Vec<String>,
/// Keep the specified chunks. If this and `--strip` is omitted, every
/// chunk except `PACK`, `XYZI`, `RGBA` and `MATT` is stripped.
#[structopt(short = "k", long = "keep")]
keep: Vec<String>,
/// Output file. Defaults to `INPUT.stripped.vox` where `INPUT` is the
/// input file path without `.vox` file extension.
#[structopt(short = "o", long = "output")]
output: Option<PathBuf>,
/// The input file from which the chunks will be stripped.
input: PathBuf,
},
/// Prints info about a VOX file.
PrintInfo {
/// The input file from which information willbe displayed
input: PathBuf,
/// Prints the palette. This will not print the palette if it's the
/// default palette. If you want this, use `-P` instead.
#[structopt(short = "p", long = "palette")]
print_palette: bool,
/// Prints the palette - even if it's the default palette.
#[structopt(short = "P")]
print_palette_even_if_default: bool,
/// Print all models. This overrides anything set with `-m` or
/// `--model`.
#[structopt(short = "M", long = "all-models")]
print_all_models: bool,
/// Print only the model with the specified index.
#[structopt(short = "m", long = "model")]
model_index: Option<usize>,
},
/// Exports a palette as image.
ExportPalette {
/// The input file from which the palette will be exported. If omitted,
/// the default palette will be exported.
input: Option<PathBuf>,
/// Output file. Defaults to `INPUT.new-palette.vox` where `INPUT` is
/// the input file path without `.vox` file extension.
#[structopt(short = "o", long = "output")]
output: PathBuf,
},
/// Replaces the palette in a VOX file.
///
/// The palette is specified with `--palette` option and must be an image.
/// Regardless of the image's shape, the first 256 pixels will be used
/// for the palette.
///
/// The images will be converted to RGBA values to be used in the palette.
///
/// Note that entry 0 in the palette is special, in that it's always
/// transparent. If you set another color for that pixel, it will be
/// ignored.
SetPalette {
/// The input file that will have it's palette changed.
input: PathBuf,
/// Path to image containing the palette. This his compatible with
/// `export-palette`. If omitted, the default palette
/// will be used.
#[structopt(short = "p", long = "palette")]
palette: Option<PathBuf>,
/// The path for the output file. The file format will be guessed using
/// the file extension.
#[structopt(short = "o", long = "output")]
output: Option<PathBuf>,
},
}
#[derive(Debug)]
pub enum StripChunks {
Strip(HashSet<ChunkId>),
Keep(HashSet<ChunkId>),
}
impl Default for StripChunks {
fn default() -> Self {
let mut s = HashSet::new();
s.insert("PACK".parse().unwrap());
s.insert("SIZE".parse().unwrap());
s.insert("XYZI".parse().unwrap());
s.insert("RGBA".parse().unwrap());
s.insert("MATT".parse().unwrap());
Self::Keep(s)
}
}
impl StripChunks {
fn strip(&self, chunk_id: ChunkId) -> bool {
match self {
Self::Strip(s) => s.contains(&chunk_id),
Self::Keep(s) => !s.contains(&chunk_id),
}
}
}
impl Args {
fn run(self) -> Result<(), Error> {
match self {
Self::Strip {
strip,
keep,
input,
output,
} => {
let strip = if !strip.is_empty() && !keep.is_empty() {
bail!("The options `--strip` and `--keep` are exlusive");
}
else if !strip.is_empty() {
StripChunks::Strip(strip.iter().map(|s| s.parse()).collect::<Result<_, _>>()?)
}
else if !keep.is_empty() {
StripChunks::Keep(keep.iter().map(|s| s.parse()).collect::<Result<_, _>>()?)
}
else {
StripChunks::default()
};
log::debug!("Stripping: {:?}", strip);
let output = output.unwrap_or_else(|| default_output_path(&input, "stripped"));
copy_map_chunks(&input, &output, |_reader, chunk, _writer| {
if !strip.strip(chunk.id()) {
Ok(true)
}
else {
log::trace!("Stripping chunk: {:?}", chunk.id());
Ok(false)
}
})?;
}
Self::PrintInfo {
input,
print_palette,
print_palette_even_if_default,
print_all_models,
model_index,
} => {
let vox = from_file(input)?;
println!("VOX version: {}", vox.version);
if print_all_models {
for (i, model) in vox.models.iter().enumerate() {
print_model(i, model);
}
}
else if let Some(model_index) = model_index {
if let Some(model) = vox.models.get(model_index) {
print_model(model_index, model);
}
else {
eprintln!("Model with index {} does not exists. There are {} models in this file.", model_index, vox.models.len());
}
}
if print_palette || print_palette_even_if_default {
if vox.palette.is_default() && !print_palette_even_if_default {
println!("Palette: default");
}
else {
println!("Palette:");
for (index, color) in vox.palette.iter() {
println!(" - #{}: {:?}", index, color);
}
}
}
}
Self::ExportPalette { input, output } => {
let vox;
let palette = if let Some(input) = input {
vox = from_file(input)?;
&vox.palette
}
else {
&DEFAULT_PALETTE
};
let image = palette.as_image();
image.save(output)?;
}
Self::SetPalette {
input,
palette,
output,
} => {
let palette = if let Some(palette) = palette {
log::debug!("Loading palette: {}", palette.display());
let image = ImageReader::open(palette)?.decode()?;
// TODO: It would be nicer to pass an `ImageBuffer` with any pixel format and
// then just convert the pixels we need.
let image = image.into_rgba8();
Palette::from_image(&image)
}
else {
log::debug!("Using default palette");
DEFAULT_PALETTE
};
let output = output.unwrap_or_else(|| default_output_path(&input, "new-palette"));
copy_map_chunks(&input, &output, |_reader, chunk, writer| {
if matches!(chunk.id(), ChunkId::Rgba) {
log::debug!("Replacing RGBA chunk");
log::debug!("{:?}", palette.get(ColorIndex::from(69)));
// Replace RGBA chunk
writer
.child_content_writer(ChunkId::Rgba, |writer| palette.write(writer))?;
Ok(false)
}
else {
Ok(true)
}
})?;
}
}
Ok(())
}
}
fn print_model(i: usize, model: &Model) {
println!("Model #{}: {:?}", i, model.size);
for voxel in &model.voxels {
println!(" - {:?}: #{}", voxel.point, voxel.color_index);
}
}
fn default_output_path<P: AsRef<Path>>(input: P, postfix: &str) -> PathBuf {
let input = input.as_ref();
let ext = input.extension().and_then(|s| s.to_str()).unwrap_or("vox");
input.with_extension(format!("{}.{}", postfix, ext))
}
fn copy_map_chunks<
P: AsRef<Path>,
Q: AsRef<Path>,
F: FnMut(&mut File, &Chunk, &mut ChunkWriter<File>) -> Result<bool, vox_format::writer::Error>,
>(
input: P,
output: Q,
mut f: F,
) -> Result<(), Error> {
let input = input.as_ref();
let output = output.as_ref();
log::debug!("Reading input: {}", input.display());
let mut reader = File::open(&input)?;
let (main_chunk, version) = read_main_chunk(&mut reader)?;
log::debug!("Writing output: {}", output.display());
let writer = OpenOptions::new().create(true).write(true).open(output)?;
let mut chunks = vec![];
for r in main_chunk.children(&mut reader) {
let chunk = r?;
chunks.push(chunk);
}
main_chunk_writer(writer, version, |chunk_writer| {
let mut buf = vec![];
for chunk in &chunks {
if f(&mut reader, chunk, chunk_writer)? {
// Copy chunk
log::trace!("Copying chunk: {:?}", chunk.id());
buf.clear();
buf.reserve(chunk.content_len().try_into()?);
chunk.content(&mut reader)?.read_to_end(&mut buf)?;
chunk_writer.child_content_writer(chunk.id(), |writer| {
writer.write_all(&buf)?;
Ok(())
})?;
// TODO: If we move the copy function into `vox-format`, we can make use of the
// fact that we can read/write the children as a blob.
if chunk.children_len() != 0 {
//todo!("TODO: Copy children. This is not implemented, because at this point
// all supported chunk types (except `MAIN`) have no children. Please open an
// issue, if you need this feature.");
log::warn!("Unexpected chunk with children. Ignoring children");
}
}
}
Ok(())
})?;
Ok(())
}
fn main() -> Result<(), Error> {
dotenv::dotenv().ok();
color_eyre::install()?;
pretty_env_logger::init();
let args = Args::from_args();
args.run()?;
Ok(())
}