Teen Programmers Unite  
 

 

Return to forum top

BMP files in C++ programs

Posted by Dante [send private reply] at June 12, 2001, 07:31:09 PM

Can anyone give me some help with loading BMP files or other sprite-like image files in C++? I know the format to a BMP, i just don't know how to use it. How are the pixels represented linearly in a BMP's code? Does anyone know a good online tutorial or example of this that is easy to understand?

Posted by Psion [send private reply] at June 12, 2001, 07:59:46 PM

I'm confused. You say you know the format, but you ask how the pixels are represented? If you are lacking any information on the format, visit http://www.wotsit.org/ for file format specs. If you can't work from there, just think it through. You can read it one byte at a time and do whatever handling you need.

Posted by gian [send private reply] at June 13, 2001, 12:27:06 AM

http://members.nbci.com/code_head/ has heaps of info about reading PCX's, might be kinda helpful, has code examples too.

Posted by Setherd123 [send private reply] at June 13, 2001, 03:47:30 PM

http://www.tamale.net has alot of great source code.
http://www.tamale.net/Windows.html <- Down at the bottom under "Examples for learning Win32 programming."

Be sure to check out Shaboozy, too!

I'd like to add, don't just steal the code, because that's no no.

Posted by Psion [send private reply] at June 13, 2001, 03:56:58 PM

That reminds me. Windows has a LoadBitmap function that you can use if you don't mind doing everything aftewards with Windows API functions as well.

Posted by nt543 [send private reply] at June 17, 2001, 09:50:08 PM

If you're using only Windows, use the bitmap as a resource and let Windows load it with LoadBitmap(). If you want to make your own loader, I have some suggestions from "Tricks of the Windows Game Programming Gurus" by Andre Lamothe (CEO@xgames3d.com).
The first thing in the bitmap is the header
WORD Type (Always = 0x4D42)
DWORD Size (Size of complete file)
WORD Reserved
WORD Reserved
DWORD Offset (Read about this later in the comment)
Next comes more info:
DWORD Size (bytes in structure for info structure)
LONG Width (Negative if the image is upside-down)
LONG Height
WORD NumColorPlanes (Always = 1)
WORD BPP (Bits per pixel - 1=monochrome,4=16 color,8=palettized,16+=full color)
DWORD Compression (0 for non-compressed bitmaps - See WINDOWS.H for other values)
DWORD ImageSize (bytes)
LONG PixelsPerMeter_X
LONG PixelsPerMeter_Y
DWORD NumColorsUsed (0 for more than 8-bit color)
DWORD NumImportantColors
Palette (If 8-bit color)
256 of these sets:
char RED
char GREEN
char BLUE
char Reserved
Actual Data:
(Upside-down if height is negative)
RGB/palette indexes




Pseudo-Code:
Open the file
Read and verify the header (save this in a structure)
Read the Info section (Save this in a structure too)
Be sure to get the width, height, and color depth
Make sure it isn't compressed
Read the palette (if the color depth is 8)
Seek to EOF-Image_Size (the image size is found in the info structure)
Allocate memory
Read the data into a buffer/texture memory/etc.
NOTE: If the height is negative, use its absolute value and be sure to vertically flip the bitmap after you load it.
The size is Width*Height*Color_Depth
Remember to close the file :)
Also free the memory when you're done

Posted by guess_who [send private reply] at July 06, 2001, 01:20:00 AM

Do .PCX's instead...it's easier (for me, at least)...I used it in my on-going 3D engine library...cheers!

Posted by TheTutor [send private reply] at July 18, 2001, 04:32:22 PM

If you want to load a .bmp in Windows here's an easy way:

/*************** Loading a bitmap AND displaying the bitmap ***************/

// Okay here's the explanation for each argument:
// hinstance is the HINSTANCE given to us by WinMain(),
// "MyBitmap.bmp" is the name of the bitmap we want to load,
// IMAGE_BITMAP is a flag to say hey we're loading a .bmp type file
// The two zeros correspond to the width and height of the image
// we're loading. We can make these zero because since we're loading
// from a file it will take the width and height that
// the file says the image we're loading is. Can you say wordy?
// All right the last parameter says that we're loading this image from
// a file

HBITMAP hbitmap = (HBITMAP)LoadImage(hinstance, "MyBitmap.bmp",
IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

HDC hdc = GetDC(hwnd); // This gets the device context
// associated with the window we made
HDC image_dc = CreateCompatibleDC(hdc); // The gets a device context
//that is of the same size and // type of device context we pass
//in.

// Must save off old object that was "selected"
HBITMAP old_hbitmap = (HBITMAP)SelectObject(image_dc,hbitmap);


// This is the sweet function that draws stuff for us
// hdc is where we want to draw stuff to (device context of our window)
// 50, 50 is the upper left corner of where we are going to draw
// 320 is the width of bitmap in pixels
// 192 is the height of our bitmap in pixels
// image_dc is the stuff we want to draw to our window
// 0,0 is the upper left corner of where we want to start coping the
// image_dc from
// SRCCOPY stands for source copy -- Meaning take everything at the
// source (image_dc) and copy it to the destination (hdc)

BitBlt(hdc,50,50,320,192,image_dc,0,0,SRCCOPY);

/*************** Done loading a bitmap AND displaying the bitmap ****************************/

Complete source code can be found at: http://www.gametutorials.com/Tutorials/Intermediate/Win32_Pg2.htm

Hope this helps :)

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.