Hacker News new | ask | show | jobs
by just_for_you 1964 days ago
I would agree on BMP being slightly insane, since there's a number of things you have to magically know about it:

1) If using 8 bits per pixel or less, there's always a color palette between the header and bitmap. If more than 8bpp, then there's none.

2) If more or equal to 8BPP, you must know that you must always use Bitfields to specify which bytes correspond to which RGB(A) channels, and that there cannot be any overlap between the channel masks.

3) There's no intuitive way to tell which version of the Bitmap file you have (there's like 5 major versions).

4) You can specify, inline, the Chromaticity and Gamma (basically an inline color profile) somewhere in the headers.

5) You can also append an ICC profile directly into the file, OR, store a Windows-style filesystem path to the profile (in which case, you must know to ignore the in-header color profile, if you have one).

6) You must know that RLE compression only works if the RGB channels cleanly map to one-byte-each (no packing).

7) RLE compression doesn't work if you use a negative height (to indicate the image is encoded from top-to-bottom, rather than the standard bottom-to-top).

8) The (un)official documentation Microsoft hosts states that BMP files can also store PNG or JPEG bitstream data, but yet no application that I've seen has ever explicitly supported this.

9) You have to know that Windows only supports 2 or so variations of packed 16-bit BMP files (565, etc).

For the most part you can just assume you're dealing with a v5 BMP file, read or write the most pertinent parts like the dimensions (skipping dumb stuff like ICC profiles, pixels-per-meter and whatnot), and just have your way with the raw data. But there is still some dumb stuff in there that shouldn't be there, considering it should be a straight-forward format. What also baffles me is that even with the bloat Microsoft added to the format, BMP isn't even extensible. So it's kinda the worst of both: It's slightly complicated to the point of not being straight-foward to work with, and yet, you can't add extra info/metadata to it.

On that note, Targa is a dreamy image format. It's got a simple 18-byte or so header, and then the raw data. And, optionally, for Targa v2 you can append a few bytes in a footer that indicate an offset for an optional standard-targa-metadata area, as well as an option metadata area for your data you wish to add to the file. Followed by a magic string, that explicitly indicate you're using v2 of the Targa spec.

1 comments

I'd forgotten about negative heights! Unexpectedly negative values in BMP files were another surprise that required extra code.