ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/oki20.c
Revision: 2.12
Committed: Mon Nov 10 12:28:56 2003 UTC (20 years, 5 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.11: +6 -5 lines
Log Message:
Fixed size of command line buffers, and allow spaces in input file paths.

File Contents

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