home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_06 / 1106077a < prev    next >
Text File  |  1993-04-08  |  6KB  |  206 lines

  1.    /**************************************************
  2.    *
  3.    *   variance(...
  4.    *
  5.    *   This function replaces the pixel in the center
  6.    *   of a 3x3 area with the square root of the 
  7.    *   sum of squares of the differences between 
  8.    *   the center pixel and its eight neighbors.
  9.    *
  10.    ***************************************************/
  11.  
  12. variance(in_name, out_name, the_image, out_image,
  13.          il, ie, ll, le, threshold, high)
  14.    char   in_name[], out_name[];
  15.    int    il, ie, ll, le, high, threshold;
  16.    short  the_image[ROWS][COLS], 
  17.           out_image[ROWS][COLS];
  18. {
  19.    int a, b, diff, i, j, length, 
  20.        max, new_hi, new_low, width;
  21.    long sum = 0;
  22.  
  23.    struct tiff_header_struct image_header;
  24.  
  25.  
  26.    if(does_not_exist(out_name)){
  27.       printf("\n\noutput file does not exist %s",
  28.              out_name);
  29.       read_tiff_header(in_name, &image_header);
  30.       round_off_image_size(&image_header,
  31.                            &length, &width);
  32.       image_header.image_length = length*ROWS;
  33.       image_header.image_width  = width*COLS;
  34.       create_allocate_tiff_file(out_name, &image_header,
  35.                                 out_image);
  36.    }  /* ends if does_not_exist */
  37.  
  38.    read_tiff_header(in_name, &image_header);
  39.  
  40.    new_hi  = 250;
  41.    new_low = 16;
  42.    if(image_header.bits_per_pixel == 4){
  43.        new_hi  = 10;
  44.        new_low = 3;
  45.    }
  46.  
  47.    max = 255;
  48.    if(image_header.bits_per_pixel == 4)
  49.       max = 16;
  50.  
  51.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  52.  
  53.    for(i=1; i<ROWS-1; i++){
  54.       if( (i%10) == 0) printf("%3d", i);
  55.       for(j=1; j<COLS-1; j++){
  56.          sum = 0;
  57.          for(a=-1; a<=1; a++){
  58.              for(b=-1; b<=1; b++){
  59.                 if( a!=0  &&  b!=0){
  60.                  diff = 0;
  61.                  diff = the_image[i][j] - 
  62.                         the_image[i+a][j+b];
  63.                  diff = diff*diff;
  64.                  sum  = sum + diff;
  65.                 }
  66.              }
  67.          }
  68.          sum = sqrt(sum);
  69.          if(sum > max) sum = max;
  70.          out_image[i][j] = sum;
  71.       }  /* ends loop over j */
  72.    }  /* ends loop over i */
  73.  
  74.       /* if desired, threshold the output image */
  75.    if(threshold == 1){
  76.        for(i=0; i<ROWS; i++){
  77.           for(j=0; j<COLS; j++){
  78.              if(out_image[i][j] > high){
  79.                   out_image[i][j] = new_hi;
  80.              }
  81.              else{
  82.                   out_image[i][j] = new_low;
  83.              }
  84.           }
  85.        }
  86.    }  /* ends if threshold == 1 */
  87.  
  88.  
  89.    fix_edges(out_image, 1);
  90.    write_array_into_tiff_image(out_name, out_image,
  91.                                il, ie, ll, le);
  92.  
  93. } /* ends variance */
  94.  
  95.  
  96.  
  97.  
  98.  
  99.      /*******************************************
  100.      *
  101.      *   range(..
  102.      *
  103.      *   This edge detector performs the
  104.      *   range operation.
  105.      *   It replaces the pixel at the center of a
  106.      *   3x3, 5x5, etc. area with the max - min
  107.      *   for that area.
  108.      *
  109.      *******************************************/
  110.  
  111.  
  112. range(in_name, out_name, the_image, out_image,
  113.       il, ie, ll, le, size, threshold, high)
  114.    char   in_name[], out_name[];
  115.    int    il, ie, ll, le,
  116.           high, threshold, size;
  117.    short  the_image[ROWS][COLS],
  118.           out_image[ROWS][COLS];
  119.  
  120. {
  121.    int    a, b, count, i, j, k,
  122.           new_hi, new_low, length,
  123.           sd2, sd2p1, ss, width;
  124.    short  *elements;
  125.    struct tiff_header_struct image_header;
  126.  
  127.    sd2   = size/2;
  128.    sd2p1 = sd2 + 1;
  129.  
  130.       /**********************************************
  131.       *
  132.       *   Allocate the elements array large enough
  133.       *   to hold size*size shorts.
  134.       *
  135.       **********************************************/
  136.  
  137.    ss       = size*size;
  138.    elements = (short *) malloc(ss * sizeof(short));
  139.    if(does_not_exist(out_name)){
  140.       printf("\n\n output file does not exist %s",
  141.              out_name);
  142.       read_tiff_header(in_name, &image_header);
  143.       round_off_image_size(&image_header,
  144.                            &length, &width);
  145.       image_header.image_length = length*ROWS;
  146.       image_header.image_width  = width*COLS;
  147.       create_allocate_tiff_file(out_name,
  148.                                 &image_header,
  149.                                 out_image);
  150.    }  /* ends if does_not_exist */
  151.  
  152.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  153.  
  154.    new_hi  = 250;
  155.    new_low = 16;
  156.    if(image_header.bits_per_pixel == 4){
  157.        new_hi  = 10;
  158.        new_low = 3;
  159.    }
  160.  
  161.       /***************************
  162.       *
  163.       *   Loop over image array
  164.       *
  165.       ****************************/
  166.  
  167.    printf("\n");
  168.    for(i=sd2; i<ROWS-sd2; i++){
  169.       if( (i%10) == 0) printf("%d ", i);
  170.       for(j=sd2; j<COLS-sd2; j++){
  171.          count = 0;
  172.          for(a=-sd2; a<sd2p1; a++){
  173.             for(b=-sd2; b<sd2p1; b++){
  174.                elements[count] = the_image[i+a][j+b];
  175.                count++;
  176.             }
  177.          }
  178.          sort_elements(elements, &ss);
  179.          out_image[i][j] = elements[ss-1]-elements[0];
  180.       }  /* ends loop over j */
  181.    }  /* ends loop over i */
  182.  
  183.      /* if desired, threshold the output image */
  184.    if(threshold == 1){
  185.        for(i=0; i<ROWS; i++){
  186.           for(j=0; j<COLS; j++){
  187.              if(out_image[i][j] > high){
  188.                   out_image[i][j] = new_hi;
  189.              }
  190.              else{
  191.                   out_image[i][j] = new_low;
  192.              }
  193.           }
  194.        }
  195.    }  /* ends if threshold == 1 */
  196.  
  197.    fix_edges(out_image, sd2);
  198.  
  199.    write_array_into_tiff_image(out_name, out_image,
  200.                                il, ie, ll, le);
  201.  
  202.    free(elements);
  203.  
  204. }  /* ends range */
  205.  
  206.