Skip to content

Commit

Permalink
assertImagesEqual : Add maxDifferenceGreater and maxDifferenceLess
Browse files Browse the repository at this point in the history
This allows using a different tolerance for pixels where A > B and pixels where A < B.
  • Loading branch information
danieldresser-ie committed Dec 14, 2023
1 parent 79c6c94 commit 4572055
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
28 changes: 28 additions & 0 deletions python/GafferImageTest/ConstantTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,5 +208,33 @@ def testLayerAffectsChannelNames( self ) :

self.assertTrue( c["out"]["channelNames"] in set( [ x[0] for x in cs ] ) )

def testAssertImagesEqual( self ) :

a = GafferImage.Constant()
a["color"].setValue( imath.Color4f( 0.5 ) )

b = GafferImage.Constant()
b["color"].setValue( imath.Color4f( 0.5 ) )

self.assertImagesEqual( a["out"], b["out"] )

a["color"].setValue( imath.Color4f( 0.75 ) )

with self.assertRaisesRegex( AssertionError, "0.25 not less than or equal to 0.0 : Channel R" ) :
self.assertImagesEqual( a["out"], b["out"] )

self.assertImagesEqual( a["out"], b["out"], maxDifference = 0.25 )

self.assertImagesEqual( a["out"], b["out"], maxDifferenceGreater = 0.25 )
with self.assertRaisesRegex( AssertionError, "-0.25 not greater than or equal to -0.0 : Channel R" ) :
self.assertImagesEqual( a["out"], b["out"], maxDifferenceLess = 0.25 )

b["color"].setValue( imath.Color4f( 1.0 ) )

self.assertImagesEqual( a["out"], b["out"], maxDifferenceLess = 0.25 )
with self.assertRaisesRegex( AssertionError, "0.25 not less than or equal to 0.0 : Channel R" ) :
self.assertImagesEqual( a["out"], b["out"], maxDifferenceGreater = 0.25 )


if __name__ == "__main__":
unittest.main()
18 changes: 15 additions & 3 deletions python/GafferImageTest/ImageTestCase.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def assertImageHashesEqual( self, imageA, imageB ) :
tileOrigin.x += GafferImage.ImagePlug.tileSize()
tileOrigin.y += GafferImage.ImagePlug.tileSize()

def assertImagesEqual( self, imageA, imageB, maxDifference = 0.0, ignoreMetadata = False, ignoreDataWindow = False, ignoreChannelNamesOrder = False, ignoreViewNamesOrder = False, metadataBlacklist = [] ) :
def assertImagesEqual( self, imageA, imageB, maxDifference = 0.0, ignoreMetadata = False, ignoreDataWindow = False, ignoreChannelNamesOrder = False, ignoreViewNamesOrder = False, metadataBlacklist = [], maxDifferenceGreater = None, maxDifferenceLess = None ) :

self.longMessage = True

Expand All @@ -107,6 +107,11 @@ def assertImagesEqual( self, imageA, imageB, maxDifference = 0.0, ignoreMetadata
else :
self.assertEqual( set( imageA.viewNames() ), set( imageB.viewNames() ) )

if maxDifferenceGreater is None:
maxDifferenceGreater = maxDifference
if maxDifferenceLess is None:
maxDifferenceLess = maxDifference

for view in imageA.viewNames():

with Gaffer.Context( Gaffer.Context.current() ) as context :
Expand Down Expand Up @@ -141,7 +146,6 @@ def assertImagesEqual( self, imageA, imageB, maxDifference = 0.0, ignoreMetadata
difference = GafferImage.Merge()
difference["in"][0].setInput( imageA )
difference["in"][1].setInput( imageB )
difference["operation"].setValue( GafferImage.Merge.Operation.Difference )

unionDataWindow = imath.Box2i( dataWindowA )
unionDataWindow.extendBy( dataWindowB )
Expand All @@ -153,7 +157,15 @@ def assertImagesEqual( self, imageA, imageB, maxDifference = 0.0, ignoreMetadata
for channelName in imageA["channelNames"].getValue() :

stats["channels"].setValue( IECore.StringVectorData( [ channelName ] * 4 ) )
self.assertLessEqual( stats["max"]["r"].getValue(), maxDifference, "Channel {0}".format( channelName ) )
difference["operation"].setValue( GafferImage.Merge.Operation.Difference )
self.assertLessEqual(
stats["max"]["r"].getValue(), max( maxDifferenceGreater, maxDifferenceLess ),
"Channel {0}".format( channelName )
)
if maxDifferenceGreater != maxDifferenceLess:
difference["operation"].setValue( GafferImage.Merge.Operation.Subtract )
self.assertLessEqual( stats["max"]["r"].getValue(), maxDifferenceLess, "Channel {0}".format( channelName ) )
self.assertGreaterEqual( stats["min"]["r"].getValue(), -maxDifferenceGreater, "Channel {0}".format( channelName ) )
# Access the tiles, because this will throw an error if the sample offsets are bogus
GafferImage.ImageAlgo.tiles( imageA )
GafferImage.ImageAlgo.tiles( imageB )
Expand Down

0 comments on commit 4572055

Please sign in to comment.