Fix integer overflow in 16-bit resampling #9480
Open
hayatoikoma wants to merge 1 commit intopython-pillow:mainfrom
Open
Fix integer overflow in 16-bit resampling #9480hayatoikoma wants to merge 1 commit intopython-pillow:mainfrom
hayatoikoma wants to merge 1 commit intopython-pillow:mainfrom
Conversation
668f9e7 to
1fe3da5
Compare
I;16) resampling by clamping float sumsThis commit fixes a bug in Resample.c where downsampling 16-bit images (I;16) using filters with negative lobes (such as Image.Resampling.LANCZOS) could result in byte corruption. Because Lanczos weighting can create overshoots (ringing artifacts) near sharp edges, the accumulated floating-point sum can sometimes exceed the 16-bit maximum (65535) or fall below zero. Previously, these out-of-bounds values were not correctly clamped before being cast or packed into the 16-bit output buffer, leading to integer overflow/underflow and corrupted pixels. This update correctly clamps the accumulated float values to the [0, 65535] range for I;16 images during resampling.
1fe3da5 to
7681016
Compare
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.
📝 Description
This PR fixes a bug in
Resample.cwhere downsampling 16-bit images (I;16) using filters with negative lobes (such asImage.Resampling.LANCZOS) could result in byte corruption.Because Lanczos weighting can create overshoots (ringing artifacts) near sharp edges, the accumulated floating-point sum can sometimes exceed the 16-bit maximum (65535) or fall below zero. Previously, these out-of-bounds values were not correctly clamped before being cast or packed into the 16-bit output buffer, leading to integer overflow/underflow and corrupted pixels.
This update correctly clamps the accumulated float values to the
[0, 65535]range forI;16images during resampling.🛠️ Changes Made
src/libImaging/Resample.c: Added bounding/clamping logic to ensure the accumulated float sum is strictly clamped to0and65535before writing to theI;16output buffer.test_resampling_clampto verify this behavior. The test constructs an image with a hard step edge (0 to 65535) and applies a 5x Lanczos downsampling, comparing the clampedI;16output against a float (F) reference image to ensure no byte corruption occurs.