I'm working on a project where I'm trying to read some data out of a png, and I'm having some issues. I'm adapting a lot of code from others and kind of mixing it together, and one of the issues I have is actually reading data from the png file. Since it's binary, it presents some difficulties. Here is the code I'm trying, but it will go through and read bytes up until it errors saying "attempt to perform arithmatic __mult on nil and number.
local function readByte(stream) return stream.read() end local function readInt(stream, bps) local bytes = {} bps = bps or 4 for i=1,bps do bytes[i] = readByte(stream) end return bytesToNum(bytes) end local function readChar(stream, num) num = num or 1 local bytes = 0 for i = 0, num - 1, 1 do bytes = bytes + (readByte(stream) * (256 ^ i)) end end
So it seems that readByte is giving readChar a nil for one of the bytes. Obviously I'm doing something wrong. BTW this code is adapted from SquidDev's bitmap library. The other difficulty I am having is downloading binary files. This one is a creation of my own, and it works well for downloading binary, but when it downloads a png and I try to open it with Windows Photo Viewer, it says that it's missing or corrupt. Thoughts?
function downloadBinaryFile(url, saveFile) local httpHandle = http.get(url) if not httpHandle then return false end local data = httpHandle.readAll() httpHandle.close() print(#data .. " bytes") local fhandle = fs.open(saveFile, "wb") for i = 1, #data do fhandle.write(string.byte(data, i)) end fhandle.close() return true end downloadBinaryFile(<image url>,"skin.png")
Edited by emanuel12324, 19 July 2018 - 09:32 PM.