Skip to content

Commit

Permalink
Adjustments > Brightness and Contrast: fix potential overflow error
Browse files Browse the repository at this point in the history
Relates to #452 .  Thank you to @opsimathic for catching and reporting!

Brightness and contrast are possibly the oldest adjustments in PhotoDemon (20+ years old at this point), making this a very old bug!

When calculating average luminance for sampled contrast, I had mistakenly typed integer divide instead of floating-point divide and this can produce an overflow for images whose net brightness exceeds MAX_INT (~4.2e9).

Problem now solved; updated nightly incoming shortly...
  • Loading branch information
tannerhelland committed Oct 21, 2022
1 parent 7105afb commit 42aa19c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions Forms/Adjustments_BrightnessContrast.frm
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ Attribute VB_Exposed = False
'Brightness and Contrast Handler
'Copyright 2001-2022 by Tanner Helland
'Created: 2/6/01
'Last updated: 09/June/16
'Last update: total overhaul; the old code was stupid and slow, and an option is now provided for a modern
' L*a*b*-based conversion (via LittleCMS, if available)
'Last updated: 21/October/22
'Last update: fix potential overflow due to unintended use of \ instead of /
' (see https://github.com/tannerhelland/PhotoDemon/issues/452)
'
'Basic brightness/contrast handler. A legacy LUT-based method is provided, but the modern L*a*b* implementation
' (via LittleCMS) is preferred.
Expand Down Expand Up @@ -248,9 +248,9 @@ Public Sub BrightnessContrast(ByVal functionParams As String, Optional ByVal toP
Else

Dim rTotal As Single, gTotal As Single, bTotal As Single
rTotal = 0#
gTotal = 0#
bTotal = 0#
rTotal = 0!
gTotal = 0!
bTotal = 0!

Dim numOfPixels As Long
numOfPixels = 0
Expand All @@ -265,11 +265,11 @@ Public Sub BrightnessContrast(ByVal functionParams As String, Optional ByVal toP
Next x
Next y

rTotal = rTotal \ numOfPixels
gTotal = gTotal \ numOfPixels
bTotal = bTotal \ numOfPixels
rTotal = rTotal / numOfPixels
gTotal = gTotal / numOfPixels
bTotal = bTotal / numOfPixels

imgMean = (rTotal + gTotal + bTotal) \ 3
imgMean = Int((rTotal + gTotal + bTotal) / 3! + 0.5!)

'As mentioned earlier, cache the sample contrast during preview mode
If toPreview Then
Expand All @@ -291,7 +291,7 @@ Public Sub BrightnessContrast(ByVal functionParams As String, Optional ByVal toP
ctCalc = srcBrightness + (((srcBrightness - imgMean) * newContrast) \ 100)
If (ctCalc > 255) Then ctCalc = 255
If (ctCalc < 0) Then ctCalc = 0
newBCTable(x) = CByte(ctCalc)
newBCTable(x) = ctCalc
Next x

End If
Expand Down
2 changes: 1 addition & 1 deletion PhotoDemon.vbp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ Description="PhotoDemon Photo Editor"
CompatibleMode="0"
MajorVer=9
MinorVer=0
RevisionVer=35
RevisionVer=36
AutoIncrementVer=1
ServerSupportFiles=0
VersionComments="Copyright 2000-2022 Tanner Helland - photodemon.org"
Expand Down

0 comments on commit 42aa19c

Please sign in to comment.