     /*******************************************
     *
     *   opening(...
     *
     *   Opening is erosion followed by dilation.
     *   This routine will use the mask erosion
     *   and dilation.  You could use the other
     *   types and you could mix the two types.
     *
     *   The number parameter specifies how
     *   erosions to perform before doing one
     *   dilation.
     *
     *******************************************/

opening(in_name, out_name, the_image, out_image,
        il, ie, ll, le, value, mask_type, number)
   char   in_name[], out_name[];
   int    il, ie, ll, le, number;
   short  the_image[ROWS][COLS],
          out_image[ROWS][COLS],
          mask_type, value;
{
   int    a, b, count, i, j, k;
   int    length, width;
   struct tiff_header_struct image_header;
   short  mask[3][3], max;

      /**************************************
      *
      *   Copy the 3x3 erosion-dilation mask
      *   specified by the mask_type.
      *
      ***************************************/

   switch(mask_type){
      case 1:
         copy_3_x_3(mask, edmask1);
         break;
      case 2:
         copy_3_x_3(mask, edmask2);
         break;
      case 3:
         copy_3_x_3(mask, edmask3);
         break;
      case 4:
         copy_3_x_3(mask, edmask4);
         break;
      default:
         printf("\nInvalid mask type, using mask 4");
         copy_3_x_3(mask, edmask4);
         break;
   }

   if(does_not_exist(out_name)){
      printf("\n\n output file does not exist %s", out_name);
      read_tiff_header(in_name, &image_header);
      round_off_image_size(&image_header,
                           &length, &width);
      image_header.image_length = length*ROWS;
      image_header.image_width  = width*COLS;
      create_allocate_tiff_file(out_name, &image_header,
                                out_image);
   }  /* ends if does_not_exist */

   read_tiff_image(in_name, the_image, il, ie, ll, le);

   mask_erosion(in_name, out_name, the_image,
                out_image, il, ie, ll, le,
                value, mask_type);

   if(number > 1){
      count = 1;
      while(count < number){
         count++;
         mask_erosion(out_name, out_name, the_image,
                       out_image, il, ie, ll, le,
                       value, mask_type);
      }  /* ends while */
   }  /* ends if number > 1 */

   mask_dilation(out_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 value, mask_type);

   write_array_into_tiff_image(out_name, out_image,
                               il, ie, ll, le);

}  /* ends opening */





     /*******************************************
     *
     *   closing(...
     *
     *   Closing is dilation followed by erosion.
     *   This routine will use the mask erosion
     *   and dilation.  You could use the other
     *   types and you could mix the two types.
     *
     *   The number parameter specifies how
     *   dilations to perform before doing one
     *   erosion.
     *
     *******************************************/

closing(in_name, out_name, the_image, out_image,
        il, ie, ll, le, value, mask_type, number)
   char   in_name[], out_name[];
   int    il, ie, ll, le, number;
   short  the_image[ROWS][COLS],
          out_image[ROWS][COLS],
          mask_type, value;
{
   int    a, b, count, i, j, k;
   int    length, width;
   struct tiff_header_struct image_header;
   short  mask[3][3], max;

      /**************************************
      *
      *   Copy the 3x3 erosion-dilation mask
      *   specified by the mask_type.
      *
      ***************************************/

   switch(mask_type){
      case 1:
         copy_3_x_3(mask, edmask1);
         break;
      case 2:
         copy_3_x_3(mask, edmask2);
         break;
      case 3:
         copy_3_x_3(mask, edmask3);
         break;
      case 4:
         copy_3_x_3(mask, edmask4);
         break;
      default:
         printf("\nInvalid mask type, using mask 4");
         copy_3_x_3(mask, edmask4);
         break;
   }

   if(does_not_exist(out_name)){
      printf("\n\n output file does not exist %s", out_name);
      read_tiff_header(in_name, &image_header);
      round_off_image_size(&image_header,
                           &length, &width);
      image_header.image_length = length*ROWS;
      image_header.image_width  = width*COLS;
      create_allocate_tiff_file(out_name, &image_header,
                                out_image);
   }  /* ends if does_not_exist */

   read_tiff_image(in_name, the_image, il, ie, ll, le);

   mask_dilation(in_name, out_name, the_image,
                 out_image, il, ie, ll, le,
                 value, mask_type);

   if(number > 1){
      count = 1;
      while(count < number){
         count++;
         mask_dilation(out_name, out_name, the_image,
                        out_image, il, ie, ll, le,
                        value, mask_type);
      }  /* ends while */
   }  /* ends if number > 1 */

   mask_erosion(out_name, out_name, the_image,
                out_image, il, ie, ll, le,
                value, mask_type);

   write_array_into_tiff_image(out_name, out_image,
                               il, ie, ll, le);

}  /* ends closing */


