views:

751

answers:

7

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.

+3  A: 

I would suggest using a library like SDL image

Grizzly
A: 

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.

asveikau
I've done basic googling already. Thanks for response though.
Bogdan Kanivets
OK. I'd still encourage you to look at the sample code in the libpng manual, if PNG is an option for you.
asveikau
+1  A: 

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:

http://users.ox.ac.uk/~orie1330/bmploader.html

Rooke
+1  A: 

You need some external library to do this (I recommend ImageMagick). The ImageMagick web site also includes documentation and examples.

bta
BMP is such a simple format a library is a massive overhead.
LiraNuna
I agree, but if resource usage isn't an issue for your app, it's much easier than rolling your own.
bta
+1  A: 

Check out for OpenCV Library developed by Intel .

Ashish
I definitely recommand opencv too.I'm using it for a multi touch project, and it's really cool :-)
Aif
Using opencv would be like using a truck to move a glass to the other end of the table. Open CV is a computer vision library, it does reading and writing images on the side.
jilles de wit
A: 

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.

svens
+1  A: 

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...

Paulo Lopes