41 #include <QHostAddress>
42 #include "igtlOSUtil.h"
43 #include "igtlImageMessage.h"
44 #include "igtlServerSocket.h"
45 #include "vtkImageData.h"
46 #include "vtkSmartPointer.h"
47 #include "vtkMetaImageReader.h"
48 #include "vtkImageImport.h"
49 #include "vtkLookupTable.h"
50 #include "vtkImageMapToColors.h"
51 #include "vtkMetaImageWriter.h"
63 ImageStreamerSonix::ImageStreamerSonix() :
64 mEmitStatusMessage(false),
65 mLastFrameTimestamp(0.0),
66 mCurrentFrameTimestamp(0.0),
74 QString ImageStreamerSonix::getType()
79 QStringList ImageStreamerSonix::getArgumentDescription()
82 retval <<
"--ipaddress: IP address to connect to, default=127.0.0.1 (localhost)";
83 retval <<
"--imagingmode: default=2 (0 = B-mode, 2 = Color)";
84 retval <<
"--datatype: Video type, default=0x008 (4 = gray, 8 = color)";
85 retval <<
"--buffersize: Grabber buffer size, default=500";
86 retval <<
"--properties: dump image properties";
87 retval <<
"--debug: Debug output";
88 retval <<
"--delay: Delay (sec) before connecting to Sonix (first time), default=80";
92 ImageStreamerSonix::~ImageStreamerSonix()
97 mSonixGrabber->Stop();
98 std::cout <<
"Releasing Ultrasonix resources" << std::endl;
99 mSonixGrabber->ReleaseSystemResources();
104 void ImageStreamerSonix::initialize(
StringMap arguments)
106 std::cout <<
"Creating sender type Sonix" << std::endl;
113 qRegisterMetaType<Frame>(
"Frame");
115 connect(
this, SIGNAL(imageOnQueue(
int)),
this, SLOT(sendOpenIGTLinkImageSlot(
int)), Qt::QueuedConnection);
116 connect(
this, SIGNAL(statusOnQueue(
int)),
this, SLOT(sendOpenIGTLinkStatusSlot(
int)), Qt::QueuedConnection);
118 connect(mSendTimer, SIGNAL(timeout()),
this, SLOT(initializeSonixSlot()));
119 this->setSendInterval(10000);
120 mSendTimer->setInterval(getSendInterval());
123 this->initializeSonixGrabber();
126 void ImageStreamerSonix::initializeSonixSlot()
128 if(!mSonixGrabber->IsInitialized())
130 mSonixGrabber->Initialize();
135 if(!mSonixGrabber->getFreezeState() &&
similar(mLastFrameTimestamp, mCurrentFrameTimestamp, 0.001))
137 std::cout <<
"initializeSonixSlot() Got no new frame. Reinitializing..." << std::endl;
140 mSonixGrabber->Stop();
141 std::cout <<
"Releasing Ultrasonix resources" << std::endl;
142 mSonixGrabber->ReleaseSystemResources();
143 disconnect(mSonixHelper, SIGNAL(frame(Frame&)),
this, SLOT(receiveFrameSlot(Frame&)));
145 this->initializeSonixGrabber();
149 mLastFrameTimestamp = mCurrentFrameTimestamp;
153 void ImageStreamerSonix::initializeSonixGrabber()
155 if (!mArguments.count(
"ipaddress"))
156 mArguments[
"ipaddress"] =
"127.0.0.1";
157 if (!mArguments.count(
"imagingmode"))
158 mArguments[
"imagingmode"] =
"0";
159 if (!mArguments.count(
"datatype"))
160 mArguments[
"datatype"] =
"0x00000008";
161 if (!mArguments.count(
"buffersize"))
162 mArguments[
"buffersize"] =
"500";
163 if (!mArguments.count(
"delay"))
164 mArguments[
"buffersize"] =
"80";
166 QString ipaddress = mArguments[
"ipaddress"];
171 bool useSharedMemory =
false;
172 if (ipaddress.contains(
"127.0.0.1"))
173 useSharedMemory =
true;
176 if (mArguments.count(
"debug"))
177 mSonixGrabber->setDebugOutput(
true);
179 mSonixGrabber->setDebugOutput(
false);
180 mSonixGrabber->setSonixConnectionDelay(delay);
181 mSonixGrabber->SetSonixIP(ipaddress.toStdString().c_str());
182 mSonixGrabber->SetImagingMode(imagingMode);
183 mSonixGrabber->SetAcquisitionDataType(acquisitionDataType);
184 mSonixGrabber->SetFrameBufferSize(bufferSize);
185 mSonixGrabber->setUseSharedMemory(useSharedMemory);
186 mSonixGrabber->Initialize();
194 mSonixGrabber->setSonixHelper(this->mSonixHelper);
195 connect(mSonixHelper, SIGNAL(frame(Frame&)),
this, SLOT(receiveFrameSlot(Frame&)), Qt::DirectConnection);
198 bool ImageStreamerSonix::startStreaming(
SenderPtr sender)
201 mSonixGrabber->Record();
202 mEmitStatusMessage =
true;
203 std::cout <<
"Started streaming from sonix device" << std::endl;
207 void ImageStreamerSonix::stopStreaming()
209 mSonixGrabber->Stop();
213 void ImageStreamerSonix::receiveFrameSlot(Frame& frame)
217 if (!mSender || !mSender->isReady())
227 this->addStatusMessageToQueue(statMsg);
231 mEmitStatusMessage =
false;
234 IGTLinkImageMessage::Pointer imgMsg = convertFrame(frame);
237 this->addImageToQueue(imgMsg);
255 QString probeFile =
"C:/Program Files/Ultrasonix/Exam/config/probes.xml";
259 std::cout <<
"Failed to initialize probe xml reader" << std::endl;
262 QDomNode probeNode = reader.getProbeNode(frame.
probeName.c_str());
263 long radius = reader.getProbeParam(probeNode,
"radius") / 1000;
264 long probeLenght = reader.getProbeLenght(probeNode) / 1000;
269 double widthReductionFactor = 0.98;
273 probeWidth = probeLenght;
275 if(reader.isProbeLinear(probeNode))
277 retval->SetProbeType(2);
280 retval->SetWidth(probeWidth);
284 retval->SetProbeType(1);
285 retval->SetWidth(probeWidth/radius);
286 double depthStart = radius;
287 double depthStartIncreaseFactor = 1.01;
288 double depthEndReductionFactor = 0.99;
289 retval->SetDepthStart(depthStart * depthStartIncreaseFactor);
290 retval->SetDepthEnd((depthStart + frame.
mImagingDepth) * depthEndReductionFactor);
293 retval->SetDeviceName(createDeviceName().c_str());
297 std::string ImageStreamerSonix::createDeviceName()
299 std::string retval =
"Sonix[BGRX]";
303 IGTLinkImageMessage::Pointer ImageStreamerSonix::convertFrame(Frame& frame)
305 IGTLinkImageMessage::Pointer retval = IGTLinkImageMessage::New();
309 int offset[] = {0, 0, 0};
312 retval->SetDimensions(size);
316 retval->SetDeviceName(createDeviceName().c_str());
318 retval->SetSubVolume(size,offset);
319 retval->AllocateScalars();
321 igtl::TimeStamp::Pointer ts;
322 ts = igtl::TimeStamp::New();
324 ts->SetTime(seconds);
325 retval->SetTimeStamp(ts);
327 igtl::Matrix4x4 matrix;
328 matrix[0][0] = 1.0; matrix[1][0] = 0.0; matrix[2][0] = 0.0; matrix[3][0] = 0.0;
329 matrix[0][1] = 0.0; matrix[1][1] = 1.0; matrix[2][1] = 0.0; matrix[3][1] = 0.0;
330 matrix[0][2] = 0.0; matrix[1][2] = 0.0; matrix[2][2] = 1.0; matrix[3][2] = 0.0;
331 matrix[0][3] = 0.0; matrix[1][3] = 0.0; matrix[2][3] = 0.0; matrix[3][3] = 1.0;
332 retval->SetMatrix(matrix);
336 int fsize = retval->GetImageSize();
337 memcpy(retval->GetScalarPointer(), frame.
mFirstPixel, fsize);
351 void ImageStreamerSonix::sendOpenIGTLinkImageSlot(
int sendNumberOfMessages)
353 if (!mSender || !mSender->isReady())
361 for(
int i=0; i<sendNumberOfMessages; ++i)
363 IGTLinkImageMessage::Pointer message = this->getLastImageMessageFromQueue();
367 package->mIgtLinkImageMessage = message;
368 mSender->send(package);
373 void ImageStreamerSonix::sendOpenIGTLinkStatusSlot(
int sendNumberOfMessage)
375 if (!this->isReadyToSend())
384 for(
int i=0; i<sendNumberOfMessage; ++i)
391 package->mIgtLinkUSStatusMessage = message;
392 mSender->send(package);
400 void ImageStreamerSonix::addImageToQueue(IGTLinkImageMessage::Pointer msg)
402 QMutexLocker sentry(&mImageMutex);
403 if(mMutexedImageMessageQueue.size() > mMaxqueueInfo)
405 mMutexedImageMessageQueue.pop_front();
409 mMutexedImageMessageQueue.push_back(msg);
410 int size = mMutexedImageMessageQueue.size();
413 emit queueInfo(size, mDroppedImages);
414 emit imageOnQueue(size);
419 IGTLinkImageMessage::Pointer ImageStreamerSonix::getLastImageMessageFromQueue()
421 QMutexLocker sentry(&mImageMutex);
422 if (mMutexedImageMessageQueue.empty())
423 return IGTLinkImageMessage::Pointer();
424 IGTLinkImageMessage::Pointer retval = mMutexedImageMessageQueue.front();
425 mMutexedImageMessageQueue.pop_front();
433 QMutexLocker sentry(&mStatusMutex);
434 if(mMutexedStatusMessageQueue.size() > mMaxqueueInfo)
436 mMutexedStatusMessageQueue.pop_front();
439 mMutexedStatusMessageQueue.push_back(msg);
440 int size = mMutexedStatusMessageQueue.size();
442 emit statusOnQueue(size);
449 QMutexLocker sentry(&mStatusMutex);
450 if (mMutexedStatusMessageQueue.empty())
453 mMutexedStatusMessageQueue.pop_front();
double mTimestamp
Timestamp in seconds since 1/1/1970 (epoch)
Support Qt support for vtkSonixVideoSource.
int mHeight
Height in pixels.
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
std::map< QString, QString > StringMap
int mWidth
Width in pixels.
int mPixelFormat
Pixel format in OSType (FourCC)
boost::shared_ptr< struct Package > PackagePtr
unsigned char * mFirstPixel
Pointer to first pixel in frame.
int convertStringWithDefault(QString text, int def)
igtl::SmartPointer< Self > Pointer
boost::shared_ptr< Sender > SenderPtr
virtual void initialize(StringMap arguments)
static vtkSonixVideoSource * New()