Thread: Load BMP from file func

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    180

    Load BMP from file func

    Alright, I have a loadBMP function, but it doesn't actually load the BMP for some reason. the function and everything is contained in a header, which is included in Unit1.cpp

    this is how I use it...
    Code:
    ...
    BITMAP_FILE bitmap;
    ....
    //load background
    if(!LoadBMP(&bitmap,"lvl2.bmp"))
       Application->MessageBoxA("Can't load BMP","ERORR",MB_OK); GameKill();
    I trace through, and it returns the error at the line indicated
    Code:
    if(bitmap->bitmapinfoheader.biBitCount == 8 || bitmap->bitmapinfoheader.biBitCount == 16 ||
           bitmap->bitmapinfoheader.biBitCount == 24)
           {
                   //delete the alst image if there was one
                   if(bitmap->buffer)
                           delete bitmap->buffer;
    
                   //allocate the memory for the image
    
                   if(!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
                   {
                           //close the file
                           _lclose(file_handle);
    
                           //return error
                            return(0);
                   }
    
                   //now read it in
                   _lread(file_handle,bitmap->buffer, bitmap->bitmapinfoheader.biSizeImage);
           }
           else
           {
                   //WOW! Serious error!  
                   return(0);<<<-------------- THIS LINE <<----------
           }
    I check the value of biSizeImage and its 24.....So why doesn't that fit the clause in the if statment?

    All that happens is that I get the message saying It wasn't loaded(at the indicated line). Any ideas?

    I've attached the header file below

    Many thanks

    DW

    Code:
    //this was taken from André LaMoth's "Tricks of the windows game programming gurus"
    //p.346  2nd Revision
    
    //NOTE:
    //assumes your not stupid and arn't asking it to load a non-BMP file. the code is commented out for that
    
    #include "Unit1.h"
    #define BITMAP_ID            0x4D42 // universal id for a bitmap
    #define MAX_COLORS_PALETTE   256
    
    //////container structure for bitmap files///////////////////////////////////////
     typedef struct BITMAP_FILE_TAG
            {
            BITMAPFILEHEADER bitmapfileheader;  // this contains the bitmapfile header
            BITMAPINFOHEADER bitmapinfoheader;  // this is all the info including the palette
            PALETTEENTRY     palette[256];      // we will store the palette here
            UCHAR            *buffer;           // this is a pointer to the data
    
            } BITMAP_FILE, *BITMAP_FILE_PTR;
    
    
    // PROTOTYPES  //////////////////////////////////////////////
    
    int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height);
    
    int LoadBMP(BITMAP_FILE_PTR bitmap, char *filename);
    
    int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap);
    
    
    //LOAD///////////////////////////////////////////////////////////////////////////
    int LoadBMP(BITMAP_FILE_PTR bitmap, char *filename)
    {
    //this function opens a *.BMP file
    
            int file_handle; //the file handle
            int i; //looping var
    
            UCHAR *temp_buffer = NULL; //used to convert 24 bit images to 16bit
            OFSTRUCT file_data; // file data information
    
            //open file if it exists
            if((file_handle = OpenFile(filename,&file_data,OF_READ)) == -1)
                    return(0); //Opening failed
    
            //load the bitmap header
            _lread(file_handle,&bitmap->bitmapfileheader,sizeof(BITMAPFILEHEADER));
    
        /*    //TEST IF THIS IS A BITMAP FILE
            if (bitmap->bitmapfileheader.bfType != BITMAP_ID)
            {
                    //this definatly isn't a bitmap, so lets unload 'er
                    _lclose(file_handle);
    
                    //return error
                    return(0);
            }   */
    
    
            //Now lets read in the infoheader
            _lread(file_handle, &bitmap->bitmapinfoheader,sizeof(BITMAPINFOHEADER));
    
            //NOW LOAD THE COLOR PALETTE IF THERE IS ONE
            if(bitmap->bitmapinfoheader.biBitCount == 8)
            {
                    _lread(file_handle, &bitmap->palette,MAX_COLORS_PALETTE*sizeof(PALETTEENTRY));
    
                    //Now set all the flags in the palette correctly and fix the reversed BGR RGBQUAD
                    for(i = 0; i < MAX_COLORS_PALETTE; i++)
                    {
                            int temp_color = bitmap->palette[i].peRed;
                            bitmap->palette[i].peRed = bitmap->palette[i].peBlue;
                            bitmap->palette[i].peBlue = temp_color;
    
                            bitmap->palette[i].peFlags = PC_NOCOLLAPSE;
                    }
            }
    
            //image data
            _lseek(file_handle, -(int)(bitmap->bitmapinfoheader.biSizeImage),SEEK_END);
    
            // now read in the image, if the image is 8 or 16 bit then simply read it
            // but if its 24 bit then read it into a temporary area and then convert
            // it to a 16 bit image
    
            if(bitmap->bitmapinfoheader.biBitCount == 8 || bitmap->bitmapinfoheader.biBitCount == 16 ||
            bitmap->bitmapinfoheader.biBitCount == 24)
            {
                    //delete the alst image if there was one
                    if(bitmap->buffer)
                            delete bitmap->buffer;
    
                    //allocate the memory for the image
    
                    if(!(bitmap->buffer = (UCHAR *)malloc(bitmap->bitmapinfoheader.biSizeImage)))
                    {
                            //close the file
                            _lclose(file_handle);
    
                            //return error
                             return(0);
                    }
    
                    //now read it in
                    _lread(file_handle,bitmap->buffer, bitmap->bitmapinfoheader.biSizeImage);
            }
            else
            {
                    //WOW! Serious error!
                    return(0);
            }
    
            //close the file
            _lclose(file_handle);
    
            //flip the bitmap
            Flip_Bitmap(bitmap->buffer,
                bitmap->bitmapinfoheader.biWidth*(bitmap->bitmapinfoheader.biBitCount/8),
                bitmap->bitmapinfoheader.biHeight);
    
            // return success
            return(1);
    }
    
    /////UNLOAD//////////////////////////////////////////////////////////////////////
    int Unload_Bitmap_File(BITMAP_FILE_PTR bitmap)
    {
            // this function releases all memory associated with "bitmap"
            if (bitmap->buffer)
            {
                    // release memory
                    delete (bitmap->buffer);
    
                    // reset pointer
                    bitmap->buffer = NULL;
    
            } // end if
    
            // return success
            return(1);
    
    }
    
    //// FLIP ///////////////////////////////////////////////////////
    
    int Flip_Bitmap(UCHAR *image, int bytes_per_line, int height)
    {
    // this function is used to flip bottom-up .BMP images
    
            UCHAR *buffer; // used to perform the image processing
            int index;     // looping index
    
            // allocate the temporary buffer
            if (!(buffer = (UCHAR *)malloc(bytes_per_line*height)))
                    return(0);
    
            // copy image to work area
            memcpy(buffer,image,bytes_per_line*height);
    
            // flip vertically
            for (index=0; index < height; index++)
                    memcpy(ℑ[((height-1) - index)*bytes_per_line], &buffer[index*bytes_per_line], bytes_per_line);
    
            // release the memory
            free(buffer);
    
            // return success
            return(1);
    
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I've only read the first 3 lines of code, but.....
    Code:
    if(!LoadBMP(&bitmap,"lvl2.bmp"))
    {
       Application->MessageBoxA("Can't load BMP","ERORR",MB_OK); 
       GameKill();
    }
    >> I check the value of biSizeImage...
    You're looking at biBitCount.

    gg
    Last edited by Codeplug; 09-20-2004 at 04:38 PM.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Quote Originally Posted by Codeplug
    I've only read the first 3 lines of code, but.....
    >> I check the value of biSizeImage...
    You're looking at biBitCount.
    Heee. Typo. Sorry. I fixed that other line, but obviously that wasn't the issue, as when I trace through the load function, the highlighted line is the one that is hitm hence returning an error!

    Could my BMP be the issue? I'll try a different image tonight, see if that helps!

    Cheers

    DW

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM