ADTF_DISPLAY_TOOLBOX  3.8.0 (ADTF 3.14.3)
Source Code for Demo 2D Video Drawer
Location
./src/examples/src/videodrawer
This example shows:
  • how to create a custom drawer.
  • how to use the ITexture interface.
  • how to use the cImage class.
  • how to use the canvas inside the Draw method.
Header
#pragma once
#include <adtffiltersdk/filter.h>
#include <adtf_base.h>
class cVideoDrawer : public adtf::ucom::object<adtf::disptb::drawerlib::cDrawer>
{
public:
ADTF_CLASS_ID_NAME(
cVideoDrawer,
"demo_video.2d_drawer.disptb.cid",
"Demo 2D Video Drawer"
);
cVideoDrawer();
~cVideoDrawer() = default;
tResult Draw(const adtf::ucom::iobject_ptr<adtf::disptb::graphicslib::ICanvas>& pCanvas) override;
private:
tResult ChangeType(const adtf::streaming::ant::IStreamType& oType);
tResult SetImageFormat(const adtf::streaming::ant::IStreamType& oType);
private:
adtf::streaming::size_limited_sample_reader<1, tFalse>* m_pReader;
adtf::streaming::tStreamImageFormat m_sCurrentFormat;
tBitmapFormat m_oSourceFormat;
};
The interface for common texture handling.
Image and bitmap handling.
Definition: image.h:39
Copyright © Audi Electronics Venture GmbH.
Struct to specifie a bitmap.
Implementation
#include "video_drawer.h"
using namespace adtf_util;
using namespace ddl;
using namespace adtf::ucom;
using namespace adtf::base;
using namespace adtf::streaming;
using namespace adtf::mediadescription;
using namespace adtf::filter;
using namespace adtf::disptb::graphicslib;
ADTF_PLUGIN_VERSION("Demo 2D Video Drawer Plugin",
disptb,
DISPTB_VERSION_MAJOR,
DISPTB_VERSION_MINOR,
DISPTB_VERSION_PATCH,
cVideoDrawer)
cVideoDrawer::cVideoDrawer() :
m_pTexture(nullptr)
{
m_sCurrentFormat.m_strFormatName = ADTF_IMAGE_FORMAT(RGB_32);
m_sCurrentFormat.m_ui32Width = 640;
m_sCurrentFormat.m_ui32Height = 480;
m_sCurrentFormat.m_szMaxByteSize = 0;
m_sCurrentFormat.m_ui8DataEndianess = PLATFORM_BYTEORDER;
object_ptr<IStreamType> pType = make_object_ptr<cStreamType>(stream_meta_type_image());
set_stream_type_image_format(*pType, m_sCurrentFormat);
ChangeType(*pType.Get());
m_pReader = CreateInputPin<size_limited_sample_reader<1, tFalse>>("video", pType, tFalse);
m_pReader->SetAcceptTypeCallback([this](const iobject_ptr<const IStreamType>& pType) -> tResult
{
return ChangeType(*pType.Get());
});
SetDescription("video", "Input pin for video data");
SetDescription("Use this Drawer to paint a received videostream as image onto canvas.");
SetHelpLink("$(ADTF_DISPLAY_TOOLBOX_DIR)/doc/displaytoolbox_html/page_2d_video_drawer_readme.html");
}
tResult cVideoDrawer::ChangeType(const IStreamType& oType)
{
if (oType == stream_meta_type_image())
{
SetImageFormat(oType);
// create an image with this format
RETURN_IF_FAILED(m_oImage.Create(&m_oSourceFormat));
}
else
{
RETURN_ERROR(ERR_INVALID_TYPE);
}
RETURN_NOERROR;
}
tResult cVideoDrawer::SetImageFormat(const adtf::streaming::ant::IStreamType& oType)
{
get_stream_type_image_format(m_sCurrentFormat, oType);
tInt nPixelSize = stream_image_format_get_generic_pixel_size(m_sCurrentFormat);
m_oSourceFormat.nHeight = m_sCurrentFormat.m_ui32Height;
m_oSourceFormat.nWidth = m_sCurrentFormat.m_ui32Width;
m_oSourceFormat.nBitsPerPixel = static_cast<tInt>(nPixelSize);
m_oSourceFormat.nPaletteSize = 0;
m_oSourceFormat.nBytesPerLine = static_cast<tInt>(nPixelSize / 8 * m_sCurrentFormat.m_ui32Width);
m_oSourceFormat.nSize = static_cast<tInt>(nPixelSize / 8 * m_sCurrentFormat.m_ui32Width) * m_sCurrentFormat.m_ui32Height;
if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::GREYSCALE_8::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_GREYSCALE_8;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::GREYSCALE_16::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_GREYSCALE_16;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::GREYSCALE_24::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_GREYSCALE_24;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::GREYSCALE_32::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_GREYSCALE_32;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::RGB_8::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_RGB_8;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::RGB_555::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_RGB_555;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::RGB_565::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_RGB_565;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::RGB_24::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_RGB_888;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::BGR_24::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_BGR_888;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::RGB_32::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_32BIT;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::BGR_32::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_32BIT;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::RGBA_32::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_RGBA_8888;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::BGRA_32::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_BGRA_8888;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::ABGR_32::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_ABGR_8888;
}
else if (m_sCurrentFormat.m_strFormatName.IsEqual(stream_image_format::ARGB_32::FormatName))
{
m_oSourceFormat.nPixelFormat = IImage::PF_ARGB_8888;
}
RETURN_NOERROR;
}
tResult cVideoDrawer::Draw(const iobject_ptr<ICanvas>& pCanvas)
{
object_ptr<const ISample> pSample;
if (IS_OK(m_pReader->GetNextSample(pSample)))
{
object_ptr_shared_locked<const ISampleBuffer> pBuffer;
if (IS_OK(pSample->Lock(pBuffer)))
{
m_oImage.Attach(static_cast<tUInt8*>(const_cast<tVoid*>(pBuffer->GetPtr())), &m_oSourceFormat);
m_oImage.SetPixelFormat(m_oImage.FlipToRGBFormat(m_oImage.GetPixelFormat()));
RETURN_IF_FAILED(pCanvas->CreateTexture(&m_oImage, m_pTexture));
}
}
// blit the texture in any case (even if the image is still the same)
tFloat32 fWidth = static_cast<tFloat32>(m_oSourceFormat.nWidth);
tFloat32 fHeight = static_cast<tFloat32>(m_oSourceFormat.nHeight);
std::array<tFloat32, 8> oPoints;
oPoints[0] = -fWidth / 2 + fWidth;
oPoints[1] = -fHeight / 2;
oPoints[2] = -fWidth / 2 + fWidth;
oPoints[3] = -fHeight / 2 + fHeight;
oPoints[4] = -fWidth / 2;
oPoints[5] = -fHeight / 2 + fHeight;
oPoints[6] = -fWidth / 2;
oPoints[7] = -fHeight / 2;
if (m_pTexture)
{
pCanvas->DrawTexture(m_pTexture, oPoints.data());
}
RETURN_NOERROR;
}