dibapi.cpp

来自「数字图像处理的灰度处理源代码.rar」· C++ 代码 · 共 1,472 行 · 第 1/3 页

CPP
1,472
字号
	int i;
	int j;

	LONG lLineBytes;

	LONG fTemp;
	LONG fBiex=20;

	lLineBytes=WIDTHBYTES(lWidth*8);

	for(i=0;i<lHeight-1;i++)
	{
		for(j=0;j<lWidth-1;j++)
		{
			lpSrc1=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i+1))+j;
			lpSrc2=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j+1;
			lpSrc=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j;

			fTemp=(LONG)sqrt((*lpSrc-*lpSrc1)*(*lpSrc-*lpSrc1)+(*lpSrc-*lpSrc2)*(*lpSrc-*lpSrc2));

			if(fTemp>255)
			{
				*lpSrc=255;
			}
			else if(fTemp<0)
			{
				*lpSrc=0;
			}
			else
			{
				*lpSrc=(unsigned char)(fTemp+0.5);
			}
		}
	}

	return TRUE;
}

	
 BOOL WINAPI Move(LPSTR lpDIBBits,LONG lWidth,LONG lHeight,LONG DX,LONG DY)
 {
	int i;                 //行循环变量
	int j;                 //列循环变量
	LPSTR	lpSrcDIBBits;	//指向源像素的指针
	LPSTR	lpDstDIBBits;	//指向临时图象对应像素的指针
	LPSTR	lpDstStartBits;	//指向临时图象对应像素的指针 		
	HLOCAL	hDstDIBBits;	//临时图象句柄
	LONG lLineBytes;

    lLineBytes=WIDTHBYTES(lWidth*8);
	hDstDIBBits= LocalAlloc(LHND, lWidth * lLineBytes);// 分配临时内存
	lpDstStartBits= (char * )LocalLock(hDstDIBBits);// 锁定内存	
	if (hDstDIBBits== NULL)// 判断是否内存分配		
		return FALSE;// 分配内存失败				
	for(i = 0; i < lHeight; i++)// 行
	{
			for(j = 0; j < lWidth; j++)	// 列
		{
				lpDstDIBBits=(char*)lpDstStartBits+lLineBytes*(lHeight-1-i)
				+j;// 指向新DIB第i行,第j个像素的指针												
			if( (j-DY>= 0) && (j-DY< lWidth) && // 像素在源DIB中的坐标j-DX
				(i-DX>= 0) && (i-DX < lHeight))// 判断是否在源图范围内
			{
				lpSrcDIBBits=(char *)lpDIBBits+lLineBytes*(lHeight-1-
					(i-DX))+(j-DY);// 指向源DIB第i0行,第j0个像素的指针								
				*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
			}
			else
			{				
				* ((unsigned char*)lpDstDIBBits) = 0;// 源图中没有的像素,赋为255
			}			
		}
	}
	memcpy(lpDIBBits, lpDstStartBits, lLineBytes * lHeight);// 复制图象		
	LocalUnlock(hDstDIBBits);// 释放内存
	LocalFree(hDstDIBBits);	
	 return TRUE;
 }


 BOOL WINAPI Horizontal(LPSTR lpDIBBits,LONG lWidth,LONG lHeight)
 {
/*   
	 int i;              //行循环变量
	int j;                 //列循环变量
	LPSTR	lpSrcDIBBits;	//指向源像素的指针
	LPSTR	lpDstDIBBits;	//指向临时图象对应像素的指针
	LPSTR	lpDstStartBits;	//指向临时图象对应像素的指针 		
	HLOCAL	hDstDIBBits;	//临时图象句柄
	LONG lLineBytes;

    lLineBytes=WIDTHBYTES(lWidth*8);
	hDstDIBBits= LocalAlloc(LHND, lWidth * lLineBytes);// 分配临时内存
	lpDstStartBits= (char * )LocalLock(hDstDIBBits);// 锁定内存	
	if (hDstDIBBits== NULL)// 判断是否内存分配		
		return FALSE;// 分配内存失败				
	for(i = 0; i < lHeight; i++)// 行
	{
			for(j = 0; j < lWidth; j++)	// 列
		{
				lpDstDIBBits=(char*)lpDstStartBits+lLineBytes*(lHeight-1-i)+j;// 指向新DIB第i行,第j个像素的指针												
	
				lpSrcDIBBits=(char *)lpDIBBits+lLineBytes*(lHeight-i)-j;// 指向源DIB第i0行,第j0个像素的指针								
				*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
		}
	}
	memcpy(lpDIBBits, lpDstStartBits, lLineBytes * lHeight);// 复制图象		
	LocalUnlock(hDstDIBBits);// 释放内存
	LocalFree(hDstDIBBits);	
	return TRUE;*/
	 //水平镜像
	unsigned char *lpSrc;
    unsigned char *lpDst;
	
	LONG i;
	LONG j;

	LONG lLineBytes;
    unsigned char fTemp;


	lLineBytes=WIDTHBYTES(lWidth*8);

	for(i=0;i<lHeight;i++)
	{
		for(j=0;j<lWidth/2;j++)
		{
			lpSrc=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j;
			lpDst=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+lLineBytes-j;
            fTemp=*lpDst;
			*lpDst=*lpSrc;
			*lpSrc=fTemp;
}
	}

	return TRUE;
 }


 BOOL WINAPI Vertical(LPSTR lpDIBBits,LONG lWidth,LONG lHeight)
 {
/*	 //有一点问题待解决!!!
	
    int i;              //行循环变量
	int j;                 //列循环变量
	LPSTR	lpSrcDIBBits;	//指向源像素的指针
	LPSTR	lpDstDIBBits;	//指向临时图象对应像素的指针
	LPSTR	lpDstStartBits;	//指向临时图象对应像素的指针 		
	HLOCAL	hDstDIBBits;	//临时图象句柄
	LONG lColBytes;

    lColBytes=WIDTHBYTES(lHeight*8);
	//hDstDIBBits= LocalAlloc(LHND, lHeight * lColBytes);// 分配临时内存
	hDstDIBBits= LocalAlloc(LHND, lColBytes*lWidth);// 分配临时内存
	lpDstStartBits= (char * )LocalLock(hDstDIBBits);// 锁定内存	
	if (hDstDIBBits== NULL)// 判断是否内存分配		
		return FALSE;// 分配内存失败				
	//for(j = 1; j < lWidth+1; j++)// 行
	for(j = 1; j < lWidth+1; j++)
	{
			for(i = 1; i <(lHeight+1); i++)	// 列
		//		for(j = 0; j < lWidth; j++)	// 列
		{
				lpDstDIBBits=(char*)lpDstStartBits+lColBytes*(j-1)+i;// 指向新DIB第i行,第j个像素的指针												
	
				lpSrcDIBBits=(char *)lpDIBBits+lColBytes*(lWidth-j-1)-i;// 指向源DIB第i0行,第j0个像素的指针								
				*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
		}
	}
//	memcpy(lpDIBBits, lpDstStartBits, lColBytes * lWidth);// 复制图象
	memcpy(lpDIBBits, lpDstStartBits, lWidth*lColBytes );// 复制图象
	LocalUnlock(hDstDIBBits);// 释放内存
	LocalFree(hDstDIBBits);	
	return TRUE;
	*/

	unsigned char*lpSrc,*flpSrc;
	LONG i;
	LONG j;

	LONG lLineBytes;

	unsigned char fTemp;


	lLineBytes=WIDTHBYTES(lWidth*8);
	for(i=0;i<lHeight/2;i++)
	{
		for(j=0;j<lWidth;j++)
		{
			lpSrc=(unsigned char*)lpDIBBits+lLineBytes*i+j;
			flpSrc=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-i)+j;
			fTemp=*flpSrc;
			*flpSrc=*lpSrc;
			*lpSrc=fTemp;
		}
	}

	return TRUE;
 }


 BOOL WINAPI Circumrotate(LPSTR lpDIBBits,LONG lWidth,LONG lHeight)
 {
	  int i;              //行循环变量
	int j;                 //列循环变量
	LPSTR	lpSrcDIBBits;	//指向源像素的指针
	LPSTR	lpDstDIBBits;	//指向临时图象对应像素的指针
	LPSTR	lpDstStartBits;	//指向临时图象对应像素的指针 		
	HLOCAL	hDstDIBBits;	//临时图象句柄
	LONG lColBytes;

    lColBytes=WIDTHBYTES(lHeight*8);
	hDstDIBBits= LocalAlloc(LHND, lWidth * lColBytes);// 分配临时内存
	lpDstStartBits= (char * )LocalLock(hDstDIBBits);// 锁定内存	
	if (hDstDIBBits== NULL)// 判断是否内存分配		
		return FALSE;// 分配内存失败				
	for(j = 0; j < lHeight; j++)// 行
	{
			for(i = 0; i < lWidth; i++)	// 列
		{
				lpDstDIBBits=(char*)lpDstStartBits+lColBytes*(lWidth-1-i)+j;// 指向新DIB第i行,第j个像素的指针												
	
				lpSrcDIBBits=(char *)lpDIBBits+lColBytes*(lWidth-
					j)-i;// 指向源DIB第i0行,第j0个像素的指针								
				*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
		}
	}
	memcpy(lpDIBBits, lpDstStartBits, lColBytes * lWidth);// 复制图象		
	LocalUnlock(hDstDIBBits);// 释放内存
	LocalFree(hDstDIBBits);	
	return TRUE;
	 
 }


	BOOL WINAPI LinerTrans(LPSTR lpDIBBits,LONG lWidth,LONG lHeight,int X1,int Y1,int X2,int Y2)
	{
		unsigned char* lpSrc;

	LONG i;
	LONG j;

	LONG lLineBytes;

	LONG fTemp;
	LONG fBiex;

	lLineBytes=WIDTHBYTES(lWidth*8);

	for(i=0;i<lHeight;i++)
	{
		for(j=0;j<lWidth;j++)
		{
			lpSrc=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j;
			fBiex=*lpSrc;
			if(X1==0)
			{
				fTemp=Y1;
			}
			else if(X2==255)
			{
				fTemp=Y2;
			}
			else if(fBiex>0&&fBiex<X1)
			{
				fTemp=fBiex*Y1/X1;
			}
			else if(fBiex>=X1&&fBiex<=X2)
			{
				fTemp=fBiex*(Y2-Y1)/(X2-X1)+Y1;
			}
			else if(fBiex>X2&&fBiex<=255)
			{
				fTemp=(fBiex+Y2-X2)*(255-Y2)/(225-X2);
			}

			if(fTemp>255)
			{
				*lpSrc=255;
			}
			else if(fTemp<0)
			{
				*lpSrc=0;
			}
			else
			{
				*lpSrc=(unsigned char)(fTemp+0.5);
			}
		}
	}

	return TRUE;

}

HGLOBAL  Zoom(LPSTR lpSrcDib, LPSTR lpSrcStartBits,long lWidth, long lHeight,
				   long lLineBytes,	WORD palSize,long lDstWidth,long lDstLineBytes,long lDstHeight,
				   float fhorRatio,float fverRatio)
{			
	LPSTR	lpDstDib;		//指向临时图象的指针	
	long i;                 //行循环变量
	long j;                 //列循环变量
	long i1;                 //行循环变量
	long j1;                 //列循环变量
	LPSTR	lpSrcDIBBits;	//指向源像素的指针
	LPSTR	lpDstDIBBits;	//指向临时图象对应像素的指针
	LPSTR	lpDstStartBits;	//指向临时图象对应像素的指针 		
			
	LPBITMAPINFOHEADER lpbmi;// 指向BITMAPINFO结构的指针
		
	// 分配内存,以保存缩放后的DIB
	HGLOBAL hDIB = (HGLOBAL) ::GlobalAlloc(GHND, lDstLineBytes* lDstHeight + *(LPDWORD)lpSrcDib +palSize);	
	if (hDIB == NULL)// 判断是否是有效的DIB对象
	{		
		return FALSE;// 不是,则返回
	}		
	lpDstDib=  (char * )::GlobalLock((HGLOBAL) hDIB);// 锁定内存		
	memcpy(lpDstDib, lpSrcDib, *(LPDWORD)lpSrcDib +palSize);// 复制DIB信息头和调色板		
	
	lpDstStartBits=lpDstDib+ *(LPDWORD)lpDstDib// 找到新DIB像素起始位置
		+palSize;// 求像素起始位置,作用如同::FindDIBBits(lpSrcDib),这里尝试使用了这种方法,以避免对全局函数的调用		
	lpbmi = (LPBITMAPINFOHEADER)lpDstDib;// 获取指针
		
	lpbmi->biWidth = lDstWidth;// 更新DIB中图象的高度和宽度
	lpbmi->biHeight =lDstHeight;	
	
	for(i = 0; i < lDstHeight; i++)// 行操作
	{		
		for(j = 0; j < lDstWidth; j++)// 列操作
		{
			// 指向新DIB第i行,第j个像素的指针
			lpDstDIBBits= (char *)lpDstStartBits + lDstLineBytes * (lDstHeight-1-i)+j;						
			i1= (long) (i / fverRatio + 0.5);// 计算该像素在源DIB中的坐标
			j1= (long) (j / fhorRatio + 0.5);			
			
			if( (j1>= 0) && (j1< lWidth) && (i1>= 0) && (i1< lHeight))
			{// 判断是否在源图内				
				lpSrcDIBBits= (char *)lpSrcStartBits+ lLineBytes * (lHeight - 1 -i1) + j1;// 指向源DIB第i行,第j个像素的指针								
				*lpDstDIBBits= *lpSrcDIBBits;// 复制像素
			}
			else
			{
				* ((unsigned char*)lpDstDIBBits) = 255;// 源图中不存在的像素,赋为255
			}				
		}		
	}
	return hDIB;
}


BOOL WINAPI MidFilter(LPSTR lpDIBBits,LONG lWidth,LONG lHeight)
{
//	unsigned char* lpSrc1,*lpSrc2,*lpSrc;
	unsigned char* lpSrc1,*lpSrc2,*lpSrc3,*lpSrc4,*lpSrc5,*lpSrc6,*lpSrc7,*lpSrc8,*lpSrc;

	int i;
	int j;

	LONG lLineBytes;

	LONG fTemp;
	LONG fBiex=20;

	lLineBytes=WIDTHBYTES(lWidth*8);

	for(i=0;i<lHeight-1;i++)
	{
		for(j=0;j<lWidth-1;j++)
		{
			lpSrc=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j;
			if((i>0) || ((i+1)<lHeight) || (j>0) || ((j+1)<lWidth))
			{
			 lpSrc1=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i+1))+j;
			 lpSrc2=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i+1))+j+1;
			 lpSrc3=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i+1))+j-1;
			 lpSrc4=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j+1;
			 lpSrc5=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j-1;
		     lpSrc6=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i-1))+j+1;
			 lpSrc7=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i-1))+j;
			 lpSrc8=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i-1))+j-1;
			//lpSrc=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j;
			 fTemp=(LONG)middnum(lpSrc1,lpSrc2,lpSrc3,lpSrc4,lpSrc5,lpSrc6,lpSrc7,lpSrc8,lpSrc);
			}else
				fTemp=(LONG)lpSrc;
			if(fTemp>255)
			{
				*lpSrc=255;
			}
			else if(fTemp<0)
			{
				*lpSrc=0;
			}
			else
			{
				*lpSrc=(unsigned char)(fTemp+0.5);
			}
		}
	}

	return TRUE;
}

unsigned char middnum(unsigned char* lpSrc1,unsigned char* lpSrc2,unsigned char* lpSrc3,unsigned char* lpSrc4,unsigned char* lpSrc5,unsigned char* lpSrc6,unsigned char* lpSrc7,unsigned char* lpSrc8,unsigned char* lpSrc)
{
	unsigned char little,mid,biggest;
	unsigned char lp[9];
	lp[0]=*lpSrc;
	lp[1]=*lpSrc1;
	lp[2]=*lpSrc2;
	lp[3]=*lpSrc3;
	lp[4]=*lpSrc4;
	lp[5]=*lpSrc5;
	lp[6]=*lpSrc6;
	lp[7]=*lpSrc7;
	lp[8]=*lpSrc8;
	int i,j;
	for(i=0;i<9;i++)
		for(j=0;j<9-i;j++)
		{
		 if(lp[j]>lp[j+1])
		 {
			 little=lp[j+1];
			 lp[j+1]=lp[j];
			 lp[j]=little;
		 }

		}
	mid=lp[4];
	biggest=lp[8];
	if(abs((LONG)(mid)-(LONG)(biggest))<0.0005)
	{
		mid=*lpSrc;
	}
	return mid;
}

/*

  BOOL WINAPI Sobel(LPSTR lpDIBBits,LONG lWidth,LONG lHeight)
  {

  int i;
	int j;
	unsigned char* lpSrc1,*lpSrc2,*lpSrc3,*lpSrc4,*lpSrc9,*lpSrc6,*lpSrc7,*lpSrc8,*lpSrc;

	LONG lLineBytes;

	LONG fTemp;
	LONG fTempx;
	LONG fTempy;
	LONG fBiex=20;

	lLineBytes=WIDTHBYTES(lWidth*8);

	for(i=0;i<lHeight-1;i++)
	{
		for(j=0;j<lWidth-1;j++)
		{
			lpSrc=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j;
			if((i>0) || ((i+1)<lHeight) || (j>0) || ((j+1)<lWidth))
			{
			 lpSrc8=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i+1))+j;
			 lpSrc9=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i+1))+j+1;
			 lpSrc7=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i+1))+j-1;
			 lpSrc6=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j+1;
			 lpSrc4=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-i)+j-1;
		     lpSrc3=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i-1))+j+1;
			 lpSrc2=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i-1))+j;
			 lpSrc1=(unsigned char*)lpDIBBits+lLineBytes*(lHeight-1-(i-1))+j-1;
			 fTempx=abs(-(int)(*lpSrc1)+(int)(*lpSrc3)-2*(int)(*lpSrc4)+2*(int)(*lpSrc6)-(int)(*lpSrc7)+(int)(*lpSrc9));
			 fTempy=abs(-(int)(*lpSrc7)+(int)(*lpSrc1)-2*(int)(*lpSrc8)+2*(int)(*lpSrc2)-(int)(*lpSrc9)+(int)(*lpSrc3));
			 fTemp=fTempx+fTempy;
			 //fTemp=(abs((int)(*lpSrc3)+(int)(*lpSrc6)+(int)(*lpSrc6)+(int)(*lpSrc9)-(int)(*lpSrc1)-(int)(*lpSrc4)-(int)(*lpSrc4)-(int)(*lpSrc7))
			//		+abs((int)(*lpSrc1)+(int)(*lpSrc2)+(int)(*lpSrc2)+(int)(*lpSrc3)-(int)(*lpSrc7)-(int)(*lpSrc8)-(int)(*lpSrc8)-(int)(*lpSrc9)))/2;
			//fTemp=sqrt(pow(abs((int)(*lpSrc3)+(int)(*lpSrc6)+(int)(*lpSrc6)+(int)(*lpSrc9)-(int)(*lpSrc1)-(int)(*lpSrc4)-(int)(*lpSrc4)-(int)(*lpSrc7)),2)
			//		+pow(abs((int)(*lpSrc1)+(int)(*lpSrc2)+(int)(*lpSrc2)+(int)(*lpSrc3)-(int)(*lpSrc7)-(int)(*lpSrc8)-(int)(*lpSrc8)-(int)(*lpSrc9)),2));
			
			}else
				fTemp=(LONG)lpSrc;
			
			if(fTemp>255)
			{
				*lpSrc=255;
			}
			else if(fTemp<0)
			{
				*lpSrc=0;
			}
			else
			{

⌨️ 快捷键说明

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