📄 sobseq.c~
字号:
#include <tina/all_tina.h>#include "bmp.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 Tv *tv = NULL;static Tv *tv1=NULL;#define MAXBIT 30
#define MAXDIM 6static int iminarg1,iminarg2;
#define IMIN(a,b) (iminarg1 = (a),iminarg2 = (b),(iminarg1) < (iminarg2) ? (iminarg1) : (iminarg2))/*-------------------------sobseq adding funtions-----------------------------------*/void sobseq(int *n,float x[])
{
int j,k,l;
unsigned long i,im,ipp;
static float fac;
static unsigned long in,ix[MAXDIM + 1],*iu[MAXBIT +1];
static unsigned long mdeg[MAXDIM +1] = {0,1,2,3,3,4,4};
static unsigned long ip[MAXDIM + 1] = {0,0,1,1,2,1,4};
static unsigned long iv[MAXDIM * MAXBIT + 1] = {0,1,1,1,1,1,1,3,1,3,3,1,1,5,7,7,3,3,5,15,11,5,15,13,9};
if (*n < 0)
{
for (k = 1;k <= MAXDIM;k ++)
ix[k] = 0;
in = 0;
if (iv[1] != 1)
return;
fac = 1.0/(1L << MAXBIT);
for (j = 1,k = 0;j <=MAXBIT;j ++,k += MAXDIM)
iu[j] =&iv[k];
for (k =1;k <=MAXDIM;k ++)
{
for (j =1;j <=(int)mdeg[k];j ++)
iu[j][k] <<= (MAXBIT - j);
for (j = mdeg[k] + 1;j <= MAXBIT;j ++)
{
ipp =ip[k];
i = iu[j-mdeg[k]][k];
i ^= (i >> mdeg[k]);
for (l = mdeg[k] - 1;l >= 1;l --)
{
if (ipp & 1)
i ^= iu[j-1][k];
ipp >>= 1;
}
iu[j][k] = i;
}
}
}
else
{
im = in ++;
for (j =1; j <= MAXBIT; j ++)
{
if (!(im & 1))
break;
im >>= 1;
}
if (j > MAXBIT)
nrerror("MAXBIT too small in sobseq");
im = (j - 1)*MAXDIM;
for (k = 1;k <= IMIN(*n,MAXDIM);k ++)
{
ix[k] ^= iv[im+k];
x[k] = ix[k]*fac;
}
}
}
void init_sobol_sq(int* dest, int nMAX){ int Start = 0.0; int End = nMAX; int dim = -1;
float f[2];
int i;
sobseq(&dim,f);
dim = 1;
int sq[nMAX]; int flag[nMAX];
for (i = 0;i < nMAX;i ++)
{
sobseq(&dim,f);
sq[i] = floor(f[1]*(End-Start)+Start);
//printf("%d %d %f %f\n",i,sq[i],f[1],f[1]*(End-Start)+Start);
flag[i] = 0;
dest[i] = i;
}
int count=0;
int temp;
for (i = 0;i < nMAX;i ++)
{
temp = sq[i];
if(flag[temp] == 1)
{
printf("NO.%d VS NO.%d ----> sqnew[%d]\n",i,dest[temp],temp);
count ++;
//dest[0] = dest[temp];
//dest[temp] = i;
}
if(flag[temp] == 0)
{
dest[temp] = i;
flag[temp] = 1;
}
}
dest[0] = nMAX-1;}extern void sobseq_matrix(int** r, int** c, int height, int width)
{
int i, j, x, y, x1, y1;
printf("\nSobseq transform\n");
int *dest; int m = height*width; dest = int_vector(m); init_sobol_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);
}extern void r_sobseq_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); sobseq_matrix(trans_r, trans_c, height, width); printf("\nR_Sobseq 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);}void Sobseq_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("Sobseq : 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("Sobseq: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); sobseq_matrix(trans_r, trans_c, height, width);/*------------------------------------------------*/
printf("\n\n=== Sobseq 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/Sobseq/"; strcpy(filename,filename0); //strcat(filename,file_name); strcat(filename,"Lena_Sobseq_"); 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("=== Sobseq transform complete! ===\n");
}
void R_Sobseq_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("Sobseq : 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_Sobseq: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_sobseq_matrix(trans_r, trans_c, height, width);/*------------------------------------------------*/
printf("\n\n=== R_Sobseq 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/Sobseq/"; strcpy(filename,filename0); strcat(filename,file_name); strcat(filename,"+R_Sobseq_"); 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_Sobseq 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 Sobseq_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("Sobseq 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/Sobseq/Lena.gif"); tv = tv_create("Sobseq original");
tv1 = tv_create("Sobseq 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("Sobseq", Sobseq_Trans,NULL); tw_button("R_Sobseq", R_Sobseq_Trans,NULL); tw_newrow(); tw_newrow();
tw_end_tool();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -