Click to See Complete Forum and Search --> : BMP Image Code
MethodicalJ
09-16-2004, 03:41 PM
Hi everyone.
I'm looking for some code that will load a .bmp image. More specifically, I need code that will load the image, and let me easily get the RGB values of each pixel, so I can use it in my graphics code, like this pseudocode:
function loadBMP( filename )
{
open the file, read the header, get the width and height
do all the other necessary stuff
a loop that gets the RBG values for each pixel, which i can modify to
convert to my format, which happens to be 3 floating point values.
}
I'm not good with file formats, so any code that takes care of detecting the pixel format, pallette, and such would be nice.
thanks for any help you can give. :confused:
madcompnerd
09-16-2004, 04:00 PM
What language?
In c/c++ I'd recommend using stdio.h, you can use fread to pull in the entirety of the image (after the headers) into one array, and index it by groups of 3). The reason for pulling it at once is simply keeping from abusing your use of the hard disk (it's bad to keep the hard disk in use longer than needed).
Course if you do that, make sure you set limits, if somebody should feed you a 2GB bmp (yes I know, unlikely) you should either error, or work with it in segments if possible.
Look at cplusplus.com for function references...
MethodicalJ
09-16-2004, 06:30 PM
It's in C.
I know how to get the file in and get rid of the buffer, i don't know how to read pixel format and stuff.
bwkaz
09-16-2004, 06:46 PM
The only thing I was able to find after a G4L search (hint hint -- maybe your Google-fu is stronger than mine today ;)) was a reference to an Eclipse class (ImageLoader) that can load Windows BMP files in Java. Doesn't sound too helpful, though...
Edit: The BMP file format is discussed in Microsoft's API reference:
http://support.microsoft.com/default.aspx?scid=kb;en-us;81498
Search through that page for "bmp file formats" -- that's the start of the official format, basically. You could also visit search.microsoft.com and search for BITMAPFILEHEADER (that's what's stored at the beginning of the BMP file), BITMAPINFOHEADER (it's stored right after the BITMAPFILEHEADER), and BITMAPINFO (stored after the BITMAPINFOHEADER) structures. Then you would be able to create equivalent structures in your C program, and read the file into them.
There is a code sample on that page also, that reads in a BMP file. It probably uses some Windows APIs to do it, but some of it might be helpful.
You could use the Imagemagick C API. It loads BMP files (amongst pretty much every other format), and it probably has the code to do whatever pixel operations you need as well.
http://www.imagemagick.org/index.html
Its like linking GIMP to your application :)
scifire
09-18-2004, 12:06 PM
I have the code that U need but it is not on my current machine Tomorrow I will get it from home and I will give it to U
ggforrest
09-27-2004, 08:10 AM
For any file format information check out http://www.wotsit.org/. They have the bitmap file format.
MethodicalJ
10-03-2004, 04:31 PM
Hey guys, I figured it out. If anyone is interested in the code ( which reads in a BMP file and lets you store the RGB channels separately, like in some arrays of floating point values for image editing ) just ask.