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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkWin32VideoSource.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/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 notice for more information.
=========================================================================*/
#include "vtkWin32VideoSource.h"
#include "vtkCriticalSection.h"
#include "vtkObjectFactory.h"
#include "vtkTimerLog.h"
#include "vtkUnsignedCharArray.h"
#include <ctype.h>
// because of warnings in windows header push and pop the warning level
#ifdef _MSC_VER
#pragma warning (push, 3)
#endif
#include "vtkWindows.h"
#include <winuser.h>
#include <vfw.h>
#ifdef _MSC_VER
#pragma warning (pop)
#endif
class vtkWin32VideoSourceInternal
{
public:
vtkWin32VideoSourceInternal() {}
HWND CapWnd;
HWND ParentWnd;
CAPSTATUS CapStatus;
CAPDRIVERCAPS CapDriverCaps;
CAPTUREPARMS CaptureParms;
LPBITMAPINFO BitMapPtr;
};
// VFW compressed formats are listed at http://www.webartz.com/fourcc/
#define VTK_BI_UYVY 0x59565955
vtkStandardNewMacro(vtkWin32VideoSource);
//----------------------------------------------------------------------------
vtkWin32VideoSource::vtkWin32VideoSource()
{
this->Internal = new vtkWin32VideoSourceInternal;
this->Initialized = 0;
this->FrameRate = 30;
this->OutputFormat = VTK_RGB;
this->NumberOfScalarComponents = 3;
this->FrameBufferBitsPerPixel = 24;
this->FlipFrames = 0;
this->FrameBufferRowAlignment = 4;
this->Internal->CapWnd = NULL;
this->Internal->ParentWnd = NULL;
this->BitMapSize = 0;
this->Internal->BitMapPtr = NULL;
this->WndClassName[0] = '\0';
this->Preview = 0;
}
//----------------------------------------------------------------------------
vtkWin32VideoSource::~vtkWin32VideoSource()
{
this->vtkWin32VideoSource::ReleaseSystemResources();
if (this->Internal->BitMapPtr != NULL)
{
delete [] (char *)(this->Internal->BitMapPtr);
}
this->Internal->BitMapPtr = NULL;
this->BitMapSize = 0;
delete this->Internal;
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Preview: " << (this->Preview ? "On\n" : "Off\n");
}
//----------------------------------------------------------------------------
// This is empty for now because we aren't displaying the capture window
LONG FAR PASCAL
vtkWin32VideoSourceWinProc(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
vtkWin32VideoSource *self = (vtkWin32VideoSource *)\
(vtkGetWindowLong(hwnd,vtkGWL_USERDATA));
switch(message) {
case WM_MOVE:
//cerr << "WM_MOVE\n";
break;
case WM_SIZE:
//cerr << "WM_SIZE\n";
break;
case WM_DESTROY:
//cerr << "WM_DESTROY\n";
self->OnParentWndDestroy();
break;
case WM_CLOSE:
//cerr << "WM_CLOSE\n";
self->PreviewOff();
return 0;
}
return(DefWindowProc(hwnd, message, wParam, lParam));
}
//----------------------------------------------------------------------------
LRESULT PASCAL vtkWin32VideoSourceCapControlProc(HWND hwndC, int nState)
{
vtkWin32VideoSource *self = (vtkWin32VideoSource *)(capGetUserData(hwndC));
if (nState == CONTROLCALLBACK_PREROLL)
{
//cerr << "controlcallback preroll\n";
self->SetStartTimeStamp(vtkTimerLog::GetUniversalTime());
}
else if (nState == CONTROLCALLBACK_CAPTURING)
{
//cerr << "controlcallback capturing\n";
}
return TRUE;
}
//----------------------------------------------------------------------------
LRESULT PASCAL vtkWin32VideoSourceCallbackProc(HWND hwndC, LPVIDEOHDR lpVHdr)
{
vtkWin32VideoSource *self = (vtkWin32VideoSource *)(capGetUserData(hwndC));
self->LocalInternalGrab(lpVHdr);
return 0;
}
//----------------------------------------------------------------------------
// this callback is left in for debug purposes
LRESULT PASCAL vtkWin32VideoSourceStatusCallbackProc(HWND vtkNotUsed(hwndC),
int nID,
LPCSTR vtkNotUsed(lpsz))
{
//vtkWin32VideoSource *self = (vtkWin32VideoSource *)(capGetUserData(hwndC));
if (nID == IDS_CAP_BEGIN)
{
//cerr << "start of capture\n";
}
if (nID == IDS_CAP_END)
{
//cerr << "end of capture\n";
}
return 1;
}
//----------------------------------------------------------------------------
LRESULT PASCAL vtkWin32VideoSourceErrorCallbackProc(HWND hwndC,
int ErrID,
LPSTR lpErrorText)
{
if (ErrID)
{
char buff[84];
sprintf(buff,"Error# %d",ErrID);
MessageBox(hwndC,lpErrorText, buff, MB_OK | MB_ICONEXCLAMATION);
//vtkGenericWarningMacro(<< buff << ' ' << lpErrorText);
}
return 1;
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::Initialize()
{
int i;
if (this->Initialized)
{
return;
}
// Preliminary update of frame buffer, just in case we don't get
// though the initialization but need the framebuffer for Updates
this->UpdateFrameBuffer();
// It is necessary to create not one, but two windows in order to
// do frame grabbing under VFW. Why do we need any?
// get necessary process info
HINSTANCE hinstance = GetModuleHandle(NULL);
strcpy(this->WndClassName,"VTKVideo");
// set up a class for the main window
WNDCLASS wc;
wc.lpszClassName = this->WndClassName;
wc.hInstance = hinstance;
wc.lpfnWndProc = reinterpret_cast<WNDPROC>(&vtkWin32VideoSourceWinProc);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = NULL;
wc.lpszMenuName = NULL;
wc.hbrBackground = NULL;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.cbClsExtra = sizeof(void *);
wc.cbWndExtra = 0;
for (i = 1; i <= 10; i++)
{
if (RegisterClass(&wc))
{
break;
}
// try again with a slightly different name
sprintf(this->WndClassName,"VTKVideo %d",i);
}
if (i > 10)
{
vtkErrorMacro(<< "Initialize: failed to register VTKVideo class"\
<< " (" << GetLastError() << ")");
return;
}
DWORD style = WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|
WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
if (this->Preview)
{
style |= WS_VISIBLE;
}
// set up the parent window, but don't show it
this->Internal->ParentWnd = CreateWindow(
this->WndClassName,
"VTK Video Window",
style,
0, 0,
this->FrameSize[0]+2*GetSystemMetrics(SM_CXFIXEDFRAME),
this->FrameSize[1]+2*GetSystemMetrics(SM_CYFIXEDFRAME)
+GetSystemMetrics(SM_CYBORDER)
+GetSystemMetrics(SM_CYSIZE),
NULL,
NULL,
hinstance,
NULL);
if (!this->Internal->ParentWnd)
{
vtkErrorMacro(<< "Initialize: failed to create window"\
<< " (" << GetLastError() << ")");
return;
}
// set the user data to 'this'
vtkSetWindowLong(this->Internal->ParentWnd,vtkGWL_USERDATA,(vtkLONG)this);
// Create the capture window
this->Internal->CapWnd = capCreateCaptureWindow("Capture",
WS_CHILD|WS_VISIBLE, 0, 0,
this->FrameSize[0], this->FrameSize[1],
this->Internal->ParentWnd,1);
if (!this->Internal->CapWnd)
{
vtkErrorMacro(<< "Initialize: failed to create capture window"\
<< " (" << GetLastError() << ")");
this->ReleaseSystemResources();
return;
}
// connect to the driver
if (!capDriverConnect(this->Internal->CapWnd,0))
{
vtkErrorMacro(<< "Initialize: couldn't connect to driver"\
<< " (" << GetLastError() << ")");
this->ReleaseSystemResources();
return;
}
capDriverGetCaps(this->Internal->CapWnd,&this->Internal->CapDriverCaps,sizeof(CAPDRIVERCAPS));
// set up the video format
this->DoVFWFormatSetup();
// set the capture parameters
capCaptureGetSetup(this->Internal->CapWnd,&this->Internal->CaptureParms,sizeof(CAPTUREPARMS));
if (this->FrameRate > 0)
{
this->Internal->CaptureParms.dwRequestMicroSecPerFrame =
int(1000000/this->FrameRate);
}
else
{
this->Internal->CaptureParms.dwRequestMicroSecPerFrame = 0;
}
this->Internal->CaptureParms.fMakeUserHitOKToCapture = FALSE;
this->Internal->CaptureParms.fYield = 1;
this->Internal->CaptureParms.fCaptureAudio = FALSE;
this->Internal->CaptureParms.vKeyAbort = 0x00;
this->Internal->CaptureParms.fAbortLeftMouse = FALSE;
this->Internal->CaptureParms.fAbortRightMouse = FALSE;
this->Internal->CaptureParms.fLimitEnabled = FALSE;
this->Internal->CaptureParms.wNumAudioRequested = 0;
this->Internal->CaptureParms.wPercentDropForError = 100;
this->Internal->CaptureParms.dwAudioBufferSize = 0;
this->Internal->CaptureParms.AVStreamMaster = AVSTREAMMASTER_NONE;
if (!capCaptureSetSetup(this->Internal->CapWnd,&this->Internal->CaptureParms,
sizeof(CAPTUREPARMS)))
{
vtkErrorMacro(<< "Initialize: setup of capture parameters failed"\
<< " (" << GetLastError() << ")");
this->ReleaseSystemResources();
return;
}
// set user data for callbacks
if (!capSetUserData(this->Internal->CapWnd,(long)this))
{
vtkErrorMacro(<< "Initialize: couldn't set user data for callback"\
<< " (" << GetLastError() << ")");
this->ReleaseSystemResources();
return;
}
// install the callback to precisely time beginning of grab
if (!capSetCallbackOnCapControl(this->Internal->CapWnd,
&vtkWin32VideoSourceCapControlProc))
{
vtkErrorMacro(<< "Initialize: couldn't set control callback"\
<< " (" << GetLastError() << ")");
this->ReleaseSystemResources();
return;
}
// install the callback to copy frames into the buffer on sync grabs
if (!capSetCallbackOnFrame(this->Internal->CapWnd,
&vtkWin32VideoSourceCallbackProc))
{
vtkErrorMacro(<< "Initialize: couldn't set frame callback"\
<< " (" << GetLastError() << ")");
this->ReleaseSystemResources();
return;
}
// install the callback to copy frames into the buffer on stream grabs
if (!capSetCallbackOnVideoStream(this->Internal->CapWnd,
&vtkWin32VideoSourceCallbackProc))
{
vtkErrorMacro(<< "Initialize: couldn't set stream callback"\
<< " (" << GetLastError() << ")");
this->ReleaseSystemResources();
return;
}
// install the callback to get info on start/end of streaming
if (!capSetCallbackOnStatus(this->Internal->CapWnd,
&vtkWin32VideoSourceStatusCallbackProc))
{
vtkErrorMacro(<< "Initialize: couldn't set status callback"\
<< " (" << GetLastError() << ")");
this->ReleaseSystemResources();
return;
}
// install the callback to send messages to user
if (!capSetCallbackOnError(this->Internal->CapWnd,
&vtkWin32VideoSourceErrorCallbackProc))
{
vtkErrorMacro(<< "Initialize: couldn't set error callback"\
<< " (" << GetLastError() << ")");
this->ReleaseSystemResources();
return;
}
capOverlay(this->Internal->CapWnd,TRUE);
// update framebuffer again to reflect any changes which
// might have occurred
this->UpdateFrameBuffer();
this->Initialized = 1;
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::SetPreview(int p)
{
if (this->Preview == p)
{
return;
}
this->Preview = p;
this->Modified();
if (this->Internal->CapWnd == NULL || this->Internal->ParentWnd == NULL)
{
return;
}
if (p)
{
ShowWindow(this->Internal->ParentWnd,SW_SHOWNORMAL);
}
else
{
ShowWindow(this->Internal->ParentWnd,SW_HIDE);
}
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::ReleaseSystemResources()
{
// destruction of ParentWnd causes OnParentWndDestroy to be called
if (this->Internal->ParentWnd)
{
DestroyWindow(this->Internal->ParentWnd);
}
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::OnParentWndDestroy()
{
if (this->Playing || this->Recording)
{
this->Stop();
}
if (this->Internal->CapWnd)
{
//MessageBox(this->Internal->ParentWnd, "capDriverDisconnect(this->Internal->CapWnd)", "", MB_OK | MB_ICONEXCLAMATION);
capDriverDisconnect(this->Internal->CapWnd);
//MessageBox(this->Internal->ParentWnd, "DestroyWindow(this->Internal->CapWnd)", "", MB_OK | MB_ICONEXCLAMATION);
DestroyWindow(this->Internal->CapWnd);
this->Internal->CapWnd = NULL;
}
if (this->WndClassName[0] != '\0')
{
UnregisterClass(this->WndClassName,GetModuleHandle(NULL));
this->WndClassName[0] = '\0';
}
this->Internal->ParentWnd = NULL;
this->Initialized = 0;
}
//----------------------------------------------------------------------------
// copy the Device Independent Bitmap from the VFW framebuffer into the
// vtkVideoSource framebuffer (don't do the unpacking yet)
void vtkWin32VideoSource::LocalInternalGrab(void* lpptr)
{
LPVIDEOHDR lpVHdr = static_cast<LPVIDEOHDR>(lpptr);
// cerr << "Grabbed\n";
// the VIDEOHDR has the following contents, for quick ref:
//
// lpData pointer to locked data buffer
// dwBufferLength Length of data buffer
// dwBytesUsed Bytes actually used
// dwTimeCaptured Milliseconds from start of stream
// dwUser for client's use
// dwFlags assorted flags (see VFW.H)
// dwReserved[4] reserved for driver
unsigned char *cptrDIB = lpVHdr->lpData;
// get a thread lock on the frame buffer
this->FrameBufferMutex->Lock();
if (this->AutoAdvance)
{
this->AdvanceFrameBuffer(1);
if (this->FrameIndex + 1 < this->FrameBufferSize)
{
this->FrameIndex++;
}
}
int index = this->FrameBufferIndex;
this->FrameCount++;
this->FrameBufferTimeStamps[index] = this->StartTimeStamp + \
0.001 * lpVHdr->dwTimeCaptured;
unsigned char *ptr = (unsigned char *)
((reinterpret_cast<vtkUnsignedCharArray*>(this->FrameBuffer[index])) \
->GetPointer(0));
// the DIB has rows which are multiples of 4 bytes
int outBytesPerRow = ((this->FrameBufferExtent[1]-
this->FrameBufferExtent[0]+1)
* this->FrameBufferBitsPerPixel + 7)/8;
outBytesPerRow += outBytesPerRow % this->FrameBufferRowAlignment;
int inBytesPerRow = this->FrameSize[0]
* (this->Internal->BitMapPtr->bmiHeader.biBitCount/8);
outBytesPerRow += outBytesPerRow % 4;
int rows = this->FrameBufferExtent[3]-this->FrameBufferExtent[2]+1;
cptrDIB += this->FrameBufferExtent[0]*\
(this->Internal->BitMapPtr->bmiHeader.biBitCount/8);
cptrDIB += this->FrameBufferExtent[2]*inBytesPerRow;
// uncompress or simply copy the DIB
switch (this->Internal->BitMapPtr->bmiHeader.biCompression)
{
case BI_RGB:
case VTK_BI_UYVY:
if (outBytesPerRow == inBytesPerRow)
{
memcpy(ptr,cptrDIB,inBytesPerRow*rows);
}
else
{
while (--rows >= 0)
{
memcpy(ptr,cptrDIB,outBytesPerRow);
ptr += outBytesPerRow;
cptrDIB += inBytesPerRow;
}
}
break;
case BI_RLE8: // not handled
case BI_RLE4:
case BI_BITFIELDS:
break;
}
this->Modified();
this->FrameBufferMutex->Unlock();
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::Grab()
{
if (this->Recording)
{
return;
}
// ensure that the frame buffer is properly initialized
this->Initialize();
if (!this->Initialized)
{
return;
}
// just do the grab, the callback does the rest
this->SetStartTimeStamp(vtkTimerLog::GetUniversalTime());
capGrabFrameNoStop(this->Internal->CapWnd);
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::Record()
{
this->Initialize();
if (!this->Initialized)
{
return;
}
if (this->Playing)
{
this->Stop();
}
if (!this->Recording)
{
this->Recording = 1;
this->Modified();
capCaptureSequenceNoFile(this->Internal->CapWnd);
}
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::Play()
{
this->vtkVideoSource::Play();
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::Stop()
{
if (this->Recording)
{
this->Recording = 0;
this->Modified();
capCaptureStop(this->Internal->CapWnd);
}
else if (this->Playing)
{
this->vtkVideoSource::Stop();
}
}
//----------------------------------------------------------------------------
// codecs
static inline void vtkYUVToRGB(unsigned char *yuv, unsigned char *rgb)
{
/* // floating point
int Y = yuv[0] - 16;
int U = yuv[1] - 128;
int V = yuv[2] - 128;
int R = 1.164*Y + 1.596*V + 0.5;
int G = 1.164*Y - 0.813*V - 0.391*U + 0.5;
int B = 1.164*Y + 2.018*U + 0.5;
*/
// integer math
int Y = (yuv[0] - 16)*76284;
int U = yuv[1] - 128;
int V = yuv[2] - 128;
int R = Y + 104595*V ;
int G = Y - 53281*V - 25625*U;
int B = Y + 132252*U;
// round
R += 32768;
G += 32768;
B += 32768;
// shift
R >>= 16;
G >>= 16;
B >>= 16;
// clamp
if (R < 0) { R = 0; }
if (G < 0) { G = 0; }
if (B < 0) { B = 0; }
if (R > 255) { R = 255; };
if (G > 255) { G = 255; };
if (B > 255) { B = 255; };
// output
rgb[0] = R;
rgb[1] = G;
rgb[2] = B;
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::UnpackRasterLine(char *outptr, char *inptr,
int start, int count)
{
char alpha = (char)(this->Opacity*255);
int compression = this->Internal->BitMapPtr->bmiHeader.biCompression;
int i;
switch (this->FrameBufferBitsPerPixel)
{
case 1:
{
int rawBits;
inptr += start/8;
i = start % 8;
while (count >= 0)
{
rawBits = *inptr++;
for (; i < 8 && --count >= 0; i++)
{
*outptr++ = -((rawBits >> i) & 0x01);
}
i = 0;
}
}
break;
case 4:
{
int rawNibbles;
inptr += start/2;
i = start % 2;
while (count >= 0)
{
rawNibbles = *inptr++;
for (; i < 8 && --count >= 0; i += 4)
{
*outptr++ = ((rawNibbles >> i) & 0x0f) << 4;
}
i = 0;
}
}
break;
case 8:
{
inptr += start;
memcpy(outptr,inptr,count);
}
break;
case 16:
{
inptr += 2*start;
if (compression == VTK_BI_UYVY)
{
switch (this->OutputFormat)
{
case VTK_LUMINANCE:
{ // unpack UY half-megapixel to one Y pixel
while (--count >= 0)
{
inptr++;
*outptr++ = *inptr++;
}
}
case VTK_RGB:
case VTK_RGBA:
{ // unpack UYVY megapixel to two RGB or RGBA pixels
unsigned char YUV[3];
//int finish = start + count;
int odd = (start % 2 == 1);
if (count > 0) { YUV[1+odd] = inptr[0]; }
if (count > 1) { YUV[0] = inptr[1]; }
if (count > 2) { YUV[2-odd] = inptr[2]; }
while (--count >= 0)
{
YUV[1+odd] = *inptr++;
YUV[0] = *inptr++;
odd = !odd;
vtkYUVToRGB(YUV,(unsigned char *)outptr);
outptr += 3;
if (this->OutputFormat == VTK_RGB)
{
continue;
}
*outptr++ = alpha;
}
}
}
}
else
{
unsigned short rawWord;
unsigned short *shptr = (unsigned short *)inptr;
switch (this->OutputFormat)
{
case VTK_RGB:
{ // unpack 16 bits to 24 bits
while (--count >= 0)
{
rawWord = *shptr++;
*outptr++ = (rawWord & 0x7c00) >> 7;
*outptr++ = (rawWord & 0x03e0) >> 2;
*outptr++ = (rawWord & 0x001f) << 3;
}
}
break;
case VTK_RGBA:
{ // unpack 16 bits to 32 bits
while (--count >= 0)
{
rawWord = *shptr++;
*outptr++ = (rawWord & 0x7c00) >> 7;
*outptr++ = (rawWord & 0x03e0) >> 2;
*outptr++ = (rawWord & 0x001f) << 3;
*outptr++ = alpha;
}
break;
}
}
}
}
case 24:
{
inptr += 3*start;
switch (this->OutputFormat)
{
case VTK_RGB:
{ // must do BGR to RGB conversion
outptr += 3;
while (--count >= 0)
{
*--outptr = *inptr++;
*--outptr = *inptr++;
*--outptr = *inptr++;
outptr += 6;
}
}
break;
case VTK_RGBA:
{ // must do BGR to RGBX conversion
outptr += 4;
while (--count >= 0)
{
*--outptr = alpha;
*--outptr = *inptr++;
*--outptr = *inptr++;
*--outptr = *inptr++;
outptr += 8;
}
}
break;
}
}
break;
case 32:
inptr += 4*start;
switch (this->OutputFormat)
{
case VTK_RGB:
{ // must do BGRX to RGB conversion
outptr += 3;
while (--count >= 0)
{
*--outptr = *inptr++;
*--outptr = *inptr++;
*--outptr = *inptr++;
inptr++;
outptr += 6;
}
}
break;
case VTK_RGBA:
{
outptr += 4;
while (--count >= 0)
{
*--outptr = alpha;
*--outptr = *inptr++;
*--outptr = *inptr++;
*--outptr = *inptr++;
inptr++;
outptr += 8;
}
}
break;
}
break;
}
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::VideoFormatDialog()
{
this->Initialize();
if (!this->Initialized)
{
return;
}
//if (!this->Internal->CapDriverCaps.fHasDlgVideoFormat)
// {
// MessageBox(this->Internal->ParentWnd,"The video device has no Format dialog.","",
// MB_OK | MB_ICONEXCLAMATION);
// return;
// }
capGetStatus(this->Internal->CapWnd,&this->Internal->CapStatus,sizeof(CAPSTATUS));
if (this->Internal->CapStatus.fCapturingNow)
{
MessageBox(this->Internal->ParentWnd, "Can't alter video format while grabbing.","",
MB_OK | MB_ICONEXCLAMATION);
return;
}
int success = capDlgVideoFormat(this->Internal->CapWnd);
if (success)
{
this->FrameBufferMutex->Lock();
this->DoVFWFormatCheck();
this->FrameBufferMutex->Unlock();
}
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::VideoSourceDialog()
{
this->Initialize();
if (!this->Initialized)
{
return;
}
//if (!this->Internal->CapDriverCaps.fHasDlgVideoSource)
// {
// MessageBox(this->Internal->ParentWnd,"The video device has no Source dialog.","",
// MB_OK | MB_ICONEXCLAMATION);
// return;
// }
capGetStatus(this->Internal->CapWnd,&this->Internal->CapStatus,sizeof(CAPSTATUS));
if (this->Internal->CapStatus.fCapturingNow)
{
MessageBox(this->Internal->ParentWnd, "Can't alter video source while grabbing.","",
MB_OK | MB_ICONEXCLAMATION);
return;
}
int success = capDlgVideoSource(this->Internal->CapWnd);
if (success)
{
this->FrameBufferMutex->Lock();
this->DoVFWFormatCheck();
this->FrameBufferMutex->Unlock();
}
}
//----------------------------------------------------------------------------
// try for the specified frame size
void vtkWin32VideoSource::SetFrameSize(int x, int y, int z)
{
if (x == this->FrameSize[0] &&
y == this->FrameSize[1] &&
z == this->FrameSize[2])
{
return;
}
if (x < 1 || y < 1 || z != 1)
{
vtkErrorMacro(<< "SetFrameSize: Illegal frame size");
return;
}
this->FrameSize[0] = x;
this->FrameSize[1] = y;
this->FrameSize[2] = z;
this->Modified();
if (this->Initialized)
{
this->FrameBufferMutex->Lock();
this->UpdateFrameBuffer();
this->DoVFWFormatSetup();
this->FrameBufferMutex->Unlock();
}
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::SetFrameRate(float rate)
{
if (rate == this->FrameRate)
{
return;
}
this->FrameRate = rate;
this->Modified();
if (this->Initialized)
{
capCaptureGetSetup(this->Internal->CapWnd,&this->Internal->CaptureParms,sizeof(CAPTUREPARMS));
if (this->FrameRate > 0)
{
this->Internal->CaptureParms.dwRequestMicroSecPerFrame =
int(1000000/this->FrameRate);
}
else
{
this->Internal->CaptureParms.dwRequestMicroSecPerFrame = 0;
}
capCaptureSetSetup(this->Internal->CapWnd,&this->Internal->CaptureParms,sizeof(CAPTUREPARMS));
}
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::SetOutputFormat(int format)
{
if (format == this->OutputFormat)
{
return;
}
this->OutputFormat = format;
// convert color format to number of scalar components
int numComponents;
switch (this->OutputFormat)
{
case VTK_RGBA:
numComponents = 4;
break;
case VTK_RGB:
numComponents = 3;
break;
case VTK_LUMINANCE:
numComponents = 1;
break;
default:
numComponents = 0;
vtkErrorMacro(<< "SetOutputFormat: Unrecognized color format.");
break;
}
this->NumberOfScalarComponents = numComponents;
if (this->FrameBufferBitsPerPixel != numComponents*8)
{
this->FrameBufferMutex->Lock();
this->FrameBufferBitsPerPixel = numComponents*8;
if (this->Initialized)
{
this->UpdateFrameBuffer();
this->DoVFWFormatSetup();
}
this->FrameBufferMutex->Unlock();
}
this->Modified();
}
//----------------------------------------------------------------------------
// check the current video format and set up the VTK video framebuffer to match
void vtkWin32VideoSource::DoVFWFormatCheck()
{
// get the real video format
int formatSize = capGetVideoFormatSize(this->Internal->CapWnd);
if (formatSize > this->BitMapSize)
{
if (this->Internal->BitMapPtr)
{
delete [] ((char *)this->Internal->BitMapPtr);
}
this->Internal->BitMapPtr = (LPBITMAPINFO) new char[formatSize];
this->BitMapSize = formatSize;
}
capGetVideoFormat(this->Internal->CapWnd,this->Internal->BitMapPtr,formatSize);
int bpp = this->Internal->BitMapPtr->bmiHeader.biBitCount;
int width = this->Internal->BitMapPtr->bmiHeader.biWidth;
int height = this->FrameSize[1] = this->Internal->BitMapPtr->bmiHeader.biHeight;
int compression = this->Internal->BitMapPtr->bmiHeader.biCompression;
if (compression == VTK_BI_UYVY)
{
this->FlipFrames = 1;
}
else if (compression == BI_RGB)
{
this->FlipFrames = 0;
}
else
{
char fourcchex[16], fourcc[8];
sprintf(fourcchex,"0x%08x",compression);
for (int i = 0; i < 4; i++)
{
fourcc[i] = (compression >> (8*i)) & 0xff;
if (!isprint(fourcc[i]))
{
fourcc[i] = '?';
}
}
fourcc[4] = '\0';
vtkWarningMacro(<< "DoVFWFormatCheck: video compression mode " <<
fourcchex << " \"" << fourcc << "\": can't grab");
}
if (bpp != this->FrameBufferBitsPerPixel)
{
switch (bpp)
{
case 1:
case 4:
case 8:
this->OutputFormat = VTK_LUMINANCE;
this->NumberOfScalarComponents = 1;
break;
case 16:
if (compression != VTK_BI_UYVY)
{
this->OutputFormat = VTK_RGB;
this->NumberOfScalarComponents = 3;
}
break;
case 24:
case 32:
if (this->OutputFormat != VTK_RGBA)
{
this->OutputFormat = VTK_RGB;
this->NumberOfScalarComponents = 3;
}
break;
}
}
if (bpp != this->FrameBufferBitsPerPixel ||
this->FrameSize[0] != width ||
this->FrameSize[1] != height)
{
this->FrameBufferBitsPerPixel = bpp;
this->FrameSize[0] = width;
this->FrameSize[1] = height;
this->Modified();
this->UpdateFrameBuffer();
}
}
//----------------------------------------------------------------------------
void vtkWin32VideoSource::DoVFWFormatSetup()
{
static int colorBits[3] = { 24, 32, 16 };
static int greyBits[3] = { 8, 4, 1 };
int i, bytesPerRow, bitCount;
// get the real video format
int formatSize = capGetVideoFormatSize(this->Internal->CapWnd);
if (formatSize > this->BitMapSize)
{
if (this->Internal->BitMapPtr)
{
delete [] ((char *)this->Internal->BitMapPtr);
}
this->Internal->BitMapPtr = (LPBITMAPINFO) new char[formatSize];
this->BitMapSize = formatSize;
}
capGetVideoFormat(this->Internal->CapWnd,this->Internal->BitMapPtr,formatSize);
// set the format of the captured frames
this->Internal->BitMapPtr->bmiHeader.biWidth = this->FrameSize[0];
this->Internal->BitMapPtr->bmiHeader.biHeight = this->FrameSize[1];
this->Internal->BitMapPtr->bmiHeader.biCompression = BI_RGB;
this->Internal->BitMapPtr->bmiHeader.biClrUsed = 0;
this->Internal->BitMapPtr->bmiHeader.biClrImportant = 0;
for (i = 0; i < 4; i++)
{ // try for a
if (this->OutputFormat == VTK_RGBA || this->OutputFormat == VTK_RGB)
{
bitCount = colorBits[i];
}
else
{
bitCount = greyBits[i];
}
bytesPerRow = (this->FrameSize[0]*bitCount+7)/8;
bytesPerRow += bytesPerRow % this->FrameBufferRowAlignment;
this->Internal->BitMapPtr->bmiHeader.biBitCount = bitCount;
this->Internal->BitMapPtr->bmiHeader.biSizeImage = bytesPerRow*this->FrameSize[1];
if (capSetVideoFormat(this->Internal->CapWnd,this->Internal->BitMapPtr,
sizeof(BITMAPINFOHEADER)))
{
break;
}
}
if (i > 4)
{
vtkWarningMacro(<< "DoVFWFormatSetup: invalid video format for device"\
<< " (" << GetLastError() << ")");
}
this->DoVFWFormatCheck();
}
|