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

  1. /* Copyright (c) 1991 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)paintjet.c 2.2 12/23/91 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  paintjet.c - program to dump pixel file to HP PaintJet color printer.
  9.  *
  10.  *     10/6/89
  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          1440            /* 8" at 180 dpi */
  22.  
  23.  
  24. main(argc, argv)
  25. int  argc;
  26. char  *argv[];
  27. {
  28.     int  i, status = 0;
  29. #ifdef MSDOS
  30.     extern int  _fmode;
  31.     _fmode = O_BINARY;
  32.     setmode(fileno(stdin), O_BINARY);
  33.     setmode(fileno(stdout), O_BINARY);
  34. #endif
  35.     if (argc < 2)
  36.         status = printp(NULL) == -1;
  37.     else
  38.         for (i = 1; i < argc; i++)
  39.             status += printp(argv[i]) == -1;
  40.     exit(status);
  41. }
  42.  
  43.  
  44. printp(fname)                           /* print a picture */
  45. char  *fname;
  46. {
  47.     FILE  *input;
  48.     int  xres, yres;
  49.     COLR  scanline[NCOLS];
  50.     int  i;
  51.  
  52.     if (fname == NULL) {
  53.         input = stdin;
  54.         fname = "<stdin>";
  55.     } else if ((input = fopen(fname, "r")) == NULL) {
  56.         fprintf(stderr, "%s: cannot open\n", fname);
  57.         return(-1);
  58.     }
  59.                 /* discard header */
  60.     if (checkheader(input, COLRFMT, NULL) < 0) {
  61.         fprintf(stderr, "%s: not a Radiance picture\n", fname);
  62.         return(-1);
  63.     }
  64.                 /* get picture dimensions */
  65.     if (fgetresolu(&xres, &yres, input) < 0) {
  66.         fprintf(stderr, "%s: bad picture size\n", fname);
  67.         return(-1);
  68.     }
  69.     if (xres > NCOLS) {
  70.         fprintf(stderr, "%s: resolution mismatch\n", fname);
  71.         return(-1);
  72.     }
  73.     /*
  74.      * Set 180 dpi, 3 planes, left margin and width.
  75.      */
  76.     printf("\033*t180R\033*r3U\033&a%dH\033*r%dS\033*r1A",
  77.             ((NCOLS-xres)>>4)<<5, xres);
  78.     
  79.     for (i = yres-1; i >= 0; i--) {
  80.         if (freadcolrs(scanline, xres, input) < 0)
  81.             return(-1);
  82.         normcolrs(scanline, xres, 0);
  83.         plotscan(scanline, xres, i);
  84.     }
  85.  
  86.     printf("\033*r0B\f");
  87.     
  88.     fclose(input);
  89.  
  90.     return(0);
  91. }
  92.  
  93.  
  94. plotscan(scan, len, y)                  /* plot a scanline */
  95. COLR  scan[];
  96. int  len;
  97. int  y;
  98. {
  99.     int  c;
  100.     register int  x, b;
  101.  
  102.     for (c = 0; c < 3; c++) {
  103.         printf("\033*b%d%c", (len+7)>>3, c<2 ? 'V' : 'W');
  104.         b = 0;
  105.         for (x = 0; x < len; x++) {
  106.             b = b<<1 | colbit(scan[x], x, c);
  107.             if ((x&7) == 7) {
  108.                 putc(b, stdout);
  109.                 b = 0;
  110.             }
  111.         }
  112.         if (x & 7)
  113.             putc(b, stdout);
  114.     }
  115. }
  116.  
  117.  
  118. colbit(col, x, a)               /* determine bit value for primary at x */
  119. COLR  col;
  120. register int  x;
  121. register int  a;
  122. {
  123.     static int  cerr[NCOLS][3];
  124.     static int  err[3];
  125.     int  b, errp;
  126.     register int  ison;
  127.  
  128.     b = col[a];
  129.     errp = err[a];
  130.     err[a] += b + cerr[x][a];
  131.     ison = err[a] > 128;
  132.     if (ison) err[a] -= 256;
  133.     err[a] /= 3;
  134.     cerr[x][a] = err[a] + errp;
  135.     return(ison);
  136. }
  137.