new image diff method

This commit is contained in:
treeform 2022-07-08 20:19:36 -07:00
parent 595ddeaa50
commit e138b44280
2 changed files with 68 additions and 46 deletions

1
.gitignore vendored
View file

@ -17,3 +17,4 @@ dump.txt
tests/fileformats/jpeg/generated tests/fileformats/jpeg/generated
tests/fileformats/jpeg/diffs tests/fileformats/jpeg/diffs
*.dylib *.dylib
tmp

View file

@ -1,4 +1,25 @@
import chroma, pixie import chroma, pixie, strformat, os, strutils
proc makeDirs(dirs: string) =
var path = ""
for dir in dirs.split("/"):
path.add dir
if not dirExists(path):
echo "mkdir ", path
createDir(path)
path.add "/"
proc diffVs(image: Image, masterPath: string) =
let
master = readImage(masterPath)
(score, xRay) = diff(image, master)
imagePath = "tmp/generated/" & masterPath
xRayPath = "tmp/xray/" & masterPath
makeDirs(imagePath.splitPath.head)
makeDirs(xRayPath.splitPath.head)
image.writeFile(imagePath)
xRay.writeFile(xRayPath)
echo &"diff {masterPath} vs {imagePath} -> {xRayPath} with {score}"
block: block:
let ctx = newContext(newImage(300, 160)) let ctx = newContext(newImage(300, 160))
@ -17,7 +38,7 @@ block:
ctx.clearRect(10, 10, 120, 100) ctx.clearRect(10, 10, 120, 100)
ctx.image.writeFile("tests/contexts/clearRect_1.png") ctx.image.diffVs("tests/contexts/clearRect_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -34,7 +55,7 @@ block:
ctx.lineTo(120, 120) ctx.lineTo(120, 120)
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/beginPath_1.png") ctx.image.diffVs("tests/contexts/beginPath_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -46,7 +67,7 @@ block:
ctx.lineTo(280, 120) ctx.lineTo(280, 120)
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/moveTo_1.png") ctx.image.diffVs("tests/contexts/moveTo_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -63,7 +84,7 @@ block:
ctx.fillStyle = "green" ctx.fillStyle = "green"
ctx.fill(region, EvenOdd) ctx.fill(region, EvenOdd)
ctx.image.writeFile("tests/contexts/fill_1.png") ctx.image.diffVs("tests/contexts/fill_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -71,7 +92,7 @@ block:
ctx.rect(10, 10, 150, 100) ctx.rect(10, 10, 150, 100)
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/stroke_1.png") ctx.image.diffVs("tests/contexts/stroke_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -94,7 +115,7 @@ block:
ctx.lineTo(280, 140) ctx.lineTo(280, 140)
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/stroke_2.png") ctx.image.diffVs("tests/contexts/stroke_2.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -112,7 +133,7 @@ block:
ctx.stroke() ctx.stroke()
ctx.fill() ctx.fill()
ctx.image.writeFile("tests/contexts/stroke_3.png") ctx.image.diffVs("tests/contexts/stroke_3.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -124,7 +145,7 @@ block:
ctx.closePath() ctx.closePath()
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/closePath_1.png") ctx.image.diffVs("tests/contexts/closePath_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -140,7 +161,7 @@ block:
ctx.bezierCurveTo(cp1, cp2, to) ctx.bezierCurveTo(cp1, cp2, to)
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/bezierCurveTo_1.png") ctx.image.diffVs("tests/contexts/bezierCurveTo_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -150,7 +171,7 @@ block:
ctx.bezierCurveTo(120, 160, 180, 10, 220, 140) ctx.bezierCurveTo(120, 160, 180, 10, 220, 140)
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/bezierCurveTo_2.png") ctx.image.diffVs("tests/contexts/bezierCurveTo_2.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -159,7 +180,7 @@ block:
ctx.quadraticCurveTo(230, 30, 50, 100) ctx.quadraticCurveTo(230, 30, 50, 100)
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/quadracticCurveTo_1.png") ctx.image.diffVs("tests/contexts/quadracticCurveTo_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -169,7 +190,7 @@ block:
ctx.quadraticCurveTo(230, 150, 250, 20) ctx.quadraticCurveTo(230, 150, 250, 20)
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/quadracticCurveTo_2.png") ctx.image.diffVs("tests/contexts/quadracticCurveTo_2.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -178,7 +199,7 @@ block:
ctx.ellipse(100, 75, 75, 50) ctx.ellipse(100, 75, 75, 50)
ctx.stroke() ctx.stroke()
ctx.image.writeFile("tests/contexts/ellipse_1.png") ctx.image.diffVs("tests/contexts/ellipse_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -186,7 +207,7 @@ block:
ctx.strokeStyle = "green" ctx.strokeStyle = "green"
ctx.strokeRect(20, 10, 160, 100) ctx.strokeRect(20, 10, 160, 100)
ctx.image.writeFile("tests/contexts/strokeRect_1.png") ctx.image.diffVs("tests/contexts/strokeRect_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -196,7 +217,7 @@ block:
ctx.strokeStyle = "#38f" ctx.strokeStyle = "#38f"
ctx.strokeRect(30, 30, 160, 90) ctx.strokeRect(30, 30, 160, 90)
ctx.image.writeFile("tests/contexts/strokeRect_2.png") ctx.image.diffVs("tests/contexts/strokeRect_2.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -204,7 +225,7 @@ block:
ctx.setTransform(mat3(1, 0.2, 0, 0.8, 1, 0, 0, 0, 1)) ctx.setTransform(mat3(1, 0.2, 0, 0.8, 1, 0, 0, 0, 1))
ctx.fillRect(0, 0, 100, 100) ctx.fillRect(0, 0, 100, 100)
ctx.image.writeFile("tests/contexts/setTransform_1.png") ctx.image.diffVs("tests/contexts/setTransform_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -212,7 +233,7 @@ block:
ctx.setTransform(mat3(1, 0.2, 0, 0.8, 1, 0, 0, 0, 1)) ctx.setTransform(mat3(1, 0.2, 0, 0.8, 1, 0, 0, 0, 1))
ctx.fillRect(0, 0, 100, 100) ctx.fillRect(0, 0, 100, 100)
ctx.image.writeFile("tests/contexts/resetTransform_1.png") ctx.image.diffVs("tests/contexts/resetTransform_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -220,7 +241,7 @@ block:
ctx.rotate(45 * PI / 180) ctx.rotate(45 * PI / 180)
ctx.fillRect(60, 0, 100, 30) ctx.fillRect(60, 0, 100, 30)
ctx.image.writeFile("tests/contexts/resetTransform_1.png") ctx.image.diffVs("tests/contexts/resetTransform_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -235,7 +256,7 @@ block:
ctx.fillRect(40, 40, 50, 20) ctx.fillRect(40, 40, 50, 20)
ctx.fillRect(40, 90, 50, 20) ctx.fillRect(40, 90, 50, 20)
ctx.image.writeFile("tests/contexts/resetTransform_2.png") ctx.image.diffVs("tests/contexts/resetTransform_2.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -249,7 +270,7 @@ block:
ctx.fillStyle = "gray" ctx.fillStyle = "gray"
ctx.fillRect(0, 0, 80, 80) ctx.fillRect(0, 0, 80, 80)
ctx.image.writeFile("tests/contexts/translate_1.png") ctx.image.diffVs("tests/contexts/translate_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -263,7 +284,7 @@ block:
ctx.fillStyle = "gray" ctx.fillStyle = "gray"
ctx.fillRect(10, 10, 8, 20) ctx.fillRect(10, 10, 8, 20)
ctx.image.writeFile("tests/contexts/scale_1.png") ctx.image.diffVs("tests/contexts/scale_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -275,7 +296,7 @@ block:
ctx.fillStyle = "red" ctx.fillStyle = "red"
ctx.fillRect(100, 0, 80, 20) ctx.fillRect(100, 0, 80, 20)
ctx.image.writeFile("tests/contexts/rotate_1.png") ctx.image.diffVs("tests/contexts/rotate_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -288,7 +309,7 @@ block:
ctx.fillText("Hello world", 50, 90) ctx.fillText("Hello world", 50, 90)
ctx.image.writeFile("tests/contexts/fillText_1.png") ctx.image.diffVs("tests/contexts/fillText_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -298,7 +319,7 @@ block:
ctx.strokeText("Hello world", 50, 90) ctx.strokeText("Hello world", 50, 90)
ctx.image.writeFile("tests/contexts/strokeText_1.png") ctx.image.diffVs("tests/contexts/strokeText_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -312,7 +333,7 @@ block:
ctx.fillRect(150, 40, 100, 100) ctx.fillRect(150, 40, 100, 100)
ctx.image.writeFile("tests/contexts/save_1.png") ctx.image.diffVs("tests/contexts/save_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -326,7 +347,7 @@ block:
ctx.fillStyle = "orange" ctx.fillStyle = "orange"
ctx.fillRect(0, 0, 100, 100) ctx.fillRect(0, 0, 100, 100)
ctx.image.writeFile("tests/contexts/clip_1.png") ctx.image.diffVs("tests/contexts/clip_1.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -343,7 +364,7 @@ block:
ctx.fillStyle = "orange" ctx.fillStyle = "orange"
ctx.fillRect(0, 0, 100, 100) ctx.fillRect(0, 0, 100, 100)
ctx.image.writeFile("tests/contexts/clip_1b.png") ctx.image.diffVs("tests/contexts/clip_1b.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -364,7 +385,7 @@ block:
ctx.fillStyle = "blue" ctx.fillStyle = "blue"
ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32) ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32)
ctx.image.writeFile("tests/contexts/clip_1c.png") ctx.image.diffVs("tests/contexts/clip_1c.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -385,7 +406,7 @@ block:
ctx.restore() ctx.restore()
ctx.image.writeFile("tests/contexts/clip_1d.png") ctx.image.diffVs("tests/contexts/clip_1d.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -409,7 +430,7 @@ block:
ctx.fillStyle = "blue" ctx.fillStyle = "blue"
ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32) ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32)
ctx.image.writeFile("tests/contexts/clip_1e.png") ctx.image.diffVs("tests/contexts/clip_1e.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -433,7 +454,7 @@ block:
ctx.restore() # Pop the layer ctx.restore() # Pop the layer
ctx.image.writeFile("tests/contexts/clip_1f.png") ctx.image.diffVs("tests/contexts/clip_1f.png")
block: block:
let ctx = newContext(newImage(300, 150)) let ctx = newContext(newImage(300, 150))
@ -446,7 +467,7 @@ block:
ctx.fillStyle = "blue" ctx.fillStyle = "blue"
ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32) ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32)
ctx.image.writeFile("tests/contexts/clip_2.png") ctx.image.diffVs("tests/contexts/clip_2.png")
block: block:
let image = newImage(300, 150) let image = newImage(300, 150)
@ -464,7 +485,7 @@ block:
ctx.fillStyle = "blue" ctx.fillStyle = "blue"
ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32) ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32)
image.writeFile("tests/contexts/clip_3.png") image.diffVs("tests/contexts/clip_3.png")
block: block:
let image = newImage(300, 150) let image = newImage(300, 150)
@ -485,7 +506,7 @@ block:
ctx.restore() ctx.restore()
image.writeFile("tests/contexts/clip_text.png") image.diffVs("tests/contexts/clip_text.png")
block: block:
let ctx = newContext(100, 100) let ctx = newContext(100, 100)
@ -517,7 +538,7 @@ block:
drawDashedLine(@[20.float32, 3, 3, 3, 3, 3, 3, 3]) drawDashedLine(@[20.float32, 3, 3, 3, 3, 3, 3, 3])
drawDashedLine(@[12.float32, 3, 3]) drawDashedLine(@[12.float32, 3, 3])
image.writeFile("tests/contexts/setLineDash_1.png") image.diffVs("tests/contexts/setLineDash_1.png")
block: block:
let let
@ -534,7 +555,7 @@ block:
ctx.fillRect(10, 10, 100, 100) ctx.fillRect(10, 10, 100, 100)
image.writeFile("tests/contexts/blendmode_1.png") image.diffVs("tests/contexts/blendmode_1.png")
block: block:
let let
@ -551,7 +572,7 @@ block:
ctx.fillStyle = "red" ctx.fillStyle = "red"
ctx.fillRect(50, 50, 100, 100) ctx.fillRect(50, 50, 100, 100)
image.writeFile("tests/contexts/globalAlpha_1.png") image.diffVs("tests/contexts/globalAlpha_1.png")
block: block:
let let
@ -560,7 +581,7 @@ block:
testImage = readImage("tests/images/pip1.png") testImage = readImage("tests/images/pip1.png")
ctx.drawImage(testImage, 0, 0) ctx.drawImage(testImage, 0, 0)
ctx.drawImage(testImage, 30, 30) ctx.drawImage(testImage, 30, 30)
image.writeFile("tests/contexts/draw_image.png") image.diffVs("tests/contexts/draw_image.png")
block: block:
let let
@ -570,7 +591,7 @@ block:
ctx.translate(30, 30) ctx.translate(30, 30)
ctx.drawImage(testImage, -30, -30) ctx.drawImage(testImage, -30, -30)
ctx.drawImage(testImage, 0, 0) ctx.drawImage(testImage, 0, 0)
image.writeFile("tests/contexts/draw_image_translated.png") image.diffVs("tests/contexts/draw_image_translated.png")
block: block:
let let
@ -581,7 +602,7 @@ block:
ctx.drawImage(testImage, 0, 0) ctx.drawImage(testImage, 0, 0)
ctx.scale(0.25, 0.25) ctx.scale(0.25, 0.25)
ctx.drawImage(testImage, 0, 0) ctx.drawImage(testImage, 0, 0)
image.writeFile("tests/contexts/draw_image_scaled.png") image.diffVs("tests/contexts/draw_image_scaled.png")
block: block:
let let
@ -589,7 +610,7 @@ block:
ctx = newContext(image) ctx = newContext(image)
testImage = readImage("tests/images/pip1.png") testImage = readImage("tests/images/pip1.png")
ctx.drawImage(testImage, 30, 30, 20, 20) ctx.drawImage(testImage, 30, 30, 20, 20)
image.writeFile("tests/contexts/draw_image_self_scaled.png") image.diffVs("tests/contexts/draw_image_self_scaled.png")
block: block:
let let
@ -597,7 +618,7 @@ block:
ctx = newContext(image) ctx = newContext(image)
rhino = readImage("tests/images/rhino.png") rhino = readImage("tests/images/rhino.png")
ctx.drawImage(rhino, 33, 71, 104, 124, 21, 20, 87, 104) ctx.drawImage(rhino, 33, 71, 104, 124, 21, 20, 87, 104)
image.writeFile("tests/contexts/draw_image_rhino.png") image.diffVs("tests/contexts/draw_image_rhino.png")
block: block:
let let
@ -605,7 +626,7 @@ block:
ctx = newContext(image) ctx = newContext(image)
rhino = readImage("tests/images/rhino.png") rhino = readImage("tests/images/rhino.png")
ctx.drawImage(rhino, rect(33, 71, 104, 124), rect(21, 20, 87, 104)) ctx.drawImage(rhino, rect(33, 71, 104, 124), rect(21, 20, 87, 104))
image.writeFile("tests/contexts/draw_image_rhino2.png") image.diffVs("tests/contexts/draw_image_rhino2.png")
block: block:
let let
@ -643,7 +664,7 @@ block:
ctx.fillStyle.color = color(0, 0, 1, 1) ctx.fillStyle.color = color(0, 0, 1, 1)
ctx.restore() ctx.restore()
ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32) ctx.fillRect(0, 0, ctx.image.width.float32, ctx.image.height.float32)
ctx.image.writeFile("tests/contexts/paintSaveRestore.png") ctx.image.diffVs("tests/contexts/paintSaveRestore.png")
block: block:
# From https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline # From https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline
@ -674,4 +695,4 @@ block:
ctx.stroke() ctx.stroke()
ctx.fillText("Abcdefghijklmnop (" & $baseline & ")", 0, y) ctx.fillText("Abcdefghijklmnop (" & $baseline & ")", 0, y)
ctx.image.writeFile("tests/contexts/textBaseline_1.png") ctx.image.diffVs("tests/contexts/textBaseline_1.png")