www.pudn.com > src.rar > send.c~


/******************************************************************** 
 * 
 * image cut tool 
 * 
 ********************************************************************/
#include 
#include "bmp.h"
static int SCRAM_n = 1;
static int START_h = 0;
static int START_w = 0;
static int CUT_height = 128;
static int CUT_width = 128;

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 image_choice = 0;
#define CUTPIX 254.56789

static char filename[MAXPATHLEN];
static Imrect *orig_im =NULL;
static Imrect *scram_im = NULL;
static Imrect *cut_im = NULL;
extern void ImrectToBMP8 (Imrect* im , const char* filename);
extern void readBMP(const char *fileName);
static void scram_choice_proc(int val) 
{ 
	scram_method = val; 
}
static void cut_choice_proc(int val) 
{ 
	cut_method = val; 
}
static image_choice_proc(int val)
{
	image_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 = NULL,*srcIm0 = NULL;

	int width, height; 
	Imregion *roi;
	int i,j;
	//BYTE pix;
	float pix;
	srcIm = read_image();
	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 Imrect* mypop_proc(void)
{
	Imrect *srcIm;
	//BYTE pix;
	float pix;
	int type;
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("send-client : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);
	return srcIm;
}
static void mysave_proc(void)
{
	Imrect *srcIm = NULL,*srcIm0 = NULL;

	int type;
	int width, height; 
	Imregion *roi;
	int i,j;
	//BYTE pix;
	float pix;

	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("send-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 myshow_proc(void)
{
	int type;
	int width, height; 
	Imregion *roi;
	Imrect *srcIm,*srcIm0;
	int i,j;
	//BYTE pix;
	float pix;
	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("send 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 reset_proc(void)
{
	strcpy(filename,"result/send/");
	//strcat(filename,file_name);
	mypop_proc();
}


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

	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("send-client : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type);

	//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);

	char filename0[64];
	char string[25];
	strcpy(filename0,"");
/*------------------------------------------------*/
	int **trans_r, **trans_c; 
	trans_r = int_matrix(height, width); 
	trans_c = int_matrix(height, width);
    	switch( scram_method ) 
    	{ 
    		case 0: 
    			fibonacci_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "fibonacci_");
			strcat(filename0, "f"); 
			break; 
    		case 1: 
    			arnold_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "arnold_");
			strcat(filename0, "a"); 
			break; 
    		case 2: 
    			qatlig_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "qatlig_");
			strcat(filename0, "q"); 
			break; 
    		case 3: 
			hilbert_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "hilbert_");
			strcat(filename0, "h"); 
			break; 
		case 4: 
			convey_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "convey_");
			strcat(filename0, "c"); 
			break; 
    		case 5: 
			latin_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "latin_");
			strcat(filename0, "la"); 
			break; 
		case 6: 
    			//graycode_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "graycode_");
			//strcat(filename0, "g");
			zigzag_matrix(trans_r, trans_c, height, width);
			strcat(filename0,"z"); 
			break; 
		case 7: 
    			baker_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "baker_");
			strcat(filename0, "b"); 
			break; 
		case 8: 
    			logistic_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "logistic_");
			strcat(filename0, "lo"); 
			break; 
		case 9:
/* 
			sampling_matrix(trans_r, trans_c, height, width);
			//strcat(filename0, "sampling_");
			strcat(filename0, "sa");
*/
			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);
			//printf("%f  ",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 .gif--------------------------*/

	gcvt(k,5,string);
	printf("scrambled %s times\n",string);
	strcat(filename, filename0);
	strcat(filename, string);
/*------------------------------------------------*/
	scram_im = im_copy(srcIm0);
	tv_imrect2(tv1, 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",filename0);	
}
static void cut_proc(void)
{
	Imrect *srcIm,*destIm; 
	int width, height; 
	Imregion *roi;
	int i,j;
	int type; 
	//BYTE pix;
	float pix;

	if (stack_check_types(IMRECT, NULL) == false) 
	{ 
		error("cut : wrong types on stack", warning); 
		return; 
	}
	srcIm = (Imrect *) stack_pop(&type); 
	width = srcIm->width; 
    	height = srcIm->height; 
	roi = srcIm->region; 
	if (roi == NULL) return ;

	destIm = im_alloc( height, width, roi, float_v );
	for(i = 0;i < height;i ++)
		for(j = 0;j < width;j ++)
			IM_PIX_SET(destIm, i, j, CUTPIX);

	char filename0[64];
	//strcpy(filename0,"_send_");
	strcpy(filename0,"_");
	int h2,w2,h4,w4;
	h2 = height/2;
	w2 = width/2;
	h4 = height/4;
	w4 = width/4;
	Imrect *cutIm;
	char string[25];
	int cut_height,cut_width;
/*------------------------------------------------*/

    	switch( cut_method ) 
    	{ 
    		case 0:
			cut_height = height;
			cut_width = width -w4;
			cutIm = im_alloc( cut_height, cut_width, NULL, float_v ); 
			for(i = 0;i < height;i ++)
				for(j = w4;j < width;j ++)
				{
					IM_PIX_GET(srcIm, i, j, pix);
					IM_PIX_SET(destIm, i, j, pix);
					IM_PIX_SET(cutIm, i, j-w4, pix);
				}
			strcat(filename0, "25%_"); 
			break; 
    		case 1: 
			cut_height = height - h2;
			cut_width = width;
			cutIm = im_alloc( cut_height, cut_width, NULL, float_v ); 
			for(i = h2;i < height;i ++)
				for(j = 0;j < width;j ++)
				{
					IM_PIX_GET(srcIm, i, j, pix);
					IM_PIX_SET(destIm, i, j, pix);
					IM_PIX_SET(cutIm, i-h2, j, pix);
				}
			strcat(filename0, "50%_"); 
			break; 
    		case 2: 
			cut_height = h2;
			cut_width = w2;
			cutIm = im_alloc( cut_height, cut_width, NULL, float_v ); 
			for(i = 0;i < h2;i ++)
				for(j = 0;j < w2;j ++)
				{
					IM_PIX_GET(srcIm, i, j, pix);
					IM_PIX_SET(destIm, i, j, pix);
					IM_PIX_SET(cutIm, i, j, pix);
				}
			strcat(filename0, "75%-L_up_"); 
			break; 
    		case 3: 
			cut_height = h2;
			cut_width = width - w2;
			cutIm = im_alloc( cut_height, cut_width, NULL, float_v ); 
			for(i = 0;i < h2;i ++)
				for(j = w2;j < width;j ++)
				{
					IM_PIX_GET(srcIm, i, j, pix);
					IM_PIX_SET(destIm, i, j, pix);
					IM_PIX_SET(cutIm, i, j-w2, pix);
				}
			strcat(filename0, "75%-R_up_");; 
			break; 
		case 4: 
			cut_height = height - h2;
			cut_width = w2;
			cutIm = im_alloc( cut_height, cut_width, NULL, float_v ); 
			for(i = h2;i < height;i ++)
				for(j = 0;j < w2;j ++)
				{
					IM_PIX_GET(srcIm, i, j, pix);
					IM_PIX_SET(destIm, i, j, pix);
					IM_PIX_SET(cutIm, i-h2, j, pix);
				}
			strcat(filename0, "75%-L_down_"); 
			break; 
    		case 5: 
			cut_height = height - h2;
			cut_width = width - w2;
			cutIm = im_alloc( cut_height, cut_width, NULL, float_v ); 
			for(i = h2;i < height;i ++)
				for(j = w2;j < width;j ++)
				{
					IM_PIX_GET(srcIm, i, j, pix);
					IM_PIX_SET(destIm, i, j, pix);
					IM_PIX_SET(cutIm, i-h2, j-w2, pix);
				}
			strcat(filename0, "75%-R_down_"); 
			break; 
		case 6: 
			cut_height = CUT_height;
			cut_width = CUT_width;
			if ((START_h + CUT_height > height)||(START_w + CUT_width > width)) 
			{ 
				printf("send-client : cut out of the image!!!\n"); 
				return; 
			}
			cutIm = im_alloc( cut_height, cut_width, NULL, float_v ); 
			for(i = START_h;i < START_h + CUT_height;i ++)
				for(j = START_w;j < START_w + CUT_width;j ++)
				{
					IM_PIX_GET(srcIm, i, j, pix);
					IM_PIX_SET(destIm, i, j, pix);
					IM_PIX_SET(cutIm, i-START_h, j-START_w, pix);
				}
			//strcat(filename0, "specify_");
			gcvt(START_h,5,string);
			strcat(filename0, string);
			strcat(filename0, "_");
			gcvt(START_w,5,string);
			strcat(filename0, string);
			strcat(filename0, "_"); 
			break; 
		default: 
			break; 
    }
/*------------------------------------------------*/
	gcvt(height,5,string);
	strcat(filename0, string);
	strcat(filename0, "x");
	gcvt(width,5,string);
	strcat(filename0, string);
	char filenamecut[64];
	//strcpy(filenamecut,"result/send/");
	//strcat(filenamecut, filename0);
	strcpy(filenamecut,filename);
	strcat(filenamecut, filename0);
	//strcat(filenamecut, ".gif"); 
	//gif_write_file(cutIm,filenamecut);
	strcat(filenamecut,".bmp");
	ImrectToBMP8(cutIm,filenamecut);
	printf("save the image :%s\n",filename);
/*----------------------------------------------*/

	strcat(filename, filename0);
	//gif_write_file(destIm,filename);
	//ImrectToBMP8(destIm,filename);
/*------------------------------------------------*/
	cut_im = im_copy(destIm);
	tv_imrect2(tv2, cut_im);
/*----------------------------------------------
	mono_image_set(destIm); 
	stack_push(destIm, IMRECT, im_free);
	im_free(cutIm);
/*----------------------------------------------*/
	mono_image_set(cutIm); 
	stack_push(cutIm, IMRECT, im_free);
	im_free(destIm);
/*----------------------------------------------*/
	printf("\n=== %s complete! ===\n",filename0);		
}


static void fulldraw(Tv *tv)
{
     tv_imrect2(tv, orig_im);
}

static void fulldraw1(Tv *tv)
{
     tv_imrect2(tv, scram_im);
}
static void fulldraw2(Tv *tv)
{
     tv_imrect2(tv, cut_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);//transformation 
		break;
    	case 2: tv_set_next(tv2);//cut 
		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 = 0, 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            send_client_tool(int x, int y) 
{ 
	static void *tool = NULL;
	if (tool) 
	{ 
		tw_show_tool(tool); 
		return; 
	} 
	tool = (void *)tw_tool("send-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/send/Lena.bmp");
	tv = tv_create("send-client original"); 
     	tv1 = tv_create("send-client scrambled");
     	tv2 = tv_create("send-client cut");
	tw_choice("Tv choice", tv_choice_proc, 0, "original","scrambled","cut", 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();
	strcpy(filename,"result/send/");
	//strcat(filename,file_name); 
	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("Transformation",Trans_proc,NULL);
   	tw_label("              ");
	tw_button("reset",reset_proc,NULL);
	tw_newrow();
   	tw_label("-----------------------------------------------------------------------");
	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("height  = ", &CUT_height, 5);
	tw_iglobal("width  = ", &CUT_width, 5);
	tw_newrow();
	tw_button("cut",cut_proc,NULL);

	tw_newrow(); 
	tw_end_tool(); 
}