diff --git a/src/pixie/images.nim b/src/pixie/images.nim index f442db7..72ad974 100644 --- a/src/pixie/images.nim +++ b/src/pixie/images.nim @@ -202,17 +202,17 @@ proc toAlphy*(image: Image) = ## Converts an image to premultiplied alpha from straight. for c in image.data.mitems: c.r = ((c.r.uint32 * c.a.uint32) div 255).uint8 - c.g = ((c.r.uint32 * c.a.uint32) div 255).uint8 - c.b = ((c.r.uint32 * c.a.uint32) div 255).uint8 + c.g = ((c.g.uint32 * c.a.uint32) div 255).uint8 + c.b = ((c.b.uint32 * c.a.uint32) div 255).uint8 proc fromAlphy*(image: Image) = ## Converts an image to from premultiplied alpha to straight. for c in image.data.mitems: if c.a == 0: continue - c.r = ((c.r.int32 * 255) div c.a.int32).uint8 - c.g = ((c.g.int32 * 255) div c.a.int32).uint8 - c.b = ((c.b.int32 * 255) div c.a.int32).uint8 + c.r = ((c.r.uint32 * 255) div c.a.uint32).uint8 + c.g = ((c.g.uint32 * 255) div c.a.uint32).uint8 + c.b = ((c.b.uint32 * 255) div c.a.uint32).uint8 proc getRgbaSmooth*(image: Image, x, y: float32): ColorRGBA {.inline.} = ## Gets a pixel as (x, y) floats. diff --git a/tests/test_images.nim b/tests/test_images.nim index ac0ff8e..a46c712 100644 --- a/tests/test_images.nim +++ b/tests/test_images.nim @@ -62,6 +62,18 @@ block: image.fill(rgba(255, 0, 0, 255)) doAssert image[0, 0] == rgba(255, 0, 0, 255) +block: + var image = newImage(10, 10) + image.fill(rgba(255, 0, 0, 128)) + image.toAlphy() + doAssert image[9, 9] == rgba(128, 0, 0, 128) + +block: + var image = newImage(10, 10) + image.fill(rgba(128, 0, 0, 128)) + image.fromAlphy() + doAssert image[9, 9] == rgba(255, 0, 0, 128) + # block: # var a = newImage(100, 100) # a.fill(rgba(255, 0, 0, 255))