www.pudn.com > src.rar > random.c~
/*---------------------------------------------------------------------------
* Arnold transformation
* ---------------------------------------------------------------------------*/
#include <tina/all_tina.h>
#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;
static long R0 = 2097152;
static long LAMBDA = 2045;
static long MOD = 2097153;
//#define R0 2097151
//#define LAMBDA 2045
//#define MOD 2097152
//#define R0 2097152
//#define MOD 2097153
/*-------------------------Random -----------------------------------
double uniform_random(double a, double b, int ar0, int lamda, int mod)
{
static double x;
long entier;
if(ar0 != 0) x = (double)ar0/(double)mod;
x *= (double)lamda;
entier = (x > 0.0)? floor(x) : ceil(x);
x -= (double)entier;
return(x*(b-a)+a);
}
void Rand_sq(int* dest, int M)
{
int flag[M];
int i,r;
for(i = 0;i < M;i ++)
{
dest[i] = i;
flag[i] = 0;
}
i=0;
r = (int)uniform_random(0, M, R0, LAMBDA,MOD);
dest[i] = r;
flag[r] = 1;
for(i = 1;i < M;i ++)
{
r = (int)uniform_random(0, M, 0, LAMBDA,MOD);
if(flag[r] == 0)
dest[i] = r;
while(flag[r])
{
r = (int)uniform_random(0, M, 0, LAMBDA,MOD);
if(flag[r] == 0)
dest[i] = r;
}
flag[r] = 1;
}
}
void random_matrix(int** r, int** c, int height, int width)
{
int i;
printf("\nRandom transform\n");
int *dest;
int m = height*width;
dest = int_vector(m);
Rand_sq(dest, m);
for(i = 0; i < m; i++)
{
r[i/width][i>width] = dest[i] / width;
c[i/width][i>width] = dest[i] > width;
}
free_int_vector(dest);
}
/*-------------------------------------------------------------------*/
/*---------------------------------------------------------------------------
To generate the scrambling mapping-matrix of arnold transformation.
---------------------------------------------------------------------------*/
void random_matrix(int** r, int** c, int height, int width)
{
int i,j;
long entier,t;
int u,v;
printf("\nRandom transform\n");
int **flag;
flag = int_matrix(height, width);
for (i = 0; i < height; i++)
{
for(j = 0; j < width; j++)
{
flag[i][j] = 0;
}
}
double x = (double)R0/(double)MOD; /* set the initial value.*/
for (i = 0; i < height ; i++)
{
for (j = 0; j< width ; j++)
{
do
{
x *= (double)LAMBDA;
entier = (x > 0.0)? floor(x) : ceil(x);
x -= (double)entier;
t = floor(height * width * x);
u = t / width;
v = t > width;
}while(flag[u][v] == 1);
flag[u][v] = 1;
r[i][j] = u;
c[i][j] = v;
}
}
free_int_matrix(flag, height);
}
/*---------------------------------------------------------------------------
To generate the inverse scrambling mapping-matrix of arnold transformation.
---------------------------------------------------------------------------*/
void r_random_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);
random_matrix(trans_r, trans_c, height, width);
printf("\nR_Random 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);
}
/*---------------------------------------------------------------------------
arnold transformation
---------------------------------------------------------------------------*/
void Random_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("Random : wrong types on stack", warning);
return;
}
srcIm = (Imrect *) stack_pop(&amt;type);
*/
srcIm = read_image();
//srcIm = DrawRGB_proc();
width = srcIm->width;
height = srcIm->height;
if (width != height)
{
error("Random: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);
random_matrix(trans_r, trans_c, height, width);
/*------------------------------------------------*/
printf("\n\n=== Random 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/Random/30/";
strcpy(filename,filename0);
//strcat(filename,file_name);
strcat(filename,"Lena_Random_");
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("=== Random transform complete! ===\n");
}
/*---------------------------------------------------------------------------
inverse arnold transformation
---------------------------------------------------------------------------*/
void R_Random_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("Random : wrong types on stack", warning);
return;
}
srcIm = (Imrect *) stack_pop(&amt;type);
*/
srcIm = read_image();
width = srcIm->width;
height = srcIm->height;
if (width != height)
{
error("R_Random: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_random_matrix(trans_r, trans_c, height, width);
/*------------------------------------------------*/
printf("\n\n=== R_Random 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/Random/";
strcpy(filename,filename0);
strcat(filename,file_name);
strcat(filename,"+R_Random_");
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_Random 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 Random_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("Random 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/Random/Lena.gif");
tv = tv_create("Random original");
tv1 = tv_create("Random 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("R0 = ", &amt;R0, 10);
tw_iglobal("LAMBDA = ", &amt;LAMBDA, 10);
tw_iglobal("MOD = ", &amt;MOD, 10);
tw_newrow();
tw_iglobal("Scrambling times = ", &amt;SCRAM_n, 5);
tw_newrow();
tw_button("Random", Random_Trans,NULL);
tw_button("R_Random", R_Random_Trans,NULL);
tw_newrow();
tw_newrow();
tw_end_tool();
}