mask minifyBy2

This commit is contained in:
Ryan Oldenburg 2021-02-07 20:33:53 -06:00
parent f627c56d8d
commit 8a933e051e
3 changed files with 37 additions and 0 deletions

View file

@ -63,5 +63,23 @@ proc `[]=`*(mask: Mask, x, y: int, value: uint8) {.inline.} =
if mask.inside(x, y):
mask.setValueUnsafe(x, y, value)
proc minifyBy2*(mask: Mask, power = 1): Mask =
## Scales the mask down by an integer scale.
if power < 0:
raise newException(PixieError, "Cannot minifyBy2 with negative power")
if power == 0:
return mask.copy()
for i in 1 .. power:
result = newMask(mask.width div 2, mask.height div 2)
for y in 0 ..< result.height:
for x in 0 ..< result.width:
let value =
mask.getValueUnsafe(x * 2 + 0, y * 2 + 0).uint32 +
mask.getValueUnsafe(x * 2 + 1, y * 2 + 0) +
mask.getValueUnsafe(x * 2 + 1, y * 2 + 1) +
mask.getValueUnsafe(x * 2 + 0, y * 2 + 1)
result.setValueUnsafe(x, y, (value div 4).uint8)
when defined(release):
{.pop.}

Binary file not shown.

After

Width:  |  Height:  |  Size: 270 B

19
tests/test_masks.nim Normal file
View file

@ -0,0 +1,19 @@
import pixie, pixie/fileformats/png
block:
let
mask = newMask(100, 100)
r = 10.0
x = 10.0
y = 10.0
h = 80.0
w = 80.0
var path: Path
path.moveTo(x + r, y)
path.arcTo(x + w, y, x + w, y + h, r)
path.arcTo(x + w, y + h, x, y + h, r)
path.arcTo(x, y + h, x, y, r)
path.arcTo(x, y, x + w, y, r)
mask.fillPath(path)
writeFile("tests/images/masks/maskMinified.png", mask.minifyBy2().encodePng())