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
use std::str;
use crate::{get_value, read, read_dict, read_int, Block, Bounded, Camera, Layer};
#[derive(Debug)]
pub enum Data {
Blocks(Block),
Camera(Camera),
Layers(Vec<Layer>, Bounded),
}
impl Data {
pub fn parse(bytes: &mut Vec<u8>) -> Vec<Data> {
let mut result = vec![];
let mut layers = vec![];
loop {
if bytes.is_empty() {
break;
}
let type_bytes = read(bytes, 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(bytes);
result.push(Data::Blocks(data));
}
"CAMR" => {
let camera = Camera::new(bytes);
result.push(Data::Camera(camera));
}
"IMG " => {
let length = read_int(bytes);
let dict = read_dict(bytes, length);
let bounds = Bounded::from_bytes(get_value(&dict, "box")).unwrap();
result.push(Data::Layers(vec![], bounds));
}
"LAYR" => {
let layer = Layer::new(bytes);
layers.push(layer);
}
_ => {
let length = read_int(bytes);
let _data = read(bytes, length);
}
}
let _crc = read_int(bytes);
}
}
for (i, item) in result.iter().enumerate() {
if let Data::Layers(_, bounds) = item {
for layer in layers.iter_mut() {
if layer.bounds.is_none() {
layer.bounds = Some(*bounds);
}
}
result[i] = Data::Layers(layers, *bounds);
break;
}
}
result
}
}