bugfix for minifyBy2 power > 1
This commit is contained in:
parent
46fe96569e
commit
c5ce1a8029
3 changed files with 15 additions and 5 deletions
|
@ -176,15 +176,16 @@ proc minifyBy2*(image: Image, power = 1): Image =
|
|||
if power == 0:
|
||||
return image.copy()
|
||||
|
||||
var src = image
|
||||
for i in 1 .. power:
|
||||
result = newImage(image.width div 2, image.height div 2)
|
||||
result = newImage(src.width div 2, src.height div 2)
|
||||
for y in 0 ..< result.height:
|
||||
for x in 0 ..< result.width:
|
||||
let
|
||||
a = image.getRgbaUnsafe(x * 2 + 0, y * 2 + 0)
|
||||
b = image.getRgbaUnsafe(x * 2 + 1, y * 2 + 0)
|
||||
c = image.getRgbaUnsafe(x * 2 + 1, y * 2 + 1)
|
||||
d = image.getRgbaUnsafe(x * 2 + 0, y * 2 + 1)
|
||||
a = src.getRgbaUnsafe(x * 2 + 0, y * 2 + 0)
|
||||
b = src.getRgbaUnsafe(x * 2 + 1, y * 2 + 0)
|
||||
c = src.getRgbaUnsafe(x * 2 + 1, y * 2 + 1)
|
||||
d = src.getRgbaUnsafe(x * 2 + 0, y * 2 + 1)
|
||||
|
||||
let color = rgba(
|
||||
((a.r.uint32 + b.r + c.r + d.r) div 4).uint8,
|
||||
|
@ -195,6 +196,9 @@ proc minifyBy2*(image: Image, power = 1): Image =
|
|||
|
||||
result.setRgbaUnsafe(x, y, color)
|
||||
|
||||
# Set src as this result for if we do another power
|
||||
src = result
|
||||
|
||||
proc magnifyBy2*(image: Image, power = 1): Image =
|
||||
## Scales image image up by 2 ^ power.
|
||||
if power < 0:
|
||||
|
|
BIN
tests/images/minifiedBy4.png
Normal file
BIN
tests/images/minifiedBy4.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 161 B |
|
@ -85,3 +85,9 @@ block:
|
|||
a = readImage("tests/images/flipped1.png")
|
||||
b = a.minifyBy2()
|
||||
b.writeFile("tests/images/minifiedBy2.png")
|
||||
|
||||
block:
|
||||
let
|
||||
a = readImage("tests/images/flipped1.png")
|
||||
b = a.minifyBy2(2)
|
||||
b.writeFile("tests/images/minifiedBy4.png")
|
||||
|
|
Loading…
Reference in a new issue