home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / sbifps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-24  |  6.1 KB  |  268 lines

  1. /*
  2.  *    sbifeps.c  --  simple binary image file to postscript format.
  3.  *
  4.  *    5 sep 1989  Olle Olsson.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <fcntl.h>
  10. /*#include <conio.h>*/
  11. #include <io.h>
  12. #include <string.h>
  13. #include <process.h>
  14. #include <bios.h>
  15. #include "sbif.h"
  16.  
  17.  
  18. /* the functions */
  19. void main( int argc, char *argv[] );
  20. void beep( void );
  21. void psbegin( int xsize, int ysize, int square_flag );
  22. void psend( void );
  23. void error( char *s, ... );
  24. void warning( char *s, ... );
  25. void usage( void );
  26.  
  27. static void usage( void )
  28. {
  29. fprintf( stderr, "Usage:\n" );
  30. fprintf( stderr, "\tsbifps [options] sbif_file\n" );
  31. fprintf( stderr, "Options are:\n" );
  32. fprintf( stderr, "\t-o string\toutput file name\n" );
  33. fprintf( stderr, "\t-b\t\tblack background\n" );
  34. fprintf( stderr, "\t-s\t\tsquare image output (default: fill paper)\n" );
  35. /*fprintf( stderr, "\t-i num\tindent amount (characters)\n" );*/
  36. fprintf( stderr, "\n" );
  37. }
  38.  
  39.  
  40. void main( argc, argv )
  41. int argc;
  42. char *argv[];
  43. {
  44. FILE *inf;            /* data file */
  45. int xlines, ylines;        /* x and y pixel count */
  46. int xbytes;            /* x byte count */
  47. int bbg;            /* flag for black background */
  48. int square;            /* flag for square image output */
  49. long nbytes;            /* total byte count */
  50. /*int indent;            /* line indent amount */
  51. int trace;            /* trace flag */
  52. char datafile[100];        /* data file name */
  53. char outfile[100];        /* output file name */
  54. char *ap, **p;            /* argument pointers */
  55. int i, c;            /* argument count */
  56. char hbuf[SBHSIZE];        /* buffer for the sbif header */
  57. char nbuf[100];            /* buffer for the name in the sbif header */
  58. unsigned char obuf[32];        /* output conversion buffer */
  59. register unsigned char *op;    /* output byte pointer */
  60. register int n;            /* output dot count */
  61.  
  62. /* read arguments */
  63. datafile[0] = outfile[0] = '\0';
  64. square = bbg = 0;
  65. trace = 0;
  66. /*indent = 0;*/
  67.  
  68. for (p = argv + 1, c = 1; c < argc; ++c)
  69.     {
  70.     if (trace) printf( "%s:\n", *p );
  71.     ap = *p++;
  72.  
  73.     if (*ap == '-')    for (i = 1; ap[i]; ++i) switch (ap[i])
  74.         {
  75.         case 'o':
  76.             if (++c >= argc)
  77.                 error( "-o: output file name missing");
  78.  
  79.             if (outfile[0])
  80.                 error( "-o: too many output files" );
  81.  
  82.             strcpy( outfile, *p++ );
  83.             continue;
  84.  
  85. /*        case 'i':    /* indent */
  86. /*            if (++c >= argc)
  87.                 error( "-i: indent value missing");
  88.  
  89.             indent = atoi( *p++ );
  90.  
  91.             if (indent < 0)
  92.                 error( "-i: indent < 0" );
  93.  
  94.             continue;
  95. */
  96.         case 'b':
  97.             bbg++;
  98.             continue;
  99.  
  100.         case 's':
  101.             square++;
  102.             continue;
  103.  
  104.         case 't':
  105.             trace++;
  106.             continue;
  107.  
  108.         default:
  109.             usage();
  110.             error( "don't understand flag '-%c'", ap[i] );
  111.         }
  112.     else if (!datafile[0])
  113.         {
  114.         strcpy( datafile, ap );
  115.         }
  116.     else
  117.         {
  118.         usage();
  119.         error( "too many data files specified" );
  120.         }
  121.     }
  122.  
  123. /* open the data file */
  124. if (!datafile[0])
  125.     error( "No SBIF data file specified" );
  126.  
  127. if ((inf = fopen( datafile, "rb" )) == NULL)
  128.     error( "Can't open input file '%s'", datafile );
  129.  
  130. /* write to stdout or a named file? */
  131. if (outfile[0])
  132.     if (freopen( outfile, "w", stdout ) == NULL)
  133.         error( "Can't create output file '%s'", outfile );
  134.  
  135. /* read the header */
  136. if (fread( hbuf, sizeof( hbuf ), 1, inf ) < 1)
  137.     error( "premature end of file '%s'", datafile );
  138.  
  139. /* read the sizes */
  140. xlines = ylines = 0;
  141. sscanf( hbuf, "%s %d %d", nbuf, &xlines, &ylines );
  142. xbytes = XBYTES( xlines );
  143.  
  144. /* use full bytes */
  145. xlines = xbytes * 8;
  146.  
  147. if (trace) fprintf( stderr, "%s x:%d y:%d  xbytes:%d\n",
  148.              nbuf, xlines, ylines, xbytes );
  149.  
  150. if (xlines <= 0 || ylines <= 0)
  151.     error( "don't understand contents of file '%s'", datafile );
  152.  
  153. /* start output */
  154. psbegin( xlines, ylines, square );
  155.  
  156. /* convert the picture */
  157. for (nbytes = (long) xbytes * ylines; nbytes > 0;)
  158.     {
  159.     /* read one buffer full  (or the rest) */
  160.     n = sizeof (obuf);
  161.     if (n > nbytes) n = nbytes;
  162.  
  163.     if (fread( obuf, n, 1, inf ) < 1)
  164.         {
  165.         if (trace) fprintf( stderr, "%ld bytes missing ", nbytes );
  166.         error( "premature end of file '%s'", datafile );
  167.         }
  168.  
  169.     /* bump count */
  170.     nbytes -= n;
  171.     
  172.     /* black background? */
  173.     if (!bbg)
  174.         {
  175.         /* the ps image operator uses zero for black */
  176.         /* invert to get a white background */
  177.         for (op = &obuf[n]; --op >= obuf;)
  178.             *op ^= 0xff;
  179.         }
  180.     
  181.     /* print */
  182.     for (op = obuf; n > 0; --n, ++op)
  183.         printf( "%02x", *op );
  184.  
  185.     printf( "\n" );
  186.     }
  187.  
  188. /* end */
  189. psend();
  190.  
  191. /* ready */
  192. exit( 0 );
  193. }
  194.  
  195.  
  196. void psbegin( x, y, square )
  197. int x, y;    /* sizes */
  198. int square;    /* flag */
  199. {
  200. /* print the prologue */
  201. printf( "%%! IFS bit image\n\n" );
  202. printf( "/picstr 256 string def      %% tmp storage \n\n" );
  203.  
  204. if (square)
  205.     {
  206.     /* unit is inch/72, an a4 standard paper is 598 by 842 */
  207.     /* (in the U.S. I think the standard is 8 by 11 inches, 576 by 792) */
  208.     printf( "50 200 translate            %% lower left corner\n" );
  209.     printf( "500 500 scale               %% map to 500 points square\n\n" );
  210.     }
  211. else
  212.     {
  213.     /* fill the paper */
  214.     printf( "10 10 translate               %% lower left corner\n" );
  215.     printf( "580 810 scale                 %% map to fill the paper\n\n" );
  216.     }
  217.  
  218. printf( "%d %d 1                     %% source data dimensions\n", x, y );
  219. printf( "[%d 0 0 -%d 0 %d]           %% map to unit square (flip y)\n", x, y, y );
  220. printf( "{                           %% procedure to read hex data\n" );
  221. printf( "  currentfile picstr readhexstring pop\n" );
  222. printf( "}\n" );
  223. printf( "%% hex data  %d * %d * 1 / 4 = %ld digits\n", x, y, ((long) x * y) / 4 );
  224. printf( "%% (don't put anything between the image operator and the hex data)\n" );
  225. printf( "image\n\n" );
  226. }
  227.  
  228. void psend( void )
  229. {
  230. int i;
  231. /*
  232.  * This is fix for x sizes that are not a multiple of 8.
  233.  * The readhexstring command seems to want more characters than needed.
  234.  * (Maybee the data must be a multiple of the "picstr" size?)
  235.  * Hide some "white space" behind a comment for readhexstring.
  236.  */
  237. for (i = 0; i < 5; ++i)
  238.     printf( "%% ffffffffffffffffffffffffffffffffffffffff\n" );
  239.  
  240.  
  241. /* show it */
  242. printf( "showpage\n" );
  243. }
  244.  
  245.  
  246.  
  247. void error( s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 )
  248. char *s;
  249. int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
  250. {
  251. fprintf( stderr, "\nError:" );
  252. fprintf( stderr, s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 );
  253. fprintf( stderr, "\n" );
  254.  
  255. exit( 2 );
  256. }
  257.  
  258. void warning( s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 )
  259. char *s;
  260. int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
  261. {
  262. fprintf( stderr, "\nWarning:" );
  263. fprintf( stderr, s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 );
  264. fprintf( stderr, "\n" );
  265. }
  266.  
  267.  
  268.