现在想从JPG文件读入图片信息,转换为比BITMAP格式,再写入BITMAPINFO
读取JPG文件使用IJL。
编译没出错,但运行时出现内存溢出。请各位高手指点迷津!!BOOL LoadJPEGFile(HWND hwnd, PTSTR pstrFileName){ //Prepare a 24Bit buffer to receive image data
BYTE * buffer24; HBITMAP hbm; long szbuff24; //Error handle
IJLERR jerr; //Create the JPEG object
JPEG_CORE_PROPERTIES jcprops; //Initialize the JPEG object
jerr = ijlInit(&jcprops);
if (jerr != IJL_OK){ MessageBox(hwnd, "IJLInit problem", TITLE, MB_OK);
return 0; } //Set the IJL data source as a filename
jcprops.JPGFile = pstrFileName ; //Read JPEG parameters from the file
jerr = ijlRead(&jcprops, IJL_JFILE_READPARAMS);
if (jerr != IJL_OK){

MessageBox(hwnd, "Error reading JPEG parameters", TITLE, MB_OK);
return 0; } //Determine the required size
szbuff24 = (jcprops.JPGWidth * C24BIT + 7) / 8 * jcprops.JPGHeight; //Resize the buffer
buffer24 = malloc(szbuff24);
if (buffer24 == NULL){ MessageBox(hwnd, "Memory Allocation Error", TITLE, MB_OK);
return 0; } //Set up the DIB specification for the JPEG decoder
jcprops.DIBWidth    = jcprops.JPGWidth;
jcprops.DIBHeight   = jcprops.JPGHeight; //Implies a bottom-up DIB.
jcprops.DIBChannels = 3;
jcprops.DIBColor    = IJL_BGR;
jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.JPGWidth,3);
jcprops.DIBBytes    = (BYTE*)buffer24; //Set the JPG color space ... this will always be somewhat of an
//educated guess at best because JPEG is "color blind" (i.e.,
//nothing in the bit stream tells you what color space the data was
//encoded from. However, in this example we assume that we are
//reading JFIF files which means that 3 channel images are in the
//YCbCr color space and 1 channel images are in the Y color space.
switch (jcprops.JPGChannels){ case 1: jcprops.JPGColor = IJL_G;
break;
    
case 3: jcprops.JPGColor = IJL_YCBCR;
break;
    
default:
//This catches everything else, but no color twist will be
//performed by the IJL.
jcprops.DIBColor = (IJL_COLOR)IJL_OTHER;
jcprops.JPGColor = (IJL_COLOR)IJL_OTHER;
break;
} //Read JPEG image data into our 24bit buffer
jerr = ijlRead(&jcprops, IJL_JFILE_READWHOLEIMAGE); //Make sure the read was successful
if (jerr != IJL_OK)
{
MessageBox(hwnd, "Error reading JPEG image", TITLE, MB_OK);
return 0;
} //The data we have from the JPEG is already 24bit
//Just create the new bitmap from our buffer
hbm = CreateBitmap (jcprops.JPGWidth, jcprops.JPGHeight, 1, 24, 0);
if(hbm == NULL)
{
MessageBox(hwnd, "Failed to create 24Bit Bitmap", TITLE, MB_OK);
return 0;
} //remove the old buffer
free( buffer24 ); hdc = GetDC(hwnd); GetDIBits(hdc, hbm, 0, (UINT)jcprops.JPGHeight, jcprops.DIBBytes, pbmi, DIB_RGB_COLORS); ReleaseDC(hwnd, hdc); //Get the DIB width and height
if (pbmi->bmiHeader.biSize == sizeof (BITMAPCOREHEADER)){

cxSrc = ((BITMAPCOREHEADER *) pbmi)->bcWidth;
cySrc = ((BITMAPCOREHEADER *) pbmi)->bcHeight;

}
else{

cxSrc = pbmi->bmiHeader.biWidth;

cySrc = abs(pbmi->bmiHeader.biHeight);

} return 1;
}