Jump to content
  • Advertisement

Archived

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

TheNerd Tk421

Loading a bmp...(sweet snippets)

This topic is 5525 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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)

That is, if you want your project to do something...

[edited by - glassJAw on August 11, 2003 1:39:39 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
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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!