FreeRDP
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 
16 static 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 };
22 static const UINT32 colorFormatCount = sizeof(colorFormatList) / sizeof(colorFormatList[0]);
23 
24 static int test_gdi_GetDC(void)
25 {
26  int rc = -1;
27  HGDI_DC hdc = NULL;
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;
42 fail:
43  gdi_DeleteDC(hdc);
44  return rc;
45 }
46 
47 static int test_gdi_CreateCompatibleDC(void)
48 {
49  int rc = -1;
50  HGDI_DC hdc = NULL;
51  HGDI_DC chdc = NULL;
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;
75 fail:
76 
77  if (chdc)
78  gdi_DeleteDC(chdc);
79 
80  gdi_DeleteDC(hdc);
81  return rc;
82 }
83 
84 static 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 = NULL;
91  HGDI_BITMAP hBitmap = NULL;
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;
123 fail:
124 
125  if (hBitmap)
126  gdi_DeleteObject((HGDIOBJECT)hBitmap);
127  else
128  winpr_aligned_free(data);
129 
130  return rc;
131 }
132 
133 static int test_gdi_CreateCompatibleBitmap(void)
134 {
135  int rc = -1;
136  HGDI_DC hdc = NULL;
137  INT32 width = 0;
138  INT32 height = 0;
139  HGDI_BITMAP hBitmap = NULL;
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;
168 fail:
169 
170  if (hBitmap)
171  gdi_DeleteObject((HGDIOBJECT)hBitmap);
172 
173  gdi_DeleteDC(hdc);
174  return rc;
175 }
176 
177 static 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, NULL);
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;
199 fail:
200  gdi_DeleteObject((HGDIOBJECT)hPen);
201  return rc;
202 }
203 
204 static 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;
219 fail:
220  gdi_DeleteObject((HGDIOBJECT)hBrush);
221  return rc;
222 }
223 
224 static int test_gdi_CreatePatternBrush(void)
225 {
226  int rc = -1;
227  HGDI_BRUSH hBrush = NULL;
228  HGDI_BITMAP hBitmap = NULL;
229  hBitmap = gdi_CreateBitmap(64, 64, 32, NULL);
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;
245 fail:
246 
247  if (hBitmap)
248  gdi_DeleteObject((HGDIOBJECT)hBitmap);
249 
250  if (hBrush)
251  gdi_DeleteObject((HGDIOBJECT)hBrush);
252 
253  return rc;
254 }
255 
256 static 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;
287 fail:
288  gdi_DeleteObject((HGDIOBJECT)hRegion);
289  return rc;
290 }
291 
292 static int test_gdi_CreateRect(void)
293 {
294  int rc = -1;
295  HGDI_RECT hRect = NULL;
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;
323 fail:
324  gdi_DeleteObject((HGDIOBJECT)hRect);
325  return rc;
326 }
327 
328 static BYTE prand(void)
329 {
330  BYTE tmp = 0;
331  winpr_RAND(&tmp, sizeof(tmp));
332  return tmp;
333 }
334 
335 static BOOL test_gdi_GetPixel(void)
336 {
337  BOOL rc = TRUE;
338 
339  for (UINT32 x = 0; x < colorFormatCount; x++)
340  {
341  UINT32 bpp = 0;
342  HGDI_DC hdc = NULL;
343  UINT32 width = 128;
344  UINT32 height = 64;
345  HGDI_BITMAP hBitmap = NULL;
346 
347  if (!(hdc = gdi_GetDC()))
348  {
349  printf("failed to get gdi device context\n");
350  return -1;
351  }
352 
353  hdc->format = colorFormatList[x];
354  hBitmap = gdi_CreateCompatibleBitmap(hdc, width, height);
355 
356  if (!hBitmap)
357  {
358  gdi_DeleteDC(hdc);
359  return -1;
360  }
361 
362  gdi_SelectObject(hdc, (HGDIOBJECT)hBitmap);
363  bpp = FreeRDPGetBytesPerPixel(hBitmap->format);
364 
365  for (UINT32 i = 0; i < height; i++)
366  {
367  for (UINT32 j = 0; j < width; j++)
368  {
369  UINT32 pixel = 0;
370  const UINT32 color =
371  FreeRDPGetColor(hBitmap->format, prand(), prand(), prand(), prand());
372  FreeRDPWriteColor(&hBitmap->data[i * hBitmap->scanline + j * bpp], hBitmap->format,
373  color);
374  pixel = gdi_GetPixel(hdc, j, i);
375 
376  if (pixel != color)
377  {
378  rc = FALSE;
379  break;
380  }
381  }
382 
383  if (!rc)
384  break;
385  }
386 
387  gdi_DeleteObject((HGDIOBJECT)hBitmap);
388  gdi_DeleteDC(hdc);
389  }
390 
391  return rc;
392 }
393 
394 static BOOL test_gdi_SetPixel(void)
395 {
396  BOOL rc = TRUE;
397 
398  for (UINT32 x = 0; x < colorFormatCount; x++)
399  {
400  UINT32 bpp = 0;
401  HGDI_DC hdc = NULL;
402  UINT32 width = 128;
403  UINT32 height = 64;
404  HGDI_BITMAP hBitmap = NULL;
405 
406  if (!(hdc = gdi_GetDC()))
407  {
408  printf("failed to get gdi device context\n");
409  return FALSE;
410  }
411 
412  hdc->format = colorFormatList[x];
413  hBitmap = gdi_CreateCompatibleBitmap(hdc, width, height);
414  gdi_SelectObject(hdc, (HGDIOBJECT)hBitmap);
415  bpp = FreeRDPGetBytesPerPixel(hBitmap->format);
416 
417  for (UINT32 i = 0; i < height; i++)
418  {
419  for (UINT32 j = 0; j < width; j++)
420  {
421  UINT32 pixel = 0;
422  const UINT32 color =
423  FreeRDPGetColor(hBitmap->format, prand(), prand(), prand(), prand());
424  gdi_SetPixel(hdc, j, i, color);
425  pixel = FreeRDPReadColor(&hBitmap->data[i * hBitmap->scanline + j * bpp],
426  hBitmap->format);
427 
428  if (pixel != color)
429  {
430  rc = FALSE;
431  break;
432  }
433  }
434 
435  if (!rc)
436  break;
437  }
438 
439  gdi_DeleteObject((HGDIOBJECT)hBitmap);
440  gdi_DeleteDC(hdc);
441  }
442 
443  return rc;
444 }
445 
446 static int test_gdi_SetROP2(void)
447 {
448  int rc = -1;
449  HGDI_DC hdc = NULL;
450 
451  if (!(hdc = gdi_GetDC()))
452  {
453  printf("failed to get gdi device context\n");
454  return -1;
455  }
456 
457  gdi_SetROP2(hdc, GDI_R2_BLACK);
458 
459  if (hdc->drawMode != GDI_R2_BLACK)
460  goto fail;
461 
462  rc = 0;
463 fail:
464  gdi_DeleteDC(hdc);
465  return rc;
466 }
467 
468 static int test_gdi_MoveToEx(void)
469 {
470  int rc = -1;
471  HGDI_DC hdc = NULL;
472  HGDI_PEN hPen = NULL;
473  HGDI_POINT prevPoint = NULL;
474  const UINT32 format = PIXEL_FORMAT_RGBA32;
475  gdiPalette* palette = NULL;
476 
477  if (!(hdc = gdi_GetDC()))
478  {
479  printf("failed to get gdi device context\n");
480  return -1;
481  }
482 
483  if (!(hPen = gdi_CreatePen(GDI_PS_SOLID, 8, 0xAABBCCDD, format, palette)))
484  {
485  printf("gdi_CreatePen failed\n");
486  goto fail;
487  }
488 
489  gdi_SelectObject(hdc, (HGDIOBJECT)hPen);
490  gdi_MoveToEx(hdc, 128, 256, NULL);
491 
492  if (hdc->pen->posX != 128)
493  goto fail;
494 
495  if (hdc->pen->posY != 256)
496  goto fail;
497 
498  prevPoint = (HGDI_POINT)malloc(sizeof(GDI_POINT));
499  ZeroMemory(prevPoint, sizeof(GDI_POINT));
500  gdi_MoveToEx(hdc, 64, 128, prevPoint);
501 
502  if (prevPoint->x != 128)
503  goto fail;
504 
505  if (prevPoint->y != 256)
506  goto fail;
507 
508  if (hdc->pen->posX != 64)
509  goto fail;
510 
511  if (hdc->pen->posY != 128)
512  goto fail;
513 
514  rc = 0;
515 fail:
516 
517  if (hPen)
518  gdi_DeleteObject((HGDIOBJECT)hPen);
519 
520  free(prevPoint);
521  gdi_DeleteDC(hdc);
522  return rc;
523 }
524 
525 int TestGdiCreate(int argc, char* argv[])
526 {
527  WINPR_UNUSED(argc);
528  WINPR_UNUSED(argv);
529  (void)fprintf(stderr, "test_gdi_GetDC()\n");
530 
531  if (test_gdi_GetDC() < 0)
532  return -1;
533 
534  (void)fprintf(stderr, "test_gdi_CreateCompatibleDC()\n");
535 
536  if (test_gdi_CreateCompatibleDC() < 0)
537  return -1;
538 
539  (void)fprintf(stderr, "test_gdi_CreateBitmap()\n");
540 
541  if (test_gdi_CreateBitmap() < 0)
542  return -1;
543 
544  (void)fprintf(stderr, "test_gdi_CreateCompatibleBitmap()\n");
545 
546  if (test_gdi_CreateCompatibleBitmap() < 0)
547  return -1;
548 
549  (void)fprintf(stderr, "test_gdi_CreatePen()\n");
550 
551  if (test_gdi_CreatePen() < 0)
552  return -1;
553 
554  (void)fprintf(stderr, "test_gdi_CreateSolidBrush()\n");
555 
556  if (test_gdi_CreateSolidBrush() < 0)
557  return -1;
558 
559  (void)fprintf(stderr, "test_gdi_CreatePatternBrush()\n");
560 
561  if (test_gdi_CreatePatternBrush() < 0)
562  return -1;
563 
564  (void)fprintf(stderr, "test_gdi_CreateRectRgn()\n");
565 
566  if (test_gdi_CreateRectRgn() < 0)
567  return -1;
568 
569  (void)fprintf(stderr, "test_gdi_CreateRect()\n");
570 
571  if (test_gdi_CreateRect() < 0)
572  return -1;
573 
574  (void)fprintf(stderr, "test_gdi_GetPixel()\n");
575 
576  if (!test_gdi_GetPixel())
577  return -1;
578 
579  (void)fprintf(stderr, "test_gdi_SetPixel()\n");
580 
581  if (!test_gdi_SetPixel())
582  return -1;
583 
584  (void)fprintf(stderr, "test_gdi_SetROP2()\n");
585 
586  if (test_gdi_SetROP2() < 0)
587  return -1;
588 
589  (void)fprintf(stderr, "test_gdi_MoveToEx()\n");
590 
591  if (test_gdi_MoveToEx() < 0)
592  return -1;
593 
594  return 0;
595 }