/*********************************************** * * file d:\cips\scale.c * * Functions: This file contains * zoom_image_array * shrink_image_array * interpolate_pixel * average_pixel * median_pixel * get_scaling_options * blank_image_array * * Purpose: * These functions implement image array * zooming (enlarging) and shrinking. * * External Calls: * wtiff.c - round_off_image_size * create_file_if_needed * write_array_into_tiff_image * tiff.c - read_tiff_header * rtiff.c - read_tiff_image * numcvrt.c - get_integer * filter.c - median_of * * Modifications: * 8 April 1992 - created * *************************************************/ #include "cips.h" /******************************************* * * zoom_image_array(... * * This function zooms in on an input * image array. It zooms by enlarging * an input image array and writing the * resulting image arrays to an output * image file. It can zoom or enlarge by * a factor of 2 or 4. * * You can zoom or enlarge an image array * by using either the replication or * interpolation methods. * *******************************************/ zoom_image_array(in_name, out_name, the_image, out_image, il, ie, ll, le, scale, method) char in_name[], out_name[], method[]; int il, ie, ll, le, scale; short the_image[ROWS][COLS], out_image[ROWS][COLS]; { int A, B, a, b, count, factor, i, j, length, width; struct tiff_header_struct image_header; /****************************************** * * Check the scale factor. If it is not * a valid factor (2 or 4), then set * it to 2. * ******************************************/ factor = scale; if(factor != 2 && factor != 4) factor = 2; create_file_if_needed(in_name, out_name, out_image); /******************************************* * * Let me try to explain the nested loops * shown below. * * We will enlarge the_image by the factor. * Therefore, we want to divide the_image * into parts of size ROWS/factor by * COLS/factor. We will loop through * the_image parts ROWS/factor and COLS/factor * using the loops over i and j. * * We divided the_image into parts so we must * include all of these parts. That is why we * do the loops over A and B. There are * factor*factor parts. * * Finally, we do the loops over a and b. * We must replicate every element in the_image * factor*factor times and put these into * out_image. The a and b loops perform this * task. * * The equations inside the []'s for * the_image and out_image are also confusing. * If you work them out, you'll see that we * are bouncing through the image arrays * and fitting the pixels into the right * places. * * The final proof is that this works. * * One final note: the factor should divide * evenly into ROWS. For example, ROWS=100 * so using a factor of 8 is not good. * 100/8 = 12.5 and this leave you with * an empty strip along the right and * bottom edges of the out_image. * *******************************************/ /***************************************** * * Replication method * ******************************************/ if(method[0] == 'r' || method[0] == 'R'){ read_tiff_image(in_name, the_image, il, ie, ll, le); count = 1; for(A=0; A 0) y = 1; if(b > 0) x = 1; diff = the_image[y+i+A*ROWS/factor][x+j+B*COLS/factor] - the_image[i+A*ROWS/factor][j+B*COLS/factor]; /****************************************** * * If you are at the edge of the input image * array, then you cannot interpolate to the * next point because there is no next point. * Therefore, set the diff to 0. * *******************************************/ if((y+i+A*ROWS/factor) >= ROWS) diff = 0; if((x+j+B*COLS/factor) >= COLS) diff = 0; num = a+b; if(num > factor) num = factor; result = the_image[i+A*ROWS/factor][j+B*COLS/factor] + num*diff/factor; return(result); } /* ends interpolate_pixel */ /******************************************* * * shrink_image_array(... * * This function shrinks a part of an * image. It takes a part of an input * image (described by il1, ie1, ll1, le1) * shrinks a 200x200 or 400x400 area down * to a 100x100 array, and writes this result * to an output file. The location in the * output file is described by il2, ie2, * ll2, le2. * * You can shrink the input image area * by using either the averaging, median, * or corner method. * *******************************************/ shrink_image_array(in_name, out_name, the_image, out_image, il1, ie1, ll1, le1, il2, ie2, ll2, le2, scale, method) char in_name[], out_name[], method[]; int il1, ie1, ll1, le1, il2, ie2, ll2, le2, scale; short the_image[ROWS][COLS], out_image[ROWS][COLS]; { int A, B, a, b, count, factor, i, j, length, width; struct tiff_header_struct image_header; /****************************************** * * Check the scale factor. If it is not * a valid factor (2 or 4), then set * it to 2. * ******************************************/ factor = scale; if(factor != 2 && factor != 4) factor = 2; create_file_if_needed(in_name, out_name, out_image); read_tiff_header(in_name, &image_header); /********************************************** * * Let me try to explain the nested loops * shown below. * * We will shrink the_image by the factor. * Therefore, we want to read in factor*factor * image arrays and produce one ROWS by COLS * array for writing to disk. * That is why we loop over A and B. * * We want to set every pixel in the out_image * array so we loop over i and j. * * The equations inside the out_image []'s * look a little strange. What we are doing is * setting every element and moving over every * time through the loops over A and B. * The first loop is for i=0,49 then i=50,99 * for a factor=2 and ROWS=100. * * The final proof is that this works. * * One final note: the factor should divide * evenly into ROWS. For example, ROWS=100 * so using a factor of 8 is not good. * 100/8 = 12.5 and this leave you with * an empty strip along the right and * bottom edges of the out_image. * ************************************************/ /******************************** * * Corner method * *********************************/ if(method[0] == 'c' || method[0] == 'C'){ count = 1; for(A=0; A