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 |
|
|
11 |
– |
#include <stdio.h> |
8 |
|
#include <math.h> |
9 |
|
#include <ctype.h> |
10 |
< |
#ifdef MSDOS |
11 |
< |
#include <fcntl.h> |
12 |
< |
#endif |
10 |
> |
|
11 |
> |
#include "platform.h" |
12 |
> |
#include "paths.h" |
13 |
> |
#include "rtio.h" |
14 |
|
#include "color.h" |
15 |
+ |
#include "resolu.h" |
16 |
|
|
17 |
|
#define UPPER(c) ((c)&~0x20) /* ASCII trick */ |
18 |
|
|
51 |
|
int bradj = 0; /* brightness adjustment */ |
52 |
|
int ncopies = 1; /* number of copies */ |
53 |
|
|
54 |
< |
extern int Aputprim(), Bputprim(), Cputprim(); |
54 |
> |
int xmax, ymax; /* input image dimensions */ |
55 |
|
|
56 |
< |
int (*putprim)() = Aputprim; /* function for writing scanline */ |
56 |
> |
typedef void putprimf_t(COLR *scn, int pri); |
57 |
|
|
58 |
< |
char *progname; |
58 |
> |
static gethfunc headline; |
59 |
> |
static putprimf_t Aputprim, Bputprim, Cputprim; |
60 |
|
|
61 |
< |
int xmax, ymax; /* input image dimensions */ |
61 |
> |
static double unit2inch(char *s); |
62 |
> |
static int matchid(char *name, char *id); |
63 |
> |
static void parsepaper(char *ps); |
64 |
> |
static void quiterr(char *err); |
65 |
> |
static void PSheader(int ac, char **av); |
66 |
> |
static void PStrailer(void); |
67 |
> |
static void PSprocdef(char *nam); |
68 |
> |
static void ra2ps(void); |
69 |
> |
static void putrle(int cnt, int cod); |
70 |
|
|
64 |
– |
extern char *malloc(); |
65 |
– |
extern double unit2inch(); |
71 |
|
|
72 |
+ |
putprimf_t *putprim = Aputprim; /* function for writing scanline */ |
73 |
|
|
74 |
< |
int |
75 |
< |
headline(s) /* check header line */ |
76 |
< |
char *s; |
74 |
> |
|
75 |
> |
static int |
76 |
> |
headline( /* check header line */ |
77 |
> |
char *s, |
78 |
> |
void *p |
79 |
> |
) |
80 |
|
{ |
81 |
< |
char fmt[32]; |
81 |
> |
char fmt[MAXFMTLEN]; |
82 |
|
|
83 |
< |
if (isformat(s)) { |
84 |
< |
formatval(fmt, s); |
85 |
< |
wrongformat = strcmp(fmt, COLRFMT); |
77 |
< |
} else if (isaspect(s)) |
83 |
> |
if (formatval(fmt, s)) |
84 |
> |
wrongformat = strcmp(fmt, COLRFMT) && strcmp(fmt, SPECFMT); |
85 |
> |
else if (isaspect(s)) |
86 |
|
pixaspect *= aspectval(s); |
87 |
+ |
else if (isncomp(s)) |
88 |
+ |
NCSAMP = ncompval(s); |
89 |
+ |
else if (iswlsplit(s)) |
90 |
+ |
wlsplitval(WLPART, s); |
91 |
|
return(0); |
92 |
|
} |
93 |
|
|
94 |
< |
|
95 |
< |
main(argc, argv) |
84 |
< |
int argc; |
85 |
< |
char *argv[]; |
94 |
> |
int |
95 |
> |
main(int argc, char *argv[]) |
96 |
|
{ |
97 |
|
int i; |
98 |
|
double d; |
99 |
|
|
100 |
< |
progname = argv[0]; |
100 |
> |
fixargv0(argv[0]); /* assigns progname */ |
101 |
|
|
102 |
|
for (i = 1; i < argc; i++) |
103 |
|
if (argv[i][0] == '-') |
170 |
|
progname, argv[i+1]); |
171 |
|
exit(1); |
172 |
|
} |
173 |
< |
#ifdef MSDOS |
164 |
< |
setmode(fileno(stdin), O_BINARY); |
165 |
< |
#endif |
173 |
> |
SET_FILE_BINARY(stdin); |
174 |
|
/* get our header */ |
175 |
|
getheader(stdin, headline, NULL); |
176 |
|
if (wrongformat || fgetresolu(&xmax, &ymax, stdin) < 0) |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
< |
double |
201 |
< |
unit2inch(s) /* determine unit */ |
202 |
< |
register char *s; |
200 |
> |
static double |
201 |
> |
unit2inch( /* determine unit */ |
202 |
> |
char *s |
203 |
> |
) |
204 |
|
{ |
205 |
|
static struct unit {char n; float f;} u[] = { |
206 |
< |
'i', 1., |
207 |
< |
'm', 1./25.4, |
208 |
< |
'c', 1./2.54, |
209 |
< |
'\0' }; |
210 |
< |
register struct unit *up; |
206 |
> |
{'i', 1.}, |
207 |
> |
{'m', 1./25.4}, |
208 |
> |
{'c', 1./2.54}, |
209 |
> |
{'\0',0} }; |
210 |
> |
struct unit *up; |
211 |
|
|
212 |
|
while (*s && !isalpha(*s)) |
213 |
|
s++; |
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
< |
int |
222 |
< |
matchid(name, id) /* see if name matches id (case insensitive) */ |
223 |
< |
char *name; |
224 |
< |
register char *id; |
221 |
> |
static int |
222 |
> |
matchid( /* see if name matches id (case insensitive) */ |
223 |
> |
char *name, |
224 |
> |
char *id |
225 |
> |
) |
226 |
|
{ |
227 |
< |
register char *s = name; |
227 |
> |
char *s = name; |
228 |
|
|
229 |
|
while (*s) { |
230 |
|
if (isalpha(*s)) { |
238 |
|
} |
239 |
|
|
240 |
|
|
241 |
< |
parsepaper(ps) /* determine paper size from name */ |
242 |
< |
char *ps; |
241 |
> |
static void |
242 |
> |
parsepaper( /* determine paper size from name */ |
243 |
> |
char *ps |
244 |
> |
) |
245 |
|
{ |
246 |
|
static struct psize {char n[12]; float w,h;} p[] = { |
247 |
< |
"envelope", 4.12, 9.5, |
248 |
< |
"executive", 7.25, 10.5, |
249 |
< |
"letter", 8.5, 11., |
250 |
< |
"lettersmall", 7.68, 10.16, |
251 |
< |
"legal", 8.5, 14., |
252 |
< |
"monarch", 3.87, 7.5, |
253 |
< |
"statement", 5.5, 8.5, |
254 |
< |
"tabloid", 11., 17., |
255 |
< |
"A3", 11.69, 16.54, |
256 |
< |
"A4", 8.27, 11.69, |
257 |
< |
"A4small", 7.47, 10.85, |
258 |
< |
"A5", 6.00, 8.27, |
259 |
< |
"A6", 4.13, 6.00, |
260 |
< |
"B4", 10.12, 14.33, |
261 |
< |
"B5", 7.17, 10.12, |
262 |
< |
"C5", 6.38, 9.01, |
263 |
< |
"C6", 4.49, 6.38, |
264 |
< |
"DL", 4.33, 8.66, |
265 |
< |
"hagaki", 3.94, 5.83, |
266 |
< |
"" }; |
267 |
< |
register struct psize *pp; |
268 |
< |
register char *s = ps; |
247 |
> |
{"envelope", 4.12, 9.5}, |
248 |
> |
{"executive", 7.25, 10.5}, |
249 |
> |
{"letter", 8.5, 11.}, |
250 |
> |
{"lettersmall", 7.68, 10.16}, |
251 |
> |
{"legal", 8.5, 14.}, |
252 |
> |
{"monarch", 3.87, 7.5}, |
253 |
> |
{"statement", 5.5, 8.5}, |
254 |
> |
{"tabloid", 11., 17.}, |
255 |
> |
{"A3", 11.69, 16.54}, |
256 |
> |
{"A4", 8.27, 11.69}, |
257 |
> |
{"A4small", 7.47, 10.85}, |
258 |
> |
{"A5", 6.00, 8.27}, |
259 |
> |
{"A6", 4.13, 6.00}, |
260 |
> |
{"B4", 10.12, 14.33}, |
261 |
> |
{"B5", 7.17, 10.12}, |
262 |
> |
{"C5", 6.38, 9.01}, |
263 |
> |
{"C6", 4.49, 6.38}, |
264 |
> |
{"DL", 4.33, 8.66}, |
265 |
> |
{"hagaki", 3.94, 5.83}, |
266 |
> |
{"",0.0,0.0} }; |
267 |
> |
struct psize *pp; |
268 |
> |
char *s = ps; |
269 |
|
double d; |
270 |
|
|
271 |
|
if (isdigit(*s)) { /* check for WWxHH specification */ |
276 |
|
height = atof(++s); |
277 |
|
width *= d; |
278 |
|
height *= d; |
279 |
< |
if (width >= 1. & height >= 1.) |
279 |
> |
if ((width >= 1.) & (height >= 1.)) |
280 |
|
return; |
281 |
|
} else /* check for match to standard size */ |
282 |
|
for (pp = p; pp->n[0]; pp++) |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
< |
quiterr(err) /* print message and exit */ |
299 |
< |
char *err; |
298 |
> |
static void |
299 |
> |
quiterr( /* print message and exit */ |
300 |
> |
char *err |
301 |
> |
) |
302 |
|
{ |
303 |
|
if (err != NULL) { |
304 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
< |
PSheader(ac, av) /* print PostScript header */ |
312 |
< |
int ac; |
313 |
< |
char **av; |
311 |
> |
static void |
312 |
> |
PSheader( /* print PostScript header */ |
313 |
> |
int ac, |
314 |
> |
char **av |
315 |
> |
) |
316 |
|
{ |
317 |
|
char *rstr; |
318 |
|
int landscape, rotate, n; |
321 |
|
/* EPS comments */ |
322 |
|
puts("%!PS-Adobe-2.0 EPSF-2.0"); |
323 |
|
printf("%%%%Title: "); printargs(ac, av, stdout); |
324 |
< |
printf("%%%%Creator: %s\n", SCCSid); |
324 |
> |
printf("%%%%Creator: %s\n", progname); |
325 |
|
printf("%%%%Pages: %d\n", ncopies); |
326 |
< |
if (landscape = xmax > pixaspect*ymax) |
326 |
> |
if ( (landscape = xmax > pixaspect*ymax) ) |
327 |
|
puts("%%Orientation: Landscape"); |
328 |
|
else |
329 |
|
puts("%%Orientation: Portrait"); |
330 |
< |
if (rotate = PWIDTH > PHEIGHT ^ landscape) { |
330 |
> |
if ( (rotate = (PWIDTH > PHEIGHT) ^ landscape) ) { |
331 |
|
pwidth = PHEIGHT; |
332 |
|
pheight = PWIDTH; |
333 |
|
} else { |
334 |
|
pwidth = PWIDTH; |
335 |
|
pheight = PHEIGHT; |
336 |
|
} |
337 |
< |
if (dpi > 100 && pixaspect >= 0.99 & pixaspect <= 1.01) |
337 |
> |
if (dpi > 100 && (pixaspect >= 0.99) & (pixaspect <= 1.01)) |
338 |
|
if (pheight/pwidth > ymax/xmax) { |
339 |
|
n = pwidth*dpi/xmax; /* floor */ |
340 |
|
iwidth = n > 0 ? (double)(n*xmax)/dpi : pwidth; |
409 |
|
} |
410 |
|
|
411 |
|
|
412 |
< |
PStrailer() /* print PostScript trailer */ |
412 |
> |
static void |
413 |
> |
PStrailer(void) /* print PostScript trailer */ |
414 |
|
{ |
415 |
|
puts("%%Trailer"); |
416 |
|
if (ncopies > 1) |
422 |
|
} |
423 |
|
|
424 |
|
|
425 |
< |
PSprocdef(nam) /* define PS procedure to read image */ |
426 |
< |
char *nam; |
425 |
> |
static void |
426 |
> |
PSprocdef( /* define PS procedure to read image */ |
427 |
> |
char *nam |
428 |
> |
) |
429 |
|
{ |
430 |
|
short itab[128]; |
431 |
< |
register int i; |
431 |
> |
int i; |
432 |
|
/* assign code values */ |
433 |
|
for (i = 0; i < 128; i++) /* clear */ |
434 |
|
itab[i] = -1; |
435 |
|
for (i = 1; i < 63; i++) /* assign greys */ |
436 |
< |
itab[code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM/devgam); |
437 |
< |
itab[code[0]] = 0; /* black is black */ |
438 |
< |
itab[code[63]] = 255; /* and white is white */ |
436 |
> |
itab[(int)code[i]] = 256.0*pow((i+.5)/64.0, CODE6GAM/devgam); |
437 |
> |
itab[(int)code[0]] = 0; /* black is black */ |
438 |
> |
itab[(int)code[63]] = 255; /* and white is white */ |
439 |
|
printf("/codetab ["); |
440 |
|
for (i = 0; i < 128; i++) { |
441 |
|
if (!(i & 0xf)) |
464 |
|
} |
465 |
|
|
466 |
|
|
467 |
< |
ra2ps() /* convert Radiance scanlines to 6-bit */ |
467 |
> |
static void |
468 |
> |
ra2ps(void) /* convert Radiance scanlines to 6-bit */ |
469 |
|
{ |
470 |
< |
register COLR *scanin; |
470 |
> |
COLR *scanin; |
471 |
|
int y; |
472 |
|
/* allocate scanline */ |
473 |
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
475 |
|
quiterr("out of memory in ra2ps"); |
476 |
|
/* convert image */ |
477 |
|
for (y = ymax-1; y >= 0; y--) { |
478 |
< |
if (freadcolrs(scanin, xmax, stdin) < 0) |
478 |
> |
if (fread2colrs(scanin, xmax, stdin, NCSAMP, WLPART) < 0) |
479 |
|
quiterr("error reading Radiance picture"); |
480 |
< |
if (putprim == Cputprim || devgam != 1.) { |
480 |
> |
if ((putprim == Cputprim) | (devgam != 1.)) { |
481 |
|
if (bradj) /* adjust exposure */ |
482 |
|
shiftcolrs(scanin, xmax, bradj); |
483 |
|
colrs_gambs(scanin, xmax); /* gamma compression */ |
494 |
|
} |
495 |
|
putchar('\n'); |
496 |
|
/* free scanline */ |
497 |
< |
free((char *)scanin); |
497 |
> |
free((void *)scanin); |
498 |
|
} |
499 |
|
|
500 |
|
|
501 |
< |
int |
502 |
< |
Aputprim(scn, pri) /* put out hex ASCII primary from scanline */ |
503 |
< |
COLR *scn; |
504 |
< |
int pri; |
501 |
> |
static void |
502 |
> |
Aputprim( /* put out hex ASCII primary from scanline */ |
503 |
> |
COLR *scn, |
504 |
> |
int pri |
505 |
> |
) |
506 |
|
{ |
507 |
|
static char hexdigit[] = "0123456789ABCDEF"; |
508 |
|
static int col = 0; |
509 |
< |
register int x, c; |
509 |
> |
int x, c; |
510 |
|
|
511 |
|
for (x = 0; x < xmax; x++) { |
512 |
|
if (pri == GRY) |
524 |
|
} |
525 |
|
|
526 |
|
|
527 |
< |
int |
528 |
< |
Bputprim(scn, pri) /* put out binary primary from scanline */ |
529 |
< |
COLR *scn; |
530 |
< |
int pri; |
527 |
> |
static void |
528 |
> |
Bputprim( /* put out binary primary from scanline */ |
529 |
> |
COLR *scn, |
530 |
> |
int pri |
531 |
> |
) |
532 |
|
{ |
533 |
< |
register int x, c; |
533 |
> |
int x, c; |
534 |
|
|
535 |
|
for (x = 0; x < xmax; x++) { |
536 |
|
if (pri == GRY) |
543 |
|
} |
544 |
|
|
545 |
|
|
546 |
< |
int |
547 |
< |
Cputprim(scn, pri) /* put out compressed primary from scanline */ |
548 |
< |
COLR *scn; |
549 |
< |
int pri; |
546 |
> |
static void |
547 |
> |
Cputprim( /* put out compressed primary from scanline */ |
548 |
> |
COLR *scn, |
549 |
> |
int pri |
550 |
> |
) |
551 |
|
{ |
552 |
< |
register int c; |
553 |
< |
register int x; |
552 |
> |
int c; |
553 |
> |
int x; |
554 |
|
int lastc, cnt; |
555 |
|
|
556 |
|
lastc = -1; cnt = 0; |
573 |
|
} |
574 |
|
|
575 |
|
|
576 |
< |
putrle(cnt, cod) /* put out cnt of cod */ |
577 |
< |
register int cnt, cod; |
576 |
> |
static void |
577 |
> |
putrle( /* put out cnt of cod */ |
578 |
> |
int cnt, |
579 |
> |
int cod |
580 |
> |
) |
581 |
|
{ |
582 |
|
static int col = 0; |
583 |
|
|