Hi everyone,
Does anybody know a good C sample that loads bitmaps and handles all the cases: rle, b/w bitmaps, so on?
Code should be cross-platform.
Thanks.
Hi everyone,
Does anybody know a good C sample that loads bitmaps and handles all the cases: rle, b/w bitmaps, so on?
Code should be cross-platform.
Thanks.
If you are tied to the BMP file format, it's pretty simple to look at the header yourself and get the pixels. See this google search. One of the more interesting matches is here. The most counter-intuitive part is that every line of pixels is 4-byte aligned. Also, watch out for compressed BMPs... (My experience is that many third-party tools have trouble with compressed BMPs, so maybe some libraries you encounter will also..)
If you aren't tied to the BMP file format, I recommend libpng. The manual provides some sample code which is pretty clear.
Chris Backhouse made a functional little BMP loader (with an eye to using them as OpenGL textures). It's C++, not C, and he admits it's not cross platform. However, it's small and easy to understand, so I thought I'd add the link here:
You need some external library to do this (I recommend ImageMagick). The ImageMagick web site also includes documentation and examples.
As others suggested you might want to use an external library like SDL. If you want to learn something and do it yourself, see my answer to this very similar question: http://stackoverflow.com/questions/1531268/getting-rgb-values-for-each-pixel-from-a-24bpp-bitmap-in-c/1532386#1532386 where you'll find C code which prints out each pixel, and have a look at the wikipedia page about bmp files, because it's very good.
If you are looking for a minimal bmp loader this link will give you all you need to know about the BMP format, data structures and sample code without any library dependency to load: http://local.wasp.uwa.edu.au/~pbourke/dataformats/bmp/ It also contains code to see the loaded BMP in a open gl texture, so pretty much all you need...