Skip to content

fix: Five places in the image decoders turned a sampling coordinate into a pixel index - #509

Merged
guitarrapc merged 1 commit into
mainfrom
fix
Nov 26, 2026
Merged

guitarrapc merged 1 commit into
mainfrom
fix

Conversation

@guitarrapc

Copy link
Copy Markdown
Owner

Summary

Five places in the image decoders turned a sampling coordinate into a pixel index, and each did it differently. They disagreed on NaN, infinity, coordinates past the int range and negative fractions. This makes all five go through the shared helpers (PixelIndex.Clamp, VectorCast.ToPixel) or a negated float range check, so every sampler picks the same pixel on every runtime.

Intent

  • Crash risk: (int)NaN is 0 from .NET 9 on, but int.MinValue on .NET 8 and .NET Framework, which run the netstandard2.0 build. Two point readers cast NaN without checking it, so a NaN coordinate would index out of range there. No path that delivers NaN has been found, but the decoder should return false rather than throw.
  • Consistency: the samplers already have shared helpers that clamp the same way on every runtime. The remaining sites now use them, or check bounds on the float.
  • Speed: the 256-bit grid sampler was the only tier still using the saturating Vector256.ConvertToInt32 followed by an integer clamp.

Before / After

Site Before After
Standard QR timing check (IsDarkAt) Hand-written clamp let NaN through to the cast: out-of-range index on netstandard2.0 hosts PixelIndex.Clamp: NaN maps to 0 on every runtime
Micro QR timing-line fit (IsDarkAt) NaN passed the x < 0 check and was cast Negated range check, so NaN is outside
rMQR format/timing check (SampleDark) Cast, then clamp: on netstandard2.0, +∞ landed on column 0 while the grid sampler took the last column PixelIndex.Clamp, same pixel as the grid. NaN still reads light
Finder ring walk (TryDarkLightDarkRun) Cast before the bound check: a point at −0.5 truncated to 0 and counted as inside, so walks toward the left or top edge ran one step long Bounds checked on the float, so left and top behave like right and bottom
256-bit grid sampler Saturating convert, then integer clamp VectorCast.ToPixel (float clamp, then the plain conversion), matching the 128-bit tier

Measured against the previous commit on AVX2:

Before After
256-bit grid sampler, version 40 0.89 ns/module 0.84 ns/module
256-bit grid sampler, version 10 0.90 ns/module 0.87 ns/module
Finder walk 6.2 ns 6.0 ns

@guitarrapc
guitarrapc merged commit 7e791f2 into main Nov 26, 2026
28 checks passed
@guitarrapc
guitarrapc deleted the fix branch November 26, 2026 10:31
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.

1 participant