📄 dib.cpp
字号:
// dib.cpp// new version for WIN32#include "stdafx.h"#include "dib.h"#include "ijl.h"#include "ipl.h"#include "resource.h"#include "UnicodeFunc.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifIMPLEMENT_SERIAL(RxDib, CObject, 0);RxDib::RxDib(){ m_hFile = NULL; m_hBitmap = NULL; m_lpBmih = NULL; m_dwSizeImage = 0; m_lpImage = NULL; m_bCompressed = FALSE; Empty();}RxDib::RxDib(CSize size, int iBitCount){ m_hFile = NULL; m_hBitmap = NULL; m_lpBmih = NULL; m_lpImage = NULL; m_bCompressed = FALSE; Empty(); ComputePaletteSize(iBitCount); m_dwSizeImage = ComputeMetrics(size, iBitCount); // no compression //m_lpBmih = (LPBITMAPINFOHEADER) new // char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries + m_dwSizeImage]; m_lpBmih = (LPBITMAPINFOHEADER) new char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries]; m_lpBmih->biSize = sizeof(BITMAPINFOHEADER); m_lpBmih->biWidth = size.cx; m_lpBmih->biHeight = size.cy; m_lpBmih->biPlanes = 1; m_lpBmih->biBitCount = iBitCount; m_lpBmih->biCompression = BI_RGB; m_lpBmih->biSizeImage = 0; m_lpBmih->biXPelsPerMeter = 0; m_lpBmih->biYPelsPerMeter = 0; m_lpBmih->biClrUsed = m_iColorTableEntries; m_lpBmih->biClrImportant = m_iColorTableEntries; m_lpvColorTable = (LPBYTE) m_lpBmih + sizeof(BITMAPINFOHEADER); MakeColorTable(); m_lpImage = new BYTE[m_dwSizeImage]; //m_lpImage = (unsigned char*)m_lpBmih + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries;}RxDib::RxDib(LPVOID hDib){ m_hFile = NULL; m_hBitmap = NULL; m_lpBmih = NULL; m_lpImage = NULL; m_bCompressed = FALSE; Empty(); ComputePaletteSize(((LPBITMAPINFOHEADER)hDib)->biBitCount); CSize size(((LPBITMAPINFOHEADER)hDib)->biWidth, ((LPBITMAPINFOHEADER)hDib)->biHeight); m_dwSizeImage = ComputeMetrics(size, ((LPBITMAPINFOHEADER)hDib)->biBitCount); // no compression //m_lpBmih = (LPBITMAPINFOHEADER) new // char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries + m_dwSizeImage]; m_lpBmih = (LPBITMAPINFOHEADER) new char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries]; CopyMemory(m_lpBmih, hDib, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries); m_lpvColorTable = (LPBYTE) m_lpBmih + sizeof(BITMAPINFOHEADER); m_lpImage = new BYTE[m_dwSizeImage]; CopyMemory(m_lpImage, (BYTE*)hDib + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries, m_dwSizeImage); //m_lpImage = (unsigned char*)m_lpBmih + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries;}// 16/24 bit dc牢 版快 32bit侩栏肺 dib甫 备己茄促RxDib::RxDib(CDC* pDC, const CSize& szImage){ m_hFile = NULL; m_hBitmap = NULL; m_lpBmih = NULL; m_lpImage = NULL; Empty(); BOOL bGotBits; CDC memDC; memDC.CreateCompatibleDC(pDC); CBitmap bitmap; bitmap.CreateCompatibleBitmap(pDC, szImage.cx, szImage.cy); CBitmap *pOldBitmap = (CBitmap*)memDC.SelectObject(&bitmap); memDC.BitBlt(0, 0, szImage.cx, szImage.cy, pDC, 0, 0, SRCCOPY); BITMAP bm; bitmap.GetBitmap(&bm); int iDeviceBitCount = bm.bmBitsPixel * bm.bmPlanes; int iBitCount = 32; LPBITMAPINFOHEADER pBmih; LPBYTE pImage; int iImageBuffer; if (iDeviceBitCount == 16 || iDeviceBitCount == 24) { ComputePaletteSize(iDeviceBitCount); iImageBuffer = ComputeMetrics(CSize(bm.bmWidth, bm.bmHeight), iDeviceBitCount); pBmih = (LPBITMAPINFOHEADER) new char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries + iImageBuffer]; pBmih->biSize = sizeof(BITMAPINFOHEADER); pBmih->biWidth = bm.bmWidth; pBmih->biHeight = bm.bmHeight; pBmih->biPlanes = 1; pBmih->biBitCount = iDeviceBitCount; pBmih->biCompression = BI_RGB; pBmih->biSizeImage = 0; pBmih->biXPelsPerMeter = 0; pBmih->biYPelsPerMeter = 0; pBmih->biClrUsed = m_iColorTableEntries; pBmih->biClrImportant = m_iColorTableEntries; m_lpvColorTable = (LPBYTE)pBmih + sizeof(BITMAPINFOHEADER); MakeColorTable(); pImage = (unsigned char*)pBmih + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries; bGotBits = GetDIBits(memDC.GetSafeHdc(), (HBITMAP)bitmap.GetSafeHandle(), 0L, // Start scan line (DWORD)pBmih->biHeight, // # of scan lines pImage, // address for bitmap bits (LPBITMAPINFO)pBmih, // address of bitmapinfo (DWORD)DIB_RGB_COLORS); // Use RGB for color table // memory俊辑 啊廉坷绰 巴俊 己傍窍搁 32bit DIB肺 函券 if (bGotBits) { ComputePaletteSize(iBitCount); m_dwSizeImage = ComputeMetrics(CSize(bm.bmWidth, bm.bmHeight), iBitCount); //m_lpBmih = (LPBITMAPINFOHEADER) new // char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries + m_dwSizeImage]; m_lpBmih = (LPBITMAPINFOHEADER) new char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries]; // Initialize the bitmapinfoheader m_lpBmih->biSize = sizeof(BITMAPINFOHEADER); m_lpBmih->biWidth = bm.bmWidth; m_lpBmih->biHeight = bm.bmHeight; m_lpBmih->biPlanes = 1; m_lpBmih->biBitCount = iBitCount; m_lpBmih->biCompression = BI_RGB; m_lpBmih->biSizeImage = 0; m_lpBmih->biXPelsPerMeter = 0; m_lpBmih->biYPelsPerMeter = 0; m_lpBmih->biClrUsed = m_iColorTableEntries; m_lpBmih->biClrImportant = m_iColorTableEntries; m_lpvColorTable = (LPBYTE)m_lpBmih + sizeof(BITMAPINFOHEADER); MakeColorTable(); m_lpImage = new BYTE[m_dwSizeImage]; //m_lpImage = (unsigned char*)m_lpBmih + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries; if (iDeviceBitCount == 16) Convert16To32(bm.bmWidth, bm.bmHeight, m_lpImage, (unsigned short*)pImage); else Convert24To32(bm.bmWidth, bm.bmHeight, m_lpImage, pImage); } else Empty(); delete[] pBmih; } else { ComputePaletteSize(iBitCount); m_dwSizeImage = ComputeMetrics(CSize(bm.bmWidth, bm.bmHeight), iBitCount); //m_lpBmih = (LPBITMAPINFOHEADER) new // char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries + m_dwSizeImage]; m_lpBmih = (LPBITMAPINFOHEADER) new char[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries]; // Initialize the bitmapinfoheader m_lpBmih->biSize = sizeof(BITMAPINFOHEADER); m_lpBmih->biWidth = bm.bmWidth; m_lpBmih->biHeight = bm.bmHeight; m_lpBmih->biPlanes = 1; m_lpBmih->biBitCount = iBitCount; m_lpBmih->biCompression = BI_RGB; m_lpBmih->biSizeImage = 0; m_lpBmih->biXPelsPerMeter = 0; m_lpBmih->biYPelsPerMeter = 0; m_lpBmih->biClrUsed = m_iColorTableEntries; m_lpBmih->biClrImportant = m_iColorTableEntries; m_lpvColorTable = (LPBYTE)m_lpBmih + sizeof(BITMAPINFOHEADER); MakeColorTable(); m_lpImage = new BYTE[m_dwSizeImage]; //m_lpImage = (unsigned char*)m_lpBmih + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * m_iColorTableEntries; bGotBits = GetDIBits(memDC.GetSafeHdc(), (HBITMAP)bitmap.GetSafeHandle(), 0L, // Start scan line (DWORD)m_lpBmih->biHeight, // # of scan lines m_lpImage, // address for bitmap bits (LPBITMAPINFO)m_lpBmih, // address of bitmapinfo (DWORD)DIB_RGB_COLORS); // Use RGB for color table if (!bGotBits) Empty(); } memDC.SelectObject(pOldBitmap); bitmap.DeleteObject();}RxDib::RxDib(const RxDib &dib){ m_hFile = NULL; m_hBitmap = NULL; m_lpBmih = NULL; m_lpImage = NULL; 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);}RxDib::~RxDib(){ Empty();}CSize RxDib::GetDimensions(){ if(m_lpBmih == NULL) return CSize(0, 0); return CSize((int) m_lpBmih->biWidth, (int) m_lpBmih->biHeight);}BOOL RxDib::Draw(CDC* pDC, CPoint origin, CSize size){ if (m_lpBmih == NULL || m_bCompressed) return FALSE; ::SetStretchBltMode(pDC->GetSafeHdc(), HALFTONE); ::SetDIBitsToDevice(pDC->GetSafeHdc(), origin.x, origin.y, size.cx, size.cy, 0, 0, 0, m_lpBmih->biHeight, m_lpImage, (LPBITMAPINFO)m_lpBmih, DIB_RGB_COLORS); return TRUE;}BOOL RxDib::DrawForAvi(CDC *pDC, CPoint origin, CSize size){ if (m_lpBmih == NULL || m_bCompressed) 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); //::SetDIBitsToDevice(pDC->GetSafeHdc(), origin.x, origin.y, size.cx, size.cy, // 0, 0, 0, m_lpBmih->biHeight, m_lpImage, (LPBITMAPINFO)m_lpBmih, DIB_RGB_COLORS); return TRUE;}BOOL RxDib::Read(CString strFilename, int iType /* = IT_BMP */){ BOOL bRet = FALSE; switch (iType) { case IT_BMP: bRet = ReadBMP(strFilename); break; case IT_JPG: case IT_JPG32: bRet = ReadJPG(strFilename); if (iType == IT_JPG32) { LPBYTE pTemp; switch (m_lpBmih->biBitCount) { case 16: m_dwSizeImage = ComputeMetrics(CSize(m_lpBmih->biWidth, m_lpBmih->biHeight), 32); pTemp = m_lpImage; m_lpImage = new BYTE[m_dwSizeImage]; Convert16To32(m_lpBmih->biWidth, m_lpBmih->biHeight, m_lpImage, (unsigned short*)pTemp); m_lpBmih->biBitCount = 32; delete[] pTemp; break; case 24: m_dwSizeImage = ComputeMetrics(CSize(m_lpBmih->biWidth, m_lpBmih->biHeight), 32); pTemp = m_lpImage; m_lpImage = new BYTE[m_dwSizeImage]; Convert24To32(m_lpBmih->biWidth, m_lpBmih->biHeight, m_lpImage, pTemp, TRUE); m_lpBmih->biBitCount = 32; delete[] pTemp; break; } } break; } return bRet;}BOOL RxDib::ReadBMP(CString strFilename){ Empty(); int iCount, iSize; BITMAPFILEHEADER bmfh; BITMAPINFOHEADER bmih; LPBYTE pbyBuffer; CString strMessage; CFile fileBitmap; TRY { if (!fileBitmap.Open(strFilename, CFile::modeRead)) return FALSE; iCount = fileBitmap.Read((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER)); if (iCount != sizeof(BITMAPFILEHEADER)) {// strMessage.LoadString(IDS_DIB_INVALID_BITMAP);// AfxMessageBox(strMessage); return FALSE; } if (bmfh.bfType != 0x4d42) {// strMessage.LoadString(IDS_DIB_INVALID_BITMAP);// AfxMessageBox(strMessage); return FALSE; } iSize = bmfh.bfOffBits - sizeof(BITMAPFILEHEADER); pbyBuffer = new BYTE[iSize]; iCount = fileBitmap.Read(pbyBuffer, iSize); // info hdr & color table bmih = *(BITMAPINFOHEADER*)pbyBuffer; m_dwSizeImage = bmih.biSizeImage; if (m_dwSizeImage == 0) m_dwSizeImage = ComputeMetrics(CSize(bmih.biWidth, bmih.biHeight), bmih.biBitCount); ComputePaletteSize(bmih.biBitCount); //m_lpBmih = (LPBITMAPINFOHEADER)new unsigned char[sizeof(BITMAPINFOHEADER) + m_iColorTableEntries * sizeof(RGBQUAD) + m_dwSizeImage]; m_lpBmih = (LPBITMAPINFOHEADER)new unsigned char[sizeof(BITMAPINFOHEADER) + m_iColorTableEntries * sizeof(RGBQUAD)]; memcpy(m_lpBmih, pbyBuffer, iSize); delete[] pbyBuffer; m_lpvColorTable = (LPBYTE) m_lpBmih + sizeof(BITMAPINFOHEADER); m_lpImage = new BYTE[m_dwSizeImage]; //m_lpImage = (LPBYTE)m_lpvColorTable + m_iColorTableEntries * sizeof(RGBQUAD); iCount = fileBitmap.Read(m_lpImage, m_dwSizeImage); // image only } CATCH (CException, e) {// strMessage.LoadString(IDS_DIB_READ_ERROR);// AfxMessageBox(strMessage); return FALSE; } END_CATCH fileBitmap.Close(); return TRUE;}BOOL RxDib::ReadJPG(CString strFilename){ // 1. read file header to get size of info hdr + color table // 2. read info hdr (to get image size) and color table // 3. read image // can't use bfSize in file header //LPBYTE lpInputImage = NULL; Empty(); DWORD dwWidth; DWORD dwHeight; DWORD dwNumChannel; DWORD dwBufferSize; BYTE *pbyBuffer = NULL; PSTR pstrFilename; pstrFilename = ToMultiByte(strFilename); if (!DecodeJPGFileToGeneralBuffer(pstrFilename, &dwWidth, &dwHeight, &dwNumChannel, &pbyBuffer, &dwBufferSize)) return FALSE; delete[] pstrFilename; switch (dwNumChannel) { case 3: ComputePaletteSize(24); break; case 4: ComputePaletteSize(32); break; } m_dwSizeImage = dwBufferSize; //m_lpBmih = (LPBITMAPINFOHEADER)new unsigned char[sizeof(BITMAPINFOHEADER) + m_iColorTableEntries * sizeof(RGBQUAD) + dwBufferSize]; m_lpBmih = (LPBITMAPINFOHEADER)new unsigned char[sizeof(BITMAPINFOHEADER) + m_iColorTableEntries * sizeof(RGBQUAD)]; m_lpBmih->biSize = sizeof(BITMAPINFOHEADER); m_lpBmih->biWidth = dwWidth; m_lpBmih->biHeight = dwHeight; m_lpBmih->biPlanes = 1; m_lpBmih->biBitCount = 24; m_lpBmih->biCompression = 0;//BI_JPEG; m_lpBmih->biSizeImage = dwBufferSize; m_lpBmih->biClrUsed = 0; m_lpBmih->biClrImportant = 0; m_lpvColorTable = (LPBYTE) m_lpBmih + sizeof(BITMAPINFOHEADER); m_lpImage = new BYTE[m_dwSizeImage]; //m_lpImage = (LPBYTE)m_lpvColorTable + m_iColorTableEntries * sizeof(RGBQUAD);////////////////////////////////////// /* IplImage *srcImage; IplImage *dstImage;// ASSERT(dwWidth % 4 == 0 && dwHeight % 4 == 0); srcImage = iplCreateImageHeader( 3, // number of channels 0, // no alpha channel 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 dstImage = iplCreateImageHeader( 3, // number of channels 0, // no alpha channel
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -