home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / vifs / sbifeps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-27  |  7.8 KB  |  377 lines

  1. /*
  2.  *    sbifeps.c  --  simple binary image file to epson printer format.
  3.  *
  4.  *    25 july 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. /* max number of dots in an epson bit image */
  18. #define MAXDOTS 480
  19.  
  20. /* the functions */
  21. void main( int argc, char *argv[] );
  22. void beep( void );
  23. void error( char *s, ... );
  24. void warning( char *s, ... );
  25. void usage( void );
  26. int prbytes( char *buffer, int size, FILE *outfile );
  27. int weps( FILE *outfile, char *obuf, int obuf_size, int indent );
  28. int wepsbegin( FILE *outfile );
  29. int wepsend( FILE *outfile );
  30.  
  31. static void usage( void )
  32. {
  33. fprintf( stderr, "Usage:\n" );
  34. fprintf( stderr, "\tsbifeps [options] sbif_file\n" );
  35. fprintf( stderr, "Options are:\n" );
  36. fprintf( stderr, "\t-o string\toutput file name\n" );
  37. fprintf( stderr, "\t-b\tuse bios to print (on lpt1:)\n" );
  38. fprintf( stderr, "\t-i num\tindent amount (characters)\n" );
  39. fprintf( stderr, "\t-x num\tx offset\n" );
  40. fprintf( stderr, "\t-y num\ty offset\n" );
  41. fprintf( stderr, "\n" );
  42. }
  43.  
  44.  
  45. void main( argc, argv )
  46. int argc;
  47. char *argv[];
  48. {
  49. register unsigned char mask;    /* pixel bit mask */
  50. register int x, y;        /* current x, y */
  51. unsigned char *vp;        /* pixel byte pointer */
  52. FILE *inf;            /* data file */
  53. FILE *outf;            /* output file */
  54. int xlines, ylines;        /* x and y pixel count */
  55. int xbytes;            /* x byte count */
  56. unsigned char *pmat;        /* the pixel matrix (part of it) */
  57. int xoffs, yoffs;        /* display offsets */
  58. int indent;            /* line indent amount */
  59. int trace;            /* trace flag */
  60. int i, c;            /* tmp */
  61. char datafile[100];        /* data file name */
  62. char outfile[100];        /* output file name */
  63. char *ap, **p;            /* argument pointers */
  64. char hbuf[SBHSIZE];        /* buffer for the sbif header */
  65. char nbuf[100];            /* buffer for the name in the sbif header */
  66. char obuf[MAXDOTS];        /* output conversion buffer */
  67. register unsigned char omask;    /* output dot bit mask */
  68. char *op;            /* output byte pointer */
  69. int olen;            /* output dot count */
  70. int biospr;            /* flag for bios print */
  71.  
  72.  
  73. /* read arguments */
  74. datafile[0] = outfile[0] = '\0';
  75. trace = 0;
  76. biospr = indent = xoffs = yoffs = 0;
  77. for (p = argv + 1, c = 1; c < argc; ++c)
  78.     {
  79.     if (trace) printf( "%s:\n", *p );
  80.     ap = *p++;
  81.  
  82.     if (*ap == '-')    for (i = 1; ap[i]; ++i) switch (ap[i])
  83.         {
  84.         case 'b':
  85.             biospr++;
  86.             continue;
  87.  
  88.         case 'o':
  89.             if (++c >= argc)
  90.                 error( "-o: output file name missing");
  91.  
  92.             if (outfile[0])
  93.                 error( "-o: too many output files" );
  94.  
  95.             strcpy( outfile, *p++ );
  96.             continue;
  97.  
  98.         case 'i':    /* indent */
  99.             if (++c >= argc)
  100.                 error( "-i: indent value missing");
  101.  
  102.             indent = atoi( *p++ );
  103.  
  104.             if (indent < 0)
  105.                 error( "-i: indent < 0" );
  106.  
  107.             continue;
  108.  
  109.         case 'x':    /* x offset */
  110.             if (++c >= argc)
  111.                 error( "-x: offset missing");
  112.  
  113.             xoffs = atoi( *p++ );
  114.  
  115.             if (xoffs <= 0)
  116.                 error( "-x: offset <= 0" );
  117.  
  118.             continue;
  119.  
  120.         case 'y':    /* y offset */
  121.             if (++c >= argc)
  122.                 error( "-y: offset missing");
  123.  
  124.             yoffs = atoi( *p++ );
  125.  
  126.             if (yoffs <= 0)
  127.                 error( "-y: offset <= 0" );
  128.  
  129.             continue;
  130.  
  131.         case 't':
  132.             trace++;
  133.             continue;
  134.  
  135.         default:
  136.             usage();
  137.             error( "don't understand flag '-%c'", ap[i] );
  138.         }
  139.     else if (!datafile[0])
  140.         {
  141.         strcpy( datafile, ap );
  142.         }
  143.     else
  144.         {
  145.         usage();
  146.         error( "too many data files specified" );
  147.         }
  148.     }
  149.  
  150. /* open the data file */
  151. if (!datafile[0])
  152.     error( "No SBIF data file specified" );
  153.  
  154. if ((inf = fopen( datafile, "rb" )) == NULL)
  155.     error( "Can't open input file '%s'", datafile );
  156.  
  157. /* write to stdout or a named file? */
  158. /* or use biosprint() */
  159. if (biospr)
  160.     outf = NULL;
  161. else if (!outfile[0])
  162.     {
  163.     /* stdout, make it binary mode */
  164.     outf = stdout;
  165.     setmode( fileno( outf ), O_BINARY );
  166.     }
  167. else if ((outf = fopen( outfile, "wb" )) == NULL)
  168.     error( "Can't create output file '%s'", outfile );
  169.  
  170. /* read the header */
  171. if (fread( hbuf, sizeof( hbuf ), 1, inf ) < 1)
  172.     error( "premature end of file '%s'", datafile );
  173.  
  174. /* read the sizes */
  175. xlines = ylines = 0;
  176. sscanf( hbuf, "%s %d %d", nbuf, &xlines, &ylines );
  177. xbytes = XBYTES( xlines );
  178.  
  179. if (trace) fprintf( stderr, "%s x:%d y:%d (offs %d, %d xbytes:%d)\n",
  180.              nbuf, xlines, ylines, xoffs, yoffs, xbytes );
  181.  
  182. if (xlines <= 0 || ylines <= 0)
  183.     error( "don't understand contents of file '%s'", datafile );
  184.  
  185. /* get a buffer for one row of the pixel matrix */
  186. if (!(pmat = (unsigned char *) calloc( xbytes, 1 )))
  187.     error( "out of memory for the pixel matrix" );
  188.  
  189. /* skip the offset */
  190. for (y = 0; y < yoffs; ++y)
  191.     if (fread( pmat, xbytes, 1, inf ) < 1)
  192.         error( "premature end of file '%s'", datafile );
  193.  
  194. /* start output */
  195. if (!wepsbegin( outf ))
  196.     error( "output write failure" );
  197.  
  198. /* clear the output buffer */
  199. memset( obuf, 0, sizeof (obuf) );
  200.  
  201. /* determine the output dot count (dot line length) */
  202. olen = ((xlines - xoffs) < sizeof (obuf)) ? (xlines - xoffs) : sizeof (obuf);
  203.  
  204. /* convert the picture */
  205. /* (flip the bytes from lying down to standing up) */
  206. for (y = 0, omask = 0; y < ylines; ++y, omask >>= 1)
  207.     {
  208.     /* read one line */
  209.     if (fread( pmat, xbytes, 1, inf ) < 1)
  210.         {
  211.         if (trace) fprintf( stderr, "end at line %d (%d bytes) ",
  212.                          y + 1, xbytes * (y + 1) );
  213.         error( "premature end of file '%s'", datafile );
  214.         }
  215.  
  216.     if (trace) fprintf( stderr, " y:%d ", y );
  217.  
  218.     /* restart at the output buffer first (top) line? */
  219.     if (!omask)
  220.         {
  221.         omask = 0x80;
  222.  
  223.         /* write the buffer */
  224.         if (y) if (!weps( outf, obuf, olen, indent ))
  225.             error( "output write failure" );
  226.  
  227.         /* clear the output buffer for the next lines */
  228.         memset( obuf, 0, sizeof (obuf) );
  229.         }
  230.  
  231.     /* scan one line */
  232.     for (x = 0, vp = pmat, op = obuf, mask = 0x80;
  233.         x < xlines && op < &obuf[sizeof (obuf)];
  234.             ++x, ++op, mask >>= 1)
  235.         {
  236.         if (!mask)
  237.             {
  238.             /* next byte */
  239.             ++vp;
  240.             mask = 0x80;
  241.             }
  242.  
  243.         /* check x offset */
  244.         if (x < xoffs)
  245.             continue;
  246.  
  247.         /* copy the pixel only if it is on */
  248.         if (*vp & mask)
  249.             {
  250.             /* (y is upside down, but so is the printer y coord.) */
  251.             *op |= omask;
  252.             }
  253.         }
  254.     }
  255.  
  256. /* write odd lines */
  257. if (omask != 0x80) if (!weps( outf, obuf, olen, indent ))
  258.     error( "output write failure" );
  259.  
  260. /* end */
  261. if (!wepsend( outf ))
  262.     error( "output write failure" );
  263.  
  264. /* ready */
  265. exit( 0 );
  266. }
  267.  
  268. int prbytes( bp, size, of )
  269. char *bp;
  270. int size;
  271. FILE *of;
  272. {
  273. int port = 0;        /* biosprint() port, 0 --> lpt1: etc. */
  274.  
  275. /* ordinary write or bios print? */
  276. if (of == NULL)
  277.     {
  278.     while (size--) if (biosprint( 0, *bp++, port ) & 0x01)
  279.                 return (0);
  280.     }
  281. else if (fwrite( bp, size, 1, of) < 1)
  282.     return (0);
  283.  
  284. /* ok */
  285. return (1);
  286. }
  287.  
  288. int wepsbegin( outf )
  289. FILE *outf;
  290. {
  291. char *ist = "\033A\010\0332";    /* the secret code */
  292.  
  293. /* set the line feed to eight dots */
  294. if (!prbytes( ist, strlen( ist ), outf ))
  295.     return (0);
  296.  
  297. /* ok */
  298. return (1);
  299. }
  300.  
  301. int wepsend( outf )
  302. FILE *outf;
  303. {
  304. /* restore the line feed length */
  305. if (!prbytes( "\0332", 2, outf ))
  306.     return (0);
  307.  
  308. /* print a formfeed */
  309. if (!prbytes( "\f", 1, outf ))
  310.     return (0);
  311.  
  312. /* ok */
  313. return (1);
  314. }
  315.  
  316.  
  317. int weps( outf, obuf, olen, indent )
  318. FILE *outf;
  319. char *obuf;
  320. int olen;
  321. int indent;
  322. {
  323. char sbuf[5];
  324.  
  325. /* indent */
  326. while (indent-- > 0) if (!prbytes( " ", 1, outf ))
  327.             return (0);
  328.  
  329. /* bit image header (60 dots/inch) */
  330. if (!prbytes( "\033K", 2, outf ))
  331.     return (0);
  332.  
  333. /* lsb of bit image length */
  334. sbuf[0] = olen & 0xff;
  335. if (!prbytes( sbuf, 1, outf ))
  336.     return (0);
  337.  
  338. /* msb of bit image length */
  339. sbuf[0] = olen >> 8;
  340. if (!prbytes( sbuf, 1, outf ))
  341.     return (0);
  342.  
  343. /* write the bit image */
  344. if (!prbytes( obuf, olen, outf ))
  345.     return (0);
  346.  
  347. /* newline */
  348. if (!prbytes( "\r\n", 2, outf ))
  349.     return (0);
  350.  
  351. /* ok */
  352. return (1);
  353. }
  354.  
  355.  
  356. void error( s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 )
  357. char *s;
  358. int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
  359. {
  360. fprintf( stderr, "\nError:" );
  361. fprintf( stderr, s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 );
  362. fprintf( stderr, "\n" );
  363.  
  364. exit( 2 );
  365. }
  366.  
  367. void warning( s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 )
  368. char *s;
  369. int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
  370. {
  371. fprintf( stderr, "\nWarning:" );
  372. fprintf( stderr, s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 );
  373. fprintf( stderr, "\n" );
  374. }
  375.  
  376.  
  377.