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

📄 image.cpp

📁 医学图象处理系统
💻 CPP
📖 第 1 页 / 共 5 页
字号:

//这里是"image.h"的实现函数定义
#include "stdafx.h"
#include <math.h>
#include <memory.h>
#include <string.h>
#include "image.h"
#include <io.h>

FloatImage::FloatImage()
{
	lp_data   = NULL;
	lp_AddRow = NULL;
	Width     = Height = 0;
}
FloatImage::~FloatImage()
{
	DeleteData();
}
FloatImage::FloatImage(unsigned int w, unsigned int h)
{
	Width  = w;
	Height = h;
	ImageSize = w * h;
	lp_data   = NULL;
	lp_AddRow = NULL;
	Construct(w, h);
}
void FloatImage::Construct(unsigned int w, unsigned int h)
{
	DeleteData();
	if( ImageSize > 0 ) lp_data   = new  float[ImageSize];
	memset(lp_data, 0, ImageSize* sizeof(float) );
	if( Height    > 0 ) lp_AddRow = new float*[Height];
	lp_AddRow[0] = lp_data;
	for( UINT i= 1; i < Height; i++)
		lp_AddRow[i] = lp_AddRow[i- 1] + Width;

}
void FloatImage::DeleteData()
{
	if(lp_data   != NULL) delete []lp_data;
	if(lp_AddRow != NULL) delete []lp_AddRow;
}
//--------------------------CImage class declaration -------------------//
//***********************************************************
//***********************************************************
C2DArray::C2DArray()
{
    m_lpDibArray = NULL;
	RowAddress   = NULL;
}
//***********************************************************

//***********************************************************
C2DArray::~C2DArray()
{
    if(m_lpDibArray != NULL)  ReleaseDibArray();    
}
//***********************************************************

//***********************************************************
void C2DArray::ReleaseDibArray()
{
	if( m_lpDibArray != NULL )
    {
		delete []m_lpDibArray;
        m_lpDibArray = NULL;
    }
	if( RowAddress != NULL)  
	{
		delete []RowAddress;
		RowAddress = NULL;
	}
}
//***********************************************************

//***********************************************************
C2DArray::C2DArray(P_LONG width, P_LONG height)
{
    m_nDibWidth  = width;
    m_nDibHeight = height;
    m_lpDibArray = NULL;
    RowAddress   = NULL;
    
	m_lpDibArray = new P_BYTE[width * height];
	if( m_lpDibArray == NULL) AfxThrowMemoryException();

	RowAddress    = new P_BYTE*[m_nDibHeight];
	RowAddress[0] = m_lpDibArray;
    for(int i=1; i<m_nDibHeight; i++)
	{
		RowAddress[i] = RowAddress[i-1] + m_nDibWidth;
	}
}
//***********************************************************

//***********************************************************
BOOL C2DArray::CheckDataRange(P_LONG x, P_LONG y)
{
	if( (x < 0) || (x >= m_nDibWidth) || (y < 0) || ( y >= m_nDibHeight) )
	{
		if( x < 0 ) x = 0;
		else if( x >= m_nDibWidth )  x = m_nDibWidth-1;

		if(y<0) y = 0;
		else if( y >= m_nDibHeight ) y = m_nDibHeight-1;

		return false;
	}
	else
		return true;
}
BOOL C2DArray::CheckDataRange(CPoint pt)
{
	return CheckDataRange(pt.x, pt.y);
}
//***********************************************************

//***********************************************************
P_BYTE C2DArray::SGetXYValue(P_LONG x, P_LONG y)
{	
#ifdef _DEBUG
	if(!CheckDataRange(x, y)) 
	{
		AfxMessageBox("下标越界!!!");
		AfxAbort();
	}
#endif    
	return RowAddress[y][x];
}
P_BYTE C2DArray::SGetXYValue(CPoint pt)
{	
    return SGetXYValue(pt.x, pt.y);
}
//***********************************************************

//***********************************************************
void  C2DArray::SSetXYValue(P_LONG x, P_LONG y, P_BYTE value)
{ 
#ifdef _DEBUG	
	if(!CheckDataRange(x,y)) 
	{
		AfxMessageBox("下标越界!!!");
		AfxAbort();
	}
#endif
	RowAddress[y][x] = value; 
}
void  C2DArray::SSetXYValue(CPoint pt, P_BYTE value)
{	
	SSetXYValue(pt.x, pt.y, value);
}

BOOL  CImage::CheckPoint(P_LONG &x, P_LONG &y)
{
	if( (x<0)||(x>=m_ImageWidth)||(y<0)||(y>=m_ImageHeight) )
	{
		if( x < 0) x = 0;
		else if( x >= m_ImageWidth )  x = m_ImageWidth-1;

		if( y < 0 ) y = 0;
		else if( y >= m_ImageHeight ) y = m_ImageHeight-1;

		return false;
	}
	else
		return true;
}
BOOL CImage::CheckPoint(CPoint &pt)          
{
	return CheckPoint(pt.x, pt.y);
}
//***********************************************************

//***********************************************************
BOOL CImage::CheckRect(CPoint &p1,CPoint &p2)
{
	if(!CheckPoint(p1) || !CheckPoint(p2)) 
		return false;
	P_LONG tempx, tempy, temp;
    tempx = p2.x - p1.x;
	tempy = p2.y - p1.y;
    
	if( tempx < 0 )
	{
		temp = p1.x; 
		p1.x = p2.x; 
		p2.x = temp;
	}
	if( tempy < 0)
	{
		temp = p1.y; 
		p1.y = p2.y; 
		p2.y = temp;
	}
	if( labs(tempx)<5||labs(tempy)<5) 
		return false;
	else
		return true;
}
BOOL CImage::CheckRect(CRect &ImageRect)
{
	if( ImageRect.left < 0 ) 
	{
		ImageRect.left = 0;
	}
	else if( ImageRect.right > (m_ImageWidth-1) ) 
	{
		ImageRect.right = m_ImageWidth- 1;
	}
	if( ImageRect.top < 0 )
	{
		ImageRect.top = 0;
	}
	else if( ImageRect.bottom > (m_ImageHeight-1) )
	{
		ImageRect.bottom = m_ImageHeight- 1;
	}

	if( ImageRect.Width()<5||ImageRect.Height()<5) 
		return false;
	else
		return true;
}
//***********************************************************

//***********************************************************
void CImage::ConstructInit()
{
 	m_fScale         = 1.0;
	m_bImgModified   = FALSE;    
    m_wColorMapNum   = 0;
    m_lpDibInfo      = NULL;
	m_lpHSIArray     = NULL;
    m_lpDibArrayBuff = NULL; 
	m_hDIB           = NULL;
	m_Bright		 = -1;
	m_Contrast		 = -1;
	m_SorceType		 = 0;
}
// 释放全部的内存
void CImage::DeleteMe()
{
	if(          m_hDIB != NULL) ReleaseCopyHandle();
	if(     m_lpDibInfo != NULL) ReleaseDibPalInfo();
	if(    m_lpDibArray != NULL) ReleaseDibArray();    
	if(    m_lpHSIArray != NULL) ReleaseHueImg();
    if(m_lpDibArrayBuff != NULL) ReleaseBuffer();  
}
//释放m_lpDibInfo内存
void CImage::ReleaseDibPalInfo()
{
	if( m_pUseful != NULL )
	{		
		delete []m_pUseful;
		m_lpDibInfo = NULL;
		m_pUseful   = NULL;
	}
}
//释放m_lpDibArrayBuff内存
void CImage::ReleaseBuffer()
{
	if( m_lpDibArrayBuff != NULL )
	{
		delete []m_lpDibArrayBuff ;
		m_lpDibArrayBuff = NULL;
	}
}
//释放m_lpHSIArray内存
void CImage::ReleaseHueImg()
{
	if( m_lpHSIArray != NULL )
	{
		delete []m_lpHSIArray;
		m_lpHSIArray = NULL;
    }
}
//***********************************************************

//***********************************************************
BOOL CImage::NewBuffer()
{
	if( m_lpDibArrayBuff != NULL ) 
		ReleaseBuffer();
	if( m_ImageSize )
	{
		m_lpDibArrayBuff = new P_BYTE[m_ImageSize];
		if( m_lpDibArrayBuff == NULL )
		{
			AfxThrowMemoryException();  // especial  exception handle 
		    return false; 
		}
		else
			return true;
	}
	else
		return false; 
}
//***********************************************************

//***********************************************************
BOOL CImage::NewHSIImg()
{
	if( m_lpHSIArray != NULL ) 
		ReleaseHueImg();
	if( m_HSIImageWidth*m_HSIImageHeight )
	{
		m_lpHSIArray = new P_BYTE[m_HSIImageWidth*m_HSIImageHeight];
		if( m_lpHSIArray  == NULL )
		{
			AfxThrowMemoryException();  // especial  exception handle 
		    return false; 
		}
		else
			return true;
	}
	else
		return false; 
}
//***********************************************************

//***********************************************************
CImage::CImage()
{
    ConstructInit();
}
//***********************************************************

//***********************************************************
CImage::~CImage()
{    
	if(          m_hDIB != NULL) ReleaseCopyHandle();
	if(     m_lpDibInfo != NULL) ReleaseDibPalInfo();
	if(    m_lpHSIArray != NULL) ReleaseHueImg();
    if(m_lpDibArrayBuff != NULL) ReleaseBuffer();  
}
CImage::CImage(CImage &source)
{
	ConstructInit();

    m_ImageSize      = source.m_ImageSize;
    m_ImageWidth     = source.m_ImageWidth;	
    m_ImageHeight    = source.m_ImageHeight;	
    m_nDibWidth      = source.m_nDibWidth;
    m_nDibHeight     = source.m_nDibHeight;
	m_wImageDepth    = source.m_wImageDepth;
	m_wColorMapNum   = source.m_wColorMapNum;  
    m_HSIImageWidth  = source.m_HSIImageWidth;
	m_HSIImageHeight = source.m_HSIImageHeight;
    
	m_dwRBitMask	 = source.m_dwRBitMask;	
	m_dwGBitMask 	 = source.m_dwGBitMask; 	
	m_dwBBitMask	 = source.m_dwBBitMask;	
	m_wlowRedBit	 = source.m_wlowRedBit;	
	m_wlowGreenBit	 = source.m_wlowGreenBit;	
	m_wlowBlueBit    = source.m_wlowBlueBit;   
	m_wNumRedBits	 = source.m_wNumRedBits;	
	m_wNumGreenBits  = source.m_wNumGreenBits; 
	m_wNumBlueBits	 = source.m_wNumBlueBits;

	AllocateMemory();
    
	if( m_lpDibInfo != NULL ) //create the bitmap info
	{
		m_lpDibInfo->bmiHeader = source.m_lpDibInfo->bmiHeader;
    	if(m_wColorMapNum) //create the color table
		{
			for(int i=0; i<m_wColorMapNum; i++)
			{
				m_lpDibInfo->bmiColors[i] = source.m_lpDibInfo->bmiColors[i];
			}
			BakupColorIndex();
		}
	}
	memcpy(m_lpDibArray, source.m_lpDibArray, m_ImageSize);    
	GenerateRowAddress();
}   
 
CImage::CImage(CImage &source, CPoint &LTp, CPoint &RBp)
{
#ifdef _DEBUG
	if(!source.CheckRect(LTp, RBp))
	{
		AfxMessageBox("下标越界!!!");
		AfxAbort();
	}
#endif  

	ConstructInit();

	m_wImageDepth    = source.m_wImageDepth;
	m_wColorMapNum   = source.m_wColorMapNum; 
	m_HSIImageWidth  = source.m_HSIImageWidth;
	m_HSIImageHeight = source.m_HSIImageHeight;
    
 	P_LONG width     = RBp.x - LTp.x + 1;
    P_LONG height    = RBp.y - LTp.y + 1;

    m_ImageWidth     = width;	
    m_ImageHeight    = height;	
    m_nDibWidth      = (width* m_wImageDepth/ 8 + 3) & ~3;
    m_nDibHeight     = height;
	m_ImageSize      = m_nDibWidth* m_nDibHeight;
	
	AllocateMemory();
    if( m_lpDibInfo != NULL ) //create the bitmap info     
	{
		m_lpDibInfo->bmiHeader          = source.m_lpDibInfo->bmiHeader;
		m_lpDibInfo->bmiHeader.biWidth  = width;
		m_lpDibInfo->bmiHeader.biHeight = source.m_IsImageDownUp ? m_nDibHeight : -m_nDibHeight;;
		if( m_wColorMapNum ) 
		{
			for(int i= 0; i< m_wColorMapNum; i++)
			{
				m_lpDibInfo->bmiColors[i] = source.m_lpDibInfo->bmiColors[i];
			}
			BakupColorIndex();
		}
	}
	GenerateRowAddress();

	BYTE *bpSourceAdd;
	int   OffSet      = LTp.x * m_wImageDepth/ 8;
	int   DataLength  = width * m_wImageDepth/ 8;
    for( P_LONG y = 0;  y < height; y++)
	{
		bpSourceAdd = source.RowAddress[LTp.y + y] + OffSet;
		memcpy( RowAddress[y], bpSourceAdd, DataLength );		
	}	
}

CImage::CImage(P_LONG width, P_LONG height, int PixelDepth, DWORD colornum)
{
	ConstructInit();    
	CreateEmpty(width, height, PixelDepth, colornum);       
}
    
//***********************************************************

//***********************************************************
BOOL CImage::CreateEmpty(P_LONG width, P_LONG height, int PixelDepth, DWORD colornum)
{
	DeleteMe();
	ConstructInit();    

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

	if( m_wImageDepth == 16 || m_wImageDepth == 32 )
		AllocateMemory(true);
	else 
		AllocateMemory();
    if(m_lpDibInfo!=NULL && m_lpDibArray!=NULL) //create the bitmap info     
	{  
		m_lpDibInfo->bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
		m_lpDibInfo->bmiHeader.biWidth         = width;
		m_lpDibInfo->bmiHeader.biHeight        = height;
		m_lpDibInfo->bmiHeader.biPlanes        = 1;
		m_lpDibInfo->bmiHeader.biBitCount      = m_wImageDepth;
		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;

		GenerateRowAddress();
	
		if( m_wImageDepth <= 8 ) //create the color table

⌨️ 快捷键说明

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