Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions png.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -303,4 +317,4 @@ local function pngImage(path, progCallback, verbose, memSave)
}
end

return pngImage
return pngImage