📄 dib.cpp
字号:
// return
return UpdateInternal();
}
BOOL CDib::ColorQuantize(int nColorBits)
{
HDIB hNewDib;
if (nColorBits > 8)
hNewDib = ConvertDIBFormat(m_hDib, nColorBits, NULL);
else
{
int nColors = 1<<nColorBits;
hNewDib = ColorQuantizeDIB(m_hDib, nColorBits, nColors);
}
if (! hNewDib)
return FALSE;
Destroy();
m_hDib = hNewDib;
return UpdateInternal();
}
BOOL CDib::ChangeToGrayscale(int nMethod, double fRedWeight, double fGreenWeight, double fBlueWeight)
{
HPALETTE hPal = ConvertToGrayscale(m_hDib, nMethod, fRedWeight, fGreenWeight, fBlueWeight);
if (hPal == NULL)
return FALSE;
// set new palette
if (m_pPalette != NULL)
delete m_pPalette;
m_pPalette = new CPalette;
m_pPalette->Attach(hPal);
// rebuild bitmap
return UpdateInternal();
}
BOOL CDib::HighPass(int Strength, int nAlgorithm)
{
if (! HighPassDIB(m_hDib, Strength, nAlgorithm))
return FALSE;
// return
return UpdateInternal();
}
BOOL CDib::LowPass(int Strength, int nAlgorithm)
{
if (! LowPassDIB(m_hDib, Strength, nAlgorithm))
return FALSE;
// return
return UpdateInternal();
}
BOOL CDib::EdgeEnhance(int Strength, int nAlgorithm)
{
if (! EdgeEnhanceDIB(m_hDib, Strength, nAlgorithm))
return FALSE;
// return
return UpdateInternal();
}
BOOL CDib::MedianFilter()
{
if (! MedianFilterDIB(m_hDib))
return FALSE;
// return
return UpdateInternal();
}
BOOL CDib::ConvertFormat(WORD wBitCount)
{
if (IsEmpty())
return FALSE;
if (GetBitCount() == wBitCount)
return TRUE;
HDIB hNewDib = ConvertDIBFormat(m_hDib, wBitCount, NULL);
if (! hNewDib)
return FALSE;
Destroy();
m_hDib = hNewDib;
return UpdateInternal();
}
/////////////////////////////////////////////////////////////////////////////////
// 函数说明:Thining:细化图像
// 参数说明:hDib为CDIB的句柄
///////////////////////////////////////////////////////////////////////////////
BOOL CDib::Thining(HDIB hDib)
{
LONG x,y;
int num;
BOOL Finished;
int nw,n,ne,w,e,sw,s,se;
//细化时的判断数组
static int erasetable[256]=
{
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1,1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,
1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,1,1,0,1,1,1,0,0,
1,1,0,0,1,1,1,0,1,1,0,0,1,0,0,0
};
WaitCursorBegin();
HDIB hNewDib = NULL;
//将图像转化成24位图像,然后再处理
WORD wBitCount = DIBBitCount(hDib);
HANDLE h;
PLOGPALETTE plogPal;
h = GlobalAlloc(GHND, sizeof(DWORD) + sizeof(PALETTEENTRY)*256);
if (! h) return FALSE;
plogPal = (PLOGPALETTE)GlobalLock(h);
if (! plogPal) return FALSE;
plogPal->palVersion=PALVERSION;
plogPal->palNumEntries=256;
plogPal->palPalEntry[0].peGreen= 0;
plogPal->palPalEntry[0].peBlue= 0;
plogPal->palPalEntry[0].peRed= 0;
plogPal->palPalEntry[0].peFlags=NULL;
plogPal->palPalEntry[1].peGreen=255;
plogPal->palPalEntry[1].peBlue=255;
plogPal->palPalEntry[1].peRed=255;
plogPal->palPalEntry[1].peFlags=NULL;
HPALETTE hpl=CreatePalette(plogPal);
HPALETTE palold=NULL;
if (wBitCount != 8)
{
if(wBitCount!=24)
{
CPalette* Cpal=GetPalette();
palold=(HPALETTE)Cpal->GetSafeHandle();
}
hNewDib = ConvertDIBFormat(hDib,8, hpl);
}
else
hNewDib = ConvertDIBFormat(hDib,8, hpl);
if (! hNewDib)
{
WaitCursorEnd();
return FALSE;
}
//新的位图属性
WORD wDIBWidth = (WORD)DIBWidth(hNewDib);
WORD wDIBHeight = (WORD)DIBHeight(hNewDib);
WORD wBytesPerLine = (WORD)BytesPerLine(hNewDib);
DWORD dwImageSize = wBytesPerLine * wDIBHeight;
//锁定图像的那查来过滤数据
HGLOBAL hFilteredBits = GlobalAlloc(GHND, dwImageSize);
if (!hFilteredBits)
{
WaitCursorEnd();
return FALSE;
}
LPBYTE lpPtr = (LPBYTE)GlobalLock(hFilteredBits);
//获得位图中的位数据
LPBYTE lpDIB = (LPBYTE)GlobalLock(hNewDib);
LPBYTE lpDIBits = FindDIBBits(lpDIB);
memcpy(lpPtr,lpDIBits, dwImageSize);
int judge=1;
Finished=FALSE;
//开始细化
while(!Finished)
{
Finished=TRUE;
for (y=1;y<wDIBHeight-1;y++)
{
lpPtr=(unsigned char *)lpDIBits+(dwImageSize-wBytesPerLine-y*wBytesPerLine);
x=1;
while(x<wDIBWidth-1)
{
if(*(lpPtr+x)==0)
{
w=(unsigned char)*(lpPtr+x-1);
e=(unsigned char)*(lpPtr+x+1);
if( (w==judge)|| (e==judge))
{
nw=(unsigned char)*(lpPtr+x+wBytesPerLine-1);
n=(unsigned char)*(lpPtr+x+wBytesPerLine);
ne=(unsigned char)*(lpPtr+x+wBytesPerLine+1);
sw=(unsigned char)*(lpPtr+x-wBytesPerLine-1);
s=(unsigned char)*(lpPtr+x-wBytesPerLine);
se=(unsigned char)*(lpPtr+x-wBytesPerLine+1);
num=nw/judge+n/judge*2+ne/judge*4+w/judge*8+e/judge*16+sw/judge*32+s/judge*64+se/judge*128;
//判断是否能够被去掉
if(erasetable[num]==1)
{
*(lpPtr+x)=(BYTE)judge;
Finished=FALSE;
x++;
}
}
}
x++;
}
}
for (x=1;x<wDIBWidth-1;x++)
{
y=1;
while(y<wDIBHeight-1)
{
lpPtr=(unsigned char *)lpDIBits+(dwImageSize-wBytesPerLine-y*wBytesPerLine);
if(*(lpPtr+x)==0)
{
n=(unsigned char)*(lpPtr+x+wBytesPerLine);
s=(unsigned char)*(lpPtr+x-wBytesPerLine);
if( (n==judge)|| (s==judge))
{
nw=(unsigned char)*(lpPtr+x+wBytesPerLine-1);
ne=(unsigned char)*(lpPtr+x+wBytesPerLine+1);
w=(unsigned char)*(lpPtr+x-1);
e=(unsigned char)*(lpPtr+x+1);
sw=(unsigned char)*(lpPtr+x-wBytesPerLine-1);
se=(unsigned char)*(lpPtr+x-wBytesPerLine+1);
num=nw/judge+n/judge*2+ne/judge*4+w/judge*8+e/judge*16+sw/judge*32+s/judge*64+se/judge*128;
//判断是否能够被去掉
if(erasetable[num]==1)
{
*(lpPtr+x)=(BYTE)judge;
Finished=FALSE;
y++;
}
}
}
y++;
}
}
}
GlobalUnlock(hFilteredBits);
GlobalFree(hFilteredBits);
GlobalUnlock(hNewDib);
//将24位图像转化成原先位数图像
HDIB hTmp = NULL;
if (wBitCount != 8)
{
if(wBitCount ==24)
hTmp = ConvertDIBFormat(hNewDib, wBitCount, NULL);
else
hTmp = ConvertDIBFormat(hNewDib, wBitCount, palold);
}
else
hTmp = CopyHandle(hNewDib);
GlobalUnlock(h);
GlobalFree(h);
GlobalFree(hNewDib);
DWORD dwSize = GlobalSize(hTmp);
//将原先的位图数据替换成处理后的位图数据
memcpy((LPBYTE)GlobalLock(hDib), (LPBYTE)GlobalLock(hTmp), dwSize);
GlobalUnlock(hTmp);
GlobalUnlock(hDib);
WaitCursorEnd();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////////
// 函数说明:Followingtreatment短枝处理
// 参数说明:para为要除掉的短枝象素点数,hDib为CDIB的句柄
///////////////////////////////////////////////////////////////////////////////
BOOL CDib::Followingtreatment(int para,HDIB hDib)
{
LONG x,y;
int mun;
//表示8个位置的变量
int nw,n,ne,w,e,sw,s,se;
WaitCursorBegin();
HDIB hNewDib = NULL;
WORD wBitCount = DIBBitCount(hDib);
HANDLE h;
PLOGPALETTE plogPal;
h = GlobalAlloc(GHND, sizeof(DWORD) + sizeof(PALETTEENTRY)*256);
if (! h) return FALSE;
plogPal = (PLOGPALETTE)GlobalLock(h);
if (! plogPal) return FALSE;
plogPal->palVersion=PALVERSION;
plogPal->palNumEntries=256;
plogPal->palPalEntry[0].peGreen= 0;
plogPal->palPalEntry[0].peBlue= 0;
plogPal->palPalEntry[0].peRed= 0;
plogPal->palPalEntry[0].peFlags=NULL;
plogPal->palPalEntry[1].peGreen=255;
plogPal->palPalEntry[1].peBlue=255;
plogPal->palPalEntry[1].peRed=255;
plogPal->palPalEntry[1].peFlags=NULL;
HPALETTE hpl=CreatePalette(plogPal);
HPALETTE palold=NULL;
if (wBitCount != 8)
{
if(wBitCount!=24)
{
CPalette* Cpal=GetPalette();
palold=(HPALETTE)Cpal->GetSafeHandle();
}
hNewDib = ConvertDIBFormat(hDib,8, hpl);
}
else
hNewDib = ConvertDIBFormat(hDib,8, hpl);
if (! hNewDib)
{
WaitCursorEnd();
return FALSE;
}
//新位图的属性
WORD wDIBWidth = (WORD)DIBWidth(hNewDib);
WORD wDIBHeight = (WORD)DIBHeight(hNewDib);
WORD wBytesPerLine = (WORD)BytesPerLine(hNewDib);
DWORD dwImageSize = wBytesPerLine * wDIBHeight;
//分配和锁定图像数据
HGLOBAL hFilteredBits = GlobalAlloc(GHND, dwImageSize);
if (!hFilteredBits)
{
WaitCursorEnd();
return FALSE;
}
LPBYTE lpPtr = (LPBYTE)GlobalLock(hFilteredBits);
//获得DIB的位值
LPBYTE lpDIB = (LPBYTE)GlobalLock(hNewDib);
LPBYTE lpDIBits = FindDIBBits(lpDIB);
memcpy(lpPtr,lpDIBits, dwImageSize);
int judge=1;
//开始短枝处理
for(mun=1;mun<para+1;mun++)
{
for (y=1;y<wDIBHeight-1;y++)
{
lpPtr=(unsigned char *)lpDIBits+(dwImageSize-wBytesPerLine-y*wBytesPerLine);
for(x=1;x<wDIBWidth-1;x++)
{
if(*(lpPtr+x)==0)
{
w=(unsigned char)*(lpPtr+x-1);
e=(unsigned char)*(lpPtr+x+1);
nw=(unsigned char)*(lpPtr+x+wBytesPerLine-1);
n=(unsigned char)*(lpPtr+x+wBytesPerLine);
ne=(unsigned char)*(lpPtr+x+wBytesPerLine+1);
sw=(unsigned char)*(lpPtr+x-wBytesPerLine-1);
s=(unsigned char)*(lpPtr+x-wBytesPerLine);
se=(unsigned char)*(lpPtr+x-wBytesPerLine+1);
//如果满足要求,先将灰度值设为123,以便后续处理,防止设为0后,删除过渡的短枝
if((ne==judge)&&(e==judge)&&(se==judge)&&(s==judge)&&(sw==judge))
{
*(lpPtr+x)=(BYTE)123;
continue;
}
if((ne==judge)&&(e==judge)&&(se==judge)&&(n==judge)&&(nw==judge))
{
*(lpPtr+x)=(BYTE)123;
continue;
}
if((nw==judge)&&(w==judge)&&(sw==judge)&&(s==judge)&&(se==judge))
{
*(lpPtr+x)=(BYTE)123;
continue;
}
if((nw==judge)&&(n==judge)&&(ne==judge)&&(w==judge)&&(sw==judge))//&&(s==255))
{
*(lpPtr+x)=(BYTE)123;
continue;
}
}
}
}
//灰度值为123,以便后续处理,防止设为0后,删除过渡的短枝
for (y=1;y<wDIBHeight-1;y++)
{
lpPtr=(unsigned char *)lpDIBits+(dwImageSize-wBytesPerLine-y*wBytesPerLine);
for(x=1;x<wDIBWidth-1;x++)
{
if(*(lpPtr+x)==123)
{
w=(unsigned char)*(lpPtr+x-1);
e=(unsigned char)*(lpPtr+x+1);
nw=(unsigned char)*(lpPtr+x+wBytesPerLine-1);
n=(unsigned char)*(lpPtr+x+wBytesPerLine);
ne=(unsigned char)*(lpPtr+x+wBytesPerLine+1);
sw=(unsigned char)*(lpPtr+x-wBytesPerLine-1);
s=(unsigned char)*(lpPtr+x-wBytesPerLine);
se=(unsigned char)*(lpPtr+x-wBytesPerLine+1);
//判断是否是孤立点,如果是孤立点,则保留(0);否则删除(1);
if((nw==judge)&&(w==judge)&&(sw==judge)&&(n==judge)&&(s==judge)&&(ne==judge)&&(se==judge)&&(e==judge))
*(lpPtr+x)=(BYTE)0;
else
*(lpPtr+x)=(BYTE)1;
}
}
}
}
GlobalUnlock(hFilteredBits);
GlobalFree(hFilteredBits);
GlobalUnlock(hNewDib);
//重建位图,变换回原来的位数
HDIB hTmp = NULL;
if (wBitCount != 8)
{
if(wBitCount ==24)
hTmp = ConvertDIBFormat(hNewDib, wBitCount, NULL);
else
hTmp = ConvertDIBFormat(hNewDib, wBitCount, palold);
}
else
hTmp = CopyHandle(hNewDib);
GlobalUnlock(h);
GlobalFree(h);
GlobalFree(hNewDib);
DWORD dwSize = GlobalSize(hTmp);
//将原先的位图数据替换成处理后的位图数据
memcpy((LPBYTE)GlobalLock(hDib), \
(LPBYTE)GlobalLock(hTmp), dwSize);
GlobalUnlock(hTmp);
GlobalFree(hTmp);
GlobalUnlock(hDib);
WaitCursorEnd();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////////
// 函数说明:Outline提取轮廓
// 参数说明:para为要除掉的短枝象素点数,hDib为CDIB的句柄
///////////////////////////////////////////////////////////////////////////////
BOOL CDib::Outline(HDIB hDib)
{
LONG x,y;
int num;
BOOL Finished;
int nw,n,ne,w,e,sw,s,se;
WaitCursorBegin();
HDIB hNewDib = NULL;
WORD wBitCount = DIBBitCount(hDib);
//将图像转化成24位图像,然后再处理
HANDLE h;
PLOGPALETTE plogPal;
h = GlobalAlloc(GHND, sizeof(DWORD) + sizeof(PALETTEENTRY)*256);
if (! h) return FALSE;
plogPal = (PLOGPALETTE)GlobalLock(h);
if (! plogPal) return FALSE;
plogPal->palVersion=PALVERSION;
plogPal->palNumEntries=256;
plogPal->palPalEntry[0].peGreen= 0;
plogPal->palPalEntry[0].peBlue= 0;
plogPal->palPalEntry[0].peRed= 0;
plogPal->palPalEntry[0].peFlags=NULL;
plogPal->palPalEntry[1].peGreen=255;
plogPal->palPalEntry[1].peBlue=255;
plogPal->palPalEntry[1].peRed=255;
plogPal->palPalEntry[1].peFlags=NULL;
HPALETTE hpl=CreatePalette(plogPal);
HPALETTE palold=NULL;
if (wBitCount != 8)
{
if(wBitCount!=24)
{
CPalette* Cpal=GetPalette();
palold=(HPALETTE)Cpal->GetSafeHandle();
}
hNewDib = ConvertDIBFormat(hDib,8, hpl);
}
else
hNewDib = ConvertDIBFormat(hDib,8, hpl);
if (! hNewDib)
{
WaitCursorEnd();
return FALSE;
}
//新的位图属性
WORD wDIBWidth = (WORD)DIBWidth(hNewDib);
WORD wDIBHeight = (WORD)DIBHeight(hNewDib);
WORD wBytesPerLine = (WORD)BytesPerLine(hNewDib);
DWORD dwImageSize = wBytesPerLine * wDIBHeight;
//锁定要处理的位图数据
HGLOBAL hFilteredBits = GlobalAlloc(GHND, dwImageSize);
if (!hFilteredBits)
{
WaitCursorEnd();
return FALSE;
}
LPBYTE lpPtr = (LPBYTE)GlobalLock(hFilteredBits);
LPBYTE lpDIB = (LPBYTE)GlobalLock(hNewDib);
LPBYTE lpDIBits = FindDIBBits(lpDIB);
unsigned char cur;
memcpy(lpPtr,lpDIBits, dwImageSize);
//开始处理
for (y=1;y<wDIBHeight-1;y++)
{
lpPtr=(unsigned char *)lpDIBits+(dwImageSize-wBytesPerLine-y*wBytesPerLine);
for (x=1;x<wDIBWidth-1;x++)
{
cur=(unsigned char)*(lpPtr+x);
if((cur==0)||(cur==9))
{
w=(unsigned char)*(lpPtr+x-1);
e=(unsigned char)*(lpPtr+x+1);
nw=(unsigned char)*(lpPtr+x+wBytesPerLine-1);
n=(unsigned char)*(lpPtr+x+wBytesPerLine);
ne=(unsigned char)*(lpPtr+x+wBytesPerLine+1);
sw=(unsigned char)*(lpPtr+x-wBytesPerLine-1);
s=(unsigned char)*(lpPtr+x-wBytesPerLine);
se=(unsigned char)*(lpPtr+x-wBytesPerLine+1);
num=nw+n+ne+w+e+sw+s+se;
//如果不是边界,将值设为9
if(num%9==0)
*(lpPtr+x)=(unsigned char)9;
}
}
}
for (y=1;y<wDIBHeight-1;y++)
{
lpPtr=(unsigned char *)lpDIBits+(dwImageSize-wBytesPerLine-y*wBytesPerLine);
for (x=1;x<wDIBWidth-1;x++)
{
cur=(unsigned char)*(lpPtr+x);
//如果不是边界,将值设为1,为白色
if(cur==9)
*(lpPtr+x)=(unsigned char)1;
}
}
//重建位图,变换回原来的位数
GlobalUnlock(hFilteredBits);
GlobalFree(hFilteredBits);
GlobalUnlock(hNewDib);
HDIB hTmp = NULL;
if (wBitCount != 8)
{
if(wBitCount ==24)
hTmp = ConvertDIBFormat(hNewDib, wBitCount, NULL);
else
hTmp = ConvertDIBFormat(hNewDib, wBitCount, palold);
}
else
hTmp = CopyHandle(hNewDib);
GlobalUnlock(h);
GlobalFree(h);
GlobalFree(hNewDib);
DWORD dwSize = GlobalSize(hTmp);
//将原先的位图数据替换成处理后的位图数据
memcpy((LPBYTE)GlobalLock(hDib), (LPBYTE)GlobalLock(hTmp), dwSize);
GlobalUnlock(hTmp);
GlobalUnlock(hDib);
WaitCursorEnd();
return TRUE;
}
DWORD CDib::Width() const
{
if (!m_hDib)
return 0;
return DIBWidth(m_hDib);
}
/*************************************************************************
*
* Height()
*
* Return Value:
*
* DWORD - height of the DIB
*
* Description:
*
* This function gets the height of the DIB from the BITMAPINFOHEADER
* height field
*
************************************************************************/
DWORD CDib::Height() const
{
if (!m_hDib)
return 0;
/* return the DIB height */
return DIBHeight(m_hDib);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -