much faster toStraightAlpha
This commit is contained in:
parent
104cfb03d6
commit
1e6070c150
1 changed files with 14 additions and 7 deletions
|
@ -81,16 +81,23 @@ proc fillUnsafe*(
|
||||||
for j in i ..< start + len:
|
for j in i ..< start + len:
|
||||||
data[j] = rgbx
|
data[j] = rgbx
|
||||||
|
|
||||||
|
const straightAlphaTable = block:
|
||||||
|
var table: array[256, array[256, uint8]]
|
||||||
|
for a in 0 ..< 256:
|
||||||
|
let multiplier = if a > 0: (255 / a.float32) else: 0
|
||||||
|
for c in 0 ..< 256:
|
||||||
|
table[a][c] = min(round((c.float32 * multiplier)), 255).uint8
|
||||||
|
table
|
||||||
|
|
||||||
proc toStraightAlpha*(data: var seq[ColorRGBA | ColorRGBX]) {.raises: [].} =
|
proc toStraightAlpha*(data: var seq[ColorRGBA | ColorRGBX]) {.raises: [].} =
|
||||||
## Converts an image from premultiplied alpha to straight alpha.
|
## Converts an image from premultiplied alpha to straight alpha.
|
||||||
## This is expensive for large images.
|
## This is expensive for large images.
|
||||||
for c in data.mitems:
|
for i in 0 ..< data.len:
|
||||||
if c.a == 0 or c.a == 255:
|
var c = data[i]
|
||||||
continue
|
c.r = straightAlphaTable[c.a][c.r]
|
||||||
let multiplier = ((255 / c.a.float32) * 255).uint32
|
c.g = straightAlphaTable[c.a][c.g]
|
||||||
c.r = ((c.r.uint32 * multiplier) div 255).uint8
|
c.b = straightAlphaTable[c.a][c.b]
|
||||||
c.g = ((c.g.uint32 * multiplier) div 255).uint8
|
data[i] = c
|
||||||
c.b = ((c.b.uint32 * multiplier) div 255).uint8
|
|
||||||
|
|
||||||
proc toPremultipliedAlpha*(data: var seq[ColorRGBA | ColorRGBX]) {.raises: [].} =
|
proc toPremultipliedAlpha*(data: var seq[ColorRGBA | ColorRGBX]) {.raises: [].} =
|
||||||
## Converts an image to premultiplied alpha from straight alpha.
|
## Converts an image to premultiplied alpha from straight alpha.
|
||||||
|
|
Loading…
Reference in a new issue