Refactor Orientation into Operation in image::raster

This commit is contained in:
Héctor Ramón Jiménez 2021-09-30 16:11:30 +07:00
parent 4fd2e1a57d
commit 21d138aa28
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 42 additions and 53 deletions

View file

@ -35,6 +35,7 @@ log = "0.4"
guillotiere = "0.6" guillotiere = "0.6"
futures = "0.3" futures = "0.3"
kamadak-exif = "0.5" kamadak-exif = "0.5"
bitflags = "1.3"
[dependencies.bytemuck] [dependencies.bytemuck]
version = "1.4" version = "1.4"

View file

@ -2,6 +2,8 @@ use crate::image::atlas::{self, Atlas};
use iced_native::image; use iced_native::image;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use bitflags::bitflags;
#[derive(Debug)] #[derive(Debug)]
pub enum Memory { pub enum Memory {
Host(::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>>), Host(::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>>),
@ -43,27 +45,27 @@ impl Cache {
let memory = match handle.data() { let memory = match handle.data() {
image::Data::Path(path) => { image::Data::Path(path) => {
if let Ok(image) = ::image_rs::open(path) { if let Ok(image) = ::image_rs::open(path) {
let orientation = std::fs::File::open(path) let operation = std::fs::File::open(path)
.ok() .ok()
.map(std::io::BufReader::new) .map(std::io::BufReader::new)
.and_then(|mut reader| { .and_then(|mut reader| {
Orientation::from_exif(&mut reader).ok() Operation::from_exif(&mut reader).ok()
}) })
.unwrap_or(Orientation::Default); .unwrap_or_else(Operation::empty);
Memory::Host(orientation.apply(image.to_bgra8())) Memory::Host(operation.perform(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 = Orientation::from_exif( let operation =
&mut std::io::Cursor::new(bytes), Operation::from_exif(&mut std::io::Cursor::new(bytes))
) .ok()
.unwrap_or(Orientation::Default); .unwrap_or_else(Operation::empty);
Memory::Host(orientation.apply(image.to_bgra8())) Memory::Host(operation.perform(image.to_bgra8()))
} else { } else {
Memory::Invalid Memory::Invalid
} }
@ -146,66 +148,52 @@ impl Cache {
} }
} }
#[derive(Debug, Clone, Copy)] bitflags! {
enum Orientation { struct Operation: u8 {
Default, const FLIP_HORIZONTALLY = 0b001;
FlippedHorizontally, const ROTATE_180 = 0b010;
FlippedVertically, const FLIP_DIAGONALLY = 0b100;
Rotated90, }
Rotated180,
Rotated270,
Rotated90AndFlippedHorizontally,
Rotated90AndFlippedVertically,
} }
impl Orientation { impl Operation {
// Meaning of the returned value is described e.g. at: // Meaning of the returned value is described e.g. at:
// https://magnushoff.com/articles/jpeg-orientation/ // https://magnushoff.com/articles/jpeg-orientation/
fn from_exif<R>(reader: &mut R) -> Result<Self, exif::Error> fn from_exif<R>(reader: &mut R) -> Result<Self, exif::Error>
where where
R: std::io::BufRead + std::io::Seek, R: std::io::BufRead + std::io::Seek,
{ {
use std::convert::TryFrom;
let exif = exif::Reader::new().read_from_container(reader)?; let exif = exif::Reader::new().read_from_container(reader)?;
Ok(exif Ok(exif
.get_field(::exif::Tag::Orientation, ::exif::In::PRIMARY) .get_field(::exif::Tag::Orientation, ::exif::In::PRIMARY)
.and_then(|field| field.value.get_uint(0)) .and_then(|field| field.value.get_uint(0))
.map(|value| match value { .and_then(|value| u8::try_from(value).ok())
2 => Orientation::FlippedHorizontally, .and_then(|value| Self::from_bits(value.saturating_sub(1)))
3 => Orientation::Rotated180, .unwrap_or_else(Self::empty))
4 => Orientation::FlippedVertically,
5 => Orientation::Rotated90AndFlippedHorizontally,
6 => Orientation::Rotated90,
7 => Orientation::Rotated90AndFlippedVertically,
8 => Orientation::Rotated270,
_ => Orientation::Default,
})
.unwrap_or(Orientation::Default))
} }
fn apply( fn perform<I>(self, mut image: I) -> I
self, where
mut img: ::image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>>, I: image_rs::GenericImage,
) -> image_rs::ImageBuffer<::image_rs::Bgra<u8>, Vec<u8>> { {
use image_rs::imageops::*; use image_rs::imageops;
match self { if self.contains(Self::FLIP_DIAGONALLY) {
Self::FlippedHorizontally => flip_horizontal_in_place(&mut img), imageops::flip_horizontal_in_place(&mut image);
Self::Rotated180 => rotate180_in_place(&mut img), imageops::flip_vertical_in_place(&mut image);
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 if self.contains(Self::ROTATE_180) {
imageops::rotate180_in_place(&mut image);
}
if self.contains(Self::FLIP_HORIZONTALLY) {
imageops::flip_horizontal_in_place(&mut image);
}
image
} }
} }