//这个程序用来读取256*256大小的单色bmp黑白图片,适当修改就可读取任意大小任意//颜色的bmp图片,在生成文件里,0和1表示黑白,不过用记事本查看会有乱码//可以用UE查看16进制形式,没有问题的#include <fstream>#include <iostream>using namespace std; typedef unsigned char var8;typedef unsigned int uvar32;typedef unsigned short int uvar16;typedef struct { var8 Blue,Green,Red,Reserved;} Palette;int main(){ uvar32 Size,Offset,Height,Width,Compress,Reserved, InfoHeaderSize,Colors,ImportantColors,DataSize,HResolution,VResolution; uvar16 Planes,Bit; int i,j; var8 type[2],imdata[256][256],imdata2[1024]; Palette Rgbquad; ifstream fin; ofstream fout; fin.open("test.bmp",ios::binary); /**//*以二进制读方式打开该文件,一定要二进制的!路径为存储图片路径*/ fout.open("out.txt", ios::out|ios::binary); //out.txt为保存结果文件,有趣的很,加上bmp数据,改了后缀就能显示了,我试过 if(!fin) { cout<<"No this file!/n"; return 1; } fin.read((char*)&type ,2); cout<<"file type: "<<type[0]<<type[1]<<endl; /**//* 两字节的内容用来识别位图的类型,偏移量0,结果file type: BM*/ fin.read((char*)&Size ,sizeof(uvar32)); cout<<"file size: "<<Size<<endl; /**//*整个文件的大小,偏移量2,结果file size: 8254*/ fin.read((char*)&Reserved,sizeof(uvar32)); cout<<"Reserved dword: "<<Reserved<<endl; /**//*保留,必须设置为0, 偏移量6,结果Reserved dword: 0*/ fin.read((char*)&Offset ,sizeof(uvar32)); cout<<"Offset: "<<Offset<<endl; /**//*从文件开始到位图数据开始之间的数据(bitmap data)之间的偏移量 ,结果Offset: 62。位图数据为256*256, 65536(位图数据)+62*8(偏移量)=(文件大小)记得是bit位*/ fin.read((char*)&InfoHeaderSize,sizeof(uvar32)); cout<<"Bitmap Info Header Size: "<<InfoHeaderSize<<endl; /**//*位图信息头(Bitmap Info Header)的长度,偏移量14, 结果 Bitmap Info Header Size: 40*/ fin.read((char*)&Width,sizeof(uvar32)); cout<<"Bitmap Width: "<<Width<<endl; /**//*位图的宽度,以象素为单位,偏移量18,Bitmap Width: 256*/ fin.read((char*)&Height,sizeof(uvar32)); cout<<"Bitmap Height: "<<Height<<endl; /**//*位图的高度,以象素为单位,如果该值是一个正数,说明图像是倒向的, 如果该值是一个负数,则说明图像是正向的。偏移量22,Bitmap Height: 256*/ fin.read((char*)&Planes,sizeof(uvar16)); cout<<"Bitmap Planes: "<<Planes<<endl; /**//*位图的位面数(注:该值将总是1),偏移量26,Bitmap Planes: 1*/ fin.read((char*)&Bit,sizeof(uvar16)); cout<<"Bits per Pixel: "<<Bit<<endl; /**//*每个象素的位数,偏移量28,单色应该1bit,Bits per Pixel: 1*/ fin.read((char*)&Compress,sizeof(uvar32)); cout<<"Compresssion Y or N: "<<Compress<<endl; /**//*压缩说明0为不压缩,偏移量30,Compresssion Y or N: 0*/ if(Compress) { fin.close();return 2;} fin.read((char*)&DataSize,sizeof(uvar32)); cout<<"Data Size: "<<DataSize<<endl; /**//*用字节数表示的位图数据的大小,偏移量34,256*256/8=8192, Data Size: 8192*/ fin.read((char*)&HResolution,sizeof(uvar32)); cout<<"HResolution: "<<HResolution<<endl; /**//*用象素/米表示的水平分辨率,偏移量38,HResolution: 0*/ fin.read((char*)&VResolution,sizeof(uvar32)); cout<<"VResolution: "<<VResolution<<endl; /**//*用象素/米表示的垂直分辨率,偏移量42,VResolution: 0*/ fin.read((char*)&Colors,sizeof(uvar32)); cout<<"Colors: "<<Colors<<endl; //fout<<Colors; /**//*位图使用的颜色数,应该为2,不过我试过很多次都输出0,不影响的*/ fin.read((char*)&ImportantColors,sizeof(uvar32)); cout<<"ImportantColors: "<<ImportantColors<<endl; /**//*指定重要的颜色数。当该域的值等于颜色数时(或者等于0时), 表示所有颜色都一样重要。偏移量50,ImportantColors: 0*/ for(i=0;i<2;i++) { fin.read((char*)&Rgbquad,sizeof(Palette)); cout<<int(Rgbquad.Blue)<<" "<<int(Rgbquad.Green)<<" "<<int(Rgbquad.Red)<<" "<<int(Rgbquad.Reserved)<<endl; } /**//*调色板规范。对于调色板中的每个表项,这4个字节用下述方法来 描述RGB的值:1字节用于蓝色分量 ,1字节用于绿色分量 ,1字节用于红色分量 1字节用于填充符(设置为0),对于彩色图像R,G,B 有各自的分量,对与黑白图像,R=G=B=灰度级。输出结果有 0 0 0 0, 255 255 255 0*/ fin.seekg(Offset,ios::beg); for( i=255;i>=0;i--) { for(j=0;j<32;j++) { fin.read((char*)&imdata[i][j],sizeof(var8)); fout<<imdata[i][j]; } } fout.close(); /**//*直接把8254个像素的灰度读入数组imdata中,由于前面调色板的格式,读出的数据无需 索引调色板。由于Height值大于零,图像倒置的,开始读入的数据是图像最后一行的灰度值。*/ return 0;}