You are here:

C++/BMP in c++

Advertisement


Question
hi
i am a university student and i want to Write a C/C++ program that extracts the width and height of images with BMP file
format.I've searched alot in web & i found some codes .but they were too complicated .plese help me to write this program.i have no idea what to do
thanks alot

Answer
Hello Andishe.

You need to know the format of the bitmap file. I assume you are asking about the windows bitmap format although I believe the same format is used on many types of computers. The article at http://en.wikipedia.org/wiki/Windows_bitmap shows the file format.

Below is a hex representation of a bitmap file which I made using Microsoft Paint. It is best viewed in a courier font. I will refer to it below.

00000000: 424d 36bb 1700 0000 0000 3600 0000 2800  BM6.......6...(.
00000010: 0000 c003 0000 1c02 0000 0100 1800 0000  ................
00000020: 0000 00bb 1700 0000 0000 0000 0000 0000  ................
00000030: 0000 0000 0000 ffff ffff ffff ffff ffff  ................
00000040: ffff ffff ffff ffff ffff ffff ffff ffff  ................
00000050: ffff ffff ffff ffff ffff ffff ffff ffff  ................
00000060: ffff ffff ffff ffff ffff ffff ffff ffff  ................
00000070: ffff ffff ffff ffff ffff ffff ffff ffff  ................
00000080: ffff ffff ffff ffff ffff ffff ffff ffff  ................
00000090: ffff ffff ffff ffff ffff ffff ffff ffff  ................
...

According to the wikipedia  article, the BMP file start with a 14 byte header. The first 2 bytes are ASCII characters.
The following entries are possible:

   * BM - Windows 3.1x, 95, NT, ... etc
   * BA - OS/2 Bitmap Array
   * CI - OS/2 Color Icon
   * CP - OS/2 Color Pointer
   * IC - OS/2 Icon
   * PT - OS/2 Pointer

If the first 2 bytes are not one of those, you don't have a valid bitmap file. The example above starts with BM.

Notice the 15'th byte, it is hex 28 or decimal 40. That is the start of the header and also the size of the header. The number indicates that the header is a windows version 3 header. It is the most common. If your bitmap is of some other type, you will have to determine the format of the header. The size of the header is actually 4 bytes long, with the byte at offset 0E being the least significant. The size continues through bytes 0F, 10, 11.

The next 4 bytes of the V3 header (C0 03 00 00) is the bitmap width, and the following 4 bytes (1C 02 00 00) are the bitmap height.

This bitmap is 960 Pixels wide by 540 high.
960 in hex is 00 00 03 C0
540 in hex is 00 00 02 1C

So you can see that the header stores the least significant bytes first.

Now the program to read the size. I will assume that I'm dealing with a V3 header. You can adapt the program to examine other bytes to determine your header type. Don't forget to check for errors.

#include <stdio.h>

int get4byteInteger(FILE* fp)
{
   int byte;
   int value;
   byte = fgetc(fp);
   value = byte;
   byte = fgetc(fp);
   value |= byte << 8;
   byte = fgetc(fp);
   value |= byte << 16;
   byte = fgetc(fp);
   value |= byte << 24;

   return value;
}

int main(void)
{
   FILE* fp = fopen("untitled.bmp", "r");
   fseek(fp, 0x12, SEEK_SET);

   int width = get4byteInteger(fp);
   int height = get4byteInteger(fp);
   printf("The width is %d, the height is %d\n", width, height);

   return 0;
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.