bc... []

BMP.cpp ( File view )

  • By shanzhu 2015-10-16
  • View(s):11
  • Download(s):1
  • Point(s): 1
			#include"picture.h"

//-----------读取位图------------
int BMP::picread(const char *filename)
{
	
	ifstream pic(filename,ios::binary);
	if(pic.fail())
	{

		cout << "open failed!" << endl;
		exit(-1);
	
}

	//读取BMP文件信息
	pic.read((char *)&head.bfType,sizeof(head.bfType));
	pic.read((char *)&head.bfSize,sizeof(head.bfSize));
	pic.read((char *)&head.bfReserved1,sizeof(head.bfReserved1));
	pic.read((char *)&head.bfReserved2,sizeof(head.bfReserved2));
	pic.read((char *)&head.bfOffBits,sizeof(head.bfOffBits));
	pic.read((char *)&info.biSize,sizeof(info.biSize));
	pic.read((char *)&info.biWidth,sizeof(info.biWidth));
	pic.read((char *)&info.biHeight,sizeof(info.biHeight));
	pic.read((char *)&info.biPlanes,sizeof(info.biPlanes));
	pic.read((char *)&info.biBitCount,sizeof(info.biBitCount));
	pic.read((char *)&info.biCompression,sizeof(info.biCompression));
	pic.read((char *)&info.biSizeImage,sizeof(info.biSizeImage));
	pic.read((char *)&info.biXPelsPerMeter,sizeof(info.biXPelsPerMeter));
	pic.read((char *)&info.biYPelsPerMeter,sizeof(info.biYPelsPerMeter));
	pic.read((char *)&info.biClrUsed,sizeof(info.biClrUsed));
	pic.read((char *)&info.biClrImportant,sizeof(info.biClrImportant));

	//输出BMP文件信息
	char bf_Type[3]="";
	bf_Type[0]=head.bfType&0xFF;
	bf_Type[1]=head.bfType>>8&0xFF;
	cout << "文件类型:" << bf_Type << endl;
	cout << "位图文件大小:" << head.bfSize << endl;
	cout << "位图数据开头:" << head.bfOffBits << endl;
	cout << "文件头大小:" << info.biSize << endl;
	cout << "图象的宽度:" << info.biWidth << endl;
	cout << "图象的高度:" << info.biHeight << endl;
	cout << "比特数/象素:" << info.biBitCount << endl;
	cout << "数据压缩类型:" << info.biCompression << endl;
	cout << "图象的大小:" << info.biSizeImage << endl;
	cout << "水平分辨率:" << info.biXPelsPerMeter << endl;
	cout << "垂直分辨率:" << info.biYPelsPerMeter << endl;
	cout << "颜色索引数:" << info.biClrUsed << endl;
	cout << "重要颜色索引数:" << info.biClrImportant << endl;

	//---------初始化BMP数据,写入RGB临时文件----------
	w=info.biWidth;
	h=abs(info.biHeight);
	org_data=new BYTE[h*w*3];
	int line_size=(((w*3+3)>>2)<<2);
	switch(info.biCompression)
	{

	case BI_RGB:							//没有压缩时
		//pic.seekg(head.bfOffBits,ios::beg);	//转到位图数据开头
		
		//图像实际每行大小(字节)
		cout << "每行实际长度:" << line_size/3 << "像素" << endl;

		if(info.biBitCount==24)				//24位图
		{

			for(unsigned short int i=0;i<h-1;++i)//每次读取一行图像数据
			{

				pic.read((char *)(org_data+i*w*3),line_size);
			
}
			pic.read((char *)(org_data+(h-1)*w*3),w);
								//最后一行只读w个值
		
}
		else if(info.biBitCount==16)	//16位图
		{

			//16位图每种颜色只占5位,不足一字节
			char t;						//将每种颜色补足一字节
			unsigned short (*line_data)=new unsigned short[line_size/2];
										//创建一行图像大小的空间,三色合为两字节
			for(unsigned short int i=0;i<h;++i)
										//每次读取一行图像数据
			{

				pic.read((char *)line_data,line_size);
				for(unsigned short int j=0;j<w;++j)
				{

					//用位运算计算每种颜色的数值
					t=line_data[j*2]&32;
					org_data[i*w*3+j*3]=t;
					t=line_data[j*2]>>5&32;
					org_data[i*w*3+j*3+1]=t;
					t=line_data[j*2]>>10&32;
					org_data[i*w*3+j*3+2]=t;
				
}
			
}
			delete[] line_data;
		
}
		else if(info.biBitCount==8)			//8位图
		{

			//8位图使用小于256种颜色的调色板
			pic.seekg(sizeof(head)+sizeof(info),ios::beg);
											//转到调色板开头
			quad=new RGBQUAD[info.biClrUsed];
											//创建调色板所需空间
			BYTE(*line_data)=new BYTE[line_size];//创建一行图像大小的空间
			pic.read((char *)quad,info.biClrUsed);
											//读取调色板
			pic.seekg(head.bfOffBits,ios::beg);//转到位图数据开头
			for(unsigned short int i=0;i<h;++i)
											//每次读取一行图像数据
			{

				pic.read((char *)line_data,line_size);
				for(unsigned short int j=0;j<w;++j)
				{

					//将每个像素对应的调色板颜色写入RGB临时文件
					org_data[i*w*3+j*3]=quad[line_data[j]].blue;
					org_data[i*w*3+j*3+1]=quad[line_data[j]].green;
					org_data[i*w*3+j*3+2]=quad[line_data[j]].red;
				
}
			
}
			delete[] line_data;
			delete[] quad;
		
}
		//保存,关闭RGB临时文件
		pic.close();
		break;
	default:
		pic.close();
		break;
	
}
	if(choose==4)
	{

		int w2=w*3*2;
		fix_data=new  BYTE[h*w2*3];
	
}
	else
	{

		fix_data=new  BYTE[h*w*3];
	
}
	return 0;

}

//-----------保存位图(默认24位BI_RGB)------------
int BMP::picwrite(const char *filename)
{

	ofstream pic(filename,ios::binary);
	if(pic.fail())
	{

		cout << "open failed!" << endl;
		exit(-1);
	
}
	cout << "-------------\n保存图像..." << endl;

	//计算图像信息
	info.biWidth=w
...
...
(Please download the complete source code to view)
			
...
Expand> <Close

Want complete source code? Download it here

Point(s): 1

Download
0 lines left, continue to read

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
picture.cpp5.94 kB24-05-12|12:27
picture.h4.80 kB24-05-12|10:52
BMP.cpp7.40 kB27-05-12|00:21
main.cpp670.00 B24-05-12|12:25
...

BMP.cpp (6.43 kB)

Need 1 point
Your Point(s)

Your Point isn't enough.

Get point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more point

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D