LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
//! Minolta / Konica Minolta MRW: a block container (see [`lightcraft_meta::mrw`]) around a 12-bit Bayer mosaic.
//!
//! Sources: the public prose description of the MRW layout (Dalibor Jelínek's MRW notes) and our own black-box
//! analysis of the eight CC0 MRW samples on raw.pixls.us (DiMAGE 7, 7Hi, A1, A2, A200, Dynax / Maxxum / ALPHA-7
//! Digital); every number below was measured on them.
//!
//! - `PRD` (24 bytes, big-endian): 8 ASCII version bytes, sensor rows and columns (`u16` each), image rows and
//! columns, data size (12 or 16 bits per stored sample), pixel size (12), storage method (`0x59` = 12-bit
//! packed: two samples in three bytes, most significant bits first; `0x52` = one 12-bit sample per big-endian
//! 16-bit word), two unknown bytes and the Bayer layout as a `u16` (`1` = RGGB, `4` = GBRG). The sensor data
//! starts after the header and is exactly sensor rows x columns samples in every sample.
//! - The sensor frame is 8 columns and 6 to 8 rows larger than the image size, but every edge holds picture (no
//! masked border), so the whole frame is the active area and the image size is the default crop.
//! - Black is 0: the darkest samples are 0 in every packed file and 13..34 in the unpacked ones, with no offset
//! left to subtract. White is the saturation plateau where the frame has one (3964 on the A1, 3965 on a clipped
//! DiMAGE 7, 4020 on the A200, 4076 on the 7D), else 4095.
//! - The layout code was checked against the embedded preview: with `1` the two greens are the equal (0,1) and
//! (1,0) positions; in the A200 (`4`) the greens are (0,0) and (1,1), and only red at (1,0) reproduces the
//! preview's red-dominated colour.
//! - `WBG` (12 bytes): four scale bytes, then four big-endian `u16` coefficients in the order of the Bayer cell
//! read row by row (RGGB: R G G B, GBRG: G B R G). Red over green and blue over green are the as-shot gains;
//! applied to the raw channel means they reproduce the preview's colour balance within the colour matrix's
//! reach on all eight samples.
//! - `TTW` is a TIFF with IFD0 (orientation), Exif and the maker note; the preview JPEG sits in it with its first
//! byte overwritten (see `preview::scan_for_jpeg`).
use crate::{BlackLevel, Cfa, ColorData, Mode, OpcodeLists, RawData, RawError, RawFormat, RawImage, Rect, Result, unpack};
use lightcraft_geom::Orientation;
use lightcraft_tiff::{Tiff, tags as t};
/// Storage method codes in `PRD`.
const PACKED_12: u8 = 0x59;
const WORDS_16: u8 = 0x52;
/// What `PRD` says about the sensor data.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Prd {
sensor: (usize, usize),
image: (usize, usize),
packed: bool,
cfa: &'static str,
}
fn be16(b: &[u8], at: usize) -> Option<u16> {
b.get(at..at + 2).map(|s| u16::from_be_bytes([s[0], s[1]]))
}
fn prd(b: &[u8]) -> Result<Prd> {
let u = |at| be16(b, at).map(usize::from).ok_or_else(|| RawError::Corrupt("Mrw PRD block too short".into()));
let (sh, sw, ih, iw) = (u(8)?, u(10)?, u(12)?, u(14)?);
let (size, storage) = (b.get(16).copied(), b.get(18).copied());
let packed = match (size, storage) {
(Some(12), Some(PACKED_12)) => true,
(Some(16), Some(WORDS_16)) => false,
_ => return Err(RawError::Unsupported(format!("Mrw storage {size:?}/{storage:?} is not decoded yet"))),
};
let cfa = match u(22)? {
1 => "RGGB",
4 => "GBRG",
other => return Err(RawError::Unsupported(format!("Mrw Bayer layout {other}"))),
};
if sw == 0 || sh == 0 || (packed && sw % 2 != 0) {
return Err(RawError::Corrupt("Mrw sensor size".into()));
}
Ok(Prd { sensor: (sw, sh), image: (iw.min(sw), ih.min(sh)), packed, cfa })
}
pub(crate) fn decode(bytes: &[u8], mode: Mode) -> Result<RawImage> {
let m = lightcraft_meta::mrw::parse(bytes).ok_or_else(|| RawError::Corrupt("Mrw block header".into()))?;
let p = prd(m.block(b"PRD").ok_or_else(|| RawError::Unsupported("Mrw file without a PRD block".into()))?)?;
let (w, h) = p.sensor;
let n = w.checked_mul(h).filter(|n| *n <= crate::MAX_SAMPLES).ok_or(RawError::Limit("image too large"))?;
let need = if p.packed { n / 2 * 3 } else { n * 2 };
let src = bytes
.get(m.data_offset..)
.and_then(|s| s.get(..need))
.ok_or_else(|| RawError::Corrupt(format!("Mrw sensor data truncated (needs {need} bytes)")))?;
let cfa = Cfa::bayer_static(p.cfa);
let (data, white) = if mode == Mode::Full {
let mut d = vec![0u16; n];
if p.packed {
unpack::unpack_msb(src, 12, &mut d);
} else {
unpack::read_u16s(src, lightcraft_tiff::ByteOrder::Big, &mut d);
for v in &mut d {
*v = (*v).min(4095);
}
}
let white = super::white_from_data(&d, 12);
(RawData::U16(d), white)
} else {
(RawData::U16(Vec::new()), 4095.0)
};
let wb = m.block(b"WBG").and_then(|b| wb_from(b, &cfa));
let tiff = m.block(b"TTW").and_then(|b| Tiff::parse(b).ok());
let mut metadata = tiff.as_ref().map(lightcraft_meta::from_tiff).unwrap_or_default();
metadata.width = Some(p.image.0 as u32);
metadata.height = Some(p.image.1 as u32);
let orientation = tiff.as_ref().and_then(|t| t.ifds.first()).and_then(|i| i.u16(t::ORIENTATION)).unwrap_or(1);
let img = RawImage {
format: RawFormat::Mrw,
width: w,
height: h,
cpp: 1,
data,
cfa: Some(cfa),
bits: 12,
black: BlackLevel::uniform(0.0),
white: vec![white],
active_area: Rect::new(0, 0, w, h),
crop: Rect::new(0, 0, p.image.0, p.image.1),
orientation: Orientation::from_exif(orientation),
color: ColorData::default(),
wb_multipliers: wb,
linearized: false,
opcodes: OpcodeLists::default(),
metadata,
lens_metadata: Default::default(),
pixel_aspect: 1.0,
};
img.validate_for(mode)?;
Ok(img)
}
/// As-shot gains from `WBG`: four coefficients in Bayer-cell order after four scale bytes.
fn wb_from(b: &[u8], cfa: &Cfa) -> Option<[f32; 3]> {
let mut sum = [0f32; 3];
let mut count = [0f32; 3];
for (i, &c) in cfa.pattern.iter().enumerate().take(4) {
let v = f32::from(be16(b, 4 + 2 * i)?);
let k = usize::from(c).min(2);
sum[k] += v;
count[k] += 1.0;
}
if count.contains(&0.0) || sum.iter().any(|&s| s <= 0.0) {
return None;
}
let mean = |k: usize| sum[k] / count[k];
Some([mean(0) / mean(1), 1.0, mean(2) / mean(1)])
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
/// A synthetic MRW: `PRD` with the given storage, `WBG`, a `TTW` TIFF with orientation 6, then the samples.
pub(crate) fn sample(w: u16, h: u16, packed: bool, layout: u16, wbg: [u16; 4], samples: &[u16]) -> Vec<u8> {
let mut prd = b"21810002".to_vec();
for v in [h, w, h - 2, w - 2] {
prd.extend_from_slice(&v.to_be_bytes());
}
prd.extend_from_slice(&[if packed { 12 } else { 16 }, 12, if packed { PACKED_12 } else { WORDS_16 }, 0, 0, 0]);
prd.extend_from_slice(&layout.to_be_bytes());
let mut wb = vec![2u8; 4];
for v in wbg {
wb.extend_from_slice(&v.to_be_bytes());
}
let mut ifd0 = lightcraft_tiff::IfdBuilder::new();
ifd0.set(t::MAKE, lightcraft_tiff::Value::Ascii("Minolta Co., Ltd.".into()));
ifd0.set(t::ORIENTATION, lightcraft_tiff::Value::Short(vec![6]));
let ttw = lightcraft_tiff::TiffWriter::default().write(&[ifd0]).unwrap();
let mut body = Vec::new();
for (name, data) in [(b"PRD", &prd), (b"WBG", &wb), (b"TTW", &ttw)] {
body.push(0);
body.extend_from_slice(name);
body.extend_from_slice(&(data.len() as u32).to_be_bytes());
body.extend_from_slice(data);
}
let mut f = b"\0MRM".to_vec();
f.extend_from_slice(&(body.len() as u32).to_be_bytes());
f.extend_from_slice(&body);
if packed {
for p in samples.chunks(2) {
let (a, b) = (p[0], p.get(1).copied().unwrap_or(0));
f.extend_from_slice(&[(a >> 4) as u8, ((a & 15) << 4) as u8 | (b >> 8) as u8, b as u8]);
}
} else {
for v in samples {
f.extend_from_slice(&v.to_be_bytes());
}
}
f
}
#[test]
fn decodes_packed_and_word_storage() {
let (w, h) = (8u16, 6u16);
let px: Vec<u16> = (0..48).map(|i| (i * 85) as u16 % 4096).collect();
for packed in [true, false] {
let f = sample(w, h, packed, 1, [408, 256, 256, 450], &px);
assert_eq!(crate::probe(&f), Some(RawFormat::Mrw));
let r = crate::decode(&f).unwrap();
assert_eq!((r.width, r.height, r.bits), (8, 6, 12));
assert_eq!(r.data, RawData::U16(px.clone()), "packed {packed}");
assert_eq!(r.cfa.as_ref().map(Cfa::name).as_deref(), Some("RGGB"));
assert_eq!(r.crop, Rect::new(0, 0, 6, 4));
assert_eq!(r.orientation, Orientation::from_exif(6));
assert_eq!(r.metadata.make.as_deref(), Some("Minolta Co., Ltd."));
let wb = r.wb_multipliers.unwrap();
assert!((wb[0] - 408.0 / 256.0).abs() < 1e-6 && (wb[2] - 450.0 / 256.0).abs() < 1e-6, "{wb:?}");
assert_eq!(crate::probe_info(&f).unwrap(), r.info());
}
}
/// GBRG (layout 4): the coefficients follow the cell, so the second is blue and the third red.
#[test]
fn gbrg_white_balance_follows_the_cell() {
let f = sample(4, 4, true, 4, [255, 416, 480, 255], &[100; 16]);
let r = crate::decode(&f).unwrap();
assert_eq!(r.cfa.as_ref().map(Cfa::name).as_deref(), Some("GBRG"));
let wb = r.wb_multipliers.unwrap();
assert!((wb[0] - 480.0 / 255.0).abs() < 1e-6 && (wb[2] - 416.0 / 255.0).abs() < 1e-6, "{wb:?}");
}
/// Truncations and corrupted header bytes give errors, never panics.
#[test]
fn malformed_files_never_panic() {
let f = sample(8, 6, true, 1, [408, 256, 256, 450], &[7; 48]);
for n in 0..f.len() {
assert!(crate::decode(&f[..n]).is_err() || n == f.len());
}
for i in 8..f.len().min(120) {
for v in [0u8, 0xff, 0x52, 0x59] {
let mut g = f.clone();
g[i] = v;
let _ = crate::decode(&g);
let _ = crate::probe_info(&g);
}
}
assert!(matches!(crate::decode(b"\0MRM\0\x01\0\0"), Err(RawError::Corrupt(_))));
assert!(matches!(crate::decode(b"\0MRM\0\0\0\0"), Err(RawError::Unsupported(w)) if w.contains("Mrw")));
}
}