1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: RpictSimulManager.cpp,v 2.14 2025/01/24 19:08:01 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* RpictSimulManager.cpp |
6 |
* |
7 |
* Rpict simulation manager implementation |
8 |
* |
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" |
17 |
#include "depthcodec.h" |
18 |
#include "random.h" |
19 |
|
20 |
/************* Imported globals from rxpmain.c *************/ |
21 |
|
22 |
extern VIEW ourview; /* viewing parameters */ |
23 |
extern int hres, vres; /* current image resolution */ |
24 |
|
25 |
extern int psample; /* pixel sample size */ |
26 |
extern double maxdiff; /* max. sample difference */ |
27 |
extern double dstrpix; /* square pixel distribution */ |
28 |
|
29 |
extern double mblur; /* motion blur parameter */ |
30 |
|
31 |
extern double dblur; /* depth-of-field blur parameter */ |
32 |
|
33 |
// Assign a pixel value (& depth) from rendered ray value |
34 |
bool |
35 |
PixelAccess::SetPixel(int x, int y, const RAY *rp) |
36 |
{ |
37 |
if (!rp) return false; |
38 |
|
39 |
COLOR col; |
40 |
float zv = 0; // get depth if needed |
41 |
if (DepthType()) |
42 |
zv = raydistance(rp); |
43 |
|
44 |
switch (ColorSpace()) { |
45 |
case RDTscolor: // keeping rendered spectrum? |
46 |
case RDTscolr: |
47 |
return SetPixel(x, y, rp->rcol, zv); |
48 |
case RDTrgb: |
49 |
case RDTrgbe: |
50 |
case RDTxyz: |
51 |
case RDTxyze: |
52 |
scolor_out(col, primp, rp->rcol); |
53 |
return SetPixel(x, y, col, zv); |
54 |
default: |
55 |
error(INTERNAL, "botched color space type in SetPixel()"); |
56 |
} |
57 |
return false; |
58 |
} |
59 |
|
60 |
// Set color space after non-empty initialization |
61 |
bool |
62 |
PixelAccess::SetColorSpace(RenderDataType cs, RGBPRIMP pr) |
63 |
{ |
64 |
if (!dtyp) return false; |
65 |
|
66 |
if (!(cs = RDTcolorT(cs))) |
67 |
cs = RDTcolorT(dtyp); |
68 |
else if (RDTcommonE(cs) ^ RDTcommonE(dtyp)) |
69 |
return false; |
70 |
|
71 |
if (NCSAMP == 3) { |
72 |
if (cs == RDTscolr) cs = RDTrgbe; |
73 |
else if (cs == RDTscolor) cs = RDTrgb; |
74 |
} |
75 |
switch (cs) { |
76 |
case RDTxyze: |
77 |
case RDTxyz: |
78 |
primp = xyzprims; |
79 |
break; |
80 |
case RDTrgbe: |
81 |
case RDTrgb: |
82 |
primp = pr ? pr : stdprims; |
83 |
break; |
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); |
92 |
return true; |
93 |
} |
94 |
|
95 |
/* |
96 |
* Set up rendering frame (call after octree loaded) |
97 |
* Overall dimensions may be adjusted for view, |
98 |
* optional pixel aspect ratio and tile grid |
99 |
* Increments frameNo if >0 |
100 |
*/ |
101 |
bool |
102 |
RpictSimulManager::NewFrame(const VIEW &v, int xydim[2], double *ap, const int *tgrid) |
103 |
{ |
104 |
double pasp = 1.; |
105 |
|
106 |
if (!xydim) return false; |
107 |
if (!ap) ap = &pasp; |
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); |
117 |
vw = pvw; |
118 |
return false; |
119 |
} |
120 |
const double va = viewaspect(&vw); |
121 |
normaspect(va, ap, &xydim[0], &xydim[1]); |
122 |
// set up tiling? |
123 |
if (tgrid && (tgrid[0] > 0) & (tgrid[1] > 0) & (tgrid[0]*tgrid[1] > 1)) { |
124 |
if ((8*tgrid[0] >= xydim[0]) | (8*tgrid[1] >= xydim[1])) { |
125 |
error(WARNING, "Excessive tiling for image size"); |
126 |
return false; |
127 |
} |
128 |
xydim[0] -= xydim[0] % (tgsize[0] = tgrid[0]); |
129 |
xydim[1] -= xydim[1] % (tgsize[1] = tgrid[1]); |
130 |
*ap = va * xydim[0] / xydim[1]; |
131 |
} else |
132 |
tgsize[0] = tgsize[1] = 1; |
133 |
|
134 |
if (vw.vaft > FTINY) rtFlags |= RTlimDist; |
135 |
else rtFlags &= ~RTlimDist; |
136 |
hvres[0] = xydim[0]; hvres[1] = xydim[1]; |
137 |
thvres[0] = hvres[0]/tgsize[0]; // presumed tile width |
138 |
thvres[1] = hvres[1]/tgsize[1]; // ...and height |
139 |
frameNo += (frameNo > 0); // caller may override after |
140 |
return true; |
141 |
} |
142 |
|
143 |
// Call-back for rendered pixel |
144 |
int |
145 |
RpictSimulManager::RtCall(RAY *r, void *cd) |
146 |
{ |
147 |
RpictSimulManager * rsp = (RpictSimulManager *)cd; |
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()"); |
153 |
return -1; |
154 |
} |
155 |
if (!rsp->doneMap.TestAndSet(tx, ty)) { |
156 |
error(WARNING, "duplicate pixel calculation"); |
157 |
return 0; |
158 |
} |
159 |
return rsp->pacc.SetPixel(tx, ty, r); |
160 |
} |
161 |
|
162 |
// Set up the specified tile (or entire image if NULL) |
163 |
bool |
164 |
RpictSimulManager::SetTile(const int ti[2]) |
165 |
{ |
166 |
tvw = vw; ptvw = pvw; |
167 |
|
168 |
if (ti) { |
169 |
if ((ti[0] < 0) | (ti[0] >= tgsize[0]) | |
170 |
(ti[1] < 0) | (ti[1] >= tgsize[1])) { |
171 |
error(INTERNAL, "illegal tile specification in SetTile()"); |
172 |
return false; |
173 |
} |
174 |
const char * verr = cropview(&tvw, |
175 |
(double)ti[0]/tgsize[0], |
176 |
(double)ti[1]/tgsize[1], |
177 |
(ti[0]+1.)/tgsize[0], |
178 |
(ti[1]+1.)/tgsize[1]); |
179 |
if (verr) { |
180 |
sprintf(errmsg, "crop failure @ tile (%d,%d)/(%d,%d): %s", |
181 |
ti[0], ti[1], tgsize[0], tgsize[1], verr); |
182 |
error(USER, errmsg); |
183 |
return false; |
184 |
} // previous tile view for blur |
185 |
if (!ptvw.type | (mblur <= FTINY) || |
186 |
cropview(&ptvw, (double)ti[0]/tgsize[0], |
187 |
(double)ti[1]/tgsize[1], |
188 |
(ti[0]+1.)/tgsize[0], |
189 |
(ti[1]+1.)/tgsize[1])) |
190 |
ptvw.type = 0; |
191 |
|
192 |
} else if ((tgsize[0] > 1) | (tgsize[1] > 1)) { |
193 |
error(INTERNAL, "missing tile specification in SetTile()"); |
194 |
return false; |
195 |
} |
196 |
return doneMap.NewBitMap(TWidth(), THeight()); |
197 |
} |
198 |
|
199 |
#define pixjitter() (.5+dstrpix*(.5-frandom())) |
200 |
|
201 |
// Send the indicated pixel to ray tracer |
202 |
bool |
203 |
RpictSimulManager::ComputePixel(int x, int y) |
204 |
{ |
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); |
214 |
doneMap.Set(x, y); |
215 |
return true; |
216 |
} |
217 |
if (ptvw.type) { // add motion blur if requested |
218 |
FVECT rorg2, rdir2; |
219 |
double dlim2 = viewray(rorg2, rdir2, &ptvw, hpos, vpos); |
220 |
if (dlim2 >= -FTINY) { |
221 |
const double d = mblur*(.5-frandom()); |
222 |
dlim = (1.-d)*dlim + d*dlim2; |
223 |
for (i = 3; i--; ) { |
224 |
rodir[0][i] = (1.-d)*rodir[0][i] + d*rorg2[i]; |
225 |
rodir[1][i] = (1.-d)*rodir[1][i] + d*rdir2[i]; |
226 |
} |
227 |
if (normalize(rodir[1]) == 0) |
228 |
return false; |
229 |
} |
230 |
} |
231 |
// depth-of-field blur if any |
232 |
if (!jitteraperture(rodir[0], rodir[1], &tvw, dblur)) |
233 |
return false; |
234 |
// include aft clipping distance? |
235 |
for (i = (dlim > FTINY)*3; i--; ) |
236 |
rodir[1][i] *= dlim; |
237 |
|
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(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 |
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 |
// 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 (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 |
} 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(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--; ) { |
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 |
} |
289 |
i = abs(noff[1][0]-noff[0][0]); |
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 = 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] + |
300 |
c1*c2*noff[3][0] + .5); |
301 |
const int py = int(y + (1.-c1)*(1.-c2)*noff[0][1] + |
302 |
c1*(1.-c2)*noff[1][1] + |
303 |
(1.-c1)*c2*noff[2][1] + |
304 |
c1*c2*noff[3][1] + .5); |
305 |
if (!doneMap.TestAndSet(px, py)) |
306 |
continue; |
307 |
float zval = 0; |
308 |
if (pacc.DepthType()) |
309 |
zval = (1.-c1)*(1.-c2)*dist[0] + c1*(1.-c2)*dist[1] + |
310 |
(1.-c1)*c2*dist[2] + c1*c2*dist[3]; |
311 |
if (spectr) { // XXX assumes pacc.NC() == NCSAMP |
312 |
SCOLOR ipval, tpval; |
313 |
copyscolor(ipval, pval[0]); |
314 |
scalescolor(ipval, (1.-c1)*(1.-c2)); |
315 |
copyscolor(tpval, pval[1]); |
316 |
scalescolor(tpval, c1*(1.-c2)); |
317 |
saddscolor(ipval, tpval); |
318 |
copyscolor(tpval, pval[2]); |
319 |
scalescolor(tpval, (1.-c1)*c2); |
320 |
saddscolor(ipval, tpval); |
321 |
copyscolor(tpval, pval[3]); |
322 |
scalescolor(tpval, c1*c2); |
323 |
saddscolor(ipval, tpval); |
324 |
pacc.SetPixel(px, py, ipval, zval); |
325 |
} else { // tristimulus interpolation |
326 |
COLOR ipval, tpval; |
327 |
copycolor(ipval, pval[0]); |
328 |
scalecolor(ipval, (1.-c1)*(1.-c2)); |
329 |
copycolor(tpval, pval[1]); |
330 |
scalecolor(tpval, c1*(1.-c2)); |
331 |
addcolor(ipval, tpval); |
332 |
copycolor(tpval, pval[2]); |
333 |
scalecolor(tpval, (1.-c1)*c2); |
334 |
addcolor(ipval, tpval); |
335 |
copycolor(tpval, pval[3]); |
336 |
scalecolor(tpval, c1*c2); |
337 |
addcolor(ipval, tpval); |
338 |
pacc.SetPixel(px, py, ipval, zval); |
339 |
} |
340 |
} |
341 |
} |
342 |
} |
343 |
|
344 |
// helper function to set up quincunx sampling |
345 |
static void |
346 |
SetQuincunx(ABitMap2 *bmp2, int noff[4][2], const int spc, bool odd, int x0, int y) |
347 |
{ |
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; |
352 |
noff[3][0] = 0; noff[3][1] = -(spc>>1); |
353 |
} else { |
354 |
noff[0][0] = spc>>1; noff[0][1] = spc>>1; |
355 |
noff[1][0] = -(spc>>1); noff[1][1] = spc>>1; |
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(const int x0, const int y0) |
380 |
{ |
381 |
if (!tvw.type || !Ready()) { |
382 |
error(INTERNAL, "need octree and view for RenderRect()"); |
383 |
return false; |
384 |
} |
385 |
ABitMap2 doneSamples = doneMap; |
386 |
int sp2 = ceil(log2((TWidth()>THeight() ? TWidth() : THeight()) - 1.)); |
387 |
int layer = 0; |
388 |
int x, y; |
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, 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 thresholds |
399 |
return false; |
400 |
ABitMap2 fillMap = sampMap; |
401 |
for (x = y = 0; sampMap.Find(&x, &y); x++) |
402 |
if (BelowSampThresh(x, y, noff)) |
403 |
sampMap.Reset(x, y); |
404 |
// spread sampling to neighbors... |
405 |
const ABitMap2 origSampMap = sampMap; |
406 |
for (x = 4; x--; ) { |
407 |
ABitMap2 stamp = origSampMap; |
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)) { |
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 |
427 |
sp2 -= layer++ & 1; // next denser sampling |
428 |
} |
429 |
if (FlushQueue() < 0) // compute stragglers |
430 |
return false; |
431 |
if ((prCB != NULL) & (barPix == NULL)) |
432 |
(*prCB)(100.); |
433 |
return true; |
434 |
} |
435 |
|
436 |
/* |
437 |
* Render the specified tile in frame |
438 |
* Tile pixels are contiguous unless ystride != 0 |
439 |
* Tiles numbered from upper-left at (0,0) |
440 |
* Pixel type influenced by this->prims assignment |
441 |
*/ |
442 |
bool |
443 |
RpictSimulManager::RenderTile(COLORV *rp, int ystride, float *zp, const int *tile) |
444 |
{ |
445 |
if (!rp | (GetWidth() <= 0) | (GetHeight() <= 0) | !vw.type) |
446 |
return false; |
447 |
if (!ystride) // contiguous rows? |
448 |
ystride = TWidth(); |
449 |
pacc.Init(rp, ystride, zp); |
450 |
if (prims == xyzprims) |
451 |
pacc.SetColorSpace(RDTxyz); |
452 |
else if (prims) |
453 |
pacc.SetColorSpace(RDTrgb, prims); |
454 |
|
455 |
return SetTile(tile) && RenderRect(); |
456 |
} |
457 |
|
458 |
// Same but store as common-exponent COLR or SCOLR |
459 |
bool |
460 |
RpictSimulManager::RenderTile(COLRV *bp, int ystride, float *zp, const int *tile) |
461 |
{ |
462 |
if (!bp | (GetWidth() <= 0) | (GetHeight() <= 0) | !vw.type) |
463 |
return false; |
464 |
if (!ystride) // contiguous rows? |
465 |
ystride = TWidth(); |
466 |
pacc.Init(bp, ystride, zp); |
467 |
if (prims == xyzprims) |
468 |
pacc.SetColorSpace(RDTxyze); |
469 |
else if (prims) |
470 |
pacc.SetColorSpace(RDTrgbe, prims); |
471 |
|
472 |
return SetTile(tile) && RenderRect(); |
473 |
} |
474 |
|
475 |
// Same but also use 16-bit encoded depth buffer |
476 |
bool |
477 |
RpictSimulManager::RenderTile(COLRV *bp, int ystride, short *dp, const int *tile) |
478 |
{ |
479 |
if (!bp | (GetWidth() <= 0) | (GetHeight() <= 0) | !vw.type) |
480 |
return false; |
481 |
if (!ystride) // contiguous rows? |
482 |
ystride = TWidth(); |
483 |
pacc.Init(bp, ystride, dp); |
484 |
if (prims == xyzprims) |
485 |
pacc.SetColorSpace(RDTxyze); |
486 |
else if (prims) |
487 |
pacc.SetColorSpace(RDTrgbe, prims); |
488 |
|
489 |
return SetTile(tile) && RenderRect(); |
490 |
} |
491 |
|
492 |
// Back to float color with 16-bit depth |
493 |
bool |
494 |
RpictSimulManager::RenderTile(COLORV *rp, int ystride, short *dp, const int *tile) |
495 |
{ |
496 |
if (!rp | (GetWidth() <= 0) | (GetHeight() <= 0) | !vw.type) |
497 |
return false; |
498 |
if (!ystride) // contiguous rows? |
499 |
ystride = TWidth(); |
500 |
pacc.Init(rp, ystride, dp); |
501 |
if (prims == xyzprims) |
502 |
pacc.SetColorSpace(RDTxyz); |
503 |
else if (prims) |
504 |
pacc.SetColorSpace(RDTrgb, prims); |
505 |
|
506 |
return SetTile(tile) && RenderRect(); |
507 |
} |
508 |
|
509 |
// Allocate a new render bar |
510 |
void |
511 |
RpictSimulManager::NewBar(int ht) |
512 |
{ |
513 |
delete [] barPix; |
514 |
delete [] barDepth; |
515 |
if (ht > GetHeight()) ht = GetHeight(); |
516 |
if ((ht <= 0) | (GetWidth() <= 0)) { |
517 |
doneMap.NewBitMap(0,0); |
518 |
pacc.Init(); |
519 |
barPix = NULL; barDepth = NULL; |
520 |
return; |
521 |
} |
522 |
thvres[0] = GetWidth(); |
523 |
thvres[1] = ht; |
524 |
const int NC = prims ? 3 : NCSAMP; |
525 |
barPix = new COLORV [ht*thvres[0]*NC]; |
526 |
barDepth = new float [ht*thvres[0]]; |
527 |
pacc.Init(barPix + (ht-1)*thvres[0]*NC, |
528 |
-thvres[0], barDepth + (ht-1)*thvres[0]); |
529 |
if (prims == xyzprims) |
530 |
pacc.SetColorSpace(RDTxyz); |
531 |
else if (prims) |
532 |
pacc.SetColorSpace(RDTrgb, prims); |
533 |
|
534 |
doneMap.NewBitMap(TWidth(), THeight()); |
535 |
} |
536 |
|
537 |
// Shift render bar area the specified amount down the frame |
538 |
bool |
539 |
RpictSimulManager::LowerBar(int v, int ytop) |
540 |
{ |
541 |
if (!barPix | !barDepth | (v > THeight()) | !tvw.type) |
542 |
return false; |
543 |
if (v <= 0) return !v; |
544 |
if ((ytop -= v) <= 0) |
545 |
return true; |
546 |
tvw.voff -= double(v)/THeight(); |
547 |
ptvw.voff -= double(v)/THeight(); |
548 |
if (v == THeight()) { |
549 |
doneMap.ClearBitMap(); |
550 |
return true; |
551 |
} |
552 |
const int NC = pacc.NC(); |
553 |
doneMap.Shift(0, v, false); // lift finished pixel samples |
554 |
memmove(barPix, barPix + NC*TWidth()*v, |
555 |
sizeof(COLORV)*NC*TWidth()*(THeight()-v)); |
556 |
memmove(barDepth, barDepth + TWidth()*v, |
557 |
sizeof(float)*TWidth()*(THeight()-v)); |
558 |
if (ytop < THeight()) // mark what we won't do as finished |
559 |
doneMap.ClearRect(0, 0, TWidth(), THeight()-ytop, true); |
560 |
return true; |
561 |
} |
562 |
|
563 |
// Continue rendering from the specified position |
564 |
bool |
565 |
RpictSimulManager::RenderBelow(int ytop, const int vstep, FILE *pfp, const int dt, FILE *dfp) |
566 |
{ |
567 |
if (ytop <= 0) |
568 |
return true; |
569 |
ptvw = pvw; // set starting bar's view |
570 |
tvw = vw; |
571 |
const char * verr = cropview(&tvw, 0., double(ytop-THeight())/GetHeight(), |
572 |
1., double(ytop)/GetHeight()); |
573 |
if (verr) { |
574 |
sprintf(errmsg, "illegal render bar below y=%d: %s", ytop, verr); |
575 |
error(INTERNAL, errmsg); |
576 |
return false; |
577 |
} |
578 |
if (!ptvw.type | (mblur <= FTINY) || |
579 |
cropview(&ptvw, 0., double(ytop-THeight())/GetHeight(), |
580 |
1., double(ytop)/GetHeight())) |
581 |
ptvw.type = 0; |
582 |
// update spectral sampling |
583 |
int rv = setspectrsamp(CNDX, WLPART); |
584 |
if (rv < 0) { |
585 |
error(USER, "unsupported spectral sampling"); |
586 |
return false; |
587 |
} |
588 |
if (!rv & (RDTcolorT(dt) != RDTscolor) & (RDTcolorT(dt) != RDTscolr)) |
589 |
error(WARNING, "spectral range incompatible with color output"); |
590 |
COLORV ** parr = NULL; // set up tiny source drawing |
591 |
float ** zarr = NULL; |
592 |
if (!ptvw.type && directvis && (dblur <= FTINY) & (mblur <= FTINY)) { |
593 |
parr = new COLORV * [THeight()]; |
594 |
zarr = new float * [THeight()]; |
595 |
for (int n = THeight(); n-- > 0; ) { |
596 |
parr[THeight()-1-n] = barPix + pacc.NC()*TWidth()*n; |
597 |
zarr[THeight()-1-n] = barDepth + TWidth()*n; |
598 |
} |
599 |
ourview = vw; hres = GetWidth(); vres = GetHeight(); |
600 |
init_drawsources(psample); |
601 |
} |
602 |
int lastOut = ytop; // render down frame |
603 |
while (ytop > 0) { |
604 |
if (prCB) |
605 |
(*prCB)(100.*(GetHeight()-ytop)/GetHeight()); |
606 |
if (!RenderRect(0, THeight()-ytop)) // render this bar |
607 |
return false; |
608 |
int nlines = lastOut - ytop + vstep; |
609 |
if (nlines > ytop) |
610 |
nlines = ytop; |
611 |
else if (parr) // drawing sources? |
612 |
drawsources(parr, prims, zarr, |
613 |
0, hres, lastOut-nlines, nlines); |
614 |
|
615 |
if (dfp) { // write out depth scanlines? |
616 |
const float * dp = barDepth + TWidth()*(ytop-lastOut); |
617 |
if (RDTdepthT(dt) == RDTdshort) { |
618 |
for (int n = TWidth()*nlines; n-- > 0; dp++) |
619 |
if (putint(depth2code(*dp, pacc.refDepth), 2, dfp) == EOF) |
620 |
error(SYSTEM, "cannot write 16-bit depth buffer"); |
621 |
} else if (putbinary(dp, sizeof(float), TWidth()*nlines, dfp) |
622 |
!= TWidth()*nlines) |
623 |
error(SYSTEM, "cannot write raw depth buffer"); |
624 |
} |
625 |
COLORV * bpos = barPix + pacc.NC()*TWidth()*(ytop-lastOut); |
626 |
while (nlines-- > 0) { // write pixel scanlines |
627 |
switch (RDTcolorT(dt)) { |
628 |
case RDTrgbe: |
629 |
case RDTxyze: |
630 |
if (fwritescan((COLOR *)bpos, TWidth(), pfp) < 0) |
631 |
error(SYSTEM, "cannot write RGBE/XYZE output"); |
632 |
break; |
633 |
case RDTscolr: |
634 |
if (fwritesscan(bpos, pacc.NC(), TWidth(), pfp) < 0) |
635 |
error(SYSTEM, "cannot write SCOLOR output"); |
636 |
break; |
637 |
case RDTrgb: |
638 |
case RDTxyz: |
639 |
case RDTscolor: |
640 |
if (putbinary(bpos, sizeof(COLORV)*pacc.NC(), TWidth(), pfp) |
641 |
!= TWidth()) |
642 |
error(SYSTEM, "cannot write SCOLOR output"); |
643 |
break; |
644 |
default: |
645 |
error(INTERNAL, "botched output color type in RenderBelow()"); |
646 |
break; |
647 |
} |
648 |
bpos += pacc.NC()*TWidth(); |
649 |
--lastOut; |
650 |
} // flush each scan bar |
651 |
if (fflush(pfp) == EOF || (dfp && fflush(dfp) == EOF)) |
652 |
error(SYSTEM, "output write error"); |
653 |
// advance down the frame |
654 |
if (lastOut > 0 && !LowerBar(vstep, ytop)) |
655 |
return false; |
656 |
ytop -= vstep; |
657 |
} |
658 |
delete [] parr; |
659 |
delete [] zarr; |
660 |
if (prCB) |
661 |
(*prCB)(100.); |
662 |
return true; |
663 |
} |
664 |
|
665 |
// Open new output picture file (and optional depth file) |
666 |
RenderDataType |
667 |
RpictSimulManager::NewOutput(FILE *pdfp[2], const char *pfname, |
668 |
RenderDataType dt, const char *dfname) |
669 |
{ |
670 |
pdfp[0] = pdfp[1] = NULL; |
671 |
if (!RDTcolorT(dt)) |
672 |
error(INTERNAL, "missing color output type in NewOutput()"); |
673 |
if (NCSAMP == 3) { |
674 |
if (RDTcolorT(dt) == RDTscolr) |
675 |
dt = RDTnewCT(dt, prims==xyzprims ? RDTxyze : RDTrgbe); |
676 |
else if (RDTcolorT(dt) == RDTscolor) |
677 |
dt = RDTnewCT(dt, prims==xyzprims ? RDTxyz : RDTrgb); |
678 |
} |
679 |
if (!RDTdepthT(dt) ^ !dfname) |
680 |
error(INTERNAL, "depth output requires file name and type in NewOutput()"); |
681 |
int fd = 1; |
682 |
if (pfname) { // open picture output file |
683 |
if (pfname[0] == '!') { |
684 |
error(INTERNAL, "writing picture to a command not supported"); |
685 |
return RDTnone; |
686 |
} |
687 |
fd = open(pfname, O_RDWR|O_CREAT|O_EXCL, 0666); |
688 |
} |
689 |
if (fd < 0) { |
690 |
if ((frameNo <= 0) | (errno != EEXIST)) { |
691 |
sprintf(errmsg, "cannot open picture file '%s'", pfname); |
692 |
error(SYSTEM, errmsg); |
693 |
} |
694 |
return RDTnone; // may be expected in sequence run |
695 |
} |
696 |
if (fd == 1) |
697 |
pdfp[0] = stdout; |
698 |
else if (!(pdfp[0] = fdopen(fd, "w+"))) |
699 |
error(SYSTEM, "failure calling fdopen()"); |
700 |
SET_FILE_BINARY(pdfp[0]); // write picture header |
701 |
if ((pdfp[0] != stdout) | (frameNo <= 1)) { |
702 |
newheader("RADIANCE", pdfp[0]); |
703 |
fputs(GetHeadStr(), pdfp[0]); |
704 |
} |
705 |
fputs(VIEWSTR, pdfp[0]); fprintview(&vw, pdfp[0]); fputc('\n', pdfp[0]); |
706 |
if (frameNo > 0) |
707 |
fprintf(pdfp[0], "FRAME=%d\n", frameNo); |
708 |
double pasp = viewaspect(&vw) * GetWidth() / GetHeight(); |
709 |
if ((0.99 > pasp) | (pasp > 1.01)) |
710 |
fputaspect(pasp, pdfp[0]); |
711 |
fputnow(pdfp[0]); |
712 |
switch (RDTcolorT(dt)) { // set primaries and picture format |
713 |
case RDTrgbe: |
714 |
if (!prims | (prims == xyzprims)) prims = stdprims; |
715 |
fputprims(prims, pdfp[0]); |
716 |
fputformat(COLRFMT, pdfp[0]); |
717 |
break; |
718 |
case RDTxyze: |
719 |
prims = xyzprims; |
720 |
fputformat(CIEFMT, pdfp[0]); |
721 |
break; |
722 |
case RDTscolr: |
723 |
prims = NULL; |
724 |
fputwlsplit(WLPART, pdfp[0]); |
725 |
fputncomp(NCSAMP, pdfp[0]); |
726 |
fputformat(SPECFMT, pdfp[0]); |
727 |
break; |
728 |
case RDTrgb: |
729 |
if (!prims | (prims == xyzprims)) prims = stdprims; |
730 |
fputprims(prims, pdfp[0]); |
731 |
fputncomp(3, pdfp[0]); |
732 |
fputendian(pdfp[0]); |
733 |
fputformat("float", pdfp[0]); |
734 |
break; |
735 |
case RDTxyz: |
736 |
prims = xyzprims; |
737 |
fputprims(prims, pdfp[0]); |
738 |
fputncomp(3, pdfp[0]); |
739 |
fputendian(pdfp[0]); |
740 |
fputformat("float", pdfp[0]); |
741 |
break; |
742 |
case RDTscolor: |
743 |
prims = NULL; |
744 |
fputwlsplit(WLPART, pdfp[0]); |
745 |
fputncomp(NCSAMP, pdfp[0]); |
746 |
fputendian(pdfp[0]); |
747 |
fputformat("float", pdfp[0]); |
748 |
break; |
749 |
default:; // pro forma - caught this above |
750 |
} |
751 |
fputc('\n', pdfp[0]); // flush picture header |
752 |
if (fflush(pdfp[0]) == EOF) { |
753 |
sprintf(errmsg, "cannot write header to picture '%s'", pfname); |
754 |
error(SYSTEM, errmsg); |
755 |
fclose(pdfp[0]); |
756 |
pdfp[0] = NULL; |
757 |
return RDTnone; |
758 |
} |
759 |
if (dfname) { // open depth output |
760 |
if (dfname[0] == '!') |
761 |
pdfp[1] = popen(dfname+1, "w"); |
762 |
else |
763 |
pdfp[1] = fopen(dfname, "w+"); |
764 |
if (!pdfp[1]) { |
765 |
sprintf(errmsg, "cannot open depth output '%s'", dfname); |
766 |
error(SYSTEM, errmsg); |
767 |
fclose(pdfp[0]); |
768 |
pdfp[0] = NULL; |
769 |
return RDTnone; |
770 |
} |
771 |
SET_FILE_BINARY(pdfp[1]); |
772 |
} |
773 |
if (RDTdepthT(dt) == RDTdshort) { // write header for 16-bit depth? |
774 |
newheader("RADIANCE", pdfp[1]); |
775 |
fputs(GetHeadStr(), pdfp[1]); |
776 |
fputs(VIEWSTR, pdfp[1]); fprintview(&vw, pdfp[1]); fputc('\n', pdfp[1]); |
777 |
fputs(DEPTHSTR, pdfp[1]); fputs(dunit, pdfp[1]); fputc('\n', pdfp[1]); |
778 |
fputformat(DEPTH16FMT, pdfp[1]); |
779 |
fputc('\n', pdfp[1]); // end-of-info |
780 |
if (fflush(pdfp[1]) == EOF) { |
781 |
sprintf(errmsg, "cannot write header to '%s'", dfname); |
782 |
error(SYSTEM, errmsg); |
783 |
fclose(pdfp[0]); fclose(pdfp[1]); |
784 |
pdfp[0] = pdfp[1] = NULL; |
785 |
return RDTnone; |
786 |
} |
787 |
} |
788 |
return dt; // ready to roll |
789 |
} |
790 |
|
791 |
/* |
792 |
* Render and write a frame to the named file |
793 |
* Include any header lines set prior to call |
794 |
* Picture file must not exist |
795 |
* Write pixels to stdout if !pfname |
796 |
* Write depth to a command if dfname[0]=='!' |
797 |
*/ |
798 |
RenderDataType |
799 |
RpictSimulManager::RenderFrame(const char *pfname, RenderDataType dt, const char *dfname) |
800 |
{ |
801 |
FILE *pdfp[2]; |
802 |
// prepare output file(s) |
803 |
dt = NewOutput(pdfp, pfname, dt, dfname); |
804 |
if (dt == RDTnone) |
805 |
return RDTnone; |
806 |
// add resolution string(s) |
807 |
fprtresolu(GetWidth(), GetHeight(), pdfp[0]); |
808 |
if (RDTdepthT(dt) == RDTdshort) |
809 |
fprtresolu(GetWidth(), GetHeight(), pdfp[1]); |
810 |
|
811 |
const int bheight = (psample > 1) ? int(8*psample+.99) : 16; |
812 |
const int vstep = bheight >> (psample > 1); |
813 |
|
814 |
NewBar(bheight); // render frame if we can |
815 |
if (!RenderBelow(GetHeight(), vstep, pdfp[0], dt, pdfp[1])) { |
816 |
fclose(pdfp[0]); |
817 |
if (pdfp[1]) (dfname[0] == '!') ? pclose(pdfp[1]) : fclose(pdfp[1]); |
818 |
return RDTnone; |
819 |
} |
820 |
NewBar(); // clean up and return |
821 |
if (pdfp[0] != stdout) |
822 |
fclose(pdfp[0]); |
823 |
if (pdfp[1]) { |
824 |
if (dfname[0] == '!') { |
825 |
int status = pclose(pdfp[1]); |
826 |
if (status) { |
827 |
sprintf(errmsg, "depth output (%s) error status: %d", |
828 |
dfname, status); |
829 |
error(USER, errmsg); |
830 |
return RDTnone; |
831 |
} |
832 |
} else |
833 |
fclose(pdfp[1]); |
834 |
} |
835 |
return dt; |
836 |
} |
837 |
|
838 |
// passed struct for header line callback |
839 |
static struct HeaderInfo { |
840 |
char fmt[MAXFMTLEN]; |
841 |
char depth_unit[32]; |
842 |
int ncomp; |
843 |
RGBPRIMS prims; |
844 |
VIEW vw; |
845 |
bool gotWL; |
846 |
bool gotprims; |
847 |
bool gotview; |
848 |
bool endianMatch; |
849 |
|
850 |
HeaderInfo() { |
851 |
strcpy(fmt, "MISSING"); |
852 |
depth_unit[0] = '\0'; |
853 |
ncomp = 3; |
854 |
vw = stdview; |
855 |
gotWL = false; |
856 |
gotprims = false; |
857 |
gotview = false; |
858 |
endianMatch = true; |
859 |
} |
860 |
} hinfo; // XXX single copy to hold custom primitives |
861 |
|
862 |
// helper function checks header line and records req. info. |
863 |
static int |
864 |
head_check(char *s, void *p) |
865 |
{ |
866 |
HeaderInfo * hp = (HeaderInfo *)p; |
867 |
int rval; |
868 |
|
869 |
if (isncomp(s)) { |
870 |
hp->ncomp = ncompval(s); |
871 |
return 1; |
872 |
} |
873 |
if (iswlsplit(s)) { |
874 |
hp->gotWL = wlsplitval(WLPART, s); |
875 |
return 1; |
876 |
} |
877 |
if (isprims(s)) { |
878 |
hp->gotprims = primsval(hp->prims, s); |
879 |
return 1; |
880 |
} |
881 |
if (isview(s)) { |
882 |
hp->gotview |= (sscanview(&hp->vw, s) > 0); |
883 |
return 1; |
884 |
} |
885 |
if (!strncmp(s, DEPTHSTR, LDEPTHSTR)) { |
886 |
strlcpy(hp->depth_unit, s+LDEPTHSTR, sizeof(hp->depth_unit)); |
887 |
char * cp = hp->depth_unit; |
888 |
while (*cp) cp++; |
889 |
while (cp > hp->depth_unit && isspace(cp[-1])) cp--; |
890 |
*cp = '\0'; |
891 |
return 1; |
892 |
} |
893 |
if ((rval = isbigendian(s)) >= 0) { |
894 |
hp->endianMatch = (rval == nativebigendian()); |
895 |
return 1; |
896 |
} |
897 |
if (formatval(hp->fmt, s)) |
898 |
return 1; |
899 |
return 0; |
900 |
} |
901 |
|
902 |
// Reopen output file(s), leaving pointers at end of (each) header |
903 |
RenderDataType |
904 |
RpictSimulManager::ReopenOutput(FILE *pdfp[2], const char *pfname, const char *dfname) |
905 |
{ |
906 |
extern const char HDRSTR[]; |
907 |
|
908 |
if (!pfname || pfname[0] == '!') { |
909 |
pdfp[0] = pdfp[1] = NULL; |
910 |
return RDTnone; |
911 |
} |
912 |
RenderDataType dt = RDTnone; |
913 |
pdfp[1] = NULL; |
914 |
pdfp[0] = fopen(pfname, "r+"); |
915 |
if (!pdfp[0]) { |
916 |
sprintf(errmsg, "cannot reopen output picture '%s'", pfname); |
917 |
error(SYSTEM, errmsg); |
918 |
return RDTnone; |
919 |
} |
920 |
SET_FILE_BINARY(pdfp[0]); // read header information |
921 |
if (getheader(pdfp[0], head_check, &hinfo) < 0) { |
922 |
fclose(pdfp[0]); |
923 |
pdfp[0] = NULL; |
924 |
return RDTnone; |
925 |
} |
926 |
if (!hinfo.gotview) { |
927 |
sprintf(errmsg, "missing view for '%s'", pfname); |
928 |
error(USER, errmsg); |
929 |
fclose(pdfp[0]); |
930 |
pdfp[0] = NULL; |
931 |
return RDTnone; |
932 |
} |
933 |
if (hinfo.ncomp < 3) { |
934 |
sprintf(errmsg, "bad # components (%d) in '%s'", hinfo.ncomp, pfname); |
935 |
error(USER, errmsg); |
936 |
fclose(pdfp[0]); |
937 |
pdfp[0] = NULL; |
938 |
return RDTnone; |
939 |
} |
940 |
// set rendering/output space |
941 |
if (!strcmp(hinfo.fmt, COLRFMT)) { |
942 |
prims = hinfo.prims; // XXX static array |
943 |
int n = 8*hinfo.gotprims; |
944 |
while (n--) |
945 |
if (!FABSEQ(hinfo.prims[0][n], stdprims[0][n])) |
946 |
break; |
947 |
if (n < 0) |
948 |
prims = stdprims; |
949 |
dt = RDTnewCT(dt, RDTrgbe); |
950 |
} else if (!strcmp(hinfo.fmt, CIEFMT)) { |
951 |
prims = xyzprims; |
952 |
dt = RDTnewCT(dt, RDTxyze); |
953 |
} else if (!strcmp(hinfo.fmt, SPECFMT)) { |
954 |
if ((hinfo.ncomp <= 3) | (hinfo.ncomp > MAXCSAMP)) { |
955 |
sprintf(errmsg, "incompatible sample count (%d) in '%s'", |
956 |
hinfo.ncomp, pfname); |
957 |
error(USER, errmsg); |
958 |
fclose(pdfp[0]); |
959 |
pdfp[0] = NULL; |
960 |
return RDTnone; |
961 |
} |
962 |
NCSAMP = hinfo.ncomp; // overrides global setting |
963 |
prims = NULL; |
964 |
dt = RDTnewCT(dt, RDTscolr); |
965 |
} else if (!strcmp(hinfo.fmt, "float")) { |
966 |
if (!hinfo.endianMatch) { |
967 |
sprintf(errmsg, "incompatible byte ordering in '%s'", pfname); |
968 |
error(USER, errmsg); |
969 |
fclose(pdfp[0]); |
970 |
pdfp[0] = NULL; |
971 |
return RDTnone; |
972 |
} |
973 |
if (hinfo.ncomp == 3) { |
974 |
prims = hinfo.prims; // custom primaries? |
975 |
int n = 8*hinfo.gotprims; |
976 |
while (n--) |
977 |
if (!FABSEQ(hinfo.prims[0][n], stdprims[0][n])) |
978 |
break; |
979 |
if (n < 0) // standard primaries? |
980 |
prims = stdprims; |
981 |
else if (hinfo.gotprims) { // or check if XYZ |
982 |
for (n = 8; n--; ) |
983 |
if (!FABSEQ(prims[0][n], xyzprims[0][n])) |
984 |
break; |
985 |
if (n < 0) |
986 |
prims = xyzprims; |
987 |
} |
988 |
if (prims == xyzprims) |
989 |
dt = RDTnewCT(dt, RDTxyz); |
990 |
else |
991 |
dt = RDTnewCT(dt, RDTrgb); |
992 |
} else { |
993 |
NCSAMP = hinfo.ncomp; // overrides global setting |
994 |
prims = NULL; |
995 |
dt = RDTnewCT(dt, RDTscolor); |
996 |
} |
997 |
} else { |
998 |
sprintf(errmsg, "unknown format (%s) for '%s'", hinfo.fmt, pfname); |
999 |
error(USER, errmsg); |
1000 |
fclose(pdfp[0]); |
1001 |
pdfp[0] = NULL; |
1002 |
return RDTnone; |
1003 |
} |
1004 |
if (hinfo.gotview) { // header view overrides |
1005 |
pvw = vw; |
1006 |
vw = hinfo.vw; |
1007 |
} |
1008 |
if (!dfname) // no depth file? |
1009 |
return dt; |
1010 |
|
1011 |
if (dfname[0] == '!') { |
1012 |
error(USER, "depth data cannot be reloaded from command"); |
1013 |
fclose(pdfp[0]); |
1014 |
pdfp[0] = NULL; |
1015 |
return RDTnone; |
1016 |
} |
1017 |
pdfp[1] = fopen(dfname, "r+"); |
1018 |
if (!pdfp[1]) { |
1019 |
sprintf(errmsg, "cannot reopen depth file '%s'", dfname); |
1020 |
error(SYSTEM, errmsg); |
1021 |
fclose(pdfp[0]); |
1022 |
pdfp[0] = NULL; |
1023 |
return RDTnone; |
1024 |
} |
1025 |
SET_FILE_BINARY(pdfp[1]); |
1026 |
int n, len = strlen(HDRSTR); |
1027 |
char buf[32]; // sniff for 16-bit header |
1028 |
if (getbinary(buf, 1, len+1, pdfp[1]) < len+1) { |
1029 |
sprintf(errmsg, "empty depth file '%s'", dfname); |
1030 |
error(SYSTEM, errmsg); |
1031 |
fclose(pdfp[0]); fclose(pdfp[1]); |
1032 |
pdfp[0] = pdfp[1] = NULL; |
1033 |
return RDTnone; |
1034 |
} |
1035 |
for (n = 0; n < len; n++) |
1036 |
if (buf[n] != HDRSTR[n]) |
1037 |
break; // not a Radiance header |
1038 |
rewind(pdfp[1]); |
1039 |
if ((n < len) | !isprint(buf[len])) |
1040 |
return RDTnewDT(dt, RDTdfloat); |
1041 |
|
1042 |
HeaderInfo dinfo; // thinking it's 16-bit encoded |
1043 |
if (getheader(pdfp[1], head_check, &dinfo) < 0) |
1044 |
sprintf(errmsg, "bad header in encoded depth file '%s'", |
1045 |
dfname); |
1046 |
else if (strcmp(dinfo.fmt, DEPTH16FMT)) |
1047 |
sprintf(errmsg, "wrong format (%s) for depth file '%s'", |
1048 |
dinfo.fmt, dfname); |
1049 |
else if (!SetReferenceDepth(dinfo.depth_unit)) |
1050 |
sprintf(errmsg, "bad/missing reference depth (%s) in '%s'", |
1051 |
dinfo.depth_unit, dfname); |
1052 |
else |
1053 |
errmsg[0] = '\0'; |
1054 |
|
1055 |
if (errmsg[0]) { |
1056 |
error(USER, errmsg); |
1057 |
fclose(pdfp[1]); fclose(pdfp[0]); |
1058 |
pdfp[0] = pdfp[1] = NULL; |
1059 |
return RDTnone; |
1060 |
} |
1061 |
return RDTnewDT(dt, RDTdshort); |
1062 |
} |
1063 |
|
1064 |
// Resume partially finished rendering |
1065 |
// Picture file must exist |
1066 |
RenderDataType |
1067 |
RpictSimulManager::ResumeFrame(const char *pfname, const char *dfname) |
1068 |
{ |
1069 |
FILE *pdfp[2]; |
1070 |
|
1071 |
RenderDataType dt = ReopenOutput(pdfp, pfname, dfname); |
1072 |
if (dt == RDTnone) |
1073 |
return RDTnone; |
1074 |
|
1075 |
int bytesPer = 0; // figure out how far we got... |
1076 |
switch (RDTcolorT(dt)) { |
1077 |
case RDTrgbe: |
1078 |
case RDTxyze: |
1079 |
break; |
1080 |
case RDTscolr: |
1081 |
bytesPer = NCSAMP + 1; // XXX assumes no compression |
1082 |
break; |
1083 |
case RDTrgb: |
1084 |
case RDTxyz: |
1085 |
bytesPer = sizeof(float)*3; |
1086 |
break; |
1087 |
case RDTscolor: |
1088 |
bytesPer = sizeof(float)*NCSAMP; |
1089 |
break; |
1090 |
default: |
1091 |
sprintf(errmsg, "unknown format for '%s'", pfname); |
1092 |
error(USER, errmsg); |
1093 |
fclose(pdfp[0]); |
1094 |
if (pdfp[1]) fclose(pdfp[1]); |
1095 |
return RDTnone; |
1096 |
} |
1097 |
RESOLU res; |
1098 |
if (!fgetsresolu(&res, pdfp[0]) || res.rt != PIXSTANDARD) { |
1099 |
sprintf(errmsg, "missing/bad resolution for '%s'", pfname); |
1100 |
error(USER, errmsg); |
1101 |
fclose(pdfp[0]); |
1102 |
if (pdfp[1]) fclose(pdfp[1]); |
1103 |
return RDTnone; |
1104 |
} |
1105 |
frameNo = 0; // set up unreferenced frame |
1106 |
int hvdim[2] = {res.xr, res.yr}; |
1107 |
double noAdj = 0; |
1108 |
if (!NewFrame(vw, hvdim, &noAdj) || |
1109 |
(hvdim[0] != res.xr) | (hvdim[1] != res.yr)) { |
1110 |
error(CONSISTENCY, "unexpected resolution change in ResumeFrame()"); |
1111 |
fclose(pdfp[0]); |
1112 |
if (pdfp[1]) fclose(pdfp[1]); |
1113 |
return RDTnone; |
1114 |
} |
1115 |
long dataStart = ftell(pdfp[0]); // picture starting point |
1116 |
if (dataStart < 0) { |
1117 |
sprintf(errmsg, "cannot seek on '%s'", pfname); |
1118 |
error(SYSTEM, errmsg); |
1119 |
fclose(pdfp[0]); |
1120 |
if (pdfp[1]) fclose(pdfp[1]); |
1121 |
return RDTnone; |
1122 |
} |
1123 |
long doneScans = 0; |
1124 |
if (bytesPer) { // fixed-width records? |
1125 |
fseek(pdfp[0], 0, SEEK_END); |
1126 |
long dataEnd = ftell(pdfp[0]); |
1127 |
doneScans = (dataEnd - dataStart)/(bytesPer*GetWidth()); |
1128 |
if (dataEnd-dataStart > bytesPer*GetWidth()*doneScans) |
1129 |
fseek(pdfp[0], dataStart + bytesPer*GetWidth()*doneScans, SEEK_SET); |
1130 |
} else { // else get compressed scanlines |
1131 |
COLR * scan = (COLR *)tempbuffer(sizeof(COLR)*GetWidth()); |
1132 |
while (freadcolrs(scan, GetWidth(), pdfp[0]) >= 0) |
1133 |
++doneScans; |
1134 |
if (!feof(pdfp[0])) { |
1135 |
sprintf(errmsg, "error reading compressed scanline from '%s'", pfname); |
1136 |
error(USER, errmsg); |
1137 |
fclose(pdfp[0]); |
1138 |
if (pdfp[1]) fclose(pdfp[1]); |
1139 |
return RDTnone; |
1140 |
} |
1141 |
} |
1142 |
if (doneScans >= GetHeight()) { // nothing left to do? |
1143 |
sprintf(errmsg, "output file '%s' is already complete", pfname); |
1144 |
error(WARNING, errmsg); |
1145 |
fclose(pdfp[0]); |
1146 |
if (pdfp[1]) fclose(pdfp[1]); |
1147 |
return dt; |
1148 |
} |
1149 |
if (!doneScans) { |
1150 |
sprintf(errmsg, "restarting empty frame '%s'", pfname); |
1151 |
error(WARNING, errmsg); |
1152 |
} |
1153 |
long toSkip = 0; |
1154 |
switch (RDTdepthT(dt)) { // append depth file, too? |
1155 |
case RDTdfloat: |
1156 |
toSkip = sizeof(float)*GetWidth()*doneScans; |
1157 |
break; |
1158 |
case RDTdshort: |
1159 |
if (!fgetsresolu(&res, pdfp[1]) || (res.rt != PIXSTANDARD) | |
1160 |
(res.xr != GetWidth()) | (res.yr != GetHeight())) { |
1161 |
sprintf(errmsg, "missing/bad resolution for '%s'", dfname); |
1162 |
error(USER, errmsg); |
1163 |
fclose(pdfp[0]); fclose(pdfp[0]); |
1164 |
return RDTnone; |
1165 |
} |
1166 |
toSkip = 2L*GetWidth()*doneScans; |
1167 |
break; |
1168 |
default:; |
1169 |
} // fseek() needed for output |
1170 |
if (pdfp[1] && fseek(pdfp[1], toSkip, SEEK_CUR) < 0) { |
1171 |
sprintf(errmsg, "cannot seek on depth file '%s'", dfname); |
1172 |
error(SYSTEM, errmsg); |
1173 |
fclose(pdfp[0]); fclose(pdfp[1]); |
1174 |
return RDTnone; |
1175 |
} |
1176 |
int bheight = (psample > 1) ? int(8*psample+.99) : 16; |
1177 |
if (bheight > GetHeight()-doneScans) |
1178 |
bheight = GetHeight()-doneScans; |
1179 |
int vstep = bheight >> (psample > 1); |
1180 |
vstep += !vstep; |
1181 |
|
1182 |
NewBar(bheight); // render remainder if we can |
1183 |
if (!RenderBelow(GetHeight()-doneScans, vstep, pdfp[0], dt, pdfp[1])) { |
1184 |
fclose(pdfp[0]); |
1185 |
if (pdfp[1]) fclose(pdfp[1]); |
1186 |
return RDTnone; |
1187 |
} |
1188 |
NewBar(); // close up and return success |
1189 |
fclose(pdfp[0]); |
1190 |
if (pdfp[1]) fclose(pdfp[1]); |
1191 |
return dt; |
1192 |
} |