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

View file

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