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

📄 geometrytrans.cpp

📁 将数字图像处理的一般算法都集中在一个MFC的框架中
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "stdafx.h"
#include "GeometryTrans.h"
#include "math.h"

#define max(a,b) (a>b?a:b)
/***********************************************************************
* 函数名称:
* GeometryTrans()
*
*说明:无参数的构造函数,对成员变量进行初始化
***********************************************************************/
GeometryTrans::GeometryTrans()
{

	m_pImgDataOut=NULL;//输出图像位图数据指针为空

	m_lpColorTableOut=NULL;//输出图像颜色表指针为空
	
	m_nColorTableLengthOut=0;//输出图像颜色表长度为0

    m_nBitCountOut=0;//输出图像每像素位数为0	

	m_imgWidthOut=0;//输出图像的宽为0

	m_imgHeightOut=0;//输出图像的高为0	
}

/***********************************************************************
* 函数名称:
* GeometryTrans()
*
*函数参数:
*  CSize size -图像大小(宽、高)
*  int nBitCount  -每像素所占位数
*  LPRGBQUAD lpColorTable  -颜色表指针
*  unsigned char *pImgData  -位图数据指针
*
*返回值:
*   无
*
*说明:本函数为带参数的构造函数,给定位图的大小、每像素位数、颜色表
*      及位图数据,调用ImgCenterDib()对基类成员初始化,并初始化派生类的
*      数据成员
***********************************************************************/
GeometryTrans::GeometryTrans(CSize size, int nBitCount, LPRGBQUAD lpColorTable,
							 unsigned char *pImgData):
ImgCenterDib(size, nBitCount, lpColorTable, pImgData)
{	
	//输出图像每像素位数与输入图像相同
    m_nBitCountOut=m_nBitCount;

	//输出图像颜色表长度
	m_nColorTableLengthOut=ComputeColorTabalLength(m_nBitCountOut);

	//输出图像颜色表与输入图像相同
	if(m_nColorTableLengthOut){
		//分配颜色表缓冲区,进行颜色表拷贝
		m_lpColorTableOut=new RGBQUAD[m_nColorTableLengthOut];
		memcpy(m_lpColorTableOut,m_lpColorTable, sizeof(RGBQUAD)*m_nColorTableLengthOut);
	}
	else// 彩色图像没有颜色表
		 m_lpColorTableOut=NULL;

	//输出图像指针为空
	m_pImgDataOut=NULL;

	//输出图像宽和高置0
	m_imgWidthOut=0;
	m_imgHeightOut=0;
}

/***********************************************************************
* 函数名称:
*   ~GeometryTrans()

*
*说明:析构函数,释放资源
***********************************************************************/
GeometryTrans::~GeometryTrans()
{
	//释放输出图像位图数据缓冲区
	if(m_pImgDataOut!=NULL){
		delete []m_pImgDataOut;
    	m_pImgDataOut=NULL;
	}

	//释放输出图像颜色表
	if(m_lpColorTableOut!=NULL){
		delete []m_lpColorTableOut;
		m_lpColorTableOut=NULL;
	}
}


/***********************************************************************
* 函数名称:
* GetDimensions()
*
*函数参数:
*  无
*
*返回值:
*   图像的尺寸,用CSize类型表达
*
*说明:返回输出图像的宽和高
***********************************************************************/
CSize GeometryTrans::GetDimensions()
{
	return CSize(m_imgWidthOut, m_imgHeightOut);
}

/***********************************************************************
* 函数名称:
* Move()
*
*函数参数:
*  int offsetX  -水平方向平移量,像素为单位
*  int offsetY  -垂直方向平移量,像素为单位
*
*返回值:
*   无
*
*说明:给定水平和垂直方向的位移量,对图像进行平移,结果输出至m_pImgDataOut
*      缓冲区中
***********************************************************************/
void GeometryTrans::Move(int offsetX, int offsetY)
{
	//释放旧的输出图像缓冲区
	if(m_pImgDataOut!=NULL){
		delete []m_pImgDataOut;
    	m_pImgDataOut=NULL;
	}

	//输出图像的宽和高
	m_imgWidthOut=m_imgWidth;
	m_imgHeightOut=m_imgHeight;

	//每行像素字节数,输出图像与输入图像相等
	int lineByte=(m_imgWidth*m_nBitCount/8+3)/4*4;

	//申请缓冲区,存放输出结果
	m_pImgDataOut=new unsigned char[lineByte*m_imgHeight];

	//置黑色
	memset(m_pImgDataOut,0,lineByte*m_imgHeight);

	//循环变量,图像坐标
	int i,j;

	//循环变量,像素的每个通道
	int k;

	//每像素字节数,输出图像与输入图像相等
	int pixelByte=m_nBitCountOut/8;

	//平移运算
	for(i=0;i<m_imgHeight;i++){
		for(j=0;j<m_imgWidth;j++){
			//输出的点在输入图像范围内
			if(i-offsetY>=0&&i-offsetY<m_imgHeight&&j-offsetX>=0&&j-offsetX<m_imgWidth)
			{
				for(k=0;k<pixelByte;k++)
					*(m_pImgDataOut+i*lineByte+j*pixelByte+k)
					=*(m_pImgData+(i-offsetY)*lineByte+(j-offsetX)*pixelByte+k);
			}
		}
	}
}

/***********************************************************************
* 函数名称:
* ZhuanZhi()
*
*说明:对图像转置,结果输出至m_pImgDataOut缓冲区中   
***********************************************************************/
void GeometryTrans::ZhuanZhi()
{
	//释放旧的输出图像缓冲区
	if(m_pImgDataOut!=NULL){
		delete []m_pImgDataOut;
    	m_pImgDataOut=NULL;
	}

	//输出图像的宽和高
	m_imgWidthOut=m_imgHeight;
	m_imgHeightOut=m_imgWidth;

	//输入图像每行像素字节数
	int lineByteOut=(m_imgWidthOut*m_nBitCount/8+3)/4*4;

	//申请缓冲区,存放输出结果
	m_pImgDataOut=new unsigned char[lineByteOut*m_imgHeightOut];

	//输入图像每行像素字节数
	int lineByteIn=(m_imgWidth*m_nBitCount/8+3)/4*4;

	//每像素字节数,输出图像与输入图像相等
	int pixelByte=m_nBitCountOut/8;

	//循环变量,图像坐标
	int i,j;

	//循环变量,像素的每个通道
	int k;

	//图像转置
	for(i=0;i<m_imgHeightOut;i++){
		for(j=0;j<m_imgWidthOut;j++){
			for(k=0;k<pixelByte;k++)
				*(m_pImgDataOut+i*lineByteOut+j*pixelByte+k)
				=*(m_pImgData+j*lineByteIn+i*pixelByte+k);
			
		}
	}
}


/***********************************************************************
* 函数名称:
* MirrorHori()
*
*说明:对图像水平镜像
***********************************************************************/
void GeometryTrans::MirrorHori()
{
    //释放旧的输出图像缓冲区
	if(m_pImgDataOut!=NULL){
		delete []m_pImgDataOut;
    	m_pImgDataOut=NULL;
	}

	//输出图像的宽和高
	m_imgWidthOut=m_imgWidth;
	m_imgHeightOut=m_imgHeight;

	//每行像素字节数,输出图像与输入图像相等
	int lineByte=(m_imgWidth*m_nBitCount/8+3)/4*4;

	//申请缓冲区,存放输出结果
	m_pImgDataOut=new unsigned char[lineByte*m_imgHeight];

	//循环变量,图像坐标
	int i,j;

	//循环变量,像素的每个通道
	int k;

	//每像素字节数,输出图像与输入图像相等
	int pixelByte=m_nBitCountOut/8;

	//水平镜像
	for(i=0;i<m_imgHeight;i++){
		for(j=0;j<m_imgWidth;j++){
			for(k=0;k<pixelByte;k++)
				*(m_pImgDataOut+i*lineByte+j*pixelByte+k)
				=*(m_pImgData+i*lineByte+(m_imgWidth-1-j)*pixelByte+k);
		}
	}
}

/***********************************************************************
* 函数名称:
* MirrorVerti()
*
*
*说明:对图像垂直镜像
***********************************************************************/
void GeometryTrans::MirrorVerti()
{
	//释放旧的输出图像缓冲区
	if(m_pImgDataOut!=NULL){
		delete []m_pImgDataOut;
    	m_pImgDataOut=NULL;
	}

	//输出图像的宽和高
	m_imgWidthOut=m_imgWidth;
	m_imgHeightOut=m_imgHeight;

	//每行像素字节数,输出图像与输入图像相等
	int lineByte=(m_imgWidth*m_nBitCount/8+3)/4*4;

	//申请缓冲区,存放输出结果
	m_pImgDataOut=new unsigned char[lineByte*m_imgHeight];

	//循环变量,图像坐标
	int i,j;

	//循环变量,像素的每个通道
	int k;

	//每像素字节数,输出图像与输入图像相等
	int pixelByte=m_nBitCountOut/8;

	//垂直镜像
	for(i=0;i<m_imgHeight;i++){
		for(j=0;j<m_imgWidth;j++){
			for(k=0;k<pixelByte;k++)
				*(m_pImgDataOut+i*lineByte+j*pixelByte+k)
				=*(m_pImgData+(m_imgHeight-1-i)*lineByte+j*pixelByte+k);
		}
	}
}

/***********************************************************************
* 函数名称:
* Clockwise90()
*
*
*说明:对图像顺时针旋转90度
***********************************************************************/
void GeometryTrans::Clockwise90()
{
	//释放旧的输出图像缓冲区
	if(m_pImgDataOut!=NULL){
		delete []m_pImgDataOut;
    	m_pImgDataOut=NULL;
	}

	//输入图像每行像素字节数
	int lineByte=(m_imgWidth*m_nBitCount/8+3)/4*4;

	//输出图像的宽和高
	m_imgWidthOut=m_imgHeight;
	m_imgHeightOut=m_imgWidth;

	//输出图像每行像素字节数
	int lineByteOut=(m_imgWidthOut*m_nBitCount/8+3)/4*4;

	//申请缓冲区,存放输出结果
	m_pImgDataOut=new unsigned char[lineByteOut*m_imgHeightOut];

	//循环变量,图像坐标
	int i,j;

	//循环变量,像素的每个通道
	int k;

	//每像素字节数,输出图像与输入图像相等
	int pixelByte=m_nBitCountOut/8;

	//顺时针90度
	for(i=0;i<m_imgHeightOut;i++){
		for(j=0;j<m_imgWidthOut;j++){
			for(k=0;k<pixelByte;k++)
				*(m_pImgDataOut+i*lineByteOut+j*pixelByte+k)
				=*(m_pImgData+j*lineByte+(m_imgWidth-1-i)*pixelByte+k);
		}
	}
}


/***********************************************************************
* 函数名称:
* Anticlockwise90()
*
*
*说明:对图像逆时针旋转90度
***********************************************************************/
void GeometryTrans::Anticlockwise90()
{
	//释放旧的输出图像缓冲区
	if(m_pImgDataOut!=NULL){
		delete []m_pImgDataOut;
    	m_pImgDataOut=NULL;
	}

	//输入图像每行像素字节数
	int lineByte=(m_imgWidth*m_nBitCount/8+3)/4*4;

	//输出图像的宽和高
	m_imgWidthOut=m_imgHeight;
	m_imgHeightOut=m_imgWidth;

	//输出图像每行像素字节数
	int lineByteOut=(m_imgWidthOut*m_nBitCount/8+3)/4*4;

	//申请缓冲区,存放输出结果
	m_pImgDataOut=new unsigned char[lineByteOut*m_imgHeightOut];

	//循环变量,图像坐标
	int i,j;

	//循环变量,像素的每个通道
	int k;

	//每像素字节数,输出图像与输入图像相等
	int pixelByte=m_nBitCountOut/8;

	//逆时针90度
	for(i=0;i<m_imgHeightOut;i++){
		for(j=0;j<m_imgWidthOut;j++){
			for(k=0;k<pixelByte;k++)
				*(m_pImgDataOut+i*lineByteOut+j*pixelByte+k)
				=*(m_pImgData+(m_imgHeight-1-j)*lineByte+i*pixelByte+k);
		}
	}
}

/***********************************************************************
* 函数名称:
* Rotate180()
*
*
*说明:对图像旋转180度
***********************************************************************/
void GeometryTrans::Rotate180()
{
	//释放旧的输出图像缓冲区
	if(m_pImgDataOut!=NULL){
		delete []m_pImgDataOut;
    	m_pImgDataOut=NULL;
	}

	//输出图像的宽和高
	m_imgWidthOut=m_imgWidth;
	m_imgHeightOut=m_imgHeight;

	//每行像素字节数,输出图像与输入图像相等
	int lineByte=(m_imgWidth*m_nBitCount/8+3)/4*4;

	//申请缓冲区,存放输出结果
	m_pImgDataOut=new unsigned char[lineByte*m_imgHeight];

	//循环变量,图像坐标
	int i,j;

	//循环变量,像素的每个通道
	int k;

	//每像素字节数,输出图像与输入图像相等
	int pixelByte=m_nBitCountOut/8;

	//旋转180度
	for(i=0;i<m_imgHeightOut;i++){
		for(j=0;j<m_imgWidthOut;j++){
			for(k=0;k<pixelByte;k++)
				*(m_pImgDataOut+i*lineByte+j*pixelByte+k)
				=*(m_pImgData+(m_imgHeight-1-i)*lineByte+(m_imgWidth-1-j)*pixelByte+k);
		}
	}
}

/*************************************************************************
 *
 * 函数名称:

⌨️ 快捷键说明

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