home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_08 / 9n08064a < prev    next >
Text File  |  1991-06-21  |  12KB  |  497 lines

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