19,472
社区成员




HDIB ReadRAWFile(CFile& file)
{
double min,max;
int width,height; //宽和高(需要自己填)
width = 368;
height = 241;
DWORD dwImageSize = width*height;
BYTE* pImageData = new BYTE[dwImageSize];
file.Read(pImageData,dwImageSize); //读取图像数据
// 填写结构体
BITMAPINFO bmi;
BITMAPINFOHEADER& bih = bmi.bmiHeader;
ZeroMemory(&bih, sizeof(BITMAPINFOHEADER));
bih.biSize = sizeof(BITMAPINFOHEADER);
bih.biWidth = width;
bih.biHeight= height;
bih.biCompression = BI_RGB;
bih.biPlanes = 1;
bih.biBitCount = 24;
// 写到DIB里面去
DWORD dwBmpBitsSize = WIDTHBYTES(width*24)*height;
HDIB hDIB = (HDIB) ::GlobalAlloc(GHND, bih.biSize + dwBmpBitsSize);
if (hDIB == 0)
{
TRACE(_T("Could not allocate memory for the DIB while loading from file! "));
delete [] pImageData;
return NULL;
}
LPSTR pDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
if (pDIB == 0)
{
TRACE(_T("Could not lock memory for the DIB while loading from file! "));
delete [] pImageData;
return NULL;
}
//信息头
CopyMemory(pDIB, &bmi.bmiHeader, bih.biSize);
//颜色板
LPRGBQUAD lpRGBQuad = LPRGBQUAD(pDIB + bih.biSize);
for ( i = 0; i < 256; i++)
{
lpRGBQuad->rgbRed = i;
lpRGBQuad->rgbGreen = i;
lpRGBQuad->rgbBlue = i;
lpRGBQuad->rgbReserved = 0;
lpRGBQuad++;
}
BYTE* pBmp = (BYTE*) (pDIB + bih.biSize + 256*sizeof(RGBQUAD));
for ( j=0; j <height; j++)
{
int nDepthInOffset = j*width;
int nDepthOutOffset = (height-j-1)*WIDTHBYTES(width*bih.biBitCount);
for (i=0; i <width; i++)
{
int nInOffset = nDepthInOffset + i;
int nOutOffset = nDepthOutOffset + i;
pBmp[nOutOffset] = pImageData[nInOffset];
}
}
//释放内存
delete [] pImageData;
::GlobalUnlock((HGLOBAL) hDIB);
return hDIB;
}