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

📄 watermethod.cpp

📁 站长!这是DCT域的图像数字水印嵌入及提取程序!请查收!暂归入加密解密类别!
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	double column[MAXBLOCKWIDTH]; //Define the parameter of the second parameter
	double temp;	              //Define the temporary variable 
	double temp1;	              //Define the first temporary variable 
	double temp2;	              //Define the second temporary variable 

	//Initiaize the transform parameters
	row[0]=sqrt(1.0/blockheight);
	column[0]=sqrt(1.0/blockwidth);	
	for(i=1;i<blockheight;i++)
	{
	  row[i]=sqrt(2.0/blockheight);
	}
	for(i=1;i<blockwidth;i++)
	{
	  column[i]=sqrt(2.0/blockwidth);
	}
    //Calculate the IDCT of blockheight*blockwidth points 
	for(timerow=0;timerow<blockheight;timerow++)
	{
		for(timecolumn=0;timecolumn<blockwidth;timecolumn++)
		{
			//Calculate the IDCT of one point
			temp1=0.0;
			for(frerow=0;frerow<blockheight;frerow++)
			{
				temp2=0.0;
				for(frecolumn=0;frecolumn<blockwidth;frecolumn++)
				{
					temp=(2.0*timecolumn+1.0)/(2.0*blockwidth)*frecolumn*PI;
					temp2=temp2+DCTBlock[frerow][frecolumn]*cos(temp)*column[frecolumn];
				}
				temp=(2.0*timerow+1.0)/(2.0*blockheight)*frerow*PI;				
				temp1=temp1+temp2*cos(temp)*row[frerow];
			}
			//The Rounding Operation (To The Nearest Integer Number)
			if(temp1>=254.5)
			{
				ReconstructedImageBlock[timerow][timecolumn]=255;
			}
			else 
			{
				if(temp1<0.5)
				{
				  ReconstructedImageBlock[timerow][timecolumn]=0;
				}
				else
				{
				  ReconstructedImageBlock[timerow][timecolumn]=(unsigned char)(temp1+0.5);
				}
			}
		}
	}
}*/
void PSEUDO(unsigned char *INPUT,unsigned char *OUTPUT,int *KEY,int NUMBER)
{
	//Generate a key--random number sequence
	int i,j;
	for(i=0;i<NUMBER;i++)
	{
		KEY[i]=i;
	}
	int *keytemp1,*keytemp2;
	srand((unsigned)time(NULL));
	for(keytemp1=KEY;keytemp1<KEY+NUMBER;keytemp1++)
	{
		*keytemp1=(int)((rand()*1.0/32767)*NUMBER);
		for(keytemp2=KEY;keytemp2<keytemp1;keytemp2++)
		{
			if(*keytemp1==*keytemp2)
			{
				*keytemp1=(int)((rand()*1.0/32767)*NUMBER);
				keytemp2=KEY;
				keytemp1=keytemp1-1;
			}
		}
	}

	//Generate output-random sequence 
	for(i=0;i<NUMBER;i++)
	{
		j=KEY[i];
		OUTPUT[i]=INPUT[j];
	}
}

//Inverse pseudo-random permute
void INVERSEPSEUDO(unsigned char *INPUT,unsigned char *OUTPUT,int *KEY,int NUMBER)
{
	int i,j;
	for(i=0;i<NUMBER;i++)
	{
		j=KEY[i];
		OUTPUT[j]=INPUT[i];
	}
}

double VARIANCE(unsigned char *INPUT,int NUMBER)
{
	double mean;
	double temp;
	double variance;
	unsigned char *p;
	//Obtain the mean of the input data
	temp=0;
	for(p=INPUT;p<INPUT+NUMBER;p++)
	{
		temp=temp+(*p);
	}
	mean=temp/NUMBER;
	//Obtain the variance of the input data
	temp=0;
	for(p=INPUT;p<INPUT+NUMBER;p++)
	{
		temp=temp+((*p)-mean)*((*p)-mean);
	}
	variance=sqrt(temp/(NUMBER-1));
	return variance ;
}


//Small to Large permute
void SEQPMT(double *INPUT,double *OUTPUT,int *SEQPMTKEY,int NUMBER)
{
	//Generate a key--random number sequence
	int i,j;
	int *keytemp1,*keytemp2,keytemp;
	for(i=0;i<4096;i++)
	{
		SEQPMTKEY[i]=i;
	}
	for(keytemp1=SEQPMTKEY;keytemp1<SEQPMTKEY+NUMBER;keytemp1++)
	{
		for(keytemp2=SEQPMTKEY;keytemp2<keytemp1;keytemp2++)
		{
			if(INPUT[*keytemp2]>INPUT[*keytemp1])
			{
				//Exchange the values of two  elements
				keytemp=*keytemp2;
				*keytemp2=*keytemp1;
				*keytemp1=keytemp;
				keytemp2=SEQPMTKEY;
				keytemp1=keytemp1-1;
			}
		}
	}
	//Generate ordinal sequence 
	for(i=0;i<NUMBER;i++)
	{
		j=SEQPMTKEY[i];
		OUTPUT[i]=INPUT[j];
	}
}

//Inverse sequence permute
void INVERSESEQPMT(double *INPUT,double *OUTPUT,int *SEQPMTKEY,int NUMBER)
{
	int i,j;
	for(i=0;i<NUMBER;i++)
	{
		j=SEQPMTKEY[i];
		OUTPUT[j]=INPUT[i];
	}
}


void SEQUENCE(unsigned char *INPUT,unsigned char *OUTPUT,int *SEQUENCEKEY,int NUMBER)
{
	//Generate a key--random number sequence
	int i,j;
	int *keytemp1,*keytemp2,keytemp;
	for(i=0;i<NUMBER;i++)
	{
		SEQUENCEKEY[i]=i;
	}
	for(keytemp1=SEQUENCEKEY;keytemp1<SEQUENCEKEY+NUMBER;keytemp1++)
	{
		for(keytemp2=SEQUENCEKEY;keytemp2<keytemp1;keytemp2++)
		{
			if(INPUT[*keytemp2]>INPUT[*keytemp1])
			{
				//Exchange the values of two  elements
				keytemp=*keytemp2;
				*keytemp2=*keytemp1;
				*keytemp1=keytemp;
				keytemp2=SEQUENCEKEY;
				keytemp1=keytemp1-1;
			}
		}
	}
	//Generate ordinal sequence 
	for(i=0;i<NUMBER;i++)
	{
		j=SEQUENCEKEY[i];
		OUTPUT[i]=INPUT[j];
	}
}

//Inverse sequence permute
void INVERSESEQUENCE(unsigned char *INPUT,unsigned char *OUTPUT,int *SEQUENCEKEY,int NUMBER)
{
	int i,j;
	for(i=0;i<NUMBER;i++)
	{
		j=SEQUENCEKEY[i];
		OUTPUT[j]=INPUT[i];
	}
}

//Calculate the mark element number
int MARKNUMBER(unsigned char *INPUT,int NUMBER)
{
	int number;
	unsigned char *p;
	number=0;
	for(p=INPUT;p<INPUT+NUMBER;p++)
	{
		if (*p==1)
		{
			number=number+1;
		}
	}
	return number;
}
void addembedwatermark(double *input1[],unsigned char *input2[],double *output[],int M1,int M2, int N1,int N2 )
{
	int i,j;
	for (i=0;i<M1;i++)
	{
		for (j=0;j<M2;j++)
		{
			output[i][j]=input1[i][j];
		}
	}
	
	if (input2[0][0]==1)
	{
		output[2][2]=input1[2][2]+10;
	}
	if (input2[0][1]==1)
	{
		output[2][4]=input1[2][4]+10;
	}
	if (input2[1][0]==1)
	{
		output[4][2]=input1[4][2]+10;
	}
	if (input2[1][1]==1)
	{
		output[3][3]=input1[3][3]+10;
	}
}

void extractwatermark(double *input1[],double *input2[],unsigned char *output[])
{
	if (input2[2][2]-input1[2][2]<8)
	{
		output[0][0]=0;
	}
	else
	{
		output[0][0]=1;
	}
	if (input2[2][4]-input1[2][4]<8)
	{
		output[0][1]=0;
	}
	else
	{
		output[0][1]=1;
	}
	if (input2[4][2]-input1[4][2]<8)
	{
		output[1][0]=0;
	}
	else
	{
		output[1][0]=1;
	}
	if (input2[3][3]-input1[3][3]<8)
	{
		output[1][1]=0;
	}
	else
	{
		output[1][1]=1;
	}
}

int CWatermethod::AddBased(long int blocks,
						 unsigned short  blockheight,
						 unsigned short  blockwidth,
						 unsigned short  hb,
						 unsigned short  wb,
						 unsigned char *SourceVector[MAXINPUTVECTORS],
						 unsigned char *WmSourceVector[MAXINPUTVECTORS],
						 unsigned char *TerminalVector[MAXINPUTVECTORS],
                         int *imageblockvarseqkey,
                         int *wmblockmarknumseqkey)
{
   long int i,j,k,l;
   unsigned short int dimension;
   int m;
   char temp[256];
   
   double *imageblockvariance;
   double *imageblockvarseq; 
   
   unsigned char *prewmimagedata;		//The permuted watermark image data in memory
   unsigned char *wmblockmarknumber;
   unsigned char *wmblockmarknumseq;	
   
   unsigned char blktime[MAXBLOCKHEIGHT][MAXBLOCKWIDTH];
   unsigned char *blocktime[MAXBLOCKHEIGHT];
   unsigned char wmblktime[2][2];
   unsigned char *wmblocktime[2];
   
   unsigned char embedimageblktime[MAXBLOCKHEIGHT][MAXBLOCKWIDTH];
   unsigned char *embedimageblocktime[MAXBLOCKHEIGHT];

   double blkfreq[MAXBLOCKHEIGHT][MAXBLOCKWIDTH];
   double *blockfreq[MAXBLOCKHEIGHT];
   double embedimageblkfreq[MAXBLOCKHEIGHT][MAXBLOCKWIDTH];
   double *embedimageblockfreq[MAXBLOCKHEIGHT];
   
   HGLOBAL hMem;
   for(l=0;l<MAXBLOCKHEIGHT;l++)
   {
	   blocktime[l]=blktime[l];
	   blockfreq[l]=blkfreq[l];
	   embedimageblocktime[l]=embedimageblktime[l];
	   embedimageblockfreq[l]=embedimageblkfreq[l];
   }
   for(l=0;l<2;l++)
   	   wmblocktime[l]=wmblktime[l];
   hMem=NULL;
   hMem=GlobalAlloc(GMEM_FIXED,2*blocks*sizeof(double)*64+2*blocks*sizeof(int)*64+128*128+2*blocks*sizeof(unsigned char)*64);
   if(hMem==NULL)
   {
	  sprintf(temp,"对不起,内存不够%fMBytes,无法进行编码!",(double)(MAXIMAGESIZE)/1024.0/1024.0);
	  MessageBox(NULL,temp,"编码",MB_ICONSTOP|MB_OK);
	  return -1;
	}
   //Alloc the memory
   imageblockvariance=(double *)hMem;
   imageblockvarseq=imageblockvariance+blocks*sizeof(double);
   imageblockvarseqkey=(int *)(imageblockvarseq+blocks*sizeof(double));
   wmblockmarknumseqkey=imageblockvarseqkey+blocks*sizeof(int);
   prewmimagedata=(unsigned char *)(wmblockmarknumseqkey+blocks*sizeof(int));
   wmblockmarknumber=prewmimagedata+128*128;
   wmblockmarknumseq=wmblockmarknumber+blocks;

   int temp1,temp2;
   int l1,k1,l2,k2;
   //the arrays
   dimension=blockwidth*blockheight;
   if(blocks>MAXENCODEVECTORS)
   {
	  GlobalFree(hMem);
	  return -1;
   }
   for(m=0;m<blocks;m++)
   {imageblockvariance[m]=VARIANCE(SourceVector[m],BLOCKSIZE);
    imageblockvarseqkey[m]=0;
	}
	//Permute the variance of the original image blocks
    SEQPMT(imageblockvariance,imageblockvarseq,imageblockvarseqkey,blocks);
	//Calculate the mark pixel number of the original watermark image blocks
	for(m=0;m<blocks;m++)
	  wmblockmarknumber[m]=MARKNUMBER(WmSourceVector[m],WMBLOCKSIZE);
	//Permute the mark pixel image number of the original watermark image blocks
	SEQUENCE(wmblockmarknumber,wmblockmarknumseq,wmblockmarknumseqkey,blocks);
	//Showing the pretreated watermark image
	for(m=0;m<blocks;m++)
	{
		temp1=*(wmblockmarknumseqkey+m);
		temp2=*(imageblockvarseqkey+m);
		l1=temp1/64;
		k1=temp1%64;
		l2=temp2/64;
		k2=temp2%64;
		for(j=0;j<2;j++)
		  for(i=0;i<2;i++)
			prewmimagedata[i+k2*2+(j+l2*2)*128]=WmSourceVector[temp1][i+j*2];
	}
	for(m=0;m<64*64;m++)
	{
		//Transmit one block data of the original image to the blocktime block of the original image 
		temp1=*(imageblockvarseqkey+m);
		temp2=*(wmblockmarknumseqkey+m);
		for(l=0;l<blockheight;l++)
		  for(k=0;k<blockwidth;k++) 	
		 	blktime[k][l]=SourceVector[temp1][k+l*blockwidth];
		//Transmit one block data of the original watermark image to the blocktime block of the original watermark image
		for(l=0; l<2;l++)
		  for(k=0; k<2; k++)	
			 wmblktime[k][l]=WmSourceVector[temp2][k+l*2];
		//DCT is use for one sub block of the original image
		DCT(blocktime,blockfreq,blockwidth,blockheight);
		//Embed the watermark into the original image
		addembedwatermark(blockfreq,wmblocktime,embedimageblockfreq,blockwidth,blockheight,2,2);
		//IDCT is use for one sub block 
		IDCT(embedimageblockfreq,embedimageblocktime,blockwidth,blockheight);
		//Transmit the the data after DCT and IDCT operation to store memory
		for(l=0; l<blockheight;l++)
		  for(k=0; k<blockwidth; k++)
			TerminalVector[temp1][k+l*blockwidth]=embedimageblocktime[k][l];
	}
	GlobalFree(hMem);
    return 0;
}

int CWatermethod::AddBasedExtract(long int blocks,
						   unsigned short  blockheight,
						   unsigned short  blockwidth,
						   unsigned short  hb,
						   unsigned short  wb,
						   unsigned char *SourceVector[MAXINPUTVECTORS],
						   unsigned char *pp01[MAXINPUTVECTORS],
						   unsigned char *TerminalVector[MAXINPUTVECTORS],
                           int *imageblockvarseqkey,
                           int *wmblockmarknumseqkey)

{
   int l,m,k;
   unsigned char blktime[MAXBLOCKHEIGHT][MAXBLOCKWIDTH];
   unsigned char *blocktime[MAXBLOCKHEIGHT];
   unsigned char extwmblktime[2][2];
   unsigned char *extwmblocktime[2];
   
   unsigned char embedimageblktime[MAXBLOCKHEIGHT][MAXBLOCKWIDTH];
   unsigned char *embedimageblocktime[MAXBLOCKHEIGHT];

   double blkfreq[MAXBLOCKHEIGHT][MAXBLOCKWIDTH];
   double *blockfreq[MAXBLOCKHEIGHT];
   double embedimageblkfreq[MAXBLOCKHEIGHT][MAXBLOCKWIDTH];
   double *embedimageblockfreq[MAXBLOCKHEIGHT];
   char temp[256];

   HGLOBAL hMem;
   hMem=NULL;
   hMem=GlobalAlloc(GMEM_FIXED,2*blocks*sizeof(double)*64+2*blocks*sizeof(int)*64+2*blocks*sizeof(unsigned char)*64);

   //hMem=GlobalAlloc(GMEM_FIXED,2*blocks*sizeof(double)+2*blocks*sizeof(int)+128*128+2*blocks*sizeof(unsigned char));
   if(hMem==NULL)
   {
	  sprintf(temp,"对不起,内存不够%fMBytes,无法进行编码!",(double)(MAXIMAGESIZE)/1024.0/1024.0);
	  MessageBox(NULL,temp,"编码",MB_ICONSTOP|MB_OK);
	  return -1;
	}
   //Alloc the memory
   imageblockvarseqkey=(int *) hMem;
   wmblockmarknumseqkey=imageblockvarseqkey+blocks*sizeof(int);

   for(l=0;l<MAXBLOCKHEIGHT;l++)
   {
	   blocktime[l]=blktime[l];

⌨️ 快捷键说明

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