#include "util.h"
#include "param.h"
MV full_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV liu_full_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV part_full_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV coher_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV new_full_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV three_step_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV new_three_step_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV four_step_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV bbgd_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV ucbd_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV cd_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV scd_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV ncd_search(u_char *prev, u_char *cur, int x, int y, int *err); //New Cross Diamond Search
MV kcd_search(u_char *prev, u_char *cur, int x, int y, int *err); //Kite Cross Diamond Search
MV hsa_search(u_char *prev, u_char *cur, int x, int y, int *err);
MV cir_search(u_char *prev, u_char *cur, int x, int y, int rx, int ry, int *err, int b, int t, int l);
MV find_halfpel(int x, int y, MV mv, u_char *prev, u_char *cur, int bs, int *err);
void set_param(char *name);
void search(int method, MV **imv, u_char *prev, u_char *cur);
void fsearch(int method, MVF **mvt, MVF **mvb, u_char *prev, u_char *cur);
void compute_bound(int x, int y, int *ux, int *lx, int *uy, int *ly);
int bound_chk(int m, int n, int cx, int cy);
void v_init(void);
void v_end(void);
#ifdef OPC
OP op;
double ls;
#endif
int im_width, im_height, mv_width, mv_height, re_width, re_height;
int im_width2, im_height2, re_width2, re_height2;
int *point,spt;
u_char *res,*prev,*iprev,*cur;
MV **imv,**hmv;
MVF **mvt,**mvb;
clock_t start,end,over=0,net=0,nets;
int mvmap[3][3]={{6,0,0},{6,9,2},{4,4,2}};
int cirmv[8][2]={{-1,-1},{0,-1},{1,-1},{1,0},{1,1},{0,1},{-1,1},{-1,0}};
/* NPDS partial distortion calculation order */
const u_char LL[16]={0,2,2,0,1,3,3,1,1,3,0,2,3,1,2,0};
const u_char KK[16]={0,2,0,2,1,3,1,3,0,2,1,3,0,2,1,3};
/* 1 9 3 13 */
/* 11 5 15 7 */
/* 4 14 2 10 */
/* 16 8 12 6 */
/* new_full_search's variable */
ME **b1,**b2,*b10;
#define N1 12
int main(int argc, char *argv[])
{
FILE *fcur,*fmv,*fhmv,*fmse,*fpt;
#ifdef OPC
FILE *fop,*fops=NULL;
#endif
int i,s=0,method;
double mse;
u_char *b_tmp;
char char_buf[4096],imagename[80];
#ifndef NO_CAPTURE
char header[80];
FILE *fres;
#endif
char *usage = "\nsearch <image> <method>\n"
" method 1 = fs \n"
" method 2 = ofs \n"
" method 3 = 3ss \n"
" method 4 = 4ss \n"
" method 5 = n3ss \n"
" method 6 = bbgds \n"
" method 7 = hsa \n"
" method 8 = coher \n"
" method 9 = pfs \n"
" method 10 = liu \n"
" method 11 = ucbds \n";
" method 12 = cds \n"
" method 13 = scds \n";
" method 14 = ncds \n"
" method 15 = kcds \n";
char *methodname[15]={"fs","ofs","3ss","4ss","n3ss","bbgds","hsa","coher","pfs","liu","ucbds","cds","scds","ncds","kcds"};
if(argc!=3){
fprintf(stderr,"%s",usage);
exit(1);
}
strcpy(imagename,argv[1]);
set_param(imagename);
if(im_width==0){
fprintf(stderr,"\nImage not found!\n");
exit(1);
}
method=atoi(argv[2]);
if(method<1||method>13){
fprintf(stderr,"\nInvalid method!\n%s",usage);
exit(1);
}
v_init();
if(FIELD)
sprintf(char_buf,"%s/%s/%s.mvf.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
else
sprintf(char_buf,"%s/%s/%s.mv.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
printf("%s\n", char_buf);
if((fmv=fopen(char_buf,"wb"))==NULL)
fopen_err();
if(HALF){
sprintf(char_buf,"%s/%s/%s.hmv.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
if((fhmv=fopen(char_buf,"wb"))==NULL)
fopen_err();
}
#ifdef OPC
if(HALF)
sprintf(char_buf,"%s/%s/%s.hop.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
else
sprintf(char_buf,"%s/%s/%s.op.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
if((fop=fopen(char_buf,"wb"))==NULL)
fopen_err();
if(method==9){
if(HALF)
sprintf(char_buf,"%s/%s/%s.hops.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
else
sprintf(char_buf,"%s/%s/%s.ops.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
if((fops=fopen(char_buf,"wb"))==NULL)
fopen_err();
}
#endif
if(HALF)
sprintf(char_buf,"%s/%s/%s.hmse.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
else
sprintf(char_buf,"%s/%s/%s.mse.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
if((fmse=fopen(char_buf,"wb"))==NULL)
fopen_err();
sprintf(char_buf,"%s/%s/%s.pt.%s",RESULT_PATH,imagename,imagename,methodname[method-1]);
if((fpt=fopen(char_buf,"wb"))==NULL)
fopen_err();
if(CIF)
s=(strcmp(imagename,"garden")==0)+(strcmp(imagename,"missa")==0)+(strcmp(imagename,"claire")==0);
if(FIELD)
sprintf(char_buf,"%s/%s/%s%03d.raw",IMAGE_PATH,imagename,imagename,START_NO);
else
sprintf(char_buf,"%s/%s/%s.%03d",IMAGE_PATH,imagename,imagename,START_NO+s);
if((fcur=fopen(char_buf,"rb"))==NULL)
fopen_err();
readframe(fcur,cur,im_width,im_height);
fclose(fcur);
start=clock();
for(i=START_NO+s;i<=END_NO+s;i++){
end=clock();
swap(cur,prev,b_tmp);
if(FIELD)
sprintf(char_buf,"%s/%s/%s%03d.raw",IMAGE_PATH,imagename,imagename,i+1);
else
sprintf(char_buf,"%s/%s/%s.%03d",IMAGE_PATH,imagename,imagename,i+1);
if((fcur=fopen(char_buf,"rb"))==NULL)
fopen_err();
readframe(fcur,cur,im_width,im_height);
fclose(fcur);
#ifdef OPC
op.a=op.c=op.p=0;
ls=0;
#endif
over+=clock()-end;
if(FIELD)
fsearch(method,mvt,mvb,prev,cur);
else
search(method,imv,prev,cur);
end=clock();
if(FIELD)
fres_image(prev,res,mvt,mvb);
else if(HALF)
hres_image(iprev,res,imv,hmv);
else
res_image(prev,res,imv);
mse=mean_squ_err(cur,res,re_width,re_height);
fwrite(&mse,sizeof(double),1,fmse);
if(FIELD){
fwrite(*mvt,sizeof(MVF),mv_width*mv_height,fmv);
fwrite(*mvb,sizeof(MVF),mv_width*mv_height,fmv);
}else
fwrite(*imv,sizeof(MV),mv_width*mv_height,fmv);
if(HALF)
fwrite(*hmv,sizeof(MV),mv_width*mv_height,fhmv);
#ifdef OPC
fwrite(&op,sizeof(OP),1,fop);
if(method==9)
fwrite(&ls,sizeof(double),1,fops);
#endif
fwrite(point,sizeof(int),mv_width*mv_height,fpt);
#ifndef NO_CAPTURE
if(i+1==CAPTURE_NO){
sprintf(char_buf,"%s/%s/%s%03d%s.pgm",RESULT_PATH,imagename,imagename,i+1,methodname[method-1]);
if((fres=fopen(char_buf,"wb"))==NULL)
fopen_err();
pgm_header(header,re_width,re_height);
fwrite(header,sizeof(char),strlen(header),fres);
writeframe(fres,res,re_width,re_height);
fclose(fres);
}
#endif
/*fprintf(stdout,"\nFrame %d\n",i);*/
over+=clock()-end;
}
end=clock();
fprintf(stdout,"\nNet time=%f\tTotal time=%f\tOverhead=%f\nNet search time=%f\n",(double)(end-start-over)/CLOC