Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(io): a developed camera raw keeps the raw's EXIF for File Info's Camera Data (#6957)

Korg1ch7296d0d8b252
diff --git a/crates/codecs/src/codecs/tiff_exif.rs b/crates/codecs/src/codecs/tiff_exif.rs
index 00d47d070..2e6885b6e 100644
--- a/crates/codecs/src/codecs/tiff_exif.rs
+++ b/crates/codecs/src/codecs/tiff_exif.rs
@@ -20,6 +20,10 @@ use super::tiff_ifd::{File, Ifd};
pub(crate) const TAG_EXIF_IFD: u16 = 34665;
/// IFD0's pointer to the GPS directory.
pub(crate) const TAG_GPS_IFD: u16 = 34853;
+/// The Exif directory's PixelXDimension and PixelYDimension: the size of the image the
+/// directory describes.
+const TAG_PIXEL_X_DIMENSION: u16 = 40962;
+const TAG_PIXEL_Y_DIMENSION: u16 = 40963;
/// The Exif directory's pointer to the Interoperability directory (not carried: it holds only
/// the interoperability index, and a copied pointer would point nowhere).
const TAG_INTEROP_IFD: u16 = 40965;
@@ -92,7 +96,20 @@ fn sub(f: &File<'_>, ifd: &Ifd, tag: u16) -> Option<Ifd> {
/// is left are left out (the small descriptive directories come first, so a huge maker note
/// cannot crowd out the GPS position).
pub(crate) fn read(b: &[u8]) -> Option<ExifDirs> {
- let f = File::parse(b)?;
+ read_file(File::parse(b)?)
+}
+
+/// The EXIF parts of a camera raw built on TIFF (NEF, ARW, CR2, DNG, Panasonic RW2), as
+/// [`read`] gives them, without the Exif directory's PixelXDimension and PixelYDimension: in a
+/// raw they give the sensor's or the preview's size, not the developed image's.
+pub(crate) fn read_raw(b: &[u8]) -> Option<ExifDirs> {
+ let mut dirs = read_file(File::parse_raw(b)?)?;
+ dirs.exif.retain(|f| !matches!(f.tag, TAG_PIXEL_X_DIMENSION | TAG_PIXEL_Y_DIMENSION));
+ Some(dirs)
+}
+
+fn read_file(f: File<'_>) -> Option<ExifDirs> {
+ let b = f.b;
if f.big {
return None;
}
diff --git a/crates/codecs/src/codecs/tiff_ifd.rs b/crates/codecs/src/codecs/tiff_ifd.rs
index 199d44ecc..63b39258c 100644
--- a/crates/codecs/src/codecs/tiff_ifd.rs
+++ b/crates/codecs/src/codecs/tiff_ifd.rs
@@ -119,6 +119,20 @@ impl<'a> File<'a> {
Some(f)
}
+ /// [`parse`](Self::parse), also taking a Panasonic RW2 raw, whose classic little-endian TIFF
+ /// header has 0x55 (`IIU\0`) in place of 42.
+ pub fn parse_raw(b: &'a [u8]) -> Option<Self> {
+ if let Some(f) = Self::parse(b) {
+ return Some(f);
+ }
+ let mut f = File { b, le: true, big: false, first_ifd: 0 };
+ if b.get(0..2)? != b"II" || f.u16_at(2)? != 0x55 {
+ return None;
+ }
+ f.first_ifd = u64::from(f.u32_at(4)?);
+ Some(f)
+ }
+
/// Length of the header; no directory can start inside it.
pub fn header_len(&self) -> u64 {
if self.big { 16 } else { 8 }
diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs
index 86611599d..e454ccf5f 100644
--- a/crates/codecs/src/lib.rs
+++ b/crates/codecs/src/lib.rs
@@ -164,6 +164,18 @@ pub fn tiff_page_orientation(bytes: &[u8], page: usize) -> u16 {
tiff::orientation(bytes, Some(page))
}
+/// The EXIF block (`Metadata::exif`) of a camera raw built on TIFF (NEF, ARW, CR2, DNG,
+/// Panasonic RW2), for the image developed from it: IFD0's descriptive tags (make, model,
+/// date…) and the Exif and GPS directories, as a TIFF opens with. Orientation is not part of
+/// it, nor the Exif directory's pixel dimensions (the sensor's or the preview's size). When those
+/// were all its Exif directory held, the block is IFD0's tags alone, so the camera's make and
+/// model are still kept. `None` when the file is not built on a classic TIFF stream or has none
+/// of these tags.
+pub fn raw_exif(bytes: &[u8]) -> Option<Vec<u8>> {
+ let dirs = crate::codecs::tiff_exif::read_raw(bytes).filter(|d| d.has_directories() || !d.ifd0.is_empty())?;
+ Some(crate::codecs::tiff_exif::block(&dirs))
+}
+
/// The limits check and the orientation, shared by every decoder.
fn finish_decode(img: Image, o: u16, opts: &DecodeOptions) -> Result<Image, CodecError> {
let l = &opts.limits;
diff --git a/crates/io/src/raw.rs b/crates/io/src/raw.rs
index 008173839..72422dfa5 100644
--- a/crates/io/src/raw.rs
+++ b/crates/io/src/raw.rs
@@ -34,7 +34,7 @@ fn limits() -> Limits {
/// A raw file's embedded JPEG preview, turned upright. Its own EXIF orientation wins when it
/// has one; otherwise the raw's IFD0 orientation applies (TIFF-based raws record it there, and
/// their previews are usually stored as the sensor reads out). Developed raws are oriented by
-/// `photocraft-raw` and carry no EXIF, so nothing is turned twice.
+/// `photocraft-raw` and their EXIF carries no orientation, so nothing is turned twice.
fn upright_preview(raw: &[u8], jpeg: &[u8]) -> Result<Image, IoError> {
let img = codecs::decode_as_with(Format::Jpeg, jpeg, &codecs::DecodeOptions { keep_orientation: true, ..Default::default() })?;
let own = img.meta.exif.as_deref().map_or(1, codecs::exif_orientation);
@@ -111,7 +111,7 @@ pub fn import_raw_tuned(name: &str, bytes: &[u8], tuning: &RawTuning) -> Result<
.map(|(k, v)| format!(", {k} {v:+.0}"))
.collect::<String>();
let summary = format!("developed in Camera Raw (exposure {:+.2} EV{tone}, {wb})", tuning.exposure);
- Ok((developed_document(name, format, dev, &summary)?, wb_applied))
+ Ok((developed_document(name, bytes, format, dev, &summary)?, wb_applied))
}
/// The tone curve `bytes` develop with by default: the fitted look's for a file without colour
@@ -164,8 +164,11 @@ fn wb_gains(temperature: f32, tint: f32) -> [f64; 3] {
}
/// A developed raw as a 16-bit ProPhoto document, with a note saying how it was developed.
-fn developed_document(name: &str, format: &str, dev: photocraft_raw::Developed, summary: &str) -> Result<ImportResult, IoError> {
- let img = Image::from_u16(dev.width, dev.height, ChannelLayout::Rgb, &dev.rgb)?;
+fn developed_document(name: &str, bytes: &[u8], format: &str, dev: photocraft_raw::Developed, summary: &str) -> Result<ImportResult, IoError> {
+ let mut img = Image::from_u16(dev.width, dev.height, ChannelLayout::Rgb, &dev.rgb)?;
+ // The raw's own EXIF (camera, lens, exposure, date), for File Info's Camera Data. It carries
+ // no orientation: the developed image is already upright.
+ img.meta.exif = codecs::raw_exif(bytes);
let mut r = image_to_document(name, &img)?;
r.document.icc_profile = Some(Arc::new(photocraft_cms::Builtin::ProPhotoCompat.profile().to_bytes().to_vec()));
let camera = camera_name(dev.info.make.as_deref(), dev.info.model.as_deref());
@@ -253,7 +256,7 @@ pub fn import_raw_with(name: &str, bytes: &[u8], opts: &DevelopOptions) -> Resul
match develop_fitted(bytes, opts) {
Ok(dev) => {
let summary = format!("{DEVELOPED_NOTE} ({} demosaic, as-shot white balance)", opts.demosaic.id());
- developed_document(name, format, dev, &summary)
+ developed_document(name, bytes, format, dev, &summary)
}
Err(RawError::Unsupported(reason)) => match photocraft_raw::embedded_preview(bytes) {
Some(p) => {