/* scalePuzzle.c * * scales the puzzle to the model's "SIZE" * * Input: one GrayImage * * * by Victor A. Dan, Dan Knights, Laura Matefy **** for CX 336 Project */ static char usage[] = "usage:\n %s inputPuzzleImage\n"; #include #include /* for INT_MAX */ #include #include #include "mcimg.h" //--------------WARNING-------------------------------------------------- //----------------------------------------------------------------------- /*if the initial image is smaller than the size it won't do anything ===>change the focus !!!!*/ //----------------------------------------------------------------------- /* comment out for speed later! */ #define IMAGE_CHECKALL #define size 212 //size of models' image void multiply(double T[9], double R[3], double V0, double V1) { //returns the vector obtained by multiplying the matrix M with the vector V R[0] = T[0]*V0 + T[1]*V1 + T[2]; R[1] = T[3]*V0 + T[4]*V1 + T[5]; R[2] = T[6]*V0 + T[7]*V1 + T[8]; } void invert(double T[9], double I[9]) { //returns the inverse of the matrix T I[0]= T[4]*T[8] - T[7]*T[5]; I[3]= T[5]*T[6] - T[3]*T[8]; I[6]= T[3]*T[7] - T[6]*T[4]; I[1]= T[7]*T[2] - T[1]*T[8]; I[4]= T[0]*T[8] - T[6]*T[2]; I[7]= -T[0]*T[7] + T[1]*T[6]; I[2]= T[1]*T[5] - T[4]*T[2]; I[5]= T[3]*T[2] - T[0]*T[5]; I[8]= T[0]*T[4] - T[3]*T[1]; } void linearInterpolation(GrayImage img0, GrayImage out1, double Tinv[9], double height, double width) { //returns an image with a linear interpolation double Result[3]; int i,j; int x0,y0, x1, y1; double x=0,y=0; double w = ((imGetWidth(img0))/2); double h = ((imGetHeight(img0))/2); double dx, dy; out1= newGrayImage(imGetWidth(img0)+width, imGetHeight(img0)+height); for(j = 0; j < (int)(height+imGetHeight(img0)); ++j) for(i=0; i < (int)(width+imGetWidth(img0)); ++i) { multiply(Tinv, Result, i-w-width/2, j-h-height/2); x = Result[0]/Result[2] + w; y = Result[1]/Result[2] + h; x0 = ((int)(x+1.0))-1; y0 = ((int)(y+1.0))-1; x1 = x0+1; y1 = y0+1; dx =( x - x0); dy = (y - y0); if ( x0 < imGetWidth(img0)&& y0 < imGetHeight(img0) && (x1 > 0) && (y1 > 0) ) { int a00 =0; int a10 =0; int a01 =0; int a11 =0; if (x0>0 && y0>0 ) { a00=imRef(img0, x0, y0); } if(x1 < (imGetWidth(img0))&& (y1 < (imGetHeight(img0)))) { a11=imRef(img0, x1, y1); } if(x0 > 0 && y1 < imGetHeight(img0)) { a01=imRef(img0, x0, y1); } if(x1 0) { a10=imRef(img0, x1, y0); } imRef(out1, i, j) = (1-dx)*(1-dy)*a00 + dx*(1-dy)*a10 + (1-dx)*dy*a01 + dx*dy*a11; } // if(dy <= 0 || dx <=0) // imRef(out1, i, j) = 0; } saveGrayImage(out1, "primitive.pgm"); } void ScaleMatrix(double M[9], double a, double b) { //creates a Scaling matrix M[0] = a; M[1] = 0; M[2] = 0; M[3] = 0; M[4] = b; M[5] = 0; M[6] = 0; M[7] = 0; M[8] = 1; } int main(int argc, char **argv){ char *img0Name; int argnum = 1, width,w, i, j ; GrayImage img0, out1, out2, out0; double s; double Matrix[9], Inverse[9]; if(argc != 2) { fprintf(stderr, usage, argv[0]); exit(1); } img0Name = argv[argnum++]; img0= loadGrayImage(img0Name); width = imGetWidth(img0); s = (float)width/(float)size; out0= newGrayImage(width, width); ScaleMatrix(Matrix, s, s); invert(Matrix, Inverse); linearInterpolation(img0, out0, Matrix, 0, 0); out1= loadGrayImage("primitive.pgm"); //fprintf(stderr, "%d\n", width); //fprintf(stderr, "%f\n", s); out2= newGrayImage(size, size); w = imGetWidth(out1); //fprintf(stderr, "%d\n", w); for(j=0; j