| 1 |
#ifndef lint |
| 2 |
static const char RCSid[] = "$Id: pmblur2.c,v 2.3 2012/10/05 15:25:12 greg Exp $"; |
| 3 |
#endif |
| 4 |
/* |
| 5 |
* pmblur2.c - program to computer better motion blur from ranimove frames. |
| 6 |
* |
| 7 |
* Created by Greg Ward on 10/3/12. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "copyright.h" |
| 11 |
|
| 12 |
#include "standard.h" |
| 13 |
#include "platform.h" |
| 14 |
#include "color.h" |
| 15 |
#include "view.h" |
| 16 |
|
| 17 |
#define ZGONE (2.*FHUGE) /* non-existent depth value */ |
| 18 |
|
| 19 |
const char *hdrspec; /* HDR image specification string */ |
| 20 |
const char *zbfspec; /* depth buffer specification string */ |
| 21 |
const char *mvospec; /* motion vector specification string */ |
| 22 |
|
| 23 |
RESOLU imres; /* image size and orientation */ |
| 24 |
double pixaspect = 1.; /* pixel aspect ratio */ |
| 25 |
|
| 26 |
COLOR *imsum; /* blurred image in progress */ |
| 27 |
VIEW vwsum; /* averaged view for blurred image */ |
| 28 |
char imfmt[32] = PICFMT; /* image format */ |
| 29 |
|
| 30 |
int nsamps; /* number of time samples */ |
| 31 |
|
| 32 |
COLOR *imbuf; /* accumulation buffer for this time sample */ |
| 33 |
float *zbuf; /* associated depth buffer */ |
| 34 |
|
| 35 |
COLR *imprev; /* previous loaded image frame */ |
| 36 |
float *zprev; /* loaded depth buffer */ |
| 37 |
double exprev; /* previous image exposure */ |
| 38 |
VIEW vwprev = STDVIEW; /* previous view */ |
| 39 |
int fnprev; /* frame number for previous view */ |
| 40 |
|
| 41 |
char *progname; /* global argv[0] */ |
| 42 |
|
| 43 |
typedef struct { |
| 44 |
VIEW *vp; /* view pointer */ |
| 45 |
int gotview; /* got view parameters? */ |
| 46 |
double ev; /* exposure value */ |
| 47 |
} IMGHEAD; /* holder for individual header information */ |
| 48 |
|
| 49 |
|
| 50 |
/* Process line from picture header */ |
| 51 |
static int |
| 52 |
headline(char *s, void *p) |
| 53 |
{ |
| 54 |
IMGHEAD *ip = (IMGHEAD *)p; |
| 55 |
char fmt[32]; |
| 56 |
|
| 57 |
if (isview(s)) { |
| 58 |
ip->gotview += (sscanview(ip->vp, s) > 0); |
| 59 |
return(1); |
| 60 |
} |
| 61 |
if (isexpos(s)) { |
| 62 |
ip->ev *= exposval(s); |
| 63 |
return(1); |
| 64 |
} |
| 65 |
if (formatval(fmt, s)) { |
| 66 |
if (globmatch(imfmt, fmt)) { |
| 67 |
strcpy(imfmt, fmt); |
| 68 |
return(1); |
| 69 |
} |
| 70 |
error(WARNING, "wrong picture format"); |
| 71 |
return(-1); |
| 72 |
} |
| 73 |
if (isaspect(s)) { |
| 74 |
pixaspect = aspectval(s); |
| 75 |
return(1); |
| 76 |
} |
| 77 |
return(0); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/* Load previous frame (allocates arrays on first call) */ |
| 82 |
static void |
| 83 |
loadprev(int fno) |
| 84 |
{ |
| 85 |
char *err; |
| 86 |
IMGHEAD ih; |
| 87 |
RESOLU rs; |
| 88 |
char fname[256]; |
| 89 |
FILE *fp; |
| 90 |
int y, hres; |
| 91 |
int fd; |
| 92 |
|
| 93 |
if (fno == fnprev) /* already loaded? */ |
| 94 |
return; |
| 95 |
sprintf(fname, hdrspec, fno); /* open picture file */ |
| 96 |
if ((fp = fopen(fname, "r")) == NULL) { |
| 97 |
sprintf(errmsg, "cannot open picture file \"%s\"", fname); |
| 98 |
error(SYSTEM, errmsg); |
| 99 |
} |
| 100 |
ih.vp = &vwprev; |
| 101 |
ih.gotview = 0; |
| 102 |
ih.ev = 1.; |
| 103 |
if (getheader(fp, headline, &ih) < 0) |
| 104 |
goto readerr; |
| 105 |
if (!ih.gotview) { |
| 106 |
sprintf(errmsg, "missing view in picture \"%s\"", fname); |
| 107 |
error(USER, errmsg); |
| 108 |
} |
| 109 |
if ((err = setview(&vwprev)) != NULL) |
| 110 |
error(USER, err); |
| 111 |
exprev = ih.ev; |
| 112 |
if (!fgetsresolu(&rs, fp)) |
| 113 |
goto readerr; |
| 114 |
if (rs.rt != PIXSTANDARD) { |
| 115 |
sprintf(errmsg, "unsupported orientation in picture \"%s\"", fname); |
| 116 |
error(USER, errmsg); |
| 117 |
} |
| 118 |
if (!imres.xr) { /* allocate buffers */ |
| 119 |
imres = rs; |
| 120 |
imsum = (COLOR *)ecalloc(imres.xr*imres.yr, sizeof(COLOR)); |
| 121 |
imbuf = (COLOR *)emalloc(sizeof(COLOR)*imres.xr*imres.yr); |
| 122 |
zbuf = (float *)emalloc(sizeof(float)*imres.xr*imres.yr); |
| 123 |
imprev = (COLR *)emalloc(sizeof(COLR)*rs.xr*rs.yr); |
| 124 |
zprev = (float *)emalloc(sizeof(float)*rs.xr*rs.yr); |
| 125 |
} else if ((imres.xr != rs.xr) | (imres.yr != rs.yr)) { |
| 126 |
sprintf(errmsg, "resolution mismatch in picture \"%s\"", fname); |
| 127 |
error(USER, errmsg); |
| 128 |
} |
| 129 |
hres = scanlen(&rs); /* load picture */ |
| 130 |
for (y = 0; y < numscans(&rs); y++) |
| 131 |
if (freadcolrs(imprev + y*hres, hres, fp) < 0) |
| 132 |
goto readerr; |
| 133 |
fclose(fp); |
| 134 |
sprintf(fname, zbfspec, fno); /* load depth buffer */ |
| 135 |
if ((fd = open(fname, O_RDONLY)) < 0) { |
| 136 |
sprintf(errmsg, "cannot open depth buffer \"%s\"", fname); |
| 137 |
error(SYSTEM, errmsg); |
| 138 |
} |
| 139 |
if (read(fd, zprev, sizeof(float)*rs.xr*rs.yr) != sizeof(float)*rs.xr*rs.yr) |
| 140 |
goto readerr; |
| 141 |
close(fd); |
| 142 |
fnprev = fno; |
| 143 |
return; |
| 144 |
readerr: |
| 145 |
sprintf(errmsg, "error loading file \"%s\"", fname); |
| 146 |
error(USER, errmsg); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/* Interpolate between two image pixels */ |
| 151 |
/* XXX skipping expensive ray vector calculations for now */ |
| 152 |
static void |
| 153 |
interp_pixel(COLOR con, const VIEW *vwn, int xn, int yn, double zval, |
| 154 |
double pos, int xp, int yp, int interpOK) |
| 155 |
{ |
| 156 |
const int hres = scanlen(&imres); |
| 157 |
RREAL ipos; |
| 158 |
FVECT wprev, wcoor, rdir; |
| 159 |
int np, xd, yd, nd; |
| 160 |
COLOR cpr; |
| 161 |
double sf; |
| 162 |
/* check if off image */ |
| 163 |
if ((xp < 0) | (xp >= hres)) |
| 164 |
return; |
| 165 |
if ((yp < 0) | (yp >= numscans(&imres))) |
| 166 |
return; |
| 167 |
np = yp*hres + xp; /* get indexed destination */ |
| 168 |
xd = (1.-pos)*xp + pos*xn + .5; |
| 169 |
yd = (1.-pos)*yp + pos*yn + .5; |
| 170 |
nd = yd*hres + xd; |
| 171 |
/* check depth */ |
| 172 |
if (interpOK) |
| 173 |
zval = (1.-pos)*zprev[np] + pos*zval; |
| 174 |
if (zval >= zbuf[nd]) |
| 175 |
return; |
| 176 |
zbuf[nd] = zval; /* assign new depth */ |
| 177 |
copycolor(imbuf[nd], con); /* assign new color */ |
| 178 |
if (!interpOK) |
| 179 |
return; |
| 180 |
scalecolor(imbuf[nd], pos); |
| 181 |
sf = (1.-pos)/exprev; |
| 182 |
colr_color(cpr, imprev[np]); |
| 183 |
scalecolor(cpr, sf); |
| 184 |
addcolor(imbuf[nd], cpr); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/* Find neighbor with minimum depth from buffer */ |
| 189 |
static int |
| 190 |
neigh_zmin(const float *zb, int n) |
| 191 |
{ |
| 192 |
const int hres = scanlen(&imres); |
| 193 |
const int vc = n / hres; |
| 194 |
const int hc = n - vc*hres; |
| 195 |
float zbest = ZGONE; |
| 196 |
int nbest = 0; |
| 197 |
|
| 198 |
if (hc > 0 && zb[n-1] < zbest) |
| 199 |
zbest = zb[nbest = n-1]; |
| 200 |
if (hc < hres-1 && zb[n+1] < zbest) |
| 201 |
zbest = zb[nbest = n+1]; |
| 202 |
if (vc > 0 && zb[n-hres] < zbest) |
| 203 |
zbest = zb[nbest = n-hres]; |
| 204 |
if (vc < numscans(&imres)-1 && zb[n+hres] < zbest) |
| 205 |
return(n+hres); |
| 206 |
return(nbest); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/* Fill in missing pixels from immediate neighbors */ |
| 211 |
static void |
| 212 |
fill_missing(void) |
| 213 |
{ |
| 214 |
int n, m; |
| 215 |
|
| 216 |
for (n = imres.xr*imres.yr; n--; ) |
| 217 |
if (zbuf[n] >= .9*FHUGE && |
| 218 |
zbuf[m = neigh_zmin(zbuf,n)] < .9*FHUGE) |
| 219 |
copycolor(imbuf[n], imbuf[m]); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
/* Load a subframe */ |
| 224 |
static void |
| 225 |
addframe(double fpos) |
| 226 |
{ |
| 227 |
COLOR backg; |
| 228 |
char *err; |
| 229 |
VIEW fvw; |
| 230 |
RESOLU rs; |
| 231 |
IMGHEAD ihead; |
| 232 |
char fname[256]; |
| 233 |
FILE *fpimg; |
| 234 |
int fdzbf, fdmvo; |
| 235 |
COLR *iscan; |
| 236 |
float *zscan; |
| 237 |
uint16 *mscan; |
| 238 |
int hres, n, h; |
| 239 |
|
| 240 |
loadprev((int)fpos); /* make sure we have previous */ |
| 241 |
fpos -= floor(fpos); |
| 242 |
if (fpos <= .02) { /* special case */ |
| 243 |
COLOR col; |
| 244 |
for (n = imres.xr*imres.yr; n--; ) { |
| 245 |
colr_color(col, imprev[n]); |
| 246 |
addcolor(imsum[n], col); |
| 247 |
} |
| 248 |
return; |
| 249 |
} |
| 250 |
/* open input files */ |
| 251 |
sprintf(fname, hdrspec, fnprev+1); |
| 252 |
if ((fpimg = fopen(fname, "r")) == NULL) |
| 253 |
goto openerr; |
| 254 |
fvw = vwprev; |
| 255 |
ihead.vp = &fvw; |
| 256 |
ihead.gotview = 0; |
| 257 |
ihead.ev = 1.; |
| 258 |
if (getheader(fpimg, headline, &ihead) < 0) |
| 259 |
goto readerr; |
| 260 |
if (!ihead.gotview) { |
| 261 |
sprintf(errmsg, "missing view in picture \"%s\"", fname); |
| 262 |
error(USER, errmsg); |
| 263 |
} |
| 264 |
if ((err = setview(&fvw)) != NULL) |
| 265 |
error(USER, err); |
| 266 |
if (!fgetsresolu(&rs, fpimg)) |
| 267 |
goto readerr; |
| 268 |
if (rs.rt != PIXSTANDARD) { |
| 269 |
sprintf(errmsg, "unsupported orientation in picture \"%s\"", fname); |
| 270 |
error(USER, errmsg); |
| 271 |
} |
| 272 |
if ((rs.xr != imres.xr) | (rs.yr != imres.yr)) { |
| 273 |
sprintf(errmsg, "resolution mismatch for picture \"%s\"", fname); |
| 274 |
error(USER, errmsg); |
| 275 |
} |
| 276 |
vwsum.type = fvw.type; /* average in view */ |
| 277 |
for (n = 3; n--; ) { |
| 278 |
vwsum.vp[n] += (1.-fpos)*vwprev.vp[n] + fpos*fvw.vp[n]; |
| 279 |
vwsum.vdir[n] += (1.-fpos)*vwprev.vdir[n] + fpos*fvw.vdir[n]; |
| 280 |
vwsum.vup[n] += (1.-fpos)*vwprev.vdir[n] + fpos*fvw.vdir[n]; |
| 281 |
} |
| 282 |
vwsum.vdist += (1.-fpos)*vwprev.vdist + fpos*fvw.vdist; |
| 283 |
vwsum.horiz += (1.-fpos)*vwprev.horiz + fpos*fvw.horiz; |
| 284 |
vwsum.vert += (1.-fpos)*vwprev.vert + fpos*fvw.vert; |
| 285 |
vwsum.voff += (1.-fpos)*vwprev.voff + fpos*fvw.voff; |
| 286 |
vwsum.hoff += (1.-fpos)*vwprev.hoff + fpos*fvw.hoff; |
| 287 |
vwsum.vfore += (1.-fpos)*vwprev.vfore + fpos*fvw.vfore; |
| 288 |
vwsum.vaft += (1.-fpos)*vwprev.vaft + fpos*fvw.vaft; |
| 289 |
sprintf(fname, zbfspec, fnprev+1); |
| 290 |
if ((fdzbf = open(fname, O_RDONLY)) < 0) |
| 291 |
goto openerr; |
| 292 |
sprintf(fname, mvospec, fnprev+1); |
| 293 |
if ((fdmvo = open(fname, O_RDONLY)) < 0) |
| 294 |
goto openerr; |
| 295 |
setcolor(backg, .0, .0, .0); /* clear image & depth buffers */ |
| 296 |
for (n = rs.xr*rs.yr; n--; ) { |
| 297 |
if (zprev[n] >= .9*FHUGE && |
| 298 |
zprev[neigh_zmin(zprev,n)] >= .9*FHUGE) |
| 299 |
colr_color(backg, imprev[n]); |
| 300 |
copycolor(imbuf[n], backg); |
| 301 |
zbuf[n] = ZGONE; |
| 302 |
} |
| 303 |
hres = scanlen(&rs); /* process a scanline at a time */ |
| 304 |
iscan = (COLR *)emalloc(sizeof(COLR)*hres); |
| 305 |
zscan = (float *)emalloc(sizeof(float)*hres); |
| 306 |
mscan = (uint16 *)emalloc(sizeof(uint16)*3*hres); |
| 307 |
for (n = 0; n < numscans(&rs); n++) { |
| 308 |
const double sf = 1./ihead.ev; |
| 309 |
COLOR cval; |
| 310 |
if (freadcolrs(iscan, hres, fpimg) < 0) |
| 311 |
goto readerr; |
| 312 |
if (read(fdzbf, zscan, sizeof(float)*hres) != |
| 313 |
sizeof(float)*hres) |
| 314 |
goto readerr; |
| 315 |
if (read(fdmvo, mscan, sizeof(uint16)*3*hres) != |
| 316 |
sizeof(uint16)*3*hres) |
| 317 |
goto readerr; |
| 318 |
for (h = hres; h--; ) { |
| 319 |
if (!mscan[3*h]) |
| 320 |
continue; /* unknown motion */ |
| 321 |
colr_color(cval, iscan[h]); |
| 322 |
scalecolor(cval, sf); |
| 323 |
interp_pixel(cval, &fvw, h, n, zscan[h], fpos, |
| 324 |
h + (int)mscan[3*h] - 32768, |
| 325 |
n - (int)mscan[3*h+1] + 32768, |
| 326 |
mscan[3*h+2]); |
| 327 |
} |
| 328 |
} |
| 329 |
/* fill in missing pixels */ |
| 330 |
fill_missing(); |
| 331 |
/* add in results */ |
| 332 |
for (n = imres.xr*imres.yr; n--; ) |
| 333 |
addcolor(imsum[n], imbuf[n]); |
| 334 |
/* clean up */ |
| 335 |
free(mscan); |
| 336 |
free(zscan); |
| 337 |
free(iscan); |
| 338 |
close(fdmvo); |
| 339 |
close(fdzbf); |
| 340 |
fclose(fpimg); |
| 341 |
return; |
| 342 |
openerr: |
| 343 |
sprintf(errmsg, "cannot open input \"%s\"", fname); |
| 344 |
error(SYSTEM, errmsg); |
| 345 |
readerr: |
| 346 |
error(SYSTEM, "error reading input file"); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
/* Average and write our image out to the given file */ |
| 351 |
static void |
| 352 |
write_average(FILE *fp) |
| 353 |
{ |
| 354 |
const int hres = scanlen(&imres); |
| 355 |
const double sf = 1./(double)nsamps; |
| 356 |
const float csf = exprev*sf; |
| 357 |
int n, h; |
| 358 |
/* normalize view & image */ |
| 359 |
for (n = 3; n--; ) { |
| 360 |
vwsum.vp[n] *= sf; |
| 361 |
vwsum.vdir[n] *= sf; |
| 362 |
vwsum.vup[n] *= sf; |
| 363 |
} |
| 364 |
vwsum.vdist *= sf; |
| 365 |
vwsum.horiz *= sf; |
| 366 |
vwsum.vert *= sf; |
| 367 |
vwsum.voff *= sf; |
| 368 |
vwsum.hoff *= sf; |
| 369 |
vwsum.vfore *= sf; |
| 370 |
vwsum.vaft *= sf; |
| 371 |
for (n = imres.xr*imres.yr; n--; ) |
| 372 |
scalecolor(imsum[n], csf); |
| 373 |
/* write it out */ |
| 374 |
fputs(VIEWSTR, fp); |
| 375 |
fprintview(&vwsum, fp); |
| 376 |
fputc('\n', fp); |
| 377 |
if ((pixaspect < .98) | (pixaspect > 1.02)) |
| 378 |
fputaspect(pixaspect, fp); |
| 379 |
fputexpos(exprev, fp); |
| 380 |
if (strcmp(imfmt, PICFMT)) |
| 381 |
fputformat(imfmt, fp); |
| 382 |
fputc('\n', fp); /* end of info. header */ |
| 383 |
fputsresolu(&imres, fp); |
| 384 |
for (n = 0; n < numscans(&imres); n++) |
| 385 |
if (fwritescan(imsum + n*hres, hres, fp) < 0) |
| 386 |
error(SYSTEM, "write error on picture output"); |
| 387 |
} |
| 388 |
|
| 389 |
/* Load frame sequence and compute motion blur output */ |
| 390 |
int |
| 391 |
main(int argc, char *argv[]) |
| 392 |
{ |
| 393 |
double fstart, fend, fstep, fcur; |
| 394 |
|
| 395 |
progname = argv[0]; |
| 396 |
SET_DEFAULT_BINARY(); |
| 397 |
if (argc != 5) |
| 398 |
goto userr; |
| 399 |
/* get frame range & sampling */ |
| 400 |
switch (sscanf(argv[1], "%lf,%lf/%d", &fstart, &fend, &nsamps)) { |
| 401 |
case 1: |
| 402 |
fend = fstart; |
| 403 |
nsamps = 0; |
| 404 |
break; |
| 405 |
case 2: |
| 406 |
nsamps = 0; |
| 407 |
/* fall through */ |
| 408 |
case 3: |
| 409 |
if (fend < fstart) |
| 410 |
goto userr; |
| 411 |
if (fend <= fstart+FTINY) |
| 412 |
nsamps = 0; |
| 413 |
break; |
| 414 |
default: |
| 415 |
goto userr; |
| 416 |
} |
| 417 |
if (fstart < 1) |
| 418 |
goto userr; |
| 419 |
if (nsamps <= 0) |
| 420 |
nsamps = (fend - fstart)*17.; |
| 421 |
if (nsamps) { |
| 422 |
if (nsamps > 100) nsamps = 100; |
| 423 |
fstep = (fend - fstart)/nsamps; |
| 424 |
} else { |
| 425 |
fstep = 1.; |
| 426 |
fstart -= .5; |
| 427 |
nsamps = 1; |
| 428 |
} |
| 429 |
hdrspec = argv[2]; |
| 430 |
zbfspec = argv[3]; |
| 431 |
mvospec = argv[4]; |
| 432 |
/* load and filter each subframe */ |
| 433 |
for (fcur = fstart + .5*fstep; fcur <= fend; fcur += fstep) |
| 434 |
addframe(fcur); |
| 435 |
/* write results to stdout */ |
| 436 |
SET_FILE_BINARY(stdout); |
| 437 |
newheader("RADIANCE", stdout); |
| 438 |
printargs(argc, argv, stdout); |
| 439 |
fputnow(stdout); |
| 440 |
write_average(stdout); |
| 441 |
return(fflush(stdout) == EOF); |
| 442 |
userr: |
| 443 |
fprintf(stderr, "Usage: %s f0,f1[/n] HDRspec ZBUFspec MVOspec\n", progname); |
| 444 |
return(1); |
| 445 |
} |