1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.9 |
static const char RCSid[] = "$Id$"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
greg |
2.7 |
* oki20.c - program to dump pixel file to OkiMate 20 printer. |
6 |
greg |
2.1 |
*/ |
7 |
|
|
|
8 |
|
|
#include <stdio.h> |
9 |
greg |
2.6 |
#ifdef MSDOS |
10 |
|
|
#include <fcntl.h> |
11 |
|
|
#endif |
12 |
greg |
2.9 |
#include <time.h> |
13 |
greg |
2.1 |
|
14 |
|
|
#include "color.h" |
15 |
|
|
#include "resolu.h" |
16 |
|
|
|
17 |
greg |
2.6 |
#define NROWS 1440 /* 10" at 144 dpi */ |
18 |
|
|
#define NCOLS 960 /* 8" at 120 dpi */ |
19 |
greg |
2.1 |
|
20 |
greg |
2.6 |
#define ASPECT (120./144.) /* pixel aspect ratio */ |
21 |
greg |
2.1 |
|
22 |
greg |
2.6 |
#define FILTER "pfilt -1 -x %d -y %d -p %f %s",NCOLS,NROWS,ASPECT |
23 |
greg |
2.1 |
|
24 |
|
|
long lpat[NCOLS]; |
25 |
|
|
|
26 |
|
|
int dofilter = 0; /* filter through pfilt first? */ |
27 |
|
|
|
28 |
greg |
2.6 |
extern FILE *popen(); |
29 |
greg |
2.1 |
|
30 |
greg |
2.6 |
|
31 |
greg |
2.1 |
main(argc, argv) |
32 |
|
|
int argc; |
33 |
|
|
char *argv[]; |
34 |
|
|
{ |
35 |
|
|
int i, status = 0; |
36 |
greg |
2.6 |
#ifdef MSDOS |
37 |
|
|
extern int _fmode; |
38 |
|
|
_fmode = O_BINARY; |
39 |
|
|
setmode(fileno(stdin), O_BINARY); |
40 |
|
|
setmode(fileno(stdout), O_BINARY); |
41 |
|
|
#endif |
42 |
greg |
2.1 |
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 |
greg |
2.2 |
if (fname == NULL) { |
66 |
|
|
sprintf(buf, FILTER, ""); |
67 |
|
|
fname = "<stdin>"; |
68 |
|
|
} else |
69 |
|
|
sprintf(buf, FILTER, fname); |
70 |
greg |
2.1 |
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 |
greg |
2.3 |
if (xres > NCOLS) { |
92 |
greg |
2.1 |
fprintf(stderr, "%s: resolution mismatch\n", fname); |
93 |
|
|
return(-1); |
94 |
|
|
} |
95 |
|
|
/* set line spacing (overlap for knitting) */ |
96 |
greg |
2.4 |
fputs("\0333\042\022", stdout); |
97 |
greg |
2.1 |
/* 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 |
greg |
2.4 |
int bpos, start, end; |
124 |
greg |
2.1 |
register long c; |
125 |
|
|
register int i; |
126 |
|
|
|
127 |
greg |
2.4 |
bpos = y % 23; |
128 |
|
|
for (i = 0; i < len; i++) |
129 |
|
|
lpat[i] |= (long)bit(scan[i],i) << bpos; |
130 |
greg |
2.1 |
|
131 |
greg |
2.4 |
if (bpos) |
132 |
|
|
return; |
133 |
|
|
/* find limits of non-zero print buffer */ |
134 |
|
|
for (i = 0; lpat[i] == 0; i++) |
135 |
|
|
if (i == len-1) { |
136 |
|
|
putchar('\n'); |
137 |
|
|
return; |
138 |
greg |
2.1 |
} |
139 |
greg |
2.4 |
start = i - i%12; |
140 |
|
|
i = len; |
141 |
|
|
while (lpat[--i] == 0) |
142 |
|
|
; |
143 |
|
|
end = i; |
144 |
|
|
/* skip to start position */ |
145 |
|
|
for (i = start/12; i-- > 0; ) |
146 |
|
|
putchar(' '); |
147 |
|
|
/* print non-zero portion of buffer */ |
148 |
|
|
fputs("\033%O", stdout); |
149 |
|
|
i = end+1-start; |
150 |
|
|
putchar(i & 255); |
151 |
|
|
putchar(i >> 8); |
152 |
|
|
for (i = start; i <= end; i++) { |
153 |
|
|
c = lpat[i]; |
154 |
greg |
2.6 |
putchar((int)(c>>16)); |
155 |
|
|
putchar((int)(c>>8 & 255)); |
156 |
|
|
putchar((int)(c & 255)); |
157 |
greg |
2.5 |
if (y) /* repeat this row next time */ |
158 |
|
|
lpat[i] = (c & 1) << 23; |
159 |
|
|
else /* or clear for next image */ |
160 |
|
|
lpat[i] = 0L; |
161 |
greg |
2.1 |
} |
162 |
greg |
2.4 |
putchar('\r'); |
163 |
|
|
putchar('\n'); |
164 |
|
|
fflush(stdout); |
165 |
greg |
2.1 |
} |
166 |
|
|
|
167 |
|
|
|
168 |
|
|
bit(col, x) /* determine bit value for pixel at x */ |
169 |
|
|
COLR col; |
170 |
|
|
register int x; |
171 |
|
|
{ |
172 |
|
|
static int cerr[NCOLS]; |
173 |
|
|
static int err; |
174 |
|
|
int b, errp; |
175 |
|
|
register int ison; |
176 |
|
|
|
177 |
|
|
b = normbright(col); |
178 |
|
|
errp = err; |
179 |
|
|
err += b + cerr[x]; |
180 |
|
|
ison = err < 128; |
181 |
|
|
if (!ison) err -= 256; |
182 |
|
|
err /= 3; |
183 |
|
|
cerr[x] = err + errp; |
184 |
|
|
return(ison); |
185 |
|
|
} |