pixie/tests/benchmark_images.nim

111 lines
2.5 KiB
Nim
Raw Normal View History

2020-12-09 00:10:20 +00:00
import chroma, pixie, benchy, system/memory
2020-12-03 20:21:58 +00:00
2020-12-09 00:10:20 +00:00
proc fill1(a: Image, rgba: ColorRGBA) =
2020-12-03 20:21:58 +00:00
for y in 0 ..< a.height:
for x in 0 ..< a.width:
a.setRgbaUnsafe(x, y, rgba)
2020-12-09 00:10:20 +00:00
proc fill2*(image: Image, rgba: ColorRgba) =
## Fills the image with a solid color.
if rgba.r == rgba.g and rgba.r == rgba.b and rgba.r == rgba.a:
nimSetMem(image.data[0].addr, rgba.r.cint, image.data.len * 4)
else:
for c in image.data.mitems:
c = rgba
proc invert1(a: Image) =
2020-12-03 20:21:58 +00:00
for y in 0 ..< a.height:
for x in 0 ..< a.width:
var rgba = a.getRgbaUnsafe(x, y)
rgba.r = 255 - rgba.r
rgba.g = 255 - rgba.g
rgba.b = 255 - rgba.b
rgba.a = 255 - rgba.a
a.setRgbaUnsafe(x, y, rgba)
2020-12-09 00:10:20 +00:00
proc invert2*(image: Image) =
for rgba in image.data.mitems:
rgba.r = 255 - rgba.r
rgba.g = 255 - rgba.g
rgba.b = 255 - rgba.b
rgba.a = 255 - rgba.a
proc applyOpacity1(a: Image, opacity: float32): Image =
2020-12-03 20:32:50 +00:00
result = newImage(a.width, a.height)
let op = (255 * opacity).uint32
for y in 0 ..< a.height:
for x in 0 ..< a.width:
var rgba = a.getRgbaUnsafe(x, y)
rgba.a = ((rgba.a.uint32 * op) div 255).clamp(0, 255).uint8
result.setRgbaUnsafe(x, y, rgba)
2020-12-09 00:10:20 +00:00
proc sharpOpacity1(a: Image): Image =
2020-12-03 20:32:50 +00:00
result = newImage(a.width, a.height)
for y in 0 ..< a.height:
for x in 0 ..< a.width:
var rgba = a.getRgbaUnsafe(x, y)
if rgba.a == 0:
result.setRgbaUnsafe(x, y, rgba(0, 0, 0, 0))
else:
result.setRgbaUnsafe(x, y, rgba(255, 255, 255, 255))
2020-12-04 06:11:54 +00:00
var a = newImage(2560, 1440)
2020-12-09 00:10:20 +00:00
timeIt "fill1":
a.fill1(rgba(255, 255, 255, 255))
doAssert a[0, 0] == rgba(255, 255, 255, 255)
keep(a)
timeIt "fill2":
a.fill2(rgba(255, 255, 255, 255))
2020-12-03 20:21:58 +00:00
doAssert a[0, 0] == rgba(255, 255, 255, 255)
2020-12-04 06:11:54 +00:00
keep(a)
2020-12-03 20:21:58 +00:00
timeIt "fill":
2020-12-04 06:11:54 +00:00
a.fill(rgba(255, 255, 255, 255))
2020-12-03 20:21:58 +00:00
doAssert a[0, 0] == rgba(255, 255, 255, 255)
2020-12-04 06:11:54 +00:00
keep(a)
2020-12-03 20:21:58 +00:00
2020-12-09 00:10:20 +00:00
timeIt "fill1_rgba":
a.fill1(rgba(63, 127, 191, 255))
doAssert a[0, 0] == rgba(63, 127, 191, 255)
keep(a)
timeIt "fill2_rgba":
a.fill2(rgba(63, 127, 191, 255))
doAssert a[0, 0] == rgba(63, 127, 191, 255)
keep(a)
timeIt "fill_rgba":
a.fill(rgba(63, 127, 191, 255))
doAssert a[0, 0] == rgba(63, 127, 191, 255)
keep(a)
timeIt "invert1":
a.invert1()
keep(a)
timeIt "invert2":
a.invert2()
2020-12-04 06:11:54 +00:00
keep(a)
2020-12-03 20:21:58 +00:00
timeIt "invert":
2020-12-04 06:11:54 +00:00
a.invert()
keep(a)
2020-12-03 20:32:50 +00:00
2020-12-09 00:10:20 +00:00
timeIt "applyOpacity1":
a = a.applyOpacity1(0.5)
2020-12-04 06:11:54 +00:00
keep(a)
2020-12-03 20:32:50 +00:00
timeIt "applyOpacity":
2020-12-04 06:11:54 +00:00
a.applyOpacity(0.5)
keep(a)
2020-12-03 20:32:50 +00:00
2020-12-09 00:10:20 +00:00
timeIt "sharpOpacity1":
a = a.sharpOpacity1()
2020-12-04 06:11:54 +00:00
keep(a)
2020-12-03 20:32:50 +00:00
timeIt "sharpOpacity":
2020-12-04 06:11:54 +00:00
a.sharpOpacity()
keep(a)