Imagepointprocesses.cpp
上传用户:alisonmail
上传日期:2013-02-28
资源大小:500k
文件大小:44k
源码类别:
图片显示
开发平台:
Visual C++
- // ImagePointProcesses.cpp
- #include "stdafx.h"
- #include "ImagePointProcesses.h"
- #include "fft.cpp"
- #include "winbase.h"
- #include "edgecontour.h"
- /**************************************************************************
- * *
- * Point Processes *
- * *
- **************************************************************************/
- CImagePointProcesses::CImagePointProcesses()
- {
- EdgeDetectType = SOBEL;
- Embossed = FALSE;
- }
- CImagePointProcesses::CImagePointProcesses( CImageObject *pImageObject )
- {
- m_pImageObject = pImageObject;
- EdgeDetectType = SOBEL;
- Embossed = FALSE;
- }
- BOOL CImagePointProcesses::ChangeBrightness( int nBrightness, int nX1, int nY1, int nX2, int nY2, CImageObject *pImageObject )
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- if( m_pImageObject->GetNumBits() == 1 ) return( FALSE );
- if( nBrightness == 100 ) return( TRUE );
- BOOL bLessThanHalf, bCompleteImage;
- m_pImageObject->NormalizeCoordinates( &nX1, &nY1, &nX2, &nY2, &bCompleteImage, &bLessThanHalf );
- unsigned char *pBuffer, *pBits, *pTemp;
- BITMAPINFOHEADER *pBIH;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors, x, y, i;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- pBIH = (BITMAPINFOHEADER *) &pBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- if( nBrightness < 0 ) nBrightness = 0;
- else if( nBrightness > 200 ) nBrightness = 200;
- LOGPALETTE *pLogPal;
- CPalette *_pPalette = m_pImageObject->GetPalette();
- if( m_pImageObject->GetNumBits() > 8 && ( pLogPal = m_pImageObject->GetLogPal() ) != NULL && ( bCompleteImage || !bLessThanHalf ) ){
- _pPalette->DeleteObject();
- for( i=0; i<nNumColors; i++ )
- BrightnessValues( nBrightness,
- pLogPal->palPalEntry[i].peRed,
- pLogPal->palPalEntry[i].peGreen,
- pLogPal->palPalEntry[i].peBlue,
- &pLogPal->palPalEntry[i].peRed,
- &pLogPal->palPalEntry[i].peGreen,
- &pLogPal->palPalEntry[i].peBlue );
- _pPalette->CreatePalette( pLogPal );
- }
- switch( m_pImageObject->GetNumBits() ){
- case 4:
- if( bCompleteImage ){
- _pPalette->DeleteObject();
- for( i=0; i<nNumColors; i++ ) BrightnessValues( nBrightness, pPalette[i].rgbRed, pPalette[i].rgbGreen, pPalette[i].rgbBlue, &pPalette[i].rgbRed, &pPalette[i].rgbGreen, &pPalette[i].rgbBlue );
- {
- LOGPALETTE *pLogPal = m_pImageObject->CreateLogPalette( pPalette, nNumColors );
- if( pLogPal != NULL ){
- _pPalette->CreatePalette( pLogPal );
- delete [] pLogPal;
- }
- }
- }
- else{
- unsigned char ucRed, ucGreen, ucBlue, Data;
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 / 2 ) );
- for( x=nX1; x<=nX2; x++ ){
- Data = *pTemp;
- if( !( x & 1 ) ) Data >>= 4;
- else Data &= 0x0f;
- ucRed = pPalette[Data].rgbRed;
- ucGreen = pPalette[Data].rgbGreen;
- ucBlue = pPalette[Data].rgbBlue;
- BrightnessValues( nBrightness, ucRed, ucGreen, ucBlue, &ucRed, &ucGreen, &ucBlue );
- Data = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- if( !( x & 1 ) ){
- Data <<= 4;
- (*pTemp) &= 0x0f;
- (*pTemp) |= Data;
- }
- else{
- (*pTemp) &= 0xf0;
- (*pTemp) |= Data;
- pTemp++;
- }
- }
- }
- }
- break;
- case 8:
- if( bCompleteImage ){
- _pPalette->DeleteObject();
- for( i=0; i<nNumColors; i++ ) BrightnessValues( nBrightness, pPalette[i].rgbRed, pPalette[i].rgbGreen, pPalette[i].rgbBlue, &pPalette[i].rgbRed, &pPalette[i].rgbGreen, &pPalette[i].rgbBlue );
- {
- LOGPALETTE *pLogPal = m_pImageObject->CreateLogPalette( pPalette, nNumColors );
- if( pLogPal != NULL ){
- _pPalette->CreatePalette( pLogPal );
- delete [] pLogPal;
- }
- }
- }
- else{
- unsigned char ucRed, ucGreen, ucBlue, Data;
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + nX1 );
- for( x=nX1; x<=nX2; x++ ){
- Data = *pTemp;
- ucRed = pPalette[Data].rgbRed;
- ucGreen = pPalette[Data].rgbGreen;
- ucBlue = pPalette[Data].rgbBlue;
- BrightnessValues( nBrightness, ucRed, ucGreen, ucBlue, &ucRed, &ucGreen, &ucBlue );
- Data = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- *pTemp++ = Data;
- }
- }
- }
- break;
- case 16:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 * 2 ) );
- for( x=nX1; x<=nX2; x++ ){
- }
- }
- break;
- case 24:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 * 3 ) );
- for( x=nX1; x<=nX2; x++ ){
- BrightnessValues( nBrightness, pTemp[0], pTemp[1], pTemp[2], &pTemp[0], &pTemp[1], &pTemp[2] );
- pTemp += 3;
- }
- }
- break;
- case 32:
- for( y=nY1; y<=nY2; y++ ){
- unsigned char ucRed, ucGreen, ucBlue;
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 * 4 ) );
- for( x=nX1; x<=nX2; x++ ){
- ucRed = pTemp[0];
- ucGreen = pTemp[1];
- ucBlue = pTemp[2];
- *pTemp++ = ucRed;
- *pTemp++ = ucGreen;
- *pTemp++ = ucBlue;
- pTemp++;
- }
- }
- break;
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- m_pImageObject->m_nLastError = IMAGELIB_SUCCESS;
- return( TRUE );
- }
- void CImagePointProcesses::BrightnessValues( int nBrightness, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue, unsigned char *pucNewRed, unsigned char *pucNewGreen, unsigned char *pucNewBlue )
- {
- if( m_pImageObject == NULL ) return;
- DWORD Red, Green, Blue;
- Red = (DWORD) ( ( (DWORD) ucRed * (DWORD) nBrightness ) / (DWORD) 100 );
- if( Red > 255 ) Red = 255;
- else if( Red == 0 && nBrightness > 100 ) Red = (DWORD) ( ( nBrightness * 255 ) / 100 );
- ucRed = (unsigned char) Red;
- Green = (DWORD) ( ( (DWORD) ucGreen * (DWORD) nBrightness ) / (DWORD) 100 );
- if( Green > 255 ) Green = 255;
- else if( Green == 0 && nBrightness > 100 ) Green = (DWORD) ( ( nBrightness * 255 ) / 100 );
- ucGreen = (unsigned char) Green;
- Blue = (DWORD) ( ( (DWORD) ucBlue * (DWORD) nBrightness ) / (DWORD) 100 );
- if( Blue > 255 ) Blue = 255;
- else if( Blue == 0 && nBrightness > 100 ) Blue = (DWORD) ( ( nBrightness * 255 ) / 100 );
- ucBlue = (unsigned char) Blue;
- *pucNewRed = ucRed;
- *pucNewGreen = ucGreen;
- *pucNewBlue = ucBlue;
- }
- BOOL CImagePointProcesses::ReverseColors( int nX1, int nY1, int nX2, int nY2, CImageObject *pImageObject )
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- BOOL bLessThanHalf, bCompleteImage;
- m_pImageObject->NormalizeCoordinates( &nX1, &nY1, &nX2, &nY2, &bCompleteImage, &bLessThanHalf );
- unsigned char *pBuffer, *pBits, *pTemp;
- BITMAPINFOHEADER *pBIH;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors, x, y, i;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- pBIH = (BITMAPINFOHEADER *) &pBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- LOGPALETTE *pLogPal = m_pImageObject->GetLogPal();
- CPalette *_pPalette = m_pImageObject->GetPalette();
- if( m_pImageObject->GetNumBits() > 8 && pLogPal != NULL && ( bCompleteImage || !bLessThanHalf ) ){
- _pPalette->DeleteObject();
- for( i=0; i<nNumColors; i++ ){
- pLogPal->palPalEntry[i].peRed = 255 - pLogPal->palPalEntry[i].peRed;
- pLogPal->palPalEntry[i].peGreen = 255 - pLogPal->palPalEntry[i].peGreen;
- pLogPal->palPalEntry[i].peBlue = 255 - pLogPal->palPalEntry[i].peBlue;
- }
- _pPalette->CreatePalette( pLogPal );
- }
- switch( m_pImageObject->GetNumBits() ){
- case 1:
- for( y=nY1; y<=nY2; y++ ){
- unsigned char Mask;
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 / 8 ) );
- for( x=nX1; x<=nX2; x++ ){
- Mask = 0x80 >> ( x & 7 );
- if( pTemp[0] & Mask ) pTemp[0] &= ( Mask ^ 0xff );
- else pTemp[0] |= Mask;
- if( ( x & 7 ) == 7 ) pTemp++;
- }
- }
- break;
- case 4:
- if( bCompleteImage ){
- _pPalette->DeleteObject();
- for( i=0; i<nNumColors; i++ ){
- pPalette[i].rgbRed = 255 - pPalette[i].rgbRed;
- pPalette[i].rgbGreen = 255 - pPalette[i].rgbGreen;
- pPalette[i].rgbBlue = 255 - pPalette[i].rgbBlue;
- }
- {
- LOGPALETTE *pLogPal = m_pImageObject->CreateLogPalette( pPalette, nNumColors );
- if( pLogPal != NULL ){
- _pPalette->CreatePalette( pLogPal );
- delete [] pLogPal;
- }
- }
- }
- else{
- unsigned char ucRed, ucGreen, ucBlue, Data;
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 / 2 ) );
- for( x=nX1; x<=nX2; x++ ){
- Data = *pTemp;
- if( !( x & 1 ) ) Data >>= 4;
- else Data &= 0x0f;
- ucRed = pPalette[Data].rgbRed;
- ucGreen = pPalette[Data].rgbGreen;
- ucBlue = pPalette[Data].rgbBlue;
- ucRed = 255 - ucRed;
- ucGreen = 255 - ucGreen;
- ucBlue = 255 - ucBlue;
- Data = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- if( !( x & 1 ) ){
- Data <<= 4;
- (*pTemp) &= 0x0f;
- (*pTemp) |= Data;
- }
- else{
- (*pTemp) &= 0xf0;
- (*pTemp) |= Data;
- pTemp++;
- }
- }
- }
- }
- break;
- case 8:
- if( bCompleteImage ){
- _pPalette->DeleteObject();
- for( i=0; i<nNumColors; i++ ){
- pPalette[i].rgbRed = 255 - pPalette[i].rgbRed;
- pPalette[i].rgbGreen = 255 - pPalette[i].rgbGreen;
- pPalette[i].rgbBlue = 255 - pPalette[i].rgbBlue;
- }
- {
- LOGPALETTE *pLogPal = m_pImageObject->CreateLogPalette( pPalette, nNumColors );
- if( pLogPal != NULL ){
- _pPalette->CreatePalette( pLogPal );
- delete [] pLogPal;
- }
- }
- }
- else{
- unsigned char ucRed, ucGreen, ucBlue, Data;
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + nX1 );
- for( x=nX1; x<=nX2; x++ ){
- Data = *pTemp;
- ucRed = pPalette[Data].rgbRed;
- ucGreen = pPalette[Data].rgbGreen;
- ucBlue = pPalette[Data].rgbBlue;
- ucRed = 255 - ucRed;
- ucGreen = 255 - ucGreen;
- ucBlue = 255 - ucBlue;
- Data = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- *pTemp++ = Data;
- }
- }
- }
- break;
- case 16:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 * 2 ) );
- for( x=nX1; x<=nX2; x++ ){
- }
- }
- break;
- case 24:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 * 3 ) );
- for( x=nX1; x<=nX2; x++ ){
- pTemp[0] = 255 - pTemp[0];
- pTemp[1] = 255 - pTemp[1];
- pTemp[2] = 255 - pTemp[2];
- pTemp += 3;
- }
- }
- break;
- case 32:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 * 4 ) );
- for( x=nX1; x<=nX2; x++ ){
- pTemp[0] = 255 - pTemp[0];
- pTemp[1] = 255 - pTemp[1];
- pTemp[2] = 255 - pTemp[2];
- pTemp[3] = 255 - pTemp[3];
- pTemp += 4;
- }
- }
- break;
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- m_pImageObject->m_nLastError = IMAGELIB_SUCCESS;
- return( TRUE );
- }
- BOOL CImagePointProcesses::MakeGray( BOOL bSetPalette, CImageObject *pImageObject )
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- unsigned char *pBuffer, *pBits, *pTemp;
- BITMAPINFOHEADER *pBIH;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors, x, y, i;
- WORD wColorBits, *pColorBits;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- pBIH = (BITMAPINFOHEADER *) &pBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- unsigned char *pLookup;
- DWORD dwGray;
- switch( m_pImageObject->GetNumBits() ){
- case 1:
- break;
- case 4:
- pLookup = new unsigned char [16];
- if( pLookup == NULL ) break;
- memset( pLookup, 0, 16 );
- for( i=0; i<16; i++ ){
- dwGray = ( (DWORD) pPalette[i].rgbRed * 30 +
- (DWORD) pPalette[i].rgbGreen * 59 +
- (DWORD) pPalette[i].rgbBlue * 11 ) / 100;
- pLookup[i] = (unsigned char) dwGray;
- }
- for( i=0; i<16; i++ ){
- pPalette[i].rgbRed = pLookup[i];
- pPalette[i].rgbGreen = pLookup[i];
- pPalette[i].rgbBlue = pLookup[i];
- }
- delete [] pLookup;
- m_pImageObject->ProcessPalette();
- if( bSetPalette && m_pImageObject->m_nPaletteBytes > 0 ){
- CWindowDC WindowDC( NULL );
- m_pImageObject->SetPalette( &WindowDC );
- }
- break;
- case 8:
- pLookup = new unsigned char [256];
- if( pLookup == NULL ) break;
- memset( pLookup, 0, 256 );
- for( i=0; i<256; i++ ){
- dwGray = ( (DWORD) pPalette[i].rgbRed * 30 +
- (DWORD) pPalette[i].rgbGreen * 59 +
- (DWORD) pPalette[i].rgbBlue * 11 ) / 100;
- pLookup[i] = (unsigned char) dwGray;
- }
- for( y=0; y<pBIH->biHeight; y++ ){
- pTemp = pBits;
- pTemp += y * nWidthBytes;
- for( x=0; x<pBIH->biWidth; x++ ) pTemp[x] = pLookup[pTemp[x]];
- }
- delete [] pLookup;
- for( i=0; i<256; i++ ){
- pPalette[i].rgbRed = i;
- pPalette[i].rgbGreen = i;
- pPalette[i].rgbBlue = i;
- }
- m_pImageObject->ProcessPalette();
- if( bSetPalette && m_pImageObject->m_nPaletteBytes > 0 ){
- CWindowDC WindowDC( NULL );
- m_pImageObject->SetPalette( &WindowDC );
- }
- break;
- case 16:
- for( y=0; y<pBIH->biHeight; y++ ){
- pTemp = pBits;
- pTemp += y * nWidthBytes;
- for( x=0; x<pBIH->biWidth; x++ ){
- pColorBits = (WORD *) pTemp[x*2];
- wColorBits = *pColorBits;
- dwGray = ( (DWORD) ( ( wColorBits & 0x7c00 ) >> 10 ) * 30 +
- (DWORD) ( ( wColorBits & 0x03e0 ) >> 5 ) * 59 +
- (DWORD) ( wColorBits & 0x001f ) * 11 ) / 100;
- *pColorBits = ( ( (WORD) dwGray << 10 ) | ( (WORD) dwGray << 5 ) | (WORD) dwGray);
- }
- }
- m_pImageObject->ProcessPalette();
- if( bSetPalette && m_pImageObject->m_nPaletteBytes > 0 ){
- CWindowDC WindowDC( NULL );
- m_pImageObject->SetPalette( &WindowDC );
- }
- break;
- case 24:
- for( y=0; y<pBIH->biHeight; y++ ){
- pTemp = pBits;
- pTemp += y * nWidthBytes;
- for( x=0; x<pBIH->biWidth; x++ ){
- dwGray = ( (DWORD) pTemp[x*3+2] * 30 +
- (DWORD) pTemp[x*3+1] * 59 +
- (DWORD) pTemp[x*3] * 11 ) / 100;
- pTemp[x*3] = (unsigned char) dwGray;
- pTemp[x*3+1] = (unsigned char) dwGray;
- pTemp[x*3+2] = (unsigned char) dwGray;
- }
- }
- m_pImageObject->ProcessPalette();
- if( bSetPalette && m_pImageObject->m_nPaletteBytes > 0 ){
- CWindowDC WindowDC( NULL );
- m_pImageObject->SetPalette( &WindowDC );
- }
- break;
- case 32:
- for( y=0; y<pBIH->biHeight; y++ ){
- pTemp = pBits;
- pTemp += y * nWidthBytes;
- for( x=0; x<pBIH->biWidth; x++ ){
- dwGray = ( (DWORD) pTemp[x*4] * 30 +
- (DWORD) pTemp[x*4+1] * 59 +
- (DWORD) pTemp[x*4+2] * 11 ) / 100;
- pTemp[x*4] = (unsigned char) dwGray;
- pTemp[x*4+1] = (unsigned char) dwGray;
- pTemp[x*4+2] = (unsigned char) dwGray;
- }
- }
- m_pImageObject->ProcessPalette();
- if( bSetPalette && m_pImageObject->m_nPaletteBytes > 0 ){
- CWindowDC WindowDC( NULL );
- m_pImageObject->SetPalette( &WindowDC );
- }
- break;
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- m_pImageObject->m_nLastError = IMAGELIB_SUCCESS;
- return( TRUE );
- }
- BOOL CImagePointProcesses::Colorize( int nX1, int nY1, int nX2, int nY2, COLORREF Color, CImageObject *pImageObject )
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- if( m_pImageObject->GetNumBits() == 1 ) return( FALSE );
- BOOL bLessThanHalf, bCompleteImage;
- m_pImageObject->NormalizeCoordinates( &nX1, &nY1, &nX2, &nY2, &bCompleteImage, &bLessThanHalf );
- unsigned char *pBuffer, *pBits, *pTemp;
- BITMAPINFOHEADER *pBIH;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors, x, y, i;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- pBIH = (BITMAPINFOHEADER *) &pBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- DWORD dwRed, dwGreen, dwBlue, dwGray;
- dwBlue = Color >> 16;
- dwGreen = ( Color >> 8 ) & 0x000000ff;
- dwRed = Color & 0x000000ff;
- LOGPALETTE *pLogPal = m_pImageObject->GetLogPal();
- CPalette *_pPalette = m_pImageObject->GetPalette();
- if( m_pImageObject->GetNumBits() > 8 && pLogPal != NULL && ( bCompleteImage || !bLessThanHalf ) ){
- _pPalette->DeleteObject();
- for( i=0; i<nNumColors; i++ ){
- dwGray = ( (DWORD) pLogPal->palPalEntry[i].peRed * 30 +
- (DWORD) pLogPal->palPalEntry[i].peGreen * 59 +
- (DWORD) pLogPal->palPalEntry[i].peBlue * 11 ) / 100;
- pLogPal->palPalEntry[i].peRed = (unsigned char) ( ( dwRed * dwGray ) / (DWORD) 255 );
- pLogPal->palPalEntry[i].peGreen = (unsigned char) ( ( dwGreen * dwGray ) / (DWORD) 255 );
- pLogPal->palPalEntry[i].peBlue = (unsigned char) ( ( dwBlue * dwGray ) / (DWORD) 255 );
- }
- _pPalette->CreatePalette( pLogPal );
- }
- switch( m_pImageObject->GetNumBits() ){
- case 4:
- if( bCompleteImage ){
- _pPalette->DeleteObject();
- for( i=0; i<nNumColors; i++ ){
- dwGray = ( (DWORD) pPalette[i].rgbRed * 30 +
- (DWORD) pPalette[i].rgbGreen * 59 +
- (DWORD) pPalette[i].rgbBlue * 11 ) / 100;
- pPalette[i].rgbRed = (unsigned char) ( ( dwRed * dwGray ) / (DWORD) 255 );
- pPalette[i].rgbGreen = (unsigned char) ( ( dwGreen * dwGray ) / (DWORD) 255 );
- pPalette[i].rgbBlue = (unsigned char) ( ( dwBlue * dwGray ) / (DWORD) 255 );
- }
- {
- LOGPALETTE *pLogPal = m_pImageObject->CreateLogPalette( pPalette, nNumColors );
- if( pLogPal != NULL ){
- _pPalette->CreatePalette( pLogPal );
- delete [] pLogPal;
- }
- }
- }
- else{
- unsigned char ucRed, ucGreen, ucBlue, Data;
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 / 2 ) );
- for( x=nX1; x<=nX2; x++ ){
- Data = *pTemp;
- if( !( x & 1 ) ) Data >>= 4;
- else Data &= 0x0f;
- ucRed = pPalette[Data].rgbRed;
- ucGreen = pPalette[Data].rgbGreen;
- ucBlue = pPalette[Data].rgbBlue;
- ucRed = 255 - ucRed;
- ucGreen = 255 - ucGreen;
- ucBlue = 255 - ucBlue;
- Data = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- if( !( x & 1 ) ){
- Data <<= 4;
- (*pTemp) &= 0x0f;
- (*pTemp) |= Data;
- }
- else{
- (*pTemp) &= 0xf0;
- (*pTemp) |= Data;
- pTemp++;
- }
- }
- }
- }
- break;
- case 8:
- if( bCompleteImage ){
- _pPalette->DeleteObject();
- for( i=0; i<nNumColors; i++ ){
- dwGray = ( (DWORD) pPalette[i].rgbRed * 30 +
- (DWORD) pPalette[i].rgbGreen * 59 +
- (DWORD) pPalette[i].rgbBlue * 11 ) / 100;
- pPalette[i].rgbRed = (unsigned char) ( ( dwRed * dwGray ) / (DWORD) 255 );
- pPalette[i].rgbGreen = (unsigned char) ( ( dwGreen * dwGray ) / (DWORD) 255 );
- pPalette[i].rgbBlue = (unsigned char) ( ( dwBlue * dwGray ) / (DWORD) 255 );
- }
- {
- LOGPALETTE *pLogPal = m_pImageObject->CreateLogPalette( pPalette, nNumColors );
- if( pLogPal != NULL ){
- _pPalette->CreatePalette( pLogPal );
- delete [] pLogPal;
- }
- }
- }
- else{
- unsigned char ucRed, ucGreen, ucBlue, Data;
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + nX1 );
- for( x=nX1; x<=nX2; x++ ){
- Data = *pTemp;
- dwGray = ( (DWORD) pPalette[Data].rgbRed * 30 +
- (DWORD) pPalette[Data].rgbGreen * 59 +
- (DWORD) pPalette[Data].rgbBlue * 11 ) / 100;
- ucRed = (unsigned char) ( ( dwRed * dwGray ) / (DWORD) 255 );
- ucGreen = (unsigned char) ( ( dwGreen * dwGray ) / (DWORD) 255 );
- ucBlue = (unsigned char) ( ( dwBlue * dwGray ) / (DWORD) 255 );
- Data = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- *pTemp++ = Data;
- }
- }
- }
- break;
- case 16:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 * 2 ) );
- for( x=nX1; x<=nX2; x++ ){
- }
- }
- break;
- case 24:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 * 3 ) );
- for( x=nX1; x<=nX2; x++ ){
- dwGray = ( (DWORD) pTemp[2] * 30 +
- (DWORD) pTemp[1] * 59 +
- (DWORD) pTemp[0] * 11 ) / 100;
- pTemp[2] = (unsigned char) ( ( dwRed * dwGray ) / (DWORD) 255 );
- pTemp[1] = (unsigned char) ( ( dwGreen * dwGray ) / (DWORD) 255 );
- pTemp[0] = (unsigned char) ( ( dwBlue * dwGray ) / (DWORD) 255 );
- pTemp += 3;
- }
- }
- break;
- case 32:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( ( m_pImageObject->GetHeight() - 1 - y ) * nWidthBytes ) + ( nX1 * 4 ) );
- for( x=nX1; x<=nX2; x++ ){
- pTemp[0] = 255 - pTemp[0];
- pTemp[1] = 255 - pTemp[1];
- pTemp[2] = 255 - pTemp[2];
- pTemp[3] = 255 - pTemp[3];
- pTemp += 4;
- }
- }
- break;
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- m_pImageObject->m_nLastError = IMAGELIB_SUCCESS;
- return( TRUE );
- }
- void CImagePointProcesses::SetImageObjectClass( CImageObject *pImageObject )
- {
- m_pImageObject = pImageObject;
- }
- int *CImagePointProcesses::CreateHistogram( int nX1, int nY1, int nX2, int nY2, unsigned char *pData, RGBQUAD *pPalette, int nWidthBytes, CImageObject *pImageObject )
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- m_pImageObject->NormalizeCoordinates( &nX1, &nY1, &nX2, &nY2 );
- int *pBuffer = new int [256*4+4];
- if( pBuffer == NULL ) return( NULL );
- memset( pBuffer, 0, ( 256 * 4 + 4 ) * sizeof( int ) );
- DWORD dwGray;
- int x, y;
- unsigned char *pTemp, ucRed, ucGreen, ucBlue;
- int Pixels = 0;
- int nHeight = m_pImageObject->GetHeight();
- switch( m_pImageObject->GetNumBits() ){
- case 1:
- break;
- case 4:
- break;
- case 8:
- for( y=0; y<nHeight; y++ ){
- pTemp = pData;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pTemp += nX1;
- for( x=nX1; x<=nX2; x++ ){
- ucRed = pPalette[pTemp[x]].rgbRed;
- ucGreen = pPalette[pTemp[x]].rgbGreen;
- ucBlue = pPalette[pTemp[x]].rgbBlue;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- dwGray &= 0x000000ff;
- pBuffer[dwGray]++;
- pBuffer[256+ucRed]++;
- pBuffer[512+ucBlue]++;
- pBuffer[768+ucGreen]++;
- Pixels++;
- }
- }
- break;
- case 16:
- for( y=0; y<nHeight; y++ ){
- pTemp = pData;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pTemp += ( nX1 * 2 );
- for( x=nX1; x<=nX2; x++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, pTemp );
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- dwGray &= 0x000000ff;
- pBuffer[dwGray]++;
- pBuffer[256+pTemp[x*3]]++;
- pBuffer[512+pTemp[x*3+1]]++;
- pBuffer[768+pTemp[x*3+2]]++;
- Pixels++;
- pTemp += 2;
- }
- }
- break;
- case 24:
- for( y=0; y<nHeight; y++ ){
- pTemp = pData;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pTemp += ( nX1 * 3 );
- for( x=nX1; x<=nX2; x++ ){
- dwGray = ( (DWORD) pTemp[x*3+2] * 30 +
- (DWORD) pTemp[x*3+1] * 59 +
- (DWORD) pTemp[x*3] * 11 ) / 100;
- dwGray &= 0x000000ff;
- pBuffer[dwGray]++;
- pBuffer[256+pTemp[x*3]]++;
- pBuffer[512+pTemp[x*3+1]]++;
- pBuffer[768+pTemp[x*3+2]]++;
- Pixels++;
- }
- }
- break;
- case 32:
- for( y=0; y<nHeight; y++ ){
- pTemp = pData;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pTemp += ( nX1 * 4 );
- for( x=nX1; x<=nX2; x++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, pTemp );
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- dwGray &= 0x000000ff;
- pBuffer[dwGray]++;
- pBuffer[256+pTemp[x*3]]++;
- pBuffer[512+pTemp[x*3+1]]++;
- pBuffer[768+pTemp[x*3+2]]++;
- Pixels++;
- pTemp += 4;
- }
- }
- break;
- }
- pBuffer[0] = pBuffer[256] = pBuffer[512] = pBuffer[768] = 0;
- int Highest = 0, i, j;
- for( i=0; i<256*4; i++ ){
- if( pBuffer[i] > Highest ) Highest = pBuffer[i];
- }
- for( j=0; j<4; j++ ){
- for( i=0; i<256; i++ ) pBuffer[j*256+i] = ( pBuffer[j*256+i] * 255 ) / Highest;
- }
- Highest = 0;
- for( i=0; i<256*4; i++ ){
- if( pBuffer[i] > Highest ) Highest = pBuffer[i];
- }
- pBuffer[256*4] = Highest;
- pBuffer[256*4+1] = Highest / 25;
- for( i=0; i<256; i++ ){
- if( pBuffer[i] >= pBuffer[256*4+1] ) break;
- }
- pBuffer[256*4+2] = i;
- for( i=255; i>=0; i-- ){
- if( pBuffer[i] >= pBuffer[256*4+1] ) break;
- }
- pBuffer[256*4+3] = i;
- if( pBuffer[256*4+2] > 255 ) pBuffer[256*4+2] = 255;
- if( pBuffer[256*4+3] < 0 ) pBuffer[256*4+3] = 0;
- return( pBuffer );
- }
- BOOL CImagePointProcesses::FFTTrans(CImageObject *pImageObject)
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- if( m_pImageObject->GetNumBits() == 1 ) return( FALSE );
- unsigned char *pBuffer, *pBits, *pTemp;
- BITMAPINFOHEADER *pBIH;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- pBIH = (BITMAPINFOHEADER *) &pBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- int nTotalPixel = pBIH->biWidth * pBIH->biHeight;
- int nWidth = pBIH->biWidth;
- int nHeight = pBIH->biHeight;
- double (* fftBuffer)[2] = new double[nTotalPixel][2];
- long x, y;
- unsigned char ucRed, ucGreen, ucBlue;
- DWORD dwGray;
- switch( m_pImageObject->GetNumBits() )
- {
- case 1:
- break;
- case 4:
- break;
- case 8:
- for( y=0; y<nHeight; y++ ){
- pTemp = pBits;
- pTemp += y * nWidthBytes;
- for( x=0; x<nWidth; x++ ){
- ucRed = pPalette[pTemp[x]].rgbRed;
- ucGreen = pPalette[pTemp[x]].rgbGreen;
- ucBlue = pPalette[pTemp[x]].rgbBlue;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- dwGray &= 0x000000ff;
- fftBuffer[y*nWidth+x][0] = (double) dwGray;
- fftBuffer[y*nWidth+x][1] = 0.0;
- }
- }
- break;
- }
- double * pCos = new double[nHeight/2];
- double * pSin = new double[nHeight/2];
- int * l = new int[nHeight];
- calW(pCos, pSin, l, nHeight);
- // char strTime[35];
- // LARGE_INTEGER Frequency;
- // LARGE_INTEGER PerformanceCount1, PerformanceCount2;
- // QueryPerformanceFrequency(&Frequency);
- // QueryPerformanceCounter(&PerformanceCount1);
- for(y=0; y<nHeight; y++){
- fft(fftBuffer+nWidth*y, pCos, pSin, l, nWidth);
- }
- imposition(fftBuffer, nWidth);
- for(x=0; x<nWidth; x++){
- fft(fftBuffer+nHeight*x, pCos, pSin, l, nHeight);
- }
- imposition(fftBuffer, nHeight);
- // QueryPerformanceCounter(&PerformanceCount2);
- // sprintf(strTime, "共耗时:%ld", PerformanceCount2.QuadPart-PerformanceCount1.QuadPart);
- // AfxMessageBox(strTime);
- double max = 0.0;
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++){
- double real = fftBuffer[y*nWidth+x][0];
- double img = fftBuffer[y*nWidth+x][1];
- double amp;
- amp = fftBuffer[y*nWidth+x][0] = log(sqrt(real*real + img*img)+1.0);
- max = ((amp>max) ? amp : max);
- }
- }
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++){
- long newx = (x>127)?(x-128):(x+128);
- long newy = (y>127)?(y-128):(y+128);
- pBits[newy*nWidthBytes+newx] = (unsigned char)(fftBuffer[y*nWidth+x][0]/max * 255.0);
- }
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- delete[] fftBuffer;
- delete[] pCos;
- delete[] pSin;
- delete[] l;
- m_pImageObject->m_nLastError = IMAGELIB_SUCCESS;
- return( TRUE );
- }
- BOOL CImagePointProcesses::DCTTrans(CImageObject * pImageObject)
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- if( m_pImageObject->GetNumBits() == 1 ) return( FALSE );
- unsigned char *pBuffer, *pBits, *pTemp;
- BITMAPINFOHEADER *pBIH;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- pBIH = (BITMAPINFOHEADER *) &pBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- int nTotalPixel = pBIH->biWidth * pBIH->biHeight;
- int nWidth = pBIH->biWidth;
- int nHeight = pBIH->biHeight;
- double (* dctBuffer) = new double[nTotalPixel];
- long x, y;
- unsigned char ucRed, ucGreen, ucBlue;
- DWORD dwGray;
- switch( m_pImageObject->GetNumBits() )
- {
- case 1:
- break;
- case 4:
- break;
- case 8:
- for( y=0; y<nHeight; y++ ){
- pTemp = pBits;
- pTemp += y * nWidthBytes;
- for( x=0; x<nWidth; x++ ){
- ucRed = pPalette[pTemp[x]].rgbRed;
- ucGreen = pPalette[pTemp[x]].rgbGreen;
- ucBlue = pPalette[pTemp[x]].rgbBlue;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- dwGray &= 0x000000ff;
- dctBuffer[y*nWidth+x] = (double) dwGray;
- }
- }
- break;
- }
- double * pCos = new double[nHeight/2];
- double * pSin = new double[nHeight/2];
- int * l = new int[nHeight];
- calW(pCos, pSin, l, nHeight);
- for(y=0; y<nHeight; y++){
- dct(dctBuffer+nWidth*y, pCos, pSin, l, nWidth);
- }
- imposition(dctBuffer, nWidth);
- for(x=0; x<nWidth; x++){
- dct(dctBuffer+nHeight*x, pCos, pSin, l, nHeight);
- }
- imposition(dctBuffer, nHeight);
- double max = 0.0;
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++){
- double result;
- result = dctBuffer[y*nWidth+x] = log10(fabs(dctBuffer[y*nWidth+x])+1.0);
- max = ((result>max) ? result : max);
- }
- }
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++){
- pBits[y*nWidth+x] = (unsigned char)(dctBuffer[(nHeight-1-y)*nWidth+x]/max*255.0);
- }
- }
- delete[] dctBuffer;
- delete[] pCos;
- delete[] pSin;
- delete[] l;
- ::GlobalUnlock( m_pImageObject->GetDib() );
- m_pImageObject->m_nLastError = IMAGELIB_SUCCESS;
- return( TRUE );
- }
- BOOL CImagePointProcesses::EdgeDetect(CImageObject *pImageObject)
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- if( m_pImageObject->GetNumBits() == 1 ) return( FALSE );
- unsigned char *pBuffer, *pBits, *pTemp;
- BITMAPINFOHEADER *pBIH;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- pBIH = (BITMAPINFOHEADER *) &pBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- int nTotalPixel = pBIH->biWidth * pBIH->biHeight;
- int nWidth = pBIH->biWidth;
- int nHeight = pBIH->biHeight;
- double * Buffer = new double[nTotalPixel];
- double * Bridge = new double[nTotalPixel];
- long x, y;
- unsigned char ucRed, ucGreen, ucBlue;
- DWORD dwGray;
- switch( m_pImageObject->GetNumBits() )
- {
- case 1:
- break;
- case 4:
- break;
- case 8:
- for( y=0; y<nHeight; y++ ){
- pTemp = pBits;
- pTemp += y * nWidthBytes;
- for( x=0; x<nWidth; x++ ){
- ucRed = pPalette[pTemp[x]].rgbRed;
- ucGreen = pPalette[pTemp[x]].rgbGreen;
- ucBlue = pPalette[pTemp[x]].rgbBlue;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- dwGray &= 0x000000ff;
- Buffer[y*nWidth+x] = Bridge[y*nWidth+x] = (double) dwGray;
- }
- }
- break;
- }
- double temp1, temp2;
- for(y=1; y<nHeight-1; y++)
- {
- for(x=1; x<nWidth-1; x++)
- {
- switch(EdgeDetectType)
- {
- case LAPLACE:
- temp1 = -Bridge[(x-1)+y*nWidth]
- -Bridge[(x+1)+y*nWidth]
- -Bridge[x+(y-1)*nWidth]
- -Bridge[x+(y+1)*nWidth]
- +Bridge[x+y*nWidth]*4;
- temp2 = -Bridge[(x-1)+(y-1)*nWidth]
- -Bridge[x+(y-1)*nWidth]
- -Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+y*nWidth]
- +Bridge[x+y*nWidth]*8
- -Bridge[(x+1)+y*nWidth]
- -Bridge[(x-1)+(y+1)*nWidth]
- -Bridge[x+(y+1)*nWidth]
- -Bridge[(x+1)+(y+1)*nWidth];
- Buffer[x+y*nWidth]=temp1+temp2;
- break;
- case SOBEL:
- temp1 = -Bridge[(x-1)+(y-1)*nWidth]
- +Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+y*nWidth]*2
- +Bridge[(x+1)+y*nWidth]*2
- -Bridge[(x-1)+(y+1)*nWidth]
- +Bridge[(x+1)+(y+1)*nWidth];
- temp2 = Bridge[(x-1)+(y-1)*nWidth]
- +Bridge[x+(y-1)*nWidth]*2
- +Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+(y+1)*nWidth]
- -Bridge[x+(y+1)*nWidth]*2
- -Bridge[(x+1)+(y+1)*nWidth];
- Buffer[x+y*nWidth]=temp1+temp2;
- break;
- case PREWITT:
- temp1 = -Bridge[(x-1)+(y-1)*nWidth]
- +Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+y*nWidth]
- +Bridge[(x+1)+y*nWidth]
- -Bridge[(x-1)+(y+1)*nWidth]
- +Bridge[(x+1)+(y+1)*nWidth];
- temp2 = Bridge[(x-1)+(y-1)*nWidth]
- +Bridge[x+(y-1)*nWidth]
- +Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+(y+1)*nWidth]
- -Bridge[x+(y+1)*nWidth]
- -Bridge[(x+1)+(y+1)*nWidth];
- Buffer[x+y*nWidth]=temp1+temp2;
- break;
- }
- }
- }
- double max = 0.0;
- double min = 0.0;
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++){
- double amp = Buffer[y*nWidth+x];
- max = ((amp>max) ? amp : max);
- min = ((amp<min) ? amp : min);
- }
- }
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++){
- if(Embossed)
- pBits[y*nWidthBytes+x] = (unsigned char)((Buffer[y*nWidth+x]-min)/(max-min)*255.0);
- else
- {
- if(Buffer[y*nWidth+x]>255.0) Buffer[y*nWidth+x]=255.0;
- else if(Buffer[y*nWidth+x]<0.0) Buffer[y*nWidth+x] = 0.0;
- pBits[y*nWidthBytes+x] = (unsigned char)Buffer[y*nWidth+x];
- }
- }
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- if(Buffer) delete[] Buffer;
- if(Bridge) delete[] Bridge;
- m_pImageObject->m_nLastError = IMAGELIB_SUCCESS;
- return( TRUE );
- }
- BOOL CImagePointProcesses::AddNoise(CImageObject * pImageObject, int nRatio)
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- if( m_pImageObject->GetNumBits() == 1 ) return( FALSE );
- unsigned char *pBuffer, *pBits;
- BITMAPINFOHEADER *pBIH;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- pBIH = (BITMAPINFOHEADER *) &pBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- int nTotalPixel = pBIH->biWidth * pBIH->biHeight;
- int nWidth = pBIH->biWidth;
- int nHeight = pBIH->biHeight;
- long x, y;
- int temp;
- srand( (unsigned)time(NULL) );
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++)
- {
- temp = rand();
- if(temp < RAND_MAX*nRatio/100) pBits[y*nWidthBytes+x] = (unsigned char)255.0;
- }
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- m_pImageObject->m_nLastError = IMAGELIB_SUCCESS;
- return( TRUE );
- }
- BOOL CImagePointProcesses::ExtractRiver(CPoint *pStartPoint, CImageObject *pImageObject)
- {
- CPoint presetPoint(42, 6);
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- if(pStartPoint == NULL)
- {
- pStartPoint = &presetPoint;
- }
- EdgeDetectType = LAPLACE;
- if( m_pImageObject->GetNumBits() == 1 ) return( FALSE );
- unsigned char *pBuffer, *pBits, *pTemp;
- BITMAPINFOHEADER *pBIH;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- pBIH = (BITMAPINFOHEADER *) &pBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- int nTotalPixel = pBIH->biWidth * pBIH->biHeight;
- int nWidth = pBIH->biWidth;
- int nHeight = pBIH->biHeight;
- double * Buffer = new double[nTotalPixel];
- double * Bridge = new double[nTotalPixel];
- long x, y;
- unsigned char ucRed, ucGreen, ucBlue;
- DWORD dwGray;
- switch( m_pImageObject->GetNumBits() )
- {
- case 1:
- break;
- case 4:
- break;
- case 8:
- for( y=0; y<nHeight; y++ ){
- pTemp = pBits;
- pTemp += y * nWidthBytes;
- for( x=0; x<nWidth; x++ ){
- ucRed = pPalette[pTemp[x]].rgbRed;
- ucGreen = pPalette[pTemp[x]].rgbGreen;
- ucBlue = pPalette[pTemp[x]].rgbBlue;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- dwGray &= 0x000000ff;
- Buffer[y*nWidth+x] = Bridge[y*nWidth+x] = (double) dwGray;
- }
- }
- break;
- }
- /* double temp1, temp2;
- for(y=1; y<nHeight-1; y++)
- {
- for(x=1; x<nWidth-1; x++)
- {
- switch(EdgeDetectType)
- {
- case LAPLACE:
- temp1 = -Bridge[(x-1)+y*nWidth]
- -Bridge[(x+1)+y*nWidth]
- -Bridge[x+(y-1)*nWidth]
- -Bridge[x+(y+1)*nWidth]
- +Bridge[x+y*nWidth]*4;
- temp2 = -Bridge[(x-1)+(y-1)*nWidth]
- -Bridge[x+(y-1)*nWidth]
- -Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+y*nWidth]
- +Bridge[x+y*nWidth]*8
- -Bridge[(x+1)+y*nWidth]
- -Bridge[(x-1)+(y+1)*nWidth]
- -Bridge[x+(y+1)*nWidth]
- -Bridge[(x+1)+(y+1)*nWidth];
- Buffer[x+y*nWidth]=temp1+temp2;
- break;
- case SOBEL:
- temp1 = -Bridge[(x-1)+(y-1)*nWidth]
- +Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+y*nWidth]*2
- +Bridge[(x+1)+y*nWidth]*2
- -Bridge[(x-1)+(y+1)*nWidth]
- +Bridge[(x+1)+(y+1)*nWidth];
- temp2 = Bridge[(x-1)+(y-1)*nWidth]
- +Bridge[x+(y-1)*nWidth]*2
- +Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+(y+1)*nWidth]
- -Bridge[x+(y+1)*nWidth]*2
- -Bridge[(x+1)+(y+1)*nWidth];
- Buffer[x+y*nWidth]=temp1+temp2;
- break;
- case PREWITT:
- temp1 = -Bridge[(x-1)+(y-1)*nWidth]
- +Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+y*nWidth]
- +Bridge[(x+1)+y*nWidth]
- -Bridge[(x-1)+(y+1)*nWidth]
- +Bridge[(x+1)+(y+1)*nWidth];
- temp2 = Bridge[(x-1)+(y-1)*nWidth]
- +Bridge[x+(y-1)*nWidth]
- +Bridge[(x+1)+(y-1)*nWidth]
- -Bridge[(x-1)+(y+1)*nWidth]
- -Bridge[x+(y+1)*nWidth]
- -Bridge[(x+1)+(y+1)*nWidth];
- Buffer[x+y*nWidth]=temp1+temp2;
- break;
- }
- }
- }
- double max = 0.0;
- double min = 0.0;
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++){
- double amp = Buffer[y*nWidth+x];
- max = ((amp>max) ? amp : max);
- min = ((amp<min) ? amp : min);
- }
- }
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++){
- if(Embossed)
- pBits[y*nWidthBytes+x] = (unsigned char)((Buffer[y*nWidth+x]-min)/(max-min)*255.0);
- else
- {
- // if(Buffer[y*nWidth+x]>255.0) Buffer[y*nWidth+x]=255.0;
- // else if(Buffer[y*nWidth+x]<0.0) Buffer[y*nWidth+x] = 0.0;
- #define SIDE 1
- if(y > SIDE && y < 255-SIDE && x > SIDE && x < 255-SIDE)
- {
- if(Buffer[y*nWidth+x]>150.0)
- Buffer[y*nWidth+x]=0.0;
- else if(Buffer[y*nWidth+x]<=150.0)
- Buffer[y*nWidth+x] = 255.0;
- }
- else
- Buffer[y*nWidth+x] = 255.0;
- pBits[y*nWidthBytes+x] = (unsigned char)Buffer[y*nWidth+x];
- }
- }
- }
- */
- for(y=0; y<nHeight; y++){
- for(x=0; x<nWidth; x++){
- if(Buffer[y*nWidth+x]>150.0)
- Buffer[y*nWidth+x]=255.0;
- else if(Buffer[y*nWidth+x]<=150.0)
- Buffer[y*nWidth+x] = 0.0;
- pBits[y*nWidthBytes+x] = (unsigned char)Buffer[y*nWidth+x];
- }
- }
- //提取河流
- TraceDIB((char *)pBits, nWidth, nHeight);
- /*
- #define NEAR_POINT 4
- double CurPointColor = Buffer[pStartPoint->x + nWidth*pStartPoint->y];
- int curX = pStartPoint->x;
- int count = 0;
- double dist = 0.0;
- int threshold = 1;
- int upper_thres = 250;
- int low_thres = 250;
- int j;
- RiverTrack[0].x = 44;//pStartPoint->x;
- RiverTrack[0].y = 4;pStartPoint->y;
- // for(x = RiverTrack[0].x; x < nWidth; x ++)
- // {
- // for(y = 0; y < 256; y ++)
- for(y = RiverTrack[0].y; y < nHeight; y ++)
- {
- for(x = 0; x < 256; x ++)
- {
- if(Buffer[y*nWidth + x] >= upper_thres)
- {
- count ++;
- RiverTrack[count].x = x;
- RiverTrack[count].y = y;
- }
- else if(Buffer[y*nWidth + x] >= low_thres)
- {
- dist = (x-RiverTrack[count].x)*(x-RiverTrack[count].x)
- + (y-RiverTrack[count].y)*(y-RiverTrack[count].y);
- if(dist < threshold * threshold)
- {
- count ++;
- RiverTrack[count].x = x;
- RiverTrack[count].y = y;
- }
- }
- }
- }
- //画出来
- for(y = 0; y < nHeight; y ++)
- for(x = 0; x < nWidth; x ++)
- pBits[y*nWidthBytes + x] = 0;
- for(j=0; j<count; j++)
- {
- pBits[RiverTrack[j].y*nWidthBytes + RiverTrack[j].x] = 255;
- }
- */
- ::GlobalUnlock( m_pImageObject->GetDib() );
- if(Buffer) delete[] Buffer;
- if(Bridge) delete[] Bridge;
- m_pImageObject->m_nLastError = IMAGELIB_SUCCESS;
- return( TRUE );
- }