home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GDEVPJET.C < prev    next >
C/C++ Source or Header  |  1992-08-03  |  8KB  |  254 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevpjet.c */
  21. /* H-P PaintJet, PaintJet XL, and DEC LJ250 driver for Ghostscript. */
  22. /* Thanks to Rob Reiss (rob@moray.berkeley.edu) for the PaintJet XL */
  23. /* modifications. */
  24. #include "gdevprn.h"
  25. #include "gdevpcl.h"
  26.  
  27. /* X_DPI and Y_DPI must be the same, and may be either 90 or 180. */
  28. #define X_DPI 180
  29. #define Y_DPI 180
  30.  
  31. /* We round up LINE_SIZE to a multiple of 8 bytes */
  32. /* because that's the unit of transposition from pixels to planes. */
  33. #define LINE_SIZE ((X_DPI * 85 / 10 + 63) / 64 * 8)
  34.  
  35. /* The device descriptors */
  36. private dev_proc_print_page(lj250_print_page);
  37. private dev_proc_print_page(paintjet_print_page);
  38. private dev_proc_print_page(pjetxl_print_page);
  39. private int pj_common_print_page(P4(gx_device_printer *, FILE *, int, const char *));
  40. private gx_device_procs paintjet_procs =
  41.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  42.     gdev_pcl_3bit_map_rgb_color, gdev_pcl_3bit_map_color_rgb);
  43. gx_device_printer gs_lj250_device =
  44.   prn_device(paintjet_procs, "lj250",
  45.     85,                /* width_10ths, 8.5" */
  46.     110,                /* height_10ths, 11" */
  47.     X_DPI, Y_DPI,
  48.     0.25, 0, 0.25, 0,        /* margins */
  49.     3, lj250_print_page);
  50. gx_device_printer gs_paintjet_device =
  51.   prn_device(paintjet_procs, "paintjet",
  52.     85,                /* width_10ths, 8.5" */
  53.     110,                /* height_10ths, 11" */
  54.     X_DPI, Y_DPI,
  55.     0.25, 0, 0.25, 0,        /* margins */
  56.     3, paintjet_print_page);
  57. private gx_device_procs pjetxl_procs =
  58.   prn_color_matrix_procs(gdev_prn_open, gdev_pcl_get_initial_matrix,
  59.     gdev_prn_output_page, gdev_prn_close,
  60.     gdev_pcl_3bit_map_rgb_color, gdev_pcl_3bit_map_color_rgb);
  61. gx_device_printer gs_pjetxl_device =
  62.   prn_device(pjetxl_procs, "pjetxl",
  63.     85,                /* width_10ths, 8.5" */
  64.     110,                /* height_10ths, 11" */
  65.     X_DPI, Y_DPI,
  66.     0.25, 0, 0, 0,            /* margins */
  67.     3, pjetxl_print_page);
  68.  
  69. /* Forward references */
  70. private int compress1_row(P3(const byte *, const byte *, byte *));
  71.  
  72. /* ------ Internal routines ------ */
  73.  
  74. /* Send a page to the LJ250.  We need to enter and exit */
  75. /* the PaintJet emulation mode. */
  76. private int
  77. lj250_print_page(gx_device_printer *pdev, FILE *prn_stream)
  78. {    fputs("\033%8", prn_stream);    /* Enter PCL emulation mode */
  79.     /* ends raster graphics to set raster graphics resolution */
  80.     fputs("\033*rB", prn_stream);
  81.     /* Exit PCL emulation mode after printing */
  82.     return pj_common_print_page(pdev, prn_stream, 0, "\033*r0B\014\033%@");
  83. }
  84.  
  85. /* Send a page to the PaintJet. */
  86. private int
  87. paintjet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  88. {    /* ends raster graphics to set raster graphics resolution */
  89.     fputs("\033*rB", prn_stream);
  90.     return pj_common_print_page(pdev, prn_stream, 0, "\033*r0B\014");
  91. }
  92.  
  93. /* Send a page to the PaintJet XL. */
  94. private int
  95. pjetxl_print_page(gx_device_printer *pdev, FILE *prn_stream)
  96. {    /* Initialize PaintJet XL for printing */
  97.     fputs("\033E", prn_stream);
  98.     /* The XL has a different vertical origin, who knows why?? */
  99.     return pj_common_print_page(pdev, prn_stream, -360, "\033*rC");
  100. }
  101.  
  102. /* Send the page to the printer.  Compress each scan line. */
  103. private int
  104. pj_common_print_page(gx_device_printer *pdev, FILE *prn_stream, int y_origin,
  105.   const char *end_page)
  106. {
  107. #define DATA_SIZE (LINE_SIZE * 8)
  108.     byte *data =
  109.         (byte *)gs_malloc(DATA_SIZE, 1,
  110.                   "paintjet_print_page(data)");
  111.     byte *plane_data =
  112.         (byte *)gs_malloc(LINE_SIZE * 3, 1,
  113.                   "paintjet_print_page(plane_data)");
  114.     if ( data == 0 || plane_data == 0 )
  115.     {    if ( data )
  116.             gs_free((char *)data, DATA_SIZE, 1,
  117.                 "paintjet_print_page(data)");
  118.         if ( plane_data )
  119.             gs_free((char *)plane_data, LINE_SIZE * 3, 1,
  120.                 "paintjet_print_page(plane_data)");
  121.         return_error(gs_error_VMerror);
  122.     }
  123.  
  124.     /* set raster graphics resolution -- 90 or 180 dpi */
  125.     fprintf(prn_stream, "\033*t%dR", X_DPI);
  126.  
  127.     /* set the line width */
  128.     fprintf(prn_stream, "\033*r%dS", DATA_SIZE);
  129.  
  130.     /* set the number of color planes */
  131.     fprintf(prn_stream, "\033*r%dU", 3);        /* always 3 */
  132.  
  133.     /* move to top left of page */
  134.     fprintf(prn_stream, "\033&a0H\033&a%dV", y_origin);
  135.  
  136.     /* select data compression */
  137.     fputs("\033*b1M", prn_stream);
  138.  
  139.     /* start raster graphics */
  140.     fputs("\033*r1A", prn_stream);
  141.  
  142.     /* Send each scan line in turn */
  143.        {    int lnum;
  144.         int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  145.         int num_blank_lines = 0;
  146.         for ( lnum = 0; lnum < pdev->height; lnum++ )
  147.            {    byte *end_data = data + line_size;
  148.             gdev_prn_copy_scan_lines(pdev, lnum,
  149.                          (byte *)data, line_size);
  150.             /* Remove trailing 0s. */
  151.             while ( end_data > data && end_data[-1] == 0 )
  152.                 end_data--;
  153.             if ( end_data == data )
  154.                {    /* Blank line */
  155.                 num_blank_lines++;
  156.                }
  157.             else
  158.                {    int i;
  159.                 byte *odp;
  160.                 byte *row;
  161.  
  162.                 /* Pad with 0s to fill out the last */
  163.                 /* block of 8 bytes. */
  164.                 memset(end_data, 0, 7);
  165.  
  166.                 /* Transpose the data to get pixel planes. */
  167.                 for ( i = 0, odp = plane_data; i < DATA_SIZE;
  168.                       i += 8, odp++
  169.                     )
  170.                  { /* The following is for 16-bit machines */
  171. #define spread3(c)\
  172.  { 0, c, c*0x100, c*0x101, c*0x10000L, c*0x10001L, c*0x10100L, c*0x10101L }
  173.                    static ulong spr40[8] = spread3(0x40);
  174.                    static ulong spr8[8] = spread3(8);
  175.                    static ulong spr2[8] = spread3(2);
  176.                    register byte *dp = data + i;
  177.                    register ulong pword =
  178.                      (spr40[dp[0]] << 1) +
  179.                      (spr40[dp[1]]) +
  180.                      (spr40[dp[2]] >> 1) +
  181.                      (spr8[dp[3]] << 1) +
  182.                      (spr8[dp[4]]) +
  183.                      (spr8[dp[5]] >> 1) +
  184.                      (spr2[dp[6]]) +
  185.                      (spr2[dp[7]] >> 1);
  186.                    odp[0] = (byte)(pword >> 16);
  187.                    odp[LINE_SIZE] = (byte)(pword >> 8);
  188.                    odp[LINE_SIZE*2] = (byte)(pword);
  189.                  }
  190.                 /* Skip blank lines if any */
  191.                 if ( num_blank_lines > 0 )
  192.                    {    /* move down from current position */
  193.                     fprintf(prn_stream, "\033&a+%dV",
  194.                         num_blank_lines * (720 / Y_DPI));
  195.                     num_blank_lines = 0;
  196.                    }
  197.  
  198.                 /* Transfer raster graphics */
  199.                 /* in the order R, G, B. */
  200.                 for ( row = plane_data + LINE_SIZE * 2, i = 0;
  201.                       i < 3; row -= LINE_SIZE, i++
  202.                     )
  203.                    {    byte temp[LINE_SIZE * 2];
  204.                     int count = compress1_row(row, row + LINE_SIZE, temp);
  205.                     fprintf(prn_stream, "\033*b%d%c",
  206.                         count, "VVW"[i]);
  207.                     fwrite(temp, sizeof(byte),
  208.                            count, prn_stream);
  209.                    }
  210.                }
  211.            }
  212.        }
  213.  
  214.     /* end the page */
  215.     fputs(end_page, prn_stream);
  216.  
  217.     gs_free((char *)data, DATA_SIZE, 1, "paintjet_print_page(data)");
  218.     gs_free((char *)plane_data, LINE_SIZE * 3, 1, "paintjet_print_page(plane_data)");
  219.  
  220.     return 0;
  221. }
  222.  
  223. /*
  224.  * Row compression for the H-P PaintJet.
  225.  * Compresses data from row up to end_row, storing the result
  226.  * starting at compressed.  Returns the number of bytes stored.
  227.  * The compressed format consists of a byte N followed by a
  228.  * data byte that is to be repeated N+1 times.
  229.  * In the worst case, the `compressed' representation is
  230.  * twice as large as the input.
  231.  * We complement the bytes at the same time, because
  232.  * we accumulated the image in complemented form.
  233.  */
  234. private int
  235. compress1_row(const byte *row, const byte *end_row,
  236.   byte *compressed)
  237. {    register const byte *in = row;
  238.     register byte *out = compressed;
  239.     while ( in < end_row )
  240.        {    byte test = *in++;
  241.         const byte *run = in;
  242.         while ( in < end_row && *in == test ) in++;
  243.         /* Note that in - run + 1 is the repetition count. */
  244.         while ( in - run > 255 )
  245.            {    *out++ = 255;
  246.             *out++ = ~test;
  247.             run += 256;
  248.            }
  249.         *out++ = in - run;
  250.         *out++ = ~test;
  251.        }
  252.     return out - compressed;
  253. }
  254.