19,468
社区成员
发帖
与我相关
我的任务
分享
/*************************************************************************
*
* Function: ReadRAWFile (CFile&)
*
* Purpose: Reads in the specified RAW file into a global chunk of
* memory.
*
* Returns: A handle to a dib (hDIB) if successful.
* NULL if an error occurs.
*
* Comments: BITMAPFILEHEADER is stripped off of the DIB.
* Everything from the end of the BITMAPFILEHEADER structure
* on is returned in the global memory handle.
*
**************************************************************************/
HDIB ReadRAWFile(CFile& file)
{
double min,max;
int width,height;
CRawdlg dlg;
if (dlg.DoModal() != IDOK) return FALSE;
width=dlg.m_width;
height=dlg.m_height;
//Allocate memory for the image
DWORD dwImageSize = width*height*12;// 12=DIBChannels
BYTE* pImageData = new BYTE[dwImageSize];
file.Read(pImageData,dwImageSize);
// Setup the DIB with the correct details
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;
// Allocate memory for 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!\n"));
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!\n"));
delete [] pImageData;
return NULL;
}
//Copy over the header to the DIB
CopyMemory(pDIB, &bmi.bmiHeader, bih.biSize);
//Copy the DIB bits from the user buffer into the DIB
BYTE* pBmp = (BYTE*) (pDIB + bih.biSize);
for (int j=0; j <height; j++)
{
int nDepthInOffset = j*width*3;
int nDepthOutOffset = (height-j-1)*WIDTHBYTES(width*24);
for (int i=0; i <width; i++)
{
int nInOffset = nDepthInOffset + i*3;
int nOutOffset = nDepthOutOffset + i*3;
pBmp[nOutOffset] = pImageData[nInOffset];
pBmp[nOutOffset+1] = pImageData[nInOffset+1];
pBmp[nOutOffset+2] = pImageData[nInOffset+2];
}
}
//Delete the memory used to load the jpeg prior to transfering to the DIB
delete [] pImageData;
::GlobalUnlock((HGLOBAL) hDIB);
return hDIB;
}