PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusDAQVideoSource.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 
6 Developed by ULL & IACTEC-IAC group
7 =========================================================Plus=header=end*/
8 
9 // Local includes
10 #include "PlusConfigure.h"
11 #include "vtkPlusChannel.h"
12 #include "vtkPlusDataSource.h"
13 #include "vtkPlusDAQVideoSource.h"
14 
15 // VTK includes
16 #include <vtkImageData.h>
17 #include <vtkObjectFactory.h>
18 
19 // DAQ System
20 #include "usb3_frm13_import.h"
21 
22 //----------------------------------------------------------------------------
23 
25 
26 //----------------------------------------------------------------------------
28 {
30  this->StartThreadForInternalUpdates = true;
31 }
32 
33 //----------------------------------------------------------------------------
35 {
36 }
37 
38 //----------------------------------------------------------------------------
39 void vtkPlusDAQVideoSource::PrintSelf(ostream& os, vtkIndent indent)
40 {
41  this->Superclass::PrintSelf(os, indent);
42  os << indent << "DAQVideoSource: CameraLink Camera" << std::endl;
43 }
44 
45 //-----------------------------------------------------------------------------
46 PlusStatus vtkPlusDAQVideoSource::ReadConfiguration(vtkXMLDataElement* rootConfigElement)
47 {
48  XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement);
49  LOG_DEBUG("Configure CameraLink Camera");
50 
51  this->m_dataMode = DATAMODE_8;
52 
53  const char* dataModeString = deviceConfig->GetAttribute("DataMode");
54  if (dataModeString)
55  {
56  switch(std::atoi(dataModeString)){
57  case 8:
58  this->m_dataMode = DATAMODE_8;
59  break;
60  case 16:
61  this->m_dataMode = DATAMODE_16;
62  break;
63  default:
64  this->m_dataMode = DATAMODE_8;
65  }
66  }
67  return PLUS_SUCCESS;
68 }
69 
70 //-----------------------------------------------------------------------------
71 PlusStatus vtkPlusDAQVideoSource::WriteConfiguration(vtkXMLDataElement* rootConfigElement)
72 {
73  XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement);
74  return PLUS_SUCCESS;
75 }
76 
77 //----------------------------------------------------------------------------
79 {
80  this->deviceRunning = false;
81 
82  if (!OpenDAQDevice())
83  {
84  LOG_ERROR("DAQVideoSource CameraLink: Failed to open.");
85  return PLUS_FAIL;
86  }
87 
89  switch (this->m_dataMode) {
90  case DATAMODE_8:
91  this->m_nbytesMode = 2;
92  break;
93  default:
94  this->m_nbytesMode = 4;
95  }
96 
97  if (!LVDS_Init())
98  {
99  LOG_ERROR("DAQVideoSource CameraLink: Failed to init.");
100  return PLUS_FAIL;
101  }
102  LVDS_SetDataMode(this->m_dataMode);
103  LVDS_CameraMode(this->cameraMode);
104  LVDS_SetDeUse(true);
105  LVDS_GetResolution(&(this->m_width), &(this->m_height));
106  this->m_nwidth = this->m_width / 2;
107  this->m_nheight = this->m_height;
108  this->m_maxBuffSize = this->m_width * this->m_height;
109  this->pImgBuf = new CAMERADATATYPE_t[this->m_maxBuffSize];
110  this->m_dwCharCount = this->m_nbytesMode * this->m_maxBuffSize;
111  this->pImgBufAux = new unsigned char[this->m_dwCharCount];
112 
113  if (!LVDS_Start())
114  {
115  LOG_ERROR("DAQVideoSource CameraLink: Failed to start");
116  return PLUS_FAIL;
117  }
118  this->deviceRunning = true;
119 
120  return PLUS_SUCCESS;
121 }
122 
123 //----------------------------------------------------------------------------
125 {
126  LVDS_Stop();
127  CloseDAQDevice();
128  this->pImgBuf = nullptr;
129  return PLUS_SUCCESS;
130 }
131 
132 //----------------------------------------------------------------------------
134 {
135  std::ostringstream ss;
136  int ix,iy;
137 
138  if (!this->deviceRunning)
139  {
140  LOG_ERROR("vtkPlusDAQVideoSource::InternalUpdate Unable to read data");
141  return PLUS_SUCCESS;
142  }
143 
144  this->m_currentTime = vtkIGSIOAccurateTimer::GetSystemTime();
145  if (!LVDS_GetFrame(&m_dwCharCount, (unsigned char*)(this->pImgBufAux)))
146  {
147  LOG_DEBUG("vtkPlusDAQVideoSource::InternalUpdate Unable to receive frame");
148  return PLUS_SUCCESS;
149  }
150 
151  // Byte assignment to output image buffer
152  for (ix = 0, iy = 0; ix < this->m_maxBuffSize; ix+=2, iy += this->m_nbytesMode) {
153  ((unsigned char*)this->pImgBuf)[ix + 0] = this->pImgBufAux[iy + 0] ;
154  ((unsigned char*)this->pImgBuf)[ix + 1] = this->pImgBufAux[iy + 1];
155  }
156 
157  vtkPlusDataSource* aSource(nullptr);
158  if (this->GetFirstActiveOutputVideoSource(aSource) == PLUS_FAIL || aSource == nullptr)
159  {
160  LOG_DEBUG("Unable to grab a video source. Skipping frame.");
161  return PLUS_SUCCESS;
162  }
163 
164  if (aSource->GetNumberOfItems() == 0)
165  {
166  // Init the buffer with the metadata from the first frame
167  aSource->SetImageType(US_IMG_BRIGHTNESS);
168  aSource->SetPixelType(VTK_UNSIGNED_SHORT);
169  aSource->SetNumberOfScalarComponents(1);
170  aSource->SetInputFrameSize(this->m_nwidth, this->m_nheight, 1);
171  }
172 
173  // Add the frame to the stream buffer
174  FrameSizeType frameSize = { static_cast<unsigned int>(this->m_nwidth), static_cast<unsigned int>(this->m_nheight), 1 };
175  if (aSource->AddItem(this->pImgBuf, aSource->GetInputImageOrientation(), frameSize, VTK_UNSIGNED_SHORT, 1, US_IMG_BRIGHTNESS, 0, this->FrameNumber, this->m_currentTime, this->m_currentTime) == PLUS_FAIL)
176  {
177  return PLUS_FAIL;
178  }
179  this->FrameNumber++;
180  return PLUS_SUCCESS;
181 }
182 
183 //----------------------------------------------------------------------------
185 {
186  if (this->OutputChannels.size() > 1)
187  {
188  LOG_WARNING("vtkPlusDAQVideoSource is expecting one output channel and there are " << this->OutputChannels.size() << " channels. First output channel will be used.");
189  }
190 
191  if (this->OutputChannels.empty())
192  {
193  LOG_ERROR("No output channels defined for vtkPlusDAQVideoSource. Cannot proceed.");
194  this->CorrectlyConfigured = false;
195  return PLUS_FAIL;
196  }
197  return PLUS_SUCCESS;
198 }
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement)
igsioStatus PlusStatus
Definition: PlusCommon.h:40
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
vtkStandardNewMacro(vtkPlusDAQVideoSource)
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)
bool RequireImageOrientationInConfiguration
PlusStatus SetImageType(US_IMAGE_TYPE imageType)
#define PLUS_FAIL
Definition: PlusCommon.h:43
virtual PlusStatus InternalConnect() VTK_OVERRIDE
PlusStatus SetPixelType(igsioCommon::VTKScalarPixelType pixelType)
PlusStatus GetFirstActiveOutputVideoSource(vtkPlusDataSource *&aVideoSource)
PlusStatus WriteConfiguration(vtkXMLDataElement *config)
unsigned long FrameNumber
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement)
virtual PlusStatus NotifyConfigured()
Class for interfacing an Infrared capture device and recording frames into a Plus buffer.
virtual US_IMAGE_ORIENTATION GetInputImageOrientation()
bool StartThreadForInternalUpdates
PlusStatus ReadConfiguration(vtkXMLDataElement *config)
virtual PlusStatus InternalDisconnect() VTK_OVERRIDE
ChannelContainer OutputChannels
PlusStatus SetNumberOfScalarComponents(unsigned int numberOfScalarComponents)
enum vtkPlusDAQVideoSource::DATAMODE_E m_dataMode
CAMERADATATYPE_t * pImgBuf
virtual int GetNumberOfItems()
enum vtkPlusDAQVideoSource::CAMERAMODE_E cameraMode
bool CorrectlyConfigured
Interface to a 3D positioning tool, video source, or generalized data stream.