1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
/*=========================================================================
Program: Image Guided Surgery Software Toolkit
Module: $RCSfile: igstkWebcamWinVideoImager.cxx,v $
Language: C++
Date: $Date: 2009-06-18 20:05:14 $
Version: $Revision: 1.2 $
Copyright (c) ISC Insight Software Consortium. All rights reserved.
See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
// Warning about: identifier was truncated to '255' characters in the
// debug information (MVC6.0 Debug)
#pragma warning( disable : 4786 )
#endif
#include "igstkWebcamWinVideoImager.h"
#include "igstkEvents.h"
#include "vtkImageData.h"
#include <itksys/SystemTools.hxx>
#include "vtkWin32VideoSource.h"
#include "vtkImageViewer2.h"
#include "vtkImageData.h"
#include "vtkImageIterator.h"
#include <vtkPNGWriter.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "itkImage.h"
#include "itkImportImageFilter.h"
#include "itkImageFileWriter.h"
#include "itkRGBPixel.h"
#define MAX_DEVICES 64
#define MAX_FORMATS 64
namespace igstk
{
//Initialize static variables
itk::MutexLock::Pointer WebcamWinVideoImager::m_FrameBufferLock =
itk::MutexLock::New();
/** Constructor: Initializes all internal variables. */
WebcamWinVideoImager::WebcamWinVideoImager(void):m_StateMachine(this)
{
this->m_NumberOfTools = 0;
this->SetThreadingEnabled( true );
// Lock for the data buffer that is used to transfer the
// frames from thread that is communicating
// with the imager to the main thread.
m_BufferLock = itk::MutexLock::New();
this->m_Capture = 0;
this->m_Cvframe = 0;
}
/** Destructor */
WebcamWinVideoImager::~WebcamWinVideoImager(void)
{
cvReleaseCapture( &m_Capture );
}
WebcamWinVideoImager::ResultType WebcamWinVideoImager::InternalOpen( void )
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::InternalOpen called ...\n");
if( ! this->Initialize() )
{
igstkLogMacro( CRITICAL, "Error initializing");
return FAILURE;
}
return SUCCESS;
}
/** Initialize socket */
bool WebcamWinVideoImager::Initialize( void )
{
igstkLogMacro( DEBUG, "igstk::WebcamWinVideoImager::Initialize called ...\n");
m_Capture = cvCaptureFromCAM( 0 );
if ( !m_Capture )
{
fprintf( stderr, "Cannot open initialize webcam!\n" );
return 1;
}
return SUCCESS;
}
/** Verify imager tool information. */
WebcamWinVideoImager::ResultType
WebcamWinVideoImager
::VerifyVideoImagerToolInformation( const VideoImagerToolType * imagerTool )
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::VerifyVideoImagerToolInformation called ...\n");
return SUCCESS;
}
/** Detach camera. */
WebcamWinVideoImager::ResultType WebcamWinVideoImager::InternalClose( void )
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::InternalClose called ...\n");
cvReleaseCapture( &m_Capture );
return SUCCESS;
}
/** Put the imaging device into imaging mode. */
WebcamWinVideoImager::ResultType
WebcamWinVideoImager::InternalStartImaging( void )
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::InternalStartImaging called ...\n");
igstk::DoubleTypeEvent evt;
evt.Set( 1.0 );
this->InvokeEvent( evt );
igstkLogMacro( DEBUG, "Start capture on device!");
return SUCCESS;
}
/** Take the imaging device out of imaging mode. */
WebcamWinVideoImager::ResultType
WebcamWinVideoImager::InternalStopImaging( void )
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::InternalStopImaging called ...\n");
/*
Stop the device
*/
return SUCCESS;
}
/** Reset the imaging device to put it back to its original state. */
WebcamWinVideoImager::ResultType WebcamWinVideoImager::InternalReset( void )
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::InternalReset called ...\n");
return SUCCESS;
}
/** Update the status and the transforms for all VideoImagerTools. */
WebcamWinVideoImager::ResultType WebcamWinVideoImager::InternalUpdateStatus()
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::InternalUpdateStatus called ...\n");
// This method and the InternalThreadedUpdateStatus are both called
// continuously in the Imaging state. This method is called from
// the main thread, while InternalThreadedUpdateStatus is called
// from the thread that actually communicates with the device.
// A shared memory buffer is used to transfer information between
// the threads, and it must be locked when either thread is
// accessing it.
m_BufferLock->Lock();
typedef VideoImagerToolFrameContainerType::const_iterator InputConstIterator;
InputConstIterator inputItr = this->m_ToolFrameBuffer.begin();
InputConstIterator inputEnd = this->m_ToolFrameBuffer.end();
VideoImagerToolsContainerType imagerToolContainer =
this->GetVideoImagerToolContainer();
unsigned int toolId = 0;
while( inputItr != inputEnd )
{
// only report tools that have useful data
if (! this->m_ToolStatusContainer[inputItr->first])
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::InternalUpdateStatus: " <<
"tool " << inputItr->first << " failed\n");
// report to the imager tool that the imager is not available
this->ReportImagingToolNotAvailable(
imagerToolContainer[inputItr->first]);
++inputItr;
continue;
}
// report to the imager tool that the tool is sending frames
this->ReportImagingToolStreaming(imagerToolContainer[inputItr->first]);
this->SetVideoImagerToolFrame(
imagerToolContainer[inputItr->first], (inputItr->second) );
this->SetVideoImagerToolUpdate(
imagerToolContainer[inputItr->first], true );
++inputItr;
++toolId;
}
m_BufferLock->Unlock();
return SUCCESS;
}
/**Update the shared memory buffer and the tool's internal frame. This
* function is called by the thread that communicates with the imager while
* the imager is in the Imaging state. */
WebcamWinVideoImager::ResultType
WebcamWinVideoImager::InternalThreadedUpdateStatus( void )
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::InternalThreadedUpdateStatus called ...\n");
cout << " ";
// Lock the buffer that this method shares with InternalUpdateStatus
m_BufferLock->Lock();
//reset the status of all the imager tools
typedef VideoImagerToolFrameContainerType::const_iterator InputConstIterator;
InputConstIterator inputItr = this->m_ToolFrameBuffer.begin();
InputConstIterator inputEnd = this->m_ToolFrameBuffer.end();
while( inputItr != inputEnd )
{
this->m_ToolStatusContainer[inputItr->first] = 0;
++inputItr;
}
try
{
igstkLogMacro( DEBUG, "InternalThreadedUpdateStatus Receive passed" );
// Check if an imager tool was added with this device name
typedef VideoImagerToolFrameContainerType::iterator InputIterator;
InputIterator deviceItr = this->m_ToolFrameBuffer.find( "Camera" );
if( deviceItr != this->m_ToolFrameBuffer.end() )
{
// create the frame
VideoImagerToolsContainerType imagerToolContainer =
this->GetVideoImagerToolContainer();
FrameType* frame = new FrameType();
frame = this->GetVideoImagerToolFrame(
imagerToolContainer[deviceItr->first]);
unsigned int frameDims[3];
imagerToolContainer[deviceItr->first]->GetFrameDimensions(frameDims);
WebcamWinVideoImager::m_FrameBufferLock->Lock();
m_Cvframe = cvQueryFrame( m_Capture );
if( !m_Cvframe )
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::InternalThreadedUpdateStatus: "
<< "Frame failed");
memcpy(frame->GetImagePtr(),
(unsigned char*)m_Cvframe->imageData,
frameDims[0]*frameDims[1]*frameDims[2]);
WebcamWinVideoImager::m_FrameBufferLock->Unlock();
//update frame validity time
frame->SetTimeToExpiration(this->GetValidityTime());
this->m_ToolFrameBuffer[ deviceItr->first ] = frame;
this->m_ToolStatusContainer[ deviceItr->first ] = 1;
}
m_BufferLock->Unlock();
return SUCCESS;
}
catch(...)
{
igstkLogMacro( CRITICAL, "Unknown error catched" );
m_BufferLock->Unlock();
return FAILURE;
}
}
WebcamWinVideoImager::ResultType
WebcamWinVideoImager::
AddVideoImagerToolToInternalDataContainers(
const VideoImagerToolType * imagerTool )
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::AddVideoImagerToolToInternalDataContainers "
"called ...\n");
if ( imagerTool == NULL )
{
return FAILURE;
}
const std::string imagerToolIdentifier =
imagerTool->GetVideoImagerToolIdentifier();
igstk::Frame* frame = new igstk::Frame();
this->m_ToolFrameBuffer[ imagerToolIdentifier ] = frame;
this->m_ToolStatusContainer[ imagerToolIdentifier ] = 0;
return SUCCESS;
}
WebcamWinVideoImager::ResultType
WebcamWinVideoImager::
RemoveVideoImagerToolFromInternalDataContainers
( const VideoImagerToolType * imagerTool )
{
igstkLogMacro( DEBUG,
"igstk::WebcamWinVideoImager::RemoveVideoImagerToolFromInternalDataContainers"
<< "called ...\n");
const std::string imagerToolIdentifier =
imagerTool->GetVideoImagerToolIdentifier();
// remove the tool from the frame buffer and status container
this->m_ToolStatusContainer.erase( imagerToolIdentifier );
this->m_ToolFrameBuffer.erase( imagerToolIdentifier );
return SUCCESS;
}
/**The "ValidateSpecifiedFrequency" method checks if the specified
* frequency is valid for the imaging device that is being used. */
WebcamWinVideoImager::ResultType
WebcamWinVideoImager::ValidateSpecifiedFrequency( double frequencyInHz )
{
const double MAXIMUM_FREQUENCY = 30;
if ( frequencyInHz < 0.0 || frequencyInHz > MAXIMUM_FREQUENCY )
{
return FAILURE;
}
return SUCCESS;
}
/** Print Self function */
void WebcamWinVideoImager::PrintSelf( std::ostream& os, itk::Indent indent )
const
{
Superclass::PrintSelf(os, indent);
os << indent << " output Imaging Source Converter parameters " << std::endl;
}
} // end of namespace igstk
|