This commit is contained in:
Ryan Oldenburg 2020-11-21 20:19:14 -06:00
parent 129458ea95
commit ae2cc68b77

View file

@ -217,35 +217,34 @@ proc decodeImageData(
inc bytePos inc bytePos
bitPos = 0 bitPos = 0
of 2: of 2:
if transparency.len == 6: var special: ColorRGBA
# Need to apply transparency check, slower. if transparency.len == 6: # Need to apply transparency check, slower.
let special = [transparency[1], transparency[3], transparency[5], 255] special.r = transparency[1]
special.g = transparency[3]
special.b = transparency[5]
special.a = 255
# While we can read an extra byte safely, do so. Much faster. # While we can read an extra byte safely, do so. Much faster.
for i in 0 ..< header.height * header.width - 1: for i in 0 ..< header.height * header.width - 1:
var rgba = cast[ptr array[4, uint8]](unfiltered[i * 3].unsafeAddr)[] var rgba = cast[ptr ColorRGBA](unfiltered[i * 3].unsafeAddr)[]
rgba[3] = 255 rgba.a = 255
if rgba == special: if rgba == special:
rgba[3] = 0 rgba.a = 0
result[i] = cast[ColorRGBA](rgba) result[i] = rgba
let
lastOffset = header.height * header.width - 1
rgb = cast[ptr array[3, uint8]](unfiltered[lastOffset * 3].unsafeAddr)[]
var rgba = [rgb[0], rgb[1], rgb[2], 255]
if rgba == special:
rgba[3] = 0
result[header.height * header.width - 1] = cast[ColorRGBA](rgba)
else: else:
# While we can read an extra byte safely, do so. Much faster. # While we can read an extra byte safely, do so. Much faster.
for i in 0 ..< header.height * header.width - 1: for i in 0 ..< header.height * header.width - 1:
var rgba = cast[ptr array[4, uint8]](unfiltered[i * 3].unsafeAddr)[] var rgba = cast[ptr ColorRGBA](unfiltered[i * 3].unsafeAddr)[]
rgba[3] = 255 rgba.a = 255
result[i] = cast[ColorRGBA](rgba) result[i] = rgba
let
lastOffset = header.height * header.width - 1 let
rgb = cast[ptr array[3, uint8]](unfiltered[lastOffset * 3].unsafeAddr)[] lastOffset = header.height * header.width - 1
result[header.height * header.width - 1] = ColorRGBA( rgb = cast[ptr array[3, uint8]](unfiltered[lastOffset * 3].unsafeAddr)[]
r: rgb[0], g: rgb[1], b: rgb[2], a: 255 var rgba = ColorRGBA(r: rgb[0], g: rgb[1], b: rgb[2], a: 255)
) if rgba == special:
rgba.a = 0
result[header.height * header.width - 1] = cast[ColorRGBA](rgba)
of 3: of 3:
var bytePos, bitPos: int var bytePos, bitPos: int
for y in 0 ..< header.height: for y in 0 ..< header.height:
@ -288,24 +287,17 @@ proc decodeImageData(
inc bytePos inc bytePos
bitPos = 0 bitPos = 0
of 4: of 4:
var bytePos: int for i in 0 ..< header.height * header.width:
for y in 0 ..< header.height: let bytePos = i * 2
for x in 0 ..< header.width: result[i] = ColorRGBA(
result[x + y * header.width] = ColorRGBA( r: unfiltered[bytePos],
r: unfiltered[bytePos], g: unfiltered[bytePos],
g: unfiltered[bytePos], b: unfiltered[bytePos],
b: unfiltered[bytePos], a: unfiltered[bytePos + 1]
a: unfiltered[bytePos + 1] )
)
bytePos += 2
of 6: of 6:
var bytePos: int for i in 0 ..< header.height * header.width:
for y in 0 ..< header.height: result[i] = cast[ColorRGBA](unfiltered.readUint32(i * 4))
for x in 0 ..< header.width:
result[x + y * header.width] = cast[ColorRGBA](
unfiltered.readUint32(bytePos)
)
bytePos += 4
else: else:
discard # Not possible, parseHeader validates discard # Not possible, parseHeader validates