/*************************************************************************** * Func: bilinear * * * * Desc: performs a bilinear transformation and writes out image file * * This code executes a simple bilinear transformation to transform * * a quadrilateral into a rectangle * * * * Params: buffer - pointer to image in memory * * cols - number of columns in image * * rows - number of rows in image * * fileout - name of output file * * u0, v0, u1, v1, u2, v2, u3, v3 - destination control points * ***************************************************************************/ void bilinear(void *buffer, int cols, int rows, int type, char *fileout, double u0, double u1, double u2, double u3, double v0, double v1, double v2, double v3) { double u01, u23; /* horizontal interpolation variables */ double v03, v12; /* vertical interpolation variables */ int u,v; /* integer coordinates to source image */ double floatu, floatv; /* floating point coordinates to source */ int i,j; /* loop variables */ double x,y; /* coordinates of destination image */ unsigned long sourcebase; /* one-dimensional index to image */ image_ptr line_buff; /* buffer for one line of gray image */ pixel_ptr pixel_buff; /* buffer for one line of pixels */ image_ptr source; /* pointer to gray image */ pixel_ptr pixel_source; /* pointer to color image */ FILE *fp; /* file pointer to output file */ /* image specific (gray vs. color) customizations */ switch(type) { case 5: source = (image_ptr) buffer; line_buff = (image_ptr) malloc(cols); break; case 6: pixel_source = (pixel_ptr) buffer; pixel_buff = (pixel_ptr) malloc(cols * sizeof(pixel_ptr)); break; default: printf("Bad type value\n"); exit(1); break; } /* open output file */ if((fp=fopen(fileout, "wb")) == NULL) { printf("Unable to open %s for output\n", fileout); exit(1); } /* write out header */ fprintf(fp, "P%d\n%d %d\n255\n", type, cols, rows); for(i=0; i=cols) || (u<0) || (v>=rows) || (v<0)) sourcebase = 0; else sourcebase = (unsigned long) v * cols + u; if(type == 6) { pixel_buff[j].r = pixel_source[sourcebase].r; pixel_buff[j].g = pixel_source[sourcebase].g; pixel_buff[j].b = pixel_source[sourcebase].b; } else line_buff[j] = source[sourcebase]; } if(type == 6) fwrite(pixel_buff, sizeof(pixel), cols, fp); else fwrite(line_buff, 1, cols, fp); } fclose(fp); }