Thread: Manually Loading Bitmap Files

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    4

    Manually Loading Bitmap Files

    Hello, I was wondering if guys could help me out here, im trying to manually a load a bitmap file in a C++ program but am a little confused. After searching through various tutorials on this subject I dont seem to be doing anything wrong but for some reason the values I am getting in my bitmap headers seem to be wrong (or maybe its just me).

    Here is the code i have to read a bitmap, admitedly it is not complete as it only opens the file and reads in to my "custom" BitmapFileHeader and BitmapInfoHeader structures.
    Code:
     //structure to contain bitmap file header information. It is the same as the win32 version except its my own.. if that makes sense
    struct BitmapFileHeader {
    	unsigned short	Type;
    	unsigned int	Size;
    	unsigned short	Reserved1;
    	unsigned short	Reserved2;
    	unsigned int	OffBits;
    };
    
    struct BitmapInfoHeader {
    	unsigned int	Size;
    	long		Width;
    	long		Height;
    	unsigned short	Planes;
    	unsigned short	BitCount;
    	unsigned int	Compression;
    	unsigned int	SizeImage;
    	long		XPelsPerMeter;
    	long		YPelsPerMeter;
    	unsigned int	ClrUsed;
    	unsigned int	ClrImrportant;
    };
    
    // Read bitmap file
    void Bitmap::Read( const char* file )
    {
    	// open file for reading
    	std::ifstream f( file, std::ios::binary );
    	if ( !f.is_open( ) )
    	{
    		std::cout << "Error: File not found.";
    		f.close( );
    	}
    
    	// read in the file and info headers
    	f.read( reinterpret_cast<char*>(&mFileHeader), sizeof( BitmapFileHeader ) );
    	f.read( reinterpret_cast<char*>(&mInfoHeader), sizeof( BitmapInfoHeader ) );
    }
    Here is the list of values that shows up when "stepping through" my code with the vs debugger:

    mFileHeader.Type = 19778 < (Thats probably the only value that looks correct to me.. when cast into char* it produces "BM")
    mFileHeader.Size = 0
    mFileHeader.Reserved1 = 0
    mFileHeader.Reserved2 = 54
    mFileHeader.OffBits = 2621440

    mInfoHeader.Size = 131072
    mInfoHeader.Width = 131072
    mInfoHeader.Height = 65536
    mInfoHeader.Planes = 24
    mInfoHeader.BitCount = 0
    mInfoHeader.Compression = 1048576
    mInfoHeader.SizeImage = 0
    mInfoHeader.XPelsPerMeter = 0
    mInfoHeader.YPelsPerMeter = 0
    mInfoHeader.ClrUsed = 0
    mInfoHeader.ClrImrportant = 0

    Im not sure but i think the compression might be the problem, however i did try another type of .bmp that had compression 0 but still had the same problems so maybe not (and im guessing compression only deals with the actual data, not the headers).

    Anywho, Thanks in advance.
    ~CC


    PS: The image I am loading is a simple 2x2 24-bit .bmp created with windows 7 version of paint. Unless im a mistaken the mInfoHeader.Width/Height should be 2 x 2
    not 131072x65536, you get my point :P
    Last edited by CodeCriminal; 10-30-2009 at 02:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading multiple files
    By SanDiego619SDSU in forum C Programming
    Replies: 4
    Last Post: 10-29-2006, 01:37 AM
  2. Loading Bitmap Problem
    By loopshot in forum Game Programming
    Replies: 1
    Last Post: 04-15-2005, 12:26 PM
  3. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  4. Loading X Files with DirectX 8 ??
    By MrWizard in forum Game Programming
    Replies: 2
    Last Post: 07-24-2002, 02:48 AM

Tags for this Thread