| 1 |
greg |
1.1 |
#ifndef lint |
| 2 |
schorsch |
2.4 |
static const char RCSid[] = "$Id: t4027.c,v 2.3 2004/01/02 12:46:13 schorsch Exp $"; |
| 3 |
greg |
1.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* t4027.c - program to dump pixel file to Tektronix 4027. |
| 6 |
|
|
* |
| 7 |
|
|
* 8/15/85 |
| 8 |
|
|
*/ |
| 9 |
|
|
|
| 10 |
|
|
#include <stdio.h> |
| 11 |
schorsch |
2.4 |
#ifndef _WIN32 |
| 12 |
|
|
#include <unistd.h> |
| 13 |
|
|
#endif |
| 14 |
greg |
1.1 |
|
| 15 |
|
|
#include "color.h" |
| 16 |
|
|
|
| 17 |
|
|
|
| 18 |
|
|
#define NROWS 462 |
| 19 |
|
|
#define NCOLS 640 |
| 20 |
|
|
|
| 21 |
|
|
#define COM 31 /* command character */ |
| 22 |
|
|
|
| 23 |
|
|
char *progname; |
| 24 |
|
|
|
| 25 |
schorsch |
2.4 |
static void plotscan(COLOR scan[], int len, int y); |
| 26 |
|
|
static int colormap(COLOR col); |
| 27 |
|
|
static void vector(int x0, int y0, int x1, int y1); |
| 28 |
|
|
static void setdcolr(int col); |
| 29 |
|
|
static void printpat(int p); |
| 30 |
|
|
static int rorb(int b); |
| 31 |
|
|
|
| 32 |
greg |
1.1 |
|
| 33 |
schorsch |
2.4 |
int |
| 34 |
|
|
main( |
| 35 |
|
|
int argc, |
| 36 |
|
|
char **argv |
| 37 |
|
|
) |
| 38 |
greg |
1.1 |
{ |
| 39 |
|
|
int xres, yres; |
| 40 |
|
|
COLOR scanline[NCOLS]; |
| 41 |
|
|
char sbuf[128]; |
| 42 |
|
|
FILE *fp; |
| 43 |
|
|
register int i; |
| 44 |
|
|
|
| 45 |
|
|
progname = argv[0]; |
| 46 |
|
|
|
| 47 |
|
|
if (argc < 2) |
| 48 |
|
|
fp = stdin; |
| 49 |
|
|
else if ((fp = fopen(argv[1], "r")) == NULL) { |
| 50 |
|
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
| 51 |
|
|
progname, argv[1]); |
| 52 |
|
|
exit(1); |
| 53 |
|
|
} |
| 54 |
|
|
/* discard header */ |
| 55 |
|
|
while (fgets(sbuf, sizeof(sbuf), fp) != NULL && sbuf[0] != '\n') |
| 56 |
|
|
; |
| 57 |
|
|
/* get picture dimensions */ |
| 58 |
|
|
if (fgets(sbuf, sizeof(sbuf), fp) == NULL || |
| 59 |
|
|
sscanf(sbuf, "-Y %d +X %d\n", &yres, &xres) != 2) { |
| 60 |
|
|
fprintf(stderr, "%s: bad picture size\n", progname); |
| 61 |
|
|
exit(1); |
| 62 |
|
|
} |
| 63 |
|
|
if (xres > NCOLS || yres > NROWS) { |
| 64 |
|
|
fprintf(stderr, "%s: resolution mismatch\n", progname); |
| 65 |
|
|
exit(1); |
| 66 |
|
|
} |
| 67 |
|
|
|
| 68 |
|
|
printf("%cMON1H", COM); |
| 69 |
|
|
printf("%cGRA1 33", COM); |
| 70 |
|
|
|
| 71 |
|
|
for (i = 0; i < yres; i++) { |
| 72 |
|
|
if (freadscan(scanline, xres, fp) < 0) { |
| 73 |
|
|
fprintf(stderr, "%s: read error\n", progname); |
| 74 |
|
|
exit(1); |
| 75 |
|
|
} |
| 76 |
|
|
plotscan(scanline, xres, NROWS-1-i); |
| 77 |
|
|
} |
| 78 |
|
|
fclose(fp); |
| 79 |
|
|
|
| 80 |
schorsch |
2.4 |
#ifndef _WIN32 /* XXX extract console functions */ |
| 81 |
greg |
1.1 |
if (isatty(fileno(stdout))) { |
| 82 |
|
|
printf("Hit return when done: "); |
| 83 |
|
|
fflush(stdout); |
| 84 |
|
|
fp = fopen("/dev/tty", "r"); |
| 85 |
|
|
getc(fp); |
| 86 |
|
|
fclose(fp); |
| 87 |
|
|
printf("%cMON34", COM); |
| 88 |
|
|
} |
| 89 |
|
|
putchar('\r'); |
| 90 |
schorsch |
2.4 |
#endif |
| 91 |
greg |
1.1 |
|
| 92 |
|
|
exit(0); |
| 93 |
schorsch |
2.4 |
return 0; /* pro forma return */ |
| 94 |
greg |
1.1 |
} |
| 95 |
|
|
|
| 96 |
|
|
|
| 97 |
schorsch |
2.4 |
static void |
| 98 |
|
|
plotscan( /* plot a scanline */ |
| 99 |
|
|
COLOR scan[], |
| 100 |
|
|
int len, |
| 101 |
|
|
int y |
| 102 |
|
|
) |
| 103 |
greg |
1.1 |
{ |
| 104 |
|
|
int color, lastcolor = -1; |
| 105 |
|
|
int lastpos = -1; |
| 106 |
|
|
register int i; |
| 107 |
|
|
|
| 108 |
|
|
for (i = 0; i < len; i++) { |
| 109 |
|
|
|
| 110 |
|
|
color = colormap(scan[i]); |
| 111 |
|
|
if (color != lastcolor) { |
| 112 |
|
|
if (lastpos >= 0) { |
| 113 |
|
|
setdcolr(lastcolor); |
| 114 |
|
|
vector(lastpos, y, i, y); |
| 115 |
|
|
} |
| 116 |
|
|
lastcolor = color; |
| 117 |
|
|
lastpos = i; |
| 118 |
|
|
} |
| 119 |
|
|
} |
| 120 |
|
|
setdcolr(lastcolor); |
| 121 |
|
|
vector(lastpos, y, len-1, y); |
| 122 |
|
|
} |
| 123 |
|
|
|
| 124 |
|
|
|
| 125 |
schorsch |
2.4 |
static int |
| 126 |
|
|
colormap( /* compute 4027 color value for col */ |
| 127 |
|
|
COLOR col |
| 128 |
|
|
) |
| 129 |
greg |
1.1 |
{ |
| 130 |
|
|
register int red, grn, blu; |
| 131 |
|
|
|
| 132 |
|
|
red = col[RED] >= 1.0 ? 3 : col[RED]*4; |
| 133 |
|
|
grn = col[GRN] >= 1.0 ? 3 : col[GRN]*4; |
| 134 |
|
|
blu = col[BLU] >= 1.0 ? 3 : col[BLU]*4; |
| 135 |
|
|
|
| 136 |
|
|
return(red<<4 | grn<<2 | blu); |
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
|
|
| 140 |
schorsch |
2.4 |
static void |
| 141 |
|
|
vector( /* output a vector */ |
| 142 |
|
|
int x0, |
| 143 |
|
|
int y0, |
| 144 |
|
|
int x1, |
| 145 |
|
|
int y1 |
| 146 |
|
|
) |
| 147 |
greg |
1.1 |
{ |
| 148 |
|
|
printf("%cVEC%d %d %d %d", COM, x0, y0, x1, y1); |
| 149 |
|
|
} |
| 150 |
|
|
|
| 151 |
|
|
|
| 152 |
schorsch |
2.4 |
static void |
| 153 |
|
|
setdcolr( /* set dithered pattern for terminal */ |
| 154 |
|
|
int col |
| 155 |
|
|
) |
| 156 |
greg |
1.1 |
{ |
| 157 |
|
|
static int cmask[3][4] = { |
| 158 |
|
|
{ 0, 0x88, 0xca, 0xff }, |
| 159 |
|
|
{ 0, 0x44, 0xb2, 0xff }, |
| 160 |
|
|
{ 0, 0x22, 0x2d, 0xff } |
| 161 |
|
|
}; |
| 162 |
schorsch |
2.3 |
/* isset is a common bitmap related macro in <sys/param.h> */ |
| 163 |
|
|
static short cisset[64]; |
| 164 |
greg |
1.1 |
int pat; |
| 165 |
|
|
register int r, g, b; |
| 166 |
|
|
|
| 167 |
schorsch |
2.3 |
if (cisset[col]) { |
| 168 |
greg |
1.1 |
printf("%cCOL P%d", COM, col); |
| 169 |
|
|
return; |
| 170 |
|
|
} |
| 171 |
|
|
|
| 172 |
|
|
printf("%cPAT P%d C7", COM, col); |
| 173 |
|
|
|
| 174 |
|
|
r = cmask[0][col>>4]; |
| 175 |
|
|
g = cmask[1][(col>>2) & 03]; |
| 176 |
|
|
b = cmask[2][col & 03]; |
| 177 |
|
|
|
| 178 |
|
|
pat = r & ~(g|b); |
| 179 |
|
|
if (pat) { |
| 180 |
|
|
printf(" C1"); /* red */ |
| 181 |
|
|
printpat(pat); |
| 182 |
|
|
} |
| 183 |
|
|
pat = g & ~(r|b); |
| 184 |
|
|
if (pat) { |
| 185 |
|
|
printf(" C2"); /* green */ |
| 186 |
|
|
printpat(pat); |
| 187 |
|
|
} |
| 188 |
|
|
pat = b & ~(r|g); |
| 189 |
|
|
if (pat) { |
| 190 |
|
|
printf(" C3"); /* blue */ |
| 191 |
|
|
printpat(pat); |
| 192 |
|
|
} |
| 193 |
|
|
pat = r & g & ~b; |
| 194 |
|
|
if (pat) { |
| 195 |
|
|
printf(" C4"); /* yellow */ |
| 196 |
|
|
printpat(pat); |
| 197 |
|
|
} |
| 198 |
|
|
pat = r & b & ~g; |
| 199 |
|
|
if (pat) { |
| 200 |
|
|
printf(" C6"); /* magenta */ |
| 201 |
|
|
printpat(pat); |
| 202 |
|
|
} |
| 203 |
|
|
pat = g & b & ~r; |
| 204 |
|
|
if (pat) { |
| 205 |
|
|
printf(" C5"); /* cyan */ |
| 206 |
|
|
printpat(pat); |
| 207 |
|
|
} |
| 208 |
|
|
pat = r & g & b; |
| 209 |
|
|
if (pat) { |
| 210 |
|
|
printf(" C0"); /* white */ |
| 211 |
|
|
printpat(pat); |
| 212 |
|
|
} |
| 213 |
schorsch |
2.3 |
cisset[col] = 1; |
| 214 |
greg |
1.1 |
printf("%cCOL P%d", COM, col); |
| 215 |
|
|
} |
| 216 |
|
|
|
| 217 |
|
|
|
| 218 |
schorsch |
2.4 |
static void |
| 219 |
|
|
printpat( /* print out a pattern */ |
| 220 |
|
|
register int p |
| 221 |
|
|
) |
| 222 |
greg |
1.1 |
{ |
| 223 |
|
|
register int i; |
| 224 |
|
|
|
| 225 |
|
|
for (i = 0; i < 14; i++) { /* 14 rows */ |
| 226 |
|
|
|
| 227 |
|
|
printf(",%d", p); |
| 228 |
|
|
p = rorb(rorb(rorb(p))); /* rotate 3 bits */ |
| 229 |
|
|
} |
| 230 |
|
|
} |
| 231 |
|
|
|
| 232 |
|
|
|
| 233 |
schorsch |
2.4 |
static int |
| 234 |
|
|
rorb( /* rotate a byte to the right */ |
| 235 |
|
|
register int b |
| 236 |
|
|
) |
| 237 |
greg |
1.1 |
{ |
| 238 |
|
|
b |= (b&01) << 8; |
| 239 |
|
|
b >>= 1; |
| 240 |
|
|
return(b); |
| 241 |
|
|
} |