home *** CD-ROM | disk | FTP | other *** search
/ Fractal Creations (Second Edition) / FRACTALS_2E.iso / frasrc.exe / PRINTER.C < prev    next >
C/C++ Source or Header  |  1993-08-21  |  46KB  |  1,511 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.  */
  80.  
  81. #include <stdlib.h>
  82.  
  83. #ifndef XFRACT
  84. #include <bios.h>
  85. #include <dos.h>
  86. #include <io.h>
  87. #endif
  88.  
  89. #include <fcntl.h>
  90. #include <sys/types.h>
  91. #include <errno.h>
  92. #include <stdio.h>    /*** for vsprintf prototype ***/
  93.  
  94. #ifndef XFRACT
  95. #include <conio.h>
  96. #include <stdarg.h>
  97. #else
  98. #include <varargs.h>
  99. #endif
  100.  
  101. #include <string.h>
  102. #include <float.h>    /* for pow() */
  103. #include <math.h>    /*  "    "   */
  104. #include "fractint.h"
  105. #include "fractype.h"
  106. #include "prototyp.h"
  107.  
  108. /* macros for near-space-saving purposes */
  109. /* CAE 9211 changed these for BC++ */
  110. #define PRINTER_PRINTF2(X,Y) {\
  111.    static char far tmp[] = X;\
  112.    Printer_printf(tmp,(Y));\
  113. }
  114. #define PRINTER_PRINTF3(X,Y,Z) {\
  115.    static char far tmp[] = X;\
  116.    Printer_printf(tmp,(Y),(Z));\
  117. }
  118. #define PRINTER_PRINTF4(X,Y,Z,W) {\
  119.    static char far tmp[] = X;\
  120.    Printer_printf(tmp,(Y),(Z),(W));\
  121. }
  122.  
  123. /********      PROTOTYPES     ********/
  124.  
  125. #ifndef XFRACT
  126. static void Printer_printf(char far *fmt,...);
  127. #else
  128. static void Printer_printf();
  129. #endif
  130. static int  _fastcall printer(int c);
  131. static void _fastcall print_title(int,int,char *);
  132. static void printer_reset();
  133. static rleprolog(int x,int y);
  134. static void _fastcall graphics_init(int,int,char *);
  135.  
  136. /********  EXTRN GLOBAL VARS  ********/
  137.  
  138. extern int xdots,ydots,            /* size of screen           */
  139.        fractype;               /* used for title block           */
  140. extern SEGTYPE       extraseg;           /* used for buffering           */
  141. extern BYTE dacbox[256][3];   /* for PostScript printing       */
  142. extern BYTE dstack[2][3][400];
  143. extern char FormName[];            /* for Title block info           */
  144. extern char LName[];               /* for Title block info           */
  145. extern char IFSName[];               /* for Title block info           */
  146. extern char PrintName[];           /* Filename for print-to-file       */
  147. extern float finalaspectratio;
  148. extern double xxmin,xxmax,xx3rd,
  149.           yymin,yymax,yy3rd,param[]; /* for Title block info       */
  150. extern int colors;
  151. extern int dotmode;
  152. extern unsigned int debugflag;
  153.  
  154. extern unsigned int  far pj_patterns[];
  155. extern BYTE far pj_reds[];
  156. extern BYTE far pj_blues[];
  157. extern BYTE far pj_greens[];
  158.  
  159. /********    GLOBALS       ********/
  160.  
  161. int Printer_Resolution,        /* 75,100,150,300 for HP;           */
  162.                    /* 60,120,240 for IBM;               */
  163.                    /* 90 or 180 for the PaintJet;           */
  164.                    /* 10-600 for PS                */
  165.                    /* 1-20 for Plotter               */
  166.     LPTNumber,               /* ==1,2,3 LPTx; or 11,12,13,14 for COM1-4  */
  167.                    /* 21,22 for direct port access for LPT1-2  */
  168.                    /* 31,32 for direct port access for COM1-2  */
  169.     Printer_Type,               /* ==1 HP,
  170.                       ==2 IBM/EPSON,
  171.                       ==3 Epson color,
  172.                       ==4 HP PaintJet,
  173.                       ==5,6 PostScript,
  174.                       ==7 HP Plotter           */
  175.     Printer_Titleblock,       /* Print info about the fractal?           */
  176.     Printer_Compress,          /* PostScript only - rle encode output       */
  177.     Printer_ColorXlat,          /* PostScript only - invert colors       */
  178.     Printer_SetScreen,          /* PostScript only - reprogram halftone ?    */
  179.     Printer_SFrequency,       /* PostScript only - Halftone Frequency K    */
  180.     Printer_SAngle,           /* PostScript only - Halftone angle     K    */
  181.     Printer_SStyle,           /* PostScript only - Halftone style     K    */
  182.     Printer_RFrequency,       /* PostScript only - Halftone Frequency R    */
  183.     Printer_RAngle,           /* PostScript only - Halftone angle     R    */
  184.     Printer_RStyle,           /* PostScript only - Halftone style     R    */
  185.     Printer_GFrequency,       /* PostScript only - Halftone Frequency G    */
  186.     Printer_GAngle,           /* PostScript only - Halftone angle     G    */
  187.     Printer_GStyle,           /* PostScript only - Halftone style     G    */
  188.     Printer_BFrequency,       /* PostScript only - Halftone Frequency B    */
  189.     Printer_BAngle,           /* PostScript only - Halftone angle     B    */
  190.     Printer_BStyle,           /* PostScript only - Halftone style     B    */
  191.     Print_To_File,          /* Print to file toggle               */
  192.     EPSFileType,          /* EPSFileType -
  193.                            1 = well-behaved,
  194.                            2 = much less behaved,
  195.                            3 = not well behaved       */
  196.     Printer_CRLF,             /* (0) CRLF (1) CR (2) LF                    */
  197.     ColorPS;                  /* (0) B&W  (1) Color                        */
  198. int pj_width;
  199. double ci,ck;
  200.  
  201. static int repeat, item, count, repeatitem, itembuf[128], rlebitsperitem,
  202.     rlebitshift, bitspersample, rleitem, repeatcount, itemsperline, items,
  203.     bitsperitem, bitshift;
  204.     
  205. static void putitem(), rleputxelval(), rleflush(), rleputrest();
  206.  
  207. static int LPTn;           /* printer number we're gonna use */
  208.  
  209. static FILE *PRFILE;
  210.  
  211. #define TONES 17           /* Number of PostScript halftone styles */
  212.  
  213. static char *HalfTone[TONES]=  {
  214.              "D mul exch D mul add 1 exch sub",
  215.              "abs exch abs 2 copy add 1 gt {1 sub D mul exch 1 sub D mul add 1 sub} {D mul exch D mul add 1 exch sub} ifelse",
  216.              "D mul exch D mul add 1 sub",
  217.              "D mul exch D mul add 0.6 exch sub abs -0.5 mul",
  218.              "D mul exch D mul add 0.6 exch sub abs 0.5 mul",
  219.              "add 2 div",
  220.              "2 exch sub exch abs 2 mul sub 3 div",
  221.              "2 copy abs exch abs gt {exch} if pop 2 mul 1 exch sub 3.5 div",
  222.              "abs exch abs add 1 exch sub",
  223.              "pop",
  224.              "/wy exch def 180 mul cos 2 div wy D D D mul mul sub mul wy add 180 mul cos",
  225.              "D 5 mul 8 div mul exch D mul exch add sqrt 1 exch sub",
  226.              "D mul D mul exch D mul D mul add 1 exch sub",
  227.              "D mul exch D mul add sqrt 1 exch sub",
  228.              "abs exch abs 2 copy gt {exch} if 1 sub D 0 eq {0.01 add} if atan 360 div",
  229.              "pop pop rand 1 add 10240 mod 5120 div 1 exch sub",
  230.              "pop abs 2 mul 1 exch sub"
  231.             };
  232.  
  233. void printer_overlay() { }    /* for restore_active_ovly */
  234.  
  235. #ifdef __BORLANDC__
  236. #if(__BORLANDC__ > 2)
  237.    #pragma warn -eff
  238. #endif
  239. #endif
  240.  
  241. static char EndOfLine[3];
  242.  
  243. void Print_Screen()
  244. {
  245.     int y,j;
  246.     char buff[192];        /* buffer for 192 sets of pixels  */
  247.                 /* This is very large so that we can*/
  248.                 /* get reasonable times printing  */
  249.                 /* from modes like MAXPIXELSxMAXPIXELS disk-*/
  250.                 /* video.  When this was 24, a MAXPIXELS*/
  251.                 /* by MAXPIXELS pic took over 2 hours to*/
  252.                 /* print.  It takes about 15 min now*/
  253.     int BuffSiz;        /* how much of buff[] we'll use   */
  254.     char far *es;        /* pointer to extraseg for buffer */
  255.     int i,x,k,            /* more indices           */
  256.     imax,            /* maximum i value (ydots/8)      */
  257.     res,            /* resolution we're gonna' use    */
  258.     high,            /* if LPTn>10 COM == com port to use*/
  259.     low,            /* misc               */
  260.                 /************************************/
  261.     ptrid;            /* Printer Id code.          */
  262.                 /* Currently, the following are   */
  263.                 /* assigned:              */
  264.                 /*          1. HPLJ (all)      */
  265.                 /*         Toshiba PageLaser*/
  266.                 /*          2. IBM Graphics      */
  267.                 /*          3. Color Printer      */
  268.                 /*          4. HP PaintJet      */
  269.                 /*          5. PostScript      */
  270.                 /************************************/
  271.     int pj_color_ptr[256];    /* Paintjet color translation */
  272.  
  273.     ENTER_OVLY(OVLY_PRINTER);
  274.                 /********   SETUP VARIABLES  ********/
  275.     memset(buff,0,192);
  276.  
  277.     EndOfLine[0]=(((Printer_CRLF==1) || (Printer_CRLF==0)) ? 0x0D : 0x0A);
  278.     EndOfLine[1]=((Printer_CRLF==0) ? 0x0A : 0x00);
  279.     EndOfLine[2]=0x00;
  280.  
  281.     if (Print_To_File>0)
  282.       {
  283.       while ((PRFILE = fopen(PrintName,"r"))) {
  284.      j = fgetc(PRFILE);
  285.      fclose(PRFILE);
  286.      if (j == EOF) break;
  287.      updatesavename((char *)PrintName);
  288.      }
  289.       if ((PRFILE = fopen(PrintName,"wb"))==NULL) Print_To_File = 0;
  290.       }
  291.  
  292. #ifdef XFRACT
  293.       putstring(3,0,0,"Printing to:");
  294.       putstring(4,0,0,PrintName);
  295.       putstring(5,0,0,"               ");
  296. #endif
  297.  
  298.     es=MK_FP(extraseg,0);
  299.  
  300.     LPTn=LPTNumber-1;
  301.     if (((LPTn>2)&&(LPTn<10))||
  302.     ((LPTn>13)&&(LPTn<20))||
  303.     ((LPTn>21)&&(LPTn<30))||
  304.     (LPTn<0)||(LPTn>31)) LPTn=0;   /* default of LPT1 (==0)      */
  305.     ptrid=Printer_Type;
  306.     if ((ptrid<1)||(ptrid>7)) ptrid=2; /* default of IBM/EPSON         */
  307.     res=Printer_Resolution;
  308. #ifndef XFRACT
  309.     if ((LPTn==20)||(LPTn==21))
  310.     {
  311.     k = (inp((LPTn==20) ? 0x37A : 0x27A)) & 0xF7;
  312.     outp((LPTn==20) ? 0x37A : 0x27A,k);
  313.     k = k & 0xFB;
  314.     outp((LPTn==20) ? 0x37A : 0x27A,k);
  315.     k = k | 0x0C;
  316.     outp((LPTn==20) ? 0x37A : 0x27A,k);
  317.     }
  318.     if ((LPTn==30)||(LPTn==31))
  319.     {
  320.     outp((LPTn==30) ? 0x3F9 : 0x2F9,0x00);
  321.     outp((LPTn==30) ? 0x3FC : 0x2FC,0x00);
  322.     outp((LPTn==30) ? 0x3FC : 0x2FC,0x03);
  323.     }
  324. #endif
  325.  
  326.     switch (ptrid) {
  327.  
  328.     case 1:
  329.         if (res<75) res=75;
  330.         if ( (res<= 75)&&(ydots> 600)) res=100;
  331.         if ( (res<=100)&&(ydots> 800)) res=150;
  332.         if (((res<=150)&&(ydots>1200))||(res>300)) res=300;
  333.         break;
  334.  
  335.     case 2:
  336.     case 3:
  337.         if (res<60) res=60;
  338.         if ((res<=60)&&(ydots>480)) res=120;
  339.         if (((res<=120)&&(ydots>960))||(res>240)) res=240;
  340.         break;
  341.  
  342.     case 4: /****** PaintJet  *****/
  343.         {
  344. #ifndef XFRACT
  345.         /* Pieter Branderhorst:
  346.            My apologies if the numbers and approach here seem to be
  347.            picked out of a hat.  They were.  They happen to result in
  348.            a tolerable mapping of screen colors to printer colors on
  349.            my machine.  There are two sources of error in getting colors
  350.            to come out right.
  351.            1) Must match some dacbox values to the 330 PaintJet dithered
  352.           colors so that they look the same.  For this we use HP's
  353.           color values in printera.asm and modify by gamma separately
  354.           for each of red/green/blue.  This mapping is ok if the
  355.           preview shown on screen is a fairly close match to what
  356.           gets printed. The defaults are what work for me.
  357.            2) Must find nearest color in HP palette to each color in
  358.           current image. For this we use Lee Crocker's least sum of
  359.           differences squared approach, modified to spread the
  360.           values using gamma 1.7.  This mods was arrived at by
  361.           trial and error, just because it improves the mapping.
  362.            */
  363.         long ldist;
  364.         int r,g,b;
  365.         double gamma_val,gammadiv;
  366.         BYTE convert[256];
  367.         BYTE scale[64];
  368.  
  369.         BYTE far *table_ptr;
  370.         res = (res < 150) ? 90 : 180;   /* 90 or 180 dpi */
  371.         if (Printer_SetScreen == 0) {
  372.         Printer_SFrequency = 21;  /* default red gamma */
  373.         Printer_SAngle       = 19;  /*       green gamma */
  374.         Printer_SStyle       = 16;  /*        blue gamma */
  375.         }
  376.         /* Convert the values in printera.asm.  We might do this just   */
  377.         /* once per run, but we'd need separate memory for that - can't */
  378.         /* just convert table in-place cause it could be in an overlay, */
  379.         /* might be paged out and then back in in original form.  Also, */
  380.         /* user might change gammas with a .par file entry mid-run.     */
  381.         for (j = 0; j < 3; ++j) {
  382.         switch (j) {
  383.             case 0: table_ptr = pj_reds;
  384.                 i = Printer_SFrequency;
  385.                 break;
  386.             case 1: table_ptr = pj_greens;
  387.                 i = Printer_SAngle;
  388.                 break;
  389.             case 2: table_ptr = pj_blues;
  390.                 i = Printer_SStyle;
  391.             }
  392.         gamma_val = 10.0 / i;
  393.         gammadiv = pow(255,gamma_val) / 255;
  394.         for (i = 0; i < 256; ++i) { /* build gamma conversion table */
  395.             if ((i & 15) == 15)
  396.             thinking(1,"Calculating color translation");
  397.             convert[i] = (int)((pow((double)i,gamma_val) / gammadiv) + 0.5);
  398.             }
  399.         for (i = 0; i < 330; ++i) {
  400.             k = convert[table_ptr[i]];
  401.             if (k > 252) k = 252;
  402.             dstack[0][j][i] = (k + 2) >> 2;
  403.         }
  404.         }
  405.         /* build comparison lookup table */
  406.         gamma_val = 1.7;
  407.         gammadiv = pow(63,gamma_val) / 63;
  408.         for (i = 0; i < 64; ++i) {
  409.            if ((j = (int)((pow((double)i,gamma_val) / gammadiv) * 4 + 0.5)) < i)
  410.           j = i;
  411.            scale[i] = j;
  412.         }
  413.         for (i = 0; i < 3; ++i) /* convert values via lookup */
  414.         for (j = 0; j < 330; ++j)
  415.             dstack[1][i][j] = scale[dstack[0][i][j]];
  416.         /* Following code and the later code which writes to Paintjet    */
  417.         /* using pj_patterns was adapted from Lee Crocker's PGIF program */
  418.         for (i = 0; i < colors; ++i) { /* find nearest match colors */
  419.         r = scale[dacbox[i][0]];
  420.         g = scale[dacbox[i][1]];
  421.         b = scale[dacbox[i][2]];
  422.         ldist = 9999999;
  423.         /* check variance vs each PaintJet color */
  424.         /* if high-res 8 color mode, consider only 1st 8 colors */
  425.         j = (res == 90) ? 330 : 8;
  426.         while (--j >= 0) {
  427.             long dist;
  428.             dist  = (unsigned)(r-dstack[1][0][j]) * (r-dstack[1][0][j]);
  429.             dist += (unsigned)(g-dstack[1][1][j]) * (g-dstack[1][1][j]);
  430.             dist += (unsigned)(b-dstack[1][2][j]) * (b-dstack[1][2][j]);
  431.             if (dist < ldist) {
  432.             ldist = dist;
  433.             k = j;
  434.             }
  435.         }
  436.         pj_color_ptr[i] = k; /* remember best fit */
  437.         }
  438.         thinking(0,NULL);
  439.     /*  if (debugflag == 900 || debugflag == 902) {
  440.         color_test();
  441.         EXIT_OVLY;
  442.         return;
  443.         }  */
  444.         if (dotmode != 11) { /* preview */
  445.         memcpy(dstack[1],dacbox,768);
  446.         for (i = 0; i < colors; ++i)
  447.             for (j = 0; j < 3; ++j)
  448.             dacbox[i][j] = dstack[0][j][pj_color_ptr[i]];
  449.         spindac(0,1);
  450.         texttempmsg("Preview. Enter=go, Esc=cancel, k=keep");
  451.         i = getakeynohelp();
  452.         if (i == 'K' || i == 'k') {
  453.             EXIT_OVLY;
  454.             return;
  455.         }
  456.         memcpy(dacbox,dstack[1],768);
  457.         spindac(0,1);
  458.         if (i == 0x1B) {
  459.             EXIT_OVLY;
  460.             return;
  461.         }
  462.         }
  463.         break;
  464. #endif
  465.         }
  466.  
  467.     case 5:
  468.     case 6: /***** PostScript *****/
  469.         if ( res < 10 && res != 0 ) res = 10; /* PostScript scales... */
  470.         if ( res > 600 ) res = 600; /* it can handle any range! */
  471.         if ((Printer_SStyle < 0) || (Printer_SStyle >= TONES))
  472.         Printer_SStyle = 0;
  473.         break;
  474.     }
  475.  
  476.     /*****  Set up buffer size for immediate user gratification *****/
  477.     /*****    AKA, if we don't have to, don't buffer the data   *****/
  478.     BuffSiz=8;
  479.     if (xdots>1024) BuffSiz=192;
  480.  
  481.     /*****   Initialize printer  *****/
  482.     if (Print_To_File < 1) {
  483.     printer_reset();
  484.     /* wait a bit, some printers need time after reset */
  485.     delay((ptrid == 4) ? 2000 : 500);
  486.     }
  487.  
  488.     /******  INITIALIZE GRAPHICS MODES    ******/
  489.  
  490.     graphics_init(ptrid,res,EndOfLine);
  491.  
  492.     if (keypressed()) {     /* one last chance before we start...*/
  493.     EXIT_OVLY;
  494.     return;
  495.     }
  496.  
  497.     memset(buff,0,192);
  498.  
  499.                 /*****    Get And Print Screen **** */
  500.     switch (ptrid) {
  501.  
  502.     case 1:                /* HP LaserJet (et al)         */
  503.         imax=(ydots/8)-1;
  504.         for (x=0;((x<xdots)&&(!keypressed()));x+=BuffSiz) {
  505.         for (i=imax;((i>=0)&&(!keypressed()));i--) {
  506.             for (y=7;((y>=0)&&(!keypressed()));y--) {
  507.             for (j=0;j<BuffSiz;j++) {
  508.                 if ((x+j)<xdots) {
  509.                 buff[j]<<=1;
  510.                 buff[j]+=(getcolor(x+j,i*8+y)&1);
  511.                 }
  512.                 }
  513.             }
  514.             for (j=0;j<BuffSiz;j++) {
  515.             *(es+j+BuffSiz*i)=buff[j];
  516.             buff[j]=0;
  517.             }
  518.             }
  519.         for (j=0;((j<BuffSiz)&&(!keypressed()));j++) {
  520.             if ((x+j)<xdots) {
  521.             PRINTER_PRINTF2("\033*b%iW",imax+1);
  522.             for (i=imax;((i>=0)&&(!keypressed()));i--) {
  523.                 printer(*(es+j+BuffSiz*i));
  524.                 }
  525.             }
  526.             }
  527.         }
  528.         if (!keypressed()) Printer_printf("\033*rB\014");
  529.         break;
  530.  
  531.     case 2:                /* IBM Graphics/Epson         */
  532.         for (x=0;((x<xdots)&&(!keypressed()));x+=8) {
  533.         switch (res) {
  534.             case 60:  Printer_printf("\033K"); break;
  535.             case 120: Printer_printf("\033L"); break;
  536.             case 240: Printer_printf("\033Z"); break;
  537.             }
  538.         high=ydots/256;
  539.         low=ydots-(high*256);
  540.         printer(low);
  541.         printer(high);
  542.         for (y=ydots-1;(y>=0);y--) {
  543.             buff[0]=0;
  544.             for (i=0;i<8;i++) {
  545.             buff[0]<<=1;
  546.             buff[0]+=(getcolor(x+i,y)&1);
  547.             }
  548.             printer(buff[0]);
  549.             }
  550.         if (keypressed()) break;
  551.         Printer_printf(EndOfLine);
  552.         }
  553.         if (!keypressed()) printer(12);
  554.         break;
  555.  
  556.     case 3:                /* IBM Graphics/Epson Color    */
  557.         high=ydots/256;
  558.         low=ydots%256;
  559.         for (x=0;((x<xdots)&&(!keypressed()));x+=8)
  560.         {
  561.         for (k=0; k<8; k++)  /* colors */
  562.             {
  563.             Printer_printf("\033r%d",k); /* set printer color */
  564.             switch (res)
  565.             {
  566.             case 60:  Printer_printf("\033K"); break;
  567.             case 120: Printer_printf("\033L"); break;
  568.             case 240: Printer_printf("\033Z"); break;
  569.             }
  570.             printer(low);
  571.             printer(high);
  572.             for (y=ydots-1;y>=0;y--)
  573.             {
  574.             buff[0]=0;
  575.             for (i=0;i<8;i++)
  576.                 {
  577.                 buff[0]<<=1;
  578.                 if ((getcolor(x+i,y)%8)==k)
  579.                 buff[0]++;
  580.                 }
  581.             printer(buff[0]);
  582.             }
  583.             if (Printer_CRLF<2) printer(13);
  584.             }
  585.         if ((Printer_CRLF==0) || (Printer_CRLF==2)) printer(10);
  586.         }
  587.         printer(12);
  588.         printer(12);
  589.         printer_reset();
  590.         break;
  591.  
  592.     case 4:               /* HP PaintJet       */
  593.         {
  594.         unsigned int fetchrows,fetched;
  595.         BYTE far *pixels, far *nextpixel;
  596.         /* for reasonable speed when using disk video, try to fetch
  597.            and store the info for 8 columns at a time instead of
  598.            doing getcolor calls down each column in separate passes */
  599.         fetchrows = 16;
  600.         while (1) {
  601.         if ((pixels = farmemalloc((long)(fetchrows)*ydots))) break;
  602.         if ((fetchrows >>= 1) == 0) {
  603.             static char far msg[]={"insufficient memory"};
  604.             stopmsg(0,msg);
  605.             break;
  606.         }
  607.         }
  608.         if (!pixels) break;
  609.         fetched = 0;
  610.         for (x = 0; (x < xdots && !keypressed()); ++x) {
  611.         if (fetched == 0) {
  612.             if ((fetched = xdots-x) > fetchrows)
  613.             fetched = fetchrows;
  614.             for (y = ydots-1; y >= 0; --y) {
  615.             if (debugflag == 602) /* flip image */
  616.                 nextpixel = pixels + y;
  617.             else              /* reverse order for unflipped */
  618.                 nextpixel = pixels + ydots-1 - y;
  619.             for (i = 0; i < fetched; ++i) {
  620.                 *nextpixel = getcolor(x+i,y);
  621.                 nextpixel += ydots;
  622.             }
  623.             }
  624.             nextpixel = pixels;
  625.         }
  626.         --fetched;
  627.         if (res == 180) { /* high-res 8 color mode */
  628.             int offset;
  629.             BYTE bitmask;
  630.             offset = -1;
  631.             bitmask = 0;
  632.             for (y = ydots - 1; y >= 0; --y) {
  633.             BYTE color;
  634.             if ((bitmask >>= 1) == 0) {
  635.                 ++offset;
  636.                 dstack[0][0][offset] = dstack[0][1][offset]
  637.                          = dstack[0][2][offset] = 0;
  638.                 bitmask = 0x80;
  639.             }
  640.             /* translate 01234567 to 70123456 */
  641.             color = pj_color_ptr[*(nextpixel++)] - 1;
  642.             if ((color & 1)) dstack[0][0][offset] += bitmask;
  643.             if ((color & 2)) dstack[0][1][offset] += bitmask;
  644.             if ((color & 4)) dstack[0][2][offset] += bitmask;
  645.             }
  646.         }
  647.         else { /* 90 dpi, build 2 lines, 2 dots per pixel */
  648.             int bitct,offset;
  649.             bitct = offset = 0;
  650.             for (y = ydots - 1; y >= 0; --y) {
  651.             unsigned int color;
  652.             color = pj_patterns[pj_color_ptr[*(nextpixel++)]];
  653.             for (i = 0; i < 3; ++i) {
  654.                 BYTE *bufptr;
  655.                 bufptr = &dstack[0][i][offset];
  656.                 *bufptr <<= 2;
  657.                 if ((color & 0x1000)) *bufptr += 2;
  658.                 if ((color & 0x0100)) ++*bufptr;
  659.                 bufptr = &dstack[1][i][offset];
  660.                 *bufptr <<= 2;
  661.                 if ((color & 0x0010)) *bufptr += 2;
  662.                 if ((color & 0x0001)) ++*bufptr;
  663.                 color >>= 1;
  664.             }
  665.             if (++bitct == 4) {
  666.                 bitct = 0;
  667.                 ++offset;
  668.             }
  669.             }
  670.         }
  671.         for (i = 0; i < ((res == 90) ? 2 : 1); ++i) {
  672.             for (j = 0; j < 3; ++j) {
  673.             BYTE *bufptr,*bufend;
  674.             Printer_printf((j < 2) ? "\033*b%dV" : "\033*b%dW",
  675.                        pj_width);
  676.             bufend = pj_width + (bufptr = dstack[i][j]);
  677.             do {
  678.                 while (printer(*bufptr)) { }
  679.             } while (++bufptr < bufend);
  680.             }
  681.         }
  682.         }
  683.         Printer_printf("\033*r0B"); /* end raster graphics */
  684.         if (!keypressed())
  685.            if (debugflag != 600)
  686.           printer(12); /* form feed */
  687.            else
  688.           Printer_printf("\n\n");
  689.         farmemfree(pixels);
  690.         break;
  691.         }
  692.  
  693.     case 5:
  694.     case 6:     /***** PostScript Portrait & Landscape *****/
  695.         {
  696.         char convert[513];
  697.         if (!ColorPS)
  698.           for (i=0; i<256; ++i)
  699.                 if (Printer_Compress) {
  700.                     convert[i] = (int)((.3*255./63. * (double)dacbox[i][0])+
  701.                                         (.59*255./63. * (double)dacbox[i][1])+
  702.                                         (.11*255./63. * (double)dacbox[i][2]));
  703.                 } else
  704.                 {
  705.                     sprintf(&convert[2*i], "%02X",
  706.                               (int)((.3*255./63. * (double)dacbox[i][0])+
  707.                                     (.59*255./63. * (double)dacbox[i][1])+
  708.                                     (.11*255./63. * (double)dacbox[i][2])));
  709.                 }
  710.         i=0;
  711.         j=0;
  712.         for (y=0;((y<ydots)&&(!keypressed()));y++)
  713.         {   unsigned char bit8;
  714.         if (Printer_Compress) {
  715.             if (ColorPS) {
  716.             for (x=0;x<xdots;x++) {
  717.                 k=getcolor(x,y);
  718.                 rleputxelval((int)dacbox[k][0]<<2);
  719.             }
  720.             rleflush();
  721.             for (x=0;x<xdots;x++) {
  722.                 k=getcolor(x,y);
  723.                 rleputxelval((int)dacbox[k][1]<<2);
  724.             }
  725.             rleflush();
  726.             for (x=0;x<xdots;x++) {
  727.                 k=getcolor(x,y);
  728.                 rleputxelval((int)dacbox[k][2]<<2);
  729.             }
  730.             rleflush();
  731.              } else {
  732.             if (Printer_ColorXlat==-2 || Printer_ColorXlat==2) {
  733.                for (x=0;x<xdots;x++) {
  734.                   k=getcolor(x,y);
  735.                   if (x % 8 == 0) {
  736.                  if (x) rleputxelval((int)bit8);
  737.                                  if (k == 0) bit8 = 0; else bit8 = 1;
  738.                   }
  739.                   else
  740.                  bit8 = (bit8 << 1) + ((k==0) ? 0 : 1);
  741.                }
  742.                if (xdots % 8) bit8 <<= (8 - (xdots % 8));
  743.                        rleputxelval((int)bit8);
  744.                rleflush();
  745.             } else {
  746.                for (x=0;x<xdots;x++) {
  747.                   k=getcolor(x,y);
  748.                   rleputxelval((int)(unsigned char)convert[k]);
  749.                }
  750.                  rleflush();
  751.             }
  752.              }
  753.         } else
  754.         {
  755.             for (x=0;x<xdots;x++)
  756.             {
  757.             k=getcolor(x,y);
  758.             if (ColorPS)
  759.               {
  760.               sprintf(&buff[i], "%02X%02X%02X", dacbox[k][0]<<2,
  761.                                 dacbox[k][1]<<2,
  762.                                 dacbox[k][2]<<2);
  763.               i+=6;
  764.               }
  765.             else
  766.               {
  767.               k*=2;
  768.               buff[i++]=convert[k];
  769.               buff[i++]=convert[k+1];
  770.               }
  771.             if (i>=64)
  772.             {
  773.                 strcpy(&buff[i],"  ");
  774.                 Printer_printf("%s%s",buff,EndOfLine);
  775.                 i=0;
  776.                 j++;
  777.                 if (j>9)
  778.                 {
  779.                 j=0;
  780.                 Printer_printf(EndOfLine);
  781.                 }
  782.             }
  783.             }
  784.         }
  785.         }
  786.         if (Printer_Compress) {
  787.         rleputrest();
  788.         } else {
  789.         strcpy(&buff[i],"  ");
  790.         Printer_printf("%s%s",buff,EndOfLine);
  791.         i=0;
  792.         j++;
  793.         if (j>9)
  794.         {
  795.             j=0;
  796.             Printer_printf(EndOfLine);
  797.         }
  798.         }
  799.         if ( (EPSFileType > 0) && (EPSFileType <3) )
  800.         Printer_printf("%s%%%%Trailer%sEPSFsave restore%s",EndOfLine,
  801.             EndOfLine,EndOfLine);
  802.         else
  803. #ifndef XFRACT
  804.         PRINTER_PRINTF4("%sshowpage%s%c",EndOfLine,EndOfLine,4);
  805. #else
  806.         PRINTER_PRINTF3("%sshowpage%s",EndOfLine,EndOfLine);
  807. #endif
  808.         break;
  809.         }
  810.  
  811.     case 7: /* HP Plotter */
  812.         {
  813.         double parm1,parm2;
  814.         for (i=0;i<3;i++)
  815.         {
  816.           PRINTER_PRINTF4("%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_PRINTF3("%s;SC;PA 0,0;SP0;%s\0",EndOfLine,EndOfLine);
  852.         PRINTER_PRINTF2(";;SP 0;%s\0",EndOfLine);
  853.         break;
  854.         }
  855.     }
  856.  
  857.     if (Print_To_File > 0) fclose(PRFILE);
  858. #ifndef XFRACT
  859.     if ((LPTn==30)||(LPTn==31))
  860.     {
  861.     for (x=0;x<2000;x++);
  862.     outp((LPTn==30) ? 0x3FC : 0x2FC,0x00);
  863.     outp((LPTn==30) ? 0x3F9 : 0x2F9,0x00);
  864.     }
  865. #else
  866.     putstring(5,0,0,"Printing done\n");
  867. #endif
  868.     EXIT_OVLY;
  869. }
  870.  
  871.  
  872. static void _fastcall graphics_init(int ptrid,int res,char *EndOfLine)
  873. {
  874.     int i,j;
  875.  
  876.     switch (ptrid) {
  877.  
  878.     case 1:
  879.         print_title(ptrid,res,EndOfLine);
  880.         PRINTER_PRINTF2("\033*t%iR\033*r0A",res);/* HP           */
  881.         break;
  882.  
  883.     case 2:
  884.     case 3:
  885.         print_title(ptrid,res,EndOfLine);
  886.         Printer_printf("\033\063\030");/* IBM                   */
  887.         break;
  888.  
  889.     case 4: /****** PaintJet *****/
  890.         print_title(ptrid,res,EndOfLine);
  891.         pj_width = ydots;
  892.         if (res == 90) pj_width <<= 1;
  893.         PRINTER_PRINTF2("\033*r0B\033*t180R\033*r3U\033*r%dS\033*b0M\033*r0A",
  894.         pj_width);
  895.         pj_width >>= 3;
  896.         break;
  897.  
  898.     case 5:   /***** PostScript *****/
  899.     case 6:   /***** PostScript Landscape *****/
  900.         if (!((EPSFileType > 0) && (ptrid==5)))
  901.         PRINTER_PRINTF2("%%!PS-Adobe%s",EndOfLine);
  902.         if ((EPSFileType > 0) &&     /* Only needed if saving to .EPS */
  903.         (ptrid == 5))
  904.         {
  905.         PRINTER_PRINTF2("%%!PS-Adobe-1.0 EPSF-2.0%s",EndOfLine);
  906.  
  907.         if (EPSFileType==1)
  908.             i=xdots+78;
  909.         else
  910.             i=(int)((double)xdots * (72.0 / (double)res))+78;
  911.  
  912.         if (Printer_Titleblock==0)
  913.             {
  914.             if (EPSFileType==1) { j = ydots + 78; }
  915.             else { j = (int)(((double)ydots * (72.0 / (double)res) / (double)finalaspectratio)+78); }
  916.             }
  917.         else
  918.             {
  919.             if (EPSFileType==1) { j = ydots + 123; }
  920.             else { j = (int)(((double)ydots * (72.0 / (double)res))+123); }
  921.             }
  922.         PRINTER_PRINTF4("%%%%TemplateBox: 12 12 %d %d%s",i,j,EndOfLine);
  923.         PRINTER_PRINTF4("%%%%BoundingBox: 12 12 %d %d%s",i,j,EndOfLine);
  924.         PRINTER_PRINTF4("%%%%PrinterRect: 12 12 %d %d%s",i,j,EndOfLine);
  925.         PRINTER_PRINTF2("%%%%Creator: Fractint PostScript%s",EndOfLine);
  926.         Printer_printf("%%%%Title: A %s fractal - %s - Fractint EPSF Type %d%s",
  927.                        curfractalspecific->name[0]=='*' ?
  928.                        &curfractalspecific->name[1] :
  929.                        curfractalspecific->name,
  930.                        PrintName,
  931.                        EPSFileType,
  932.                        EndOfLine);
  933.         if (Printer_Titleblock==1)
  934.             PRINTER_PRINTF2("%%%%DocumentFonts: Helvetica%s",EndOfLine);
  935.         PRINTER_PRINTF2("%%%%EndComments%s",EndOfLine);
  936.         PRINTER_PRINTF2("/EPSFsave save def%s",EndOfLine);
  937.         PRINTER_PRINTF2("0 setgray 0 setlinecap 1 setlinewidth 0 setlinejoin%s",EndOfLine);
  938.         PRINTER_PRINTF2("10 setmiterlimit [] 0 setdash newpath%s",EndOfLine);
  939.         }
  940.  
  941.         /* Common code for all PostScript */
  942.         PRINTER_PRINTF2("/Tr {translate} def%s",EndOfLine);
  943.         PRINTER_PRINTF2("/Mv {moveto} def%s",EndOfLine);
  944.         PRINTER_PRINTF2("/D {dup} def%s",EndOfLine);
  945.         PRINTER_PRINTF2("/Rh {readhexstring} def%s",EndOfLine);
  946.         PRINTER_PRINTF2("/Cf {currentfile} def%s",EndOfLine);
  947.         PRINTER_PRINTF2("/Rs {readstring} def%s",EndOfLine);
  948.  
  949.         if (Printer_Compress) {
  950.         rleprolog(xdots,ydots);
  951.         } else 
  952.         {
  953.         PRINTER_PRINTF3("/picstr %d string def%s",
  954.             ColorPS?xdots*3:xdots,EndOfLine);
  955.         Printer_printf("/dopic { gsave %d %d 8 [%d 0 0 %d 0 %d]%s",
  956.                      xdots, ydots, xdots, -ydots, ydots,
  957.                      EndOfLine);
  958.         PRINTER_PRINTF2("{ Cf picstr Rh pop }%s", EndOfLine);
  959.         if (ColorPS)
  960.         {
  961.             PRINTER_PRINTF2(" false 3 colorimage grestore } def%s",
  962.              EndOfLine);
  963.         }
  964.         else
  965.         {
  966.             PRINTER_PRINTF2(" image grestore } def%s", EndOfLine);
  967.         }
  968.             }
  969.         if (Printer_Titleblock==1)
  970.         {
  971.         PRINTER_PRINTF2("/Helvetica findfont 8 scalefont setfont%s",EndOfLine);
  972.         if (ptrid==5) Printer_printf("30 60 Mv ");
  973.         else          Printer_printf("552 30 Mv 90 rotate ");
  974.         print_title(ptrid,res,EndOfLine);
  975.         if (ptrid==6) Printer_printf("-90 rotate ");
  976.         }
  977.  
  978.         if (EPSFileType != 1) /* Do not use on a WELL BEHAVED .EPS */
  979.           {
  980.           if ((ptrid == 5)&&(EPSFileType==2)&&
  981.           ((Printer_ColorXlat!=0)||(Printer_SetScreen!=0)))
  982.             PRINTER_PRINTF2("%%%%BeginFeature%s",EndOfLine);
  983.           if (ColorPS)
  984.         {
  985.         if (Printer_ColorXlat==1)
  986.             PRINTER_PRINTF2("{1 exch sub} D D D setcolortransfer%s",EndOfLine);
  987.         if (Printer_ColorXlat>1)
  988.             PRINTER_PRINTF4("{%d mul round %d div} D D D setcolortransfer%s",
  989.                        Printer_ColorXlat,Printer_ColorXlat,EndOfLine);
  990.         if (Printer_ColorXlat<-1)
  991.             PRINTER_PRINTF4("{%d mul round %d div 1 exch sub} D D D setcolortransfer",
  992.                        Printer_ColorXlat,Printer_ColorXlat,EndOfLine);
  993.  
  994.         if (Printer_SetScreen==1)
  995.             {
  996.             Printer_printf("%d %d {%s}%s",
  997.                        Printer_RFrequency,
  998.                        Printer_RAngle,
  999.                        HalfTone[Printer_RStyle],
  1000.                        EndOfLine);
  1001.             Printer_printf("%d %d {%s}%s",
  1002.                        Printer_GFrequency,
  1003.                        Printer_GAngle,
  1004.                        HalfTone[Printer_GStyle],
  1005.                        EndOfLine);
  1006.             Printer_printf("%d %d {%s}%s",
  1007.                        Printer_BFrequency,
  1008.                        Printer_BAngle,
  1009.                        HalfTone[Printer_BStyle],
  1010.                        EndOfLine);
  1011.             Printer_printf("%d %d {%s}%s",
  1012.                        Printer_SFrequency,
  1013.                        Printer_SAngle,
  1014.                        HalfTone[Printer_SStyle],
  1015.                        EndOfLine);
  1016.             PRINTER_PRINTF2("setcolorscreen%s", EndOfLine);
  1017.             }
  1018.             }
  1019.           else
  1020.           {
  1021.          if (Printer_ColorXlat==-2 || Printer_ColorXlat==2) {
  1022.             /* b&w case requires no mask building */
  1023.         }
  1024.         else {
  1025.            if (Printer_ColorXlat==1)
  1026.               PRINTER_PRINTF2("{1 exch sub} settransfer%s",EndOfLine);
  1027.            if (Printer_ColorXlat>1)
  1028.               PRINTER_PRINTF4("{%d mul round %d div} settransfer%s",
  1029.                       Printer_ColorXlat,Printer_ColorXlat,EndOfLine);
  1030.            if (Printer_ColorXlat<-1)
  1031.               PRINTER_PRINTF4("{%d mul round %d div 1 exch sub} settransfer",
  1032.                       Printer_ColorXlat,Printer_ColorXlat,EndOfLine);
  1033.         
  1034.            if (Printer_SetScreen==1)
  1035.               Printer_printf("%d %d {%s} setscreen%s",
  1036.                      Printer_SFrequency,
  1037.                      Printer_SAngle,
  1038.                      HalfTone[Printer_SStyle],
  1039.                      EndOfLine);
  1040.         }
  1041.           }
  1042.  
  1043.           if (ptrid == 5)
  1044.          {
  1045.             if ((EPSFileType==2)&&((Printer_ColorXlat!=0)||(Printer_SetScreen!=0)))
  1046.             PRINTER_PRINTF2("%%%%EndFeature%s",EndOfLine);
  1047.             if (res == 0)
  1048.             {
  1049.             PRINTER_PRINTF2("30 191.5 Tr 552 %4.1f",
  1050.                     (552.0*(double)finalaspectratio));
  1051.             }
  1052.             else
  1053.             {
  1054.             PRINTER_PRINTF4("30 %d Tr %f %f",
  1055.                      75 - ((Printer_Titleblock==1) ? 0 : 45),
  1056.                      ((double)xdots*(72.0/(double)res)),
  1057.                      ((double)xdots*(72.0/(double)res)*(double)finalaspectratio));
  1058.             }
  1059.           }
  1060.         else                 /* For Horizontal PostScript */
  1061.         {
  1062.             if (res == 0)
  1063.             {
  1064.             PRINTER_PRINTF2("582 30 Tr 90 rotate 732 %4.1f",
  1065.                     (732.0*(double)finalaspectratio));
  1066.             }
  1067.             else
  1068.             {
  1069.             PRINTER_PRINTF4("%d 30 Tr 90 rotate %f %f",
  1070.                      537 + ((Printer_Titleblock==1) ? 0 : 45),
  1071.                      ((double)xdots*(72.0/(double)res)),
  1072.                      ((double)xdots*(72.0/(double)res)*(double)finalaspectratio));
  1073.             }
  1074.         }
  1075.         PRINTER_PRINTF2(" scale%s",EndOfLine);
  1076.           }
  1077.  
  1078.         else if (ptrid == 5)       /* To be used on WELL-BEHAVED .EPS */
  1079.         Printer_printf("30 %d Tr %d %d scale%s",
  1080.                     75 - ((Printer_Titleblock==1) ? 0 : 45),
  1081.                     xdots,ydots,EndOfLine);
  1082.  
  1083.         PRINTER_PRINTF2("dopic%s",EndOfLine);
  1084.         break;
  1085.  
  1086.     case 7: /* HP Plotter */
  1087.         if (res<1) res=1;
  1088.         if (res>10) res=10;
  1089.         ci = (((double)xdots*((double)res-1.0))/2.0);
  1090.         ck = (((double)ydots*((double)res-1.0))/2.0);
  1091.         Printer_printf(";IN;SP0;SC%d,%d,%d,%d;%s\0",
  1092.         (int)(-ci),(int)((double)xdots+ci),
  1093.         (int)((double)ydots+ck),(int)(-ck),EndOfLine);
  1094.         break;
  1095.     }
  1096. }
  1097.  
  1098.  
  1099. static void _fastcall print_title(int ptrid,int res,char *EndOfLine)
  1100. {
  1101.     char buff[80];
  1102.     int postscript;
  1103.     if (Printer_Titleblock == 0)
  1104.     return;
  1105.     postscript = (ptrid == 5 || ptrid ==6);
  1106.     if (!postscript)
  1107.     Printer_printf(EndOfLine);
  1108.     else
  1109.     Printer_printf("(");
  1110.     Printer_printf((curfractalspecific->name[0]=='*')
  1111.              ? &curfractalspecific->name[1]
  1112.              : curfractalspecific->name);
  1113.     if (fractype == FORMULA || fractype == FFORMULA)
  1114.     Printer_printf(" %s",FormName);
  1115.     if (fractype == LSYSTEM)
  1116.     Printer_printf(" %s",LName);
  1117.     if (fractype == IFS || fractype == IFS3D)
  1118.     Printer_printf(" %s",IFSName);
  1119.     Printer_printf(" - %dx%d - %d DPI", xdots, ydots, res);
  1120.     if (!postscript)
  1121.     Printer_printf(EndOfLine);
  1122.     else {
  1123.     Printer_printf(") show%s",EndOfLine);
  1124.     if (ptrid==5) Printer_printf("30 50 moveto (");
  1125.     else          Printer_printf("-90 rotate 562 30 moveto 90 rotate (");
  1126.     }
  1127.     Printer_printf("Corners: Top-Left=%.16g/%.16g Bottom-Right=%.16g/%.16g",
  1128.            xxmin,yymax,xxmax,yymin);
  1129.     if (xx3rd != xxmin || yy3rd != yymin) {
  1130.     if (!postscript)
  1131.         Printer_printf("%s        ",EndOfLine);
  1132.     Printer_printf(" Bottom-Left=%4.4f/%4.4f",xx3rd,yy3rd);
  1133.     }
  1134.     if (!postscript)
  1135.     Printer_printf(EndOfLine);
  1136.     else {
  1137.     Printer_printf(") show%s",EndOfLine);
  1138.     if (ptrid==5) Printer_printf("30 40 moveto (");
  1139.     else          Printer_printf("-90 rotate 572 30 moveto 90 rotate (");
  1140.     }
  1141.     showtrig(buff);
  1142.     Printer_printf("Parameters: %4.4f/%4.4f/%4.4f/%4.4f %s",
  1143.            param[0],param[1],param[2],param[3],buff);
  1144.     if (!postscript)
  1145.     Printer_printf(EndOfLine);
  1146.     else
  1147.     Printer_printf(") show%s",EndOfLine);
  1148. }
  1149.  
  1150. /* This function prints a string to the the printer with BIOS calls. */
  1151.  
  1152. #ifndef XFRACT
  1153. static void Printer_printf(char far *fmt,...)
  1154. #else
  1155. static void Printer_printf(va_alist)
  1156. va_dcl
  1157. #endif
  1158. {
  1159. int i;
  1160. char s[500];
  1161. int x=0;
  1162. va_list arg;
  1163.  
  1164. #ifndef XFRACT
  1165. va_start(arg,fmt);
  1166. #else
  1167. char far *fmt;
  1168. va_start(arg);
  1169. fmt = va_arg(arg,char far *);
  1170. #endif
  1171.  
  1172. {
  1173.    /* copy far to near string */
  1174.    char fmt1[100];
  1175.    i=0;
  1176.    while(fmt[i]) {
  1177.      fmt1[i] = fmt[i];
  1178.      i++;
  1179.    }
  1180.    fmt1[i] = '\0';
  1181.    vsprintf(s,fmt1,arg);
  1182. }   
  1183.  
  1184. if (Print_To_File>0)    /* This is for printing to file */
  1185.     fprintf(PRFILE,"%s",s);
  1186. else            /* And this is for printing to printer */
  1187.     while (s[x])
  1188.     if (printer(s[x++])!=0)
  1189.         while (!keypressed()) { if (printer(s[x-1])==0) break; }
  1190. }
  1191.  
  1192. /* This function standardizes both _bios_printer and _bios_serialcom
  1193.  * in one function.  It takes its arguments and rearranges them and calls
  1194.  * the appropriate bios call.  If it then returns result !=0, there is a
  1195.  * problem with the printer.
  1196.  */
  1197. static int _fastcall printer(int c)
  1198. {
  1199.     if (Print_To_File>0) return ((fprintf(PRFILE,"%c",c))<1);
  1200. #ifndef XFRACT
  1201.     if (LPTn<9)  return (((_bios_printer(0,LPTn,c))+0x0010)&0x0010);
  1202.     if (LPTn<19) return ((_bios_serialcom(1,(LPTn-10),c))&0x9E00);
  1203.     if ((LPTn==20)||(LPTn==21))
  1204.     {
  1205.     int PS=0;
  1206.     while ((PS & 0xF8) != 0xD8)
  1207.         { PS = inp((LPTn==20) ? 0x379 : 0x279);
  1208.           if (keypressed()) return(1); }
  1209.     outp((LPTn==20) ? 0x37C : 0x27C,c);
  1210.     PS = inp((LPTn==20) ? 0x37A : 0x27A);
  1211.     outp((LPTn==20) ? 0x37A : 0x27A,(PS | 0x01));
  1212.     outp((LPTn==20) ? 0x37A : 0x27A,PS);
  1213.     return(0);
  1214.     }
  1215.     if ((LPTn==30)||(LPTn==31))
  1216.     {
  1217.     while (((inp((LPTn==30) ? 0x3FE : 0x2FE)&0x30)!=0x30) ||
  1218.            ((inp((LPTn==30) ? 0x3FD : 0x2FD)&0x60)!=0x60))
  1219.         { if (keypressed()) return (1); }
  1220.     outp((LPTn==30) ? 0x3F8 : 0x2F8,c);
  1221.     return(0);
  1222.     }
  1223. #endif
  1224.  
  1225.     /* MCP 7-7-91, If we made it down to here, we may as well error out. */
  1226.     return(-1);
  1227. }
  1228.  
  1229. #ifdef __BORLANDC__
  1230. #if(__BORLANDC__ > 2)
  1231.    #pragma warn +eff
  1232. #endif
  1233. #endif
  1234.  
  1235. static void printer_reset()
  1236. {
  1237. #ifndef XFRACT
  1238.     if (Print_To_File < 1)
  1239.     if (LPTn<9)      _bios_printer(1,LPTn,0);
  1240.     else if (LPTn<19) _bios_serialcom(3,(LPTn-10),0);
  1241. #endif
  1242. }
  1243.  
  1244.  
  1245. /** debug code for pj_ color table checkout
  1246. color_test()
  1247. {
  1248.    int x,y,color,i,j,xx,yy;
  1249.    int bw,cw,bh,ch;
  1250.    setvideomode(videoentry.videomodeax,
  1251.         videoentry.videomodebx,
  1252.         videoentry.videomodecx,
  1253.         videoentry.videomodedx);
  1254.    bw = xdots/25; cw = bw * 2 / 3;
  1255.    bh = ydots/10; ch = bh * 2 / 3;
  1256.    dacbox[0][0] = dacbox[0][1] = dacbox[0][2] = 60;
  1257.    if (debugflag == 902)
  1258.       dacbox[0][0] = dacbox[0][1] = dacbox[0][2] = 0;
  1259.    for (x = 0; x < 25; ++x)
  1260.       for (y = 0; y < 10; ++y) {
  1261.      if (x < 11) i = (32 - x) * 10 + y;
  1262.          else    i = (24 - x) * 10 + y;
  1263.      color = x * 10 + y + 1;
  1264.      dacbox[color][0] = dstack[0][0][i];
  1265.      dacbox[color][1] = dstack[0][1][i];
  1266.      dacbox[color][2] = dstack[0][2][i];
  1267.      for (i = 0; i < cw; ++i) {
  1268.         xx = x * bw + bw / 6 + i;
  1269.         yy = y * bh + bh / 6;
  1270.         for (j = 0; j < ch; ++j)
  1271.            putcolor(xx,yy++,color);
  1272.         }
  1273.      }
  1274.    spindac(0,1);
  1275.    getakey();
  1276. }
  1277. **/
  1278.  
  1279.  
  1280. /*
  1281.  * The following code for compressed PostScript is based on pnmtops.c.  It is
  1282.  * copyright (C) 1989 by Jef Poskanzer, and carries the following notice:
  1283.  * "Permission to use, copy, modify, and distribute this software and its
  1284.  *  documentation for any purpose and without fee is hereby granted, provided
  1285.  *  that the above copyright notice appear in all copies and that both that
  1286.  *  copyright notice and this permission notice appear in supporting
  1287.  *  documentation.  This software is provided "as is" without express or
  1288.  *  implied warranty."
  1289.  */
  1290.  
  1291.  
  1292. static rleprolog(x,y)
  1293. int x,y;
  1294. {
  1295.     itemsperline = 0;
  1296.     items = 0;
  1297.     bitspersample = 8;
  1298.     repeat = 1;
  1299.     rlebitsperitem = 0;
  1300.     rlebitshift = 0;
  1301.     count = 0;
  1302.  
  1303.     PRINTER_PRINTF2( "/rlestr1 1 string def%s", EndOfLine );
  1304.     PRINTER_PRINTF2( "/rdrlestr {%s", EndOfLine );    /* s -- nr */
  1305.     PRINTER_PRINTF2( "  /rlestr exch def%s", EndOfLine );    /* - */
  1306.     PRINTER_PRINTF2( "  Cf rlestr1 Rh pop%s", EndOfLine );  /* s1 */
  1307.     PRINTER_PRINTF2( "  0 get%s", EndOfLine );                     /* c */
  1308.     PRINTER_PRINTF2( "  D 127 le {%s", EndOfLine );         /* c */
  1309.     PRINTER_PRINTF2( "    Cf rlestr 0%s", EndOfLine );      /* c f s 0 */
  1310.     PRINTER_PRINTF2( "    4 3 roll%s", EndOfLine );                /* f s 0 c */
  1311.     PRINTER_PRINTF2( "    1 add  getinterval%s", EndOfLine );      /* f s */
  1312.     PRINTER_PRINTF2( "    Rh pop%s", EndOfLine );    /* s */
  1313.     PRINTER_PRINTF2( "    length%s", EndOfLine );                  /* nr */
  1314.     PRINTER_PRINTF2( "  } {%s", EndOfLine );                       /* c */
  1315.     PRINTER_PRINTF2( "    256 exch sub D%s", EndOfLine );     /* n n */
  1316.     PRINTER_PRINTF2( "    Cf rlestr1 Rh pop%s", EndOfLine );/* n n s1 */
  1317.     PRINTER_PRINTF2( "    0 get%s", EndOfLine );                   /* n n c */
  1318.     PRINTER_PRINTF2( "    exch 0 exch 1 exch 1 sub {%s", EndOfLine );           /* n c 0 1 n-1*/
  1319.     PRINTER_PRINTF2( "      rlestr exch 2 index put%s", EndOfLine );
  1320.     PRINTER_PRINTF2( "    } for%s", EndOfLine );                   /* n c */
  1321.     PRINTER_PRINTF2( "    pop%s", EndOfLine );                     /* nr */
  1322.     PRINTER_PRINTF2( "  } ifelse%s", EndOfLine );                  /* nr */
  1323.     PRINTER_PRINTF2( "} bind def%s", EndOfLine );
  1324.     PRINTER_PRINTF2( "/Rs {%s", EndOfLine );           /* s -- s */
  1325.     PRINTER_PRINTF2( "  D length 0 {%s", EndOfLine );         /* s l 0 */
  1326.     PRINTER_PRINTF2( "    3 copy exch%s", EndOfLine );          /* s l n s n l*/
  1327.     PRINTER_PRINTF2( "    1 index sub%s", EndOfLine );          /* s l n s n r*/
  1328.     PRINTER_PRINTF2( "    getinterval%s", EndOfLine );          /* s l n ss */
  1329.     PRINTER_PRINTF2( "    rdrlestr%s", EndOfLine );       /* s l n nr */
  1330.     PRINTER_PRINTF2( "    add%s", EndOfLine );                     /* s l n */
  1331.     PRINTER_PRINTF2( "    2 copy le { exit } if%s", EndOfLine );   /* s l n */
  1332.     PRINTER_PRINTF2( "  } loop%s", EndOfLine );                    /* s l l */
  1333.     PRINTER_PRINTF2( "  pop pop%s", EndOfLine );                   /* s */
  1334.     PRINTER_PRINTF2( "} bind def%s", EndOfLine );
  1335.     if (ColorPS) {
  1336.     PRINTER_PRINTF3( "/rpicstr %d string def%s", x,EndOfLine );
  1337.     PRINTER_PRINTF3( "/gpicstr %d string def%s", x,EndOfLine );
  1338.     PRINTER_PRINTF3( "/bpicstr %d string def%s", x,EndOfLine );
  1339.     } else {
  1340.     PRINTER_PRINTF3( "/picstr %d string def%s", x,EndOfLine );
  1341.     }
  1342.     PRINTER_PRINTF2( "/dopic {%s", EndOfLine);
  1343.     PRINTER_PRINTF2( "/gsave%s", EndOfLine);
  1344.     if (ColorPS) {
  1345.        PRINTER_PRINTF4( "%d %d 8%s", x, y, EndOfLine);
  1346.     } else { /* b&w */
  1347.        if (Printer_ColorXlat==-2) {
  1348.           PRINTER_PRINTF4( "%d %d true%s", x, y, EndOfLine);
  1349.        } else if (Printer_ColorXlat==2) {
  1350.           PRINTER_PRINTF4( "%d %d false%s", x, y, EndOfLine);
  1351.        } else {
  1352.       PRINTER_PRINTF4( "%d %d 8%s", x, y, EndOfLine);
  1353.        }
  1354.     }
  1355.     Printer_printf( "[%d 0 0 %d 0 %d]%s", x, -y, y, EndOfLine);
  1356.     if (ColorPS) {
  1357.     PRINTER_PRINTF2( "{rpicstr Rs}%s", EndOfLine);
  1358.     PRINTER_PRINTF2( "{gpicstr Rs}%s", EndOfLine);
  1359.     PRINTER_PRINTF2( "{bpicstr Rs}%s", EndOfLine);
  1360.     PRINTER_PRINTF2( "true 3 colorimage%s", EndOfLine);
  1361.     } else {
  1362.        if (Printer_ColorXlat==-2 || Printer_ColorXlat==2) {
  1363.       /* save file space and printing time (if scaling is right) */
  1364.       PRINTER_PRINTF2( "{picstr Rs} imagemask%s", EndOfLine);
  1365.        } else {
  1366.       PRINTER_PRINTF2( "{picstr Rs} image%s", EndOfLine);
  1367.        }
  1368.     }
  1369.     PRINTER_PRINTF2( "} def%s", EndOfLine);
  1370. }
  1371.  
  1372. static void
  1373. rleputbuffer()
  1374.     {
  1375.     int i;
  1376.  
  1377.     if ( repeat )
  1378.     {
  1379.     item = 256 - count;
  1380.     putitem();
  1381.     item = repeatitem;
  1382.     putitem();
  1383.     }
  1384.     else
  1385.     {
  1386.     item = count - 1;
  1387.     putitem();
  1388.     for ( i = 0; i < count; ++i )
  1389.         {
  1390.         item = itembuf[i];
  1391.         putitem();
  1392.         }
  1393.     }
  1394.     repeat = 1;
  1395.     count = 0;
  1396.     }
  1397.  
  1398. static void
  1399. rleputitem()
  1400.     {
  1401.     int i;
  1402.  
  1403.     if ( count == 128 )
  1404.     rleputbuffer();
  1405.  
  1406.     if ( repeat && count == 0 )
  1407.     { /* Still initializing a repeat buf. */
  1408.     itembuf[count] = repeatitem = rleitem;
  1409.     ++count;
  1410.     }
  1411.     else if ( repeat )
  1412.     { /* Repeating - watch for end of run. */
  1413.     if ( rleitem == repeatitem )
  1414.         { /* Run continues. */
  1415.         itembuf[count] = rleitem;
  1416.         ++count;
  1417.         }
  1418.     else
  1419.         { /* Run ended - is it long enough to dump? */
  1420.         if ( count > 2 )
  1421.         { /* Yes, dump a repeat-mode buffer and start a new one. */
  1422.         rleputbuffer();
  1423.         itembuf[count] = repeatitem = rleitem;
  1424.         ++count;
  1425.         }
  1426.         else
  1427.         { /* Not long enough - convert to non-repeat mode. */
  1428.         repeat = 0;
  1429.         itembuf[count] = repeatitem = rleitem;
  1430.         ++count;
  1431.         repeatcount = 1;
  1432.         }
  1433.         }
  1434.     }
  1435.     else
  1436.     { /* Not repeating - watch for a run worth repeating. */
  1437.     if ( rleitem == repeatitem )
  1438.         { /* Possible run continues. */
  1439.         ++repeatcount;
  1440.         if ( repeatcount > 3 )
  1441.         { /* Long enough - dump non-repeat part and start repeat. */
  1442.         count = count - ( repeatcount - 1 );
  1443.         rleputbuffer();
  1444.         count = repeatcount;
  1445.         for ( i = 0; i < count; ++i )
  1446.             itembuf[i] = rleitem;
  1447.         }
  1448.         else
  1449.         { /* Not long enough yet - continue as non-repeat buf. */
  1450.         itembuf[count] = rleitem;
  1451.         ++count;
  1452.         }
  1453.         }
  1454.     else
  1455.         { /* Broken run. */
  1456.         itembuf[count] = repeatitem = rleitem;
  1457.         ++count;
  1458.         repeatcount = 1;
  1459.         }
  1460.     }
  1461.  
  1462.     rleitem = 0;
  1463.     rlebitsperitem = 0;
  1464.     }
  1465.  
  1466. static void
  1467. putitem()
  1468.     {
  1469.     char* hexits = "0123456789abcdef";
  1470.  
  1471.     if ( itemsperline == 30 )
  1472.         {
  1473.     Printer_printf("%s",EndOfLine);
  1474.         itemsperline = 0;
  1475.         }
  1476.     Printer_printf("%c%c", hexits[item >> 4], hexits[item & 15] );
  1477.     ++itemsperline;
  1478.     ++items;
  1479.     item = 0;
  1480.     bitsperitem = 0;
  1481.     bitshift = 8 - bitspersample;
  1482.     }
  1483.  
  1484. static void
  1485. rleputxelval( xv )
  1486.     int xv;
  1487.     {
  1488.     if ( rlebitsperitem == 8 )
  1489.     rleputitem();
  1490.     rleitem += xv<<bitshift;
  1491.     rlebitsperitem += bitspersample;
  1492.     rlebitshift -= bitspersample;
  1493.     }
  1494.  
  1495. static void
  1496. rleflush()
  1497.     {
  1498.     if ( rlebitsperitem > 0 )
  1499.     rleputitem();
  1500.     if ( count > 0 )
  1501.     rleputbuffer();
  1502.     }
  1503.  
  1504. static void
  1505. rleputrest()
  1506.     {
  1507.     rleflush();
  1508.     Printer_printf( "%s",EndOfLine );
  1509.     Printer_printf( "grestore%s",EndOfLine );
  1510.     }
  1511.