Skip to content

Clip float-to-uint8 conversions in save_8bit to prevent overflow wrap#9

Open
nick-shaw wants to merge 1 commit into
mainfrom
clip-uint8-cast-v2
Open

Clip float-to-uint8 conversions in save_8bit to prevent overflow wrap#9
nick-shaw wants to merge 1 commit into
mainfrom
clip-uint8-cast-v2

Conversation

@nick-shaw
Copy link
Copy Markdown
Collaborator

Casts in save_8bit were vulnerable to uint8 wraparound if input image values strayed slightly outside the nominal [0, 1] (float) or [0, 2^bits - 1] (integer) range — TPATs shouldn't contain out of range values, but that's not a guarantee that they won't! NumPy's .astype(np.uint8) wraps modulo 256 on overflow, producing visually broken output where bright pixels become near-black.

Wrapping each expression in np.clip(..., 0, 255) is cheap and defensive: no behaviour change for in-range input, clipping of out-of-range input. Discovered via a separately-fixed analogous bug in blackmagic-io.

@nick-shaw nick-shaw requested a review from yergin May 13, 2026 11:23
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