NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 #include "cxTestVideoSource.h"
33 #include <QTimer>
34 #include <vtkImageImport.h>
35 #include <vtkImageData.h>
36 #include "cxData.h"
37 
38 namespace cx
39 {
40 
41 TestVideoSource::TestVideoSource(QString uid, QString name, int width, int height)
42 {
43  mUid = uid;
44  mName = name;
45  mConnected = false;
46  mStreaming = false;
47  mValidData = true;
48  mInitialized = false;
49  mFrames = 0;
50  mResolution = 1;
51  mWidth = width;
52  mHeight = height;
53  mImageImport = vtkImageImportPtr::New();
54  setResolution(mResolution);
55  mImageTimer = new QTimer(this);
56  connect(mImageTimer, SIGNAL(timeout()), this, SLOT(processBuffer()));
57  mBuffer = (uint8_t*)malloc(width * height * 3);
58 
59  mImageImport->SetDataScalarTypeToUnsignedChar();
60  mImageImport->SetNumberOfScalarComponents(3);
61  mImageImport->SetWholeExtent(0, mWidth - 1, 0, mHeight - 1, 0, 0);
62  mImageImport->SetDataExtentToWholeExtent();
63 }
64 
66 {
67  stop();
68  free(mBuffer);
69 }
70 
71 void TestVideoSource::setResolution(double resolution)
72 {
73  mResolution = resolution;
74  mImageImport->SetDataSpacing(mResolution, mResolution, 1);
75 }
76 
78 {
79  return mImageImport->GetOutput();
80 }
81 
83 {
84  if (mStreaming)
85  {
86  return;
87  }
88  if (!isConnected())
89  {
90  setConnected(true);
91  }
92  mStreaming = true;
93  mImageTimer->start(40);
94 }
95 
97 {
98  mImageTimer->stop();
99  mStreaming = false;
100 }
101 
102 static void TestImage(int width, int height, int frames, uint8_t *image, double mmPerPixel)
103 {
104  for (int x = 0; x < width; ++x)
105  {
106  for (int y = 0; y < height; ++y)
107  {
108  uint8_t *pix = &image[(y*width + x) * 3];
109  pix[0] = (x*255/width)+frames;
110  pix[1] = (y*255/height)+frames;
111  pix[2] = 127;
112  double mmPerPixel = 0.1;
113  if ( (int)(x * mmPerPixel) % 10 == 0)
114  {
115  uint8_t *pix = &image[(y*width + x) * 3];
116  pix[0] = 0;
117  pix[1] = 0;
118  pix[2] = 0;
119  }
120  }
121  }
122 }
123 
124 void TestVideoSource::processBuffer()
125 {
126  TestImage(mWidth, mHeight, mFrames, mBuffer, mResolution);
127  mFrames++;
128  mImageImport->SetImportVoidPointer(mBuffer);
129  mImageImport->Update();
130  mImageImport->Modified();
131  mInitialized = true;
132  emit newFrame();
133 }
134 
136 {
137  return (double) mImageImport->GetOutput()->GetMTime();
138 }
139 
141 {
142  TimeInfo retval;
143  retval.mAcquisitionTime.setMSecsSinceEpoch(this->getTimestamp());
144  return retval;
145 }
146 
147 } // 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:65
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)