[GDI32_APITEST]
[reactos.git] / rostests / apitests / gdi32 / CreateBitmap.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPL - See COPYING in the top level directory
4 * PURPOSE: Test for CreateBitmap
5 * PROGRAMMERS: Timo Kreuzer
6 */
7
8 #include <stdio.h>
9 #include <wine/test.h>
10 #include <windows.h>
11
12 #define DEFAULT_BITMAP 21
13
14 void Test_CreateBitmap_Params()
15 {
16 HBITMAP hbmp;
17
18 /* All of these should get us the default bitmap */
19 hbmp = CreateBitmap(0, 0, 0, 0, NULL);
20 ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
21 hbmp = CreateBitmap(1, 0, 0, 0, NULL);
22 ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
23 hbmp = CreateBitmap(0, 1, 0, 0, NULL);
24 ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
25 hbmp = CreateBitmap(0, 1, 1, 0, NULL);
26 ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
27 hbmp = CreateBitmap(0, 1, 63, 33, NULL);
28 ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
29 hbmp = CreateBitmap(0, -4, -32, 233, NULL);
30 ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
31
32 SetLastError(0);
33 hbmp = CreateBitmap(1, -1, 1, 0, NULL);
34 ok(hbmp == 0, "\n");
35 ok_err(ERROR_INVALID_PARAMETER);
36
37 SetLastError(0);
38 hbmp = CreateBitmap(-1, 1, 1, 0, NULL);
39 ok(hbmp == 0, "\n");
40 ok_err(ERROR_INVALID_PARAMETER);
41
42 SetLastError(0);
43 hbmp = CreateBitmap(-1, 1, 1, 1, NULL);
44 ok(hbmp == 0, "\n");
45 ok_err(ERROR_INVALID_PARAMETER);
46
47 SetLastError(0);
48 hbmp = CreateBitmap(1, -1, 1, 1, NULL);
49 ok(hbmp == 0, "\n");
50 ok_err(ERROR_INVALID_PARAMETER);
51
52 /* Check if an overflow in cPlanes * cBitsPixel is handled */
53 SetLastError(0);
54 hbmp = CreateBitmap(1, 1, 2, 0x80000004, NULL);
55 ok(hbmp == 0, "\n");
56 ok_err(ERROR_INVALID_PARAMETER);
57
58 /* Check for maximum width */
59 hbmp = CreateBitmap(0x7FFFFFF, 1, 1, 1, NULL);
60 ok(hbmp != 0, "\n");
61 DeleteObject(hbmp);
62
63 SetLastError(0);
64 hbmp = CreateBitmap(0x8000000, 1, 1, 1, NULL);
65 ok(hbmp == 0, "\n");
66 ok_err(ERROR_INVALID_PARAMETER);
67
68 /* Check for maximum height */
69 hbmp = CreateBitmap(1, 0x1FFFFF00, 1, 1, NULL);
70 //ok(hbmp != 0, "\n"); // fails on windows 2003
71 DeleteObject(hbmp);
72
73 SetLastError(0);
74 hbmp = CreateBitmap(1, 0x1FFFFFFF, 1, 1, NULL);
75 ok(hbmp == 0, "\n");
76 ok_err(0);
77
78 /* Check for overflow in width * height */
79 hbmp = CreateBitmap(0x20000, 0x1FFFF, 1, 1, NULL);
80 //ok(hbmp != 0, "\n"); // fails on windows 2003
81 DeleteObject(hbmp);
82
83 SetLastError(0);
84 hbmp = CreateBitmap(0x20000, 0x20000, 1, 1, NULL);
85 ok(hbmp == 0, "\n");
86 ok_err(0);
87
88 /* Check huge allocation */
89 SetLastError(0);
90 hbmp = CreateBitmap(0x2000, 0x20000, 32, 1, NULL);
91 ok(hbmp == 0, "\n");
92 ok_err(ERROR_INVALID_PARAMETER);
93
94 }
95
96 void Test_CreateBitmap()
97 {
98 HBITMAP hbmp;
99 BITMAP bitmap;
100 ULONG cjWidthBytes, cBitsPixel, cExpectedBitsPixel;
101
102 hbmp = CreateBitmap(0, 0, 0, 0, NULL);
103 ok(hbmp != 0, "should get a 1x1 bitmap\n");
104 ok(hbmp == GetStockObject(DEFAULT_BITMAP), "\n");
105 ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
106 ok_int(bitmap.bmType, 0);
107 ok_int(bitmap.bmWidth, 1);
108 ok_int(bitmap.bmHeight, 1);
109 ok_int(bitmap.bmWidthBytes, 2);
110 ok_int(bitmap.bmPlanes, 1);
111 ok_int(bitmap.bmBitsPixel, 1);
112 ok_ptr(bitmap.bmBits, 0);
113 DeleteObject(hbmp);
114
115 hbmp = CreateBitmap(1, 2, 1, 1, NULL);
116 ok(hbmp != 0, "should get a 1x2 bitmap\n");
117 ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
118 ok_int(bitmap.bmType, 0);
119 ok_int(bitmap.bmWidth, 1);
120 ok_int(bitmap.bmHeight, 2);
121 ok_int(bitmap.bmWidthBytes, 2);
122 ok_int(bitmap.bmPlanes, 1);
123 ok_int(bitmap.bmBitsPixel, 1);
124 ok_ptr(bitmap.bmBits, 0);
125 DeleteObject(hbmp);
126
127 for (cBitsPixel = 0; cBitsPixel <= 32; cBitsPixel++)
128 {
129 /* CreateBitmap API accepts any number as BitsPixels param.
130 but it just can create 1, 4, 8, 16, 24, 32 bpp Bitmaps */
131 if (cBitsPixel <= 1) cExpectedBitsPixel = 1;
132 else if (cBitsPixel <= 4) cExpectedBitsPixel = 4;
133 else if (cBitsPixel <= 8) cExpectedBitsPixel = 8;
134 else if (cBitsPixel <= 16) cExpectedBitsPixel = 16;
135 else if (cBitsPixel <= 24) cExpectedBitsPixel = 24;
136 else if (cBitsPixel <= 32) cExpectedBitsPixel = 32;
137
138 hbmp = CreateBitmap(1, 2, 1, cBitsPixel, NULL);
139 ok(hbmp != 0, "should get a 1x2 bitmap %ld\n", cBitsPixel);
140 ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
141
142 /* calculate expected line width */
143 cjWidthBytes = ((((ULONG)bitmap.bmWidth) * ((ULONG)bitmap.bmBitsPixel) + 15) & ~15) >> 3;
144
145 ok_int(bitmap.bmType, 0);
146 ok_int(bitmap.bmWidth, 1);
147 ok_int(bitmap.bmHeight, 2);
148 ok_int(bitmap.bmPlanes, 1);
149 ok_int(bitmap.bmBitsPixel, cExpectedBitsPixel);
150 ok_int(bitmap.bmWidthBytes, cjWidthBytes);
151 ok_ptr(bitmap.bmBits, 0);
152 DeleteObject(hbmp);
153
154 }
155
156 hbmp = CreateBitmap(1, 2, 1, 33, NULL);
157 ok(hbmp == 0, "Expected failure for 33 bpp\n");
158
159
160
161 }
162
163 START_TEST(CreateBitmap)
164 {
165 //Test_CreateBitmap_Params();
166 Test_CreateBitmap();
167
168 }