FreeRDP
TestGdiRect.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/shape.h>
7 #include <freerdp/gdi/region.h>
8 #include <freerdp/gdi/bitmap.h>
9 
10 #include <winpr/crt.h>
11 #include <winpr/print.h>
12 
13 #include "line.h"
14 #include "brush.h"
15 #include "clipping.h"
16 
17 static int test_gdi_PtInRect(void)
18 {
19  int rc = -1;
20  HGDI_RECT hRect = NULL;
21  UINT32 left = 20;
22  UINT32 top = 40;
23  UINT32 right = 60;
24  UINT32 bottom = 80;
25 
26  if (!(hRect = gdi_CreateRect(left, top, right, bottom)))
27  {
28  printf("gdi_CreateRect failed\n");
29  return rc;
30  }
31 
32  if (gdi_PtInRect(hRect, 0, 0))
33  goto fail;
34 
35  if (gdi_PtInRect(hRect, 500, 500))
36  goto fail;
37 
38  if (gdi_PtInRect(hRect, 40, 100))
39  goto fail;
40 
41  if (gdi_PtInRect(hRect, 10, 40))
42  goto fail;
43 
44  if (!gdi_PtInRect(hRect, 30, 50))
45  goto fail;
46 
47  if (!gdi_PtInRect(hRect, left, top))
48  goto fail;
49 
50  if (!gdi_PtInRect(hRect, right, bottom))
51  goto fail;
52 
53  if (!gdi_PtInRect(hRect, right, 60))
54  goto fail;
55 
56  if (!gdi_PtInRect(hRect, 40, bottom))
57  goto fail;
58 
59  rc = 0;
60 fail:
61  gdi_DeleteObject((HGDIOBJECT)hRect);
62  return rc;
63 }
64 
65 static int test_gdi_FillRect(void)
66 {
67  int rc = -1;
68  HGDI_DC hdc = NULL;
69  HGDI_RECT hRect = NULL;
70  HGDI_BRUSH hBrush = NULL;
71  HGDI_BITMAP hBitmap = NULL;
72  UINT32 color = 0;
73  UINT32 pixel = 0;
74  UINT32 rawPixel = 0;
75  UINT32 badPixels = 0;
76  UINT32 goodPixels = 0;
77  UINT32 width = 200;
78  UINT32 height = 300;
79  UINT32 left = 20;
80  UINT32 top = 40;
81  UINT32 right = 60;
82  UINT32 bottom = 80;
83 
84  if (!(hdc = gdi_GetDC()))
85  {
86  printf("failed to get gdi device context\n");
87  goto fail;
88  }
89 
90  hdc->format = PIXEL_FORMAT_XRGB32;
91 
92  if (!(hRect = gdi_CreateRect(left, top, right, bottom)))
93  {
94  printf("gdi_CreateRect failed\n");
95  goto fail;
96  }
97 
98  hBitmap = gdi_CreateCompatibleBitmap(hdc, width, height);
99  ZeroMemory(hBitmap->data, 1ULL * width * height * FreeRDPGetBytesPerPixel(hdc->format));
100  gdi_SelectObject(hdc, (HGDIOBJECT)hBitmap);
101  color = FreeRDPGetColor(PIXEL_FORMAT_ARGB32, 0xAA, 0xBB, 0xCC, 0xFF);
102  hBrush = gdi_CreateSolidBrush(color);
103  gdi_FillRect(hdc, hRect, hBrush);
104  badPixels = 0;
105  goodPixels = 0;
106 
107  for (UINT32 x = 0; x < width; x++)
108  {
109  for (UINT32 y = 0; y < height; y++)
110  {
111  rawPixel = gdi_GetPixel(hdc, x, y);
112  pixel = FreeRDPConvertColor(rawPixel, hdc->format, PIXEL_FORMAT_ARGB32, NULL);
113 
114  if (gdi_PtInRect(hRect, x, y))
115  {
116  if (pixel == color)
117  {
118  goodPixels++;
119  }
120  else
121  {
122  printf("actual:%08" PRIX32 " expected:%08" PRIX32 "\n", gdi_GetPixel(hdc, x, y),
123  color);
124  badPixels++;
125  }
126  }
127  else
128  {
129  if (pixel == color)
130  {
131  badPixels++;
132  }
133  else
134  {
135  goodPixels++;
136  }
137  }
138  }
139  }
140 
141  if (goodPixels != width * height)
142  goto fail;
143 
144  if (badPixels != 0)
145  goto fail;
146 
147  rc = 0;
148 fail:
149  gdi_DeleteObject((HGDIOBJECT)hBrush);
150  gdi_DeleteObject((HGDIOBJECT)hBitmap);
151  gdi_DeleteObject((HGDIOBJECT)hRect);
152  gdi_DeleteDC(hdc);
153  return rc;
154 }
155 
156 int TestGdiRect(int argc, char* argv[])
157 {
158  WINPR_UNUSED(argc);
159  WINPR_UNUSED(argv);
160 
161  if (test_gdi_PtInRect() < 0)
162  return -1;
163 
164  if (test_gdi_FillRect() < 0)
165  return -1;
166 
167  return 0;
168 }