1 |
– |
/* Copyright (c) 1997 Silicon Graphics, Inc. */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ SGI"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Radiance picture to PostScript file translator -- one way! |
6 |
|
*/ |
8 |
|
#include <stdio.h> |
9 |
|
#include <math.h> |
10 |
|
#include <ctype.h> |
11 |
< |
#ifdef MSDOS |
12 |
< |
#include <fcntl.h> |
16 |
< |
#endif |
11 |
> |
|
12 |
> |
#include "platform.h" |
13 |
|
#include "color.h" |
14 |
|
|
15 |
|
#define UPPER(c) ((c)&~0x20) /* ASCII trick */ |
16 |
|
|
17 |
|
#define CODE6GAM 1.47 /* gamma for 6-bit codes */ |
18 |
< |
#define DEFDGAM 1.0 /* default device gamma */ |
18 |
> |
#define DEFGGAM 1.0 /* greyscale device gamma */ |
19 |
> |
#define DEFCGAM 1.8 /* color device gamma */ |
20 |
|
|
21 |
|
#define GRY -1 /* artificial index for grey */ |
22 |
|
|
39 |
|
int wrongformat = 0; /* input in wrong format? */ |
40 |
|
double pixaspect = 1.0; /* pixel aspect ratio */ |
41 |
|
|
42 |
< |
double devgam = DEFDGAM; /* device gamma response */ |
42 |
> |
double devgam = 0.; /* device gamma response */ |
43 |
|
double hmarg = DEFMARG, |
44 |
|
vmarg = DEFMARG; /* horizontal and vertical margins */ |
45 |
|
double width = DEFWIDTH, |
46 |
|
height = DEFHEIGHT; /* default paper width and height */ |
47 |
< |
int docolor = 0; /* produce color image? */ |
47 |
> |
double dpi = 0; /* print density (0 if unknown) */ |
48 |
> |
int docolor = 1; /* produce color image? */ |
49 |
|
int bradj = 0; /* brightness adjustment */ |
50 |
|
int ncopies = 1; /* number of copies */ |
51 |
|
|
55 |
|
|
56 |
|
char *progname; |
57 |
|
|
58 |
< |
int xmax, ymax; |
58 |
> |
int xmax, ymax; /* input image dimensions */ |
59 |
|
|
62 |
– |
extern char *malloc(); |
60 |
|
extern double unit2inch(); |
61 |
|
|
62 |
|
|
63 |
+ |
int |
64 |
|
headline(s) /* check header line */ |
65 |
|
char *s; |
66 |
|
{ |
71 |
|
wrongformat = strcmp(fmt, COLRFMT); |
72 |
|
} else if (isaspect(s)) |
73 |
|
pixaspect *= aspectval(s); |
74 |
+ |
return(0); |
75 |
|
} |
76 |
|
|
77 |
|
|
102 |
|
case 'C': /* compressed ASCII encoding */ |
103 |
|
putprim = Cputprim; |
104 |
|
break; |
105 |
+ |
case 'd': /* print density */ |
106 |
+ |
dpi = atof(argv[++i]); |
107 |
+ |
break; |
108 |
|
case 'g': /* device gamma adjustment */ |
109 |
|
devgam = atof(argv[++i]); |
110 |
|
break; |
155 |
|
progname, argv[i+1]); |
156 |
|
exit(1); |
157 |
|
} |
158 |
< |
#ifdef MSDOS |
157 |
< |
setmode(fileno(stdin), O_BINARY); |
158 |
< |
#endif |
158 |
> |
SET_FILE_BINARY(stdin); |
159 |
|
/* get our header */ |
160 |
|
getheader(stdin, headline, NULL); |
161 |
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) |
162 |
|
quiterr("bad picture format"); |
163 |
|
/* gamma compression */ |
164 |
+ |
if (devgam <= 0.05) |
165 |
+ |
devgam = docolor ? DEFCGAM : DEFGGAM; |
166 |
|
if (putprim == Cputprim) |
167 |
< |
setcolrgam(CODE6GAM/devgam); |
167 |
> |
setcolrgam(CODE6GAM); |
168 |
|
else if (devgam != 1.) |
169 |
< |
setcolrgam(1./devgam); |
169 |
> |
setcolrgam(devgam); |
170 |
|
/* write header */ |
171 |
< |
PSheader(i <= argc-1 ? argv[i] : "<stdin>"); |
171 |
> |
PSheader(argc, argv); |
172 |
|
/* convert file */ |
173 |
|
ra2ps(); |
174 |
|
/* write trailer */ |
176 |
|
exit(0); |
177 |
|
userr: |
178 |
|
fprintf(stderr, |
179 |
< |
"Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-g gamma] [input [output]]\n", |
179 |
> |
"Usage: %s [-b|c][-A|B|C][-e +/-stops][-p paper][-m[h|v] margin][-d dpi][-g gamma] [input [output]]\n", |
180 |
|
progname); |
181 |
|
exit(1); |
182 |
|
} |
186 |
|
unit2inch(s) /* determine unit */ |
187 |
|
register char *s; |
188 |
|
{ |
189 |
< |
static struct unit {char n; float f} u[] = { |
189 |
> |
static struct unit {char n; float f;} u[] = { |
190 |
|
'i', 1., |
191 |
|
'm', 1./25.4, |
192 |
|
'c', 1./2.54, |
225 |
|
char *ps; |
226 |
|
{ |
227 |
|
static struct psize {char n[12]; float w,h;} p[] = { |
228 |
+ |
"envelope", 4.12, 9.5, |
229 |
+ |
"executive", 7.25, 10.5, |
230 |
|
"letter", 8.5, 11., |
231 |
+ |
"lettersmall", 7.68, 10.16, |
232 |
|
"legal", 8.5, 14., |
228 |
– |
"executive", 7.25, 10.5, |
229 |
– |
"envelope", 4.12, 9.5, |
233 |
|
"monarch", 3.87, 7.5, |
234 |
+ |
"statement", 5.5, 8.5, |
235 |
|
"tabloid", 11., 17., |
236 |
|
"A3", 11.69, 16.54, |
237 |
|
"A4", 8.27, 11.69, |
238 |
+ |
"A4small", 7.47, 10.85, |
239 |
|
"A5", 6.00, 8.27, |
240 |
|
"A6", 4.13, 6.00, |
241 |
|
"B4", 10.12, 14.33, |
242 |
|
"B5", 7.17, 10.12, |
238 |
– |
"DL", 4.33, 8.66, |
243 |
|
"C5", 6.38, 9.01, |
244 |
|
"C6", 4.49, 6.38, |
245 |
+ |
"DL", 4.33, 8.66, |
246 |
|
"hagaki", 3.94, 5.83, |
247 |
|
"" }; |
248 |
|
register struct psize *pp; |
271 |
|
fprintf(stderr, "_Name________Width_Height_(inches)\n"); |
272 |
|
for (pp = p; pp->n[0]; pp++) |
273 |
|
fprintf(stderr, "%-11s %5.2f %5.2f\n", pp->n, pp->w, pp->h); |
274 |
+ |
fprintf(stderr, "Or use WWxHH size specification\n"); |
275 |
|
exit(1); |
276 |
|
} |
277 |
|
|
287 |
|
} |
288 |
|
|
289 |
|
|
290 |
< |
PSheader(name) /* print PostScript header */ |
291 |
< |
char *name; |
290 |
> |
PSheader(ac, av) /* print PostScript header */ |
291 |
> |
int ac; |
292 |
> |
char **av; |
293 |
|
{ |
294 |
|
char *rstr; |
295 |
< |
int landscape = 0; |
295 |
> |
int landscape, rotate, n; |
296 |
|
double pwidth, pheight; |
297 |
|
double iwidth, iheight; |
298 |
|
/* EPS comments */ |
299 |
|
puts("%!PS-Adobe-2.0 EPSF-2.0"); |
300 |
< |
printf("%%%%Title: %s\n", name); |
301 |
< |
printf("%%%%Creator: %s = %s\n", progname, SCCSid); |
300 |
> |
printf("%%%%Title: "); printargs(ac, av, stdout); |
301 |
> |
printf("%%%%Creator: %s\n", progname); |
302 |
|
printf("%%%%Pages: %d\n", ncopies); |
303 |
|
if (landscape = xmax > pixaspect*ymax) |
304 |
|
puts("%%Orientation: Landscape"); |
305 |
|
else |
306 |
|
puts("%%Orientation: Portrait"); |
307 |
< |
if (PWIDTH > PHEIGHT ^ landscape) { |
307 |
> |
if (rotate = PWIDTH > PHEIGHT ^ landscape) { |
308 |
|
pwidth = PHEIGHT; |
309 |
|
pheight = PWIDTH; |
310 |
|
} else { |
311 |
|
pwidth = PWIDTH; |
312 |
|
pheight = PHEIGHT; |
313 |
|
} |
314 |
< |
if (pheight/pwidth > pixaspect*ymax/xmax) { |
315 |
< |
iwidth = pwidth; |
316 |
< |
iheight = pwidth*pixaspect*ymax/xmax; |
317 |
< |
} else { |
318 |
< |
iheight = pheight; |
319 |
< |
iwidth = pheight*xmax/(pixaspect*ymax); |
320 |
< |
} |
321 |
< |
if (pwidth == PHEIGHT) |
314 |
> |
if (dpi > 100 && pixaspect >= 0.99 & pixaspect <= 1.01) |
315 |
> |
if (pheight/pwidth > ymax/xmax) { |
316 |
> |
n = pwidth*dpi/xmax; /* floor */ |
317 |
> |
iwidth = n > 0 ? (double)(n*xmax)/dpi : pwidth; |
318 |
> |
iheight = iwidth*ymax/xmax; |
319 |
> |
} else { |
320 |
> |
n = pheight*dpi/ymax; /* floor */ |
321 |
> |
iheight = n > 0 ? (double)(n*ymax)/dpi : pheight; |
322 |
> |
iwidth = iheight*xmax/ymax; |
323 |
> |
} |
324 |
> |
else |
325 |
> |
if (pheight/pwidth > pixaspect*ymax/xmax) { |
326 |
> |
iwidth = pwidth; |
327 |
> |
iheight = iwidth*pixaspect*ymax/xmax; |
328 |
> |
} else { |
329 |
> |
iheight = pheight; |
330 |
> |
iwidth = iheight*xmax/(pixaspect*ymax); |
331 |
> |
} |
332 |
> |
if (rotate) |
333 |
|
printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", |
334 |
|
HMARGIN+(pheight-iheight)*.5, |
335 |
|
VMARGIN+(pwidth-iwidth)*.5, |
356 |
|
PSprocdef("read6bitRLE"); |
357 |
|
/* set up transformation matrix */ |
358 |
|
printf("%f %f translate\n", HMARGIN, VMARGIN); |
359 |
< |
if (pwidth == PHEIGHT) { |
359 |
> |
if (rotate) { |
360 |
|
printf("%f 0 translate\n", PWIDTH); |
361 |
|
puts("90 rotate"); |
362 |
|
} |
407 |
|
for (i = 0; i < 128; i++) /* clear */ |
408 |
|
itab[i] = -1; |
409 |
|
for (i = 1; i < 63; i++) /* assign greys */ |
410 |
< |
itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM); |
410 |
> |
itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM/devgam); |
411 |
|
itab[code[0]] = 0; /* black is black */ |
412 |
|
itab[code[63]] = 255; /* and white is white */ |
413 |
|
printf("/codetab ["); |
467 |
|
} |
468 |
|
putchar('\n'); |
469 |
|
/* free scanline */ |
470 |
< |
free((char *)scanin); |
470 |
> |
free((void *)scanin); |
471 |
|
} |
472 |
|
|
473 |
|
|