Skip to content

Commit

Permalink
apply orientation EXIF to single image containers
Browse files Browse the repository at this point in the history
  • Loading branch information
o-tho committed Dec 1, 2024
1 parent bd58ac6 commit 9cc8efd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ image = {version = "0.25", default-features = false, features = [
"jpeg",
"png",
"tiff",
"pnm",
"bmp"
]}
nalgebra = "0.33.2"
serde = {version = "1.0.214", features= ["derive"]}
Expand Down
21 changes: 20 additions & 1 deletion src/image_container.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::image_helpers::{binary_image_from_image, fax_to_grayimage};
use crate::ErrorWrapper;
use image::{DynamicImage, GrayImage};
use image::{DynamicImage, GrayImage, ImageDecoder};

use pdf::any::AnySync;
use pdf::file::NoLog;
Expand Down Expand Up @@ -28,6 +28,25 @@ pub struct SingleImageContainer {
pub image: DynamicImage,
}

impl SingleImageContainer {
pub fn from_data_with_format(data: &[u8], format: image::ImageFormat) -> Self {
let reader = image::ImageReader::with_format(std::io::Cursor::new(data), format);
if let Ok(mut decoder) = reader.into_decoder() {
let orientation = decoder
.orientation()
.unwrap_or(image::metadata::Orientation::NoTransforms);

if let Ok(mut dynimage) = image::DynamicImage::from_decoder(decoder) {
dynimage.apply_orientation(orientation);

return SingleImageContainer { image: dynimage };
}
}

panic!("could not decode single image!");
}
}

pub trait ImageContainer {
fn get_page(&mut self, n: usize) -> Result<GrayImage, ErrorWrapper>;

Expand Down
7 changes: 0 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,6 @@ fn main() -> Result<(), ErrorWrapper> {
_ => println!("Unsupported file type: {:?}", imagefile),
}
}
Some(("test", _sub_matches)) => {
println!("foo");
let t: Template =
serde_json::from_reader(std::fs::File::open("private/40qtemplate.json")?)?;
let outfile = std::fs::File::create("cbortest.cbor")?;
let _ = serde_cbor::to_writer(outfile, &t);
}
Some(("debug", sub_matches)) => {
let templatepath = sub_matches
.get_one::<String>("template")
Expand Down
16 changes: 8 additions & 8 deletions src/webapp/generate_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ fn raw_data_to_container(data: &Vec<u8>) -> Option<Box<dyn ImageContainer + '_>>
return Some(Box::new(container));
}
"image/png" => {
let image =
image::load_from_memory_with_format(&clonedata, image::ImageFormat::Png)
.unwrap();
let container = SingleImageContainer { image: image };
let container = SingleImageContainer::from_data_with_format(
&clonedata,
image::ImageFormat::Png,
);
return Some(Box::new(container));
}
"image/jpeg" => {
let image =
image::load_from_memory_with_format(&clonedata, image::ImageFormat::Jpeg)
.unwrap();
let container = SingleImageContainer { image: image };
let container = SingleImageContainer::from_data_with_format(
&clonedata,
image::ImageFormat::Jpeg,
);
return Some(Box::new(container));
}
_ => log::error!(
Expand Down

0 comments on commit 9cc8efd

Please sign in to comment.