Repository navigation
Description
Offline smart previews lose source metadata needed for edits
Found while adding lossless smart previews (see PR #1400). The defect is on main b3d392b4 and the PR branch lossless-smart-previews at cf1c803a; the lossless fixture below isolates its effect from JPEG quantization.
Problem
Smart preview loading returns pixels and the stored camera tone curve with info set to None. Catalog reconstruction does not restore camera colour metadata, DNG local tone maps, or semantic mattes. Edits can render differently offline even when the proxy stores the reduced source pixels exactly.
The metadata loss exists on main at b3d392b4 and affects LCSP1 Compact too. The exact pixel counts below use LCSP2 Lossless from the PR branch at cf1c803a, so JPEG quantization does not affect the comparison. Code inspection establishes the Compact scope; these counts do not measure Compact.
Reproduction and fixture generation
In a checkout of PR #1400's branch, lossless-smart-previews at cf1c803a, save the code below as crates/engine/examples/repro_smart_metadata.rs. Run cargo run -p lightcraft-engine --example repro_smart_metadata, then remove that example. The code and generated fixtures do not need to be committed. It uses only existing workspace dependencies and generates all five fixtures in a fresh temporary directory. No downloaded or unpublished image is needed.
The generator creates a 96 by 64 Bayer DNG, a linear DNG with the same camera colour metadata, a float merge DNG, a DNG with a two-point gain map, and an oriented linear DNG with a semantic sky matte. The TIFF/DNG fields, sample formulas, colour matrices and edit settings are all specified in the code. Each case builds Lossless, asserts identical decoded source samples, renames the original, and compares online and offline 8-bit output with identical edits. The online job receives the saved original pixels and metadata; the offline job uses the normal filesystem loader and proxy fallback.
use lightcraft_develop::{DevelopSettings, LocalAdjustments, Mask, MaskComponent, MaskOp, MaskShape, WbMode};
use lightcraft_engine::{Session, files, media, smart};
use lightcraft_geom::Orientation;
use lightcraft_raster::Rgb32f;
use serde_json::json;
use std::{path::Path, sync::Arc};
fn scene(w: usize, h: usize) -> Rgb32f {
let mut s = Rgb32f::new(w, h);
for (i, p) in s.data.iter_mut().enumerate() {
let x = (i % w) as f32 / w as f32;
let y = (i / w) as f32 / h as f32;
*p = [x * 2.2 - 0.02, y * 0.7 + 0.01, (x + y) * 0.6];
}
s
}
fn synthetic_dng(linear: bool, mono: bool, tone: bool) -> Vec<u8> {
use lightcraft_raw::*;
let (w, h) = (96, 64);
let cpp = if linear && !mono { 3 } else { 1 };
let data = (0..w * h * cpp).map(|i| 256 + ((i * 97) % 12000) as u16).collect();
let mut raw = RawImage {
format: RawFormat::Dng,
width: w,
height: h,
cpp,
data: RawData::U16(data),
cfa: if linear { None } else { Some(Cfa::bayer("RGGB").unwrap()) },
bits: 14,
black: BlackLevel::uniform(256.0),
white: vec![16000.0],
active_area: Rect::new(0, 0, w, h),
crop: Rect::new(0, 0, w, h),
orientation: Orientation::Normal,
color: ColorData {
illuminant: [17, 21],
color_matrix: [
Some(lightcraft_color::Mat3([[0.9, 0.2, -0.15], [-0.3, 1.25, 0.08], [0.02, -0.12, 0.85]])),
Some(lightcraft_color::Mat3([[0.7, 0.3, -0.1], [-0.35, 1.3, 0.1], [0.05, -0.2, 1.0]])),
],
as_shot_neutral: Some([0.5, 1.0, 0.7]),
..Default::default()
},
wb_multipliers: None,
linearized: false,
opcodes: OpcodeLists::default(),
metadata: Default::default(),
};
if tone {
raw.color.profile.gain_table_map = Some(gaintable::GainTableMap {
points_v: 1,
points_h: 2,
points_n: 1,
spacing_v: 1.0,
spacing_h: 1.0,
origin_v: 0.0,
origin_h: 0.0,
weights: [1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0, 0.0, 0.0],
gamma: 1.0,
gains: vec![1.0, 4.0],
});
}
write_dng(&raw, &Default::default()).unwrap()
}
fn semantic_dng() -> Vec<u8> {
use lightcraft_tiff::tags::{self as t, photometric};
use lightcraft_tiff::{ByteOrder, IfdBuilder, ImageData, TiffWriter, Value};
let (w, h) = (16u32, 8u32);
let image = |ifd: &mut IfdBuilder, w: u32, h: u32, cpp: u16, bits: u16, data: Vec<u8>| {
ifd.set(t::IMAGE_WIDTH, Value::Long(vec![w]));
ifd.set(t::IMAGE_LENGTH, Value::Long(vec![h]));
ifd.set(t::BITS_PER_SAMPLE, Value::Short(vec![bits; cpp as usize]));
ifd.set(t::SAMPLES_PER_PIXEL, Value::Short(vec![cpp]));
ifd.set(t::COMPRESSION, Value::Short(vec![1]));
ifd.set_image(ImageData::Strips { rows_per_strip: h, strips: vec![data] });
};
let mut raw = IfdBuilder::new();
raw.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![0]));
raw.set(t::PHOTOMETRIC, Value::Short(vec![photometric::LINEAR_RAW]));
image(&mut raw, w, h, 3, 16, 12000u16.to_le_bytes().repeat((w * h * 3) as usize));
let matte = |name: &str, right: bool| {
let mut m = IfdBuilder::new();
m.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![t::SUBFILE_SEMANTIC_MASK]));
m.set(t::PHOTOMETRIC, Value::Short(vec![photometric::MASK]));
m.set(t::SEMANTIC_NAME, Value::Ascii(name.into()));
image(&mut m, w / 2, h / 2, 1, 8, (0..w / 2 * h / 2).map(|i| if (i % (w / 2) >= w / 4) == right { 255 } else { 0 }).collect());
m
};
let mut ifd0 = IfdBuilder::new();
ifd0.set(t::NEW_SUBFILE_TYPE, Value::Long(vec![1]));
ifd0.set(t::DNG_VERSION, Value::Byte(vec![1, 6, 0, 0]));
ifd0.set(t::MAKE, Value::Ascii("Apple".into()));
ifd0.set(t::ORIENTATION, Value::Short(vec![6]));
ifd0.set(t::COLOR_MATRIX_1, Value::SRational(vec![(1, 1), (0, 1), (0, 1), (0, 1), (1, 1), (0, 1), (0, 1), (0, 1), (1, 1)]));
ifd0.set(t::CALIBRATION_ILLUMINANT_1, Value::Short(vec![21]));
ifd0.set(t::PHOTOMETRIC, Value::Short(vec![photometric::RGB]));
image(&mut ifd0, 4, 2, 3, 8, vec![128; 24]);
ifd0.add_sub_ifd(raw);
ifd0.add_sub_ifd(matte("urn:com:apple:photo:2020:aux:semanticskymatte", true));
ifd0.add_sub_ifd(matte("urn:com:apple:photo:2020:aux:semanticsomethingelse", false));
TiffWriter::new(ByteOrder::Little, false).write(&[ifd0]).unwrap()
}
fn compare(root: &Path, name: &str, bytes: Vec<u8>, edit: DevelopSettings, expected: usize) {
let path = root.join(name);
std::fs::write(&path, &bytes).unwrap();
let (pixels, info) = files::load_bytes(&bytes, 2560).unwrap();
let mut s = Session::new().with_fs();
s.open_library(root.join(format!("lib-{name}")), false).unwrap();
s.execute("library.import", &json!({"paths": [path]})).unwrap();
s.execute("library.smartPreviews", &json!({"format": "lossless"})).unwrap();
let photo = s.catalog.photos().next().unwrap().clone();
let proxy = smart::load(&smart::file_path(s.media.smart_dir.as_ref().unwrap(), &photo)).unwrap();
assert_eq!(pixels.data, proxy.image.data);
s.set_develop(photo.id, edit, "reproduction").unwrap();
std::fs::rename(&path, path.with_extension("offline")).unwrap();
s.media.rendered.clear();
s.media.clear_sources();
let mut online = s.render_job(photo.id, 96, 96, false, true).unwrap();
online.source = media::SourceRef::Loaded(Box::new(media::DecodedSource::new(Arc::new(pixels), Some(info))));
let online = online.run().rendered.unwrap().image;
let offline = s.render_now(photo.id, 96, 96).unwrap().image;
let changed = online.data.iter().zip(&offline.data).filter(|(a, b)| a != b).count();
println!("{name}: {changed}/{} differing pixels", online.data.len());
assert_eq!(changed, expected);
}
fn main() {
let root = std::env::temp_dir().join(format!("lc-smart-metadata-{}", std::process::id()));
std::fs::create_dir(&root).unwrap();
let mut wb = DevelopSettings::default();
wb.wb.mode = WbMode::Custom;
wb.wb.temp = 3000.0;
wb.wb.tint = 20.0;
compare(&root, "bayer.dng", synthetic_dng(false, false, false), wb.clone(), 6144);
compare(&root, "linear.dng", synthetic_dng(true, false, false), wb.clone(), 6144);
let raw = lightcraft_raw::decode(&synthetic_dng(false, false, false)).unwrap();
let merged = lightcraft_merge::write_linear_dng(
&scene(96, 64),
&lightcraft_merge::FrameColor::Camera(Box::new(raw.color)),
Orientation::Normal,
&Default::default(),
0.0,
lightcraft_merge::DngSamples::Float,
)
.unwrap();
compare(&root, "merge-float.dng", merged, wb, 6141);
let mut local = DevelopSettings::default();
local.profile.camera_local_tone = true;
local.light.exposure = -3.0;
compare(&root, "local-tone.dng", synthetic_dng(true, false, true), local, 6135);
let mut sky = DevelopSettings::default();
sky.masks.push(Mask {
components: vec![MaskComponent { name: None, op: MaskOp::Add, invert: false, shape: MaskShape::Sky }],
adjust: LocalAdjustments { exposure: 2.0, ..Default::default() },
..Default::default()
});
compare(&root, "semantic.dng", semantic_dng(), sky, 4608);
std::fs::remove_dir_all(root).unwrap();
}Observed results
Rechecked on 2026-10-13 at cf1c803a. The example compiles without API changes and all five counts match the generator's assertions.
Bayer white balance changes 6,144 of 6,144 pixels. Linear DNG white balance changes 6,144 of 6,144. Float merge DNG white balance changes 6,141 of 6,144. The local tone map changes 6,135 of 6,144. The semantic sky mask changes 4,608 of 4,608. The generator asserts these results.
Source samples are equal before rendering. Required decoder metadata is present online and absent offline. The public A7CR sample did not expose these fields, so its matching offline renders do not establish parity for these DNGs.
Expected result
Offline editing should preserve the decoder metadata required to interpret edits, or explicitly report unsupported offline edits. Identical reduced source pixels and identical edits should produce identical output on the same grid.
The affected paths are crates/engine/src/smart.rs and crates/engine/src/media.rs.
Related reports
Issue #709 covers JPEG quantization, which PR #1400 addresses. Issue #1492 (parallel smart preview builds) touches the same build loop but not metadata preservation. I found no existing report or fix for this metadata loss.
Activity
midasmatterhorn commentedon Oct 14, 2026
Confirmed on current main: the proxy kept the pixels and the camera tone curve only, so offline the render fell back to the catalog's header facts — a custom Kelvin white balance, Camera local tone mapping and stored semantic mattes all rendered differently once the original was away. The fix stores those facts in a versioned tail after the proxy's JPEG (the old format is still read, and old proxies rebuild when the original is online), and a new engine test renames the original and requires the offline render to match the online one bit for bit (6144/6144 pixels differed before for the white-balance case). PR: #1809
LR-CLOUD-SMARTPREVIEW: add opt-in lossless smart previews (storytold#709