/
ColorBasics.cpp
679 lines (564 loc) · 20.3 KB
/
ColorBasics.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
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
//------------------------------------------------------------------------------
// <copyright file="ColorBasics.cpp" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
#include "stdafx.h"
#include <strsafe.h>
#include "ColorBasics.h"
#include "resource.h"
#ifdef _DEBUG
#include <direct.h>
#endif
/// <summary>
/// Constructor
/// </summary>
CColorBasics::CColorBasics() :
m_pD2DFactory(NULL),
m_pDrawColor(NULL),
m_hNextColorFrameEvent(INVALID_HANDLE_VALUE),
m_pColorStreamHandle(INVALID_HANDLE_VALUE),
m_hNextSkeletonEvent(INVALID_HANDLE_VALUE),
m_pSkeletonStreamHandle(INVALID_HANDLE_VALUE),
m_bSaveScreenshot(false),
m_pNuiSensor(NULL)
{
}
/// <summary>
/// Destructor
/// </summary>
CColorBasics::~CColorBasics()
{
if (m_pNuiSensor)
{
m_pNuiSensor->NuiShutdown();
}
if (m_hNextColorFrameEvent != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hNextColorFrameEvent);
}
if (m_hNextSkeletonEvent && (m_hNextSkeletonEvent != INVALID_HANDLE_VALUE))
{
CloseHandle(m_hNextSkeletonEvent);
}
// clean up Direct2D renderer
delete m_pDrawColor;
m_pDrawColor = NULL;
// clean up Direct2D
SafeRelease(m_pD2DFactory);
SafeRelease(m_pNuiSensor);
}
void CColorBasics::setDasher(DASHout *dash){
dasher = dash;
}
/// <summary>
/// Creates the main window and begins processing
/// </summary>
/// <param name="hInstance">handle to the application instance</param>
/// <param name="nCmdShow">whether to display minimized, maximized, or normally</param>
int CColorBasics::Run(HINSTANCE hInstance, int nCmdShow, DASHout* dasher)
{
MSG msg = {0};
WNDCLASS wc;
DWORD event_res;
// Dialog custom window class
ZeroMemory(&wc, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APP));
wc.lpfnWndProc = DefDlgProcW;
wc.lpszClassName = "ColorBasicsAppDlgWndClass";
if (!RegisterClass(&wc))
{
return 0;
}
// Create main application window
HWND hWndApp = CreateDialogParam(
hInstance,
MAKEINTRESOURCE(IDD_APP),
NULL,
(DLGPROC)CColorBasics::MessageRouter,
reinterpret_cast<LPARAM>(this));
// Show window
ShowWindow(hWndApp, nCmdShow);
const int eventCount = 2;
HANDLE hEvents[eventCount];
#ifdef _DEBUG
char cwd[1024];
if (_getcwd(cwd, sizeof(cwd)) != NULL) fprintf(stdout, "Current working dir: %s\n", cwd);
fprintf(stdout, "Checking Threader... ");
if(dasher->threader->mutex == NULL){
printErr("WARNING: thread initialization error\n");
}else{
fprintf(stdout, "OK\n");
}
if(dasher->colFrameCount==0)getchar();
#endif
// Main message loop
while (WM_QUIT != msg.message)
{
hEvents[0] = m_hNextColorFrameEvent;
hEvents[1] = m_hNextSkeletonEvent;
// Check to see if we have either a message (by passing in QS_ALLINPUT)
// Or a Kinect event (hEvents)
// Update() will check for Kinect events individually, in case more than one are signalled
//more info on MsgWaitForMultipleObjects function: https://msdn.microsoft.com/en-us/library/windows/desktop/ms684242%28v=vs.85%29.aspx
event_res = MsgWaitForMultipleObjects(eventCount, hEvents, FALSE, INFINITE, QS_ALLINPUT);
// Explicitly check the Kinect frame event since MsgWaitForMultipleObjects
// can return for other reasons even though it is signaled.
if((0 <= event_res)&&(event_res < 2)) Update(dasher, event_res);
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
{
#ifdef _DEBUG
if(WM_KEYDOWN == msg.message)
switch(msg.wParam){
case(VK_SPACE): //we have space
case(0x50): //or 'P'
//system("PAUSE");
printf("used for testing \n");
break;
}
#endif
// If a dialog message will be taken care of by the dialog proc
if ((hWndApp != NULL) && IsDialogMessageW(hWndApp, &msg))
{
continue;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
if(WM_QUIT == msg.message){
if(dasher->segment_started){
muxer_close_segment(dasher);
if(dasher->skelFrameCount>0){
printf("\n\n FINAL FLUSHING \n\n"); //TODOk remove
flush_skeleton_coordinates(dasher->seg_num);
dasher->video_done = true; //TODOk check
}
}
muxer_destroy(dasher);
system("PAUSE");
}
return static_cast<int>(msg.wParam);
}
/// <summary>
/// Main processing function
/// </summary>
void CColorBasics::Update(DASHout* dasher, DWORD event_res)
{
u64 timeref = 0; //TODO fix/move this
if (NULL == m_pNuiSensor)
{
return;
}
if ( WAIT_OBJECT_0 == WaitForSingleObject(m_hNextColorFrameEvent, INFINITE) )
{
ProcessColor(dasher);
}
if (event_res == 1 && WAIT_OBJECT_0 == WaitForSingleObject(m_hNextSkeletonEvent, INFINITE) )
{
//TODO add process skel function
timeref = gf_sys_clock_high_res() - dasher->sys_start;
ProcessSkeleton(dasher, timeref);
}
if(dasher->nextColourFrame){
int res = muxer_encode(dasher, (u8 *) dasher->nextColourFrame->kinectFrame, dasher->nextColourFrame->size,dasher->nextColourFrame->pts);
if((res>=0)&&(!dasher->segment_started)){
res = muxer_open_segment(dasher, "x64/Debug/out", "seg", dasher->seg_num);
timeref = gf_sys_clock_high_res() - dasher->sys_start;
printf("\t\tOpening segment time : %llu\n", timeref);
}
if(res>=0){
res = muxer_write_frame(dasher, dasher->colFrameCount);
dasher->colFrameCount++;
}
if(res==1){
res = muxer_close_segment(dasher);
if(res==GF_OK){
write_playlist_segment(dasher->seg_num, timeref);
}
if(dasher->skelFrameCount>0){
printf("\n\n FLUSHING \n\n"); //TODOk remove
res = flush_skeleton_coordinates(dasher->seg_num);
if(res == false){
printf("\n\n\n\nFLUSHING ERROR\n\n");
}
}
dasher->seg_num++;
}
dasher->nextColourFrame = NULL;
}
}
/// <summary>
/// Handles window messages, passes most to the class instance to handle
/// </summary>
/// <param name="hWnd">window message is for</param>
/// <param name="uMsg">message</param>
/// <param name="wParam">message data</param>
/// <param name="lParam">additional message data</param>
/// <returns>result of message processing</returns>
LRESULT CALLBACK CColorBasics::MessageRouter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CColorBasics* pThis = NULL;
if (WM_INITDIALOG == uMsg)
{
pThis = reinterpret_cast<CColorBasics*>(lParam);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
}
else
{
pThis = reinterpret_cast<CColorBasics*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
}
if (pThis)
{
return pThis->DlgProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
/// <summary>
/// Handle windows messages for the class instance
/// </summary>
/// <param name="hWnd">window message is for</param>
/// <param name="uMsg">message</param>
/// <param name="wParam">message data</param>
/// <param name="lParam">additional message data</param>
/// <returns>result of message processing</returns>
LRESULT CALLBACK CColorBasics::DlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
{
// Bind application window handle
m_hWnd = hWnd;
// Init Direct2D
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pD2DFactory);
// Create and initialize a new Direct2D image renderer (take a look at ImageRenderer.h)
// We'll use this to draw the data we receive from the Kinect to the screen
m_pDrawColor = new ImageRenderer();
HRESULT hr = m_pDrawColor->Initialize(GetDlgItem(m_hWnd, IDC_VIDEOVIEW), m_pD2DFactory, cColorWidth, cColorHeight, cColorWidth * sizeof(long));
if (FAILED(hr))
{
SetStatusMessage(L"Failed to initialize the Direct2D draw device.");
}
// Look for a connected Kinect, and create it if found
CreateFirstConnected();
}
break;
// If the titlebar X is clicked, destroy app
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
// Quit the main message pump
PostQuitMessage(0);
break;
// Handle button press
case WM_COMMAND:
// If it was for the screenshot control and a button clicked event, save a screenshot next frame
if (IDC_BUTTON_SCREENSHOT == LOWORD(wParam) && BN_CLICKED == HIWORD(wParam))
{
m_bSaveScreenshot = true;
}
break;
}
return FALSE;
}
/// <summary>
/// Create the first connected Kinect found
/// </summary>
/// <returns>indicates success or failure</returns>
HRESULT CColorBasics::CreateFirstConnected()
{
INuiSensor * pNuiSensor;
HRESULT hr;
int iSensorCount = 0;
hr = NuiGetSensorCount(&iSensorCount);
if (FAILED(hr))
{
return hr;
}
// Look at each Kinect sensor
for (int i = 0; i < iSensorCount; ++i)
{
// Create the sensor so we can check status, if we can't create it, move on to the next
hr = NuiCreateSensorByIndex(i, &pNuiSensor);
if (FAILED(hr))
{
continue;
}
// Get the status of the sensor, and if connected, then we can initialize it
hr = pNuiSensor->NuiStatus();
if (S_OK == hr)
{
m_pNuiSensor = pNuiSensor;
break;
}
// This sensor wasn't OK, so release it since we're not using it
pNuiSensor->Release();
}
if (NULL != m_pNuiSensor)
{
// Initialize the Kinect and specify that we'll be using color
hr = m_pNuiSensor->NuiInitialize(NUI_INITIALIZE_FLAG_USES_COLOR | NUI_INITIALIZE_FLAG_USES_SKELETON);
if (SUCCEEDED(hr))
{
// Create an event that will be signaled when color data is available
m_hNextColorFrameEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
// Create an event that will be signaled when skeleton data is available
m_hNextSkeletonEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
// Open a color image stream to receive color frames
//TODO: Check image format https://msdn.microsoft.com/en-us/library/nuiimagecamera.nui_image_type.aspx (we now have RGB)
hr = m_pNuiSensor->NuiImageStreamOpen(
NUI_IMAGE_TYPE_COLOR,
NUI_IMAGE_RESOLUTION_640x480,
0,
2,
m_hNextColorFrameEvent,
&m_pColorStreamHandle);
if(!FAILED(hr)){
// Open a skeleton stream to receive skeleton data
hr = m_pNuiSensor->NuiSkeletonTrackingEnable(m_hNextSkeletonEvent, 0);
}
}
}
if (NULL == m_pNuiSensor || FAILED(hr))
{
SetStatusMessage(L"No ready Kinect found!");
return E_FAIL;
}
return hr;
}
/// <summary>
/// Get the name of the file where screenshot will be stored.
/// </summary>
/// <param name="screenshotName">
/// [out] String buffer that will receive screenshot file name.
/// </param>
/// <param name="screenshotNameSize">
/// [in] Number of characters in screenshotName string buffer.
/// </param>
/// <returns>
/// S_OK on success, otherwise failure code.
/// </returns>
HRESULT GetScreenshotFileName(wchar_t *screenshotName, UINT screenshotNameSize)
{
wchar_t *knownPath = NULL;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Pictures, 0, NULL, &knownPath);
if (SUCCEEDED(hr))
{
// Get the time
wchar_t timeString[MAX_PATH];
GetTimeFormatEx(NULL, 0, NULL, L"hh'-'mm'-'ss", timeString, _countof(timeString));
// File name will be KinectSnapshot-HH-MM-SS.wav
StringCchPrintfW(screenshotName, screenshotNameSize, L"%s\\KinectSnapshot-%s.bmp", knownPath, timeString);
}
CoTaskMemFree(knownPath);
return hr;
}
/// <summary>
/// Handle new color data
/// </summary>
/// <returns>indicates success or failure</returns>
void CColorBasics::ProcessColor(DASHout* dasher)
{
HRESULT hr;
NUI_IMAGE_FRAME imageFrame;
colourFrame *cFrame;
// Attempt to get the color frame
hr = m_pNuiSensor->NuiImageStreamGetNextFrame(m_pColorStreamHandle, 0, &imageFrame);
if (FAILED(hr))
{
printf("failed in processcolor \n ");
return;
}
INuiFrameTexture * pTexture = imageFrame.pFrameTexture;
NUI_LOCKED_RECT LockedRect;
// Lock the frame data so the Kinect knows not to modify it while we're reading it
pTexture->LockRect(0, &LockedRect, NULL, 0);
// Make sure we've received valid data
if (LockedRect.Pitch != 0)
{
//init the frame to be parsed in FFMPEG
cFrame = init_cFrame(cColorWidth*cColorHeight*3);
if(!cFrame){
printf("fudge!\n");
gf_free(cFrame);
return;
}
//Frame number
cFrame->number = imageFrame.dwFrameNumber;
if (!dasher->sys_start) {
dasher->sys_start = gf_sys_clock_high_res();
dasher->prev_pts = 0;
cFrame->pts = 0;
} else {
//TODO: fix timing here and
cFrame->pts = 30*(gf_sys_clock_high_res() - dasher->sys_start)/1000000;
printf("CTS diff is %d ms\n", (u32) (cFrame->pts - dasher->prev_pts) / 1000);
dasher->prev_pts = cFrame->pts;
}
//convert RGBA to RGB
unsigned char * currFrame = (unsigned char *)LockedRect.pBits;
int j = 0;
for (int i = 0; i < cColorWidth * cColorHeight * 4; i += 4){
(cFrame->kinectFrame)[i - j] = currFrame[i + 2];
(cFrame->kinectFrame)[i - j + 1] = currFrame[i + 1];
(cFrame->kinectFrame)[i - j + 2] = currFrame[i];
j++;
}
dasher->nextColourFrame = cFrame;
// Draw the data with Direct2D
m_pDrawColor->Draw(static_cast<BYTE *>(LockedRect.pBits), LockedRect.size);
#if 0
// If the user pressed the screenshot button, save a screenshot
if (m_bSaveScreenshot)
{
WCHAR statusMessage[cStatusMessageMaxLen];
// Retrieve the path to My Photos
WCHAR screenshotPath[MAX_PATH];
GetScreenshotFileName(screenshotPath, _countof(screenshotPath));
// Write out the bitmap to disk
hr = SaveBitmapToFile(static_cast<BYTE *>(LockedRect.pBits), cColorWidth, cColorHeight, 32, screenshotPath);
if (SUCCEEDED(hr))
{
// Set the status bar to show where the screenshot was saved
StringCchPrintf( statusMessage, cStatusMessageMaxLen, L"Screenshot saved to %s", screenshotPath);
}
else
{
StringCchPrintf( statusMessage, cStatusMessageMaxLen, L"Failed to write screenshot to %s", screenshotPath);
}
SetStatusMessage(statusMessage);
// toggle off so we don't save a screenshot again next frame
m_bSaveScreenshot = false;
}
#endif
}
// We're done with the texture so unlock it
pTexture->UnlockRect(0);
// Release the frame
m_pNuiSensor->NuiImageStreamReleaseFrame(m_pColorStreamHandle, &imageFrame);
}
void CColorBasics::ProcessSkeleton(DASHout* dasher, u64 timeref){
putchar('\n');
NUI_SKELETON_FRAME skeletonFrame = {0};
HRESULT hr = m_pNuiSensor->NuiSkeletonGetNextFrame(0, &skeletonFrame);
skeletalData skeletonToThread;
DWORD threadId;
if ( FAILED(hr) )
{
return;
}
// smooth out the skeleton data
m_pNuiSensor->NuiTransformSmooth(&skeletonFrame, NULL);
for (int i = 0 ; i < NUI_SKELETON_COUNT; ++i)
{
NUI_SKELETON_TRACKING_STATE trackingState = skeletonFrame.SkeletonData[i].eTrackingState;
if (NUI_SKELETON_TRACKED == trackingState)
{
push_skeleton_coordinates(skeletonFrame, i, dasher->skelFrameCount, timeref, dasher->seg_num);
skeletonToThread.skel =skeletonFrame;
skeletonToThread.index = i;
skeletonToThread.skel_num = dasher->skelFrameCount;
skeletonToThread.timeref = timeref;
skeletonToThread.seg_num = dasher->seg_num;
skeletonToThread.dasher = dasher;
skeletonToThread.threader = (Threader *) dasher->threader;
//TODOk check from HERE
printf("sendin' %d %u %u %u \n",skeletonFrame, i, dasher->skelFrameCount, timeref, dasher->seg_num);
if(dasher->threader == NULL)
printf("and it aint null\n");
if(!(dasher->skelFrameCount==NULL))
printf("but the skelly's wrong\n");
/*
memcpy(&skeletonToThread.skel, &skeletonFrame, sizeof(skeletonFrame));
memcpy(&skeletonToThread.index, &i, sizeof(int));
memcpy(&skeletonToThread.skel_num, &dasher->skelFrameCount, sizeof(u64));
memcpy(&skeletonToThread.timeref, &timeref, sizeof(u64));
memcpy(&skeletonToThread.seg_num, &dasher->seg_num, sizeof(u64));
*/
//TODOk check to HERE
HANDLE hThread = CreateThread(0,0, (LPTHREAD_START_ROUTINE) generate_projected_coords, &skeletonToThread,0,&threadId);
//if(hThread != NULL) dasher->threader->threadcount++;
//Increase skeleton frame count
dasher->skelFrameCount++;
printf("\n tracked %d, %d \n", skeletonFrame.SkeletonData[i].dwTrackingID, i);
return; //we assume only one skeleton
// We're tracking the skeleton, draw it
// DrawSkeleton(skeletonFrame.SkeletonData[i], width, height);
}
else if (NUI_SKELETON_POSITION_ONLY == trackingState)
{
// we've only received the center point of the skeleton (not the coords)
// we do not care about this now
}
}
}
/// <summary>
/// Set the status bar message
/// </summary>
/// <param name="szMessage">message to display</param>
void CColorBasics::SetStatusMessage(WCHAR * szMessage)
{
SendDlgItemMessageW(m_hWnd, IDC_STATUS, WM_SETTEXT, 0, (LPARAM)szMessage);
}
#if 0
/// <summary>
/// Save passed in image data to disk as a bitmap
/// </summary>
/// <param name="pBitmapBits">image data to save</param>
/// <param name="lWidth">width (in pixels) of input image data</param>
/// <param name="lHeight">height (in pixels) of input image data</param>
/// <param name="wBitsPerPixel">bits per pixel of image data</param>
/// <param name="lpszFilePath">full file path to output bitmap to</param>
/// <returns>indicates success or failure</returns>
HRESULT CColorBasics::SaveBitmapToFile(BYTE* pBitmapBits, LONG lWidth, LONG lHeight, WORD wBitsPerPixel, LPCWSTR lpszFilePath)
{
DWORD dwByteCount = lWidth * lHeight * (wBitsPerPixel / 8);
BITMAPINFOHEADER bmpInfoHeader = {0};
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER); // Size of the header
bmpInfoHeader.biBitCount = wBitsPerPixel; // Bit count
bmpInfoHeader.biCompression = BI_RGB; // Standard RGB, no compression
bmpInfoHeader.biWidth = lWidth; // Width in pixels
bmpInfoHeader.biHeight = -lHeight; // Height in pixels, negative indicates it's stored right-side-up
bmpInfoHeader.biPlanes = 1; // Default
bmpInfoHeader.biSizeImage = dwByteCount; // Image size in bytes
BITMAPFILEHEADER bfh = {0};
bfh.bfType = 0x4D42; // 'M''B', indicates bitmap
bfh.bfOffBits = bmpInfoHeader.biSize + sizeof(BITMAPFILEHEADER); // Offset to the start of pixel data
bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage; // Size of image + headers
// Create the file on disk to write to
HANDLE hFile = CreateFileW(lpszFilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// Return if error opening file
if (NULL == hFile)
{
return E_ACCESSDENIED;
}
DWORD dwBytesWritten = 0;
// Write the bitmap file header
if ( !WriteFile(hFile, &bfh, sizeof(bfh), &dwBytesWritten, NULL) )
{
CloseHandle(hFile);
return E_FAIL;
}
// Write the bitmap info header
if ( !WriteFile(hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwBytesWritten, NULL) )
{
CloseHandle(hFile);
return E_FAIL;
}
// Write the RGB Data
if ( !WriteFile(hFile, pBitmapBits, bmpInfoHeader.biSizeImage, &dwBytesWritten, NULL) )
{
CloseHandle(hFile);
return E_FAIL;
}
// Close the file
CloseHandle(hFile);
return S_OK;
}
#endif