www.pudn.com > src.rar > qatlig.c


/*---------------------------------------------------------------------------
*    qatlig transformation
* ---------------------------------------------------------------------------*/
#include 
#include "bmp.h" 

#define MAXPATHLEN 64
static char directory_name[MAXPATHLEN];
static char file_name[MAXPATHLEN];
static char pathname[MAXPATHLEN]; 
static void *pdir=0, *pfile=0;

static int SCRAM_n = 1;

static Tv *tv = NULL;
static Tv *tv1=NULL;

/*---------------------------------------------------------------------------
     To generate the scrambling mapping-matrix of qatlig transformation.
 ---------------------------------------------------------------------------*/
void qatlig_matrix(int** r, int** c, int height, int width) 
{ 
    int i, j, x1, y1, x2, y2, x3, y3, x, y; 
/* 
	double a_QATLIG = 9.14849113758545604e+002; 
	double b_QATLIG = 1.13322919891824768e+004; 
	double c_QATLIG = 5.17813914782433339e+003; 
	double d_QATLIG = 8.11207945415277645e+004; 
	double e_QATLIG = 1.16156755002857162e+002; 
	double f_QATLIG = 2.12755882099826667e+003; 
*/ 
	double a_QATLIG = 9.4849113758545604e+002; 
	double b_QATLIG = 1.3322919891824768e+004; 
	double c_QATLIG = 5.7813914782433339e+003; 
	double d_QATLIG = 8.1207945415277645e+004; 
	double e_QATLIG = 1.6156755002857162e+002; 
	double f_QATLIG = 2.2755882099826667e+003;
 
	printf("\nQatlig transform\n"); 
	for (i = 0; i < height  ; i++) 
	{ 
		for (j = 0; j< width ; j++) 
		{ 
			x1 = (i+(int)(((double)j)*(d_QATLIG-1)/c_QATLIG+0.5))%height; 
			y1 = j; 
 
			x2 = x1; 
			y2 = (y1+(int)(((double)x1)*c_QATLIG+0.5))%width; 
 
			x3 = (x2+(int)((a_QATLIG-1.0)*((double)y2)/c_QATLIG+0.5))%height; 
			y3 = y2; 
 
			x = (x3+(int)(e_QATLIG+0.5))%height; 
			y = (y3+(int)(f_QATLIG+0.5))%width; 
 
			r[i][j] = x; 
			c[i][j] = y; 
			/*r1[i][j] = x; 
			c1[i][j] = y;*/ 
		} 
	} 
}
 /*---------------------------------------------------------------------------
  To generate the inverse scrambling mapping-matrix of qatlig transformation.
 ---------------------------------------------------------------------------*/
void r_qatlig_matrix(int** src_r,int** src_c, int height, int width)
{
	int i,j,r,c;
	int **trans_r, **trans_c; 
	trans_r = int_matrix(height, width); 
	trans_c = int_matrix(height, width);
	qatlig_matrix(trans_r, trans_c, height, width);
	printf("\nR_Qatlig transform\n");
	for(i = 0; i < height; i++) 
	{ 
		for(j = 0; j < width; j++) 
		{ 
			r = trans_r[i][j]; 
			c = trans_c[i][j]; 
			src_r[r][c] = i; 
			src_c[r][c] = j; 
		} 
	}
	free_int_matrix(trans_r, height); 
	free_int_matrix(trans_c, height);
}

static 	Imrect *read_image(void)
{
	Imrect *srcIm;
/*----------------Read the image---------------------------*/
  	(void) strip_spaces(file_name);
  	(void) strip_spaces(directory_name);
  	(void) string_append(pathname, directory_name, "/", file_name, NULL);
	srcIm = ReadGIF(pathname, 1);
/*------------------------------------------------*/
	return(srcIm);
}


 /*---------------------------------------------------------------------------
     qatlig transformation
 ---------------------------------------------------------------------------*/
void Qatlig_Trans(int nTrans) 
{ 
	Imrect *srcIm,*destIm,*tmpIm; 
	int width, height; 
	Imregion *roi;
	int i,j,k;
	int type; 
	BYTE pix;
/*
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("Qatlig : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);
*/
	srcIm = read_image();
	//srcIm = DrawRGB_proc(); 
	width = srcIm->width; 
    	height = srcIm->height; 
	if (width != height) 
	{ 
		error("Qatlig:width != height", warning); 
		return; 
	} 
 
	roi = srcIm->region; 
	if (roi == NULL) return ;

/*------------------------------------------------*/
	int **trans_r, **trans_c; 
	trans_r = int_matrix(height, width); 
	trans_c = int_matrix(height, width);
	qatlig_matrix(trans_r, trans_c, height, width);
/*------------------------------------------------*/ 
	printf("\n\n=== Qatlig Transform begin: ===\n");

	destIm = im_alloc( height, width, roi, int_v );

	int r,c; 
	for( k = 0; k < SCRAM_n; k++) 
	{	
		for (i = 0; i < height  ; i++) 
		{ 
			for (j = 0; j< width ; j++) 
			{				
				IM_PIX_GET(srcIm, i, j, pix); 
				IM_PIX_SET(destIm, trans_r[i][j], trans_c[i][j], pix); 
			} 
		} 
 		tmpIm = srcIm; 
		srcIm = destIm; 
		destIm = tmpIm;
	}
/*----------------------Save the image as .gif--------------------------
	char filename[64];
	char string[25];
	gcvt(k+1,5,string);
	printf("string=%s\n",string);
	char *filename0 ="result/Qatlig/";
	strcpy(filename,filename0);
	//strcat(filename,file_name);
	strcat(filename,"Lena_Qatlig_");
	strcat(filename, string);
	//strcat(filename,".gif");
	//gif_write_file(srcIm,filename);
	//gif_write_file_color(srcIm,filename);
	strcat(filename,".bmp");
	ImrectToBMP8(srcIm,filename);
}
/*------------------------------------------------*/
	tv_imrect2(tv1, srcIm);

	im_free(destIm);
	free_int_matrix(trans_r, height); 
	free_int_matrix(trans_c, height);
	printf("=== Qatlig transform complete! ===\n"); 
} 
/*---------------------------------------------------------------------------
    inverse qatlig transformation
 ---------------------------------------------------------------------------*/
void R_Qatlig_Trans(int nTrans) 
{ 
	Imrect *srcIm,*destIm,*tmpIm; 
	int width, height; 
	Imregion *roi;
	int i,j,k;
	int type; 
	BYTE pix;
/*
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("Qatlig : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);
*/
	srcIm = read_image(); 
	width = srcIm->width; 
    	height = srcIm->height; 
	if (width != height) 
	{ 
		error("R_Qatlig:width != height", warning); 
		return; 
	} 
 
	roi = srcIm->region; 
	if (roi == NULL) return ; 

/*------------------------------------------------*/
	int **trans_r, **trans_c; 
	trans_r = int_matrix(height, width); 
	trans_c = int_matrix(height, width);
	r_qatlig_matrix(trans_r, trans_c, height, width);
/*------------------------------------------------*/ 
	printf("\n\n=== R_Qatlig Transform begin: ===\n");

	destIm = im_alloc( height, width, roi, int_v );

	int r,c; 
	for( k = 0; k < SCRAM_n; k++) 
	{	
		for (i = 0; i < height  ; i++) 
		{ 
			for (j = 0; j< width ; j++) 
			{				
				IM_PIX_GET(srcIm, i, j, pix); 
				IM_PIX_SET(destIm, trans_r[i][j], trans_c[i][j], pix); 
			} 
		} 
 		tmpIm = srcIm; 
		srcIm = destIm; 
		destIm = tmpIm;
	}
/*----------------------Save the image as .gif--------------------------*/
	char filename[64];
	char string[25];
	gcvt(k,5,string);
	printf("string=%s\n",string);
	char *filename0 ="result/Qatlig/";
	strcpy(filename,filename0);
	strcat(filename,file_name);
	strcat(filename,"+R_Qatlig_");
	strcat(filename, string);
	strcat(filename,".gif");
	gif_write_file(srcIm,filename);
/*------------------------------------------------*/
	tv_imrect2(tv1, srcIm);

	im_free(destIm);
	free_int_matrix(trans_r, height); 
	free_int_matrix(trans_c, height);
	printf("=== R_Qatlig transform complete! ===\n"); 
}

static void tv_choice_proc(choice) 
int     choice; 
{ 
    switch (choice) 
    	{ 
    	case 0: tv_set_next(tv); 
		break; 
    	case 1: tv_set_next(tv1); 
		break; 
    	default:error("tv_choice_proc: unknown choice\n", warning); 
		break; 
    	}
}

static void scan_proc(void)
{
    scan_files(directory_name,file_name);
    tw_sglobal_reset(pdir);
    tw_sglobal_reset(pfile);
}



void            QATLIG_tool(int x, int y) 
{ 
	static void *tool = NULL;
 
	static void tv_choice_proc();
	if (tool) 
	{ 
		tw_show_tool(tool); 
		return; 
	} 
	tool = (void *)tw_tool("Qatlig Scrambling tool", x, y);
    	/* Initialise pathname from environment variable (or #define) */
    	(void) environ_pathname_get(directory_name, file_name,
				"TINA_IMAGE_DEFAULT",
				"/home/wzz/newimscramdegree/result/Qatlig/Lena.gif");

	tv = tv_create("Qatlig original"); 
     	tv1 = tv_create("Qatlig scrambled");
         tw_choice("Tv choice", tv_choice_proc, 0, "original","scrambled", NULL);
	tw_newrow(); 
	tw_newrow();

	/*---------------------------*/
  	pdir = (void *) tw_sglobal("Directory:", directory_name, 32);
    	tw_button("scan", scan_proc, NULL);
  	tw_newrow();
  	pfile = (void*) tw_sglobal("File:", file_name, 32);
  	tw_newrow();
	/*---------------------------*/
	tw_iglobal("Scrambling times = ", &SCRAM_n, 5); 
	tw_newrow(); 
	tw_button("Qatlig", Qatlig_Trans,NULL);
	tw_button("R_Qatlig", R_Qatlig_Trans,NULL);
	tw_newrow();
	tw_newrow(); 
	tw_end_tool(); 
}