home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_06 / 9n06098a < prev    next >
Text File  |  1991-04-14  |  13KB  |  464 lines

  1. /*********************************************
  2. *   file d:\cips\djet.c
  3. *
  4. *
  5. *   Purpose: These functions print a 200x200 
  6. *      image using dithering to an HP DeskJet 
  7. *      or compatable (Laserjet). This uses an 
  8. *      8x8 matrix which gives 64 shades of 
  9. *      gray.
  10. *
  11. *   External Calls:
  12. *      rtiff.c - read_tiff_image (March CUJ,
  13. *                                 Listing 3)
  14. *
  15. *   Modifications:
  16. *      January 1991 - created
  17. *      25 August 1991 - modified for use in the 
  18. *         C Image Processing System.
  19. **********************************************/
  20.  
  21. #include "d:\cips\cips.h"
  22.  
  23. #define ESCAPE 27
  24. #define FORMFEED  '\014'
  25.  
  26. short r[200];
  27.  
  28.  
  29.  /*******************************************
  30.  *
  31.  *   The patterns array holds the rows to the
  32.  *   8x8 matrices used for printing 
  33.  *   shades of gray.
  34.  *
  35.  ********************************************/
  36.  
  37. char patterns[64][8] =
  38.    { {255, 255, 255, 255, 255, 255, 255, 255},
  39.      {255, 255, 255, 255, 255, 255, 255, 127},
  40.      {255, 255, 255, 255, 255, 255, 255,  63},
  41.      {255, 255, 255, 255, 255, 255, 255,  31},
  42.      {255, 255, 255, 255, 255, 255, 255,  15},
  43.      {255, 255, 255, 255, 255, 255, 255,   7},
  44.      {255, 255, 255, 255, 255, 255, 255,   3},
  45.      {255, 255, 255, 255, 255, 255, 255,   1},
  46.      {255, 255, 255, 255, 255, 255, 255,   0},
  47.      {255, 255, 255, 255, 255, 255, 127,   0},
  48.      {255, 255, 255, 255, 255, 255,  63,   0},
  49.      {255, 255, 255, 255, 255, 255,  31,   0},
  50.      {255, 255, 255, 255, 255, 255,  15,   0},
  51.      {255, 255, 255, 255, 255, 255,   7,   0},
  52.      {255, 255, 255, 255, 255, 255,   3,   0},
  53.      {255, 255, 255, 255, 255, 255,   1,   0},
  54.      {255, 255, 255, 255, 255, 255,   0,   0},
  55.      {255, 255, 255, 255, 255, 127,   0,   0},
  56.      {255, 255, 255, 255, 255,  63,   0,   0},
  57.      {255, 255, 255, 255, 255,  31,   0,   0},
  58.      {255, 255, 255, 255, 255,  15,   0,   0},
  59.      {255, 255, 255, 255, 255,   7,   0,   0},
  60.      {255, 255, 255, 255, 255,   3,   0,   0},
  61.      {255, 255, 255, 255, 255,   1,   0,   0},
  62.      {255, 255, 255, 255, 255,   0,   0,   0},
  63.      {255, 255, 255, 255, 127,   0,   0,   0},
  64.      {255, 255, 255, 255,  63,   0,   0,   0},
  65.      {255, 255, 255, 255,  31,   0,   0,   0},
  66.      {255, 255, 255, 255,  15,   0,   0,   0},
  67.      {255, 255, 255, 255,   7,   0,   0,   0},
  68.      {255, 255, 255, 255,   3,   0,   0,   0},
  69.      {255, 255, 255, 255,   1,   0,   0,   0},
  70.      {255, 255, 255, 255,   0,   0,   0,   0},
  71.      {255, 255, 255, 127,   0,   0,   0,   0},
  72.      {255, 255, 255,  63,   0,   0,   0,   0},
  73.      {255, 255, 255,  31,   0,   0,   0,   0},
  74.      {255, 255, 255,  15,   0,   0,   0,   0},
  75.      {255, 255, 255,   7,   0,   0,   0,   0},
  76.      {255, 255, 255,   3,   0,   0,   0,   0},
  77.      {255, 255, 255,   1,   0,   0,   0,   0},
  78.      {255, 255, 255,   0,   0,   0,   0,   0},
  79.      {255, 255, 127,   0,   0,   0,   0,   0},
  80.      {255, 255,  63,   0,   0,   0,   0,   0},
  81.      {255, 255,  31,   0,   0,   0,   0,   0},
  82.      {255, 255,  15,   0,   0,   0,   0,   0},
  83.      {255, 255,   7,   0,   0,   0,   0,   0},
  84.      {255, 255,   3,   0,   0,   0,   0,   0},
  85.      {255, 255,   1,   0,   0,   0,   0,   0},
  86.      {255, 255,   0,   0,   0,   0,   0,   0},
  87.      {255, 127,   0,   0,   0,   0,   0,   0},
  88.      {255,  63,   0,   0,   0,   0,   0,   0},
  89.      {255,  31,   0,   0,   0,   0,   0,   0},
  90.      {255,  15,   0,   0,   0,   0,   0,   0},
  91.      {255,   7,   0,   0,   0,   0,   0,   0},
  92.      {255,   3,   0,   0,   0,   0,   0,   0},
  93.      {255,   1,   0,   0,   0,   0,   0,   0},
  94.      {255,   0,   0,   0,   0,   0,   0,   0},
  95.      {127,   0,   0,   0,   0,   0,   0,   0},
  96.      { 63,   0,   0,   0,   0,   0,   0,   0},
  97.      { 31,   0,   0,   0,   0,   0,   0,   0},
  98.      { 15,   0,   0,   0,   0,   0,   0,   0},
  99.      {  7,   0,   0,   0,   0,   0,   0,   0},
  100.      {  3,   0,   0,   0,   0,   0,   0,   0},
  101.      {  1,   0,   0,   0,   0,   0,   0,   0}};
  102.  
  103.  
  104. print_graphics_image(image1, image2, image_name,
  105.                 il, ie, ll, le, image_colors,
  106.                 invert, caption)
  107.    char  caption[], image_name[];
  108.    int   image_colors, invert, il, ie, ll, le;
  109.    short image1[ROWS][COLS], image2[ROWS][COLS];
  110. {
  111.    char c[80],
  112.    page[80];
  113.  
  114.    FILE *printer;
  115.  
  116.    int  i,
  117.    j;
  118.  
  119.    unsigned long histogram[256], final_hist[256];
  120.    printer = fopen("prn", "w");
  121.  
  122.  
  123.  /*****************************************
  124.  *   Print a few blank lines on the page.
  125.  ******************************************/
  126.  
  127.    strcpy(page, "                             ");
  128.    my_fwriteln(printer, page);
  129.    my_fwriteln(printer, page);
  130.  
  131.  /************************************
  132.  *   Read in two image arrays.
  133.  *************************************/
  134.  
  135.    printf("\nReading image");
  136.    read_tiff_image(image_name, image1, il, ie, 
  137.                    ll, le);
  138.    printf("\nReading image");
  139.    read_tiff_image(image_name, image2, il, ie+100, 
  140.                    ll, le+100);
  141.  
  142.    if(invert == 1){
  143.       for(i=0; i<ROWS; i++){
  144.          for(j=0; j<COLS; j++){
  145.             if(image_colors == 256){
  146.                image1[i][j] = 255 - image1[i][j];
  147.                image2[i][j] = 255 - image2[i][j];
  148.             }
  149.             if(image_colors == 16){
  150.                image1[i][j] = 15 - image1[i][j];
  151.                image2[i][j] = 15 - image2[i][j];
  152.             }
  153.          }
  154.       }
  155.    }
  156.  
  157.    /**************************************************
  158.    *   Alter the images to 64 gray shades.
  159.    ***************************************************/
  160.    if(image_colors == 256){
  161.       for(i=0; i<ROWS; i++){
  162.          for(j=0; j<COLS; j++){
  163.             image1[i][j] = image1[i][j]/4;
  164.             image2[i][j] = image2[i][j]/4;
  165.         }
  166.       }
  167.    }  /* ends if image_colors == 256 */
  168.  
  169.  
  170.    if(image_colors == 16){
  171.       for(i=0; i<ROWS; i++){
  172.          for(j=0; j<COLS; j++){
  173.             image1[i][j] = image1[i][j]*4;
  174.             image2[i][j] = image2[i][j]*4;
  175.          }
  176.       }
  177.    }  /* ends if image_colors == 16 */
  178.  
  179. /*******************************************
  180. *   Now set the graphics mode on the printer
  181. ********************************************/
  182.  
  183.    printf("\nBegin");
  184.    end_graphics_mode(printer);
  185.    select_300_dpi_resolution(printer);
  186.    set_raster_width(printer);
  187.    start_raster_graphics(printer);
  188.    select_full_graphics_mode(printer);
  189.  
  190. /**************************************************
  191. *   Print the two arrays to make a 100x200 output.
  192. *   To do this you loop over 100 rows, set the 
  193. *   r buffer to the image values, set the
  194. *   graphics, and print the row via function
  195. *   print_original_200_row.
  196. ***************************************************/
  197.  
  198.    for(i=0; i<100; i++){
  199.       for(j=0; j<100; j++){
  200.          r[j]     = image1[i][j];
  201.          r[j+100] = image2[i][j];
  202.       }  /* ends loop over j */
  203.  
  204.       end_graphics_mode(printer);
  205.       select_300_dpi_resolution(printer);
  206.       set_raster_width(printer);
  207.       start_raster_graphics(printer);
  208.       select_full_graphics_mode(printer);
  209.  
  210.       print_original_200_row(printer, r);
  211.  
  212.       printf("\n\tPrinting row %d", i);
  213.    }  /* ends loop over i */
  214.  
  215.  /*********************************************
  216.  *   In order to print 200x200 repeat
  217.  *   the above steps for 2 more 100x100 arrays
  218.  **********************************************/
  219.  
  220.    printf("\nReading image");
  221.    read_tiff_image(image_name, image1, il+100, ie,
  222.                    ll+100, le);
  223.    printf("\nReading image");
  224.    read_tiff_image(image_name, image2,
  225.               il+100, ie+100, ll+100, le+100);
  226.  
  227.    if(invert == 1){
  228.       for(i=0; i<ROWS; i++){
  229.          for(j=0; j<COLS; j++){
  230.             if(image_colors == 256){
  231.                image1[i][j] = 255 - image1[i][j];
  232.                image2[i][j] = 255 - image2[i][j];
  233.             }
  234.             if(image_colors == 16){
  235.                image1[i][j] = 15 - image1[i][j];
  236.                image2[i][j] = 15 - image2[i][j];
  237.             }
  238.          }
  239.       }
  240.    }
  241.  
  242. /**************************************************
  243. *   Alter the images to 64 shades of gray.
  244. ***************************************************/
  245.  
  246.  
  247.    if(image_colors == 256){
  248.       for(i=0; i<ROWS; i++){
  249.          for(j=0; j<COLS; j++){
  250.             image1[i][j] = image1[i][j]/4;
  251.             image2[i][j] = image2[i][j]/4;
  252.         }
  253.       }
  254.    }  /* ends if image_colors == 256 */
  255.  
  256.  
  257.    if(image_colors == 16){
  258.       for(i=0; i<ROWS; i++){
  259.          for(j=0; j<COLS; j++){
  260.             image1[i][j] = image1[i][j]*4;
  261.             image2[i][j] = image2[i][j]*4;
  262.          }
  263.       }
  264.    }  /* ends if image_colors == 16 */
  265.  
  266.  
  267.    printf("\nBegin");
  268.    end_graphics_mode(printer);
  269.    select_300_dpi_resolution(printer);
  270.    set_raster_width(printer);
  271.    start_raster_graphics(printer);
  272.    select_full_graphics_mode(printer);
  273.  
  274.  
  275. /**************************************************
  276. *   Print the two arrays to make a 100x200 output.
  277. *   To do this you loop over 100 rows, set the 
  278. *   r buffer to the image values, set the
  279. *   graphics, and print the row via function
  280. *   print_original_200_row.
  281. ***************************************************/
  282.    for(i=0; i<100; i++){
  283.       for(j=0; j<100; j++){
  284.          r[j]     = image1[i][j];
  285.          r[j+100] = image2[i][j];
  286.       }  /* ends loop over j */
  287.  
  288.       end_graphics_mode(printer);
  289.       select_300_dpi_resolution(printer);
  290.       set_raster_width(printer);
  291.       start_raster_graphics(printer);
  292.       select_full_graphics_mode(printer);
  293.  
  294.       print_original_200_row(printer, r);
  295.  
  296.       printf("\n\tPrinting row %d", i);
  297.    }  /* ends loop over i */
  298.  
  299. /**************************************************
  300. *   Print a couple of blank lines then print      
  301. *   the caption. 
  302. ***************************************************/
  303.  
  304.    end_graphics_mode(printer);
  305.    strcpy(page, "      ");
  306.    my_fwriteln(printer, page);
  307.    my_fwriteln(printer, page);
  308.  
  309.    sprintf(page, "                      %s", caption);
  310.    my_fwriteln(printer, page);
  311.  
  312.    putc(FORMFEED, printer);
  313.  
  314.    fclose(printer);
  315.  
  316.    printf("\nEnd");
  317.  
  318. }  /* ends print_graphics_image */
  319.  
  320. /*********************************
  321. *   get_graphics_caption(...
  322. **********************************/
  323.  
  324. get_graphics_caption(caption)
  325.    char caption[];
  326. {
  327.    printf("\nEnter the caption to be printed\n---");
  328.    read_string(caption);
  329.  
  330. }  /* ends get_graphics_caption */
  331.  
  332.  
  333. select_full_graphics_mode(printer)
  334.  
  335.    FILE *printer;
  336. {
  337.    putc(ESCAPE, printer);
  338.    putc('*', printer);
  339.    putc('b', printer);
  340.    putc('0', printer);
  341.    putc('M', printer);
  342.  
  343. }
  344.  
  345.  
  346. set_horizontal_offset(printer)
  347.  
  348.    FILE *printer;
  349. {
  350.    putc(ESCAPE, printer);
  351.    putc('*', printer);
  352.    putc('b', printer);
  353.    putc('4', printer);
  354.    putc('9', printer);
  355.    putc('6', printer);
  356.    putc('X', printer);
  357.  
  358. }
  359.  
  360.  
  361. end_graphics_mode(printer)
  362.    FILE *printer;
  363. {
  364.    putc(ESCAPE, printer);
  365.    putc('*', printer);
  366.    putc('r', printer);
  367.    putc('B', printer);
  368. }
  369.  
  370.  
  371. set_raster_width(printer)
  372.  
  373.    FILE *printer;
  374. {
  375.    putc(ESCAPE, printer);
  376.    putc('*', printer);
  377.    putc('r', printer);
  378.    putc('2', printer);
  379.    putc('2', printer);
  380.    putc('0', printer);
  381.    putc('0', printer);
  382.    putc('S', printer);
  383.  
  384. }
  385.  
  386.  
  387. start_raster_graphics(printer)
  388.  
  389.    FILE *printer;
  390. {
  391.    putc(ESCAPE, printer);
  392.    putc('*', printer);
  393.    putc('r', printer);
  394.    putc('0', printer);
  395.    putc('A', printer);
  396.  
  397. }
  398.  
  399.  
  400.  
  401. select_300_dpi_resolution(printer)
  402.  
  403.    FILE *printer;
  404. {
  405.    putc(ESCAPE, printer);
  406.    putc('*', printer);
  407.    putc('t', printer);
  408.    putc('3', printer);
  409.    putc('0', printer);
  410.    putc('0', printer);
  411.    putc('R', printer);
  412.  
  413. }
  414.  
  415.  
  416. print_bytes(printer, buffer)
  417.    FILE *printer;
  418.    char buffer[];
  419. {
  420.    int        i;
  421.  
  422.    putc(ESCAPE, printer);
  423.    putc('*', printer);
  424.    putc('b', printer);
  425.    putc('2', printer);
  426.    putc('0', printer);
  427.    putc('0', printer);
  428.    putc('W', printer);
  429.  
  430.    for(i=0; i<200; i++){
  431.       putc(buffer[i], printer);
  432.    }
  433. }  /* ends print_bytes */
  434.  
  435. /********************************
  436. *   print_original_200_row(...
  437. *********************************/
  438.  
  439. print_original_200_row(printer, short_row)
  440.    FILE  *printer;
  441.    short short_row[200];
  442. {
  443.    char  row[8][200];
  444.    char  c[200], response[80];
  445.    int     i, j, k;
  446.    short value;
  447.  
  448.    for(i=0; i<200; i++){
  449.       value = short_row[i];
  450.       if(value > 63) value = 63;
  451.       if(value < 0)  value =  0;
  452.  
  453.       for(j=0; j<8; j++)
  454.          row[j][i] = patterns[value][j];
  455.    }  /* ends loop over i */
  456.  
  457.    for(i=0; i<8; i++){
  458.       for(j=0; j<200; j++)
  459.          c[j] = row[i][j];
  460.       set_horizontal_offset(printer);
  461.       print_bytes(printer, c);
  462.    }  /* ends loop over i */
  463. }  /* ends print_original_200_row */
  464.