home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVNP6.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  8KB  |  258 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevnp6.c */
  20. /* NEC dot-matrix printer driver for Ghostscript: */
  21. /* tested on P6, should also work on P6+ and similar models. */
  22. #include "gdevprn.h"
  23.  
  24. /* Thanks to Andreas Schwab (schwab@ls5.informatik.uni-dortmund.de) */
  25. /* for improvements to this code. */
  26.  
  27. /* The device descriptors */
  28. private dev_proc_print_page (necp6_print_page);
  29. gx_device_printer gs_necp6_device
  30.   = prn_device (prn_std_procs, "necp6",
  31.         DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  32.         360, 360,
  33.         0, 0, 0.5, 0,    /* margins */
  34.         1, necp6_print_page);
  35.  
  36. /* ------ Internal routines ------ */
  37.  
  38. /* Forward references */
  39. private void necp6_output_run (P4 (byte *, int, int, FILE *));
  40. private void necp6_improve_bitmap (P2 (byte *, int));
  41.  
  42. /* Send the page to the printer. */
  43. private int
  44. necp6_print_page (gx_device_printer *pdev, FILE *prn_stream)
  45. {
  46.   int xres = pdev->x_pixels_per_inch;
  47.   int yres = pdev->y_pixels_per_inch;
  48.   int x_high = (xres == 360);
  49.   int y_high = (yres == 360);
  50.   int bits_per_column = (y_high ? 48 : 24);
  51.   uint line_size = gdev_prn_raster (pdev);
  52.   uint in_size = line_size * bits_per_column;
  53.   byte *in = (byte *) gs_malloc (in_size, 1, "necp6_print_page (in)");
  54.   uint out_size = ((pdev->width + 7) & -8) * 3;
  55.   byte *out = (byte *) gs_malloc (out_size, 1, "necp6_print_page (out)");
  56.   int y_passes = (y_high ? 2 : 1);
  57.   int dots_per_space = xres / 10;    /* pica space = 1/10" */
  58.   int bytes_per_space = dots_per_space * 3;
  59.   int skip = 0, lnum = 0, ypass;
  60.  
  61.   /* Check allocations */
  62.   if (in == 0 || out == 0)
  63.     {
  64.       if (out)
  65.     gs_free ((char *) out, out_size, 1, "necp6_print_page (out)");
  66.       if (in)
  67.     gs_free ((char *) in, in_size, 1, "necp6_print_page (in)");
  68.       return_error (gs_error_VMerror);
  69.     }
  70.  
  71.   /* Initialize the printer and reset the margins. */
  72. #define init_string "\033@\033P\033l\000\r\034\063\001\033Q"
  73.   fwrite (init_string, sizeof (init_string) - 1, sizeof (char), prn_stream);
  74.   fputc ((int) (pdev->width / pdev->x_pixels_per_inch * 10) + 2,
  75.      prn_stream);
  76.  
  77.   /* Print lines of graphics */
  78.   while (lnum < pdev->height)
  79.     {
  80.       byte *inp;
  81.       byte *in_end;
  82.       byte *out_end;
  83.       byte *out_blk;
  84.       register byte *outp;
  85.       int lcnt;
  86.  
  87.       /* Copy 1 scan line and test for all zero. */
  88.       gdev_prn_copy_scan_lines (pdev, lnum, in, line_size);
  89.       if (in[0] == 0
  90.       && !memcmp ((char *) in, (char *) in + 1, line_size - 1))
  91.     {
  92.       lnum++;
  93.       skip += 2 - y_high;
  94.       continue;
  95.     }
  96.  
  97.       /* Vertical tab to the appropriate position. */
  98.       while ((skip >> 1) > 255)
  99.     {
  100.       fputs ("\033J\377", prn_stream);
  101.       skip -= 255 * 2;
  102.     }
  103.  
  104.       if (skip)
  105.     {
  106.       if (skip >> 1)
  107.         fprintf (prn_stream, "\033J%c", skip >> 1);
  108.       if (skip & 1)
  109.         fputc ('\n', prn_stream);
  110.     }
  111.  
  112.       /* Copy the rest of the scan lines. */
  113.       if (y_high)
  114.     {
  115.       inp = in + line_size;
  116.       for (lcnt = 1; lcnt < 24; lcnt++, inp += line_size)
  117.         if (!gdev_prn_copy_scan_lines (pdev, lnum + lcnt * 2, inp,
  118.                        line_size))
  119.           {
  120.         memset (inp, 0, (24 - lcnt) * line_size);
  121.         break;
  122.           }
  123.       inp = in + line_size * 24;
  124.       for (lcnt = 0; lcnt < 24; lcnt++, inp += line_size)
  125.         if (!gdev_prn_copy_scan_lines (pdev, lnum + lcnt * 2 + 1, inp,
  126.                        line_size))
  127.           {
  128.         memset (inp, 0, (24 - lcnt) * line_size);
  129.         break;
  130.           }
  131.     }
  132.       else
  133.     {
  134.       lcnt = 1 + gdev_prn_copy_scan_lines (pdev, lnum + 1, in + line_size,
  135.                            in_size - line_size);
  136.       if (lcnt < 24)
  137.         /* Pad with lines of zeros. */
  138.         memset (in + lcnt * line_size, 0, in_size - lcnt * line_size);
  139.     }
  140.  
  141.       for (ypass = 0; ypass < y_passes; ypass++)
  142.     {
  143.       out_end = out;
  144.       inp = in;
  145.       if (ypass)
  146.         inp += line_size * 24;
  147.       in_end = inp + line_size;
  148.  
  149.       for (; inp < in_end; inp++, out_end += 24)
  150.         {
  151.           memflip8x8 (inp, line_size, out_end, 3);
  152.           memflip8x8 (inp + line_size * 8, line_size, out_end + 1, 3);
  153.           memflip8x8 (inp + line_size * 16, line_size, out_end + 2, 3);
  154.         }
  155.       /* Remove trailing 0s. */
  156.       while (out_end - 3 >= out && out_end[-1] == 0
  157.          && out_end[-2] == 0 && out_end[-3] == 0)
  158.         out_end -= 3;
  159.  
  160.       for (out_blk = outp = out; outp < out_end;)
  161.         {
  162.           /* Skip a run of leading 0s. */
  163.           /* At least 10 are needed to make tabbing worth it. */
  164.  
  165.           if (outp[0] == 0 && outp + 12 <= out_end
  166.           && outp[1] == 0 && outp[2] == 0
  167.           && outp[3] == 0 && outp[4] == 0 && outp[5] == 0
  168.           && outp[6] == 0 && outp[7] == 0 && outp[8] == 0
  169.           && outp[9] == 0 && outp[10] == 0 && outp[11] == 0)
  170.         {
  171.           byte *zp = outp;
  172.           int tpos;
  173.           byte *newp;
  174.           outp += 12;
  175.           while (outp + 3 <= out_end
  176.              && outp[0] == 0 && outp[1] == 0 && outp[2] == 0)
  177.             outp += 3;
  178.           tpos = (outp - out) / bytes_per_space;
  179.           newp = out + tpos * bytes_per_space;
  180.           if (newp > zp + 10)
  181.             {
  182.               /* Output preceding bit data. */
  183.               /* only false at beginning of line */
  184.               if (zp > out_blk)
  185.             {
  186.               if (x_high)
  187.                 necp6_improve_bitmap (out_blk, (int) (zp - out_blk));
  188.               necp6_output_run (out_blk, (int) (zp - out_blk),
  189.                       x_high, prn_stream);
  190.             }
  191.               /* Tab over to the appropriate position. */
  192.               fprintf (prn_stream, "\033D%c%c\t", tpos, 0);
  193.               out_blk = outp = newp;
  194.             }
  195.         }
  196.           else
  197.         outp += 3;
  198.         }
  199.       if (outp > out_blk)
  200.         {
  201.           if (x_high)
  202.         necp6_improve_bitmap (out_blk, (int) (outp - out_blk));
  203.           necp6_output_run (out_blk, (int) (outp - out_blk), x_high,
  204.                   prn_stream);
  205.         }
  206.  
  207.       fputc ('\r', prn_stream);
  208.       if (ypass < y_passes - 1)
  209.         fputc ('\n', prn_stream);
  210.     }
  211.       skip = 48 - y_high;
  212.       lnum += bits_per_column;
  213.     }
  214.  
  215.   /* Eject the page and reinitialize the printer */
  216.   fputs ("\f\033@", prn_stream);
  217.   fflush (prn_stream);
  218.  
  219.   gs_free ((char *) out, out_size, 1, "necp6_print_page (out)");
  220.   gs_free ((char *) in, in_size, 1, "necp6_print_page (in)");
  221.  
  222.   return 0;
  223. }
  224.  
  225. /* Output a single graphics command. */
  226. private void
  227. necp6_output_run (byte *data, int count, int x_high, FILE *prn_stream)
  228. {
  229.   int xcount = count / 3;
  230.   fputc (033, prn_stream);
  231.   fputc ('*', prn_stream);
  232.   fputc ((x_high ? 40 : 39), prn_stream);
  233.   fputc (xcount & 0xff, prn_stream);
  234.   fputc (xcount >> 8, prn_stream);
  235.   fwrite (data, 1, count, prn_stream);
  236. }
  237.  
  238. /* If xdpi == 360, the NEC P6 cannot print adjacent pixels.  Clear the
  239.    second last pixel of every run of set pixels, so that the last pixel
  240.    is always printed.  */
  241. private void
  242. necp6_improve_bitmap (byte *data, int count)
  243. {
  244.   int i;
  245.   register byte *p = data + 6;
  246.  
  247.       for (i = 6; i < count; i += 3, p += 3)
  248.     {
  249.       p[-6] &= ~(~p[0] & p[-3]);
  250.       p[-5] &= ~(~p[1] & p[-2]);
  251.       p[-4] &= ~(~p[2] & p[-1]);
  252.     }
  253.       p[-6] &= ~p[-3];
  254.       p[-5] &= ~p[-2];
  255.       p[-4] &= ~p[-1];
  256.  
  257. }
  258.