www.pudn.com > Watermarking.rar > E_BLIND.cpp


#include "stdio.h" 
#include "stdlib.h" 
#include "math.h" 
 
void E_BLIND( unsigned char *c, int width, int height,int m, double alpha, double *wr ); 
int D_LC( unsigned char *c, int width, int height, double tlc, double *wr ); 
void ModulateOneBit( int m, double *wr, double *wm, int width, int height ); 
void AddScaledPattern( unsigned char *c, int width, int height, double alpha, double *w ); 
double ImgPatInnerProduct( unsigned char *c, double *w, int width, int height ); 
int ClipRound( double v ); 
 
#define MAX_IMG_SIZE 65536 
#define NO_WMK 3 
#define embed_strength 0.8 
#define detect_threshold 0.7 
//#define max_x 256 
//#define max_y 256 
 
void main() 
{ 
	unsigned char c[MAX_IMG_SIZE]; 
//	c=NULL; 
	double wr[MAX_IMG_SIZE]; 
//	wr=NULL; 
	 
	FILE *in; 
	FILE *out; 
	//scanf("%d %d", &max_x, &max_y); 
	 
	//open source file 
	if((in=fopen("lenna256.raw","rb"))==NULL) 
	{	 
		printf("Cannot open source file\n"); 
		exit(-1); 
	} 
    printf("source file open OK!!\n"); 
	 
	//read source file 
	fread(c, 1, MAX_IMG_SIZE, in); 
    printf("file read OK!!\n");	 
	 
///////////////////////////////////// 
	//reference pattern 
	int i=0; 
	for(i=0;i tlc ) 
    m = 1; 
  else if( lc < -tlc ) 
    m = 0; 
  else 
    m = NO_WMK; 
 
  return m; 
} 
 
/*--------------------------------------------------------------------------* 
 | ModulateOneBit -- encode a one-bit message by either copying or negating | 
 |                   a given reference pattern                              | 
 |                                                                          | 
 | Arguments:                                                               | 
 |   m -- message to be encoded                                             | 
 |   wr -- reference pattern                                                | 
 |   wm -- where to store resulting message pattern                         | 
 |   width -- width of wm                                                   | 
 |   height -- height of wm                                                 | 
 |                                                                          | 
 | Return value:                                                            | 
 |   none                                                                   | 
 *--------------------------------------------------------------------------*/ 
 
void ModulateOneBit( int m, double *wr, double *wm, int width, int height ) 
{ 
  int i;                               /* index into patterns */ 
 
  if( m == 0 ) 
  {	 
	//printf("m=0\n"); 
    for( i = 0; i < width * height; i = i + 1 ) 
      wm[ i ] = -wr[ i ]; 
  } 
  else 
  {	 
	//printf("m=1\n"); 
    for( i = 0; i < width * height; i = i + 1 ) 
      wm[ i ] = wr[ i ]; 
  } 
} 
 
/*--------------------------------------------------------------------------* 
 | AddScaledPattern -- scale and add a pattern to an image with clipping    | 
 |                     and rounding                                         | 
 |                                                                          | 
 | This multiplies w by alpha to obtain the added pattern, and adds         | 
 | it to c, clipping and rounding each pixel to an 8-bit integer.           | 
 |                                                                          | 
 | Arguments:                                                               | 
 |   c -- image to which to add pattern (changed in place)                  | 
 |   width -- width of image                                                | 
 |   height -- height of image                                              | 
 |   alpha -- scaling factor                                                | 
 |   w -- pattern to scale and add (width times height array of doubles)    | 
 |                                                                          | 
 | Return value:                                                            | 
 |   none                                                                   | 
 *--------------------------------------------------------------------------*/ 
 
void AddScaledPattern( unsigned char *c, int width, int height, 
                       double alpha, double *w ) 
{ 
  int i;                               /* pixel index */ 
 
  for( i = 0; i < width * height; i = i + 1 ) 
    c[ i ] = ClipRound( (double)c[ i ] + alpha * w[ i ] ); 
 
  	////////////////////////////////////////////////// 
    FILE *out; 
 
    if((out=fopen("watermark.raw","wb"))==NULL) 
	{ 
			printf("Cannot open source file\n"); 
			exit(-1); 
	}    
	printf("target file open OK!!\n"); 
 
	fwrite(c,1,MAX_IMG_SIZE,out); 
	printf("write suceess!!\n"); 
 
} 
 
/*--------------------------------------------------------------------------* 
 | ImgPatInnerProduct -- get the inner product of an image and a pattern    | 
 |                                                                          | 
 | Arguments:                                                               | 
 |   c -- image                                                             | 
 |   w -- pattern                                                           | 
 |   width -- width of both patterns                                        | 
 |   height -- height of both patterns                                      | 
 |                                                                          | 
 | Return value:                                                            | 
 |   inner product of c and w                                               | 
 *--------------------------------------------------------------------------*/ 
 
double ImgPatInnerProduct( unsigned char *c, double *w, 
                           int width, int height ) 
{ 
  double product;                      /* inner product of c and w */ 
  int i;                               /* index into patterns */ 
 
  product = 0; 
  for( i = 0; i < width * height; i = i + 1 ) 
    product = product + c[ i ] * w[ i ]; 
 
  return product; 
} 
 
/*--------------------------------------------------------------------------* 
 | ClipRound -- clip and round a real value to an 8-bit integer             | 
 |                                                                          | 
 | Arguments:                                                               | 
 |   v -- real value                                                        | 
 |                                                                          | 
 | Return value:                                                            | 
 |   clipped and rounded value                                              | 
 *--------------------------------------------------------------------------*/ 
 
int ClipRound( double v ) 
{ 
  int iv;                              /* clipped and rounded value */ 
 
  if( v < 0 ) 
    iv = 0; 
  else if( v > 255 ) 
    iv = 255; 
  else 
    iv = (int)floor( v + .5 ); 
 
  return iv; 
}