/* getTiles.c * * finds the puzzle board on the image and outputs the Tiles * * Input: one GrayImage * * * by Victor A. Dan, Dan Knights, Laura Matefy **** for CX 336 Project */ static char usage[] = "usage:\n %s inputPuzzleImage jth of puzzle, ith of puzzle\n note: i and j less than or equal to 4\n"; #include #include /* for INT_MAX */ #include #include #include "mcimg.h" #include /* comment out for speed later! */ //#define IMAGE_CHECKALL #define prop 1.083 #define err 9//to avoid error from rPounding void getBoard(GrayImage im, GrayImage out) { int S,F, i, j; float f, f1; float width = imGetWidth(im); int width1 = imGetWidth(out); int height1 = imGetHeight(out); f=width * (prop -1); S = ROUND(f); f1= width - f; F = ROUND(f1); //fprintf(stderr, "%d, %d, %d\n", S, F, width1); for(j=S;j= 0 && j-S>=0 && i-S < width1 && j-S < height1) imRef(out, i-S, j-S) = imRef(im, i, j); } } void getTile(GrayImage im, GrayImage out, int i, int j) { //we can be sure that i <=3 and j<=3 : see MAIN() int width = imGetWidth(im); int height = imGetHeight(im); int x=0, y=0, ix, iy; float x1 = width/4; float y1 = height/4; x= ROUND(x1); y=ROUND(y1); for(iy=0; iy=0 && Y>=0 ) imRef(out, ix, iy)=imRef(im, X, Y); } } int main(int argc, char **argv) { char *im1name; char s[100]; GrayImage im1,im3, out; int argnum=1,x, y; float f, f1, width; int SIZE1, SIZE2, SIZE; if (argc!= 2 ) { fprintf(stderr, usage, argv[0]); exit(1); } im1name = argv[argnum++]; im1 = loadGrayImage(im1name); width = imGetWidth(im1); if(!(width > 0)) exit(0); f=width * (prop -1); f1 = width - 2*f; SIZE=ROUND(f); SIZE1 = ROUND(f1); SIZE2 = ROUND(SIZE1/4); if(SIZE1<=0) exit(0); if(SIZE2-err <= 0) exit(0); im3 = newGrayImage(SIZE1, SIZE1); getBoard(im1, im3); for(x=0; x<4;x++) for(y=0; y<4;y++) { char outName[100]; sprintf(outName, "out%d", x+1 + 4*y); out = newGrayImage(SIZE2-err, SIZE2-err); getTile(im3, out, x, y); sprintf(s, "tile%d.pgm", x+1 + 4*y); saveGrayImage(out, s); imFree(out); } //fprintf(stderr, "I'm running\n"); //imFree(out); return(0); }