www.pudn.com > ImageSplite.rar > JPEGAPI.cpp
#include "stdafx.h" #include "dibapi.h" #include "jpegapi.h" #include#include "Intel\include\IJL.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #ifndef DIBIMAGE_NO_JPEG /************************************************************************* * * Function: ReadJPEGFile (CFile&) * * Purpose: Reads in the specified JPEG 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 ReadJPEGFile(CFile& file) { //For correct operation of the T2A macro, see MFC Tech Note 59 USES_CONVERSION; //Keep a local copy of the filename for usage with the IJL functions. //Note that since IJL does not support Unicode we must perform the //Unicode to Ascii conversion char lpszAsciiFileName[MAX_PATH]; TCHAR pszFileName[_MAX_PATH]; _tcscpy(pszFileName, file.GetFilePath()); strcpy(lpszAsciiFileName, T2A(pszFileName)); ASSERT(strlen(lpszAsciiFileName)); //Use the IJL to load up the jpeg JPEG_CORE_PROPERTIES image; ZeroMemory(&image, sizeof(JPEG_CORE_PROPERTIES)); //Init the IJL if (ijlInit(&image) != IJL_OK) { TRACE(_T("Cannot initialize Intel JPEG library!\n")); return NULL; } //Read in the Jpeg file parameters image.JPGFile = lpszAsciiFileName; if (ijlRead(&image, IJL_JFILE_READPARAMS) != IJL_OK) { TRACE(_T("Cannot read JPEG file header from %s file!\n"), image.JPGFile); ijlFree(&image); return NULL; } //Allocate memory for the image DWORD dwImageSize = image.JPGWidth*image.JPGHeight*image.DIBChannels; BYTE* pImageData = new BYTE[dwImageSize]; //Call the IJL to load the Jpeg from file image.DIBWidth = image.JPGWidth; image.DIBHeight = image.JPGHeight; image.DIBBytes = pImageData; if (ijlRead(&image, IJL_JFILE_READWHOLEIMAGE) != IJL_OK) { TRACE(_T("Cannot read image pImageData from %s file!\n"), image.JPGFile); delete [] pImageData; ijlFree(&image); return NULL; } //Finished with IJL if (ijlFree(&image) != IJL_OK) TRACE(_T("Cannot free Intel JPEG library!\n")); // Setup the DIB with the correct details BITMAPINFO bmi; BITMAPINFOHEADER& bih = bmi.bmiHeader; ZeroMemory(&bih, sizeof(BITMAPINFOHEADER)); bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = image.JPGWidth; bih.biHeight = image.JPGHeight; bih.biCompression = BI_RGB; bih.biPlanes = 1; bih.biBitCount = 24; // Allocate memory for DIB DWORD dwBmpBitsSize = WIDTHBYTES(image.JPGWidth*24)*image.JPGHeight; 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 biBitCount != 24) { TRACE(_T("Only 16 million colors (24 bit) images can be saved as JPEG!\n")); ::GlobalUnlock((HGLOBAL) hDib); return FALSE; } if (lpBI->biCompression != BI_RGB) { TRACE(_T("RLE-compressed images can't be saved as JPEG!\n")); ::GlobalUnlock((HGLOBAL) hDib); return FALSE; } //Init the IJL JPEG_CORE_PROPERTIES image; ZeroMemory(&image, sizeof(JPEG_CORE_PROPERTIES)); if (ijlInit(&image) != IJL_OK) { TRACE(_T("Can't initialize Intel JPEG library!\n")); ::GlobalUnlock((HGLOBAL) hDib); return FALSE; } //Setup the "image" settings image.JPGFile = lpszAsciiFileName; image.jquality = dwQuality; //[0...100], best is 100, LIB's default is 75, our default is 100 image.DIBWidth = lpBI->biWidth; image.DIBHeight = lpBI->biHeight; image.JPGWidth = lpBI->biWidth; image.JPGHeight = lpBI->biHeight; //Get a pointer to the DIB bits BYTE* pImageData = ((BYTE*)lpBI) + lpBI->biSize; //Allocate some memory to save the Dib bits into BYTE* pBmp = new BYTE[lpBI->biWidth*lpBI->biHeight*3]; image.DIBBytes = pBmp; //Copy the DIB bits from the DIB into the user buffer for (LONG j=0; j biHeight; j++) { int nDepthOutOffset = (lpBI->biHeight-j-1)*lpBI->biWidth*3; int nDepthInOffset = j*WIDTHBYTES(lpBI->biWidth*24); for (LONG i=0; i biWidth; 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]; } } ::GlobalUnlock((HGLOBAL)hDib); //Call the IJL to write the Jpeg to file if (ijlWrite(&image, IJL_JFILE_WRITEWHOLEIMAGE) != IJL_OK) { TRACE(_T("Can't write jpeg image\n") ); delete [] pBmp; return FALSE; } //Finished with IJL if (ijlFree(&image) != IJL_OK) TRACE(_T("Can't free Intel JPEG library!\n")); delete [] pBmp; return TRUE; } #endif //DIBIMAGE_NO_JPEG