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
use std::{
io::Read,
str,
};
use crate::{
Block,
Bounded,
Camera,
get_value,
Layer,
read,
read_dict,
read_int,
};
#[derive(Debug)]
pub enum Data {
Blocks(Block),
Camera(Camera),
Layers(Vec<Layer>, Bounded),
}
impl Data {
pub fn parse(stream: &mut dyn Read) -> Vec<Data> {
let mut result = vec![];
let mut layers = vec![];
loop {
let type_bytes = read(stream, 4);
let mut zero_count = 0;
for byte in type_bytes.iter() {
if byte == &0 {
zero_count += 1;
}
}
if zero_count == 4 {
break;
}
if let Ok(type_str) = str::from_utf8(&type_bytes) {
match type_str {
"BL16" => {
let data = Block::new(stream);
let _crc: i32 = read_int(stream);
result.push(Data::Blocks(data));
},
"CAMR" => {
let camera = Camera::new(stream);
let _crc: i32 = read_int(stream);
result.push(Data::Camera(camera));
},
"IMG " => {
let length = read_int(stream);
let dict = read_dict(stream, length);
let bounds = Bounded::from_bytes(get_value(&dict, "box")).unwrap();
let _crc: i32 = read_int(stream);
result.push(Data::Layers(vec![], bounds));
},
"LAYR" => {
let layer = Layer::new(stream);
let _crc: i32 = read_int(stream);
layers.push(layer);
},
// "PREV" => {
// let length = read_int(stream);
// let _data = read(stream, length);
// let _crc: i32 = read_int(stream);
// result.push(Chunk::Preview);
// },
_ => {
let length = read_int(stream);
let _data = read(stream, length);
let _crc: i32 = read_int(stream);
},
}
}
}
for (i, item) in result.iter().enumerate() {
match item {
Data::Layers(_, bounds) => {
for layer in layers.iter_mut() {
if layer.bounds.is_none() {
layer.bounds = Some(bounds.clone());
}
}
result[i] = Data::Layers(layers, bounds.clone());
break;
}
_ => {}
}
}
result
}
}