⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmp_file.cpp

📁 实时监控
💻 CPP
字号:
/*
 *	描述:	Bitmap 文件功能封装 .h文件
 *	模块:	NLib
 *	作者:	Mr.Nodman.
 *	时间:	2003-6-18
 *	依靠:	MFC
 *	版本:	1.0 测试
 *	历史:	2003-6-18 初步完成
 */

#include "bmp_file.h"
#include "dib_draw.h"

template<class T>
int init_struct(T& t)
{
	ZeroMemory(&t, sizeof(T));
	return sizeof(T);
}

//////////////////////////////////////////////////////////////////////////
void bmp_file::init()
{
	bfh.bfSize = init_struct(bfh);
	bih.biSize = init_struct(bih);
	bih.biPlanes = 1;
}

/// deprecated
bool bmp_file::load_from_res(UINT idb)
{
	CBitmap bmp;
	if( !bmp.LoadBitmap(idb) )
		return false;

	BITMAP bm;
	bmp.GetBitmap(&bm);

	init();
	bfh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
	bfh.bfSize = 0;
	bfh.bfType = MAKEWORD('B', 'M');

	bih.biBitCount = bm.bmBitsPixel;
	bih.biWidth = bm.bmWidth;
	bih.biHeight = bm.bmHeight;
	bih.biCompression = 0;

	data.alloc(bm.bmWidthBytes*bm.bmHeight);

	bmp.GetBitmapBits(bm.bmWidthBytes*bm.bmHeight, data);

	v_invert();
//	memcpy((byte*)data, bm.bmBits, bm.bmWidthBytes*bm.bmHeight);

	return true;
}

bool bmp_file::load_from_file(LPCTSTR filename)
{
	DWORD size = 0;

	CFile file;
	if( !file.Open(filename, CFile::modeRead | CFile::typeBinary) )
		return false;

	file.Read(&bfh, sizeof(bfh));
	file.Read(&bih, sizeof(bih));
	
	if( bih.biSizeImage )
		size = bih.biSizeImage;
	else
		size = bih.biWidth*bih.biHeight*bih.biBitCount/8;
	data.alloc(size);
	file.Read(data, size);

	return true;
}

bool bmp_file::save_to_file(LPCTSTR filename, int w, int h, 
							DWORD fourcc, int bitcount, int pixelstride, void* data,
							bool invert )
{
	init();

	bih.biBitCount = bitcount;
	bih.biCompression = fourcc;
	bih.biWidth = w;
	bih.biHeight = h;
	bih.biSizeImage = w*h*bitcount/8;

	bfh.bfType = MAKEWORD('B', 'M');
	bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	bfh.bfSize = bfh.bfOffBits + bih.biSizeImage;

	CFile file;
	if( !file.Open(filename, CFile::modeCreate | CFile::modeWrite))
		return false;

	file.Write(&bfh, sizeof(bfh));
	file.Write(&bih, sizeof(bih));
	
	if (invert)  {
		if (bitcount == pixelstride)  {
			for (int i=0; i<h; i++)  {
				int pitch = w*bitcount/8 ;
				byte* d = ((byte*)data)+(h-i-1)*pitch ;
				file.Write(d, pitch) ;
			}
		}
		else  {
			int  bytes  = bitcount/8 ;
			int  stride = pixelstride/8 ;
			int  pitch  = w*stride ;
			for (int i=0; i<h; i++)  {
				byte* d = ((byte*)data)+(h-i-1)*pitch ;
				for (int j=0; j<w; j++)  {
					file.Write(d, bytes) ;
					d+= stride ;
				}
			}
		}
	}
	else
		file.WriteHuge(data, w*h*bitcount/8);
	file.Close();
	return true;
}

void bmp_file::draw(CWnd* site)
{
	dib_draw dd;
	if( !dd.create(site, bih.biWidth, bih.biHeight, bih.biCompression, bih.biBitCount) )
		return;

	dd.draw(data);
	dd.destroy();
}

void bmp_file::draw(CDC* pdc, CRect rc)
{
	dib_draw dd;
	dd.create(NULL, bih.biWidth, bih.biHeight, bih.biCompression, bih.biBitCount);
	dd.draw(data, pdc, rc);
}

//////////////////////////////////////////////////////////////////////////
// !!!internal use only!!!
/*
byte* bmp_file::f(int x, int y)
{
	int pitch = bih.biWidth*bih.biBitCount/8;
	byte* p = data;

	p += (bih.biHeight-1-y)*pitch + (x)*3;

	return p;
}
*/
void bmp_file::v_invert()
{
	DWORD pitch = bih.biWidth*bih.biBitCount/8;
	bytebuf tmp(pitch);

	for( int i=0; i<bih.biHeight/2; i++ )
	{
		byte* source = (byte*)data + (bih.biHeight-1-i)*pitch;
		byte* target = (byte*)data + i*pitch;
		memcpy( tmp, source, pitch );
		memcpy( source, target, pitch );
		memcpy( target, tmp, pitch);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -