From 8d32114d25e8bcb7bf4975ce0fa7a07343835bd1 Mon Sep 17 00:00:00 2001 From: Brett Gordon Date: Tue, 15 Sep 2020 09:50:53 -0400 Subject: [PATCH] support returning color index and transparency of paletted images --- png.lua | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/png.lua b/png.lua index 88fc38d..1b293d4 100644 --- a/png.lua +++ b/png.lua @@ -96,12 +96,23 @@ local function getDataPLTE(stream, length) data.colors[i] = { R = readByte(stream), G = readByte(stream), - B = readByte(stream) + B = readByte(stream), + A = 255 } end return data end +local function getDatatRNS(stream, length, palette) + local data = {} + data["numColors"] = length + data["colors"] = {} + for i = 1, length do + palette.colors[i].A = readByte(stream) + end + return data +end + local function extractChunkData(stream) local chunkData = {} local length @@ -116,7 +127,9 @@ local function extractChunkData(stream) elseif (type == "IDAT") then chunkData[type] = getDataIDAT(stream, length, chunkData[type]) elseif (type == "PLTE") then - chunkData[type] = getDataPLTE(stream, length) + chunkData[type] = getDataPLTE(stream, length) + elseif (type == "tRNS") then + chunkData[type] = getDatatRNS(stream, length, chunkData["PLTE"]) else readChar(stream, length) end @@ -128,7 +141,7 @@ end local function makePixel(stream, depth, colorType, palette) local bps = math.floor(depth/8) --bits per sample - local pixelData = { R = 0, G = 0, B = 0, A = 0 } + local pixelData = { R = 0, G = 0, B = 0, A = 0, I = 0 } local grey local index local color @@ -150,7 +163,8 @@ local function makePixel(stream, depth, colorType, palette) pixelData.R = color.R pixelData.G = color.G pixelData.B = color.B - pixelData.A = 255 + pixelData.A = color.A + pixelData.I = index elseif colorType == 4 then grey = readInt(stream, bps) pixelData.R = grey @@ -303,4 +317,4 @@ local function pngImage(path, progCallback, verbose, memSave) } end -return pngImage \ No newline at end of file +return pngImage