home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d386 / xlispstat.lha / XLispStat / src3.lzh / UNIX / postscript.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-30  |  6.5 KB  |  232 lines

  1. /* postscript - XLISP-STAT postscript hard copy routines.              */
  2. /* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
  3. /* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
  4. /* You may give out copies of this software; for conditions see the    */
  5. /* file COPYING included with this distribution.                       */
  6. /* Postscript adapted from file pbmtops.c of Jef Poskanzer's pbm       */
  7. /* system:                                                             */
  8.  
  9. /* pbmtops.c - read a portable bitmap and produce a PostScript bitmap file
  10. **
  11. ** Copyright (C) 1988 by Jef Poskanzer.
  12. **
  13. ** Permission to use, copy, modify, and distribute this software and its
  14. ** documentation for any purpose and without fee is hereby granted, provided
  15. ** that the above copyright notice appear in all copies and that both that
  16. ** copyright notice and this permission notice appear in supporting
  17. ** documentation.  This software is provided "as is" without express or
  18. ** implied warranty.
  19. */
  20.  
  21. #include <stdio.h>
  22.  
  23. #ifdef DODO
  24. sample()
  25. {
  26.   scale = 1.0;
  27.     
  28.   /* Compute padding to round cols up to the nearest multiple of 8. */
  29.   padright = ( ( cols + 7 ) / 8 ) * 8 - cols;
  30.  
  31.   psputinit(file, cols, rows, scale );
  32.   for ( row = 0; row < rows; row++ ) {
  33.     for ( col = 0; col < cols; col++ )
  34.       psputbit( bits[row][col] );
  35.     for ( col = 0; col < padright; col++ )
  36.       psputbit( 0 );
  37.   }
  38.   psputrest( );
  39. }
  40. #endif DODO
  41.  
  42. /**************************************************************************/
  43. /**                                                                      **/
  44. /**                            Global Variables                          **/
  45. /**                                                                      **/
  46. /**************************************************************************/
  47.  
  48. static int item, bitsperitem, bitshift, rlitemsperline;
  49. static int repeat, itembuf[128], count, repeatitem, repeatcount;
  50. static FILE *fp;
  51.  
  52. /**************************************************************************/
  53. /**                                                                      **/
  54. /**                            Public Interface                          **/
  55. /**                                                                      **/
  56. /**************************************************************************/
  57.  
  58. /* set up global variables and print the postscript preamble */
  59. psputinit(file, cols, rows, scale )
  60.      FILE *file;
  61.      int cols, rows;
  62.      double scale;
  63. {
  64.   int scols, srows;
  65.   
  66.   fp = file;
  67.  
  68.   scols = cols * scale * 0.96 + 0.5;    /*   0.96 is the multiple of   */
  69.   srows = rows * scale * 0.96 + 0.5;    /* 72/300 that is closest to 1 */
  70.  
  71.   fprintf(fp, "%%! XLISP-STAT postscript image\n");
  72.   fprintf(fp, "\n" );
  73.   fprintf(fp, "/rlestr1 1 string def\n" );
  74.   fprintf(fp, "/rlestr 128 string def\n" );
  75.   fprintf(fp, "/readrlestring {\n" );
  76.   fprintf(fp, "  currentfile rlestr1 readhexstring pop  0 get\n" );
  77.   fprintf(fp, "  dup 127 le {\n" );
  78.   fprintf(fp, "    currentfile rlestr 0  4 3 roll  1 add  getinterval\n" );
  79.   fprintf(fp, "    readhexstring  pop\n" );
  80.   fprintf(fp, "  } {\n" );
  81.   fprintf(fp, "    256 exch sub  dup\n" );
  82.   fprintf(fp, "    currentfile rlestr1 readhexstring pop  0 get\n" );
  83.   fprintf(fp, "    exch 0 exch 1 exch 1 sub { rlestr exch 2 index put } for\n" );
  84.   fprintf(fp, "    pop  rlestr exch 0 exch getinterval\n" );
  85.   fprintf(fp, "  } ifelse\n" );
  86.   fprintf(fp, "} bind def\n" );
  87.   fprintf(fp, "\n" );
  88.   fprintf(fp,
  89.      "%d %d translate\t%% move to lower left corner of box\n",
  90.      300 - ( scols/2 ), 400 - ( srows/2 ) );
  91.   fprintf(fp, "%d %d scale\t\t%% scale box\n", scols, srows );
  92.   fprintf(fp, "\n" );
  93.   fprintf(fp, "%d %d 1\t\t\t%% width height bits/sample\n", cols, rows );
  94.   fprintf(fp,
  95.     "[ %d 0 0 -%d 0 %d ]\t%% transformation matrix\n", cols, rows, rows );
  96.   fprintf(fp, "{ readrlestring }\t%% proc\n" );
  97.   fprintf(fp, "image\n" );
  98.  
  99.   rlitemsperline = 0;
  100.   item = 0;
  101.   bitsperitem = 0;
  102.   bitshift = 7;
  103.  
  104.   repeat = 1;
  105.   count = 0;
  106. }
  107.  
  108. /* enter a bit into the image */
  109. psputbit(b)
  110.      int b;
  111. {
  112.   if ( bitsperitem == 8 ) {
  113.     putitem( );
  114.   }
  115.   if ( ! b )
  116.     item += 1 << bitshift;
  117.   bitsperitem++;
  118.   bitshift--;
  119. }
  120.  
  121. /* clean up and print the showpage command */
  122. psputrest( )
  123. {
  124.   if ( bitsperitem > 0 )
  125.     putitem( );
  126.   if ( count > 0 )
  127.     putrlbuffer( );
  128.   fprintf(fp, "\nshowpage\n" );
  129. }
  130.  
  131. /**************************************************************************/
  132. /**                                                                      **/
  133. /**                           Internal Routines                          **/
  134. /**                                                                      **/
  135. /**************************************************************************/
  136.  
  137. static putrlitem( rlitem )
  138.      int rlitem;
  139. {
  140.   if ( rlitemsperline == 30 ) {
  141.     putc('\n', fp);
  142.     rlitemsperline = 0;
  143.   }
  144.   rlitemsperline++;
  145.   fprintf(fp, "%02x", rlitem );
  146. }
  147.  
  148. static putrlbuffer( )
  149. {
  150.   int i;
  151.  
  152.   if ( repeat ) {
  153.     putrlitem( 256 - count );
  154.     putrlitem( repeatitem );
  155.   }
  156.   else {
  157.     putrlitem( count - 1 );
  158.     for ( i = 0; i < count; i++ )
  159.       putrlitem( itembuf[i] );
  160.   }
  161.   repeat = 1;
  162.   count = 0;
  163. }
  164.  
  165. static putitem( )
  166. {
  167.   int i;
  168.  
  169.   if ( count == 128 )
  170.     putrlbuffer( );
  171.  
  172.   if ( repeat && count == 0 ) {
  173.     /* Still initializing a repeat buf. */
  174.     itembuf[count] = repeatitem = item;
  175.     count++;
  176.   }
  177.   else if ( repeat ) {
  178.     /* Repeating - watch for end of run. */
  179.     if ( item == repeatitem ) {
  180.       /* Run continues. */
  181.       itembuf[count] = item;
  182.       count++;
  183.     }
  184.     else { 
  185.       /* Run ended - is it long enough to dump? */
  186.       if ( count > 2 ) {
  187.     /* Yes, dump a repeat-mode buffer and start a new one. */
  188.     putrlbuffer( );
  189.     itembuf[count] = repeatitem = item;
  190.     count++;
  191.       }
  192.       else {
  193.     /* Not long enough - convert to non-repeat mode. */
  194.     repeat = 0;
  195.     itembuf[count] = repeatitem = item;
  196.     count++;
  197.     repeatcount = 1;
  198.       }
  199.     }
  200.   }
  201.   else {
  202.     /* Not repeating - watch for a run worth repeating. */
  203.     if ( item == repeatitem ) {
  204.       /* Possible run continues. */
  205.       repeatcount++;
  206.       if ( repeatcount > 3 ) {
  207.     /* Long enough - dump non-repeat part and start repeat. */
  208.     count = count - ( repeatcount - 1 );
  209.     putrlbuffer( );
  210.     count = repeatcount;
  211.     for ( i = 0; i < count; i++ )
  212.       itembuf[i] = item;
  213.       }
  214.       else {
  215.     /* Not long enough yet - continue as non-repeat buf. */
  216.     itembuf[count] = item;
  217.     count++;
  218.       }
  219.     }
  220.     else {
  221.       /* Broken run. */
  222.       itembuf[count] = repeatitem = item;
  223.       count++;
  224.       repeatcount = 1;
  225.     }
  226.   }
  227.  
  228.   item = 0;
  229.   bitsperitem = 0;
  230.   bitshift = 7;
  231. }
  232.