www.pudn.com > src.rar > repair.c


/********************************************************************
*
* image repair tool
*
********************************************************************/
#include <tina/all_tina.h>
#include "bmp.h"
static Tv *tv = NULL;
static Tv *tv1=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 repair_path = 0;

static char filename[MAXPATHLEN];
static Imrect *orig_im =NULL;
static Imrect *dest_im = NULL;

static int MCOUNT = 1;

extern double get_med(double data,int n);

static void repair_choice_proc(int val)
{
repair_path = 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);
srcIm = ReadGIF(pathname, 1);
/*------------------------------------------------*/
return(srcIm);
}
static void mypush_proc(void)
{
Imrect *srcIm,*srcIm0;
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);
}
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("repair : wrong types on stack", warning);
return;
}
srcIm = (Imrect *) stack_pop(&amt;type);
return srcIm;
}
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("repair : wrong types on stack", warning);
return;
}
srcIm = (Imrect *) stack_pop(&amt;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 mysave_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("repair : wrong types on stack", warning);
return;
}
srcIm = (Imrect *) stack_pop(&amt;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");
printf("saved the image :>s\n",filename);
gif_write_file(srcIm0,filename);
mono_image_set(srcIm0);
stack_push(srcIm0, IMRECT, im_free);
}

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

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

orig_im = im_copy(srcIm);
tv_imrect2(tv, orig_im);

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("[>d,>d]=>f \n",i,j,pix);
}

printf("start repair!\n");
char filename0[64];
strcpy(filename0,"repair");
float cutpix;
switch( repair_path )
{
case 0:
cutpix = 254.56789;
break;
case 1:
cutpix = 254.0;
break;
default:
break;
}


int adapt =1;
int m_count =0;
float gray_sum = 0.0;
float pixm;
int t,h;
int count_adapt[25];
const float f = 0.000001;
for(i = 0;i < 25; i ++)
count_adapt[i] = 0;
for(i = 0;i < height; i ++)
for(j = 0;j < width; j ++)
{
pix = im_get_pixf(srcIm0,i,j);
adapt = 1;
if(fabs(pix - cutpix) < f )//lost point
{
m_count = 0;
gray_sum = 0.0;
while(m_count < MCOUNT )//at least have MCOUNT points in the mask
{
m_count = 0;
gray_sum = 0.0;
for(t = i - adapt; t <= i + adapt; t ++)
for(h = j - adapt; h <= j + adapt; h ++)
{
if((t<0)||(t>=height)||(h<0)||(h>=width))
continue;
pixm = im_get_pixf(srcIm0,t,h);
//printf("\n(>d,>d)n[>d->d]=>f ",i,j,t,h,pixm);
if(fabs(pixm - cutpix) > f)//orig point
{
//printf("[>d,>d][>d->d]fabs>f ",i,j,t,h,fabs(pixm - cutpix));
gray_sum += pixm;
m_count ++;
}
}
if(m_count < MCOUNT)
adapt ++;
}
//printf("gray=>f,m_c=>d ",gray_sum,m_count);
pixm = (float)(gray_sum/m_count);//mean
count_adapt[adapt] ++;
//IM_PIX_SET(destIm,i,j,pix);
im_put_pixf(pixm,destIm,i,j);
//printf("@>f ",pix);
//printf("[>d,>d]=>f ",i,j,pix);
}
else
{
//IM_PIX_SET(destIm,i,j,pix);
im_put_pixf(pix,destIm,i,j);
count_adapt[0] ++;
}
}
/*----------------------------------------------*/
for(i = 0;i < 25; i ++)
if(count_adapt[i] != 0)
printf("\ncount_adapt[>d] = >d",i,count_adapt[i]);
strcat(filename, filename0);
/*------------------------------------------------*/
dest_im = im_copy(destIm);
tv_imrect2(tv1, dest_im);
mono_image_set(destIm);
stack_push(destIm, IMRECT, im_free);
im_free(srcIm0);
printf("\n=== >s complete! ===\n",filename0);
}
static void fulldrawo(Tv *tv)
{
tv_imrect2(tv, orig_im);
}

static void fulldrawd(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);
}

static void tool_proc(void)
{
static int x = 200, y = 330;
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 repair_tool(int x, int y)
{
static void *tool = NULL;
if (tool)
{
tw_show_tool(tool);
return;
}
tool = (void *)tw_tool("Image repair 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/repair/Lena25.gif");
tv = tv_create("repair original");
tv1 = tv_create("repaired"); tw_choice("Tv choice", tv_choice_proc, 0, "original","repaired", NULL);
tv_set_fulldraw(tv, fulldrawo);
tv_set_fulldraw(tv1,fulldrawd);
/*---------------------------*/
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();
strcpy(filename,"result/repair/");
strcat(filename,file_name);
/*---------------------------*/
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();
tw_choice("repair from ", repair_choice_proc, 0,\
"stack", "image", NULL);
tw_newrow();
tw_iglobal("not less than ", &amt;MCOUNT, 5);
tw_label("original points in the mask ");
tw_newrow();
tw_label("image repaired method:");
tw_newrow();
tw_button("mean",mean_repair_proc,NULL);

tw_newrow();
tw_end_tool();
}