/* * compare.c * * * * Copyright (C) 2002 - 2002 Benninghaus, Rottweil * EMail: el@eb-themen.de * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef _compare_c #define _compare_c #ifdef __cplusplus extern "C" { #endif comparevalues cv1,cv2; int POSOFFSET( int x, int y ) { int res; res = y * WIDTH; res = res + x; return( res ); } void drawCross( unsigned char *buf, int x, int y, int len, unsigned char val ) { int off,offset,mlen; unsigned char *p; offset = POSOFFSET(x,y); p = &buf[offset]; if(len <= 0){ p[0] = val; return; } /*middle x point, and line (len) to right*/ off = 0; while( ( off <= len ) && ( ( x + off ) < WIDTH ) ){ p[off] = val; off++; } /* middle x point - 1, and line ( len - 1 ) left*/ mlen = len * -1; off = -1; while( ( off >= mlen ) && ( ( x + off ) >= 0 ) ){ p[off] = val; off--; } /*middle y point - 1, and line ( len - 1 ) up*/ mlen = len * -1; off = -1; while( ( off >= mlen ) && ( ( y + off ) >= 0 ) ){ offset = POSOFFSET( x, y + off ); p = &buf[offset]; p[0] = val; off--; } /*middle y point + 1, and line ( len - 1 ) down*/ off = 1; while( ( off <= len ) && ( ( y + off ) < HEIGHT ) ){ offset = POSOFFSET( x, y + off ); p = &buf[offset]; p[0] = val; off++; } } /* ######################################################################## */ int find( unsigned char *buf, filtervalues *fv, comparevalues *cv ) { int x,y,sx,sy,ex,ey,offset; unsigned char *p; int ct = 0; cv->left = 0; cv->right = 0; cv->up = 0; cv->down = 0; cv->prev_left = cv->left; cv->prev_right = cv->right; cv->prev_up = cv->up; cv->prev_down = cv->down; cv->valid = 0; if( ( fv->mincol >= fv->maxcol) || ( fv->nr_bright < 1 ) || ( fv->nr_dark < 1) ){ return(0); } ct = 0; while(ct < 5){ cv->spot[ct].x = -1; cv->spot[ct].y = -1; ct++; } x = fv->max.x; y = fv->max.y; offset = POSOFFSET( 0, y); p = &buf[offset]; ex = x; sx = x; while ( ( ( ex + 1 ) < WIDTH ) && (p[ex + 1] == 255 ) ){ ex++; } while ( ( ( sx - 1 ) >= 0 ) && ( p[sx - 1] == 255 ) ){ sx--; } if(ex < sx){ return(0); } cv->spot[0].x = ex - sx; cv->spot[0].x = cv->spot[0].x / 2; cv->spot[0].x = cv->spot[0].x + sx; x = cv->spot[0].x; ey = y; sy = y; while ( ey < HEIGHT ){ offset = POSOFFSET( x , ey ); p = &buf[offset]; if(p[0] != 255){ break; } ey++; } ey--; while ( sy >= 0 ){ offset = POSOFFSET( x , sy ); p = &buf[offset]; if(p[0] != 255){ break; } sy--; } sy++; if(ey < sy){ return(0); } y = ey - sy; y = y / 2; y = y + sy; cv->spot[0].y = y; cv->spot[1].y = y; cv->spot[2].y = y; cv->spot[3].y = sy; cv->spot[3].x = x; cv->spot[4].y = ey; cv->spot[4].x = x; /*now x,y points to the center of spot*/ sx = x; ex = x; offset = POSOFFSET( 0, y); p = &buf[offset]; while ( ( ( ex + 1 ) < WIDTH ) && (p[ex + 1] == 255 ) ){ ex++; } while ( ( ( sx - 1 ) >= 0 ) && ( p[sx - 1] == 255 ) ){ sx--; } if(ex < sx){ return(0); } cv->spot[1].x = sx; cv->spot[2].x = ex; while( cv->valid < 5 ){ if( ( cv->spot[cv->valid].x < 0 ) || ( cv->spot[cv->valid].y < 0 ) ){ cv->valid = 0; return(0); } cv->valid++; } return( cv->valid ); } int compare( unsigned char *buf ) { int ct,diffx,diffy,seeing; cv1.left = 0; cv1.right = 0; cv1.up = 0; cv1.down = 0; cv1.changed = 0; if( ! find( buf, &fv2, &cv2 ) ){ return(0); } seeing = cfg.seeing * -1; ct = 1; while(ct < 5){ diffx = cv2.spot[ct].x - cv1.spot[ct].x; diffy = cv2.spot[ct].y - cv1.spot[ct].y; if( ( diffx > cfg.seeing) || ( diffy > cfg.seeing ) || ( diffx < seeing) || ( diffy < seeing ) ){ break; } ct++; } if(ct >= 5 ){ return(1); } if( diffx > cfg.seeing ){ cv1.right = 1; } else if( diffx < seeing ) { cv1.left = 1; } if( diffy > cfg.seeing ){ cv1.up = 1; } else if( diffy < seeing ) { cv1.down = 1; } if( (cv1.prev_right != cv1.right) || (cv1.prev_left != cv1.left) || (cv1.prev_up != cv1.up) || (cv1.prev_down != cv1.down) ){ cv1.changed = 1; cv1.prev_left = cv1.left; cv1.prev_right = cv1.right; cv1.prev_up = cv1.up; cv1.prev_down = cv1.down; } return(2); } #ifdef __cplusplus } #endif #endif /*_compare_c*/