Re: [ANN] File-to-image

Olive Simmons <[email protected]>
Newsgroups gmane.comp.lang.lua.general
Message-ID <[email protected]>
 > > 
https://github.com/appgurueu/modlib/blob/59ed71df15e4e01e0bb096e7b1d709ea9fdb25cb/minetest/png.lua
 > > So what I'm saying is, if it tickles your fancy, directly writing 
PNGs from Lua is an option.
 > >
 > > This uses some Luanti (formerly Minetest) APIs that wrap zlib to 
do the (de)compression.
 > > It's pretty short, about ~100 loc for the encoder. There's also a 
pretty much full-fledged decoder there.
 >
 > Yeah I know that PNG is not complex for simple cases. But even in 
your use case it requires deflate codec.

Writing usable deflate from within Lua is pretty easy, compression can 
be skipped and you can just write uncompressed chunks. The RFC is 
pretty confusing trying to work on a bit-stream but still have 
endianness, here's an extract from a script of mine you can maybe adapt:

local spack, char, concat = string.pack, string.char, table.concat
local function writedeflatechunk(file, s, final)
  file:write(spack("<BI2I2", final and 0x01 or 0x00, #s, (~#s)&0xffff) 
.. s)
end
-- works on 0-based arrays
local function writedeflate(file, data)
  for i = 0, #data, 1024-5 do
     if data[i+1023-5] then
        local cmap = {} for j=0,1023-5 do cmap[j+1] = char(data[i+j]) 
end
        writedefchunk(file, concat(cdata))
     else
        local cmap = {} for j=0,#map-i do cmap[j+1] = char(data[i+j]) 
end
        writedefchunk(file, concat(cdata), true)
     end
  end
end

(ps. if there's a better way to turn a 0-based array into a byte string 
I'd love to know, surprisingly this isn't a huge performance issue)

 > I've discovered there is text format that is almost as simple as 
possible.

PNM/PBM/PGM files!
It's a super handy format for chucking out graphs and image work, if 
you install netpbm from your package manager you'll get a whole list of 
{pbm,pgm,pam}to{png,gif,tiff,svg,tga,...} and vice-versa commands.
Unfortunately it doesn't actually have wide support in most image 
progams, iirc gimp works with it, and about half of Linux image viewers 
seem to. A bit silly given it's so simple.



-- 
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/6NGN9T.446JEQJDF7393%40gmail.com.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.