FreeRDP
Loading...
Searching...
No Matches
TestGdiCreate.c
1
2#include <freerdp/gdi/gdi.h>
3
4#include <freerdp/gdi/dc.h>
5#include <freerdp/gdi/pen.h>
6#include <freerdp/gdi/region.h>
7#include <freerdp/gdi/bitmap.h>
8
9#include <winpr/crt.h>
10#include <winpr/crypto.h>
11
12#include "line.h"
13#include "brush.h"
14#include "drawing.h"
15
16static const UINT32 colorFormatList[] = {
17 PIXEL_FORMAT_RGB15, PIXEL_FORMAT_BGR15, PIXEL_FORMAT_RGB16, PIXEL_FORMAT_BGR16,
18 PIXEL_FORMAT_RGB24, PIXEL_FORMAT_BGR24, PIXEL_FORMAT_ARGB32, PIXEL_FORMAT_ABGR32,
19 PIXEL_FORMAT_XRGB32, PIXEL_FORMAT_XBGR32, PIXEL_FORMAT_RGBX32, PIXEL_FORMAT_BGRX32
20
21};
22static const UINT32 colorFormatCount = sizeof(colorFormatList) / sizeof(colorFormatList[0]);
23
24static int test_gdi_GetDC(void)
25{
26 int rc = -1;
27 HGDI_DC hdc = nullptr;
28
29 if (!(hdc = gdi_GetDC()))
30 {
31 printf("failed to get gdi device context\n");
32 return -1;
33 }
34
35 if (hdc->format != PIXEL_FORMAT_XRGB32)
36 goto fail;
37
38 if (hdc->drawMode != GDI_R2_BLACK)
39 goto fail;
40
41 rc = 0;
42fail:
43 gdi_DeleteDC(hdc);
44 return rc;
45}
46
47static int test_gdi_CreateCompatibleDC(void)
48{
49 int rc = -1;
50 HGDI_DC hdc = nullptr;
51 HGDI_DC chdc = nullptr;
52
53 if (!(hdc = gdi_GetDC()))
54 {
55 printf("failed to get gdi device context\n");
56 return -1;
57 }
58
59 hdc->format = PIXEL_FORMAT_RGB16;
60 hdc->drawMode = GDI_R2_XORPEN;
61
62 if (!(chdc = gdi_CreateCompatibleDC(hdc)))
63 {
64 printf("gdi_CreateCompatibleDC failed\n");
65 goto fail;
66 }
67
68 if (chdc->format != hdc->format)
69 goto fail;
70
71 if (chdc->drawMode != hdc->drawMode)
72 goto fail;
73
74 rc = 0;
75fail:
76
77 if (chdc)
78 gdi_DeleteDC(chdc);
79
80 gdi_DeleteDC(hdc);
81 return rc;
82}
83
84static int test_gdi_CreateBitmap(void)
85{
86 int rc = -1;
87 UINT32 format = PIXEL_FORMAT_ARGB32;
88 INT32 width = 0;
89 INT32 height = 0;
90 BYTE* data = nullptr;
91 HGDI_BITMAP hBitmap = nullptr;
92 width = 32;
93 height = 16;
94
95 if (!(data = (BYTE*)winpr_aligned_malloc(4ULL * width * height, 16)))
96 {
97 printf("failed to allocate aligned bitmap data memory\n");
98 return -1;
99 }
100
101 if (!(hBitmap = gdi_CreateBitmap(width, height, format, data)))
102 {
103 printf("gdi_CreateBitmap failed\n");
104 goto fail;
105 }
106
107 if (hBitmap->objectType != GDIOBJECT_BITMAP)
108 goto fail;
109
110 if (hBitmap->format != format)
111 goto fail;
112
113 if (hBitmap->width != width)
114 goto fail;
115
116 if (hBitmap->height != height)
117 goto fail;
118
119 if (hBitmap->data != data)
120 goto fail;
121
122 rc = 0;
123fail:
124
125 if (hBitmap)
126 gdi_DeleteObject((HGDIOBJECT)hBitmap);
127 else
128 winpr_aligned_free(data);
129
130 return rc;
131}
132
133static int test_gdi_CreateCompatibleBitmap(void)
134{
135 int rc = -1;
136 HGDI_DC hdc = nullptr;
137 INT32 width = 0;
138 INT32 height = 0;
139 HGDI_BITMAP hBitmap = nullptr;
140
141 if (!(hdc = gdi_GetDC()))
142 {
143 printf("failed to get gdi device context\n");
144 return -1;
145 }
146
147 hdc->format = PIXEL_FORMAT_ARGB32;
148 width = 32;
149 height = 16;
150 hBitmap = gdi_CreateCompatibleBitmap(hdc, width, height);
151
152 if (hBitmap->objectType != GDIOBJECT_BITMAP)
153 goto fail;
154
155 if (hBitmap->format != hdc->format)
156 goto fail;
157
158 if (hBitmap->width != width)
159 goto fail;
160
161 if (hBitmap->height != height)
162 goto fail;
163
164 if (!hBitmap->data)
165 goto fail;
166
167 rc = 0;
168fail:
169
170 if (hBitmap)
171 gdi_DeleteObject((HGDIOBJECT)hBitmap);
172
173 gdi_DeleteDC(hdc);
174 return rc;
175}
176
177static int test_gdi_CreatePen(void)
178{
179 int rc = -1;
180 const UINT32 format = PIXEL_FORMAT_RGBA32;
181 HGDI_PEN hPen = gdi_CreatePen(GDI_PS_SOLID, 8, 0xAABBCCDD, format, nullptr);
182
183 if (!hPen)
184 {
185 printf("gdi_CreatePen failed\n");
186 return -1;
187 }
188
189 if (hPen->style != GDI_PS_SOLID)
190 goto fail;
191
192 if (hPen->width != 8)
193 goto fail;
194
195 if (hPen->color != 0xAABBCCDD)
196 goto fail;
197
198 rc = 0;
199fail:
200 gdi_DeleteObject((HGDIOBJECT)hPen);
201 return rc;
202}
203
204static int test_gdi_CreateSolidBrush(void)
205{
206 int rc = -1;
207 HGDI_BRUSH hBrush = gdi_CreateSolidBrush(0xAABBCCDD);
208
209 if (hBrush->objectType != GDIOBJECT_BRUSH)
210 goto fail;
211
212 if (hBrush->style != GDI_BS_SOLID)
213 goto fail;
214
215 if (hBrush->color != 0xAABBCCDD)
216 goto fail;
217
218 rc = 0;
219fail:
220 gdi_DeleteObject((HGDIOBJECT)hBrush);
221 return rc;
222}
223
224static int test_gdi_CreatePatternBrush(void)
225{
226 int rc = -1;
227 HGDI_BRUSH hBrush = nullptr;
228 HGDI_BITMAP hBitmap = nullptr;
229 hBitmap = gdi_CreateBitmap(64, 64, 32, nullptr);
230 hBrush = gdi_CreatePatternBrush(hBitmap);
231
232 if (!hBitmap || !hBrush)
233 goto fail;
234
235 if (hBrush->objectType != GDIOBJECT_BRUSH)
236 goto fail;
237
238 if (hBrush->style != GDI_BS_PATTERN)
239 goto fail;
240
241 if (hBrush->pattern != hBitmap)
242 goto fail;
243
244 rc = 0;
245fail:
246
247 if (hBitmap)
248 gdi_DeleteObject((HGDIOBJECT)hBitmap);
249
250 if (hBrush)
251 gdi_DeleteObject((HGDIOBJECT)hBrush);
252
253 return rc;
254}
255
256static int test_gdi_CreateRectRgn(void)
257{
258 int rc = -1;
259 INT32 x1 = 32;
260 INT32 y1 = 64;
261 INT32 x2 = 128;
262 INT32 y2 = 256;
263 HGDI_RGN hRegion = gdi_CreateRectRgn(x1, y1, x2, y2);
264
265 if (!hRegion)
266 return rc;
267
268 if (hRegion->objectType != GDIOBJECT_REGION)
269 goto fail;
270
271 if (hRegion->x != x1)
272 goto fail;
273
274 if (hRegion->y != y1)
275 goto fail;
276
277 if (hRegion->w != x2 - x1 + 1)
278 goto fail;
279
280 if (hRegion->h != y2 - y1 + 1)
281 goto fail;
282
283 if (hRegion->null)
284 goto fail;
285
286 rc = 0;
287fail:
288 gdi_DeleteObject((HGDIOBJECT)hRegion);
289 return rc;
290}
291
292static int test_gdi_CreateRect(void)
293{
294 int rc = -1;
295 GDI_RECT* hRect = nullptr;
296 INT32 x1 = 32;
297 INT32 y1 = 64;
298 INT32 x2 = 128;
299 INT32 y2 = 256;
300
301 if (!(hRect = gdi_CreateRect(x1, y1, x2, y2)))
302 {
303 printf("gdi_CreateRect failed\n");
304 return -1;
305 }
306
307 if (hRect->objectType != GDIOBJECT_RECT)
308 goto fail;
309
310 if (hRect->left != x1)
311 goto fail;
312
313 if (hRect->top != y1)
314 goto fail;
315
316 if (hRect->right != x2)
317 goto fail;
318
319 if (hRect->bottom != y2)
320 goto fail;
321
322 rc = 0;
323fail:
324 gdi_DeleteObject((HGDIOBJECT)hRect);
325 return rc;
326}
327
328static BYTE prand(void)
329{
330 BYTE tmp = 0;
331 if (winpr_RAND(&tmp, sizeof(tmp)) < 0)
332 {
333 (void)fprintf(stderr, "winpr_RAND faild,retry...\n");
334 // NOLINTNEXTLINE(concurrency-mt-unsafe)
335 exit(-1);
336 }
337 return tmp;
338}
339
340static BOOL test_gdi_GetPixel(void)
341{
342 BOOL rc = TRUE;
343
344 for (UINT32 x = 0; x < colorFormatCount; x++)
345 {
346 UINT32 bpp = 0;
347 HGDI_DC hdc = nullptr;
348 UINT32 width = 128;
349 UINT32 height = 64;
350 HGDI_BITMAP hBitmap = nullptr;
351
352 if (!(hdc = gdi_GetDC()))
353 {
354 printf("failed to get gdi device context\n");
355 return -1;
356 }
357
358 hdc->format = colorFormatList[x];
359 hBitmap = gdi_CreateCompatibleBitmap(hdc, width, height);
360
361 if (!hBitmap)
362 {
363 gdi_DeleteDC(hdc);
364 return -1;
365 }
366
367 gdi_SelectObject(hdc, (HGDIOBJECT)hBitmap);
368 bpp = FreeRDPGetBytesPerPixel(hBitmap->format);
369
370 for (UINT32 i = 0; i < height; i++)
371 {
372 for (UINT32 j = 0; j < width; j++)
373 {
374 UINT32 pixel = 0;
375 const UINT32 color =
376 FreeRDPGetColor(hBitmap->format, prand(), prand(), prand(), prand());
377 FreeRDPWriteColor(&hBitmap->data[i * hBitmap->scanline + j * bpp], hBitmap->format,
378 color);
379 pixel = gdi_GetPixel(hdc, j, i);
380
381 if (pixel != color)
382 {
383 rc = FALSE;
384 break;
385 }
386 }
387
388 if (!rc)
389 break;
390 }
391
392 gdi_DeleteObject((HGDIOBJECT)hBitmap);
393 gdi_DeleteDC(hdc);
394 }
395
396 return rc;
397}
398
399static BOOL test_gdi_SetPixel(void)
400{
401 BOOL rc = TRUE;
402
403 for (UINT32 x = 0; x < colorFormatCount; x++)
404 {
405 UINT32 bpp = 0;
406 HGDI_DC hdc = nullptr;
407 UINT32 width = 128;
408 UINT32 height = 64;
409 HGDI_BITMAP hBitmap = nullptr;
410
411 if (!(hdc = gdi_GetDC()))
412 {
413 printf("failed to get gdi device context\n");
414 return FALSE;
415 }
416
417 hdc->format = colorFormatList[x];
418 hBitmap = gdi_CreateCompatibleBitmap(hdc, width, height);
419 gdi_SelectObject(hdc, (HGDIOBJECT)hBitmap);
420 bpp = FreeRDPGetBytesPerPixel(hBitmap->format);
421
422 for (UINT32 i = 0; i < height; i++)
423 {
424 for (UINT32 j = 0; j < width; j++)
425 {
426 UINT32 pixel = 0;
427 const UINT32 color =
428 FreeRDPGetColor(hBitmap->format, prand(), prand(), prand(), prand());
429 gdi_SetPixel(hdc, j, i, color);
430 pixel = FreeRDPReadColor(&hBitmap->data[i * hBitmap->scanline + j * bpp],
431 hBitmap->format);
432
433 if (pixel != color)
434 {
435 rc = FALSE;
436 break;
437 }
438 }
439
440 if (!rc)
441 break;
442 }
443
444 gdi_DeleteObject((HGDIOBJECT)hBitmap);
445 gdi_DeleteDC(hdc);
446 }
447
448 return rc;
449}
450
451static int test_gdi_SetROP2(void)
452{
453 int rc = -1;
454 HGDI_DC hdc = nullptr;
455
456 if (!(hdc = gdi_GetDC()))
457 {
458 printf("failed to get gdi device context\n");
459 return -1;
460 }
461
462 gdi_SetROP2(hdc, GDI_R2_BLACK);
463
464 if (hdc->drawMode != GDI_R2_BLACK)
465 goto fail;
466
467 rc = 0;
468fail:
469 gdi_DeleteDC(hdc);
470 return rc;
471}
472
473static int test_gdi_MoveToEx(void)
474{
475 int rc = -1;
476 HGDI_DC hdc = nullptr;
477 HGDI_PEN hPen = nullptr;
478 HGDI_POINT prevPoint = nullptr;
479 const UINT32 format = PIXEL_FORMAT_RGBA32;
480 gdiPalette* palette = nullptr;
481
482 if (!(hdc = gdi_GetDC()))
483 {
484 printf("failed to get gdi device context\n");
485 return -1;
486 }
487
488 if (!(hPen = gdi_CreatePen(GDI_PS_SOLID, 8, 0xAABBCCDD, format, palette)))
489 {
490 printf("gdi_CreatePen failed\n");
491 goto fail;
492 }
493
494 gdi_SelectObject(hdc, (HGDIOBJECT)hPen);
495 gdi_MoveToEx(hdc, 128, 256, nullptr);
496
497 if (hdc->pen->posX != 128)
498 goto fail;
499
500 if (hdc->pen->posY != 256)
501 goto fail;
502
503 prevPoint = (HGDI_POINT)malloc(sizeof(GDI_POINT));
504 ZeroMemory(prevPoint, sizeof(GDI_POINT));
505 gdi_MoveToEx(hdc, 64, 128, prevPoint);
506
507 if (prevPoint->x != 128)
508 goto fail;
509
510 if (prevPoint->y != 256)
511 goto fail;
512
513 if (hdc->pen->posX != 64)
514 goto fail;
515
516 if (hdc->pen->posY != 128)
517 goto fail;
518
519 rc = 0;
520fail:
521
522 if (hPen)
523 gdi_DeleteObject((HGDIOBJECT)hPen);
524
525 free(prevPoint);
526 gdi_DeleteDC(hdc);
527 return rc;
528}
529
530int TestGdiCreate(int argc, char* argv[])
531{
532 WINPR_UNUSED(argc);
533 WINPR_UNUSED(argv);
534 (void)fprintf(stderr, "test_gdi_GetDC()\n");
535
536 if (test_gdi_GetDC() < 0)
537 return -1;
538
539 (void)fprintf(stderr, "test_gdi_CreateCompatibleDC()\n");
540
541 if (test_gdi_CreateCompatibleDC() < 0)
542 return -1;
543
544 (void)fprintf(stderr, "test_gdi_CreateBitmap()\n");
545
546 if (test_gdi_CreateBitmap() < 0)
547 return -1;
548
549 (void)fprintf(stderr, "test_gdi_CreateCompatibleBitmap()\n");
550
551 if (test_gdi_CreateCompatibleBitmap() < 0)
552 return -1;
553
554 (void)fprintf(stderr, "test_gdi_CreatePen()\n");
555
556 if (test_gdi_CreatePen() < 0)
557 return -1;
558
559 (void)fprintf(stderr, "test_gdi_CreateSolidBrush()\n");
560
561 if (test_gdi_CreateSolidBrush() < 0)
562 return -1;
563
564 (void)fprintf(stderr, "test_gdi_CreatePatternBrush()\n");
565
566 if (test_gdi_CreatePatternBrush() < 0)
567 return -1;
568
569 (void)fprintf(stderr, "test_gdi_CreateRectRgn()\n");
570
571 if (test_gdi_CreateRectRgn() < 0)
572 return -1;
573
574 (void)fprintf(stderr, "test_gdi_CreateRect()\n");
575
576 if (test_gdi_CreateRect() < 0)
577 return -1;
578
579 (void)fprintf(stderr, "test_gdi_GetPixel()\n");
580
581 if (!test_gdi_GetPixel())
582 return -1;
583
584 (void)fprintf(stderr, "test_gdi_SetPixel()\n");
585
586 if (!test_gdi_SetPixel())
587 return -1;
588
589 (void)fprintf(stderr, "test_gdi_SetROP2()\n");
590
591 if (test_gdi_SetROP2() < 0)
592 return -1;
593
594 (void)fprintf(stderr, "test_gdi_MoveToEx()\n");
595
596 if (test_gdi_MoveToEx() < 0)
597 return -1;
598
599 return 0;
600}