4 ms·
Yes, IIRC it's about an 18 byte binary header and then BGRABGRABGRA... or BGRBGRBGR... bytes for uncompressed (there's also an RLE mode, but I never use it). T
by a_e_k 5y ago
Yes, IIRC it's about an 18 byte binary header and then BGRABGRABGRA... or BGRBGRBGR... bytes for uncompressed (there's also an RLE mode, but I never use it). The only real gotchas are the channel ordering and that the image might be flipped top-to-bottom if you're not careful.
These days though, I use tend to use the even simpler PPM format. It uses normal RGB byte ordering from top to bottom, and it's simple enough that I can code a basic writer for it from memory:
fprintf(stream, "P6 %d %d 255\n", width, height);
fwrite(image, 1, height * width * 3, stream);
(Often, I won't even bother opening a file to write to. I'll just emit the image to stdout and then pipe it to Image Magick for display or to convert to a compressed image format for storage.)
- madmoose 5y agoPPM P6 is also what I use all the time - I usually have to convert from paletted format to RGB first. A PPM paletted format would save me a lot of space. The built-in support for PPM in Preview on Mac OS X makes it extremely useful.
- a_e_k 5y agoFor my use, alpha is what I tend to miss in PPM. There's the PAM extension [0] but it's not nearly as widely supported as basic PPM. Usually at that point, I reach for PNG via stb_image_write [1]. [0] https://en.wikipedia.org/wiki/Netpbm#PAM_graphics_format https://en.wikipedia.org/wiki/Netpbm#PAM_graphics_format [1] https://github.com/nothings/stb/blob/master/stb_image_write.h https://github.com/nothings/stb/blob/master/stb_image_write....