Skip to content

Commit

Permalink
Add AssertImage2() to test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
a740g committed May 12, 2024
1 parent ddca8ae commit 7024a19
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tests/compile_tests/font/test.bas
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ SUB PrintFontDetails (handle AS LONG, testFileName AS STRING)
_FONT 16
_DEST _CONSOLE

AssertImage image, testFileName + "." + TEST_IMAGE_FORMAT
AssertImage2 image, testFileName + "." + TEST_IMAGE_FORMAT

_FREEIMAGE image

Expand All @@ -104,7 +104,7 @@ SUB PrintFontDetails (handle AS LONG, testFileName AS STRING)
_FONT 16
_DEST _CONSOLE

AssertImage image, testFileName + "u." + TEST_IMAGE_FORMAT
AssertImage2 image, testFileName + "u." + TEST_IMAGE_FORMAT

_FREEIMAGE image

Expand Down
78 changes: 78 additions & 0 deletions tests/compile_tests/utilities/imageassert.bm
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,81 @@ SUB SaveImage (image AS LONG, filename AS STRING)
PUT #f&, , b$
CLOSE #f&
END SUB


FUNCTION GetColorDelta~& (color1 AS _UNSIGNED LONG, color2 AS _UNSIGNED LONG)
GetColorDelta = ABS(_RED32(color1) - _RED32(color2)) + ABS(_GREEN32(color1) - _GREEN32(color2)) + ABS(_BLUE32(color1) - _BLUE32(color2)) + ABS(_ALPHA32(color1) - _ALPHA32(color2))
END FUNCTION


' Same as AssertImage() but is a bit more forgiving
SUB AssertImage2 (originalActualImage AS LONG, expectedFileName AS STRING)
CONST TOLERANCE_LIMIT = 4

DIM actualImage AS LONG
DIM ResultsDir AS STRING, TestPrefix AS STRING

ResultsDir = COMMAND$(1)
TestPrefix = COMMAND$(2)

' Make sure the test result will be seen
_DEST _CONSOLE

' Convert to 32-bit for comparisons
actualImage = _NEWIMAGE(_WIDTH(originalActualImage), _HEIGHT(originalActualImage), 32)
_PUTIMAGE , originalActualImage, actualImage

'First save the result
IF LEN(ResultsDir) THEN _SAVEIMAGE ResultsDir + "/" + TestPrefix + "_" + expectedFileName + "_result.bmp", actualImage

'Compare both images, print whether they are identical
DIM expectedImage AS LONG
expectedImage = _LOADIMAGE(expectedFileName, 32)

DIM actual AS _MEM, expected AS _MEM

IF _WIDTH(actualImage) <> _WIDTH(expectedImage) THEN
PRINT "Failure! Image width differs, actual:"; _WIDTH(actualImage); ", Expected:"; _WIDTH(expectedImage)
GOTO freeImages
END IF

IF _HEIGHT(actualImage) <> _HEIGHT(expectedImage) THEN
PRINT "Failure! Image height differs, actual:"; _HEIGHT(actualImage); ", Expected:"; _HEIGHT(expectedImage)
GOTO freeImages
END IF

actual = _MEMIMAGE(actualImage)
expected = _MEMIMAGE(expectedImage)

IF actual.SIZE <> expected.SIZE THEN
PRINT "Failure! Image sizes differ, Actual:"; actual.SIZE; ", Expected:"; expected.SIZE
GOTO freeImages
END IF

DIM w&: w& = _WIDTH(expectedImage)
DIM h&: h& = _HEIGHT(expectedImage)

DIM x&, y&, pixelOffset AS _OFFSET
DIM AS _UNSIGNED LONG actualPixel, expectedPixel

FOR x& = 0 TO w& - 1
FOR y& = 0 TO h& - 1
pixelOffset = (y& * w& + x&) * 4

actualPixel = _MEMGET(actual, actual.OFFSET + pixelOffset, _UNSIGNED LONG)
expectedPixel = _MEMGET(expected, expected.OFFSET + pixelOffset, _UNSIGNED LONG)

IF actualPixel <> expectedPixel _ANDALSO GetColorDelta(actualPixel, expectedPixel) > TOLERANCE_LIMIT THEN
PRINT "Failure! Image pixels at ("; x&; ","; y&; ") differ, actual: 0x"; HEX$(actualPixel); ", expected: 0x"; HEX$(expectedPixel)
GOTO freeImages
END IF
NEXT
NEXT

PRINT "Success, images are identical!"

freeImages:
_MEMFREE actual
_MEMFREE expected
_FREEIMAGE actualImage
END SUB

0 comments on commit 7024a19

Please sign in to comment.