home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_01 / 1001055a < prev    next >
Text File  |  1991-12-12  |  7KB  |  267 lines

  1.        /***********************************************
  2.        *
  3.        *       file d:\cips\edge3.c
  4.        *
  5.        *       Functions: This file contains
  6.        *          gaussian_edge
  7.        *          enhance_edges  
  8.        *
  9.        *       Purpose:
  10.        *          These functions implement several
  11.        *          types of advanced edge detection.
  12.        *
  13.        *       External Calls:
  14.        *          wtiff.c - does_not_exist
  15.        *                    round_off_image_size
  16.        *                    create_allocate_tiff_file
  17.        *                    write_array_into_tiff_image
  18.        *          tiff.c - read_tiff_header
  19.        *          rtiff.c - read_tiff_image
  20.        *          numcvrt.c - get_integer
  21.        *          edge.c - fix_edges
  22.        *
  23.        *       Modifications:
  24.        *          26 March 1991 - created
  25.        *
  26.        *************************************************/
  27.  
  28.  
  29. #include "d:\cips\cips.h"
  30.  
  31.  
  32. short enhance_mask[3][3] =  {
  33.        {-1,  0, -1},
  34.        { 0,  4,  0},
  35.        {-1,  0, -1} };
  36.  
  37.  
  38. short g7[7][7] = {
  39.      {  0,  0, -1, -1, -1,  0,  0},
  40.      {  0, -2, -3, -3, -3, -2,  0},
  41.      { -1, -3,  5,  5,  5, -3, -1},
  42.      { -1, -3,  5, 16,  5, -3, -1},
  43.      { -1, -3,  5,  5,  5, -3, -1},
  44.      {  0, -2, -3, -3, -3, -2,  0},
  45.      {  0,  0, -1, -1, -1,  0,  0}};
  46.  
  47. short g9[9][9] = {
  48.    {  0,  0,  0,  -1, -1, -1,  0,  0,  0},
  49.    {  0, -2, -3,  -3, -3, -3, -3, -2,  0},
  50.    {  0, -3, -2,  -1, -1, -1, -2, -3,  0},
  51.    { -1, -3, -1,   9,  9,  9, -1, -3, -1},
  52.    { -1, -3, -1,   9, 19,  9, -1, -3, -1},
  53.  
  54.    { -1, -3, -1,   9,  9,  9, -1, -3, -1},
  55.    {  0, -3, -2,  -1, -1, -1, -2, -3,  0},
  56.    {  0, -2, -3,  -3, -3, -3, -3, -2,  0},
  57.    {  0,  0,  0,  -1, -1, -1,  0,  0,  0}};
  58.  
  59.  
  60.  
  61.    /**************************************************
  62.    *
  63.    *   gaussian_edge(...
  64.    *
  65.    *
  66.    ***************************************************/
  67.  
  68. gaussian_edge(in_name, out_name, the_image, out_image,
  69.                 il, ie, ll, le, size, threshold, high)
  70.    char   in_name[], out_name[];
  71.    int    high, il, ie, ll, le, size, threshold;
  72.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  73. {
  74.    char response[80];
  75.    long sum;
  76.    int  a, b, absdiff, absmax, diff, i, j,
  77.         length, lower, max, new_hi, new_low, 
  78.         scale, start, stop, upper, width;
  79.  
  80.    struct tiff_header_struct image_header;
  81.  
  82.  
  83.    if(does_not_exist(out_name)){
  84.       printf("\n\nGAUSSIAN> output file does not exist %s",
  85.              out_name);
  86.       read_tiff_header(in_name, &image_header);
  87.       round_off_image_size(&image_header,
  88.                            &length, &width);
  89.       image_header.image_length = length*ROWS;
  90.       image_header.image_width  = width*COLS;
  91.       create_allocate_tiff_file(out_name, &image_header,
  92.                                 out_image);
  93.    }  /* ends if does_not_exist */
  94.  
  95.    read_tiff_header(in_name, &image_header);
  96.  
  97.    new_hi  = 250;
  98.    new_low = 16;
  99.    if(image_header.bits_per_pixel == 4){
  100.        new_hi  = 10;
  101.        new_low = 3;
  102.    }
  103.  
  104.    max = 255;
  105.    if(image_header.bits_per_pixel == 4)
  106.       max = 16;
  107.  
  108.  
  109.    if(size == 7){
  110.       lower = -3;
  111.       upper =  4;
  112.       start =  3;
  113.       stop  =  ROWS-3;
  114.  
  115.       scale =  2;
  116.    }
  117.  
  118.    if(size == 9){
  119.       lower = -4;
  120.       upper =  5;
  121.       start =  4;
  122.       stop  =  ROWS-4;
  123.       scale =  2;
  124.    }
  125.  
  126.  
  127.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  128.  
  129.    for(i=0; i<ROWS; i++)
  130.       for(j=0; j<COLS; j++)
  131.          out_image[i][j] = 0;
  132.  
  133.  
  134.    for(i=start; i<stop; i++){
  135.       if ( (i%10) == 0) printf(" i=%d", i);
  136.       for(j=start; j<stop; j++){
  137.  
  138.       sum = 0;
  139.  
  140.       for(a=lower; a<upper; a++){
  141.          for(b=lower; b<upper; b++){
  142.             if(size == 7)
  143.                sum = sum + the_image[i+a][j+b] *
  144.                      g7[a+3][b+3];
  145.             if(size == 9)
  146.                sum = sum + the_image[i+a][j+b] *
  147.                      g9[a+4][b+4];
  148.          } /* ends loop over a */
  149.       }  /* ends loop over b */
  150.  
  151.       if(sum < 0) sum = 0;
  152.       if(sum > max) sum = max;
  153.       out_image[i][j] = sum;
  154.  
  155.  
  156.       }  /* ends loop over j */
  157.    }  /* ends loop over i */
  158.  
  159.      /* if desired, threshold the output image */
  160.    if(threshold == 1){
  161.        for(i=0; i<ROWS; i++){
  162.           for(j=0; j<COLS; j++){
  163.              if(out_image[i][j] > high){
  164.                   out_image[i][j] = new_hi;
  165.              }
  166.              else{
  167.                   out_image[i][j] = new_low;
  168.              }
  169.           }
  170.        }
  171.    }  /* ends if threshold == 1 */
  172.  
  173.  
  174.    fix_edges(out_image, size/2);
  175.  
  176.  
  177.    write_array_into_tiff_image(out_name, out_image,
  178.                                il, ie, ll, le);
  179.  
  180. } /* ends gaussian_edge */
  181.  
  182.  
  183.      /*******************************************
  184.      *
  185.      * enhance_edges(...
  186.      *
  187.      * This function enhances the edges in an
  188.      * input image and writes the enhanced
  189.      * result to an output image.  It operates
  190.      * much the same way as detect_edges
  191.      * except it uses only one type of mask.
  192.      * 
  193.      * The threshold and high parameters perform
  194.      * a different role in this function.  The 
  195.      * threshold parameter does not exist.  The
  196.      * high parameter determines if the edge is
  197.      * strong enough to enhance or change the
  198.      * input image.
  199.      *
  200.      *******************************************/
  201.  
  202.  
  203. enhance_edges(in_name, out_name, the_image, out_image,
  204.              il, ie, ll, le, high)
  205.    char   in_name[], out_name[];
  206.    int    high, il, ie, ll, le;
  207.    short  the_image[ROWS][COLS], out_image[ROWS][COLS];
  208.  
  209. {
  210.    int    a, b, i, j, k,
  211.           length, max, new_hi, 
  212.           new_lo, sum, width;
  213.    struct tiff_header_struct image_header;
  214.  
  215.  
  216.    if(does_not_exist(out_name)){
  217.       printf("\n\nEE> output file does not exist %s", 
  218.               out_name);
  219.       read_tiff_header(in_name, &image_header);
  220.       round_off_image_size(&image_header,
  221.                            &length, &width);
  222.       image_header.image_length = length*ROWS;
  223.       image_header.image_width  = width*COLS;
  224.       create_allocate_tiff_file(out_name, &image_header,
  225.                                 out_image);
  226.    }  /* ends if does_not_exist */
  227.  
  228.    read_tiff_header(in_name, &image_header);
  229.  
  230.    max = 255;
  231.    if(image_header.bits_per_pixel == 4)
  232.       max = 16;
  233.  
  234.  
  235.  
  236.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  237.  
  238.          /* Do convolution over image array */
  239.    for(i=1; i<ROWS-1; i++){
  240.       if( (i%10) == 0) printf("%d ", i);
  241.       for(j=1; j<COLS-1; j++){
  242.          sum = 0;
  243.          for(a=-1; a<2; a++){
  244.             for(b=-1; b<2; b++){
  245.                sum = sum +
  246.                      the_image[i+a][j+b] *
  247.                      enhance_mask[a+1][b+1];
  248.             }
  249.          }
  250.          if(sum < 0)   sum = 0;
  251.          if(sum > max) sum = max;
  252.          if(sum > high)
  253.             out_image[i][j] = max;
  254.          else
  255.             out_image[i][j] = the_image[i][j];
  256.       }  /* ends loop over j */
  257.    }  /* ends loop over i */
  258.  
  259.    fix_edges(out_image, 1);
  260.  
  261.  
  262.    write_array_into_tiff_image(out_name, out_image,
  263.                                il, ie, ll, le);
  264. }  /* ends enhance_edges */
  265. /* End of File */ 
  266.  
  267.