diff --git a/src/pixie/images.nim b/src/pixie/images.nim index 97617e0..c1e7f13 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -169,6 +169,36 @@ proc superImage*(image: Image, x, y, w, h: int): Image = copyWidth * 4 ) +proc diff*(master, image: Image): (float32, Image) = + ## Compares the parameters and returns a score and image of the difference. + let + w = max(master.width, image.width) + h = max(master.height, image.height) + diffImage = newImage(w, h) + + var + diffScore = 0 + diffTotal = 0 + for x in 0 ..< w: + for y in 0 ..< h: + let + m = master[x, y] + u = image[x, y] + diff = (m.r.int - u.r.int) + (m.g.int - u.g.int) + (m.b.int - u.b.int) + var c: ColorRGBX + c.r = abs(m.a.int - u.a.int).clamp(0, 255).uint8 + c.g = diff.clamp(0, 255).uint8 + c.b = (-diff).clamp(0, 255).uint8 + c.a = 255 + diffImage.setRgbaUnsafe(x, y, c) + diffScore += abs(m.r.int - u.r.int) + + abs(m.g.int - u.g.int) + + abs(m.b.int - u.b.int) + + abs(m.a.int - u.a.int) + diffTotal += 255 * 4 + + (100 * diffScore.float32 / diffTotal.float32, diffImage) + proc minifyBy2*(image: Image, power = 1): Image = ## Scales the image down by an integer scale. if power < 0: diff --git a/tests/test_svg.nim b/tests/test_svg.nim index 3fe95e1..27ef0fe 100644 --- a/tests/test_svg.nim +++ b/tests/test_svg.nim @@ -17,7 +17,8 @@ for file in files: let original = readFile(&"tests/images/svg/{file}.svg") image = decodeSvg(original) - # gold = readImage(&"tests/images/svg/{file}.png") + gold = readImage(&"tests/images/svg/{file}.png") - # doAssert image.data == gold.data - image.writeFile(&"tests/images/svg/{file}.png") + let (diffScore, _) = diff(image, gold) + doAssert diffScore < 1 + # image.writeFile(&"tests/images/svg/{file}.png")