📄 arnold.c
字号:
/*---------------------------------------------------------------------------* Arnold transformation* ---------------------------------------------------------------------------*/#include <tina/all_tina.h>#include "bmp.h"
#include "gnuplot_i.h"#define MAXPATHLEN 64static 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 Nsize = 256;static Tv *tv = NULL;static Tv *tv1=NULL;/*--------------------------------------------------------------------------- To generate the scrambling mapping-matrix of arnold transformation. ---------------------------------------------------------------------------*/void arnold_matrix(int** r, int** c, int height, int width)
{
int i, j, x, y;
printf("\nArnold transform\n");
for(i = 0; i < height; i++)
for(j = 0; j < width; j++)
{ r[i][j] = (i + j) % width;
c[i][j] = (i + 2*j) % height; }
}/*--------------------------------------------------------------------------- To generate the inverse scrambling mapping-matrix of arnold transformation. ---------------------------------------------------------------------------*/void r_arnold_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); arnold_matrix(trans_r, trans_c, height, width); printf("\nR_Arnold 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);}/*--------------------------------------------------------------------------- To generate the test image that you want. e.g.,RGB.gif If you want to generate gray images, you can use the function gif_write_file(); and gif_write_file_color() for color iamges. ---------------------------------------------------------------------------*/static Imrect *DrawRGB_proc(void){ int i,j; Imrect *RGBIm,*srcIm; int width, height;
Imregion *roi; srcIm = read_image();
width = srcIm->width;
height = srcIm->height;
roi = srcIm->region;
if (roi == NULL) return ; RGBIm = im_alloc( height, width, roi, int_v ); for(i = 0;i <256; i ++) for(j = 0;j <256; j++) IM_PIX_SET(RGBIm, i, j, 255); //green for(i = 192;i < 256;i ++) for(j = 0;j < 64;j ++) { IM_PIX_SET(RGBIm, i, j, 99); } //red for(i = 0;i < 64;i ++) for(j = 64;j < 128;j ++) { IM_PIX_SET(RGBIm, i, j, 49); } //blue for(i = 128;i < 192;i ++) for(j = 128;j < 192;j ++) { IM_PIX_SET(RGBIm, i, j, 149); } //gif_write_file_color(RGBIm,"RGB.gif"); //gif_write_file(RGBIm,"gRGB.gif"); return(RGBIm); im_free(RGBIm);}/*--------------------------------------------------------------------------- arnold transformation ---------------------------------------------------------------------------*/void Arnold_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("Arnold : 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("Arnold: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); arnold_matrix(trans_r, trans_c, height, width);/*------------------------------------------------*/
printf("\n\n=== Arnold 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 or .bmp--------------------------*/ //gif_write_file_color(srcIm,"arnold_1_y.gif"); char filename[64]; char string[25]; gcvt(k,5,string); strcpy(filename,"result/Arnold/"); strcat(filename,"arnold_"); strcat(filename, string); strcat(filename,".gif"); gif_write_file_color(srcIm,filename);/*------------------------------------------------ //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("=== Arnold transform complete! ===\n");
}
/*--------------------------------------------------------------------------- inverse arnold transformation ---------------------------------------------------------------------------*/void R_Arnold_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("Arnold : 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_Arnold: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_arnold_matrix(trans_r, trans_c, height, width);/*------------------------------------------------*/
printf("\n\n=== R_Arnold 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/Arnold/"; strcpy(filename,filename0); strcat(filename,file_name); strcat(filename,"+R_arnold_"); 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_Arnold transform complete! ===\n");
}/*--------------------------------------------------------------------------- To calculate the periodictity of the arnold transformation. The result will show the periodictity of different sizes, from 2 to Nsize. ---------------------------------------------------------------------------*/static void periodicity_proc(void){ int x=1,y=1,xn,yn,n,N; n = 1; FILE *fp; char fn[64]; char fname[64]; char fnpng[64]; char str[25]; strcpy(fn,"arnold_periodicity_"); gcvt(Nsize,5,str); strcat(fn, str); strcpy(fname,fn); strcat(fname,".txt"); fp = fopen(fname,"wb"); for(N = 2; N <= Nsize; N ++) { x=1; y=1; for(n = 1; ; n ++) { xn = x + y; yn = x + 2*y; if(xn%N==1 && yn%N==1) break; x = xn%N; y = yn%N; } printf("%5d %5d\n",N,n); fprintf(fp,"%5d %5d\n",N,n); } fclose(fp); strcpy(fnpng,fn); strcat(fnpng,".png"); gnuplot_ctrl *h; h = gnuplot_init(); gnuplot_cmd(h,"plot '%s' using 1:2 with linespoints title '%s'", fname,fn); gnuplot_cmd(h,"set term png"); gnuplot_cmd(h,"set output '%s' ",fnpng); gnuplot_cmd(h,"replot");}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 Arnold_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("Arnold 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/Arnold/gRGB.gif"); tv = tv_create("Arnold original");
tv1 = tv_create("Arnold 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("Arnold", Arnold_Trans,NULL); tw_button("R_Arnold", R_Arnold_Trans,NULL); tw_newrow(); //tw_button("DrawRGB",DrawRGB_proc,NULL); tw_iglobal("Width = Height = ", &Nsize, 5); tw_button("Periodicity",periodicity_proc,NULL); tw_newrow();
tw_end_tool();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -