Skip to content

Commit 32af43f

Browse files
AudiFormerclaude
andcommitted
0.9.0: measure inside the crop, and Show border buffer
- The manifest asks for "analyzeArea": "crop", so an app that supports it measures the photo as cropped and the border buffer applies inside the crop. Apps without it measure the whole photo, as before. - Show border buffer: dims everything outside the measured area in a safelight red, positioned from the host's _image (works across bands). Checked end to end with the app's CLI (built with analyzeArea): a crop is measured at its own shape, and the overlay frames the measured area. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Pfz9rQoZiGz59pAiiTUhXo
1 parent 5a85a5f commit 32af43f

6 files changed

Lines changed: 104 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Measurements made with one version can need **Measure** again in a later one; th
44
"Reference error" is the mean difference from a Negative Lab Pro conversion of the same scan, in
55
levels of 255 (see the README).
66

7+
## 0.9.0 (Measure again)
8+
- **Measures inside your crop**: the border buffer now applies after cropping, in apps that support
9+
`"analyzeArea": "crop"` (see the README); others still measure the whole photo.
10+
- **Show border buffer**: a checkbox that dims everything outside the measured area in safelight red
11+
while you set the border buffer.
12+
713
## 0.8.0
814
- **Glow** (new) and **Fade** (now −50 to +50), fitted to reference conversions: Glow brightens the
915
mids and settles the whites; Fade lifts the blacks to a matte print. Negative values push the other way.

‎Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "chromaneg"
33
description = "Film negative conversion for Chromaforge: a WebAssembly pixel-filter plug-in"
4-
version = "0.8.0"
4+
version = "0.9.0"
55
edition = "2024"
66
rust-version = "1.88"
77
license = "MIT"

‎README.md‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ the app's `docs/plugins.md` ▸ *Pixel filters*), runs in its sandbox and asks f
1717
3. Installing a newer version replaces the old one (same plug-in id); press **Measure** again on photos
1818
measured with an older version when its notes say the measurement format changed.
1919

20-
**Compatibility:** Chromaforge v26.43 or newer with pixel-filter plug-ins. In v26.43.4, clicking the
20+
**Compatibility:** Chromaforge v26.43 or newer with pixel-filter plug-ins.
21+
22+
**Measuring inside the crop:** ChromaNeg asks the app to measure the photo as cropped
23+
(`"analyzeArea": "crop"` in its manifest), so the border buffer applies inside your crop. That needs
24+
an app change that's on the `analyze-crop` branch of
25+
[nwh212/chromaforge-plugins](https://github.com/nwh212/chromaforge-plugins/tree/analyze-crop) and
26+
offered upstream. Without it, the app measures the whole uncropped photo: the border buffer applies
27+
to the full scan, and on a cropped photo the *Show border buffer* overlay doesn't match what's
28+
measured. Uncropped photos behave the same either way. In v26.43.4, clicking the
2129
Develop ▸ *Plug-in Filters* section header shows "section.pluginFilters: no such Edit section" and the
2230
section won't open; that's an app bug, fixed on the `fix-plugin-filters-section` branch of
2331
[nwh212/chromaforge-plugins](https://github.com/nwh212/chromaforge-plugins/tree/fix-plugin-filters-section).
@@ -39,9 +47,11 @@ Rust 1.88 or newer. `cargo test` runs the conversion natively (see *Test* below)
3947
border if you like, though the conversion removes the mask either way.
4048
2. In Develop ▸ Plug-in Filters, add **ChromaNeg**. It asks for the **Source** stage, so it runs on
4149
the linear capture before every other develop step.
42-
3. Set **Type** and **Border buffer %**, so the film holder, sprocket holes and edge print lie
43-
outside the measured area. Then press **Measure**. Until you measure, the filter only inverts
44-
the image, and the orange cast stays.
50+
3. Crop to the frame if you like, then set **Type** and **Border buffer %**, so the film holder,
51+
sprocket holes and edge print lie outside the measured area. Tick **Show border buffer** to see it:
52+
everything outside the measured area is dimmed in safelight red (untick it before exporting).
53+
Then press **Measure**. Until you measure, the filter only inverts the image, and the orange cast
54+
stays. After changing the crop or the border buffer, press Measure again.
4555
4. Pick a **Look** and a **Tone profile**, then fine-tune with the sliders or with the normal develop
4656
sliders, which act on the converted positive.
4757
5. **A whole roll:** set up one frame (Measure, Look, sliders), then Copy and Paste (or Sync) its
@@ -53,7 +63,7 @@ Rust 1.88 or newer. `cargo test` runs the conversion natively (see *Test* below)
5363

5464
| Group | Parameters |
5565
|---|---|
56-
| Convert | Type (Color Negative, B&W Negative, Positive), Look, Film stock, Pre-saturation 1–5, Border buffer % |
66+
| Convert | Type (Color Negative, B&W Negative, Positive), Look, Film stock, Pre-saturation 1–5, Border buffer %, Show border buffer |
5767
| Tone | Tone profile, Exposure, Brightness, Contrast, Lights, Darks, Whites, Blacks, Glow, Fade, Soft highs, Soft lows, Auto exposure, White clip, Black clip |
5868
| Colour | WB (Auto-Neutral, Auto-Color, Auto-Warm, Auto-Cool, None), Temp, Tint |
5969

‎src/convert.rs‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ pub struct Params {
278278
pub glow: f32,
279279
pub soft_highs: bool,
280280
pub soft_lows: bool,
281+
pub show_border: bool,
281282
pub auto_exposure: bool,
282283
pub white_clip: i64,
283284
pub black_clip: i64,
@@ -322,6 +323,7 @@ impl Params {
322323
glow: num("glow", 0.0),
323324
soft_highs: flag("softHighs"),
324325
soft_lows: flag("softLows"),
326+
show_border: flag("showBorder"),
325327
auto_exposure: flag("autoExposure"),
326328
white_clip: int("whiteClip"),
327329
black_clip: int("blackClip"),
@@ -888,6 +890,46 @@ fn tone(mut t: f32, p: &Params, contrast: f32, pr: &Profile) -> f32 {
888890
t.clamp(0.0, T_MAX)
889891
}
890892

893+
/// The border buffer preview ("Show border buffer"): dims what lies outside the measured area in a
894+
/// safelight red. Positions come from the host's `_image` (the band's place in the frame); the
895+
/// measured area is the frame inset by the border buffer on every side, as Measure uses it when the
896+
/// host measures the frame the filter renders (`"analyzeArea": "crop"`).
897+
pub struct BorderOverlay {
898+
/// The band's top-left in the frame, its width, and the measured area in frame pixels.
899+
x: usize,
900+
y: usize,
901+
band_w: usize,
902+
inside: (usize, usize, usize, usize),
903+
}
904+
905+
impl BorderOverlay {
906+
pub fn new(image: Option<&Value>, band_w: usize, border: f32) -> BorderOverlay {
907+
let get = |k: &str| image.and_then(|v| v.get(k)).and_then(Value::as_f64).filter(|f| f.is_finite() && *f >= 0.0).map(|f| f as usize);
908+
let (x, y) = (get("x").unwrap_or(0), get("y").unwrap_or(0));
909+
let (cw, ch) = (get("canvasWidth").unwrap_or(band_w), get("canvasHeight").unwrap_or(usize::MAX / 2));
910+
let inset = |n: usize| ((n as f32 * border / 100.0) as usize).min(n.saturating_sub(1) / 2);
911+
let (ix, iy) = (inset(cw), inset(ch));
912+
BorderOverlay { x, y, band_w: band_w.max(1), inside: (ix, iy, cw.saturating_sub(ix), ch.saturating_sub(iy)) }
913+
}
914+
915+
/// Whether frame pixel (`fx`, `fy`) is measured.
916+
fn measured(&self, fx: usize, fy: usize) -> bool {
917+
let (x0, y0, x1, y1) = self.inside;
918+
(x0..x1).contains(&fx) && (y0..y1).contains(&fy)
919+
}
920+
921+
/// Dims the pixels of `px` (interleaved RGB, starting at band pixel `start`) outside the measured area.
922+
pub fn apply(&self, px: &mut [f32], start: usize) {
923+
for (i, c) in px.as_chunks_mut::<3>().0.iter_mut().enumerate() {
924+
let n = start + i;
925+
if !self.measured(self.x + n % self.band_w, self.y + n / self.band_w) {
926+
let l = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
927+
*c = [0.08 + 0.35 * l, 0.15 * l, 0.12 * l];
928+
}
929+
}
930+
}
931+
}
932+
891933
/// Converts interleaved linear RGB in place.
892934
pub fn convert(px: &mut [f32], p: &Params) {
893935
Converter::new(p).run(px);
@@ -1124,6 +1166,33 @@ mod tests {
11241166
assert!((srgb_decode(srgb_encode(0.18)) - 0.18).abs() < 1e-5);
11251167
}
11261168

1169+
#[test]
1170+
fn the_border_preview_dims_exactly_the_unmeasured_frame() {
1171+
// a 100 × 50 frame drawn as one band; a 10 % buffer leaves 10..90 × 5..45 measured
1172+
let image = json!({"x": 0, "y": 0, "width": 100, "height": 50, "canvasWidth": 100, "canvasHeight": 50});
1173+
let o = BorderOverlay::new(Some(&image), 100, 10.0);
1174+
let mut px = vec![0.5f32; 100 * 50 * 3];
1175+
o.apply(&mut px, 0);
1176+
let at = |x: usize, y: usize| px[(y * 100 + x) * 3 + 1];
1177+
assert_eq!(at(50, 25), 0.5, "inside stays");
1178+
assert_eq!(at(10, 5), 0.5, "the inner corner stays");
1179+
assert!(at(9, 25) < 0.2 && at(50, 4) < 0.2 && at(90, 25) < 0.2 && at(50, 45) < 0.2, "outside dims");
1180+
// a lower band of the same frame (rows 25..50), started mid-band: positions follow `_image`
1181+
let lower = json!({"x": 0, "y": 25, "width": 100, "height": 25, "canvasWidth": 100, "canvasHeight": 50});
1182+
let o = BorderOverlay::new(Some(&lower), 100, 10.0);
1183+
let mut px = vec![0.5f32; 100 * 3];
1184+
o.apply(&mut px, 19 * 100); // band row 19 = frame row 44 (inside); the next would be 45
1185+
assert_eq!(px[50 * 3 + 1], 0.5);
1186+
let mut px = vec![0.5f32; 100 * 3];
1187+
o.apply(&mut px, 20 * 100);
1188+
assert!(px[50 * 3 + 1] < 0.2, "frame row 45 is outside");
1189+
// no `_image`: the band is the frame's width, nothing breaks
1190+
let o = BorderOverlay::new(None, 10, 10.0);
1191+
let mut px = vec![0.5f32; 30];
1192+
o.apply(&mut px, 0);
1193+
assert!(px.iter().all(|v| v.is_finite()));
1194+
}
1195+
11271196
#[test]
11281197
fn hostile_input_never_panics_or_goes_non_finite() {
11291198
let mut px = vec![f32::NAN, f32::INFINITY, -1.0, 0.0, 1e30, -0.0];

‎src/lib.rs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ use serde_json::Value;
2727
pub const MANIFEST: &str = r#"{
2828
"id": "io.github.nwh212.chromaneg",
2929
"name": "ChromaNeg",
30-
"version": "0.8.0",
30+
"version": "0.9.0",
3131
"kind": "filter",
3232
"stage": "source",
33+
"analyzeArea": "crop",
3334
"description": "Converts film negatives and slides. Add it to a photo, set Type and Border buffer, then press Measure. Measure again after changing Border buffer.",
3435
"author": "",
3536
"overlap": 0,
@@ -39,6 +40,7 @@ pub const MANIFEST: &str = r#"{
3940
"stock": {"type": "choice", "label": "Film stock", "options": ["Generic", "None (as scanned)", "Kodak Portra 160", "Kodak Portra 400", "Kodak Portra 800", "Kodak Ektar 100", "Kodak Gold 200", "Kodak UltraMax 400", "Kodak ColorPlus 200", "Kodak Vision3 50D", "Kodak Vision3 250D", "Kodak Vision3 500T", "CineStill 50D", "CineStill 400D", "CineStill 800T", "Fujifilm Superia X-TRA 400", "Fujicolor C200", "Fujifilm 200", "Fujifilm 400", "Fujifilm Pro 400H", "Harman Phoenix 200", "Harman Phoenix II", "Lomography 400", "Lomography 800"], "default": "Generic"},
4041
"preSaturation":{"type": "int", "label": "Pre-saturation", "min": 1, "max": 5, "default": 3},
4142
"borderBuffer": {"type": "number", "label": "Border buffer %", "min": 0, "max": 25, "default": 10},
43+
"showBorder": {"type": "bool", "label": "Show border buffer", "default": false},
4244
4345
"toneProfile": {"type": "choice", "label": "Tone profile", "options": ["Standard", "Soft", "Hard", "Highlight Soft", "Highlight Hard", "Shadow Soft", "Shadow Hard", "Cine Log", "Cine Rich", "Cine Flat", "Linear", "Linear Gamma", "Linear Deep", "Linear Flat"], "default": "Standard"},
4446
"exposure": {"type": "number", "label": "Exposure", "min": -5, "max": 5, "default": 0},
@@ -148,15 +150,21 @@ pub extern "C" fn pc_alloc(len: i32) -> i32 {
148150
#[unsafe(no_mangle)]
149151
#[allow(clippy::too_many_arguments)]
150152
pub extern "C" fn pc_filter(buf: i32, len: i32, w: i32, h: i32, channels: i32, format: i32, params: i32, params_len: i32) -> i32 {
151-
let Some(_) = rgb_args(len, w, h, channels, format) else { return BAD_ARGS };
152-
let p = convert::Params::from_json(&read_params(params, params_len));
153+
let Some((band_w, _)) = rgb_args(len, w, h, channels, format) else { return BAD_ARGS };
154+
let json = read_params(params, params_len);
155+
let p = convert::Params::from_json(&json);
153156
let conv = convert::Converter::new(&p);
157+
// the border buffer preview: where this band sits in the frame, and the measured area
158+
let overlay = p.show_border.then(|| convert::BorderOverlay::new(json.get("_image"), band_w, p.border));
154159
let done = with_block(buf, len, |bytes| {
155160
let mut px: Vec<f32> = Vec::with_capacity(CHUNK_PX * 3);
156-
for chunk in bytes.chunks_mut(CHUNK_PX * 12) {
161+
for (n, chunk) in bytes.chunks_mut(CHUNK_PX * 12).enumerate() {
157162
px.clear();
158163
px.extend(chunk.as_chunks::<4>().0.iter().map(|c| f32::from_le_bytes(*c)));
159164
conv.run(&mut px);
165+
if let Some(o) = &overlay {
166+
o.apply(&mut px, n * CHUNK_PX);
167+
}
160168
for (d, v) in chunk.as_chunks_mut::<4>().0.iter_mut().zip(px.iter()) {
161169
*d = v.to_le_bytes();
162170
}

0 commit comments

Comments
 (0)