mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
craft-raw: a CR2 opens with the camera's aspect crop (AspectInfo), as a CR3 does (#1336)
This commit is contained in:
1 parent
19f851b8db
commit
1c927e5e4d
3 files changed
+80
-17
No files matched your search
+62
-3
@@ -24,6 +24,12 @@
|
||||
//! bytes (ColorData versions -3 and -4), which are paired up in the maker note's byte order first.
|
||||
//! - The same array lists the levels of the camera's white-balance presets, each with the colour temperature Canon
|
||||
//! gives it ([`kelvin_presets_from_color_data`]): a Kelvin scale for a body without colour matrices.
|
||||
//! - `AspectInfo` (maker note `0x009a`, as in CR3: aspect code, width, height, left, top): the in-camera aspect
|
||||
//! ratio (3:2, 4:3, 16:9, 1:1) is not applied to the sensor data, and the rectangle is measured from the top-left
|
||||
//! of the `SensorInfo` image area. It becomes the default crop (storytold/lightcraft#3313). Checked on the CC0
|
||||
//! PowerShot G12 and S120 files shot at 3:2 (3648 x 2432 at top 152 of a 3648 x 2736 area, 4000 x 2664 at top 168
|
||||
//! of 4000 x 3000: centred) and a G16 shot at 4:3 (the whole area); files shot at the sensor's own aspect store
|
||||
//! the whole area or no tag.
|
||||
//! - sRAW / mRAW (raw IFD tag `0xc6c5` = 4) hold subsampled YCbCr instead of a mosaic: see [`sraw`].
|
||||
|
||||
mod sraw;
|
||||
@@ -273,10 +279,14 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result<RawIma
|
||||
let mn =
|
||||
tiff.exif().and_then(|e| e.get(t::MAKER_NOTE)).and_then(|e| makernote::parse_makernote(bytes, e.offset, e.count() as u64, tiff.order, &make));
|
||||
let mut active = Rect::new(0, 0, width, height);
|
||||
let mut crop = Rect::new(0, 0, width, height);
|
||||
if let Some(si) = mn.as_ref().and_then(|m| m.ifd.u64s(SENSOR_INFO)).filter(|v| v.len() >= 9) {
|
||||
let (l, tp, r, b) = (si[5] as usize, si[6] as usize, si[7] as usize, si[8] as usize);
|
||||
if r > l && b > tp && r < width && b < height {
|
||||
active = Rect::new(l, tp, r - l + 1, b - tp + 1);
|
||||
// `AspectInfo` is measured from the image area's origin, so it only applies when that area is known.
|
||||
let image = Rect::new(0, 0, active.width, active.height);
|
||||
crop = crate::shared::aspect_crop(mn.as_ref().map(|m| &m.ifd), image).unwrap_or(image);
|
||||
}
|
||||
}
|
||||
let color_data = mn.as_ref().and_then(|m| m.ifd.value(COLOR_BALANCE).map(|v| words(v, m.order)));
|
||||
@@ -306,7 +316,7 @@ pub(crate) fn decode(bytes: &[u8], mode: Mode, limits: &Limits) -> Result<RawIma
|
||||
black,
|
||||
white: vec![white],
|
||||
active_area: active,
|
||||
crop: Rect::new(0, 0, active.width, active.height),
|
||||
crop,
|
||||
orientation: Orientation::from_exif(ifd0.u16(t::ORIENTATION).unwrap_or(1)),
|
||||
color: ColorData { kelvin_presets, ..Default::default() },
|
||||
wb_multipliers: wb,
|
||||
@@ -604,11 +614,20 @@ pub(crate) mod tests {
|
||||
/// A maker note of `entries` (tag, TIFF type, value bytes), each value stored after the IFD, decoded; the value
|
||||
/// offsets (relative to the TIFF header) are patched once the note's position is known.
|
||||
fn decode_with_note(entries: &[(u16, u16, Vec<u8>)]) -> RawImage {
|
||||
decode_sized_with_note(64, 10, entries)
|
||||
}
|
||||
|
||||
/// [`decode_with_note`] with a `w` x `h` sensor.
|
||||
fn decode_sized_with_note(w: usize, h: usize, entries: &[(u16, u16, Vec<u8>)]) -> RawImage {
|
||||
let ifd_len = 2 + 12 * entries.len() + 4;
|
||||
let (mut note, mut data, mut at) = (Vec::new(), Vec::new(), Vec::new());
|
||||
note.extend_from_slice(&(entries.len() as u16).to_le_bytes());
|
||||
for (tag, ty, value) in entries {
|
||||
let count = if *ty == 3 { value.len() / 2 } else { value.len() } as u32;
|
||||
let count = match ty {
|
||||
3 => value.len() / 2,
|
||||
4 => value.len() / 4,
|
||||
_ => value.len(),
|
||||
} as u32;
|
||||
note.extend_from_slice(&tag.to_le_bytes());
|
||||
note.extend_from_slice(&ty.to_le_bytes());
|
||||
note.extend_from_slice(&count.to_le_bytes());
|
||||
@@ -618,7 +637,7 @@ pub(crate) mod tests {
|
||||
}
|
||||
note.extend_from_slice(&0u32.to_le_bytes());
|
||||
note.extend_from_slice(&data);
|
||||
let (mut bytes, _) = synthetic_cr2_note(64, 10, None, Some(note));
|
||||
let (mut bytes, _) = synthetic_cr2_note(w, h, None, Some(note));
|
||||
let tiff = Tiff::parse(&bytes).unwrap();
|
||||
let off = tiff.exif().unwrap().get(t::MAKER_NOTE).unwrap().offset as usize;
|
||||
for (field, value) in at {
|
||||
@@ -766,4 +785,44 @@ pub(crate) mod tests {
|
||||
assert_eq!(r.active_area, Rect::new(left, top, w - left, h - top));
|
||||
assert!((13300.0..=13433.0).contains(&r.white[0]), "{:?}", r.white);
|
||||
}
|
||||
|
||||
/// A 68 x 50 sensor whose `SensorInfo` image area is the 64 x 48 (4:3) at (4, 2), with `aspect` as its
|
||||
/// `AspectInfo` (LONG x 5), decoded.
|
||||
fn decode_with_aspect(aspect: Option<[u32; 5]>) -> RawImage {
|
||||
let si: Vec<u8> = [68u16, 50, 0, 0, 0, 4, 2, 67, 49].iter().flat_map(|v| v.to_le_bytes()).collect();
|
||||
let mut entries = vec![(SENSOR_INFO, 3, si)];
|
||||
if let Some(a) = aspect {
|
||||
entries.push((crate::shared::ASPECT_INFO, 4, a.iter().flat_map(|v| v.to_le_bytes()).collect()));
|
||||
}
|
||||
decode_sized_with_note(68, 50, &entries)
|
||||
}
|
||||
|
||||
/// storytold/lightcraft#3313: a CR2 shot at 3:2 on a 4:3 sensor (PowerShot G12: `AspectInfo` 3648 x 2432 at
|
||||
/// top 152 of the 3648 x 2736 image area) opens with that rectangle as its crop, as a CR3 does; the active
|
||||
/// area is unchanged.
|
||||
#[test]
|
||||
fn aspect_info_replaces_the_crop() {
|
||||
let image = decode_with_aspect(Some([0, 64, 42, 0, 3]));
|
||||
assert_eq!(image.active_area, Rect::new(4, 2, 64, 48));
|
||||
assert_eq!(image.crop, Rect::new(0, 3, 64, 42));
|
||||
assert_eq!(image.info().developed_size(), (64, 42));
|
||||
// 1:1 with a left offset
|
||||
assert_eq!(decode_with_aspect(Some([1, 48, 48, 8, 0])).crop, Rect::new(8, 0, 48, 48));
|
||||
// the whole image area, as stored for a shot at the sensor's own aspect (G16 at 4:3), or no tag: unchanged
|
||||
for aspect in [Some([2, 64, 48, 0, 0]), None] {
|
||||
assert_eq!(decode_with_aspect(aspect).crop, Rect::new(0, 0, 64, 48), "{aspect:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Rectangles that don't fit the image area leave the crop alone and cannot overflow; without a usable
|
||||
/// `SensorInfo` the origin `AspectInfo` is measured from is unknown, so it isn't applied either.
|
||||
#[test]
|
||||
fn aspect_info_that_does_not_fit_is_ignored() {
|
||||
for bad in [[0, 65, 42, 0, 3], [0, 64, 42, 0, 7], [0, 64, 42, 1, 3], [0, 1, 42, 0, 0], [0, 64, 0, 0, 0], [0, u32::MAX, 42, 0, 0]] {
|
||||
assert_eq!(decode_with_aspect(Some(bad)).crop, Rect::new(0, 0, 64, 48), "{bad:?}");
|
||||
}
|
||||
let aspect = [0u32, 32, 8, 0, 1].iter().flat_map(|v| v.to_le_bytes()).collect();
|
||||
let image = decode_with_note(&[(crate::shared::ASPECT_INFO, 4, aspect)]);
|
||||
assert_eq!((image.active_area, image.crop), (Rect::new(0, 0, 64, 10), Rect::new(0, 0, 64, 10)));
|
||||
}
|
||||
}
|
||||
+2
-14
@@ -19,14 +19,13 @@
|
||||
//! Unknown camera metadata versions are ignored rather than assigned a guessed white balance.
|
||||
|
||||
use crate::crx;
|
||||
use crate::shared::{black_from_columns, white_from_data};
|
||||
use crate::shared::{aspect_crop, black_from_columns, white_from_data};
|
||||
use crate::{BlackLevel, Camera, Cfa, ColorData, Limits, Mode, OpcodeLists, Orientation, RawData, RawError, RawFormat, RawImage, Rect, Result};
|
||||
use craft_metadata::cr3::{Cr3ImageArea, Cr3Track, Cr3TrackKind, parse_cr3};
|
||||
use craft_tiff::{Ifd, Tiff};
|
||||
|
||||
const SENSOR_INFO: u16 = 0x00e0;
|
||||
const COLOR_DATA: u16 = 0x4001;
|
||||
const ASPECT_INFO: u16 = 0x009a;
|
||||
|
||||
/// Pick the first largest raw track. Equal-sized later tracks can be the Dual Pixel delta, which
|
||||
/// must never replace the main sensor image. An invalid full-size descriptor is reported, not
|
||||
@@ -93,18 +92,6 @@ fn sensor_crop(maker: Option<&Ifd>, width: usize, height: usize) -> Option<Rect>
|
||||
)
|
||||
}
|
||||
|
||||
/// The shot's aspect-ratio rectangle from `AspectInfo`, placed inside `image` (the recommended crop). `None`
|
||||
/// when the tag is absent or malformed, or its rectangle doesn't fit inside `image`.
|
||||
fn aspect_crop(maker: Option<&Ifd>, image: Rect) -> Option<Rect> {
|
||||
let data = maker?.u64s(ASPECT_INFO)?;
|
||||
let [width, height, left, top] = [1usize, 2, 3, 4].map(|i| data.get(i).and_then(|&v| usize::try_from(v).ok()));
|
||||
let (width, height, left, top) = (width?, height?, left?, top?);
|
||||
if width < 2 || height < 2 || left.checked_add(width)? > image.width || top.checked_add(height)? > image.height {
|
||||
return None;
|
||||
}
|
||||
Some(Rect::new(image.x.checked_add(left)?, image.y.checked_add(top)?, width, height))
|
||||
}
|
||||
|
||||
fn geometry(area: Option<&Cr3ImageArea>, maker: Option<&Ifd>, width: usize, height: usize) -> (Rect, Rect) {
|
||||
let area = area.filter(|a| usize::from(a.width) == width && usize::from(a.height) == height);
|
||||
let sensor = sensor_crop(maker, width, height);
|
||||
@@ -385,6 +372,7 @@ fn extend_white(image: &mut RawImage) -> bool {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::shared::ASPECT_INFO;
|
||||
use craft_tiff::{IfdBuilder, Value};
|
||||
|
||||
fn maker(version: u16, wb_at: usize, black_at: usize, white_at: usize) -> Ifd {
|
||||
|
||||
@@ -223,6 +223,22 @@ fn solve4(mut a: [[f64; 4]; 4], mut b: [f64; 4]) -> Option<[f64; 4]> {
|
||||
x.iter().all(|v| v.is_finite()).then_some(x)
|
||||
}
|
||||
|
||||
/// Canon maker-note `AspectInfo` (`0x009a`): aspect code, width, height, left, top, as 32-bit values.
|
||||
pub(crate) const ASPECT_INFO: u16 = 0x009a;
|
||||
|
||||
/// The shot's aspect-ratio rectangle from a Canon maker note's `AspectInfo`, placed inside `image` (the image area
|
||||
/// it is measured from). `None` when the tag is absent or malformed, or its rectangle doesn't fit inside `image`.
|
||||
/// Used by CR2 and CR3, so it lives here rather than behind either decoder's feature.
|
||||
pub(crate) fn aspect_crop(maker: Option<&craft_tiff::Ifd>, image: Rect) -> Option<Rect> {
|
||||
let data = maker?.u64s(ASPECT_INFO)?;
|
||||
let [width, height, left, top] = [1usize, 2, 3, 4].map(|i| data.get(i).and_then(|&v| usize::try_from(v).ok()));
|
||||
let (width, height, left, top) = (width?, height?, left?, top?);
|
||||
if width < 2 || height < 2 || left.checked_add(width)? > image.width || top.checked_add(height)? > image.height {
|
||||
return None;
|
||||
}
|
||||
Some(Rect::new(image.x.checked_add(left)?, image.y.checked_add(top)?, width, height))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) fn declare_strip_len(file: &mut [u8], len: u32, declared: u32) {
|
||||
let big = file.starts_with(b"MM");
|
||||
|
||||
Reference in new issue
Block a user