clc;clear all;close all;
fid=fopen( 't_2.bmp','rb');%此处为想要读取的文件名
bfType=fread(fid,2);
[s, errmsg] = sprintf('the file type:%s',char(bfType));
disp(s);
bfSize=fread(fid,4);
fsize=bfSize(4)*256^3+bfSize(3)*256^2+bfSize(2)*256+bfSize(1);
[s, errmsg] = sprintf('the file size:%d byte',fsize);
disp(s);
bfOffbits=fread(fid,8);
foffset=bfOffbits(8)*256^3+bfOffbits(7)*256^2+bfOffbits(6)*256+bfOffbits(5);
[s, errmsg] = sprintf('offset to start of pixel data:%d byte',foffset);
disp(s);
biSize=fread(fid,4);
isize=biSize(4)*256^3+biSize(3)*256^2+biSize(2)*256+biSize(1);
[s, errmsg] = sprintf('the size of infoheader:%d byte',isize);
disp(s);
infoRead=fread(fid,8);
biWidth=infoRead(4)*256^3+infoRead(3)*256^2+infoRead(2)*256+infoRead(1);
biHeight=infoRead(8)*256^3+infoRead(7)*256^2+infoRead(6)*256+infoRead(5);
[s, errmsg] = sprintf('width: %d \nheight:%d ',biWidth,biHeight);
disp(s);
infoRead=fread(fid,28);
biBitCount=infoRead(4)*256+infoRead(3);
biCompression=infoRead(8)*256^3+infoRead(7)*256^2+infoRead(6)*256+infoRead(5);
biSizeImage=infoRead(12)*256^3+infoRead(11)*256^2+infoRead(10)*256+infoRead(9);
biXPelsPerMeter=infoRead(16)*256^3+infoRead(15)*256^2+infoRead(14)*256+infoRead(13);
biYPelsPerMeter=infoRead(20)*256^3+infoRead(19)*256^2+infoRead(18)*256+infoRead(17);
biCirUsed=infoRead(24)*256^3+infoRead(23)*256^2+infoRead(22)*256+infoRead(21);
biCirImportant=infoRead(28)*256^3+infoRead(27)*256^2+infoRead(26)*256+infoRead(25);
[s, errmsg] = sprintf('the bit count: %d, color count: %d',...
biBitCount,2^biBitCount);
disp(s);
if biCompression==0
disp('no compression');
else if biCompression==1
disp('RLE8 compression');
else if biCompression==2
disp('RLE4 compression');
else disp('bit fields compression');
end
end
end
if biXPelsPerMeter~=0
[s, errmsg] = sprintf('horizontal resolution:%d pixels per meter',biXPelsPerMeter);
disp(s);
else disp('default horizontal resulution');
end
if biYPelsPerMeter~=0
[s, errmsg] = sprintf('verticular resolution:%d pixels per meter',biYPelsPerMeter);
disp(s);
else disp('default verticular resulution');
end
if biSizeImage~=0
[s, errmsg] = sprintf('image size: %d',biSizeImage);
disp(s);
else disp('No compression');
end
if biCirUsed~=0
[s, errmsg] = sprintf('%d color actually used',biCirUsed);
disp(s);
else disp('all colors are used');
end
if biCirImportant~=0
[s, errmsg] = sprintf('count of significant colors:%d',biCirImportant);
disp(s);
else disp('all colors are significant');
end
skip=mod((4-mod(((biWidth * biBitCount)/8),4)),4);
if ((biBitCount==24)&&(foffset==54))
for i=1:biHeight
if biHeight>0
indHeight=biHeight+1-i;
else indHeight=i;
end
for j=1:biWidth
colorRead=fread(fid,3);
I(indHeight,j,1)=colorRead(3);
I(indHeight,j,2)=colorRead(2);
I(indHeight,j,3)=colorRead(1);
end
fread(fid,skip);
end
else if(biBitCount==32)&&(bfOffbits==54)
for i=1:biHeight
if biHeight>0
indHeight=biHeight+1-i;
else indHeight=i;
end
for j=1:biWidth
colorRead=fread(fid,4);
I(indHeight,j,1)=colorRead(3);
I(indHeight,j,2)=colorRead(2);
I(indHeight,j,3)=colorRead(1);
end
fread(fid,skip);
end
else
LUT=fread(fid,(2^biBitCount)*4);
for i=1:biHeight
if biHeight>0
indHeight=biHeight+1-i;
else indHeight=i;
end
for j=1:biWidth
LUTIndex=fread(fid,1);
I(indHeight,j,1)=LUT(LUTIndex*4+3);
I(indHeight,j,2)=LUT(LUTIndex*4+2);
I(indHeight,j,3)=LUT(LUTIndex*4+1);
end
fread(fid,skip);
end
end
end
imshow(uint8(I)),title('Display BMP')
- 1
- 2
- 3
前往页