Merge pull request #306 from guzba/master

magnifyBy2 faster
This commit is contained in:
treeform 2021-10-17 20:19:46 -07:00 committed by GitHub
commit e67cfd60a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 6 deletions

View file

@ -328,14 +328,23 @@ proc magnifyBy2*(image: Image, power = 1): Image {.raises: [PixieError].} =
let scale = 2 ^ power let scale = 2 ^ power
result = newImage(image.width * scale, image.height * scale) result = newImage(image.width * scale, image.height * scale)
for y in 0 ..< result.height:
for y in 0 ..< image.height:
# Write one row of pixels duplicated by scale
for x in 0 ..< image.width: for x in 0 ..< image.width:
let let
rgba = image.getRgbaUnsafe(x, y div scale) rgbx = image.getRgbaUnsafe(x, y)
idx = result.dataIndex(x * scale, y) idx = result.dataIndex(x * scale, y * scale)
for i in 0 ..< scale div 2: for i in 0 ..< scale:
result.data[idx + i * 2 + 0] = rgba result.data[idx + i] = rgbx
result.data[idx + i * 2 + 1] = rgba # Copy that row of pixels into (scale - 1) more rows
let rowStart = result.dataIndex(0, y * scale)
for i in 1 ..< scale:
copyMem(
result.data[rowStart + result.width * i].addr,
result.data[rowStart].addr,
result.width * 4
)
proc applyOpacity*(target: Image | Mask, opacity: float32) {.raises: [].} = proc applyOpacity*(target: Image | Mask, opacity: float32) {.raises: [].} =
## Multiplies alpha of the image by opacity. ## Multiplies alpha of the image by opacity.

View file

@ -44,6 +44,17 @@ block:
a.draw(b, translate(vec2(25, 25)) * scale(vec2(0.5, 0.5)), bmNormal) a.draw(b, translate(vec2(25, 25)) * scale(vec2(0.5, 0.5)), bmNormal)
keep(b) keep(b)
block:
let
a = newImage(1000, 1000)
b = newImage(500, 500)
a.fill(rgba(255, 0, 0, 255))
b.fill(rgba(0, 255, 0, 255))
timeIt "draw [scale 2]":
a.draw(b, translate(vec2(25, 25)) * scale(vec2(2, 2)), bmNormal)
keep(b)
block: block:
let let
a = newImage(1000, 1000) a = newImage(1000, 1000)