Skip to content

Detail: snap the 100% view to whole screen pixels - #1831

Merged
echelon merged 1 commit into
storytold:mainfrom
john01px2022:fix-1683-snap-100-view
Oct 15, 2026
Merged

echelon merged 1 commit into
storytold:mainfrom
john01px2022:fix-1683-snap-100-view

Conversation

@john01px2022

@john01px2022 john01px2022 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1683. A full-size JPEG looked soft in the editor at 100%, even though import, decode, render and export all keep its detail.

Cause

At 100% the photo should map one texel to one screen pixel. fit_rect (in panels/detail.rs) centres the photo on the canvas and does not snap its corner to the physical pixel grid. When the canvas has an odd width, the corner lands between two pixels. In the file from the issue, on a 2x display, the canvas is 1279.5 pt wide, so the photo's left edge sat at x = -648.25 pt. That is -1296.5 physical pixels, half a pixel off. The GPU then drew every texel with its bilinear filter, which averages neighbouring pixels. That blurred the whole view.

The pipeline was not at fault. I checked each stage with the file from the issue (2268623-qr6i.jpg, 5152x7728, progressive, ICC profile, 4:2:0):

Stage Result
lightcraft-cli render, no edits, full size 48.6 dB PSNR, 98% of the fine detail kept
100% view, window render from the full-resolution source ui.inspect reports region.full 5152x7728 and the full source held in memory, so the sharp render was already being used
100% view, half-pixel offset 39.5 dB PSNR, 68% of the fine detail kept

What changed

fit_rect now rounds the photo's top-left corner to a whole physical pixel when the photo is 1:1 with the screen. The rule is a Zoom::Percent zoom and (w * ppp - img_px[0]).abs() < 0.5. The size is unchanged. Only the position moves, by at most half a physical pixel.

  • The sharp window render is placed from the photo's corner by whole-pixel offsets, so it lands on the pixel grid once the corner does.
  • Fit and Fill are untouched. They always resample.
  • Other percentages are untouched. At those zooms the texture is resampled anyway, so snapping would not sharpen anything. It would also make a pinch jump: trackpad_pinch_anchors_fractional_zoom_and_scroll_pans pinches by 0.1% and requires the point under the cursor to stay put within 1e-5. My first version snapped at every percentage and failed that test, so I narrowed the rule to 1:1.
  • fit_rect is shared, so the Compare view gets the same fix. One function, one place to read the rule.
  • Only panels/detail.rs changes (+26, -1).

Before and after

Crops of the plush eye from the original post. These are the reporter's screenshots, not output from this branch.

Before: soft in LightCraft After: matches the original
before after

Measured against the original file, headless at 2x:

PSNR Fine detail kept
main, default centred pan 39.5 dB 68%
This branch, same pan 71.4 dB 100%
main, pan nudged by hand to a whole pixel 71.4 dB 100%

The last row is how I found the cause. The same render, shifted by half a pixel, goes from 71.4 dB to 39.5 dB.

How I measured it:

  1. Import the file into a fresh library, then lightcraft-cli snapshot --size 1600x1000 --scale 2 with a script that runs view.detail, view.navigate {zoom: {percent: 100}, pan: [0.5, 0.5]} and ui.settle, then ui.screenshot.
  2. Cut a 512x512 grayscale patch from the middle of the screenshot and find the same patch in the original by cross-correlation.
  3. PSNR between the two patches. "Fine detail kept" is the mean absolute difference between neighbouring pixels along x, view divided by original.

The offset in this file was in x only (y already sat on a whole pixel), so the detail figure uses the x direction.

Tests

  • New: panels::detail::preview_geometry_tests::percent_zoom_lands_on_whole_physical_pixels. It uses the 1279.5 pt canvas from the issue and asserts the corner is a whole physical pixel and the size is still the photo's own pixels, at ppp 1, 1.25, 2 and 3, with three pans each. I checked it against main by removing the snap and re-running: it fails with "-1936.25 pt is not a whole pixel at 1x".
  • trackpad_pinch_anchors_fractional_zoom_and_scroll_pans and the rest of lightcraft-ui-egui pass.
  • Checked by eye in the desktop app: the file from the issue at 100% now matches the original's resolution.

A note for anyone running the crate's tests alone: cargo test -p lightcraft-ui-egui fails 6 tests_model_setup tests with "this build cannot run denoise models". The denoise feature reaches lightcraft-engine through the lightcraft app crate's defaults, so a single-crate run has it off. cargo test --workspace unifies the feature and they pass. This happens on main too.

CI

cargo xtask ci is green on this branch, all eight steps:

Step Result
fmt pass
clippy (workspace, all targets, -D warnings) pass
heif pass
test (cargo test --workspace) pass, 3453 tests across 104 binaries, 0 failures
parity pass
layers pass
assets pass
wasm pass, all 28 crates check for wasm32-unknown-unknown

The wasm step needed rustup target add wasm32-unknown-unknown on my machine first. Worth knowing for anyone whose first cargo xtask ci fails there with "the wasm32-unknown-unknown target may not be installed".

No workflow runs on this PR: freebsd.yml needs a Cargo.toml or lockfile change, packaging-lint.yml needs .github/, packaging/ or app-icon changes, and release.yml is release-branch only. This change touches only crates/ui-egui/src/panels/detail.rs.

Not done

  • No row added to docs/parity-checklist.md. This is a bug fix, not a new feature, and the LR-DEV-LOUPE behaviour it belongs to is already listed.
  • The new test checks geometry, not pixels. The pixel figures above come from snapshot measurements that are not in the test suite.
  • The Export dialog still opens at a 2048 px long edge on a first export, which is the separate suggestion in the issue thread. The CLI export of this file was already full size and faithful.
  • Fit and other zoom levels stay softer than a 1:1 view by design, because the photo is resampled to fit the screen.

🤖 Generated with Claude Code

At 100% the photo's corner could sit half a physical pixel off, for
example in a 1279.5 pt canvas on a 2x display. The GPU then resampled
the photo with its bilinear filter and blurred it. On a 5152x7728 JPEG
the 100% view kept 68% of the fine detail.

fit_rect now rounds the corner to a whole physical pixel when the photo
is 1:1 with the screen. Other zooms resample anyway, and a pinch must
not jump, so they stay as they were. With the fix the same view matches
the original (71.4 dB PSNR, 100% of the detail).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@echelon

echelon commented Oct 15, 2026

Copy link
Copy Markdown
Member

Thank you @john01px2022! This landed on main in integration batch 36 (#1843).

If you haven't already, please join our Discord: https://discord.gg/artcraft. We'd love to have you there.

@john01px2022

Copy link
Copy Markdown
Contributor Author

cargo xtask ci is green on this branch, all eight steps: fmt, clippy, heif, test (3453 tests across 104 binaries, 0 failures), parity, layers, assets and wasm (28 crates check for wasm32-unknown-unknown).

I also checked the new test against main by removing the snap and re-running it. It fails there with "-1936.25 pt is not a whole pixel at 1x", and passes with the snap in place.

aiongg pushed a commit to aiongg/lightcraft that referenced this pull request Oct 21, 2026
…Lanczos decode)

At Fit the loupe was softer than a good downscale of the same file. Four
resamples stacked up between the source and the screen:

- the render edge is rounded up to a 64 px step, so the texture was drawn up
  to ~6 % smaller than its size and the GPU shrank it with 2-tap bilinear
  sampling;
- the fitted texture sat on a fractional pixel position, so even at the right
  size every screen pixel was averaged with its neighbour (storytold#1831 fixed the
  same for 100 %);
- the render box truncated its short side (2560 x 1706 for a 2560 x 1707
  source), a slightly anisotropic resample that blurred one axis only;
- the codec's decode-time shrink at 2:1 and more, and SourceRef::Fit, used a
  box filter, which halves the contrast of detail near the output's limit.

RenderJob::display_fit shrinks a full-quality render that is larger than its
drawn size to exactly that size with Lanczos-3 on the worker (after the caches
have the full render; drafts untouched; part of the key). The Detail view sets
it for the After and Before renders at rest, and snap_fit_rect draws a texture
that is its drawn size on whole physical pixels. The render boxes round their
short side, and the 2:1-and-more shrinks use Lanczos-3.

Refs storytold#1472, storytold#1042.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: jpg images drop in resolution when imported?

2 participants