📄 dib.cpp
字号:
*(lpDstStart + (j * 4)) = col[3]; *(lpDstStart + (j * 4 + 1)) = col[2]; *(lpDstStart + (j * 4 + 2)) = col[1]; *(lpDstStart + (j * 4 + 3)) = col[0]; } } return TRUE;}BOOL RxDib::Convert24To32(int iWidth, int iHeight, LPBYTE lpDst, LPBYTE lpSrc, BOOL bWithPadding){ int iBplSrc, iBplDst, i, j; LPBYTE lpSrcStart, lpDstStart; if (bWithPadding) { iBplSrc = (iWidth * 24) / 32; if ((iWidth * 24) % 32) iBplSrc++; iBplSrc *= 4; } else iBplSrc = iWidth * 3; iBplDst = iWidth * 4; for (i = 0; i < iHeight; i++) { lpSrcStart = lpSrc + (iBplSrc * i); lpDstStart = lpDst + (iBplDst * i); for (j = 0; j < iWidth; j++) { *(lpDstStart + (j * 4)) = *(lpSrcStart + (j * 3)); *(lpDstStart + (j * 4 + 1)) = *(lpSrcStart + (j * 3 + 1)); *(lpDstStart + (j * 4 + 2)) = *(lpSrcStart + (j * 3 + 2)); *(lpDstStart + (j * 4 + 3)) = 0; } } return TRUE;}void RxDib::BGRA_to_RGBA(BYTE* pData, int iWidth, int iHeight){ int i, j, iPad, iLineWidth; BYTE r, g, b, a; BYTE* ptr; ptr = pData; iPad = IJL_DIB_PAD_BYTES(iWidth, 4); iLineWidth = iWidth * 4 + iPad; for (i = 0; i < iHeight; i++) { ptr = pData + iLineWidth * i; for (j = 0; j < iWidth; j++) { b = ptr[0]; g = ptr[1]; r = ptr[2]; a = ptr[3]; ptr[0] = r; ptr[1] = g; ptr[2] = b; ptr += 4; } } return;}void RxDib::RGBA_FPX_to_BGRA(BYTE* pData, int iWidth, int iHeight){ int i, j, iPad, iLineWidth; BYTE r, g, b, a; BYTE* ptr; ptr = pData; iPad = IJL_DIB_PAD_BYTES(iWidth, 4); iLineWidth = iWidth * 4 + iPad; for (i = 0; i < iHeight; i++) { ptr = pData + iLineWidth * i; for (j = 0; j < iWidth; j++) { r = ptr[0]; g = ptr[1]; b = ptr[2]; a = ptr[3]; ptr[2] = (r * a + 1) >> 8; ptr[1] = (g * a + 1) >> 8; ptr[0] = (b * a + 1) >> 8; ptr += 4; } } return;}BOOL RxDib::CompressAsJPEG(){ // data啊 绝绰 版快 return if (m_lpBmih == NULL || m_lpImage == NULL) return FALSE; if (m_bCompressed) return TRUE; BOOL bRes = TRUE; LPBYTE pData; int i, j, iSize; JPEG_CORE_PROPERTIES jprops; ZeroMemory(&jprops, sizeof(JPEG_CORE_PROPERTIES)); if (ijlInit(&jprops) != IJL_OK) { bRes = FALSE; return FALSE; } // Setup DIB jprops.DIBWidth = m_lpBmih->biWidth; jprops.DIBHeight = m_lpBmih->biHeight; switch (m_lpBmih->biBitCount) { case 24: jprops.DIBColor = IJL_BGR; jprops.DIBChannels = 3; jprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jprops.DIBWidth, 3); jprops.JPGColor = IJL_YCBCR; jprops.JPGChannels = 3; jprops.JPGSubsampling = IJL_411; jprops.DIBBytes = reinterpret_cast<BYTE*>(m_lpImage); break; case 32: jprops.DIBColor = IJL_BGR; jprops.DIBChannels = 3; jprops.DIBPadBytes = 0; jprops.JPGColor = IJL_YCBCR; jprops.JPGChannels = 3; jprops.JPGSubsampling = IJL_411; pData = new BYTE[jprops.DIBWidth * jprops.DIBHeight * 3]; iSize = jprops.DIBWidth * jprops.DIBHeight * 4; for (i = 0, j = 0; i < iSize; i += 4, j += 3) { pData[j] = m_lpImage[i + 0]; pData[j + 1] = m_lpImage[i + 1]; pData[j + 2] = m_lpImage[i + 2]; } jprops.DIBBytes = reinterpret_cast<BYTE*>(pData); break; default: break; } // Setup JPEG jprops.JPGWidth = m_lpBmih->biWidth; jprops.JPGHeight = m_lpBmih->biHeight; jprops.JPGSizeBytes = jprops.JPGWidth * jprops.JPGHeight * jprops.JPGChannels; jprops.JPGBytes = new BYTE[jprops.JPGSizeBytes]; jprops.jquality = 100; if (jprops.DIBColor == IJL_RGBA_FPX) { BGRA_to_RGBA(jprops.DIBBytes, jprops.DIBWidth, jprops.DIBHeight); } if (ijlWrite(&jprops, IJL_JBUFF_WRITEWHOLEIMAGE) != IJL_OK) { bRes = FALSE; return FALSE; } if (m_lpBmih->biBitCount == 32) delete[] pData; delete[] m_lpImage; m_lpImage = new BYTE[jprops.JPGSizeBytes]; CopyMemory(m_lpImage, jprops.JPGBytes, jprops.JPGSizeBytes); m_dwSizeImage = jprops.JPGSizeBytes; delete[] jprops.JPGBytes; m_bCompressed = TRUE; ijlFree(&jprops); return bRes;}BOOL RxDib::DecompressAsBMP(){ if (m_lpBmih == NULL || m_lpImage == NULL) return FALSE; if (!m_bCompressed) return TRUE; LPBYTE pImageData = NULL; int i, j, h, iDstWidthBytes, iSrcWidthBytes; LPBYTE pSrc, pDst; JPEG_CORE_PROPERTIES jprops; ZeroMemory(&jprops, sizeof(JPEG_CORE_PROPERTIES)); TRY if (ijlInit( &jprops ) != IJL_OK) { TRACE(_T("Cannot initialize Intel JPEG library\n")); AfxThrowUserException(); } jprops.JPGFile = NULL; jprops.JPGBytes = m_lpImage; jprops.JPGSizeBytes = m_dwSizeImage; if (ijlRead(&jprops, IJL_JBUFF_READPARAMS) != IJL_OK) { TRACE(_T("Cannot read parameters\n")); AfxThrowUserException(); } switch (jprops.JPGChannels) { case 1: jprops.JPGColor = IJL_G; jprops.DIBChannels = 3; jprops.DIBColor = IJL_BGR; break; case 3: jprops.JPGColor = IJL_YCBCR; jprops.DIBChannels = 3; jprops.DIBColor = IJL_BGR; break; case 4: jprops.JPGColor = IJL_YCBCRA_FPX; jprops.DIBChannels = 4; jprops.DIBColor = IJL_RGBA_FPX; break; default: // This catches everything else, but no // color twist will be performed by the IJL. jprops.DIBColor = (IJL_COLOR)IJL_OTHER; jprops.JPGColor = (IJL_COLOR)IJL_OTHER; jprops.DIBChannels = jprops.JPGChannels; break; } jprops.DIBWidth = jprops.JPGWidth; jprops.DIBHeight = jprops.JPGHeight; jprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jprops.DIBWidth, jprops.DIBChannels); m_dwSizeImage = (jprops.DIBWidth * jprops.DIBChannels + jprops.DIBPadBytes) * jprops.DIBHeight; pImageData = new BYTE[m_dwSizeImage]; if (pImageData == NULL) { AfxThrowUserException(); } jprops.DIBBytes = pImageData; if (ijlRead(&jprops, IJL_JBUFF_READWHOLEIMAGE) != IJL_OK) { delete[] pImageData; AfxThrowUserException(); } if (ijlFree( &jprops ) != IJL_OK) { delete[] pImageData; return FALSE; } delete[] m_lpImage; if (m_lpBmih->biBitCount == 32 && jprops.JPGChannels == 3) { m_dwSizeImage = jprops.DIBWidth * jprops.DIBHeight * 4; m_lpImage = new BYTE[m_dwSizeImage]; iSrcWidthBytes = (jprops.DIBPadBytes + jprops.DIBWidth * jprops.JPGChannels); iDstWidthBytes = jprops.DIBWidth * 4; for (h = 0; h < jprops.DIBHeight; h++) { pSrc = pImageData + h * iSrcWidthBytes; pDst = m_lpImage + h * iDstWidthBytes; for (i = 0, j = 0; i < iDstWidthBytes; i += 4, j += 3) { pDst[i] = pSrc[j]; pDst[i + 1] = pSrc[j + 1]; pDst[i + 2] = pSrc[j + 2]; pDst[i + 3] = 0; } } delete[] pImageData; } else { if (jprops.DIBColor == IJL_RGBA_FPX) { RGBA_FPX_to_BGRA(pImageData, jprops.DIBWidth, jprops.DIBHeight); } m_lpImage = pImageData; } m_bCompressed = FALSE; CATCH_ALL(e) ijlFree( &jprops ); if (pImageData) delete[] pImageData; return FALSE; END_CATCH_ALL return TRUE;}RxDib* RxDib::GetDecompressedDIB(){ LPBYTE lpDecompressedImage = NULL; DWORD dwImageSize; if (m_lpBmih == NULL || m_lpImage == NULL) return NULL; if (m_bCompressed) { LPBYTE pImageData = NULL; int i, j, h, iDstWidthBytes, iSrcWidthBytes; LPBYTE pSrc, pDst; JPEG_CORE_PROPERTIES jprops; ZeroMemory(&jprops, sizeof(JPEG_CORE_PROPERTIES)); TRY if (ijlInit( &jprops ) != IJL_OK) { TRACE(_T("Cannot initialize Intel JPEG library\n")); AfxThrowUserException(); } jprops.JPGFile = NULL; jprops.JPGBytes = m_lpImage; jprops.JPGSizeBytes = m_dwSizeImage; if (ijlRead(&jprops, IJL_JBUFF_READPARAMS) != IJL_OK) { TRACE(_T("Cannot read parameters\n")); AfxThrowUserException(); } switch (jprops.JPGChannels) { case 1: jprops.JPGColor = IJL_G; jprops.DIBChannels = 3; jprops.DIBColor = IJL_BGR; break; case 3: jprops.JPGColor = IJL_YCBCR; jprops.DIBChannels = 3; jprops.DIBColor = IJL_BGR; break; case 4: jprops.JPGColor = IJL_YCBCRA_FPX; jprops.DIBChannels = 4; jprops.DIBColor = IJL_RGBA_FPX; break; default: // This catches everything else, but no // color twist will be performed by the IJL. jprops.DIBColor = (IJL_COLOR)IJL_OTHER; jprops.JPGColor = (IJL_COLOR)IJL_OTHER; jprops.DIBChannels = jprops.JPGChannels; break; } jprops.DIBWidth = jprops.JPGWidth; jprops.DIBHeight = jprops.JPGHeight; jprops.DIBPadBytes = IJL_DIB_PAD_BYTES(jprops.DIBWidth, jprops.DIBChannels); dwImageSize = (jprops.DIBWidth * jprops.DIBChannels + jprops.DIBPadBytes) * jprops.DIBHeight; pImageData = new BYTE[dwImageSize]; if (pImageData == NULL) { AfxThrowUserException(); } jprops.DIBBytes = pImageData; if (ijlRead(&jprops, IJL_JBUFF_READWHOLEIMAGE) != IJL_OK) { delete[] pImageData; AfxThrowUserException(); } if (ijlFree( &jprops ) != IJL_OK) { delete[] pImageData; return NULL; } if (m_lpBmih->biBitCount == 32 && jprops.JPGChannels == 3) { dwImageSize = jprops.DIBWidth * jprops.DIBHeight * 4; lpDecompressedImage = new BYTE[dwImageSize]; iSrcWidthBytes = (jprops.DIBPadBytes + jprops.DIBWidth * jprops.JPGChannels); iDstWidthBytes = jprops.DIBWidth * 4; for (h = 0; h < jprops.DIBHeight; h++) { pSrc = pImageData + h * iSrcWidthBytes; pDst = lpDecompressedImage + h * iDstWidthBytes; for (i = 0, j = 0; i < iDstWidthBytes; i += 4, j += 3) { pDst[i] = pSrc[j]; pDst[i + 1] = pSrc[j + 1]; pDst[i + 2] = pSrc[j + 2]; pDst[i + 3] = 0; } } delete[] pImageData; } else { if (jprops.DIBColor == IJL_RGBA_FPX) { RGBA_FPX_to_BGRA(pImageData, jprops.DIBWidth, jprops.DIBHeight); } lpDecompressedImage = pImageData; } CATCH_ALL(e) ijlFree( &jprops ); if (pImageData) delete[] pImageData; return NULL; END_CATCH_ALL } else { dwImageSize = m_dwSizeImage; lpDecompressedImage = m_lpImage; } RxDib* pNewDib = new RxDib(GetDimensions(), m_lpBmih->biBitCount); CopyMemory(pNewDib->m_lpBmih, m_lpBmih, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries); CopyMemory(pNewDib->m_lpImage, lpDecompressedImage, dwImageSize); if (lpDecompressedImage) delete[] lpDecompressedImage; return pNewDib;}RxDib RxDib::operator=(const RxDib& dib){ Empty(); m_bCompressed = dib.m_bCompressed; m_dwSizeImage = dib.m_dwSizeImage; m_iColorTableEntries = dib.m_iColorTableEntries; m_lpBmih = (LPBITMAPINFOHEADER) new BYTE[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries]; CopyMemory(m_lpBmih, dib.m_lpBmih, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries); m_lpvColorTable = (LPBYTE)m_lpBmih + sizeof(BITMAPINFOHEADER); m_lpImage = new BYTE[m_dwSizeImage]; CopyMemory(m_lpImage, dib.m_lpImage, m_dwSizeImage); return *this;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -