PDA

Click to See Complete Forum and Search --> : C/C++ - Loading Bitmap Files (Manually)


BeholderOf
Sep 18th, 2003, 03:16 PM
This is how you maually load a bmp file

The bitmap file format:
Bitmap file header
Bitmap info header
palette data
Bitmap Dada

so on with the code part, this is our struct we need to create to hold the bitmap file header

typedef struct tagBITMAPFILEHEADER
{
WORD bfType; //specifies the file type
DWORD bfSize; //specifies the size in bytes of the bitmap file
WORD bfReserved1; //reserved; must be 0
WORD bfReserved2; //reserved; must be 0
DWORD bOffBits; //species the offset in bytes from the bitmapfileheader to the bitmap bits
}BITMAPFILEHEADER;

the bftype field checks to see if you are infact loading a bmp file, and if you are the field should be 0x4D42.

Now we need to create our bitmapinfoheader struct. This holds info about our bitmap(self explainitory).

typedef struct tagBITMAPINFOHEADER
{
DWORD biSize; //specifies the number of bytes required by the struct
LONG biWidth; //specifies width in pixels
LONG biHeight; //species height in pixels
WORD biPlanes; //specifies the number of color planes, must be 1
WORD biBitCount; //specifies the number of bit per pixel
DWORD biCompression;//spcifies the type of compression
DWORD biSizeImage; //size of image in bytes
LONG biXPelsPerMeter; //number of pixels per meter in x axis
LONG biYPelsPerMeter; //number of pixels per meter in y axis
DWORD biClrUsed; //number of colors used by th ebitmap
DWORD biClrImportant; //number of colors that are important
}BITMAPINFOHEADER;


Now on to loading our Bitmap

unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)

FILE *filePtr; //our file pointer
BITMAPFILEHEADER bitmapFileHeader; //our bitmap file header
unsigned char *bitmapImage; //store image data
int imageIdx=0; //image index counter
unsigned char tempRGB; //our swap variable

//open filename in read binary mode
filePtr = fopen(filename,"rb");
if (filePtr == NULL)
return NULL;

//read the bitmap file header
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER,1,filePtr);

//verify that this is a bmp file by check bitmap id
if (bitmapFileHeader.bfType !=0x4D42)
{
fclose(filePtr);
return NULL;
}

//read the bitmap info header
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER),1,filePtr);

//move file point to the begging of bitmap data
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

//allocate enough memory for the bitmap image data
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

//verify memory allocation
if (!bitmapImage)
{
free(bitmapImage);
fclose(filePtr);
return NULL;
}

//read in the bitmap image data
fread(bitmapImage,bitmapInfoHeader->biSizeImage,filePtr);

//make sure bitmap image data was read
if (bitmapImage == NULL)
{
fclose(filePtr);
return NULL;
}

//swap the r and b values to get RGB (bitmap is BGR)
for (imageIdx = 0,imageIdx < bitmapInfoHeader->biSizeImage;imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}

//close file and return bitmap iamge data
fclose(filePtr);
return bitmapImage;
}


Now to make use of all of this:

BITMAPINFOHEADER bitmapInfoHeader;
unsigned char *bitmapData;
...
bitmapData = LoadBitmapFile("mypic.bmp",&bitmapInfoHeader);
//now do what you want with it, later on i will show you how to display it in a normal window


Later on ill put up Writing a to bmp and how to load a targa file and how to display them.

ramiz
Jun 11th, 2005, 12:00 PM
Can any one tell me how to display a Bitmap on a GLUT window or a Win32 window

WaraxeBloodstain
Sep 9th, 2007, 03:45 AM
Your code doesn't work under Dev C++. It gives a hundred error messages. Missa thinks its the Dev C++ fault. What compile do you use?

Afterthoughts: maybe Missa should try compiling it as C and not C++? Hummm

marcjack
Jan 24th, 2008, 12:46 PM
The reason why this is giving you so many errors is because the code contains them. Starting with a missing curly at the second line, continuing with a missing bracket...

pivke
Oct 13th, 2008, 01:07 PM
Hey,
sry for my bad english, Im a german baby boy (;
So, I wrote a Bitmaploader, too. But in OOP (ObjectOrientatedProgramming)
The text's in the Messagebox's are in German, if you guys want it in
english i can also write it in english. so, have fun.
#include "fstream"
class Bitmap
{
public:
void *LoadBitmap(const char *Filename);

//BITMAPFILEHEADER
typedef struct
{
WORD bfType;
DWORD bfSize;
DWORD bfReserved;
DWORD bfOffBits;
}BITMAPFILEHEADER;

//BITMAPINFOHEADER
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}BITMAPINFOHEADER;

BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER InfoHeader;
};

void *Bitmap::LoadBitmap(const char *Filename)
{
Bitmap o_Bitmap;
unsigned char *bitmap_Image;
FILE *File=NULL;
unsigned int ImageIdx=0;
if(!Filename)
{
MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
}
else
{
File=fopen(Filename,"rb");
}
fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
if(FileHeader.bfType != 0x4D42)
{
MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
}
fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
fseek(File,o_Bitmap.FileHeader.bfOffBits,SEEK_SET);
if(o_Bitmap.InfoHeader.biSizeImage != 0)
{
bitmap_Image=new unsigned char[o_Bitmap.InfoHeader.biSizeImage];
}
if(!bitmap_Image)
{
free(bitmap_Image);
fclose(File);
}
if(bitmap_Image==NULL)
{
fclose(File);
}
for(ImageIdx=0;ImageIdx < o_Bitmap.InfoHeader.biSizeImage; ImageIdx+=3)
{
bitmap_Image[ImageIdx] = bitmap_Image[+2];
bitmap_Image[ImageIdx+2] = bitmap_Image[ImageIdx];
}
fclose(File);
return bitmap_Image;
};



Thanks

pivke
Oct 27th, 2008, 10:44 AM
Hey guys,
i just wanted to say that I can now handle the RunLengthEncoding (RLE). To be precise the RLE4 (8 Bit) and RLE8 (24 Bit). If some guys are interested in the SourceCode, I can post it. But if there comes no feedback, I don't post it...So let me know if you want to see the full length of code!
Thanks!

Commodore
Oct 29th, 2008, 06:12 AM
Hi!
Yes I would like to see the entire code.
I was following this to, but didn't login yet.

Thx for the help i got here so far :)

pivke
Oct 29th, 2008, 01:46 PM
Hey!
Very cool. So, how i said one time before, the Bitmapdecoder can read
all RLE's. I've added 2 more methods, in this the next operation i will
get starting to overgive the textureinformations to the OpenGL-Paraneters
to laod the texture. So, if you want i can also write from the
OpenGL-Operations in the Win32-API.
So, here is the Code:
#include "fstream"
class Bitmap
{
private:
unsigned long BPP;
unsigned long width;
unsigned long height;
unsigned long size;
unsigned char *bitmap_Image;
unsigned int bps;
GLuint KompressionFormat;

public:
Bitmap();
~Bitmap();

void *LoadBitmap(const char *Filename);
void Load8BitKompression(const char *Filename);
void Load24BitKompression(const char *Filename);

#define BI_RGB 0L
#define BI_RLE4 2L
#define BI_RLE8 4L

#pragma pack(push,1);
//BITMAPFILEHEADER
typedef struct
{
WORD bfType;
DWORD bfSize;
DWORD bfReserved;
DWORD bfOffBits;
}BITMAPFILEHEADER;

//BITMAPINFOHEADER
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
}BITMAPINFOHEADER;

#pragma pack(pop);

void Kompression();

BITMAPFILEHEADER FileHeader;
BITMAPINFOHEADER InfoHeader;
};

Bitmap::Bitmap()
{
BPP=0;
width=0;
height=0;
bitmap_Image=NULL;
size=0;
bps=0;
KompressionFormat=0;
}

Bitmap::~Bitmap()
{
BPP=NULL;
width=NULL;
height=NULL;
delete[] bitmap_Image;
KompressionFormat=NULL;
size=NULL;
bps=NULL;
}

void *Bitmap::LoadBitmap(const char *Filename)
{
Bitmap o_Bitmap;
FILE *File=NULL;
unsigned int ImageIdx=0;
unsigned char *bitmap_Image=NULL;

if(!Filename)
{
MessageBox(NULL,"Konnte Filename nicht finden!","Error",MB_OK|MB_ICONERROR);
return false;
}
else
{
File=fopen(Filename,"rb");
if(!File)
{
MessageBox(NULL,"'File' hat keinen Inhalt!","Error",MB_OK|MB_ICONERROR);
return 0;
}
}

fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
if(ferror(File))
{
MessageBox(NULL,"'FileHeader' konnte nicht gelesen werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
return 0;
}


fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);
if(ferror(File))
{
MessageBox(NULL,"'InfoHeader' konnte nicht gelesen werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
return 0;
}

fseek(File,o_Bitmap.FileHeader.bfOffBits,SEEK_SET);
if(ferror(File))
{
MessageBox(NULL,"Zu 'bfOffBits' konnte nicht gesprungen werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
return 0;
}

if(o_Bitmap.InfoHeader.biSizeImage != 0)
{
bitmap_Image=new unsigned char[o_Bitmap.InfoHeader.biSizeImage];
fread(bitmap_Image,o_Bitmap.InfoHeader.biSizeImage,1,File);
}

if(o_Bitmap.FileHeader.bfType != 0x4D42)
{
MessageBox(NULL,"Ungültiges Bildformat!","Error",MB_OK|MB_ICONERROR);
fclose(File);
}

if(!bitmap_Image)
{
free(bitmap_Image);
fclose(File);
}

if(bitmap_Image==NULL)
{
fclose(File);
}

switch(o_Bitmap.InfoHeader.biCompression)
{
case BI_RGB:
MessageBox(NULL,"Standardkompression ist aktiviert 'BI_RGB'!","Info",MB_OK);
break;

case BI_RLE4:
Load8BitKompression(Filename);
break;

case BI_RLE8:
Load24BitKompression(Filename);
break;

default:
MessageBox(NULL,"Keine Kompression vorhanden","Error",MB_OK|MB_ICONERROR);
}

fclose(File);
};

void Bitmap::Load8BitKompression(const char *Filename)
{
Bitmap o_Bitmap;
FILE *File=NULL;
MessageBox(NULL,"'BI_RLE4'-Kompression ist aktiviert!","Info",MB_OK);
File=fopen(Filename,"rb");
fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);

if(FileHeader.bfType != 8)
{
MessageBox(NULL,"8-Bitzustand konnte nicht gefunden werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
}

BPP=o_Bitmap.InfoHeader.biBitCount;
width=o_Bitmap.InfoHeader.biHeight;
height=o_Bitmap.InfoHeader.biHeight;
KompressionFormat=GL_RGB;

if(InfoHeader.biSizeImage != 0)
{
bitmap_Image=new unsigned char[o_Bitmap.InfoHeader.biSizeImage];
fread(bitmap_Image,o_Bitmap.InfoHeader.biSizeImage,1,File);
}

};

void Bitmap::Load24BitKompression(const char *Filename)
{
Bitmap o_Bitmap;
FILE *File=NULL;
MessageBox(NULL,"'BI_RLE8'-Kompression ist aktiviert!","Info",MB_OK);
File=fopen(Filename,"rb");
fread(&o_Bitmap.FileHeader,sizeof(BITMAPFILEHEADER),1,File);
fread(&o_Bitmap.InfoHeader,sizeof(BITMAPINFOHEADER),1,File);

if(o_Bitmap.FileHeader.bfType != 24)
{
MessageBox(NULL,"24-Bitzustand konnte nicht gefunden werden!","Error",MB_OK|MB_ICONERROR);
fclose(File);
}

BPP=o_Bitmap.InfoHeader.biBitCount;
width=o_Bitmap.InfoHeader.biHeight;
height=o_Bitmap.InfoHeader.biHeight;
KompressionFormat=GL_COLOR_INDEX;
};

So, i don't think that you can find an error or mistake. I hope you enjoy it and thanks for everything.

(Sry for my bad english, im german!)

Commodore
Nov 3rd, 2008, 06:06 AM
It still gives many error's .
I can generate a file whit it . But the file is not usefull.

Any idea how this can be ?

pivke
Nov 4th, 2008, 12:58 PM
So, there can be a few reasons. So, you have to create a Win32-Application. It doesn't work if you just include it. I don't exactly what problem you have, but the Includes you have to include are:
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <glaux.h>
#include <iostream>
#include <conio.h>

using namespace std;
#include "Bitmap.h"
The path of the OpenGL's libs mustn't be right, i installed them on "gl\...". Which Compiler [IDE] do you use? I can write for you in Win32-API or in DirectX if you want...just say (:

Maybe the problem is just you don't know how to do this all, i just uploaded the whole project for you, here: (You must have OpenGL for this (-;)
http://www.megaupload.com/de/?d=OE2VLS8J

Another way (I think it's better), youre registering on my site:
(Please don't erase !!Mods!!!)
http://www.Online-Programmierung.com

So just register under "Forum" and write your problem in it, i or some guys will help you. In this subforum aren't accepted a big quantity of posts ;-).

I hope i have helped you, so if there a few more questions or problems, just register ;-).

Commodore
Nov 5th, 2008, 05:59 AM
Yes i have used win-32 projects before.
But i didnt realise that i needed to make one for this program to.

When i make a win-32 one, to add the gl/.. thing i need to do something the configs.

At the linker thing, the input => additional dependencies => and here i must put some type for code.

I forgot what it was. Do U think u know what it is.

So far you helped me allot, and sorry for my english to. I'm Flemish.

Commodore
Nov 5th, 2008, 06:09 AM
And it doesnt work to register on the forum.
At the momment i"m not at home. So I will dwnload it later on.

Thx for all the help allready

pivke
Nov 5th, 2008, 07:55 AM
@Forum
I created a Account for you, I sended you the access dates per PM (Personal Message).

@To your problem
Sry, I have to write some candidacies. So, at this problem no time. Just rewrite with your acces dates and I will answer soon. Sry for all the problems ;-)

http://www.Online-Programmierung.com

Commodore
Nov 5th, 2008, 03:04 PM
@ forum

it doesnt seem to reconaise my account...

@ problem
i made a win-32 version to paste it in.
and i ajusted the properties so the gl\ should work
It seems that the path to the project is wrong.
I am using c++ express edition btw.


The program looks pretty good i must say.A fieuw translation things that i need to make and it's 100%

/thx

pivke
Nov 6th, 2008, 07:50 AM
@forum
Maybe you wont to post there, but ok....
@problem
You should try to compile this projectfile:
http://www.joachimrohde.com/cms/xoops/modules/articles/article.php?id=10
This must work, if it does'nt, we know that your problem is the OpenGL-Include...;-)

If it works, just put in the Headers in the main and create a new Header and copy in there the bitmapdecoder....And yea, you have to call the function in the main.

Commodore
Nov 6th, 2008, 04:47 PM
thanks!

I understand some german :D , but to read it, thats something ellse.
In belgium we have another 5 day holiday at the end of this i'll reply whit a report on if i get it to work.


But i'm sure i'll be able to lett it work so ;)

Commodore
Nov 12th, 2008, 06:41 AM
Hello again

Afther my long weekend i'm back on the work again.
I was wondering, could it be possible to make this in c# to ?

pivke
Nov 12th, 2008, 10:01 AM
Hey,
what do you mean, my Bitmapdecoder or the Tutorials on the site i gave you!?

Commodore
Nov 14th, 2008, 11:13 AM
no, the file i downloaded.
but got it to work now :)

At the moment i'm writing a rapport on all this.
and i'm looking for a way to generate a bmp file in c#

so that when i launch the program i would be able to say what the height is , the width and so . Even what colour.

i must say, i find that alot more difficult then reading one :)

pivke
Nov 14th, 2008, 03:16 PM
Hey, first of all, I'm not sure about youre saying me^^
So, I just looked on "wikipedia" for the article. You just have to look, what size the variables must have to load in there the Bitmapinformations. If you don't know what to do, you can cheat with my loader.

I think the c-compiler problems are a few commandos. You have to search them!

For example, the fread()-command:
http://www.google.de/search?hl=de&q=c+file+reading&btnG=Google-Suche&meta=
You know? You just have to look for the commands and to replace them.
I hope i helped you a lil bit...(:

The next weeks me and my mates want to erase the official site. We want to have a new, she's gonna be very good. So check it up, the next time ;-)

Good luck with the Bitmapdecoder.

Commodore
Nov 15th, 2008, 06:52 AM
the decoder works fine by now :)

and indeed, on wikipedia there is allot of information about bmp's.
Very usefull to. Be sure that when your new site is on to post a link here.
so i can have watch at it :). btw will it be in German to ? or in English.

pivke
Nov 16th, 2008, 03:35 AM
Hey (:
Cool, so it works.
The new site will have a phpbb-Forum, so you can switch between the English or Germanlanguage ;-)
I'll post the link here.

deepu8
Mar 10th, 2009, 01:35 AM
this is interesting but I want to know that is it work in turbo compiler also.
And which compiler spot that coding.

pivke
Mar 10th, 2009, 08:17 AM
Hey,

sry, I can't decrypt your question. When you want to know, if you can write this decoder in Turbo-Pascal, i think yes, it has to be. You just have to know, how to open binary-files and how to read in the data-blocks.

pivke

deepu8
Mar 12th, 2009, 01:32 AM
Hey,

sry, I can't decrypt your question. When you want to know, if you can write this decoder in Turbo-Pascal, i think yes, it has to be. You just have to know, how to open binary-files and how to read in the data-blocks.

pivke

I have tried it in turbo compiler it is not working in the turbo compiler..

pivke
Mar 12th, 2009, 03:45 AM
Are you're sure, that you have created the right project? You have to include some Headers. Please show me your Error-Log.

Tobiasgar
Sep 1st, 2011, 03:41 AM
Hi and Help! I can't open texture files when writing a OpenGL screen saver in windows envirnoment. Could anyone tell me how to load bitmap textures in screen saver programming??
:(