16 #include "PlusConfigure.h" 17 #include "vtkObjectFactory.h" 20 #include "vtkUnsignedCharArray.h" 27 #pragma warning (push, 3) 30 #include "vtkWindows.h" 38 class vtkPlusWin32VideoSource2Internal
42 vtkPlusWin32VideoSource2Internal()
50 virtual ~vtkPlusWin32VideoSource2Internal()
52 delete [](
char*)(BitMapInfoPtr);
61 LOG_ERROR(
"Cannot get bitmap info, capture window has not been created yet");
64 int formatSize = capGetVideoFormatSize(CapWnd);
65 if (formatSize > this->BitMapInfoSize)
67 delete []((
char*)BitMapInfoPtr);
68 BitMapInfoPtr = (LPBITMAPINFO)
new char[formatSize];
69 BitMapInfoSize = formatSize;
71 if (!capGetVideoFormat(CapWnd, BitMapInfoPtr, formatSize))
73 LOG_ERROR(
"Cannot get bitmap info from capture window");
81 if (!capSetVideoFormat(CapWnd, BitMapInfoPtr, BitMapInfoSize))
83 LOG_ERROR(
"Cannot set bitmap video format for capture window");
92 CAPDRIVERCAPS CapDriverCaps;
93 CAPTUREPARMS CaptureParms;
94 LPBITMAPINFO BitMapInfoPtr;
100 #if ( _MSC_VER >= 1300 ) // Visual studio .NET 101 #pragma warning ( disable : 4311 ) 102 #pragma warning ( disable : 4312 ) 103 #define vtkGetWindowLong GetWindowLongPtr 104 #define vtkSetWindowLong SetWindowLongPtr 105 #define vtkGWL_USERDATA GWLP_USERDATA 106 #else // regular Visual studio 107 #define vtkGetWindowLong GetWindowLong 108 #define vtkSetWindowLong SetWindowLong 109 #define vtkGWL_USERDATA GWL_USERDATA 114 : Internal(new vtkPlusWin32VideoSource2Internal)
138 os << indent <<
"Preview: " << (this->
Preview ?
"On\n" :
"Off\n");
148 LOG_TRACE(
"WM_MOVE");
151 LOG_TRACE(
"WM_SIZE");
154 LOG_TRACE(
"WM_DESTROY");
155 self->OnParentWndDestroy();
158 LOG_TRACE(
"WM_CLOSE");
162 return DefWindowProc(hwnd,
message, wParam, lParam);
169 if (nState == CONTROLCALLBACK_PREROLL)
171 LOG_TRACE(
"controlcallback preroll");
173 else if (nState == CONTROLCALLBACK_CAPTURING)
175 LOG_TRACE(
"controlcallback capturing");
193 if (nID == IDS_CAP_BEGIN)
195 LOG_TRACE(
"start of capture");
197 if (nID == IDS_CAP_END)
199 LOG_TRACE(
"end of capture");
209 LOG_ERROR(
"Video for Windows error: #" << ErrID);
224 HINSTANCE hinstance = GetModuleHandle(NULL);
229 wc.hInstance = hinstance;
231 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
233 wc.lpszMenuName = NULL;
234 wc.hbrBackground = NULL;
235 wc.style = CS_HREDRAW | CS_VREDRAW;
236 wc.cbClsExtra =
sizeof(
void*);
239 std::string windowNameStr;
240 bool registrationSuccessful =
false;
241 const int MAX_WINDOW_CLASS_REGISTRATION_ATTEMPTS = 32;
242 for (
int i = 1;
i <= MAX_WINDOW_CLASS_REGISTRATION_ATTEMPTS;
i++)
244 if (RegisterClass(&wc))
247 registrationSuccessful =
true;
251 std::ostringstream windowName;
252 windowName <<
"VTKVideo " <<
i << std::ends;
253 windowNameStr = windowName.str();
254 wc.lpszClassName = windowNameStr.c_str();
256 if (!registrationSuccessful)
258 LOG_ERROR(
"Initialize: failed to register VTKVideo class (" << GetLastError() <<
")");
262 DWORD style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
273 LOG_ERROR(
"Unable to retrieve the video source in the Win32Video device.");
278 this->
Internal->ParentWnd = CreateWindow(this->
WndClassName,
"Plus video capture window", style, 0, 0,
279 frameSize[0] + 2 * GetSystemMetrics(SM_CXFIXEDFRAME),
280 frameSize[1] + 2 * GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYSIZE),
281 NULL, NULL, hinstance, NULL);
285 LOG_ERROR(
"Initialize: failed to create window (" << GetLastError() <<
")");
293 this->
Internal->CapWnd = capCreateCaptureWindow(
"Capture", WS_CHILD | WS_VISIBLE, 0, 0,
294 frameSize[0], frameSize[1], this->
Internal->ParentWnd, 1);
298 LOG_ERROR(
"Initialize: failed to create capture window (" << GetLastError() <<
")");
304 if (!capDriverConnect(this->
Internal->CapWnd, 0))
306 LOG_ERROR(
"Initialize: couldn't connect to driver (" << GetLastError() <<
")");
311 capDriverGetCaps(this->
Internal->CapWnd, &this->Internal->CapDriverCaps,
sizeof(CAPDRIVERCAPS));
314 this->
Internal->GetBitmapInfoFromCaptureDevice();
319 LOG_ERROR(
"Failed to set requested frame size in the capture device");
324 int width = this->
Internal->BitMapInfoPtr->bmiHeader.biWidth;
327 this->
Internal->ParentWnd = CreateWindow(this->
WndClassName,
"Plus video capture window", style, 0, 0,
328 width + 2 * GetSystemMetrics(SM_CXFIXEDFRAME),
329 height + 2 * GetSystemMetrics(SM_CYFIXEDFRAME) + GetSystemMetrics(SM_CYBORDER) + GetSystemMetrics(SM_CYSIZE),
330 NULL, NULL, hinstance, NULL);
334 LOG_ERROR(
"Initialize: failed to create window (" << GetLastError() <<
")");
342 this->
Internal->CapWnd = capCreateCaptureWindow(
"Capture", WS_CHILD | WS_VISIBLE, 0, 0,
343 frameSize[0], frameSize[1], this->
Internal->ParentWnd, 1);
347 LOG_ERROR(
"Initialize: failed to create capture window (" << GetLastError() <<
")");
353 if (!capDriverConnect(this->
Internal->CapWnd, 0))
355 LOG_ERROR(
"Initialize: couldn't connect to driver (" << GetLastError() <<
")");
360 capDriverGetCaps(this->
Internal->CapWnd, &this->Internal->CapDriverCaps,
sizeof(CAPDRIVERCAPS));
363 capCaptureGetSetup(this->
Internal->CapWnd, &this->Internal->CaptureParms,
sizeof(CAPTUREPARMS));
371 this->
Internal->CaptureParms.dwRequestMicroSecPerFrame = 0;
374 this->
Internal->CaptureParms.fMakeUserHitOKToCapture =
FALSE;
375 this->
Internal->CaptureParms.fYield = 1;
377 this->
Internal->CaptureParms.vKeyAbort = 0x00;
381 this->
Internal->CaptureParms.wNumAudioRequested = 0;
382 this->
Internal->CaptureParms.wPercentDropForError = 100;
383 this->
Internal->CaptureParms.dwAudioBufferSize = 0;
384 this->
Internal->CaptureParms.AVStreamMaster = AVSTREAMMASTER_NONE;
386 if (!capCaptureSetSetup(this->
Internal->CapWnd, &this->Internal->CaptureParms,
sizeof(CAPTUREPARMS)))
388 LOG_ERROR(
"Initialize: setup of capture parameters failed (" << GetLastError() <<
")");
394 if (!capSetUserData(this->
Internal->CapWnd, (LONG_PTR)
this))
396 LOG_ERROR(
"Initialize: couldn't set user data for callback (" << GetLastError() <<
")");
404 LOG_ERROR(
"Initialize: couldn't set control callback (" << GetLastError() <<
")");
412 LOG_ERROR(
"Initialize: couldn't set frame callback (" << GetLastError() <<
")");
419 LOG_ERROR(
"Initialize: couldn't set stream callback (" << GetLastError() <<
")");
426 LOG_ERROR(
"Initialize: couldn't set status callback (" << GetLastError() <<
")");
433 LOG_ERROR(
"Initialize: couldn't set error callback (" << GetLastError() <<
")");
451 LOG_DEBUG(
"capDriverDisconnect(this->Internal->CapWnd)");
452 capDriverDisconnect(this->
Internal->CapWnd);
453 LOG_DEBUG(
"DestroyWindow(this->Internal->CapWnd)");
454 DestroyWindow(this->
Internal->CapWnd);
460 UnregisterClass(this->
WndClassName, GetModuleHandle(NULL));
470 if (this->
Preview == showPreview)
479 if (this->
Internal->CapWnd == NULL || this->Internal->ParentWnd == NULL)
481 LOG_ERROR(
"Capture windows have not been intialized");
486 ShowWindow(this->
Internal->ParentWnd, SW_SHOWNORMAL);
490 ShowWindow(this->
Internal->ParentWnd, SW_HIDE);
502 DestroyWindow(this->
Internal->ParentWnd);
516 int inputCompression = this->
Internal->BitMapInfoPtr->bmiHeader.biCompression;
523 LOG_TRACE(
"Grabbed");
525 LPVIDEOHDR lpVHdr = static_cast<LPVIDEOHDR>(lpVideoHeader);
537 unsigned char* inputPixelsPtr = lpVHdr->lpData;
539 FrameSizeType outputFrameSize;
542 LOG_ERROR(
"Unable to retrieve frame size.");
548 LOG_ERROR(
"Error while decoding the grabbed image");
556 LOG_ERROR(
"Unable to retrieve the video source in the Win32Video device.");
559 double indexTime = aSource->
GetStartTime() + 0.001 * lpVHdr->dwTimeCaptured;
571 if (!capGrabFrameNoStop(this->
Internal->CapWnd))
573 LOG_ERROR(
"Initialize: failed to request a single frame grab (" << GetLastError() <<
")");
582 if (!capCaptureSequenceNoFile(this->
Internal->CapWnd))
584 LOG_ERROR(
"Initialize: failed to request continuous frame grabbing (" << GetLastError() <<
")");
596 if (!capCaptureStop(this->
Internal->CapWnd))
598 LOG_ERROR(
"Initialize: failed to request continuous frame grabbing (" << GetLastError() <<
")");
609 LOG_ERROR(
"vtkPlusWin32VideoSource2::VideoFormatDialog failed, need to connect to the device first");
619 capGetStatus(this->
Internal->CapWnd, &this->Internal->CapStatus,
sizeof(CAPSTATUS));
620 if (this->
Internal->CapStatus.fCapturingNow)
622 LOG_ERROR(
"vtkPlusWin32VideoSource2::VideoFormatDialog failed, can't alter video format while grabbing");
626 int success = capDlgVideoFormat(this->
Internal->CapWnd);
629 LOG_ERROR(
"vtkPlusWin32VideoSource2::VideoFormatDialog failed (" << GetLastError() <<
")");
641 LOG_ERROR(
"vtkPlusWin32VideoSource2::VideoSourceDialog failed, need to connect to the device first");
651 capGetStatus(this->
Internal->CapWnd, &this->Internal->CapStatus,
sizeof(CAPSTATUS));
652 if (this->
Internal->CapStatus.fCapturingNow)
654 LOG_ERROR(
"vtkPlusWin32VideoSource2::VideoFormatDialog failed, can't alter video source while grabbing");
658 int success = capDlgVideoSource(this->
Internal->CapWnd);
661 LOG_ERROR(
"vtkPlusWin32VideoSource2::VideoSourceDialog failed (" << GetLastError() <<
")");
673 LOG_ERROR(this->
GetDeviceId() <<
": Unable to retrieve video source.");
683 this->
Internal->GetBitmapInfoFromCaptureDevice();
684 this->
Internal->BitMapInfoPtr->bmiHeader.biWidth = frameSize[0];
685 this->
Internal->BitMapInfoPtr->bmiHeader.biHeight = frameSize[1];
688 LOG_ERROR(
"Failed to set requested frame size in the capture device");
708 capCaptureGetSetup(this->
Internal->CapWnd, &this->Internal->CaptureParms,
sizeof(CAPTUREPARMS));
715 this->
Internal->CaptureParms.dwRequestMicroSecPerFrame = 0;
717 capCaptureSetSetup(this->
Internal->CapWnd, &this->Internal->CaptureParms,
sizeof(CAPTUREPARMS));
728 unsigned int numberOfScalarComponents = 0;
732 numberOfScalarComponents = 4;
735 numberOfScalarComponents = 3;
738 numberOfScalarComponents = 1;
741 numberOfScalarComponents = 0;
742 LOG_ERROR(
"SetOutputFormat: Unrecognized color format.");
746 if (numberOfScalarComponents != 1)
748 LOG_ERROR(
"Currently only 1 component image output is supported. Requested " << numberOfScalarComponents <<
" components");
757 LOG_ERROR(
"Unable to retrieve the video source in the win32video device on channel " << (*it)->GetChannelId());
769 this->
Internal->GetBitmapInfoFromCaptureDevice();
773 LOG_ERROR(
"Failed to set requested frame size in the capture device");
784 this->
Internal->GetBitmapInfoFromCaptureDevice();
786 unsigned int width(this->
Internal->BitMapInfoPtr->bmiHeader.biWidth);
787 unsigned int height(this->
Internal->BitMapInfoPtr->bmiHeader.biHeight);
789 unsigned int numberOfScalarComponents = 1;
794 LOG_ERROR(
"Unable to access video source in vtkPlusWin32VideoSource2. Critical failure.");
813 LOG_WARNING(
"Win32VideoSource is expecting one output channel and there are " << this->
OutputChannels.size() <<
" channels. First output channel will be used.");
819 LOG_ERROR(
"No output channels defined for win32 video source. Cannot proceed.");
virtual PlusStatus SetOutputFormat(int format)
virtual PlusStatus SetInputFrameSize(vtkPlusDataSource &aSource, unsigned int x, unsigned int y, unsigned int z)
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
vtkPlusWin32VideoSource2()
PlusStatus VideoFormatDialog()
virtual PlusStatus InternalStopRecording()
LRESULT PASCAL vtkPlusWin32VideoSource2CapControlProc(HWND hwndC, int nState)
Video-for-Windows video digitizer.
virtual PlusStatus SetAcquisitionRate(double rate)
virtual std::string GetDeviceId() const
PlusStatus SetInputFrameSize(unsigned int x, unsigned int y, unsigned int z)
virtual PlusStatus AddItem(vtkImageData *frame, US_IMAGE_ORIENTATION usImageOrientation, US_IMAGE_TYPE imageType, long frameNumber, double unfilteredTimestamp=UNDEFINED_TIMESTAMP, double filteredTimestamp=UNDEFINED_TIMESTAMP, const igsioFieldMapType *customFields=NULL)
LRESULT PASCAL vtkPlusWin32VideoSource2ErrorCallbackProc(HWND hwndC, int ErrID, LPSTR lpErrorText)
static bool IsConvertToGraySupported(int inputCompression)
bool RequireImageOrientationInConfiguration
virtual PlusStatus InternalUpdate()
PlusStatus SetPixelType(igsioCommon::VTKScalarPixelType pixelType)
virtual PlusStatus NotifyConfigured()
~vtkPlusWin32VideoSource2()
PlusStatus GetFirstVideoSource(vtkPlusDataSource *&anImage)
virtual double GetStartTime()
LRESULT PASCAL vtkPlusWin32VideoSource2StatusCallbackProc(HWND vtkNotUsed(hwndC), int nID, LPCSTR vtkNotUsed(lpsz))
virtual PlusStatus Disconnect()
LONG FAR PASCAL vtkPlusWin32VideoSource2WinProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
virtual PlusStatus SetFrameSize(const FrameSizeType &frameSize)
static std::string GetCompressionModeAsString(int inputCompression)
virtual PlusStatus InternalDisconnect()
PlusStatus AddFrameToBuffer(void *lpVideoHeader)
virtual PlusStatus InternalStartRecording()
void ReleaseSystemResources()
PhidgetLCD_Font int * width
igsioVideoFrame UncompressedVideoFrame
LRESULT PASCAL vtkPlusWin32VideoSource2CallbackProc(HWND hwndC, LPVIDEOHDR lpVideoHeader)
void SetPreview(int showPreview)
virtual US_IMAGE_ORIENTATION GetInputImageOrientation()
void OnParentWndDestroy()
static PlusStatus ConvertToGray(int inputCompression, int width, int height, unsigned char *s, unsigned char *d)
PhidgetLCD_Font int int * height
virtual int GetConnected() const
vtkStandardNewMacro(vtkPlusWin32VideoSource2)
ChannelContainer OutputChannels
PlusStatus SetNumberOfScalarComponents(unsigned int numberOfScalarComponents)
PlusStatus VideoSourceDialog()
PlusStatus UpdateFrameBuffer()
ChannelContainer::iterator ChannelContainerIterator
virtual PlusStatus InternalConnect()
FrameSizeType GetInputFrameSize() const
virtual void SetWndClassName(const char *)
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
vtkPlusWin32VideoSource2Internal * Internal
Interface to a 3D positioning tool, video source, or generalized data stream.