home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 343_01 / cips.c < prev    next >
C/C++ Source or Header  |  1992-04-15  |  19KB  |  516 lines

  1.  
  2.  
  3.  
  4.  
  5.        /***********************************************
  6.        *
  7.        *       file d:\cips\cips.c
  8.        *
  9.        *       Functions: This file contains
  10.        *          main
  11.        *          clear_text_screen
  12.        *          show_menu
  13.        *          show_image
  14.        *
  15.        *       Purpose:
  16.        *          This file contains the main calling
  17.        *          routine in the C Image Processing System.
  18.        *
  19.        *       External Calls:
  20.        *          numcvrt.c - get_integer
  21.        *          gin.c - get_image_name
  22.        *          rtiff.c - read_tiff_image
  23.        *          tiff.c - read_tiff_header
  24.        *          rstring.c - read_string
  25.        *          display.c - display_image
  26.        *                      display_menu_for_display_image
  27.        *          pi.c - print_image
  28.        *          ht.c - display_using_halftoning
  29.        *                 get_threshold_value
  30.        *          djet.c - print_graphics_image
  31.        *                   get_graphics_caption
  32.        *          hist.c - display_menu_for_histogram
  33.        *                   calculate_area_histogram
  34.        *                   print_histogram
  35.        *                   show_histogram
  36.        *          edge.c - detect_edges
  37.        *                   get_edge_options
  38.        *                   quick_edge
  39.        *          edge2.c - homogeneity
  40.        *                    difference_edge
  41.        *                    contrast_edge
  42.        *          edge3.c - gaussian_edge
  43.        *                    enhance_edges
  44.        *          filter.c - filter_image
  45.        *                     median_filter
  46.        *                     get_filter_options
  47.        *          addsub.c - add_image_array
  48.        *                     subtract_image_array
  49.        *          cutp.c - cut_image_piece
  50.        *                   paste_image_piece
  51.        *                   check_cut_and_paste_limits
  52.        *          rotate.c - rotate_flip_image_array
  53.        *          scale.c - zoom_image_array
  54.        *                    shrink_image_array
  55.        *
  56.        *       Modifications:
  57.        *          26 June 1990 - created
  58.        *
  59.        *************************************************/
  60.  
  61. #include "d:\cips\cips.h"
  62.  
  63.  
  64. short the_image[ROWS][COLS];
  65. short out_image[ROWS][COLS];
  66.  
  67. main()
  68. {
  69.  
  70.    char caption[80],
  71.         color_transform[80],
  72.         low_high[80],
  73.         method[80],
  74.         monitor_type[80],
  75.         name[80],
  76.         name2[80],
  77.         name3[80],
  78.         rep[80],
  79.         zoom_shrink[80];
  80.  
  81.  
  82.    int  color                = 99,
  83.         detect_threshold     = 1,
  84.         detect_type          = 1,
  85.         display_colors       = 16,
  86.         filter_type          = 1,
  87.         high                 = 100,
  88.         horizontal           = 3,
  89.         i                    = 0,
  90.         ie                   = 1,
  91.         ie2                  = 1,
  92.         ie3                  = 1,
  93.         il                   = 1,
  94.         il2                  = 1,
  95.         il3                  = 1,
  96.         image_colors         = 256,
  97.         invert               = 0,
  98.         j                    = 0,
  99.         length               = 3,
  100.         le                   = COLS+1,
  101.         le2                  = COLS+1,
  102.         le3                  = COLS+1,
  103.         ll                   = ROWS+1,
  104.         ll2                  = ROWS+1,
  105.         ll3                  = ROWS+1,
  106.         not_finished         = 1,
  107.         print                = 0,
  108.         response             = 99,
  109.         rotation_type        = 1,
  110.         scale                = 2,
  111.         show_hist            = 0,
  112.         size                 = 7,
  113.         threshold            = 128,
  114.         vertical             = 3,
  115.         width                = 3;
  116.  
  117.    long     mean_of_pixels;
  118.    unsigned long histogram[256];
  119.    short    low_hi_filter[3][3];
  120.    struct   tiff_header_struct image_header;
  121.  
  122.  
  123.    clear_text_screen();
  124.  
  125.    strcpy(color_transform, "Straight mode");
  126.    strcpy(monitor_type, "VGA");
  127.    strcpy(low_high, "l");
  128.    strcpy(zoom_shrink, "z");
  129.    strcpy(method, "r");
  130.  
  131.    strcpy(name,  "d:/pix/adam256.tif");
  132.    strcpy(name2, "d:/pix/output.tif");
  133.    strcpy(name3, "d:/pix/output.tif");
  134.  
  135.      while(not_finished){
  136.  
  137.         show_menu();
  138.  
  139.         get_integer(&response);
  140.  
  141.         switch (response){
  142.  
  143.         case 1:/* display image header */
  144.          get_image_name(name);
  145.          read_tiff_header(name, &image_header);
  146.          printf("\n\nCIPS> The image header is:");
  147.          printf("\n\t\twidth=%ld length=%ld  start=%ld  bits=%ld",
  148.               image_header.image_width,
  149.               image_header.image_length,
  150.               image_header.strip_offset,
  151.               image_header.bits_per_pixel);
  152.          printf("\nCIPS> Hit Enter to continue");
  153.          read_string(rep);
  154.         break;
  155.  
  156.         case 2:/* display image numbers */
  157.          get_image_name(name);
  158.          get_parameters(&il, &ie, &ll, &le);
  159.          read_tiff_image(name, the_image, il, ie, ll, le);
  160.          show_image(the_image, il, ie);
  161.          break;
  162.  
  163.         case 3:   /* print image numbers */
  164.          get_image_name(name);
  165.          get_parameters(&il, &ie, &ll, &le);
  166.          read_tiff_image(name, the_image, il, ie, ll, le);
  167.          print_image(the_image, name, 1, 1, 1, 100, 18,
  168.                      il, ie);
  169.         break;
  170.  
  171.         case 4:   /* display image */
  172.          get_image_name(name);
  173.          read_tiff_header(name, &image_header);
  174.          get_parameters(&il, &ie, &ll, &le);
  175.          display_menu_for_display_image(&image_colors,
  176.                    &display_colors, &invert,
  177.                    color_transform, monitor_type,
  178.                    &show_hist);
  179.          display_image(name, the_image, il, ie,
  180.                    ll, le, &image_header, monitor_type,
  181.                    color_transform, invert,
  182.                    image_colors, display_colors, show_hist);
  183.         break;
  184.  
  185.         case 5:   /* display image using halftoning */
  186.          get_image_name(name);
  187.          read_tiff_header(name, &image_header);
  188.          get_parameters(&il, &ie, &ll, &le);
  189.          display_menu_for_display_image(&image_colors,
  190.                    &display_colors, &invert,
  191.                    color_transform, monitor_type,
  192.                    &show_hist);
  193.          get_threshold_value(&threshold, &print);
  194.          display_using_halftoning(the_image, name,
  195.                    il, ie, ll, le, threshold,
  196.                    invert, image_colors, &image_header,
  197.                    monitor_type, print, show_hist,
  198.                    color_transform);
  199.         break;
  200.  
  201.         case 6:   /* print graphics image */
  202.          get_image_name(name);
  203.          read_tiff_header(name, &image_header);
  204.          get_parameters(&il, &ie, &ll, &le);
  205.          display_menu_for_display_image(&image_colors,
  206.                    &display_colors, &invert,
  207.                    color_transform, monitor_type,
  208.                    &show_hist);
  209.          get_graphics_caption(caption);
  210.          print_graphics_image(the_image, out_image, name,
  211.                    il, ie, ll, le, image_colors,
  212.                    invert, caption, show_hist,
  213.                    color_transform);
  214.         break;
  215.  
  216.         case 7:   /* print or display histogram numbers */
  217.          get_image_name(name);
  218.          read_tiff_header(name, &image_header);
  219.          get_parameters(&il, &ie, &ll, &le);
  220.          display_menu_for_histogram(&print, &vertical,
  221.                    &horizontal);
  222.          calculate_area_histogram(histogram, vertical,
  223.                    horizontal, the_image, name,
  224.                    il, ie, ll, le);
  225.          if(print == 0)
  226.             show_histogram(histogram);
  227.          if(print == 1)
  228.             print_histogram(histogram, name);
  229.         break;
  230.  
  231.         case 8:  /* perform edge detection */
  232.            printf("\nCIPS> Enter input image name\n");
  233.            get_image_name(name);
  234.            printf("\nCIPS> Enter output image name\n");
  235.            get_image_name(name2);
  236.            get_parameters(&il, &ie, &ll, &le);
  237.            get_edge_options(&detect_type, &detect_threshold,
  238.                             &high, &size);
  239.            if(detect_type == 1  ||
  240.               detect_type == 2  ||
  241.               detect_type == 3)
  242.               detect_edges(name, name2, the_image, out_image,
  243.                            il, ie, ll, le, detect_type,
  244.                            detect_threshold, high);
  245.            if(detect_type == 4)
  246.               quick_edge(name, name2, the_image, out_image,
  247.                          il, ie, ll, le, detect_threshold,
  248.                          high);
  249.            if(detect_type == 5)
  250.                 homogeneity(name, name2, the_image, out_image,
  251.                          il, ie, ll, le, detect_threshold,
  252.                          high);
  253.            if(detect_type == 6)
  254.                 difference_edge(name, name2, the_image, out_image,
  255.                               il, ie, ll, le, detect_threshold,
  256.                               high);
  257.            if(detect_type == 7)
  258.                 contrast_edge(name, name2, the_image, out_image,
  259.                             il, ie, ll, le, detect_threshold,
  260.                             high);
  261.            if(detect_type == 8)
  262.                 gaussian_edge(name, name2, the_image, out_image,
  263.                              il, ie, ll, le, size,
  264.                             detect_threshold, high);
  265.          break;
  266.  
  267.         case 9: /* perform edge enhancement */
  268.            printf("\nCIPS> Enter input image name\n");
  269.            get_image_name(name);
  270.            printf("\nCIPS> Enter output image name\n");
  271.            get_image_name(name2);
  272.            get_parameters(&il, &ie, &ll, &le);
  273.            printf("\nCIPS> Enter high threshold parameter");
  274.            printf(" \n\t___\b\b\b");
  275.            get_integer(&high);
  276.            enhance_edges(name, name2, the_image, out_image,
  277.                   il, ie, ll, le, high);
  278.          break;
  279.  
  280.         case 10: /* perform image filtering */
  281.            printf("\nCIPS> Enter input image name\n");
  282.            get_image_name(name);
  283.            printf("\nCIPS> Enter output image name\n");
  284.            get_image_name(name2);
  285.            get_parameters(&il, &ie, &ll, &le);
  286.            get_filter_options(&filter_type, low_high);
  287.            if(low_high[0] == 'l' ||
  288.                low_high[0] == 'L' ||
  289.                low_high[0] == 'h' ||
  290.                low_high[0] == 'H'){
  291.               setup_filters(filter_type, low_high, low_hi_filter);
  292.               filter_image(name, name2, the_image, out_image,
  293.                            il, ie, ll, le,
  294.                            low_hi_filter, filter_type);
  295.            }
  296.  
  297.            if(low_high[0] == 'm' ||
  298.                low_high[0] == 'M')
  299.                  median_filter(name, name2, the_image, out_image,
  300.                              il, ie, ll, le, filter_type);
  301.  
  302.         break;
  303.  
  304.         case 11: /* perform image addition and subtraction */
  305.            printf("\nCIPS> Image Addition:"
  306.                 "\n         output = first + second"
  307.                 "\n      Image Subtractions:"
  308.                 "\n         output = first - second");
  309.            printf("\nCIPS> Enter first image name\n");
  310.            get_image_name(name);
  311.            printf("\nCIPS> Enter second image name\n");
  312.            get_image_name(name2);
  313.            printf("\nCIPS> Enter output image name\n");
  314.            get_image_name(name3);
  315.            printf("\nCIPS> Enter parameters for first image");
  316.            get_parameters(&il, &ie, &ll, &le);
  317.            printf("\nCIPS> Enter parameters for second image");
  318.            get_parameters(&il2, &ie2, &ll2, &le2);
  319.            printf("\nCIPS> Enter parameters for output image");
  320.            get_parameters(&il3, &ie3, &ll3, &le3);
  321.            printf("\n\nCIPS> Enter a=addition    s=subtraction\n");
  322.            printf("\nCIPS> _\b");
  323.            read_string(low_high);
  324.            if(low_high[0] == 'a' || low_high[0] == 'A')
  325.               add_image_array(name, name2, name3,
  326.                   the_image, out_image,
  327.                   il, ie, ll, le,
  328.                   il2, ie2, ll2, le2,
  329.                   il3, ie3, ll3, le3);
  330.            if(low_high[0] == 's' || low_high[0] == 'S')
  331.               subtract_image_array(name, name2, name3,
  332.                   the_image, out_image,
  333.                   il, ie, ll, le,
  334.                   il2, ie2, ll2, le2,
  335.                   il3, ie3, ll3, le3);
  336.         break;
  337.  
  338.         case 12: /* image cutting and pasting */
  339.            printf("\n\nCIPS> Cut from source image and paste to"
  340.                   " destination image");
  341.            printf("\nCIPS> Enter source image name");
  342.            get_image_name(name);
  343.            get_parameters(&il, &ie, &ll, &le);
  344.            check_cut_and_paste_limits(&il, &ie, &ll, &le);
  345.            cut_image_piece(name, the_image,
  346.                            il, ie, ll, le);
  347.            printf("\nCIPS> Enter destination image name");
  348.            get_image_name(name2);
  349.            printf("\nCIPS> Enter destination image parameters");
  350.            get_parameters(&il, &ie, &ll, &le);
  351.            check_cut_and_paste_limits(&il, &ie, &ll, &le);
  352.            paste_image_piece(name2, name, the_image,
  353.                              out_image, il, ie, ll, le);
  354.         break;
  355.  
  356.         case 13: /* image rotation and flipping */
  357.            printf("\nCIPS> Enter source image name");
  358.            get_image_name(name);
  359.            get_parameters(&il, &ie, &ll, &le);
  360.            printf("\nCIPS> Enter destination image name");
  361.            printf("\nCIPS> (source can equal destination)");
  362.            get_image_name(name2);
  363.            printf("\nCIPS> Enter destination image parameters");
  364.            get_parameters(&il2, &ie2, &ll2, &le2);
  365.            printf("\nCIPS> Enter number of Rotations (1, 2, 3)");
  366.            printf("\nCIPS> or type of Flip (4=horizontal 5=vertical)");
  367.            printf("\nCIPS> __\b");
  368.            get_integer(&rotation_type);
  369.            rotate_flip_image_array(name, name2, the_image,
  370.                                out_image, il, ie, ll, le,
  371.                                il2, ie2, ll2, le2,
  372.                                rotation_type);
  373.         break;
  374.  
  375.         case 14: /* image scaling */
  376.            printf("\nCIPS> Enter input image name");
  377.            get_image_name(name);
  378.            get_parameters(&il, &ie, &ll, &le);
  379.            printf("\nCIPS> Enter output image name");
  380.            get_image_name(name2);
  381.            get_scaling_options(zoom_shrink, &scale, method);
  382.            if(zoom_shrink[0] == 'z' || zoom_shrink[0] == 'Z')
  383.               zoom_image_array(name, name2, the_image, out_image,
  384.                                il, ie, ll, le, scale, method);
  385.            if(zoom_shrink[0] == 's' || zoom_shrink[0] == 'S'){
  386.                printf("\nCIPS> Enter output image parameters");
  387.                  get_parameters(&il2, &ie2, &ll2, &le2);
  388.                shrink_image_array(name, name2, the_image, out_image,
  389.                                   il, ie, ll, le, il2, ie2, ll2, le2,
  390.                                   scale, method);
  391.            }
  392.         break;
  393.  
  394.         case 15: /* create image */
  395.            printf("\nCIPS> Enter input name of image to create");
  396.            get_image_name(name);
  397.            printf("\nCIPS> Enter number of %d blocks wide",COLS);
  398.            printf("\n      ___\b\b");
  399.            get_integer(&width);
  400.            printf("\nCIPS> Enter number of %d blocks tall",ROWS);
  401.            printf("\n      ___\b\b");
  402.            get_integer(&length);
  403.            image_header.lsb            = 1;
  404.            image_header.bits_per_pixel = 8;
  405.            image_header.image_length   = length*COLS;
  406.            image_header.image_width    = width*ROWS;
  407.            image_header.strip_offset   = 1000;
  408.            for(i=0; i<ROWS; i++)
  409.               for(j=0; j<COLS; j++)
  410.                  the_image[i][j] = 0;
  411.            create_allocate_tiff_file(name,&image_header,the_image);
  412.         break;
  413.  
  414.         case 20:  /* exit system */
  415.          not_finished = 0;
  416.         break;
  417.  
  418.         default:
  419.          printf("\nCould not understand response, try again");
  420.         break;
  421.  
  422.      }               /* ends switch response          */
  423.   }               /* ends while not finished */
  424. }               /* ends main                  */
  425.  
  426.  
  427.  
  428.  
  429.    /******************************************************
  430.    *
  431.    *   clear_text_screen()
  432.    *
  433.    *   This calls Microsoft C functions to clear the text
  434.    *   screen and set a blue background with gray text.
  435.    *
  436.    *******************************************************/
  437.  
  438.  
  439. clear_text_screen()
  440. {
  441.    _setvideomode(_TEXTC80);      /* MSC 6.0 statements */
  442.    _setbkcolor(1);
  443.    _settextcolor(7);
  444.    _clearscreen(_GCLEARSCREEN);
  445. }  /* ends clear_text_screen */
  446.  
  447.  
  448.  
  449.  
  450.    /******************************************************
  451.    *
  452.    *   show_image(...
  453.    *
  454.    *   This function displays the image numbers on the
  455.    *   screen as text.  It displays 20 rows  with 18
  456.    *   columns each.
  457.    *
  458.    *******************************************************/
  459.  
  460. show_image(image, il, ie)
  461.    int   il, ie;
  462.    short image[ROWS][COLS];
  463. {
  464.    int i, j;
  465.    printf("\n   ");
  466.    for(i=0; i<18; i++)
  467.       printf("-%3d", i+ie);
  468.  
  469.    for(i=0; i<20; i++){
  470.       printf("\n%2d>", i+il);
  471.       for(j=0; j<18; j++)
  472.          printf("-%3d", image[i][j]);
  473.    }
  474.  
  475.    printf("\nPress enter to continue");
  476.    get_integer(&i);
  477.  
  478. }  /* ends show_image  */
  479.  
  480.  
  481.  
  482.  
  483.  
  484.    /******************************************************
  485.    *
  486.    *   show_menu(..
  487.    *
  488.    *   This function displays the CIPS main menu.
  489.    *
  490.    *******************************************************/
  491. show_menu()
  492. {
  493.  
  494.         printf("\n\nWelcome to CIPS");
  495.         printf("\nThe C Image Processing System");
  496.         printf("\nThese are you choices:");
  497.         printf("\n\t1.  Display image header");
  498.         printf("\n\t2.  Show image numbers");
  499.         printf("\n\t3.  Print image numbers");
  500.         printf("\n\t4.  Display image (VGA & EGA only)");
  501.         printf("\n\t5.  Display or print image using halftoning");
  502.         printf("\n\t6.  Print graphics image using dithering");
  503.         printf("\n\t7.  Print or display histogram numbers");
  504.         printf("\n\t8.  Perform edge detection");
  505.         printf("\n\t9.  Perform edge enhancement");
  506.         printf("\n\t10. Perform image filtering");
  507.         printf("\n\t11. Perform image addition and subtraction");
  508.         printf("\n\t12. Perform image cutting and pasting");
  509.         printf("\n\t13. Perform image rotation and flipping");
  510.         printf("\n\t14. Perform image scaling");
  511.         printf("\n\t15. Create a blank image");
  512.         printf("\n\t20. Exit system");
  513.         printf("\n\nEnter choice _\b");
  514.  
  515. }    /* ends show_menu */
  516.