1 |
/* Copyright (c) 1988 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Send pictures to PC animation system. |
9 |
* |
10 |
* 6/20/88 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
|
15 |
#include "random.h" |
16 |
#include "color.h" |
17 |
#include "clntrpc.h" |
18 |
#include "scan.h" |
19 |
|
20 |
#define GAMMA 2.0 /* gamma correction factor */ |
21 |
|
22 |
FILE *popen(); |
23 |
|
24 |
char *pcom = NULL; /* pipe command */ |
25 |
|
26 |
BYTE gammamap[256]; /* gamma correction table */ |
27 |
|
28 |
|
29 |
main(argc, argv) |
30 |
int argc; |
31 |
char *argv[]; |
32 |
{ |
33 |
char *progname; |
34 |
int nframes; |
35 |
int port = PCPROGRAM; |
36 |
/* initialize */ |
37 |
compgamma(); |
38 |
for (progname = *argv++; --argc; argv++) |
39 |
if (!strcmp(*argv, "-p") && argv[1]) { |
40 |
port = atoi(*++argv); argc--; |
41 |
} else if (!strcmp(*argv, "-u") && argv[1]) { |
42 |
pcom = *++argv; argc--; |
43 |
} else |
44 |
break; |
45 |
if (!argc) { |
46 |
fputs("Usage: ", stderr); |
47 |
fputs(progname, stderr); |
48 |
fputs(" [-p port] [-u uncompress] hostname [-c copies][-r record] [frame] ..\n", |
49 |
stderr); |
50 |
exit(1); |
51 |
} |
52 |
scry_open(*argv++, TCP, port); |
53 |
scry_set_compress(NONE); |
54 |
scry_set_image(TARGA_IMAGE); |
55 |
scry_set_record(PREVIEW); |
56 |
scry_set_copy_num(3); |
57 |
/* send frames */ |
58 |
nframes = 0; |
59 |
for ( ; --argc; argv++) |
60 |
if (!strcmp(*argv, "-r") && argv[1]) { |
61 |
scry_set_record(atoi(*++argv)); argc--; |
62 |
} else if (!strcmp(*argv, "-c") && argv[1]) { |
63 |
scry_set_copy_num(atoi(*++argv)); argc--; |
64 |
} else { |
65 |
sendframe(*argv); |
66 |
nframes++; |
67 |
} |
68 |
if (nframes == 0) /* get stdin */ |
69 |
sendframe(NULL); |
70 |
/* clean up */ |
71 |
scry_close(); |
72 |
exit(0); |
73 |
} |
74 |
|
75 |
|
76 |
sendframe(file) /* convert and send a frame */ |
77 |
char *file; |
78 |
{ |
79 |
char command[128]; |
80 |
COLR scanin[SCANLINE]; |
81 |
int xres, yres; |
82 |
int xbeg, ybeg; |
83 |
FILE *fp; |
84 |
int y; |
85 |
register int x; |
86 |
/* open file */ |
87 |
if (file == NULL) { |
88 |
if (pcom != NULL) |
89 |
fp = popen(pcom, "r"); |
90 |
else |
91 |
fp = stdin; |
92 |
file = "<stdin>"; |
93 |
} else { |
94 |
if (pcom != NULL) { |
95 |
sprintf(command, "( %s ) < %s", pcom, file); |
96 |
fp = popen(command, "r"); |
97 |
} else |
98 |
fp = fopen(file, "r"); |
99 |
} |
100 |
if (fp == NULL) { |
101 |
perror(file); |
102 |
exit(1); |
103 |
} |
104 |
/* get dimensions */ |
105 |
getheader(fp, NULL); |
106 |
if (fgetresolu(&xres, &yres, fp) != (YMAJOR|YDECR) || |
107 |
xres > SCANLINE || yres > NUMSCANS) { |
108 |
fputs(file, stderr); |
109 |
fputs(": bad picture size\n", stderr); |
110 |
exit(1); |
111 |
} |
112 |
/* compute center */ |
113 |
xbeg = (SCANLINE-xres)/2; |
114 |
ybeg = (NUMSCANS-yres)/2; |
115 |
/* clear output */ |
116 |
bzero(sc_frame_arr, sizeof(sc_frame_arr)); |
117 |
/* get frame */ |
118 |
for (y = yres-1; y >= 0; y--) { |
119 |
if (freadcolrs(scanin, xres, fp) < 0) { |
120 |
fputs(file, stderr); |
121 |
fputs(": read error\n", stderr); |
122 |
exit(1); |
123 |
} |
124 |
normcolrs(scanin, xres, 0); /* normalize */ |
125 |
for (x = 0; x < xres; x++) /* convert */ |
126 |
sc_frame_arr[y+ybeg][x+xbeg] = |
127 |
((gammamap[scanin[x][RED]]+(random()&7))&0xf8)<<7 |
128 |
| ((gammamap[scanin[x][GRN]]+(random()&7))&0xf8)<<2 |
129 |
| (gammamap[scanin[x][BLU]]+(random()&7))>>3; |
130 |
} |
131 |
/* send frame */ |
132 |
scry_send_frame(); |
133 |
/* close file */ |
134 |
if (pcom != NULL) |
135 |
pclose(fp); |
136 |
else |
137 |
fclose(fp); |
138 |
} |
139 |
|
140 |
|
141 |
compgamma() /* compute gamma correction map */ |
142 |
{ |
143 |
extern double pow(); |
144 |
register int i, val; |
145 |
|
146 |
for (i = 0; i < 256; i++) { |
147 |
val = pow((i+0.5)/256.0, 1.0/GAMMA) * 256.0; |
148 |
if (val > 248) val = 248; |
149 |
gammamap[i] = val; |
150 |
} |
151 |
} |