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 / HIST.C < prev    next >
Text File  |  1990-09-08  |  12KB  |  499 lines

  1.  
  2.  
  3.  
  4.        /*****************************************************
  5.        *
  6.        *       file d:\cips\hist.c
  7.        *
  8.        *       Functions: This file contains
  9.        *           calculate_histogram
  10.        *           calculate_histogram
  11.        *           zero_histogram
  12.        *           perform_histogram_equalization
  13.        *           show_histogram
  14.        *           print_histogram
  15.        *           smooth_histogram
  16.        *
  17.        *
  18.        *       Purpose: These functions calculate and display the
  19.        *          histogram of an input image array.
  20.        *
  21.        *       Modifications:
  22.        *           July 86 - ported to IBM-PC
  23.        *           August 1990 - modified for use in the
  24.        *               C Image Processing System
  25.        *
  26.        *******************************************************/
  27.  
  28. #include "d:\cips\cips.h"
  29.  
  30.  
  31. #define PRINT_WIDTH  80
  32. #define FORMFEED     '\014'
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.         /*****************************************
  43.         *
  44.         *    perform_histogram_equalization(...
  45.         *
  46.         ******************************************/
  47.  
  48. perform_histogram_equalization(image, histogram, new_grays, area)
  49.    float new_grays, area;
  50.    short image[ROWS][COLS];
  51.    unsigned long histogram[];
  52. {
  53.    int i,
  54.        j,
  55.        k;
  56.    unsigned long sum,
  57.             sum_of_h[256];
  58.  
  59.    double constant;
  60.  
  61.  
  62.  
  63.    sum = 0;
  64.    for(i=0; i<256; i++){
  65.       sum         = sum + histogram[i];
  66.       sum_of_h[i] = sum;
  67.    }
  68.  
  69.       /* constant = new # of gray levels div by area */
  70.    constant = new_grays/area;
  71.    for(i=0; i<ROWS; i++){
  72.       for(j=0; j<COLS; j++){
  73.          k           = image[i][j];
  74.          image[i][j] = sum_of_h[k] * constant;
  75.       }
  76.    }
  77. }  /* ends perform_histogram_equalization */
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.         /*****************************************
  90.         *
  91.         *   zero_histogram(...
  92.         *
  93.         ******************************************/
  94.  
  95. zero_histogram(histogram)
  96.    unsigned long histogram[];
  97. {
  98.    int i;
  99.    for(i=0; i<256; i++)
  100.       histogram[i] = 0;
  101. }  /* ends zero_histogram */
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.         /*****************************************
  111.         *
  112.         *   calcualte_histogram(...
  113.         *
  114.         ******************************************/
  115.  
  116. calculate_histogram(image, histogram)
  117.    short  image[ROWS][COLS];
  118.    unsigned long histogram[];
  119. {
  120.    int i,j,k;
  121.    for(i=0; i<ROWS; i++){
  122.       for(j=0; j<COLS; j++){
  123.          k = image[i][j];
  124.          histogram[k] = histogram[k] + 1;
  125.       }
  126.    }
  127. }  /* ends calculate_histogram */
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.         /*********************************************
  137.         *
  138.         *       show_histogram(histogram)
  139.         *
  140.         *        This function shows the histogram
  141.         *        on the screen as numbers and stars.
  142.         *
  143.         **********************************************/
  144.  
  145.  
  146.  
  147. show_histogram(histogram)
  148.         unsigned long histogram[];
  149. {
  150.         int     count,
  151.                 i,
  152.                 j;
  153.         unsigned long max, scale;
  154.  
  155.  
  156.         max   = 0;
  157.         count = 0;
  158.  
  159.         for(i=0; i<GRAY_LEVELS; i++)
  160.            if(histogram[i] > max)
  161.               max = histogram[i];
  162.  
  163.         if(max > (70 - 12))
  164.            scale = max/(70 - 12);
  165.         else
  166.            scale = 1;
  167.  
  168.         printf("\n max=%ld scale=%ld",max, scale);
  169.  
  170.         printf("\n\ngray    count");
  171.         printf("\nlevel");
  172.  
  173.         for(i=0; i<256; i++){
  174.            if(histogram[i] == 0)
  175.               ++count;
  176.            else 
  177.               count = 0;
  178.  
  179.            if(count < 2){
  180.               printf("\n %4d: %7ld",i,histogram[i]);
  181.               for(j=0; j<((int)(histogram[i]/scale)); j++){
  182.                  printf("*");
  183.               }         /* ends loop over j             */
  184.            }            /* ends if count < 5            */
  185.         }               /* ends loop over i GRAY_LEVELS */
  186. }       /* ends show_histogram */
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.         /*********************************************
  197.         *
  198.         *        print_histogram(histogram)
  199.         *
  200.         *        This function prints the histogram
  201.         *       input to the function.
  202.         *
  203.         **********************************************/
  204.  
  205.  
  206.  
  207. print_histogram(histogram, name)
  208.         char name[];
  209.         unsigned long histogram[];
  210. {
  211.         char    string[300],
  212.                 output[300];
  213.  
  214.         int     count,
  215.                 i,
  216.                 j,
  217.                 line_counter,
  218.                 print_counter;
  219.         unsigned long scale, max;
  220.  
  221.         FILE *printer;
  222.  
  223.         if( (printer = fopen("prn", "w")) == NULL)
  224.            printf("\nPH> Could not open printer");
  225.         else
  226.            printf("\nPH> The print file is opened");
  227.  
  228.         max           = 0;
  229.         count         = 0;
  230.         print_counter = 0;
  231.  
  232.         for(i=0; i<256; i++)
  233.            if(histogram[i] > max)
  234.               max = histogram[i];
  235.  
  236.         if(max > (PRINT_WIDTH - 12))
  237.            scale = max/(PRINT_WIDTH - 12);
  238.         else
  239.            scale = 1;
  240.  
  241.         printf("\n max=%ld scale=%ld",max, scale);
  242.  
  243.         printf("\nPI> Print header");
  244.         line_counter = 0;
  245.         long_clear_buffer(string);
  246.         sprintf(string, "          This image is -- %s --", name);
  247.         my_fwriteln(printer, string);
  248.         ++line_counter;
  249.  
  250.         long_clear_buffer(string);
  251.         sprintf(string, " ");
  252.         my_fwriteln(printer, string);
  253.         ++line_counter;
  254.  
  255.         long_clear_buffer(string);
  256.         sprintf(string, "          gray    count");
  257.         my_fwriteln(printer, string);
  258.         ++line_counter;
  259.         long_clear_buffer(string);
  260.         sprintf(string, "          level");
  261.         my_fwriteln(printer, string);
  262.         ++line_counter;
  263.  
  264.         for(i=0; i<256; i++){
  265.            if(histogram[i] == 0)
  266.               ++count;
  267.            else 
  268.               count = 0;
  269.  
  270.            if(count < 2){
  271.               printf(" %4d: %7ld",i,histogram[i]);
  272.               print_counter++;
  273.               if(print_counter >= 6){
  274.                  printf("\n");
  275.                  print_counter = 0;
  276.               }  /* ends if print_counter >= 6 */
  277.  
  278.               long_clear_buffer(string);
  279.               sprintf(string, 
  280.                 "           %3d: %7ld ->",i,histogram[i]);
  281.               my_fwrite(printer, string);
  282.               long_clear_buffer(string);
  283.               sprintf(output, " ");
  284.               for(j=0; j<((int)(histogram[i]/scale)); j++){
  285.                  sprintf(string, "*");
  286.                  append_string(string, output);
  287.               }         /* ends loop over j                */
  288.               my_fwriteln(printer, output);
  289.               ++line_counter;
  290.               if(line_counter >= 55){
  291.                  line_counter = 0;
  292.                  putc(FORMFEED, printer);
  293.               }  /* ends if line_counter >=55  */
  294.  
  295.            }                    /* ends if count < 2 */
  296.         }                 /* ends loop over i */
  297.         putc(FORMFEED, printer);
  298.         fclose(printer);
  299.  
  300. }        /* ends print_histogram */
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.         /*********************************************
  310.         *
  311.         *       display_histogram(histogram)
  312.         *
  313.         *       This function shows the histogram
  314.         *       input to the function.
  315.         *
  316.         **********************************************/
  317.  
  318.  
  319.  
  320. display_histogram(histogram, x, y,
  321.                   line_color,data_color)
  322.         int data_color, line_color, x, y;
  323.         unsigned long histogram[];
  324. {
  325.         int     count,
  326.                 i,
  327.                 j,
  328.                 length;
  329.         unsigned long scale, max;
  330.  
  331.         max   = 0;
  332.         count = 0;
  333.  
  334.         for(i=0; i<256; i++)
  335.            if(histogram[i] > max)
  336.               max = histogram[i];
  337.  
  338.         if(max > (300 - 12))
  339.            scale = max/(100 - 12);
  340.         else
  341.            scale = 1;
  342.  
  343.  
  344.    /***************************
  345.    *
  346.    *   clear out an area for
  347.    *   this histogram display
  348.    *
  349.    ****************************/
  350.  
  351.         _setcolor(0);
  352.         for(i=0; i<258; i++){
  353.            for(j=0; j<100; j++){
  354.               _setpixel(x-1+i, y-j);
  355.            }
  356.         }
  357.  
  358.          /***************************
  359.          *
  360.          *  loop thru the histogram
  361.          *  and plot the scaled data
  362.          *
  363.          ****************************/
  364.  
  365.  
  366.         _setlinestyle(0xFFFF);
  367.         _setcolor(data_color);
  368.  
  369.         for(i=0; i<256; i++){
  370.            if(histogram[i] != 0){
  371.               length = histogram[i]/scale;
  372.               _moveto(x+i, y);
  373.               _lineto(x+i, y-length);
  374.            }
  375.         }  /* ends loop over i GRAY_LEVELS */
  376.  
  377.  
  378.  
  379.          /***************************
  380.          *
  381.          *  draw the histogram
  382.          *  axes
  383.          *
  384.          ****************************/
  385.  
  386.  
  387.         _setcolor(line_color);
  388.  
  389.         _moveto(x, y);
  390.         _lineto(x+255, y);
  391.         _moveto(x, y);
  392.         _lineto(x, y-5);
  393.         _moveto(x+50, y);
  394.         _lineto(x+50, y-5);
  395.         _moveto(x+100, y);
  396.         _lineto(x+100, y-5);
  397.         _moveto(x+150, y);
  398.         _lineto(x+150, y-5);
  399.         _moveto(x+200, y);
  400.         _lineto(x+200, y-5);
  401.         _moveto(x+255, y);
  402.         _lineto(x+255, y-5);
  403.  
  404.  
  405. }       /* ends display_histogram */
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415. display_menu_for_histogram(print, vertical, horizontal)
  416.    int        *print, *vertical, *horizontal;
  417. {
  418.    char response[80];
  419.    int  int_response, not_finished, r;
  420.  
  421.    not_finished = 1;
  422.    while(not_finished){
  423.       printf("\n\nHISTOGRAM> Enter choice (0 for no change) ");
  424.       printf("\nHISTOGRAM> 1. print is %d (1=print 0=display)",
  425.               *print);
  426.       printf("\nHISTOGRAM> 2. # of vertical 100x100 areas %d",
  427.               *vertical);
  428.       printf("\nHISTOGRAM> 3. # of horizontal 100x100 areas %d",
  429.              *horizontal);
  430.       printf("\nHISTOGRAM> _\b");
  431.       get_integer(&r);
  432.  
  433.       if(r == 0)
  434.          not_finished = 0;
  435.  
  436.  
  437.       if(r == 1){
  438.          printf(
  439.             "\nHISTOGRAM> Enter 1 for print or 0 for display");
  440.          printf("\nHISTOGRAM> ___");
  441.          get_integer(&int_response);
  442.          *print = int_response;
  443.       }  /* ends if r == 1 */
  444.  
  445.       if(r == 2){
  446.          printf(
  447.             "\nHISTOGRAM> Enter # of vertical 100x100 areas ");
  448.          printf("\nHISTOGRAM> _\b");
  449.          get_integer(&int_response);
  450.          *vertical = int_response;
  451.       }  /* ends if r == 2  */
  452.  
  453.       if(r == 3){
  454.          printf(
  455.          "\nHISTOGRAM> Enter # of horizontal 100x100 areas ");
  456.          printf("\nHISTOGRAM> ___");
  457.          get_integer(&int_response);
  458.          *horizontal = int_response;
  459.       }  /* ends if r == 3 */
  460.  
  461.  
  462.  
  463.    }  /* ends while not_finished  */
  464.  
  465. }  /* ends display_menu  */
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474. calculate_area_histogram(histogram, vertical, horizontal,
  475.                    the_image, name, il, ie, ll, le)
  476.    char     name[];
  477.    int      horizontal, il, ie, ll, le, vertical;
  478.    short    the_image[ROWS][COLS];
  479.    unsigned long histogram[];
  480. {
  481.    int count, i, j;
  482.  
  483.    printf("\nCalculating histograms");
  484.    zero_histogram(histogram);
  485.    count = 1;
  486.  
  487.    for(i=0; i<vertical; i++){
  488.       for(j=0; j<horizontal; j++){
  489.          printf("\n\tcalculating %d of %d",
  490.                count, horizontal*vertical);
  491.          read_tiff_image(name, the_image,
  492.                il+i*ROWS, ie+j*COLS, ll+i*ROWS, le+j*COLS);
  493.          calculate_histogram(the_image, histogram);
  494.          count++;
  495.       }  /* ends loop over j */
  496.    }  /* ends loop over i */
  497.  
  498. }  /* calcualte_area_histogram */
  499.