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 { |
26 |
|
#define RDTcolorT(f) RenderDataType((f) & RDTcolorM) |
27 |
|
#define RDTdepthT(f) RenderDataType((f) & RDTdepthM) |
28 |
|
#define RDTcommonE(f) (RDTcolorT(f) >= RDTscolr) |
29 |
< |
#define RDTnewCT(f,c) RenderDataType((f) & ~RDTcolorM | (c)) |
30 |
< |
#define RDTnewDT(f,d) RenderDataType((f) & ~RDTdepthM | (d)) |
29 |
> |
#define RDTnewCT(f,c) RenderDataType((f) & ~RDTcolorM | (c) & RDTcolorM) |
30 |
> |
#define RDTnewDT(f,d) RenderDataType((f) & ~RDTdepthM | (d) & RDTdepthM) |
31 |
|
|
32 |
|
/// Pixel accessor (read/write to caller's buffer with possible conversion) |
33 |
|
class PixelAccess { |
42 |
|
long rowStride; // # values to next y position |
43 |
|
int dtyp; // data type flags |
44 |
|
RGBPRIMP primp; // color primaries if tristimulus |
45 |
< |
COLORMAT xyz2myrgbmat; // custom color conversion matrix |
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 |
> |
} |
49 |
|
COLORV * CF3(int x, int y) { |
50 |
|
return pbase.f + (rowStride*y + x)*3; |
51 |
|
} |
52 |
< |
COLORV * GetCF3(int x, int y) const { |
53 |
< |
return const_cast<COLORV *>(pbase.f + (rowStride*y + x)*3); |
52 |
> |
const COLRV * CB3(int x, int y) const { |
53 |
> |
return pbase.b + (rowStride*y + x)*4; |
54 |
|
} |
53 |
– |
COLORV * SCF(int x, int y) { |
54 |
– |
return pbase.f + (rowStride*y + x)*NCSAMP; |
55 |
– |
} |
56 |
– |
COLORV * GetSCF(int x, int y) const { |
57 |
– |
return const_cast<COLORV *>(pbase.f + (rowStride*y + x)*NCSAMP); |
58 |
– |
} |
55 |
|
COLRV * CB3(int x, int y) { |
56 |
|
return pbase.b + (rowStride*y + x)*4; |
57 |
|
} |
58 |
< |
COLRV * GetCB3(int x, int y) const { |
59 |
< |
return const_cast<COLRV *>(pbase.b + (rowStride*y + x)*4); |
58 |
> |
const COLORV * SCF(int x, int y) const { |
59 |
> |
return pbase.f + (rowStride*y + x)*NCSAMP; |
60 |
|
} |
61 |
+ |
COLORV * SCF(int x, int y) { |
62 |
+ |
return pbase.f + (rowStride*y + x)*NCSAMP; |
63 |
+ |
} |
64 |
+ |
const COLRV * SCB(int x, int y) const { |
65 |
+ |
return pbase.b + (rowStride*y + x)*(NCSAMP+1); |
66 |
+ |
} |
67 |
|
COLRV * SCB(int x, int y) { |
68 |
|
return pbase.b + (rowStride*y + x)*(NCSAMP+1); |
69 |
|
} |
70 |
< |
COLRV * GetSCB(int x, int y) const { |
71 |
< |
return const_cast<COLRV *>(pbase.b + (rowStride*y + x)*(NCSAMP+1)); |
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 |
204 |
|
bool GetPixel(int x, int y, COLORV *pv, float *zp=NULL) const { |
205 |
|
if (NC() == 3) { |
206 |
|
if (RDTcommonE(dtyp)) |
207 |
< |
colr_color(pv, GetCB3(x,y)); |
207 |
> |
colr_color(pv, CB3(x,y)); |
208 |
|
else |
209 |
< |
copycolor(pv, GetCF3(x,y)); |
209 |
> |
copycolor(pv, CF3(x,y)); |
210 |
|
} else if (RDTcommonE(dtyp)) |
211 |
< |
scolr_scolor(pv, GetSCB(x,y)); |
211 |
> |
scolr_scolor(pv, SCB(x,y)); |
212 |
|
else |
213 |
< |
copyscolor(pv, GetSCF(x,y)); |
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; |
226 |
|
const int nc = NC(); |
227 |
|
if (nc == 3) { |
228 |
|
if (RDTcommonE(dtyp)) |
229 |
< |
copycolr(CB3(dx,dy), GetCB3(sx,sy)); |
229 |
> |
copycolr(CB3(dx,dy), CB3(sx,sy)); |
230 |
|
else |
231 |
< |
copycolor(CF3(dx,dy), GetCF3(sx,sy)); |
231 |
> |
copycolor(CF3(dx,dy), CF3(sx,sy)); |
232 |
|
} else if (RDTcommonE(dtyp)) |
233 |
< |
copyscolr(SCB(dx,dy), GetSCB(sx,sy)); |
233 |
> |
copyscolr(SCB(dx,dy), SCB(sx,sy)); |
234 |
|
else |
235 |
< |
copyscolor(SCF(dx,dy), GetSCF(sx,sy)); |
236 |
< |
switch (RDTdepthT(dtyp)) { |
235 |
> |
copyscolor(SCF(dx,dy), SCF(sx,sy)); |
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; |
272 |
< |
void FillSquare(int x, int y, const int noff[4][2]); |
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); |
275 |
> |
bool LowerBar(int v, int ytop); |
276 |
|
bool RenderBelow(int ytop, const int vstep, FILE *pfp, |
277 |
|
const int dt, FILE *dfp=NULL); |
278 |
|
public: |
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 |
405 |
< |
/// Picture to stdout if pfname==NULL |
406 |
< |
/// Depth written to a command if dfname[0]=='!' |
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, |
408 |
|
RenderDataType dt=RDTrgbe, |
409 |
|
const char *dfname=NULL); |
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) { |