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