can´t read that (bitmap) file header

Started by
8 comments, last by LongJohn 21 years, 3 months ago
hi,i have collected a lot of information about this bitmap file format the last days,concerning the BITMAPFILEHEADER and the BITMAPFILEINFOHEADER. i just can´t extract the values... i´, using slackware linux with gcc 3.2,trying to code only c++. the following should,ask for the bitmap-file-name,check if it´s there,the read the first 2 entrys -wich are always BM as meant to be-, switch to the 11 byte,read the next 4 and give me back the offbits value..but it gives nothing ! could u tell me 1)how can i read each value from the headers 2)i can not take int or long for bfoffbits i tried bothe file.read anf file.get..: #include #include using namespace std; int main() { char filename[15]; char Texturename[15]; ifstream file; //This is my file-handle //const char BITMAPFILEHEADER[20]; cout << "Texturname ? "; cin >> Texturename; strcpy (filename, Texturename); //make texturename "comparable" file.open(filename); //open it /*checking if file is there,if not exit prog */ if(file.is_open()) { cout << "could open the file \n"; } else { cout << "can not open file\n "; return 0; } cout << "filecheck passed \n" << "reading BITMAPFILEHEADER \n "; char ersterteil[2]; file.read(ersterteil,2); cout << "\n the 2 chars from file.read() :" << ersterteil << ":----------\n"; char bfoffbits[4]; file.seekg(11, ios::beg); file.read(bfoffbits,4); cout << "\nhere comes bfoffbits :" << bfoffbits << ":------------\n"; }
Advertisement
Good god man your making this more difficult than it has to be.


typedef struct tagBITMAPFILEHEADER
{ // bmfh
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;

This is a copy of what a bitmap file header looks like in MSDN. Now because your on linux this isn''t a default definition....SO copy and paste this structure into your code.

Now you can do the following.

BITMAPFILEHEADER myFileHeader;

FILE *fp = fopen("filename.bmp","rb";
fread((void*)&myFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
fclose(fp);

All done you just read in the file''s header. You should also be able to do this in C++ like this:


file.open(filename); //open it
file.read(myFileHeader,sizeof(BITMAPFILEHEADER));
thx but:
even after changing word <=> unsigned int
dword <=> usingned long
it 1) doesn´t compile because there is no sizeof, i tried
size():
file.read(headersize,size(BITMAPFILEHEADER) );
but this gives me a :
bitmap.cpp: In function `int main()'':
bitmap.cpp:60: parse error before `)'' token


and NO there´s nothing wrong before or after.

finally there MUST be a way to do this on my oqn without cut and paste PLEEEASE help me.
What? No sizeof() operator? This is standard in all C++, its a keyword operator, I believe. Sizeof returns the number of bytes in a structure, so if you know it ahead of time, feel free to use a numerical value, but I am almost certain that you should be guarenteed a sizeof() operator- its standard.

Brendan
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
yes,there is a sizeof but it doesn´t give out anything.

can really nobody show a small peace of c++ readind out these damn first 14 bytes .. ?
i don´t want to cut and paste because this will end in more stupid newbie questions in forums like this one..
Firstly... word != unsigned int.. word = unsigned short. An int can be 16 or 32-bits, while a short may ONLY be 16-bits, and a long is always 32-bits. Use those for stuff like this. Second, exactly what are you trying to do? If you shoot me an email @ BillyB@mrsnj.com I can give you my bitmap class that loads bitmaps of any bitdepth, and writes them out as 24-bit bitmaps, has the entire header as a single struct, automagically allocates memory, etc. It really depends on what exactly you want to know about these bitmaps.



  typedef struct tagBITMAPFILEHEADER { // bmfh unsigned short bfType;unsigned long bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigned long bfOffBits; } BITMAPFILEHEADER;/* short + long + short + short + long 2 + 4 + 2 + 2 + 4 = 14 bytes.*/BITMAPFILEHEADER bmfh;char Read_Header(char *fName){ FILE *in; in = fopen(fName,"rb"); fread(&bmfh,1,14,in); //read in our bitmapfileheader, 14 bytes./*sizeof(BITMAPFILEHEADER) should be 14, so you might want to double check that!*/ fclose(in);}  


Billy - BillyB@mrsnj.com
unsigned short..does that even exist in linux/gcc 3.2 ?
a) your problem is not sizeof(), but the BITMAPFILEHEADER type. It''s a Windows defined structure, and doesn''t exist under Linux.

b) Define the BITMAPFILEHEADER structure manually, as shown by the previous AP.

c) unsigned short is a fundamental C/C++ type, it is defined by the standard, and will work on any C/C++ compiler.

d) Moved to beginners forum
yes,but why should i define the whole bitmapfileheader if i want the values on their own ?

quote:Original post by LongJohn
yes,but why should i define the whole bitmapfileheader if i want the values on their own ?



Because it''s easier and faster than reading in each individual variable and skipping others to get the needed data. After you do the fread or in << statement you should be able to access each individual variable in the myBitmapFileInfoHeader just by typing in myBitmapFileInfoHeader.(enter structure variable here).

It is the simplest cleanest way to read in these values. There is absolutely no reason to create more work for yourself trying to solve data alignment issues, and selectively read in the individual variables in a structure such as the bitmapfileinfo header which has been saved out to a file.

This topic is closed to new replies.

Advertisement