Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/tiger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
155 changes: 100 additions & 55 deletions experiments/benchmark_cairo.nim
Original file line number Diff line number Diff line change
@@ -1,96 +1,141 @@
import benchy, cairo, chroma, math, pixie
import benchy, cairo, chroma, math, pixie, pixie/paths {.all.}, strformat

proc doDiff(a, b: Image, name: string) =
let (diffScore, diffImage) = diff(a, b)
echo &"{name} score: {diffScore}"
diffImage.writeFile(&"{name}_diff.png")

block:
let path = newPath()
path.moveTo(0, 0)
path.lineTo(1920, 0)
path.lineTo(1920, 1080)
path.lineTo(0, 1080)
path.closePath()

let shapes = path.commandsToShapes(true, 1)

let
surface = imageSurfaceCreate(FORMAT_ARGB32, 1920, 1080)
ctx = surface.create()

ctx.setSourceRgba(0, 0, 1, 1)

timeIt "cairo1":
ctx.newPath()
ctx.moveTo(0, 0)
ctx.lineTo(1920, 0)
ctx.lineTo(1920, 1080)
ctx.lineTo(0, 1080)
ctx.closePath()
ctx.moveTo(shapes[0][0].x, shapes[0][0].y)
for shape in shapes:
for v in shape:
ctx.lineTo(v.x, v.y)
ctx.fill()
surface.flush()

# discard surface.writeToPng("cairo1.png")

let a = newImage(1920, 1080)
a.fill(rgba(255, 255, 255, 255))

timeIt "pixie1":
let p = newPath()
p.moveTo(0, 0)
p.lineTo(1920, 0)
p.lineTo(1920, 1080)
p.lineTo(0, 1080)
p.closePath()
p.moveTo(shapes[0][0])
for shape in shapes:
for v in shape:
p.lineTo(v)
a.fillPath(p, rgba(0, 0, 255, 255))

# a.writeFile("pixie1.png")

block:
let path = newPath()
path.moveTo(500, 240)
path.lineTo(1500, 240)
path.lineTo(1920, 600)
path.lineTo(0, 600)
path.closePath()

let shapes = path.commandsToShapes(true, 1)

let
surface = imageSurfaceCreate(FORMAT_ARGB32, 1920, 1080)
ctx = surface.create()

ctx.setSourceRgba(0, 0, 1, 1)

timeIt "cairo2":
ctx.setSourceRgba(1, 1, 1, 1)
let operator = ctx.getOperator()
ctx.setOperator(OperatorSource)
ctx.paint()
ctx.setOperator(operator)

ctx.setSourceRgba(0, 0, 1, 1)

ctx.newPath()
ctx.moveTo(500, 240)
ctx.lineTo(1500, 240)
ctx.lineTo(1920, 600)
ctx.lineTo(0, 600)
ctx.closePath()
ctx.moveTo(shapes[0][0].x, shapes[0][0].y)
for shape in shapes:
for v in shape:
ctx.lineTo(v.x, v.y)
ctx.fill()
surface.flush()
surface.flush()

# discard surface.writeToPng("cairo2.png")

let a = newImage(1920, 1080)
a.fill(rgba(255, 255, 255, 255))

timeIt "pixie2":
a.fill(rgba(255, 255, 255, 255))

let p = newPath()
p.moveTo(500, 240)
p.lineTo(1500, 240)
p.lineTo(1920, 600)
p.lineTo(0, 600)
p.closePath()
p.moveTo(shapes[0][0])
for shape in shapes:
for v in shape:
p.lineTo(v)
a.fillPath(p, rgba(0, 0, 255, 255))

# a.writeFile("pixie2.png")

# block:
# let
# a = imageSurfaceCreate(FORMAT_ARGB32, 1000, 1000)
# b = imageSurfaceCreate(FORMAT_ARGB32, 500, 500)
# ac = a.create()
# bc = b.create()

# ac.setSourceRgba(1, 0, 0, 1)
# ac.newPath()
# ac.rectangle(0, 0, 1000, 1000)
# ac.fill()

# bc.setSourceRgba(0, 1, 0, 1)
# bc.newPath()
# bc.rectangle(0, 0, 500, 500)
# bc.fill()

# let pattern = patternCreateForSurface(b)

# timeIt "a":
# ac.setSource(pattern)
# ac.save()
# ac.translate(25.2, 25.2)
# ac.rectangle(0, 0, 500, 500)
# ac.fill()
# ac.restore()

# discard a.writeToPng("a.png")
block:
let path = parsePath("""
M 100,300
A 200,200 0,0,1 500,300
A 200,200 0,0,1 900,300
Q 900,600 500,900
Q 100,600 100,300 z
""")

let shapes = path.commandsToShapes(true, 1)

let
surface = imageSurfaceCreate(FORMAT_ARGB32, 1000, 1000)
ctx = surface.create()

timeIt "cairo3":
ctx.setSourceRgba(1, 1, 1, 1)
let operator = ctx.getOperator()
ctx.setOperator(OperatorSource)
ctx.paint()
ctx.setOperator(operator)

ctx.setSourceRgba(1, 0, 0, 1)

ctx.newPath()
ctx.moveTo(shapes[0][0].x, shapes[0][0].y)
for shape in shapes:
for v in shape:
ctx.lineTo(v.x, v.y)
ctx.fill()
surface.flush()

# discard surface.writeToPng("cairo3.png")

let a = newImage(1000, 1000)

timeIt "pixie3":
a.fill(rgba(255, 255, 255, 255))

let p = newPath()
p.moveTo(shapes[0][0])
for shape in shapes:
for v in shape:
p.lineTo(v)
a.fillPath(p, rgba(255, 0, 0, 255))

# a.writeFile("pixie3.png")

# doDiff(readImage("cairo3.png"), a, "cairo3")
Loading