Validate PyO3 buffer dimensions; fix mono OKLab midpoint comment#49
Open
balloob wants to merge 1 commit into
Open
Validate PyO3 buffer dimensions; fix mono OKLab midpoint comment#49balloob wants to merge 1 commit into
balloob wants to merge 1 commit into
Conversation
Finding 1 (behavior/safety): the PyO3 `dither_image` entry point ignored the caller's `height` argument and let `ImageBuffer::new` derive height by integer division. In a release build a caller passing a `width` that does not match the byte-buffer length would silently truncate trailing pixels instead of erroring. The entry point now validates that the pixel buffer length equals width x height x 3 (flat RGB) and raises `PyValueError` on mismatch. A binding-level pytest exercises both the mismatch and the happy path. The wasm binding takes no `height` argument (dimensions are derived from width alone) and so has no equivalent ignored-parameter issue; it is left unchanged. Finding 2 (docs): two comments in algorithms.rs claimed the mono decision midpoint "sits at OKLab L=0.5 ≈ sRGB 188". That conflates linear 0.5 (which sRGB-encodes to ~188) with OKLab L=0.5, whose neutral-gray sRGB value is ~100. Corrected both comments to ≈ sRGB 100. Comments only; no logic changed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012g2e8mr132vcizx92WsgiR
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Two small, low-risk fixes to the epaper-dithering core and its PyO3 binding.
Finding 1 (behavior/safety): PyO3 entry point ignored the caller's
heightpackages/python/src/lib.rs'sdither_imagediscarded itsheightargument (let _ = height;) and letImageBuffer::newderive height via integer division, with only adebug_assert!guarding consistency. In a release build, a caller passing awidththat does not match the byte-buffer length would silently truncate trailing pixels instead of raising.The entry point now validates that the pixel buffer length equals
width × height × 3(the flat-RGB layout, confirmed 3 channels) using checked multiplication, and raisesPyValueErroron mismatch (or on overflow). A binding-level pytest (TestBufferValidation) covers both the mismatch-raises and the happy path.The wasm binding (
packages/rust/wasm/src/lib.rs) takes noheightargument at all — dimensions are derived fromwidthalone — so it has no equivalent ignored-parameter issue and is left unchanged.Finding 2 (docs): wrong comment about the mono OKLab midpoint
Two comments in
packages/rust/core/src/algorithms.rsclaimed the mono decision midpoint "sits at OKLab L=0.5 ≈ sRGB 188". That conflates linear 0.5 (which sRGB-encodes to ~188) with OKLab L=0.5, whose neutral-gray sRGB value is ~100. Both comments now read ≈ sRGB 100. Comments only — no logic changed, and theordered_dither_activity_is_perceptually_uniformtest is unaffected.Verification
cargo buildandcargo testinpackages/rust: 48 unit + 3 regression tests pass.cargo checkonpackages/python: clean.maturin develop+pytest: 58 tests pass, including the two new validation tests.🤖 Generated with Claude Code
https://claude.ai/code/session_012g2e8mr132vcizx92WsgiR