Repository navigation
LR-EDIT-OPTICS-PROFILE: apply Nikon Z 6 and Z 7 distortion #3796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexChambers
wants to merge
35
commits into
storytold:main
Choose a base branch
from
AlexChambers:t3/nikon-lens-corrections
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+400
−107
Open
Changes from 6 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
39e0b73
LR-EDIT-OPTICS-PROFILE: add Nikon lens measurement probes
AlexChambers 76e378f
LR-EDIT-OPTICS-PROFILE: bound and validate Nikon measurement inputs
AlexChambers 1fc1070
LR-EDIT-OPTICS-PROFILE: respect Nikon distortion correction modes
AlexChambers 374f0c5
LR-EDIT-OPTICS-PROFILE: enable validated Nikon Z 6 and Z 7 distortion
AlexChambers 7cd55dc
LR-EDIT-OPTICS-PROFILE: fix reconnect rebuilds and reserve cache vers…
AlexChambers 4c70016
LR-EDIT-OPTICS-PROFILE: merge current main for final Nikon validation
AlexChambers 622c345
LR-EDIT-OPTICS-PROFILE: harden lens measurement tooling
AlexChambers 6391274
LR-EDIT-OPTICS-PROFILE: merge main after tooling review
AlexChambers d1ed0ac
LR-EDIT-OPTICS-PROFILE: key sweep caches by input digest
AlexChambers 898005a
LR-EDIT-OPTICS-PROFILE: merge main and preserve gap notes
AlexChambers a91f835
LR-EDIT-OPTICS-PROFILE: merge main with process V5
AlexChambers 0d58ca9
LR-EDIT-OPTICS-PROFILE: merge main and retain reserved cache version
AlexChambers 5d96141
LR-EDIT-OPTICS-PROFILE: merge onboarding updates and preserve harness…
AlexChambers a6c1370
LR-EDIT-OPTICS-PROFILE: merge current main 92adb0478
AlexChambers eae8538
LR-EDIT-OPTICS-PROFILE: refresh the parity summary after the merge
AlexChambers c808ee3
LR-EDIT-OPTICS-PROFILE: merge current main 8a0b91a95
AlexChambers ce0638a
LR-EDIT-OPTICS-PROFILE: the wire job test is main's again, which #378…
AlexChambers 5d5291b
LR-EDIT-OPTICS-PROFILE: merge current main 1a301fc2d (Lens Blur, High…
AlexChambers 44b3d91
LR-EDIT-OPTICS-PROFILE: LOOK_VERSION 28, since open #3943 takes 26 an…
AlexChambers d15357d
LR-EDIT-OPTICS-PROFILE: merge current main fd0b914e6 (security batch,…
AlexChambers 09c9a68
LR-EDIT-OPTICS-PROFILE: the Nikon lens test uses craft_tiff (main mov…
AlexChambers 4b9699e
LR-EDIT-OPTICS-PROFILE: merge current main f72ba11a2 (craft-raw cutov…
AlexChambers 19d6c7d
LR-EDIT-OPTICS-PROFILE: take craft-libs a207d20 for the Nikon Z 6 and…
AlexChambers 3c9764a
LR-EDIT-OPTICS-PROFILE: the Nikon doc drops its Last updated header a…
AlexChambers 20d1c3b
LR-EDIT-OPTICS-PROFILE: merge current main 6b2b7e6d1 (TIFF, JPEG and …
AlexChambers f26d2eb
LR-EDIT-OPTICS-PROFILE: retry CI (the Linux gate's only failure was a…
AlexChambers 14f3ff9
Merge remote-tracking branch 'origin/main' into t3/nikon-lens-correct…
AlexChambers fc807b4
Merge remote-tracking branch 'origin/main' into t3/nikon-lens-correct…
AlexChambers 60792f5
Merge remote-tracking branch 'origin/main' into t3/nikon-lens-correct…
AlexChambers 372212f
Merge remote-tracking branch 'origin/main' into t3/nikon-lens-correct…
AlexChambers 57d788c
Merge commit '6be64516e250d7d588bd5d4ba7f734e04a1df32f' (origin/main)…
AlexChambers b31282b
Merge remote-tracking branch 'origin/main' into t3/nikon-lens-correct…
AlexChambers 78a065b
Merge remote-tracking branch 'origin/main' into t3/nikon-lens-correct…
AlexChambers 5bc6142
Merge remote-tracking branch 'origin/main' into t3/nikon-lens-correct…
AlexChambers 9daf437
LR-EDIT-OPTICS-PROFILE: keep Nikon distortion decoding in craft-raw
AlexChambers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| //! Measurement only: dump an uncorrected quarter-size camera-RGB proxy and render the current source. | ||
| //! | ||
| //! `cargo run --release -p lightcraft-engine --example nikon_lens_probe -- IN OUT_DIR [OPCODES.json]` | ||
| //! Optional JSON is an array of experimental `OpcodeList3` lens opcodes. It replaces the lens description | ||
| //! after source loading, without changing the decoder, saved settings, or camera-look fit. Candidate renders | ||
| //! therefore isolate optics; they do not predict a future camera-look refit. See docs/raw/nikon_lens_probe.py. | ||
| #![forbid(unsafe_code)] | ||
| #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] | ||
|
|
||
| use std::io::{Read, Write}; | ||
| use std::path::Path; | ||
|
|
||
| use lightcraft_develop::DevelopSettings; | ||
| use lightcraft_engine::files; | ||
| use lightcraft_pipeline::{RenderRequest, SourceInfo}; | ||
| use lightcraft_raster::Rgb32f; | ||
|
|
||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| let args: Vec<_> = std::env::args_os().skip(1).collect(); | ||
| let input = args.first().ok_or("usage: nikon_lens_probe IN OUT_DIR [OPCODES.json]")?; | ||
| let dir = Path::new(args.get(1).ok_or("missing OUT_DIR")?); | ||
| std::fs::create_dir_all(dir)?; | ||
| let bytes = std::fs::read(input)?; | ||
| let mut raw = lightcraft_raw::decode(&bytes)?; | ||
| let mut header = raw.info(); | ||
| std::fs::write(dir.join("raw.json"), serde_json::to_vec_pretty(&header)?)?; | ||
| raw.opcodes.list3.clear(); | ||
| let proxy = raw.develop_binned(4, 1.0)?.ok_or("cannot bin this raw")?; | ||
| let mut out = std::io::BufWriter::new(std::fs::File::create(dir.join("camera-rgb.f32"))?); | ||
| out.write_all(&u32::try_from(proxy.width)?.to_le_bytes())?; | ||
| out.write_all(&u32::try_from(proxy.height)?.to_le_bytes())?; | ||
| for pixel in &proxy.data { | ||
| for value in pixel { | ||
| out.write_all(&value.to_le_bytes())?; | ||
| } | ||
| } | ||
| out.flush()?; | ||
| drop(raw); | ||
| let (source, mut info) = files::load_bytes(&bytes, 1200)?; | ||
| let mut settings = DevelopSettings::default(); | ||
| settings.wb.temp = info.as_shot_temp; | ||
| settings.wb.tint = info.as_shot_tint; | ||
| settings.optics.lens_profile = info.lens.is_some(); | ||
| render(&source, &info, &settings, &dir.join("baseline.png"))?; | ||
| if let Some(path) = args.get(2) { | ||
| let data = read_candidates(std::fs::File::open(path)?)?; | ||
| header.opcodes.list3 = serde_json::from_slice(&data)?; | ||
| validate_candidates(&header.opcodes.list3)?; | ||
| info.lens = files::embedded_lens(&header); | ||
| settings.optics.lens_profile = info.lens.is_some(); | ||
| render(&source, &info, &settings, &dir.join("candidate.png"))?; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Read one byte beyond the limit to distinguish a complete input from an oversized one. | ||
| fn read_candidates(reader: impl Read) -> std::io::Result<Vec<u8>> { | ||
| let mut data = Vec::new(); | ||
| reader.take(65_537).read_to_end(&mut data)?; | ||
| if data.len() > 65_536 { | ||
| return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "candidate opcodes exceed 64 KiB")); | ||
| } | ||
| Ok(data) | ||
| } | ||
|
|
||
| /// Diagnostic inputs have a deliberately small range; reject arbitrary operations and invalid gains. | ||
| fn validate_candidates(ops: &[lightcraft_raw::Opcode]) -> Result<(), &'static str> { | ||
| use lightcraft_raw::Opcode; | ||
| if ops.is_empty() || ops.len() > 2 { | ||
| return Err("provide one or two lens opcodes"); | ||
| } | ||
| for op in ops { | ||
| let center = match op { | ||
| Opcode::WarpRectilinear { planes, center } if planes.len() == 1 && planes.iter().flatten().all(|v| v.is_finite() && v.abs() <= 16.0) => { | ||
| center | ||
| } | ||
| Opcode::FixVignetteRadial { k, center } if k.iter().all(|v| v.is_finite() && v.abs() <= 16.0) => { | ||
| for i in 0..=256 { | ||
| let r2 = (f64::from(i) / 256.0).powi(2); | ||
| let gain = 1.0 + r2 * (k[0] + r2 * (k[1] + r2 * (k[2] + r2 * (k[3] + r2 * k[4])))); | ||
| if !gain.is_finite() || !(0.01..=16.0).contains(&gain) { | ||
| return Err("candidate vignette gain is outside 0.01 to 16"); | ||
| } | ||
| } | ||
| center | ||
| } | ||
| _ => return Err("candidate must be a bounded single-plane warp or radial vignette"), | ||
| }; | ||
| if !center.iter().all(|v| v.is_finite() && (0.0..=1.0).contains(v)) { | ||
| return Err("candidate center must be inside the active area"); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Use the same CPU renderer and PNG encoder as the app, with no catalog or filesystem hooks. | ||
| fn render(source: &Rgb32f, info: &SourceInfo, settings: &DevelopSettings, path: &Path) -> Result<(), Box<dyn std::error::Error>> { | ||
| let rendered = lightcraft_pipeline::render(source, info, settings, &RenderRequest::fit(1200, 1200)); | ||
| let image = lightcraft_codecs::encode::EncodeImage::rgba8(&rendered.image); | ||
| std::fs::write(path, lightcraft_codecs::encode::encode_png(&image, &Default::default())?)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| /// A hostile stream must be rejected after at most limit + 1 bytes, without reading to EOF. | ||
| #[test] | ||
| fn candidate_reader_bounds_the_read_and_accepts_the_exact_limit() { | ||
| let mut endless = std::io::repeat(b' '); | ||
| let mut counted = (&mut endless).take(1_000_000); | ||
| let error = read_candidates(&mut counted).unwrap_err(); | ||
| assert_eq!(error.kind(), std::io::ErrorKind::InvalidData); | ||
| assert_eq!(counted.limit(), 1_000_000 - 65_537); | ||
| assert_eq!(read_candidates(std::io::repeat(b' ').take(65_536)).unwrap().len(), 65_536); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.