From ff45edf131816b63e6f76b17f4bc758d66236a6c Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Wed, 15 Jun 2022 12:22:01 -0500 Subject: [PATCH] faster --- src/pixie/fileformats/png.nim | 40 +++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/pixie/fileformats/png.nim b/src/pixie/fileformats/png.nim index 78ad7d5..ae6acda 100644 --- a/src/pixie/fileformats/png.nim +++ b/src/pixie/fileformats/png.nim @@ -114,39 +114,51 @@ proc unfilter( rowBytes ) of 1: # Sub + let + uncompressedStartIdx = uncompressedIdx(1, y) + unfilteredStartIx = unfiteredIdx(0, y) for x in 0 ..< rowBytes: - var value = uncompressed.readUint8(uncompressedIdx(x + 1, y)) + var value = uncompressed.readUint8(uncompressedStartIdx + x) if x - bpp >= 0: - value += result.readUint8(unfiteredIdx(x - bpp, y)) - result[unfiteredIdx(x, y)] = value.char + value += result.readUint8(unfilteredStartIx + x - bpp) + result[unfilteredStartIx + x] = value.char of 2: # Up + let + uncompressedStartIdx = uncompressedIdx(1, y) + unfilteredStartIx = unfiteredIdx(0, y) for x in 0 ..< rowBytes: - var value = uncompressed.readUint8(uncompressedIdx(x + 1, y)) + var value = uncompressed.readUint8(uncompressedStartIdx + x) if y - 1 >= 0: value += result.readUint8(unfiteredIdx(x, y - 1)) - result[unfiteredIdx(x, y)] = value.char + result[unfilteredStartIx + x] = value.char of 3: # Average + let + uncompressedStartIdx = uncompressedIdx(1, y) + unfilteredStartIx = unfiteredIdx(0, y) for x in 0 ..< rowBytes: var - value = uncompressed.readUint8(uncompressedIdx(x + 1, y)) + value = uncompressed.readUint8(uncompressedStartIdx + x) left, up: int if x - bpp >= 0: - left = result[unfiteredIdx(x - bpp, y)].int + left = result[unfilteredStartIx + x - bpp].int if y - 1 >= 0: - up = result[unfiteredIdx(x, y - 1)].int + up = result[unfilteredStartIx + x - rowBytes].int value += ((left + up) div 2).uint8 - result[unfiteredIdx(x, y)] = value.char + result[unfilteredStartIx + x] = value.char of 4: # Paeth + let + uncompressedStartIdx = uncompressedIdx(1, y) + unfilteredStartIx = unfiteredIdx(0, y) for x in 0 ..< rowBytes: var - value = uncompressed.readUint8(uncompressedIdx(x + 1, y)) + value = uncompressed.readUint8(uncompressedStartIdx + x) left, up, upLeft: int if x - bpp >= 0: - left = result[unfiteredIdx(x - bpp, y)].int + left = result[unfilteredStartIx + x - bpp].int if y - 1 >= 0: - up = result[unfiteredIdx(x, y - 1)].int + up = result[unfilteredStartIx + x - rowBytes].int if x - bpp >= 0 and y - 1 >= 0: - upLeft = result[unfiteredIdx(x - bpp, y - 1)].int + upLeft = result[unfilteredStartIx + x - rowBytes - bpp].int template paethPredictor(a, b, c: int): int = let p = a + b - c @@ -160,7 +172,7 @@ proc unfilter( else: c value += paethPredictor(up, left, upLeft).uint8 - result[unfiteredIdx(x, y)] = value.char + result[unfilteredStartIx + x] = value.char else: discard # Not possible, parseHeader validates