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

  1. /*
  2.  *    vsbif.c  --  view a simple binary image file.
  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 <dos.h>
  15. #include "sbif.h"
  16. #include "egargb.h"    /* for the initshow() prototype */
  17.  
  18. /* stop character (if more than one screen) */
  19. #define ESC '\033'
  20.  
  21. /* plot size */
  22. #define MAX_COL    (max_x_coord)
  23. #define MAX_ROW    (max_y_coord)
  24.  
  25. /* globals */
  26. extern int grmode;            /* mode for setgraphmode() */
  27. extern int max_x_coord, max_y_coord;    /* screen max x & y */
  28.  
  29. /* the functions */
  30. void main( int argc, char *argv[] );
  31. void beep( void );
  32. void error( char *s, ... );
  33. void warning( char *s, ... );
  34. void usage( void );
  35.  
  36. static void usage( void )
  37. {
  38. fprintf( stderr, "Usage:\n" );
  39. fprintf( stderr, "\tvsbif [options] sbif_file\n" );
  40. fprintf( stderr, "Options are:\n" );
  41. fprintf( stderr, "\t-x num\tx offset\n" );
  42. fprintf( stderr, "\t-y num\ty offset\n" );
  43. fprintf( stderr, "\n" );
  44. }
  45.  
  46.  
  47. void main( argc, argv )
  48. int argc;
  49. char *argv[];
  50. {
  51. register unsigned char mask;    /* pixel bit mask */
  52. register int x, y;        /* current x, y */
  53. unsigned char *vp;        /* pixel byte pointer */
  54. FILE *inf;            /* data file */
  55. int xlines, ylines;        /* x and y pixel count */
  56. int xbytes;            /* x byte count */
  57. unsigned char *pmat;        /* the pixel matrix (part of it) */
  58. int xoffs, yoffs;        /* display offsets */
  59. int trace;            /* trace flag */
  60. int i, c;            /* tmp */
  61. char datafile[100];        /* data file name */
  62. char *ap, **p;            /* argument pointers */
  63. char hbuf[SBHSIZE];        /* buffer for the sbif header */
  64. char nbuf[100];            /* buffer for the name in the sbif header */
  65.  
  66. /* read arguments */
  67. datafile[0] = '\0';
  68. trace = 0;
  69. xoffs = yoffs = 0;
  70. for (p = argv + 1, c = 1; c < argc; ++c)
  71.     {
  72.     if (trace) printf( "%s:\n", *p );
  73.     ap = *p++;
  74.  
  75.     if (*ap == '-')    for (i = 1; ap[i]; ++i) switch (ap[i])
  76.         {
  77.  
  78.         case 'x':    /* x offset */
  79.             if (++c >= argc)
  80.                 error( "-x: offset missing");
  81.  
  82.             xoffs = atoi( *p++ );
  83.  
  84.             if (xoffs <= 0)
  85.                 error( "-x: offset <= 0" );
  86.  
  87.             continue;
  88.  
  89.         case 'y':    /* y offset */
  90.             if (++c >= argc)
  91.                 error( "-y: offset missing");
  92.  
  93.             yoffs = atoi( *p++ );
  94.  
  95.             if (yoffs <= 0)
  96.                 error( "-y: offset <= 0" );
  97.  
  98.             continue;
  99.  
  100.         case 't':
  101.             trace++;
  102.             continue;
  103.  
  104.         default:
  105.             usage();
  106.             error( "don't understand flag '-%c'", ap[i] );
  107.         }
  108.     else if (!datafile[0])
  109.         {
  110.         strcpy( datafile, ap );
  111.         }
  112.     else
  113.         {
  114.         usage();
  115.         error( "too many data files specified" );
  116.         }
  117.     }
  118.  
  119. /* open the data file */
  120. if (!datafile[0])
  121.     error( "No SBIF data file specified" );
  122.  
  123. if ((inf = fopen( datafile, "rb" )) == NULL)
  124.     error( "Can't open input file '%s'", datafile );
  125.  
  126.  
  127. /* read the header */
  128. if (fread( hbuf, sizeof( hbuf ), 1, inf ) < 1)
  129.     error( "premature end of file '%s'", datafile );
  130.  
  131. /* read the sizes */
  132. xlines = ylines = 0;
  133. sscanf( hbuf, "%s %d %d", nbuf, &xlines, &ylines );
  134. xbytes = XBYTES( xlines );
  135.  
  136. if (trace) fprintf( stderr, "%s x:%d y:%d (offs %d, %d xbytes:%d)\n",
  137.              nbuf, xlines, ylines, xoffs, yoffs, xbytes );
  138.  
  139. if (xlines <= 0 || ylines <= 0)
  140.     error( "don't understand contents of file '%s'", datafile );
  141.  
  142. /* get a buffer for one row of the pixel matrix */
  143. if (!(pmat = (unsigned char *) calloc( xbytes, 1 )))
  144.     error( "out of memory for the pixel matrix" );
  145.  
  146. /* skip the offset */
  147. ylines -= yoffs;
  148. for (y = 0; y < yoffs; ++y)
  149.     if (fread( pmat, xbytes, 1, inf ) < 1)
  150.         error( "premature end of file '%s'", datafile );
  151.  
  152. /* go to graphic mode */
  153. if (trace)
  154.     {
  155.     fprintf( stderr, "press any key to go to graphic mode",
  156.                 ylines );
  157.     getch();
  158.     }
  159.  
  160. initshow();
  161.  
  162. /* show the picture */
  163. for (; ylines > 0; ylines -= MAX_ROW + 1)
  164.     {
  165.     /* show one screenfull */
  166.     for (y = 0; y < ylines && y <= MAX_ROW; ++y)
  167.     {
  168.     /* read one line */
  169.     if (fread( pmat, xbytes, 1, inf ) < 1)
  170.         {
  171.         printf( "end at line %d (%d bytes) ", y + 1, xbytes * (y + 1) );
  172.         sleep( 5 );
  173.         endshow();
  174.         error( "premature end of file '%s'", datafile );
  175.         }
  176.  
  177.     if (trace) putpixel( 0, MAX_ROW - y, EGA_RED );
  178.  
  179.     for (x = 0, mask = 0x80, vp = pmat; x < xlines; ++x, mask >>= 1)
  180.         {
  181.         if (!mask)
  182.             {
  183.             /* next byte */
  184.             ++vp;
  185.             mask = 0x80;
  186.             }
  187.  
  188.         /* check x offset */
  189.         if (x < xoffs)
  190.             continue;
  191.  
  192.         /* show the pixel if it is on */
  193.         if (!(*vp & mask))
  194.             continue;
  195.  
  196.         /* (y is upside down, but so is the screen y coordinate) */
  197.         putpixel( x - xoffs, y, EGA_LIGHTGRAY );
  198.         }
  199.     }
  200.  
  201.     /* wait for a key */
  202.     if (getch() == ESC)
  203.     break;
  204.  
  205.     /* if there is another screen */
  206.     cleardevice();
  207.     }
  208.  
  209. /* ready */
  210. endshow();
  211. exit( 0 );
  212. }
  213.  
  214.  
  215.  
  216. void error( s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 )
  217. char *s;
  218. int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
  219. {
  220. fprintf( stderr, "\nError:" );
  221. fprintf( stderr, s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 );
  222. fprintf( stderr, "\n" );
  223.  
  224. exit( 2 );
  225. }
  226.  
  227. void warning( s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 )
  228. char *s;
  229. int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
  230. {
  231. fprintf( stderr, "\nWarning:" );
  232. fprintf( stderr, s, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10 );
  233. fprintf( stderr, "\n" );
  234. }
  235.  
  236.  
  237.