home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / radsrc22 / src / px / mt160r.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-19  |  2.7 KB  |  147 lines

  1. /* Copyright (c) 1991 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)mt160r.c 2.3 1/29/92 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  mt160r.c - program to dump pixel file to Mannesman-Tally 160.
  9.  *
  10.  *     8/16/85
  11.  */
  12.  
  13. #include  <stdio.h>
  14. #ifdef MSDOS
  15. #include  <fcntl.h>
  16. #endif
  17.  
  18. #include  "color.h"
  19. #include  "resolu.h"
  20.  
  21. #define  NCOLS          880             /* for wide carriage */
  22.  
  23.  
  24. main(argc, argv)
  25. int  argc;
  26. char  *argv[];
  27. {
  28.     int  i;
  29.     int  status = 0;
  30. #ifdef MSDOS
  31.     extern int  _fmode;
  32.     _fmode = O_BINARY;
  33.     setmode(fileno(stdin), O_BINARY);
  34.     setmode(fileno(stdout), O_BINARY);
  35. #endif
  36.     if (argc < 2)
  37.         status += printp(NULL) == -1;
  38.     else
  39.         for (i = 1; i < argc; i++)
  40.             status += printp(argv[i]) == -1;
  41.     exit(status);
  42. }
  43.  
  44.  
  45. printp(fname)                           /* print a picture */
  46. char  *fname;
  47. {
  48.     FILE  *input;
  49.     int  xres, yres;
  50.     COLR  scanline[NCOLS];
  51.     int  i;
  52.     
  53.     if (fname == NULL) {
  54.         input = stdin;
  55.         fname = "<stdin>";
  56.     } else if ((input = fopen(fname, "r")) == NULL) {
  57.         fprintf(stderr, "%s: cannot open\n", fname);
  58.         return(-1);
  59.     }
  60.                 /* discard header */
  61.     if (checkheader(input, COLRFMT, NULL) < 0) {
  62.         fprintf(stderr, "%s: not a Radiance picture\n", fname);
  63.         return(-1);
  64.     }
  65.                 /* get picture dimensions */
  66.     if (fgetresolu(&xres, &yres, input) < 0) {
  67.         fprintf(stderr, "%s: bad picture size\n", fname);
  68.         return(-1);
  69.     }
  70.     if (xres > NCOLS) {
  71.         fprintf(stderr, "%s: resolution mismatch\n", fname);
  72.         return(-1);
  73.     }
  74.  
  75.     fputs("\033[6~\033[7z", stdout);
  76. #ifdef _IOLBF
  77.     stdout->_flag &= ~_IOLBF;
  78. #endif
  79.     
  80.     for (i = yres-1; i >= 0; i--) {
  81.         if (freadcolrs(scanline, xres, input) < 0) {
  82.             fprintf(stderr, "%s: read error (y=%d)\n", fname, i);
  83.             return(-1);
  84.         }
  85.         normcolrs(scanline, xres, 0);
  86.         plotscan(scanline, xres, i);
  87.     }
  88.  
  89.     fputs("\f\033[6~", stdout);
  90.  
  91.     fclose(input);
  92.  
  93.     return(0);
  94. }
  95.  
  96.  
  97. plotscan(scan, len, y)                  /* plot a scanline */
  98. COLR  scan[];
  99. int  len;
  100. int  y;
  101. {
  102.     static BYTE  pat[NCOLS];
  103.     int  bpos;
  104.     register int  i;
  105.  
  106.     if (bpos = y & 7) {
  107.  
  108.         for (i = 0; i < len; i++)
  109.             pat[i] |= bit(scan[i], i) << bpos;
  110.  
  111.     } else {
  112.  
  113.         fputs("\033%5", stdout);
  114.         putchar(len & 255);
  115.         putchar(len >> 8);
  116.  
  117.         for (i = 0; i < len; i++) {
  118.             pat[i] |= bit(scan[i], i);
  119.             putchar(pat[i]);
  120.             pat[i] = 0;
  121.         }
  122.         putchar('\r');
  123.         putchar('\n');
  124.         fflush(stdout);
  125.     }
  126. }
  127.  
  128.  
  129. bit(clr, x)                             /* return bit for color at x */
  130. COLR  clr;
  131. register int  x;
  132. {
  133.     static int  cerr[NCOLS];
  134.     static int  err;
  135.     int  b, errp;
  136.     register int  isblack;
  137.  
  138.     b = normbright(clr);
  139.     errp = err;
  140.     err += b + cerr[x];
  141.     isblack = err < 128;
  142.     if (!isblack) err -= 256;
  143.     err /= 3;
  144.     cerr[x] = err + errp;
  145.     return(isblack);
  146. }
  147.