-
Notifications
You must be signed in to change notification settings - Fork 8
/
VideoWnd.cpp
628 lines (573 loc) · 15.5 KB
/
VideoWnd.cpp
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
// VideoWnd.cpp : implementation file
//
#include "StdAfx.h"
#include "VideoWnd.h"
#include <atlimage.h>
#include <atltypes.h>
#include <atlconv.h>
#include <VFW.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////////////////
// Win32Window
///////////////////////////////////////////////////////////////////////////////
static const wchar_t kWindowBaseClassName[] = L"QWindowClass";
HINSTANCE Win32Window::instance_ = NULL;
ATOM Win32Window::window_class_ = 0;
Win32Window::Win32Window() : wnd_(NULL) {
}
Win32Window::~Win32Window() {
ASSERT(NULL == wnd_);
}
bool Win32Window::Create(HWND parent, const wchar_t* title, DWORD style,
DWORD exstyle, int x, int y, int cx, int cy) {
if (wnd_) {
// Window already exists.
return false;
}
if (!window_class_) {
if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(&Win32Window::WndProc),
&instance_)) {
return false;
}
// Class not registered, register it.
WNDCLASSEX wcex;
memset(&wcex, 0, sizeof(wcex));
wcex.cbSize = sizeof(wcex);
wcex.hInstance = instance_;
wcex.lpfnWndProc = &Win32Window::WndProc;
wcex.lpszClassName = kWindowBaseClassName;
window_class_ = ::RegisterClassEx(&wcex);
if (!window_class_) {
return false;
}
}
wnd_ = ::CreateWindowEx(exstyle, kWindowBaseClassName, title, style,
x, y, cx, cy, parent, NULL, instance_, this);
return (NULL != wnd_);
}
void Win32Window::Destroy() {
::DestroyWindow(wnd_);
}
void Win32Window::Shutdown() {
if (window_class_) {
::UnregisterClass(MAKEINTATOM(window_class_), instance_);
window_class_ = 0;
}
}
bool Win32Window::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam,
LRESULT& result) {
switch (uMsg) {
case WM_CLOSE:
if (!OnClose()) {
result = 0;
return true;
}
break;
}
return false;
}
LRESULT Win32Window::WndProc(HWND hwnd, UINT uMsg,
WPARAM wParam, LPARAM lParam) {
Win32Window* that = reinterpret_cast<Win32Window*>(
::GetWindowLongPtr(hwnd, GWLP_USERDATA));
if (!that && (WM_CREATE == uMsg)) {
CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lParam);
that = static_cast<Win32Window*>(cs->lpCreateParams);
that->wnd_ = hwnd;
::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(that));
}
if (that) {
LRESULT result;
bool handled = that->OnMessage(uMsg, wParam, lParam, result);
if (WM_DESTROY == uMsg) {
for (HWND child = ::GetWindow(hwnd, GW_CHILD); child;
child = ::GetWindow(child, GW_HWNDNEXT)) {
}
}
if (WM_NCDESTROY == uMsg) {
::SetWindowLongPtr(hwnd, GWLP_USERDATA, NULL);
that->wnd_ = NULL;
that->OnNcDestroy();
}
if (handled) {
return result;
}
}
return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
}
const static int dpixeladd = 24 / 8;
inline int GetDIBColor(int nWidth, int nHeight, byte *Buffer, int X, int Y)
{
BYTE *pRgb = Buffer + dpixeladd * (X + (nHeight - 1 - Y) * nWidth);
return RGB(pRgb[2], pRgb[1], pRgb[0]);
}
inline int SetDIBColor(int nWidth, int nHeight, byte *Buffer, int X, int Y, int value)
{
BYTE *ptr = Buffer + dpixeladd*(X + (nHeight - 1 - Y) * nWidth);
*ptr = value >> 16;
*(ptr + 1) = value >> 8;
*(ptr + 2) = value & 0xFF;
return 1;
}
VOID RGBRotate90(BYTE *pSrc, BYTE* pDst, int &nHeight, int& nWidth)
{
for (int w = 0; w<nWidth; w++)
{
for (int h = 0; h<nHeight; h++)
{
int SrcR = GetDIBColor(nWidth, nHeight, pSrc, w, h);
SetDIBColor(nHeight, nWidth, pDst, nHeight - 1 - h, w, SrcR);
}
}
std::swap(nHeight, nWidth);
}
VOID RGBRotate180_(BYTE *pSrc, BYTE* pDst, int nHeight, int nWidth)
{
memcpy(pDst, pSrc, nHeight * nWidth * 3);
for (int h = 0; h<nHeight / 2; h++) //(nHeight/2)
{
for (int w = 0; w<nWidth; w++)//nWidth
{
int SrcR = GetDIBColor(nWidth, nHeight, pDst, w, h);
int DesR = GetDIBColor(nWidth, nHeight, pDst, nWidth - w - 1, nHeight - h - 1);
SetDIBColor(nWidth, nHeight, pDst, w, h, DesR);
SetDIBColor(nWidth, nHeight, pDst, nWidth - w - 1, nHeight - h - 1, SrcR);
}
}
}
VOID RGBRotate180(BYTE *pSrc, BYTE* pDst, int nHeight, int nWidth)
{
for (int h = 0; h<nHeight; h++)
{
for (int w = 0; w<nWidth; w++)//nWidth
{
int SrcR = GetDIBColor(nWidth, nHeight, pSrc, w, h);
SetDIBColor(nWidth, nHeight, pDst, nWidth - w - 1, nHeight - h - 1, SrcR);
}
}
}
VOID RGBRotate270(BYTE *pSrc, BYTE* pDst, int &nHeight, int &nWidth)
{
for (int w = 0; w<nWidth; w++)
{
for (int h = 0; h<nHeight; h++)
{
int SrcR = GetDIBColor(nWidth, nHeight, pSrc, w, h);
SetDIBColor(nHeight, nWidth, pDst, h, nWidth - w - 1, SrcR);
}
}
std::swap(nHeight, nWidth);
}
CVideoBuffer::CVideoBuffer()
{
m_buf = NULL;
m_w = 0;
m_h = 0;
}
CVideoBuffer::~CVideoBuffer()
{
CleanUpBuffer();
}
//分配符合视频大小的内存
BYTE *CVideoBuffer::GetBuffer(int w, int h)
{
m_mutex.lock();
void *ptr = NULL;
if (w == 0 || (w == m_w&&h == m_h))
{
//已经分配了要求大小的内存
ptr = m_buf;
goto RET;
}
int stride = (w * 3 + 3) / 4 * 4;
if (m_buf)
{
//
ptr = realloc(m_buf, stride*h);
if (ptr == NULL)
{
CleanUpBuffer();
}
}
else
{
ptr = malloc(stride*h);
}
if (ptr == NULL)
goto RET;
m_buf = ptr;
m_w = w;
m_h = h;
memset(m_buf, 0, stride*h);
RET:
if (!ptr)
m_mutex.unlock();
return (BYTE*)ptr;
}
void CVideoBuffer::ReleaseBuffer()
{
if (m_buf)
m_mutex.unlock();
}
void CVideoBuffer::CleanUpBuffer()
{
m_mutex.lock();
if (m_buf)
{
free(m_buf);
m_buf = NULL;
m_w = 0;
m_h = 0;
}
m_mutex.unlock();
}
/////////////////////////////////////////////////////////////////////////////
// CVideoWnd
static CImage defImage;
CVideoWnd::CVideoWnd()
{
memset(&m_bmi, 0, sizeof(m_bmi));
m_bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_bmi.bmiHeader.biPlanes = 1;
m_bmi.bmiHeader.biBitCount = 24;
m_bShowVol = FALSE;
m_bSizeChanged = FALSE;
m_bFit = TRUE;
m_nDefOrgin = 0;
m_clrBK = 0;
m_clrText = 0xFFFFFF;
m_nTextFmt = DT_BOTTOM | DT_SINGLELINE;
m_bUseGDI = TRUE;
m_hDrawDIB = NULL;
if (!m_bUseGDI) {
m_hDrawDIB = DrawDibOpen();
}
}
CVideoWnd::~CVideoWnd()
{
if (m_hDrawDIB) {
DrawDibClose(m_hDrawDIB);
m_hDrawDIB = NULL;
}
if (handle()) {
Destroy();
}
}
bool CVideoWnd::OnMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& result) {
switch (uMsg)
{
case WM_ERASEBKGND:
{
HDC dc = (HDC)wParam;
OnEraseBkgnd(dc);
result = 1;
return true;
}
break;
case WM_CREATE:
case WM_LBUTTONDBLCLK:
case WM_RBUTTONDOWN:
default:
return __super::OnMessage(uMsg, wParam, lParam, result);
break;
}
}
/////////////////////////////////////////////////////////////////////////////
// CVideoWnd message handlers
#include "Core\UIRender.h"
BOOL CVideoWnd::OnEraseBkgnd(HDC hDC)
{
CRect rc;
GetClientRect(handle(), rc);
BYTE *pBuffer = m_buffer.GetBuffer();
if (pBuffer)
{
if (m_bSizeChanged)
{
m_bSizeChanged = FALSE;
CRect rcWnd;
GetWindowRect(handle(), rcWnd);
//Make the client area fit video size
SetWindowPos(handle(), 0, 0, 0,
m_bmi.bmiHeader.biWidth + rcWnd.Width() - rc.Width(),
abs(m_bmi.bmiHeader.biHeight) + rcWnd.Height() - rc.Height(), SWP_NOMOVE);
}
else
{
::SetBkColor(hDC, m_clrBK);
RECT rcBK;
if (!m_bFit) {
//绘制黑边
float fRate = m_bmi.bmiHeader.biWidth * 1.0f / abs(m_bmi.bmiHeader.biHeight);
int newW = rc.Height() * fRate;
if (newW>rc.Width()) {
int newH = rc.Width() / fRate;
int oldH = rc.Height();
rc.top = (oldH - newH) / 2;
rc.bottom = rc.top + newH;
rcBK = { 0, 0, rc.Width(), rc.top };
::ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcBK, NULL, 0, NULL);
rcBK = { 0, rc.bottom, rc.Width(), rc.bottom + rc.top };
::ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcBK, NULL, 0, NULL);
}
else {
int oldW = rc.Width();
rc.left = (oldW - newW) / 2;
rc.right = rc.left + newW;
rcBK = { 0, 0, rc.left, rc.Height() };
::ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcBK, NULL, 0, NULL);
rcBK = { rc.right, 0, rc.right + rc.left, rc.Height() };
::ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcBK, NULL, 0, NULL);
}
}
if (m_bUseGDI) {
int oldMode = SetStretchBltMode(hDC, COLORONCOLOR);
StretchDIBits(hDC, rc.left, rc.top, rc.Width(), rc.Height(),
0, 0, m_bmi.bmiHeader.biWidth, abs(m_bmi.bmiHeader.biHeight),
pBuffer, &m_bmi, DIB_RGB_COLORS, SRCCOPY);
SetStretchBltMode(hDC, oldMode);
}
else {
DrawDibDraw(m_hDrawDIB, hDC,
rc.left, rc.top, rc.Width(), rc.Height(),
&m_bmi.bmiHeader, pBuffer,
0, 0, m_bmi.bmiHeader.biWidth, abs(m_bmi.bmiHeader.biHeight),
DDF_NOTKEYFRAME);
}
}
if (points.size()) {
float xscale = rc.Width() * 1.0 / m_bmi.bmiHeader.biWidth;
float yscale = rc.Height() * 1.0 / abs(m_bmi.bmiHeader.biHeight);
#define PT_SIZE 3
HGDIOBJ bush = CreateSolidBrush(RGB(255, 0, 0));
HGDIOBJ oldBush = ::SelectObject(hDC, bush);
for (POINT pt : points) {
pt.x *= xscale;
pt.y *= yscale;
Ellipse(hDC, pt.x - PT_SIZE, pt.y - PT_SIZE, pt.x + PT_SIZE, pt.y + PT_SIZE);
//SetPixel(hDC, pt.x, pt.y, RGB(255, 0, 0));
}
SelectObject(hDC, oldBush);
DeleteObject(bush);
}
m_buffer.ReleaseBuffer();
}
else if (defImage) {
/*
HBITMAP hBmp = CreateCompatibleBitmap(hDC, rc.Width(), rc.Height());
HDC hMemDc = CreateCompatibleDC(hDC);
HGDIOBJ hOld = SelectObject(hMemDc, hBmp);
int oldMode = SetStretchBltMode(hMemDc, COLORONCOLOR);
defImage.StretchBlt(hMemDc, rc);
SetStretchBltMode(hMemDc, oldMode);
SelectObject(hMemDc, hOld);
DeleteObject(hMemDc);
HGDIOBJ hBmpBush = CreatePatternBrush(hBmp);
DeleteObject(hBmp);
SelectObject(hDC, hBmpBush);
SelectObject(hDC, CreatePen(PS_SOLID, 6, 0x00FFFFFF));
RoundRect(hDC, rc.left, rc.top, rc.right, rc.bottom, rc.Width() / 4, rc.Height() / 4);
DeleteObject(hBmpBush);
*/
int oldMode = SetStretchBltMode(hDC, COLORONCOLOR);
defImage.StretchBlt(hDC, rc);
SetStretchBltMode(hDC, oldMode);
}
else {
HBRUSH brush = ::CreateSolidBrush(m_clrBK);
::FillRect(hDC, &rc, brush);
::DeleteObject(brush);
}
if (m_szText.length()) {
HGDIOBJ old_font = ::SelectObject(hDC, ::GetStockObject(DEFAULT_GUI_FONT));
SetBkMode(hDC, TRANSPARENT);
COLORREF clr = SetTextColor(hDC, m_clrText);
DrawTextA(hDC, m_szText.c_str(), -1, rc, m_nTextFmt);
SetTextColor(hDC, clr);
::SelectObject(hDC, old_font);
}
if (m_bShowVol) {
GetClientRect(handle(), rc);
rc.bottom = 5;
rc.right = m_nVol * rc.right / 32727;
COLORREF ref = RGB(21, 194, 60);
if (m_nVol > 0.8 * 32727)
ref = RGB(128, 0, 0);
HBRUSH brush = ::CreateSolidBrush(ref);
FillRect(hDC, rc, brush);
::DeleteObject(brush);
/* 渐变色绘制
GetClientRect(handle(), rc);
rc.top = rc.bottom - 20;
rc.right = 100;
SelectObject(hDC, GetStockObject(NULL_BRUSH));
::Rectangle(hDC,rc.left, rc.top, rc.right, rc.bottom);
rc.right = m_nVol * rc.right / 32727;
InflateRect(&rc, -1, -1);
DuiLib::CRenderEngine::DrawGradient(hDC, rc, 0xFF3cc215, 0xFF800000, false, 32);
*/
}
return TRUE;
}
BYTE * CVideoWnd::GetBuffer(int w, int h)
{
BYTE *ptr = NULL;
BOOL bVisible = (GetWindowLong(handle(), GWL_STYLE) & WS_VISIBLE);
if (handle() && bVisible)//IsWindowVisible())
{
ptr = m_buffer.GetBuffer(w, h);
if (ptr)
{
if (w != m_bmi.bmiHeader.biWidth || h != abs(m_bmi.bmiHeader.biHeight))
{
m_bmi.bmiHeader.biWidth = w;
m_bmi.bmiHeader.biHeight = -h;
m_bmi.bmiHeader.biSizeImage = w * h * (m_bmi.bmiHeader.biBitCount >> 3);
// m_bSizeChanged=TRUE;
}
//We don't call ReleaseBuffer() here, and call it later
}
}
return ptr;
}
BOOL CVideoWnd::ReleaseBuffer()
{
m_buffer.ReleaseBuffer();
Refresh();
return TRUE;
}
BOOL CVideoWnd::FillBuffer(BYTE* pRgb, int width, int height, int orgin)
{
BYTE* pDst = NULL;
if (orgin % 2)
pDst = GetBuffer(height, width);
else
pDst = GetBuffer(width, height);
if (pDst)
{
switch (orgin % 4) {
case 0:
memcpy(pDst, pRgb, width*height * 3);
break;
case 1:
RGBRotate90(pRgb, pDst, height, width);
break;
case 2:
RGBRotate180(pRgb, pDst, height, width);
break;
case 3:
RGBRotate270(pRgb, pDst, height, width);
break;
}
ReleaseBuffer();
return TRUE;
}
return FALSE;
}
void CVideoWnd::SetFitMode(BOOL bFit)
{
m_bFit = bFit;
Refresh();
}
void CVideoWnd::SetBkColor(COLORREF color)
{
m_clrBK = color;
Refresh();
}
void CVideoWnd::SetText(const char* text, COLORREF cl, DWORD mode)
{
m_szText = text;
m_nTextFmt = mode;
m_clrText = cl;
Refresh();
}
void CVideoWnd::Clear()
{
m_buffer.CleanUpBuffer();
Refresh();
}
void CVideoWnd::Refresh()
{
if (handle())
InvalidateRect(handle(), NULL, TRUE);
}
//创建位图文件
bool SaveBitmap(const char* bmpPath, BYTE * pBuffer, int lWidth, int lHeight, int nByte = 3)
{
// 生成bmp文件
HANDLE hf = CreateFileA(
bmpPath, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, NULL, NULL);
if (hf == INVALID_HANDLE_VALUE)return 0;
// 写文件头
BITMAPFILEHEADER bfh;
memset(&bfh, 0, sizeof(bfh));
bfh.bfType = 'MB';
bfh.bfSize = sizeof(bfh) + lWidth*lHeight * nByte + sizeof(BITMAPINFOHEADER);
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
DWORD dwWritten = 0;
WriteFile(hf, &bfh, sizeof(bfh), &dwWritten, NULL);
// 写位图格式
BITMAPINFOHEADER bih;
memset(&bih, 0, sizeof(bih));
bih.biSize = sizeof(bih);
bih.biWidth = lWidth;
bih.biHeight = lHeight;// -lHeight;
bih.biPlanes = 1;
bih.biBitCount = nByte * 8;
WriteFile(hf, &bih, sizeof(bih), &dwWritten, NULL);
// 写位图数据
WriteFile(hf, pBuffer, lWidth*lHeight*nByte, &dwWritten, NULL);
CloseHandle(hf);
return 0;
}
BOOL CVideoWnd::TaskSnap(LPCTSTR path)
{
BOOL bRet = FALSE;
LPSTREAM lpStream = NULL;
BYTE* b = m_buffer.GetBuffer();
if (b&&SUCCEEDED(CreateStreamOnHGlobal(NULL, TRUE, &lpStream))) {
DWORD size = m_bmi.bmiHeader.biSizeImage;
// 写文件头
BITMAPFILEHEADER bfh;
memset(&bfh, 0, sizeof(bfh));
bfh.bfType = 'MB';
bfh.bfSize = sizeof(bfh) + size + sizeof(BITMAPINFOHEADER);
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
ULONG nWritten = 0;
lpStream->Write(&bfh, sizeof bfh, &nWritten);
lpStream->Write(&m_bmi.bmiHeader, sizeof BITMAPINFOHEADER, &nWritten);
lpStream->Write(b, size, &nWritten);
CImage img;
if (SUCCEEDED(img.Load(lpStream)))
bRet = SUCCEEDED(img.Save(path));
lpStream->Release();
}
if (b) m_buffer.ReleaseBuffer();
return bRet;
}
bool CVideoWnd::SetDefaultImage(const char* path)
{
USES_CONVERSION;
return SUCCEEDED(defImage.Load(A2T(path)));
}
bool CVideoWnd::SetVisible(bool bShow)
{
return ::ShowWindow(handle(), bShow ? SW_SHOW : SW_HIDE);
}
bool CVideoWnd::GetVisible()
{
return ::IsWindowVisible(handle());
}
void CVideoWnd::SetPoint(std::vector<POINT>& pts)
{
m_buffer.GetBuffer();
points = pts;
m_buffer.ReleaseBuffer();
}