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
use std::path::PathBuf;
use crate::{dictionary::Dictionary, format, packer::Packer, Rect, Size};
#[derive(Debug)]
pub struct Context {
packer: Packer,
dictionary: Dictionary,
}
impl Context {
pub fn new(size: Size) -> Self {
Self {
packer: Packer::new(size),
dictionary: Dictionary::new(size),
}
}
pub fn pack(&mut self, path: &PathBuf, gap: u32) -> Option<Rect> {
let image = image::open(path).ok()?;
if let Some(rect) = self.packer.pack(&image, gap) {
self.dictionary.record(path, &rect);
return Some(rect);
}
None
}
pub fn save_to_file(
&self,
name: &str,
image: format::ImageFormat,
dict: Option<format::DictionaryFormat>,
) -> anyhow::Result<()> {
self.packer.save(name, image)?;
if let Some(dict) = dict {
self.dictionary.save(name, dict)?;
}
Ok(())
}
}