Archived

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

TheNerd Tk421

Loading a bmp...(sweet snippets)

Recommended Posts

#include "Bitmap.h"


Bitmap::Bitmap(){
    reset();
}


Bitmap::Bitmap(char *file){
    reset();
    loadBMP(file);
}


Bitmap::~Bitmap(){
    if(colours!=0) {
        delete[] colours;
    }
    if(data!=0) {
        delete[] data;
    }
}


bool Bitmap::loadBMP(char *file) {
    FILE *in;                  
    char *tempData;       
    int numColours;            


    loaded=false;

    if(colours!=0) {
        delete[] colours;
    }
    if(data!=0) {
        delete[] data;
    }


    in=fopen(file,"rb");


    if(in==NULL) {
        error="File not found";
        fclose(in);
        return false;
    }


    fread(&bmfh,sizeof(BitmapFileHeader),1,in);
	cout << "sizeof(BitmapFileHeader)=" << sizeof(BitmapFileHeader) << endl;

    if(bmfh.bfType!=BITMAP_MAGIC_NUMBER) {
        error="File is not in DIB format";
        fclose(in);
        return false;
    }


    fread(&bmih,sizeof(BitmapInfoHeader),1,in);
	cout << "sizeof(BitmapInfoHeader)=" << sizeof(BitmapInfoHeader) << endl;

 
    width=bmih.biWidth;
    height=bmih.biHeight;
    bpp=bmih.biBitCount;
	cout << "biBitCount      =" << bmih.biBitCount << endl;
	cout << "biClrImportant  =" << bmih.biClrImportant << endl;
	cout << "biClrUsed       =" << bmih.biClrUsed << endl;
	cout << "biCompression   =" << bmih.biCompression << endl;
	cout << "biHeight        =" << bmih.biHeight << endl;
	cout << "biPlanes        =" << bmih.biPlanes << endl;
	cout << "biSize          =" << bmih.biSize << endl;
	cout << "biSizeImage     =" << bmih.biSizeImage << endl;
	cout << "biWidth         =" << bmih.biWidth << endl;
	cout << "biXPelsPerMeter =" << bmih.biXPelsPerMeter << endl;
	cout << "biYPelsPerMeter =" << bmih.biYPelsPerMeter << endl;


    dataSize=(width*height*(unsigned int)(bmih.biBitCount/8.0));


    numColours=1<<bmih.biBitCount;

    if(bpp<8) {
        error="File is not 8 or 24 bits per pixel";
        fclose(in);
        return false;
    }


    if(bpp==8) {
    	colours=new RGBQuad[numColours];
    	fread(colours,sizeof(RGBQuad),numColours,in);
    }


    tempData=new char[dataSize];

 
    if(tempData==NULL) {
        error="Not enough memory to allocate a temporary buffer";
        fclose(in);
        return false;
    }

  
    fread(tempData,sizeof(char),dataSize,in);

  
    fclose(in);

  
    byteWidth=padWidth=(int)((float)width*(float)bpp/8.0);

  
    while(padWidth%4!=0) {
        padWidth++;
    }

    if(bpp==8) {
    	loaded=convert8(tempData);
   	}
    else if(bpp==24) {
    	loaded=convert24(tempData);
   	}


    delete[] tempData;

   
    error="Bitmap loaded";


    return loaded;
}


void Bitmap::reset(void) {
    loaded=false;
    colours=0;
    data=0;
    error="";
}

bool Bitmap::convert24(char* tempData) {
	int offset,diff;

	diff=width*height*RGB_BYTE_SIZE;
 
    data=new char[diff];

   
    if(data==NULL) {
        error="Not enough memory to allocate an image buffer";
        delete[] data;
        return false;
    }

    if(height>0) {
        offset=padWidth-byteWidth;
   
        for(int i=0;i<dataSize;i+=3) {

            if((i+1)%padWidth==0) {
                i+=offset;
            }
          
            *(data+i+2)=*(tempData+i);
            *(data+i+1)=*(tempData+i+1);
            *(data+i)=*(tempData+i+2);
        }
    }

   
    else {
        offset=padWidth-byteWidth;
        int j=dataSize-3;
       
        for(int i=0;i<dataSize;i+=3) {
           
            if((i+1)%padWidth==0) {
                i+=offset;
            }
          
            *(data+j+2)=*(tempData+i);
            *(data+j+1)=*(tempData+i+1);
            *(data+j)=*(tempData+i+2);
            j-=3;
        }
    }

    return true;
}

bool Bitmap::convert8(char* tempData) {
	int offset,diff;

	diff=width*height*RGB_BYTE_SIZE;
 
    data=new char[diff];


    if(data==NULL) {
        error="Not enough memory to allocate an image buffer";
        delete[] data;
        return false;
    }

    if(height>0) {
        offset=padWidth-byteWidth;
        int j=0;
    
        for(int i=0;i<dataSize*RGB_BYTE_SIZE;i+=3) {
          
            if((i+1)%padWidth==0) {
                i+=offset;
            }
           
            *(data+i)=colours[*(tempData+j)].rgbRed;
            *(data+i+1)=colours[*(tempData+j)].rgbGreen;
            *(data+i+2)=colours[*(tempData+j)].rgbBlue;
            j++;
        }
    }

   
    else {
        offset=padWidth-byteWidth;
        int j=dataSize-1;
     
        for(int i=0;i<dataSize*RGB_BYTE_SIZE;i+=3) {
           
            if((i+1)%padWidth==0) {
                i+=offset;
            }
            
            *(data+i)=colours[*(tempData+j)].rgbRed;
            *(data+i+1)=colours[*(tempData+j)].rgbGreen;
            *(data+i+2)=colours[*(tempData+j)].rgbBlue;
            j--;
        }
    }

    return true;
}
errors: [Linker error] undefined reference to `WinMain@16' ..................... my header file:
#ifndef _BITMAP_H
#define _BITMAP_H

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

const short BITMAP_MAGIC_NUMBER=19778;
const int RGB_BYTE_SIZE=3;

#pragma pack(push,bitmap_data,1)

typedef struct tagRGBQuad {
	char rgbBlue;
	char rgbGreen;
	char rgbRed;
	char rgbReserved;
} RGBQuad;

typedef struct tagBitmapFileHeader {
	unsigned short bfType;
	unsigned int bfSize;
	unsigned short bfReserved1;
	unsigned short bfReserved2;
	unsigned int bfOffBits;
} BitmapFileHeader;

typedef struct tagBitmapInfoHeader {
	unsigned int biSize;
	int biWidth;
	int biHeight;
	unsigned short biPlanes;
	unsigned short biBitCount;
	unsigned int biCompression;
	unsigned int biSizeImage;
	int biXPelsPerMeter;
	int biYPelsPerMeter;
	unsigned int biClrUsed;
	unsigned int biClrImportant;
} BitmapInfoHeader;

#pragma pack(pop,bitmap_data)

class Bitmap {
public:
    
    RGBQuad *colours;
    char *data;
    bool loaded;
    int width,height;
    unsigned short bpp;
    string error;
   
    Bitmap(void);
    Bitmap(char *);
    ~Bitmap();
    bool loadBMP(char *);
private:
    
    BitmapFileHeader bmfh;
    BitmapInfoHeader bmih;
    int byteWidth;            
    int padWidth;             
    unsigned int dataSize;                
    
    void reset(void);
    bool convert24(char *);		
    bool convert8(char *);		
};

#endif _BITMAP_H
[edited by - TheNerd Tk421 on August 10, 2003 11:27:03 PM]

Share this post


Link to post
Share on other sites
Is that what''s required to load a bitmap from a file? Seems intimidating. Not sure what the linker error means. Gotta be a simpler way to load .bmp files.

I found this online which I''m gonna use. If you just want to load a bitmap from a file and display it on screen, you can use the Win GDI function LoadImage. No need to parse the BMP file. Here is something I found online, the bare minimum you need. Not tested yet, but something to work with....

BMP.cpp

Phil P





Share this post


Link to post
Share on other sites
You know is easier using an API to load bitmaps, sounds, etc this one is very simple and is really great for 2d games not so good for 3d but is a start. click


[edited by - FtMonkey on August 11, 2003 1:41:24 AM]

[edited by - FtMonkey on August 11, 2003 1:46:27 AM]

Share this post


Link to post
Share on other sites
<< You need an entry point (WinMain) >>

Ah, I should have thought of it. I figured the guy was giving just a small portion. Yeah, he needs a WinMain, and a WinProc.

He should keep it simple and use LoadImage and get some Win GDI thing going.

Phil P

Share this post


Link to post
Share on other sites
Oh yeah I forgot you can use windows GDI it's way easier than what your doing now. About your problem the two previous posts should solve your problem I wasn't sure if you were just including a section of your code so I couldn't give you a good answer...Anyways make sure you have WinMain declared there and don't forget about including the windows procedure, unlike console apps you need those there.

[edited by - FtMonkey on August 11, 2003 2:00:22 AM]

Share this post


Link to post
Share on other sites
That's a lie!! I have allegro with dev-c++ and it works perfect if you follow the docs you'll install it without a problem. Oh and I have no idea what OpenXDK is...

Just make sure you use the correct build in your case mingw32...


[edited by - FtMonkey on August 11, 2003 2:08:57 AM]

Share this post


Link to post
Share on other sites
<< Dev-C++ makes me mad >>

You gotta break down, mow some lawns, flip some burgers, or ask Mom and Dad for $300 to buy Visual Studio Pro. Or better find a local college bookstore that sells it cheaper. I assume you''re in high school, grade school, or at least kindergarten. Haven''t used Dev C myself, using Visual Studio 6, here is .NET Pro

Visual Studio .NET Pro

Phil P

Share this post


Link to post
Share on other sites
quote:
Original post by TheNerd Tk421
I hate Dev-C++!!!!... nothing works with it.. i downloaded allegro..i have to have other compilers to compile it.. i downloaded OpenXDK.. i have to have VC++ or .net...Dev-C++ makes me mad


??? you choose the wrong project type and its the the IDE's fault? hate to break this to you but VisualStudio isn't some magic wand that will give a brain....

ps. FYI:

Console Application -> int main(int argc, char *argv[])
Windows Application -> int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)


/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''t use it...

[edited by - guppy on August 11, 2003 2:14:33 AM]

Share this post


Link to post
Share on other sites
Dev-C++ is sort of buggy but is not bad considering is free besides the reason why still has some bugs is because is not fully finished, but that doesn't mean you can't use it to make a good game in it...

[edited by - FtMonkey on August 11, 2003 2:20:12 AM]

Share this post


Link to post
Share on other sites
Well you say you want to get a better IDE but personally I have them both on my computer. And I admit MSVC is easier to use personally I find it a lot easier for windows programming other than that you can do the same great game on any of the two...Dev-C++ is a little harder but is not a bad IDE, don''t try to find the solution in a better compiler cuz they don''t write the code for you, that you have to learn how to do it in your own...

Share this post


Link to post
Share on other sites