diff --git a/Sources/geometrize/Bitmap.swift b/Sources/geometrize/Bitmap.swift index eafefea..890fa7d 100644 --- a/Sources/geometrize/Bitmap.swift +++ b/Sources/geometrize/Bitmap.swift @@ -371,3 +371,30 @@ extension Bitmap { } } + +extension Bitmap { + + /// Calculates the root-mean-square error between two bitmaps. + func differenceFull(with second: Bitmap) -> Double { + assert(width == second.width) + assert(height == second.height) + + var total: Int64 = 0 + + for y in 0.. Double { - assert(first.width == second.width) - assert(first.height == second.height) - - let width = first.width - let height = first.height - var total: Int64 = 0 - - for y in 0.. differenceFull(first: blackBitmap, second: blackBitmapOnePixelChanged)) + XCTAssertTrue(blackBitmap.differenceFull(with: blackBitmapTwoPixelsChanged) > blackBitmap.differenceFull(with: blackBitmapOnePixelChanged)) // Now the same for white image let whiteBitmap = Bitmap(width: 10, height: 10, color: .white) // Difference with itself is 0 - XCTAssertEqual(differenceFull(first: whiteBitmap, second: whiteBitmap), 0) + XCTAssertEqual(whiteBitmap.differenceFull(with: whiteBitmap), 0) var whiteBitmapOnePixelChanged = whiteBitmap whiteBitmapOnePixelChanged[0, 0] = .black @@ -29,7 +29,7 @@ final class CoreTests: XCTestCase { whiteBitmapTwoPixelsChanged[0, 1] = .black // Changing two pixels means there's more difference than changing one. - XCTAssertTrue(differenceFull(first: whiteBitmap, second: whiteBitmapTwoPixelsChanged) > differenceFull(first: whiteBitmap, second: whiteBitmapOnePixelChanged)) + XCTAssertTrue(whiteBitmap.differenceFull(with: whiteBitmapTwoPixelsChanged) > whiteBitmap.differenceFull(with: whiteBitmapOnePixelChanged)) } func testDifferenceFullComparingResultWithCPlusPlus() throws { @@ -37,7 +37,7 @@ final class CoreTests: XCTestCase { let bitmapFirst = Bitmap(stringLiteral: try String(contentsOf: firstUrl)) let secondUrl = Bundle.module.url(forResource: "differenceFull bitmap second", withExtension: "txt")! let bitmapSecond = Bitmap(stringLiteral: try String(contentsOf: secondUrl)) - XCTAssertEqual(differenceFull(first: bitmapFirst, second: bitmapSecond), 0.170819, accuracy: 0.000001) + XCTAssertEqual(bitmapFirst.differenceFull(with: bitmapSecond), 0.170819, accuracy: 0.000001) } func testDifferencePartialComparingResultWithCPlusPlus() throws {