Jump to content
  • Advertisement
Sign in to follow this  
LittleFreak

How do I read bmp info..

This topic is 5142 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to be able to read in each pixil of a 512x512 bmp and load each pixil int a 3x1 array(R G and B) and then load those arrays it into a 512x512 array. I have no idea how to do this so if anyone knows how or knows of a tutoral that will help me do this would be alot of help thanks. Also im using Directx with c++ if theres an easyer way to do with directx. Also would like to be able to edit and save the bmp too. [Edited by - LittleFreak on August 25, 2004 10:02:28 PM]

Share this post


Link to post
Share on other sites
Advertisement
There are two things that you need to know to load a bitmap.

The first is the bitmap file header (
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_62uq.asp )

This structure defines what you need to know about the file itself. The structure isn't terribly important, but you need to load it before you load the next: bitmap info header
( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1rw2.asp )

This describes the format of the bitmap. From this information, you can figure out how much data to load. For example, 24 BPP at 256x256 pixels = 1572,864 bits = 196,608 Bytes = 192KB. Unfortunately this does not mean that you read 192KB of data.. you really read in 256KB of data (262,144 bytes) because 24bpp bitmaps are padded to 32bpp. The simplest way to read this data is to use fread() and read Width * Height amount of RGB structures. The RGB structure you can make yourself and should contain a byte for red, a byte for green, one for blue, and an extra one for that bit of padded space. Piece of cake.

Oh, if you want to do it the easy way you can use the windows GDI to load an hBitmap object, then attach a DirectX surface to it.

(please excuse if there is a mistake in the theory, It's been years since I've had to manually load a bitmap)

Share this post


Link to post
Share on other sites
i wanted it to make my own height map like things.wanted to take the info from each pixil and use the number for infomation in my game.

Share this post


Link to post
Share on other sites
I wrote some source a while ago for my GBA development. It took either a 16 color depth or 256 color depth bmp file and turned it into a header file.

Here is the meat of the code:


//2 bytes/pixel for 16 colors
if(color_depth == 16)
{
fseek(in,0x12,SEEK_SET);
fread(&width,4,1,in);
fseek(in,0x16,SEEK_SET);
fread(&height,4,1,in);
fseek(in,0x436,SEEK_SET);

printf("Writing 2bytes per pixel in 16 colors\n");

for(j=0;j<height;j++)
{
for(i=0;i<width;i++)
{
pixel_in = fgetc(in);
pixel_one = pixel_in >> 4; //>> 4
pixel_two = pixel_in & 0x0F; // & 0x0F

fprintf(out,"%#.4x, ",pixel_one);
fprintf(out,"%#.4x, ",pixel_two);

bytes = bytes + 2;
}
fprintf(out,"\n");

}
fprintf(out,"};");
}

//check the color depth that the user input
//1 byte per pixel (8 bits). 1 char = 1 pixel (256 colors)
if(color_depth == 256)
{
printf("Writing 1 byte per pixel in 256 colors\n");
fseek(in,0x12,SEEK_SET);
fread(&width,4,1,in);
fseek(in,0x16,SEEK_SET);
fread(&height,4,1,in);
fseek(in,0x436,SEEK_SET);

for(j=0; j<height; j++)
{
for(i=0;i<width;i++)
{
fprintf(out,"%#.4x, ",fgetc(in));
bytes++;
}
fprintf(out,"\n");
}
fprintf(out,"};");
}



Hopefully that helps you out.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!