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