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

📄 wvlttrans.cpp

📁 一种图像水印加密算法 该算法抗攻击性强 安全性高 可以直接运行
💻 CPP
📖 第 1 页 / 共 5 页
字号:
					
				}
				if(abs(LH2[i][j])>Threshold && count<MarkLength)
				{
					if(count == MarkLength)
						break;
					count++;
					CoeffChoosed[i][j] = LH2[i][j];//将此值保存到选定系数数组中
					//将此位置坐标二值对保存
					//pic.LoadPlace[count].first = i;
					//pic.LoadPlace[count].second = j;
					
					
				}
				if(abs(HL2[i][j])>Threshold && count<MarkLength)
				{
					if(count == MarkLength)
						break;
					count++;
					CoeffChoosed[i][j] = HL2[i][j];//将此值保存到选定系数数组中
					//将此位置坐标二值对保存
					//pic.LoadPlace[count].first = i;
					//pic.LoadPlace[count].second = j;
					
					
				}
				if(abs(HH2[i][j])>Threshold && count<MarkLength)
				{
					if(count == MarkLength)
						break;
					count++;
					CoeffChoosed[i][j] = HH2[i][j];//将此值保存到选定系数数组中
					//将此位置坐标二值对保存
					//pic.LoadPlace[count].first = i;
					//pic.LoadPlace[count].second = j;
					
					
				}
				if(abs(LH3[i][j])>Threshold && count<MarkLength)
				{
					if(count == MarkLength)
						break;
					count++;
					CoeffChoosed[i][j] = LH3[i][j];//将此值保存到选定系数数组中
					//将此位置坐标二值对保存
					//pic.LoadPlace[count].first = i;//这样赋值为何总错
					//pic.LoadPlace[count].second = j;
					
					
				}				
				if(abs(HL3[i][j])>Threshold && count<MarkLength)
				{
					if(count == MarkLength)
						break;
					count++;
					CoeffChoosed[i][j] = HL3[i][j];//将此值保存到选定系数数组中
					//将此位置坐标二值对保存
					//pic.LoadPlace[count].first = i;//此处有错10.13
					//pic.LoadPlace[count].second = j;					
					
				}				
				if(abs(HH3[i][j])>Threshold && count<MarkLength)//为何子带的值总为大多0
				{	
					if(count == MarkLength)
						break;
					count++;
					CoeffChoosed[i][j] = HH3[i][j];//将此值保存到选定系数数组中
					//将此位置坐标二值对保存
					//pic.LoadPlace[count].first = i;
					//pic.LoadPlace[count].second = j;					
				
				}
            
			}//end for

		}//end for
		if(count < MarkLength)
		{
			Threshold = 0.5*Threshold;//修改阈值
		}		

	}//end while
	//看看选定的系数个数是否为MarkLength个
	/*
	int n = 0;
	for(i=0; i<biHeight; i++)
	{
		for(j=0; j<biWidth; j++)
		{
			if (CoeffChoosed[i][j] != 0)
				n++;
		}
	}
	*/

}

//三个数由大小排序,结果保存到result[]中
void CWvltTrans::sort(float &a,float &b,float &c,float *sortresult)
{
	float temp;//临时变量
	//short *result=new short[3];
	if(a<b)
	{
		if(b<c)//此时c最大,a最小
		{
			temp = a;
			a = c;
			c = temp;
		}
		else//b>c,此时还需要比较a,c
		{
			if(a<c)//此时需a先跟c交换,b再跟a交换
			{
				temp = a;
				a = c;
				c = temp;

				temp = a;
				a = b;
				b = temp;
			}
			else//此时c<a<b,需a和b交换
			{
				temp = a;
				a = b;
				b = temp;

			}
		}
	}
	else//a>b
	{
		if(a<c)//c>a>b,此时需a先跟c交换,b再跟c交换
		{
			temp = a;
			a = c;
			c = temp;

			temp = b;
			b = c;
			c = temp;
		}
		else//a>c,此时还需要比较b,c
		{
		    if(b<c)//a>c>b,此时需b跟c交换
			{
				temp = b;
				b = c;
				c = temp;				
			}
			    
		}
	}
	sortresult[0] = a;//最大的
    sortresult[1] = b;//中间值
	sortresult[2] = c;//最小的

}

//抖动法选择系统
void CWvltTrans::ChooseCoeffByQantity()
{
	
	//allocBuffers();
	int group;//选择要加载水印的组数,每组3个系数,通过修改3个系数中的中间值来加载水印
    //先从第三层开始找HL3,LH3,HH3相应各组系数的中值,然后第二层,直到找到MarkLength个中值
    float* TempCoeff = new float[bmSize];//分配这么多空间对吗?
	//short TempCoeff[10000];
	int i,j;
	int k =0;
	//while(k<bmSize-(int)bmSize/64)//将系数变成一维数组
	while(k<bmSize)//将系数变成一维数组
	{
		//第三层
		for(i=0; i<biHeight>>3; i++)//HL3
		{
			for(j= biWidth>>3; j<biWidth>>2; j++)
			{
				if(k == bmSize)
					break;
				TempCoeff[k] = m_WvltCoeff[i][j];
                k++;
			}
		}
		for(i=biHeight>>3; i<biHeight>>2 ; i++)//LH3
		{
			for(j=0; j<biWidth>>3; j++)
			{
				if(k == bmSize)
					break;
				TempCoeff[k] = m_WvltCoeff[i][j];
                 k++;
			}
		}
		for(i=biHeight>>3; i<biHeight>>2 ; i++)//HH3
		{
			for(j=biWidth>>3; j< biWidth>>2; j++)
			{
				if(k == bmSize)
					break;
				TempCoeff[k] = m_WvltCoeff[i][j];
                 k++;
			}
		}
		//第二层
		for(i=0; i<biHeight>>2 ; i++)//HL2
		{
			for(j=biWidth>>2; j< biWidth>>1; j++)
			{
				if(k == bmSize)
					break;
				TempCoeff[k] = m_WvltCoeff[i][j];
                 k++;
			}
		}

		for(i=biHeight>>2; i<biHeight>>1 ; i++)//LH2
		{
			for(j=0; j<biWidth>>2; j++)
			{
				if(k == bmSize)
					break;
				TempCoeff[k] = m_WvltCoeff[i][j];
                k++;
			}
		}

		for(i=biHeight>>2; i<biHeight>>1 ; i++)//HH2
		{
			for(j=biWidth>>2; j< biWidth>>1; j++)
			{
				if(k == bmSize)
					break;
				TempCoeff[k] = m_WvltCoeff[i][j];
                 k++;
			}
		}
        //第一层
		for(i=0; i<biHeight>>1 ; i++)//HL1
		{
			for(j=biWidth>>1; j< biWidth; j++)
			{
				if(k == bmSize)
					break;
				TempCoeff[k] = m_WvltCoeff[i][j];
                 k++;
			}
		}

		for(i=biHeight>>1; i<biHeight ; i++)//LH1
		{
			for(j=0; j<biWidth>>1; j++)
			{
				if(k == bmSize)
					break;
				TempCoeff[k] = m_WvltCoeff[i][j];
                 k++;
			}
		}

		for(i=biHeight>>1; i<biHeight ; i++)//HH1
		{
			for(j=biWidth>>1; j< biWidth; j++)
			{
				if(k == bmSize)
					break;
				TempCoeff[k] = m_WvltCoeff[i][j];
                 k++;
			}
		}

	}//end while
	
	float *CoeffSmall = new float[MarkLength];//
    float *CoeffLarge = new float[MarkLength];
	float *CoeffMedia = new float [MarkLength];//用全局变量时有错误
	float distance ;
	int M;
	//short* CoeffEverySect = NULL;
	float CoeffEverySect[500];
	
	
	//short* result = new short[3];//为何分配时会有错误
	float result[3] = {0, 0, 0};
	
	//每三个系数为一组,取每组的中值保存到CoeffMedia中
	for(group=0; group<MarkLength; group++)
	{
        sort(TempCoeff[3*group],TempCoeff[3*group+1],TempCoeff[3*group+2],result);
        CoeffLarge[group] = result[0];
		CoeffMedia[group] = result[1];
		CoeffSmall[group] = result[2];
		distance = 0.05*(abs(CoeffLarge[group]) + abs(CoeffSmall[group]))/2.0;
		/*
		if(distance < CoeffLarge[group] - CoeffSmall[group])
		{
			CString Temp = "";
			Temp.Format("组:%d",group);
			AfxMessageBox(Temp + "变化太小而无法加载水印!");
			continue;
		}
		*/
		float diff = abs(CoeffLarge[group] - CoeffSmall[group]);
		if(distance != 0 && distance<diff)
		{
		    M = (int)(diff/distance);	
		    if(M != 0)
			{
				//CoeffEverySect = new short[M];//为何有错
				//将当前组分成一小段一小段
				for(int l=0; l<M; l++)
				{
					CoeffEverySect[l] = CoeffSmall[group] + l*distance;								

				}
				CoeffEverySect[M] = CoeffLarge[group];
				//通过修改每组中间值完成水印嵌入
				for(l=0; l<M; l++)
				{
					if( (CoeffMedia[group]>CoeffEverySect[l]||CoeffMedia[group]==CoeffEverySect[l]) &&
					   (CoeffMedia[group]<CoeffEverySect[l+1]||CoeffMedia[group]==CoeffEverySect[l+1]))
					{
						if( ((l%2 ==0)&&(WaterMarkForLoad[group]==1)) ||((l%2 !=0)&&(WaterMarkForLoad[group]==0)) )//WaterMarkForLoad[group]的值没传进来
						{
							CoeffMedia[group] = CoeffEverySect[l];

						}
						if( ((l%2 !=0)&&(WaterMarkForLoad[group]==1))||((l%2 ==0)&&(WaterMarkForLoad[group]==0)) )
						{
							CoeffMedia[group] = CoeffEverySect[l-1];

						}
					}
				}//end for
                /*
				if(CoeffEverySect != NULL)
				{
					delete CoeffEverySect;
					CoeffEverySect = NULL;
				}
				*/
			}
		}	
		

	}//end for
	
	
	//用修改后的中间值更新对应的小波变换后系数
	group = 0;
	/*
	if(CoeffAfterLoad == NULL)
	{
		CoeffAfterLoad = new short* [biHeight];//存放加载水印后的系数
		for(i = 0; i < biHeight; i ++)
		{
			if(CoeffAfterLoad[i] == NULL)
			{
			    CoeffAfterLoad[i] = new short [biWidth];
			}

		}
	}
	*/
	
	while(group < MarkLength)
	{
		for(i=0; i<biHeight>>3; i++)//HL3
		{
			for(j= biWidth>>3; j<biWidth>>2; j++)
			{
                CoeffAfterLoad[i][j] = m_WvltCoeff[i][j];
				if(group == MarkLength)
						break;
				//这样对应可以吗
				sort(TempCoeff[3*group],TempCoeff[3*group+1],TempCoeff[3*group+2],result);
				if(m_WvltCoeff[i][j] == result[1])
				{
					CoeffAfterLoad[i][j] = CoeffMedia[group];//CoeffAfterLoad[i][j]中保存加载水印后的系数
					group++;
					
				}
			}
		}
		for(i=biHeight>>3; i<biHeight>>2 ; i++)//LH3
		{
			for(j=0; j<biWidth>>3; j++)
			{
				CoeffAfterLoad[i][j] = m_WvltCoeff[i][j];
				if(group == MarkLength)
						break;
				//这样对应可以吗
				sort(TempCoeff[3*group],TempCoeff[3*group+1],TempCoeff[3*group+2],result);
				if(m_WvltCoeff[i][j] == result[1])
				{
					CoeffAfterLoad[i][j] = CoeffMedia[group];
					group++;
					
				}
			}
		}
		for(i=biHeight>>3; i<biHeight>>2 ; i++)//HH3
		{
			for(j=biWidth>>3; j< biWidth>>2; j++)
			{
				CoeffAfterLoad[i][j] = m_WvltCoeff[i][j];
				if(group == MarkLength)
						break;
				//这样对应可以吗
				sort(TempCoeff[3*group],TempCoeff[3*group+1],TempCoeff[3*group+2],result);
				if(m_WvltCoeff[i][j] == result[1])
				{
					CoeffAfterLoad[i][j] = CoeffMedia[group];
					group++;
					
				}
			}
		}
		//第二层
		for(i=0; i<biHeight>>2 ; i++)//HL2
		{
			for(j=biWidth>>2; j< biWidth>>1; j++)
			{
				CoeffAfterLoad[i][j] = m_WvltCoeff[i][j];
				if(group == MarkLength)
						break

⌨️ 快捷键说明

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