Bogdan Kanivets
Bogdan Kanivets

Reputation: 562

C code for loading bitmap

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.

Upvotes: 1

Views: 7759

Answers (7)

Paulo Lopes
Paulo Lopes

Reputation: 5801

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://paulbourke.net/dataformats/bmp/.

It also contains code to see the loaded BMP in a open gl texture, so pretty much all you need...

Upvotes: 3

svens
svens

Reputation: 11628

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: Getting RGB values for each pixel from a 24bpp Bitmap for conversion to GBA format in C 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.

Upvotes: 0

Ashish
Ashish

Reputation: 8529

Check out for OpenCV Library developed by Intel .

Upvotes: 1

bta
bta

Reputation: 45057

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

Upvotes: 1

Rooke
Rooke

Reputation: 2033

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

Upvotes: 1

asveikau
asveikau

Reputation: 40226

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.

Upvotes: 0

Grizzly
Grizzly

Reputation: 20191

I would suggest using a library like SDL image

Upvotes: 5

Related Questions