| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: mt160r.c,v 2.8 2004/03/28 20:33:14 schorsch Exp $";
|
| 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 |
#include <time.h>
|
| 12 |
|
| 13 |
#include "platform.h"
|
| 14 |
#include "color.h"
|
| 15 |
#include "resolu.h"
|
| 16 |
|
| 17 |
#define NCOLS 880 /* for wide carriage */
|
| 18 |
|
| 19 |
static int printp(char *fname);
|
| 20 |
static void plotscan(COLR scan[], int len, int y);
|
| 21 |
static int bit(COLR clr, int x);
|
| 22 |
|
| 23 |
|
| 24 |
int
|
| 25 |
main(
|
| 26 |
int argc,
|
| 27 |
char *argv[]
|
| 28 |
)
|
| 29 |
{
|
| 30 |
int i;
|
| 31 |
int status = 0;
|
| 32 |
SET_DEFAULT_BINARY();
|
| 33 |
SET_FILE_BINARY(stdin);
|
| 34 |
SET_FILE_BINARY(stdout);
|
| 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 |
static int
|
| 45 |
printp( /* print a picture */
|
| 46 |
char *fname
|
| 47 |
)
|
| 48 |
{
|
| 49 |
FILE *input;
|
| 50 |
int xres, yres;
|
| 51 |
COLR scanline[NCOLS];
|
| 52 |
int i;
|
| 53 |
|
| 54 |
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 |
if (checkheader(input, COLRFMT, NULL) < 0) {
|
| 63 |
fprintf(stderr, "%s: not a Radiance picture\n", fname);
|
| 64 |
return(-1);
|
| 65 |
}
|
| 66 |
/* get picture dimensions */
|
| 67 |
if (fgetresolu(&xres, &yres, input) < 0) {
|
| 68 |
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 |
if (freadcolrs(scanline, xres, input) < 0) {
|
| 80 |
fprintf(stderr, "%s: read error (y=%d)\n", fname, i);
|
| 81 |
return(-1);
|
| 82 |
}
|
| 83 |
normcolrs(scanline, xres, 0);
|
| 84 |
plotscan(scanline, xres, i);
|
| 85 |
}
|
| 86 |
|
| 87 |
fputs("\f\033[6~", stdout);
|
| 88 |
|
| 89 |
fclose(input);
|
| 90 |
|
| 91 |
return(0);
|
| 92 |
}
|
| 93 |
|
| 94 |
|
| 95 |
static void
|
| 96 |
plotscan( /* plot a scanline */
|
| 97 |
COLR scan[],
|
| 98 |
int len,
|
| 99 |
int y
|
| 100 |
)
|
| 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 |
static int
|
| 130 |
bit( /* return bit for color at x */
|
| 131 |
COLR clr,
|
| 132 |
register int x
|
| 133 |
)
|
| 134 |
{
|
| 135 |
static int cerr[NCOLS];
|
| 136 |
static int err;
|
| 137 |
int b, errp;
|
| 138 |
register int isblack;
|
| 139 |
|
| 140 |
b = normbright(clr);
|
| 141 |
errp = err;
|
| 142 |
err += b + cerr[x];
|
| 143 |
isblack = err < 128;
|
| 144 |
if (!isblack) err -= 256;
|
| 145 |
err /= 3;
|
| 146 |
cerr[x] = err + errp;
|
| 147 |
return(isblack);
|
| 148 |
}
|