www.pudn.com > src.rar > latin.c
/*--------------------------------------------------------------------------- * latin 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 int K1_n = 23; static int K2_n = 110; static int save_type = 0; static int image_choice = 0; static char filename[MAXPATHLEN]; static Imrect *orig_im = NULL; static Imrect *dest_im = NULL; static Tv *tv = NULL; static Tv *tv1=NULL; static void save_choice_proc(int val) { save_type = 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); switch(save_type) { case 0: strcat(filenamesave,".bmp"); ImrectToBMP8(srcIm0,filenamesave); break; case 1: strcat(filenamesave,".gif"); gif_write_file_color(srcIm0,filenamesave); break; default:error("mysave : unknown choice\n", warning); break; } printf("saved the image :%s\n",filenamesave); mono_image_set(srcIm0); stack_push(srcIm0, IMRECT, im_free); } static void myshow_proc(void) { Imrect *srcIm,*srcIm0; int type; int width, height; Imregion *roi; 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/Latin/"); //strcat(filename,file_name); mypop_proc(); } /*--------------------------------------------------------------------------- To generate the scrambling mapping-matrix of latin transformation. ---------------------------------------------------------------------------*/ void latin_matrix(int** r, int** c, int height, int width) { int i, j, x, y; printf("\nLatin transform\n"); for (i = 0; i < height; i++) { for (j = 0; j< width; j++) { x = (K1_n * i + j) % width; y = (K2_n * i + j) % width; r[i][j] = x; c[i][j] = y; } } } /*--------------------------------------------------------------------------- To generate the inverse scrambling mapping-matrix of latin transformation. ---------------------------------------------------------------------------*/ void r_latin_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); latin_matrix(trans_r, trans_c, height, width); printf("\nR_Latin 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); } /*--------------------------------------------------------------------------- latin transformation ---------------------------------------------------------------------------*/ void Latin_Trans(int nTrans) { Imrect *srcIm = NULL,*destIm = NULL,*tmpIm = NULL,*srcIm0 = NULL; int width, height; Imregion *roi; int i,j,k; int type; //BYTE pix; float pix; if (stack_check_types(IMRECT, NULL) == false) { error("Latin : 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("Latin: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); latin_matrix(trans_r, trans_c, height, width); /*------------------------------------------------*/ printf("\n\n=== Latin Transform begin: ===\n"); 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 -------------------------- char string[25]; gcvt(k+1,5,string); printf("string=%s\n",string); //strcat(filename,"Latin_"); //strcat(filename, string); char filenamesave[64]; strcpy(filenamesave,"result/Latin/100/Lena_Latin_"); strcat(filenamesave, string); switch(save_type) { case 0: strcat(filenamesave,".bmp"); ImrectToBMP8(srcIm0,filenamesave); //strcat(filenamesave,".gif"); //gif_write_file(srcIm0,filenamesave); break; case 1: strcat(filenamesave,".gif"); gif_write_file_color(srcIm0,filenamesave); break; case 2: break; default:error("latin trans: unknown choice\n", warning); break; } printf("saved the image :%s\n",filenamesave); } /*------------------------------------------------*/ dest_im = im_copy(srcIm0); tv_imrect2(tv1, dest_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("=== Latin transform complete! ===\n"); } /*--------------------------------------------------------------------------- inverse latin transformation ---------------------------------------------------------------------------*/ void R_Latin_Trans(int nTrans) { 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("Latin : 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_Latin: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_latin_matrix(trans_r, trans_c, height, width); /*------------------------------------------------*/ printf("\n\n=== R_Latin Transform begin: ===\n"); 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 --------------------------*/ char string[25]; gcvt(k,5,string); printf("string=%s\n",string); strcat(filename,"R_Latin_"); strcat(filename, string); char filenamesave[64]; strcpy(filenamesave,filename); switch(save_type) { case 0: strcat(filenamesave,".bmp"); ImrectToBMP8(srcIm0,filenamesave); //strcat(filenamesave,".gif"); //gif_write_file(srcIm0,filenamesave); break; case 1: strcat(filenamesave,".gif"); gif_write_file_color(srcIm0,filenamesave); break; case 2: break; default:error("latin R_trans: unknown choice\n", warning); break; } printf("saved the image :%s\n",filenamesave); /*------------------------------------------------*/ dest_im = im_copy(srcIm0); tv_imrect2(tv1, dest_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("=== R_Latin transform complete! ===\n"); } static void fulldraw(Tv *tv) { tv_imrect2(tv, orig_im); } static void fulldraw1(Tv *tv) { tv_imrect2(tv, dest_im); } 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 Latin_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("Latin 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/Latin/Lena.bmp"); tv = tv_create("Latin original"); tv1 = tv_create("Latin scrambled"); tw_choice("Tv choice", tv_choice_proc, 0, "original","scrambled", NULL); tv_set_fulldraw(tv, fulldraw); tv_set_fulldraw(tv1,fulldraw1); tw_newrow(); strcpy(filename,"result/Latin/"); //strcat(filename,file_name); /*---------------------------*/ 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_newrow(); tw_choice("choice", save_choice_proc, 0, "Gray", "RGB", "without save", NULL); tw_label(" "); tw_button("save",mysave_proc,NULL); tw_newrow(); tw_iglobal("a(k1) = ", &K1_n, 5); tw_iglobal("a(k2) = ", &K2_n, 5); tw_newrow(); tw_iglobal("Scrambling times = ", &SCRAM_n, 5); tw_newrow(); tw_button("Latin", Latin_Trans,NULL); tw_button("R_Latin", R_Latin_Trans,NULL); tw_label(" "); tw_button("reset",reset_proc,NULL); tw_newrow(); tw_newrow(); tw_end_tool(); }