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
use std::sync::Arc;
use custom_error::custom_error;
use image_properties::{ImageProperties, ZoomLevelInfo};
use crate::dezoomer::*;
mod image_properties;
#[derive(Default)]
pub struct ZoomifyDezoomer;
impl Dezoomer for ZoomifyDezoomer {
fn name(&self) -> &'static str {
"zoomify"
}
fn zoom_levels(&mut self, data: &DezoomerInput) -> Result<ZoomLevels, DezoomerError> {
self.assert(data.uri.contains("/ImageProperties.xml"))?;
let DezoomerInputWithContents { uri, contents } = data.with_contents()?;
let levels = load_from_properties(uri, contents)?;
Ok(levels)
}
}
custom_error! {pub ZoomifyError
XmlError{source: serde_xml_rs::Error} = "Unable to parse ImageProperties.xml: {source}"
}
impl From<ZoomifyError> for DezoomerError {
fn from(err: ZoomifyError) -> Self {
DezoomerError::Other { source: err.into() }
}
}
fn load_from_properties(url: &str, contents: &[u8]) -> Result<ZoomLevels, ZoomifyError> {
let image_properties: ImageProperties = serde_xml_rs::from_reader(contents)?;
let base_url = &Arc::new(url.split("/ImageProperties.xml").next().unwrap().into());
let levels: Vec<ZoomLevelInfo> = image_properties.levels();
let levels: ZoomLevels = levels.into_iter().enumerate()
.map(move |(level, level_info)| ZoomifyLevel {
base_url: Arc::clone(base_url),
level_info,
level,
})
.into_zoom_levels();
Ok(levels)
}
struct ZoomifyLevel {
base_url: Arc<String>,
level_info: ZoomLevelInfo,
level: usize,
}
impl TilesRect for ZoomifyLevel {
fn size(&self) -> Vec2d {
self.level_info.size
}
fn tile_size(&self) -> Vec2d {
self.level_info.tile_size
}
fn tile_url(&self, pos: Vec2d) -> String {
format!(
"{base}/TileGroup{group}/{z}-{x}-{y}.jpg",
base = self.base_url,
group = self.level_info.tile_group(pos),
x = pos.x,
y = pos.y,
z = self.level
)
}
}
impl std::fmt::Debug for ZoomifyLevel {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Zoomify Image")
}
}
#[test]
fn test_panorama() {
let url = "http://x.fr/y/ImageProperties.xml?t";
let contents = br#"
<IMAGE_PROPERTIES
WIDTH="174550" HEIGHT="16991" NUMTILES="61284"
NUMIMAGES="1" VERSION="1.8" TILESIZE="256"/>"#;
let mut props = load_from_properties(url, contents).unwrap();
assert_eq!(props.len(), 11);
let level = &mut props[3];
let tiles: Vec<String> = level.next_tiles(None).into_iter().map(|t| t.url).collect();
assert_eq!(
tiles,
vec![
"http://x.fr/y/TileGroup0/3-0-0.jpg",
"http://x.fr/y/TileGroup0/3-1-0.jpg",
"http://x.fr/y/TileGroup0/3-2-0.jpg",
"http://x.fr/y/TileGroup0/3-3-0.jpg",
"http://x.fr/y/TileGroup0/3-4-0.jpg",
"http://x.fr/y/TileGroup0/3-5-0.jpg"
]
);
}
#[test]
fn test_tilegroups() {
use std::collections::HashSet;
let url = "http://x.fr/y/ImageProperties.xml?t";
let contents = br#"<IMAGE_PROPERTIES WIDTH="12000" HEIGHT="9788"
NUMTILES="2477" NUMIMAGES="1" VERSION="1.8" TILESIZE="256"/>"#;
let mut props = load_from_properties(url, contents).unwrap();
let level = &mut props[5];
let tiles: HashSet<String> = level.next_tiles(None).into_iter().map(|t| t.url).collect();
assert!(tiles.contains("http://x.fr/y/TileGroup1/5-0-14.jpg"));
assert!(tiles.contains("http://x.fr/y/TileGroup2/5-0-15.jpg"));
}