⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.cpp

📁 医学图象处理系统
💻 CPP
📖 第 1 页 / 共 5 页
字号:
        case 16: return HighColorImage;
        case 24: return TrueColorImage;
		case 32: return UltraColorImage;
		default: return IllegalImage;
    }   
}
//***********************************************************

//***********************************************************
void  CImage::InitTrueColorInfo()
{
	if( m_lpDibInfo != NULL )
	{
		m_lpDibInfo->bmiHeader.biSize			= sizeof(BITMAPINFOHEADER);
		m_lpDibInfo->bmiHeader.biWidth			= m_ImageWidth;
		m_lpDibInfo->bmiHeader.biHeight			= m_ImageHeight;
		m_lpDibInfo->bmiHeader.biPlanes			= 1;
		m_lpDibInfo->bmiHeader.biBitCount       = 24;
		m_lpDibInfo->bmiHeader.biCompression    = BI_RGB;
		m_lpDibInfo->bmiHeader.biSizeImage		= m_ImageSize;
		m_lpDibInfo->bmiHeader.biXPelsPerMeter  = 0;
		m_lpDibInfo->bmiHeader.biYPelsPerMeter  = 0;
		m_lpDibInfo->bmiHeader.biClrUsed		= m_wColorMapNum;
		m_lpDibInfo->bmiHeader.biClrImportant   = 0;
	}
} 
//***********************************************************

//***********************************************************
void  CImage::InitGrayImageInfo()
{
	if( m_lpDibInfo != NULL )
	{  
		m_lpDibInfo->bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
		m_lpDibInfo->bmiHeader.biWidth         = m_ImageWidth;
		m_lpDibInfo->bmiHeader.biHeight        = m_ImageHeight;
		m_lpDibInfo->bmiHeader.biPlanes        = 1;
		m_lpDibInfo->bmiHeader.biBitCount      = 8;
		m_lpDibInfo->bmiHeader.biCompression   = BI_RGB;
		m_lpDibInfo->bmiHeader.biSizeImage     = m_ImageSize;
		m_lpDibInfo->bmiHeader.biXPelsPerMeter = 0;
		m_lpDibInfo->bmiHeader.biYPelsPerMeter = 0;
		m_lpDibInfo->bmiHeader.biClrUsed       = m_wColorMapNum;
		m_lpDibInfo->bmiHeader.biClrImportant  = 0;
	
		if( m_wImageDepth == 8)// create the color table
		{
			for(int i= 0;i <m_wColorMapNum; i++)
			{
				m_lpDibInfo->bmiColors[i].rgbBlue     = (P_BYTE)i;
				m_lpDibInfo->bmiColors[i].rgbGreen    = (P_BYTE)i;
				m_lpDibInfo->bmiColors[i].rgbRed      = (P_BYTE)i;
				m_lpDibInfo->bmiColors[i].rgbReserved = 0;
			} 
			BakupColorIndex();
		}        
	}
}
//***********************************************************
 
//***********************************************************
BOOL  CImage::BakupColorIndex()
{
	if( m_wImageDepth <= 8 )
	{
		for(int i= 0; i< m_wColorMapNum; i++)
		{
			ColorIndex[i].rgbtBlue  = m_lpDibInfo->bmiColors[i].rgbBlue;
			ColorIndex[i].rgbtGreen = m_lpDibInfo->bmiColors[i].rgbGreen;
			ColorIndex[i].rgbtRed   = m_lpDibInfo->bmiColors[i].rgbRed;	
		}
		return true;			
	}
	return false;
}
void CImage::GenerateRowAddress()
{
	if( RowAddress != NULL ) 
	{
		delete []RowAddress;
		RowAddress = NULL;
	}
	RowAddress = new P_BYTE*[m_ImageHeight];

	m_IsImageDownUp = m_lpDibInfo->bmiHeader.biHeight > 0;

	if( m_IsImageDownUp )
	{
		RowAddress[m_nDibHeight-1] = m_lpDibArray;
		for(int i= m_nDibHeight- 2; i>=0 ; i--)
		{
			RowAddress[i] = RowAddress[i+1] + m_nDibWidth;
		}
	}
	else
	{
		RowAddress[0] = m_lpDibArray;
		for(int i= 1; i< m_nDibHeight; i++)
		{
			RowAddress[i] = RowAddress[i-1] + m_nDibWidth;
		}
	}
}
//***********************************************************

//***********************************************************
void CImage::AllocateMemory(BOOL IsExistRGBMask)
{
	DeleteMe();
	m_lpDibArray = new P_BYTE[m_ImageSize];
	memset(m_lpDibArray, 0, m_ImageSize);
	if(m_lpDibArray == NULL)  AfxThrowMemoryException();
	
	//为m_hDibInfo图像信息头分配内存
	DWORD DibInfoSize;
	if(!IsExistRGBMask)
	{		
		DibInfoSize = m_wColorMapNum*sizeof(RGBQUAD) + sizeof(BITMAPINFOHEADER);
	}
	else 
	{
		DibInfoSize = 3* sizeof(DWORD) + sizeof(BITMAPINFOHEADER);
	}
	
	m_pUseful = new P_BYTE[DibInfoSize];
	if ( m_pUseful == NULL)
	{
		ReleaseDibArray();
		AfxThrowMemoryException();
	}
	m_lpDibInfo = (LPBITMAPINFO)m_pUseful;	
}
//***********************************************************

//***********************************************************
void   CImage::ReleaseCopyHandle()
{
	if( m_hDIB != NULL )
    {
		GlobalUnlock( m_hDIB );
        GlobalFree( m_hDIB );
        m_hDIB = NULL;
    }
}
//***********************************************************

//***********************************************************
HANDLE CImage::GetCopyHandle(CPoint LTp, CPoint RBp)
{
#ifdef _DEBUG
	if(!CheckRect(LTp,RBp))
	{
		AfxMessageBox("下标越界!!!");
		AfxAbort();
	}
#endif
	P_LONG  width   = (RBp.x - LTp.x+1);
    P_LONG  height  = (RBp.y - LTp.y+1);
   
	width = (width* m_wImageDepth/ 8 + 3) & ~3;
	
	P_LONG  dwBitsSize = width * height;
	P_LONG  DibInfoSize;

	DibInfoSize = sizeof(BITMAPINFOHEADER) + m_wColorMapNum*sizeof(RGBQUAD);
    dwBitsSize += (DibInfoSize + sizeof(BITMAPFILEHEADER));
	m_hDIB      = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwBitsSize);
    
	LPSTR  lpDIBHdr          = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
    LPSTR  lpDIBBits         = lpDIBHdr + DibInfoSize;
    LPBITMAPINFO CopyDibInfo = (LPBITMAPINFO)lpDIBHdr;
    CopyDibInfo->bmiHeader   = m_lpDibInfo->bmiHeader;
    
	CopyDibInfo->bmiHeader.biWidth       = (RBp.x - LTp.x + 1);
    CopyDibInfo->bmiHeader.biHeight      = height;
	CopyDibInfo->bmiHeader.biSizeImage   = width * height;
	if( m_wImageDepth <= 8)
	{
		for(int i=0 ;i< m_wColorMapNum; i++)
		{
			CopyDibInfo->bmiColors[i] = m_lpDibInfo->bmiColors[i];
		} 
	} 
	int   OffsetX  = LTp.x* m_wImageDepth/ 8;
	LPSTR lpTarget = lpDIBBits + (height-1) * width;
	for( P_LONG j= 0; j< height; j++, lpTarget -= width)
	{		
		memcpy( lpTarget,  RowAddress[LTp.y+ j] + OffsetX,  width);
	}
	return m_hDIB;    
}
//***********************************************************

//***********************************************************
void  CImage::Inverse()
{	
	eImageType dImageType = GetImageType();
	if(dImageType == IndexColorImage)
	{
		for(int gray=0;gray < m_wColorMapNum;gray++)
		{
			m_lpDibInfo->bmiColors[gray].rgbBlue  = ~m_lpDibInfo->bmiColors[gray].rgbBlue ;
			m_lpDibInfo->bmiColors[gray].rgbGreen = ~m_lpDibInfo->bmiColors[gray].rgbGreen;
			m_lpDibInfo->bmiColors[gray].rgbRed   = ~m_lpDibInfo->bmiColors[gray].rgbRed  ;
		}                           
	}
	else
	{
		DWORD *lpTemp = (DWORD *)m_lpDibArray;
		P_LONG tempL  = m_ImageSize / 4;
		for(P_LONG i= 0; i< tempL; i++)
			*lpTemp = ~*lpTemp++;
	}
}
//***********************************************************

//***********************************************************
void  CImage::VerticalFlip()
{
	P_BYTE *temp = new P_BYTE[m_nDibWidth]; // Inverse the IMG format image
    int  dim = m_ImageHeight / 2;
	for(int i = 0;i < dim ;i++) 
	{
		memcpy( temp,          RowAddress[i],                m_nDibWidth);
        memcpy( RowAddress[i], RowAddress[m_nDibHeight-1-i], m_nDibWidth);
        memcpy( RowAddress[m_nDibHeight-1-i] ,      temp,    m_nDibWidth);
    }
	if(temp != NULL) delete []temp;
}
//***********************************************************

//***********************************************************
void  CImage::HorizontalFlip()
{
	if( m_wImageDepth < 8) return;
	int  dim = m_ImageWidth / 2;
	int  Last = (m_ImageWidth - 1)* m_wImageDepth/ 8;
	switch( m_wImageDepth )
	{
		case  8:
		{
			P_BYTE *fp, *bp, swap;
	        for(int j= 0; j< m_ImageHeight; j++)
			{	
				fp = RowAddress[j];
				bp = RowAddress[j] + Last;
		        for(int i = 0;i < dim ;i++) 
				{
					swap  = *fp;
					*fp++ = *bp;
					*bp-- =  swap;
				}
			}
			break;
		}
		case  16:
		{
			WORD *fp, *bp, swap;
	        for(int j=0; j<m_ImageHeight; j++)
			{	
				fp = (WORD*)RowAddress[j];
				bp = (WORD*)(RowAddress[j] + Last);
			    for(int i = 0;i < dim ;i++) 
				{
					swap  = *fp;
					*fp++ = *bp;
					*bp-- =  swap;
				}
			}
			break;
		}
		case  24:
		{
			RGBTRIPLE *fp, *bp, swap;
	        for(int j=0; j<m_ImageHeight; j++)
			{	
				fp = (RGBTRIPLE*)RowAddress[j];
				bp = (RGBTRIPLE*)(RowAddress[j] + Last);
		        for(int i = 0;i < dim ;i++) 
				{
					swap  = *fp;
					*fp++ = *bp;
					*bp-- =  swap;
				}
			}
			break;
		}
		case  32:
		{
			DWORD *fp, *bp, swap;
	        for(int j=0; j<m_ImageHeight; j++)
			{	
				fp = (DWORD*)RowAddress[j];
				bp = (DWORD*)(RowAddress[j] + Last);
		        for(int i = 0;i < dim ;i++) 
				{
					swap  = *fp;
					*fp++ = *bp;
					*bp-- =  swap;
				}
			}
			break;
		}
	}
}
//***********************************************************

//***********************************************************
void CImage::Rotate90(BOOL Clockwise)
{
	if( m_wImageDepth < 8 ) return;
	BackUp();
	if(    m_lpDibArray != NULL) ReleaseDibArray();
	if(    m_lpHSIArray != NULL) ReleaseHueImg();    

    P_LONG    m_OldImageWidth  = m_ImageWidth;
	P_LONG    m_OldImageHeight = m_ImageHeight;
	P_LONG    m_OldnDibWidth   = m_nDibWidth;

	m_ImageWidth     = m_OldImageHeight;	
    m_ImageHeight    = m_OldImageWidth;	

    m_nDibWidth      = (m_ImageWidth*m_wImageDepth/8+3) & ~3;
    m_nDibHeight     = m_ImageHeight;
	m_ImageSize      = m_nDibWidth * m_nDibHeight;
    m_wColorMapNum   = GetColorMapNum(m_wImageDepth);
    m_HSIImageWidth  = (m_ImageWidth+3) & ~3;
	m_HSIImageHeight = m_ImageHeight;

	m_lpDibInfo->bmiHeader.biWidth	   = m_ImageWidth;
	m_lpDibInfo->bmiHeader.biHeight	   = m_IsImageDownUp ? m_ImageHeight : -m_ImageHeight;
	m_lpDibInfo->bmiHeader.biSizeImage = m_ImageSize;

    m_lpDibArray = new P_BYTE[m_ImageSize];
	if(m_lpDibArray == NULL) AfxThrowMemoryException();
	BYTE **lpBRowAddress = new BYTE*[m_OldImageHeight];
	long i, j;
	if( m_IsImageDownUp )
	{
		lpBRowAddress[m_OldImageHeight- 1] = m_lpDibArrayBuff;
		for(i= m_OldImageHeight- 2; i>=0 ; i--)
		{
			lpBRowAddress[i] = lpBRowAddress[i+1] + m_OldnDibWidth;
		}
	}
	else
	{
		lpBRowAddress[0] = m_lpDibArrayBuff;
		for(i= 1; i< m_OldImageHeight; i++)
		{
			lpBRowAddress[i] = lpBRowAddress[i-1] + m_OldnDibWidth;
		}
	}
    GenerateRowAddress();
   
	DWORD start = GetTickCount();
	if( Clockwise )
	{
		switch(m_wImageDepth)
		{
			case  8:
			{
				for(j= 0; j< m_OldImageHeight; j++)
				{
					for(i= 0; i< m_OldImageWidth; i++)
						RowAddress[i][m_OldImageHeight- j- 1] = lpBRowAddress[j][i];
				}
				break;
			}
			case  16:
			{
				WORD *BRowAddress, *fp;
				for(P_LONG j= 0; j< m_OldImageHeight; j++)
				{
					BRowAddress = (WORD*)(lpBRowAddress[j]);
					for(P_LONG i= 0; i< m_OldImageWidth; i++)
					{
						fp = (WORD*)RowAddress[i];
						fp[m_OldImageHeight- j- 1] = BRowAddress[i];
					}
				}
				break;
			}
			case  24:
			{  
				RGBTRIPLE *BRowAddress, *fp;
				for(P_LONG j= 0; j< m_OldImageHeight; j++)
				{
					BRowAddress = (RGBTRIPLE*)(lpBRowAddress[j]);
					for(P_LONG i= 0; i< m_OldImageWidth; i++)
					{
						fp = (RGBTRIPLE*)RowAddress[i];
						fp[m_OldImageHeight- j- 1] = BRowAddress[i];
					}
				}
				break;
			}
			case  32:
			{
				DWORD *BRowAddress, *fp;
				for(P_LONG j= 0; j< m_OldImageHeight; j++)
				{
					BRowAddress = (DWORD*)(lpBRowAddress[j]);
					for(P_LONG i= 0; i< m_OldImageWidth; i++)
					{
						fp = (DWORD*)RowAddress[i];
						fp[m_OldImageHeight- j- 1] = BRowAddress[i];
					}
				}
				break;
			}
		}
	}
	else
	{
		switch(m_wImageDepth)
		{
			case  8:
			{
				for(j= 0; j< m_OldImageHeight; j++)
				{
					for(i= 0; i< m_OldImageWidth; i++)
						RowAddress[m_ImageHeight- i- 1][j]    = lpBRowAddress[j][i];
				}
				break;
			}
			case  16:
			{
				WORD *BRowAddress, *fp;
				for(P_LONG j= 0; j< m_OldImageHeight; j++)
				{
					BRowAddress = (WORD*)(lpBRowAddress[j]);
					for(P_LONG i= 0; i< m_OldImageWidth; i++)
					{
						fp = (WORD*)RowAddress[m_ImageHeight- i- 1];
						fp[j] = BRowAddress[i];
					}
				}
				break;
			}
			case  24:
			{  
				RGBTRIPLE *BRowAddress, *fp;
				for(P_LONG j= 0; j< m_OldImageHeight; j++)
				{
					BRowAddress = (RGBTRIPLE*)(lpBRowAddress[j]);
					for(P_LONG i= 0; i< m_OldImageWidth; i++)
					{
						fp = (RGBTRIPLE*)RowAddress[m_ImageHeight- i- 1];
						fp[j] = BRowAddress[i];
					}
				}
				break;
			}
			case  32:
			{
				DWORD *BRowAddress, *fp;
				for(P_LONG j= 0; j< m_OldImageHeight; j++)
				{
					BRowAddress = (DWORD*)(lpBRowAddress[j]);
					for(P_LONG i= 0; i< m_OldImageWidth; i++)
					{
						fp = (DWORD*)RowAddress[m_ImageHeight- i- 1];
						fp[j] = BRowAddress[i];
					}
				}
				break;
			}
		}
	}
	DWORD use = GetTickCount() - start;
	TRACE("use = %d\n ", use);
    if(m_lpDibArrayBuff != NULL) ReleaseBuffer();
	if(lpBRowAddress != NULL) delete []lp

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -