home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / PRINTER.C < prev    next >
C/C++ Source or Header  |  1991-07-07  |  32KB  |  1,014 lines

  1. /*  Printer.c
  2.  *    This module is linked as an overlay, use ENTER_OVLY and EXIT_OVLY.
  3.  *    Simple screen printing functions for FRACTINT
  4.  *    By Matt Saucier CIS: [72371,3101]      7/2/89
  5.  *    "True-to-the-spirit" of FRACTINT, this code makes few checks that you
  6.  *    have specified a valid resolution for the printer (just in case yours
  7.  *    has more dots/line than the Standard HP and IBM/EPSON,
  8.  *    (eg, Wide Carriage, etc.))
  9.  *
  10.  *    PostScript support by Scott Taylor [72401,410] / (DGWM18A)   10/8/90
  11.  *    For PostScript, use 'printer=PostScript/resolution' where resolution
  12.  *    is ANY NUMBER between 10 and 600. Common values: 300,150,100,75.
  13.  *    Default resolution for PostScript is 150 pixels/inch.
  14.  *    At 200 DPI, a fractal that is 640x480 prints as a 3.2"x2.4" picture.
  15.  *    PostScript printer names:
  16.  *
  17.  *    PostScript/PS            = Portrait printing
  18.  *    PostScriptH/PostScriptL/PSH/PSL = Landscape printing
  19.  *
  20.  *    This code supports printers attached to a LPTx (1-3) parallel port.
  21.  *    It also now supports serial printers AFTER THEY ARE CONFIGURED AND
  22.  *    WORKING WITH THE DOS MODE COMMAND, eg. MODE COM1:9600,n,8,1 (for HP)
  23.  *    (NOW you can also configure the serial port with the comport= command)
  24.  *    Printing calls are made directly to the BIOS for DOS can't handle fast
  25.  *    transfer of data to the HP.  (Or maybe visa-versa, HP can't handle the
  26.  *    slow transfer of data from DOS)
  27.  *
  28.  *    I just added direct port access for COM1 and COM2 **ONLY**. This method
  29.  *    does a little more testing than BIOS, and may work (especially on
  30.  *    serial printer sharing devices) where the old method doesn't. I noticed
  31.  *    maybe a 5% speed increase at 9600 baud. These are selected in the
  32.  *    printer=.../.../31 for COM1 or 32 for COM2.
  33.  *
  34.  *    I also added direct parallel port access for LPT1 and LPT2 **ONLY**.
  35.  *    This toggles the "INIT" line of the parallel port to reset the printer
  36.  *    for each print session. It will also WAIT for a error / out of paper /
  37.  *    not selected condition instead of quitting with an error.
  38.  *
  39.  *    Supported Printers:    Tested Ok:
  40.  *     HP LaserJet
  41.  *        LJ+,LJII         MDS
  42.  *     Toshiba PageLaser     MDS (Set FRACTINT to use HP)
  43.  *     IBM Graphics         MDS
  44.  *     EPSON
  45.  *        Models?         Untested.
  46.  *     IBM LaserPrinter
  47.  *        with PostScript     SWT
  48.  *     HP Plotter         SWT
  49.  *
  50.  *    Future support to include OKI 20 (color) printer, and just about
  51.  *    any printer you request.
  52.  *
  53.  *    Future modifications to include a more flexible, standard interface
  54.  *    with the surrounding program, for easier portability to other
  55.  *    programs.
  56.  *
  57.  * PostScript Styles:
  58.  *  0  Dot
  59.  *  1  Dot*           [Smoother]
  60.  *  2  Inverted Dot
  61.  *  3  Ring
  62.  *  4  Inverted Ring
  63.  *  5  Triangle        [45-45-90]
  64.  *  6  Triangle*       [30-75-75]
  65.  *  7  Grid
  66.  *  8  Diamond
  67.  *  9  Line
  68.  * 10  Microwaves
  69.  * 11  Ellipse
  70.  * 12  RoundBox
  71.  * 13  Custom
  72.  * 14  Star
  73.  * 15  Random
  74.  * 16  Line*           [Not much different]
  75.  *
  76.  *  *  Alternate style
  77.  */
  78.  
  79. #include <stdlib.h>
  80. #include <bios.h>
  81. #include <dos.h>
  82. #include <io.h>
  83. #include <fcntl.h>
  84. #include <sys\types.h>
  85. #include <errno.h>
  86. #include <stdio.h>    /*** for vsprintf prototype ***/
  87. #include <conio.h>
  88. #include <string.h>
  89. #include <stdarg.h>
  90. #include <float.h>    /* for pow() */
  91. #include <math.h>    /*  "    "   */
  92. #include "fractint.h"
  93. #include "fractype.h"
  94.  
  95. /********      PROTOTYPES     ********/
  96.  
  97.        void printer_overlay(void);
  98.        void Print_Screen(void);
  99. static void Printer_printf(char *fmt,...);
  100. static int  _fastcall printer(int c);
  101. static void _fastcall print_title(int,int,char *);
  102. static void printer_reset();
  103.  
  104. extern int keypressed (void);
  105. extern void updatesavename (char *);
  106. extern void showtrig (char *);
  107.  
  108. /********  EXTRN GLOBAL VARS  ********/
  109.  
  110. extern int xdots,ydots,            /* size of screen           */
  111.        extraseg,               /* used for buffering           */
  112.        fractype;               /* used for title block           */
  113. extern unsigned char dacbox[256][3];   /* for PostScript printing       */
  114. extern unsigned char dstack[2][3][400];
  115. extern char FormName[];            /* for Title block info           */
  116. extern char LName[];               /* for Title block info           */
  117. extern char IFSName[];               /* for Title block info           */
  118. extern char PrintName[];           /* Filename for print-to-file       */
  119. extern float finalaspectratio;
  120. extern double xxmin,xxmax,xx3rd,
  121.           yymin,yymax,yy3rd,param[4]; /* for Title block info       */
  122. extern int colors;
  123. extern int dotmode;
  124. extern unsigned int debugflag;
  125.  
  126. extern unsigned int  far pj_patterns[];
  127. extern unsigned char far pj_reds[];
  128. extern unsigned char far pj_blues[];
  129. extern unsigned char far pj_greens[];
  130.  
  131. extern int thinking(int,char *);
  132.  
  133. /********    GLOBALS       ********/
  134.  
  135. int Printer_Resolution,        /* 75,100,150,300 for HP;           */
  136.                    /* 60,120,240 for IBM;               */
  137.                    /* 90 or 180 for the PaintJet;           */
  138.                    /* 10-600 for PS                */
  139.                    /* 1-20 for Plotter               */
  140.     LPTNumber,               /* ==1,2,3 LPTx; or 11,12,13,14 for COM1-4  */
  141.                    /* 21,22 for direct port access for LPT1-2  */
  142.                    /* 31,32 for direct port access for COM1-2  */
  143.     Printer_Type,               /* ==1 HP,
  144.                       ==2 IBM/EPSON,
  145.                       ==3 Epson color,
  146.                       ==4 HP PaintJet,
  147.                       ==5,6 PostScript,
  148.                       ==7 HP Plotter           */
  149.     Printer_Titleblock,       /* Print info about the fractal?           */
  150.     Printer_ColorXlat,          /* PostScript only - invert colors       */
  151.     Printer_SetScreen,          /* PostScript only - reprogram halftone ?    */
  152.     Printer_SFrequency,       /* PostScript only - Halftone Frequency       */
  153.     Printer_SAngle,          /* PostScript only - Halftone angle       */
  154.     Printer_SStyle,          /* PostScript only - Halftone style       */
  155.     Print_To_File,          /* Print to file toggle               */
  156.     EPSFileType,          /* EPSFileType -
  157.                            1 = well-behaved,
  158.                            2 = much less behaved,
  159.                            3 = not well behaved       */
  160.     Printer_CRLF;          /* (0) CRLF (1) CR (2) LF            */
  161.  
  162. static int LPTn;           /* printer number we're gonna use */
  163.  
  164. static FILE *PRFILE;
  165.  
  166. #define TONES 17           /* Number of PostScript halftone styles */
  167.  
  168. static char *HalfTone[TONES]=  {
  169.              "dup mul exch dup mul add 1 exch sub",
  170.              "abs exch abs 2 copy add 1 gt {1 sub dup mul exch 1 sub dup mul add 1 sub} {dup mul exch dup mul add 1 exch sub} ifelse",
  171.              "dup mul exch dup mul add 1 sub",
  172.              "dup mul exch dup mul add 0.6 exch sub abs -0.5 mul",
  173.              "dup mul exch dup mul add 0.6 exch sub abs 0.5 mul",
  174.              "add 2 div",
  175.              "2 exch sub exch abs 2 mul sub 3 div",
  176.              "2 copy abs exch abs gt {exch} if pop 2 mul 1 exch sub 3.5 div",
  177.              "abs exch abs add 1 exch sub",
  178.              "pop",
  179.              "/wy exch def 180 mul cos 2 div wy dup dup dup mul mul sub mul wy add 180 mul cos",
  180.              "dup 5 mul 8 div mul exch dup mul exch add sqrt 1 exch sub",
  181.              "dup mul dup mul exch dup mul dup mul add 1 exch sub",
  182.              "dup mul exch dup mul add sqrt 1 exch sub",
  183.              "abs exch abs 2 copy gt {exch} if 1 sub dup 0 eq {0.01 add} if atan 360 div",
  184.              "pop pop rand 1 add 10240 mod 5120 div 1 exch sub",
  185.              "pop abs 2 mul 1 exch sub"
  186.             };
  187.  
  188. void printer_overlay() { }    /* for restore_active_ovly */
  189.  
  190. void Print_Screen()
  191. {
  192.     int y,j;
  193.     char buff[192];        /* buffer for 192 sets of pixels  */
  194.                 /* This is very large so that we can*/
  195.                 /* get reasonable times printing  */
  196.                 /* from modes like 2048x2048 disk-*/
  197.                 /* video.  When this was 24, a 2048*/
  198.                 /* by 2048 pic took over 2 hours to*/
  199.                 /* print.  It takes about 15 min now*/
  200.     int BuffSiz;        /* how much of buff[] we'll use   */
  201.     char far *es;        /* pointer to extraseg for buffer */
  202.     int i,x,k,            /* more indices           */
  203.     imax,            /* maximum i value (ydots/8)      */
  204.     res,            /* resolution we're gonna' use    */
  205.     high,            /* if LPTn>10 COM == com port to use*/
  206.     low,            /* misc               */
  207.                 /************************************/
  208.     ptrid;            /* Printer Id code.          */
  209.                 /* Currently, the following are   */
  210.                 /* assigned:              */
  211.                 /*          1. HPLJ (all)      */
  212.                 /*         Toshiba PageLaser*/
  213.                 /*          2. IBM Graphics      */
  214.                 /*          3. Color Printer      */
  215.                 /*          4. HP PaintJet      */
  216.                 /*          5. PostScript      */
  217.                 /************************************/
  218.     double ci,ck;
  219.     int pj_width;
  220.     int pj_color_ptr[256];    /* Paintjet color translation */
  221.     char EndOfLine[3];
  222.  
  223.     ENTER_OVLY(OVLY_PRINTER);
  224.                 /********   SETUP VARIABLES  ********/
  225.     memset(buff,0,192);
  226.  
  227.     EndOfLine[0]=(((Printer_CRLF==1) || (Printer_CRLF==0)) ? 0x0D : 0x0A);
  228.     EndOfLine[1]=((Printer_CRLF==0) ? 0x0A : 0x00);
  229.     EndOfLine[2]=0x00;
  230.  
  231.     if (Print_To_File>0)
  232.       {
  233.       while ((PRFILE = fopen(PrintName,"r"))) {
  234.      j = fgetc(PRFILE);
  235.      fclose(PRFILE);
  236.      if (j == EOF) break;
  237.      updatesavename((char *)PrintName);
  238.      }
  239.       if ((PRFILE = fopen(PrintName,"wb"))==NULL) Print_To_File = 0;
  240.       }
  241.  
  242.     es=MK_FP(extraseg,0);
  243.  
  244.     LPTn=LPTNumber-1;
  245.     if (((LPTn>2)&&(LPTn<10))||
  246.     ((LPTn>13)&&(LPTn<20))||
  247.     ((LPTn>21)&&(LPTn<30))||
  248.     (LPTn<0)||(LPTn>31)) LPTn=0;   /* default of LPT1 (==0)      */
  249.     ptrid=Printer_Type;
  250.     if ((ptrid<1)||(ptrid>7)) ptrid=2; /* default of IBM/EPSON         */
  251.     res=Printer_Resolution;
  252.     if ((LPTn==20)||(LPTn==21))
  253.     {
  254.     k = (inp((LPTn==20) ? 0x37A : 0x27A)) & 0xF7;
  255.     outp((LPTn==20) ? 0x37A : 0x27A,k);
  256.     k = k & 0xFB;
  257.     outp((LPTn==20) ? 0x37A : 0x27A,k);
  258.     k = k | 0x0C;
  259.     outp((LPTn==20) ? 0x37A : 0x27A,k);
  260.     }
  261.     if ((LPTn==30)||(LPTn==31))
  262.     {
  263.     outp((LPTn==30) ? 0x3F9 : 0x2F9,0x00);
  264.     outp((LPTn==30) ? 0x3FC : 0x2FC,0x00);
  265.     outp((LPTn==30) ? 0x3FC : 0x2FC,0x03);
  266.     }
  267.  
  268.     switch (ptrid) {
  269.  
  270.     case 1:
  271.         if (res<75) res=75;
  272.         if ( (res<= 75)&&(ydots> 600)) res=100;
  273.         if ( (res<=100)&&(ydots> 800)) res=150;
  274.         if (((res<=150)&&(ydots>1200))||(res>300)) res=300;
  275.         break;
  276.  
  277.     case 2:
  278.     case 3:
  279.         if (res<60) res=60;
  280.         if ((res<=60)&&(ydots>480)) res=120;
  281.         if (((res<=120)&&(ydots>960))||(res>240)) res=240;
  282.         break;
  283.  
  284.     case 4: /****** PaintJet  *****/
  285.         {
  286.         /* Pieter Branderhorst:
  287.            My apologies if the numbers and approach here seem to be
  288.            picked out of a hat.  They were.  They happen to result in
  289.            a tolerable mapping of screen colors to printer colors on
  290.            my machine.  There are two sources of error in getting colors
  291.            to come out right.
  292.            1) Must match some dacbox values to the 330 PaintJet dithered
  293.           colors so that they look the same.  For this we use HP's
  294.           color values in printera.asm and modify by gamma separately
  295.           for each of red/green/blue.  This mapping is ok if the
  296.           preview shown on screen is a fairly close match to what
  297.           gets printed. The defaults are what work for me.
  298.            2) Must find nearest color in HP palette to each color in
  299.           current image. For this we use Lee Crocker's least sum of
  300.           differences squared approach, modified to spread the
  301.           values using gamma 1.7.  This mods was arrived at by
  302.           trial and error, just because it improves the mapping.
  303.            */
  304.         long ldist;
  305.         int r,g,b;
  306.         double gamma,gammadiv;
  307.         unsigned char convert[256];
  308.         unsigned char scale[64];
  309.  
  310.         unsigned char far *table_ptr;
  311.         res = (res < 150) ? 90 : 180;   /* 90 or 180 dpi */
  312.         if (Printer_SetScreen == 0) {
  313.         Printer_SFrequency = 21;  /* default red gamma */
  314.         Printer_SAngle       = 19;  /*       green gamma */
  315.         Printer_SStyle       = 16;  /*        blue gamma */
  316.         }
  317.         /* Convert the values in printera.asm.  We might do this just   */
  318.         /* once per run, but we'd need separate memory for that - can't */
  319.         /* just convert table in-place cause it could be in an overlay, */
  320.         /* might be paged out and then back in in original form.  Also, */
  321.         /* user might change gammas with a .par file entry mid-run.     */
  322.         for (j = 0; j < 3; ++j) {
  323.         switch (j) {
  324.             case 0: table_ptr = pj_reds;
  325.                 i = Printer_SFrequency;
  326.                 break;
  327.             case 1: table_ptr = pj_greens;
  328.                 i = Printer_SAngle;
  329.                 break;
  330.             case 2: table_ptr = pj_blues;
  331.                 i = Printer_SStyle;
  332.             }
  333.         gamma = 10.0 / i;
  334.         gammadiv = pow(255,gamma) / 255;
  335.         for (i = 0; i < 256; ++i) { /* build gamma conversion table */
  336.             if ((i & 15) == 15)
  337.             thinking(1,"Calculating color translation");
  338.             convert[i] = (int)((pow((double)i,gamma) / gammadiv) + 0.5);
  339.             }
  340.         for (i = 0; i < 330; ++i) {
  341.             k = convert[table_ptr[i]];
  342.             if (k > 252) k = 252;
  343.             dstack[0][j][i] = (k + 2) >> 2;
  344.         }
  345.         }
  346.         /* build comparison lookup table */
  347.         gamma = 1.7;
  348.         gammadiv = pow(63,gamma) / 63;
  349.         for (i = 0; i < 64; ++i) {
  350.            if ((j = (int)((pow((double)i,gamma) / gammadiv) * 4 + 0.5)) < i)
  351.           j = i;
  352.            scale[i] = j;
  353.         }
  354.         for (i = 0; i < 3; ++i) /* convert values via lookup */
  355.         for (j = 0; j < 330; ++j)
  356.             dstack[1][i][j] = scale[dstack[0][i][j]];
  357.         /* Following code and the later code which writes to Paintjet    */
  358.         /* using pj_patterns was adapted from Lee Crocker's PGIF program */
  359.         for (i = 0; i < colors; ++i) { /* find nearest match colors */
  360.         r = scale[dacbox[i][0]];
  361.         g = scale[dacbox[i][1]];
  362.         b = scale[dacbox[i][2]];
  363.         ldist = 9999999;
  364.         /* check variance vs each PaintJet color */
  365.         /* if high-res 8 color mode, consider only 1st 8 colors */
  366.         j = (res == 90) ? 330 : 8;
  367.         while (--j >= 0) {
  368.             long dist;
  369.             dist  = (unsigned)(r-dstack[1][0][j]) * (r-dstack[1][0][j]);
  370.             dist += (unsigned)(g-dstack[1][1][j]) * (g-dstack[1][1][j]);
  371.             dist += (unsigned)(b-dstack[1][2][j]) * (b-dstack[1][2][j]);
  372.             if (dist < ldist) {
  373.             ldist = dist;
  374.             k = j;
  375.             }
  376.         }
  377.         pj_color_ptr[i] = k; /* remember best fit */
  378.         }
  379.         thinking(0,NULL);
  380.     /*  if (debugflag == 900 || debugflag == 902) {
  381.         color_test();
  382.         EXIT_OVLY;
  383.         return;
  384.         }  */
  385.         if (dotmode != 11) { /* preview */
  386.         memcpy(dstack[1],dacbox,768);
  387.         for (i = 0; i < colors; ++i)
  388.             for (j = 0; j < 3; ++j)
  389.             dacbox[i][j] = dstack[0][j][pj_color_ptr[i]];
  390.         spindac(0,1);
  391.         texttempmsg("Preview. Enter=go, Esc=cancel, k=keep");
  392.         i = getakeynohelp();
  393.         if (i == 'K' || i == 'k') {
  394.             EXIT_OVLY;
  395.             return;
  396.         }
  397.         memcpy(dacbox,dstack[1],768);
  398.         spindac(0,1);
  399.         if (i == 0x1B) {
  400.             EXIT_OVLY;
  401.             return;
  402.         }
  403.         }
  404.         break;
  405.         }
  406.  
  407.     case 5:
  408.     case 6: /***** PostScript *****/
  409.         if ( res < 10 && res != 0 ) res = 10; /* PostScript scales... */
  410.         if ( res > 600 ) res = 600; /* it can handle any range! */
  411.         if ((Printer_SStyle < 0) || (Printer_SStyle >= TONES))
  412.         Printer_SStyle = 0;
  413.         break;
  414.     }
  415.  
  416.     /*****  Set up buffer size for immediate user gratification *****/
  417.     /*****    AKA, if we don't have to, don't buffer the data   *****/
  418.     BuffSiz=8;
  419.     if (xdots>1024) BuffSiz=192;
  420.  
  421.     /*****   Initialize printer  *****/
  422.     if (Print_To_File < 1) {
  423.     printer_reset();
  424.     /* wait a bit, some printers need time after reset */
  425.     delay((ptrid == 4) ? 2000 : 500);
  426.     }
  427.  
  428.     /******  INITIALIZE GRAPHICS MODES    ******/
  429.     switch (ptrid) {
  430.  
  431.     case 1:
  432.         print_title(ptrid,res,EndOfLine);
  433.         Printer_printf("\x1B*t%iR\x1B*r0A",res);/* HP           */
  434.         break;
  435.  
  436.     case 2:
  437.     case 3:
  438.         print_title(ptrid,res,EndOfLine);
  439.         Printer_printf("\x1B\x33\x18");/* IBM                   */
  440.         break;
  441.  
  442.     case 4: /****** PaintJet *****/
  443.         print_title(ptrid,res,EndOfLine);
  444.         pj_width = ydots;
  445.         if (res == 90) pj_width <<= 1;
  446.         Printer_printf("\x1B*r0B\x1B*t180R\x1B*r3U\x1B*r%dS\x1B*b0M\x1B*r0A",
  447.         pj_width);
  448.         pj_width >>= 3;
  449.         break;
  450.  
  451.     case 5:   /***** PostScript *****/
  452.     case 6:   /***** PostScript Landscape *****/
  453.         if (!((EPSFileType > 0) && (ptrid==5)))
  454.         Printer_printf("%%!PS-Adobe%s",EndOfLine);
  455.         if ((EPSFileType > 0) &&     /* Only needed if saving to .EPS */
  456.         (ptrid == 5))
  457.         {
  458.         Printer_printf("%%!PS-Adobe-1.0 EPSF-2.0%s",EndOfLine);
  459.  
  460.         if (EPSFileType==1)
  461.             i=xdots+78;
  462.         else
  463.             i=(int)((double)xdots * (72.0 / (double)res))+78;
  464.  
  465.         if (Printer_Titleblock==0)
  466.             {
  467.             if (EPSFileType==1) { j = ydots + 78; }
  468.             else { j = (int)(((double)ydots * (72.0 / (double)res) / (double)finalaspectratio)+78); }
  469.             }
  470.         else
  471.             {
  472.             if (EPSFileType==1) { j = ydots + 123; }
  473.             else { j = (int)(((double)ydots * (72.0 / (double)res))+123); }
  474.             }
  475.         Printer_printf("%%%%TemplateBox: 12 12 %d %d%s",i,j,EndOfLine);
  476.         Printer_printf("%%%%BoundingBox: 12 12 %d %d%s",i,j,EndOfLine);
  477.         Printer_printf("%%%%PrinterRect: 12 12 %d %d%s",i,j,EndOfLine);
  478.         Printer_printf("%%%%Creator: Fractint PostScript%s",EndOfLine);
  479.         Printer_printf("%%%%Title: A %s fractal - %s - Fractint EPSF Type %d%s",
  480.                        curfractalspecific->name[0]=='*' ?
  481.                        &curfractalspecific->name[1] :
  482.                        curfractalspecific->name,
  483.                        PrintName,
  484.                        EPSFileType,
  485.                        EndOfLine);
  486.         if (Printer_Titleblock==1)
  487.             Printer_printf("%%%%DocumentFonts: Helvetica%s",EndOfLine);
  488.         Printer_printf("%%%%EndComments%s",EndOfLine);
  489.         Printer_printf("/EPSFsave save def%s",EndOfLine);
  490.         Printer_printf("0 setgray 0 setlinecap 1 setlinewidth 0 setlinejoin%s",EndOfLine);
  491.         Printer_printf("10 setmiterlimit [] 0 setdash newpath%s",EndOfLine);
  492.         }
  493.  
  494.         /* Common code for all PostScript */
  495.         Printer_printf("/dopic { gsave %d %d 8 [%d 0 0 %d 0 %d]%s",
  496.                      xdots, ydots, xdots, -ydots, ydots,
  497.                      EndOfLine);
  498.         Printer_printf("{ currentfile %d string readhexstring pop } image grestore } def%s", xdots, EndOfLine);
  499.         if (Printer_Titleblock==1)
  500.         {
  501.         Printer_printf("/Helvetica findfont 12 scalefont setfont%s",EndOfLine);
  502.         if (ptrid==5) Printer_printf("30 60 moveto ");
  503.         else          Printer_printf("552 30 moveto 90 rotate ");
  504.         print_title(ptrid,res,EndOfLine);
  505.         }
  506.  
  507.         if (EPSFileType != 1) /* Cannot use on a WELL BEHAVED .EPS */
  508.         {
  509.         if (ptrid == 5)
  510.             if ((EPSFileType==2)&&((Printer_ColorXlat!=0)||(Printer_SetScreen!=0)))
  511.             Printer_printf("%%%%BeginFeature%s",EndOfLine);
  512.  
  513.         if (Printer_ColorXlat==1)
  514.             Printer_printf("{1 exch sub} settransfer%s",EndOfLine);
  515.         if (Printer_ColorXlat>1)
  516.             Printer_printf("{%d mul round %d div} settransfer%s",
  517.                        Printer_ColorXlat,Printer_ColorXlat,EndOfLine);
  518.         if (Printer_ColorXlat<-1)
  519.             Printer_printf("{%d mul round %d div 1 exch sub} settransfer",
  520.                        Printer_ColorXlat,Printer_ColorXlat,EndOfLine);
  521.  
  522.         if (Printer_SetScreen==1)
  523.             Printer_printf("%d %d {%s} setscreen%s",
  524.                        Printer_SFrequency,
  525.                        Printer_SAngle,
  526.                        HalfTone[Printer_SStyle],EndOfLine);
  527.  
  528.         if (ptrid == 5)
  529.             {
  530.             if ((EPSFileType==2)&&((Printer_ColorXlat!=0)||(Printer_SetScreen!=0)))
  531.             Printer_printf("%%%%EndFeature%s",EndOfLine);
  532.             if (res == 0)
  533.             Printer_printf("30 191.5 translate 552 %4.1f",
  534.                     (552.0*(double)finalaspectratio));
  535.             else
  536.             Printer_printf("30 %d translate %f %f",
  537.                      75 - ((Printer_Titleblock==1) ? 0 : 45),
  538.                      ((double)xdots*(72.0/(double)res)),
  539.                      ((double)ydots*(72.0/(double)res)/(double)finalaspectratio));
  540.             }
  541.         else                 /* For Horizontal PostScript */
  542.             if (res == 0)
  543.             Printer_printf("582 30 translate 90 rotate 732 552");
  544.             else
  545.             Printer_printf("%d 30 translate 90 rotate %f %f",
  546.                      537 + ((Printer_Titleblock==1) ? 0 : 45),
  547.                      ((double)xdots*(72.0/(double)res)),
  548.                      ((double)ydots*(72.0/(double)res)/(double)finalaspectratio));
  549.         Printer_printf(" scale%s",EndOfLine);
  550.         }
  551.  
  552.         else if (ptrid == 5)       /* To be used on WELL-BEHAVED .EPS */
  553.         Printer_printf("30 %d translate %d %d scale%s",
  554.                     75 - ((Printer_Titleblock==1) ? 0 : 45),
  555.                     xdots,ydots,EndOfLine);
  556.  
  557.         Printer_printf("dopic%s",EndOfLine);
  558.         break;
  559.  
  560.     case 7: /* HP Plotter */
  561.         if (res<1) res=1;
  562.         if (res>10) res=10;
  563.         ci = (((double)xdots*((double)res-1.0))/2.0);
  564.         ck = (((double)ydots*((double)res-1.0))/2.0);
  565.         Printer_printf(";IN;SP0;SC%d,%d,%d,%d;%s\0",
  566.         (int)(-ci),(int)((double)xdots+ci),
  567.         (int)((double)ydots+ck),(int)(-ck),EndOfLine);
  568.         break;
  569.     }
  570.  
  571.     if (keypressed()) {     /* one last chance before we start...*/
  572.     EXIT_OVLY;
  573.     return;
  574.     }
  575.  
  576.     memset(buff,0,192);
  577.  
  578.                 /*****    Get And Print Screen **** */
  579.     switch (ptrid) {
  580.  
  581.     case 1:                /* HP LaserJet (et al)         */
  582.         imax=(ydots/8)-1;
  583.         for (x=0;((x<xdots)&&(!keypressed()));x+=BuffSiz) {
  584.         for (i=imax;((i>=0)&&(!keypressed()));i--) {
  585.             for (y=7;((y>=0)&&(!keypressed()));y--) {
  586.             for (j=0;j<BuffSiz;j++) {
  587.                 if ((x+j)<xdots) {
  588.                 buff[j]<<=1;
  589.                 buff[j]+=(getcolor(x+j,i*8+y)&1);
  590.                 }
  591.                 }
  592.             }
  593.             for (j=0;j<BuffSiz;j++) {
  594.             *(es+j+BuffSiz*i)=buff[j];
  595.             buff[j]=0;
  596.             }
  597.             }
  598.         for (j=0;((j<BuffSiz)&&(!keypressed()));j++) {
  599.             if ((x+j)<xdots) {
  600.             Printer_printf("\x1B*b%iW",imax+1);
  601.             for (i=imax;((i>=0)&&(!keypressed()));i--) {
  602.                 printer(*(es+j+BuffSiz*i));
  603.                 }
  604.             }
  605.             }
  606.         }
  607.         if (!keypressed()) Printer_printf("\x1B*rB\x0C");
  608.         break;
  609.  
  610.     case 2:                /* IBM Graphics/Epson         */
  611.         for (x=0;((x<xdots)&&(!keypressed()));x+=8) {
  612.         switch (res) {
  613.             case 60:  Printer_printf("\x1BK"); break;
  614.             case 120: Printer_printf("\x1BL"); break;
  615.             case 240: Printer_printf("\x1BZ"); break;
  616.             }
  617.         high=ydots/256;
  618.         low=ydots-(high*256);
  619.         printer(low);
  620.         printer(high);
  621.         for (y=ydots-1;(y>=0);y--) {
  622.             buff[0]=0;
  623.             for (i=0;i<8;i++) {
  624.             buff[0]<<=1;
  625.             buff[0]+=(getcolor(x+i,y)&1);
  626.             }
  627.             printer(buff[0]);
  628.             }
  629.         if (keypressed()) break;
  630.         Printer_printf(EndOfLine);
  631.         }
  632.         if (!keypressed()) printer(12);
  633.         break;
  634.  
  635.     case 3:                /* IBM Graphics/Epson Color    */
  636.         high=ydots/256;
  637.         low=ydots%256;
  638.         for (x=0;((x<xdots)&&(!keypressed()));x+=8)
  639.         {
  640.         for (k=0; k<8; k++)  /* colors */
  641.             {
  642.             Printer_printf("\x1Br%d",k); /* set printer color */
  643.             switch (res)
  644.             {
  645.             case 60:  Printer_printf("\x1BK"); break;
  646.             case 120: Printer_printf("\x1BL"); break;
  647.             case 240: Printer_printf("\x1BZ"); break;
  648.             }
  649.             printer(low);
  650.             printer(high);
  651.             for (y=ydots-1;y>=0;y--)
  652.             {
  653.             buff[0]=0;
  654.             for (i=0;i<8;i++)
  655.                 {
  656.                 buff[0]<<=1;
  657.                 if ((getcolor(x+i,y)%8)==k)
  658.                 buff[0]++;
  659.                 }
  660.             printer(buff[0]);
  661.             }
  662.             if (Printer_CRLF<2) printer(13);
  663.             }
  664.         if ((Printer_CRLF==0) || (Printer_CRLF==2)) printer(10);
  665.         }
  666.         printer(12);
  667.         printer(12);
  668.         printer_reset();
  669.         break;
  670.  
  671.     case 4:               /* HP PaintJet       */
  672.         {
  673.         unsigned int fetchrows,fetched;
  674.         unsigned char far *pixels, far *nextpixel;
  675.         /* for reasonable speed when using disk video, try to fetch
  676.            and store the info for 8 columns at a time instead of
  677.            doing getcolor calls down each column in separate passes */
  678.         fetchrows = 16;
  679.         while (1) {
  680.         if ((pixels = farmemalloc((long)(fetchrows)*ydots))) break;
  681.         if ((fetchrows >>= 1) == 0) {
  682.             static char far msg[]={"insufficient memory"};
  683.             stopmsg(0,msg);
  684.             break;
  685.         }
  686.         }
  687.         if (!pixels) break;
  688.         fetched = 0;
  689.         for (x = 0; (x < xdots && !keypressed()); ++x) {
  690.         if (fetched == 0) {
  691.             if ((fetched = xdots-x) > fetchrows)
  692.             fetched = fetchrows;
  693.             for (y = ydots-1; y >= 0; --y) {
  694.             if (debugflag == 602) /* flip image */
  695.                 nextpixel = pixels + y;
  696.             else              /* reverse order for unflipped */
  697.                 nextpixel = pixels + ydots-1 - y;
  698.             for (i = 0; i < fetched; ++i) {
  699.                 *nextpixel = getcolor(x+i,y);
  700.                 nextpixel += ydots;
  701.             }
  702.             }
  703.             nextpixel = pixels;
  704.         }
  705.         --fetched;
  706.         if (res == 180) { /* high-res 8 color mode */
  707.             int offset;
  708.             unsigned char bitmask;
  709.             offset = -1;
  710.             bitmask = 0;
  711.             for (y = ydots - 1; y >= 0; --y) {
  712.             unsigned char color;
  713.             if ((bitmask >>= 1) == 0) {
  714.                 ++offset;
  715.                 dstack[0][0][offset] = dstack[0][1][offset]
  716.                          = dstack[0][2][offset] = 0;
  717.                 bitmask = 0x80;
  718.             }
  719.             /* translate 01234567 to 70123456 */
  720.             color = pj_color_ptr[*(nextpixel++)] - 1;
  721.             if ((color & 1)) dstack[0][0][offset] += bitmask;
  722.             if ((color & 2)) dstack[0][1][offset] += bitmask;
  723.             if ((color & 4)) dstack[0][2][offset] += bitmask;
  724.             }
  725.         }
  726.         else { /* 90 dpi, build 2 lines, 2 dots per pixel */
  727.             int bitct,offset;
  728.             bitct = offset = 0;
  729.             for (y = ydots - 1; y >= 0; --y) {
  730.             unsigned int color;
  731.             color = pj_patterns[pj_color_ptr[*(nextpixel++)]];
  732.             for (i = 0; i < 3; ++i) {
  733.                 unsigned char *bufptr;
  734.                 bufptr = &dstack[0][i][offset];
  735.                 *bufptr <<= 2;
  736.                 if ((color & 0x1000)) *bufptr += 2;
  737.                 if ((color & 0x0100)) ++*bufptr;
  738.                 bufptr = &dstack[1][i][offset];
  739.                 *bufptr <<= 2;
  740.                 if ((color & 0x0010)) *bufptr += 2;
  741.                 if ((color & 0x0001)) ++*bufptr;
  742.                 color >>= 1;
  743.             }
  744.             if (++bitct == 4) {
  745.                 bitct = 0;
  746.                 ++offset;
  747.             }
  748.             }
  749.         }
  750.         for (i = 0; i < ((res == 90) ? 2 : 1); ++i) {
  751.             for (j = 0; j < 3; ++j) {
  752.             unsigned char *bufptr,*bufend;
  753.             Printer_printf((j < 2) ? "\x1B*b%dV" : "\x1B*b%dW",
  754.                        pj_width);
  755.             bufend = pj_width + (bufptr = dstack[i][j]);
  756.             do {
  757.                 while (printer(*bufptr)) { }
  758.             } while (++bufptr < bufend);
  759.             }
  760.         }
  761.         }
  762.         Printer_printf("\x1B*r0B"); /* end raster graphics */
  763.         if (!keypressed())
  764.            if (debugflag != 600)
  765.           printer(12); /* form feed */
  766.            else
  767.           Printer_printf("\n\n");
  768.         farmemfree(pixels);
  769.         break;
  770.         }
  771.  
  772.     case 5:
  773.     case 6:     /***** PostScript Portrait & Landscape *****/
  774.         {
  775.         char convert[513];
  776.         for (i=0; i<256; ++i)
  777.         sprintf(&convert[2*i], "%02X",
  778.                   (int)((1.20 * (double)dacbox[i][0])+
  779.                     (2.36 * (double)dacbox[i][1])+
  780.                     (0.44 * (double)dacbox[i][2])));
  781.         i=0;
  782.         j=0;
  783.         for (y=0;((y<ydots)&&(!keypressed()));y++)
  784.         {
  785.         for (x=0;x<xdots;x++)
  786.         {
  787.             k=getcolor(x,y)*2;
  788.             buff[i++]=convert[k];
  789.             buff[i++]=convert[k+1];
  790.             if (i>=64)
  791.             {
  792.             strcpy(&buff[i],"  ");
  793.             Printer_printf("%s%s",buff,EndOfLine);
  794.             i=0;
  795.             j++;
  796.             if (j>9)
  797.             {
  798.                 j=0;
  799.                 Printer_printf(EndOfLine);
  800.             }
  801.             }
  802.         }
  803.         }
  804.         if ( (EPSFileType > 0) && (EPSFileType <3) )
  805.         Printer_printf("%s%%%%Trailer%sEPSFsave restore%s",EndOfLine,EndOfLine,EndOfLine);
  806.         else
  807.         Printer_printf("%sshowpage%s%c",EndOfLine,EndOfLine,4);
  808.         break;
  809.         }
  810.  
  811.     case 7: /* HP Plotter */
  812.         {
  813.         double parm1,parm2;
  814.         for (i=0;i<3;i++)
  815.         {
  816.           Printer_printf("%sSP %d;%s\0",EndOfLine,(i+1),EndOfLine);
  817.           for (y=0;(y<ydots)&&(!keypressed());y++)
  818.           {
  819.         for (x=0;x<xdots;x++)
  820.         {
  821.           j=dacbox[getcolor(x,y)][i];
  822.           if (j>0)
  823.           {
  824.             switch(Printer_SStyle)
  825.             {
  826.               case 0:
  827.             ci=0.004582144*(double)j;
  828.             ck=-.007936057*(double)j;
  829.             parm1 = (double)x+.5+ci+(((double)i-1.0)/3);
  830.             parm2 = (double)y+.5+ck;
  831.             break;
  832.               case 1:
  833.             ci=-.004582144*(double)j+(((double)i+1.0)/8.0);
  834.             ck=-.007936057*(double)j;
  835.             parm1 = (double)x+.5+ci;
  836.             parm2 = (double)y+.5+ck;
  837.             break;
  838.               case 2:
  839.             ci=-.0078125*(double)j+(((double)i+1.0)*.003906250);
  840.             ck=-.0078125*(double)j;
  841.             parm1 = (double)x+.5+ci;
  842.             parm2 = (double)y+.5+ck;
  843.             break;
  844.             }
  845.             Printer_printf("PA %f,%f;PD;PR %f,%f;PU;\0",
  846.             parm1,parm2, ci*((double)-2), ck*((double)-2));
  847.           }
  848.         }
  849.           }
  850.         }
  851.         Printer_printf("%s;SC;PA 0,0;SP0;%s\0",EndOfLine,EndOfLine);
  852.         Printer_printf(";;SP 0;%s\0",EndOfLine);
  853.         break;
  854.         }
  855.     }
  856.  
  857.     if (Print_To_File > 0) fclose(PRFILE);
  858.     if ((LPTn==30)||(LPTn==31))
  859.     {
  860.     for (x=0;x<2000;x++);
  861.     outp((LPTn==30) ? 0x3FC : 0x2FC,0x00);
  862.     outp((LPTn==30) ? 0x3F9 : 0x2F9,0x00);
  863.     }
  864.     EXIT_OVLY;
  865. }
  866.  
  867. static void _fastcall print_title(int ptrid,int res,char *EndOfLine)
  868. {
  869.     char buff[80];
  870.     int postscript;
  871.     if (Printer_Titleblock == 0)
  872.     return;
  873.     postscript = (ptrid == 5 || ptrid ==6);
  874.     if (!postscript)
  875.     Printer_printf(EndOfLine);
  876.     else
  877.     Printer_printf("(");
  878.     Printer_printf((curfractalspecific->name[0]=='*')
  879.              ? &curfractalspecific->name[1]
  880.              : curfractalspecific->name);
  881.     if (fractype == FORMULA || fractype == FFORMULA)
  882.     Printer_printf(" %s",FormName);
  883.     if (fractype == LSYSTEM)
  884.     Printer_printf(" %s",LName);
  885.     if (fractype == IFS || fractype == IFS3D)
  886.     Printer_printf(" %s",IFSName);
  887.     Printer_printf(" - %dx%d - %d DPI", xdots, ydots, res);
  888.     if (!postscript)
  889.     Printer_printf(EndOfLine);
  890.     else {
  891.     Printer_printf(") show%s",EndOfLine);
  892.     if (ptrid==5) Printer_printf("30 45 moveto (");
  893.     else          Printer_printf("-90 rotate 567 30 moveto 90 rotate (");
  894.     }
  895.     Printer_printf("Corners: Top-Left=%4.4f/%4.4f Bottom-Right=%4.4f/%4.4f",
  896.            xxmin,yymax,xxmax,yymin);
  897.     if (xx3rd != xxmin || yy3rd != yymin) {
  898.     if (!postscript)
  899.         Printer_printf("%s        ",EndOfLine);
  900.     Printer_printf(" Bottom-Left=%4.4f/%4.4f",xx3rd,yy3rd);
  901.     }
  902.     if (!postscript)
  903.     Printer_printf(EndOfLine);
  904.     else {
  905.     Printer_printf(") show%s",EndOfLine);
  906.     if (ptrid==5) Printer_printf("30 30 moveto (");
  907.     else          Printer_printf("-90 rotate 582 30 moveto 90 rotate (");
  908.     }
  909.     showtrig(buff);
  910.     Printer_printf("Parameters: %4.4f/%4.4f/%4.4f/%4.4f %s",
  911.            param[0],param[1],param[2],param[3],buff);
  912.     if (!postscript)
  913.     Printer_printf(EndOfLine);
  914.     else
  915.     Printer_printf(") show%s",EndOfLine);
  916. }
  917.  
  918. /* This function prints a string to the the printer with BIOS calls. */
  919.  
  920. static void Printer_printf(char *fmt,...)
  921. {
  922. char s[500];
  923. int x=0;
  924. va_list arg;
  925.  
  926. va_start(arg,fmt);
  927. vsprintf(s,fmt,arg);
  928.  
  929. if (Print_To_File>0)    /* This is for printing to file */
  930.     fprintf(PRFILE,"%s",s);
  931. else            /* And this is for printing to printer */
  932.     while (s[x])
  933.     if (printer(s[x++])!=0)
  934.         while (!keypressed()) { if (printer(s[x-1])==0) break; }
  935. }
  936.  
  937. /* This function standardizes both _bios_printer and _bios_serialcom
  938.  * in one function.  It takes its arguments and rearranges them and calls
  939.  * the appropriate bios call.  If it then returns result !=0, there is a
  940.  * problem with the printer.
  941.  */
  942. static int _fastcall printer(int c)
  943. {
  944.     if (Print_To_File>0) return ((fprintf(PRFILE,"%c",c))<1);
  945.     if (LPTn<9)  return (((_bios_printer(0,LPTn,c))+0x0010)&0x0010);
  946.     if (LPTn<19) return ((_bios_serialcom(1,(LPTn-10),c))&0x9E00);
  947.     if ((LPTn==20)||(LPTn==21))
  948.     {
  949.     int PS=0;
  950.     while ((PS & 0xF8) != 0xD8)
  951.         { PS = inp((LPTn==20) ? 0x379 : 0x279);
  952.           if (keypressed()) return(1); }
  953.     outp((LPTn==20) ? 0x37C : 0x27C,c);
  954.     PS = inp((LPTn==20) ? 0x37A : 0x27A);
  955.     outp((LPTn==20) ? 0x37A : 0x27A,(PS | 0x01));
  956.     outp((LPTn==20) ? 0x37A : 0x27A,PS);
  957.     return(0);
  958.     }
  959.     if ((LPTn==30)||(LPTn==31))
  960.     {
  961.     while (((inp((LPTn==30) ? 0x3FE : 0x2FE)&0x30)!=0x30) ||
  962.            ((inp((LPTn==30) ? 0x3FD : 0x2FD)&0x60)!=0x60))
  963.         { if (keypressed()) return (1); }
  964.     outp((LPTn==30) ? 0x3F8 : 0x2F8,c);
  965.     return(0);
  966.     }
  967.  
  968.     /* MCP 7-7-91, If we made it down to here, we may as well error out. */
  969.     return(-1);
  970. }
  971.  
  972. static void printer_reset()
  973. {
  974.     if (Print_To_File < 1)
  975.     if (LPTn<9)      _bios_printer(1,LPTn,0);
  976.     else if (LPTn<19) _bios_serialcom(3,(LPTn-10),0);
  977. }
  978.  
  979.  
  980. /** debug code for pj_ color table checkout
  981. color_test()
  982. {
  983.    int x,y,color,i,j,xx,yy;
  984.    int bw,cw,bh,ch;
  985.    setvideomode(videoentry.videomodeax,
  986.         videoentry.videomodebx,
  987.         videoentry.videomodecx,
  988.         videoentry.videomodedx);
  989.    bw = xdots/25; cw = bw * 2 / 3;
  990.    bh = ydots/10; ch = bh * 2 / 3;
  991.    dacbox[0][0] = dacbox[0][1] = dacbox[0][2] = 60;
  992.    if (debugflag == 902)
  993.       dacbox[0][0] = dacbox[0][1] = dacbox[0][2] = 0;
  994.    for (x = 0; x < 25; ++x)
  995.       for (y = 0; y < 10; ++y) {
  996.      if (x < 11) i = (32 - x) * 10 + y;
  997.          else    i = (24 - x) * 10 + y;
  998.      color = x * 10 + y + 1;
  999.      dacbox[color][0] = dstack[0][0][i];
  1000.      dacbox[color][1] = dstack[0][1][i];
  1001.      dacbox[color][2] = dstack[0][2][i];
  1002.      for (i = 0; i < cw; ++i) {
  1003.         xx = x * bw + bw / 6 + i;
  1004.         yy = y * bh + bh / 6;
  1005.         for (j = 0; j < ch; ++j)
  1006.            putcolor(xx,yy++,color);
  1007.         }
  1008.      }
  1009.    spindac(0,1);
  1010.    getakey();
  1011. }
  1012. **/
  1013.  
  1014.