dibapi.cpp
来自「一个非常全的vc编程的原程序代码是关于图像处理的!」· C++ 代码 · 共 2,454 行 · 第 1/5 页
CPP
2,454 行
{
memcpy(lpTarget->bmiColors, lpSource->bmiColors, 16*sizeof(RGBQUAD));
}
else
{
MessageBox(NULL, "error no match color table", "error", MB_OK);
return FALSE;
}
}
break;
}
case 1:
{
lpTarget->bmiColors[0].rgbRed = 0;
lpTarget->bmiColors[0].rgbGreen = 0;
lpTarget->bmiColors[0].rgbBlue = 0;
lpTarget->bmiColors[0].rgbReserved = 0;
lpTarget->bmiColors[1].rgbRed = 255;
lpTarget->bmiColors[1].rgbGreen = 255;
lpTarget->bmiColors[1].rgbBlue = 255;
lpTarget->bmiColors[1].rgbReserved = 0;
break;
}
case 32:
case 24:
case 16:
default:
break;
}
return TRUE;
}
/************************************************************
Create a new DIB of the requested format,copy the source
image to the new DIB.
parameter: LPBYTE(source), UINT(width), UINT(height),
UINT(colornum), BOOL(stretch?), HPALETTE
return: HDIB
************************************************************/
HDIB ConvertDIBFormat(LPBYTE lpDib, UINT nWidth, UINT nHeight,
UINT nBpp, BOOL bStretch, HPALETTE hPalSrc)
{
LPBITMAPINFO lpSrcDib = (LPBITMAPINFO)lpDib;
LPBITMAPINFO lpbmi = NULL;
LPBYTE lpSourceBits, lpTargetBits, lpResult;
HDC hDC = NULL, hSourceDC, hTargetDC;
HBITMAP hSourceBitmap, hTargetBitmap, hOldTargetBitmap,hOldSourceBitmap;
DWORD dwSourceBitsSize, dwTargetBitsSize, dwTargetHeaderSize,dwColorNum;
HDIB hDib;
if(nBpp <= 8)
dwColorNum = 1<<nBpp;
else
dwColorNum = 0;
dwTargetHeaderSize = sizeof(BITMAPINFO)+(dwColorNum*sizeof(RGBQUAD));
lpbmi = (LPBITMAPINFO)malloc(dwTargetHeaderSize);
lpbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
lpbmi->bmiHeader.biWidth = nWidth;
lpbmi->bmiHeader.biHeight = nHeight;
lpbmi->bmiHeader.biPlanes = 1;
lpbmi->bmiHeader.biBitCount = nBpp;
lpbmi->bmiHeader.biCompression = BI_RGB;
lpbmi->bmiHeader.biSizeImage = 0;
lpbmi->bmiHeader.biXPelsPerMeter = 0;
lpbmi->bmiHeader.biYPelsPerMeter = 0;
lpbmi->bmiHeader.biClrUsed = 0;
lpbmi->bmiHeader.biClrImportant = 0;
if(!CopyColorTable(lpbmi, (LPBITMAPINFO)lpSrcDib, hPalSrc))
{
free(lpbmi);
return NULL;
}
hDC = GetDC(NULL);
hTargetBitmap = CreateDIBSection(hDC, lpbmi, DIB_RGB_COLORS, (VOID**)&lpTargetBits, NULL, 0);
hSourceBitmap = CreateDIBSection(hDC, lpSrcDib, DIB_RGB_COLORS, (VOID**)&lpSourceBits, NULL, 0);
hSourceDC = CreateCompatibleDC(hDC);
hTargetDC = CreateCompatibleDC(hDC);
dwSourceBitsSize = lpSrcDib->bmiHeader.biHeight*BytesPerLine((LPBYTE)&(lpSrcDib->bmiHeader));
dwTargetBitsSize = lpbmi->bmiHeader.biHeight*BytesPerLine((LPBYTE)&(lpSrcDib->bmiHeader));
memcpy(lpSourceBits, FindDIBBits((LPBYTE)lpSrcDib), dwSourceBitsSize);
lpbmi->bmiHeader.biSizeImage = dwTargetBitsSize;
hOldSourceBitmap = (HBITMAP)SelectObject(hSourceDC, hSourceBitmap);
hOldTargetBitmap = (HBITMAP)SelectObject(hTargetDC, hTargetBitmap);
if(lpSrcDib->bmiHeader.biBitCount <= 8)
SetDIBColorTable( hSourceDC,
0,
1<<lpSrcDib->bmiHeader.biBitCount,
lpSrcDib->bmiColors);
if(lpbmi->bmiHeader.biBitCount <= 8)
SetDIBColorTable( hTargetDC,
0,
1<<lpSrcDib->bmiHeader.biBitCount,
lpSrcDib->bmiColors);
if((lpSrcDib->bmiHeader.biWidth == lpbmi->bmiHeader.biWidth)
&& (lpSrcDib->bmiHeader.biHeight == lpbmi->bmiHeader.biHeight))
{
BitBlt( hTargetDC,
0,
0,
lpbmi->bmiHeader.biWidth,
lpbmi->bmiHeader.biHeight,
hSourceDC,
0,
0,
SRCCOPY);
}
else
{
if(bStretch)
{
SetStretchBltMode(hTargetDC, COLORONCOLOR);
StretchBlt( hTargetDC,
0,
0,
lpbmi->bmiHeader.biWidth,
lpbmi->bmiHeader.biHeight,
hSourceDC,
0,
0,
lpSrcDib->bmiHeader.biWidth,
lpSrcDib->bmiHeader.biHeight,
SRCCOPY);
}
else
{
BitBlt( hTargetDC,
0,
0,
lpbmi->bmiHeader.biWidth,
lpbmi->bmiHeader.biHeight,
hSourceDC,
0,
0,
SRCCOPY);
}
}
SelectObject(hSourceDC, hOldSourceBitmap);
SelectObject(hTargetDC, hOldTargetBitmap);
DeleteDC(hSourceDC);
DeleteDC(hTargetDC);
ReleaseDC(NULL, hDC);
GdiFlush();
hDib = GlobalAlloc(GHND,dwTargetHeaderSize+dwTargetBitsSize);
lpResult = (LPBYTE)GlobalLock(hDib);
memcpy(lpResult, lpbmi, dwTargetHeaderSize);
memcpy(FindDIBBits((LPBYTE)lpResult), lpTargetBits, dwTargetBitsSize);
DeleteObject(hTargetBitmap);
DeleteObject(hSourceBitmap);
free(lpbmi);
GlobalUnlock(hDib);
return hDib;
}
/************************************************************
Create a new DIB of the requested format, copy the source
image to the new DIB.
parameter: HDIB(hDib), UINT(width), UINT(height),
UINT(colornum), BOOL(stretch?), HPALETTE
return: HDIB
************************************************************/
HDIB ConvertDIBFormat(HDIB hDib, UINT nWidth, UINT nHeight,
UINT nBpp, BOOL bStretch, HPALETTE hPalSrc)
{
LPBYTE lpDib;
lpDib = (LPBYTE)GlobalLock(hDib);
HDIB hNewDib;
hNewDib = ConvertDIBFormat( lpDib,
nWidth,
nHeight,
nBpp,
bStretch,
hPalSrc);
GlobalUnlock(hDib);
return hNewDib;
}
/************************************************************
Create a new DIB of the requested format, copy the source
image to the new DIB.
parameter: LPBYTE(source), UINT(colornum), HPALETTE
return: HDIB
************************************************************/
HDIB ConvertDIBFormat(LPBYTE lpSrcDib, UINT nBpp, HPALETTE hPalSrc)
{
return ConvertDIBFormat(lpSrcDib,
(UINT)DIBWidth(lpSrcDib),
(UINT)DIBHeight(lpSrcDib),
nBpp,
FALSE,
hPalSrc);
}
/************************************************************
Create a new DIB of the requested format, copy the source
image to the new DIB.
parameter: HDIB(source), UINT(colornum), HPALETTE
return: HDIB
************************************************************/
HDIB ConvertDIBFormat(HDIB hDib, UINT nBpp, HPALETTE hPalSrc)
{
return ConvertDIBFormat(hDib,
(UINT)DIBWidth(hDib),
(UINT)DIBHeight(hDib),
nBpp,
FALSE,
hPalSrc);
}
/**************************************************
create a DIB from a bitmap using the specified palette.
parameter: HBITMAP, HPALETTE
return: HDIB
**************************************************/
HDIB BitmapToDIB(HBITMAP hBitmap, HPALETTE hPalette)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HDIB hDib, h;
HDC hDC;
WORD biBits;
if(!hBitmap)
return NULL;
if(!GetObject(hBitmap, sizeof(bm), (LPBYTE)&bm))
return NULL;
if(hPalette == NULL)
hPalette = (HPALETTE)GetStockObject(DEFAULT_PALETTE);
biBits = bm.bmPlanes*bm.bmBitsPixel;
if(biBits <= 1)
biBits = 1;
else if(biBits <= 4)
biBits = 4;
else if(biBits <= 8)
biBits = 8;
else
biBits = 24;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = biBits;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwLen = bi.biSize+PaletteSize((LPBYTE)&bi);
hDC = GetDC(NULL);
hPalette = SelectPalette(hDC, hPalette, FALSE);
RealizePalette(hDC);
hDib = GlobalAlloc(GHND, dwLen);
if(!hDib)
{
SelectPalette(hDC, hPalette, TRUE);
RealizePalette(hDC);
ReleaseDC(NULL, hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
GetDIBits( hDC,
hBitmap,
0,
(UINT)bi.biHeight,
NULL,
(LPBITMAPINFO)lpbi,
DIB_RGB_COLORS);
bi = *lpbi;
GlobalUnlock(hDib);
if(bi.biSizeImage == 0)
bi.biSizeImage = WIDTHBYTES((DWORD)bm.bmWidth*biBits)*bm.bmHeight;
dwLen = bi.biSize+PaletteSize((LPBYTE)&bi)+bi.biSizeImage;
if(h = GlobalReAlloc(hDib, dwLen, 0))
hDib = h;
else
{
GlobalFree(hDib);
hDib = NULL;
SelectPalette(hDC, hPalette, TRUE);
RealizePalette(hDC);
ReleaseDC(NULL, hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
if(GetDIBits( hDC,
hBitmap,
0,
(UINT)bi.biHeight,
(LPBYTE)lpbi+(WORD)(lpbi->biSize)+PaletteSize((LPBYTE)lpbi),
(LPBITMAPINFO)lpbi,
DIB_RGB_COLORS)
== 0)
{
GlobalUnlock(hDib);
hDib = NULL;
SelectPalette(hDC, hPalette, TRUE);
RealizePalette(hDC);
ReleaseDC(NULL, hDC);
return NULL;
}
bi = *lpbi;
GlobalUnlock(hDib);
SelectPalette(hDC, hPalette, TRUE);
RealizePalette(hDC);
ReleaseDC(NULL, hDC);
return hDib;
}
HDIB BitmapToDIB(HBITMAP hBitmap, HPALETTE hPalette, WORD wBitCount)
{
HDIB hNewDib;
if(!hBitmap)
return NULL;
BITMAP bm;
GetObject(hBitmap, sizeof(bm), (LPBYTE)&bm);
int biBits = bm.bmPlanes*bm.bmBitsPixel;
if(biBits <= 1)
biBits = 1;
else if(biBits <= 4)
biBits = 4;
else if(biBits <= 8)
biBits = 8;
else
biBits = 24;
HDIB hDib = BitmapToDIB(hBitmap, hPalette);
if(!hDib)
return NULL;
if(wBitCount == biBits)
hNewDib = hDib;
else
{
hNewDib = ConvertDIBFormat(hDib, wBitCount, hPalette);
GlobalFree(hDib);
}
return hNewDib;
}
/**************************************************
DDB to DIB.
parameter: HBITMAP, HPALETTE, WORD
return: HDIB
**************************************************/
HBITMAP DIBToBitmap(HDIB hDib, HPALETTE hPalette)
{
LPBYTE lpDibHdr, lpDibBits;
HBITMAP hBitmap;
HDC hDC;
HPALETTE hOldPal = NULL;
if(!hDib)
return NULL;
lpDibHdr = (LPBYTE)GlobalLock(hDib);
lpDibBits = FindDIBBits(lpDibHdr);
hDC = GetDC(NULL);
if(!hDC)
{
GlobalUnlock(hDib);
return NULL;
}
if(hPalette)
{
hOldPal = SelectPalette(hDC, hPalette, FALSE);
RealizePalette(hDC);
}
hBitmap = CreateDIBitmap( hDC,
(LPBITMAPINFOHEADER)lpDibHdr,
CBM_INIT,
lpDibBits,
(LPBITMAPINFO)lpDibHdr,
DIB_RGB_COLORS);
if(hOldPal)
SelectPalette(hDC, hOldPal, FALSE);
ReleaseDC(NULL, hDC);
GlobalUnlock(hDib);
return hBitmap;
}
/*************************************************
convert the bits per pixel and/or the compression
format of the specified DDB.
parameter: HBITMAP, WORD(wBitCount),
WORD(dwCompression), HPALETTE
return: HDIB
*************************************************/
HDIB ChangeBitmapFormat(HBITMAP hBitmap, WORD biBits, DWORD biCompression, HPALETTE hPalette)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HDIB hDib;
HANDLE h;
HDC hDC;
if(!hBitmap)
return NULL;
if(hPalette == NULL)
hPalette = (HPALETTE)GetStockObject(DEFAULT_PALETTE);
GetObject(hBitmap, sizeof(bm), (LPBYTE)&bm);
if(biBits == 0)
{
biBits = bm.bmPlanes*bm.bmBitsPixel;
if(biBits <= 1)
biBits = 1;
else if(biBits <= 4)
biBits = 4;
else if(biBits <= 8)
biBits = 8;
else
biBits = 24;
}
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = biBits;
bi.biCompression = biCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwLen = bi.biSize+PaletteSize((LPBYTE)&bi);
hDC = GetDC(NULL);
HPALETTE hPalT = SelectPalette(hDC, hPalette, FALSE);
RealizePalette(hDC);
hDib = GlobalAlloc(GHND, dwLen);
if(!hDib)
{
SelectPalette(hDC, hPalT, FALSE);
ReleaseDC(NULL, hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
*lpbi = bi;
GetDIBits(hDC, hBitmap, 0L, (DWORD)bi.biHeight, (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
bi = *lpbi;
GlobalUnlock(hDib);
if(bi.biSizeImage == 0)
{
bi.biSizeImage = WIDTHBYTES((DWORD)bm.bmWidth*biBits)*bm.bmHeight;
if(biCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage*3)/2;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?