home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TIFF / TACS40.ZIP / TIFFW421.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-09  |  27.6 KB  |  1,025 lines

  1. /***************************************************************************\
  2. *
  3. * PVCS Software Configuration Management Control Information
  4. *
  5. * TIFFW421.C
  6. *
  7. * $Revision:   1.3  $
  8. *
  9. * $Date:   19 Apr 1988 11:06:24  $
  10. *
  11. * $Author:   BOBG  $
  12. *
  13. * $Logfile:   F:/PUBLIC/421/WORK/SRC/TIFFW421.C_V  $
  14. *
  15. * $Log:   F:/PUBLIC/421/WORK/SRC/TIFFW421.C_V  $
  16. *    
  17. *       Rev 1.3   19 Apr 1988 11:06:24   BOBG
  18. *    revise PostScript header and trailer
  19. *    
  20. *       Rev 1.2   15 Apr 1988 17:20:24   BOBG
  21. *    clean up PostScript
  22. *    
  23. *       Rev 1.1   06 Apr 1988 10:10:48   BOBG
  24. *    print options revised
  25. *    
  26. *       Rev 1.0   20 Mar 1988 18:14:40   tomh
  27. *    Initial revision.
  28. *
  29. \***************************************************************************/
  30.  
  31.  
  32. /* #include "windows.h" */
  33. /* #include "gawb.h" */
  34. #include "tiffint.h"
  35. /* #include "extfuncs.h" */
  36. #include "prints.h"
  37. #include "wintypes.h"
  38. #include <fcntl.h>
  39. /* #include <stdlib.h> */
  40. #include <stdio.h> 
  41.  
  42. #undef DEBUG
  43. #undef DBUG
  44. #ifdef DBUG
  45. /* #include <stdio.h> */
  46. #endif
  47.  
  48. #define QCBUFFSZ 2048
  49.  
  50. #define L_SHORT 2
  51. #define L_LONG    4
  52. #define L_RATIO 8
  53.  
  54. #define THRESHOLDING_TAG    0x107
  55.  
  56. #define FARFMTLINE "%Fs\r\n"
  57.  
  58. /* Standard C library routines */
  59.  
  60. int sprintf ();
  61. int open ();
  62. int close ();
  63. int read ();
  64. int write ();
  65. long lseek (int, long, int);
  66. int atoi ();
  67.  
  68. /* externals from the tiff library */
  69. extern int write_tag();
  70. extern int write_image();
  71. extern int tlpWrite();
  72. extern void close_create();
  73. extern void time();
  74.  
  75. /* statics defined here */
  76. static int B_RS232File = 1;    /* 0=big or 1=small eps file size */
  77. static PRINTER *pPrintS = NULL;
  78.         
  79. static char pathName[64];
  80. static int sizePSStrings;
  81. /* static int nStringEntries; */
  82.  
  83. /* forward references */
  84. BOOL InsertPSFile(PSTR, int);
  85. BOOL LoadPSStrings();
  86. int SetupPSStrings();
  87. void NCpyFar();
  88. int GetLine();
  89. void JumpLFCR();
  90. BOOL IsLFCR();
  91. void InitPSStrings();
  92. long converttops();
  93. void write_ps_trailer();
  94. void write_ps_head();
  95.  
  96. /* library exports */
  97. int TLFileSetup();
  98. int TLImageWrite();
  99. void TLCloseOutput();
  100. int TLSetupPSStrings();
  101.  
  102. struct ps {
  103. char far * BL;          /*   */
  104. char far * SHOWPAGE;    /* showpage */
  105. char far * GSAVE;       /* gsave  */
  106. char far * GRESTORE;    /* grestore  */
  107. char far * ADD;         /* add */
  108. char far * SUB;         /* sub */
  109. char far * NEG;         /* neg */
  110. char far * EXCH;        /* exch */
  111. char far * PSTYPE;      /* %%!PS-Adobe-2.0 EPSF-1.2 */
  112. char far * BOUNDER;     /* %%%%BoundingBox: %d %d %ld %ld */
  113. char far * CREATE;      /* %%Creator: Publish Pac 2.0  */
  114. char far * TITLE;        /* %%%%Title: %s */
  115. char far * CRDTE;       /* %%%%CreationDate: %s */
  116. char far * ENDCOM;      /* %%EndComments */
  117. char far * TRAILER;     /* %%Trailer */
  118. char far * PIXELHEIGHT; /* /height %d def */
  119. char far * PIXELWIDTH;  /* /width %d def */
  120. char far * NEGHEIGHT;   /* /negheight %d neg def */
  121. char far * DOT;         /* /xdot 72 %ld div def /ydot 72 %ld div def */
  122. char far * GREY_LEVEL;  /* /grey %d def */
  123. char far * THEIMAGE;     /* /theimage */
  124. char far * ASC_IMG_DEF; /* { width height grey [width 0 0 negheight 0 height] 
  125.                        {currentfile picstr readhexstring pop} image} def */
  126. char far * BIN_IMG_DEF; /* { width height grey [width 0 0 negheight 0 height]
  127.                        {currentfile picstr readstring pop} image} def */
  128. char far * SCALE;       /* width xdot mul %Fs height ydot mul scale */
  129. char far * PICSTRING;   /* /picstr %d string def */
  130. char far * IMAGE_CALL;  /* theimage */
  131. char far * CONCATPROC;  /* /concatprocs { /proc2 exch cvlit def /proc1 exch cvlit def */
  132. char far * CONCAT1;     /* /newproc proc1 length proc2 length add array def */
  133. char far * CONCAT2;     /* newproc 0 proc1 putinterval \n\r newproc proc1 length proc2 putinterval */
  134. char far * CONCAT3;     /* newproc cvx } def */
  135. char far * EXCHV;       /* { 1 exch sub } currenttransfer concatprocs settransfer */
  136. char far * NEGATIV;     /* currenttransfer { 1 exch sub } concatprocs settransfer */
  137. char far * COMPRESSV;   /* { 84 mul 8 add 100 div } currenttransfer concatprocs settransfer */
  138. char far * LIGHTENV;    /* { 90 mul 10 add 100 div } currenttransfer concatprocs settransfer */
  139. char far * DARKENV;     /* { 9 mul 10 div } currenttransfer concatprocs settransfer */
  140. char far * HALFTONE;    /* %d %d { dup mul exch dup mul add 1 %Fs sub } setscreen */
  141. char far * HORZLINE;    /* %d %d { exch pop abs sqrt 2 mul 1 %Fs sub } setscreen */
  142. char far * WAVYLINE;    /* %d %d { exch 180 mul sin .2 mul add 180 mul 4 mul sin %Fs } setscreen */
  143. char far * PAPERCENTER; /* /xpapercntr 306 def  /ypapercntr 396 def */
  144. char far * TRANSLATE;   /* xpapercntr width xdot mul 2 div %Fs  ypapercntr height ydot mul 2 div sub translate */
  145. char far * BLACKNEG;    /* gsave newpath 0 0 moveto 8.5 72 mul 0 rlineto 0 11 72 mul rlineto 8.5 72 mul neg 0 rlineto closepath 
  146.                        0 setgray fill grestore */
  147. char far * LINEWIDTH;   /* %d %d div setlinewidth */
  148. char far * BOXRULE;     /* newpath 0 0 moveto width xdot %Fs mul 0 rlineto 0 height ydot mul rlineto width xdot mul neg %Fs 0 rlineto 
  149.                        closepath stroke */
  150. char far * DOCFONT_HELVB; /* %%DocumentFonts: Helvetica-Bold */
  151. } PS;
  152.  
  153. /* LPSTR   lpPSStrBuf = (LPSTR)NULL; */
  154. /* HANDLE hPSStrBuf = NULL; */
  155.  
  156.  
  157. /* file_setup ***************************************************
  158.     passed parameter fd = output file handle
  159.                type = type of output
  160.                 FF_EPSF Encapsulated PostScript
  161.                 FF_TIFF Tiff File
  162.            image_att = Image Attribute structure
  163.            pPrinterSettings = PrinterSettings struct
  164.                       lpBuf = application-owned storage for PostScript strings
  165.                     sizeBuf = size of Postscript strings buffer
  166.     return parameter
  167.             -1 = error, otherwise the file handle
  168. *****************************************************************/
  169. int TLFileSetup(fd,type,image_att, pPrinterSettings, lpBuf, sizeBuf)
  170. HANDLE fd;
  171. int type;
  172. struct IMG_ATT *image_att;
  173. PRINTER *pPrinterSettings;
  174. LPSTR lpBuf;
  175. int sizeBuf;
  176. {
  177.     int rv;
  178.     int res;
  179.     long wpt;
  180.     long lpt;
  181.  
  182.     /* cast the file handle as a integer */
  183.     fd = (int) fd;
  184.  
  185.     pPrintS = pPrinterSettings;
  186.  
  187.     if(type == FF_EPSF) {
  188.                      /* make sure buffer is adequate */
  189.         if (lpBuf==(LPSTR)NULL || sizeBuf<sizePSStrings) {
  190.              /* close (fd); */
  191.             return (-1);
  192.         }
  193.         /* compute the bounding box */
  194.  
  195.         wpt = converttops(image_att->imwidth,&(image_att->xresolution));
  196.         lpt = converttops(image_att->imlength,&(image_att->yresolution));
  197.  
  198.                     /* load PostScript strings */
  199.         if (! LoadPSStrings(lpBuf)) {
  200.             /* close (fd); */
  201.             return (-1); 
  202.         }
  203.                 /* now write the postscript header to the file */
  204.         write_ps_head(fd,(long)image_att->imwidth,
  205.             (long)image_att->imlength,
  206.             (long)wpt,(long)lpt,
  207.             image_att->bits_sample * image_att->sample_pix,
  208.             &(image_att->path[0]),
  209.             &(image_att->xresolution),
  210.             &(image_att->yresolution),
  211.             image_att->photointerp);
  212.  
  213.     } else {
  214.         /* create a tiff file */
  215.         res = image_att->resolution;
  216.  
  217.         if((rv = write_tag(fd,res,TAG_IMWIDTH,
  218.             (LPSTR)&image_att->imwidth,L_SHORT)) == NULL) {
  219.             /* close(fd); */
  220.             return(-1);
  221.         }
  222.  
  223.         if((rv = write_tag(fd,res,TAG_IMLENGTH,
  224.             (LPSTR) &image_att->imlength,L_SHORT)) == NULL) {
  225.             /* close(fd); */
  226.             return(-1);
  227.         }
  228.  
  229.         if((rv=write_tag(fd,res,TAG_BITSPIX,
  230.             (LPSTR)&image_att->bits_sample,L_SHORT)) == NULL) {
  231.             /* close(fd); */
  232.             return(-1);
  233.         }
  234.  
  235.         if((rv=write_tag(fd,res,TAG_COMPRESS,
  236.             (LPSTR)&image_att->compress,L_SHORT)) == NULL ) {
  237.             /* close(fd); */
  238.             return(-1);
  239.         }
  240.  
  241.         if((rv=write_tag(fd,res,TAG_PHOTOINTERP,
  242.             (LPSTR)&image_att->photointerp,L_SHORT)) == NULL ) {
  243.             /* close(fd); */
  244.             return(-1);
  245.         }
  246.  
  247.         if((rv=write_tag(fd,res,TAG_FILLORD,
  248.             (LPSTR)&image_att->fillorder,L_SHORT)) == NULL ) {
  249.             /* close(fd); */
  250.             return(-1);
  251.         }
  252.  
  253.         if((rv=write_tag(fd,res,TAG_ORIENT,
  254.             (LPSTR)&image_att->orient,L_SHORT)) == NULL) {
  255.             /* close(fd); */
  256.             return(-1);
  257.         }
  258.  
  259.         if((rv=write_tag(fd,res,TAG_SAMPIX,
  260.             (LPSTR)&image_att->sample_pix,L_SHORT)) == NULL) {
  261.             /* close(fd); */
  262.             return(-1);
  263.         }
  264.  
  265.         if((rv=write_tag(fd,res,TAG_ROW_STRIP,
  266.             (LPSTR)&image_att->rows_strip,L_LONG)) == NULL) {
  267.             /* close(fd); */
  268.             return(-1);
  269.         }
  270.  
  271.         if((rv=write_tag(fd,res,TAG_MINSAMP,
  272.             (LPSTR)&image_att->min_sample,L_SHORT)) == NULL) {
  273.             /* close(fd); */
  274.             return(-1);
  275.         }
  276.  
  277.         if((rv=write_tag(fd,res,TAG_MAXSAMP,
  278.             (LPSTR)&image_att->max_sample,L_SHORT)) == NULL) {
  279.             /* close(fd); */
  280.             return(-1);
  281.         }
  282.  
  283.         if((rv=write_tag(fd,res,TAG_XRES,
  284.             (LPSTR)&image_att->xresolution,L_RATIO)) == NULL) {
  285.             /* close(fd); */
  286.             return(-1);
  287.         }
  288.  
  289.         if((rv=write_tag(fd,res,TAG_YRES,
  290.             (LPSTR)&image_att->yresolution,L_RATIO)) == NULL) {
  291.             /* close(fd); */
  292.             return(-1);
  293.         }
  294.  
  295.         if((rv = write_tag(fd,res,TAG_PLANAR,
  296.             (LPSTR)&image_att->planarconfig,L_SHORT)) == NULL) {
  297.             /* close(fd); */
  298.             return(-1);
  299.         }
  300.  
  301.         if(image_att->sample_pix * image_att->bits_sample > 1) {
  302.  
  303.             if((rv = write_tag(fd,res,TAG_GREYRES,
  304.                 (LPSTR)&image_att->grey_res_unit,
  305.                     L_SHORT)) == NULL)  {
  306.                 /* close(fd); */
  307.                 return(-1);
  308.         }
  309.  
  310.             if((rv = write_tag(fd,res,TAG_GREYCRV,
  311.                     (LPSTR)image_att->grey_curve,
  312.                     L_SHORT << image_att->bits_sample)) == NULL) {
  313.                 /* close(fd); */
  314.                 return(-1);
  315.             }
  316.         } else if( image_att->threshhold == 2) {
  317.             /* here is where info to write out cell width and */
  318.             /* height should go */
  319.             /* called only when a 1/2 tone image is output */
  320.  
  321.             /* default to 651 halftone pattern if one not */
  322.             /* defined */
  323.  
  324.             if(image_att->cell_length == 0)
  325.                 image_att->cell_length = 4;
  326.             if(image_att->cell_width == 0) 
  327.                 image_att->cell_width = 8;
  328.  
  329.             if((rv = write_tag(fd,res,THRESHOLDING_TAG,
  330.                 (LPSTR)&image_att->threshhold,
  331.                     L_SHORT)) == NULL) {
  332.                 /* close(fd); */
  333.                 return(-1);
  334.             }
  335.  
  336.             if((rv = write_tag(fd,res,TAG_CELL_LENGTH,
  337.                 (LPSTR)&image_att->cell_length,
  338.                     L_SHORT)) == NULL)  {
  339.                 /* close(fd); */
  340.                 return(-1);
  341.             }
  342.             if((rv = write_tag(fd,res,TAG_CELL_WIDTH,
  343.                 (LPSTR)&image_att->cell_width,
  344.                     L_SHORT)) == NULL)  {
  345.                 /* close(fd); */
  346.                 return(-1);
  347.             }
  348.         } else {   /* this is a line art scan */
  349.             if((rv = write_tag(fd,res,THRESHOLDING_TAG,
  350.                 (LPSTR)&image_att->threshhold,
  351.                     L_SHORT)) == NULL) {
  352.                 /* close(fd); */
  353.                 return(-1);
  354.             }
  355.         }
  356.     }
  357.     return(fd);
  358. }
  359.  
  360. long converttops(dots,ratpt)
  361. unsigned short dots;
  362. RATIO *ratpt;
  363. {
  364.  
  365.     long ldots;
  366.     long factor;
  367.  
  368.     factor = 72L << 14;        /* multiply points per inch by 16k */
  369.  
  370.     ldots = ratpt->upper / ratpt->lower;
  371.  
  372.     ldots = factor / ldots;
  373.     ldots *= (long) dots;
  374.  
  375.     ldots >>= 14;
  376.  
  377.     return ldots;
  378. }
  379.  
  380. void
  381. write_ps_head(fd,dwidth,dlength,pwidth,plength,greybit,fname,xptr,yptr,photointerp)
  382. int fd;
  383. long dwidth;
  384. long dlength;
  385. long pwidth;
  386. long plength;
  387. short greybit;
  388. char *fname;
  389. RATIO *xptr;
  390. RATIO *yptr;
  391. short photointerp;
  392. {
  393.  
  394.     int num, denom;
  395.     long ltime;
  396.     struct tm *tmst;
  397.     struct tm *localtime();
  398.     char *asctime();
  399.     int count;
  400.     char obuf[145];
  401.     char tbuf[145];
  402. /*    int rotToVert = 0; */
  403.  
  404.  
  405.     count = sprintf(obuf, FARFMTLINE, PS.PSTYPE);
  406.     write(fd, obuf, count);
  407.  
  408.     sprintf(tbuf, FARFMTLINE, PS.BOUNDER);
  409.     count = sprintf(obuf,tbuf,0,0,pwidth,plength);
  410.     write(fd,obuf,count);    /* BOUNDING BOX */
  411.  
  412.     count = sprintf(obuf, FARFMTLINE, PS.CREATE);
  413.     write(fd,obuf,count);  /* CREATOR */
  414.  
  415.     sprintf(tbuf, FARFMTLINE, PS.TITLE);
  416.     count = sprintf(obuf, tbuf, fname);
  417.     write(fd,obuf,count);  /* TITLE */
  418.  
  419.     time(<ime);    /* get the time */
  420.     tmst = localtime(<ime);
  421.     sprintf(tbuf, FARFMTLINE, PS.CRDTE);
  422.     count = sprintf(obuf,tbuf,asctime(tmst)); /* creation time */
  423.     write(fd,obuf,count);
  424.  
  425.     if (pPrintS && pPrintS->formatQCBar) {    /* yes there will be a QC Bar */
  426.         count = sprintf(obuf, FARFMTLINE, PS.DOCFONT_HELVB);
  427.         write(fd, obuf, count);
  428.     }
  429.     count = sprintf(obuf, FARFMTLINE, PS.ENDCOM);
  430.     write(fd, obuf, count);
  431.  
  432.     count = sprintf (obuf, FARFMTLINE, PS.GSAVE);
  433.     write (fd, obuf, count);
  434.  
  435.     count = sprintf (obuf, FARFMTLINE, PS.CONCATPROC);
  436.     write (fd, obuf, count);
  437.     count = sprintf (obuf, FARFMTLINE, PS.CONCAT1);
  438.     write (fd, obuf, count);
  439.     count = sprintf (obuf, FARFMTLINE, PS.CONCAT2);
  440.     write (fd, obuf, count);
  441.     count = sprintf (obuf, FARFMTLINE, PS.CONCAT3);
  442.     write (fd, obuf, count);
  443.  
  444.     sprintf(tbuf, FARFMTLINE, PS.GREY_LEVEL);
  445.     count = sprintf(obuf, tbuf, greybit);
  446.     write(fd,obuf,count);
  447.  
  448.     sprintf(tbuf, FARFMTLINE, PS.PIXELHEIGHT);
  449.     count = sprintf(obuf, tbuf, dlength  );
  450.     write(fd,obuf,count);
  451.  
  452.     sprintf(tbuf, FARFMTLINE, PS.PIXELWIDTH);
  453.     count = sprintf(obuf, tbuf, dwidth);
  454.     write(fd,obuf,count);
  455.  
  456.     sprintf(tbuf, FARFMTLINE, PS.NEGHEIGHT);
  457.     count = sprintf(obuf, tbuf, dlength);
  458.     write(fd,obuf,count);
  459.  
  460.     sprintf(tbuf, FARFMTLINE, PS.DOT);
  461.     count = sprintf(obuf, tbuf, xptr->upper, yptr->upper);
  462.     write(fd,obuf,count);
  463.  
  464.     sprintf(tbuf, FARFMTLINE, PS.PICSTRING);
  465.     count = sprintf(obuf, tbuf, (((dwidth * greybit) +7) / 8));
  466.     write(fd,obuf,count);
  467.  
  468.     count = sprintf(obuf, FARFMTLINE, PS.PAPERCENTER);
  469.     write(fd, obuf, count);
  470.  
  471.  
  472.                              /* if there is a valid printer settings pointer */
  473.                             /* halftoning, polarity, transfer are specified */
  474.     if (pPrintS != (PRINTER *)NULL) {
  475.  
  476.         if (pPrintS->imageData == ID_DATAASCII)
  477.             B_RS232File = 0;
  478.         else
  479.             B_RS232File = 1;
  480.                                             /* negPrintPolarity? */
  481.         if((pPrintS->outptPolarity == ID_NEGATIVEPOL) || 
  482.                               (pPrintS->outptPolarity == ID_FLOPFILMNEG)) {
  483.             count = sprintf(obuf, FARFMTLINE, PS.BLACKNEG);
  484.             write(fd, obuf, count);
  485.         }            
  486.  
  487.         if (pPrintS->formatQCBar)    /* yes do a QC Bar */
  488.                             /* dont fail because of InsertQCBar error */
  489.             if (pPrintS->outptPolarity == ID_FLOPFILMNEG ||
  490.                 pPrintS->outptPolarity == ID_POSPAPERPLATE)
  491.                 InsertPSFile("QCBARPN.PS", fd);
  492.             else
  493.                 InsertPSFile("QCBAROPQ.PS", fd);
  494.  
  495.                             /*write the appropriate setscreen instructn line*/
  496.         count = 0;
  497.         switch (pPrintS->screenPatt) {
  498.             case ID_SCRNHALFTONE :
  499.             case ID_SCRNMEZZOTINT :
  500.                 sprintf(tbuf, FARFMTLINE, PS.HALFTONE);
  501.                 count = sprintf(obuf, tbuf, pPrintS->screenFreq,
  502.                          pPrintS->screenAngl,
  503.                          (pPrintS->outptPolarity==ID_POSITIVEPOL ||
  504.                           pPrintS->outptPolarity==ID_POSPAPERREPRO ||
  505.                           pPrintS->outptPolarity==ID_POSPAPERPLATE) ? 
  506.                                                             PS.EXCH : PS.BL );
  507. #ifdef DBUG
  508. fprintf(stdprn, "halftone screen function %d chars,\n\t%s\n",count ,obuf);
  509. #endif
  510.                 break;
  511.             case ID_SCRNLINE :
  512.                 sprintf(tbuf, FARFMTLINE, PS.HORZLINE);
  513.                 count = sprintf(obuf, tbuf, pPrintS->screenFreq,
  514.                         pPrintS->screenAngl+pPrintS->deviceRot/*+rotToVert*/,
  515.                          (pPrintS->outptPolarity==ID_POSITIVEPOL ||
  516.                           pPrintS->outptPolarity==ID_POSPAPERREPRO ||
  517.                           pPrintS->outptPolarity==ID_POSPAPERPLATE) ? 
  518.                                                             PS.EXCH : PS.BL );
  519.                 break;
  520.             case ID_SCRNWAVYLINE :
  521.                 sprintf(tbuf, FARFMTLINE, PS.WAVYLINE);
  522.                 count = sprintf(obuf, tbuf, (pPrintS->screenFreq + 2) / 4 ,
  523.                         pPrintS->screenAngl,
  524.                          (pPrintS->outptPolarity==ID_POSITIVEPOL ||
  525.                           pPrintS->outptPolarity==ID_POSPAPERREPRO ||
  526.                           pPrintS->outptPolarity==ID_POSPAPERPLATE) ? 
  527.                                                             PS.EXCH : PS.BL );
  528.                 break;
  529. #ifdef COMMENTOUT
  530. *            case ID_SCRNDASHES :
  531. *                count = sprintf(obuf, DASHES1,     
  532. *                        (pPrintS->screenFreq + 2) / 3,
  533. *                        pPrintS->screenAngl,
  534. *                        pPrintS->outptPolarity==ID_POSITIVEPOL ? PS.BL : PS.NEG );
  535. *                write (fd, obuf, count);                        
  536. *                count = sprintf(obuf, DASHES2);
  537. *                write (fd, obuf, count);                        
  538. *                count = sprintf(obuf, DASHES3,     
  539. *                        pPrintS->outptPolarity==ID_POSITIVEPOL ? PS.BL : PS.NEG );
  540. *                write (fd, obuf, count);
  541. *                count = sprintf(obuf, DASHES4);     
  542. *                write (fd, obuf, count);
  543. *                count = sprintf(obuf, DASHES5,     
  544. *                        pPrintS->outptPolarity==ID_POSITIVEPOL ? PS.BL : PS.NEG );
  545. *                write (fd, obuf, count);    
  546. *                count = sprintf(obuf, DASHES6);     
  547. *
  548. *            case ID_SCRNELLIP.DOT :
  549. *                count = sprintf(obuf, ELLIPDOT, pPrintS->screenFreq,
  550. *                         pPrintS->screenAngl,
  551. *                         pPrintS->outptPolarity==ID_POSITIVEPOL ? PS.EXCH : PS.BL );
  552. *                break;
  553. #endif
  554.             default :
  555.                 break;
  556.         }
  557.         if (count) {
  558.             write (fd, obuf, count);        /* setscreen instruction line */
  559. #ifdef DBUG
  560. fprintf(stdprn, "whatever screen function %d chars,\n\t%s\n",count ,obuf);
  561. #endif
  562.         }
  563.  
  564.  
  565.         if((pPrintS->outptPolarity == ID_NEGATIVEPOL) || 
  566.                               (pPrintS->outptPolarity == ID_FLOPFILMNEG)) {
  567.             count = sprintf(obuf, FARFMTLINE, PS.NEGATIV);
  568.             write (fd, obuf, count);        /* inverse after transfer */
  569.         }
  570.  
  571. /*         if (pPrintS->formatCropM) {
  572. /*            count = sprintf(obuf, PS.LINEWIDTH, 1, 2);
  573. /*            write(fd, obuf, count);
  574. /*            write(fd, CROPM, strlen(CROPM));
  575. /*        } */
  576.  
  577.         if (pPrintS->formatBox) {
  578.             if (pPrintS->formatBox == ID_BOXMED) {
  579.                 num = 2; denom = 1;
  580.             } else
  581.                 if (pPrintS->formatBox == ID_BOXTHICK) {
  582.                     num = 7; denom = 2;
  583.                 } else {
  584.                     num = 2; denom = 3;
  585.                 }
  586.                     
  587.             sprintf(tbuf, FARFMTLINE, PS.LINEWIDTH);
  588.             count = sprintf(obuf, tbuf, num, denom);
  589.             write(fd, obuf, count);
  590.         }
  591.         
  592. #ifdef DBUG
  593. fprintf (stdprn, "just checking validity before filing GSAVE\n\t%Fs\n", PS.GSAVE);
  594. #endif
  595.         count = sprintf(obuf, FARFMTLINE, PS.GSAVE);
  596.         write(fd, obuf, count);
  597.  
  598.         sprintf(tbuf, FARFMTLINE, PS.TRANSLATE);
  599.         count = sprintf(obuf, tbuf, 
  600.                     pPrintS->outptPolarity==ID_FLOPFILMNEG ? PS.ADD : PS.SUB );
  601.         write(fd, obuf, count);
  602.  
  603.         sprintf(tbuf, FARFMTLINE, PS.SCALE);
  604.         count = sprintf(obuf, tbuf,
  605.                     pPrintS->outptPolarity==ID_FLOPFILMNEG ? PS.NEG : PS.BL );
  606.         write(fd, obuf, count);
  607.  
  608.                             /*write the appropriate settransfer instrn line */
  609.         switch (pPrintS->grayExpan) {
  610.             case ID_NORMALGRAY :
  611.                 break;
  612.             case ID_COMPRESSGRAY :
  613.                 count = sprintf(obuf, FARFMTLINE, PS.COMPRESSV);
  614.                 write(fd, obuf, count);
  615.                 break;
  616.             case ID_LIGHTENGRAY :
  617.                 count = sprintf(obuf, FARFMTLINE, PS.LIGHTENV);
  618.                 write(fd, obuf, count);
  619.                 break;
  620.             case ID_DARKENGRAY :
  621.                 count = sprintf(obuf, FARFMTLINE, PS.DARKENV);
  622.                 write(fd, obuf, count);
  623.                 break;
  624.             default :
  625.                 break;
  626.         }
  627.     } else {                                /* no printer settings */
  628.  
  629.         count = sprintf(obuf, FARFMTLINE, PS.GSAVE);
  630.         write(fd, obuf, count);
  631.  
  632.         sprintf(tbuf, FARFMTLINE, PS.TRANSLATE);
  633.         count = sprintf(obuf, tbuf, PS.SUB) ;
  634.         write(fd, obuf, count);
  635.  
  636.         sprintf(tbuf, FARFMTLINE, PS.SCALE);
  637.         count = sprintf(obuf, tbuf, PS.BL);
  638.         write(fd, obuf, count);
  639.     }
  640.  
  641.     if (!photointerp) {
  642.         count = sprintf (obuf, FARFMTLINE, PS.EXCHV); /* inverse before transfer*/
  643.         write(fd, obuf, count);
  644.     }
  645.  
  646.     count = sprintf (obuf, FARFMTLINE, PS.THEIMAGE);
  647.     write(fd, obuf, count);
  648.  
  649.     if(!B_RS232File) {
  650.         count = sprintf(obuf,FARFMTLINE, PS.ASC_IMG_DEF);
  651.     } else {
  652.         count = sprintf(obuf,FARFMTLINE, PS.BIN_IMG_DEF);
  653.     }
  654.     write(fd,obuf,count);
  655.  
  656.     count = sprintf (obuf, FARFMTLINE, PS.GSAVE);
  657.     write(fd, obuf, count);
  658.                                                    /* before data leave */
  659.     count = sprintf (obuf, "%Fs ", PS.IMAGE_CALL); /* exactly 1 BL, no LFCR */
  660.     write(fd, obuf, count);
  661.  
  662. }
  663.  
  664. /* image_write ***************************************************
  665.     passed parameters  fd = integer value for file handle
  666.              type = Encapsulated PostScript or Tiff
  667.             plane = PLANE_1
  668.              startpos = starting position within the file
  669.             lines = number of lines handle
  670.               databuf = buffer from which to pull data
  671.            bytes_per_line = number of bytes per line
  672.     return
  673.          number of lines written
  674. ******************************************************************/
  675. int TLImageWrite(fd,type,plane,startpos,lines,databuf,bytes_per_line)
  676. int fd;
  677. int type;
  678. short plane;
  679. long startpos;
  680. long lines;
  681. LPSTR databuf;
  682. long bytes_per_line;
  683. {
  684.  
  685.     int ij,ic;
  686.     char c;
  687.     long retval;
  688.     LPSTR buffer;
  689.     LPSTR bigalloc();
  690.     void bigfree();
  691.  
  692.     /* if encapsulated postscript, take the passed data,
  693.        and write it to the disk. */
  694.  
  695.     if(type == FF_EPSF) {
  696.         retval = lines;
  697.  
  698.         /* if encapsulated postscript, take the passed data,
  699.            and write it to the disk. */
  700.  
  701.         /* encapsulated postscript file may not be updated,
  702.             therefor the data that is passed to this function
  703.             in multiple calls, must be in sequence.  That is,
  704.             the data passed to this function to be written to
  705.             an epsf file, should start at scan line 0.  If the
  706.             function is passed 10 scan lines, the next call to
  707.             the function should be passed the data for lines 11
  708.             through 21.  And so on to the end of the image.
  709.         */
  710.  
  711.     /* if were sending to a laser writer over a RS-232 line, */
  712.     /* then the data must consist of printable ascii characters */
  713.     /* B_RS232File = 0 is big file (default) 1 is little file */
  714.  
  715.         if(!B_RS232File) {
  716.              buffer = (LPSTR)bigalloc(bytes_per_line * 2);
  717.              while(lines > 0 ) {
  718.                  for(ic = 0, ij = 0; ic < bytes_per_line;ic++) {
  719.                      c = (databuf[ic] >> 4) & 0xf;
  720.                      if(c <=9) {
  721.                          buffer[ij++] = c | 0x30;
  722.                      } else {
  723.                          buffer[ij++] = c + 55;
  724.                      }
  725.                      c = databuf[ic] & 0xf;
  726.                      if(c <=9) {
  727.                          buffer[ij++] = c | 0x30;
  728.                      } else {
  729.                          buffer[ij++] = c + 55;
  730.                          }
  731.                  }
  732.                  
  733.                  tlpWrite(fd,(LPSTR)buffer,bytes_per_line * 2);
  734.                  databuf+=bytes_per_line;
  735.                  lines--;
  736.              }
  737.              bigfree(buffer); 
  738.             return((int) retval);
  739.         } else {
  740.     
  741.             tlpWrite(fd,(LPSTR)databuf,bytes_per_line*lines);
  742.             return ((int) retval);
  743.         }
  744.     } else {
  745.  
  746.         return((int)(write_image(fd,FULL_RES,plane,
  747.                 startpos,lines,databuf)));
  748.     }
  749. }
  750.  
  751. void TLCloseOutput(fdes,type)
  752. int fdes;
  753. int type;
  754. {
  755.     if ( type == FF_EPSF) {
  756.         write_ps_trailer(fdes);
  757.         /* close(fdes); */
  758.         /* FreePSStrings(); */
  759.     } else {
  760.         close_create(fdes);
  761.          /* close(fdes); */
  762.     }
  763. }
  764.  
  765. void write_ps_trailer(fdes)
  766. int fdes;
  767. {
  768.     int count;
  769.     char obuf[145];
  770.     char tbuf[145];
  771.  
  772.     count = sprintf (obuf, FARFMTLINE, PS.GRESTORE);
  773.     write(fdes, obuf, count);
  774.     count = sprintf (obuf, FARFMTLINE, PS.GRESTORE);
  775.     write(fdes, obuf, count);
  776.     if (pPrintS != (PRINTER *)NULL) { 
  777.         if (pPrintS->formatBox) {
  778.             sprintf(tbuf, FARFMTLINE, PS.TRANSLATE);
  779.             count = sprintf(obuf, tbuf,
  780.                         pPrintS->outptPolarity==ID_FLOPFILMNEG ? PS.ADD : PS.SUB );
  781.             write(fdes, obuf, count);
  782.             sprintf(tbuf, FARFMTLINE, PS.BOXRULE);
  783.             count = sprintf(obuf, tbuf,
  784.                         pPrintS->outptPolarity==ID_FLOPFILMNEG ? PS.NEG : PS.BL ,
  785.                         pPrintS->outptPolarity==ID_FLOPFILMNEG ? PS.NEG : PS.BL );
  786.             write(fdes, obuf, count);
  787.         } 
  788.     }
  789.     count = sprintf (obuf, FARFMTLINE, PS.SHOWPAGE);
  790.     write(fdes, obuf, count);
  791.     count = sprintf (obuf, FARFMTLINE, PS.GRESTORE);
  792.     write(fdes, obuf, count);
  793.     count = sprintf (obuf, FARFMTLINE, PS.TRAILER);
  794.     write(fdes, obuf, count);
  795.  
  796. }
  797.                             /* copy an entire PostScript file to output file*/
  798. BOOL                        /* return success flag */
  799. InsertPSFile(sInFName, fdOut)
  800. PSTR sInFName;
  801. int fdOut;
  802. {
  803.     char fileName[64];
  804.     char pBuf[QCBUFFSZ];
  805.     int fdIn;
  806.     long inFLen;
  807.  
  808.     sprintf(fileName, "%s%s",  pathName, sInFName);
  809.  
  810.     if ((fdIn = open (fileName, O_RDONLY | O_BINARY)) < 0) {
  811. #ifdef DBUG
  812. fprintf(stdprn, "IPS open fails\n");
  813. #endif
  814.         return (FALSE);
  815.     }
  816.  
  817.     inFLen = lseek(fdIn, 0L, 2);
  818.     lseek (fdIn, 0L, 0);
  819.     while (1)
  820.         if (inFLen < (long)QCBUFFSZ) {
  821.             read (fdIn, pBuf, (int)inFLen);
  822.             write (fdOut, pBuf, (int)inFLen); 
  823.             break;
  824.         } else {
  825.             read (fdIn, pBuf, QCBUFFSZ);
  826.             write (fdOut, pBuf, QCBUFFSZ);
  827.             inFLen -= (long)QCBUFFSZ;
  828.             if (inFLen < 1L)
  829.                 break;
  830.         }
  831.     close(fdIn);
  832.     return (TRUE);
  833. }
  834.  
  835. void
  836. InitPSStrings(lpPSStrBuf, countPSStrs)
  837. LPSTR lpPSStrBuf;
  838. int countPSStrs;
  839. {
  840.     int i;
  841.     for (i = 0; i < countPSStrs; i++)
  842.       *((char far * *)&PS+i) = lpPSStrBuf;
  843. }
  844.  
  845.  
  846.  
  847. BOOL
  848. IsLFCR(c)
  849. int c;
  850. {
  851.     return (c=='\n' || c=='\r');
  852. }
  853.  
  854. void
  855. JumpLFCR(pF)
  856. FILE *pF;
  857. {
  858.     int c;
  859.     while (IsLFCR(c=fgetc(pF)))
  860.         ;
  861.     if (c != EOF)
  862.         ungetc(c, pF);
  863. }
  864.  
  865.                            /* get a line from file in buf*/
  866.                         /* no \n, \r */
  867.                         /* null terminate string */
  868.                         /* position on next line */
  869. int                        /* return character count */
  870. GetLine(pF, buf)
  871. FILE *pF;
  872. char * buf;
  873. {
  874.     int c;
  875.     char * pmB = buf;                      /* movable buffer pointer */
  876.     while ((c = fgetc(pF)) != EOF) {
  877.         if (! IsLFCR(c))
  878.             *pmB++ = (char)c;
  879.         else {
  880.             *pmB = '\0';                /* terminate string */
  881.             JumpLFCR(pF);
  882.             break;
  883.         }
  884.     }
  885.     return (pmB - buf);
  886. }
  887.  
  888. void
  889. NCpyFar(lpTo, pFrom, nMany)
  890. char far *lpTo;
  891. char *pFrom;
  892. int nMany;
  893. {
  894.     for( ; nMany; nMany--)
  895.         *lpTo++ = *pFrom++;
  896. }
  897.  
  898. BOOL                           /* return success flag */
  899. LoadPSStrings(lpPSStrBuf)
  900. LPSTR lpPSStrBuf;
  901. {
  902.     int countFileStrs = 0;
  903.     int i;
  904.     int bufSize = 0;
  905.     int lineLen;
  906.     char lineBuf[144];
  907.     char far * lpmB;                /* movable pointer */
  908.     FILE * pF;    
  909.     char fileName[64];
  910.     int cnt;
  911.  
  912.     sprintf(fileName, "%s%s",  pathName, "STRINGS.PS");
  913.  
  914.     if ((pF = fopen(fileName, "rb")) == NULL) {
  915.  
  916.         pathName[0] = '\0';
  917.         return (FALSE);
  918.     }
  919.     if (GetLine(pF, lineBuf) > 0)    
  920.         countFileStrs = atoi(lineBuf);
  921.     InitPSStrings(lpPSStrBuf, countFileStrs);
  922. #ifdef DOOBUG
  923. fprintf(stdprn, "after init: ");
  924. fprintf(stdprn, "SHOWPAGE = %s\n", PS.SHOWPAGE);
  925. fprintf(stdprn, "ASC_IMG_DEF = %s\n", PS.ASC_IMG_DEF);
  926. #endif
  927.  
  928. #ifdef DOOBUG
  929. fprintf (stdprn, "Again, file string count is %d\n", atoi(lineBuf));
  930. #endif
  931.  
  932.     lpmB = lpPSStrBuf;
  933.     for (i = 0 ; i < countFileStrs; i++) {
  934.         GetLine(pF, lineBuf);        /* the symbol */
  935.         lineLen = GetLine(pF, lineBuf);    /* get the string */
  936.                                  /* be sure not to exceed allocated buffsize */
  937.         if (((lpmB + lineLen) - lpPSStrBuf) > sizePSStrings) {
  938. #ifdef DBUG
  939. fprintf (stdprn, "LoadPSStrings exceeds buffer allocation\n");
  940. #endif
  941.               fclose(pF);
  942.             return (FALSE);
  943.         }
  944.         NCpyFar (lpmB, lineBuf, lineLen);   /* copy into far strings buffer */
  945. /*         strncpy(lpmB, lineBuf, lineLen); */  /* copy into strings buffer */
  946.        *((char far **)&PS+i) = lpmB;        /* tell struct member where */
  947.  
  948. #ifdef DOOBUG
  949. fprintf ( stdprn, "\tGot %d char line: %Fs\n", lineLen, (*((char far **)&PS +i)));
  950. #endif
  951.         lpmB += lineLen;            /* update pointer */
  952.         *lpmB++ = '\0';            /* terminate string, upd ptr */
  953.     }
  954.     fclose (pF);
  955.     return (TRUE);
  956. }
  957.  
  958.  
  959.                  /* call before FileSetup for EPS files */
  960.                 /* opens, reads size, closes strings file */
  961.                 /* checks #entries against exact sizeof PS */
  962.                 /* library keeps filesize & path */
  963.                 /* return size of buffer needed for strings */
  964. int                /* or 0 onerr */
  965. TLSetupPSStrings (pstrPath)
  966. PSTR pstrPath;
  967. {
  968.     int i;
  969.     int nStringEntries;
  970.     char lineBuf[144];
  971.     FILE * pF;    
  972.     int cnt;
  973.     char fileName[64];
  974.  
  975.     sprintf(pathName, pstrPath);    /* save path for future reference */
  976.     sprintf(fileName, "%s%s",  pathName, "STRINGS.PS");
  977. #ifdef DEBUG
  978. fprintf(stdprn, "TLSPSS got pathName %s\n", pathName);
  979. fprintf(stdprn, "full path fileName is %s\n", fileName);
  980. #endif
  981.     if ((pF = fopen(fileName, "rb")) == NULL) {
  982. #ifdef DEBUG
  983. fprintf(stdprn, "TLSPSS fopen strings.ps failed\n");
  984. #endif
  985.         pathName[0] = '\0';
  986.         return (0);
  987.     }
  988.     if (GetLine(pF, lineBuf) > 0)        /* make sure count is there*/
  989.         nStringEntries = atoi(lineBuf);
  990.     else {
  991.         pathName[0] = '\0';
  992.         fclose (pF);
  993.         return (0);
  994.     }
  995.                         /* make sure count agrees */
  996.     if (nStringEntries != sizeof(PS) / sizeof(LPSTR)) {
  997. #ifdef DBUG
  998. fprintf(stdprn, "TLSPSS nStringEntries=%d disagrees with size of PS=%d\n",
  999.                     nStringEntries, sizeof(PS) / sizeof(LPSTR));
  1000. #endif
  1001.         pathName[0] = '\0';
  1002.         fclose (pF);
  1003.         return (0);
  1004.     }        
  1005.     sizePSStrings = 0;            /* initialize accumulator */
  1006.                 /* measure total length PostScript strings */
  1007.     for (i = 0 ; i < nStringEntries; i++) {
  1008.         GetLine(pF, lineBuf);        /* the symbol */
  1009.         cnt = GetLine(pF, lineBuf); /* the PostScript string */
  1010.         if (cnt)            
  1011.             sizePSStrings += cnt+1;
  1012.         else {                /* unexpected end of data */
  1013. #ifdef DBUG
  1014. fprintf(stdprn, "TLSPSSunexpected end of data\n");
  1015. #endif
  1016.             sizePSStrings = 0;    /* something is wrong */
  1017.             pathName[0] = '\0';
  1018.             break;
  1019.         }
  1020.     }
  1021.     fclose (pF);                /* close for now */
  1022.     return (sizePSStrings);            /* and keep as semi-global */
  1023. }
  1024.     
  1025.