Merge pull request #53 from guzba/master

bugfix + test
This commit is contained in:
treeform 2021-01-19 16:56:58 -08:00 committed by GitHub
commit 8e2b4dc386
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

View file

@ -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.

View file

@ -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))