IMAGEAREAPROCESSES.CPP
上传用户:alisonmail
上传日期:2013-02-28
资源大小:500k
文件大小:56k
- // ImageAreaProcesses.cpp
- #include "stdafx.h"
- #include "ImageAreaProcesses.h"
- #define MAX( a, b ) ( a > b ? a : b )
- /**************************************************************************
- * *
- * Area Processes *
- * *
- **************************************************************************/
- CImageAreaProcesses::CImageAreaProcesses()
- {
- }
- CImageAreaProcesses::CImageAreaProcesses( CImageObject *pImageObject )
- {
- m_pImageObject = pImageObject;
- }
- BOOL CImageAreaProcesses::MedianFilter( 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 );
- m_pImageObject->NormalizeCoordinates( &nX1, &nY1, &nX2, &nY2 );
- int nWidth = m_pImageObject->GetWidth();
- int nHeight = m_pImageObject->GetHeight();
- if( nX1 < 2 ) nX1 = 2;
- if( nY1 < 2 ) nY1 = 2;
- if( nX2 >= nWidth - 2 ) nX2 = nWidth - 2;
- if( nY2 >= nHeight - 2 ) nY2 = nHeight - 2;
- DWORD dwMedianList[9];
- int nLoc[9];
- unsigned char Data;
- unsigned char *pOldBuffer, *pNewBuffer, *pOldBits, *pNewBits, *pOldTemp, *pNewTemp;
- BITMAPFILEHEADER *pOldBFH, *pNewBFH;
- BITMAPINFOHEADER *pOldBIH, *pNewBIH;
- RGBQUAD *pOldPalette, *pNewPalette;
- int nWidthBytes, nNumColors, x, y, i, j, k, l;
- pOldBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pOldBuffer == NULL ) return( FALSE );
- pOldBFH = (BITMAPFILEHEADER *) pOldBuffer;
- pOldBIH = (BITMAPINFOHEADER *) &pOldBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pOldPalette = (RGBQUAD *) &pOldBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pOldBits = (unsigned char *) &pOldBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- DWORD dwNewSize;
- HGLOBAL hNewDib;
- dwNewSize = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) + nNumColors * sizeof( RGBQUAD ) + nWidthBytes * m_pImageObject->GetHeight();
- hNewDib = ::GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, dwNewSize );
- if( hNewDib == NULL ){
- m_pImageObject->m_nLastError = IMAGELIB_MEMORY_ALLOCATION_ERROR;
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( FALSE );
- }
- pNewBuffer = (unsigned char *) ::GlobalLock( hNewDib );
- if( pNewBuffer == NULL ){
- ::GlobalFree( hNewDib );
- m_pImageObject->m_nLastError = IMAGELIB_MEMORY_LOCK_ERROR;
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( FALSE );
- }
- pNewBFH = (BITMAPFILEHEADER *) pNewBuffer;
- pNewBIH = (BITMAPINFOHEADER *) &pNewBuffer[sizeof(BITMAPFILEHEADER)];
- pNewPalette = (RGBQUAD *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pNewBits = (unsigned char *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- *pNewBFH = *pOldBFH;
- *pNewBIH = *pOldBIH;
- for( i=0; i<nNumColors; i++ ) pNewPalette[i] = pOldPalette[i];
- memcpy( pNewBits, pOldBits, nWidthBytes * nHeight );
- unsigned char ucRed, ucGreen, ucBlue;
- switch( m_pImageObject->GetNumBits() ){
- case 8:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( nX1 - 1 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += nX1;
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- Data = pOldTemp[i+l];
- dwMedianList[i] = ( (DWORD) pOldPalette[Data].rgbRed * 30 +
- (DWORD) pOldPalette[Data].rgbGreen * 59 +
- (DWORD) pOldPalette[Data].rgbBlue * 11 ) / 100;
- nLoc[i] = ( i + l );
- }
- for( i=0; i<3; i++ ){
- Data = pOldTemp[i+l+nWidthBytes];
- dwMedianList[i+3] = ( (DWORD) pOldPalette[Data].rgbRed * 30 +
- (DWORD) pOldPalette[Data].rgbGreen * 59 +
- (DWORD) pOldPalette[Data].rgbBlue * 11 ) / 100;
- nLoc[i+3] = ( i + l ) + nWidthBytes;
- }
- for( i=0; i<3; i++ ){
- Data = pOldTemp[i+l+nWidthBytes*2];
- dwMedianList[i+6] = ( (DWORD) pOldPalette[Data].rgbRed * 30 +
- (DWORD) pOldPalette[Data].rgbGreen * 59 +
- (DWORD) pOldPalette[Data].rgbBlue * 11 ) / 100;
- nLoc[i+6] = ( i + l ) + nWidthBytes * 2;
- }
- // Sort the list;
- for( i=1; i<9; i++ ){
- for( j=0; j<i; j++ ){
- if( dwMedianList[i] < dwMedianList[j] ){
- DWORD dwTmp;
- int nTmp;
- dwTmp = dwMedianList[i];
- nTmp = nLoc[i];
- for( k=i; k>j; k-- ){
- dwMedianList[k] = dwMedianList[k-1];
- nLoc[k] = nLoc[k-1];
- }
- dwMedianList[j] = dwTmp;
- nLoc[j] = nTmp;
- break;
- }
- }
- }
- // Get the median RGB values and store them
- Data = pOldTemp[nLoc[4]];
- *pNewTemp++ = (unsigned char) m_pImageObject->GetNearestIndex( pNewPalette[Data].rgbRed, pNewPalette[Data].rgbGreen, pNewPalette[Data].rgbBlue, pNewPalette, nNumColors );
- l++;
- }
- }
- break;
- case 16:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( ( nX1 - 1 ) * 2 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += ( nX1 * 2 );
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[(i+l)*2] );
- dwMedianList[i] = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- nLoc[i] = ( i + l ) * 2;
- }
- for( i=0; i<3; i++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(i+l)*2] );
- dwMedianList[i+3] = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- nLoc[i+3] = ( i + l ) * 2 + nWidthBytes;
- }
- for( i=0; i<3; i++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes*2+(i+l)*2] );
- dwMedianList[i+6] = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- nLoc[i+6] = ( i + l ) * 2 + nWidthBytes * 2;
- }
- // Sort the list;
- for( i=1; i<9; i++ ){
- for( j=0; j<i; j++ ){
- if( dwMedianList[i] < dwMedianList[j] ){
- DWORD dwTmp;
- int nTmp;
- dwTmp = dwMedianList[i];
- nTmp = nLoc[i];
- for( k=i; k>j; k-- ){
- dwMedianList[k] = dwMedianList[k-1];
- nLoc[k] = nLoc[k-1];
- }
- dwMedianList[j] = dwTmp;
- nLoc[j] = nTmp;
- break;
- }
- }
- }
- // Get the median RGB values and store them
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[nLoc[4]] );
- PUTRGB555( ucRed, ucGreen, ucBlue, pNewTemp );
- pNewTemp += 2;
- l++;
- }
- }
- break;
- case 24:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( ( nX1 - 1 ) * 3 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += ( nX1 * 3 );
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- dwMedianList[i] = ( (DWORD) pOldTemp[(i+l)*3+2] * 30 +
- (DWORD) pOldTemp[(i+l)*3+1] * 59 +
- (DWORD) pOldTemp[(i+l)*3] * 11 ) / 100;
- nLoc[i] = ( i + l ) * 3;
- }
- for( i=0; i<3; i++ ){
- dwMedianList[i+3] = ( (DWORD) pOldTemp[nWidthBytes+(i+l)*3+2] * 30 +
- (DWORD) pOldTemp[nWidthBytes+(i+l)*3+1] * 59 +
- (DWORD) pOldTemp[nWidthBytes+(i+l)*3] * 11 ) / 100;
- nLoc[i+3] = ( i + l ) * 3 + nWidthBytes;
- }
- for( i=0; i<3; i++ ){
- dwMedianList[i+6] = ( (DWORD) pOldTemp[nWidthBytes*2+(i+l)*3+2] * 30 +
- (DWORD) pOldTemp[nWidthBytes*2+(i+l)*3+1] * 59 +
- (DWORD) pOldTemp[nWidthBytes*2+(i+l)*3] * 11 ) / 100;
- nLoc[i+6] = ( i + l ) * 3 + nWidthBytes * 2;
- }
- // Sort the list;
- for( i=1; i<9; i++ ){
- for( j=0; j<i; j++ ){
- if( dwMedianList[i] < dwMedianList[j] ){
- DWORD dwTmp;
- int nTmp;
- dwTmp = dwMedianList[i];
- nTmp = nLoc[i];
- for( k=i; k>j; k-- ){
- dwMedianList[k] = dwMedianList[k-1];
- nLoc[k] = nLoc[k-1];
- }
- dwMedianList[j] = dwTmp;
- nLoc[j] = nTmp;
- break;
- }
- }
- }
- // Get the median RGB values and store them
- *pNewTemp++ = pOldTemp[nLoc[4]];
- *pNewTemp++ = pOldTemp[nLoc[4]+1];
- *pNewTemp++ = pOldTemp[nLoc[4]+2];
- l++;
- }
- }
- break;
- case 32:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( ( nX1 - 1 ) * 4 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += ( nX1 * 4 );
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[(i+l)*4] );
- dwMedianList[i] = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- nLoc[i] = ( i + l ) * 4;
- }
- for( i=0; i<3; i++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(i+l)*4] );
- dwMedianList[i+3] = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- nLoc[i+3] = ( i + l ) * 4 + nWidthBytes;
- }
- for( i=0; i<3; i++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes*2+(i+l)*4] );
- dwMedianList[i+6] = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- nLoc[i+6] = ( i + l ) * 4 + nWidthBytes * 2;
- }
- // Sort the list;
- for( i=1; i<9; i++ ){
- for( j=0; j<i; j++ ){
- if( dwMedianList[i] < dwMedianList[j] ){
- DWORD dwTmp;
- int nTmp;
- dwTmp = dwMedianList[i];
- nTmp = nLoc[i];
- for( k=i; k>j; k-- ){
- dwMedianList[k] = dwMedianList[k-1];
- nLoc[k] = nLoc[k-1];
- }
- dwMedianList[j] = dwTmp;
- nLoc[j] = nTmp;
- break;
- }
- }
- }
- // Get the median RGB values and store them
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[nLoc[4]] );
- PUTRGB888( ucRed, ucGreen, ucBlue, pNewTemp );
- pNewTemp += 4;
- l++;
- }
- }
- break;
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- ::GlobalFree( m_pImageObject->GetDib() );
- ::GlobalUnlock( hNewDib );
- m_pImageObject->SetDib( hNewDib );
- return( TRUE );
- }
- BOOL CImageAreaProcesses::ChangeContrast( int nContrast, 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( nContrast == 100 ) return( TRUE );
- return( EqualizeContrast( nX1, nY1, nX2, nY2, nContrast ) );
- }
- BOOL CImageAreaProcesses::EqualizeContrast( int nX1, int nY1, int nX2, int nY2, int nThresholdFactor, CImageObject *pImageObject )
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- if( m_pImageObject->GetNumBits() == 1 ) return( FALSE );
- int nDestMin = 0;
- int nDestMax = 255;
- BOOL bLessThanHalf, bCompleteImage;
- m_pImageObject->NormalizeCoordinates( &nX1, &nY1, &nX2, &nY2, &bCompleteImage, &bLessThanHalf );
- unsigned char *pBuffer, *pBits, *pTemp;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( FALSE );
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- int *pnHistogramBuffer;
- pnHistogramBuffer = CreateHistogram( nX1, nY1, nX2, nY2, pBits, pPalette, nWidthBytes );
- if( pnHistogramBuffer == NULL ){
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( FALSE );
- }
- int x, y, nTemp, i;
- int nMinIndex = pnHistogramBuffer[256*4+2];
- int nMaxIndex = pnHistogramBuffer[256*4+3];
- int nNumSteps;
- int nThreshold = pnHistogramBuffer[256*4+1];
- if( nMinIndex >= 253 && nMaxIndex <= 2 ){
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( FALSE );
- }
- nNumSteps = nMaxIndex - nMinIndex + 1;
- if( nThresholdFactor != -1 ){
- nDestMin = ( nMinIndex * ( 200 - nThresholdFactor ) ) / 100;
- if( nDestMin > 255 ) nDestMin = 255;
- nDestMax = 255 - ( ( ( 255 - nMaxIndex ) * ( 200 - nThresholdFactor ) ) / 100 );
- if( nDestMax < 0 ) nDestMax = 0;
- while( nDestMin > nDestMax ) nDestMin--, nDestMax++;
- if( nDestMin > 255 ) nDestMin = 255;
- if( nDestMax < 0 ) nDestMax = 0;
- }
- double dStep = 256.0 / (double) nNumSteps;
- double dDestStep = (double) ( nDestMax - nDestMin + 1 ) / 256.0;
- double result;
- unsigned char ucRed, ucGreen, ucBlue, ucData;
- DWORD dwGray, dwNewGray;
- CPalette *_pPalette = m_pImageObject->GetPalette();
- RGBQUAD OldPalette[256];
- for( i=0; i<256; i++ ) memset( &OldPalette[i], 0, sizeof( RGBQUAD ) );
- for( i=0; i<nNumColors; i++ ) OldPalette[i] = pPalette[i];
- if( m_pImageObject->GetNumBits() <= 8 && ( bCompleteImage || !bLessThanHalf ) ){
- for( i=0; i<nNumColors; i++ ){
- OldPalette[i] = pPalette[i];
- dwGray = ( (DWORD) pPalette[i].rgbRed * 30 +
- (DWORD) pPalette[i].rgbGreen * 59 +
- (DWORD) pPalette[i].rgbBlue * 11 ) / 100;
- if( dwGray <= (DWORD) nThreshold ) pPalette[i].rgbRed = pPalette[i].rgbGreen = pPalette[i].rgbBlue = 0;
- else{
- nTemp = (int) dwGray - nMinIndex;
- if( nTemp < 0 ) nTemp = 0;
- result = (double) nTemp;
- result *= dStep;
- result += (double) nDestMin;
- result *= dDestStep;
- dwNewGray = (DWORD) result;
- if( dwNewGray > 255 ) dwNewGray = 255;
- if( dwNewGray > dwGray ){
- do{
- if( pPalette[i].rgbRed < 255 ) pPalette[i].rgbRed++;
- if( pPalette[i].rgbGreen < 255 ) pPalette[i].rgbGreen++;
- if( pPalette[i].rgbBlue < 255 ) pPalette[i].rgbBlue++;
- dwGray = ( (DWORD) pPalette[i].rgbRed * 30 +
- (DWORD) pPalette[i].rgbGreen * 59 +
- (DWORD) pPalette[i].rgbBlue * 11 ) / 100;
- } while( dwGray < dwNewGray );
- }
- else if( dwNewGray < dwGray ){
- do{
- if( pPalette[i].rgbRed > 0 ) pPalette[i].rgbRed--;
- if( pPalette[i].rgbGreen > 0 ) pPalette[i].rgbGreen--;
- if( pPalette[i].rgbBlue > 0 ) pPalette[i].rgbBlue--;
- dwGray = ( (DWORD) pPalette[i].rgbRed * 30 +
- (DWORD) pPalette[i].rgbGreen * 59 +
- (DWORD) pPalette[i].rgbBlue * 11 ) / 100;
- } while( dwGray > dwNewGray );
- }
- }
- }
- _pPalette->DeleteObject();
- LOGPALETTE *pLogPal = m_pImageObject->CreateLogPalette( pPalette, nNumColors );
- if( pLogPal != NULL ){
- _pPalette->CreatePalette( pLogPal );
- delete [] pLogPal;
- }
- }
- int nWidth = m_pImageObject->GetWidth();
- int nHeight = m_pImageObject->GetHeight();
- switch( m_pImageObject->GetNumBits() ){
- case 4:
- if( !bCompleteImage ){
- if( !bLessThanHalf ){
- for( y=0; y<nHeight; y++ ){
- pTemp = pBits;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- for( x=0; x<nWidth; x++ ){
- if( !( x >= nX1 && y >= nY1 && x <= nX2 && y <= nY2 ) ){
- ucData = pTemp[0];
- ucRed = OldPalette[ucData].rgbRed;
- ucGreen = OldPalette[ucData].rgbGreen;
- ucBlue = OldPalette[ucData].rgbBlue;
- ucData = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- pTemp[0] = ucData;
- }
- pTemp++;
- }
- }
- }
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pTemp += ( nX1 / 2 );
- for( x=nX1; x<=nX2; x++ ){
- ucData = pTemp[0];
- if( !( x & 1 ) ) ucData >>= 4;
- ucData &= 0x0f;
- ucRed = OldPalette[ucData].rgbRed;
- ucGreen = OldPalette[ucData].rgbGreen;
- ucBlue = OldPalette[ucData].rgbBlue;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- nTemp = (int) dwGray - nMinIndex;
- if( nTemp < 0 ) nTemp = 0;
- result = (double) nTemp;
- result *= dStep;
- result += (double) nDestMin;
- result *= dDestStep;
- dwNewGray = (DWORD) result;
- if( dwNewGray > 255 ) dwNewGray = 255;
- if( dwNewGray > dwGray ){
- do{
- if( ucRed < 255 ) ucRed++;
- if( ucGreen < 255 ) ucGreen++;
- if( ucBlue < 255 ) ucBlue++;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- } while( dwGray < dwNewGray );
- }
- else if( dwNewGray < dwGray ){
- do{
- if( ucRed > 0 ) ucRed--;
- if( ucGreen > 0 ) ucGreen--;
- if( ucBlue > 0 ) ucBlue--;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- } while( dwGray > dwNewGray );
- }
- ucData = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- if( !( x & 1 ) ) pTemp[0] = ( ucData << 4 );
- else pTemp[0] |= ucData;
- }
- if( ( x & 1 ) ) pTemp++;
- }
- }
- break;
- case 8:
- if( !bCompleteImage ){
- if( !bLessThanHalf ){
- for( y=0; y<nHeight; y++ ){
- pTemp = pBits;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- for( x=0; x<nWidth; x++ ){
- if( !( x >= nX1 && y >= nY1 && x <= nX2 && y <= nY2 ) ){
- ucData = pTemp[0];
- ucRed = OldPalette[ucData].rgbRed;
- ucGreen = OldPalette[ucData].rgbGreen;
- ucBlue = OldPalette[ucData].rgbBlue;
- ucData = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- pTemp[0] = ucData;
- }
- pTemp++;
- }
- }
- }
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pTemp += nX1;
- for( x=nX1; x<=nX2; x++ ){
- ucData = pTemp[0];
- ucRed = OldPalette[ucData].rgbRed;
- ucGreen = OldPalette[ucData].rgbGreen;
- ucBlue = OldPalette[ucData].rgbBlue;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- nTemp = (int) dwGray - nMinIndex;
- if( nTemp < 0 ) nTemp = 0;
- result = (double) nTemp;
- result *= dStep;
- result += (double) nDestMin;
- result *= dDestStep;
- dwNewGray = (DWORD) result;
- if( dwNewGray > 255 ) dwNewGray = 255;
- if( dwNewGray > dwGray ){
- do{
- if( ucRed < 255 ) ucRed++;
- if( ucGreen < 255 ) ucGreen++;
- if( ucBlue < 255 ) ucBlue++;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- } while( dwGray < dwNewGray );
- }
- else if( dwNewGray < dwGray ){
- do{
- if( ucRed > 0 ) ucRed--;
- if( ucGreen > 0 ) ucGreen--;
- if( ucBlue > 0 ) ucBlue--;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- } while( dwGray > dwNewGray );
- }
- ucData = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pPalette, nNumColors );
- *pTemp++ = ucData;
- }
- }
- }
- break;
- case 16:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- 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;
- nTemp = (int) dwGray - nMinIndex;
- if( nTemp < 0 ) nTemp = 0;
- result = (double) nTemp;
- result *= dStep;
- result += (double) nDestMin;
- result *= dDestStep;
- dwNewGray = (DWORD) result;
- if( dwNewGray > 255 ) dwNewGray = 255;
- if( dwNewGray > dwGray ){
- do{
- if( ucRed < 255 ) ucRed++;
- if( ucGreen < 255 ) ucGreen++;
- if( ucBlue < 255 ) ucBlue++;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- } while( dwGray < dwNewGray );
- }
- else if( dwNewGray < dwGray ){
- do{
- if( ucRed > 0 ) ucRed--;
- if( ucGreen > 0 ) ucGreen--;
- if(ucBlue > 0 ) ucBlue--;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- } while( dwGray > dwNewGray );
- }
- PUTRGB555( ucRed, ucGreen, ucBlue, pTemp );
- pTemp += 2;
- }
- }
- break;
- case 24:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pTemp += ( nX1 * 3 );
- for( x=nX1; x<=nX2; x++ ){
- dwGray = ( (DWORD) pTemp[2] * 30 +
- (DWORD) pTemp[1] * 59 +
- (DWORD) pTemp[0] * 11 ) / 100;
- nTemp = (int) dwGray - nMinIndex;
- if( nTemp < 0 ) nTemp = 0;
- result = (double) nTemp;
- result *= dStep;
- result += (double) nDestMin;
- result *= dDestStep;
- dwNewGray = (DWORD) result;
- if( dwNewGray > 255 ) dwNewGray = 255;
- if( dwNewGray > dwGray ){
- do{
- if( pTemp[0] < 255 ) pTemp[0]++;
- if( pTemp[1] < 255 ) pTemp[1]++;
- if( pTemp[2] < 255 ) pTemp[2]++;
- dwGray = ( (DWORD) pTemp[2] * 30 +
- (DWORD) pTemp[1] * 59 +
- (DWORD) pTemp[0] * 11 ) / 100;
- } while( dwGray < dwNewGray );
- }
- else if( dwNewGray < dwGray ){
- do{
- if( pTemp[0] > 0 ) pTemp[0]--;
- if( pTemp[1] > 0 ) pTemp[1]--;
- if( pTemp[2] > 0 ) pTemp[2]--;
- dwGray = ( (DWORD) pTemp[2] * 30 +
- (DWORD) pTemp[1] * 59 +
- (DWORD) pTemp[0] * 11 ) / 100;
- } while( dwGray > dwNewGray );
- }
- pTemp += 3;
- }
- }
- break;
- case 32:
- for( y=nY1; y<=nY2; y++ ){
- pTemp = pBits;
- pTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pTemp += ( nX1 * 4 );
- for( x=nX1; x<=nX2; x++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, pTemp );
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- nTemp = (int) dwGray - nMinIndex;
- if( nTemp < 0 ) nTemp = 0;
- result = (double) nTemp;
- result *= dStep;
- result += (double) nDestMin;
- result *= dDestStep;
- dwNewGray = (DWORD) result;
- if( dwNewGray > 255 ) dwNewGray = 255;
- if( dwNewGray > dwGray ){
- do{
- if( ucRed < 255 ) ucRed++;
- if( ucGreen < 255 ) ucGreen++;
- if( ucBlue < 255 ) ucBlue++;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- } while( dwGray < dwNewGray );
- }
- else if( dwNewGray < dwGray ){
- do{
- if( ucRed > 0 ) ucRed--;
- if( ucGreen > 0 ) ucGreen--;
- if(ucBlue > 0 ) ucBlue--;
- dwGray = ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100;
- } while( dwGray > dwNewGray );
- }
- PUTRGB888( ucRed, ucGreen, ucBlue, pTemp );
- pTemp += 4;
- }
- }
- break;
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( TRUE );
- }
- BOOL CImageAreaProcesses::HighpassFilter( int nX1, int nY1, int nX2, int nY2, CImageObject *pImageObject )
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- static DWORD dwFact2[] = { 1, 1, 1, 1, 16, 1, 1, 1, 1 };
- LowpassFilter( nX1, nY1, nX2, nY2, dwFact2 );
- return( TRUE );
- }
- BOOL CImageAreaProcesses::LowpassFilter( int nX1, int nY1, int nX2, int nY2, DWORD *dwFact, CImageObject *pImageObject )
- {
- if( pImageObject != NULL ) m_pImageObject = pImageObject;
- if( m_pImageObject == NULL ) return( FALSE );
- if( m_pImageObject->GetNumBits() == 1 ) return( FALSE );
- m_pImageObject->NormalizeCoordinates( &nX1, &nY1, &nX2, &nY2 );
- int nWidth = m_pImageObject->GetWidth();
- int nHeight = m_pImageObject->GetHeight();
- if( nX1 < 2 ) nX1 = 2;
- if( nY1 < 2 ) nY1 = 2;
- if( nX2 >= nWidth - 2 ) nX2 = nWidth - 2;
- if( nY2 >= nHeight - 2 ) nY2 = nHeight - 2;
- DWORD dwGrayTotal, dwDivisor = 0;
- unsigned char Data;
- DWORD *dwFactor;
- static DWORD dwFact1[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
- if( dwFact == NULL ) dwFactor = dwFact1;
- else dwFactor = dwFact;
- int i;
- for( i=0; i<9; i++ ) dwDivisor += dwFactor[i];
- unsigned char *pOldBuffer, *pNewBuffer, *pOldBits, *pNewBits, *pOldTemp, *pNewTemp;
- BITMAPFILEHEADER *pOldBFH, *pNewBFH;
- BITMAPINFOHEADER *pOldBIH, *pNewBIH;
- RGBQUAD *pOldPalette, *pNewPalette;
- int nWidthBytes, nNumColors, x, y, j, l;
- pOldBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pOldBuffer == NULL ) return( FALSE );
- pOldBFH = (BITMAPFILEHEADER *) pOldBuffer;
- pOldBIH = (BITMAPINFOHEADER *) &pOldBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pOldPalette = (RGBQUAD *) &pOldBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pOldBits = (unsigned char *) &pOldBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- DWORD dwNewSize, dwGray, dwTemp;
- HGLOBAL hNewDib;
- dwNewSize = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) + nNumColors * sizeof( RGBQUAD ) + nWidthBytes * nHeight;
- hNewDib = ::GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, dwNewSize );
- if( hNewDib == NULL ){
- m_pImageObject->m_nLastError = IMAGELIB_MEMORY_ALLOCATION_ERROR;
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( FALSE );
- }
- pNewBuffer = (unsigned char *) ::GlobalLock( hNewDib );
- if( pNewBuffer == NULL ){
- ::GlobalFree( hNewDib );
- m_pImageObject->m_nLastError = IMAGELIB_MEMORY_LOCK_ERROR;
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( FALSE );
- }
- pNewBFH = (BITMAPFILEHEADER *) pNewBuffer;
- pNewBIH = (BITMAPINFOHEADER *) &pNewBuffer[sizeof(BITMAPFILEHEADER)];
- pNewPalette = (RGBQUAD *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pNewBits = (unsigned char *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- *pNewBFH = *pOldBFH;
- *pNewBIH = *pOldBIH;
- for( i=0; i<nNumColors; i++ ) pNewPalette[i] = pOldPalette[i];
- memcpy( pNewBits, pOldBits, nWidthBytes * nHeight );
- unsigned char ucRed, ucGreen, ucBlue;
- switch( m_pImageObject->GetNumBits() ){
- case 8:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( nX1 - 1 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += nX1;
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- // List all pixel gray values in the 3x3 area
- j = 0;
- for( i=0; i<3; i++ ){
- Data = pOldTemp[i+l];
- dwGrayTotal += ( ( ( (DWORD) pOldPalette[Data].rgbRed * 30 +
- (DWORD) pOldPalette[Data].rgbGreen * 59 +
- (DWORD) pOldPalette[Data].rgbBlue * 11 ) / 100 ) * dwFactor[j++] );
- }
- for( i=0; i<3; i++ ){
- Data = pOldTemp[i+l+nWidthBytes];
- dwGrayTotal += ( ( ( (DWORD) pOldPalette[Data].rgbRed * 30 +
- (DWORD) pOldPalette[Data].rgbGreen * 59 +
- (DWORD) pOldPalette[Data].rgbBlue * 11 ) / 100 ) * dwFactor[j++] );
- }
- for( i=0; i<3; i++ ){
- Data = pOldTemp[i+l+nWidthBytes*2];
- dwGrayTotal += ( ( ( (DWORD) pOldPalette[Data].rgbRed * 30 +
- (DWORD) pOldPalette[Data].rgbGreen * 59 +
- (DWORD) pOldPalette[Data].rgbBlue * 11 ) / 100 ) * dwFactor[j++] );
- }
- dwGrayTotal /= dwDivisor;
- Data = pOldTemp[1+nWidthBytes];
- ucRed = pOldPalette[Data].rgbRed;
- ucGreen = pOldPalette[Data].rgbGreen;
- ucBlue = pOldPalette[Data].rgbBlue;
- dwGray = ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 );
- if( dwGray != 0 ) ucRed = (unsigned char) ( ( (DWORD) pOldPalette[Data].rgbRed * dwGrayTotal ) / dwGray );
- else ucRed = (unsigned char) ( ( dwGray + dwGrayTotal ) / 2 );
- if( dwGray != 0 ) ucGreen = (unsigned char) ( ( (DWORD) pOldPalette[Data].rgbGreen * dwGrayTotal ) / dwGray );
- else ucGreen = (unsigned char) ( ( dwGray + dwGrayTotal ) / 2 );
- if( dwGray != 0 ) ucBlue = (unsigned char) ( ( (DWORD) pOldPalette[Data].rgbBlue * dwGrayTotal ) / dwGray );
- else ucBlue = (unsigned char) ( ( dwGray + dwGrayTotal ) / 2 );
- *pNewTemp++ = (unsigned char) m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pNewPalette, nNumColors );
- l++;
- }
- }
- break;
- case 16:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( ( nX1 - 1 ) * 2 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += ( nX1 * 2 );
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- dwGrayTotal = 0;
- j = 0;
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[(i+l)*2] );
- dwGrayTotal += ( ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 ) * dwFactor[j++] );
- }
- for( i=0; i<3; i++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(i+l)*2] );
- dwGrayTotal += ( ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 ) * dwFactor[j++] );
- }
- for( i=0; i<3; i++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes*2+(i+l)*2] );
- dwGrayTotal += ( ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 ) * dwFactor[j++] );
- }
- dwGrayTotal /= dwDivisor;
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(1+l)*2] );
- dwGray = ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 );
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(1+l)*2] );
- if( dwGray != 0 ) dwTemp = ( ( (DWORD) ucRed * dwGrayTotal ) / dwGray );
- else dwTemp = ( dwGrayTotal + dwGray ) / 2;
- if( dwTemp > 255 ) dwTemp = 255;
- ucRed = (unsigned char) dwTemp;
- if( dwGray != 0 ) dwTemp = ( ( (DWORD) ucGreen * dwGrayTotal ) / dwGray );
- else dwTemp = ( dwGrayTotal + dwGray ) / 2;
- if( dwTemp > 255 ) dwTemp = 255;
- ucGreen = (unsigned char) dwTemp;
- if( dwGray != 0 ) dwTemp = ( ( (DWORD) ucBlue * dwGrayTotal ) / dwGray );
- else dwTemp = ( dwGrayTotal + dwGray ) / 2;
- if( dwTemp > 255 ) dwTemp = 255;
- ucBlue = (unsigned char) dwTemp;
- PUTRGB555( ucRed, ucGreen, ucBlue, pNewTemp );
- pNewTemp += 2;
- l++;
- }
- }
- break;
- case 24:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( ( nX1 - 1 ) * 3 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += ( nX1 * 3 );
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- dwGrayTotal = 0;
- j = 0;
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- dwGrayTotal += ( ( ( (DWORD) pOldTemp[(i+l)*3+2] * 30 +
- (DWORD) pOldTemp[(i+l)*3+1] * 59 +
- (DWORD) pOldTemp[(i+l)*3] * 11 ) / 100 ) * dwFactor[j++] );
- }
- for( i=0; i<3; i++ ){
- dwGrayTotal += ( ( ( (DWORD) pOldTemp[nWidthBytes+(i+l)*3+2] * 30 +
- (DWORD) pOldTemp[nWidthBytes+(i+l)*3+1] * 59 +
- (DWORD) pOldTemp[nWidthBytes+(i+l)*3] * 11 ) / 100 ) * dwFactor[j++] );
- }
- for( i=0; i<3; i++ ){
- dwGrayTotal += ( ( ( (DWORD) pOldTemp[nWidthBytes*2+(i+l)*3+2] * 30 +
- (DWORD) pOldTemp[nWidthBytes*2+(i+l)*3+1] * 59 +
- (DWORD) pOldTemp[nWidthBytes*2+(i+l)*3] * 11 ) / 100 ) * dwFactor[j++] );
- }
- dwGrayTotal /= dwDivisor;
- dwGray = ( ( (DWORD) pOldTemp[nWidthBytes+(1+l)*3+2] * 30 +
- (DWORD) pOldTemp[nWidthBytes+(1+l)*3+1] * 59 +
- (DWORD) pOldTemp[nWidthBytes+(1+l)*3] * 11 ) / 100 );
- if( dwGray != 0 ) dwTemp = ( ( (DWORD) pOldTemp[(l+1)*3+nWidthBytes+2] * dwGrayTotal ) / dwGray );
- else dwTemp = ( dwGrayTotal + dwGray ) / 2;
- if( dwTemp > 255 ) dwTemp = 255;
- pNewTemp[2] = (unsigned char) dwTemp;
- if( dwGray != 0 ) dwTemp = ( ( (DWORD) pOldTemp[(l+1)*3+nWidthBytes+1] * dwGrayTotal ) / dwGray );
- else dwTemp = ( dwGrayTotal + dwGray ) / 2;
- if( dwTemp > 255 ) dwTemp = 255;
- pNewTemp[1] = (unsigned char) dwTemp;
- if( dwGray != 0 ) dwTemp = ( ( (DWORD) pOldTemp[(l+1)*3+nWidthBytes] * dwGrayTotal ) / dwGray );
- else dwTemp = ( dwGrayTotal + dwGray ) / 2;
- if( dwTemp > 255 ) dwTemp = 255;
- pNewTemp[0] = (unsigned char) dwTemp;
- pNewTemp += 3;
- l++;
- }
- }
- break;
- case 32:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( ( nX1 - 1 ) * 4 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += ( nX1 * 4 );
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- dwGrayTotal = 0;
- j = 0;
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[(i+l)*4] );
- dwGrayTotal += ( ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 ) * dwFactor[j++] );
- }
- for( i=0; i<3; i++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(i+l)*4] );
- dwGrayTotal += ( ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 ) * dwFactor[j++] );
- }
- for( i=0; i<3; i++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes*2+(i+l)*4] );
- dwGrayTotal += ( ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 ) * dwFactor[j++] );
- }
- dwGrayTotal /= dwDivisor;
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(1+l)*4] );
- dwGray = ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 );
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(1+l)*4] );
- if( dwGray != 0 ) dwTemp = ( ( (DWORD) ucRed * dwGrayTotal ) / dwGray );
- else dwTemp = ( dwGrayTotal + dwGray ) / 2;
- if( dwTemp > 255 ) dwTemp = 255;
- ucRed = (unsigned char) dwTemp;
- if( dwGray != 0 ) dwTemp = ( ( (DWORD) ucGreen * dwGrayTotal ) / dwGray );
- else dwTemp = ( dwGrayTotal + dwGray ) / 2;
- if( dwTemp > 255 ) dwTemp = 255;
- ucGreen = (unsigned char) dwTemp;
- if( dwGray != 0 ) dwTemp = ( ( (DWORD) ucBlue * dwGrayTotal ) / dwGray );
- else dwTemp = ( dwGrayTotal + dwGray ) / 2;
- if( dwTemp > 255 ) dwTemp = 255;
- ucBlue = (unsigned char) dwTemp;
- PUTRGB888( ucRed, ucGreen, ucBlue, pNewTemp );
- pNewTemp += 4;
- l++;
- }
- }
- break;
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- ::GlobalFree( m_pImageObject->GetDib() );
- ::GlobalUnlock( hNewDib );
- m_pImageObject->SetDib( hNewDib );
- return( TRUE );
- }
- BOOL CImageAreaProcesses::EdgeEnhance( 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 );
- m_pImageObject->NormalizeCoordinates( &nX1, &nY1, &nX2, &nY2 );
- int nWidth = m_pImageObject->GetWidth();
- int nHeight = m_pImageObject->GetHeight();
- if( nX1 < 2 ) nX1 = 2;
- if( nY1 < 2 ) nY1 = 2;
- if( nX2 >= nWidth - 2 ) nX2 = nWidth - 2;
- if( nY2 >= nHeight - 2 ) nY2 = nHeight - 2;
- DWORD dwGrayTotal;
- unsigned char Data;
- unsigned char *pOldBuffer, *pNewBuffer, *pOldBits, *pNewBits, *pOldTemp, *pNewTemp;
- BITMAPFILEHEADER *pOldBFH, *pNewBFH;
- BITMAPINFOHEADER *pOldBIH, *pNewBIH;
- RGBQUAD *pOldPalette, *pNewPalette;
- int nWidthBytes, nNumColors, x, y, j, l;
- pOldBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pOldBuffer == NULL ) return( FALSE );
- pOldBFH = (BITMAPFILEHEADER *) pOldBuffer;
- pOldBIH = (BITMAPINFOHEADER *) &pOldBuffer[sizeof(BITMAPFILEHEADER)];
- nNumColors = m_pImageObject->GetNumColors();
- pOldPalette = (RGBQUAD *) &pOldBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pOldBits = (unsigned char *) &pOldBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- DWORD dwNewSize, dwTemp, dwGrayList[9];
- HGLOBAL hNewDib;
- dwNewSize = sizeof( BITMAPFILEHEADER ) + sizeof( BITMAPINFOHEADER ) + nNumColors * sizeof( RGBQUAD ) + nWidthBytes * nHeight;
- hNewDib = ::GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, dwNewSize );
- if( hNewDib == NULL ){
- m_pImageObject->m_nLastError = IMAGELIB_MEMORY_ALLOCATION_ERROR;
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( FALSE );
- }
- pNewBuffer = (unsigned char *) ::GlobalLock( hNewDib );
- if( pNewBuffer == NULL ){
- ::GlobalFree( hNewDib );
- m_pImageObject->m_nLastError = IMAGELIB_MEMORY_LOCK_ERROR;
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( FALSE );
- }
- pNewBFH = (BITMAPFILEHEADER *) pNewBuffer;
- pNewBIH = (BITMAPINFOHEADER *) &pNewBuffer[sizeof(BITMAPFILEHEADER)];
- pNewPalette = (RGBQUAD *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pNewBits = (unsigned char *) &pNewBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- *pNewBFH = *pOldBFH;
- *pNewBIH = *pOldBIH;
- int i;
- for( i=0; i<nNumColors; i++ ) pNewPalette[i] = pOldPalette[i];
- memcpy( pNewBits, pOldBits, nWidthBytes * nHeight );
- unsigned char ucRed, ucGreen, ucBlue;
- DWORD dwLineAEIAveBelow, dwLineAEIAveAbove, dwLineAEIMaxDif;
- DWORD dwLineBEHAveBelow, dwLineBEHAveAbove, dwLineBEHMaxDif;
- DWORD dwLineCEGAveBelow, dwLineCEGAveAbove, dwLineCEGMaxDif;
- DWORD dwLineDEFAveBelow, dwLineDEFAveAbove, dwLineDEFMaxDif;
- DWORD dwMaxDif;
- switch( m_pImageObject->GetNumBits() ){
- case 8:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( nX1 - 1 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += nX1;
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- // List all pixel gray values in the 3x3 area
- j = 0;
- for( i=0; i<3; i++ ){
- Data = pOldTemp[i+l];
- dwGrayList[j++] = ( ( (DWORD) pOldPalette[Data].rgbRed * 30 +
- (DWORD) pOldPalette[Data].rgbGreen * 59 +
- (DWORD) pOldPalette[Data].rgbBlue * 11 ) / 100 );
- }
- for( i=0; i<3; i++ ){
- Data = pOldTemp[i+l+nWidthBytes];
- dwGrayList[j++] = ( ( (DWORD) pOldPalette[Data].rgbRed * 30 +
- (DWORD) pOldPalette[Data].rgbGreen * 59 +
- (DWORD) pOldPalette[Data].rgbBlue * 11 ) / 100 );
- }
- for( i=0; i<3; i++ ){
- Data = pOldTemp[i+l+nWidthBytes*2];
- dwGrayList[j++] = ( ( (DWORD) pOldPalette[Data].rgbRed * 30 +
- (DWORD) pOldPalette[Data].rgbGreen * 59 +
- (DWORD) pOldPalette[Data].rgbBlue * 11 ) / 100 );
- }
- dwLineAEIAveBelow = ( dwGrayList[3] + dwGrayList[6] + dwGrayList[7] ) / 3;
- dwLineAEIAveAbove = ( dwGrayList[1] + dwGrayList[2] + dwGrayList[5] ) / 3;
- if( dwLineAEIAveBelow > dwLineAEIAveAbove ) dwLineAEIMaxDif = dwLineAEIAveBelow - dwLineAEIAveAbove;
- else dwLineAEIMaxDif = dwLineAEIAveAbove - dwLineAEIAveBelow;
- dwLineBEHAveBelow = ( dwGrayList[0] + dwGrayList[3] + dwGrayList[6] ) / 3;
- dwLineBEHAveAbove = ( dwGrayList[2] + dwGrayList[4] + dwGrayList[8] ) / 3;
- if( dwLineBEHAveBelow > dwLineBEHAveAbove ) dwLineBEHMaxDif = dwLineBEHAveBelow - dwLineBEHAveAbove;
- else dwLineBEHMaxDif = dwLineBEHAveAbove - dwLineBEHAveBelow;
- dwLineCEGAveBelow = ( dwGrayList[5] + dwGrayList[7] + dwGrayList[8] ) / 3;
- dwLineCEGAveAbove = ( dwGrayList[0] + dwGrayList[1] + dwGrayList[3] ) / 3;
- if( dwLineCEGAveBelow > dwLineCEGAveAbove ) dwLineCEGMaxDif = dwLineCEGAveBelow - dwLineCEGAveAbove;
- else dwLineCEGMaxDif = dwLineCEGAveAbove - dwLineCEGAveBelow;
- dwLineDEFAveBelow = ( dwGrayList[6] + dwGrayList[7] + dwGrayList[8] ) / 3;
- dwLineDEFAveAbove = ( dwGrayList[0] + dwGrayList[1] + dwGrayList[2] ) / 3;
- if( dwLineDEFAveBelow > dwLineDEFAveAbove ) dwLineDEFMaxDif = dwLineDEFAveBelow - dwLineDEFAveAbove;
- else dwLineDEFMaxDif = dwLineDEFAveAbove - dwLineDEFAveBelow;
- dwMaxDif = MAX( dwLineAEIMaxDif, dwLineBEHMaxDif );
- dwMaxDif = MAX( dwLineCEGMaxDif, dwMaxDif );
- dwMaxDif = MAX( dwLineDEFMaxDif, dwMaxDif );
- Data = pOldTemp[1+l+nWidthBytes];
- if( dwMaxDif > 20 ){
- ucRed = pOldPalette[Data].rgbRed;
- ucGreen = pOldPalette[Data].rgbGreen;
- ucBlue = pOldPalette[Data].rgbBlue;
- dwTemp = ( ( (DWORD) ucRed * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- ucRed = (unsigned char) dwTemp;
- dwTemp = ( ( (DWORD) ucGreen * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- ucGreen = (unsigned char) dwTemp;
- dwTemp = ( ( (DWORD) ucBlue * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- ucBlue = (unsigned char) dwTemp;
- Data = m_pImageObject->GetNearestIndex( ucRed, ucGreen, ucBlue, pNewPalette, nNumColors );
- }
- *pNewTemp++ = Data;
- l++;
- }
- }
- break;
- case 16:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( ( nX1 - 1 ) * 2 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += ( nX1 * 2 );
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- dwGrayTotal = 0;
- j = 0;
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[(i+l)*2] );
- dwGrayList[j++] = ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 );
- }
- for( i=0; i<3; i++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(i+l)*2] );
- dwGrayList[j++] = ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 );
- }
- for( i=0; i<3; i++ ){
- GETRGB555( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes*2+(i+l)*2] );
- dwGrayList[j++] = ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 );
- }
- dwLineAEIAveBelow = ( dwGrayList[3] + dwGrayList[6] + dwGrayList[7] ) / 3;
- dwLineAEIAveAbove = ( dwGrayList[1] + dwGrayList[2] + dwGrayList[5] ) / 3;
- if( dwLineAEIAveBelow > dwLineAEIAveAbove ) dwLineAEIMaxDif = dwLineAEIAveBelow - dwLineAEIAveAbove;
- else dwLineAEIMaxDif = dwLineAEIAveAbove - dwLineAEIAveBelow;
- dwLineBEHAveBelow = ( dwGrayList[0] + dwGrayList[3] + dwGrayList[6] ) / 3;
- dwLineBEHAveAbove = ( dwGrayList[2] + dwGrayList[4] + dwGrayList[8] ) / 3;
- if( dwLineBEHAveBelow > dwLineBEHAveAbove ) dwLineBEHMaxDif = dwLineBEHAveBelow - dwLineBEHAveAbove;
- else dwLineBEHMaxDif = dwLineBEHAveAbove - dwLineBEHAveBelow;
- dwLineCEGAveBelow = ( dwGrayList[5] + dwGrayList[7] + dwGrayList[8] ) / 3;
- dwLineCEGAveAbove = ( dwGrayList[0] + dwGrayList[1] + dwGrayList[3] ) / 3;
- if( dwLineCEGAveBelow > dwLineCEGAveAbove ) dwLineCEGMaxDif = dwLineCEGAveBelow - dwLineCEGAveAbove;
- else dwLineCEGMaxDif = dwLineCEGAveAbove - dwLineCEGAveBelow;
- dwLineDEFAveBelow = ( dwGrayList[6] + dwGrayList[7] + dwGrayList[8] ) / 3;
- dwLineDEFAveAbove = ( dwGrayList[0] + dwGrayList[1] + dwGrayList[2] ) / 3;
- if( dwLineDEFAveBelow > dwLineDEFAveAbove ) dwLineDEFMaxDif = dwLineDEFAveBelow - dwLineDEFAveAbove;
- else dwLineDEFMaxDif = dwLineDEFAveAbove - dwLineDEFAveBelow;
- dwMaxDif = MAX( dwLineAEIMaxDif, dwLineBEHMaxDif );
- dwMaxDif = MAX( dwLineCEGMaxDif, dwMaxDif );
- dwMaxDif = MAX( dwLineDEFMaxDif, dwMaxDif );
- if( dwMaxDif > 20 ){
- GETRGB555( ucRed, ucGreen, ucBlue, pOldTemp[(1+l)*2+nWidthBytes] );
- dwTemp = ( ( (DWORD) ucRed * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- ucRed = (unsigned char) dwTemp;
- dwTemp = ( ( (DWORD) ucGreen * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- ucGreen = (unsigned char) dwTemp;
- dwTemp = ( ( (DWORD) ucBlue * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- ucBlue = (unsigned char) dwTemp;
- PUTRGB555( ucRed, ucGreen, ucBlue, pNewTemp );
- }
- else{
- GETRGB555( ucRed, ucGreen, ucBlue, pOldTemp[(1+l)*2+nWidthBytes] );
- PUTRGB555( ucRed, ucGreen, ucBlue, pNewTemp );
- }
- pNewTemp += 2;
- l++;
- }
- }
- break;
- case 24:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( ( nX1 - 1 ) * 3 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += ( nX1 * 3 );
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- dwGrayTotal = 0;
- j = 0;
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- dwGrayList[j++] = ( ( (DWORD) pOldTemp[(i+l)*3+2] * 30 +
- (DWORD) pOldTemp[(i+l)*3+1] * 59 +
- (DWORD) pOldTemp[(i+l)*3] * 11 ) / 100 );
- }
- for( i=0; i<3; i++ ){
- dwGrayList[j++] = ( ( (DWORD) pOldTemp[nWidthBytes+(i+l)*3+2] * 30 +
- (DWORD) pOldTemp[nWidthBytes+(i+l)*3+1] * 59 +
- (DWORD) pOldTemp[nWidthBytes+(i+l)*3] * 11 ) / 100 );
- }
- for( i=0; i<3; i++ ){
- dwGrayList[j++] = ( ( (DWORD) pOldTemp[nWidthBytes*2+(i+l)*3+2] * 30 +
- (DWORD) pOldTemp[nWidthBytes*2+(i+l)*3+1] * 59 +
- (DWORD) pOldTemp[nWidthBytes*2+(i+l)*3] * 11 ) / 100 );
- }
- dwLineAEIAveBelow = ( dwGrayList[3] + dwGrayList[6] + dwGrayList[7] ) / 3;
- dwLineAEIAveAbove = ( dwGrayList[1] + dwGrayList[2] + dwGrayList[5] ) / 3;
- if( dwLineAEIAveBelow > dwLineAEIAveAbove ) dwLineAEIMaxDif = dwLineAEIAveBelow - dwLineAEIAveAbove;
- else dwLineAEIMaxDif = dwLineAEIAveAbove - dwLineAEIAveBelow;
- dwLineBEHAveBelow = ( dwGrayList[0] + dwGrayList[3] + dwGrayList[6] ) / 3;
- dwLineBEHAveAbove = ( dwGrayList[2] + dwGrayList[4] + dwGrayList[8] ) / 3;
- if( dwLineBEHAveBelow > dwLineBEHAveAbove ) dwLineBEHMaxDif = dwLineBEHAveBelow - dwLineBEHAveAbove;
- else dwLineBEHMaxDif = dwLineBEHAveAbove - dwLineBEHAveBelow;
- dwLineCEGAveBelow = ( dwGrayList[5] + dwGrayList[7] + dwGrayList[8] ) / 3;
- dwLineCEGAveAbove = ( dwGrayList[0] + dwGrayList[1] + dwGrayList[3] ) / 3;
- if( dwLineCEGAveBelow > dwLineCEGAveAbove ) dwLineCEGMaxDif = dwLineCEGAveBelow - dwLineCEGAveAbove;
- else dwLineCEGMaxDif = dwLineCEGAveAbove - dwLineCEGAveBelow;
- dwLineDEFAveBelow = ( dwGrayList[6] + dwGrayList[7] + dwGrayList[8] ) / 3;
- dwLineDEFAveAbove = ( dwGrayList[0] + dwGrayList[1] + dwGrayList[2] ) / 3;
- if( dwLineDEFAveBelow > dwLineDEFAveAbove ) dwLineDEFMaxDif = dwLineDEFAveBelow - dwLineDEFAveAbove;
- else dwLineDEFMaxDif = dwLineDEFAveAbove - dwLineDEFAveBelow;
- dwMaxDif = MAX( dwLineAEIMaxDif, dwLineBEHMaxDif );
- dwMaxDif = MAX( dwLineCEGMaxDif, dwMaxDif );
- dwMaxDif = MAX( dwLineDEFMaxDif, dwMaxDif );
- if( dwMaxDif > 20 ){
- dwTemp = ( ( (DWORD) pOldTemp[(1+l)*3+nWidthBytes] * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- pNewTemp[0] = (unsigned char) dwTemp;
- dwTemp = ( ( (DWORD) pOldTemp[(1+l)*3+nWidthBytes+1] * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- pNewTemp[1] = (unsigned char) dwTemp;
- dwTemp = ( ( (DWORD) pOldTemp[(1+l)*3+nWidthBytes+2] * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- pNewTemp[2] = (unsigned char) dwTemp;
- }
- else{
- pNewTemp[0] = pOldTemp[(1+l)*3+nWidthBytes];
- pNewTemp[1] = pOldTemp[(1+l)*3+nWidthBytes+1];
- pNewTemp[2] = pOldTemp[(1+l)*3+nWidthBytes+2];
- }
- pNewTemp += 3;
- l++;
- }
- }
- break;
- case 32:
- for( y=nY1; y<=nY2; y++ ){
- pOldTemp = pOldBits;
- pOldTemp += ( ( nHeight - 1 - y - 1 ) * nWidthBytes );
- pOldTemp += ( ( nX1 - 1 ) * 4 );
- pNewTemp = pNewBits;
- pNewTemp += ( ( nHeight - 1 - y ) * nWidthBytes );
- pNewTemp += ( nX1 * 4 );
- l = 0;
- for( x=nX1; x<=nX2; x++ ){
- dwGrayTotal = 0;
- j = 0;
- // List all pixel gray values in the 3x3 area
- for( i=0; i<3; i++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[(i+l)*4] );
- dwGrayList[j++] = ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 );
- }
- for( i=0; i<3; i++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes+(i+l)*4] );
- dwGrayList[j++] = ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 );
- }
- for( i=0; i<3; i++ ){
- GETRGB888( ucRed, ucGreen, ucBlue, &pOldTemp[nWidthBytes*2+(i+l)*4] );
- dwGrayList[j++] = ( ( (DWORD) ucRed * 30 +
- (DWORD) ucGreen * 59 +
- (DWORD) ucBlue * 11 ) / 100 );
- }
- dwLineAEIAveBelow = ( dwGrayList[3] + dwGrayList[6] + dwGrayList[7] ) / 3;
- dwLineAEIAveAbove = ( dwGrayList[1] + dwGrayList[2] + dwGrayList[5] ) / 3;
- if( dwLineAEIAveBelow > dwLineAEIAveAbove ) dwLineAEIMaxDif = dwLineAEIAveBelow - dwLineAEIAveAbove;
- else dwLineAEIMaxDif = dwLineAEIAveAbove - dwLineAEIAveBelow;
- dwLineBEHAveBelow = ( dwGrayList[0] + dwGrayList[3] + dwGrayList[6] ) / 3;
- dwLineBEHAveAbove = ( dwGrayList[2] + dwGrayList[4] + dwGrayList[8] ) / 3;
- if( dwLineBEHAveBelow > dwLineBEHAveAbove ) dwLineBEHMaxDif = dwLineBEHAveBelow - dwLineBEHAveAbove;
- else dwLineBEHMaxDif = dwLineBEHAveAbove - dwLineBEHAveBelow;
- dwLineCEGAveBelow = ( dwGrayList[5] + dwGrayList[7] + dwGrayList[8] ) / 3;
- dwLineCEGAveAbove = ( dwGrayList[0] + dwGrayList[1] + dwGrayList[3] ) / 3;
- if( dwLineCEGAveBelow > dwLineCEGAveAbove ) dwLineCEGMaxDif = dwLineCEGAveBelow - dwLineCEGAveAbove;
- else dwLineCEGMaxDif = dwLineCEGAveAbove - dwLineCEGAveBelow;
- dwLineDEFAveBelow = ( dwGrayList[6] + dwGrayList[7] + dwGrayList[8] ) / 3;
- dwLineDEFAveAbove = ( dwGrayList[0] + dwGrayList[1] + dwGrayList[2] ) / 3;
- if( dwLineDEFAveBelow > dwLineDEFAveAbove ) dwLineDEFMaxDif = dwLineDEFAveBelow - dwLineDEFAveAbove;
- else dwLineDEFMaxDif = dwLineDEFAveAbove - dwLineDEFAveBelow;
- dwMaxDif = MAX( dwLineAEIMaxDif, dwLineBEHMaxDif );
- dwMaxDif = MAX( dwLineCEGMaxDif, dwMaxDif );
- dwMaxDif = MAX( dwLineDEFMaxDif, dwMaxDif );
- if( dwMaxDif > 20 ){
- GETRGB888( ucRed, ucGreen, ucBlue, pOldTemp[(1+l)*4+nWidthBytes] );
- dwTemp = ( ( (DWORD) ucRed * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- ucRed = (unsigned char) dwTemp;
- dwTemp = ( ( (DWORD) ucGreen * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- ucGreen = (unsigned char) dwTemp;
- dwTemp = ( ( (DWORD) ucBlue * 12 ) / 10 );
- if( dwTemp > 255 ) dwTemp = 255;
- ucBlue = (unsigned char) dwTemp;
- PUTRGB888( ucRed, ucGreen, ucBlue, pNewTemp );
- }
- else{
- GETRGB888( ucRed, ucGreen, ucBlue, pOldTemp[(1+l)*4+nWidthBytes] );
- PUTRGB888( ucRed, ucGreen, ucBlue, pNewTemp );
- }
- pNewTemp += 4;
- l++;
- }
- }
- break;
- }
- ::GlobalUnlock( m_pImageObject->GetDib() );
- ::GlobalFree( m_pImageObject->GetDib() );
- ::GlobalUnlock( hNewDib );
- m_pImageObject->SetDib( hNewDib );
- return( TRUE );
- }
- int *CImageAreaProcesses::GetHistogram( int nX1, int nY1, int nX2, int nY2 )
- {
- m_pImageObject->NormalizeCoordinates( &nX1, &nY1, &nX2, &nY2 );
- unsigned char *pBuffer, *pBits;
- RGBQUAD *pPalette;
- int nWidthBytes, nNumColors;
- pBuffer = (unsigned char *) m_pImageObject->GetDIBPointer( &nWidthBytes, m_pImageObject->GetNumBits() );
- if( pBuffer == NULL ) return( NULL );
- nNumColors = m_pImageObject->GetNumColors();
- pPalette = (RGBQUAD *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)];
- pBits = (unsigned char *) &pBuffer[sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nNumColors*sizeof(RGBQUAD)];
- int *nHistogramBuffer = CreateHistogram( nX1, nY1, nX2, nY2, pBits, pPalette, nWidthBytes );
- ::GlobalUnlock( m_pImageObject->GetDib() );
- return( nHistogramBuffer );
- }
- int *CImageAreaProcesses::CreateHistogram( int nX1, int nY1, int nX2, int nY2, unsigned char *pData, RGBQUAD *pPalette, int nWidthBytes )
- {
- 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 nWidth = m_pImageObject->GetWidth();
- 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<m_pImageObject->GetHeight(); 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 );
- }