mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge PR #7791: Camera raw: a developed raw keeps its EXIF (RW2 included), so File Info shows its Camera Data (#6957)
This commit is contained in:
commit
a0d6bf2dcd
5 files changed
+164
-6
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -172,6 +172,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;
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -46,6 +46,118 @@ fn cr2_opens() {
|
||||
assert_eq!(r.document.depth, SampleType::U16);
|
||||
}
|
||||
|
||||
/// #6957: a developed raw keeps the raw's EXIF (camera, exposure), so File Info shows its Camera
|
||||
/// Data. The raw's Orientation is left out: the developed image is already upright.
|
||||
#[test]
|
||||
fn a_developed_raw_keeps_its_exif() {
|
||||
let (w, h) = (48, 16);
|
||||
let data = mosaic(&scene(w, h), w, [0, 1, 1, 2], 256, 12000);
|
||||
let spec =
|
||||
Cr2Spec { width: w, height: h, data, precision: 14, components: 2, slices: vec![24, 24], borders: None, wb_rggb: None, orientation: 6, model_id: None };
|
||||
let r = import("IMG_0001.CR2", &spec.build()).unwrap();
|
||||
assert!(r.warnings.iter().any(|w| w.contains(photocraft_io::raw::DEVELOPED_NOTE)), "developed, not a preview: {:?}", r.warnings);
|
||||
let exif = r.document.metadata.exif.as_deref().expect("the raw's EXIF");
|
||||
let camera = photocraft_algo::exif::read(exif);
|
||||
assert_eq!(camera.make.as_deref(), Some("Canon"));
|
||||
assert_eq!(camera.model.as_deref(), Some("Canon EOS Synthetic"));
|
||||
assert_eq!(camera.exposure_time, Some(0.01));
|
||||
assert_eq!(photocraft_codecs::exif_orientation(exif), 1, "no orientation to apply a second time");
|
||||
assert_eq!((r.document.size.width, r.document.size.height), (16, 48));
|
||||
}
|
||||
|
||||
/// The tags of the Exif directory of an EXIF block (a little-endian TIFF stream).
|
||||
fn exif_ifd_tags(b: &[u8]) -> Vec<u16> {
|
||||
assert_eq!(&b[..2], b"II");
|
||||
let u16_at = |i: usize| u16::from_le_bytes([b[i], b[i + 1]]);
|
||||
let u32_at = |i: usize| u32::from_le_bytes([b[i], b[i + 1], b[i + 2], b[i + 3]]) as usize;
|
||||
let entries = |at: usize| (0..usize::from(u16_at(at))).map(move |k| at + 2 + 12 * k);
|
||||
let ifd0 = u32_at(4);
|
||||
let exif = entries(ifd0).find(|&e| u16_at(e) == 34665).map(|e| u32_at(e + 8)).expect("an Exif directory");
|
||||
entries(exif).map(u16_at).collect()
|
||||
}
|
||||
|
||||
/// A Panasonic RW2 (its TIFF header has 0x55 for 42) with an Exif directory: the developed
|
||||
/// document keeps it, without the Exif pixel dimensions (the sensor's, not the image's).
|
||||
#[test]
|
||||
fn a_developed_rw2_keeps_its_exif() {
|
||||
let (w, h, bits) = (20usize, 12usize, 12u32);
|
||||
let data: Vec<u16> = (0..w * h).map(|i| (i * 13 % 4000) as u16 + 200).collect();
|
||||
let mut t = TiffBuilder::default();
|
||||
let raw = t.blob(photocraft_raw::testgen::rw2_pack(&data, w, bits));
|
||||
let exif = t.ifd(vec![(33434, Val::Rational(vec![(1, 60)])), (40962, Val::Long(vec![w as u32])), (40963, Val::Long(vec![h as u32]))]);
|
||||
let white = ((1u32 << bits) - 1) as u16;
|
||||
let ifd0 = t.ifd(vec![
|
||||
(0x0002, Val::Short(vec![w as u16])),
|
||||
(0x0003, Val::Short(vec![h as u16])),
|
||||
(0x0004, Val::Short(vec![2])),
|
||||
(0x0005, Val::Short(vec![2])),
|
||||
(0x0006, Val::Short(vec![h as u16 - 2])),
|
||||
(0x0007, Val::Short(vec![w as u16 - 2])),
|
||||
(0x0009, Val::Short(vec![1])),
|
||||
(0x000A, Val::Short(vec![bits as u16])),
|
||||
(0x000E, Val::Short(vec![white])),
|
||||
(0x000F, Val::Short(vec![white])),
|
||||
(0x0010, Val::Short(vec![white])),
|
||||
(0x002D, Val::Short(vec![5])),
|
||||
(0x010F, Val::Ascii("Panasonic".into())),
|
||||
(0x0110, Val::Ascii("DC-Synthetic".into())),
|
||||
(0x0112, Val::Short(vec![1])),
|
||||
(0x0118, Val::Blobs(vec![raw])),
|
||||
(34665, Val::Ifds(vec![exif])),
|
||||
]);
|
||||
t.chain = vec![ifd0];
|
||||
let mut bytes = t.build();
|
||||
bytes[2..4].copy_from_slice(b"U\0");
|
||||
let r = import("P1000001.RW2", &bytes).unwrap();
|
||||
assert!(r.warnings.iter().any(|w| w.contains(photocraft_io::raw::DEVELOPED_NOTE)), "developed, not a preview: {:?}", r.warnings);
|
||||
let exif = r.document.metadata.exif.as_deref().expect("the raw's EXIF");
|
||||
let camera = photocraft_algo::exif::read(exif);
|
||||
assert_eq!((camera.make.as_deref(), camera.model.as_deref()), (Some("Panasonic"), Some("DC-Synthetic")));
|
||||
assert!(camera.exposure_time.is_some_and(|t| (t - 1.0 / 60.0).abs() < 1e-9));
|
||||
let tags = exif_ifd_tags(exif);
|
||||
assert!(tags.contains(&33434), "{tags:?}");
|
||||
assert!(!tags.contains(&40962) && !tags.contains(&40963), "no Exif pixel dimensions: {tags:?}");
|
||||
}
|
||||
|
||||
/// A raw whose Exif directory holds only the pixel dimensions, which a developed image drops:
|
||||
/// IFD0's camera make and model are kept on their own rather than lost with the empty directory.
|
||||
#[test]
|
||||
fn a_developed_raw_keeps_its_camera_when_the_exif_directory_empties() {
|
||||
let (w, h, bits) = (20usize, 12usize, 12u32);
|
||||
let data: Vec<u16> = (0..w * h).map(|i| (i * 13 % 4000) as u16 + 200).collect();
|
||||
let mut t = TiffBuilder::default();
|
||||
let raw = t.blob(photocraft_raw::testgen::rw2_pack(&data, w, bits));
|
||||
let exif = t.ifd(vec![(40962, Val::Long(vec![w as u32])), (40963, Val::Long(vec![h as u32]))]);
|
||||
let white = ((1u32 << bits) - 1) as u16;
|
||||
let ifd0 = t.ifd(vec![
|
||||
(0x0002, Val::Short(vec![w as u16])),
|
||||
(0x0003, Val::Short(vec![h as u16])),
|
||||
(0x0004, Val::Short(vec![2])),
|
||||
(0x0005, Val::Short(vec![2])),
|
||||
(0x0006, Val::Short(vec![h as u16 - 2])),
|
||||
(0x0007, Val::Short(vec![w as u16 - 2])),
|
||||
(0x0009, Val::Short(vec![1])),
|
||||
(0x000A, Val::Short(vec![bits as u16])),
|
||||
(0x000E, Val::Short(vec![white])),
|
||||
(0x000F, Val::Short(vec![white])),
|
||||
(0x0010, Val::Short(vec![white])),
|
||||
(0x002D, Val::Short(vec![5])),
|
||||
(0x010F, Val::Ascii("Panasonic".into())),
|
||||
(0x0110, Val::Ascii("DC-Synthetic".into())),
|
||||
(0x0112, Val::Short(vec![1])),
|
||||
(0x0118, Val::Blobs(vec![raw])),
|
||||
(34665, Val::Ifds(vec![exif])),
|
||||
]);
|
||||
t.chain = vec![ifd0];
|
||||
let mut bytes = t.build();
|
||||
bytes[2..4].copy_from_slice(b"U\0");
|
||||
let r = import("P1000001.RW2", &bytes).unwrap();
|
||||
let exif = r.document.metadata.exif.as_deref().expect("the raw's camera, without an Exif directory");
|
||||
let camera = photocraft_algo::exif::read(exif);
|
||||
assert_eq!((camera.make.as_deref(), camera.model.as_deref()), (Some("Panasonic"), Some("DC-Synthetic")));
|
||||
assert_eq!(camera.exposure_time, None);
|
||||
}
|
||||
|
||||
/// A NEF-like file with Nikon's (undocumented) compression and a full-size
|
||||
/// baseline JPEG preview in IFD0.
|
||||
fn nef_with_preview() -> Vec<u8> {
|
||||
|
||||
Reference in new issue
Block a user