Archived

This topic is now archived and is closed to further replies.

Need help loading bitmaps in C++.

This topic is 5378 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

Im rusty in my programming. Can someone tell me how I can write this program in C++. I want a program that gets every pixel from a user specified bitmap and stores it in a bool array of either true ( if its white ) or false ( if its non-white ) for each pixel. From there I want to write it to a file. What I need help on is how do I load the bitmap so that I can use getpixel(x,y) on it. I know how to do the rest.

Share this post


Link to post
Share on other sites
I checked out that site but im a little confused by it.

So your saying i should read the bitmap in like a text file and in it will have the pixel information in it.

I do have a problem though that it will be compressed since I want to read 256 color bitmaps.

can someone start me out on this with some sample code. Im a little confused.

[edited by - goomyman on March 25, 2003 9:33:42 PM]

Share this post


Link to post
Share on other sites
no, make sure you read it in in binary format. i never have worked with compressed bitmaps, but it can''t be that much harder.

the structure of the bitmap is on that link,
just offset the file pointer to the following locations, reading in the data...

0012h width 1 dword horizontal width of bitmap in pixels.

0016h height 1 dword vertical height of bitmap in pixels.

001ah planes 1 word number of planes in this bitmap.

001ch bits per pixel 1 word

0036h palette n * 4 byte

0436h bitmap data x bytes

should be all you need, since your know the format of the bmp already. but i''m not gunna write the code for ya sorry.

Share this post


Link to post
Share on other sites
Thanks for the help, I understand it but I dont know how to read in binary data.

Do i read it into an unsigned char array? How would I set the offset?

As you can see im confused, but I understand what your saying i just dont know how to put it to work.

Can someone else give me some sample code to go by?

Also how would i determine what color the data is once I read it in. Which 1 is it?

Share this post


Link to post
Share on other sites
quote:

I do have a problem though that it will be compressed since I want to read 256 color bitmaps.
[quote]

Compress or 256 color, or both?

The compression algorthm used on bitmaps is usually RLE, which is rather easy and easy to find, www.google.com. If anyone is loading bitmaps, I suggest RLE, because RLE loads at worse case, the same as bitmaps, and at best case much much faster than bitmaps.

For 256 color bitmaps, there will be a palete at the begining of the bitmap. Save the palete, and then match the pixels in the file, with the palete entry to decode the bitmap. Its rather easy.

Share this post


Link to post
Share on other sites
I could use a monochrome bitmap instead of a 256 color. Its still compressed with RLE.

Im now totally lost though. In code. How would i go about reading in the pixel color if its white or black from the bitmap.

Im sorry, thanks for all your help guys.



[edited by - goomyman on March 27, 2003 2:08:49 AM]

Share this post


Link to post
Share on other sites

void Load(CString filename)
{
DWORD result;
HANDLE bitmap = LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
BITMAP bm;
GetObject(bitmap, sizeof(bm), &bm);
HDC memdc = CreateCompatibleDC(NULL);
SelectObject(memdc, bitmap);
for(int i = 0; i < bm.bmWidth; i++)
{
for(int i2 = 0; i2 < bm.bmHeight; i2++)
{
RGB color;
color = GetPixel(memdc, i, bm.bmHeight-i2);
if(color.bRed == 255 && color.bGreen == 255 && color.bBlue == 255)
{
array[i2] = TRUE;
}
else
{
array[i][i2] = FALSE;
}
}
}
DeleteObject(memdc);
DeleteObject(bitmap);

//Save here possibly with String tokenizer
}

Share this post


Link to post
Share on other sites