/****************************************************************************** Image I/O and Image processing code Copyright 2000 by the University of Waterloo All rights reserved. Developed by: Richard Mann, Department of Computer Science ******************************************************************************/ #include #include #include #include #define min(A,B) ((A)<(B)?(A):(B)) #define max(A,B) ((A)>(B)?(A):(B)) #define abs(A) ((A)>(0)?(A):(-A)) #define sign(A,B) ((B)>=(0)?(A):(-(A))) #define sqr(A) ((A)*(A)) #define round(A) (floor(0.5+(A))) #ifndef PI #define PI M_PI #endif /* image accessor: image(x,y) */ #define I(imageP, nx, ny, x, y) imageP[x+y*nx] /* maximum # of image modes (excluding outlier) */ #define MAXMODES 4 void error(char *s1, char *s2) { fprintf(stderr, "ERROR: %s %s\n", s1, s2); exit(1); } /* * 2d interpolation of image pixel data. * Assumes point (x,y) within range of pixels array (ie., 0..nx-1,0..ny-1). */ float interp2(float *imageP, int nx, int ny, float x, float y) { int xl, xh, yl, yh; float dx, dy; float p1, p2, p3, p4; xl=(int)floor(x); xh=(int)ceil(x); yl=(int)floor(y); yh=(int)ceil(y); if (xl<0 || yl<0 || xh>=nx || yh>=ny) { fprintf(stderr, "INTERP2: x:%f, y:%f, nx:%d, ny:%d, xl:%d, xh:%d, yl:%d, yh:%d\n", x,y,nx,ny,xl,xh,yl,yh); exit(1); } dx=x-(float)xl; dy=y-(float)yl; p1=I(imageP,nx,ny,xl,yl); p2=I(imageP,nx,ny,xl,yh); p3=I(imageP,nx,ny,xh,yl); p4=I(imageP,nx,ny,xh,yh); return(dx*dy*p4+(1-dx)*dy*p3+dx*(1-dy)*p2+(1-dx)*(1-dy)*p1); } /* * 1d filter. Does work in place */ void filter_image_x(float *imageP, int nx, int ny, float *filtP, int nfilt) { float *rowP; float s; int c, x, y, fi, xi; c = (int)floor(nfilt/2.0); /* center of filter */ if ((rowP=(float*)malloc((size_t)nx*sizeof(float)))==NULL) error("Cannot allocate memory", "image_filter_x"); for (y=0; y=0 && xi=0 && yis) s=I(imageP,nx,ny,x,y); } } return(s); } float image_min(float *imageP, int nx, int ny) { int x,y; float s; s=I(imageP,nx,ny,0,0); for (y=0;y