Raw-rs: Flip and rotate image based on camera orientation (#1954)

* add Orientation tag

* fix errors

* Apply the transformation to the image

* Fix BASE_PATH in raw-rs tests

* Create output folder if it doesn't exist

* Nit

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Elbert Ronnie
2024-09-05 15:18:58 -07:00
committed by GitHub
co-authored by Keavon Chambers
parent 70dacac474
commit d9ae01bf90
12 changed files with 147 additions and 8 deletions
+8 -1
View File
@@ -9,7 +9,8 @@ use crate::metadata::identify::CameraModel;
use tag_derive::Tag;
use tiff::file::TiffRead;
use tiff::tags::{Compression, ImageLength, ImageWidth, StripByteCounts, SubIfd, Tag};
use tiff::tags::{Compression, ImageLength, ImageWidth, Orientation, StripByteCounts, SubIfd, Tag};
use tiff::values::Transform;
use tiff::{Ifd, TiffError};
use std::io::{Read, Seek};
@@ -26,6 +27,7 @@ pub struct RawImage {
pub width: usize,
pub height: usize,
pub cfa_pattern: [u8; 4],
pub transform: Transform,
pub maximum: u16,
pub black: SubtractBlack,
pub camera_model: Option<CameraModel>,
@@ -42,6 +44,7 @@ pub struct Image<T> {
/// We can assume this will be 3 for all non-obscure, modern cameras.
/// See <https://github.com/GraphiteEditor/Graphite/pull/1923#discussion_r1725070342> for more information.
pub channels: u8,
pub transform: Transform,
pub rgb_to_camera: Option<[[f64; 3]; 3]>,
pub(crate) histogram: Option<[[usize; 0x2000]; 3]>,
}
@@ -60,6 +63,7 @@ pub fn decode<R: Read + Seek>(reader: &mut R) -> Result<RawImage, DecoderError>
let ifd = Ifd::new_first_ifd(&mut file)?;
let camera_model = metadata::identify::identify_camera_model(&ifd, &mut file).unwrap();
let transform = ifd.get_value::<Orientation, _>(&mut file)?;
let mut raw_image = if camera_model.model == "DSLR-A100" {
decoder::arw1::decode_a100(ifd, &mut file)
@@ -78,6 +82,7 @@ pub fn decode<R: Read + Seek>(reader: &mut R) -> Result<RawImage, DecoderError>
};
raw_image.camera_model = Some(camera_model);
raw_image.transform = transform;
Ok(raw_image)
}
@@ -90,6 +95,7 @@ pub fn process_8bit(raw_image: RawImage) -> Image<u8> {
data: image.data.iter().map(|x| (x >> 8) as u8).collect(),
width: image.width,
height: image.height,
transform: image.transform,
rgb_to_camera: image.rgb_to_camera,
histogram: image.histogram,
}
@@ -101,6 +107,7 @@ pub fn process_16bit(raw_image: RawImage) -> Image<u16> {
let raw_image = crate::preprocessing::scale_colors::scale_colors(raw_image);
let image = crate::demosaicing::linear_demosaicing::linear_demosaic(raw_image);
let image = crate::postprocessing::convert_to_rgb::convert_to_rgb(image);
let image = crate::postprocessing::transform::transform(image);
crate::postprocessing::gamma_correction::gamma_correction(image)
}