💾 Archived View for dioskouroi.xyz › thread › 24996791 captured on 2020-11-07 at 00:50:58. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

Echo/printf to write images in 5 LoC with zero libraries or headersnetpbm

Author: stargrave

Score: 126

Comments: 23

Date: 2020-11-05 09:27:01

Web Link

________________________________________________________________________________

rwmj wrote at 2020-11-05 10:31:24:

Been doing this forever. From C it's the easiest way to output images, using popen and piping to netpbm or imagemagick. Without error handling it goes like this:

fp = popen ("pnmtojpeg > output.jpg", "w");
  fprintf (fp, "P3 %d %d 255", width, height);
  /* print pixels as R G B triples */
  pclose (fp);

RBerenguel wrote at 2020-11-05 13:06:21:

Likewise, I used it back when I rendered fractals (code in C, too) for some papers. PPM/PNM is a lovely (if bulky) format since it’s human debuggable as well.

lelandbatey wrote at 2020-11-05 20:36:45:

PPM is especially useful for fractals because, as an optionally text-based format, the max brightness of any pixel is arbitrary. This means you can have unlimited color depth, something that's really useful when you want to do things like brighten up _really_ dim areas of an image, areas that would be aliased to 0 if represented with only 24 bit color (like in PNG). This was really useful for a toy buddhabrot renderer I wrote a while back:

https://github.com/lelandbatey/rust_buddhabrot

roywiggins wrote at 2020-11-05 15:20:37:

PPM always reminds me of the 2008 winner of the Underhanded C Competition.

http://www.underhanded-c.org/_page_id_17.html

kaszanka wrote at 2020-11-05 16:32:44:

I hope there's another edition of this contest someday, I really loved seeing the subtle bugs the contestants intentionally came up with.

saagarjha wrote at 2020-11-05 11:20:02:

I love love love NetPBM. It’s really easy to drop into your code for debugging or simple image exporting and it is so easy to work with. The amount of information you can get out of a good image is just so high that getting NetPBM output of your complicated thing can be so worth it, even if you are writing it from scratch each time. Plus you can kind of “read” it without an image viewer if you use P1.

The other one I really like is Graphviz :)

chubot wrote at 2020-11-05 17:33:28:

Very related from 5 days ago:

_How to create minimal music with code in any programming language_

https://news.ycombinator.com/item?id=24940624

Basically pipe values into mplayer, etc.

These should go in my #shell-the-good-parts series, which is very backlogged...

https://www.oilshell.org/blog/tags.html?tag=shell-the-good-p...

an_ko wrote at 2020-11-05 10:32:55:

I've used GIMP as a game level editor this way. Draw your level, save it as PPM, and parse it into tiles in your game. The file is dead easy to parse. For a "real game" you might want to use Tiled or another actual map editor, but for little fun things this is light and Unixy and reliable.

oefrha wrote at 2020-11-05 10:22:41:

you can easily slot into Netpbm’s wonderfully Unix-y set of tools by reading/writing PPM on stdin/stdout

No experience with the netpbm suite of tools, but ImageMagick (which TFA mentioned addition to netpbm) supports reading/writing any supported format on stdin/stdout, the skills of which I dare say are a bit more transferrable. A trivial example:

$ <my_image.ppm convert ppm:- jpeg:- | convert jpeg:- png:- >my_image.png && identify my_image.png
  my_image.png PNG 250x250 250x250+0+0 8-bit sRGB 15836B 0.000u 0:00.000

ktpsns wrote at 2020-11-05 10:33:37:

That's also my feeling -- the CLI tooling with imagemagick or various image libraries in various programming languages are quite good. For instance, with ipython, numpy and OpenCV I can edit images in a REPL fashion without even thinking about image file formats.

Anyway, if you like PPM, you may also like

https://tools.suckless.org/farbfeld/

. The guys from suckless picked up the unix philosophy for simple file formats and pipes, cf. the examples

https://tools.suckless.org/farbfeld/examples/

ratww wrote at 2020-11-05 10:45:30:

This reminds me of Wavefront OBJ files, which is the format I use the mostly for a lot of my 3D stuff. It's limited but super easy to parse and generate, and macOS Finder can preview it natively.

https://en.wikipedia.org/wiki/Wavefront_.obj_file

userbinator wrote at 2020-11-05 20:33:01:

When I first heard of these text-based raster image formats, I was surprised at both their inefficiency and seemingly widespread use (outside of PCs), especially given their age.

My go-to for uncompressed bitmaps is BMP, also trivial to read and write, and is easy to manipulate the image in memory with indexing operations.

jedimastert wrote at 2020-11-05 13:29:11:

Chris Wellons uses the same format extensively

https://nullprogram.com/blog/2017/11/03/

https://nullprogram.com/blog/2020/06/29/

spiritplumber wrote at 2020-11-05 17:06:13:

PPM is easy to work with when you're close to the metal. I remember one time I bet a MUSH admin that I could get a webcam working on the MUSH command line, and basically implemented an ascii cam over serial using a Parallax Propeller.

Won the bet and promptly got kicked from the MUSH. Fun times.

codazoda wrote at 2020-11-05 20:47:59:

Finding this format might be timely for me. I broke out my TRS-80 CoCo the other day and I've been trying to do some graphics work inside its constraints. Maybe this format, or a variation of it, could work for that.

jnwatson wrote at 2020-11-05 13:32:08:

There is something to be said for simple file formats.

I recall making pbms for my Data Structures class in the mid-90's. It is definitely a good target for budding programmers.

memset wrote at 2020-11-05 12:07:46:

This is interesting, netpbm is new to me!

Is there a similar format for video, which can be generated on the fly without transcoding or invoking ffmpeg?

tylerlarson wrote at 2020-11-05 13:29:33:

Any format that is uncompressed like this is huge. 8bits per channel (RGB/A) times width times height. Many image formats have animated versions of them that allow basic arrays of data.

I've created renderers that do this. You can render out data as fast as your hard drive can write the data but yes these files are often ginormous. RGBA at 1000x1000 at 30FPS is about 7.2Gb per min.

There are raw file formats but because the files are so large they are normally thought of as streaming formats that you would pipe to another process like ffmpeg or gstreamer. If you actually want something saved to disk that other things can read your options are normally MJPEG, APNG, WebM or GIF. Some of the older video formats like AVI or MPEG might also work.

Personally I think animated PNG files are the cleanest simplest format internally, there are arrays of RGBA pixels that are gzipped plus the header data. Because the pixel data is compressed the files are smaller but still huge in comparison to a normal video file.

The answer to your question is also pretty complicated because most of these formats have many different formats that they internally allow, for instance MPEG, AVI and other video formats can be saved with pixel data in different formats. The problem is the specs for these things are often not fully supported by all of the players. To get useable files you will need to be specific about what you are trying to target. The formats that I normally target need to work in a web browser but if you just need it to work on your computer VLC can play almost anything.

tleb_ wrote at 2020-11-05 12:33:26:

You could just output raw frames into a file and read it using ffplay or vlc. The easiest pixel format is probably RGB 24 bits.

  ffplay -f rawvideo -pixel_format rgb24 -video_size 42x42 -framerate 42 foo.raw
    vlc --demux rawvideo --rawvid-fps 42 --rawvid-width 42 --rawvid-height 42 --rawvid-chroma=RV24 --rawvid-aspect-ratio 1:1 foo.raw

I experienced noticeable latency using ffplay so not great for real-time (multiple 100ms, upping the framerate to something like 100-200 reduces the latency but it's stupid). I have much better latency sending the frame data to a browser using websockets and displaying it in a canvas...

Sources:

https://ffmpeg.org/ffmpeg-formats.html#rawvideo

https://forum.videolan.org/viewtopic.php?f=13&t=57938&p=1936...

https://wiki.videolan.org/Chroma/

jedimastert wrote at 2020-11-05 13:30:12:

https://nullprogram.com/blog/2017/11/03/

justusw wrote at 2020-11-05 12:20:21:

While not allowing you to interface arbitrary programs to edit video streams, ffmpeg filter graphs come pretty close to the idea of Unix pipes. You can read more about it here

https://ffmpeg.org/ffmpeg-filters.html#Description

2038AD wrote at 2020-11-05 17:58:34:

In a similar vein there is Blind

https://tools.suckless.org/blind/

saagarjha wrote at 2020-11-05 12:11:20:

I suspect it would be absurdly large.