Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
839fcfa
LR-LIB-PEOPLE: write face regions and names to XMP sidecars
edcasati Nov 18, 2026
23c3dac
LR-LIB-PEOPLE: fit face names and cleared regions to the sidecar on save
edcasati Nov 18, 2026
9f910a0
LR-LIB-PEOPLE: write boxes on turned files in the stored frame, as Li…
edcasati Nov 18, 2026
27b534b
LR-LIB-PEOPLE: keep a deliberate clear of file regions as an empty list
edcasati Nov 18, 2026
ebffce7
LR-LIB-PEOPLE: keep region entries LightCraft can't read when writing…
edcasati Nov 18, 2026
968e04e
LR-LIB-PEOPLE: describe clipping before skipping a box that can't be …
edcasati Nov 18, 2026
c35f0ef
LR-LIB-PEOPLE: state the detector-box overlap as intersection over union
edcasati Nov 18, 2026
75fa2ed
LR-LIB-PEOPLE: keep the file's orientation in the catalog; saves neve…
edcasati Nov 18, 2026
bd2aabc
LR-LIB-PEOPLE: write boxes on turned, mirrored photos (Exif 5, 7) in …
edcasati Nov 18, 2026
582022b
LR-LIB-PEOPLE: one orientation for a photo's size and its file_orient…
edcasati Nov 18, 2026
a36657c
LR-LIB-PEOPLE: write 5/7 boxes upright when the list keeps no size
edcasati Nov 18, 2026
b809f0b
LR-LIB-PEOPLE: an unreadable sidecar fails Read Metadata even when th…
edcasati Nov 18, 2026
9a9b4c1
LR-LIB-PEOPLE: bound the orientation backfill and probe only for it
edcasati Nov 18, 2026
50d8ee1
LR-LIB-PEOPLE: a JPEG XMP read that fails is unreadable, not a missin…
edcasati Nov 18, 2026
9931b52
LR-LIB-PEOPLE: read a preview-only raw's boxes by its stated orientation
edcasati Nov 19, 2026
9773de7
LR-LIB-PEOPLE: Synchronize Folder reads face boxes by the import's or…
edcasati Nov 19, 2026
35be41e
LR-LIB-PEOPLE: read a non-raw's orientation from its header, not the …
edcasati Nov 19, 2026
3065c01
LR-LIB-PEOPLE: write boxes upright when a kept entry has a rotation
edcasati Nov 19, 2026
e000ed1
LR-LIB-PEOPLE: no orientation for a PSD whose fixed header is invalid
edcasati Nov 19, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion crates/catalog/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ pub struct Meta {
pub creator: String,
pub keywords: Vec<String>,
/// Face/pet/focus regions read from XMP (MWG-RS), on the upright (EXIF-oriented) photo.
/// Removing or resizing one edits the catalog only; LightCraft never writes regions to XMP.
/// Written to the photo's sidecar when one is saved (`lightcraft_engine::sidecar`), the detector's unnamed boxes
/// excepted.
/// Left out of the catalog JSON when empty (most photos), so older catalogs read unchanged.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub regions: Vec<lightcraft_meta::Region>,
Expand All @@ -228,6 +229,13 @@ pub struct Meta {
/// written to XMP. Left out of the catalog JSON when unknown, so older catalogs read unchanged.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub focus: Option<lightcraft_meta::FocusPoint>,
/// The photo file's own orientation, as the import read it (`ProbeInfo::upright_by`, the one that made `width` ×
/// `height` upright and the photo is shown by): how its stored pixels turn upright. A sidecar save writes face regions in the stored frame by
/// it (`lightcraft_engine::sidecar`) without reading the file. `None` for a file that states none, and for a photo
/// catalogued before it was kept until Read Metadata from File reads it. Left out of the catalog JSON when unknown,
/// so older catalogs read unchanged.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file_orientation: Option<lightcraft_meta::Orientation>,
}

impl Meta {
Expand Down
1 change: 1 addition & 0 deletions crates/codecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod jpeg;
#[cfg(feature = "jxl")]
mod jxl;
mod jxl_encode;
pub mod orientation;
mod other;
mod png_codec;
pub mod print;
Expand Down
513 changes: 513 additions & 0 deletions crates/codecs/src/orientation.rs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions crates/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ lightcraft-raw = { workspace = true }
lightcraft-denoise-core = { workspace = true }
lightcraft-denoise = { workspace = true, optional = true }
lightcraft-meta = { workspace = true }
# finds the text of the region entries in a sidecar that LightCraft can't read, to keep them on save (sidecar.rs)
roxmltree = "0.21"
lightcraft-lensdb = { workspace = true }
lightcraft-faces = { workspace = true }
miniz_oxide = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/engine/src/cmd/face_detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! made do not matter), the detector runs on it, and the faces come back as fractions of the photo's width
//! and height, the same frame face regions from XMP use. Unless `apply` is false, they are added to the photo as
//! unnamed face regions in one undoable step; earlier detections are replaced, and regions that came from XMP (or
//! that you drew or named) are never touched. Like every region edit it is catalog-only: no sidecar is written.
//! that you drew or named) are never touched. Unnamed detected boxes are LightCraft's bookkeeping and are never written to a sidecar.

use std::path::PathBuf;
use std::sync::{Arc, Mutex, PoisonError};
Expand Down Expand Up @@ -291,7 +291,7 @@ fn detect(s: &mut Session, p: &Value) -> Result<Value> {
let applied = !ops.is_empty();
if applied {
s.commit("Detect Faces", Op::Batch { ops })?;
// regions are not written to XMP, so a sidecar must not be rewritten for this
// unnamed detected boxes are not written to XMP, so a sidecar must not be rewritten for this
s.skip_auto_write = true;
}
Ok(json!({"detector": LABEL, "applied": applied, "photos": results, "ms": started.elapsed().as_secs_f64() * 1000.0}))
Expand Down
16 changes: 7 additions & 9 deletions crates/engine/src/cmd/face_recognize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
//! what teaches the suggestions). `faces.renamePerson` renames every in-library face of one person, or merges two
//! people when asked.
//!
//! Everything here is a suggestion for the user to confirm; nothing is ever named automatically. All of it is
//! catalog-only (no sidecar is written) and naming is undoable.
//! Everything here is a suggestion for the user to confirm; nothing is ever named automatically. Suggestions stay
//! in the library; a confirmed name reaches the photo's sidecar whenever it is written (`crate::sidecar`), and naming
//! is undoable.

use lightcraft_catalog::Op;
use serde_json::{Value, json};
Expand Down Expand Up @@ -74,7 +75,6 @@ fn name_faces(s: &mut Session, p: &Value) -> Result<Value> {
}
let photos = ops.len();
s.commit(if name.is_some() { "Name Faces" } else { "Clear Face Names" }, Op::Batch { ops })?;
s.skip_auto_write = true;
Ok(json!({"named": named, "photos": photos, "name": name}))
}

Expand All @@ -89,7 +89,6 @@ fn set_name(s: &mut Session, p: &Value) -> Result<Value> {
let region = meta.regions.get_mut(index).ok_or_else(|| bad(C, "no such region"))?;
apply_name(region, &name);
s.commit(if name.is_some() { "Name Face" } else { "Clear Face Name" }, Op::SetMeta { id, meta: Box::new(meta) })?;
s.skip_auto_write = true;
Ok(json!({"id": id.0, "index": index, "name": name}))
}

Expand Down Expand Up @@ -172,7 +171,6 @@ fn rename_person(s: &mut Session, p: &Value) -> Result<Value> {
}
let photos = ops.len();
s.commit(if other { "Merge People" } else { "Rename Person" }, Op::Batch { ops })?;
s.skip_auto_write = true;
if s.filter.person.as_deref().is_some_and(|n| n.trim().to_lowercase() == from_key) {
s.filter.person = Some(to.clone());
}
Expand Down Expand Up @@ -391,7 +389,7 @@ mod imp {
meta.regions.push(Region { rect: *rect, kind: RegionKind::Face, name: None, description: Some(format!("{MARK}{LABEL}")) });
}
if s.apply_system(Op::SetMeta { id, meta: Box::new(meta) }).is_ok() {
// regions are not written to XMP, so no sidecar may be rewritten for this
// unnamed detected boxes are not written to XMP, so no sidecar may be rewritten for this
s.skip_auto_write = true;
}
}
Expand Down Expand Up @@ -1181,7 +1179,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Name Faces",
[],
None,
"{faces: [{photo, index}], name} → {named, photos} — name (or, with a null or empty name, clear) many face regions as one undoable step; the sidecar is not touched",
"{faces: [{photo, index}], name} → {named, photos} — name (or, with a null or empty name, clear) many face regions as one undoable step; with auto-write on, the sidecar of each changed original is rewritten (its `mwg-rs:Regions` and `Iptc4xmpExt:PersonInImage`)",
super::always,
name_faces
),
Expand All @@ -1199,7 +1197,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Name Face",
[],
None,
"{id?, index, name} — name a face region (null or empty clears it); undoable; the sidecar is not touched",
"{id?, index, name} — name a face region (null or empty clears it); undoable; with auto-write on, the sidecar of each changed original is rewritten (its `mwg-rs:Regions` and `Iptc4xmpExt:PersonInImage`)",
super::always,
set_name
),
Expand All @@ -1208,7 +1206,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Rename Person",
[],
None,
"{from, to, merge?: bool} → {faces, photos, from, to, merged} — give every in-library face of `from` the name `to` (trimmed, any capitalization), one undo step; refused when `to` is already someone else unless `merge` is true, which combines the two; the sidecar is not touched",
"{from, to, merge?: bool} → {faces, photos, from, to, merged} — give every in-library face of `from` the name `to` (trimmed, any capitalization), one undo step; refused when `to` is already someone else unless `merge` is true, which combines the two; with auto-write on, the sidecar of each changed original is rewritten (its `mwg-rs:Regions` and `Iptc4xmpExt:PersonInImage`)",
super::always,
rename_person
),
Expand Down
6 changes: 2 additions & 4 deletions crates/engine/src/cmd/library/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Remove Face Box",
[],
None,
"{id?, index} — remove one face / pet region (by its position in the photo's regions) from the photo in the catalog; undoable. The XMP sidecar is never rewritten for this, even with auto-write on, so reading the metadata from the file brings the region back",
"{id?, index} — remove one face / pet region (by its position in the photo's regions) from the photo; undoable. With auto-write on, the sidecar of each changed original is rewritten (its `mwg-rs:Regions` and `Iptc4xmpExt:PersonInImage`)",
always,
|s, p| {
let id =
Expand All @@ -615,7 +615,6 @@ pub fn specs() -> Vec<CommandSpec> {
}
let gone = meta.regions.remove(index);
s.commit("Remove Face Box", Op::SetMeta { id, meta: Box::new(meta) })?;
s.skip_auto_write = true;
Ok(json!({"removed": gone.name}))
}
),
Expand All @@ -624,7 +623,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Resize Face Box",
[],
None,
"{id?, index, rect: {x0, y0, x1, y1}} — set one face / pet region's box (normalized, in the photo's upright frame; clamped to the photo, at least 0.5 % each way) in the catalog; undoable. Like photo.removeRegion it never rewrites the XMP sidecar",
"{id?, index, rect: {x0, y0, x1, y1}} — set one face / pet region's box (normalized, in the photo's upright frame; clamped to the photo, at least 0.5 % each way) ; undoable. With auto-write on, the sidecar of each changed original is rewritten (its `mwg-rs:Regions` and `Iptc4xmpExt:PersonInImage`)",
Comment thread
edcasati marked this conversation as resolved.
Resolved
always,
|s, p| {
const C: &str = "photo.setRegion";
Expand All @@ -649,7 +648,6 @@ pub fn specs() -> Vec<CommandSpec> {
let region = meta.regions.get_mut(index).ok_or_else(|| bad(C, "no such region"))?;
region.rect = lightcraft_geom::Rect { x0, y0, x1, y1 };
s.commit("Resize Face Box", Op::SetMeta { id, meta: Box::new(meta) })?;
s.skip_auto_write = true;
Ok(json!({"rect": {"x0": x0, "y0": y0, "x1": x1, "y1": y1}}))
}
),
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub use load::{load_bytes, load_bytes_decoded, load_bytes_with, load_vec, load_v
use preview::PLAN_PREFIX;
pub(crate) use preview::decode_raw_preview;
pub use preview::{PreviewRead, embedded_preview_srgb, fs_preview_loader, load_embedded_preview, preview_window};
pub use probe::{embedded_lens, probe_bytes};
pub use probe::{ORIENTATION_HEAD, Orientations, embedded_lens, orientations, orientations_of_file, probe_bytes, upright_by};
#[cfg(test)]
use probe::{meta_of, preview_reason};

Expand Down
87 changes: 87 additions & 0 deletions crates/engine/src/files/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub(super) fn meta_of(m: &lightcraft_meta::Metadata) -> (Meta, Option<String>) {
keywords: lightcraft_catalog::keywords::from_file(&m.keywords, &m.hierarchical_keywords),
regions: m.regions.clone(),
focus: m.focus.clone(),
// (the import keeps `ProbeInfo::upright_by`)
file_orientation: None,
};
(meta, m.capture_time.as_ref().map(|d| d.to_iso()))
}
Expand Down Expand Up @@ -125,6 +127,88 @@ fn ext_upper(name: &str) -> String {
std::path::Path::new(name).extension().map(|e| e.to_string_lossy().to_uppercase()).unwrap_or_default()
}

/// The orientation a file's size is made upright by on import ([`ProbeInfo::upright_by`]) and nothing else: a raw's
/// from its decoder, an image's from its header's Exif (what [`probe_bytes`] turns them by). `None` where the import
/// has none: a raw described from its embedded preview, or a file it can't read. No hashing, no metadata.
pub fn upright_by(bytes: &[u8]) -> Option<Orientation> {
orientations(bytes).kept
}

/// A file's orientations, from its bytes, by the import's rules.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Orientations {
/// What the catalog keeps as the file's orientation: [`upright_by`].
pub kept: Option<Orientation>,
/// What the import reads the file's face boxes by: `kept`, else (a raw described from its embedded preview) the
/// orientation its metadata states ([`ProbeInfo::orientation`]). Not kept: it isn't what the size was turned by.
pub read_by: Option<Orientation>,
}

/// [`Orientations`] of a file's bytes. Like [`upright_by`], no hashing; the metadata is read only for a raw the decoder
/// can't describe.
pub fn orientations(bytes: &[u8]) -> Orientations {
if lightcraft_raw::probe(bytes).is_some() {
return match lightcraft_raw::probe_info(bytes) {
Ok(raw) => Orientations { kept: Some(raw.orientation), read_by: Some(raw.orientation) },
Err(_) => Orientations { kept: None, read_by: lightcraft_meta::extract(bytes).orientation },
};
}
let Some(fmt) = lightcraft_codecs::sniff(bytes) else { return Orientations::default() };
if fmt.not_decodable().is_some() {
return Orientations::default();
}
let kept = lightcraft_codecs::read_header(bytes).ok().map(|h| Orientation::from_exif(h.orientation));
Orientations { kept, read_by: kept }
}

/// [`Orientations`] of the file at `path` (none when it can't be read or is over `max` bytes), reading no more of it
/// than its orientation needs. A raw, and a file of at most [`ORIENTATION_HEAD`] bytes, is read whole (within `max`)
/// and given [`orientations`]. Any other image is read where its header reader finds the orientation
/// ([`lightcraft_codecs::orientation::header_orientation`]: a few reads, however large the file), and whole only where
/// that can't tell.
pub fn orientations_of_file(path: &str, max: u64) -> Orientations {
orientations_read(path, max).unwrap_or_default()
}

/// How much of a file [`orientations_of_file`] reads first: enough to tell a raw (as upstream's sync did) and to
/// take a small file whole.
pub const ORIENTATION_HEAD: u64 = 1 << 20;

fn orientations_read(path: &str, max: u64) -> Option<Orientations> {
use std::io::{Read, Seek};
let mut file = crate::read_file::open_for_read(path).ok()?;
let meta = file.metadata().ok()?;
if !meta.is_file() || meta.len() > max {
return None;
}
let len = meta.len();
let mut head = Vec::new();
(&mut file).take(ORIENTATION_HEAD.min(len)).read_to_end(&mut head).ok()?;
if head.len() as u64 >= len {
return Some(orientations(&head));
}
let whole = |mut file: std::fs::File| -> Option<Orientations> {
file.rewind().ok()?;
crate::read_file::read_open(file, max).ok().map(|bytes| orientations(&bytes))
};
// a raw's orientation is its decoder's, which needs the file
if lightcraft_raw::probe(&head).is_some() {
return whole(file);
}
let format = lightcraft_codecs::sniff(&head)?;
if format.not_decodable().is_some() {
return None;
}
match lightcraft_codecs::orientation::header_orientation(&mut file, len, format) {
Ok(Some(o)) => {
let o = Some(Orientation::from_exif(o));
Some(Orientations { kept: o, read_by: o })
}
Ok(None) => whole(file),
Err(_) => None,
}
}

/// Probe a file's bytes: kind, dimensions (oriented), metadata. No pixel data is decompressed:
/// raws are described from their headers ([`lightcraft_raw::probe_info`]), other images too
/// ([`lightcraft_codecs::read_header`], which refuses truncated files but can't see damage inside
Expand Down Expand Up @@ -186,6 +270,7 @@ pub fn probe_bytes(name: &str, bytes: &[u8]) -> Result<ProbeInfo, String> {
content_hash,
xmp: lightcraft_meta::embedded(bytes).xmp,
orientation: Some(raw.orientation),
upright_by: Some(raw.orientation),
preview_only: None,
});
}
Expand Down Expand Up @@ -232,6 +317,8 @@ pub fn probe_bytes(name: &str, bytes: &[u8]) -> Result<ProbeInfo, String> {
embedded_lens: None,
xmp: None,
orientation: m.orientation,
// the header's: what the size was turned by, and what the decoder shows
upright_by: Some(o),
preview_only: None,
})
}
Expand Down
7 changes: 6 additions & 1 deletion crates/engine/src/import/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,19 @@ pub fn commit_prepared(s: &mut Session, opts: &ImportOptions, now: &str, prepare
}
let sidecar = sidecar.map(|sc| sc.resolve_label(&s.catalog)).filter(|sc| *sc != crate::sidecar::SidecarData::default());
// the photo is upright as the file's own orientation turns it, when the file states one
let sidecar = sidecar.map(|sc| crate::sidecar::SidecarData { orientation: info.orientation.or(sc.orientation), ..sc });
// the orientation the photo's size was made upright by turns the sidecar's boxes too, so the import, a
// save (`Meta::file_orientation`) and a re-read go by the same one
let sidecar =
sidecar.map(|sc| crate::sidecar::SidecarData { orientation: info.upright_by.or(info.orientation).or(sc.orientation), ..sc });
// a copy is catalogued under its new name
let name = Path::new(&stored).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_else(|| path.clone());
let mut p = Photo::new(id, Source::File { path: stored }, &name, &info.format, info.width, info.height, &now);
p.kind = info.kind;
p.file_size = info.file_size;
p.captured = info.captured;
p.meta = info.meta;
// what the size was made upright by: a sidecar save writes face boxes by it
p.meta.file_orientation = info.upright_by;
p.as_shot_wb = info.as_shot_wb;
p.content_hash = info.content_hash;
p.embedded_lens = info.embedded_lens;
Expand Down
2 changes: 2 additions & 0 deletions crates/engine/src/lightroom_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,8 @@ pub(crate) fn apply_prepared(s: &mut crate::Session, mut data: CatalogImport, co
p.meta.iso = m.iso;
p.meta.focus = m.focus.clone();
}
// the same bytes turn upright the same way
p.meta.file_orientation = duplicate.meta.file_orientation;
// the settings a file import gives it (as-shot white balance, embedded lens
// corrections, the default preset); the catalog's settings apply on top
crate::import::apply_import_defaults(s, &mut p);
Expand Down
5 changes: 5 additions & 0 deletions crates/engine/src/media/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,11 @@ pub struct ProbeInfo {
/// The file's Exif orientation, when it states one: how its stored pixels turn upright (`width` and `height` are
/// the upright size).
pub orientation: Option<lightcraft_geom::Orientation>,
/// The orientation `width` × `height` were turned upright by, the one the photo is shown by: a raw decoder's, or
/// an image header's Exif (an orientation only XMP states turns neither the pixels nor the size). `None` when the
/// size came from something else (a raw described from its embedded preview). The catalog keeps it
/// (`Meta::file_orientation`): face boxes are written and read back by it.
pub upright_by: Option<lightcraft_geom::Orientation>,
/// A raw variant that can't be decoded yet: why. The file is described (and will be shown and
/// edited) from its embedded preview; see [`lightcraft_catalog::Photo::preview_only`].
pub preview_only: Option<String>,
Expand Down
Loading