13 |
|
#include "RtraceSimulManager.h" |
14 |
|
#include "view.h" |
15 |
|
#include "depthcodec.h" |
16 |
– |
#include "abitmap.h" |
16 |
|
|
17 |
|
/// Data type flags for pixel access and output |
18 |
|
enum RenderDataType { |
42 |
|
long rowStride; // # values to next y position |
43 |
|
int dtyp; // data type flags |
44 |
|
RGBPRIMP primp; // color primaries if tristimulus |
45 |
+ |
bool swap2d; // need to swap 16-bit depths? |
46 |
|
const COLORV * CF3(int x, int y) const { |
47 |
|
return pbase.f + (rowStride*y + x)*3; |
48 |
|
} |
67 |
|
COLRV * SCB(int x, int y) { |
68 |
|
return pbase.b + (rowStride*y + x)*(NCSAMP+1); |
69 |
|
} |
70 |
+ |
void EncodeDepth16(int x, int y, float z) { |
71 |
+ |
short * sp = dbase.s + rowStride*y + x; |
72 |
+ |
*sp = depth2code(z, refDepth); |
73 |
+ |
if (swap2d) swap16((char *)sp, 1); |
74 |
+ |
} |
75 |
+ |
float DecodeDepth16(int x, int y) const { |
76 |
+ |
short sv = dbase.s[rowStride*y + x]; |
77 |
+ |
if (swap2d) swap16((char *)&sv, 1); |
78 |
+ |
return code2depth(sv, refDepth); |
79 |
+ |
} |
80 |
|
public: |
81 |
|
double refDepth; // reference depth |
82 |
|
PixelAccess() { |
83 |
|
refDepth = 1.; |
84 |
+ |
swap2d = !nativebigendian(); |
85 |
|
Init(); |
86 |
|
} |
87 |
|
PixelAccess(COLORV *rp, int ystride, float *zp=NULL) { |
88 |
|
refDepth = 1.; |
89 |
+ |
swap2d = !nativebigendian(); |
90 |
|
Init(rp, ystride, zp); |
91 |
|
} |
92 |
|
PixelAccess(COLRV *bp, int ystride, float *zp=NULL) { |
93 |
|
refDepth = 1.; |
94 |
+ |
swap2d = !nativebigendian(); |
95 |
|
Init(bp, ystride, zp); |
96 |
|
} |
97 |
|
PixelAccess(COLRV *bp, int ystride, short *dp) { |
98 |
|
refDepth = 1.; |
99 |
+ |
swap2d = !nativebigendian(); |
100 |
|
Init(bp, ystride, dp); |
101 |
|
} |
102 |
+ |
PixelAccess(COLORV *rp, int ystride, short *dp) { |
103 |
+ |
refDepth = 1.; |
104 |
+ |
swap2d = !nativebigendian(); |
105 |
+ |
Init(rp, ystride, dp); |
106 |
+ |
} |
107 |
|
void Init() { |
108 |
|
pbase.f = NULL; dbase.f = NULL; |
109 |
|
rowStride = 0; |
141 |
|
} |
142 |
|
if (dp) dtyp |= RDTdshort; |
143 |
|
} |
144 |
+ |
void Init(COLORV *rp, int ystride, short *dp) { |
145 |
+ |
pbase.f = rp; dbase.s = dp; |
146 |
+ |
rowStride = ystride; |
147 |
+ |
if (NCSAMP > 3) { |
148 |
+ |
dtyp = RDTscolor; primp = NULL; |
149 |
+ |
} else { |
150 |
+ |
dtyp = RDTrgb; primp = stdprims; |
151 |
+ |
} |
152 |
+ |
if (dp) dtyp |= RDTdshort; |
153 |
+ |
} |
154 |
|
/// Set color space after non-empty initialization |
155 |
|
bool SetColorSpace(RenderDataType cs, RGBPRIMP pr=NULL); |
156 |
|
/// Get color space |
194 |
|
scolor_scolr(SCB(x,y), pv); |
195 |
|
else |
196 |
|
copyscolor(SCF(x,y), pv); |
197 |
< |
if (RDTdepthT(dtyp) == RDTdfloat) |
197 |
> |
if (DepthType() == RDTdfloat) |
198 |
|
dbase.f[rowStride*y + x] = z; |
199 |
< |
else if (RDTdepthT(dtyp) == RDTdshort) |
200 |
< |
dbase.s[rowStride*y + x] = depth2code(z, refDepth); |
199 |
> |
else if (DepthType() == RDTdshort) |
200 |
> |
EncodeDepth16(x, y, z); |
201 |
|
return true; |
202 |
|
} |
203 |
|
/// Retrieve pixel color (& depth) -- may convert either/both |
212 |
|
else |
213 |
|
copyscolor(pv, SCF(x,y)); |
214 |
|
if (!zp) return true; |
215 |
< |
if (RDTdepthT(dtyp) == RDTdfloat) |
215 |
> |
if (DepthType() == RDTdfloat) |
216 |
|
*zp = dbase.f[rowStride*y + x]; |
217 |
< |
else if (RDTdepthT(dtyp) == RDTdshort) |
218 |
< |
*zp = code2depth(dbase.s[rowStride*y + x], refDepth); |
217 |
> |
else if (DepthType() == RDTdshort) |
218 |
> |
*zp = DecodeDepth16(x, y); |
219 |
|
else |
220 |
|
*zp = .0f; |
221 |
|
return true; |
233 |
|
copyscolr(SCB(dx,dy), SCB(sx,sy)); |
234 |
|
else |
235 |
|
copyscolor(SCF(dx,dy), SCF(sx,sy)); |
236 |
< |
switch (RDTdepthT(dtyp)) { |
236 |
> |
switch (DepthType()) { |
237 |
|
case RDTdfloat: |
238 |
|
dbase.f[rowStride*dy + dx] = |
239 |
|
dbase.f[rowStride*sy + sx]; |
266 |
|
COLORV * barPix; // current render bar pixels |
267 |
|
float * barDepth; // current render bar depths |
268 |
|
bool SetTile(const int ti[2]); |
269 |
< |
bool RenderRect(); |
269 |
> |
bool RenderRect(const int x0 = 0, const int y0 = 0); |
270 |
|
bool ComputePixel(int x, int y); |
271 |
< |
bool BelowSampThresh(int x, int y, const int noff[4][2]) const; |
271 |
> |
bool BelowSampThresh(const int x, const int y, |
272 |
> |
const int noff[4][2]) const; |
273 |
|
void FillSquare(const int x, const int y, const int noff[4][2]); |
274 |
|
void NewBar(int ht = 0); |
275 |
|
bool LowerBar(int v, int ytop); |
291 |
|
frameNo = 0; |
292 |
|
} |
293 |
|
~RpictSimulManager() { |
294 |
< |
NewBar(); |
294 |
> |
delete [] barPix; delete [] barDepth; |
295 |
|
} |
296 |
|
/// Load octree and prepare renderer |
297 |
|
bool LoadOctree(const char *octn) { |
310 |
|
return RtraceSimulManager::AddHeader(ac, av); |
311 |
|
} |
312 |
|
/// Get header lines if any |
313 |
< |
const char * GetHeader() const { |
314 |
< |
return RtraceSimulManager::GetHeader(); |
313 |
> |
const char * GetHeadStr() const { |
314 |
> |
return RtraceSimulManager::GetHeadStr(); |
315 |
|
} |
316 |
+ |
/// Look for specific header keyword, return value |
317 |
+ |
const char * GetHeadStr(const char *key, bool inOK = false) const { |
318 |
+ |
return RtraceSimulManager::GetHeadStr(key, inOK); |
319 |
+ |
} |
320 |
|
/// Set number of computation threads (0 => #cores) |
321 |
|
int SetThreadCount(int nt = 0) { |
322 |
|
return RtraceSimulManager::SetThreadCount(nt); |
359 |
|
/// Increments frameNo if >0 |
360 |
|
bool NewFrame(const VIEW &v, int xydim[2], double *ap=NULL, |
361 |
|
const int *tgrid=NULL); |
362 |
+ |
/// Get current view if set |
363 |
+ |
const VIEW * GetView() const { |
364 |
+ |
if (!vw.type) return NULL; |
365 |
+ |
return &vw; |
366 |
+ |
} |
367 |
+ |
/// Writeable previous view (for motion blur) |
368 |
+ |
VIEW & PreView() { |
369 |
+ |
return pvw; |
370 |
+ |
} |
371 |
|
/// Get current picture width |
372 |
|
int GetWidth() const { |
373 |
|
return hvres[0]; |
396 |
|
/// Same but also use 16-bit encoded depth buffer |
397 |
|
bool RenderTile(COLRV *bp, int ystride, short *dp, |
398 |
|
const int *tile=NULL); |
399 |
+ |
/// Back to float color with 16-bit depth |
400 |
+ |
bool RenderTile(COLORV *rp, int ystride, short *dp, |
401 |
+ |
const int *tile=NULL); |
402 |
|
/// Render and write a frame to the named file |
403 |
|
/// Include any header lines set prior to call |
404 |
< |
/// Picture file must not already exist |
404 |
> |
/// Picture file must not exist |
405 |
|
/// Write pixels to stdout if !pfname |
406 |
|
/// Write depth to a command if dfname[0]=='!' |
407 |
|
RenderDataType RenderFrame(const char *pfname, |
410 |
|
/// Resume partially finished rendering |
411 |
|
/// Picture file must exist with valid header |
412 |
|
RenderDataType ResumeFrame(const char *pfname, |
413 |
+ |
const char *dfname=NULL); |
414 |
+ |
/// Prepare new picture (and depth) output |
415 |
+ |
/// Called by RenderFrame() after NewFrame() |
416 |
+ |
RenderDataType NewOutput(FILE *pdfp[2], const char *pfname, |
417 |
+ |
RenderDataType dt=RDTrgbe, |
418 |
+ |
const char *dfname=NULL); |
419 |
+ |
/// Reopen existing picture (and depth) file |
420 |
+ |
/// Called by ResumeFrame() |
421 |
+ |
/// File pointers @ end of header (before res.) |
422 |
+ |
RenderDataType ReopenOutput(FILE *pdfp[2], const char *pfname, |
423 |
|
const char *dfname=NULL); |
424 |
|
/// Close octree, free data, return status |
425 |
|
int Cleanup(bool everything = false) { |