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 / DJET.C < prev    next >
Text File  |  1990-09-08  |  24KB  |  895 lines

  1.  
  2.  
  3.    /********************************************************
  4.    *
  5.    *   file d:\cips\djet.c
  6.    *
  7.    *   Functions: This file contains
  8.    *      end_graphics_mode
  9.    *      get_graphics_caption
  10.    *      print_bytes
  11.    *      print_graphics_image
  12.    *      print_original_200_row
  13.    *      select_300_dpi_resolution
  14.    *      select_full_graphics_mode
  15.    *      set_horizontal_offset
  16.    *      set_raster_width
  17.    *      start_raster_graphics
  18.    *
  19.    *   Purpose:
  20.    *      These functions print a 200x200 image using
  21.    *      dithering to an HP DeskJet or compatable (Laserjet).  
  22.    *      This uses an 8x8 matrix which gives 64 shades of 
  23.    *      gray.
  24.    *
  25.    *   External Calls:
  26.    *          rtiff.c - read_tiff_image
  27.    *           hist.c - zero_histogram
  28.    *                    calculate_histogram
  29.    *                    perform_histogram_equalization
  30.    *
  31.    *
  32.    *   Modifications:
  33.    *      January 1991 - created
  34.    *      25 August 1991 - modified for use in the 
  35.    *         C Image Processing System.
  36.    *
  37.     ┌─────┐   ┌─────┐
  38.     │     │   │     │   The function print_graphics_image
  39.     │     │   │     │   begins with 2 100x100 image arrays
  40.     │     │   │     │
  41.     │     │   │     │
  42.     └─────┘   └─────┘          
  43.               
  44.     ┌───────────────┐
  45.     │               │   It joins them into 
  46.     │               │   1 100x200 image array
  47.     │               │
  48.     │               │
  49.     └───────────────┘
  50.         
  51.     ┌───────────────┐
  52.     └───────────────┘
  53.     ┌───────────────┐
  54.     └───────────────┘ 
  55.            .            It loops and creates
  56.            .            100 200 element image arrays
  57.            .
  58.     ┌───────────────┐
  59.     └───────────────┘
  60.     
  61.     
  62.           The function print_original_200_row receives a 200 
  63.           element array
  64.     ┌┬──────────────────────────┬┐
  65.     └┴──────────────────────────┴┘
  66.  
  67.           This array is transformed into a 8x200 array of 
  68.           characters called 'row'
  69.     ┌───────── ... ■───────┐
  70.     ├───────── ... ■───────┤
  71.     ├───────── ... ■───────┤
  72.     ├───────── ... ■───────┤
  73.     ├───────── ... ■───────┤
  74.     ├───────── ... ■───────┤
  75.     ├───────── ... ■───────┤
  76.     ├───────── ... ■───────┤
  77.     └───────── ... ■───────┘
  78.  
  79.           Each column of this array is a 1x8 character array which 
  80.           is an 8-bit x 8-bit array
  81.     ╔══╗
  82.     ║  ║
  83.     ╚══╝
  84.           Each row of 'row' is passed to the funnction print_bytes 
  85.           for graphics printing 
  86.     
  87.     
  88.  
  89.  
  90.  
  91.  
  92.    ********************************************************/
  93.  
  94.  
  95. #include "d:\cips\cips.h"
  96.  
  97. #define ESCAPE 27
  98. #define FORMFEED  '\014'
  99.  
  100. short r[200];
  101.  
  102.  
  103.  
  104.       /*******************************************
  105.       *
  106.       *   The patterns array holds the rows to the
  107.       *   8x8 matrices used for printing 
  108.       *   shades of gray.
  109.       *
  110.       ********************************************/
  111.  
  112. char patterns[64][8] =
  113.    { {255, 255, 255, 255, 255, 255, 255, 255},
  114.      {255, 255, 255, 255, 255, 255, 255, 127},
  115.      {255, 255, 255, 255, 255, 255, 255,  63},
  116.      {255, 255, 255, 255, 255, 255, 255,  31},
  117.      {255, 255, 255, 255, 255, 255, 255,  15},
  118.      {255, 255, 255, 255, 255, 255, 255,   7},
  119.      {255, 255, 255, 255, 255, 255, 255,   3},
  120.      {255, 255, 255, 255, 255, 255, 255,   1},
  121.      {255, 255, 255, 255, 255, 255, 255,   0},
  122.      {255, 255, 255, 255, 255, 255, 127,   0},
  123.      {255, 255, 255, 255, 255, 255,  63,   0},
  124.      {255, 255, 255, 255, 255, 255,  31,   0},
  125.      {255, 255, 255, 255, 255, 255,  15,   0},
  126.      {255, 255, 255, 255, 255, 255,   7,   0},
  127.      {255, 255, 255, 255, 255, 255,   3,   0},
  128.      {255, 255, 255, 255, 255, 255,   1,   0},
  129.      {255, 255, 255, 255, 255, 255,   0,   0},
  130.      {255, 255, 255, 255, 255, 127,   0,   0},
  131.      {255, 255, 255, 255, 255,  63,   0,   0},
  132.      {255, 255, 255, 255, 255,  31,   0,   0},
  133.      {255, 255, 255, 255, 255,  15,   0,   0},
  134.      {255, 255, 255, 255, 255,   7,   0,   0},
  135.      {255, 255, 255, 255, 255,   3,   0,   0},
  136.      {255, 255, 255, 255, 255,   1,   0,   0},
  137.      {255, 255, 255, 255, 255,   0,   0,   0},
  138.      {255, 255, 255, 255, 127,   0,   0,   0},
  139.      {255, 255, 255, 255,  63,   0,   0,   0},
  140.      {255, 255, 255, 255,  31,   0,   0,   0},
  141.      {255, 255, 255, 255,  15,   0,   0,   0},
  142.      {255, 255, 255, 255,   7,   0,   0,   0},
  143.      {255, 255, 255, 255,   3,   0,   0,   0},
  144.      {255, 255, 255, 255,   1,   0,   0,   0},
  145.      {255, 255, 255, 255,   0,   0,   0,   0},
  146.      {255, 255, 255, 127,   0,   0,   0,   0},
  147.      {255, 255, 255,  63,   0,   0,   0,   0},
  148.      {255, 255, 255,  31,   0,   0,   0,   0},
  149.      {255, 255, 255,  15,   0,   0,   0,   0},
  150.      {255, 255, 255,   7,   0,   0,   0,   0},
  151.      {255, 255, 255,   3,   0,   0,   0,   0},
  152.      {255, 255, 255,   1,   0,   0,   0,   0},
  153.      {255, 255, 255,   0,   0,   0,   0,   0},
  154.      {255, 255, 127,   0,   0,   0,   0,   0},
  155.      {255, 255,  63,   0,   0,   0,   0,   0},
  156.      {255, 255,  31,   0,   0,   0,   0,   0},
  157.      {255, 255,  15,   0,   0,   0,   0,   0},
  158.      {255, 255,   7,   0,   0,   0,   0,   0},
  159.      {255, 255,   3,   0,   0,   0,   0,   0},
  160.      {255, 255,   1,   0,   0,   0,   0,   0},
  161.      {255, 255,   0,   0,   0,   0,   0,   0},
  162.      {255, 127,   0,   0,   0,   0,   0,   0},
  163.      {255,  63,   0,   0,   0,   0,   0,   0},
  164.      {255,  31,   0,   0,   0,   0,   0,   0},
  165.      {255,  15,   0,   0,   0,   0,   0,   0},
  166.      {255,   7,   0,   0,   0,   0,   0,   0},
  167.      {255,   3,   0,   0,   0,   0,   0,   0},
  168.      {255,   1,   0,   0,   0,   0,   0,   0},
  169.      {255,   0,   0,   0,   0,   0,   0,   0},
  170.      {127,   0,   0,   0,   0,   0,   0,   0},
  171.      { 63,   0,   0,   0,   0,   0,   0,   0},
  172.      { 31,   0,   0,   0,   0,   0,   0,   0},
  173.      { 15,   0,   0,   0,   0,   0,   0,   0},
  174.      {  7,   0,   0,   0,   0,   0,   0,   0},
  175.      {  3,   0,   0,   0,   0,   0,   0,   0},
  176.      {  1,   0,   0,   0,   0,   0,   0,   0}};
  177.  
  178.  
  179.  
  180.  
  181.  
  182. print_graphics_image(image1, image2, image_name,
  183.                      il, ie, ll, le, image_colors,
  184.                      invert, caption, show_hist,
  185.                      color_transform)
  186.    char  caption[], image_name[], color_transform[];
  187.    int   image_colors, invert,
  188.          il, ie, ll, le, show_hist;
  189.    short image1[ROWS][COLS], image2[ROWS][COLS];
  190. {
  191.    char c[80],
  192.         page[80];
  193.  
  194.    FILE *printer;
  195.  
  196.    int  i,
  197.         j;
  198.  
  199.    unsigned long histogram[256], final_hist[256];
  200.    printer = fopen("prn", "w");
  201.  
  202.  
  203.       /**********************************************
  204.       *
  205.       *   Print a few blank lines on the page.
  206.       *
  207.       ***********************************************/
  208.  
  209.    strcpy(page, "                             ");
  210.    my_fwriteln(printer, page);
  211.    my_fwriteln(printer, page);
  212.  
  213.  
  214.       /*****************************************************
  215.       *
  216.       *   Read in two image arrays.
  217.       *
  218.       ******************************************************/
  219.  
  220.    printf("\nReading image");
  221.    read_tiff_image(image_name, image1, il, ie, ll, le);
  222.  
  223.  
  224.    printf("\nReading image");
  225.    read_tiff_image(image_name, image2, il, ie+100, ll, le+100);
  226.  
  227.  
  228.       /*****************************************************
  229.       *
  230.       *   If show_hist is 1 OR do hist equalization
  231.       *   then zero the histogram and
  232.       *   calculate it for the two image arrays.
  233.       *
  234.       ******************************************************/
  235.  
  236.    if( (show_hist == 1)  ||
  237.        (color_transform[0] == 'H')){
  238.       zero_histogram(histogram);
  239.       zero_histogram(final_hist);
  240.       printf("\nDJET> Calculating histograms");
  241.       calculate_histogram(image1, histogram);
  242.       calculate_histogram(image2, histogram);
  243.    }
  244.  
  245.         /**************************************************
  246.         *
  247.         *   Alter the images to 64 gray shades.
  248.         *   Either do it with straight multiply
  249.         *   and divide or use hist equalization.
  250.         *
  251.         *   If using hist equalization then you must
  252.         *   also read and calculate the hist for
  253.         *   the other two image arrays that will be
  254.         *   printed.
  255.         *
  256.         ***************************************************/
  257.  
  258.    if(color_transform[0] == 'S'){
  259.    if(image_colors == 256){
  260.       for(i=0; i<ROWS; i++){
  261.          for(j=0; j<COLS; j++){
  262.             image1[i][j] = image1[i][j]/4;
  263.             image2[i][j] = image2[i][j]/4;
  264.         }
  265.       }
  266.    }  /* ends if image_colors == 256 */
  267.  
  268.  
  269.    if(image_colors == 16){
  270.       for(i=0; i<ROWS; i++){
  271.          for(j=0; j<COLS; j++){
  272.             image1[i][j] = image1[i][j]*4;
  273.             image2[i][j] = image2[i][j]*4;
  274.         }
  275.       }
  276.    }  /* ends if image_colors == 16 */
  277.    }  /* ends if color_transform == S */
  278.  
  279.    if(color_transform[0] == 'H'){
  280.  
  281.       printf("\nReading image");
  282.       read_tiff_image(image_name, image1, il+100, ie, ll+100, le);
  283.       printf("\nReading image");
  284.       read_tiff_image(image_name, image2,
  285.                    il+100, ie+100, ll+100, le+100);
  286.       printf("\nDJET> Calculating histograms");
  287.       calculate_histogram(image1, histogram);
  288.       calculate_histogram(image2, histogram);
  289.  
  290.       printf("\nReading image");
  291.       read_tiff_image(image_name, image1, il, ie, ll, le);
  292.  
  293.       printf("\nReading image");
  294.       read_tiff_image(image_name, image2, il, ie+100, ll, le+100);
  295.  
  296.       printf("\nDJET> Equalizing histogram");
  297.       perform_histogram_equalization(image1, histogram,
  298.                                      64.0, 40000.0);
  299.       printf("\nDJET> Equalizing histogram");
  300.       perform_histogram_equalization(image2, histogram,
  301.                                      64.0, 40000.0);
  302.  
  303.       printf("\nDJET> Calculating histograms");
  304.       calculate_histogram(image1, final_hist);
  305.       calculate_histogram(image2, final_hist);
  306.  
  307.  
  308.    }  /* ends if color_transform == H */
  309.  
  310.  
  311.  
  312.       /************************************************
  313.       *
  314.       *   If invert is set them invert the transformed
  315.       *   image arrays (they now only have 64 shades
  316.       *   of gray).
  317.       *
  318.       *************************************************/
  319.  
  320.    if(invert == 1){
  321.       for(i=0; i<ROWS; i++){
  322.          for(j=0; j<COLS; j++){
  323.                image1[i][j] = 63 - image1[i][j];
  324.                image2[i][j] = 63 - image2[i][j];
  325.          }
  326.       }
  327.    }
  328.  
  329.  
  330.  
  331.         /**************************************************
  332.         *
  333.         *   Now set the graphics mode on the printer
  334.         *
  335.         ***************************************************/
  336.  
  337.    printf("\nBegin");
  338.    end_graphics_mode(printer);
  339.    select_300_dpi_resolution(printer);
  340.    set_raster_width(printer);
  341.    start_raster_graphics(printer);
  342.    select_full_graphics_mode(printer);
  343.  
  344.         /**************************************************
  345.         *
  346.         *   Print the two arrays to make a 100x200 output.
  347.         *   To do this you loop over 100 rows, set the 
  348.         *   r buffer to the image values, set the
  349.         *   graphics, and print the row via function
  350.         *   print_original_200_row.
  351.         *
  352.         ***************************************************/
  353.  
  354.    for(i=0; i<100; i++){
  355.       for(j=0; j<100; j++){
  356.          r[j]     = image1[i][j];
  357.          r[j+100] = image2[i][j];
  358.       }  /* ends loop over j */
  359.  
  360.       end_graphics_mode(printer);
  361.       select_300_dpi_resolution(printer);
  362.       set_raster_width(printer);
  363.       start_raster_graphics(printer);
  364.       select_full_graphics_mode(printer);
  365.  
  366.       print_original_200_row(printer, r);
  367.  
  368.       printf("\n\tPrinting row %d", i);
  369.    }  /* ends loop over i */
  370.  
  371.            /* ends first half */
  372.  
  373.  
  374.  
  375.       /*****************************************************
  376.       *
  377.       *   In order to print 200x200 repeat
  378.       *   the above steps for 2 more 100x100 arrays
  379.       *
  380.       ******************************************************/
  381.  
  382.  
  383.    printf("\nReading image");
  384.    read_tiff_image(image_name, image1, il+100, ie, ll+100, le);
  385.    printf("\nReading image");
  386.    read_tiff_image(image_name, image2,
  387.                    il+100, ie+100, ll+100, le+100);
  388.  
  389.  
  390.         /**************************************************
  391.         *
  392.         *   Alter the images to 64 shades of gray.
  393.         *
  394.         *   Either do it with straight multiply
  395.         *   and divide or use hist equalization.
  396.         *
  397.         ***************************************************/
  398.  
  399.  
  400.    if(color_transform[0] == 'S'){
  401.    if(image_colors == 256){
  402.       for(i=0; i<ROWS; i++){
  403.          for(j=0; j<COLS; j++){
  404.             image1[i][j] = image1[i][j]/4;
  405.             image2[i][j] = image2[i][j]/4;
  406.         }
  407.       }
  408.    }  /* ends if image_colors == 256 */
  409.  
  410.  
  411.    if(image_colors == 16){
  412.       for(i=0; i<ROWS; i++){
  413.          for(j=0; j<COLS; j++){
  414.             image1[i][j] = image1[i][j]*4;
  415.             image2[i][j] = image2[i][j]*4;
  416.         }
  417.       }
  418.    }  /* ends if image_colors == 16 */
  419.    }  /* ends if color_transform == S */
  420.  
  421.  
  422.  
  423.    if(color_transform[0] == 'H'){
  424.  
  425.       printf("\nDJET> Equalizing histogram");
  426.       perform_histogram_equalization(image1, histogram,
  427.                                      64.0, 40000.0);
  428.       printf("\nDJET> Equalizing histogram");
  429.       perform_histogram_equalization(image2, histogram,
  430.                                      64.0, 40000.0);
  431.  
  432.       printf("\nDJET> Calculating histograms");
  433.       calculate_histogram(image1, final_hist);
  434.       calculate_histogram(image2, final_hist);
  435.  
  436.    }  /* ends if color_transform == S */
  437.  
  438.  
  439.  
  440.  
  441.       /************************************************
  442.       *
  443.       *   If invert is set them invert the transformed
  444.       *   image arrays (they now only have 64 shades
  445.       *   of gray).
  446.       *
  447.       *************************************************/
  448.  
  449.    if(invert == 1){
  450.       for(i=0; i<ROWS; i++){
  451.          for(j=0; j<COLS; j++){
  452.                image1[i][j] = 63 - image1[i][j];
  453.                image2[i][j] = 63 - image2[i][j];
  454.          }
  455.       }
  456.    }
  457.  
  458.  
  459.  
  460.    printf("\nBegin");
  461.    end_graphics_mode(printer);
  462.    select_300_dpi_resolution(printer);
  463.    set_raster_width(printer);
  464.    start_raster_graphics(printer);
  465.    select_full_graphics_mode(printer);
  466.  
  467.  
  468.         /**************************************************
  469.         *
  470.         *   Print the two arrays to make a 100x200 output.
  471.         *   To do this you loop over 100 rows, set the 
  472.         *   r buffer to the image values, set the
  473.         *   graphics, and print the row via function
  474.         *   print_original_200_row.
  475.         *
  476.         ***************************************************/
  477.  
  478.    for(i=0; i<100; i++){
  479.       for(j=0; j<100; j++){
  480.          r[j]     = image1[i][j];
  481.          r[j+100] = image2[i][j];
  482.       }  /* ends loop over j */
  483.  
  484.       end_graphics_mode(printer);
  485.       select_300_dpi_resolution(printer);
  486.       set_raster_width(printer);
  487.       start_raster_graphics(printer);
  488.       select_full_graphics_mode(printer);
  489.  
  490.       print_original_200_row(printer, r);
  491.  
  492.       printf("\n\tPrinting row %d", i);
  493.    }  /* ends loop over i */
  494.  
  495.  
  496.       /*****************************************************
  497.       *
  498.       *   If show_hist is 1 then calculate the histogram
  499.       *   for the two image arrays and print the histogram.
  500.       *
  501.       ******************************************************/
  502.  
  503.    if(show_hist == 1){
  504.       if(color_transform[0] == 'S'){
  505.          calculate_histogram(image1, histogram);
  506.          calculate_histogram(image2, histogram);
  507.          print_hist_image(printer, histogram);
  508.       }
  509.       if(color_transform[0] == 'H'){
  510.          print_hist_image(printer, final_hist);
  511.       }
  512.    }
  513.  
  514.         /**************************************************
  515.         *
  516.         *   Print a couple of blank lines then print      
  517.         *   the caption. 
  518.         *
  519.         ***************************************************/
  520.  
  521.    end_graphics_mode(printer);
  522.    strcpy(page, "      ");
  523.    my_fwriteln(printer, page);
  524.    my_fwriteln(printer, page);
  525.  
  526.    sprintf(page, "                      %s", caption);
  527.    my_fwriteln(printer, page);
  528.  
  529.    putc(FORMFEED, printer);
  530.  
  531.    fclose(printer);
  532.  
  533.    printf("\nEnd");
  534.  
  535. }  /* ends print_graphics_image */
  536.  
  537.  
  538.  
  539.    /****************************************************
  540.    *
  541.    *   get_graphics_caption(...
  542.    *
  543.    ****************************************************/
  544.  
  545. get_graphics_caption(caption)
  546.    char caption[];
  547. {
  548.    printf("\nEnter the caption to be printed\n---");
  549.    read_string(caption);
  550.  
  551. }  /* ends get_graphics_caption */
  552.  
  553.  
  554.  
  555.  
  556.  
  557. select_full_graphics_mode(printer)
  558.  
  559.    FILE *printer;
  560. {
  561.    putc(ESCAPE, printer);
  562.    putc('*', printer);
  563.    putc('b', printer);
  564.    putc('0', printer);
  565.    putc('M', printer);
  566.  
  567. }
  568.  
  569.  
  570. set_horizontal_offset(printer)
  571.  
  572.    FILE *printer;
  573. {
  574.    putc(ESCAPE, printer);
  575.    putc('*', printer);
  576.    putc('b', printer);
  577.    putc('4', printer);
  578.    putc('9', printer);
  579.    putc('6', printer);
  580.    putc('X', printer);
  581.  
  582. }
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589. set_shorter_horizontal_offset(printer)
  590.  
  591.    FILE *printer;
  592. {
  593.    putc(ESCAPE, printer);
  594.    putc('*', printer);
  595.    putc('b', printer);
  596.    putc('4', printer);
  597.    putc('8', printer);
  598.    putc('0', printer);
  599.    putc('X', printer);
  600.  
  601. }
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608. end_graphics_mode(printer)
  609.    FILE *printer;
  610. {
  611.    putc(ESCAPE, printer);
  612.    putc('*', printer);
  613.    putc('r', printer);
  614.    putc('B', printer);
  615. }
  616.  
  617.  
  618. set_raster_width(printer)
  619.  
  620.    FILE *printer;
  621. {
  622.    putc(ESCAPE, printer);
  623.    putc('*', printer);
  624.    putc('r', printer);
  625.    putc('2', printer);
  626.    putc('2', printer);
  627.    putc('0', printer);
  628.    putc('0', printer);
  629.    putc('S', printer);
  630.  
  631. }
  632.  
  633.  
  634.  
  635. start_raster_graphics(printer)
  636.  
  637.    FILE *printer;
  638. {
  639.    putc(ESCAPE, printer);
  640.    putc('*', printer);
  641.    putc('r', printer);
  642.    putc('0', printer);
  643.    putc('A', printer);
  644.  
  645. }
  646.  
  647.  
  648.  
  649. select_300_dpi_resolution(printer)
  650.  
  651.    FILE *printer;
  652. {
  653.    putc(ESCAPE, printer);
  654.    putc('*', printer);
  655.    putc('t', printer);
  656.    putc('3', printer);
  657.    putc('0', printer);
  658.    putc('0', printer);
  659.    putc('R', printer);
  660.  
  661. }
  662.  
  663.  
  664.  
  665.  
  666. print_bytes(printer, buffer)
  667.    FILE *printer;
  668.    char buffer[];
  669. {
  670.    int        i;
  671.  
  672.    putc(ESCAPE, printer);
  673.    putc('*', printer);
  674.    putc('b', printer);
  675.    putc('2', printer);
  676.    putc('0', printer);
  677.    putc('0', printer);
  678.    putc('W', printer);
  679.  
  680.  
  681.    for(i=0; i<200; i++){
  682.       putc(buffer[i], printer);
  683.    }
  684. }  /* ends print_bytes */
  685.  
  686.  
  687.  
  688.  
  689.    /**************************************************
  690.    *
  691.    *   print_original_200_row(...
  692.    *
  693.    ***************************************************/
  694.  
  695. print_original_200_row(printer, short_row)
  696.    FILE  *printer;
  697.    short short_row[200];
  698. {
  699.    char  row[8][200];
  700.    char  c[200], response[80];
  701.    int         i, j, k;
  702.    short value;
  703.    for(i=0; i<200; i++){
  704.       value = short_row[i];
  705.       if(value > 63) value = 63;
  706.       if(value < 0)  value =  0;
  707.  
  708.       for(j=0; j<8; j++)
  709.          row[j][i] = patterns[value][j];
  710.  
  711.    }  /* ends loop over i */
  712.  
  713.    for(i=0; i<8; i++){
  714.       for(j=0; j<200; j++)
  715.          c[j] = row[i][j];
  716.       set_horizontal_offset(printer);
  717.       print_bytes(printer, c);
  718.    }  /* ends loop over i */
  719.  
  720. }  /* ends print_original_200_row */
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.    /***********************************
  728.    *
  729.    *   print_hist_image(...
  730.    *
  731.    ************************************/
  732.  
  733.  
  734.  
  735. print_hist_image(printer, hist)
  736.    FILE *printer;
  737.    unsigned long hist[];
  738. {
  739.    char   c, d;
  740.    int          i, j, k;
  741.    unsigned long limit, max;
  742.  
  743.    d = 0;
  744.    c = 255;
  745.  
  746.       /********************************
  747.       *
  748.       *   First scale the histogram
  749.       *
  750.       *********************************/
  751.  
  752.    max = 0;
  753.    for(i=0; i<256; i++)
  754.       if(hist[i] > max) max = hist[i];
  755.  
  756.    if(max > 200){
  757.       for(i=0; i<256; i++){
  758.         hist[i] = (hist[i]*200)/max;
  759.       }
  760.    }
  761.  
  762.  
  763.       /********************************
  764.       *
  765.       *   Second print it
  766.       *
  767.       *   Print a space between the image
  768.       *   and the histogram.
  769.       *
  770.       *********************************/
  771.  
  772.  
  773.    for(i=0; i<20; i++){
  774.          end_graphics_mode(printer);
  775.          select_300_dpi_resolution(printer);
  776.          set_raster_width(printer);
  777.          start_raster_graphics(printer);
  778.          select_full_graphics_mode(printer);
  779.          set_horizontal_offset(printer);
  780.          putc(ESCAPE, printer);
  781.          putc('*', printer);
  782.          putc('b', printer);
  783.          putc('2', printer);
  784.          putc('0', printer);
  785.          putc('0', printer);
  786.          putc('W', printer);
  787.  
  788.          for(j=0; j<200; j++)
  789.             putc(d, printer);
  790.    }
  791.  
  792.  
  793.    printf("\n\nHIST> Now printing the histogram");
  794.    for(i=0; i<256; i++){
  795.       printf("\n\tHIST> Histogram[%d]=%ld", i, hist[i]);
  796.  
  797.             /* print the line 2 times */
  798.       for(k=0; k<2; k++){
  799.  
  800.          end_graphics_mode(printer);
  801.          select_300_dpi_resolution(printer);
  802.          set_raster_width(printer);
  803.          start_raster_graphics(printer);
  804.          select_full_graphics_mode(printer);
  805.  
  806.  
  807.             /***************************
  808.             *
  809.             *        Print grid marks every
  810.             *        50 pixels.  Do this by
  811.             *        setting a shorter margin
  812.             *        then printing 2 marks then
  813.             *        the data.
  814.             *
  815.             ****************************/
  816.  
  817.          if( (i ==   0)        ||
  818.              (i ==  50) ||
  819.              (i == 100) ||
  820.              (i == 150) ||
  821.              (i == 200) ||
  822.              (i == 255)){
  823.  
  824.             set_shorter_horizontal_offset(printer);
  825.             putc(ESCAPE, printer);
  826.             putc('*', printer);
  827.             putc('b', printer);
  828.             putc('2', printer);
  829.             putc('0', printer);
  830.             putc('2', printer);
  831.             putc('W', printer);
  832.  
  833.             putc(c, printer);
  834.             putc(c, printer);
  835.  
  836.  
  837.             if(hist[i] >= 200)
  838.                hist[i] = 200;
  839.  
  840.             limit = 200 - hist[i];
  841.  
  842.             if(hist[i] == 0)
  843.                putc(c, printer);
  844.  
  845.             for(j=0; j<hist[i]; j++)
  846.                putc(c, printer);
  847.  
  848.             for(j=0; j<limit; j++)
  849.               putc(d, printer);
  850.  
  851.          }  /* ends print grid marks */
  852.  
  853.  
  854.             /***************************
  855.             *
  856.             *        If you do not print
  857.             *        grid marks, set the normal
  858.             *        margin and then print the
  859.             *        data.
  860.             *
  861.             ****************************/
  862.  
  863.          else{
  864.             set_horizontal_offset(printer);
  865.             /* this prints 200 bytes so print 200 */
  866.             putc(ESCAPE, printer);
  867.             putc('*', printer);
  868.             putc('b', printer);
  869.             putc('2', printer);
  870.             putc('0', printer);
  871.             putc('0', printer);
  872.             putc('W', printer);
  873.  
  874.             if(hist[i] >= 200)
  875.                hist[i] = 200;
  876.  
  877.             limit = 200 - hist[i];
  878.  
  879.             if(hist[i] == 0)
  880.                putc(c, printer);
  881.  
  882.             for(j=0; j<hist[i]; j++)
  883.                putc(c, printer);
  884.  
  885.             for(j=0; j<limit; j++)
  886.               putc(d, printer);
  887.  
  888.          }  /* ends else no grid marks */
  889.  
  890.       }  /* ends loop over k */
  891.  
  892.    }  /* ends loop over i */
  893.  
  894. }  /* ends print_hist_image */
  895.