#include <stdio.h>
#include <stdlib.h>
#include "emvt.h"

unsigned char cur_block[BLOCK_SIZE][BLOCK_SIZE] ;
unsigned char ref_block[BLOCK_SIZE+2*EXCURSION][BLOCK_SIZE+2*EXCURSION] ;
char ref_dx,ref_dy ;

void emvt() {
   unsigned char i,j ;
   char dx,dy ;
   unsigned int  ref_dist ;
   unsigned int distance ;

   ref_dist = (1<<30)  ;
   for(dy=-EXCURSION;dy<EXCURSION;dy++) {
     for(dx=-EXCURSION;dx<EXCURSION;dx++) {
       distance = 0 ;
       for(i=0;i<BLOCK_SIZE;i++) {
         for(j=0;j<BLOCK_SIZE;j++) {
	  distance=distance+abs(cur_block[i][j]-ref_block[EXCURSION+i+dy][EXCURSION+j+dx]) ;
	 }
       }
       //printf("d:%d\n", distance) ;
       if (distance < ref_dist) {
        ref_dist=distance ;
	ref_dx = dx ;
	ref_dy = dy ;
      }
     }
   }
}
