📄 scale.c
字号:
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<factor; A++){
for(B=0; B<factor; B++){
printf("\n shrinking by corner %3d of %3d",
count++, factor*factor);
if(image_header.image_length < ll1+A*ROWS ||
image_header.image_width < le1+B*COLS)
blank_image_array(the_image);
else
read_tiff_image(in_name, the_image,
il1+A*ROWS, ie1+B*COLS,
ll1+A*ROWS, le1+B*COLS);
for(i=0; i<ROWS/factor; i++){
for(j=0; j<COLS/factor; j++){
out_image[i+A*ROWS/factor][j+B*COLS/factor] =
the_image[factor*i][factor*j];
} /* ends loop over j */
} /* ends loop over i */
} /* ends loop over B */
} /* ends loop over A */
write_array_into_tiff_image(out_name, out_image,
il2, ie2, ll2, le2);
} /* ends corner method */
/******************************
*
* Average Method
*
******************************/
if(method[0] == 'a' || method[0] == 'A'){
count = 1;
for(A=0; A<factor; A++){
for(B=0; B<factor; B++){
printf("\n shrinking by average %3d of %3d",
count++, factor*factor);
if(image_header.image_length < ll1+A*ROWS ||
image_header.image_width < le1+B*COLS)
blank_image_array(the_image);
else
read_tiff_image(in_name, the_image,
il1+A*ROWS, ie1+B*COLS,
ll1+A*ROWS, le1+B*COLS);
for(i=0; i<ROWS/factor; i++){
for(j=0; j<COLS/factor; j++){
out_image[i+A*ROWS/factor][j+B*COLS/factor] =
average_pixel(the_image, factor, i, j);
} /* ends loop over j */
} /* ends loop over i */
} /* ends loop over B */
} /* ends loop over A */
write_array_into_tiff_image(out_name, out_image,
il2, ie2, ll2, le2);
} /* ends average method */
/************************
*
* Median Method
*
*************************/
if(method[0] == 'm' || method[0] == 'M'){
count = 1;
for(A=0; A<factor; A++){
for(B=0; B<factor; B++){
printf("\n shrinking by median %3d of %3d",
count++, factor*factor);
if(image_header.image_length < ll1+A*ROWS ||
image_header.image_width < le1+B*COLS)
blank_image_array(the_image);
else
read_tiff_image(in_name, the_image,
il1+A*ROWS, ie1+B*COLS,
ll1+A*ROWS, le1+B*COLS);
for(i=0; i<ROWS/factor; i++){
for(j=0; j<COLS/factor; j++){
out_image[i+A*ROWS/factor][j+B*COLS/factor] =
median_pixel(the_image, factor, i, j);
} /* ends loop over j */
} /* ends loop over i */
} /* ends loop over B */
} /* ends loop over A */
write_array_into_tiff_image(out_name, out_image,
il2, ie2, ll2, le2);
} /* ends median method */
} /* ends shrink_image_array */
/***********************************************
*
* average_pixel(...
*
* This function calculates the average
* pixel value of a factor x factor array
* of pixels inside the the_image array.
* The coordinates i and j point to the upper
* left hand corner of the small array.
*
***********************************************/
average_pixel(the_image, factor, i, j)
int factor, i, j;
short the_image[ROWS][COLS];
{
int a, b, result = 0;
for(a=0; a<factor; a++)
for(b=0; b<factor; b++)
result = result +
the_image[factor*i+a][factor*j+a];
result = result/(factor*factor);
return(result);
} /* ends average_pixel */
/***********************************************
*
* median_pixel(...
*
* This function calculates the median
* pixel value of a factor x factor array
* of pixels inside the the_image array.
* The coordinates i and j point to the upper
* left hand corner of the small array.
*
***********************************************/
median_pixel(the_image, factor, i, j)
int factor, i, j;
short the_image[ROWS][COLS];
{
int a, b, count, ff, result = 0;
short *elements;
ff = factor*factor;
elements = (short *) malloc(ff * sizeof(short));
count = 0;
for(a=0; a<factor; a++){
for(b=0; b<factor; b++){
elements[count] =
the_image[factor*i+a][factor*j+b];
count++;
}
}
result = median_of(elements, &ff);
free(elements);
return(result);
} /* ends median_pixel */
/***********************************************
*
* get_scaling_options(...
*
* This function queries the user for the
* parameters needed to perform scaling.
*
***********************************************/
get_scaling_options(zoom_shrink, scale, method)
int *scale;
char method[], zoom_shrink[];
{
int not_finished = 1, response;
while(not_finished){
printf("\n\t1. Zoom or Shrink is - %s",
zoom_shrink);
printf("\n\t2. Scale factor is %d", *scale);
printf("\n\t3. Scaling Method is - %s", method);
printf(
"\n\t Replication or Interpolation for Zooming"
"\n\t Averaging Median or Corner for Shrinking");
printf("\n\n\tEnter choice (0 = no change) _\b");
get_integer(&response);
if(response == 0){
not_finished = 0;
}
if(response == 1){
printf("\nEnter Zoom or Shrink (z or s) __\b");
gets(zoom_shrink);
}
if(response == 2){
printf("\nEnter Scale Factor (2 or 4) __\b");
get_integer(scale);
}
if(response == 3){
printf("\nEnter Scaling Method: "
"Replication or Interpolation for Zooming"
"\n "
"Averaging Median or Corner for Shrinking"
"\n\t__\b");
gets(method);
}
} /* ends while not_finished */
} /* ends get_scaling_options */
/***********************************************
*
* blank_image_array(...
*
* This function blanks out an image array
* by filling it with zeros.
*
***********************************************/
blank_image_array(image)
short image[ROWS][COLS];
{
int i, j;
for(i=0; i<ROWS; i++)
for(j=0; j<COLS; j++)
image[i][j] = 0;
} /* ends blank_image_array */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -