1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746 |
- #include <streams.h>
- #include <limits.h>
- #include <dvdmedia.h>
- #include <strsafe.h>
- #include <checkbmi.h>
- static UINT MsgDestroy;
- CBaseWindow::CBaseWindow(BOOL bDoGetDC, bool bDoPostToDestroy) :
- m_hInstance(g_hInst),
- m_hwnd(NULL),
- m_hdc(NULL),
- m_bActivated(FALSE),
- m_pClassName(NULL),
- m_ClassStyles(0),
- m_WindowStyles(0),
- m_WindowStylesEx(0),
- m_ShowStageMessage(0),
- m_ShowStageTop(0),
- m_MemoryDC(NULL),
- m_hPalette(NULL),
- m_bBackground(FALSE),
- #ifdef DEBUG
- m_bRealizing(FALSE),
- #endif
- m_bNoRealize(FALSE),
- m_bDoPostToDestroy(bDoPostToDestroy)
- {
- m_bDoGetDC = bDoGetDC;
- }
- HRESULT CBaseWindow::PrepareWindow()
- {
- if (m_hwnd) return NOERROR;
- ASSERT(m_hwnd == NULL);
- ASSERT(m_hdc == NULL);
-
- m_pClassName = GetClassWindowStyles(&m_ClassStyles,
- &m_WindowStyles,
- &m_WindowStylesEx);
- if (m_pClassName == NULL) {
- return E_FAIL;
- }
-
- m_ShowStageMessage = RegisterWindowMessage(SHOWSTAGE);
-
- if (0 == m_ShowStageMessage) {
- return AmGetLastErrorToHResult();
- }
- m_ShowStageTop = RegisterWindowMessage(SHOWSTAGETOP);
- if (0 == m_ShowStageTop) {
- return AmGetLastErrorToHResult();
- }
- m_RealizePalette = RegisterWindowMessage(REALIZEPALETTE);
- if (0 == m_RealizePalette) {
- return AmGetLastErrorToHResult();
- }
- MsgDestroy = RegisterWindowMessage(TEXT("AM_DESTROY"));
- if (0 == MsgDestroy) {
- return AmGetLastErrorToHResult();
- }
- return DoCreateWindow();
- }
- #ifdef DEBUG
- CBaseWindow::~CBaseWindow()
- {
- ASSERT(m_hwnd == NULL);
- ASSERT(m_hdc == NULL);
- }
- #endif
- HRESULT CBaseWindow::DoneWithWindow()
- {
- if (!IsWindow(m_hwnd) || (GetWindowThreadProcessId(m_hwnd, NULL) != GetCurrentThreadId())) {
- if (IsWindow(m_hwnd)) {
-
-
- ASSERT(GetWindowThreadProcessId(m_hwnd, NULL) != GetCurrentThreadId());
- if (m_bDoPostToDestroy) {
- HRESULT hr = S_OK;
- CAMEvent m_evDone(FALSE, &hr);
- if (FAILED(hr)) {
- return hr;
- }
-
-
-
-
- PostMessage(m_hwnd, MsgDestroy, (WPARAM)(HANDLE)m_evDone, 0);
- WaitDispatchingMessages(m_evDone, INFINITE);
- } else {
- SendMessage(m_hwnd, MsgDestroy, 0, 0);
- }
- }
-
-
-
-
-
- m_hdc = NULL;
-
-
-
-
- if (m_MemoryDC)
- {
- EXECUTE_ASSERT(DeleteDC(m_MemoryDC));
- m_MemoryDC = NULL;
- }
-
- m_hwnd = NULL;
- return NOERROR;
- }
- const HWND hwnd = m_hwnd;
- if (hwnd == NULL) {
- return NOERROR;
- }
- InactivateWindow();
- NOTE("Inactivated");
-
- SetWindowLong(hwnd,GWL_STYLE,m_WindowStyles);
- ASSERT(GetParent(hwnd) == NULL);
- NOTE1("Reset window styles %d",m_WindowStyles);
-
- UninitialiseWindow();
- DbgLog((LOG_TRACE, 2, TEXT("Destroying 0x%8.8X"), hwnd));
- if (!DestroyWindow(hwnd)) {
- DbgLog((LOG_TRACE, 0, TEXT("DestroyWindow %8.8X failed code %d"),
- hwnd, GetLastError()));
- DbgBreak("");
- }
-
- m_pClassName = NULL;
- m_ClassStyles = 0;
- m_WindowStyles = 0;
- m_WindowStylesEx = 0;
- m_ShowStageMessage = 0;
- m_ShowStageTop = 0;
- return NOERROR;
- }
- HRESULT CBaseWindow::InactivateWindow()
- {
-
- if (m_bActivated == FALSE) {
- return S_FALSE;
- }
- m_bActivated = FALSE;
- ShowWindow(m_hwnd,SW_HIDE);
- return NOERROR;
- }
- HRESULT CBaseWindow::CompleteConnect()
- {
- m_bActivated = FALSE;
- return NOERROR;
- }
- HRESULT CBaseWindow::ActivateWindow()
- {
-
- if (m_bActivated == TRUE || GetParent(m_hwnd) != NULL) {
- SetWindowPos(m_hwnd,
- HWND_TOP,
- 0, 0, 0, 0,
- SWP_NOMOVE |
- SWP_NOSIZE);
- m_bActivated = TRUE;
- return S_FALSE;
- }
-
- RECT WindowRect, ClientRect = GetDefaultRect();
- GetWindowRect(m_hwnd,&WindowRect);
- AdjustWindowRectEx(&ClientRect,GetWindowLong(m_hwnd,GWL_STYLE),
- FALSE,GetWindowLong(m_hwnd,GWL_EXSTYLE));
-
- UINT WindowFlags = (SWP_NOACTIVATE | SWP_FRAMECHANGED);
- WindowRect.left -= (WindowRect.left & 3);
- WindowRect.top -= (WindowRect.top & 3);
- SetWindowPos(m_hwnd,
- HWND_TOP,
- WindowRect.left,
- WindowRect.top,
- WIDTH(&ClientRect),
- HEIGHT(&ClientRect),
- WindowFlags);
- m_bActivated = TRUE;
- return NOERROR;
- }
- HRESULT CBaseWindow::PerformanceAlignWindow()
- {
- RECT ClientRect,WindowRect;
- GetWindowRect(m_hwnd,&WindowRect);
- ASSERT(m_bActivated == TRUE);
-
- if (GetParent(m_hwnd)) {
- return NOERROR;
- }
-
- GetClientRect(m_hwnd, &ClientRect);
- MapWindowPoints(m_hwnd, HWND_DESKTOP, (LPPOINT) &ClientRect, 2);
- WindowRect.left -= (ClientRect.left & 3);
- WindowRect.top -= (ClientRect.top & 3);
- UINT WindowFlags = (SWP_NOACTIVATE | SWP_NOSIZE);
- SetWindowPos(m_hwnd,
- HWND_TOP,
- WindowRect.left,
- WindowRect.top,
- (int) 0,(int) 0,
- WindowFlags);
- return NOERROR;
- }
- HRESULT CBaseWindow::SetPalette(HPALETTE hPalette)
- {
-
- {
- CAutoLock cWindowLock(&m_WindowLock);
- CAutoLock cPaletteLock(&m_PaletteLock);
- ASSERT(hPalette);
- m_hPalette = hPalette;
- }
- return SetPalette();
- }
- HRESULT CBaseWindow::SetPalette()
- {
- if (!m_bNoRealize) {
- SendMessage(m_hwnd, m_RealizePalette, 0, 0);
- return S_OK;
- } else {
-
- ASSERT(m_hdc);
- ASSERT(m_MemoryDC);
- CAutoLock cPaletteLock(&m_PaletteLock);
- SelectPalette(m_hdc,m_hPalette,m_bBackground);
- SelectPalette(m_MemoryDC,m_hPalette,m_bBackground);
- return S_OK;
- }
- }
- void CBaseWindow::UnsetPalette()
- {
- CAutoLock cWindowLock(&m_WindowLock);
- CAutoLock cPaletteLock(&m_PaletteLock);
-
- HPALETTE hPalette = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
- ASSERT(hPalette);
- SelectPalette(GetWindowHDC(), hPalette, TRUE);
- SelectPalette(GetMemoryHDC(), hPalette, TRUE);
- m_hPalette = NULL;
- }
- void CBaseWindow::LockPaletteLock()
- {
- m_PaletteLock.Lock();
- }
- void CBaseWindow::UnlockPaletteLock()
- {
- m_PaletteLock.Unlock();
- }
- HRESULT CBaseWindow::DoRealisePalette(BOOL bForceBackground)
- {
- {
- CAutoLock cPaletteLock(&m_PaletteLock);
- if (m_hPalette == NULL) {
- return NOERROR;
- }
-
- ASSERT(m_hdc);
- ASSERT(m_MemoryDC);
- SelectPalette(m_hdc,m_hPalette,m_bBackground || bForceBackground);
- SelectPalette(m_MemoryDC,m_hPalette,m_bBackground);
- }
-
-
-
-
-
-
-
-
-
-
-
- ASSERT(CritCheckOut(&m_WindowLock));
- ASSERT(CritCheckOut(&m_PaletteLock));
- EXECUTE_ASSERT(RealizePalette(m_hdc) != GDI_ERROR);
- EXECUTE_ASSERT(RealizePalette(m_MemoryDC) != GDI_ERROR);
- return (GdiFlush() == FALSE ? S_FALSE : S_OK);
- }
- LRESULT CALLBACK WndProc(HWND hwnd,
- UINT uMsg,
- WPARAM wParam,
- LPARAM lParam)
- {
-
-
-
-
-
- CBaseWindow *pBaseWindow = _GetWindowLongPtr<CBaseWindow*>(hwnd,0);
- if (pBaseWindow == NULL) {
-
-
-
-
-
-
-
- if (uMsg == WM_NCCREATE) {
- SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) & ~0x400000);
- }
- if ((uMsg != WM_NCCREATE)
- || (NULL == (pBaseWindow = *(CBaseWindow**) ((LPCREATESTRUCT)lParam)->lpCreateParams)))
- {
- return(DefWindowProc(hwnd, uMsg, wParam, lParam));
- }
-
- #ifdef DEBUG
- SetLastError(0);
- #endif
- LONG_PTR rc = _SetWindowLongPtr(hwnd, (DWORD) 0, pBaseWindow);
- #ifdef DEBUG
- if (0 == rc) {
-
-
- LONG lasterror = GetLastError();
- ASSERT(0 == lasterror);
-
-
- }
- #endif
- }
-
- if (uMsg == MsgDestroy && uMsg != 0) {
- pBaseWindow->DoneWithWindow();
- if (pBaseWindow->m_bDoPostToDestroy) {
- EXECUTE_ASSERT(SetEvent((HANDLE)wParam));
- }
- return 0;
- }
- return pBaseWindow->OnReceiveMessage(hwnd,uMsg,wParam,lParam);
- }
- BOOL CBaseWindow::OnSize(LONG Width, LONG Height)
- {
- m_Width = Width;
- m_Height = Height;
- return TRUE;
- }
- BOOL CBaseWindow::OnClose()
- {
- ShowWindow(m_hwnd,SW_HIDE);
- return TRUE;
- }
- HRESULT CBaseWindow::UninitialiseWindow()
- {
-
- if (m_hwnd == NULL) {
- ASSERT(m_hdc == NULL);
- ASSERT(m_MemoryDC == NULL);
- return NOERROR;
- }
-
- EXECUTE_ASSERT(GdiFlush());
- if (m_hdc)
- {
- EXECUTE_ASSERT(ReleaseDC(m_hwnd,m_hdc));
- m_hdc = NULL;
- }
- if (m_MemoryDC)
- {
- EXECUTE_ASSERT(DeleteDC(m_MemoryDC));
- m_MemoryDC = NULL;
- }
-
- m_hwnd = NULL;
- return NOERROR;
- }
- HRESULT CBaseWindow::InitialiseWindow(HWND hwnd)
- {
-
- ASSERT(IsWindow(hwnd));
- m_hwnd = hwnd;
- if (m_bDoGetDC)
- {
- EXECUTE_ASSERT(m_hdc = GetDC(hwnd));
- EXECUTE_ASSERT(m_MemoryDC = CreateCompatibleDC(m_hdc));
- EXECUTE_ASSERT(SetStretchBltMode(m_hdc,COLORONCOLOR));
- EXECUTE_ASSERT(SetStretchBltMode(m_MemoryDC,COLORONCOLOR));
- }
- return NOERROR;
- }
- HRESULT CBaseWindow::DoCreateWindow()
- {
- WNDCLASS wndclass;
- BOOL bRegistered;
- HWND hwnd;
- bRegistered = GetClassInfo(m_hInstance,
- m_pClassName,
- &wndclass);
-
-
-
-
- if (m_bDoGetDC)
- {
- m_ClassStyles |= CS_OWNDC;
- }
- if (bRegistered == FALSE) {
-
- wndclass.lpszClassName = m_pClassName;
- wndclass.style = m_ClassStyles;
- wndclass.lpfnWndProc = WndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = sizeof(CBaseWindow *);
- wndclass.hInstance = m_hInstance;
- wndclass.hIcon = NULL;
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
- wndclass.hbrBackground = (HBRUSH) NULL;
- wndclass.lpszMenuName = NULL;
- RegisterClassW(&wndclass);
- }
-
-
-
- CBaseWindow *pBaseWindow = this;
- hwnd = CreateWindowEx(m_WindowStylesEx,
- m_pClassName,
- TEXT("ActiveMovie Window"),
- m_WindowStyles,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- DEFWIDTH,
- DEFHEIGHT,
- NULL,
- NULL,
- m_hInstance,
- &pBaseWindow);
-
-
-
- if (hwnd == NULL) {
- DWORD Error = GetLastError();
- return AmHresultFromWin32(Error);
- }
-
- ASSERT(GetWindowLongPtr(hwnd, 0) == (LONG_PTR)this);
-
-
-
-
-
- InitialiseWindow(hwnd);
- DbgLog((LOG_TRACE, 2, TEXT("Created window class (%s) HWND(%8.8X)"),
- m_pClassName, hwnd));
- return S_OK;
- }
- LRESULT CBaseWindow::OnReceiveMessage(HWND hwnd,
- UINT uMsg,
- WPARAM wParam,
- LPARAM lParam)
- {
- ASSERT(IsWindow(hwnd));
- if (PossiblyEatMessage(uMsg, wParam, lParam))
- return 0;
-
-
-
-
-
- if (uMsg == m_ShowStageMessage) {
- BOOL bVisible = IsWindowVisible(hwnd);
- SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
- SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW |
- (bVisible ? SWP_NOACTIVATE : 0));
-
- if (wParam == TRUE) {
- SetForegroundWindow(hwnd);
- }
- return (LRESULT) 1;
- }
-
-
-
-
-
- if (uMsg == m_ShowStageTop) {
- HWND HwndTop = (wParam == TRUE ? HWND_TOPMOST : HWND_NOTOPMOST);
- BOOL bVisible = IsWindowVisible(hwnd);
- SetWindowPos(hwnd, HwndTop, 0, 0, 0, 0,
- SWP_NOMOVE | SWP_NOSIZE |
- (wParam == TRUE ? SWP_SHOWWINDOW : 0) |
- (bVisible ? SWP_NOACTIVATE : 0));
- return (LRESULT) 1;
- }
-
- if (uMsg == m_RealizePalette) {
- ASSERT(m_hwnd == hwnd);
- return OnPaletteChange(m_hwnd,WM_QUERYNEWPALETTE);
- }
- switch (uMsg) {
-
- case WM_SYSCOLORCHANGE:
- InvalidateRect(hwnd,NULL,FALSE);
- return (LRESULT) 1;
-
- case WM_PALETTECHANGED:
- OnPaletteChange((HWND)wParam,uMsg);
- return (LRESULT) 0;
-
-
-
- case WM_QUERYNEWPALETTE:
- ASSERT(m_hwnd == hwnd);
- return OnPaletteChange(m_hwnd,uMsg);
-
-
-
-
- case WM_MOVE:
- if (IsWindowVisible(m_hwnd)) {
- PostMessage(m_hwnd,WM_PAINT,0,0);
- }
- break;
-
- case WM_SIZE:
- OnSize(LOWORD(lParam), HIWORD(lParam));
- return (LRESULT) 0;
-
- case WM_CLOSE:
- OnClose();
- return (LRESULT) 0;
- }
- return DefWindowProc(hwnd,uMsg,wParam,lParam);
- }
- LRESULT CBaseWindow::OnPaletteChange(HWND hwnd,UINT Message)
- {
-
- if (m_hwnd == NULL || hwnd == NULL) {
- return (LRESULT) 0;
- }
- ASSERT(!m_bRealizing);
-
- if ((Message == WM_QUERYNEWPALETTE || hwnd != m_hwnd)) {
-
-
-
-
- if (!IsWindowVisible(m_hwnd)) {
- DbgLog((LOG_TRACE, 1, TEXT("Realizing when invisible!")));
- return (LRESULT) 0;
- }
-
- #ifdef DEBUG
- m_bRealizing = TRUE;
- #endif
- DoRealisePalette(Message != WM_QUERYNEWPALETTE);
- #ifdef DEBUG
- m_bRealizing = FALSE;
- #endif
-
- if (Message == WM_PALETTECHANGED) {
- InvalidateRect(m_hwnd,NULL,FALSE);
- }
- }
- return (LRESULT) 1;
- }
- bool CBaseWindow::WindowExists()
- {
- return !!IsWindow(m_hwnd);
- }
- RECT CBaseWindow::GetDefaultRect()
- {
- RECT DefaultRect = {0,0,DEFWIDTH,DEFHEIGHT};
- ASSERT(m_hwnd);
-
- return DefaultRect;
- }
- LONG CBaseWindow::GetWindowWidth()
- {
- ASSERT(m_hwnd);
-
- return m_Width;
- }
- LONG CBaseWindow::GetWindowHeight()
- {
- ASSERT(m_hwnd);
-
- return m_Height;
- }
- HWND CBaseWindow::GetWindowHWND()
- {
- ASSERT(m_hwnd);
-
- return m_hwnd;
- }
- HDC CBaseWindow::GetWindowHDC()
- {
- ASSERT(m_hwnd);
- ASSERT(m_hdc);
- return m_hdc;
- }
- HDC CBaseWindow::GetMemoryHDC()
- {
- ASSERT(m_hwnd);
- ASSERT(m_MemoryDC);
- return m_MemoryDC;
- }
- #ifdef DEBUG
- HPALETTE CBaseWindow::GetPalette()
- {
-
-
- ASSERT(CritCheckIn(&m_PaletteLock));
- return m_hPalette;
- }
- #endif
- HRESULT CBaseWindow::DoShowWindow(LONG ShowCmd)
- {
- ShowWindow(m_hwnd,ShowCmd);
- return NOERROR;
- }
- void CBaseWindow::PaintWindow(BOOL bErase)
- {
- InvalidateRect(m_hwnd,NULL,bErase);
- }
- void CBaseWindow::DoSetWindowForeground(BOOL bFocus)
- {
- SendMessage(m_hwnd,m_ShowStageMessage,(WPARAM) bFocus,(LPARAM) 0);
- }
- CDrawImage::CDrawImage(__inout CBaseWindow *pBaseWindow) :
- m_pBaseWindow(pBaseWindow),
- m_hdc(NULL),
- m_MemoryDC(NULL),
- m_bStretch(FALSE),
- m_pMediaType(NULL),
- m_bUsingImageAllocator(FALSE)
- {
- ASSERT(pBaseWindow);
- ResetPaletteVersion();
- SetRectEmpty(&m_TargetRect);
- SetRectEmpty(&m_SourceRect);
- m_perfidRenderTime = MSR_REGISTER(TEXT("Single Blt time"));
- }
- void CDrawImage::DisplaySampleTimes(IMediaSample *pSample)
- {
- #ifdef DEBUG
-
-
-
-
- BOOL bAccept = DbgCheckModuleLevel(LOG_TRACE, 5);
- if (bAccept == FALSE) {
- return;
- }
- #endif
- TCHAR szTimes[TIMELENGTH];
- ASSERT(pSample);
- RECT ClientRect;
- SIZE Size;
-
- pSample->GetTime((REFERENCE_TIME*)&m_StartSample, (REFERENCE_TIME*)&m_EndSample);
- HWND hwnd = m_pBaseWindow->GetWindowHWND();
- EXECUTE_ASSERT(GetClientRect(hwnd,&ClientRect));
-
- (void)StringCchPrintf(szTimes,NUMELMS(szTimes),TEXT("%08d : %08d"),
- m_StartSample.Millisecs(),
- m_EndSample.Millisecs());
- ASSERT(lstrlen(szTimes) < TIMELENGTH);
-
- GetTextExtentPoint32(m_hdc,szTimes,lstrlen(szTimes),&Size);
- INT XPos = ((ClientRect.right - ClientRect.left) - Size.cx) / 2;
- INT YPos = ((ClientRect.bottom - ClientRect.top) - Size.cy) * 4 / 5;
-
- if ((XPos > 0) && (YPos > 0)) {
- TextOut(m_hdc,XPos,YPos,szTimes,lstrlen(szTimes));
- }
- }
- void CDrawImage::UpdateColourTable(HDC hdc,__in BITMAPINFOHEADER *pbmi)
- {
- ASSERT(pbmi->biClrUsed);
- RGBQUAD *pColourTable = (RGBQUAD *)(pbmi+1);
-
- UINT uiReturn = SetDIBColorTable(hdc,(UINT) 0,
- pbmi->biClrUsed,
- pColourTable);
-
- ASSERT(uiReturn == pbmi->biClrUsed);
- }
- RECT CDrawImage::ScaleSourceRect(const RECT *pSource)
- {
- ASSERT(pSource);
- return *pSource;
- }
- void CDrawImage::FastRender(IMediaSample *pMediaSample)
- {
- BITMAPINFOHEADER *pbmi;
- DIBDATA *pDibData;
- BYTE *pImage;
- HBITMAP hOldBitmap;
- CImageSample *pSample;
- ASSERT(m_pMediaType);
-
-
-
- pbmi = HEADER(m_pMediaType->Format());
- pSample = (CImageSample *) pMediaSample;
- pDibData = pSample->GetDIBData();
- hOldBitmap = (HBITMAP) SelectObject(m_MemoryDC,pDibData->hBitmap);
-
- HRESULT hr = pMediaSample->GetPointer(&pImage);
- if (FAILED(hr)) {
- return;
- }
-
-
-
-
-
- if (pDibData->PaletteVersion < GetPaletteVersion()) {
- ASSERT(pbmi->biBitCount <= iPALETTE);
- UpdateColourTable(m_MemoryDC,pbmi);
- pDibData->PaletteVersion = GetPaletteVersion();
- }
-
-
-
-
-
-
- RECT SourceRect = ScaleSourceRect(&m_SourceRect);
-
- if (m_bStretch == FALSE) {
-
- BitBlt(
- (HDC) m_hdc,
- m_TargetRect.left,
- m_TargetRect.top,
- m_TargetRect.right - m_TargetRect.left,
- m_TargetRect.bottom - m_TargetRect.top,
- m_MemoryDC,
- SourceRect.left,
- SourceRect.top,
- SRCCOPY);
- } else {
-
- StretchBlt(
- (HDC) m_hdc,
- m_TargetRect.left,
- m_TargetRect.top,
- m_TargetRect.right - m_TargetRect.left,
- m_TargetRect.bottom - m_TargetRect.top,
- m_MemoryDC,
- SourceRect.left,
- SourceRect.top,
- SourceRect.right - SourceRect.left,
- SourceRect.bottom - SourceRect.top,
- SRCCOPY);
- }
-
-
-
- #ifdef DEBUG
- DisplaySampleTimes(pMediaSample);
- #endif
-
- SelectObject(m_MemoryDC,hOldBitmap);
- }
- void CDrawImage::SlowRender(IMediaSample *pMediaSample)
- {
-
- ASSERT(m_pMediaType);
- BITMAPINFOHEADER *pbmi = HEADER(m_pMediaType->Format());
- BYTE *pImage;
-
- HRESULT hr = pMediaSample->GetPointer(&pImage);
- if (FAILED(hr)) {
- return;
- }
-
-
-
-
-
-
- RECT SourceRect = ScaleSourceRect(&m_SourceRect);
- LONG lAdjustedSourceTop = SourceRect.top;
-
-
- if (pbmi->biHeight > 0) {
- lAdjustedSourceTop = pbmi->biHeight - SourceRect.bottom;
- }
-
- if (m_bStretch == FALSE) {
-
- SetDIBitsToDevice(
- (HDC) m_hdc,
- m_TargetRect.left,
- m_TargetRect.top,
- m_TargetRect.right - m_TargetRect.left,
- m_TargetRect.bottom - m_TargetRect.top,
- SourceRect.left,
- lAdjustedSourceTop,
- (UINT) 0,
- pbmi->biHeight,
- pImage,
- (BITMAPINFO *) pbmi,
- DIB_RGB_COLORS);
- } else {
-
- StretchDIBits(
- (HDC) m_hdc,
- m_TargetRect.left,
- m_TargetRect.top,
- m_TargetRect.right - m_TargetRect.left,
- m_TargetRect.bottom - m_TargetRect.top,
- SourceRect.left,
- lAdjustedSourceTop,
- SourceRect.right - SourceRect.left,
- SourceRect.bottom - SourceRect.top,
- pImage,
- (BITMAPINFO *) pbmi,
- DIB_RGB_COLORS,
- SRCCOPY);
- }
-
-
-
-
-
-
-
- #ifdef DEBUG
- DisplaySampleTimes(pMediaSample);
- #endif
- }
- BOOL CDrawImage::DrawImage(IMediaSample *pMediaSample)
- {
- ASSERT(m_hdc);
- ASSERT(m_MemoryDC);
- NotifyStartDraw();
-
-
-
-
- if (m_bUsingImageAllocator == FALSE) {
- SlowRender(pMediaSample);
- EXECUTE_ASSERT(GdiFlush());
- NotifyEndDraw();
- return TRUE;
- }
-
- FastRender(pMediaSample);
- EXECUTE_ASSERT(GdiFlush());
- NotifyEndDraw();
- return TRUE;
- }
- BOOL CDrawImage::DrawVideoImageHere(
- HDC hdc,
- IMediaSample *pMediaSample,
- __in LPRECT lprcSrc,
- __in LPRECT lprcDst
- )
- {
- ASSERT(m_pMediaType);
- BITMAPINFOHEADER *pbmi = HEADER(m_pMediaType->Format());
- BYTE *pImage;
-
- HRESULT hr = pMediaSample->GetPointer(&pImage);
- if (FAILED(hr)) {
- return FALSE;
- }
- RECT SourceRect;
- RECT TargetRect;
- if (lprcSrc) {
- SourceRect = *lprcSrc;
- }
- else SourceRect = ScaleSourceRect(&m_SourceRect);
- if (lprcDst) {
- TargetRect = *lprcDst;
- }
- else TargetRect = m_TargetRect;
- LONG lAdjustedSourceTop = SourceRect.top;
-
-
- if (pbmi->biHeight > 0) {
- lAdjustedSourceTop = pbmi->biHeight - SourceRect.bottom;
- }
-
- BOOL bRet = (0 != StretchDIBits(hdc,
- TargetRect.left,
- TargetRect.top,
- TargetRect.right - TargetRect.left,
- TargetRect.bottom - TargetRect.top,
- SourceRect.left,
- lAdjustedSourceTop,
- SourceRect.right - SourceRect.left,
- SourceRect.bottom - SourceRect.top,
- pImage,
- (BITMAPINFO *)pbmi,
- DIB_RGB_COLORS,
- SRCCOPY));
- return bRet;
- }
- void CDrawImage::SetDrawContext()
- {
- m_MemoryDC = m_pBaseWindow->GetMemoryHDC();
- m_hdc = m_pBaseWindow->GetWindowHDC();
- }
- void CDrawImage::SetTargetRect(__in RECT *pTargetRect)
- {
- ASSERT(pTargetRect);
- m_TargetRect = *pTargetRect;
- SetStretchMode();
- }
- void CDrawImage::GetTargetRect(__out RECT *pTargetRect)
- {
- ASSERT(pTargetRect);
- *pTargetRect = m_TargetRect;
- }
- void CDrawImage::SetSourceRect(__in RECT *pSourceRect)
- {
- ASSERT(pSourceRect);
- m_SourceRect = *pSourceRect;
- SetStretchMode();
- }
- void CDrawImage::GetSourceRect(__out RECT *pSourceRect)
- {
- ASSERT(pSourceRect);
- *pSourceRect = m_SourceRect;
- }
- void CDrawImage::SetStretchMode()
- {
-
- LONG SourceWidth = m_SourceRect.right - m_SourceRect.left;
- LONG SinkWidth = m_TargetRect.right - m_TargetRect.left;
- LONG SourceHeight = m_SourceRect.bottom - m_SourceRect.top;
- LONG SinkHeight = m_TargetRect.bottom - m_TargetRect.top;
- m_bStretch = TRUE;
- if (SourceWidth == SinkWidth) {
- if (SourceHeight == SinkHeight) {
- m_bStretch = FALSE;
- }
- }
- }
- void CDrawImage::NotifyAllocator(BOOL bUsingImageAllocator)
- {
- m_bUsingImageAllocator = bUsingImageAllocator;
- }
- BOOL CDrawImage::UsingImageAllocator()
- {
- return m_bUsingImageAllocator;
- }
- void CDrawImage::NotifyMediaType(__in CMediaType *pMediaType)
- {
- m_pMediaType = pMediaType;
- }
- LONG CDrawImage::GetPaletteVersion()
- {
- return m_PaletteVersion;
- }
- void CDrawImage::ResetPaletteVersion()
- {
- m_PaletteVersion = PALETTE_VERSION;
- }
- void CDrawImage::IncrementPaletteVersion()
- {
- m_PaletteVersion++;
- }
- CImageAllocator::CImageAllocator(__inout CBaseFilter *pFilter,
- __in_opt LPCTSTR pName,
- __inout HRESULT *phr) :
- CBaseAllocator(pName,NULL,phr,TRUE,TRUE),
- m_pFilter(pFilter)
- {
- ASSERT(phr);
- ASSERT(pFilter);
- }
- #ifdef DEBUG
- CImageAllocator::~CImageAllocator()
- {
- ASSERT(m_bCommitted == FALSE);
- }
- #endif
- void CImageAllocator::Free()
- {
- ASSERT(m_lAllocated == m_lFree.GetCount());
- EXECUTE_ASSERT(GdiFlush());
- CImageSample *pSample;
- DIBDATA *pDibData;
- while (m_lFree.GetCount() != 0) {
- pSample = (CImageSample *) m_lFree.RemoveHead();
- pDibData = pSample->GetDIBData();
- EXECUTE_ASSERT(DeleteObject(pDibData->hBitmap));
- EXECUTE_ASSERT(CloseHandle(pDibData->hMapping));
- delete pSample;
- }
- m_lAllocated = 0;
- }
- STDMETHODIMP CImageAllocator::CheckSizes(__in ALLOCATOR_PROPERTIES *pRequest)
- {
-
- if (m_pMediaType == NULL) {
- return VFW_E_NOT_CONNECTED;
- }
-
-
-
-
- VIDEOINFOHEADER *pVideoInfo = (VIDEOINFOHEADER *) m_pMediaType->Format();
-
-
-
-
- if ((DWORD) pRequest->cbBuffer < pVideoInfo->bmiHeader.biSizeImage) {
- return E_INVALIDARG;
- }
-
- if (pRequest->cbPrefix > 0) {
- return E_INVALIDARG;
- }
- pRequest->cbBuffer = pVideoInfo->bmiHeader.biSizeImage;
- return NOERROR;
- }
- STDMETHODIMP CImageAllocator::SetProperties(
- __in ALLOCATOR_PROPERTIES * pRequest,
- __out ALLOCATOR_PROPERTIES * pActual)
- {
- ALLOCATOR_PROPERTIES Adjusted = *pRequest;
-
- HRESULT hr = CheckSizes(&Adjusted);
- if (FAILED(hr)) {
- return hr;
- }
- return CBaseAllocator::SetProperties(&Adjusted, pActual);
- }
- HRESULT CImageAllocator::Alloc(void)
- {
- ASSERT(m_pMediaType);
- CImageSample *pSample;
- DIBDATA DibData;
-
- HRESULT hr = CBaseAllocator::Alloc();
- if (FAILED(hr)) {
- return hr;
- }
-
-
-
-
- ASSERT(m_lAllocated == 0);
- while (m_lAllocated < m_lCount) {
-
- hr = CreateDIB(m_lSize,DibData);
- if (FAILED(hr)) {
- return hr;
- }
-
- pSample = CreateImageSample(DibData.pBase,m_lSize);
- if (pSample == NULL) {
- EXECUTE_ASSERT(DeleteObject(DibData.hBitmap));
- EXECUTE_ASSERT(CloseHandle(DibData.hMapping));
- return E_OUTOFMEMORY;
- }
-
- pSample->SetDIBData(&DibData);
- m_lFree.Add(pSample);
- m_lAllocated++;
- }
- return NOERROR;
- }
- CImageSample *CImageAllocator::CreateImageSample(__in_bcount(Length) LPBYTE pData,LONG Length)
- {
- HRESULT hr = NOERROR;
- CImageSample *pSample;
-
- pSample = new CImageSample((CBaseAllocator *) this,
- NAME("Video sample"),
- (HRESULT *) &hr,
- (LPBYTE) pData,
- (LONG) Length);
- if (pSample == NULL || FAILED(hr)) {
- delete pSample;
- return NULL;
- }
- return pSample;
- }
- HRESULT CImageAllocator::CreateDIB(LONG InSize,DIBDATA &DibData)
- {
- BITMAPINFO *pbmi;
- BYTE *pBase;
- HANDLE hMapping;
- HBITMAP hBitmap;
-
- hMapping = CreateFileMapping(hMEMORY,
- NULL,
- PAGE_READWRITE,
- (DWORD) 0,
- InSize,
- NULL);
- if (hMapping == NULL) {
- DWORD Error = GetLastError();
- return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, Error);
- }
-
-
-
-
- pbmi = (BITMAPINFO *) HEADER(m_pMediaType->Format());
- if (m_pMediaType == NULL) {
- DbgBreak("Invalid media type");
- }
- hBitmap = CreateDIBSection((HDC) NULL,
- pbmi,
- DIB_RGB_COLORS,
- (VOID **) &pBase,
- hMapping,
- (DWORD) 0);
- if (hBitmap == NULL || pBase == NULL) {
- EXECUTE_ASSERT(CloseHandle(hMapping));
- DWORD Error = GetLastError();
- return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, Error);
- }
-
- DibData.hBitmap = hBitmap;
- DibData.hMapping = hMapping;
- DibData.pBase = pBase;
- DibData.PaletteVersion = PALETTE_VERSION;
- GetObject(hBitmap,sizeof(DIBSECTION),(VOID *)&DibData.DibSection);
- return NOERROR;
- }
- void CImageAllocator::NotifyMediaType(__in CMediaType *pMediaType)
- {
- m_pMediaType = pMediaType;
- }
- STDMETHODIMP_(ULONG) CImageAllocator::NonDelegatingAddRef()
- {
- return m_pFilter->AddRef();
- }
- STDMETHODIMP_(ULONG) CImageAllocator::NonDelegatingRelease()
- {
- return m_pFilter->Release();
- }
- CImageSample::CImageSample(__inout CBaseAllocator *pAllocator,
- __in_opt LPCTSTR pName,
- __inout HRESULT *phr,
- __in_bcount(length) LPBYTE pBuffer,
- LONG length) :
- CMediaSample(pName,pAllocator,phr,pBuffer,length),
- m_bInit(FALSE)
- {
- ASSERT(pAllocator);
- ASSERT(pBuffer);
- }
- void CImageSample::SetDIBData(__in DIBDATA *pDibData)
- {
- ASSERT(pDibData);
- m_DibData = *pDibData;
- m_bInit = TRUE;
- }
- __out DIBDATA *CImageSample::GetDIBData()
- {
- ASSERT(m_bInit == TRUE);
- return &m_DibData;
- }
- CImagePalette::CImagePalette(__inout CBaseFilter *pBaseFilter,
- __inout CBaseWindow *pBaseWindow,
- __inout CDrawImage *pDrawImage) :
- m_pBaseWindow(pBaseWindow),
- m_pFilter(pBaseFilter),
- m_pDrawImage(pDrawImage),
- m_hPalette(NULL)
- {
- ASSERT(m_pFilter);
- }
- #ifdef DEBUG
- CImagePalette::~CImagePalette()
- {
- ASSERT(m_hPalette == NULL);
- }
- #endif
- BOOL CImagePalette::ShouldUpdate(const VIDEOINFOHEADER *pNewInfo,
- const VIDEOINFOHEADER *pOldInfo)
- {
-
- if (pOldInfo == NULL) {
- return TRUE;
- }
-
- if (ContainsPalette(pNewInfo) == FALSE) {
- if (ContainsPalette(pOldInfo) == FALSE) {
- return FALSE;
- }
- }
-
- DWORD VideoEntries = pNewInfo->bmiHeader.biClrUsed;
- if (ContainsPalette(pNewInfo) == TRUE)
- if (ContainsPalette(pOldInfo) == TRUE)
- if (pOldInfo->bmiHeader.biClrUsed == VideoEntries)
- if (pOldInfo->bmiHeader.biClrUsed > 0)
- if (memcmp((PVOID) GetBitmapPalette(pNewInfo),
- (PVOID) GetBitmapPalette(pOldInfo),
- VideoEntries * sizeof(RGBQUAD)) == 0) {
- return FALSE;
- }
- return TRUE;
- }
- HRESULT CImagePalette::PreparePalette(const CMediaType *pmtNew,
- const CMediaType *pmtOld,
- __in LPSTR szDevice)
- {
- const VIDEOINFOHEADER *pNewInfo = (VIDEOINFOHEADER *) pmtNew->Format();
- const VIDEOINFOHEADER *pOldInfo = (VIDEOINFOHEADER *) pmtOld->Format();
- ASSERT(pNewInfo);
-
-
-
-
- if (ShouldUpdate(pNewInfo,pOldInfo) == FALSE) {
- NOTE("No update needed");
- return S_FALSE;
- }
-
-
-
-
- RemovePalette();
- m_pFilter->NotifyEvent(EC_PALETTE_CHANGED,0,0);
-
- if (ContainsPalette(pNewInfo) == FALSE) {
- NOTE("New has no palette");
- return S_FALSE;
- }
- if (m_pBaseWindow) {
- m_pBaseWindow->LockPaletteLock();
- }
-
-
-
-
- NOTE("Making new colour palette");
- m_hPalette = MakePalette(pNewInfo, szDevice);
- ASSERT(m_hPalette != NULL);
- if (m_pBaseWindow) {
- m_pBaseWindow->UnlockPaletteLock();
- }
-
-
-
- if (m_pBaseWindow) m_pBaseWindow->SetPalette(m_hPalette);
-
-
-
- if (m_pDrawImage) m_pDrawImage->IncrementPaletteVersion();
- return NOERROR;
- }
- HRESULT CImagePalette::CopyPalette(const CMediaType *pSrc,__out CMediaType *pDest)
- {
-
- VIDEOINFOHEADER *pDestInfo = (VIDEOINFOHEADER *) pDest->Format();
- pDestInfo->bmiHeader.biClrUsed = 0;
- pDestInfo->bmiHeader.biClrImportant = 0;
-
- if (PALETTISED(pDestInfo) == FALSE) {
- NOTE("No destination palette");
- return S_FALSE;
- }
-
- const VIDEOINFOHEADER *pSrcInfo = (VIDEOINFOHEADER *) pSrc->Format();
- if (ContainsPalette(pSrcInfo) == FALSE) {
- NOTE("No source palette");
- return S_FALSE;
- }
-
- DWORD PaletteEntries = pSrcInfo->bmiHeader.biClrUsed;
- if (PaletteEntries == 0) {
- DWORD Maximum = (1 << pSrcInfo->bmiHeader.biBitCount);
- NOTE1("Setting maximum colours (%d)",Maximum);
- PaletteEntries = Maximum;
- }
-
- ASSERT(pSrcInfo->bmiHeader.biClrUsed <= iPALETTE_COLORS);
- ASSERT(pSrcInfo->bmiHeader.biClrImportant <= PaletteEntries);
- ASSERT(COLORS(pDestInfo) == GetBitmapPalette(pDestInfo));
- pDestInfo->bmiHeader.biClrUsed = PaletteEntries;
- pDestInfo->bmiHeader.biClrImportant = pSrcInfo->bmiHeader.biClrImportant;
- ULONG BitmapSize = GetBitmapFormatSize(HEADER(pSrcInfo));
- if (pDest->FormatLength() < BitmapSize) {
- NOTE("Reallocating destination");
- pDest->ReallocFormatBuffer(BitmapSize);
- }
-
- CopyMemory((PVOID) COLORS(pDestInfo),
- (PVOID) GetBitmapPalette(pSrcInfo),
- PaletteEntries * sizeof(RGBQUAD));
- return NOERROR;
- }
- HRESULT CImagePalette::RemovePalette()
- {
- if (m_pBaseWindow) {
- m_pBaseWindow->LockPaletteLock();
- }
-
- if (m_hPalette != NULL) {
- if (m_pBaseWindow) {
-
-
- ASSERT(m_hPalette == m_pBaseWindow->GetPalette());
- m_pBaseWindow->UnsetPalette();
- }
- EXECUTE_ASSERT(DeleteObject(m_hPalette));
- m_hPalette = NULL;
- }
- if (m_pBaseWindow) {
- m_pBaseWindow->UnlockPaletteLock();
- }
- return NOERROR;
- }
- HPALETTE CImagePalette::MakePalette(const VIDEOINFOHEADER *pVideoInfo, __in LPSTR szDevice)
- {
- ASSERT(ContainsPalette(pVideoInfo) == TRUE);
- ASSERT(pVideoInfo->bmiHeader.biClrUsed <= iPALETTE_COLORS);
- BITMAPINFOHEADER *pHeader = HEADER(pVideoInfo);
- const RGBQUAD *pColours;
- LOGPALETTE *lp;
- HPALETTE hPalette;
- lp = (LOGPALETTE *) new BYTE[sizeof(LOGPALETTE) + SIZE_PALETTE];
- if (lp == NULL) {
- return NULL;
- }
-
-
-
-
-
- lp->palVersion = PALVERSION;
- lp->palNumEntries = (USHORT) pHeader->biClrUsed;
- if (lp->palNumEntries == 0) lp->palNumEntries = (1 << pHeader->biBitCount);
- pColours = GetBitmapPalette(pVideoInfo);
- for (DWORD dwCount = 0;dwCount < lp->palNumEntries;dwCount++) {
- lp->palPalEntry[dwCount].peRed = pColours[dwCount].rgbRed;
- lp->palPalEntry[dwCount].peGreen = pColours[dwCount].rgbGreen;
- lp->palPalEntry[dwCount].peBlue = pColours[dwCount].rgbBlue;
- lp->palPalEntry[dwCount].peFlags = 0;
- }
- MakeIdentityPalette(lp->palPalEntry, lp->palNumEntries, szDevice);
-
- hPalette = CreatePalette(lp);
- ASSERT(hPalette != NULL);
- delete[] lp;
- return hPalette;
- }
- HRESULT CImagePalette::MakeIdentityPalette(__inout_ecount_full(iColours) PALETTEENTRY *pEntry,INT iColours, __in LPSTR szDevice)
- {
- PALETTEENTRY SystemEntries[10];
- BOOL bIdentityPalette = TRUE;
- ASSERT(iColours <= iPALETTE_COLORS);
- const int PalLoCount = 10;
- const int PalHiStart = 246;
-
- if (iColours < 10) {
- return S_FALSE;
- }
-
-
-
- HDC hdc;
- if (szDevice == NULL || lstrcmpiLocaleIndependentA(szDevice, "DISPLAY") == 0)
- hdc = CreateDCA("DISPLAY", NULL, NULL, NULL);
- else
- hdc = CreateDCA(NULL, szDevice, NULL, NULL);
- if (NULL == hdc) {
- return E_OUTOFMEMORY;
- }
- INT Reserved = GetDeviceCaps(hdc,NUMRESERVED);
- if (Reserved != 20) {
- DeleteDC(hdc);
- return S_FALSE;
- }
-
-
-
- UINT Result = GetSystemPaletteEntries(hdc,0,PalLoCount,SystemEntries);
- for (UINT Count = 0;Count < Result;Count++) {
- if (SystemEntries[Count].peRed != pEntry[Count].peRed ||
- SystemEntries[Count].peGreen != pEntry[Count].peGreen ||
- SystemEntries[Count].peBlue != pEntry[Count].peBlue) {
- bIdentityPalette = FALSE;
- }
- }
-
- Result = GetSystemPaletteEntries(hdc,PalHiStart,PalLoCount,SystemEntries);
- for (UINT Count = 0;Count < Result;Count++) {
- if (INT(Count) + PalHiStart < iColours) {
- if (SystemEntries[Count].peRed != pEntry[PalHiStart + Count].peRed ||
- SystemEntries[Count].peGreen != pEntry[PalHiStart + Count].peGreen ||
- SystemEntries[Count].peBlue != pEntry[PalHiStart + Count].peBlue) {
- bIdentityPalette = FALSE;
- }
- }
- }
-
- DeleteDC(hdc);
- if (bIdentityPalette == FALSE) {
- return S_FALSE;
- }
-
- for (UINT Count = PalLoCount;INT(Count) < min(PalHiStart,iColours);Count++) {
- pEntry[Count].peFlags = PC_NOCOLLAPSE;
- }
- return NOERROR;
- }
- CImageDisplay::CImageDisplay()
- {
- RefreshDisplayType(NULL);
- }
- HRESULT CImageDisplay::RefreshDisplayType(__in_opt LPSTR szDeviceName)
- {
- CAutoLock cDisplayLock(this);
-
- ZeroMemory((PVOID)&m_Display,sizeof(VIDEOINFOHEADER)+sizeof(TRUECOLORINFO));
- m_Display.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- m_Display.bmiHeader.biBitCount = FALSE;
-
-
- HDC hdcDisplay;
-
- if (szDeviceName == NULL || lstrcmpiLocaleIndependentA(szDeviceName, "DISPLAY") == 0)
- hdcDisplay = CreateDCA("DISPLAY", NULL, NULL, NULL);
- else
- hdcDisplay = CreateDCA(NULL, szDeviceName, NULL, NULL);
- if (hdcDisplay == NULL) {
- ASSERT(FALSE);
- DbgLog((LOG_ERROR,1,TEXT("ACK! Can't get a DC for %hs"),
- szDeviceName ? szDeviceName : "<NULL>"));
- return E_FAIL;
- } else {
- DbgLog((LOG_TRACE,3,TEXT("Created a DC for %s"),
- szDeviceName ? szDeviceName : "<NULL>"));
- }
- HBITMAP hbm = CreateCompatibleBitmap(hdcDisplay,1,1);
- if ( hbm )
- {
- GetDIBits(hdcDisplay,hbm,0,1,NULL,(BITMAPINFO *)&m_Display.bmiHeader,DIB_RGB_COLORS);
-
- GetDIBits(hdcDisplay,hbm,0,1,NULL,(BITMAPINFO *)&m_Display.bmiHeader,DIB_RGB_COLORS);
- DeleteObject(hbm);
- }
- DeleteDC(hdcDisplay);
-
- ASSERT(CheckHeaderValidity(&m_Display));
- UpdateFormat(&m_Display);
- DbgLog((LOG_TRACE,3,TEXT("New DISPLAY bit depth =%d"),
- m_Display.bmiHeader.biBitCount));
- return NOERROR;
- }
- BOOL CImageDisplay::CheckBitFields(const VIDEOINFO *pInput)
- {
- DWORD *pBitFields = (DWORD *) BITMASKS(pInput);
- for (INT iColour = iRED;iColour <= iBLUE;iColour++) {
-
- DWORD SetBits = CountSetBits(pBitFields[iColour]);
- if (SetBits > iMAXBITS || SetBits == 0) {
- NOTE1("Bit fields for component %d invalid",iColour);
- return FALSE;
- }
-
- DWORD PrefixBits = CountPrefixBits(pBitFields[iColour]);
-
-
-
-
-
-
- DWORD TestField = pBitFields[iColour] >> PrefixBits;
- DWORD Mask = ULONG_MAX << SetBits;
- if (TestField & Mask) {
- NOTE1("Bit fields for component %d not contiguous",iColour);
- return FALSE;
- }
- }
- return TRUE;
- }
- DWORD CImageDisplay::CountSetBits(DWORD Field)
- {
-
- DWORD Count = 0;
- DWORD init = Field;
-
- while (init) {
- init = init & (init - 1);
- Count++;
- }
- return Count;
- }
- DWORD CImageDisplay::CountPrefixBits(DWORD Field)
- {
- DWORD Mask = 1;
- DWORD Count = 0;
- while (TRUE) {
- if (Field & Mask) {
- return Count;
- }
- Count++;
- ASSERT(Mask != 0x80000000);
- if (Mask == 0x80000000) {
- return Count;
- }
- Mask <<= 1;
- }
- }
- BOOL CImageDisplay::CheckHeaderValidity(const VIDEOINFO *pInput)
- {
-
- if (pInput->bmiHeader.biWidth <= 0 ||
- pInput->bmiHeader.biHeight <= 0) {
- NOTE("Invalid bitmap dimensions");
- return FALSE;
- }
-
- if (pInput->bmiHeader.biCompression != BI_RGB) {
- if (pInput->bmiHeader.biCompression != BI_BITFIELDS) {
- NOTE("Invalid compression format");
- return FALSE;
- }
- }
-
- if (pInput->bmiHeader.biCompression == BI_BITFIELDS) {
- if (pInput->bmiHeader.biBitCount != 16) {
- if (pInput->bmiHeader.biBitCount != 32) {
- NOTE("BI_BITFIELDS not 16/32 bit depth");
- return FALSE;
- }
- }
- }
-
- if (pInput->bmiHeader.biCompression == BI_BITFIELDS) {
- if (CheckBitFields(pInput) == FALSE) {
- NOTE("Bit fields are not valid");
- return FALSE;
- }
- }
-
- if (pInput->bmiHeader.biPlanes != 1) {
- NOTE("Number of planes not one");
- return FALSE;
- }
-
- if (pInput->bmiHeader.biSizeImage != GetBitmapSize(&pInput->bmiHeader)) {
- if (pInput->bmiHeader.biSizeImage) {
- NOTE("Image size incorrectly set");
- return FALSE;
- }
- }
-
- if (pInput->bmiHeader.biSize != sizeof(BITMAPINFOHEADER)) {
- NOTE("Size of BITMAPINFOHEADER wrong");
- return FALSE;
- }
- return CheckPaletteHeader(pInput);
- }
- BOOL CImageDisplay::CheckPaletteHeader(const VIDEOINFO *pInput)
- {
-
- if (PALETTISED(pInput) == FALSE) {
- if (pInput->bmiHeader.biClrUsed) {
- NOTE("Invalid palette entries");
- return FALSE;
- }
- return TRUE;
- }
-
- if (pInput->bmiHeader.biCompression != BI_RGB) {
- NOTE("Palettised video must be BI_RGB");
- return FALSE;
- }
-
- if (pInput->bmiHeader.biClrUsed > PALETTE_ENTRIES(pInput)) {
- NOTE("Too many colours in palette");
- return FALSE;
- }
-
- if (pInput->bmiHeader.biClrImportant > pInput->bmiHeader.biClrUsed) {
- NOTE("Too many important colours");
- return FALSE;
- }
- return TRUE;
- }
- const VIDEOINFO *CImageDisplay::GetDisplayFormat()
- {
- return &m_Display;
- }
- BOOL CImageDisplay::IsPalettised()
- {
- return PALETTISED(&m_Display);
- }
- WORD CImageDisplay::GetDisplayDepth()
- {
- return m_Display.bmiHeader.biBitCount;
- }
- HRESULT CImageDisplay::UpdateFormat(__inout VIDEOINFO *pVideoInfo)
- {
- ASSERT(pVideoInfo);
- BITMAPINFOHEADER *pbmi = HEADER(pVideoInfo);
- SetRectEmpty(&pVideoInfo->rcSource);
- SetRectEmpty(&pVideoInfo->rcTarget);
-
- if (PALETTISED(pVideoInfo)) {
- if (pVideoInfo->bmiHeader.biClrUsed == 0) {
- pVideoInfo->bmiHeader.biClrUsed = PALETTE_ENTRIES(pVideoInfo);
- }
- }
-
-
-
- if (pVideoInfo->bmiHeader.biClrImportant > pVideoInfo->bmiHeader.biClrUsed) {
- pVideoInfo->bmiHeader.biClrImportant = PALETTE_ENTRIES(pVideoInfo);
- }
-
- if (pVideoInfo->bmiHeader.biSizeImage == 0) {
- pVideoInfo->bmiHeader.biSizeImage = GetBitmapSize(&pVideoInfo->bmiHeader);
- }
- return NOERROR;
- }
- HRESULT CImageDisplay::CheckVideoType(const VIDEOINFO *pInput)
- {
-
- if (CheckHeaderValidity(pInput) == FALSE) {
- return E_INVALIDARG;
- }
-
- if (m_Display.bmiHeader.biBitCount == pInput->bmiHeader.biBitCount) {
- if (PALETTISED(pInput) == TRUE) {
- ASSERT(PALETTISED(&m_Display) == TRUE);
- NOTE("(Video) Type connection ACCEPTED");
- return NOERROR;
- }
- }
-
- if (m_Display.bmiHeader.biBitCount > pInput->bmiHeader.biBitCount) {
- NOTE("(Video) Mismatch agreed");
- return NOERROR;
- }
-
- if (m_Display.bmiHeader.biBitCount < pInput->bmiHeader.biBitCount) {
- NOTE("(Video) Format mismatch");
- return E_INVALIDARG;
- }
-
- ASSERT(m_Display.bmiHeader.biBitCount == pInput->bmiHeader.biBitCount);
- ASSERT(PALETTISED(pInput) == FALSE);
- ASSERT(PALETTISED(&m_Display) == FALSE);
-
-
-
-
- const DWORD *pInputMask = GetBitMasks(pInput);
- const DWORD *pDisplayMask = GetBitMasks((VIDEOINFO *)&m_Display);
- if (pInputMask[iRED] != pDisplayMask[iRED] ||
- pInputMask[iGREEN] != pDisplayMask[iGREEN] ||
- pInputMask[iBLUE] != pDisplayMask[iBLUE]) {
- NOTE("(Video) Bit field mismatch");
- return E_INVALIDARG;
- }
- NOTE("(Video) Type connection ACCEPTED");
- return NOERROR;
- }
- const DWORD *CImageDisplay::GetBitMasks(const VIDEOINFO *pVideoInfo)
- {
- static const DWORD FailMasks[] = {0,0,0};
- if (pVideoInfo->bmiHeader.biCompression == BI_BITFIELDS) {
- return BITMASKS(pVideoInfo);
- }
- ASSERT(pVideoInfo->bmiHeader.biCompression == BI_RGB);
- switch (pVideoInfo->bmiHeader.biBitCount) {
- case 16: return bits555;
- case 24: return bits888;
- case 32: return bits888;
- default: return FailMasks;
- }
- }
- HRESULT CImageDisplay::CheckMediaType(const CMediaType *pmtIn)
- {
-
- const GUID *pFormatType = pmtIn->FormatType();
- if (*pFormatType != FORMAT_VideoInfo) {
- NOTE("Format GUID not a VIDEOINFOHEADER");
- return E_INVALIDARG;
- }
- ASSERT(pmtIn->Format());
-
- ULONG Length = pmtIn->FormatLength();
- if (Length < SIZE_VIDEOHEADER) {
- NOTE("Format smaller than a VIDEOHEADER");
- return E_FAIL;
- }
- VIDEOINFO *pInput = (VIDEOINFO *) pmtIn->Format();
-
- const GUID *pMajorType = pmtIn->Type();
- if (*pMajorType != MEDIATYPE_Video) {
- NOTE("Major type not MEDIATYPE_Video");
- return E_INVALIDARG;
- }
-
- const GUID *pSubType = pmtIn->Subtype();
- if (GetBitCount(pSubType) == USHRT_MAX) {
- NOTE("Invalid video media subtype");
- return E_INVALIDARG;
- }
- return CheckVideoType(pInput);
- }
- BOOL CImageDisplay::GetColourMask(__out DWORD *pMaskRed,
- __out DWORD *pMaskGreen,
- __out DWORD *pMaskBlue)
- {
- CAutoLock cDisplayLock(this);
- *pMaskRed = 0xFF;
- *pMaskGreen = 0xFF;
- *pMaskBlue = 0xFF;
-
- if (m_Display.bmiHeader.biBitCount < 16) {
- return FALSE;
- }
-
-
-
- if (m_Display.bmiHeader.biBitCount == 24) {
- ASSERT(m_Display.bmiHeader.biCompression == BI_RGB);
- return TRUE;
- }
-
- const DWORD *pBitFields = (DWORD *) GetBitMasks((VIDEOINFO *)&m_Display);
- DWORD *pOutputMask[] = { pMaskRed, pMaskGreen, pMaskBlue };
-
-
-
- for (INT iColour = iRED;iColour <= iBLUE;iColour++) {
-
- DWORD PrefixBits = CountPrefixBits(pBitFields[iColour]);
- DWORD SetBits = CountSetBits(pBitFields[iColour]);
-
-
-
- *(pOutputMask[iColour]) = pBitFields[iColour] >> PrefixBits;
- *(pOutputMask[iColour]) <<= (iMAXBITS - SetBits);
- }
- return TRUE;
- }
- STDAPI ConvertVideoInfoToVideoInfo2(__inout AM_MEDIA_TYPE *pmt)
- {
- if (pmt->formattype != FORMAT_VideoInfo) {
- return E_INVALIDARG;
- }
- if (NULL == pmt->pbFormat || pmt->cbFormat < sizeof(VIDEOINFOHEADER)) {
- return E_INVALIDARG;
- }
- VIDEOINFO *pVideoInfo = (VIDEOINFO *)pmt->pbFormat;
- DWORD dwNewSize;
- HRESULT hr = DWordAdd(pmt->cbFormat, sizeof(VIDEOINFOHEADER2) - sizeof(VIDEOINFOHEADER), &dwNewSize);
- if (FAILED(hr)) {
- return hr;
- }
- PVOID pvNew = CoTaskMemAlloc(dwNewSize);
- if (pvNew == NULL) {
- return E_OUTOFMEMORY;
- }
- CopyMemory(pvNew, pmt->pbFormat, FIELD_OFFSET(VIDEOINFOHEADER, bmiHeader));
- ZeroMemory((PBYTE)pvNew + FIELD_OFFSET(VIDEOINFOHEADER, bmiHeader),
- sizeof(VIDEOINFOHEADER2) - sizeof(VIDEOINFOHEADER));
- CopyMemory((PBYTE)pvNew + FIELD_OFFSET(VIDEOINFOHEADER2, bmiHeader),
- pmt->pbFormat + FIELD_OFFSET(VIDEOINFOHEADER, bmiHeader),
- pmt->cbFormat - FIELD_OFFSET(VIDEOINFOHEADER, bmiHeader));
- VIDEOINFOHEADER2 *pVideoInfo2 = (VIDEOINFOHEADER2 *)pvNew;
- pVideoInfo2->dwPictAspectRatioX = (DWORD)pVideoInfo2->bmiHeader.biWidth;
- pVideoInfo2->dwPictAspectRatioY = (DWORD)abs(pVideoInfo2->bmiHeader.biHeight);
- pmt->formattype = FORMAT_VideoInfo2;
- CoTaskMemFree(pmt->pbFormat);
- pmt->pbFormat = (PBYTE)pvNew;
- pmt->cbFormat += sizeof(VIDEOINFOHEADER2) - sizeof(VIDEOINFOHEADER);
- return S_OK;
- }
- STDAPI CheckVideoInfoType(const AM_MEDIA_TYPE *pmt)
- {
- if (NULL == pmt || NULL == pmt->pbFormat) {
- return E_POINTER;
- }
- if (pmt->majortype != MEDIATYPE_Video ||
- pmt->formattype != FORMAT_VideoInfo ||
- pmt->cbFormat < sizeof(VIDEOINFOHEADER)) {
- return VFW_E_TYPE_NOT_ACCEPTED;
- }
- const VIDEOINFOHEADER *pHeader = (const VIDEOINFOHEADER *)pmt->pbFormat;
- if (!ValidateBitmapInfoHeader(
- &pHeader->bmiHeader,
- pmt->cbFormat - FIELD_OFFSET(VIDEOINFOHEADER, bmiHeader))) {
- return VFW_E_TYPE_NOT_ACCEPTED;
- }
- return S_OK;
- }
- STDAPI CheckVideoInfo2Type(const AM_MEDIA_TYPE *pmt)
- {
- if (NULL == pmt || NULL == pmt->pbFormat) {
- return E_POINTER;
- }
- if (pmt->majortype != MEDIATYPE_Video ||
- pmt->formattype != FORMAT_VideoInfo2 ||
- pmt->cbFormat < sizeof(VIDEOINFOHEADER2)) {
- return VFW_E_TYPE_NOT_ACCEPTED;
- }
- const VIDEOINFOHEADER2 *pHeader = (const VIDEOINFOHEADER2 *)pmt->pbFormat;
- if (!ValidateBitmapInfoHeader(
- &pHeader->bmiHeader,
- pmt->cbFormat - FIELD_OFFSET(VIDEOINFOHEADER2, bmiHeader))) {
- return VFW_E_TYPE_NOT_ACCEPTED;
- }
- return S_OK;
- }
|