8 #include "PlusConfigure.h" 15 #include <vtkImageData.h> 16 #include <vtkInformation.h> 17 #include <vtkInformationVector.h> 18 #include <vtkMultiThreader.h> 19 #include <vtkObjectFactory.h> 20 #include <vtkStreamingDemandDrivenPipeline.h> 21 #include <vtksys/SystemTools.hxx> 108 if (!vtkPlusICCapturingSource::Instance)
111 vtkPlusICCapturingSource::Instance = (
vtkPlusICCapturingSource*)vtkObjectFactory::CreateInstance(
"vtkPlusICCapturingSource");
112 if (!vtkPlusICCapturingSource::Instance)
116 if (!vtkPlusICCapturingSource::Instance)
122 return vtkPlusICCapturingSource::Instance;
128 if (vtkPlusICCapturingSource::Instance == instance)
133 if (vtkPlusICCapturingSource::Instance)
135 vtkPlusICCapturingSource::Instance->Delete();;
137 vtkPlusICCapturingSource::Instance = instance;
143 instance->Register(NULL);
149 std::ostringstream version;
150 version <<
"The Imaging Source UDSHL-" << UDSHL_LIB_VERSION_MAJOR <<
"." << UDSHL_LIB_VERSION_MINOR;
151 return version.str();
162 bool vtkPlusICCapturingSource::vtkPlusICCapturingSourceNewFrameCallback(
unsigned char*
data,
unsigned long size,
unsigned long frameNumber)
164 if (
data == NULL || size == 0)
166 LOG_ERROR(
"No actual frame data received from the framegrabber");
189 LOG_ERROR(
"Unable to retrieve the video source in the ICCapturing device.");
195 DShowLib::Grabber* grabber = static_cast<DShowLib::Grabber*>(this->
FrameGrabber);
196 if (grabber ==
nullptr || grabber->getAcqSizeMaxX() < 0 || grabber->getAcqSizeMaxY() < 0)
198 LOG_ERROR(
"Negative frame size sent from ICC device. Cannot continue.");
202 FrameSizeType frameSize = {static_cast<unsigned int>(grabber->getAcqSizeMaxX()), static_cast<unsigned int>(grabber->getAcqSizeMaxY()), 1};
204 int frameBufferBitsPerPixel = grabber->getVideoFormat().getBitsPerPixel();
205 if (frameBufferBitsPerPixel != 8)
207 LOG_ERROR(
"vtkPlusICCapturingSource::AddFrameToBuffer: only 8-bit acquisition is supported, current frameBufferBitsPerPixel=" << frameBufferBitsPerPixel);
221 if (!DShowLib::InitLibrary())
223 LOG_ERROR(
"The IC capturing library could not be initialized");
229 static bool exitFunctionAdded =
false;
230 if (!exitFunctionAdded)
232 atexit(DShowLib::ExitLibrary);
233 exitFunctionAdded =
true;
244 LOG_ERROR(
"The IC capturing library could not be initialized - invalid device name: " << this->
GetDeviceName());
252 LOG_ERROR(
"The IC capturing library could not be initialized - invalid video norm: " << this->
GetVideoNorm());
265 int bitsPerPixel = static_cast<DShowLib::Grabber*>(
FrameGrabber)->getVideoFormat().getBitsPerPixel();
266 if (bitsPerPixel != 8)
268 LOG_ERROR(
"The IC capturing library could not be initialized - invalid bits per pixel: " << bitsPerPixel);
275 LOG_ERROR(
"Unable to retrieve the video source in the ICCapturing device.");
280 FrameSizeType frameSize = {0, 0, 1};
281 frameSize[0] = static_cast<DShowLib::Grabber*>(
FrameGrabber)->getAcqSizeMaxX();
282 frameSize[1] = static_cast<DShowLib::Grabber*>(
FrameGrabber)->getAcqSizeMaxY();
288 LOG_ERROR(
"The IC capturing library could not be initialized - invalid input channel: " << this->
GetInputChannel());
307 DShowLib::FrameTypeInfoArray acceptedTypes = DShowLib::FrameTypeInfoArray::createRGBArray();
310 smart_ptr<DShowLib::FrameHandlerSink> pSink = DShowLib::FrameHandlerSink::create(DShowLib::eRGB8, 1);
314 pSink->setSnapMode(
false);
317 static_cast<DShowLib::Grabber*>(
FrameGrabber)->setSinkType(pSink);
325 LOG_DEBUG(
"Disconnect from IC capturing");
334 DShowLib::ExitLibrary();
342 if (!static_cast<DShowLib::Grabber*>(
FrameGrabber)->startLive(
false))
344 LOG_ERROR(
"Framegrabber startLive failed, cannot start the recording");
353 if (!static_cast<DShowLib::Grabber*>(
FrameGrabber)->stopLive())
355 LOG_ERROR(
"Framegrabber stopLive failed");
364 LOG_TRACE(
"vtkPlusICCapturingSource::ReadConfiguration");
370 (*it)->UnRegister(
this);
376 it->second->UnRegister(
this);
381 it->second->UnRegister(
this);
387 XML_READ_CSTRING_ATTRIBUTE_OPTIONAL(
DeviceName, deviceConfig);
388 XML_READ_CSTRING_ATTRIBUTE_OPTIONAL(
VideoNorm, deviceConfig);
389 XML_READ_CSTRING_ATTRIBUTE_OPTIONAL(
VideoFormat, deviceConfig);
390 XML_READ_STD_ARRAY_ATTRIBUTE_OPTIONAL(
int, 2,
FrameSize, deviceConfig);
398 XML_READ_CSTRING_ATTRIBUTE_OPTIONAL(
InputChannel, deviceConfig);
399 XML_READ_SCALAR_ATTRIBUTE_OPTIONAL(
int,
ICBufferSize, deviceConfig);
409 imageAcquisitionConfig->SetAttribute(
"DeviceName", this->
DeviceName);
410 imageAcquisitionConfig->SetAttribute(
"VideoNorm", this->
VideoNorm);
411 imageAcquisitionConfig->SetAttribute(
"VideoFormat", this->
VideoFormat);
413 frameSize[0] = static_cast<int>(this->
FrameSize[0]);
414 frameSize[1] = static_cast<int>(this->
FrameSize[1]);
415 imageAcquisitionConfig->SetVectorAttribute(
"FrameSize", 2, frameSize);
416 imageAcquisitionConfig->SetAttribute(
"InputChannel", this->
InputChannel);
417 imageAcquisitionConfig->SetIntAttribute(
"ICBufferSize", this->
ICBufferSize);
449 LOG_WARNING(
"ICCapturingSource is expecting one output channel and there are " << this->
OutputChannels.size() <<
" channels. First output channel will be used.");
455 LOG_ERROR(
"No output channels defined for vtkPlusICCapturingSource. Cannot proceed.");
466 if (videoFormatFrameSizeString == NULL)
473 std::vector<std::string> splitVideoFormatFrameSize;
474 igsioCommon::SplitStringIntoTokens(videoFormatFrameSizeString,
' ', splitVideoFormatFrameSize);
475 if (splitVideoFormatFrameSize.size() != 2)
485 std::vector<std::string> splitFrameSize;
486 igsioCommon::SplitStringIntoTokens(splitVideoFormatFrameSize[1],
'x', splitFrameSize);
487 if (splitFrameSize.size() != 2 || splitFrameSize[0].empty() || splitFrameSize[1].empty())
492 if (splitFrameSize[0][0] !=
'(' || splitFrameSize[1][splitFrameSize[1].size() - 1] !=
')')
498 splitFrameSize[0].erase(splitFrameSize[0].begin());
499 splitFrameSize[1].erase(splitFrameSize[1].end() - 1);
502 unsigned int frameSizeX = 0;
503 unsigned int frameSizeY = 0;
504 if (igsioCommon::StringToUInt(splitFrameSize[0].c_str(), frameSizeX) ==
PLUS_FAIL)
508 if (igsioCommon::StringToUInt(splitFrameSize[1].c_str(), frameSizeY) ==
PLUS_FAIL)
521 std::ostringstream ss;
524 std::string formatString = ss.str();
533 if (!DShowLib::InitLibrary())
535 LOG_ERROR(
"The IC capturing library could not be initialized");
538 DShowLib::Grabber grabber;
539 DShowLib::Grabber::tVidCapDevListPtr pVidCapDevList = grabber.getAvailableVideoCaptureDevices();
540 DShowLib::ExitLibrary();
542 if (pVidCapDevList == 0)
547 for (DShowLib::Grabber::tVidCapDevList::iterator it = pVidCapDevList->begin(); it != pVidCapDevList->end(); ++it)
549 deviceNames.push_back(it->toString());
558 if (!DShowLib::InitLibrary())
560 LOG_ERROR(
"The IC capturing library could not be initialized");
563 DShowLib::Grabber grabber;
566 LOG_ERROR(
"Could not connect to device: " <<
deviceName);
567 DShowLib::ExitLibrary();
570 DShowLib::Grabber::tVidNrmListPtr pVidNrmList = grabber.getAvailableVideoNorms();
571 DShowLib::ExitLibrary();
573 if (pVidNrmList == 0)
575 LOG_ERROR(
"Error getting list of capture video norms for device: " << grabber.getLastError().toString());
579 for (DShowLib::Grabber::tVidNrmList::iterator it = pVidNrmList->begin(); it != pVidNrmList->end(); ++it)
581 videoNorms.push_back(it->toString());
588 videoFormats.clear();
590 if (!DShowLib::InitLibrary())
592 LOG_ERROR(
"The IC capturing library could not be initialized");
595 DShowLib::Grabber grabber;
598 LOG_ERROR(
"Could not connect to device: " <<
deviceName);
599 DShowLib::ExitLibrary();
602 if (!grabber.setVideoNorm(videoNorm))
604 LOG_ERROR(
"Failed to set video norm: " << videoNorm);
605 DShowLib::ExitLibrary();
608 DShowLib::Grabber::tVidFmtListPtr pVidFmtList = grabber.getAvailableVideoFormats();
609 DShowLib::ExitLibrary();
611 if (pVidFmtList == 0)
613 LOG_ERROR(
"Error getting list of capture video formats for device " << grabber.getLastError().toString());
617 for (DShowLib::Grabber::tVidFmtList::iterator it = pVidFmtList->begin(); it != pVidFmtList->end(); ++it)
619 videoFormats.push_back(it->toString());
626 LOG_INFO(
"Available video formats and frame sizes for all available capture devices:");
627 std::vector<std::string> deviceNames;
629 for (std::vector<std::string>::iterator deviceNameIt = deviceNames.begin(); deviceNameIt != deviceNames.end(); ++deviceNameIt)
631 LOG_INFO(
" Device name: " << (*deviceNameIt));
632 std::vector<std::string> videoNorms;
634 for (std::vector<std::string>::iterator videoNormIt = videoNorms.begin(); videoNormIt != videoNorms.end(); ++videoNormIt)
636 LOG_INFO(
" Video norm: " << (*videoNormIt));
637 std::vector<std::string> videoModes;
639 for (std::vector<std::string>::iterator videoModeIt = videoModes.begin(); videoModeIt != videoModes.end(); ++videoModeIt)
641 LOG_INFO(
" " << (*videoModeIt));
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
virtual void SetVideoFormat(const char *)
virtual void SetDeviceName(const char *)
virtual char * GetVideoNorm()
void ParseDShowLibVideoFormatString(const char *videoFormatFrameSizeString)
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement)
~vtkPlusICCapturingSource()
static vtkPlusICCapturingSourceCleanup Cleanup
static vtkPlusICCapturingSource * GetInstance()
ChannelContainer InputChannels
~vtkPlusICCapturingSourceCleanup()
void LogListOfCaptureDevices()
PlusStatus SetInputFrameSize(unsigned int x, unsigned int y, unsigned int z)
virtual PlusStatus AddItem(vtkImageData *frame, US_IMAGE_ORIENTATION usImageOrientation, US_IMAGE_TYPE imageType, long frameNumber, double unfilteredTimestamp=UNDEFINED_TIMESTAMP, double filteredTimestamp=UNDEFINED_TIMESTAMP, const igsioFieldMapType *customFields=NULL)
vtkPlusICCapturingSource()
DataSourceContainer VideoSources
static void SetInstance(vtkPlusICCapturingSource *instance)
virtual void SetVideoNorm(const char *)
bool RequireImageOrientationInConfiguration
DataSourceContainer Tools
Class for providing video input interfaces between VTK and ICCapturing frame grabber device.
PlusStatus SetPixelType(igsioCommon::VTKScalarPixelType pixelType)
void GetListOfCaptureVideoNorms(std::vector< std::string > &videoNorms, const std::string &deviceName)
virtual char * GetVideoFormat()
Class that listens for incoming framegrabber images.
virtual int GetICBufferSize()
void SetFrameSize(const FrameSizeType &frameSize)
virtual PlusStatus Disconnect()
PlusStatus GetFirstActiveOutputVideoSource(vtkPlusDataSource *&aVideoSource)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *config)
virtual PlusStatus InternalDisconnect()
virtual void SetCorrectlyConfigured(bool)
void GetListOfCaptureVideoModes(std::vector< std::string > &videoModes, const std::string &deviceName, const std::string &videoNorm)
std::string GetDShowLibVideoFormatString()
unsigned long FrameNumber
virtual std::string GetSdkVersion()
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *config)
void setBufferSize(unsigned long NumBuffers)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement)
virtual PlusStatus InternalConnect()
PlusStatus AddFrameToBuffer(unsigned char *data, unsigned long size, unsigned long frameNumber)
DataSourceContainer::iterator DataSourceContainerIterator
virtual PlusStatus InternalStartRecording()
static vtkPlusICCapturingSource * New()
virtual US_IMAGE_ORIENTATION GetInputImageOrientation()
Class that cleans up (deletes singleton instance of) vtkPlusICCapturingSource when destroyed.
void SetICCapturingSourceNewFrameCallback(ICCapturingSourceNewFramePtr cb)
virtual PlusStatus NotifyConfigured()
virtual PlusStatus InternalStopRecording()
ChannelContainer OutputChannels
virtual char * GetInputChannel()
ICCapturingListener * FrameGrabberListener
FrameSizeType GetFrameSize() const
ChannelContainer::iterator ChannelContainerIterator
void GetListOfCaptureDevices(std::vector< std::string > &deviceNames)
vtkPlusICCapturingSourceCleanup()
virtual char * GetDeviceName()
Interface to a 3D positioning tool, video source, or generalized data stream.