VC裁剪bmp

#include 
#include 
#include 
using namespace cv;
using namespace std;

typedef struct mw_Rect{
	int x ;
	int y ;
	int width ;
	int height ;
}mw_Rect;

/*****************************************************
功能:
	输入bmp字节流,按照指定大小区域裁剪输出bmp字节流
参数:
	bmp[in]	需要裁剪的bmp字节流(包含头信息的完整bmp)
	len[in]	需要裁剪的bmp字节流的长度
	lenOut[out]	裁剪出来的bmp字节流长度
	rect[in]	裁剪的区域大小矩形框
返回值:
	返回裁剪处理啊的bmp字节流,包含头信息,直接
	写到文件中就是一个bmp文件
*****************************************************/
unsigned char* CutImg2(unsigned char *bmp , int len , int *lenOut , mw_Rect rect){
	int lbx, lby, cutwidth, cutheight;

	lbx=rect.x;//²Ã¼ôÆðʼλÖã¨×óϽÇ×ø±êϵ£©
	lby=rect.y;//
	cutwidth=rect.width;//²Ã¼ôµÄ¿í¶È
	cutheight=rect.height;//²Ã¼ôµÄ¸ß¶È

	BITMAPFILEHEADER myfileHdr; 
	BITMAPINFOHEADER mySinfo,myDinfo; 
	memcpy_s(&myfileHdr , sizeof(myfileHdr) , bmp , sizeof(myfileHdr));
	memcpy_s(&mySinfo , sizeof(mySinfo) , bmp+sizeof(myfileHdr) , sizeof(mySinfo));
	DWORD dwSize = (mySinfo.biWidth * mySinfo.biBitCount + 31) / 32 * 4 * mySinfo.biHeight;
	PBYTE pBufs = new BYTE[dwSize];
	memcpy_s(pBufs , dwSize , bmp+sizeof(myfileHdr)+sizeof(mySinfo) , dwSize);

	DWORD dwSize1 = (cutwidth * 24 + 31) / 32 * 4 *cutheight; //¼ÆËãÄ¿µÄÎļþλͼÊý¾ÝµÄ´óС
	DWORD dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwSize1; 
	DWORD dwSize2 = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
	
	myfileHdr.bfType = 0x4D42; // "BM"
	myfileHdr.bfSize = dwDIBSize; 
	myfileHdr.bfReserved1 = 0; 
	myfileHdr.bfReserved2 = 0; 
	myfileHdr.bfOffBits = dwSize2;
	
	myDinfo.biWidth = cutwidth; //ÉèÖÃÄ¿µÄÎļþµÄ¿í¶È
	myDinfo.biHeight = cutheight; //ÉèÖÃÄ¿µÄÎļþµÄ¸ß¶È
	myDinfo.biSize   = sizeof(BITMAPINFOHEADER);
	myDinfo.biPlanes   = 1;
	myDinfo.biBitCount = 24;
	myDinfo.biCompression = BI_RGB;
	myDinfo.biSizeImage = 0;
	myDinfo.biXPelsPerMeter = 0;
	myDinfo.biYPelsPerMeter = 0;
	myDinfo.biClrImportant = 0;
	myDinfo.biClrUsed = 0;

	PBYTE pBufd = new BYTE[dwSize1]; //ÉùÃ÷Ä¿µÄÎļþλͼÐÅÏ¢Äڴ滺³åÇø
	BYTE *pSrc, *pDes;
	int j=0;
	for(int i = lby; i < myDinfo.biHeight + lby; i++) 
	{ 
	      pSrc = pBufs + (mySinfo.biWidth * mySinfo.biBitCount + 31) / 32 * 4 * i + lbx * 3; //Ö¸¶¨×ø±êµãµÄ¶¨Î»
	      pDes = pBufd + (myDinfo.biWidth * myDinfo.biBitCount + 31) / 32 * 4 * j;  
	      memcpy(pDes, pSrc, (myDinfo.biWidth * myDinfo.biBitCount + 31) / 32 * 4); 
	      j++;
		  //::MessageBox(NULL,pSrc,"aaaa",MB_OK|MB_ICONINFORMATION);
	}
	*lenOut = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwSize1;
	unsigned char *cutImg = new unsigned char[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwSize1];
	memset(cutImg,0,sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwSize1);

	memcpy_s(cutImg , sizeof(BITMAPFILEHEADER) , &myfileHdr , sizeof(BITMAPFILEHEADER));
	memcpy_s(cutImg+sizeof(BITMAPFILEHEADER) , sizeof(BITMAPINFOHEADER) , &myDinfo , sizeof(BITMAPINFOHEADER));
	memcpy_s(cutImg+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) , dwSize , pBufd , dwSize1);
	delete[] pBufs;
	pBufs = NULL;
	delete[] pBufd;
	pBufd = NULL;
	return cutImg;
}

你可能感兴趣的:(VC裁剪bmp)