Fix ReverseImages for even image counts, and a 32-bit overflow in ReduceColors - #57
Open
undisker wants to merge 1 commit into
Open
Fix ReverseImages for even image counts, and a 32-bit overflow in ReduceColors#57undisker wants to merge 1 commit into
undisker wants to merge 1 commit into
Conversation
TMultiImage.ReverseImages loops "to GetImageCount div 2". For an EVEN count that reaches the middle pair a second time and swaps it back: [A,B,C,D] I=0 -> exchange(0,3) -> [D,B,C,A] I=1 -> exchange(1,2) -> [D,C,B,A] correct so far I=2 -> exchange(2,1) -> [D,B,C,A] undoes it Odd counts are unaffected (the last iteration is a self-exchange no-op). Using (GetImageCount - 1) div 2 is correct for both parities. ReduceColors declares NumPixels as Int64 but computes it as "Width * Height" with two Integer operands, so the multiplication is done in 32 bits before the assignment widens it - the exact trap the PBuffer comment added in 318c312 warns about. Casting one operand fixes it. Found while maintaining a downstream fork.
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, independent fixes found while maintaining a downstream fork. Happy to split them into separate PRs if you would prefer.
1.
TMultiImage.ReverseImagesis wrong for an even image countThe loop runs
for I := 0 to GetImageCount div 2, which for an even count reaches the middle pair a second time and swaps it back:Expected
[D,C,B,A], actual[D,B,C,A].Odd counts are unaffected — the final iteration is a self-exchange, so it is a harmless no-op rather than a wrong result.
(GetImageCount - 1) div 2is correct for both parities.2.
ReduceColorscomputesNumPixelsin 32 bitsNumPixelsis declaredInt64, buthas two
Integeroperands, so the multiplication is performed in 32 bits and only then widened on assignment. This is exactly the trap described in thePBuffercomment added in 318c312:Casting one operand fixes it. Note the two nearby sites already do this (
Int64(Width) * HeightandNativeInt(Width) * Height); this one looks like it was simply missed in that pass.Both changes are two lines plus a comment. No behaviour change for images that were already working.