-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathphpwb_bitmap.c
More file actions
317 lines (255 loc) · 7.75 KB
/
phpwb_bitmap.c
File metadata and controls
317 lines (255 loc) · 7.75 KB
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
/*******************************************************************************
WINBINDER - The native Windows binding for PHP for PHP
Copyright Hypervisual - see LICENSE.TXT for details
Author: Rubem Pechansky (https://github.com/crispy-computing-machine/Winbinder)
ZEND wrapper for bitmap functions
*******************************************************************************/
//----------------------------------------------------------------- DEPENDENCIES
#include <math.h>
#include "phpwb.h"
extern BOOL IsBitmap(HANDLE handle);
extern BOOL IsIcon(HANDLE handle);
//----------------------------------------------------------- EXPORTED FUNCTIONS
/**
* [ index is the index of the image on the file if filename is an icon library. Default is 0.]
*
* @return [type] [return description]
*/
ZEND_FUNCTION(wb_load_image)
{
char *s;
size_t s_len;
zend_long index;
zend_long param = 0;
zend_bool index_isnull;
zend_bool param_isnull;
HANDLE hImage;
TCHAR *wcs = 0;
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"s|ll", &s, &s_len, &index, ¶m) == FAILURE)
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STRING(s,s_len)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(index, index_isnull)
Z_PARAM_LONG_OR_NULL(param, param_isnull)
ZEND_PARSE_PARAMETERS_END();
wcs = Utf82WideChar(s, s_len);
hImage = wbLoadImage(wcs, index, param);
wbFree(wcs);
//wbFree(s); // dont free filenames, mangles them :)
if (!hImage) {
RETURN_NULL();
} else {
RETURN_LONG((LONG_PTR)hImage);
}
}
ZEND_FUNCTION(wb_save_image)
{
zend_long hbm;
char *s;
size_t s_len;
TCHAR *wcs = 0;
BOOL ret;
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &hbm, &s, &s_len) == FAILURE)
// ZEND_PARSE_PARAMETERS_START() takes two arguments minimal and maximal parameters count.
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(hbm)
Z_PARAM_STRING(s, s_len)
ZEND_PARSE_PARAMETERS_END();
if (!hbm){
RETURN_BOOL(FALSE);
}
if (!IsBitmap((HANDLE)hbm)) {
if (IsIcon((HANDLE)hbm))
wbError(TEXT("wb_save_image"), MB_ICONWARNING, TEXT("Expected a bitmap handle; icon/cursor handles are not supported."));
else
wbError(TEXT("wb_save_image"), MB_ICONWARNING, TEXT("Expected a bitmap handle."));
RETURN_BOOL(FALSE);
}
wcs = Utf82WideChar(s, s_len);
ret = wbSaveBitmap((HBITMAP)hbm, wcs);
//wbFree(wcs);
#//wbFree(s);
RETURN_BOOL(ret);
}
// Zend function to wrap wbRotateBitmap
ZEND_FUNCTION(wb_rotate_image)
{
zend_long hbm;
zend_long angle;
HANDLE ret;
// Parse parameters
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(hbm)
Z_PARAM_LONG(angle)
ZEND_PARSE_PARAMETERS_END();
if (!hbm) {
RETURN_NULL();
}
if (!IsBitmap((HANDLE)hbm)) {
if (IsIcon((HANDLE)hbm))
wbError(TEXT("wb_rotate_image"), MB_ICONWARNING, TEXT("Expected a bitmap handle; icon/cursor handles are not supported."));
else
wbError(TEXT("wb_rotate_image"), MB_ICONWARNING, TEXT("Expected a bitmap handle."));
RETURN_NULL();
}
// Call the wbRotateBitmap function
ret = wbRotateBitmap((HBITMAP)hbm, angle);
// Return the rotated image handle or NULL
if (ret != NULL) {
RETURN_LONG((zend_long)ret);
} else {
RETURN_NULL();
}
}
// Zend function to wrap wbResizeBitmap
ZEND_FUNCTION(wb_resize_image)
{
zend_long hbm;
zend_long newWidth, newHeight;
HBITMAP ret;
// Parse parameters
ZEND_PARSE_PARAMETERS_START(3, 3)
Z_PARAM_LONG(hbm)
Z_PARAM_LONG(newWidth)
Z_PARAM_LONG(newHeight)
ZEND_PARSE_PARAMETERS_END();
if (!hbm) {
RETURN_NULL();
}
if (!IsBitmap((HANDLE)hbm)) {
if (IsIcon((HANDLE)hbm))
wbError(TEXT("wb_resize_image"), MB_ICONWARNING, TEXT("Expected a bitmap handle; icon/cursor handles are not supported."));
else
wbError(TEXT("wb_resize_image"), MB_ICONWARNING, TEXT("Expected a bitmap handle."));
RETURN_NULL();
}
// Call the wbResizeBitmap function
ret = wbResizeBitmap((HBITMAP)hbm, newWidth, newHeight);
// Return the rotated image handle or NULL
if (ret != NULL) {
RETURN_LONG((zend_long)ret);
} else {
RETURN_NULL();
}
}
ZEND_FUNCTION(wb_create_image)
{
zend_long w, h, bmi = 0, bits = 0;
int nargs;
nargs = ZEND_NUM_ARGS();
zend_bool bmi_isnull, bits_isnull;
// if (zend_parse_parameters(nargs TSRMLS_CC, "ll|ll", &w, &h, &bmi, &bits) == FAILURE)
// ZEND_PARSE_PARAMETERS_START() takes two arguments minimal and maximal parameters count.
ZEND_PARSE_PARAMETERS_START(2, 4)
Z_PARAM_LONG(w)
Z_PARAM_LONG(h)
Z_PARAM_OPTIONAL
Z_PARAM_LONG_OR_NULL(bmi, bmi_isnull)
Z_PARAM_LONG_OR_NULL(bits, bits_isnull)
ZEND_PARSE_PARAMETERS_END();
if (nargs == 3)
{
wbError(TEXT("wb_create_image"), MB_ICONWARNING, TEXT("Invalid parameter count passed to function"));
RETURN_LONG(0);
}
RETURN_LONG((LONG_PTR)wbCreateBitmap(w, h, (BITMAPINFO *)bmi, (void *)bits));
}
ZEND_FUNCTION(wb_get_image_data)
{
zend_long hbm;
BYTE *lpBits = NULL;
DWORDLONG size;
zend_bool compress4to3 = FALSE;
zend_bool compress4to3_isnull;
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &hbm, &compress4to3) == FAILURE)
// ZEND_PARSE_PARAMETERS_START() takes two arguments minimal and maximal parameters count.
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_LONG(hbm)
Z_PARAM_OPTIONAL // Everything after optional
Z_PARAM_BOOL_OR_NULL(compress4to3, compress4to3_isnull)
ZEND_PARSE_PARAMETERS_END();
// lpBits long pointer to BYTE array
if (!hbm) {
RETURN_NULL();
}
if (!IsBitmap((HANDLE)hbm)) {
if (IsIcon((HANDLE)hbm))
wbError(TEXT("wb_get_image_data"), MB_ICONWARNING, TEXT("Expected a bitmap handle; icon/cursor handles are not supported."));
else
wbError(TEXT("wb_get_image_data"), MB_ICONWARNING, TEXT("Expected a bitmap handle."));
RETURN_NULL();
}
size = wbGetBitmapBits((HBITMAP)hbm, &lpBits, compress4to3);
if (!lpBits){
RETURN_NULL();
}
// 2016_08_12 - Jared Allard: we don't need a TRUE to be passed anymore.
RETVAL_STRINGL(lpBits, size);
//wbFree(lpBits);
}
ZEND_FUNCTION(wb_create_mask)
{
zend_long hbm, c;
//if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &hbm, &c) == FAILURE)
// ZEND_PARSE_PARAMETERS_START() takes two arguments minimal and maximal parameters count.
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_LONG(hbm)
Z_PARAM_LONG(c)
ZEND_PARSE_PARAMETERS_END();
if (!hbm){
RETURN_NULL();
}
RETURN_LONG((LONG_PTR)wbCreateMask((HBITMAP)hbm, c));
}
ZEND_FUNCTION(wb_screenshot)
{
char *filename = NULL;
size_t filename_len;
wchar_t* wstr = NULL;
LONG_PTR result;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STRING_OR_NULL(filename, filename_len)
ZEND_PARSE_PARAMETERS_END();
// If filename is not NULL, convert it to wide string
if(filename != NULL) {
int wchars_num = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
if (wchars_num <= 0){
RETURN_BOOL(FALSE);
}
wstr = malloc(wchars_num * sizeof(wchar_t));
if (!wstr){
RETURN_BOOL(FALSE);
}
if (!MultiByteToWideChar(CP_UTF8, 0, filename, -1, wstr, wchars_num)){
free(wstr);
RETURN_BOOL(FALSE);
}
}
// Pass filename (as wide string) or NULL to CaptureScreen
result = (LONG_PTR)CaptureScreen(wstr);
if(wstr != NULL) {
free(wstr);
}
RETURN_LONG(result);
}
ZEND_FUNCTION(wb_destroy_image)
{
zend_long hbm;
//if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &hbm) == FAILURE)
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(hbm)
ZEND_PARSE_PARAMETERS_END();
if (!hbm){
RETURN_BOOL(FALSE);
}
RETURN_BOOL(wbDestroyBitmap((HBITMAP)hbm));
}
#ifdef __LCC__
// This is needed by PHP 5.1
int _finite(double x)
{
return (fpclassify(x) >= 0);
}
#endif
//------------------------------------------------------------------ END OF FILE