13 |
|
|
14 |
|
#include "standard.h" |
15 |
|
#include "platform.h" |
16 |
+ |
#include "paths.h" |
17 |
|
#include "resolu.h" |
18 |
|
#include "color.h" |
18 |
– |
#include "random.h" |
19 |
– |
/* our probabilities */ |
20 |
– |
#define PROB_LEFT1 0.1 |
21 |
– |
#define PROB_RIGHT1 0.1 |
22 |
– |
#define PROB_LEFT2 0.2 |
23 |
– |
#define PROB_RIGHT2 0.2 |
24 |
– |
#define PROB_LEFT3 0.07 |
25 |
– |
#define PROB_RIGHT3 0.07 |
26 |
– |
#define PROB_DOWN1 0.1 |
19 |
|
|
20 |
|
#define MAXMOD 2048 /* maximum number of modifiers */ |
21 |
|
#define USESORT 12 /* switch to sorted search */ |
22 |
|
|
23 |
+ |
double smoothing = 0.8; /* weight for moving average filter */ |
24 |
+ |
double mixfact = 0.3; /* amount to mix above/below */ |
25 |
+ |
|
26 |
|
char *modlist[MAXMOD]; /* (sorted) modifier list */ |
27 |
|
int nmods = 0; /* number of modifiers */ |
28 |
|
|
29 |
< |
unsigned char *hasmod, *moved[2]; /* scanline bitmaps */ |
30 |
< |
COLR *scan[2]; /* i/o scanlines */ |
29 |
> |
unsigned char *hasmod; /* scanline bitmap */ |
30 |
> |
unsigned char *hasmod1; |
31 |
> |
COLOR *scan[2]; /* i/o scanlines */ |
32 |
|
RESOLU pres; /* input resolution */ |
33 |
|
int bmwidth; /* bytes per bitmap */ |
34 |
< |
/* conflicting def's in param.h */ |
35 |
< |
#undef tstbit |
34 |
> |
|
35 |
> |
#undef tstbit /* conflicting def's in param.h */ |
36 |
|
#undef setbit |
37 |
|
#undef clrbit |
38 |
|
#undef tglbit |
78 |
|
static int |
79 |
|
read_scan(void) |
80 |
|
{ |
81 |
+ |
const int spread = (int)(smoothing*10); |
82 |
|
int x, width = scanlen(&pres); |
83 |
|
unsigned char *tbmp; |
84 |
< |
COLR *tscn; |
84 |
> |
COLOR *tscn; |
85 |
|
char modbuf[516]; |
86 |
< |
/* advance buffers */ |
90 |
< |
tbmp = moved[0]; |
91 |
< |
moved[0] = moved[1]; |
92 |
< |
moved[1] = tbmp; |
86 |
> |
/* advance buffer */ |
87 |
|
tscn = scan[0]; |
88 |
|
scan[0] = scan[1]; |
89 |
|
scan[1] = tscn; |
90 |
|
/* check if we are at the end */ |
91 |
|
if (linesread >= numscans(&pres)) |
92 |
|
return(0); |
93 |
< |
/* clear bitmaps */ |
94 |
< |
if (linesread) |
101 |
< |
memset(hasmod, 0, bmwidth); |
102 |
< |
memset(moved[1], 0, bmwidth); |
93 |
> |
/* set scanline bitmap */ |
94 |
> |
if (linesread) { |
95 |
|
/* load & check materials */ |
96 |
< |
for (x = 0; x < width*(linesread>0); x++) { |
97 |
< |
int len; |
98 |
< |
if (fgets(modbuf, sizeof(modbuf), mafp) == NULL) { |
99 |
< |
fprintf(stderr, "Error reading from rtrace!\n"); |
100 |
< |
return(-1); |
96 |
> |
memset(hasmod1, 0, bmwidth); |
97 |
> |
for (x = 0; x < width; x++) { |
98 |
> |
int len; |
99 |
> |
if (fgets(modbuf, sizeof(modbuf), mafp) == NULL) { |
100 |
> |
fprintf(stderr, "Error reading from rtrace!\n"); |
101 |
> |
return(-1); |
102 |
> |
} |
103 |
> |
len = strlen(modbuf); |
104 |
> |
if (len < 3 || (modbuf[len-1] != '\n') | |
105 |
> |
(modbuf[len-2] != '\t')) { |
106 |
> |
fprintf(stderr, "Garbled rtrace output: %s", modbuf); |
107 |
> |
return(-1); |
108 |
> |
} |
109 |
> |
modbuf[len-2] = '\0'; |
110 |
> |
if (find_mod(modbuf) >= 0) |
111 |
> |
setbit(hasmod1, x); |
112 |
|
} |
113 |
< |
len = strlen(modbuf); |
114 |
< |
if (len < 3 || (modbuf[len-1] != '\n') | |
115 |
< |
(modbuf[len-2] != '\t')) { |
116 |
< |
fprintf(stderr, "Garbled rtrace output: %s", modbuf); |
117 |
< |
return(-1); |
113 |
> |
/* smear to cover around object */ |
114 |
> |
memset(hasmod, 0, bmwidth); |
115 |
> |
for (x = spread; x < width-spread; x++) { |
116 |
> |
int ox; |
117 |
> |
if (!tstbit(hasmod1,x)) continue; |
118 |
> |
for (ox = -spread; ox <= spread; ox++) |
119 |
> |
setbit(hasmod,x+ox); |
120 |
|
} |
116 |
– |
modbuf[len-2] = '\0'; |
117 |
– |
if (find_mod(modbuf) >= 0) |
118 |
– |
setbit(hasmod, x); |
121 |
|
} |
122 |
|
/* read next picture scanline */ |
123 |
< |
if (freadcolrs(scan[1], width, infp) < 0) { |
123 |
> |
if (freadscan(scan[1], width, infp) < 0) { |
124 |
|
fprintf(stderr, "%s: error reading scanline %d\n", |
125 |
|
infname, linesread); |
126 |
|
return(-1); |
152 |
|
/* allocate bitmaps & buffers */ |
153 |
|
bmwidth = (scanlen(&pres)+7) >> 3; |
154 |
|
hasmod = (unsigned char *)malloc(bmwidth); |
155 |
< |
moved[0] = (unsigned char *)malloc(bmwidth); |
156 |
< |
moved[1] = (unsigned char *)malloc(bmwidth); |
157 |
< |
scan[0] = (COLR *)malloc(scanlen(&pres)*sizeof(COLR)); |
158 |
< |
scan[1] = (COLR *)malloc(scanlen(&pres)*sizeof(COLR)); |
157 |
< |
if (!hasmod | !moved[0] | !moved[1] | !scan[0] | !scan[1]) { |
155 |
> |
hasmod1 = (unsigned char *)malloc(bmwidth); |
156 |
> |
scan[0] = (COLOR *)malloc(scanlen(&pres)*sizeof(COLOR)); |
157 |
> |
scan[1] = (COLOR *)malloc(scanlen(&pres)*sizeof(COLOR)); |
158 |
> |
if (!hasmod | !hasmod1 | !scan[0] | !scan[1]) { |
159 |
|
perror("malloc"); |
160 |
|
return(0); |
161 |
|
} |
171 |
|
int width = scanlen(&pres); |
172 |
|
int height = numscans(&pres); |
173 |
|
int x, xstart = 0, xstop = width, xstep = 1; |
174 |
< |
COLR tclr; |
174 |
> |
COLOR cmavg; |
175 |
|
|
175 |
– |
#define CSWAP(y0,x0,y1,x1) { copycolr(tclr,scan[y0][x0]); \ |
176 |
– |
copycolr(scan[y1][x1],scan[y0][x0]); \ |
177 |
– |
copycolr(scan[y0][x0],tclr); \ |
178 |
– |
setbit(moved[y0],x0); setbit(moved[y1],x1); } |
179 |
– |
|
176 |
|
if (alldone) /* finished last scanline? */ |
177 |
|
return(0); |
178 |
|
alldone = (linesread >= height); |
183 |
|
xstart = width-1; |
184 |
|
xstop = -1; |
185 |
|
xstep = -1; |
186 |
< |
} /* process this scanline */ |
186 |
> |
} |
187 |
> |
setcolor(cmavg, .0f, .0f, .0f); /* process this scanline */ |
188 |
|
for (x = xstart; x != xstop; x += xstep) { |
189 |
< |
double runif = frandom(); |
190 |
< |
if (tstbit(moved[0],x) || !tstbit(hasmod,x)) |
191 |
< |
continue; |
192 |
< |
if (runif < PROB_LEFT1) { |
193 |
< |
if (x > 0 && !tstbit(moved[0],x-1)) |
194 |
< |
CSWAP(0,x,0,x-1); |
195 |
< |
continue; |
196 |
< |
} |
197 |
< |
runif -= PROB_LEFT1; |
198 |
< |
if (runif < PROB_RIGHT1) { |
199 |
< |
if (x < width-1 && !tstbit(moved[0],x+1)) |
200 |
< |
CSWAP(0,x,0,x+1); |
204 |
< |
continue; |
205 |
< |
} |
206 |
< |
runif -= PROB_RIGHT1; |
207 |
< |
if (runif < PROB_LEFT2) { |
208 |
< |
if (x > 1 && !tstbit(moved[0],x-2)) |
209 |
< |
CSWAP(0,x,0,x-2); |
210 |
< |
continue; |
211 |
< |
} |
212 |
< |
runif -= PROB_LEFT2; |
213 |
< |
if (runif < PROB_RIGHT2) { |
214 |
< |
if (x < width-2 && !tstbit(moved[0],x+2)) |
215 |
< |
CSWAP(0,x,0,x+2); |
216 |
< |
continue; |
217 |
< |
} |
218 |
< |
runif -= PROB_RIGHT2; |
219 |
< |
if (runif < PROB_LEFT3) { |
220 |
< |
if (x > 2 && !tstbit(moved[0],x-3)) |
221 |
< |
CSWAP(0,x,0,x-3); |
222 |
< |
continue; |
223 |
< |
} |
224 |
< |
runif -= PROB_LEFT3; |
225 |
< |
if (runif < PROB_RIGHT3) { |
226 |
< |
if (x < width-3 && !tstbit(moved[0],x+3)) |
227 |
< |
CSWAP(0,x,0,x+3); |
228 |
< |
continue; |
229 |
< |
} |
230 |
< |
runif -= PROB_RIGHT3; |
231 |
< |
if (runif < PROB_DOWN1) { |
232 |
< |
if (linesread < height-1) |
233 |
< |
CSWAP(0,x,1,x); |
234 |
< |
continue; |
235 |
< |
} |
236 |
< |
runif -= PROB_DOWN1; |
189 |
> |
COLOR cmix; |
190 |
> |
if (!tstbit(hasmod,x)) continue; |
191 |
> |
/* apply moving average */ |
192 |
> |
scalecolor(scan[0][x], 1.-smoothing); |
193 |
> |
scalecolor(cmavg, smoothing); |
194 |
> |
addcolor(cmavg, scan[0][x]); |
195 |
> |
copycolor(scan[0][x], cmavg); |
196 |
> |
/* mix pixel into next scanline */ |
197 |
> |
copycolor(cmix, scan[0][x]); |
198 |
> |
scalecolor(cmix, mixfact); |
199 |
> |
scalecolor(scan[1][x], 1.-mixfact); |
200 |
> |
addcolor(scan[1][x], cmix); |
201 |
|
} |
202 |
< |
/* write it out */ |
203 |
< |
if (fwritecolrs(scan[0], width, stdout) < 0) { |
202 |
> |
/* write out result */ |
203 |
> |
if (fwritescan(scan[0], width, stdout) < 0) { |
204 |
|
perror("write error"); |
205 |
|
return(-1); |
206 |
|
} |
207 |
|
return(1); |
244 |
– |
#undef CSWAP |
208 |
|
} |
209 |
|
|
210 |
|
static int |
220 |
|
} |
221 |
|
fclose(infp); |
222 |
|
free(hasmod); |
223 |
< |
free(moved[0]); |
261 |
< |
free(moved[1]); |
223 |
> |
free(hasmod1); |
224 |
|
free(scan[0]); |
225 |
|
free(scan[1]); |
226 |
|
return(1); |
250 |
|
} |
251 |
|
nmods += rval; |
252 |
|
break; |
253 |
+ |
case 's': /* filter smoothing amount */ |
254 |
+ |
smoothing = atof(argv[++i]); |
255 |
+ |
if ((smoothing <= FTINY) | (smoothing >= 1.-FTINY)) { |
256 |
+ |
fprintf(stderr, "%s: smoothing factor must be in (0,1) range\n", |
257 |
+ |
argv[0]); |
258 |
+ |
return(1); |
259 |
+ |
} |
260 |
+ |
break; |
261 |
|
default: |
262 |
|
fprintf(stderr, "%s: unknown option '%s'\n", |
263 |
|
argv[0], argv[i]); |
264 |
|
return(1); |
265 |
|
} |
266 |
|
if ((argc-i < 2) | (argc-i > 3)) { |
267 |
< |
fprintf(stderr, "Usage: %s [-m modname][-M modfile] octree input.hdr [output.hdr]\n", |
267 |
> |
fprintf(stderr, "Usage: %s [-m modname][-M modfile][-s smoothing] octree input.hdr [output.hdr]\n", |
268 |
|
argv[0]); |
269 |
|
return(1); |
270 |
|
} |