-
Notifications
You must be signed in to change notification settings - Fork 0
/
Imaging.cpp
325 lines (272 loc) · 9.95 KB
/
Imaging.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
#include "Imaging.h"
#include "Errors.h"
WindowsImagingComponent wic;
bool Whiten( HDC source_dc, HBITMAP source_bitmap, unsigned int width, unsigned int height, HDC *pwhitened_dc, HBITMAP *pwhitened_bitmap, HGDIOBJ* pwhitened_dc_deselectobj, BYTE **ppwhitened_data )
{
//FIX UP THE pwhitened_data and ppwhitened_data redundancy
int result;
BYTE *pwhitened_data;
if( pwhitened_bitmap == NULL )
{
logger.printf( _T("Whiten(); error: pwhitened_bitmap is NULL! \r\n") );
return false;
}
pwhitened_data = (BYTE *) VirtualAlloc( NULL, width * 4 * height, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE );
if( pwhitened_data == NULL )
{
logger.printf( _T("Whiten()::VirtualAlloc(); FATAL ERROR: %d\r\n"), GetLastError() );
return false;
}
*ppwhitened_data = pwhitened_data;
result = GetBitmapBits( source_bitmap, width * 4 * height, pwhitened_data );
if( result == NULL )
{
logger.printf( _T("Whiten()::GetBitmapBits(); FATAL ERROR\r\n") );
return false;
}
for( int i = 0; i < width * 4 * height; i++ )
{
pwhitened_data[i] = (pwhitened_data[i] + 255) / 2;
}
*pwhitened_bitmap = CreateBitmap( width, height, 1, 32, pwhitened_data );
if( *pwhitened_bitmap == NULL )
{
logger.printf( _T("Whiten()::CreateBitmap(); FATAL ERROR\r\n") );
return false;
}
*pwhitened_dc = CreateCompatibleDC( source_dc );
if( !*pwhitened_dc )
{
logger.printf( _T("Whiten()::CreateCompatibleDC(source_dc); FATAL ERROR\r\n"));
Sleep(INFINITE);
}
*pwhitened_dc_deselectobj = SelectObject( *pwhitened_dc, *pwhitened_bitmap );
if( *pwhitened_dc_deselectobj == NULL || *pwhitened_dc_deselectobj == HGDI_ERROR )
{
logger.printf( _T("Whiten()::SelectObject( whitened_dc, whitened_bitmap ); FATAL ERROR\r\n"));
Sleep(INFINITE);
}
return true;
}
bool CaptureScreen( HDC *pcapture_dc, HBITMAP *phbmp, HGDIOBJ *pcapture_dc_deselectobj )
{
int errmsg;
HWND desktop_window;
RECT desktop_rect;
HDC desktop_dc;
HDC capture_dc;
HBITMAP hbmp;
HGDIOBJ hgdiobj_return;
desktop_window = GetDesktopWindow();
GetWindowRect( desktop_window, &desktop_rect );
desktop_dc = GetDC( desktop_window );
if( !desktop_dc )
{
logger.printf( _T("CaptureScreen()::GetDC(); FATAL ERROR\r\n"));
return false;
}
capture_dc = CreateCompatibleDC( desktop_dc );
if( !capture_dc )
{
logger.printf( _T("CaptureScreen()::CreateCompatibleDC(); FATAL ERROR\r\n"));
return false;
}
hbmp = CreateCompatibleBitmap( desktop_dc, desktop_rect.right-desktop_rect.left, desktop_rect.bottom-desktop_rect.top );
if( !hbmp )
{
logger.printf( _T("CaptureScreen()::CreateCompatibleBitmap(); FATAL ERROR\r\n"));
return false;
}
hgdiobj_return = SelectObject( capture_dc, hbmp );
if( hgdiobj_return == NULL || hgdiobj_return == HGDI_ERROR )
{
logger.printf( _T("CaptureScreen()::SelectObject( capture_dc, hbmp ); FATAL ERROR\r\n"));
return false;
}
errmsg = BitBlt( capture_dc, 0, 0, desktop_rect.right-desktop_rect.left, desktop_rect.bottom-desktop_rect.top, desktop_dc, 0, 0, SRCCOPY|CAPTUREBLT );
if( !errmsg )
{
logger.printf( _T("CaptureScreen()::BitBlt(); FATAL ERROR: %x\r\n"), GetLastError() );
return false;
}
/*hgdiobj_return = SelectObject( capture_dc, hgdiobj_return );
if( hgdiobj_return == NULL || hgdiobj_return == HGDI_ERROR )
{
logger.printf( _T("CaptureScreen()::SelectObject( capture_dc, hgdiobj_return ); FATAL ERROR\r\n"));
return false;
}*/
*pcapture_dc = capture_dc;
*phbmp = hbmp;
*pcapture_dc_deselectobj = hgdiobj_return;
ReleaseDC( desktop_window, desktop_dc );
return true;
}
bool WindowsImagingComponent::Initialize()
{
HRESULT result;
result = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*) &this->pFactory);
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::Initialize()::CoCreateInstance(); FATAL ERROR: %x\r\n"), result );
return false;
}
return true;
}
bool WindowsImagingComponent::ConvertBitmapToPng( HBITMAP hbmp, unsigned int width, unsigned int height, BYTE **ppimage_buffer, ULARGE_INTEGER *pimgbuf_real_len )
{
HRESULT result;
if( !this->pFactory )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng() error: IWICImagingFactory is missing\r\n") );
return false;
}
result = this->pFactory->CreateStream( &this->pStream );
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::CreateStream(); FATAL ERROR: %x\r\n"), result );
return false;
}
*ppimage_buffer = (BYTE *)VirtualAlloc( NULL, PNG_BUFFER_SIZE, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE );
result = this->pStream->InitializeFromMemory( *ppimage_buffer, PNG_BUFFER_SIZE );
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::InitializeFromMemory(); FATAL ERROR: %x\r\n"), result );
return false;
}
/*result = this->pStream->InitializeFromFilename( TEXT("captureoutput.png"), GENERIC_WRITE );
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::InitializeFromFilename(); FATAL ERROR: %x\r\n"), result );
return false;
}*/
result = this->pFactory->CreateEncoder( GUID_ContainerFormatPng, NULL, &this->pBitmapEncoder );
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::CreateEncoder(); FATAL ERROR: %x\r\n"), result );
return false;
}
result = this->pBitmapEncoder->Initialize( this->pStream, WICBitmapEncoderNoCache );
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::Initialize(); FATAL ERROR: %x\r\n"), result );
return false;
}
result = this->pBitmapEncoder->CreateNewFrame( &this->pBitmapFrame, NULL ); //propertybag is NULL because it's complex
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::CreateNewFrame(); FATAL ERROR: %x\r\n"), result );
return false;
}
result = this->pBitmapFrame->Initialize( NULL ); //initializing with no propertybag
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::Initialize( NULL ); FATAL ERROR: %x\r\n"), result );
return false;
}
result = this->pFactory->CreateBitmapFromHBITMAP( hbmp, NULL, WICBitmapIgnoreAlpha, &this->pWicBitmap );
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::CreateBitmapFromHBITMAP(); FATAL ERROR: %x\r\n"), result );
return false;
}
WICRect wicrect;
wicrect.X = 0;
wicrect.Y = 0;
wicrect.Width = width;
wicrect.Height = height;
result = this->pBitmapFrame->WriteSource( this->pWicBitmap, &wicrect );
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::WriteSource(); FATAL ERROR: %x\r\n"), result );
return false;
}
result = this->pBitmapFrame->Commit();
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::pBItmapFrame->Commit(); FATAL ERROR: %x\r\n"), result );
return false;
}
result = this->pBitmapEncoder->Commit();
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::pBitmapEncoder->Commit(); FATAL ERROR: %x\r\n"), result );
return false;
}
LARGE_INTEGER zero = {0};
result = this->pStream->Seek( zero, STREAM_SEEK_CUR, pimgbuf_real_len );
if( !SUCCEEDED(result) )
{
logger.printf( _T("WindowsImagingComponent::ConvertBitmapToPng()::pStream->Seek(STREAM_SEEK_END); FATAL ERROR: %x\r\n"), result );
return false;
}
if( this->pWicBitmap )
this->pWicBitmap->Release();
this->pWicBitmap = NULL;
if ( this->pBitmapFrame )
this->pBitmapFrame->Release();
this->pBitmapFrame = NULL;
if ( this->pBitmapEncoder )
this->pBitmapEncoder->Release();
this->pBitmapEncoder = NULL;
if ( this->pStream )
this->pStream->Release();
this->pStream = NULL;
return true;
}
bool WindowsImagingComponent::CaptureDCRegion( HDC source_dc, HBITMAP source_bitmap, unsigned int x, unsigned int y, unsigned int width, unsigned int height, BYTE **ppimage_buffer, ULARGE_INTEGER *pimgbuf_real_len )
{
int errmsg;
HDC capture_dc;
HBITMAP hbmp;
HGDIOBJ source_hgdiobj_return;
HGDIOBJ capture_hgdiobj_return;
source_hgdiobj_return = SelectObject( source_dc, source_bitmap );
if( source_hgdiobj_return == NULL || source_hgdiobj_return == HGDI_ERROR )
{
logger.printf( _T("CaptureDCRegion()::SelectObject( source_dc, source_bitmap ); FATAL ERROR\r\n"));
return false;
}
capture_dc = CreateCompatibleDC( source_dc );
if( !capture_dc )
{
logger.printf( _T("CaptureDCRegion()::CreateCompatibleDC( source_dc ); FATAL ERROR\r\n"));
return false;
}
hbmp = CreateCompatibleBitmap( source_dc, width, height );
if( !hbmp )
{
logger.printf( _T("CaptureDCRegion()::CreateCompatibleBitmap( source_dc, width, height ); FATAL ERROR\r\n"));
return false;
}
capture_hgdiobj_return = SelectObject( capture_dc, hbmp );
if( capture_hgdiobj_return == NULL || capture_hgdiobj_return == HGDI_ERROR )
{
logger.printf( _T("CaptureDCRegion()::SelectObject( capture_dc, hbmp ); FATAL ERROR\r\n"));
return false;
}
errmsg = BitBlt( capture_dc, 0, 0, width, height, source_dc, x, y, SRCCOPY|CAPTUREBLT );
if( !errmsg )
{
logger.printf( _T("CaptureDCRegion()::BitBlt( capture_dc, 0, 0, width, height, desktop_dc, x, y, SRCCOPY|CAPTUREBLT ); FATAL ERROR: %x\r\n"), GetLastError() );
return false;
}
this->ConvertBitmapToPng( hbmp, width, height, ppimage_buffer, pimgbuf_real_len );
//RELEASE THINGS
capture_hgdiobj_return = SelectObject( capture_dc, capture_hgdiobj_return );
if( capture_hgdiobj_return == NULL || capture_hgdiobj_return == HGDI_ERROR )
{
DeleteObject( hbmp );
DeleteDC( capture_dc );
logger.printf( _T("CaptureDCRegion()::SelectObject( capture_dc ) [deselecting object]; FATAL ERROR\r\n"));
return false;
}
source_hgdiobj_return = SelectObject( source_dc, source_hgdiobj_return );
if( source_hgdiobj_return == NULL || source_hgdiobj_return == HGDI_ERROR )
{
logger.printf( _T("CaptureDCRegion()::SelectObject( source_dc ) [deselecting object]; FATAL ERROR\r\n"));
return false;
}
DeleteObject( hbmp );
DeleteDC( capture_dc );
return true;
}