Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

export: preserve graphic watermark insets when fitting

nevicia2010-hubab96162e039d
diff --cc crates/engine/src/export.rs
index 261064ccf,e9d1cbc58..895664064
--- a/crates/engine/src/export.rs
+++ b/crates/engine/src/export.rs
@@@ -514,25 -513,20 +514,27 @@@ fn logo_coverage(width: usize, height:
if width == 0 || height == 0 || logo.width == 0 || logo.height == 0 {
return;
}
- let lw = ((wm.image_width.clamp(0.01, 1.0) * width as f32).round() as usize).clamp(1, width);
- let lh = ((lw as f32 * logo.height as f32 / logo.width as f32).round() as usize).clamp(1, height);
+ let turns = wm.quarter_turns();
- // Fit the turned box with one scale: clamping height alone distorts tall graphics.
- let (max_w, max_h) = if turns % 2 == 1 { (height, width) } else { (width, height) };
- let scale = (wm.image_width.clamp(0.01, 1.0) as f64 * width as f64 / logo.width as f64)
- .min(max_w as f64 / logo.width as f64)
- .min(max_h as f64 / logo.height as f64);
++ // Reserve the inset on anchored axes before fitting the turned box. Centered axes
++ // ignore insets, just as placement below does. One scale preserves the graphic's shape.
++ let short = width.min(height) as f32;
++ let (ix, iy) = (wm.inset_x.clamp(0.0, 0.4) * short, wm.inset_y.clamp(0.0, 0.4) * short);
++ use Anchor::*;
++ let available_w = width as f64 - if matches!(wm.anchor, Top | Center | Bottom) { 0.0 } else { ix as f64 };
++ let available_h = height as f64 - if matches!(wm.anchor, Left | Center | Right) { 0.0 } else { iy as f64 };
++ let (max_w, max_h) = if turns % 2 == 1 { (available_h, available_w) } else { (available_w, available_h) };
++ let scale =
++ (wm.image_width.clamp(0.01, 1.0) as f64 * width as f64 / logo.width as f64).min(max_w / logo.width as f64).min(max_h / logo.height as f64);
+ let lw = (logo.width as f64 * scale).round().max(1.0) as usize;
+ let lh = (logo.height as f64 * scale).round().max(1.0) as usize;
let pre: lightcraft_raster::Image<[f32; 4]> = lightcraft_raster::Image::from_fn(logo.width, logo.height, |x, y| {
let p = logo.get(x, y);
let a = p[3] as f32 / 255.0;
[p[0] as f32 * a, p[1] as f32 * a, p[2] as f32 * a, a]
});
let scaled = lightcraft_raster::resample::resize(&pre, lw, lh, lightcraft_raster::resample::Filter::Mitchell);
-- let short = width.min(height) as f32;
-- let (ix, iy) = (wm.inset_x.clamp(0.0, 0.4) * short, wm.inset_y.clamp(0.0, 0.4) * short);
- let turns = wm.quarter_turns();
let (bw, bh) = if turns % 2 == 1 { (lh, lw) } else { (lw, lh) };
let (w, h, lwf, lhf) = (width as f32, height as f32, bw as f32, bh as f32);
-- use Anchor::*;
let ox = match wm.anchor {
TopLeft | Left | BottomLeft => ix,
Top | Center | Bottom => (w - lwf) / 2.0,
@@@ -2351,96 -2346,6 +2353,139 @@@ mod tests
assert!(b.len() <= kb as usize * 1024);
}
+ #[test]
+ fn hdr_jpeg_size_limit_respected() {
+ let img = Rgba8::from_fn(256, 192, |x, y| {
+ let n = (x as u32).wrapping_mul(73856093) ^ (y as u32).wrapping_mul(19349663);
+ [n as u8, (n >> 8) as u8, (n >> 16) as u8, 255]
+ });
+ let hdr = DeepImage {
+ width: img.width,
+ height: img.height,
+ space: OutputSpace::Srgb,
+ samples: DeepSamples::F32(
+ img.data.iter().flat_map(|p| p[..3].iter().map(|v| lightcraft_color::transfer::decode_srgb8(*v) * 4.0)).collect(),
+ ),
+ };
+ let opts = ExportOptions { hdr: true, quality: 100, limit_kb: Some(u32::MAX), ..Default::default() };
+ let full = encode_gain_map_jpeg(&img, &hdr, &opts, None).unwrap();
+ let kb = (full.len() / 2048) as u32;
+ let limited = encode_gain_map_jpeg(&img, &hdr, &ExportOptions { limit_kb: Some(kb), ..opts.clone() }, None).unwrap();
+ assert!(limited.len() <= kb as usize * 1024, "{} bytes exceeds {kb} KB", limited.len());
+ let decoded = lightcraft_codecs::gainmap::read_jpeg(&limited).unwrap();
+ assert!(decoded.meta.alternate_headroom > 1.0, "the gain map must survive size limiting");
+ let impossible = encode_gain_map_jpeg(&img, &hdr, &ExportOptions { limit_kb: Some(1), ..opts }, None);
+ assert!(impossible.is_err_and(|e| e.contains("cannot fit within 1 KB")));
+ }
+
+ #[test]
+ fn graphic_watermark_honours_exif_orientation() {
+ let dir = std::env::temp_dir().join(format!("lc-wm-orientation-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let logo = Rgba8::from_fn(40, 20, |x, _| if x < 20 { [255, 0, 0, 255] } else { [0, 0, 255, 255] });
+ for orientation in [2, 6] {
+ let meta = Metadata { orientation: Some(lightcraft_geom::Orientation::from_exif(orientation)), ..Default::default() };
+ let exif = lightcraft_meta::write_exif(&meta);
+ let bytes = lightcraft_codecs::encode_jpeg(
+ &EncodeImage::rgba8(&logo),
+ 100,
+ ChromaSubsampling::S444,
+ &EncodeMeta { exif: Some(&exif), ..Default::default() },
+ )
+ .unwrap();
+ let path = dir.join(format!("oriented-{orientation}.jpg"));
+ std::fs::write(&path, bytes).unwrap();
+ let wm = Watermark {
+ image: path.to_string_lossy().into(),
+ image_width: if orientation == 6 { 0.25 } else { 0.5 },
+ opacity: 1.0,
+ inset_x: 0.0,
+ inset_y: 0.0,
+ ..Default::default()
+ };
+ let mut img = Rgba8::from_fn(80, 80, |_, _| [0, 0, 0, 255]);
+ draw_watermark(&mut img, &wm);
+ if orientation == 6 {
+ assert!(img.get(70, 45)[0] > 240 && img.get(70, 75)[2] > 240, "EXIF 6: red above blue");
+ } else {
+ assert!(img.get(45, 70)[2] > 240 && img.get(75, 70)[0] > 240, "EXIF 2: blue left of red");
+ }
+ }
+ std::fs::remove_dir_all(dir).unwrap();
+ }
+
+ #[test]
+ fn graphic_watermark_keeps_aspect_when_fitted_and_rotated() {
+ let dir = std::env::temp_dir().join(format!("lc-wm-aspect-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let path = dir.join("tall.png");
+ let logo = Rgba8::from_fn(40, 160, |_, _| [255, 0, 0, 255]);
+ std::fs::write(&path, lightcraft_codecs::encode_png(&EncodeImage::rgba8(&logo), &EncodeMeta::default()).unwrap()).unwrap();
+ for (rotation, expected) in [(0, (20, 80)), (90, (120, 30)), (180, (20, 80)), (270, (120, 30))] {
+ let wm = Watermark {
+ image: path.to_string_lossy().into(),
+ image_width: 0.5,
+ opacity: 1.0,
+ inset_x: 0.0,
+ inset_y: 0.0,
+ rotation,
+ ..Default::default()
+ };
+ let mut img = Rgba8::from_fn(120, 80, |_, _| [0, 0, 0, 255]);
+ draw_watermark(&mut img, &wm);
+ let lit: Vec<_> = img.data.iter().enumerate().filter(|(_, p)| p[0] > 200).map(|(i, _)| (i % 120, i / 120)).collect();
+ let bounds = (
+ lit.iter().map(|p| p.0).max().unwrap() - lit.iter().map(|p| p.0).min().unwrap() + 1,
+ lit.iter().map(|p| p.1).max().unwrap() - lit.iter().map(|p| p.1).min().unwrap() + 1,
+ );
+ assert_eq!(bounds, expected, "{rotation} degrees: fitting must preserve the logo's aspect ratio");
+ }
+ std::fs::remove_dir_all(dir).unwrap();
+ }
+
++ #[test]
++ fn graphic_watermark_fit_preserves_anchor_insets() {
++ let dir = std::env::temp_dir().join(format!("lc-wm-insets-{}", std::process::id()));
++ std::fs::create_dir_all(&dir).unwrap();
++ let path = dir.join("tall.png");
++ let logo = Rgba8::from_fn(40, 160, |_, _| [255, 0, 0, 255]);
++ std::fs::write(&path, lightcraft_codecs::encode_png(&EncodeImage::rgba8(&logo), &EncodeMeta::default()).unwrap()).unwrap();
++ use Anchor::*;
++ for (anchor, rotation, expected) in [
++ (BottomRight, 90, (0, 44, 111, 71)),
++ (TopLeft, 270, (8, 8, 119, 35)),
++ (Right, 90, (0, 26, 111, 53)),
++ (BottomRight, 0, (94, 0, 111, 71)),
++ (TopLeft, 180, (8, 8, 25, 79)),
++ (Bottom, 0, (51, 0, 68, 71)),
++ // Centered axes ignore their inset rather than shrinking the image unnecessarily.
++ (Center, 90, (0, 25, 119, 54)),
++ (Top, 90, (0, 8, 119, 37)),
++ ] {
++ let wm = Watermark {
++ image: path.to_string_lossy().into(),
++ image_width: 0.5,
++ opacity: 1.0,
++ inset_x: 0.1,
++ inset_y: 0.1,
++ rotation,
++ anchor,
++ ..Default::default()
++ };
++ let mut img = Rgba8::from_fn(120, 80, |_, _| [0, 0, 0, 255]);
++ draw_watermark(&mut img, &wm);
++ let lit: Vec<_> = img.data.iter().enumerate().filter(|(_, p)| p[0] > 200).map(|(i, _)| (i % 120, i / 120)).collect();
++ let bounds = (
++ lit.iter().map(|p| p.0).min().unwrap(),
++ lit.iter().map(|p| p.1).min().unwrap(),
++ lit.iter().map(|p| p.0).max().unwrap(),
++ lit.iter().map(|p| p.1).max().unwrap(),
++ );
++ assert_eq!(bounds, expected, "{anchor:?}, {rotation} degrees");
++ }
++ std::fs::remove_dir_all(dir).unwrap();
++ }
++
#[test]
fn sharpen_increases_edge_contrast() {
let mut img = Rgba8::new(8, 8);
diff --cc docs/parity-checklist.md
index c09583828,4e0531793..1a8a69590
--- a/docs/parity-checklist.md
+++ b/docs/parity-checklist.md
@@@ -1,6 -1,6 +1,6 @@@
# Lightroom parity checklist
- > **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-18 · **Change:** maintenance (refresh export watermark and HDR size-limit fixes onto current main, retaining Print Page Options and opaque RGB export) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6
-> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-15 · **Change:** minor (LRC-PRINT-PAGE: Print Page Options) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6
++> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-20 · **Change:** minor (graphic watermark fitting retains anchored insets) · **Target:** Adobe Lightroom Classic 15.5 / Lightroom 9.6
The measured, row-by-row checklist: one row per Lightroom feature, menu item and shortcut, what we
have and what is missing. It answers *does the feature exist?* (feature breadth). Whether it is good
@@@ -891,7 -891,3 +891,11 @@@ Latin stays upright. Advanced Japanese
| 2026-10-13 | minor | LRC-LIB-CATALOG-IMPORT review: offline Local promotion and result dismissal across library switches covered; redundant operation/settings copies removed. Status and summary counts unchanged. |
| 2026-10-14 | patch | LRC-LIB-CATALOG-IMPORT: reconciled with upstream phased progress, cancellation kinds, WAL-only import, translations and current progress tracking. Status and summary counts unchanged. |
| 2026-10-13 | minor | KEYC-COMPARE: `=` / `-` (also `+`) increase / decrease the grid thumbnail size (`view.thumbSizeUp` / `view.thumbSizeDown`, View menu), grids only; status unchanged (🟡) |
+
+### 2026-10-18 — export regression integration (maintenance)
+
+Graphic watermarks honour EXIF rotation/mirroring through the shared `read_graphic` path and keep their aspect ratio when fitted to the output. HDR JPEG `limitKb` includes the gain map and metadata. Refreshed onto main fb7ac08b, retaining upstream export bounds, colour metadata, JPEG encoding and opaque RGB/real-alpha handling (#1691); no feature-count or readiness estimate changes.
++
++### 2026-10-20 — graphic watermark inset regression
++
++Oversized graphics fit the space remaining after the applicable anchor insets, with one aspect-preserving scale before quarter-turn rotation. Centered axes continue to ignore insets. Pixel-bound regressions cover edge/corner anchors, centered placement and all four rotations. Main d999e382 is integrated; HDR size-limit and EXIF-orientation fixes are unchanged.