Merge pull request #313 from guzba/master

faster flips
This commit is contained in:
treeform 2021-10-25 08:19:58 -07:00 committed by GitHub
commit a718fb6b1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 10 deletions

View file

@ -179,22 +179,20 @@ proc flipHorizontal*(image: Image) {.raises: [].} =
let w = image.width div 2
for y in 0 ..< image.height:
for x in 0 ..< w:
let
rgba1 = image.getRgbaUnsafe(x, y)
rgba2 = image.getRgbaUnsafe(image.width - x - 1, y)
image.setRgbaUnsafe(image.width - x - 1, y, rgba1)
image.setRgbaUnsafe(x, y, rgba2)
swap(
image.data[image.dataIndex(x, y)],
image.data[image.dataIndex(image.width - x - 1, y)]
)
proc flipVertical*(image: Image) {.raises: [].} =
## Flips the image around the X axis.
let h = image.height div 2
for y in 0 ..< h:
for x in 0 ..< image.width:
let
rgba1 = image.getRgbaUnsafe(x, y)
rgba2 = image.getRgbaUnsafe(x, image.height - y - 1)
image.setRgbaUnsafe(x, image.height - y - 1, rgba1)
image.setRgbaUnsafe(x, y, rgba2)
swap(
image.data[image.dataIndex(x, y)],
image.data[image.dataIndex(x, image.height - y - 1)]
)
proc subImage*(image: Image, x, y, w, h: int): Image {.raises: [PixieError].} =
## Gets a sub image from this image.

View file

@ -49,6 +49,16 @@ timeIt "magnifyBy2":
reset()
timeIt "flipHorizontal":
image.flipHorizontal()
reset()
timeIt "flipVertical":
image.flipVertical()
reset()
timeIt "invert":
image.invert()