/*********************************************************/ /* program 21: translate */ /*********************************************************/ #define XSIZE 640 #define YSIZE 480 #define RED 0 #define GREEN 1 #define BLUE 2 #define XOFF 65.4 #define YOFF 24.8 #define INTERP 1 main() { unsigned char *ir,*ig,*ib; unsigned char *or,*og,*ob; unsigned char *i,*o; long int ix,iy,c; long int ix0,iy0; long int ix1,iy1; long int ox,oy; long int cx,cy; double X,Y; double DX,DY; double A,B,C,D,AB,CD; double v; /* allocate input and output memory buffers */ ir = (unsigned char *) malloc (XSIZE*YSIZE); ig = (unsigned char *) malloc (XSIZE*YSIZE); ib = (unsigned char *) malloc (XSIZE*YSIZE); or = (unsigned char *) malloc (XSIZE*YSIZE); og = (unsigned char *) malloc (XSIZE*YSIZE); ob = (unsigned char *) malloc (XSIZE*YSIZE); /* read the input image */ read_image("input",ir,ig,ib); /* process all lines of image */ for (oy = 0; oy < YSIZE; oy++) { for (ox = 0; ox < XSIZE; ox++) { /* process the red, green, and blue planes one at a time */ for (c = RED; c <= BLUE; c++) { /* initialize pointers for current color plane */ if (c == RED ) i = ir; if (c == GREEN) i = ig; if (c == BLUE ) i = ib; if (c == RED ) o = or; if (c == GREEN) o = og; if (c == BLUE ) o = ob; if (INTERP == 0) { /* nearest neighbor */ ix = ox - (long int)(XOFF + 0.5); iy = oy - (long int)(YOFF + 0.5); if ((ix >= 0) && (ix < XSIZE) && (iy >= 0) && (iy < YSIZE)) o[(oy*XSIZE)+ox] = i[(iy*XSIZE)+ix]; else o[(oy*XSIZE)+ox] = 0x0; } else { /* bilinear interpolation */ X = (double)ox - XOFF; if (X >= 0.0) ix0 = (long)X; else ix0 = (long)(X-1.0); Y = (double)oy - YOFF; if (Y >= 0.0) iy0 = (long)Y; else iy0 = (long)(Y-1.0); ix1 = ix0 + 1; iy1 = iy0 + 1; DX = X - (double)ix0; DY = Y - (double)iy0; A = 0.0; B = 0.0; C = 0.0; D = 0.0; if ((ix0 >= 0) && (ix0 < XSIZE) && (iy0 >= 0) && (iy0 < YSIZE)) A = (float)i[(iy0*XSIZE)+ix0]; if ((ix1 >= 0) && (ix1 < XSIZE) && (iy0 >= 0) && (iy0 < YSIZE)) B = (float)i[(iy0*XSIZE)+ix1]; if ((ix0 >= 0) && (ix0 < XSIZE) && (iy1 >= 0) && (iy1 < YSIZE)) C = (float)i[(iy1*XSIZE)+ix0]; if ((ix1 >= 0) && (ix1 < XSIZE) && (iy1 >= 0) && (iy1 < YSIZE)) D = (float)i[(iy1*XSIZE)+ix1]; AB = (A * (1.0-DX)) + (B * DX); CD = (C * (1.0-DX)) + (D * DX); v = (AB * (1.0-DY)) + (CD * DY); o[(oy*XSIZE)+ox] = (unsigned char)v; } } } } /* write the output image */ write_image("output",or,og,ob); /* free memory buffers */ free(ir); free(ig); free(ib); free(or); free(og); free(ob); }