FreeRDP
Loading...
Searching...
No Matches
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
17static int test_gdi_PtInRect(void)
18{
19 int rc = -1;
20 GDI_RECT* hRect = nullptr;
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;
64fail:
65 gdi_DeleteObject((HGDIOBJECT)hRect);
66 return rc;
67}
68
69static int test_gdi_FillRect(void)
70{
71 int rc = -1;
72 HGDI_DC hdc = nullptr;
73 GDI_RECT* hRect = nullptr;
74 HGDI_BRUSH hBrush = nullptr;
75 HGDI_BITMAP hBitmap = nullptr;
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 if (!hBitmap)
106 goto fail;
107 ZeroMemory(hBitmap->data, 1ULL * width * height * FreeRDPGetBytesPerPixel(hdc->format));
108 gdi_SelectObject(hdc, (HGDIOBJECT)hBitmap);
109 color = FreeRDPGetColor(PIXEL_FORMAT_ARGB32, 0xAA, 0xBB, 0xCC, 0xFF);
110 hBrush = gdi_CreateSolidBrush(color);
111 if (!hBrush)
112 goto fail;
113 if (!gdi_FillRect(hdc, hRect, hBrush))
114 goto fail;
115 badPixels = 0;
116 goodPixels = 0;
117
118 for (UINT32 x = 0; x < width; x++)
119 {
120 for (UINT32 y = 0; y < height; y++)
121 {
122 rawPixel = gdi_GetPixel(hdc, x, y);
123 pixel = FreeRDPConvertColor(rawPixel, hdc->format, PIXEL_FORMAT_ARGB32, nullptr);
124
125 if (gdi_PtInRect(hRect, WINPR_ASSERTING_INT_CAST(int, x),
126 WINPR_ASSERTING_INT_CAST(int, y)))
127 {
128 if (pixel == color)
129 {
130 goodPixels++;
131 }
132 else
133 {
134 printf("actual:%08" PRIX32 " expected:%08" PRIX32 "\n", gdi_GetPixel(hdc, x, y),
135 color);
136 badPixels++;
137 }
138 }
139 else
140 {
141 if (pixel == color)
142 {
143 badPixels++;
144 }
145 else
146 {
147 goodPixels++;
148 }
149 }
150 }
151 }
152
153 if (goodPixels != width * height)
154 goto fail;
155
156 if (badPixels != 0)
157 goto fail;
158
159 rc = 0;
160fail:
161 gdi_DeleteObject((HGDIOBJECT)hBrush);
162 gdi_DeleteObject((HGDIOBJECT)hBitmap);
163 gdi_DeleteObject((HGDIOBJECT)hRect);
164 gdi_DeleteDC(hdc);
165 return rc;
166}
167
168int TestGdiRect(int argc, char* argv[])
169{
170 WINPR_UNUSED(argc);
171 WINPR_UNUSED(argv);
172
173 if (test_gdi_PtInRect() < 0)
174 return -1;
175
176 if (test_gdi_FillRect() < 0)
177 return -1;
178
179 return 0;
180}