Introduce Orientation enum in image::raster
This commit is contained in:
parent
82e3e316dd
commit
03fee3106f
1 changed files with 72 additions and 37 deletions
|
|
@ -46,17 +46,24 @@ impl Cache {
|
||||||
let orientation = std::fs::File::open(path)
|
let orientation = std::fs::File::open(path)
|
||||||
.ok()
|
.ok()
|
||||||
.map(std::io::BufReader::new)
|
.map(std::io::BufReader::new)
|
||||||
.and_then(|mut reader| exif_orientation(&mut reader));
|
.and_then(|mut reader| {
|
||||||
Memory::Host(fix_orientation(image.to_bgra8(), orientation))
|
Orientation::from_exif(&mut reader).ok()
|
||||||
|
})
|
||||||
|
.unwrap_or(Orientation::Default);
|
||||||
|
|
||||||
|
Memory::Host(orientation.apply(image.to_bgra8()))
|
||||||
} else {
|
} else {
|
||||||
Memory::NotFound
|
Memory::NotFound
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
image::Data::Bytes(bytes) => {
|
image::Data::Bytes(bytes) => {
|
||||||
if let Ok(image) = ::image_rs::load_from_memory(&bytes) {
|
if let Ok(image) = ::image_rs::load_from_memory(&bytes) {
|
||||||
let orientation =
|
let orientation = Orientation::from_exif(
|
||||||
exif_orientation(&mut std::io::Cursor::new(bytes));
|
&mut std::io::Cursor::new(bytes),
|
||||||
Memory::Host(fix_orientation(image.to_bgra8(), orientation))
|
)
|
||||||
|
.unwrap_or(Orientation::Default);
|
||||||
|
|
||||||
|
Memory::Host(orientation.apply(image.to_bgra8()))
|
||||||
} else {
|
} else {
|
||||||
Memory::Invalid
|
Memory::Invalid
|
||||||
}
|
}
|
||||||
|
|
@ -139,38 +146,66 @@ impl Cache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fix_orientation(
|
#[derive(Debug, Clone, Copy)]
|
||||||
mut img: ::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>>,
|
enum Orientation {
|
||||||
orientation: Option<u32>,
|
Default,
|
||||||
) -> ::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>> {
|
FlippedHorizontally,
|
||||||
use ::image_rs::imageops::*;
|
FlippedVertically,
|
||||||
match orientation.unwrap_or(1) {
|
Rotated90,
|
||||||
2 => flip_horizontal_in_place(&mut img),
|
Rotated180,
|
||||||
3 => rotate180_in_place(&mut img),
|
Rotated270,
|
||||||
4 => flip_vertical_in_place(&mut img),
|
Rotated90AndFlippedHorizontally,
|
||||||
5 => {
|
Rotated90AndFlippedVertically,
|
||||||
img = rotate90(&img);
|
|
||||||
flip_horizontal_in_place(&mut img);
|
|
||||||
}
|
|
||||||
6 => img = rotate90(&img),
|
|
||||||
7 => {
|
|
||||||
img = rotate90(&img);
|
|
||||||
flip_vertical_in_place(&mut img);
|
|
||||||
}
|
|
||||||
8 => img = rotate270(&img),
|
|
||||||
_ => {}
|
|
||||||
};
|
|
||||||
img
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Meaning of the returned value is described e.g. at:
|
impl Orientation {
|
||||||
// https://magnushoff.com/articles/jpeg-orientation/
|
// Meaning of the returned value is described e.g. at:
|
||||||
fn exif_orientation<R>(reader: &mut R) -> Option<u32>
|
// https://magnushoff.com/articles/jpeg-orientation/
|
||||||
where
|
fn from_exif<R>(reader: &mut R) -> Result<Self, exif::Error>
|
||||||
R: std::io::BufRead + std::io::Seek,
|
where
|
||||||
{
|
R: std::io::BufRead + std::io::Seek,
|
||||||
let exif = ::exif::Reader::new().read_from_container(reader).ok()?;
|
{
|
||||||
exif.get_field(::exif::Tag::Orientation, ::exif::In::PRIMARY)?
|
let exif = ::exif::Reader::new().read_from_container(reader)?;
|
||||||
.value
|
|
||||||
.get_uint(0)
|
Ok(exif
|
||||||
|
.get_field(::exif::Tag::Orientation, ::exif::In::PRIMARY)
|
||||||
|
.and_then(|field| field.value.get_uint(0))
|
||||||
|
.map(|value| match value {
|
||||||
|
2 => Orientation::FlippedHorizontally,
|
||||||
|
3 => Orientation::Rotated180,
|
||||||
|
4 => Orientation::FlippedVertically,
|
||||||
|
5 => Orientation::Rotated90AndFlippedHorizontally,
|
||||||
|
6 => Orientation::Rotated90,
|
||||||
|
7 => Orientation::Rotated90AndFlippedVertically,
|
||||||
|
8 => Orientation::Rotated270,
|
||||||
|
_ => Orientation::Default,
|
||||||
|
})
|
||||||
|
.unwrap_or(Orientation::Default))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn apply(
|
||||||
|
self,
|
||||||
|
mut img: ::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>>,
|
||||||
|
) -> ::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>> {
|
||||||
|
use ::image_rs::imageops::*;
|
||||||
|
|
||||||
|
match self {
|
||||||
|
Self::FlippedHorizontally => flip_horizontal_in_place(&mut img),
|
||||||
|
Self::Rotated180 => rotate180_in_place(&mut img),
|
||||||
|
Self::FlippedVertically => flip_vertical_in_place(&mut img),
|
||||||
|
Self::Rotated90AndFlippedHorizontally => {
|
||||||
|
img = rotate90(&img);
|
||||||
|
flip_horizontal_in_place(&mut img);
|
||||||
|
}
|
||||||
|
Self::Rotated90 => img = rotate90(&img),
|
||||||
|
Self::Rotated90AndFlippedVertically => {
|
||||||
|
img = rotate90(&img);
|
||||||
|
flip_vertical_in_place(&mut img);
|
||||||
|
}
|
||||||
|
Self::Rotated270 => img = rotate270(&img),
|
||||||
|
Self::Default => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
img
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue