9 |
|
* Created by Greg Ward on 07/11/2024. |
10 |
|
*/ |
11 |
|
|
12 |
+ |
#define DEBUG 1 // XXX temporary! |
13 |
+ |
|
14 |
|
#include <ctype.h> |
15 |
|
#include "platform.h" |
16 |
|
#include "RpictSimulManager.h" |
52 |
|
scolor_out(col, primp, rp->rcol); |
53 |
|
return SetPixel(x, y, col, zv); |
54 |
|
default: |
55 |
< |
error(INTERNAL, "missing color space type in SetPixel()"); |
55 |
> |
error(INTERNAL, "botched color space type in SetPixel()"); |
56 |
|
} |
57 |
|
return false; |
58 |
|
} |
81 |
|
case RDTrgb: |
82 |
|
primp = pr ? pr : stdprims; |
83 |
|
break; |
84 |
< |
default: // RDTscolr | RDTscolor |
84 |
> |
case RDTscolr: |
85 |
> |
case RDTscolor: |
86 |
|
primp = NULL; |
87 |
|
break; |
88 |
+ |
default: |
89 |
+ |
error(INTERNAL, "botched color space type in SetColorSpace()"); |
90 |
|
} |
91 |
|
dtyp = RDTnewCT(dtyp, cs); |
87 |
– |
if (primp) |
88 |
– |
xyz2myrgbmat[0][0] = 0; |
92 |
|
return true; |
93 |
|
} |
94 |
|
|
105 |
|
|
106 |
|
if (!xydim) return false; |
107 |
|
if (!ap) ap = &pasp; |
108 |
< |
pvw = vw; // save previous view for motion blur |
109 |
< |
vw = v; |
108 |
> |
if (&v == &vw) { |
109 |
> |
pvw.type = 0; |
110 |
> |
} else { |
111 |
> |
pvw = vw; // save previous view for motion blur |
112 |
> |
vw = v; |
113 |
> |
} |
114 |
|
const char * verr = setview(&vw); |
115 |
|
if (verr) { |
116 |
|
error(WARNING, verr); |
145 |
|
RpictSimulManager::RtCall(RAY *r, void *cd) |
146 |
|
{ |
147 |
|
RpictSimulManager * rsp = (RpictSimulManager *)cd; |
148 |
< |
const int ty = r->rno / rsp->TWidth(); |
149 |
< |
const int tx = r->rno - (RNUMBER)ty*rsp->TWidth(); |
148 |
> |
const int ty = (r->rno-1) / rsp->TWidth(); |
149 |
> |
const int tx = r->rno-1 - (RNUMBER)ty*rsp->TWidth(); |
150 |
|
|
151 |
|
if (ty >= rsp->THeight()) { |
152 |
|
error(INTERNAL, "bad pixel calculation position in RtCall()"); |
202 |
|
bool |
203 |
|
RpictSimulManager::ComputePixel(int x, int y) |
204 |
|
{ |
205 |
< |
static const SCOLOR scBlack = {0}; |
205 |
> |
DCHECK(doneMap.OffBitMap(x,y), |
206 |
> |
CONSISTENCY, "illegal pixel index in ComputPixel()"); |
207 |
|
int i; |
208 |
|
FVECT rodir[2]; |
209 |
|
double hpos = (x+pixjitter())/TWidth(); |
210 |
|
double vpos = (y+pixjitter())/THeight(); |
211 |
|
double dlim = viewray(rodir[0], rodir[1], &tvw, hpos, vpos); |
212 |
|
if (dlim < -FTINY) { // off view? |
213 |
< |
pacc.SetPixel(x, y, scBlack); |
213 |
> |
pacc.SetPixel(x, y, scblack); |
214 |
|
doneMap.Set(x, y); |
215 |
|
return true; |
216 |
|
} |
235 |
|
for (i = (dlim > FTINY)*3; i--; ) |
236 |
|
rodir[1][i] *= dlim; |
237 |
|
|
238 |
< |
return EnqueueRay(rodir[0], rodir[1], (RNUMBER)y*TWidth()+x); |
238 |
> |
return EnqueueRay(rodir[0], rodir[1], (RNUMBER)y*TWidth()+x+1); |
239 |
|
} |
240 |
|
|
241 |
|
// Check if neighbor differences are below pixel sampling threshold |
242 |
|
bool |
243 |
< |
RpictSimulManager::BelowSampThresh(int x, int y, const int noff[4][2]) const |
243 |
> |
RpictSimulManager::BelowSampThresh(const int x, const int y, const int noff[4][2]) const |
244 |
|
{ |
245 |
|
SCOLOR pval[4]; |
246 |
|
float dist[4]; |
247 |
|
int i, j; |
248 |
|
|
249 |
|
for (i = 4; i--; ) { // get pixels from tile store |
250 |
< |
int px = x + noff[i][0]; |
251 |
< |
int py = y + noff[i][1]; |
250 |
> |
const int px = x + noff[i][0]; |
251 |
> |
const int py = y + noff[i][1]; |
252 |
|
if (!doneMap.Check(px, py) || |
253 |
|
!pacc.GetPixel(px, py, pval[i], &dist[i])) |
254 |
|
return false; |
255 |
|
} |
256 |
< |
const bool spectr = (pacc.NC() > 3); |
257 |
< |
for (i = 4; --i; ) // do pairwise comparisons |
258 |
< |
for (j = i; j--; ) { |
259 |
< |
if (pacc.DepthType() && |
252 |
< |
(dist[i] - dist[j] > maxdiff*dist[j]) | |
256 |
> |
// do pairwise comparisons |
257 |
> |
for (i = (pacc.DepthType() != RDTnone)*4; --i > 0; ) |
258 |
> |
for (j = i; j--; ) |
259 |
> |
if ((dist[i] - dist[j] > maxdiff*dist[j]) | |
260 |
|
(dist[j] - dist[i] > maxdiff*dist[i])) |
261 |
|
return false; |
262 |
< |
if (spectr ? sbigsdiff(pval[i], pval[j], maxdiff) : |
263 |
< |
bigdiff(pval[i], pval[j], maxdiff)) |
262 |
> |
if (pacc.NC() > 3) { |
263 |
> |
for (i = 4; --i; ) |
264 |
> |
for (j = i; j--; ) |
265 |
> |
if (sbigsdiff(pval[i], pval[j], maxdiff)) |
266 |
|
return false; |
267 |
< |
} |
268 |
< |
return true; // linear interpolation OK |
267 |
> |
} else { |
268 |
> |
for (i = 4; --i; ) |
269 |
> |
for (j = i; j--; ) |
270 |
> |
if (bigdiff(pval[i], pval[j], maxdiff)) |
271 |
> |
return false; |
272 |
> |
} |
273 |
> |
return true; |
274 |
|
} |
275 |
|
|
276 |
|
// Fill an interior square patch with interpolated values |
277 |
|
void |
278 |
< |
RpictSimulManager::FillSquare(int x, int y, const int noff[4][2]) |
278 |
> |
RpictSimulManager::FillSquare(const int x, const int y, const int noff[4][2]) |
279 |
|
{ |
280 |
|
SCOLOR pval[4]; |
281 |
|
float dist[4]; |
282 |
|
int i, j; |
283 |
|
// assumes 4 corners are valid! |
284 |
< |
for (i = 4; i--; ) |
284 |
> |
for (i = 4; i--; ) { |
285 |
> |
DCHECK(!doneMap.Check(x+noff[i][0], y+noff[i][1]), |
286 |
> |
CONSISTENCY, "inclusion of bad pixel in FillSquare()"); |
287 |
|
pacc.GetPixel(x+noff[i][0], y+noff[i][1], pval[i], &dist[i]); |
288 |
< |
|
288 |
> |
} |
289 |
|
i = abs(noff[1][0]-noff[0][0]); |
290 |
< |
j = abs(noff[1][1]-noff[0][1]); |
291 |
< |
const int slen = i > j ? i : j; |
276 |
< |
const double sf = 1./slen; |
290 |
> |
j = abs(noff[1][1]-noff[0][1]); // i==j for diamond fill |
291 |
> |
const int slen = (i > j) ? i : j; |
292 |
|
const bool spectr = (pacc.NC() > 3); |
293 |
< |
for (i = 0; i <= slen; i++) { // bilinear interpolant |
294 |
< |
const double c1 = i*sf; |
295 |
< |
for (j = 0; j <= slen; j++) { |
296 |
< |
const double c2 = j*sf; |
293 |
> |
for (i = slen+1 + (i==j)*slen; i--; ) { |
294 |
> |
const double c1 = (i>slen ? i-slen-.5 : (double)i)/slen; |
295 |
> |
for (j = slen + (i<=slen); j--; ) { |
296 |
> |
const double c2 = (j + (i>slen)*.5)/slen; |
297 |
|
const int px = int(x + (1.-c1)*(1.-c2)*noff[0][0] + |
298 |
|
c1*(1.-c2)*noff[1][0] + |
299 |
|
(1.-c1)*c2*noff[2][0] + |
343 |
|
|
344 |
|
// helper function to set up quincunx sampling |
345 |
|
static void |
346 |
< |
SetQuincunx(ABitMap2 *bmp2, int noff[4][2], const int spc, const bool odd) |
346 |
> |
SetQuincunx(ABitMap2 *bmp2, int noff[4][2], const int spc, bool odd, int x0, int y) |
347 |
|
{ |
348 |
< |
for (int y = 0; y < bmp2->Height(); y += spc>>1) |
334 |
< |
for (int x = odd*(spc>>1); x < bmp2->Width(); x += spc) |
335 |
< |
bmp2->Set(x, y); |
336 |
< |
// order neighbors CCW |
337 |
< |
if (odd) { |
348 |
> |
if (odd) { // order neighbors CCW |
349 |
|
noff[0][0] = spc>>1; noff[0][1] = 0; |
350 |
|
noff[1][0] = 0; noff[1][1] = spc>>1; |
351 |
|
noff[2][0] = -(spc>>1); noff[2][1] = 0; |
356 |
|
noff[2][0] = -(spc>>1); noff[2][1] = -(spc>>1); |
357 |
|
noff[3][0] = spc>>1; noff[3][1] = -(spc>>1); |
358 |
|
} |
359 |
+ |
int nsteps; // non-negative range |
360 |
+ |
if (x0 < -(spc>>1)) { |
361 |
+ |
nsteps = (spc-1 - x0 - (spc>>1))/spc; |
362 |
+ |
x0 += nsteps*spc; |
363 |
+ |
} |
364 |
+ |
if (y < 0) { // get past y==0 |
365 |
+ |
nsteps = ((spc>>1)-1 - y)/(spc>>1); |
366 |
+ |
y += nsteps*(spc>>1); |
367 |
+ |
odd ^= nsteps&1; |
368 |
+ |
} |
369 |
+ |
while (y < bmp2->Height()) { |
370 |
+ |
for (int x = x0 + odd*(spc>>1); x < bmp2->Width(); x += spc) |
371 |
+ |
bmp2->Set(x, y); |
372 |
+ |
y += spc>>1; |
373 |
+ |
odd = !odd; |
374 |
+ |
} |
375 |
|
} |
376 |
|
|
377 |
|
// Render (or finish rendering) current tile |
378 |
|
bool |
379 |
< |
RpictSimulManager::RenderRect() |
379 |
> |
RpictSimulManager::RenderRect(const int x0, const int y0) |
380 |
|
{ |
381 |
|
if (!tvw.type || !Ready()) { |
382 |
|
error(INTERNAL, "need octree and view for RenderRect()"); |
386 |
|
int sp2 = ceil(log2((TWidth()>THeight() ? TWidth() : THeight()) - 1.)); |
387 |
|
int layer = 0; |
388 |
|
int x, y; |
362 |
– |
fprintf(stderr, "Rendering %dx%d tile with psample=%d, maxdiff=%.3f ...\n", |
363 |
– |
TWidth(), THeight(), psample, maxdiff); |
389 |
|
while (sp2 > 0) { |
390 |
|
ABitMap2 sampMap(TWidth(), THeight()); |
391 |
|
int noff[4][2]; |
392 |
|
if ((prCB != NULL) & (barPix == NULL)) |
393 |
|
(*prCB)(100.*doneMap.SumTotal()/doneMap.Width()/doneMap.Height()); |
394 |
< |
SetQuincunx(&sampMap, noff, 1<<sp2, layer&1); |
394 |
> |
SetQuincunx(&sampMap, noff, 1<<sp2, layer&1, x0, y0); |
395 |
|
sampMap -= doneSamples; // avoid resampling pixels |
396 |
|
// Are we into adaptive sampling realm? |
397 |
|
if (noff[0][0]*noff[0][0] + noff[0][1]*noff[0][1] < psample*psample) { |
398 |
< |
if (FlushQueue() < 0) // need results to check threshold |
398 |
> |
if (FlushQueue() < 0) // need results to check thresholds |
399 |
|
return false; |
400 |
|
ABitMap2 fillMap = sampMap; |
401 |
|
for (x = y = 0; sampMap.Find(&x, &y); x++) |
403 |
|
sampMap.Reset(x, y); |
404 |
|
// spread sampling to neighbors... |
405 |
|
const ABitMap2 origSampMap = sampMap; |
406 |
< |
for (int yoff = -(1<<(sp2-1)); |
382 |
< |
yoff <= 1<<(sp2-1); yoff += 1<<sp2) |
383 |
< |
for (int xoff = -(1<<(sp2-1)); |
384 |
< |
xoff <= 1<<(sp2-1); xoff += 1<<sp2) { |
406 |
> |
for (x = 4; x--; ) { |
407 |
|
ABitMap2 stamp = origSampMap; |
408 |
< |
stamp.Shift(xoff, yoff); |
408 |
> |
stamp.Shift(noff[x][0] + noff[(x+1)&3][0], |
409 |
> |
noff[x][1] + noff[(x+1)&3][1]); |
410 |
|
sampMap |= stamp; |
411 |
|
} // ...but don't resample what's done |
412 |
|
sampMap -= doneSamples; |
413 |
|
// interpolate smooth regions |
414 |
|
fillMap -= sampMap; |
415 |
+ |
fillMap -= doneSamples; |
416 |
|
for (x = y = 0; fillMap.Find(&x, &y); x++) |
417 |
|
FillSquare(x, y, noff); |
418 |
|
doneSamples |= doneMap; |
419 |
|
} // compute required ray samples |
420 |
|
for (x = y = 0; sampMap.Find(&x, &y); x++) |
421 |
< |
if (!ComputePixel(x, y)) |
421 |
> |
if (!ComputePixel(x, y)) { |
422 |
> |
sprintf(errmsg, "ComputePixel(%d,%d) failed", x, y); |
423 |
> |
error(WARNING, errmsg); |
424 |
|
return false; |
425 |
+ |
} |
426 |
|
doneSamples |= sampMap; // samples now done or at least queued |
400 |
– |
fprintf(stderr, "Sampled %ld pixels at (sp2,layer)=(%d,%d)\n", |
401 |
– |
(long)sampMap.SumTotal(), sp2, layer); |
402 |
– |
fprintf(stderr, "\t%ld pixels (%.3f%%) completed (+%ld in process)\n", |
403 |
– |
(long)doneMap.SumTotal(), 100.*doneMap.SumTotal()/doneMap.Width()/doneMap.Height(), |
404 |
– |
(long)(doneSamples.SumTotal()-doneMap.SumTotal())); |
427 |
|
sp2 -= layer++ & 1; // next denser sampling |
428 |
|
} |
429 |
|
if (FlushQueue() < 0) // make sure we got everyone |
430 |
|
return false; |
431 |
|
x = y = 0; |
432 |
|
if (doneMap.Find(&x, &y, false)) { |
433 |
< |
sprintf(errmsg, "missed %ld tile pixels, e.g. (%d,%d)", |
434 |
< |
(long)doneMap.Width()*doneMap.Height() - |
435 |
< |
doneMap.SumTotal(), x, y); |
433 |
> |
sprintf(errmsg, "missed %.4f%% of pixels in rectangle\n", |
434 |
> |
100. - 100.*doneMap.SumTotal() / |
435 |
> |
doneMap.Width() / doneMap.Height()); |
436 |
|
error(WARNING, errmsg); |
437 |
|
} |
438 |
|
if ((prCB != NULL) & (barPix == NULL)) |
458 |
|
pacc.SetColorSpace(RDTxyz); |
459 |
|
else if (prims) |
460 |
|
pacc.SetColorSpace(RDTrgb, prims); |
461 |
< |
|
461 |
> |
|
462 |
|
return SetTile(tile) && RenderRect(); |
463 |
|
} |
464 |
|
|
496 |
|
return SetTile(tile) && RenderRect(); |
497 |
|
} |
498 |
|
|
499 |
+ |
// Back to float color with 16-bit depth |
500 |
+ |
bool |
501 |
+ |
RpictSimulManager::RenderTile(COLORV *rp, int ystride, short *dp, const int *tile) |
502 |
+ |
{ |
503 |
+ |
if (!rp | (GetWidth() <= 0) | (GetHeight() <= 0) | !vw.type) |
504 |
+ |
return false; |
505 |
+ |
if (!ystride) // contiguous rows? |
506 |
+ |
ystride = TWidth(); |
507 |
+ |
pacc.Init(rp, ystride, dp); |
508 |
+ |
if (prims == xyzprims) |
509 |
+ |
pacc.SetColorSpace(RDTxyz); |
510 |
+ |
else if (prims) |
511 |
+ |
pacc.SetColorSpace(RDTrgb, prims); |
512 |
+ |
|
513 |
+ |
return SetTile(tile) && RenderRect(); |
514 |
+ |
} |
515 |
+ |
|
516 |
|
// Allocate a new render bar |
517 |
|
void |
518 |
|
RpictSimulManager::NewBar(int ht) |
543 |
|
|
544 |
|
// Shift render bar area the specified amount down the frame |
545 |
|
bool |
546 |
< |
RpictSimulManager::LowerBar(int v) |
546 |
> |
RpictSimulManager::LowerBar(int v, int ytop) |
547 |
|
{ |
509 |
– |
if (v <= 0) return !v; |
548 |
|
if (!barPix | !barDepth | (v > THeight()) | !tvw.type) |
549 |
|
return false; |
550 |
< |
tvw.voff -= double(v)/GetHeight(); |
551 |
< |
ptvw.voff -= double(v)/GetHeight(); |
550 |
> |
if (v <= 0) return !v; |
551 |
> |
if ((ytop -= v) <= 0) |
552 |
> |
return true; |
553 |
> |
tvw.voff -= double(v)/THeight(); |
554 |
> |
ptvw.voff -= double(v)/THeight(); |
555 |
|
if (v == THeight()) { |
556 |
|
doneMap.ClearBitMap(); |
557 |
|
return true; |
562 |
|
sizeof(COLORV)*NC*TWidth()*(THeight()-v)); |
563 |
|
memmove(barDepth, barDepth + TWidth()*v, |
564 |
|
sizeof(float)*TWidth()*(THeight()-v)); |
565 |
+ |
if (ytop < THeight()) // mark what we won't do as finished |
566 |
+ |
doneMap.ClearRect(0, 0, TWidth(), THeight()-ytop, true); |
567 |
|
return true; |
568 |
|
} |
569 |
|
|
586 |
|
cropview(&ptvw, 0., double(ytop-THeight())/GetHeight(), |
587 |
|
1., double(ytop)/GetHeight())) |
588 |
|
ptvw.type = 0; |
589 |
< |
// set up spectral sampling |
590 |
< |
if (setspectrsamp(CNDX, WLPART) <= 0) { |
589 |
> |
// update spectral sampling |
590 |
> |
int rv = setspectrsamp(CNDX, WLPART); |
591 |
> |
if (rv < 0) { |
592 |
|
error(USER, "unsupported spectral sampling"); |
593 |
|
return false; |
594 |
|
} |
595 |
+ |
if (!rv & (RDTcolorT(dt) != RDTscolor) & (RDTcolorT(dt) != RDTscolr)) |
596 |
+ |
error(WARNING, "spectral range incompatible with color output"); |
597 |
|
COLORV ** parr = NULL; // set up tiny source drawing |
598 |
|
float ** zarr = NULL; |
599 |
|
if (!ptvw.type && directvis && dblur <= FTINY) { |
608 |
|
} |
609 |
|
int lastOut = ytop; // render down frame |
610 |
|
while (ytop > 0) { |
565 |
– |
if (ytop < THeight()) // mark what we won't do as finished |
566 |
– |
doneMap.ClearRect(0, 0, TWidth(), THeight()-ytop, true); |
611 |
|
if (prCB) |
612 |
|
(*prCB)(100.*(GetHeight()-ytop)/GetHeight()); |
613 |
< |
if (!RenderRect()) // render this bar |
613 |
> |
if (!RenderRect(0, THeight()-ytop)) // render this bar |
614 |
|
return false; |
615 |
|
int nlines = lastOut - ytop + THeight(); |
616 |
|
if (nlines > ytop) |
649 |
|
error(SYSTEM, "cannot write SCOLOR output"); |
650 |
|
break; |
651 |
|
default: |
652 |
< |
error(INTERNAL, "missing output color type in RenderBelow()"); |
652 |
> |
error(INTERNAL, "botched output color type in RenderBelow()"); |
653 |
|
break; |
654 |
|
} |
655 |
|
bpos += pacc.NC()*TWidth(); |
658 |
|
if (fflush(pfp) == EOF || (dfp && fflush(dfp) == EOF)) |
659 |
|
error(SYSTEM, "output write error"); |
660 |
|
// advance down the frame |
661 |
< |
if (lastOut > 0 && !LowerBar(vstep)) |
661 |
> |
if (lastOut > 0 && !LowerBar(vstep, ytop)) |
662 |
|
return false; |
663 |
|
ytop -= vstep; |
664 |
|
} |
669 |
|
return true; |
670 |
|
} |
671 |
|
|
672 |
< |
/* |
629 |
< |
* Render and write a frame to the named file |
630 |
< |
* Include any header lines set prior to call |
631 |
< |
* Picture file must not already exist |
632 |
< |
* Picture to stdout if pfname==NULL |
633 |
< |
* Depth written to a command if dfname[0]=='!' |
634 |
< |
*/ |
672 |
> |
// Open new output picture file (and optional depth file) |
673 |
|
RenderDataType |
674 |
< |
RpictSimulManager::RenderFrame(const char *pfname, RenderDataType dt, const char *dfname) |
674 |
> |
RpictSimulManager::NewOutput(FILE *pdfp[2], const char *pfname, |
675 |
> |
RenderDataType dt, const char *dfname) |
676 |
|
{ |
677 |
< |
int fd = 1; |
639 |
< |
FILE * pfp = NULL; |
640 |
< |
FILE * dfp = NULL; |
641 |
< |
|
677 |
> |
pdfp[0] = pdfp[1] = NULL; |
678 |
|
if (!RDTcolorT(dt)) |
679 |
< |
error(INTERNAL, "missing pixel output type in RenderFrame()"); |
679 |
> |
error(INTERNAL, "missing color output type in NewOutput()"); |
680 |
|
if (NCSAMP == 3) { |
681 |
|
if (RDTcolorT(dt) == RDTscolr) |
682 |
|
dt = RDTnewCT(dt, prims==xyzprims ? RDTxyze : RDTrgbe); |
684 |
|
dt = RDTnewCT(dt, prims==xyzprims ? RDTxyz : RDTrgb); |
685 |
|
} |
686 |
|
if (!RDTdepthT(dt) ^ !dfname) |
687 |
< |
error(INTERNAL, "depth output requires file name and type in RenderFrame()"); |
687 |
> |
error(INTERNAL, "depth output requires file name and type in NewOutput()"); |
688 |
> |
int fd = 1; |
689 |
|
if (pfname) { // open picture output file |
690 |
|
if (pfname[0] == '!') { |
691 |
|
error(INTERNAL, "writing picture to a command not supported"); |
692 |
|
return RDTnone; |
693 |
|
} |
694 |
< |
fd = open(pfname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
694 |
> |
fd = open(pfname, O_RDWR|O_CREAT|O_EXCL, 0666); |
695 |
|
} |
696 |
|
if (fd < 0) { |
697 |
|
if ((frameNo <= 0) | (errno != EEXIST)) { |
698 |
|
sprintf(errmsg, "cannot open picture file '%s'", pfname); |
699 |
|
error(SYSTEM, errmsg); |
700 |
|
} |
701 |
< |
return RDTnone; // expected in parallel sequence |
701 |
> |
return RDTnone; // may be expected in sequence run |
702 |
|
} |
703 |
|
if (fd == 1) |
704 |
< |
pfp = stdout; |
705 |
< |
else if (!(pfp = fdopen(fd, "w"))) |
704 |
> |
pdfp[0] = stdout; |
705 |
> |
else if (!(pdfp[0] = fdopen(fd, "w+"))) |
706 |
|
error(SYSTEM, "failure calling fdopen()"); |
707 |
< |
SET_FILE_BINARY(pfp); // write picture header |
708 |
< |
if ((pfp != stdout) | (frameNo <= 1)) { |
709 |
< |
newheader("RADIANCE", pfp); |
710 |
< |
fputs(GetHeader(), pfp); |
707 |
> |
SET_FILE_BINARY(pdfp[0]); // write picture header |
708 |
> |
if ((pdfp[0] != stdout) | (frameNo <= 1)) { |
709 |
> |
newheader("RADIANCE", pdfp[0]); |
710 |
> |
fputs(GetHeadStr(), pdfp[0]); |
711 |
|
} |
712 |
< |
fputs(VIEWSTR, pfp); fprintview(&vw, pfp); fputc('\n', pfp); |
712 |
> |
fputs(VIEWSTR, pdfp[0]); fprintview(&vw, pdfp[0]); fputc('\n', pdfp[0]); |
713 |
|
if (frameNo > 0) |
714 |
< |
fprintf(pfp, "FRAME=%d\n", frameNo); |
714 |
> |
fprintf(pdfp[0], "FRAME=%d\n", frameNo); |
715 |
|
double pasp = viewaspect(&vw) * GetWidth() / GetHeight(); |
716 |
< |
if (!FABSEQ(pasp, 1.0)) |
717 |
< |
fputaspect(pasp, pfp); |
718 |
< |
fputnow(pfp); |
716 |
> |
if ((0.99 > pasp) | (pasp > 1.01)) |
717 |
> |
fputaspect(pasp, pdfp[0]); |
718 |
> |
fputnow(pdfp[0]); |
719 |
|
switch (RDTcolorT(dt)) { // set primaries and picture format |
720 |
|
case RDTrgbe: |
721 |
|
if (!prims | (prims == xyzprims)) prims = stdprims; |
722 |
< |
fputprims(prims, pfp); |
723 |
< |
fputformat(COLRFMT, pfp); |
722 |
> |
fputprims(prims, pdfp[0]); |
723 |
> |
fputformat(COLRFMT, pdfp[0]); |
724 |
|
break; |
725 |
|
case RDTxyze: |
726 |
|
prims = xyzprims; |
727 |
< |
fputformat(CIEFMT, pfp); |
727 |
> |
fputformat(CIEFMT, pdfp[0]); |
728 |
|
break; |
729 |
|
case RDTscolr: |
730 |
|
prims = NULL; |
731 |
< |
fputwlsplit(WLPART, pfp); |
732 |
< |
fputncomp(NCSAMP, pfp); |
733 |
< |
fputformat(SPECFMT, pfp); |
731 |
> |
fputwlsplit(WLPART, pdfp[0]); |
732 |
> |
fputncomp(NCSAMP, pdfp[0]); |
733 |
> |
fputformat(SPECFMT, pdfp[0]); |
734 |
|
break; |
735 |
|
case RDTrgb: |
736 |
|
if (!prims | (prims == xyzprims)) prims = stdprims; |
737 |
< |
fputprims(prims, pfp); |
738 |
< |
fputncomp(3, pfp); |
739 |
< |
fputendian(pfp); |
740 |
< |
fputformat("float", pfp); |
737 |
> |
fputprims(prims, pdfp[0]); |
738 |
> |
fputncomp(3, pdfp[0]); |
739 |
> |
fputendian(pdfp[0]); |
740 |
> |
fputformat("float", pdfp[0]); |
741 |
|
break; |
742 |
|
case RDTxyz: |
743 |
|
prims = xyzprims; |
744 |
< |
fputprims(prims, pfp); |
745 |
< |
fputncomp(3, pfp); |
746 |
< |
fputendian(pfp); |
747 |
< |
fputformat("float", pfp); |
744 |
> |
fputprims(prims, pdfp[0]); |
745 |
> |
fputncomp(3, pdfp[0]); |
746 |
> |
fputendian(pdfp[0]); |
747 |
> |
fputformat("float", pdfp[0]); |
748 |
|
break; |
749 |
|
case RDTscolor: |
750 |
|
prims = NULL; |
751 |
< |
fputwlsplit(WLPART, pfp); |
752 |
< |
fputncomp(NCSAMP, pfp); |
753 |
< |
fputendian(pfp); |
754 |
< |
fputformat("float", pfp); |
751 |
> |
fputwlsplit(WLPART, pdfp[0]); |
752 |
> |
fputncomp(NCSAMP, pdfp[0]); |
753 |
> |
fputendian(pdfp[0]); |
754 |
> |
fputformat("float", pdfp[0]); |
755 |
|
break; |
756 |
< |
default:; |
756 |
> |
default:; // pro forma - caught this above |
757 |
|
} |
758 |
< |
fputc('\n', pfp); // end picture header |
759 |
< |
fprtresolu(GetWidth(), GetHeight(), pfp); |
760 |
< |
if (dfname) { |
758 |
> |
fputc('\n', pdfp[0]); // flush picture header |
759 |
> |
if (fflush(pdfp[0]) == EOF) { |
760 |
> |
sprintf(errmsg, "cannot write header to picture '%s'", pfname); |
761 |
> |
error(SYSTEM, errmsg); |
762 |
> |
fclose(pdfp[0]); |
763 |
> |
pdfp[0] = NULL; |
764 |
> |
return RDTnone; |
765 |
> |
} |
766 |
> |
if (dfname) { // open depth output |
767 |
|
if (dfname[0] == '!') |
768 |
< |
dfp = popen(dfname+1, "w"); |
768 |
> |
pdfp[1] = popen(dfname+1, "w"); |
769 |
|
else |
770 |
< |
dfp = fopen(dfname, "w"); |
771 |
< |
if (!dfp) { |
770 |
> |
pdfp[1] = fopen(dfname, "w+"); |
771 |
> |
if (!pdfp[1]) { |
772 |
|
sprintf(errmsg, "cannot open depth output '%s'", dfname); |
773 |
|
error(SYSTEM, errmsg); |
774 |
+ |
fclose(pdfp[0]); |
775 |
+ |
pdfp[0] = NULL; |
776 |
|
return RDTnone; |
777 |
|
} |
778 |
< |
SET_FILE_BINARY(dfp); |
778 |
> |
SET_FILE_BINARY(pdfp[1]); |
779 |
|
} |
780 |
|
if (RDTdepthT(dt) == RDTdshort) { // write header for 16-bit depth? |
781 |
< |
newheader("RADIANCE", dfp); |
782 |
< |
fputs(GetHeader(), dfp); |
783 |
< |
fputs(VIEWSTR, dfp); fprintview(&vw, dfp); fputc('\n', dfp); |
784 |
< |
fputs(DEPTHSTR, dfp); fputs(dunit, dfp); fputc('\n', dfp); |
785 |
< |
fputformat(DEPTH16FMT, dfp); |
786 |
< |
fputc('\n', dfp); // end-of-info |
787 |
< |
fprtresolu(GetWidth(), GetHeight(), dfp); |
781 |
> |
newheader("RADIANCE", pdfp[1]); |
782 |
> |
fputs(GetHeadStr(), pdfp[1]); |
783 |
> |
fputs(VIEWSTR, pdfp[1]); fprintview(&vw, pdfp[1]); fputc('\n', pdfp[1]); |
784 |
> |
fputs(DEPTHSTR, pdfp[1]); fputs(dunit, pdfp[1]); fputc('\n', pdfp[1]); |
785 |
> |
fputformat(DEPTH16FMT, pdfp[1]); |
786 |
> |
fputc('\n', pdfp[1]); // end-of-info |
787 |
> |
if (fflush(pdfp[1]) == EOF) { |
788 |
> |
sprintf(errmsg, "cannot write header to '%s'", dfname); |
789 |
> |
error(SYSTEM, errmsg); |
790 |
> |
fclose(pdfp[0]); fclose(pdfp[1]); |
791 |
> |
pdfp[0] = pdfp[1] = NULL; |
792 |
> |
return RDTnone; |
793 |
> |
} |
794 |
|
} |
795 |
< |
const int bheight = (psample > 1) ? int(2*psample+.99) : 4; |
796 |
< |
const int vstep = bheight >> (psample > 1); |
795 |
> |
return dt; // ready to roll |
796 |
> |
} |
797 |
|
|
798 |
+ |
/* |
799 |
+ |
* Render and write a frame to the named file |
800 |
+ |
* Include any header lines set prior to call |
801 |
+ |
* Picture file must not exist |
802 |
+ |
* Write pixels to stdout if !pfname |
803 |
+ |
* Write depth to a command if dfname[0]=='!' |
804 |
+ |
*/ |
805 |
+ |
RenderDataType |
806 |
+ |
RpictSimulManager::RenderFrame(const char *pfname, RenderDataType dt, const char *dfname) |
807 |
+ |
{ |
808 |
+ |
FILE *pdfp[2]; |
809 |
+ |
// prepare output file(s) |
810 |
+ |
dt = NewOutput(pdfp, pfname, dt, dfname); |
811 |
+ |
if (dt == RDTnone) |
812 |
+ |
return RDTnone; |
813 |
+ |
// add resolution string(s) |
814 |
+ |
fprtresolu(GetWidth(), GetHeight(), pdfp[0]); |
815 |
+ |
if (RDTdepthT(dt) == RDTdshort) |
816 |
+ |
fprtresolu(GetWidth(), GetHeight(), pdfp[1]); |
817 |
+ |
|
818 |
+ |
const int bheight = (psample > 1) ? int(8*psample+.99) : 16; |
819 |
+ |
const int vstep = bheight >> (psample > 1); |
820 |
+ |
|
821 |
|
NewBar(bheight); // render frame if we can |
822 |
< |
if (!RenderBelow(GetHeight(), vstep, pfp, dt, dfp)) { |
823 |
< |
fclose(pfp); |
824 |
< |
if (dfp) (dfname[0] == '!') ? pclose(dfp) : fclose(dfp); |
751 |
< |
Cleanup(); |
822 |
> |
if (!RenderBelow(GetHeight(), vstep, pdfp[0], dt, pdfp[1])) { |
823 |
> |
fclose(pdfp[0]); |
824 |
> |
if (pdfp[1]) (dfname[0] == '!') ? pclose(pdfp[1]) : fclose(pdfp[1]); |
825 |
|
return RDTnone; |
826 |
|
} |
827 |
|
NewBar(); // clean up and return |
828 |
< |
if (pfp != stdout) |
829 |
< |
fclose(pfp); |
830 |
< |
if (dfp) { |
828 |
> |
if (pdfp[0] != stdout) |
829 |
> |
fclose(pdfp[0]); |
830 |
> |
if (pdfp[1]) { |
831 |
|
if (dfname[0] == '!') { |
832 |
< |
int status = pclose(dfp); |
832 |
> |
int status = pclose(pdfp[1]); |
833 |
|
if (status) { |
834 |
|
sprintf(errmsg, "depth output (%s) error status: %d", |
835 |
|
dfname, status); |
837 |
|
return RDTnone; |
838 |
|
} |
839 |
|
} else |
840 |
< |
fclose(dfp); |
840 |
> |
fclose(pdfp[1]); |
841 |
|
} |
842 |
|
return dt; |
843 |
|
} |
844 |
|
|
845 |
|
// passed struct for header line callback |
846 |
< |
struct HeaderInfo { |
846 |
> |
static struct HeaderInfo { |
847 |
|
char fmt[MAXFMTLEN]; |
848 |
|
char depth_unit[32]; |
849 |
|
int ncomp; |
864 |
|
gotview = false; |
865 |
|
endianMatch = true; |
866 |
|
} |
867 |
< |
}; |
867 |
> |
} hinfo; // XXX single copy to hold custom primitives |
868 |
|
|
869 |
|
// helper function checks header line and records req. info. |
870 |
|
static int |
906 |
|
return 0; |
907 |
|
} |
908 |
|
|
909 |
< |
// Resume partially finished rendering |
837 |
< |
// Picture file must exist |
909 |
> |
// Reopen output file(s), leaving pointers at end of (each) header |
910 |
|
RenderDataType |
911 |
< |
RpictSimulManager::ResumeFrame(const char *pfname, const char *dfname) |
911 |
> |
RpictSimulManager::ReopenOutput(FILE *pdfp[2], const char *pfname, const char *dfname) |
912 |
|
{ |
913 |
< |
if (!pfname || pfname[0] == '!') |
842 |
< |
return RDTnone; |
913 |
> |
extern const char HDRSTR[]; |
914 |
|
|
915 |
+ |
if (!pfname || pfname[0] == '!') { |
916 |
+ |
pdfp[0] = pdfp[1] = NULL; |
917 |
+ |
return RDTnone; |
918 |
+ |
} |
919 |
|
RenderDataType dt = RDTnone; |
920 |
< |
FILE * dfp = NULL; |
921 |
< |
FILE * pfp = fopen(pfname, "r+"); |
922 |
< |
if (!pfp) { |
920 |
> |
pdfp[1] = NULL; |
921 |
> |
pdfp[0] = fopen(pfname, "r+"); |
922 |
> |
if (!pdfp[0]) { |
923 |
|
sprintf(errmsg, "cannot reopen output picture '%s'", pfname); |
924 |
|
error(SYSTEM, errmsg); |
925 |
|
return RDTnone; |
926 |
|
} |
927 |
< |
SET_FILE_BINARY(pfp); |
928 |
< |
HeaderInfo hinfo; // read header information & dimensions |
929 |
< |
RESOLU res; |
930 |
< |
if (getheader(pfp, head_check, &hinfo) < 0) { |
856 |
< |
fclose(pfp); |
927 |
> |
SET_FILE_BINARY(pdfp[0]); // read header information |
928 |
> |
if (getheader(pdfp[0], head_check, &hinfo) < 0) { |
929 |
> |
fclose(pdfp[0]); |
930 |
> |
pdfp[0] = NULL; |
931 |
|
return RDTnone; |
932 |
|
} |
859 |
– |
if (!fgetsresolu(&res, pfp) || res.rt != PIXSTANDARD) { |
860 |
– |
sprintf(errmsg, "missing/bad resolution for '%s'", pfname); |
861 |
– |
error(USER, errmsg); |
862 |
– |
fclose(pfp); |
863 |
– |
return RDTnone; |
864 |
– |
} |
933 |
|
if (!hinfo.gotview) { |
934 |
|
sprintf(errmsg, "missing view for '%s'", pfname); |
935 |
|
error(USER, errmsg); |
936 |
< |
fclose(pfp); |
936 |
> |
fclose(pdfp[0]); |
937 |
> |
pdfp[0] = NULL; |
938 |
|
return RDTnone; |
939 |
|
} |
940 |
|
if (hinfo.ncomp < 3) { |
941 |
|
sprintf(errmsg, "bad # components (%d) in '%s'", hinfo.ncomp, pfname); |
942 |
|
error(USER, errmsg); |
943 |
< |
fclose(pfp); |
943 |
> |
fclose(pdfp[0]); |
944 |
> |
pdfp[0] = NULL; |
945 |
|
return RDTnone; |
946 |
|
} |
947 |
< |
int bytesPer = 0; // complicated part to set rendering/output space |
947 |
> |
// set rendering/output space |
948 |
|
if (!strcmp(hinfo.fmt, COLRFMT)) { |
949 |
< |
prims = hinfo.prims; |
949 |
> |
prims = hinfo.prims; // XXX static array |
950 |
|
int n = 8*hinfo.gotprims; |
951 |
|
while (n--) |
952 |
|
if (!FABSEQ(hinfo.prims[0][n], stdprims[0][n])) |
962 |
|
sprintf(errmsg, "incompatible sample count (%d) in '%s'", |
963 |
|
hinfo.ncomp, pfname); |
964 |
|
error(USER, errmsg); |
965 |
< |
fclose(pfp); |
965 |
> |
fclose(pdfp[0]); |
966 |
> |
pdfp[0] = NULL; |
967 |
|
return RDTnone; |
968 |
|
} |
969 |
< |
NCSAMP = hinfo.ncomp; // overrides global setting |
969 |
> |
NCSAMP = hinfo.ncomp; // overrides global setting |
970 |
|
prims = NULL; |
971 |
|
dt = RDTnewCT(dt, RDTscolr); |
901 |
– |
bytesPer = hinfo.ncomp + 1; // XXX assumes no compression |
972 |
|
} else if (!strcmp(hinfo.fmt, "float")) { |
973 |
|
if (!hinfo.endianMatch) { |
974 |
|
sprintf(errmsg, "incompatible byte ordering in '%s'", pfname); |
975 |
|
error(USER, errmsg); |
976 |
< |
fclose(pfp); |
976 |
> |
fclose(pdfp[0]); |
977 |
> |
pdfp[0] = NULL; |
978 |
|
return RDTnone; |
979 |
|
} |
980 |
|
if (hinfo.ncomp == 3) { |
981 |
< |
prims = hinfo.prims; // custom primaries? |
981 |
> |
prims = hinfo.prims; // custom primaries? |
982 |
|
int n = 8*hinfo.gotprims; |
983 |
|
while (n--) |
984 |
|
if (!FABSEQ(hinfo.prims[0][n], stdprims[0][n])) |
985 |
|
break; |
986 |
< |
if (n < 0) // standard primaries? |
986 |
> |
if (n < 0) // standard primaries? |
987 |
|
prims = stdprims; |
988 |
|
else if (hinfo.gotprims) { // or check if XYZ |
989 |
|
for (n = 8; n--; ) |
1001 |
|
prims = NULL; |
1002 |
|
dt = RDTnewCT(dt, RDTscolor); |
1003 |
|
} |
933 |
– |
bytesPer = sizeof(float)*hinfo.ncomp; |
1004 |
|
} else { |
1005 |
|
sprintf(errmsg, "unknown format (%s) for '%s'", hinfo.fmt, pfname); |
1006 |
|
error(USER, errmsg); |
1007 |
< |
fclose(pfp); |
1007 |
> |
fclose(pdfp[0]); |
1008 |
> |
pdfp[0] = NULL; |
1009 |
|
return RDTnone; |
1010 |
|
} |
1011 |
< |
vw.type = 0; // set up new (unreferenced) frame |
1012 |
< |
frameNo = 0; |
1011 |
> |
if (hinfo.gotview) { // header view overrides |
1012 |
> |
pvw = vw; |
1013 |
> |
vw = hinfo.vw; |
1014 |
> |
} |
1015 |
> |
if (!dfname) // no depth file? |
1016 |
> |
return dt; |
1017 |
> |
|
1018 |
> |
if (dfname[0] == '!') { |
1019 |
> |
error(USER, "depth data cannot be reloaded from command"); |
1020 |
> |
fclose(pdfp[0]); |
1021 |
> |
pdfp[0] = NULL; |
1022 |
> |
return RDTnone; |
1023 |
> |
} |
1024 |
> |
pdfp[1] = fopen(dfname, "r+"); |
1025 |
> |
if (!pdfp[1]) { |
1026 |
> |
sprintf(errmsg, "cannot reopen depth file '%s'", dfname); |
1027 |
> |
error(SYSTEM, errmsg); |
1028 |
> |
fclose(pdfp[0]); |
1029 |
> |
pdfp[0] = NULL; |
1030 |
> |
return RDTnone; |
1031 |
> |
} |
1032 |
> |
SET_FILE_BINARY(pdfp[1]); |
1033 |
> |
int n, len = strlen(HDRSTR); |
1034 |
> |
char buf[32]; // sniff for 16-bit header |
1035 |
> |
if (getbinary(buf, 1, len+1, pdfp[1]) < len+1) { |
1036 |
> |
sprintf(errmsg, "empty depth file '%s'", dfname); |
1037 |
> |
error(SYSTEM, errmsg); |
1038 |
> |
fclose(pdfp[0]); fclose(pdfp[1]); |
1039 |
> |
pdfp[0] = pdfp[1] = NULL; |
1040 |
> |
return RDTnone; |
1041 |
> |
} |
1042 |
> |
for (n = 0; n < len; n++) |
1043 |
> |
if (buf[n] != HDRSTR[n]) |
1044 |
> |
break; // not a Radiance header |
1045 |
> |
rewind(pdfp[1]); |
1046 |
> |
if ((n < len) | !isprint(buf[len])) |
1047 |
> |
return RDTnewDT(dt, RDTdfloat); |
1048 |
> |
|
1049 |
> |
HeaderInfo dinfo; // thinking it's 16-bit encoded |
1050 |
> |
if (getheader(pdfp[1], head_check, &dinfo) < 0) |
1051 |
> |
sprintf(errmsg, "bad header in encoded depth file '%s'", |
1052 |
> |
dfname); |
1053 |
> |
else if (strcmp(dinfo.fmt, DEPTH16FMT)) |
1054 |
> |
sprintf(errmsg, "wrong format (%s) for depth file '%s'", |
1055 |
> |
dinfo.fmt, dfname); |
1056 |
> |
else if (!SetReferenceDepth(dinfo.depth_unit)) |
1057 |
> |
sprintf(errmsg, "bad/missing reference depth (%s) in '%s'", |
1058 |
> |
dinfo.depth_unit, dfname); |
1059 |
> |
else |
1060 |
> |
errmsg[0] = '\0'; |
1061 |
> |
|
1062 |
> |
if (errmsg[0]) { |
1063 |
> |
error(USER, errmsg); |
1064 |
> |
fclose(pdfp[1]); fclose(pdfp[0]); |
1065 |
> |
pdfp[0] = pdfp[1] = NULL; |
1066 |
> |
return RDTnone; |
1067 |
> |
} |
1068 |
> |
return RDTnewDT(dt, RDTdshort); |
1069 |
> |
} |
1070 |
> |
|
1071 |
> |
// Resume partially finished rendering |
1072 |
> |
// Picture file must exist |
1073 |
> |
RenderDataType |
1074 |
> |
RpictSimulManager::ResumeFrame(const char *pfname, const char *dfname) |
1075 |
> |
{ |
1076 |
> |
FILE *pdfp[2]; |
1077 |
> |
|
1078 |
> |
RenderDataType dt = ReopenOutput(pdfp, pfname, dfname); |
1079 |
> |
if (dt == RDTnone) |
1080 |
> |
return RDTnone; |
1081 |
> |
|
1082 |
> |
int bytesPer = 0; // figure out how far we got... |
1083 |
> |
switch (RDTcolorT(dt)) { |
1084 |
> |
case RDTrgbe: |
1085 |
> |
case RDTxyze: |
1086 |
> |
break; |
1087 |
> |
case RDTscolr: |
1088 |
> |
bytesPer = NCSAMP + 1; // XXX assumes no compression |
1089 |
> |
break; |
1090 |
> |
case RDTrgb: |
1091 |
> |
case RDTxyz: |
1092 |
> |
bytesPer = sizeof(float)*3; |
1093 |
> |
break; |
1094 |
> |
case RDTscolor: |
1095 |
> |
bytesPer = sizeof(float)*NCSAMP; |
1096 |
> |
break; |
1097 |
> |
default: |
1098 |
> |
sprintf(errmsg, "unknown format for '%s'", pfname); |
1099 |
> |
error(USER, errmsg); |
1100 |
> |
fclose(pdfp[0]); |
1101 |
> |
if (pdfp[1]) fclose(pdfp[1]); |
1102 |
> |
return RDTnone; |
1103 |
> |
} |
1104 |
> |
RESOLU res; |
1105 |
> |
if (!fgetsresolu(&res, pdfp[0]) || res.rt != PIXSTANDARD) { |
1106 |
> |
sprintf(errmsg, "missing/bad resolution for '%s'", pfname); |
1107 |
> |
error(USER, errmsg); |
1108 |
> |
fclose(pdfp[0]); |
1109 |
> |
if (pdfp[1]) fclose(pdfp[1]); |
1110 |
> |
return RDTnone; |
1111 |
> |
} |
1112 |
> |
frameNo = 0; // set up unreferenced frame |
1113 |
|
int hvdim[2] = {res.xr, res.yr}; |
1114 |
|
double noAdj = 0; |
1115 |
< |
if (!NewFrame(hinfo.vw, hvdim, &noAdj) || |
1115 |
> |
if (!NewFrame(vw, hvdim, &noAdj) || |
1116 |
|
(hvdim[0] != res.xr) | (hvdim[1] != res.yr)) { |
1117 |
< |
fclose(pfp); |
1117 |
> |
error(CONSISTENCY, "unexpected resolution change in ResumeFrame()"); |
1118 |
> |
fclose(pdfp[0]); |
1119 |
> |
if (pdfp[1]) fclose(pdfp[1]); |
1120 |
|
return RDTnone; |
1121 |
|
} |
1122 |
< |
long dataStart = ftell(pfp); // picture starting point |
1122 |
> |
long dataStart = ftell(pdfp[0]); // picture starting point |
1123 |
|
if (dataStart < 0) { |
1124 |
|
sprintf(errmsg, "cannot seek on '%s'", pfname); |
1125 |
|
error(SYSTEM, errmsg); |
1126 |
< |
fclose(pfp); |
1126 |
> |
fclose(pdfp[0]); |
1127 |
> |
if (pdfp[1]) fclose(pdfp[1]); |
1128 |
|
return RDTnone; |
1129 |
|
} |
1130 |
|
long doneScans = 0; |
1131 |
|
if (bytesPer) { // fixed-width records? |
1132 |
< |
fseek(pfp, 0, SEEK_END); |
1133 |
< |
long dataEnd = ftell(pfp); |
1132 |
> |
fseek(pdfp[0], 0, SEEK_END); |
1133 |
> |
long dataEnd = ftell(pdfp[0]); |
1134 |
|
doneScans = (dataEnd - dataStart)/(bytesPer*GetWidth()); |
1135 |
|
if (dataEnd-dataStart > bytesPer*GetWidth()*doneScans) |
1136 |
< |
fseek(pfp, dataStart + bytesPer*GetWidth()*doneScans, SEEK_SET); |
1136 |
> |
fseek(pdfp[0], dataStart + bytesPer*GetWidth()*doneScans, SEEK_SET); |
1137 |
|
} else { // else get compressed scanlines |
1138 |
|
COLR * scan = (COLR *)tempbuffer(sizeof(COLR)*GetWidth()); |
1139 |
< |
while (freadcolrs(scan, GetWidth(), pfp) >= 0) |
1139 |
> |
while (freadcolrs(scan, GetWidth(), pdfp[0]) >= 0) |
1140 |
|
++doneScans; |
1141 |
< |
if (!feof(pfp)) { |
1141 |
> |
if (!feof(pdfp[0])) { |
1142 |
|
sprintf(errmsg, "error reading compressed scanline from '%s'", pfname); |
1143 |
|
error(USER, errmsg); |
1144 |
< |
fclose(pfp); |
1144 |
> |
fclose(pdfp[0]); |
1145 |
> |
if (pdfp[1]) fclose(pdfp[1]); |
1146 |
|
return RDTnone; |
1147 |
|
} |
1148 |
|
} |
1149 |
|
if (doneScans >= GetHeight()) { // nothing left to do? |
1150 |
|
sprintf(errmsg, "output file '%s' is already complete", pfname); |
1151 |
|
error(WARNING, errmsg); |
1152 |
< |
fclose(pfp); |
1152 |
> |
fclose(pdfp[0]); |
1153 |
> |
if (pdfp[1]) fclose(pdfp[1]); |
1154 |
|
return dt; |
1155 |
|
} |
1156 |
|
if (!doneScans) { |
1157 |
|
sprintf(errmsg, "restarting empty frame '%s'", pfname); |
1158 |
|
error(WARNING, errmsg); |
1159 |
|
} |
1160 |
< |
if (dfname) { // append depth file, too? |
1161 |
< |
if (dfname[0] == '!') { |
1162 |
< |
error(USER, "depth data cannot be reloaded from command"); |
1163 |
< |
fclose(pfp); |
1160 |
> |
long toSkip = 0; |
1161 |
> |
switch (RDTdepthT(dt)) { // append depth file, too? |
1162 |
> |
case RDTdfloat: |
1163 |
> |
toSkip = sizeof(float)*GetWidth()*doneScans; |
1164 |
> |
break; |
1165 |
> |
case RDTdshort: |
1166 |
> |
if (!fgetsresolu(&res, pdfp[1]) || (res.rt != PIXSTANDARD) | |
1167 |
> |
(res.xr != GetWidth()) | (res.yr != GetHeight())) { |
1168 |
> |
sprintf(errmsg, "missing/bad resolution for '%s'", dfname); |
1169 |
> |
error(USER, errmsg); |
1170 |
> |
fclose(pdfp[0]); fclose(pdfp[0]); |
1171 |
|
return RDTnone; |
1172 |
|
} |
1173 |
< |
dfp = fopen(dfname, "a"); |
1174 |
< |
if (!dfp) { |
1175 |
< |
sprintf(errmsg, "cannot reopen depth file '%s'", dfname); |
1176 |
< |
error(SYSTEM, errmsg); |
1177 |
< |
fclose(pfp); |
1178 |
< |
return RDTnone; |
1179 |
< |
} |
1180 |
< |
SET_FILE_BINARY(dfp); |
1181 |
< |
const long dflen = ftell(dfp); |
999 |
< |
if (dflen != sizeof(float)*GetWidth()*doneScans) { |
1000 |
< |
fclose(dfp); |
1001 |
< |
dfp = fopen(dfname, "r+"); |
1002 |
< |
if (!dfp) return RDTnone; // WTH? |
1003 |
< |
SET_FILE_BINARY(dfp); |
1004 |
< |
} |
1005 |
< |
if (dflen < sizeof(float)*GetWidth()*doneScans) { |
1006 |
< |
HeaderInfo dinfo; |
1007 |
< |
if (getheader(dfp, head_check, &dinfo) < 0) |
1008 |
< |
sprintf(errmsg, "bad header in encoded depth file '%s'", |
1009 |
< |
dfname); |
1010 |
< |
else if (strcmp(dinfo.fmt, DEPTH16FMT)) |
1011 |
< |
sprintf(errmsg, "wrong format (%s) for depth file '%s'", |
1012 |
< |
dinfo.fmt, dfname); |
1013 |
< |
else if (!SetReferenceDepth(dinfo.depth_unit)) |
1014 |
< |
sprintf(errmsg, "bad/missing reference depth (%s) in '%s'", |
1015 |
< |
dinfo.depth_unit, dfname); |
1016 |
< |
else if (!fscnresolu(hvdim, hvdim+1, dfp) || |
1017 |
< |
(hvdim[0] != GetWidth()) | (hvdim[1] != GetHeight())) |
1018 |
< |
sprintf(errmsg, "bad/mismatched resolution in '%s'", |
1019 |
< |
dfname); |
1020 |
< |
else |
1021 |
< |
errmsg[0] = '\0'; |
1022 |
< |
|
1023 |
< |
if (errmsg[0]) { |
1024 |
< |
error(USER, errmsg); |
1025 |
< |
fclose(dfp); |
1026 |
< |
fclose(pfp); |
1027 |
< |
return RDTnone; |
1028 |
< |
} |
1029 |
< |
const long dStart = ftell(dfp); |
1030 |
< |
if (dflen-dStart < 2*GetWidth()*doneScans) { |
1031 |
< |
sprintf(errmsg, "missing %ld depths in '%s'", |
1032 |
< |
(long)GetWidth()*doneScans - (dflen-dStart)/2, |
1033 |
< |
dfname); |
1034 |
< |
error(WARNING, errmsg); |
1035 |
< |
} |
1036 |
< |
fseek(dfp, dStart + 2*GetWidth()*doneScans, SEEK_SET); |
1037 |
< |
dt = RDTnewDT(dt, RDTdshort); |
1038 |
< |
} else { |
1039 |
< |
if (dflen > sizeof(float)*GetWidth()*doneScans) |
1040 |
< |
fseek(dfp, sizeof(float)*GetWidth()*doneScans, SEEK_SET); |
1041 |
< |
dt = RDTnewDT(dt, RDTdfloat); |
1042 |
< |
} |
1173 |
> |
toSkip = 2L*GetWidth()*doneScans; |
1174 |
> |
break; |
1175 |
> |
default:; |
1176 |
> |
} // fseek() needed for output |
1177 |
> |
if (pdfp[1] && fseek(pdfp[1], toSkip, SEEK_CUR) < 0) { |
1178 |
> |
sprintf(errmsg, "cannot seek on depth file '%s'", dfname); |
1179 |
> |
error(SYSTEM, errmsg); |
1180 |
> |
fclose(pdfp[0]); fclose(pdfp[1]); |
1181 |
> |
return RDTnone; |
1182 |
|
} |
1183 |
< |
int bheight = (psample > 1) ? int(2*psample+.99) : 4; |
1183 |
> |
int bheight = (psample > 1) ? int(8*psample+.99) : 16; |
1184 |
|
if (bheight > GetHeight()-doneScans) |
1185 |
|
bheight = GetHeight()-doneScans; |
1186 |
|
int vstep = bheight >> (psample > 1); |
1187 |
|
vstep += !vstep; |
1188 |
|
|
1189 |
|
NewBar(bheight); // render remainder if we can |
1190 |
< |
if (!RenderBelow(GetHeight()-doneScans, vstep, pfp, dt, dfp)) { |
1191 |
< |
fclose(pfp); |
1192 |
< |
if (dfp) fclose(dfp); |
1054 |
< |
Cleanup(); |
1190 |
> |
if (!RenderBelow(GetHeight()-doneScans, vstep, pdfp[0], dt, pdfp[1])) { |
1191 |
> |
fclose(pdfp[0]); |
1192 |
> |
if (pdfp[1]) fclose(pdfp[1]); |
1193 |
|
return RDTnone; |
1194 |
|
} |
1195 |
|
NewBar(); // close up and return success |
1196 |
< |
fclose(pfp); |
1197 |
< |
if (dfp) fclose(dfp); |
1196 |
> |
fclose(pdfp[0]); |
1197 |
> |
if (pdfp[1]) fclose(pdfp[1]); |
1198 |
|
return dt; |
1199 |
|
} |