| 1 | greg | 1.1 | #ifndef lint | 
| 2 | schorsch | 2.8 | static const char       RCSid[] = "$Id: mt160r.c,v 2.7 2003/06/05 19:29:34 schorsch Exp $"; | 
| 3 | greg | 1.1 | #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 | greg | 2.6 | #include  <time.h> | 
| 12 | greg | 1.1 |  | 
| 13 | schorsch | 2.7 | #include  "platform.h" | 
| 14 | greg | 1.1 | #include  "color.h" | 
| 15 | greg | 1.9 | #include  "resolu.h" | 
| 16 | greg | 1.1 |  | 
| 17 | greg | 2.4 | #define  NCOLS          880             /* for wide carriage */ | 
| 18 | greg | 1.1 |  | 
| 19 | schorsch | 2.8 | static int printp(char  *fname); | 
| 20 |  |  | static void plotscan(COLR  scan[], int  len, int  y); | 
| 21 |  |  | static int bit(COLR  clr, int  x); | 
| 22 | greg | 1.1 |  | 
| 23 | schorsch | 2.8 |  | 
| 24 |  |  | int | 
| 25 |  |  | main( | 
| 26 |  |  | int  argc, | 
| 27 |  |  | char  *argv[] | 
| 28 |  |  | ) | 
| 29 | greg | 1.1 | { | 
| 30 |  |  | int  i; | 
| 31 |  |  | int  status = 0; | 
| 32 | schorsch | 2.7 | SET_DEFAULT_BINARY(); | 
| 33 |  |  | SET_FILE_BINARY(stdin); | 
| 34 |  |  | SET_FILE_BINARY(stdout); | 
| 35 | greg | 1.1 | 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 | schorsch | 2.8 | static int | 
| 45 |  |  | printp(                         /* print a picture */ | 
| 46 |  |  | char  *fname | 
| 47 |  |  | ) | 
| 48 | greg | 1.1 | { | 
| 49 |  |  | FILE  *input; | 
| 50 |  |  | int  xres, yres; | 
| 51 | greg | 1.3 | COLR  scanline[NCOLS]; | 
| 52 | greg | 1.1 | int  i; | 
| 53 | greg | 2.4 |  | 
| 54 | greg | 1.1 | if (fname == NULL) { | 
| 55 |  |  | input = stdin; | 
| 56 |  |  | fname = "<stdin>"; | 
| 57 |  |  | } else if ((input = fopen(fname, "r")) == NULL) { | 
| 58 |  |  | fprintf(stderr, "%s: cannot open\n", fname); | 
| 59 |  |  | return(-1); | 
| 60 |  |  | } | 
| 61 |  |  | /* discard header */ | 
| 62 | greg | 1.7 | if (checkheader(input, COLRFMT, NULL) < 0) { | 
| 63 |  |  | fprintf(stderr, "%s: not a Radiance picture\n", fname); | 
| 64 |  |  | return(-1); | 
| 65 |  |  | } | 
| 66 | greg | 1.1 | /* get picture dimensions */ | 
| 67 | greg | 1.9 | if (fgetresolu(&xres, &yres, input) < 0) { | 
| 68 | greg | 1.1 | fprintf(stderr, "%s: bad picture size\n", fname); | 
| 69 |  |  | return(-1); | 
| 70 |  |  | } | 
| 71 |  |  | if (xres > NCOLS) { | 
| 72 |  |  | fprintf(stderr, "%s: resolution mismatch\n", fname); | 
| 73 |  |  | return(-1); | 
| 74 |  |  | } | 
| 75 |  |  |  | 
| 76 |  |  | fputs("\033[6~\033[7z", stdout); | 
| 77 |  |  |  | 
| 78 |  |  | for (i = yres-1; i >= 0; i--) { | 
| 79 | greg | 1.3 | if (freadcolrs(scanline, xres, input) < 0) { | 
| 80 | greg | 1.1 | fprintf(stderr, "%s: read error (y=%d)\n", fname, i); | 
| 81 |  |  | return(-1); | 
| 82 |  |  | } | 
| 83 | greg | 1.6 | normcolrs(scanline, xres, 0); | 
| 84 | greg | 1.1 | plotscan(scanline, xres, i); | 
| 85 |  |  | } | 
| 86 |  |  |  | 
| 87 |  |  | fputs("\f\033[6~", stdout); | 
| 88 |  |  |  | 
| 89 |  |  | fclose(input); | 
| 90 |  |  |  | 
| 91 |  |  | return(0); | 
| 92 |  |  | } | 
| 93 |  |  |  | 
| 94 |  |  |  | 
| 95 | schorsch | 2.8 | static void | 
| 96 |  |  | plotscan(                       /* plot a scanline */ | 
| 97 |  |  | COLR  scan[], | 
| 98 |  |  | int  len, | 
| 99 |  |  | int  y | 
| 100 |  |  | ) | 
| 101 | greg | 1.1 | { | 
| 102 |  |  | static BYTE  pat[NCOLS]; | 
| 103 |  |  | int  bpos; | 
| 104 |  |  | register int  i; | 
| 105 |  |  |  | 
| 106 | schorsch | 2.8 | if ((bpos = y & 7)) { | 
| 107 | greg | 1.1 |  | 
| 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 | greg | 2.3 | fflush(stdout); | 
| 125 | greg | 1.1 | } | 
| 126 |  |  | } | 
| 127 |  |  |  | 
| 128 |  |  |  | 
| 129 | schorsch | 2.8 | static int | 
| 130 |  |  | bit(                            /* return bit for color at x */ | 
| 131 |  |  | COLR  clr, | 
| 132 |  |  | register int  x | 
| 133 |  |  | ) | 
| 134 | greg | 1.1 | { | 
| 135 | greg | 1.3 | static int  cerr[NCOLS]; | 
| 136 | greg | 2.2 | static int  err; | 
| 137 |  |  | int  b, errp; | 
| 138 | greg | 1.1 | register int  isblack; | 
| 139 |  |  |  | 
| 140 | greg | 1.4 | b = normbright(clr); | 
| 141 | greg | 1.8 | errp = err; | 
| 142 | greg | 1.1 | err += b + cerr[x]; | 
| 143 | greg | 1.3 | isblack = err < 128; | 
| 144 |  |  | if (!isblack) err -= 256; | 
| 145 | greg | 1.8 | err /= 3; | 
| 146 |  |  | cerr[x] = err + errp; | 
| 147 | greg | 1.1 | return(isblack); | 
| 148 |  |  | } |