home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / os2 / hpgl312.zip / TO_PCL.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  7KB  |  348 lines

  1. /*
  2.    Copyright (c) 1991 - 1993 Heinz W. Werntges.  All rights reserved.
  3.    Distributed by Free Software Foundation, Inc.
  4.  
  5. This file is part of HP2xx.
  6.  
  7. HP2xx is distributed in the hope that it will be useful, but
  8. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  9. to anyone for the consequences of using it or for whether it serves any
  10. particular purpose or works at all, unless he says so in writing.  Refer
  11. to the GNU General Public License, Version 2 or later, for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. HP2xx, but only under the conditions described in the GNU General Public
  15. License.  A copy of this license is supposed to have been
  16. given to you along with HP2xx so you can know your rights and
  17. responsibilities.  It should be in a file named COPYING.  Among other
  18. things, the copyright notice and this notice must be preserved on all
  19. copies.
  20.  
  21. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  22. */
  23.  
  24. /** to_pcl.c: PCL converter part of project "hp2xx"
  25.  **
  26.  ** 91/01/19  V 1.00  HWW  Reorganized
  27.  ** 91/01/29  V 1.01  HWW  Tested on SUN
  28.  ** 91/02/01  V 1.02  HWW  Deskjet specials acknowledged
  29.  ** 91/02/15  V 1.03  HWW  VAX_C support added
  30.  ** 91/02/20  V 1.04b HWW  x & y positioning: Now absolute!
  31.  **              Some VAX_C changes
  32.  ** 91/06/09  V 1.05  HWW  New options added
  33.  ** 91/10/15  V 1.06  HWW  ANSI_C
  34.  ** 91/10/25  V 1.07  HWW  VAX: fopen() augmentations used, open() removed
  35.  ** 92/05/17  V 1.07b HWW  Output to stdout if outfile == '-'
  36.  ** 92/05/19  V 1.07c HWW  Abort if color mode
  37.  ** 92/12/23  V 1.08a HWW  Color for Deskjet (beginning)
  38.  ** 93/04/02  V 1.08b HWW  DotBlock --> Byte
  39.  ** 93/04/13  V 1.09a HWW  CMYK supported
  40.  **/
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include "bresnham.h"
  45. #include "hp2xx.h"
  46.  
  47.  
  48. #define    PCL_FIRST     1            /* Bit mask!     */
  49. #define    PCL_LAST     2            /* Bit mask!    */
  50.  
  51.  
  52.  
  53. static    Byte    *p_K, *p_C, *p_M, *p_Y;        /* Buffer ptrs (CMYK bits) */
  54.  
  55.  
  56.  
  57. void    Buf_to_PCL (Byte *buf, int nb, int mode, FILE *fd)
  58. /**
  59.  ** Output the raw bit stream
  60.  **   (This should be an ideal place for data compression)
  61.  **/
  62. {
  63.   if (mode & PCL_FIRST)
  64.     fprintf(fd,"\033*b");
  65.  
  66.   if (mode & PCL_LAST)
  67.     fprintf(fd,"%dW", nb);
  68.   else
  69.     fprintf(fd,"%1dv", nb);
  70.  
  71. /*  Following change keeps the VAX people happy:    */
  72. /*fwrite (buf, 1, nb, fd);        */
  73.   fwrite (buf, nb, 1, fd);
  74. }
  75.  
  76.  
  77.  
  78.  
  79. void    KCMY_Buf_to_PCL (int nb, int is_KCMY, FILE *fd)
  80. {
  81.   if (is_KCMY)
  82.   {
  83.     Buf_to_PCL (p_K, nb, PCL_FIRST, fd);
  84.     Buf_to_PCL (p_C, nb, 0,         fd);
  85.   }
  86.   else            /* is only CMY:    */
  87.     Buf_to_PCL (p_C, nb, PCL_FIRST, fd);
  88.  
  89.   Buf_to_PCL (p_M, nb, 0,        fd);
  90.   Buf_to_PCL (p_Y, nb, PCL_LAST, fd);
  91. }
  92.  
  93.  
  94.  
  95.  
  96. void    KCMY_to_K (int nb)
  97. /**
  98.  ** Color -> B/W conversion:
  99.  ** Any set bit will show up black
  100.  **/
  101. {
  102. int    i;
  103. Byte    *pK = p_K, *pC = p_C, *pM = p_M, *pY = p_Y;
  104.  
  105.   for (i=0; i < nb; i++)
  106.     *pK++ |= ((*pC++ | *pM++) | *pY++);
  107. }
  108.  
  109.  
  110.  
  111.  
  112. void    K_to_CMY (int nb)
  113. /**
  114.  ** CMYK-to-CMY conversion:
  115.  ** Any set bit in the "black" layer sets all C,M,Y bits to emulate "black"
  116.  **/
  117. {
  118. int    i;
  119. Byte    *pK = p_K, *pC = p_C, *pM = p_M, *pY = p_Y;
  120.  
  121.   for (i=0; i < nb; i++, pK++)
  122.   {
  123.     *pC++ |= *pK;
  124.     *pM++ |= *pK;
  125.     *pY++ |= *pK;
  126.   }
  127. }
  128.  
  129.  
  130.  
  131.  
  132. void    init_printer (FILE *fd)
  133. {
  134.   fputc(ESC,fd); fputc('E',fd);    /* Esc-E */
  135. }
  136.  
  137.  
  138.  
  139.  
  140. void    start_graphmode (PAR *p, int width, FILE *fd)
  141. {
  142. /**
  143.  ** X & Y offsets: Use "decipoints" as unit to stick to PCL level 3
  144.  **        1 dpt = 0.1 pt = 1/720 in
  145.  **/
  146.   if (p->yoff != 0.0)
  147.     fprintf(fd,"\033&a%dV",(int)(p->yoff * 720.0 / 25.4) );
  148.   if (p->xoff != 0.0)
  149.     fprintf(fd,"\033&a%dH",(int)(p->xoff * 720.0 / 25.4) );
  150.  
  151. /**
  152.  ** Set Graphics Resolution (300 / 150 / 100 / 75):
  153.  ** This is NO PCL level 3 feature, but LaserjetII and compatibles
  154.  ** seem to accept it.
  155.  **/
  156.   fprintf(fd,"\033*t%dR", p->dpi_x);
  157.  
  158. /**
  159.  ** Set Raster Width (in dots)
  160.  **    Deskjet feature, good for saving internal memory!
  161.  **/
  162.   if (p->specials)
  163.   {
  164.     fprintf(fd,"\033*r%dS", width);
  165.     switch (p->specials)
  166.     {
  167.       case 4:    /* KCMY             */
  168.         fprintf(fd,"\033*r-4U");
  169.         break;
  170.       case 3:    /* CMY                */
  171.         fprintf(fd,"\033*r-3U");
  172.         break;
  173.       default:    /* Single color plane        */
  174.         fprintf(fd,"\033*r1U");
  175.         break;
  176.     }
  177.   }
  178.  
  179. /**
  180.  ** Start Raster Graphics at current position
  181.  ** This is NO PCL level 3 feature, but LaserjetII and compatibles
  182.  ** seem to accept it.
  183.  **/
  184.   fprintf(fd,"\033*r1A");
  185. }
  186.  
  187.  
  188.  
  189.  
  190. void    end_graphmode (FILE *fd)
  191. {
  192. /**
  193.  ** End Raster Graphics
  194.  **/
  195.   fprintf(fd,"\033*rB");
  196. }
  197.  
  198.  
  199.  
  200.  
  201.  
  202. void    PicBuf_to_PCL (PicBuf *picbuf, PAR *p)
  203. /**
  204.  ** Main interface routine
  205.  **/
  206. {
  207. FILE    *fd = stdout;
  208. RowBuf    *row;
  209. int    row_c, i, x, color_index, offset;
  210. Byte    mask;
  211.  
  212.   if (!p->quiet)
  213.     fprintf(stderr, "\nWriting PCL output\n");
  214.  
  215.   if (picbuf->depth > 1 && p->specials < 3)
  216.     fprintf(stderr,
  217.         "\nWARNING: Monochrome output despite active colors selected!\n");
  218.  
  219.   if (picbuf->depth > 1)    /* Allocate buffers for CMYK conversion    */
  220.   {
  221.     p_K = calloc (picbuf->nb, sizeof(Byte));
  222.     p_C = calloc (picbuf->nb, sizeof(Byte));
  223.     p_M = calloc (picbuf->nb, sizeof(Byte));
  224.     p_Y = calloc (picbuf->nb, sizeof(Byte));
  225.     if (p_K == NULL || p_C == NULL || p_M == NULL || p_Y == NULL)
  226.     {
  227.         fprintf(stderr,
  228.             "\nCannot 'calloc' CMYK memory -- sorry, use B/W!\n");
  229.         goto PCL_exit;
  230.     }
  231.   }
  232.  
  233.   if (*p->outfile != '-')
  234.   {
  235. #ifdef VAX
  236.     if ((fd = fopen(p->outfile, WRITE_BIN, "rfm=var","mrs=512")) == NULL)
  237.     {
  238. #else
  239.     if ((fd = fopen(p->outfile, WRITE_BIN)) == NULL)
  240.     {
  241. #endif
  242.         perror ("hp2xx -- opening output file");
  243.         free_PicBuf (picbuf, p->swapfile);
  244.         exit (ERROR);
  245.     }
  246.   }
  247.  
  248.   if (p->init_p)
  249.     init_printer (fd);
  250.  
  251.   start_graphmode (p, picbuf->nc, fd);
  252.  
  253.   /**
  254.    ** Loop for all rows:
  255.    ** Counting back since highest index is lowest line on paper...
  256.    **/
  257.  
  258.   for (row_c = picbuf->nr - 1; row_c >= 0; row_c--)
  259.   {
  260.     if ((!p->quiet) && (row_c % 10 == 0))
  261.           /* For the impatients among us ...    */
  262.         putc('.',stderr);
  263.  
  264.     row = get_RowBuf(picbuf, row_c);
  265.  
  266.     if (picbuf->depth == 1)
  267.         Buf_to_PCL (row->buf, picbuf->nb, PCL_FIRST | PCL_LAST, fd);
  268.     else
  269.     {
  270.         for (x=0; x < picbuf->nb; x++)
  271.             p_K[x] = p_C[x] = p_M[x] = p_Y[x] = 0;
  272.  
  273.         for (x=offset=0; x < (picbuf->nb << 3); x++, offset = (x >> 3))
  274.         {
  275.             color_index = index_from_RowBuf(row, x, picbuf);
  276.  
  277.             if (color_index == xxBackground)
  278.                 continue;
  279.             else
  280.             {
  281.                 mask = 0x80;
  282.                 if ((i = x & 0x07) != 0)
  283.                     mask >>= i;
  284.                 switch (color_index)
  285.                 {
  286.                   case xxForeground:
  287.                     *(p_K + offset) |= mask;
  288.                     break;
  289.                   case xxRed:
  290.                     *(p_M + offset) |= mask;
  291.                     *(p_Y + offset) |= mask;
  292.                     break;
  293.                   case xxGreen:
  294.                     *(p_C + offset) |= mask;
  295.                     *(p_Y + offset) |= mask;
  296.                     break;
  297.                   case xxBlue:
  298.                     *(p_C + offset) |= mask;
  299.                     *(p_M + offset) |= mask;
  300.                     break;
  301.                   case xxCyan:
  302.                     *(p_C + offset) |= mask;
  303.                     break;
  304.                   case xxMagenta:
  305.                     *(p_M + offset) |= mask;
  306.                     break;
  307.                   case xxYellow:
  308.                     *(p_Y + offset) |= mask;
  309.                     break;
  310.                   default:
  311.                     break;
  312.                 }
  313.             }
  314.         }
  315.  
  316.         switch (p->specials)
  317.         {
  318.           case 3:
  319.             K_to_CMY (picbuf->nb);
  320.             /* drop thru    */
  321.           case 4:
  322.             KCMY_Buf_to_PCL (picbuf->nb, (p->specials == 4), fd);
  323.             break;
  324.           default:
  325.             KCMY_to_K (picbuf->nb);
  326.             Buf_to_PCL (p_K, picbuf->nb, PCL_FIRST | PCL_LAST, fd);
  327.             break;
  328.         }
  329.     }
  330.   }
  331.  
  332.   end_graphmode (fd);
  333.   if (p->formfeed)
  334.     putc (FF, fd);
  335.   if (!p->quiet)
  336.     fputc ('\n', stderr);
  337.   if (fd != stdout)
  338.     fclose (fd);
  339.  
  340. PCL_exit:
  341.   if (p_Y != NULL)    free(p_Y);
  342.   if (p_M != NULL)    free(p_M);
  343.   if (p_C != NULL)    free(p_C);
  344.   if (p_K != NULL)    free(p_K);
  345.  
  346.   p_K = p_C = p_M = p_Y = NULL;
  347. }
  348.