CustusX  15.4.0-beta
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 
37 namespace cx
38 {
39 
40 TestVideoSource::TestVideoSource(QString uid, QString name, int width, int height)
41 {
42  mUid = uid;
43  mName = name;
44  mConnected = false;
45  mStreaming = false;
46  mValidData = true;
47  mInitialized = false;
48  mFrames = 0;
49  mResolution = 1;
50  mWidth = width;
51  mHeight = height;
52  mImageImport = vtkImageImportPtr::New();
53  setResolution(mResolution);
54  mImageTimer = new QTimer(this);
55  connect(mImageTimer, SIGNAL(timeout()), this, SLOT(processBuffer()));
56  mBuffer = (uint8_t*)malloc(width * height * 3);
57 
58  mImageImport->SetDataScalarTypeToUnsignedChar();
59  mImageImport->SetNumberOfScalarComponents(3);
60  mImageImport->SetWholeExtent(0, mWidth - 1, 0, mHeight - 1, 0, 0);
61  mImageImport->SetDataExtentToWholeExtent();
62 }
63 
65 {
66  stop();
67  free(mBuffer);
68 }
69 
70 void TestVideoSource::setResolution(double resolution)
71 {
72  mResolution = resolution;
73  mImageImport->SetDataSpacing(mResolution, mResolution, 1);
74 }
75 
77 {
78  return mImageImport->GetOutput();
79 }
80 
82 {
83  if (mStreaming)
84  {
85  return;
86  }
87  if (!isConnected())
88  {
89  setConnected(true);
90  }
91  mStreaming = true;
92  mImageTimer->start(40);
93 }
94 
96 {
97  mImageTimer->stop();
98  mStreaming = false;
99 }
100 
101 static void TestImage(int width, int height, int frames, uint8_t *image, double mmPerPixel)
102 {
103  for (int x = 0; x < width; ++x)
104  {
105  for (int y = 0; y < height; ++y)
106  {
107  uint8_t *pix = &image[(y*width + x) * 3];
108  pix[0] = (x*255/width)+frames;
109  pix[1] = (y*255/height)+frames;
110  pix[2] = 127;
111  double mmPerPixel = 0.1;
112  if ( (int)(x * mmPerPixel) % 10 == 0)
113  {
114  uint8_t *pix = &image[(y*width + x) * 3];
115  pix[0] = 0;
116  pix[1] = 0;
117  pix[2] = 0;
118  }
119  }
120  }
121 }
122 
123 void TestVideoSource::processBuffer()
124 {
125  TestImage(mWidth, mHeight, mFrames, mBuffer, mResolution);
126  mFrames++;
127  mImageImport->SetImportVoidPointer(mBuffer);
128  mImageImport->Update();
129  mImageImport->Modified();
130  mInitialized = true;
131  emit newFrame();
132 }
133 
135 {
136  return (double) mImageImport->GetOutput()->GetMTime();
137 }
138 
139 } // namespace cx
virtual bool isConnected() const
return true when a connection to the data source is established.
virtual void setConnected(bool state)
virtual void stop()
stop streaming
virtual double getTimestamp()
virtual void start()
start streaming
virtual vtkImageDataPtr getVtkImageData()
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)