Fix flip.
This commit is contained in:
parent
c6eb92d923
commit
ed5679787d
5 changed files with 30 additions and 6 deletions
|
@ -126,17 +126,25 @@ proc magnifyBy2*(image: Image): Image =
|
|||
|
||||
proc flipHorizontal*(image: Image) =
|
||||
## Flips the image around the Y axis.
|
||||
let w = image.width div 2
|
||||
for y in 0 ..< image.height:
|
||||
for x in 0 ..< image.width:
|
||||
let rgba = image.getRgbaUnsafe(x, y)
|
||||
image.setRgbaUnsafe(image.width - x - 1, y, rgba)
|
||||
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)
|
||||
|
||||
proc flipVertical*(image: Image) =
|
||||
## Flips the image around the X axis.
|
||||
for y in 0 ..< image.height:
|
||||
let h = image.height div 2
|
||||
for y in 0 ..< h:
|
||||
for x in 0 ..< image.width:
|
||||
let rgba = image.getRgbaUnsafe(x, y)
|
||||
image.setRgbaUnsafe(x, image.height - y - 1, rgba)
|
||||
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)
|
||||
|
||||
func lerp(a, b: Color, v: float32): Color {.inline.} =
|
||||
result.r = lerp(a.r, b.r, v)
|
||||
|
|
BIN
tests/images/flipped1.png
Normal file
BIN
tests/images/flipped1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 356 B |
BIN
tests/images/flipped2.png
Normal file
BIN
tests/images/flipped2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 351 B |
BIN
tests/images/flipped3.png
Normal file
BIN
tests/images/flipped3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 343 B |
16
tests/test_flips.nim
Normal file
16
tests/test_flips.nim
Normal file
|
@ -0,0 +1,16 @@
|
|||
import pixie, chroma, vmath
|
||||
|
||||
block:
|
||||
|
||||
var a = newImage(101, 101)
|
||||
a.fill(rgba(255, 0, 0, 255))
|
||||
var b = newImage(50, 50)
|
||||
b.fill(rgba(0, 255, 0, 255))
|
||||
|
||||
a.draw(b, vec2(0, 0))
|
||||
|
||||
a.writeFile("tests/images/flipped1.png")
|
||||
a.flipVertical()
|
||||
a.writeFile("tests/images/flipped2.png")
|
||||
a.flipHorizontal()
|
||||
a.writeFile("tests/images/flipped3.png")
|
Loading…
Reference in a new issue