📄 dib.cpp
字号:
IPL_DEPTH_8U, // data of byte type "RGB", // color model "BGR", // color order IPL_DATA_ORDER_PIXEL, // channel arrangement IPL_ORIGIN_TL, // top left orientation IPL_ALIGN_DWORD, // 8 bytes align dwWidth, // image width dwHeight, // image height NULL, NULL, NULL, NULL); // not tiled if( NULL == srcImage ) return FALSE; //srcImage->imageData = (char*)lpInputImage; srcImage->imageData = (char*)pbyBuffer; if( NULL == dstImage ) return FALSE; dstImage->imageData = (char*)m_lpImage;// iplSet(dstImage, 0); iplMirror(srcImage, dstImage, 0); //iplCopy(srcImage, dstImage); iplDeallocate( srcImage, IPL_IMAGE_HEADER ); iplDeallocate( dstImage, IPL_IMAGE_HEADER ); *///////////////////////////////////////////////////// memcpy(m_lpImage, pbyBuffer, m_dwSizeImage); delete[] pbyBuffer; //delete[] lpInputImage; return TRUE;}BOOL RxDib::DecodeJPGFileToGeneralBuffer(LPCSTR lpszPathName, DWORD* width, DWORD* height, DWORD* nchannels, BYTE** buffer, DWORD *buffersize){ BOOL bres; IJLERR jerr; DWORD wholeimagesize; BYTE* pixel_buf = NULL; // Allocate the IJL JPEG_CORE_PROPERTIES structure. JPEG_CORE_PROPERTIES jcprops; ZeroMemory(&jcprops, sizeof(JPEG_CORE_PROPERTIES)); bres = TRUE; jerr = ijlInit(&jcprops); if (IJL_OK != jerr) return FALSE; // Get information on the JPEG image // (i.e., width, height, and channels). jcprops.JPGFile = const_cast<LPSTR>(lpszPathName); jerr = ijlRead(&jcprops, IJL_JFILE_READPARAMS); if (IJL_OK != jerr) return FALSE; // 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; jcprops.DIBChannels = 3; jcprops.DIBColor = IJL_BGR; break; case 3: jcprops.JPGColor = IJL_YCBCR; jcprops.DIBChannels = 3; jcprops.DIBColor = IJL_BGR; break; case 4: jcprops.JPGColor = IJL_YCBCRA_FPX; jcprops.DIBChannels = 4; jcprops.DIBColor = IJL_RGBA_FPX; 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; jcprops.DIBChannels = jcprops.JPGChannels; break; } // Set up local data. jcprops.DIBWidth = jcprops.JPGWidth; jcprops.DIBHeight = jcprops.JPGHeight; jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.DIBWidth, jcprops.DIBChannels); // Compute size of desired pixel buffer. // Allocate memory to hold the decompressed image data. wholeimagesize = (jcprops.DIBWidth * jcprops.DIBChannels + jcprops.DIBPadBytes) * jcprops.DIBHeight; pixel_buf = new BYTE [wholeimagesize]; if (NULL == pixel_buf) return FALSE; // Set up the info on the desired DIB properties. jcprops.DIBBytes = pixel_buf; // Now get the actual JPEG image data into the pixel buffer. jerr = ijlRead(&jcprops, IJL_JFILE_READWHOLEIMAGE); if (IJL_OK != jerr) { delete [] pixel_buf; return FALSE; } // Clean up the IntelR JPEG Library. if (ijlFree(&jcprops) != IJL_OK) { TRACE(_T("Cannot free Intel(R) JPEG library.")); } // 困酒贰肺 第笼绰促. int iLineWidth = jcprops.DIBWidth * jcprops.DIBChannels + jcprops.DIBPadBytes; LPBYTE pTempBuffer = new BYTE[iLineWidth]; for (int i = 0; i < jcprops.DIBHeight / 2; i++) { memcpy(pTempBuffer, pixel_buf + (i * iLineWidth), iLineWidth); memcpy(pixel_buf + (i * iLineWidth), pixel_buf + ((jcprops.DIBHeight - i - 1) * iLineWidth), iLineWidth); memcpy(pixel_buf + ((jcprops.DIBHeight - i - 1) * iLineWidth), pTempBuffer, iLineWidth); } delete[] pTempBuffer; *width = jcprops.DIBWidth; *height = jcprops.DIBHeight; *nchannels = jcprops.DIBChannels; *buffer = pixel_buf; *buffersize = wholeimagesize; return bres;} // DecodeJPGFileToGeneralBuffer()BOOL RxDib::EncodeJPGFileFromDIB(LPCSTR lpszPathName){ BOOL bres; IJLERR jerr; // Allocate the IJL JPEG_CORE_PROPERTIES structure. LPBYTE pData; int i, j, h, iDstWidthBytes, iSrcWidthBytes; LPBYTE pSrc, pDst; JPEG_CORE_PROPERTIES jcprops; ZeroMemory(&jcprops, sizeof(JPEG_CORE_PROPERTIES)); bres = TRUE; __try{ // Initialize the IntelR JPEG Library. jerr = ijlInit(&jcprops); if(IJL_OK != jerr){ bres = FALSE; __leave; } jcprops.DIBWidth = m_lpBmih->biWidth; jcprops.DIBHeight = m_lpBmih->biHeight; // Implies a bottom-up DIB. switch(m_lpBmih->biBitCount) { case 24: jcprops.DIBColor = IJL_BGR; jcprops.DIBChannels = 3; jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.DIBWidth, 3); jcprops.JPGColor = IJL_YCBCR; jcprops.JPGChannels = 3; jcprops.JPGSubsampling = IJL_411; jcprops.DIBBytes = reinterpret_cast<BYTE*>(m_lpImage); break; case 32: jcprops.DIBColor = IJL_BGR; jcprops.DIBChannels = 3; jcprops.DIBPadBytes = 0; jcprops.JPGColor = IJL_YCBCR; jcprops.JPGChannels = 3; jcprops.JPGSubsampling = IJL_411; pData = new BYTE[jcprops.DIBWidth * jcprops.DIBHeight * 3]; iDstWidthBytes = jcprops.DIBWidth * 3; iSrcWidthBytes = jcprops.DIBWidth * 4; for (h = 0; h < jcprops.DIBHeight; h++) { pDst = pData + (jcprops.DIBHeight - h - 1) * iDstWidthBytes; pSrc = m_lpImage + h * iSrcWidthBytes; for (i = 0, j = 0; i < iSrcWidthBytes; i += 4, j += 3) { pDst[j] = pSrc[i]; pDst[j + 1] = pSrc[i + 1]; pDst[j + 2] = pSrc[i + 2]; } } jcprops.DIBBytes = reinterpret_cast<BYTE*>(pData); break; default: jcprops.DIBColor = IJL_G; jcprops.DIBChannels = 1; jcprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jcprops.DIBWidth, 3); jcprops.JPGColor = IJL_YCBCR; jcprops.JPGChannels = 3; jcprops.JPGSubsampling = IJL_411; jcprops.DIBBytes = reinterpret_cast<BYTE*>(m_lpImage); break; } if (jcprops.DIBColor == IJL_RGBA_FPX) { BGRA_to_RGBA(jcprops.DIBBytes, jcprops.DIBWidth, jcprops.DIBHeight); } jcprops.JPGFile = const_cast<LPSTR>(lpszPathName); jcprops.JPGWidth = m_lpBmih->biWidth; jcprops.JPGHeight = m_lpBmih->biHeight; jcprops.jquality = 100; // Select "good" image quality jerr = ijlWrite(&jcprops, IJL_JFILE_WRITEWHOLEIMAGE); if (m_lpBmih->biBitCount == 32) delete[] pData; if (IJL_OK != jerr) { bres = FALSE; __leave; } } // __try __finally{ // Clean up the IntelR JPEG Library. ijlFree(&jcprops); } return bres;} // EncodeJPGFileFromDIB()BOOL RxDib::Write(CString strFilename, int iType /* = IT_BMP */){ BOOL bRet = FALSE; switch (iType) { case IT_BMP: bRet = WriteBMP(strFilename); break; case IT_JPG: bRet = WriteJPG(strFilename); break; } return bRet;}BOOL RxDib::WriteJPG(CString strFilename){ BOOL bRet = FALSE; if (m_lpBmih) { PSTR pstrFilename = ToMultiByte(strFilename); bRet = EncodeJPGFileFromDIB(pstrFilename); delete[] pstrFilename; } return bRet;}BOOL RxDib::WriteBMP(CString strFilename){ BITMAPFILEHEADER bmfh; CFile fileBitmap; bmfh.bfType = 0x4d42; // 'BM' int iSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries + m_dwSizeImage; bmfh.bfSize = iSize + sizeof(BITMAPFILEHEADER); // meaning of bfSize open to interpretation (bytes, words, dwords?) -- we won't use it bmfh.bfReserved1 = bmfh.bfReserved2 = 0; bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries; TRY { CFileException e; BOOL re = fileBitmap.Open(strFilename, CFile::modeReadWrite | CFile::modeCreate, &e); fileBitmap.Write((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER)); fileBitmap.Write((LPVOID) m_lpBmih, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries); fileBitmap.Write((LPVOID) m_lpImage, m_dwSizeImage); } CATCH (CException, e) {// CString strMessage;// strMessage.LoadString(IDS_DIB_WRITE_ERROR);// AfxMessageBox(strMessage); return FALSE; } END_CATCH fileBitmap.Close(); return TRUE;}void RxDib::Serialize(CArchive& ar){ ar.Flush(); if(ar.IsStoring()) { Write(ar.GetFile()->GetFilePath()); } else { Read(ar.GetFile()->GetFilePath()); }}// helper functionsvoid RxDib::ComputePaletteSize(int iBitCount){ if(m_lpBmih == NULL || m_lpBmih->biClrUsed == 0 || iBitCount > 8) { switch(iBitCount) { case 1: m_iColorTableEntries = 2; break; case 4: m_iColorTableEntries = 16; break; case 8: m_iColorTableEntries = 256; break; case 16: case 24: case 32: m_iColorTableEntries = 0; break; default: ASSERT(FALSE); } } else { m_iColorTableEntries = m_lpBmih->biClrUsed; } ASSERT(m_iColorTableEntries <= 256); }void RxDib::ComputeMetrics(){ m_dwSizeImage = m_lpBmih->biSizeImage; if (m_dwSizeImage == 0) { DWORD dwBytes = ((DWORD) m_lpBmih->biWidth * m_lpBmih->biBitCount) / 32; if(((DWORD) m_lpBmih->biWidth * m_lpBmih->biBitCount) % 32) { dwBytes++; } dwBytes *= 4; m_dwSizeImage = dwBytes * m_lpBmih->biHeight; // no compression } m_lpvColorTable = (LPBYTE) m_lpBmih + sizeof(BITMAPINFOHEADER);}int RxDib::ComputeMetrics(const CSize& szImage, int iBitCount){ int iBytes = (szImage.cx * iBitCount) / 32; if ((szImage.cx * iBitCount) % 32) { iBytes++; } iBytes *= (4 * szImage.cy); return iBytes;}void RxDib::Empty(){ DetachMapFile(); if (m_lpBmih) { delete [] m_lpBmih; m_lpBmih = NULL; } if(m_hBitmap != NULL) { ::DeleteObject(m_hBitmap); m_hBitmap = NULL; }// if(m_lpImage != NULL && bFirst == FALSE){// delete m_lpImage; if (m_lpImage) { delete[] m_lpImage; m_lpImage = NULL; }// }// else m_lpImage = NULL; m_lpvColorTable = NULL; m_iColorTableEntries = 0; m_dwSizeImage = 0; m_lpvFile = NULL; m_hMap = NULL; m_hFile = NULL; m_bCompressed = FALSE;}void RxDib::DetachMapFile(){ if(m_hFile == NULL) return; ::UnmapViewOfFile(m_lpvFile); ::CloseHandle(m_hMap); ::CloseHandle(m_hFile); m_hFile = NULL;}DWORD RxDib::Bpl(){ DWORD dwBytes = ((DWORD) m_lpBmih->biWidth * m_lpBmih->biBitCount) / 32; if(((DWORD) m_lpBmih->biWidth * m_lpBmih->biBitCount) % 32) dwBytes++; dwBytes *= 4; return dwBytes;}// For grey versionvoid RxDib::MakeColorTable(){ RGBQUAD *colorTable = (RGBQUAD *)m_lpvColorTable; for(int i=0; i<m_iColorTableEntries; i++){ colorTable[i].rgbBlue = i; colorTable[i].rgbGreen = i; colorTable[i].rgbRed = i; colorTable[i].rgbReserved = 0; }}BOOL RxDib::Stretch(CDC *pDC, CPoint origin, CSize size){ if (m_lpBmih == NULL) return FALSE; ::SetStretchBltMode(pDC->GetSafeHdc(), HALFTONE); ::StretchDIBits(pDC->GetSafeHdc(), origin.x, origin.y, size.cx, size.cy, 0, 0, m_lpBmih->biWidth, m_lpBmih->biHeight, m_lpImage, (LPBITMAPINFO) m_lpBmih, DIB_RGB_COLORS, SRCCOPY); return TRUE;}BOOL RxDib::Convert16To32(int iWidth, int iHeight, LPBYTE lpDst, unsigned short* lpSrc, BOOL bWithPadding){ int iBplSrc, iBplDst, i, j; unsigned short* lpSrcStart; LPBYTE lpDstStart; iBplSrc = iWidth * 16 / 32; if ((iWidth * 16) % 32) iBplSrc++; iBplSrc *= 4; iBplDst = iWidth * 4; BYTE col[4]; unsigned short nTmp; col[0] = 0; for (i = 0; i < iHeight; i++) { lpSrcStart = (unsigned short*)((LPBYTE)lpSrc + (iBplSrc * i)); lpDstStart = lpDst + (iBplDst * i); for (j = 0; j < iWidth; j++) { nTmp = *(lpSrcStart + j); col[3] = BYTE((nTmp & 0x1F) / 31. * 255); // blue col[2] = BYTE(((nTmp >> 5) & 0x1F) / 31. * 255); // green col[1] = BYTE(((nTmp >> 10) & 0x1F) / 31. * 255); // red
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -