Skip to content

Commit c4c2e85

Browse files
committed
ovis: add getCompositorTexture for reading custom render targets
1 parent ff9d313 commit c4c2e85

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

modules/ovis/include/opencv2/ovis.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ class CV_EXPORTS_W WindowScene {
161161
*/
162162
CV_WRAP virtual void getScreenshot(OutputArray frame) = 0;
163163

164+
/**
165+
* read back the texture of an active compositor
166+
* @param compname name of the compositor
167+
* @param texname name of the texture inside the compositor
168+
* @param mrtIndex if texture is a MRT, specifies the attachment
169+
* @param out the texture contents
170+
*/
171+
CV_WRAP virtual void getCompositorTexture(const String& compname, const String& texname,
172+
OutputArray out, int mrtIndex = 0) = 0;
173+
164174
/**
165175
* get the depth for the current frame.
166176
*

modules/ovis/src/ovis.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,57 @@ class WindowSceneImpl : public WindowScene
382382
}
383383
}
384384

385+
void getCompositorTexture(const String& compname, const String& texname, OutputArray out,
386+
int mrtIndex) CV_OVERRIDE
387+
{
388+
CompositorManager& cm = CompositorManager::getSingleton();
389+
CompositorChain* chain = cm.getCompositorChain(frameSrc->getViewport(0));
390+
CV_Assert(chain && "no active compositors");
391+
392+
CompositorInstance* inst = chain->getCompositor(compname);
393+
if(!inst)
394+
CV_Error_(Error::StsBadArg, ("no active compositor named: %s", compname.c_str()));
395+
396+
TexturePtr tex = inst->getTextureInstance(texname, mrtIndex);
397+
if(!tex)
398+
CV_Error_(Error::StsBadArg, ("no texture named: %s", texname.c_str()));
399+
400+
PixelFormat src_type = tex->getFormat();
401+
int dst_type;
402+
switch(src_type)
403+
{
404+
case PF_BYTE_RGB:
405+
dst_type = CV_8UC3;
406+
break;
407+
case PF_BYTE_RGBA:
408+
dst_type = CV_8UC4;
409+
break;
410+
case PF_FLOAT32_RGB:
411+
dst_type = CV_32FC3;
412+
break;
413+
case PF_FLOAT32_RGBA:
414+
dst_type = CV_32FC4;
415+
break;
416+
case PF_DEPTH16:
417+
dst_type = CV_16U;
418+
break;
419+
default:
420+
CV_Error(Error::StsNotImplemented, "unsupported texture format");
421+
}
422+
423+
out.create(tex->getHeight(), tex->getWidth(), dst_type);
424+
425+
Mat mat = out.getMat();
426+
PixelBox pb(tex->getWidth(), tex->getHeight(), 1, src_type, mat.ptr());
427+
tex->getBuffer()->blitToMemory(pb, pb);
428+
429+
if(CV_MAT_CN(dst_type) < 3)
430+
return;
431+
432+
// convert to OpenCV channel order
433+
cvtColor(mat, mat, CV_MAT_CN(dst_type) == 3 ? COLOR_RGB2BGR : COLOR_RGBA2BGRA);
434+
}
435+
385436
void setBackground(const Scalar& color) CV_OVERRIDE
386437
{
387438
// hide background plane

0 commit comments

Comments
 (0)