void CXBlobMTooldlg::SaveJpgTest()
{
int width = 999;
int height = 1000;
int band = 1;
char * pImageData = Vision_GetHwnd(0, 0, 0, width, height);
int stride = (width + 3) / 4 * 4;
int rowSize = width * band;
int strideSize = stride * band;
char * pConvertData = new char[stride * height * band];
char * pCopyData = pConvertData;
pImageData += ((height - 1) * rowSize);
for (int row = 0; row < height; row++)
{
memcpy(pCopyData, pImageData, rowSize);
pCopyData += strideSize;
pImageData -= rowSize;
}
BITMAPINFO * bmInfo = (BITMAPINFO*)malloc(sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD));
bmInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfo->bmiHeader.biWidth = width;
bmInfo->bmiHeader.biHeight = height;
bmInfo->bmiHeader.biPlanes = 1;
//bmInfo->bmiHeader.biBitCount = 8;//8位灰阶数据
bmInfo->bmiHeader.biBitCount = band * 8;
bmInfo->bmiHeader.biCompression = 0;//BI_RGB
bmInfo->bmiHeader.biClrUsed = 0;//或256
bmInfo->bmiHeader.biSizeImage = 0;
bmInfo->bmiHeader.biXPelsPerMeter = 0;
bmInfo->bmiHeader.biYPelsPerMeter = 0;
bmInfo->bmiHeader.biClrImportant = 0;
BITMAPINFOHEADER &bmInfohdr = (BITMAPINFOHEADER)bmInfo->bmiHeader;
for (int i = 0; i<256; i++)
{
bmInfo->bmiColors[i].rgbRed = i;
bmInfo->bmiColors[i].rgbGreen = i;
bmInfo->bmiColors[i].rgbBlue = i;
}
HDC hDC = ::GetDC(0);
HBITMAP hBmp = CreateDIBitmap(hDC,
&bmInfohdr,
CBM_INIT,
pConvertData,
bmInfo,
DIB_RGB_COLORS);
CImage mmage;
mmage.Attach(hBmp);
int retval = mmage.Save("d:\123.jpg", Gdiplus::ImageFormatJPEG);
delete[] pConvertData;
}
void CXBlobMTooldlg::SaveBmp(const char *fileName, unsigned char *pImgData, int imgLength, int iWidth, int iHeight)
{
BITMAPFILEHEADER targetfileheader;
BITMAPINFOHEADER targetinfoheader;
memset(&targetfileheader, 0, sizeof(BITMAPFILEHEADER));
memset(&targetinfoheader, 0, sizeof(BITMAPINFOHEADER));
//构造灰度图的文件头
targetfileheader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256;
targetfileheader.bfSize = 192 * 192 + sizeof(RGBQUAD) * 256 + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
targetfileheader.bfReserved1 = 0;
targetfileheader.bfReserved2 = 0;
targetfileheader.bfType = 0x4d42;
//构造灰度图的信息头
targetinfoheader.biBitCount = 8;
targetinfoheader.biSize = sizeof(BITMAPINFOHEADER);
targetinfoheader.biHeight = iHeight;
targetinfoheader.biWidth = iWidth;
targetinfoheader.biPlanes = 1;
targetinfoheader.biCompression = BI_RGB;
targetinfoheader.biSizeImage = 0;
targetinfoheader.biXPelsPerMeter = 0;
targetinfoheader.biYPelsPerMeter = 0;
targetinfoheader.biClrImportant = 0;
targetinfoheader.biClrUsed = 0;
//构造灰度图的调色版
RGBQUAD rgbquad[256];
int i;
for (i = 0; i<256; i++)
{
rgbquad[i].rgbBlue = i;
rgbquad[i].rgbGreen = i;
rgbquad[i].rgbRed = i;
rgbquad[i].rgbReserved = 0;
}
BYTE* ptargetData;
ptargetData = new BYTE[iWidth*iHeight];
//由于BMP图像对于行是倒置的,即图像显示的第一行是最后一行数据,所以要倒置,这里的pcutface已经////是灰度图像了
for (long i = iHeight -1; i >= 0; i--)
{
for (long j = 0; j<iWidth; j++)
{
ptargetData[i * iWidth + j] = pImgData[(iHeight-1 - i) * iWidth + j];
}
}
CFile file;
if (file.Open(fileName, CFile::modeCreate | CFile::modeWrite))
{
file.Write(&targetfileheader, sizeof(BITMAPFILEHEADER));
file.Write(&targetinfoheader, sizeof(BITMAPINFOHEADER));
file.Write(&rgbquad, sizeof(RGBQUAD) * 256);
file.Write(ptargetData, iWidth*iHeight); //这里targetbuf的大小为192x192
file.Close();
delete ptargetData;
ptargetData = NULL;
}
}