1 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* oki20c.c - program to dump pixel file to OkiMate 20 color printer. |
9 |
*/ |
10 |
|
11 |
#include <stdio.h> |
12 |
#ifdef MSDOS |
13 |
#include <fcntl.h> |
14 |
#endif |
15 |
|
16 |
#include "color.h" |
17 |
#include "resolu.h" |
18 |
|
19 |
#define NROWS 1440 /* 10" at 144 dpi */ |
20 |
#define NCOLS 960 /* 8" at 120 dpi */ |
21 |
|
22 |
#define ASPECT (120./144.) /* pixel aspect ratio */ |
23 |
|
24 |
#define FILTER "pfilt -1 -x %d -y %d -p %f %s",NCOLS,NROWS,ASPECT |
25 |
|
26 |
/* |
27 |
* Subtractive primaries are ordered: Yellow, Magenta, Cyan. |
28 |
*/ |
29 |
|
30 |
#define sub_add(sub) (2-(sub)) /* map subtractive to additive pri. */ |
31 |
|
32 |
long lpat[NCOLS][3]; |
33 |
|
34 |
int dofilter = 0; /* filter through pfilt first? */ |
35 |
|
36 |
extern FILE *popen(); |
37 |
|
38 |
|
39 |
main(argc, argv) |
40 |
int argc; |
41 |
char *argv[]; |
42 |
{ |
43 |
int i, status = 0; |
44 |
#ifdef MSDOS |
45 |
extern int _fmode; |
46 |
_fmode = O_BINARY; |
47 |
setmode(fileno(stdin), O_BINARY); |
48 |
setmode(fileno(stdout), O_BINARY); |
49 |
#endif |
50 |
if (argc > 1 && !strcmp(argv[1], "-p")) { |
51 |
dofilter++; |
52 |
argv++; argc--; |
53 |
} |
54 |
if (argc < 2) |
55 |
status = printp(NULL) == -1; |
56 |
else |
57 |
for (i = 1; i < argc; i++) |
58 |
status += printp(argv[i]) == -1; |
59 |
exit(status); |
60 |
} |
61 |
|
62 |
|
63 |
printp(fname) /* print a picture */ |
64 |
char *fname; |
65 |
{ |
66 |
char buf[64]; |
67 |
FILE *input; |
68 |
int xres, yres; |
69 |
COLR scanline[NCOLS]; |
70 |
int i; |
71 |
|
72 |
if (dofilter) { |
73 |
if (fname == NULL) { |
74 |
sprintf(buf, FILTER, ""); |
75 |
fname = "<stdin>"; |
76 |
} else |
77 |
sprintf(buf, FILTER, fname); |
78 |
if ((input = popen(buf, "r")) == NULL) { |
79 |
fprintf(stderr, "Cannot execute: %s\n", buf); |
80 |
return(-1); |
81 |
} |
82 |
} else if (fname == NULL) { |
83 |
input = stdin; |
84 |
fname = "<stdin>"; |
85 |
} else if ((input = fopen(fname, "r")) == NULL) { |
86 |
fprintf(stderr, "%s: cannot open\n", fname); |
87 |
return(-1); |
88 |
} |
89 |
/* discard header */ |
90 |
if (checkheader(input, COLRFMT, NULL) < 0) { |
91 |
fprintf(stderr, "%s: not a Radiance picture\n", fname); |
92 |
return(-1); |
93 |
} |
94 |
/* get picture dimensions */ |
95 |
if (fgetresolu(&xres, &yres, input) < 0) { |
96 |
fprintf(stderr, "%s: bad picture size\n", fname); |
97 |
return(-1); |
98 |
} |
99 |
if (xres > NCOLS) { |
100 |
fprintf(stderr, "%s: resolution mismatch\n", fname); |
101 |
return(-1); |
102 |
} |
103 |
/* set line spacing (overlap for knitting) */ |
104 |
fputs("\0333\042", stdout); |
105 |
/* put out scanlines */ |
106 |
for (i = yres-1; i >= 0; i--) { |
107 |
if (freadcolrs(scanline, xres, input) < 0) { |
108 |
fprintf(stderr, "%s: read error (y=%d)\n", fname, i); |
109 |
return(-1); |
110 |
} |
111 |
normcolrs(scanline, xres, 0); |
112 |
plotscan(scanline, xres, i); |
113 |
} |
114 |
/* advance page */ |
115 |
putchar('\f'); |
116 |
|
117 |
if (dofilter) |
118 |
pclose(input); |
119 |
else |
120 |
fclose(input); |
121 |
|
122 |
return(0); |
123 |
} |
124 |
|
125 |
|
126 |
plotscan(scan, len, y) /* plot a scanline */ |
127 |
COLR scan[]; |
128 |
int len; |
129 |
int y; |
130 |
{ |
131 |
int bpos; |
132 |
register long c; |
133 |
register int i, j; |
134 |
|
135 |
if (bpos = y % 23) { |
136 |
|
137 |
for (j = 0; j < 3; j++) |
138 |
for (i = 0; i < len; i++) |
139 |
lpat[i][j] |= (long)colbit(scan[i],i,j) << bpos; |
140 |
return; |
141 |
} |
142 |
fputs("\033\031", stdout); |
143 |
|
144 |
for (j = 0; j < 3; j++) { |
145 |
i = (NCOLS + len)/2; /* center image */ |
146 |
fputs("\033%O", stdout); |
147 |
putchar(i & 255); |
148 |
putchar(i >> 8); |
149 |
while (i-- > len) { |
150 |
putchar(0); |
151 |
putchar(0); |
152 |
putchar(0); |
153 |
} |
154 |
for (i = 0; i < len; i++) { |
155 |
c = lpat[i][j] | colbit(scan[i],i,j); |
156 |
putchar((int)(c>>16)); |
157 |
putchar((int)(c>>8 & 255)); |
158 |
putchar((int)(c & 255)); |
159 |
if (y) /* repeat this row */ |
160 |
lpat[i][j] = (c & 1) << 23; |
161 |
else /* or clear for next image */ |
162 |
lpat[i][j] = 0L; |
163 |
} |
164 |
putchar('\r'); |
165 |
} |
166 |
putchar('\n'); |
167 |
fflush(stdout); |
168 |
} |
169 |
|
170 |
|
171 |
colbit(col, x, s) /* determine bit value for primary at x */ |
172 |
COLR col; |
173 |
register int x; |
174 |
int s; |
175 |
{ |
176 |
static int cerr[NCOLS][3]; |
177 |
static int err[3]; |
178 |
int b, errp; |
179 |
register int a, ison; |
180 |
|
181 |
a = sub_add(s); /* use additive primary */ |
182 |
b = col[a]; |
183 |
errp = err[a]; |
184 |
err[a] += b + cerr[x][a]; |
185 |
ison = err[a] < 128; |
186 |
if (!ison) err[a] -= 256; |
187 |
err[a] /= 3; |
188 |
cerr[x][a] = err[a] + errp; |
189 |
return(ison); |
190 |
} |