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 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
+ |
#include <string.h> |
10 |
|
#include <math.h> |
11 |
|
#include <ctype.h> |
12 |
< |
#ifdef MSDOS |
13 |
< |
#include <fcntl.h> |
16 |
< |
#endif |
12 |
> |
|
13 |
> |
#include "platform.h" |
14 |
|
#include "color.h" |
15 |
+ |
#include "resolu.h" |
16 |
|
|
17 |
|
#define UPPER(c) ((c)&~0x20) /* ASCII trick */ |
18 |
|
|
19 |
|
#define CODE6GAM 1.47 /* gamma for 6-bit codes */ |
20 |
< |
#define DEFDGAM 1.0 /* default device gamma */ |
20 |
> |
#define DEFGGAM 1.0 /* greyscale device gamma */ |
21 |
> |
#define DEFCGAM 1.8 /* color device gamma */ |
22 |
|
|
23 |
|
#define GRY -1 /* artificial index for grey */ |
24 |
|
|
41 |
|
int wrongformat = 0; /* input in wrong format? */ |
42 |
|
double pixaspect = 1.0; /* pixel aspect ratio */ |
43 |
|
|
44 |
< |
double devgam = DEFDGAM; /* device gamma response */ |
44 |
> |
double devgam = 0.; /* device gamma response */ |
45 |
|
double hmarg = DEFMARG, |
46 |
|
vmarg = DEFMARG; /* horizontal and vertical margins */ |
47 |
|
double width = DEFWIDTH, |
48 |
|
height = DEFHEIGHT; /* default paper width and height */ |
49 |
|
double dpi = 0; /* print density (0 if unknown) */ |
50 |
< |
int docolor = 0; /* produce color image? */ |
50 |
> |
int docolor = 1; /* produce color image? */ |
51 |
|
int bradj = 0; /* brightness adjustment */ |
52 |
|
int ncopies = 1; /* number of copies */ |
53 |
|
|
54 |
< |
extern int Aputprim(), Bputprim(), Cputprim(); |
54 |
> |
char *progname; |
55 |
> |
int xmax, ymax; /* input image dimensions */ |
56 |
|
|
57 |
< |
int (*putprim)() = Aputprim; /* function for writing scanline */ |
57 |
> |
typedef void putprimf_t(COLR *scn, int pri); |
58 |
|
|
59 |
< |
char *progname; |
59 |
> |
static gethfunc headline; |
60 |
> |
static putprimf_t Aputprim, Bputprim, Cputprim; |
61 |
|
|
62 |
< |
int xmax, ymax; /* input image dimensions */ |
62 |
> |
static double unit2inch(register char *s); |
63 |
> |
static int matchid(char *name, char *id); |
64 |
> |
static void parsepaper(char *ps); |
65 |
> |
static void quiterr(char *err); |
66 |
> |
static void PSheader(int ac, char **av); |
67 |
> |
static void PStrailer(void); |
68 |
> |
static void PSprocdef(char *nam); |
69 |
> |
static void ra2ps(void); |
70 |
> |
static void putrle(int cnt, int cod); |
71 |
|
|
63 |
– |
extern char *malloc(); |
64 |
– |
extern double unit2inch(); |
72 |
|
|
73 |
+ |
putprimf_t *putprim = Aputprim; /* function for writing scanline */ |
74 |
|
|
75 |
< |
headline(s) /* check header line */ |
76 |
< |
char *s; |
75 |
> |
|
76 |
> |
static int |
77 |
> |
headline( /* check header line */ |
78 |
> |
char *s, |
79 |
> |
void *p |
80 |
> |
) |
81 |
|
{ |
82 |
|
char fmt[32]; |
83 |
|
|
86 |
|
wrongformat = strcmp(fmt, COLRFMT); |
87 |
|
} else if (isaspect(s)) |
88 |
|
pixaspect *= aspectval(s); |
89 |
+ |
return(0); |
90 |
|
} |
91 |
|
|
92 |
< |
|
93 |
< |
main(argc, argv) |
81 |
< |
int argc; |
82 |
< |
char *argv[]; |
92 |
> |
int |
93 |
> |
main(int argc, char *argv[]) |
94 |
|
{ |
95 |
|
int i; |
96 |
|
double d; |
168 |
|
progname, argv[i+1]); |
169 |
|
exit(1); |
170 |
|
} |
171 |
< |
#ifdef MSDOS |
161 |
< |
setmode(fileno(stdin), O_BINARY); |
162 |
< |
#endif |
171 |
> |
SET_FILE_BINARY(stdin); |
172 |
|
/* get our header */ |
173 |
|
getheader(stdin, headline, NULL); |
174 |
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) |
175 |
|
quiterr("bad picture format"); |
176 |
|
/* gamma compression */ |
177 |
+ |
if (devgam <= 0.05) |
178 |
+ |
devgam = docolor ? DEFCGAM : DEFGGAM; |
179 |
|
if (putprim == Cputprim) |
180 |
|
setcolrgam(CODE6GAM); |
181 |
|
else if (devgam != 1.) |
182 |
|
setcolrgam(devgam); |
183 |
|
/* write header */ |
184 |
< |
PSheader(i <= argc-1 ? argv[i] : "<stdin>"); |
184 |
> |
PSheader(argc, argv); |
185 |
|
/* convert file */ |
186 |
|
ra2ps(); |
187 |
|
/* write trailer */ |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
< |
double |
199 |
< |
unit2inch(s) /* determine unit */ |
200 |
< |
register char *s; |
198 |
> |
static double |
199 |
> |
unit2inch( /* determine unit */ |
200 |
> |
register char *s |
201 |
> |
) |
202 |
|
{ |
203 |
< |
static struct unit {char n; float f} u[] = { |
204 |
< |
'i', 1., |
205 |
< |
'm', 1./25.4, |
206 |
< |
'c', 1./2.54, |
207 |
< |
'\0' }; |
203 |
> |
static struct unit {char n; float f;} u[] = { |
204 |
> |
{'i', 1.}, |
205 |
> |
{'m', 1./25.4}, |
206 |
> |
{'c', 1./2.54}, |
207 |
> |
{'\0',0} }; |
208 |
|
register struct unit *up; |
209 |
|
|
210 |
|
while (*s && !isalpha(*s)) |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
< |
int |
220 |
< |
matchid(name, id) /* see if name matches id (case insensitive) */ |
221 |
< |
char *name; |
222 |
< |
register char *id; |
219 |
> |
static int |
220 |
> |
matchid( /* see if name matches id (case insensitive) */ |
221 |
> |
char *name, |
222 |
> |
register char *id |
223 |
> |
) |
224 |
|
{ |
225 |
|
register char *s = name; |
226 |
|
|
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
< |
parsepaper(ps) /* determine paper size from name */ |
240 |
< |
char *ps; |
239 |
> |
static void |
240 |
> |
parsepaper( /* determine paper size from name */ |
241 |
> |
char *ps |
242 |
> |
) |
243 |
|
{ |
244 |
|
static struct psize {char n[12]; float w,h;} p[] = { |
245 |
< |
"envelope", 4.12, 9.5, |
246 |
< |
"executive", 7.25, 10.5, |
247 |
< |
"letter", 8.5, 11., |
248 |
< |
"lettersmall", 7.68, 10.16, |
249 |
< |
"legal", 8.5, 14., |
250 |
< |
"monarch", 3.87, 7.5, |
251 |
< |
"statement", 5.5, 8.5, |
252 |
< |
"tabloid", 11., 17., |
253 |
< |
"A3", 11.69, 16.54, |
254 |
< |
"A4", 8.27, 11.69, |
255 |
< |
"A4small", 7.47, 10.85, |
256 |
< |
"A5", 6.00, 8.27, |
257 |
< |
"A6", 4.13, 6.00, |
258 |
< |
"B4", 10.12, 14.33, |
259 |
< |
"B5", 7.17, 10.12, |
260 |
< |
"C5", 6.38, 9.01, |
261 |
< |
"C6", 4.49, 6.38, |
262 |
< |
"DL", 4.33, 8.66, |
263 |
< |
"hagaki", 3.94, 5.83, |
264 |
< |
"" }; |
245 |
> |
{"envelope", 4.12, 9.5}, |
246 |
> |
{"executive", 7.25, 10.5}, |
247 |
> |
{"letter", 8.5, 11.}, |
248 |
> |
{"lettersmall", 7.68, 10.16}, |
249 |
> |
{"legal", 8.5, 14.}, |
250 |
> |
{"monarch", 3.87, 7.5}, |
251 |
> |
{"statement", 5.5, 8.5}, |
252 |
> |
{"tabloid", 11., 17.}, |
253 |
> |
{"A3", 11.69, 16.54}, |
254 |
> |
{"A4", 8.27, 11.69}, |
255 |
> |
{"A4small", 7.47, 10.85}, |
256 |
> |
{"A5", 6.00, 8.27}, |
257 |
> |
{"A6", 4.13, 6.00}, |
258 |
> |
{"B4", 10.12, 14.33}, |
259 |
> |
{"B5", 7.17, 10.12}, |
260 |
> |
{"C5", 6.38, 9.01}, |
261 |
> |
{"C6", 4.49, 6.38}, |
262 |
> |
{"DL", 4.33, 8.66}, |
263 |
> |
{"hagaki", 3.94, 5.83}, |
264 |
> |
{"",0.0,0.0} }; |
265 |
|
register struct psize *pp; |
266 |
|
register char *s = ps; |
267 |
|
double d; |
274 |
|
height = atof(++s); |
275 |
|
width *= d; |
276 |
|
height *= d; |
277 |
< |
if (width >= 1. & height >= 1.) |
277 |
> |
if ((width >= 1.) & (height >= 1.)) |
278 |
|
return; |
279 |
|
} else /* check for match to standard size */ |
280 |
|
for (pp = p; pp->n[0]; pp++) |
293 |
|
} |
294 |
|
|
295 |
|
|
296 |
< |
quiterr(err) /* print message and exit */ |
297 |
< |
char *err; |
296 |
> |
static void |
297 |
> |
quiterr( /* print message and exit */ |
298 |
> |
char *err |
299 |
> |
) |
300 |
|
{ |
301 |
|
if (err != NULL) { |
302 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
306 |
|
} |
307 |
|
|
308 |
|
|
309 |
< |
PSheader(name) /* print PostScript header */ |
310 |
< |
char *name; |
309 |
> |
static void |
310 |
> |
PSheader( /* print PostScript header */ |
311 |
> |
int ac, |
312 |
> |
char **av |
313 |
> |
) |
314 |
|
{ |
315 |
|
char *rstr; |
316 |
|
int landscape, rotate, n; |
318 |
|
double iwidth, iheight; |
319 |
|
/* EPS comments */ |
320 |
|
puts("%!PS-Adobe-2.0 EPSF-2.0"); |
321 |
< |
printf("%%%%Title: %s\n", name); |
322 |
< |
printf("%%%%Creator: %s = %s\n", progname, SCCSid); |
321 |
> |
printf("%%%%Title: "); printargs(ac, av, stdout); |
322 |
> |
printf("%%%%Creator: %s\n", progname); |
323 |
|
printf("%%%%Pages: %d\n", ncopies); |
324 |
< |
if (landscape = xmax > pixaspect*ymax) |
324 |
> |
if ( (landscape = xmax > pixaspect*ymax) ) |
325 |
|
puts("%%Orientation: Landscape"); |
326 |
|
else |
327 |
|
puts("%%Orientation: Portrait"); |
328 |
< |
if (rotate = PWIDTH > PHEIGHT ^ landscape) { |
328 |
> |
if ( (rotate = (PWIDTH > PHEIGHT) ^ landscape) ) { |
329 |
|
pwidth = PHEIGHT; |
330 |
|
pheight = PWIDTH; |
331 |
|
} else { |
332 |
|
pwidth = PWIDTH; |
333 |
|
pheight = PHEIGHT; |
334 |
|
} |
335 |
< |
if (dpi > 100 && pixaspect >= 0.99 & pixaspect <= 1.01) |
335 |
> |
if (dpi > 100 && (pixaspect >= 0.99) & (pixaspect <= 1.01)) |
336 |
|
if (pheight/pwidth > ymax/xmax) { |
337 |
|
n = pwidth*dpi/xmax; /* floor */ |
338 |
|
iwidth = n > 0 ? (double)(n*xmax)/dpi : pwidth; |
407 |
|
} |
408 |
|
|
409 |
|
|
410 |
< |
PStrailer() /* print PostScript trailer */ |
410 |
> |
static void |
411 |
> |
PStrailer(void) /* print PostScript trailer */ |
412 |
|
{ |
413 |
|
puts("%%Trailer"); |
414 |
|
if (ncopies > 1) |
420 |
|
} |
421 |
|
|
422 |
|
|
423 |
< |
PSprocdef(nam) /* define PS procedure to read image */ |
424 |
< |
char *nam; |
423 |
> |
static void |
424 |
> |
PSprocdef( /* define PS procedure to read image */ |
425 |
> |
char *nam |
426 |
> |
) |
427 |
|
{ |
428 |
|
short itab[128]; |
429 |
|
register int i; |
431 |
|
for (i = 0; i < 128; i++) /* clear */ |
432 |
|
itab[i] = -1; |
433 |
|
for (i = 1; i < 63; i++) /* assign greys */ |
434 |
< |
itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM/devgam); |
435 |
< |
itab[code[0]] = 0; /* black is black */ |
436 |
< |
itab[code[63]] = 255; /* and white is white */ |
434 |
> |
itab[(int)code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM/devgam); |
435 |
> |
itab[(int)code[0]] = 0; /* black is black */ |
436 |
> |
itab[(int)code[63]] = 255; /* and white is white */ |
437 |
|
printf("/codetab ["); |
438 |
|
for (i = 0; i < 128; i++) { |
439 |
|
if (!(i & 0xf)) |
462 |
|
} |
463 |
|
|
464 |
|
|
465 |
< |
ra2ps() /* convert Radiance scanlines to 6-bit */ |
465 |
> |
static void |
466 |
> |
ra2ps(void) /* convert Radiance scanlines to 6-bit */ |
467 |
|
{ |
468 |
|
register COLR *scanin; |
469 |
|
int y; |
492 |
|
} |
493 |
|
putchar('\n'); |
494 |
|
/* free scanline */ |
495 |
< |
free((char *)scanin); |
495 |
> |
free((void *)scanin); |
496 |
|
} |
497 |
|
|
498 |
|
|
499 |
< |
int |
500 |
< |
Aputprim(scn, pri) /* put out hex ASCII primary from scanline */ |
501 |
< |
COLR *scn; |
502 |
< |
int pri; |
499 |
> |
static void |
500 |
> |
Aputprim( /* put out hex ASCII primary from scanline */ |
501 |
> |
COLR *scn, |
502 |
> |
int pri |
503 |
> |
) |
504 |
|
{ |
505 |
|
static char hexdigit[] = "0123456789ABCDEF"; |
506 |
|
static int col = 0; |
522 |
|
} |
523 |
|
|
524 |
|
|
525 |
< |
int |
526 |
< |
Bputprim(scn, pri) /* put out binary primary from scanline */ |
527 |
< |
COLR *scn; |
528 |
< |
int pri; |
525 |
> |
static void |
526 |
> |
Bputprim( /* put out binary primary from scanline */ |
527 |
> |
COLR *scn, |
528 |
> |
int pri |
529 |
> |
) |
530 |
|
{ |
531 |
|
register int x, c; |
532 |
|
|
541 |
|
} |
542 |
|
|
543 |
|
|
544 |
< |
int |
545 |
< |
Cputprim(scn, pri) /* put out compressed primary from scanline */ |
546 |
< |
COLR *scn; |
547 |
< |
int pri; |
544 |
> |
static void |
545 |
> |
Cputprim( /* put out compressed primary from scanline */ |
546 |
> |
COLR *scn, |
547 |
> |
int pri |
548 |
> |
) |
549 |
|
{ |
550 |
|
register int c; |
551 |
|
register int x; |
571 |
|
} |
572 |
|
|
573 |
|
|
574 |
< |
putrle(cnt, cod) /* put out cnt of cod */ |
575 |
< |
register int cnt, cod; |
574 |
> |
static void |
575 |
> |
putrle( /* put out cnt of cod */ |
576 |
> |
register int cnt, |
577 |
> |
register int cod |
578 |
> |
) |
579 |
|
{ |
580 |
|
static int col = 0; |
581 |
|
|