magnifyBy2 10% faster

This commit is contained in:
Ryan Oldenburg 2021-10-11 19:17:31 -05:00
parent 68b2c52b85
commit 89d1608a32

View file

@ -309,15 +309,14 @@ proc minifyBy2*(image: Image, power = 1): Image {.raises: [PixieError].} =
b = src.getRgbaUnsafe(x * 2 + 1, y * 2 + 0) b = src.getRgbaUnsafe(x * 2 + 1, y * 2 + 0)
c = src.getRgbaUnsafe(x * 2 + 1, y * 2 + 1) c = src.getRgbaUnsafe(x * 2 + 1, y * 2 + 1)
d = src.getRgbaUnsafe(x * 2 + 0, y * 2 + 1) d = src.getRgbaUnsafe(x * 2 + 0, y * 2 + 1)
rgba = rgbx(
let color = rgbx(
((a.r.uint32 + b.r + c.r + d.r) div 4).uint8, ((a.r.uint32 + b.r + c.r + d.r) div 4).uint8,
((a.g.uint32 + b.g + c.g + d.g) div 4).uint8, ((a.g.uint32 + b.g + c.g + d.g) div 4).uint8,
((a.b.uint32 + b.b + c.b + d.b) div 4).uint8, ((a.b.uint32 + b.b + c.b + d.b) div 4).uint8,
((a.a.uint32 + b.a + c.a + d.a) div 4).uint8 ((a.a.uint32 + b.a + c.a + d.a) div 4).uint8
) )
result.setRgbaUnsafe(x, y, color) result.setRgbaUnsafe(x, y, rgba)
# Set src as this result for if we do another power # Set src as this result for if we do another power
src = result src = result
@ -333,10 +332,10 @@ proc magnifyBy2*(image: Image, power = 1): Image {.raises: [PixieError].} =
for x in 0 ..< image.width: for x in 0 ..< image.width:
let let
rgba = image.getRgbaUnsafe(x, y div scale) rgba = image.getRgbaUnsafe(x, y div scale)
scaledX = x * scale idx = result.dataIndex(x * scale, y)
idx = result.dataIndex(scaledX, y) for i in 0 ..< scale div 2:
for i in 0 ..< scale: result.data[idx + i * 2 + 0] = rgba
result.data[idx + i] = rgba result.data[idx + i * 2 + 1] = rgba
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.