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


/******************************************************************** 
 * 
 * image cut tool 
 * 
 ********************************************************************/
#include 
#include "bmp.h"
#include 
#include 
static int SCRAM_n = 1;
static int START_h = 0;
static int START_w = 0;
static int ORIG_height = 256;
static int ORIG_width = 256;
static int MCOUNT = 1;

static Tv *tv = NULL;
static Tv *tv1 = NULL;
static Tv *tv2 = NULL;
#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_method = 0;
static int cut_method = 0;
static int repair_method = 0;
static int image_choice = 0;
static int show_choice = 0;
//#define CUTPIX 254.56789
#define CUTPIX 255
static char filename[MAXPATHLEN];
static Imrect *orig_im =NULL;
static Imrect *contra_scram_im = NULL;
static Imrect *repair_im = NULL;

static int iminarg1,iminarg2; 
#define IMAX(a,b) (iminarg1 = (a),iminarg2 = (b),(iminarg1) > (iminarg2) ? (iminarg1) : (iminarg2))

static Imrect *adaptim = NULL;

extern void nhwfm_proc(void);

static void scram_choice_proc(int val) 
{ 
	scram_method = val; 
}
static void cut_choice_proc(int val) 
{ 
	cut_method = val; 
}
static void repair_choice_proc(int val)
{
	repair_method = val;
}
static image_choice_proc(int val)
{
	image_choice = val;
}
static show_choice_proc(int val)
{
	show_choice = val;
}
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);
	switch( image_choice )
	{
		case 0:
			srcIm = BmpToImrect(pathname);
			break;
		case 1:
			srcIm = ReadGIF(pathname, 1);
			break;
		default:
			break;
	}
/*------------------------------------------------*/
	return(srcIm);
}
static void mypush_proc(void)
{
	Imrect *srcIm,*srcIm0;
	int width, height; 
	Imregion *roi;
	int i,j;
	float pix;
	srcIm = read_image();
	width = srcIm->width; 
    	height = srcIm->height; 
	roi = srcIm->region; 
	if (roi == NULL) return ;
	orig_im = im_copy(srcIm);
	tv_imrect2(tv, orig_im);
	srcIm0 = im_alloc( height, width, roi, float_v );
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j++)
		{
			IM_PIX_GET(srcIm, i, j, pix); 
			IM_PIX_SET(srcIm0, i, j, pix);
		}
	mono_image_set(srcIm0); 
	stack_push(srcIm0, IMRECT, im_free);
}
static Imrect* mypop_proc(void)
{
	Imrect *srcIm;
	float pix;
	int type;
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("receive-client : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);
	return srcIm;
}
static void mysave_proc(void)
{
	int type;
	int width, height; 
	Imregion *roi;
	Imrect *srcIm,*srcIm0;
	int i,j;
	float pix;
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("receive-client : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);
	width = srcIm->width; 
    	height = srcIm->height; 
	roi = srcIm->region; 
	if (roi == NULL) return ;
	srcIm0 = im_alloc( height, width, roi, float_v );
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j++)
		{
			IM_PIX_GET(srcIm, i, j, pix); 
			IM_PIX_SET(srcIm0, i, j, pix);
		}
	//strcat(filename,".gif");
	//gif_write_file(srcIm0,filename);
	char filenamesave[64];
	strcpy(filenamesave,filename);
	strcat(filenamesave,".bmp");
	ImrectToBMP8(srcIm0,filenamesave);
	printf("saved the image :%s\n",filenamesave);

	mono_image_set(srcIm0); 
	stack_push(srcIm0, IMRECT, im_free);
}

static void reset_proc(void)
{
	strcpy(filename,"result/receive/");
	//strcat(filename,file_name);
	mypop_proc();
}
static void myshow_proc(void)
{
	Imrect *srcIm,*srcIm0;
	int type;
	int width, height; 
	Imregion *roi;                                         
	int i,j;
	float pix;
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("receive show : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);
	width = srcIm->width; 
    	height = srcIm->height; 
	roi = srcIm->region; 
	if (roi == NULL) return ;
	srcIm0 = im_alloc( height, width, roi, float_v );
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j++)
		{
			IM_PIX_GET(srcIm, i, j, pix); 
			IM_PIX_SET(srcIm0, i, j, pix);
		}
	orig_im = im_copy(srcIm);
	tv_imrect2(tv, orig_im);
	mono_image_set(srcIm0); 
	stack_push(srcIm0, IMRECT, im_free);
}
static void rebuild_proc(void)
{
	Imrect *srcIm,*rebuIm;
	int type;
	int width, height; 
	Imregion *roi;
	int i,j;
	float pix;
	int start_h,start_w;
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("receive-client : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);
	width = srcIm->width; 
    	height = srcIm->height; 
	roi = srcIm->region; 
	if (roi == NULL) return ;
	if((START_h+height>ORIG_height)||(START_w+width>ORIG_width))
	{
		printf("rebuild: out of the region!\n");
		return ;
	}
	rebuIm = im_alloc( ORIG_height, ORIG_width, NULL, float_v );//
	for(i = 0;i < ORIG_height;i ++)
		for(j = 0;j < ORIG_width;j ++)
			IM_PIX_SET(rebuIm, i, j, CUTPIX);
	strcpy(filename,"result/receive/");
	//strcat(filename,file_name);
	char filename0[64];
	strcpy(filename0,"rebuild_");
	char string[25];
/*------------------------------------------------*/
    	switch( cut_method ) 
    	{ 
    		case 0:
			start_h = 0;
			start_w = ORIG_width/4;
			strcat(filename0, "25%_"); 
			break; 
    		case 1: 
			start_h = ORIG_height/2;
			start_w = 0;
			strcat(filename0, "50%_"); 
			break; 
    		case 2: 
			start_h = 0;
			start_w = 0;
			strcat(filename0, "75%-L_up_"); 
			break; 
    		case 3: 
			start_h = 0;
			start_w = ORIG_width/2;
			strcat(filename0, "75%-R_up_");; 
			break; 
		case 4: 
			start_h = ORIG_height/2;
			start_w = 0;
			strcat(filename0, "75%-L_down_"); 
			break; 
    		case 5:
			start_h = ORIG_height/2;
			start_w = ORIG_width/2;
			strcat(filename0, "75%-R_down_"); 
			break; 
		case 6:
			start_h = START_h ;
			start_w = START_w ;
			gcvt(START_h,5,string);
			strcat(filename0, string);
			strcat(filename0, "_");
			gcvt(START_w,5,string);
			strcat(filename0, string);
			strcat(filename0, "_"); 
			break; 
		default: 
			break; 
    }
/*------------------------------------------------*/
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j++)
		{
			IM_PIX_GET(srcIm, i, j, pix); 
			IM_PIX_SET(rebuIm, i+start_h, j+start_w, pix);
		}
	orig_im = im_copy(rebuIm);
	tv_imrect2(tv, orig_im);
	strcat(filename, filename0);
	mono_image_set(rebuIm); 
	stack_push(rebuIm, IMRECT, im_free);
	printf("rebuild complete!\n");
}

static void Contra_Trans_proc(void)
{
	Imrect *srcIm,*destIm,*tmpIm,*srcIm0; 
	int width, height; 
	Imregion *roi;
	int i,j,k;
	int type; 
	float pix;

	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("Contra_Trans : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);
 
	width = srcIm->width; 
    	height = srcIm->height; 
	roi = srcIm->region; 
	if (roi == NULL) return ;
	orig_im = im_copy(srcIm);
	tv_imrect2(tv, orig_im);
	char filename0[64];
	char string[25];
	strcpy(filename0,"_C_");
/*------------------------------------------------*/
	int **trans_r, **trans_c; 
	trans_r = int_matrix(height, width); 
	trans_c = int_matrix(height, width);
    	switch( scram_method ) 
    	{ 
    		case 0: 
    			r_fibonacci_matrix(trans_r, trans_c, height, width);;
			strcat(filename0, "f"); 
			break; 
    		case 1: 
    			r_arnold_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "a"); 
			break; 
    		case 2: 
    			r_qatlig_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "q"); 
			break; 
    		case 3: 
			r_hilbert_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "h"); 
			break; 
		case 4: 
			r_convey_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "c"); 
			break; 
    		case 5: 
			r_latin_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "la"); 
			break; 
		case 6:
/* 
    			r_graycode_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "g");
*/
			r_zigzag_matrix(trans_r, trans_c, height, width);
			strcat(filename0,"z"); 
			break; 
		case 7: 
    			r_baker_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "b"); 
			break; 
		case 8: 
    			r_logistic_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "lo"); 
			break; 
		case 9:
/* 
			r_sampling_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "sa");
*/
			r_random_matrix(trans_r, trans_c, height, width);
			strcat(filename0, "ra_");
 
			break; 
		default: 
			break; 
    }
/*------------------------------------------------*/
 
	printf("\n=== %sTransformation begin: ===\n",filename0);

	destIm = im_alloc( height, width, roi, float_v );
	srcIm0 = im_alloc( height, width, roi, float_v );
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j++)
		{
			IM_PIX_GET(srcIm, i, j, pix); 
			IM_PIX_SET(srcIm0, i, j, pix);
		} 
	for( k = 0; k < SCRAM_n; k++) 
	{	
		for (i = 0; i < height  ; i++) 
		{ 
			for (j = 0; j< width ; j++) 
			{				
				IM_PIX_GET(srcIm0, i, j, pix); 
				IM_PIX_SET(destIm, trans_r[i][j], trans_c[i][j], pix); 
			} 
		} 
 		tmpIm = srcIm0; 
		srcIm0 = destIm; 
		destIm = tmpIm;
	}
/*----------------------Save the image as .bmp--------------------------*/

	gcvt(k,5,string);
	printf("Contra_scrambled %s times\n",string);
	strcat(filename0, string);
	strcat(filename, filename0);
	char filename1[64];
	strcpy(filename1,filename);
	strcat(filename1,".bmp");
	ImrectToBMP8(srcIm0,filename1);
/*------------------test------------------------------
	Imrect *crackim;
	crackim = im_alloc(128, 128, NULL, float_v );
	int index = 0;
	for(i=0;i<256;i++)
	{
		for(j=0;j<256;j++)
		{
			IM_PIX_GET(srcIm0, i, j, pix);
			if(fabs(pix - 254.56789) > 0.00001)
			{
				IM_PIX_SET(crackim, index/128, index%128, pix);
				index++;
				printf(" (%d,%d)%d ",i,j,index);
				if(index==128*128)
					break;
			}

		}
				if(index==128*128)
					break;
	}
	ImrectToBMP8(crackim,"crackim.bmp");
	im_free(crackim);
/*------------------------------------------------*/
	contra_scram_im = im_copy(srcIm0);
	tv_imrect2(tv1, contra_scram_im);
	mono_image_set(srcIm0); 
	stack_push(srcIm0, IMRECT, im_free);
	im_free(destIm);
	free_int_matrix(trans_r, height); 
	free_int_matrix(trans_c, height);
	printf("=== %stransformatioin complete! ===\n",filename);
}

static void repair_proc(void)
{
	Imrect *srcIm,*destIm,*srcIm0;
 
	int width, height; 
	Imregion *roi;
	int i,j;
	int type;
	float pix;
	float pixa,pixb;
	//char filename_in[64];
	//strcpy(filename_in,filename);
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("mean_repair : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type); 
	width = srcIm->width; 
    	height = srcIm->height; 
	roi = srcIm->region; 
	if (roi == NULL) return ;
	Imrect *comp_orig = NULL,*dest_a_b = NULL,*dest_c = NULL;
	//Lenaorig = BmpToImrect("/home/wzz/newimscramdegree/result/receive/Lena.bmp");
	//Lenaorig = ReadGIF("/home/wzz/newimscramdegree/result/receive/Lena.gif",1);
	//comp_orig = BmpToImrect("/home/wzz/newimscramdegree/result/g256.bmp");
	comp_orig = ReadGIF("/home/wzz/newimscramdegree/result/Lena.gif",1);
	//comp_orig = ReadGIF("/home/wzz/newimscramdegree/result/newscramdegree/Lena_512x512.gif",1);

	if (comp_orig == NULL)
	{
		printf("read image error.");
		return;
	}
	destIm = im_alloc( height, width, roi, float_v );
	srcIm0 = im_alloc( height, width, roi, float_v );
	dest_a_b = im_alloc( height, width, roi, float_v );
	dest_c = im_alloc( height, width, roi, float_v );
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j++)
		{
			IM_PIX_GET(srcIm, i, j, pix);
			IM_PIX_SET(srcIm0, i, j, pix);
		
		}

	printf("start repair!\n");
	char filename0[64];
	char string[25];
	//strcpy(filename0,"_repair");
	//float cutpix = 	254.56789;
        float cutpix = 	255;
	int adapt =1;
	int m_count =0;
	float gray_sum = 0.0;
	float pixm,pixm1,pixm2,pixm3;
	int t,h;
	int count_adapt[25];
	const float f = 0.00001;
	float *neighbor,*togetmed;
	int n = 0;
	int r;
	int **flag;
	int w,ws = 0;
	int lp = 0;
	int pol;
	int hn[256];
	float ho[256];
	float hws = 0.0;
	for(i = 0;i < 25; i ++)
		count_adapt[i] = 0;
	flag = int_matrix(height, width);
	for(i = 0;i < 256; i ++)
		hn[i] = 0;
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j ++)
		{
			pix = im_get_pixf(srcIm0,i,j);
			if(fabs(pix - cutpix) < f )
			{
				flag[i][j] = 0;
				lp ++;
			}
			else 
			{
				hn[(int)pix]++;
				flag[i][j] = 1;
			}
		}
	pol = (int)((float)lp/(float)(height*width)*100+0.5);
	printf("propability of lost pexel : %f  \n",(float)pol/100);
	for(i = 0;i < 256; i ++)
	{
		ho[i] = (float)hn[i]/((1 - (float)pol/100)*height*width);
	}
/*-------------------------------------------------------------------------*/
	Imrect *adaptim;
	int v1,v2;
	Tv *tvo=NULL;
    	void *displayo; 
    	displayo=(void *)display_tool(20,20,256,256); 
    	tvo=(Tv *)tv_create("show adapt"); 
    	tv_install(tvo, displayo);
    	adaptim = im_alloc( 256, 256, NULL, int_v ); 
    	//tv_set_fulldraw(tvo, fulldrawadapt); 
   	//tv_repaint(tvo);
/*
   	Ipos pos1;
	tv_set_color(tvo, green);
	for(i = 0;i < 256;i ++)
		for(j = 0;j < 256;j ++)
		{
			pos1=ipos(i,j);
			tv_point(tvo,pos1);	
		}
*/
	for(i = 0;i < 256;i ++)
		for(j = 0;j < 256;j ++)
		{
			IM_PIX_SET(adaptim, i, j, 255);	
		}
/*-------------------------------------------------------------------------*/
   	switch( repair_method ) 
    	{ 
    		case 0:
/*--------------------------------mean-------------------------------------*/
	printf("mean\n");
	strcpy(filename0,"_mean");
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j ++)
		{
			pix = im_get_pixf(srcIm0,i,j);
			adapt = 1;
			//if(fabs(pix - cutpix) < f )//lost point
			if(flag[i][j]==0)
			{
				m_count = 0;
				gray_sum = 0.0;
				while(m_count < MCOUNT )//at least have MCOUNT points in the mask
				{
					m_count = 0;
					gray_sum = 0.0;
					for(t = i - adapt; t <= i + adapt; t ++)
						for(h = j - adapt; h <= j + adapt; h ++)
						{
							if((t<0)||(t>=height)||(h<0)||(h>=width))
								continue;
							pixm = im_get_pixf(srcIm0,t,h);
							//if(fabs(pixm - cutpix) > f)//orig point
							if(flag[t][h]==1)
							{
								gray_sum += pixm;
								m_count ++;
							}
						}
					if(m_count < MCOUNT)
						adapt ++;
				}
				pixm = (float)(gray_sum/m_count);//mean
				count_adapt[adapt] ++;
				im_put_pixf(pixm,destIm,i,j);
			}
			else 
			{
				im_put_pixf(pix,destIm,i,j);
				count_adapt[0] ++;
			}
		}
/*---------------------------------------------------------------------*/
 
			break; 
    		case 1:
/*------------------------------median---------------------------------------*/
	printf("median\n");
	strcpy(filename0,"_median");
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j ++)
		{
			pix = im_get_pixf(srcIm0,i,j);
			adapt = 1;
			//if(fabs(pix - cutpix) < f )//lost point
			if(flag[i][j]==0)
			{
				m_count = 0;
				gray_sum = 0.0;
				while(m_count < MCOUNT )//at least have MCOUNT points in the mask
				{
					m_count = 0;
					gray_sum = 0.0;
					n = (2*adapt+1)*(2*adapt+1);
					neighbor = fl_vector(n);
					for(t = i - adapt; t <= i + adapt; t ++)
						for(h = j - adapt; h <= j + adapt; h ++)
						{
							if((t<0)||(t>=height)||(h<0)||(h>=width))
								continue;
							pixm = im_get_pixf(srcIm0,t,h);
							//if(fabs(pixm - cutpix) > f)//orig point
							if(flag[t][h]==1)
							{
								neighbor[m_count]= pixm;
								m_count ++;
							}
						}
					if(m_count < MCOUNT)
					{
						adapt ++;
						free_fl_vector(neighbor);
					}
				}
				togetmed = fl_vector(m_count);
				for(r = 0;r < m_count;r ++)
					togetmed[r] = neighbor[r];
				free_fl_vector(neighbor);
				pixm = get_median(togetmed,m_count);//median
				count_adapt[adapt] ++;
				im_put_pixf(pixm,destIm,i,j);
				free_fl_vector(togetmed);
			}
			else 
			{
				im_put_pixf(pix,destIm,i,j);
				count_adapt[0] ++;
			}
		}
/*----------------------------------------------------------------------------*/ 
			break; 
    		case 2:
/*-------------------------------mode--------------------------------------*/ 
/* [Statistics] The value or item occurring most frequently in a series of observations or statistical data*/
	printf("mode\n");
	strcpy(filename0,"_mode");
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j ++)
		{
			pix = im_get_pixf(srcIm0,i,j);
			adapt = 1;
			//if(fabs(pix - cutpix) < f )//lost point
			if(flag[i][j]==0)
			{
				m_count = 0;
				gray_sum = 0.0;
				while(m_count < MCOUNT )//at least have MCOUNT points in the mask
				{
					m_count = 0;
					gray_sum = 0.0;
					n = (2*adapt+1)*(2*adapt+1);
					neighbor = fl_vector(n);
					for(t = i - adapt; t <= i + adapt; t ++)
						for(h = j - adapt; h <= j + adapt; h ++)
						{
							if((t<0)||(t>=height)||(h<0)||(h>=width))
								continue;
							pixm = im_get_pixf(srcIm0,t,h);
							//if(fabs(pixm - cutpix) > f)//orig point
							if(flag[t][h]==1)
							{
								neighbor[m_count]= pixm;
								m_count ++;
							}
						}
					if(m_count < MCOUNT)
					{
						adapt ++;
						free_fl_vector(neighbor);
					}
				}
				togetmed = fl_vector(m_count);
				for(r = 0;r < m_count;r ++)
					togetmed[r] = neighbor[r];
				free_fl_vector(neighbor);
				pixm = get_mode(togetmed,m_count);//mode
				count_adapt[adapt] ++;
				im_put_pixf(pixm,destIm,i,j);
				free_fl_vector(togetmed);
			}
			else 
			{
				im_put_pixf(pix,destIm,i,j);
				count_adapt[0] ++;
			}
		}
/*--------------------------------------------------------------------------*/ 
			break;

		case 3:
/*-------------------------------w-mean-------------------------------------*/
	printf("w-mean\n");
	strcpy(filename0,"_w-mean");
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j ++)
		{
			pix = im_get_pixf(srcIm0,i,j);
			adapt = 1;
			//if(fabs(pix - cutpix) < f )//lost point
			if(flag[i][j]==0)
			{
				m_count = 0;
				gray_sum = 0.0;
				ws = 0;
				while(m_count < MCOUNT )//at least have MCOUNT points in the mask
				{
					m_count = 0;
					gray_sum = 0.0;
					ws = 0;
/*-----------------------------------------------------------------------------*/
					if(show_choice==1)
					{
						getchar();
						printf("(%d,%d) adapt=%d %dx%d",i,j,adapt,2*adapt+1,2*adapt+1);
					}
/*-----------------------------------------------------------------------------*/
					for(v1 = 0;v1 < 256;v1 ++)
						for(v2 = 0;v2 < 256;v2 ++)
						{
							IM_PIX_SET(adaptim, v1, v2, 255);	
						}
					for(v1 =0;v1 < (adapt*2+1)*10; v1++ )
					{
						IM_PIX_SET(adaptim, v1, (adapt*2+1)*10, 0);	
					}
					for(v1 =0;v1 < (adapt*2+1)*10; v1++ )
					{
						IM_PIX_SET(adaptim, (adapt*2+1)*10,v1, 0);	
					}
						
/*-----------------------------------------------------------------------------*/

					for(t = i - adapt; t <= i + adapt; t ++)
						for(h = j - adapt; h <= j + adapt; h ++)
						{

							if((t<0)||(t>=height)||(h<0)||(h>=width))
								continue;
/*-----------------------------------------------------------------------------*/
							if(t==i&&h==j)
							{
								for(v1 = 0; v1 <10; v1++)
									for(v2 = 0; v2 <10; v2++)
									{
										IM_PIX_SET(adaptim, (adapt-i+t)*10+v1, (adapt-j+h)*10+v2, 211);		
									}
							}
/*-----------------------------------------------------------------------------*/
							pixm = im_get_pixf(srcIm0,t,h);
							//if(fabs(pixm - cutpix) > f)//orig point
							if(flag[t][h]==1)//orig pexel
							{
								w = adapt+1-IMAX(abs(i-t),abs(j-h));
								gray_sum += w*pixm;
								ws += w;
								m_count ++;
/*-----------------------------------------------------------------------------*/
								for(v1 = 0; v1 <10; v1++)
									for(v2 = 0; v2 <10; v2++)
									{
										IM_PIX_SET(adaptim, (adapt-i+t)*10+v1, (adapt-j+h)*10+v2, 0);	
									}
/*-----------------------------------------------------------------------------*/
							}
						}
					if(m_count < MCOUNT)
						adapt ++;
					tv_imrect2(tvo, adaptim);
					tv_repaint(tvo);
				}
				pixm = (float)(gray_sum/ws);//mean
				count_adapt[adapt] ++;
				im_put_pixf(pixm,destIm,i,j);
			}
			else 
			{
				im_put_pixf(pix,destIm,i,j);
				count_adapt[0] ++;
			}
		}
/*---------------------------------------------------------------------*/
			break;
	case 4:
/*-------------------------------hwm-------------------------------------*/
	printf("hwm\n");
	strcpy(filename0,"_hwm");
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j ++)
		{
			pix = im_get_pixf(srcIm0,i,j);
			adapt = 1;
			//if(fabs(pix - cutpix) < f )//lost point
			if(flag[i][j]==0)
			{
				m_count = 0;
				gray_sum = 0.0;
				hws = 0.0;
				while(m_count < MCOUNT )//at least have MCOUNT points in the mask
				{
					m_count = 0;
					gray_sum = 0.0;
					hws = 0.0;
					for(t = i - adapt; t <= i + adapt; t ++)
						for(h = j - adapt; h <= j + adapt; h ++)
						{
							if((t<0)||(t>=height)||(h<0)||(h>=width))
								continue;
							pixm = im_get_pixf(srcIm0,t,h);
							//if(fabs(pixm - cutpix) > f)//orig point
							if(flag[t][h]==1)//orig pexel
							{
								gray_sum += ho[(int)pixm]*pixm;
								hws += ho[(int)pixm];
								m_count ++;
							}
						}
					if(m_count < MCOUNT)
						adapt ++;
				}
				pixm = (float)(gray_sum/hws);//mean
				count_adapt[adapt] ++;
				im_put_pixf(pixm,destIm,i,j);
			}
			else 
			{
				im_put_pixf(pix,destIm,i,j);
				count_adapt[0] ++;
			}
		}
/*---------------------------------------------------------------------*/
			break; 
		default: 
			break; 
    	}

/*----------------------------------------------*/

	gcvt(MCOUNT,5,string);
	strcat(filename0, string);
	//strcat(filename, filename0);
	char filename1[64];	
	char filename2[64];
	char filename3[64];
	char filenametxt[64];
	strcpy(filename1,"result/receive/repair");//filename without change
	gcvt(pol,5,string);
	strcat(filename1, string);
	strcat(filename1,filename0);
	strcpy(filename2,filename1);
	strcpy(filename3,filename1);
	strcpy(filenametxt,filename1);
	strcat(filename1,".bmp");
	ImrectToBMP8(destIm,filename1);

	strcat(filenametxt,".txt");
	FILE *fp;
	fp = fopen(filenametxt,"wb");
/*----------------------------------------------*/
	for(i = 0;i < 25; i ++)
		if(count_adapt[i] != 0)
		{
			printf("count_adapt[%d] = %d\n",i,count_adapt[i]);
			fprintf(fp,"count_adapt[%d] = %d\n",i,count_adapt[i]);
		}
/*----------------------------------------------*/
	int count[256];
	for(i = 0;i < 256; i ++)
		count[i] = 0;
	float mse = 0.0 ,psnr = 0.0 ,mae = 0.0, nmse = 0.0 ,op = 0.0;
	for(i = 0; i < height; i ++)
		for(j = 0; j < width; j ++)
		{
			IM_PIX_GET(destIm, i, j, pixb);
			IM_PIX_GET(comp_orig, i, j, pixa);
			pix = fabs(pixa - pixb);
			IM_PIX_SET(dest_a_b, i, j, pix);
			//printf("%f,%f,%f\n",pixa,pixb,pix);		
			count[(int)pix] ++;
			IM_PIX_SET(dest_c,i,j,255 - pix);
/*
			if(pix < 40)
			{
				IM_PIX_SET(dest_c,i,j,255 );
			}
			else 
			{
				IM_PIX_SET(dest_c,i,j,0);
			}
*/
			mse += pix*pix;
			mae += pix;
			op += pixa*pixa;
		}
/*----------------------------------------------
	for(i = 0;i < 256; i ++)
		if(count[i] != 0)
		{
			printf("count[%d] = %d\n",i,count[i]);
			fprintf(fp, "count[%d] = %d\n",i,count[i]);
		}
/*----------------------------------------------*/
	nmse = mse / op;
	mse = mse/(height*width);
	psnr = 10*log10(255*255/mse);
	mae = mae/(height*width);
	printf("Percent       MSE       PSNR          MAE        NMSE   \n");
	printf("%f   %f   %f   %f   %f\n", (float)pol/100, mse, psnr, mae, nmse);
	//fprintf(fp, "MSE = %f , PSNR = %f , MAE = %f , NMSE = %f\n", mse, psnr, mae, nmse);
	fprintf(fp, "%f  %f  %f  %f  %f\n", (float)pol/100, mse, psnr, mae, nmse);
	fclose(fp);
/*------------------------------------------------
	strcat(filename2,"_comp.bmp");
	ImrectToBMP8(dest_a_b,filename2);
	strcat(filename3,"_comp_r.bmp");
	ImrectToBMP8(dest_c,filename3);
/*------------------------------------------------*/
	im_free(dest_a_b);
	im_free(dest_c);
/*------------------------------------------------*/
	repair_im = im_copy(destIm);
	tv_imrect2(tv2, repair_im);
/*------------------------------------------------
	mono_image_set(destIm); 
	stack_push(destIm, IMRECT, im_free);
	im_free(srcIm0);
/*------------------------------------------------*/
	free_int_matrix(flag, height);
	mono_image_set(srcIm0); 
	stack_push(srcIm0, IMRECT, im_free);
	im_free(destIm);
	printf("\n=== %s complete! ===\n",filename0);
	//strcpy(filename,filename_in);
	im_free(adaptim);

}

static void compare_proc(void)
{
/*----------------Pop the org_image from stack-------------------*/
	Imrect *orig_a;
	Imrect *orig_b;
	Imrect *dest_a_b;
	Imrect *dest_c;

    	int     type;
	int width, height; 
	float pix,pixa,pixb; 
	Imregion *roi;
	int i,j;

	if (stack_check_types(IMRECT, NULL) == false) 
	{
		printf("pop ͼÏñb ʧ°Ü£¡\n"); 
		error("Motion_proc : wrong types on stack", warning); 
		return; 
	} 
	orig_b = (Imrect *) stack_pop(&type);

	printf("pop ͼÏñb ³É¹¦£¡\n");



	if (stack_check_types(IMRECT, NULL) == false) 
	{
		printf("pop ͼÏña ʧ°Ü£¡\n"); 
		error("Motion_proc : wrong types on stack", warning); 
		return; 
	} 
	orig_a = (Imrect *) stack_pop(&type);

	printf("pop ͼÏña ³É¹¦£¡\n");
/*-----------------------------------------------------------------------------*/

	width = orig_a->width; 
    	height = orig_a->height; 
	roi = orig_a->region; 
	if (roi == NULL) return ;

	dest_a_b = im_alloc( height, width, roi, float_v );
	dest_c = im_alloc( height, width, roi, float_v );
	int count[256];
	for(i = 0;i < 256; i ++)
		count[i] = 0;
	for(i = 0; i < height; i ++)
		for(j = 0; j < width; j ++)
		{
			IM_PIX_GET(orig_b, i, j, pixb);
			IM_PIX_GET(orig_a, i, j, pixa);
			pix = fabs(pixa - pixb);
			IM_PIX_SET(dest_a_b, i, j, pix);
			//printf("%f,%f,%f\n",pixa,pixb,pix);		
			count[(int)pix] ++;
			IM_PIX_SET(dest_c,i,j,255 - pix);
/*
			if(pix < 40)
			{
				IM_PIX_SET(dest_c,i,j,255 );
			}
			else 
			{
				IM_PIX_SET(dest_c,i,j,0);
			}
*/
		}
	for(i = 0;i < 256; i ++)
		if(count[i] != 0)
			printf("count[%d] = %d\n",i,count[i]);

	char filename1[64];
	strcpy(filename1,"result/receive/compare/cmean1.bmp");
	ImrectToBMP8(dest_a_b,filename1);
	ImrectToBMP8(dest_c,"result/receive/compare/mean1c.bmp");
	im_free(dest_a_b);
	im_free(dest_c);
	//mono_image_set(dest_a_b); 
	//stack_push(dest_a_b, IMRECT, im_free);
}
static void test_proc(void)
{
/*
	int i;
	float *data;
	float pixm;
	data = fl_vector(11);
	for(i = 0; i < 11; i ++)
		data[i] = i;
	pixm = get_median(data,11);
	printf("median=%f\n",pixm);
	data[7]=9;
	pixm = get_mode(data,11);
	printf("mode= %f\n",pixm);
	free_fl_vector(data);
*/
	Imrect *srcIm,*srcIm0;
	float pix;
	int type;
	int width, height;
	Imregion *roi;
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("receive-client : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);
	width = srcIm->width; 
    	height = srcIm->height; 
	roi = srcIm->region; 
	if (roi == NULL) return ;
	int i,j;
	srcIm0 = im_alloc( height, width, roi, float_v );
	for(i = 0;i < height; i ++)
		for(j = 0;j < width; j++)
		{
			IM_PIX_GET(srcIm, i, j, pix);
			IM_PIX_SET(srcIm0, i, j, pix);
		}
	ImrectToBMP8(srcIm0,"result/receive/compare/test.bmp");
	//im_free(srcIm0);
	mono_image_set(srcIm0); 
	stack_push(srcIm0, IMRECT, im_free);
}
static void fulldraw(Tv *tv)
{
     tv_imrect2(tv, orig_im);
}

static void fulldraw1(Tv *tv)
{
     tv_imrect2(tv, contra_scram_im);
}
static void fulldraw2(Tv *tv)
{
     tv_imrect2(tv, repair_im);
}

static void tv_choice_proc(choice) 
int     choice; 
{ 
    switch (choice) 
    	{ 
    	case 0: tv_set_next(tv);//original 
		break; 
    	case 1: tv_set_next(tv1);//contra-transformation 
		break;
    	case 2: tv_set_next(tv2);//repair 
		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);
}

static void tool_proc(void) 
{ 
    static int x = 200, y = 0; 
    static int xr = 0, yr = 0; 
 
    tv_tool(x+xr, y+yr); 
    xr += 275; 
    yr += 0; 
    if (x+xr>780) 
    { 
       xr = yr = 0; 
       y += 50; 
    } 
    if (y>400) y = 200; 
} 
 

/********** Tool creation **********/ 
void            receive_client_tool(int x, int y) 
{ 
	static void *tool = NULL;
	if (tool) 
	{ 
		tw_show_tool(tool); 
		return; 
	} 
	tool = (void *)tw_tool("receive-client 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/receive/receive.bmp");
	tv = tv_create("receive-client original"); 
     	tv1 = tv_create("receive-client contra-thansformation");
     	tv2 = tv_create("receive-client repair");         tw_choice("Tv choice", tv_choice_proc, 0, "original","contra-thansformation","repair",NULL);
	tv_set_fulldraw(tv, fulldraw);
	tv_set_fulldraw(tv1,fulldraw1);
	tv_set_fulldraw(tv2,fulldraw2);
	/*---------------------------*/
  	tw_newrow(); 
	tw_label("Display"); 
	tw_button("New Tvtool", tool_proc, NULL);
	/*---------------------------*/
 	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_choice("input image choice", image_choice_proc, 0, "BMP","GIF", NULL);
   	tw_label("              ");
	tw_button("push",mypush_proc,NULL);
	tw_button("pop",mypop_proc,NULL);
	tw_button("show",myshow_proc,NULL); 
	tw_button("save",mysave_proc,NULL);
	tw_newrow();
	tw_choice("Cut %", cut_choice_proc, 0,\ 
              "25%", "50%", "75%-L_up", "75%-R_up", "75%-L_down", "75%-R_down", "specify",  NULL);
	tw_newrow();
	tw_iglobal("start point:h= ", &START_h, 5);
	tw_iglobal("w= ", &START_w, 5);
	tw_iglobal("o-height=", &ORIG_height, 5);
	tw_iglobal("o-width=", &ORIG_width, 5);
	tw_newrow();
	tw_button("use the keys to rebuild",rebuild_proc,NULL);
	tw_newrow();
   	tw_label("----------------------------------------------------------------");
	tw_newrow(); 
	tw_choice("Scrambling Method:", scram_choice_proc, 0,\ 
              "Fibonacci", "Arnold", "QATLIG", "Hilbert", "Convey", "Latin", "Zigzag", "Baker", "Logistic", "Random",  NULL); 
	tw_newrow();
	tw_iglobal("Scrambling times = ", &SCRAM_n, 5);
	tw_newrow();
	tw_button("Contra-Transformation",Contra_Trans_proc,NULL);
   	tw_label("              ");
	tw_button("reset",reset_proc,NULL);
	tw_newrow();
   	tw_label("----------------------------------------------------------------");
	tw_newrow();
	tw_choice("repair method:", repair_choice_proc, 0,\ 
              "mean", "median", "mode", "w-mean","hwm", NULL);
         tw_newrow();
	tw_choice("show:", show_choice_proc, 0,\ 
              "result", "step by step", NULL);
	tw_newrow();
	tw_iglobal("not less than ", &MCOUNT, 5);
	tw_label("original points in the mask "); 
	tw_newrow();
	tw_button("repair",repair_proc,NULL);
	tw_button("compare",compare_proc,NULL);
	tw_button("test",test_proc,NULL);
	tw_button("nhwfm", nhwfm_proc, NULL); 
	tw_end_tool(); 
}