home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_01 / cips1001.exe / EDGE3.C < prev    next >
Text File  |  1991-04-20  |  7KB  |  272 lines

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