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

📄 multimark.cpp

📁 十分详细的基于Wavelet的图像水印嵌入程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	h=pDoc->m_nHeight;

	Lx=(Seedx*Seedx)%Nx;
	Ly=(Seedy*Seedy)%Ny;

	*(nRecPoint)=(Lx%w);
	*(nRecPoint+1)=(Ly%h);

	int Tempw,Temph;
	BOOL blFlag;
	int count=1;

	BYTE *bDrawWatermark;
	
	bDrawWatermark=new BYTE[nWaterMarkWidth*nWaterMarkHeight];

	pb=pDoc->m_pbPic;

	while(count<m_nMarkWidth*m_nMarkHeight)
	{
		blFlag=TRUE;
		Lx=(Lx*Lx)%Nx;
		Ly=(Ly*Ly)%Ny;

		Tempw=(Lx%w);
		Temph=(Ly%h);
		
		for(j=0;j<count;j++)
		{
			if((Tempw==*(nRecPoint+2*j))&&(Temph==*(nRecPoint+2*j+1)))
			{
				blFlag=FALSE;
			}
			Lx=Lx+1;
			Ly=Ly+2;
		}
		if(blFlag)
		{
			*(nRecPoint+2*count)=Tempw;
			*(nRecPoint+2*count+1)=Temph;
			count++;
		}
	}/**/

	if(pDoc->m_nBitCount== 8 )
	{
		for(i=0;i<m_nMarkWidth*m_nMarkHeight;i++)
		{
			Tempw=*(nRecPoint+2*i);
			Temph=*(nRecPoint+2*i+1);
			if((*(pb+Temph*w+Tempw)%(2*bit))>=bit)
			{
				*(bDrawWatermark+i)=255;
			}else
				*(bDrawWatermark+i)=0;
		}
	}else if(pDoc->m_nBitCount== 24)
	{
		for(i=0;i<m_nMarkWidth*m_nMarkHeight;i++)
		{
			Tempw=*(nRecPoint+2*i);
			Temph=*(nRecPoint+2*i+1);
			if((*(pb+Temph*w*3+Tempw*3)%(2*bit))>=bit)
			{
				*(bDrawWatermark+i)=255;
			}else
				*(bDrawWatermark+i)=0;
		}
	}


	BF.bfOffBits =54;
	BF.bfReserved1 =0;
	BF.bfReserved2 =0;
	BF.bfSize =int((nWaterMarkWidth + 3) / 4) * 4;
	BF.bfType =19778;

	BI.biBitCount =24;
	BI.biClrImportant =0;
	BI.biClrUsed =0;
	BI.biCompression =0;
	BI.biHeight =nWaterMarkHeight;
	BI.biPlanes =1;
	BI.biSize =40;
	BI.biSizeImage =0;
	BI.biWidth =nWaterMarkWidth;
	BI.biXPelsPerMeter =0;
	BI.biYPelsPerMeter =0;
		
	int nSizeWidth;
	nSizeWidth=((nWaterMarkWidth*3+3)/4)*4 ;
	BYTE *lpTemp;

	lpTemp = new BYTE[nSizeWidth*nWaterMarkHeight];
	int l;
	
	for(j=0;j<nWaterMarkHeight;j++){
		for(i=0;i<nWaterMarkWidth ;i++){
			for(l=0;l<3;l++){
				lpTemp[j*nSizeWidth+3*i+l]=bDrawWatermark[j*nWaterMarkWidth+i];
			}
		}							//for i
	}

	CFileDialog odlg(FALSE,"bmp","*.bmp");	//显示写入图像的文件

	CFile fOutFile;

//	if(odlg.DoModal()==IDOK){

	CString sFileName="c:\\temp.bmp";
//		fOutFile.Open (odlg.GetPathName(),CFile::modeCreate | CFile::modeWrite );
		fOutFile.Open (sFileName,CFile::modeCreate | CFile::modeWrite );
		fOutFile.Write((LPVOID) &BF, sizeof(BITMAPFILEHEADER));
		fOutFile.Write((LPVOID) &BI,  sizeof(BITMAPINFOHEADER));
		fOutFile.Write ((LPVOID) lpTemp, nWaterMarkHeight*nSizeWidth );
		fOutFile.Close();
//	}

/*	CClientDC dlgDC(this);
	for(i=0;i<nWaterMarkHeight;i++)
	{
		for(j=0;j<nWaterMarkWidth;j++)
		{

			dlgDC.SetPixel(pDoc->m_nWidth+j,nWaterMarkHeight+nWaterMarkHeight-i-1,RGB(*(bDrawWatermark+nWaterMarkWidth*i+j),*(bDrawWatermark+i*nWaterMarkWidth+j),*(bDrawWatermark+i*nWaterMarkWidth+j)));
		}
	}*/
	free(bDrawWatermark);
	free(lpTemp);

	fOutFile.Open(sFileName,CFile::modeRead);
//	fOutFile.Open(odlg.GetPathName(),CFile::modeRead);
	m_dibDrawMark.Read(&fOutFile);
	fOutFile.Close();
	free(nRecPoint);
	Invalidate();
}

void CMultiMarkView::OnVisualMarkDctDwt() 
{
	CMultiMarkDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CDct Dct;
	int *pnI;
	long i,temp,j,l;
	long lgSizePicture;
	int *pn;
	BYTE *pb;
	pb=pDoc->m_Dib.m_lpImage;
	pn=pDoc->m_pnPic;

	int nPointx=50;
	int nPointy=pDoc->m_nHeight-150;
	int *pnCover;

//**************************************************************************
//**************************************************************************
//水印图像与原始图像融合

	if(m_nMarkBitCount==8 && pDoc->m_nBitCount==8)
	{
		pnCover=new int[m_nMarkWidth*m_nMarkHeight];
		for(i=0;i<m_nMarkHeight;i++)
		{
			for(j=0;j<m_nMarkWidth;j++)
			{
				*(pnCover+i*m_nMarkWidth+j)=*(pn+(i+nPointy)*pDoc->m_nWidth+(nPointx+j));

				*(pn+(i+nPointy)*pDoc->m_nWidth+(nPointx+j))=*(m_pnMark+i*m_nMarkWidth+j);
			}
		}
	}
	else if(m_nMarkBitCount==24 && pDoc->m_nBitCount==8)
	{
		pnCover=new int[m_nMarkWidth*m_nMarkHeight];
		for(i=0;i<m_nMarkHeight;i++)
		{
			for(j=0;j<m_nMarkWidth;j++)
			{
				*(pnCover+i*m_nMarkWidth+j)=*(pn+(i+nPointy)*pDoc->m_nWidth+(nPointx+j));

				*(pn+(i+nPointy)*pDoc->m_nWidth+(nPointx+j))=(*(m_pnMark+3*i*m_nMarkWidth+3*j)+*(m_pnMark+3*i*m_nMarkWidth+3*j+1)+*(m_pnMark+3*i*m_nMarkWidth+3*j+2))/3;
			}
		}
	}
	else if(m_nMarkBitCount==8 && pDoc->m_nBitCount==24)
	{
		pnCover=new int[m_nMarkWidth*m_nMarkHeight*3];
		for(i=0;i<m_nMarkHeight;i++)
		{
			for(j=0;j<m_nMarkWidth;j++)
			{
				*(pnCover+3*i*m_nMarkWidth+3*j)=*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j));
				*(pnCover+3*i*m_nMarkWidth+3*j+1)=*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+1);
				*(pnCover+3*i*m_nMarkWidth+3*j+2)=*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+2);
				*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j))=*(m_pnMark+i*m_nMarkWidth+j);
				*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+1)=*(m_pnMark+i*m_nMarkWidth+j);
				*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+2)=*(m_pnMark+i*m_nMarkWidth+j);
			}
		}
	}
	else if(m_nMarkBitCount==24 && pDoc->m_nBitCount==24)
	{
		pnCover=new int[m_nMarkWidth*m_nMarkHeight*3];
		for(i=0;i<m_nMarkHeight;i++)
		{
			for(j=0;j<m_nMarkWidth;j++)
			{
				*(pnCover+3*i*m_nMarkWidth+3*j)=*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j));
				*(pnCover+3*i*m_nMarkWidth+3*j+1)=*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+1);
				*(pnCover+3*i*m_nMarkWidth+3*j+2)=*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+2);
				*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j))=*(m_pnMark+3*i*m_nMarkWidth+3*j);
				*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+1)=*(m_pnMark+3*i*m_nMarkWidth+3*j+1);
				*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+2)=*(m_pnMark+3*i*m_nMarkWidth+3*j+2);
			}
		}
	}

//**************************************************************************
//**************************************************************************
//覆盖图像进行有损压缩

	int nBlockx;
	int nBlocky;
	int x,y;
	nBlockx=m_nMarkWidth/8;
	nBlocky=m_nMarkHeight/8;
	double T[8][8];
	int nScale=4;				//保留4*4个系数
//	int nRemain[8][8][4][4];	//保留系数

	if(pDoc->m_nBitCount==8)
	{
		for(i=0;i<nBlocky;i++)
		{
			for(j=0;j<nBlockx;j++)
			{				
				
				for(x=0;x<8;x++)
				{
					for(y=0;y<8;y++)
					{
						T[y][x]=(double)*(pnCover+(i*8+x)*m_nMarkWidth+j*8+y);
					}
				}
				Dct.FloatDCT(T);

				for(x=0;x<nScale;x++)
				{
					for(y=0;y<nScale;y++)
					{
						nRemain[j][i][y][x]=(int)T[y][x];
					}
				}
				//********************************
				//********************************
				//*******测试
/*	*/			for(x=0;x<8;x++)
				{
					for(y=0;y<8;y++)
					{
						if(!(x<nScale && y<nScale))
						{
							T[y][x]=0;
						}
					}
				}
				//**********************************
				//**********************************

/*	*/			Dct.FloatIDCT(T);
				for(x=0;x<8;x++)

				{
					for(y=0;y<8;y++)
					{
						*(pnCover+(i*8+x)*m_nMarkWidth+j*8+y)=(int)T[y][x];
					}
				}
			}
		}
	}
	int nCof;
	int nLay;
	int bit,count;
//**************************************************************************
//**************************************************************************
//融合图像进行整数小波变换,将DCT系数嵌入到中间
	lgSizePicture=(long)pDoc->m_nHeight*(long)pDoc->m_nWidth;
	
	pnI= new int [(long)pDoc->m_nHeight*(long)pDoc->m_nWidth];

	
	if( pDoc->m_nBitCount== 8 )
	{

		for( i = 0; i < lgSizePicture; i++ )	
			*( pnI + i) = (*( pDoc->m_pnPic+i)-128);

		m_IntWT.IntWT_2D( pnI, pDoc->m_nHeight, pDoc->m_nWidth, 1, 1 );
//		m_IntWT.IntWT( pnI, pDoc->m_nHeight, pDoc->m_nWidth, 1, 1 );

//		memcpy( pnRecIntW, pnI, sizeof(int)*lgSizePicture );

		//++++++++++++++++++++++++++++++++++++++++++++++
		//++++++++++++++++++++++++++++++++++++++++++++++
		//+++++信息隐藏
		count=1;
		for(i=0;i<nBlocky;i++)
		{
			for(j=0;j<nBlockx;j++)
			{
				for(x=0;x<4;x++)
				{
					for(y=0;y<4;y++)
					{
						nCof=nRemain[j][i][y][x];
						for(nLay=0;nLay<16;nLay++)
						{
							bit=nCof%2;
							nCof=nCof/2;
							if(*(pnI+count*4)>=0)
							{
								*(pnI+count*4)=(*(pnI+count*4)/4)*4+3*bit;
							}else 
							{
								*(pnI+count*4)=(*(pnI+count*4)/4)*4-3*bit;
							}
							count++;
						}
					}
				}
			}
		}
        //++++++++++++++++++++++++++++++++++++++++++++++++
		//++++++++++++++++++++++++++++++++++++++++++++++++

		m_IntWT.IntWT_2D_I( pnI, pDoc->m_nHeight, pDoc->m_nWidth, 1, 1 );	
		
		for(i=0;i<lgSizePicture;i++)
		{
			if((*(pnI+i)+128)<0)
			{
				temp=0;
			}
			else if((*(pnI+i)+128)>255)
			{
				temp=255;
			}
			else
				temp=(int)(*(pnI+i)+128);

			*(pnI+i)=temp;
		}
		
		memcpy( pDoc->m_pnPic, pnI, sizeof(int)*lgSizePicture );

	}
	else if( pDoc->m_nBitCount==24 )
	{
		for(int p=0;p<3;p++)
		{
			for( i = 0; i < lgSizePicture; i++ )	
				*( pnI + i) = (*( pDoc->m_pnPic+3*i+p)-128);

			m_IntWT.IntWT_2D( pnI, pDoc->m_nHeight, pDoc->m_nWidth, 1, 1 );
			
			m_IntWT.IntWT_2D_I( pnI, pDoc->m_nHeight, pDoc->m_nWidth, 1, 1 );	

			for(i=0;i<lgSizePicture;i++)
			{
				if((*(pnI+i)+128)<0)
				{
					temp=0;
				}
				else if((*(pnI+i)+128)>255)
				{
					temp=255;
				}
				else
					temp=(int)(*(pnI+i)+128);

				*(pnI+i)=temp;
			}
			
			for( i = 0; i < lgSizePicture; i++ )	
				*( pDoc->m_pnPic+3*i+p)= (*( pnI + i));
		}

	}
		
	free(pnI);
	//*****************************************************
	//*****************************************************
	//**********测试
/*	if(pDoc->m_nOriBitCount==8)
	{
		for(i=0;i<m_nMarkHeight;i++)
		{
			for(j=0;j<m_nMarkWidth;j++)
			{
				*(pn+(i+nPointy)*pDoc->m_nWidth+(nPointx+j))=*(pnCover+i*m_nMarkWidth+j);
			}
		}
	}
	else if(pDoc->m_nOriBitCount==24)
	{
		for(i=0;i<m_nMarkHeight;i++)
		{
			for(j=0;j<m_nMarkWidth;j++)
			{
				*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j))=*(pnCover+3*i*m_nMarkWidth+3*j);
				*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+1)=*(pnCover+3*i*m_nMarkWidth+3*j+1);
				*(pn+3*(i+nPointy)*pDoc->m_nWidth+3*(nPointx+j)+2)=*(pnCover+3*i*m_nMarkWidth+3*j+2);
			}
		}
	}*/

	//*************************************************************
	//*************************************************************
	
	if(pDoc->m_nBitCount==24)
	{
		for(j=0;j<pDoc->m_nHeight;j++){
			for(i=0;i<pDoc->m_nWidth ;i++){
				for(l=0;l<3;l++){
					*pDoc->m_Dib.m_lpImage=(BYTE)*pDoc->m_pnPic;
					pDoc->m_pnPic=pDoc->m_pnPic+1;
					pDoc->m_Dib.m_lpImage=pDoc->m_Dib.m_lpImage+1;						
				}						//for l
			}							//for i
			if(((pDoc->m_nWidth*3)%4)!=0){		//加入附加字节
				pDoc->m_Dib.m_lpImage=pDoc->m_Dib.m_lpImage+4-((pDoc->m_nWidth*3)%4);
			}
		}
	}else if(pDoc->m_nBitCount==8)
	{
		for(j=0;j<pDoc->m_nHeight;j++){
			for(i=0;i<pDoc->m_nWidth ;i++){
				*pDoc->m_Dib.m_lpImage=(BYTE)*pDoc->m_pnPic;
				pDoc->m_pnPic=pDoc->m_pnPic+1;
				pDoc->m_Dib.m_lpImage=pDoc->m_Dib.m_lpImage+1;						
			
			}							//for i
			if(((pDoc->m_nWidth)%4)!=0){		//加入附加字节
				pDoc->m_Dib.m_lpImage=pDoc->m_Dib.m_lpImage+4-((pDoc->m_nWidth)%4);
			}
		}
	}
	pDoc->m_Dib.m_lpImage=pb;
	pDoc->m_pnPic=pn;
	Invalidate();		
}

void CMultiMarkView::OnMarkDctDwtDraw() 
{
	CMultiMarkDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CDct Dct;
	int *pnI;
	long i,j,l;
	long lgSizePicture;
	int *pn;
	BYTE *pb;
	pb=pDoc->m_Dib.m_lpImage;
	pn=pDoc->m_pnPic;

	int nPointx=50;
	int nPointy=pDoc->m_nHeight-150;
	int *pnCover;

	int nCof;
	int nLay;
	int bit,count,nTemp;
	CFile fOutFile;
	
	int nBlockx;
	int nBlocky;
	int x,y;
	nBlockx=8;
	nBlocky=8;
	double T[8][8];
	int nScale=4;				//保留4*4个系数
//	int nRemain[8][8][4][4];	//保留系数
//**************************************************************************
//**************************************************************************
//融合图像进行整数小波变换,将DCT系数提取出来
	lgSizePicture=(long)pDoc->m_nHeight*(long)pDoc->m_nWidth;
	
	pnI= new int [(long)pDoc->m_nHeight*(long)pDoc->m_nWidth];

	
	if( pDoc->m_nBitCount== 8 )
	{
		for( i = 0; i < lgSizePicture; i++ )	
			*( pnI + i) = (*( pDoc->m_pnPic+i)-128);

		m_IntWT.IntWT_2D( pnI, pDoc->m_nHeight, pDoc->m_nWidth, 1, 1 );

		//++++++++++++++++++++++++++++++++++++++++++++++
		//++++++++++++++++++++++++++++++++++++++++++++++
		//+++++信息提取
		
/**/	count=1;
		for(i=0;i<nBlocky;i++)
		{
			for(j=0;j<nBlockx;j++)
			{
				for(x=0;x<4;x++)
				{
					for(y=0;y<4;y++)
					{
						nCof=0;
						for(nLay=0;nLay<16;nLay++)
						{
							nTemp=1;
							for(int r=0;r<(nLay);r++)
							{
								nTemp=nTemp*2;
							}
							if((*(pnI+count*4)%4)<2 && (*(pnI+count*4)%4)>-2)
							{
								bit=0;
							}else
							{
								bit=1;
							}
							nCof=nCof+bit*nTemp;
							
							count++;
						}
//						nRemain[j][i][y][x]=nCof;
					}
				}
			}
		}

        //++++++++++++++++++++++++++++++++++++++++++++++++
		//++++++++++++++++++++++++++++++++++++++++++++++++
	}
		
	free(pnI);

	//+++++++++++++++++++++++++++++++++++++++++++++++++++++
	//+++++++++++++++++++++++++++++++++++++++++++++++++++++
	
	if(pDoc->m_nBitCount==8)
	{
		pnCover=new int[64*64];
		for(i=0;i<nBlocky;i++)
		{
			for(j=0;j<nBlockx;j++)
			{				
				

⌨️ 快捷键说明

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