Fraxinus  2023.01.05-dev+develop.0da12
An IGT application
cxTestVideoSource.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include "cxTestVideoSource.h"
12 #include <QTimer>
13 #include <vtkImageImport.h>
14 #include <vtkImageData.h>
15 #include "cxData.h"
16 
17 namespace cx
18 {
19 
20 TestVideoSource::TestVideoSource(QString uid, QString name, int width, int height)
21 {
22  mUid = uid;
23  mName = name;
24  mConnected = false;
25  mStreaming = false;
26  mValidData = true;
27  mInitialized = false;
28  mFrames = 0;
29  mResolution = 1;
30  mWidth = width;
31  mHeight = height;
32  mImageImport = vtkImageImportPtr::New();
33  setResolution(mResolution);
34  mImageTimer = new QTimer(this);
35  connect(mImageTimer, SIGNAL(timeout()), this, SLOT(processBuffer()));
36  mBuffer = (uint8_t*)malloc(width * height * 3);
37 
38  mImageImport->SetDataScalarTypeToUnsignedChar();
39  mImageImport->SetNumberOfScalarComponents(3);
40  mImageImport->SetWholeExtent(0, mWidth - 1, 0, mHeight - 1, 0, 0);
41  mImageImport->SetDataExtentToWholeExtent();
42 }
43 
45 {
46  stop();
47  free(mBuffer);
48 }
49 
50 void TestVideoSource::setResolution(double resolution)
51 {
52  mResolution = resolution;
53  mImageImport->SetDataSpacing(mResolution, mResolution, 1);
54 }
55 
57 {
58  return mImageImport->GetOutput();
59 }
60 
62 {
63  if (mStreaming)
64  {
65  return;
66  }
67  if (!isConnected())
68  {
69  setConnected(true);
70  }
71  mStreaming = true;
72  mImageTimer->start(40);
73 }
74 
76 {
77  mImageTimer->stop();
78  mStreaming = false;
79 }
80 
81 static void TestImage(int width, int height, int frames, uint8_t *image, double mmPerPixel)
82 {
83  for (int x = 0; x < width; ++x)
84  {
85  for (int y = 0; y < height; ++y)
86  {
87  uint8_t *pix = &image[(y*width + x) * 3];
88  pix[0] = (x*255/width)+frames;
89  pix[1] = (y*255/height)+frames;
90  pix[2] = 127;
91  double mmPerPixel = 0.1;
92  if ( (int)(x * mmPerPixel) % 10 == 0)
93  {
94  uint8_t *pix = &image[(y*width + x) * 3];
95  pix[0] = 0;
96  pix[1] = 0;
97  pix[2] = 0;
98  }
99  }
100  }
101 }
102 
103 void TestVideoSource::processBuffer()
104 {
105  TestImage(mWidth, mHeight, mFrames, mBuffer, mResolution);
106  mFrames++;
107  mImageImport->SetImportVoidPointer(mBuffer);
108  mImageImport->Update();
109  mImageImport->Modified();
110  mInitialized = true;
111  emit newFrame();
112 }
113 
115 {
116  return (double) mImageImport->GetOutput()->GetMTime();
117 }
118 
120 {
121  TimeInfo retval;
122  retval.mAcquisitionTime.setMSecsSinceEpoch(this->getTimestamp());
123  return retval;
124 }
125 
126 } // namespace cx
virtual bool isConnected() const
return true when a connection to the data source is established.
virtual void setConnected(bool state)
virtual TimeInfo getAdvancedTimeInfo()
virtual void stop()
stop streaming
virtual double getTimestamp()
virtual void start()
start streaming
virtual vtkImageDataPtr getVtkImageData()
QDateTime mAcquisitionTime
Possibly modified time stamp.
Definition: cxData.h:45
TestVideoSource(QString uid, QString name, int width, int height)
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
void newFrame()
emitted when a new frame has arrived (getVtkImageData() returns something new). info/status/name/vali...
virtual void setResolution(double resolution)
Namespace for all CustusX production code.