-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for DZI (Microsoft Deep Zoom)
- Loading branch information
Showing
8 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "dezoomify-rs" | ||
version = "1.4.0" | ||
version = "1.5.0" | ||
authors = ["Ophir LOJKINE <[email protected]>"] | ||
edition = "2018" | ||
license-file = "LICENSE" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use serde::Deserialize; | ||
|
||
use crate::Vec2d; | ||
|
||
use super::DziError; | ||
|
||
#[derive(Debug, Deserialize, PartialEq)] | ||
pub struct DziFile { | ||
#[serde(rename = "Overlap", default)] | ||
pub overlap: u32, | ||
#[serde(rename = "TileSize", default)] | ||
pub tile_size: u32, | ||
#[serde(rename = "Format", default)] | ||
pub format: String, | ||
#[serde(rename = "Size", default)] | ||
pub sizes: Vec<Size>, | ||
} | ||
|
||
impl DziFile { | ||
pub fn get_size(&self) -> Result<Vec2d, DziError> { | ||
self.sizes.iter().next() | ||
.map(|s| Vec2d { x: s.width, y: s.height }) | ||
.ok_or(DziError::NoSize) | ||
} | ||
pub fn get_tile_size(&self) -> Vec2d { | ||
Vec2d::square(self.tile_size) | ||
} | ||
pub fn max_level(&self) -> u32 { | ||
let size = self.get_size().unwrap(); | ||
log2(size.x.max(size.y)) | ||
} | ||
} | ||
|
||
fn log2(n: u32) -> u32 { | ||
32 - (n - 1).leading_zeros() | ||
} | ||
|
||
#[derive(Debug, Deserialize, PartialEq)] | ||
pub struct Size { | ||
#[serde(rename = "Width", default)] | ||
pub width: u32, | ||
#[serde(rename = "Height", default)] | ||
pub height: u32, | ||
} | ||
|
||
#[test] | ||
fn test_dzi() { | ||
let dzi: DziFile = serde_xml_rs::from_str(r#" | ||
<Image | ||
Format="png" Overlap="2" TileSize="256"> | ||
<Size Height="3852" Width="5393"/> | ||
</Image>"# | ||
).unwrap(); | ||
assert_eq!(dzi.get_size().unwrap(), Vec2d { x: 5393, y: 3852 }); | ||
assert_eq!(dzi.get_tile_size(), Vec2d { x: 256, y: 256 }); | ||
assert_eq!(dzi.max_level(), 13); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
use std::sync::Arc; | ||
|
||
use custom_error::custom_error; | ||
use dzi_file::DziFile; | ||
|
||
use crate::dezoomer::*; | ||
|
||
mod dzi_file; | ||
|
||
#[derive(Default)] | ||
pub struct DziDezoomer; | ||
|
||
impl Dezoomer for DziDezoomer { | ||
fn name(&self) -> &'static str { | ||
"deepzoom" | ||
} | ||
|
||
fn zoom_levels(&mut self, data: &DezoomerInput) -> Result<ZoomLevels, DezoomerError> { | ||
let DezoomerInputWithContents { uri, contents } = data.with_contents()?; | ||
let levels = load_from_properties(uri, contents)?; | ||
Ok(levels) | ||
} | ||
} | ||
|
||
custom_error! {pub DziError | ||
XmlError{source: serde_xml_rs::Error} = "Unable to parse the dzi file: {source}", | ||
NoSize = "Expected a size in the DZI file", | ||
InvalidTileSize = "Invalid tile size", | ||
} | ||
|
||
impl From<DziError> for DezoomerError { | ||
fn from(err: DziError) -> Self { | ||
DezoomerError::Other { source: err.into() } | ||
} | ||
} | ||
|
||
fn load_from_properties(url: &str, contents: &[u8]) -> Result<ZoomLevels, DziError> { | ||
let image_properties: DziFile = serde_xml_rs::from_reader(contents)?; | ||
|
||
if image_properties.tile_size == 0 { | ||
return Err(DziError::InvalidTileSize); | ||
} | ||
|
||
let dot_pos = url.rfind('.').unwrap_or(url.len() - 1); | ||
let base_url = &Arc::new(format!("{}_files", &url[0..dot_pos])); | ||
|
||
let size = image_properties.get_size()?; | ||
let max_level = image_properties.max_level(); | ||
let levels = std::iter::successors( | ||
Some(size), | ||
|&size| { | ||
if size.x > 1 || size.y > 1 { | ||
Some(size.ceil_div(Vec2d::square(2))) | ||
} else { | ||
None | ||
} | ||
}, | ||
).enumerate() | ||
.map(|(level_num, size)| { | ||
DziLevel { | ||
base_url: Arc::clone(base_url), | ||
size, | ||
tile_size: image_properties.get_tile_size(), | ||
format: image_properties.format.clone(), | ||
level: max_level - level_num as u32, | ||
} | ||
}) | ||
.into_zoom_levels(); | ||
Ok(levels) | ||
} | ||
|
||
struct DziLevel { | ||
base_url: Arc<String>, | ||
size: Vec2d, | ||
tile_size: Vec2d, | ||
format: String, | ||
level: u32, | ||
} | ||
|
||
impl TilesRect for DziLevel { | ||
fn size(&self) -> Vec2d { | ||
self.size | ||
} | ||
|
||
fn tile_size(&self) -> Vec2d { | ||
self.tile_size | ||
} | ||
|
||
fn tile_url(&self, pos: Vec2d) -> String { | ||
format!( | ||
"{base}/{level}/{x}_{y}.{format}", | ||
base = self.base_url, | ||
level = self.level, | ||
x = pos.x, | ||
y = pos.y, | ||
format = self.format | ||
) | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for DziLevel { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "Deep Zoom Image") | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_panorama() { | ||
let url = "http://x.fr/y/test.dzi"; | ||
let contents = br#" | ||
<Image | ||
TileSize="256" | ||
Overlap="2" | ||
Format="jpg" | ||
> | ||
<Size Width="600" Height="300"/> | ||
<DisplayRects></DisplayRects> | ||
</Image>"#; | ||
let mut props = load_from_properties(url, contents).unwrap(); | ||
assert_eq!(props.len(), 11); | ||
let level = &mut props[1]; | ||
let tiles: Vec<String> = level.next_tiles(None).into_iter().map(|t| t.url).collect(); | ||
assert_eq!( | ||
tiles, | ||
vec![ | ||
"http://x.fr/y/test_files/9/0_0.jpg", | ||
"http://x.fr/y/test_files/9/1_0.jpg" | ||
] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters