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" |
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); |
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(); |
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 |
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]); // i==j for diamond fill |
291 |
|
const int slen = (i > j) ? i : j; |
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] < 4*psample*psample) { |
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; |
405 |
|
const ABitMap2 origSampMap = sampMap; |
406 |
|
for (x = 4; x--; ) { |
407 |
|
ABitMap2 stamp = origSampMap; |
408 |
< |
stamp.Shift(noff[x][0], noff[x][1]); |
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; |
427 |
|
return false; |
428 |
|
x = y = 0; |
429 |
|
if (doneMap.Find(&x, &y, false)) { |
430 |
< |
sprintf(errmsg, "missed %ld tile pixels, e.g. (%d,%d)", |
431 |
< |
(long)doneMap.Width()*doneMap.Height() - |
432 |
< |
doneMap.SumTotal(), x, y); |
430 |
> |
sprintf(errmsg, "missed %.4f%% of pixels in rectangle\n", |
431 |
> |
100. - 100.*doneMap.SumTotal() / |
432 |
> |
doneMap.Width() / doneMap.Height()); |
433 |
|
error(WARNING, errmsg); |
434 |
|
} |
435 |
|
if ((prCB != NULL) & (barPix == NULL)) |
456 |
|
else if (prims) |
457 |
|
pacc.SetColorSpace(RDTrgb, prims); |
458 |
|
|
459 |
< |
int x0=0, y0=0; |
442 |
< |
if (tile) { |
443 |
< |
x0 = -tile[0]*TWidth(); |
444 |
< |
y0 = -tile[1]*THeight(); |
445 |
< |
} |
446 |
< |
return SetTile(tile) && RenderRect(x0, y0); |
459 |
> |
return SetTile(tile) && RenderRect(); |
460 |
|
} |
461 |
|
|
462 |
|
// Same but store as common-exponent COLR or SCOLR |
473 |
|
else if (prims) |
474 |
|
pacc.SetColorSpace(RDTrgbe, prims); |
475 |
|
|
476 |
< |
int x0=0, y0=0; |
464 |
< |
if (tile) { |
465 |
< |
x0 = -tile[0]*TWidth(); |
466 |
< |
y0 = -tile[1]*THeight(); |
467 |
< |
} |
468 |
< |
return SetTile(tile) && RenderRect(x0, y0); |
476 |
> |
return SetTile(tile) && RenderRect(); |
477 |
|
} |
478 |
|
|
479 |
|
// Same but also use 16-bit encoded depth buffer |
490 |
|
else if (prims) |
491 |
|
pacc.SetColorSpace(RDTrgbe, prims); |
492 |
|
|
493 |
< |
int x0=0, y0=0; |
486 |
< |
if (tile) { |
487 |
< |
x0 = -tile[0]*TWidth(); |
488 |
< |
y0 = -tile[1]*THeight(); |
489 |
< |
} |
490 |
< |
return SetTile(tile) && RenderRect(x0, y0); |
493 |
> |
return SetTile(tile) && RenderRect(); |
494 |
|
} |
495 |
|
|
496 |
|
// Back to float color with 16-bit depth |
507 |
|
else if (prims) |
508 |
|
pacc.SetColorSpace(RDTrgb, prims); |
509 |
|
|
510 |
< |
int x0=0, y0=0; |
508 |
< |
if (tile) { |
509 |
< |
x0 = -tile[0]*TWidth(); |
510 |
< |
y0 = -tile[1]*THeight(); |
511 |
< |
} |
512 |
< |
return SetTile(tile) && RenderRect(x0, y0); |
510 |
> |
return SetTile(tile) && RenderRect(); |
511 |
|
} |
512 |
|
|
513 |
|
// Allocate a new render bar |
559 |
|
sizeof(COLORV)*NC*TWidth()*(THeight()-v)); |
560 |
|
memmove(barDepth, barDepth + TWidth()*v, |
561 |
|
sizeof(float)*TWidth()*(THeight()-v)); |
562 |
< |
if (ytop < THeight()) { // mark what we won't do as finished |
562 |
> |
if (ytop < THeight()) // mark what we won't do as finished |
563 |
|
doneMap.ClearRect(0, 0, TWidth(), THeight()-ytop, true); |
566 |
– |
memset(barPix, 0, sizeof(COLORV)*NC*TWidth()*(THeight()-ytop)); |
567 |
– |
memset(barDepth, 0, sizeof(float)*TWidth()*(THeight()-ytop)); |
568 |
– |
} |
564 |
|
return true; |
565 |
|
} |
566 |
|
|
583 |
|
cropview(&ptvw, 0., double(ytop-THeight())/GetHeight(), |
584 |
|
1., double(ytop)/GetHeight())) |
585 |
|
ptvw.type = 0; |
586 |
< |
// set up spectral sampling |
587 |
< |
if (setspectrsamp(CNDX, WLPART) <= 0) { |
586 |
> |
// update spectral sampling |
587 |
> |
int rv = setspectrsamp(CNDX, WLPART); |
588 |
> |
if (rv < 0) { |
589 |
|
error(USER, "unsupported spectral sampling"); |
590 |
|
return false; |
591 |
|
} |
592 |
+ |
if (!rv & (RDTcolorT(dt) != RDTscolor) & (RDTcolorT(dt) != RDTscolr)) |
593 |
+ |
error(WARNING, "spectral range incompatible with color output"); |
594 |
|
COLORV ** parr = NULL; // set up tiny source drawing |
595 |
|
float ** zarr = NULL; |
596 |
|
if (!ptvw.type && directvis && dblur <= FTINY) { |
673 |
|
{ |
674 |
|
pdfp[0] = pdfp[1] = NULL; |
675 |
|
if (!RDTcolorT(dt)) |
676 |
< |
error(INTERNAL, "botched color output type in NewOutput()"); |
676 |
> |
error(INTERNAL, "missing color output type in NewOutput()"); |
677 |
|
if (NCSAMP == 3) { |
678 |
|
if (RDTcolorT(dt) == RDTscolr) |
679 |
|
dt = RDTnewCT(dt, prims==xyzprims ? RDTxyze : RDTrgbe); |
688 |
|
error(INTERNAL, "writing picture to a command not supported"); |
689 |
|
return RDTnone; |
690 |
|
} |
691 |
< |
fd = open(pfname, O_WRONLY|O_CREAT|O_EXCL, 0666); |
691 |
> |
fd = open(pfname, O_RDWR|O_CREAT|O_EXCL, 0666); |
692 |
|
} |
693 |
|
if (fd < 0) { |
694 |
|
if ((frameNo <= 0) | (errno != EEXIST)) { |
695 |
|
sprintf(errmsg, "cannot open picture file '%s'", pfname); |
696 |
|
error(SYSTEM, errmsg); |
697 |
|
} |
698 |
< |
return RDTnone; // expected in parallel sequence |
698 |
> |
return RDTnone; // may be expected in sequence run |
699 |
|
} |
700 |
|
if (fd == 1) |
701 |
|
pdfp[0] = stdout; |
702 |
< |
else if (!(pdfp[0] = fdopen(fd, "w"))) |
702 |
> |
else if (!(pdfp[0] = fdopen(fd, "w+"))) |
703 |
|
error(SYSTEM, "failure calling fdopen()"); |
704 |
|
SET_FILE_BINARY(pdfp[0]); // write picture header |
705 |
|
if ((pdfp[0] != stdout) | (frameNo <= 1)) { |
706 |
|
newheader("RADIANCE", pdfp[0]); |
707 |
< |
fputs(GetHeader(), pdfp[0]); |
707 |
> |
fputs(GetHeadStr(), pdfp[0]); |
708 |
|
} |
709 |
|
fputs(VIEWSTR, pdfp[0]); fprintview(&vw, pdfp[0]); fputc('\n', pdfp[0]); |
710 |
|
if (frameNo > 0) |
750 |
|
fputendian(pdfp[0]); |
751 |
|
fputformat("float", pdfp[0]); |
752 |
|
break; |
753 |
< |
default:; |
753 |
> |
default:; // pro forma - caught this above |
754 |
|
} |
755 |
< |
fputc('\n', pdfp[0]); // flush picture header + resolution |
755 |
> |
fputc('\n', pdfp[0]); // flush picture header |
756 |
|
if (fflush(pdfp[0]) == EOF) { |
757 |
|
sprintf(errmsg, "cannot write header to picture '%s'", pfname); |
758 |
|
error(SYSTEM, errmsg); |
764 |
|
if (dfname[0] == '!') |
765 |
|
pdfp[1] = popen(dfname+1, "w"); |
766 |
|
else |
767 |
< |
pdfp[1] = fopen(dfname, "w"); |
767 |
> |
pdfp[1] = fopen(dfname, "w+"); |
768 |
|
if (!pdfp[1]) { |
769 |
|
sprintf(errmsg, "cannot open depth output '%s'", dfname); |
770 |
|
error(SYSTEM, errmsg); |
776 |
|
} |
777 |
|
if (RDTdepthT(dt) == RDTdshort) { // write header for 16-bit depth? |
778 |
|
newheader("RADIANCE", pdfp[1]); |
779 |
< |
fputs(GetHeader(), pdfp[1]); |
779 |
> |
fputs(GetHeadStr(), pdfp[1]); |
780 |
|
fputs(VIEWSTR, pdfp[1]); fprintview(&vw, pdfp[1]); fputc('\n', pdfp[1]); |
781 |
|
fputs(DEPTHSTR, pdfp[1]); fputs(dunit, pdfp[1]); fputc('\n', pdfp[1]); |
782 |
|
fputformat(DEPTH16FMT, pdfp[1]); |
812 |
|
if (RDTdepthT(dt) == RDTdshort) |
813 |
|
fprtresolu(GetWidth(), GetHeight(), pdfp[1]); |
814 |
|
|
815 |
< |
const int bheight = (psample > 1) ? int(4*psample+.99) : 8; |
815 |
> |
const int bheight = (psample > 1) ? int(8*psample+.99) : 16; |
816 |
|
const int vstep = bheight >> (psample > 1); |
817 |
|
|
818 |
|
NewBar(bheight); // render frame if we can |
1006 |
|
pdfp[0] = NULL; |
1007 |
|
return RDTnone; |
1008 |
|
} |
1009 |
+ |
if (hinfo.gotview) { // header view overrides |
1010 |
+ |
pvw = vw; |
1011 |
+ |
vw = hinfo.vw; |
1012 |
+ |
} |
1013 |
|
if (!dfname) // no depth file? |
1014 |
|
return dt; |
1015 |
|
|
1029 |
|
} |
1030 |
|
SET_FILE_BINARY(pdfp[1]); |
1031 |
|
int n, len = strlen(HDRSTR); |
1032 |
< |
char buf[32]; // sniff for 16-bit header |
1032 |
> |
char buf[32]; // sniff for 16-bit header |
1033 |
|
if (getbinary(buf, 1, len+1, pdfp[1]) < len+1) { |
1034 |
|
sprintf(errmsg, "empty depth file '%s'", dfname); |
1035 |
|
error(SYSTEM, errmsg); |
1039 |
|
} |
1040 |
|
for (n = 0; n < len; n++) |
1041 |
|
if (buf[n] != HDRSTR[n]) |
1042 |
< |
break; // not a Radiance header |
1042 |
> |
break; // not a Radiance header |
1043 |
|
rewind(pdfp[1]); |
1044 |
|
if ((n < len) | !isprint(buf[len])) |
1045 |
|
return RDTnewDT(dt, RDTdfloat); |
1046 |
|
|
1047 |
< |
HeaderInfo dinfo; // thinking it's 16-bit encoded |
1047 |
> |
HeaderInfo dinfo; // thinking it's 16-bit encoded |
1048 |
|
if (getheader(pdfp[1], head_check, &dinfo) < 0) |
1049 |
|
sprintf(errmsg, "bad header in encoded depth file '%s'", |
1050 |
|
dfname); |
1083 |
|
case RDTxyze: |
1084 |
|
break; |
1085 |
|
case RDTscolr: |
1086 |
< |
bytesPer = hinfo.ncomp + 1; // XXX assumes no compression |
1086 |
> |
bytesPer = NCSAMP + 1; // XXX assumes no compression |
1087 |
|
break; |
1088 |
|
case RDTrgb: |
1089 |
|
case RDTxyz: |
1090 |
|
bytesPer = sizeof(float)*3; |
1091 |
|
break; |
1092 |
|
case RDTscolor: |
1093 |
< |
bytesPer = sizeof(float)*hinfo.ncomp; |
1093 |
> |
bytesPer = sizeof(float)*NCSAMP; |
1094 |
|
break; |
1095 |
|
default: |
1096 |
< |
sprintf(errmsg, "unknown format (%s) for '%s'", hinfo.fmt, pfname); |
1096 |
> |
sprintf(errmsg, "unknown format for '%s'", pfname); |
1097 |
|
error(USER, errmsg); |
1098 |
|
fclose(pdfp[0]); |
1099 |
|
if (pdfp[1]) fclose(pdfp[1]); |
1107 |
|
if (pdfp[1]) fclose(pdfp[1]); |
1108 |
|
return RDTnone; |
1109 |
|
} |
1110 |
< |
vw.type = 0; // set up new (unreferenced) frame |
1109 |
< |
frameNo = 0; |
1110 |
> |
frameNo = 0; // set up unreferenced frame |
1111 |
|
int hvdim[2] = {res.xr, res.yr}; |
1112 |
|
double noAdj = 0; |
1113 |
< |
if (!NewFrame(hinfo.vw, hvdim, &noAdj) || |
1113 |
> |
if (!NewFrame(vw, hvdim, &noAdj) || |
1114 |
|
(hvdim[0] != res.xr) | (hvdim[1] != res.yr)) { |
1115 |
|
error(CONSISTENCY, "unexpected resolution change in ResumeFrame()"); |
1116 |
|
fclose(pdfp[0]); |
1178 |
|
fclose(pdfp[0]); fclose(pdfp[1]); |
1179 |
|
return RDTnone; |
1180 |
|
} |
1181 |
< |
int bheight = (psample > 1) ? int(4*psample+.99) : 8; |
1181 |
> |
int bheight = (psample > 1) ? int(8*psample+.99) : 16; |
1182 |
|
if (bheight > GetHeight()-doneScans) |
1183 |
|
bheight = GetHeight()-doneScans; |
1184 |
|
int vstep = bheight >> (psample > 1); |