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