[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

    Search the Q&A Archives

...read the BMP file through C

<< Back to general questions

Question by mottai
Submitted on 11/19/2003
Related FAQ: N/A
Rating: Rate this question: Vote
How to read the BMP file through C

Answer by w00t
Submitted on 2/12/2004
Rating:  Rate this answer: Vote
A BMP is made out of a header part and the pixels. The header contains basically 2 records with info about width, height etc.
These are the header record layouts:
typedef struct {
   unsigned short type;
   unsigned long size;
   unsigned short reserved1;
   unsigned short reserved2;
   unsigned long offsetbits;
typedef struct {
   unsigned long size;
   unsigned long width;
   unsigned long height;
   unsigned short planes;
   unsigned short bitcount;
   unsigned long compression;
   unsigned long sizeimage;
   long xpelspermeter;
   long ypelspermeter;
   unsigned long colorsused;
   unsigned long colorsimportant;
After these headers the pixels are written:
typedef struct {
   unsigned char blue;
   unsigned char green;
   unsigned char red;
You should open the bmp in binary and the rest is up to you (invert colors, darken/lighten colors etc).


Answer by Marius
Submitted on 6/6/2004
Rating:  Rate this answer: Vote

First of all, let me thank to w00t for the information!
Second let me contribute to this thread with a little about what I learned:

I had a problem with structure packing!
In VC60 the default is 8byte packing!
So when I built the program, the 14 byte BMPFileHeader was packed to 16 bytes (integer multiple of 8).

The structure consists of:
  * 2 bytes (unsigned short) type;
  * 4 bytes (unsigned long) size;
  * 2 bytes (unsigned short) reserved1;
  * 2 bytes (unsigned short) reserved2;
  * 4 bytes (unsigned long) offsetbits;

the first three components coult be pached in 8 bytes but the last two were packed as follows:

  * 2 bytes padding
  * 2 bytes (unsigned short) reserved2;
  * 4 bytes (unsigned long) offsetbits;

The proof is that when printing reserved2, it was 54 = 14+40, where 14=real sizeof(BMPFileHeader) and 40=real sizeof(BMPInfoHeader), so that the new reserved2 got over the old offsetbits!

To resolve the problem, either modify structure packing value from the Code Generation option of Project Settings (beware that settings are separate for Debug and Release versions!) or, much better, by inserting the following preprocessor code BEFORE the definition of the implied structures:

#pragma hdrstop
#pragma pack 2

  Of course, there should be also a test for VISUALC compiler... but I don't know how to do it! Hopefully, the program is a homework and the teacher tests it on MS VisualC. :D
   Also I could use something like:
#pragma hdrstop
#pragma pack (push,2)
//structure defs
#pragma pack (pop)

or something like that... but I didn't test it!

Have a nice coding! :D


Answer by loot
Submitted on 7/26/2005
Rating: Not yet rated Rate this answer: Vote
are you guys computer freaks or something


Answer by Gaurav
Submitted on 10/2/2005
Rating: Not yet rated Rate this answer: Vote
hi this how u can access the Bmp file header this is working fine..
Thanks to Maris.....for his valuable program code.
Here is the code.


#pragma pack(2) /*2 byte packing */
typedef struct
unsigned short int type;
unsigned int size;
unsigned short int reserved1,reserved2;
unsigned int offset;

#pragma pack() /* Default packing */
typedef struct
   unsigned int size;
   int width,height;
   unsigned short int bits;
   unsigned int compression;
   unsigned int imagesize;
   int xresolution,yresolution;
   unsigned int ncolors;
   unsigned int importantcolors;

void main()
   Header headfirst;
   Infoheader headsecond;
   FILE *fin,*fout;
   fin = fopen("d:/pic.bmp","rb+");
      printf("Error opening first file");


Answer by LeLorrain
Submitted on 1/7/2006
Rating: Not yet rated Rate this answer: Vote
Yoy can test for MS VC++ 6.00 with the pre-processor variable _MSC_VER which would be set to 1200. example:

#if defined (_MSC_VER) && (_MSC_VER == 1200)
// Change default packing to 2 instead of 8 in MS VC++ 6.00
#pragma hdrstop
#pragma pack (push,2)
    BITMAPINFOHEADER    bitmapInfoHeader;
    BITMAPFILEHEADER    bitmapFileHeader;    #if defined (_MSC_VER) && (_MSC_VER == 1200)
#pragma pack (pop)




Answer by Wezel
Submitted on 2/4/2006
Rating: Not yet rated Rate this answer: Vote
I have tried this kind of code before but I always get incorrect info, but cannot seem to find out what I do wrong.

Type :19778
Size :11
Reserved1 :0
Reserved2 :54

Size :240000000
Width :25165824
Height :65536
planes :24
Bitcount :0
Compression :1073741824
Sizeimage :13
Druk op een toets om door te gaan. . .

For example, how is the size expressed? The filesize is more or less 700KB so what is the connection between 240000000 and the real size?

Thank you at advance for your reply.


Answer by Apurba Saha
Submitted on 3/2/2006
Rating: Not yet rated Rate this answer: Vote
How i can make a BMP file from the Data(After Decoding the file)...I have Decode a monochrome BMP file and get the matrix(Width X Height).I want to chang the data manually and again place it in aBMP file.So that i can get a New BMP file with My own Data.It is too much urgent for my Image-Processing


Answer by cvatoi
Submitted on 5/5/2006
Rating: Not yet rated Rate this answer: Vote


Answer by CRYSTAL
Submitted on 10/2/2006
Rating: Not yet rated Rate this answer: Vote


Answer by MegaByte
Submitted on 1/19/2007
Rating: Not yet rated Rate this answer: Vote
How can I make 4 bytes into a long i am not programming in c but in gml i can read bytes as decimal intgers but how can i put them into a long?


Answer by Fren
Submitted on 2/3/2007
Rating: Not yet rated Rate this answer: Vote
This info was helpful me too thanks Marius and w00t


Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.

Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.


FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.


<< Back to general questions

[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.