Sign in to follow this  
_Sigma

Bmp Load function

Recommended Posts

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...
...
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 errors out at the line indicated
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
//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(&#8465;[((height-1) - index)*bytes_per_line], &buffer[index*bytes_per_line], bytes_per_line);

        // release the memory
        free(buffer);

        // return success
        return(1);

}


[Edited by - Coder on September 19, 2004 11:49:35 PM]

Share this post


Link to post
Share on other sites
Yea, I remember having problems with that function, too...

Are you using DirectDraw7, or just that loader function? If you are still using DD7, you may want to consider upgrading to DX9. Even though DirectDraw has been deprecated in DirectX9, D3DX still has all the functionality that you need for 2D apps (to draw a texture to screen, just use ID3DXSprite). It will take some effort on your part to move up, but the benefits are well worth it.

If you upgrade, you can simply use D3DXCreateTextureFromFile() to load all of your images - .bmp, .dds, .hdr, .jpg, .png, and .tga (just to name a few).

Share this post


Link to post
Share on other sites
Well since I'm using Lamoth's book, I would kinda like to keep it simple for my first DX app, then move up to 3D when I read his 3D book. =) Are there any tutorials about what you suggested? Also, do you have any idea whats wrong with that function? because it *should* work, but it doesn't....:(

thanks

[edit]
Ya, i'm using DirectDraw 7 interfaces atm. 32-bit. Using that function to load the file, then puting it on a surface, the blting it to the backbuffer

Share this post


Link to post
Share on other sites
I guess I just really don't see the point in spending a lot of time learning old technology just to learn it, considering you aren't going to use it again. Also, Lamoth's 3D book teaches you a lot of stuff that is cool to know, but you don't *really* need to know it. Direct3D already handles a lot, so you don't need to spend your time re-inventing the wheel.

I understand where you're coming from, because Lamoth's 3D book does go over a lot of graphics theory, which you do need to know (you will definetly struggle without it). However, you don't need to read a 1000 page book on DirectDraw7 and a 1500 page book on software renderering to grasp the concept of a matrix [wink].

If you want to learn 3D, I'd say just go for it. Download the new SDK and check out some of the great tutorial sites listed here (DrunkenHyena and Andy Pike are especially good).

If you want a really good book to help you out on the graphics theory side, I recommend Real-Time Rendering. It rocks. Also, MathWorld is great for that kind of stuff.

Share this post


Link to post
Share on other sites
First, start with MSDN...

BITMAPFILEHEADER
and
BITMAPINFOHEADER

Then check the following:

- The image data starts at BITMAPFILEINFO::bfOffBits, using 'EOF - BITMAPINFOHEADER::biSizeImage' is unreliable.
- Check your image compression is BI_RGB first, and that your bitmaps are uncompressed and sensible colour depths.
- BITMAPINFOHEADER might be susceptible to compiler packing & realignment; try reading it in element-wise or make your own structure.
- Bitmap rows are padded if the width isn't a multiple of 4, you'll have to read them in line-by line if this is the case.
- Bitmaps are stored upside-down.


My personal recommendation - use a tried & tested loading mechanism and forget about it.

Jans.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by circlesoft
If you want to learn 3D, I'd say just go for it.


aye, I *do* want to learn 3D, but I also want to learn 2D, and I already have the book, so I might as well finish 'er!

>>My personal recommendation - use a tried & tested loading mechanism and forget about it.

Alright, can you supply a method? ;)

But my code *should* work tho. I read the stuff on MSDN, and everything *looks* in order....

I also have a flip function, so that if the BMP is upsidedown, it doesn't matter...

Thanks for the advice guys,

Sigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this