Read the Header of bitmap

Hi can anyone guide me how to open a bitmap or bmp file header ? I have opened in hex editor but I need to open it by using matlab ?please guide

11 Comments

um i tried fopen and fread . I got the header file but it is in characters form.Since its hexadecimal the string has values like 1A 4D .so if we try str2num () i get an empty matrix. Secondly the header has a file like this '2334455' if i change it into numeric by str2num it will give a single number 2334455.But i need someting like this [22 33 44 55].since in bitmap every byte has two values.a snipshot of header and conversion is attached
You need hex2dec (and you can use reshape to make sure each pair of characters is interpreted as 1 value).
You should always try to avoid str2num (use str2double instead), as it calls eval and is therefore vulnerable to malicious input.
I completely forgot about reshape command thank you very much . Tha k you for your advice
Use a %2x format when reading the data
Note: actual BMP files are not stored as hex characters.
filename = fullfile(matlabroot, 'toolbox/matlab/winfun/mwsamp/mwsampctl.bmp');
fid = fopen(filename);
b64 = fread(fid, [1 64], '*uint8');
fclose(fid);
disp(b64(1:3))
66 77 238
fprintf('%02x ', b64);
42 4d ee 00 00 00 00 00 00 00 76 00 00 00 28 00 00 00 10 00 00 00 0f 00 00 00 01 00 04 00 00 00 00 00 78 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80 00 00 80
fprintf('\n');
fprintf(' %c ', b64);
B M î v ( x
fprintf('\n');
The 42 4d is a representation of what is in the file. The first byte in the file is decimal 66, which is hex 42, and is the character code for 'B' . The file does not have the characters '424dee' stored in it: it starts with what would be represented in decimal as 66 77 238
I'm sorry I didn't quite understand your point . Are you saying that all of these values of the header are hexadecimal ? Or are you referring to the first 2 bytes which are identification of file
No. None of the values in the header are hexadecimal. BMP files are binary files. They are stored as a sequence of 8-bit bytes, in binary. But we cannot see binary directly, so we have to convert it to something we can see and understand.
The BMP file that I show the beginning of, above, mwsampctl.bmp starts with some binary values 01000010 01001101 11101110 . The decimal equivalents of those are 66 77 238 and the hex equivalents are 42 4d ee .
The snapshot you posted, on the other hand, the file you indicate you want to process, starts with the binary representation of an apostrophe, and then has the binary representation of the character for zero repeated a number of times on the first row.
I = char("'00");
char(strjoin(string(dec2bin(I+0,8))))
ans = '00100111 00110000 00110000'
char(strjoin(string(I+0)))
ans = '39 48 48'
char(strjoin(string(dec2hex(I+0))))
ans = '27 30 30'
so the binary form of the file you show starts with 00100111 0010000 00110000 which is decimal 39 48 48 and hex 27 30 30 . Notice those 00 in the file are not binary 00000000 : the 00 in what you showed is not a file storing binary 0: it is a file storing the character '0' . The character '0' happens to be encoded as decimal 48 (there are historic reasons why character positions 0 to 32 do not have any printable representations. Binary 0 was literally no voltage, and if you left the line with no voltage for a while because you had nothing to send, you would not want to be translating that to a printable character.)
It is true that there is a sense in which your file is binary as well, but what that binary is encoding is the character represetnation of hexadecimal -- the difference between binary zeros and the character '0' to 'f' used to represent hexadecimal for human understanding.
BMP files do not store anything in hexadecimal for human understanding. BMP files store everything in binary. The bits are the reality for BMP files. When humans want to examine the file, they need to represent the bits in a way that humans can understand. That might include converting the binary to hexadecimal for display purposes... but what you are working with in that snapshot is somethat that was already converted from bits into the character representation of hexadecimal. The '32ff' you see at the beginning of one of those lines is the binary representation of an apostrophe and then of the character '3' and then of the character '2' and so on -- whereas a true BMP file would have bits at that point that encode decimal 50 then decimal 255...
Right so if I use your code I will have the header in decimal values ? Another thing is it possible to save the bmp file as a variable by using fopen (without using imread)?
Can I ask why you want to avoid imread?
Walter Roberson
Walter Roberson on 11 Nov 2021
Edited: Walter Roberson on 11 Nov 2021
I am saying that if you were reading from a real bmp file then using fread with '*uint8' would give you back numeric values. You could display them in decimal if you want.
But the snapshot you posted is not a real bmp file: it is part of a hexadecimal dump of a bmp file. You need to decide which kind of file you need to process.
Consider the numeric value denoted by the word sixty-six. That is an abstract value that exists by itself independent of representation. It can be represented in multiple ways. One of the ways is 66, which is the character 6 followed by the character 6. Another way is 01000010 which is character 0 followed by character 1 then several character 0 and so on. Another way is 42 which is the character 4 followed by the character 2.
But in computer languages the character 0 is not the same as the numeric value zero. The character 0 is represented by the numeric value forty-eight, the character 1 is represented by the numeric value forty-nine and so on.
A real BMP file starts with a byte with numeric value sixty-six. Which happens to be also be the numeric value used to encode the character B (this was deliberate.)
But the file you posted a snapshot of... it starts with numeric value thirty-nine. Which is the same as the numeric value used to encode apostrophe. And then your file has numeric value forty-eight. Which is the encoding of the character 0, not the numeric value zero.
Someone has taken a binary file and converted it to characters that represent the hexadecimal form.
Consider that the numeric value one hundred and twenty three can be stored in a single byte: you could store it in a file that is a single byte. Or you could store it in a file as the sequence of characters 1 2 3 (which would be numeric values forty-nine fifty fifty-one inside the file) which would be a file of length three bytes. Or you could store it as the sequence of characters 7 B which would be numeric values fifty-five sixty-six in the file, a file of length two bytes. And the file you showed us would not use the single numeric value one hundred and twenty three: it would use the characters 7B (well, 7b really but I do not have the character code for b memorized. Ninety-eight I think it would be)
I wasn't trying to read the bmp file rather I was trying to open the header of the bmp file . I used unit8 which you mentioned earlier but that returns the image data . But do you know how we can load coloured/RGB image by using fread I tried by using Fread(file name ,[520 520],unit8 ,'b') RGB image size is 520*520*3 The 3 saves the bytes of colour of the pixel .since it is not used in size it gives grey image Any idea how to solve this proble

Sign in to comment.

 Accepted Answer

clc; clear all; close all;
bmp = fopen('common_demos.bmp','rb');
type = fread(bmp,2,'char')
type = 2×1
66 77
bmpsize = fread(bmp,1,'long')
bmpsize = 12656
bfReserved1and2 = fread(bmp,1,'long')
bfReserved1and2 = 0
bfOffBits = fread(bmp,1,'long')
bfOffBits = 54
biSize = fread(bmp,1,'long')
biSize = 40
biWidth = fread(bmp,1,'long')
biWidth = 66
biHeight = fread(bmp,1,'long')
biHeight = 63
biPlanes = fread(bmp,1,'short')
biPlanes = 1
biBitCount = fread(bmp,1,'short')
biBitCount = 24
biCompression = fread(bmp,1,'long')
biCompression = 0
biSizeImage = fread(bmp,1,'long')
biSizeImage = 12602
biXPelsPerMeter = fread(bmp,1,'long')
biXPelsPerMeter = 2834
biYPelsPerMeter = fread(bmp,1,'long')
biYPelsPerMeter = 2834
biClrUsed = fread(bmp,1,'long')
biClrUsed = 0
biClrImportant = fread(bmp,1,'long')
biClrImportant = 0
fclose(bmp);

3 Comments

Tina
Tina on 11 Nov 2021
Edited: Tina on 11 Nov 2021
Thank you so much. i didnt knew we can use fopen('rb') thank you so much
'rb' is not documented. The 'b' will be ignored. Using 'r' is equivalent.
People mistakenly think they need to use 'rb' because in C you would use 'rb' for binary and 'r' for text files. In MATLAB it is 'r' for binary, and 'rt' for text files.
Oh okay . Thank you

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Release

R2019a

Asked:

on 11 Nov 2021

Edited:

on 11 Nov 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!