#define WORD unsigned short
#define DWORD int
#define BYTE unsigned char
#include<iostream.h>
#include<fstream.h>
BYTE *store1,*store2,*store3,*store4;
struct BMPH{
WORD BMPId;
DWORD FileSize;
WORD Reserved1;
WORD Reserved2;
DWORD ImageOffset;
DWORD HeaderSize;
DWORD ImageWidth;
DWORD ImageHeight;
WORD EquipLevel;
WORD BitsPerPixel;
DWORD EncodeType;
DWORD ImageSize;
DWORD XPixelPerMeter;
DWORD YPixelPerMeter;
DWORD ColorUsed;
DWORD ColorImportant;
}bh;
void BMPHeadRead(ifstream fp)
{
fp.seekg(0,ios::beg);
fp.read((char*)(&bh.BMPId),2);
fp.read((char*)(&bh.FileSize),4);
fp.read((char*)(&bh.Reserved1),2);
fp.read((char*)(&bh.Reserved2),2);
fp.read((char*)(&bh.ImageOffset),4);
fp.read((char*)(&bh.HeaderSize),4);
fp.read((char*)(&bh.ImageWidth),4);
fp.read((char*)(&bh.ImageHeight),4);
fp.read((char*)(&bh.EquipLevel),2);
fp.read((char*)(&bh.BitsPerPixel),2);
fp.read((char*)(&bh.EncodeType),4);
fp.read((char*)(&bh.ImageSize),4);
fp.read((char*)(&bh.XPixelPerMeter),4);
fp.read((char*)(&bh.YPixelPerMeter),4);
fp.read((char*)(&bh.ColorUsed),4);
fp.read((char*)(&bh.ColorImportant),4);
}
void ReadData1(ifstream fp)
{
BYTE *lpTempBuf=new BYTE[bh.ImageHeight*bh.ImageWidth*(bh.BitsPerPixel/8)];
store1=lpTempBuf;
int ExtCount=(4-bh.ImageWidth%4)%4;
fp.seekg(bh.ImageOffset,ios::beg);
for(int i=0;i<bh.ImageHeight;i++)
{
for(int j=0;j<bh.ImageWidth;j++)
{
for(int k=0;k<(bh.BitsPerPixel/8);k++)
{
fp.read(lpTempBuf,1);
lpTempBuf++;
}
}
fp.seekg(ExtCount,ios::cur);
}
}
void ReadData2(ifstream fp)
{
BYTE *lpTempBuf=new BYTE[bh.ImageHeight*bh.ImageWidth*(bh.BitsPerPixel/8)];
store2=lpTempBuf;
int ExtCount=(4-bh.ImageWidth%4)%4;
for(int i=0;i<bh.ImageHeight;i++)
{
for(int j=bh.ImageWidth-1;j>=0;j--)
{
fp.seekg(bh.ImageOffset+(i*bh.ImageWidth+j)*(bh.BitsPerPixel/8),ios::beg);
for(int k=0;k<(bh.BitsPerPixel/8);k++)
{
fp.read(lpTempBuf,1);
lpTempBuf++;
}
}
fp.seekg(ExtCount,ios::cur);
}
}
void ReadData3(ifstream fp)
{
BYTE *lpTempBuf=new BYTE[bh.ImageHeight*bh.ImageWidth*(bh.BitsPerPixel/8)];
store3=lpTempBuf;
int ExtCount=(4-bh.ImageWidth%4)%4;
for(int i=bh.ImageHeight-1;i>=0;i--)
{
for(int j=0;j<bh.ImageWidth;j++)
{
fp.seekg(bh.ImageOffset+(i*bh.ImageWidth+j)*(bh.BitsPerPixel/8),ios::beg);
for(int k=0;k<(bh.BitsPerPixel/8);k++)
{
fp.read(lpTempBuf,1);
lpTempBuf++;
}
}
fp.seekg(ExtCount,ios::cur);
}
}
void ReadData4(ifstream fp)
{
BYTE *lpTempBuf=new BYTE[bh.ImageHeight*bh.ImageWidth*(bh.BitsPerPixel/8)];
store4=lpTempBuf;
int ExtCount=(4-bh.ImageWidth%4)%4;
for(int i=bh.ImageHeight-1;i>=0;i--)
{
for(int j=bh.ImageWidth-1;j>=0;j--)
{
fp.seekg(bh.ImageOffset+(i*bh.ImageWidth+j)*(bh.BitsPerPixel/8),ios::beg);
for(int k=0;k<(bh.BitsPerPixel/8);k++)
{
fp.read(lpTempBuf,1);
lpTempBuf++;
}
}
fp.seekg(ExtCount,ios::cur);
}
}
void BMPWrite(char * filename,BYTE *lpLine)
{
ofstream BMPFile;
int ExtCount=(4-bh.ImageWidth%4)%4;
static BYTE zero=0;
BMPFile.open(filename,ios::binary);
BMPFile.seekp(0,ios::beg);
BMPFile.write((char*)(&bh.BMPId),2);
BMPFile.write((char*)(&bh.FileSize),4);
BMPFile.write((char*)(&bh.Reserved1),2);
BMPFile.write((char*)(&bh.Reserved2),2);
BMPFile.write((char*)(&bh.ImageOffset),4);
BMPFile.write((char*)(&bh.HeaderSize),4);
BMPFile.write((char*)(&bh.ImageWidth),4);
BMPFile.write((char*)(&bh.ImageHeight),4);
BMPFile.write((char*)(&bh.EquipLevel),2);
BMPFile.write((char*)(&bh.BitsPerPixel),2);
BMPFile.write((char*)(&bh.EncodeType),4);
BMPFile.write((char*)(&bh.ImageSize),4);
BMPFile.write((char*)(&bh.XPixelPerMeter),4);
BMPFile.write((char*)(&bh.YPixelPerMeter),4);
BMPFile.write((char*)(&bh.ColorUsed),4);
BMPFile.write((char*)(&bh.ColorImportant),4);
BMPFile.seekp(bh.ImageOffset,ios::beg);
for(int i=0;i<bh.ImageHeight;i++)
{
for(int j=0;j<bh.ImageWidth;j++)
{
for(int k=0;k<(bh.BitsPerPixel/8);k++)
{
BMPFile.write(lpLine,1);
lpLine++;
}
}
for( j=0;j<ExtCount;j++)
BMPFile.write(&zero, 1);
}
BMPFile.close();
}
class readf
{
public:
readf()
{
ifstream fp("F:\\课程设计http://www.cnblogs.com/ujnlu/admin/file://bgimg.bmp%22,ios::binary/);
BMPHeadRead(fp);
cout<<" BMP文件的大小为:"<<bh.FileSize/8<<"字节"<<endl;
cout<<" BMP文件的颜色数为:"<<bh.BitsPerPixel/8<< " 字节"<<endl;
ReadData1(fp);
ReadData2(fp);
ReadData3(fp);
ReadData4(fp);
BMPWrite("F:\\课程设计http://www.cnblogs.com/ujnlu/admin/file://store//原图像同步复制.bmp",store1);
BMPWrite("F:\\课程设计http://www.cnblogs.com/ujnlu/admin/file://store//原图像左右翻转.bmp",store2);
BMPWrite("F:\\课程设计http://www.cnblogs.com/ujnlu/admin/file://store//原图像前后翻转.bmp",store3);
BMPWrite("F:\\课程设计http://www.cnblogs.com/ujnlu/admin/file://store//原图像对角翻转.bmp",store4);
fp.close();
}
~readf()
{
}
};
int main()
{
readf picfile;
return 0;
}
------------------------