Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6d864f2
LR-LIB-PEOPLE: write face regions and names to XMP sidecars
edcasati Nov 18, 2026
1c30966
LR-LIB-PEOPLE: fit face names and cleared regions to the sidecar on save
edcasati Nov 18, 2026
a3fce4c
LR-LIB-PEOPLE: write boxes on turned files in the stored frame, as Li…
edcasati Nov 18, 2026
708fc1f
LR-LIB-PEOPLE: keep a deliberate clear of file regions as an empty list
edcasati Nov 18, 2026
25e2439
LR-LIB-PEOPLE: keep region entries LightCraft can't read when writing…
edcasati Nov 18, 2026
f8aaf77
LR-LIB-PEOPLE: describe clipping before skipping a box that can't be …
edcasati Nov 18, 2026
5ef57ab
LR-LIB-PEOPLE: state the detector-box overlap as intersection over union
edcasati Nov 18, 2026
6bcc7dd
LR-LIB-PEOPLE: keep the file's orientation in the catalog; saves neve…
edcasati Nov 18, 2026
4404be9
LR-LIB-PEOPLE: write boxes on turned, mirrored photos (Exif 5, 7) in …
edcasati Nov 18, 2026
12ce4cb
LR-LIB-PEOPLE: one orientation for a photo's size and its file_orient…
edcasati Nov 18, 2026
d4c354c
LR-LIB-PEOPLE: write 5/7 boxes upright when the list keeps no size
edcasati Nov 18, 2026
45ba2fc
LR-LIB-PEOPLE: an unreadable sidecar fails Read Metadata even when th…
edcasati Nov 18, 2026
7098018
LR-LIB-PEOPLE: bound the orientation backfill and probe only for it
edcasati Nov 18, 2026
305f513
LR-LIB-PEOPLE: a JPEG XMP read that fails is unreadable, not a missin…
edcasati Nov 18, 2026
ce3ceee
LR-LIB-PEOPLE: read a preview-only raw's boxes by its stated orientation
edcasati Nov 19, 2026
8c63fc5
LR-LIB-PEOPLE: Synchronize Folder reads face boxes by the import's or…
edcasati Nov 19, 2026
528c700
LR-LIB-PEOPLE: read a non-raw's orientation from its header, not the …
edcasati Nov 19, 2026
47cfa37
LR-LIB-PEOPLE: write boxes upright when a kept entry has a rotation
edcasati Nov 19, 2026
bc7d71d
LR-LIB-PEOPLE: no orientation for a PSD whose fixed header is invalid
edcasati Nov 19, 2026
cbf0011
LR-LIB-PEOPLE: a sidecar's face boxes keep the size they were drawn o…
SamLovick Dec 1, 2026
f25a843
LR-LIB-PEOPLE: faces.renamePerson's doc says the sidecar follows with…
SamLovick Dec 1, 2026
44ddffd
LR-LIB-PEOPLE: format the import's FileFacts after adding upright_by
SamLovick Dec 1, 2026
43f2fe7
LR-LIB-PEOPLE: read TIFF entries with as_chunks (clippy::chunks_exact…
SamLovick Dec 1, 2026
383be5e
LR-LIB-PEOPLE: answer the review of the face region writer
SamLovick Dec 1, 2026
b66c27c
LR-LIB-PEOPLE: a save that leaves the faces out says so, and names ar…
SamLovick Dec 1, 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
512 changes: 512 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 @@ -30,6 +30,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
7 changes: 6 additions & 1 deletion crates/engine/src/cmd/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ fn convert(s: &mut Session, p: &Value) -> Result<Value> {
]));
continue;
}
let packet = crate::sidecar::sidecar_packet(&ph, &s.catalog);
// face boxes go in by the raw's orientation, read from it for a photo catalogued before it was kept; none go in
// when it can't be told
let packet = match crate::sidecar::with_writing_orientation(&ph, path) {
Some(q) => crate::sidecar::sidecar_packet(&q, &s.catalog),
None => crate::sidecar::sidecar_packet_with(&ph, &s.catalog, false),
};
match write_dng_for(s, path, packet) {
Ok(out) => {
let file_name = Path::new(&out).file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();
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
19 changes: 9 additions & 10 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,13 +89,13 @@ 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}))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/// `faces.renamePerson {from, to, merge?}`: give every in-library face of `from` the exact name `to`, as one undo
/// step. Names match the People view (trimmed, case-insensitive). A `to` that is already someone else is refused
/// unless `merge` is set, and then the two people become one. The sidecar is not touched, and a refusal changes nothing.
/// unless `merge` is set, and then the two people become one. With auto-write on, each changed original's sidecar is
/// rewritten; a refusal changes nothing.
fn rename_person(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "faces.renamePerson";
let from = match p.get("from") {
Expand Down Expand Up @@ -172,7 +172,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 +390,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 +1180,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 +1198,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 +1207,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 @@ -721,7 +721,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 @@ -737,7 +737,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 @@ -746,7 +745,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`)",
always,
|s, p| {
const C: &str = "photo.setRegion";
Expand All @@ -771,7 +770,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
8 changes: 6 additions & 2 deletions crates/engine/src/cmd/xmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
let mut merged = Vec::new();
let mut backups = Vec::new();
let mut failed = Vec::new();
let mut faces_not_written = Vec::new();
let ids = s.targets(p)?;
let owners = crate::sidecar::StemOwners::among(&s.catalog, &ids);
for id in ids {
Expand All @@ -21,6 +22,9 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
if r.merged {
merged.push(path.clone());
}
if r.faces_left {
faces_not_written.push(json!({"id": id.0, "path": path, "error": "the file's orientation can't be read, so its face regions and names were not written; the sidecar's are left as they are"}));
}
if let Some(b) = r.backup {
backups.push(json!({"path": path, "backup": b.display().to_string()}));
}
Expand All @@ -29,7 +33,7 @@ fn save(s: &mut Session, p: &Value) -> Result<Value> {
Err(e) => failed.push(json!({"id": id.0, "error": e.to_string()})),
}
}
Ok(json!({"written": written, "merged": merged, "backups": backups, "failed": failed}))
Ok(json!({"written": written, "merged": merged, "backups": backups, "failed": failed, "facesNotWritten": faces_not_written}))
}

fn read(s: &mut Session, p: &Value) -> Result<Value> {
Expand Down Expand Up @@ -86,7 +90,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Save Metadata to File",
["Photo", "Metadata"],
Some("Cmd+S"),
"{ids?} — writes each photo's XMP sidecar (metadata + develop settings) next to the original, merged into an existing one (other apps' data kept; an unreadable one is backed up first) → {written: [path], merged: [path], backups: [{path, backup}], failed}",
"{ids?} — writes each photo's XMP sidecar (metadata + develop settings) next to the original, merged into an existing one (other apps' data kept; an unreadable one is backed up first) → {written: [path], merged: [path], backups: [{path, backup}], failed, facesNotWritten: [{id, path, error}] (written without the photo's face regions and names, its file's orientation unreadable; the sidecar's are kept)}",
has_selection,
save
),
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 @@ -142,6 +144,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 @@ -204,6 +288,7 @@ pub fn probe_bytes(name: &str, bytes: &[u8]) -> Result<ProbeInfo, String> {
content_hash,
xmp,
orientation: Some(raw.orientation),
upright_by: Some(raw.orientation),
preview_only: None,
});
}
Expand Down Expand Up @@ -250,6 +335,8 @@ pub fn probe_bytes(name: &str, bytes: &[u8]) -> Result<ProbeInfo, String> {
embedded_lens: None,
xmp,
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
Loading