This commit is contained in:
Ryan Oldenburg 2021-01-28 20:55:47 -06:00
parent cb14a09367
commit bd18a9868a

View file

@ -142,30 +142,25 @@ proc subImage*(image: Image, x, y, w, h: int): Image =
w * 4 w * 4
) )
proc minifyBy2*(image: Image): Image = proc minifyBy2*(image: Image, power = 1): Image =
## Scales the image down by an integer scale.
result = newImage(image.width div 2, image.height div 2)
for y in 0 ..< result.height:
for x in 0 ..< result.width:
var color =
image.getRgbaUnsafe(x * 2 + 0, y * 2 + 0).color / 4.0 +
image.getRgbaUnsafe(x * 2 + 1, y * 2 + 0).color / 4.0 +
image.getRgbaUnsafe(x * 2 + 1, y * 2 + 1).color / 4.0 +
image.getRgbaUnsafe(x * 2 + 0, y * 2 + 1).color / 4.0
result.setRgbaUnsafe(x, y, color.rgba)
proc minifyBy2*(image: Image, power: int): Image =
## Scales the image down by an integer scale. ## Scales the image down by an integer scale.
if power < 0: if power < 0:
raise newException(PixieError, "Cannot minifyBy2 with negative power") raise newException(PixieError, "Cannot minifyBy2 with negative power")
if power == 0: if power == 0:
result = image.copy() return image.copy()
else:
for i in 1 .. power:
result = result.minifyBy2()
proc magnifyBy2*(image: Image, power: int): Image = for i in 1 .. power:
result = newImage(image.width div 2, image.height div 2)
for y in 0 ..< result.height:
for x in 0 ..< result.width:
var color =
image.getRgbaUnsafe(x * 2 + 0, y * 2 + 0).color / 4.0 +
image.getRgbaUnsafe(x * 2 + 1, y * 2 + 0).color / 4.0 +
image.getRgbaUnsafe(x * 2 + 1, y * 2 + 1).color / 4.0 +
image.getRgbaUnsafe(x * 2 + 0, y * 2 + 1).color / 4.0
result.setRgbaUnsafe(x, y, color.rgba)
proc magnifyBy2*(image: Image, power = 1): Image =
## Scales image image up by 2 ^ power. ## Scales image image up by 2 ^ power.
if power < 0: if power < 0:
raise newException(PixieError, "Cannot magnifyBy2 with negative power") raise newException(PixieError, "Cannot magnifyBy2 with negative power")
@ -177,9 +172,6 @@ proc magnifyBy2*(image: Image, power: int): Image =
var rgba = image.getRgbaUnsafe(x div scale, y div scale) var rgba = image.getRgbaUnsafe(x div scale, y div scale)
result.setRgbaUnsafe(x, y, rgba) result.setRgbaUnsafe(x, y, rgba)
proc magnifyBy2*(image: Image): Image =
image.magnifyBy2(1)
proc toPremultipliedAlpha*(image: Image) = proc toPremultipliedAlpha*(image: Image) =
## Converts an image to premultiplied alpha from straight alpha. ## Converts an image to premultiplied alpha from straight alpha.
var i: int var i: int