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