On 2026-01-24 19:40, 'Lars Müller' via lua-l wrote:
> Fun fact: PNG is actually a very simple format, especially if you just
> want to write an ARGB8 file.
>
> I have some related code around:
> <
https://github.com/appgurueu/modlib/blob/59ed71df15e4e01e0bb096e7b1d709ea9fdb25cb/minetest/png.lua>
>
> 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.
> That is a couple more hundred lines, but still not that large.
>
> So what I'm saying is, if it tickles your fancy, directly writing PNGs
> from Lua is an option.
>
> Or just bindings to libpng. 🙃
>
> - Lars
>
> p.s. alternatively, there's also the "quite okay image format" (QOI,
> <
https://qoiformat.org/>) that seems to live up to its name and is
> even simpler than PNG.
>
Hello Lars,
Thanks for related sources to PNG and QOI. Yeah I know that PNG is not
complex for simple cases. But even in your use case it requires deflate
codec.
I've discovered there is text format that is almost as simple as
possible. Just ASCII tokens:
P2
3 2 255
128 000 64
092 000 32
So codec for it can be written on-the-fly without reading documentation.
It is supported by stock viewer on my Linux Mint so so far it is best
fit for my mathematical experiments. (Which is basically just
generating and visualizing integer matrices.)
Of course there is no compression, so generated files are not suitable
for storing in collections.
But it amazes me how fast and simple I can do things in Lua
(in comparison with say C or Pascal). For example there to set pixel
I typically have to create record like (x: int, y: int) or
take coordinates as explicit "x" and "y" arguments. In Lua I'm just
passing { 100, 50 } without even bothering to name data and not
thinking about strict types compatibility.
-- Martin