ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/oki20.c
Revision: 2.13
Committed: Sun Mar 28 20:33:14 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R6P1, rad3R6
Changes since 2.12: +25 -13 lines
Log Message:
Continued ANSIfication, and other fixes and clarifications.

File Contents

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