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 "precomp.h"
9 
10 void Test_CreateBitmap_Params()
11 {
12     HBITMAP hbmp;
13 
14     /* All of these should get us the default bitmap */
15     hbmp = CreateBitmap(0, 0, 0, 0, NULL);
16     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
17     hbmp = CreateBitmap(1, 0, 0, 0, NULL);
18     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
19     hbmp = CreateBitmap(0, 1, 0, 0, NULL);
20     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
21     hbmp = CreateBitmap(0, 1, 1, 0, NULL);
22     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
23     hbmp = CreateBitmap(0, 1, 63, 33, NULL);
24     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
25     hbmp = CreateBitmap(0, -4, -32, 233, NULL);
26     ok(hbmp == GetStockObject(21), "should get the default bitmap\n");
27 
28     SetLastError(0);
29     hbmp = CreateBitmap(1, -1, 1, 0, NULL);
30     ok(hbmp == 0, "CreateBitmap should fail\n");
31     ok_err(ERROR_INVALID_PARAMETER);
32 
33     SetLastError(0);
34     hbmp = CreateBitmap(-1, 1, 1, 0, NULL);
35     ok(hbmp == 0, "CreateBitmap should fail\n");
36     ok_err(ERROR_INVALID_PARAMETER);
37 
38     SetLastError(0);
39     hbmp = CreateBitmap(-1, 1, 1, 1, NULL);
40     ok(hbmp == 0, "CreateBitmap should fail\n");
41     ok_err(ERROR_INVALID_PARAMETER);
42 
43     SetLastError(0);
44     hbmp = CreateBitmap(1, -1, 1, 1, NULL);
45     ok(hbmp == 0, "CreateBitmap should fail\n");
46     ok_err(ERROR_INVALID_PARAMETER);
47 
48     /* Check if an overflow in cPlanes * cBitsPixel is handled */
49     SetLastError(0);
50     hbmp = CreateBitmap(1, 1, 2, 0x80000004, NULL);
51     ok(hbmp == 0, "CreateBitmap should fail\n");
52     ok_err(ERROR_INVALID_PARAMETER);
53 
54     /* Check for maximum width */
55     hbmp = CreateBitmap(0x7FFFFFF, 1, 1, 1, NULL);
56     ok(hbmp != 0, "CreateBitmap failed\n");
57     DeleteObject(hbmp);
58 
59     SetLastError(0);
60     hbmp = CreateBitmap(0x8000000, 1, 1, 1, NULL);
61     ok(hbmp == 0, "CreateBitmap should fail\n");
62     ok_err(ERROR_INVALID_PARAMETER);
63 
64     /* Check for maximum height */
65     hbmp = CreateBitmap(1, 0x1FFFFF00, 1, 1, NULL);
66     //ok(hbmp != 0, "\n"); // fails on windows 2003
67     DeleteObject(hbmp);
68 
69     SetLastError(0);
70     hbmp = CreateBitmap(1, 0x1FFFFFFF, 1, 1, NULL);
71     ok(hbmp == 0, "CreateBitmap should fail\n");
72     ok_err(0);
73 
74     SetLastError(0);
75     hbmp = CreateBitmap(1, -1, 1, 1, NULL);
76     ok(hbmp == 0, "CreateBitmap should fail\n");
77     ok_err(ERROR_INVALID_PARAMETER);
78 
79     /* Test huge allocation (256 GB) */
80     SetLastError(0);
81     hbmp = CreateBitmap(0x40000, 0x40000, 32, 1, NULL);
82     ok(hbmp == 0, "CreateBitmap should fail\n");
83     ok_err(ERROR_INVALID_PARAMETER);
84 
85     /* Test planes / bpp */
86     hbmp = CreateBitmap(10, 10, 32, 1, NULL);
87     ok(hbmp != 0, "CreateBitmap failed\n");
88     DeleteObject(hbmp);
89     hbmp = CreateBitmap(10, 10, 5, 5, NULL);
90     ok(hbmp != 0, "CreateBitmap failed\n");
91     DeleteObject(hbmp);
92 
93     SetLastError(0);
94     hbmp = CreateBitmap(10, 10, 33, 1, NULL);
95     ok(hbmp == 0, "CreateBitmap should fail\n");
96     ok_err(ERROR_INVALID_PARAMETER);
97 
98     SetLastError(0);
99     hbmp = CreateBitmap(10, 10, 1, 33, NULL);
100     ok(hbmp == 0, "CreateBitmap should fail\n");
101     ok_err(ERROR_INVALID_PARAMETER);
102 
103     SetLastError(0);
104     hbmp = CreateBitmap(10, 10, 6, 6, NULL);
105     ok(hbmp == 0, "CreateBitmap should fail\n");
106     ok_err(ERROR_INVALID_PARAMETER);
107 
108     SetLastError(0);
109     hbmp = CreateBitmap(10, 10, 8, 8, NULL);
110     ok(hbmp == 0, "CreateBitmap should fail\n");
111     ok_err(ERROR_INVALID_PARAMETER);
112 
113 }
114 
115 void Test_CreateBitmap()
116 {
117     HBITMAP hbmp;
118     BITMAP bitmap;
119     ULONG cjWidthBytes, cBitsPixel, cExpectedBitsPixel;
120     int result, TestBitsPixel, ExpectedBitsPixel;
121 
122     hbmp = CreateBitmap(0, 0, 0, 0, NULL);
123     ok(hbmp != 0, "should get a 1x1 bitmap\n");
124     ok(hbmp == GetStockObject(DEFAULT_BITMAP), "\n");
125     ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
126     ok_int(bitmap.bmType, 0);
127     ok_int(bitmap.bmWidth, 1);
128     ok_int(bitmap.bmHeight, 1);
129     ok_int(bitmap.bmWidthBytes, 2);
130     ok_int(bitmap.bmPlanes, 1);
131     ok_int(bitmap.bmBitsPixel, 1);
132     ok_ptr(bitmap.bmBits, 0);
133     DeleteObject(hbmp);
134 
135     /* Above 32 bpp should fail */
136     hbmp = CreateBitmap(1, 2, 1, 33, NULL);
137     ok(hbmp == 0, "should fail\n");
138 
139     for (TestBitsPixel = 0; TestBitsPixel <= 32; TestBitsPixel++)
140     {
141         /* CreateBitmap API accepts any number as BitsPixels param.
142            but it can only create 1, 4, 8, 16, 24, 32 bpp bitmaps */
143         if (TestBitsPixel <= 1)      ExpectedBitsPixel = 1;
144         else if(TestBitsPixel <= 4)  ExpectedBitsPixel = 4;
145         else if(TestBitsPixel <= 8)  ExpectedBitsPixel = 8;
146         else if(TestBitsPixel <= 16) ExpectedBitsPixel = 16;
147         else if(TestBitsPixel <= 24) ExpectedBitsPixel = 24;
148         else if(TestBitsPixel <= 32) ExpectedBitsPixel = 32;
149 
150         /* Calculate proper bmWidthBytes */
151 
152         hbmp = CreateBitmap(1, 2, 1, TestBitsPixel, NULL);
153         ok(hbmp != 0, "should get a 1x2 bitmap\n");
154         result = GetObject(hbmp, sizeof(bitmap), &bitmap);
155         ok(result > 0, "result = %d\n", result);
156         ok(bitmap.bmType == 0, "bmType = %ld\n", bitmap.bmType);
157         ok(bitmap.bmWidth == 1, "bmWidth = %ld\n", bitmap.bmWidth);
158         ok(bitmap.bmHeight == 2, "bmHeight = %ld\n", bitmap.bmHeight);
159         ok(bitmap.bmPlanes == 1, "bmPlanes = %d\n", bitmap.bmPlanes);
160         ok(bitmap.bmBitsPixel == ExpectedBitsPixel, "bmBitsPixel = %d ExpectedBitsPixel= %d \n", bitmap.bmBitsPixel, ExpectedBitsPixel);
161         ok(bitmap.bmBits == 0, "bmBits = %p\n", bitmap.bmBits);
162         DeleteObject(hbmp);
163     }
164 
165     hbmp = CreateBitmap(1, 2, 1, 1, NULL);
166     ok(hbmp != 0, "should get a 1x2 bitmap\n");
167     ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
168     ok_int(bitmap.bmType, 0);
169     ok_int(bitmap.bmWidth, 1);
170     ok_int(bitmap.bmHeight, 2);
171     ok_int(bitmap.bmWidthBytes, 2);
172     ok_int(bitmap.bmPlanes, 1);
173     ok_int(bitmap.bmBitsPixel, 1);
174     ok_ptr(bitmap.bmBits, 0);
175     DeleteObject(hbmp);
176 
177     for (cBitsPixel = 0; cBitsPixel <= 32; cBitsPixel++)
178     {
179         /* CreateBitmap API accepts any number as BitsPixels param.
180            but it just can create 1, 4, 8, 16, 24, 32 bpp Bitmaps */
181         if (cBitsPixel <= 1) cExpectedBitsPixel = 1;
182         else if (cBitsPixel <= 4) cExpectedBitsPixel = 4;
183         else if (cBitsPixel <= 8) cExpectedBitsPixel = 8;
184         else if (cBitsPixel <= 16) cExpectedBitsPixel = 16;
185         else if (cBitsPixel <= 24) cExpectedBitsPixel = 24;
186         else if (cBitsPixel <= 32) cExpectedBitsPixel = 32;
187 
188         hbmp = CreateBitmap(1, 2, 1, cBitsPixel, NULL);
189         ok(hbmp != 0, "should get a 1x2 bitmap %ld\n", cBitsPixel);
190         ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
191 
192         /* calculate expected line width */
193         cjWidthBytes = ((((ULONG)bitmap.bmWidth) * ((ULONG)bitmap.bmBitsPixel) + 15) & ~15) >> 3;
194 
195         ok_int(bitmap.bmType, 0);
196         ok_int(bitmap.bmWidth, 1);
197         ok_int(bitmap.bmHeight, 2);
198         ok_int(bitmap.bmPlanes, 1);
199         ok_int(bitmap.bmBitsPixel, cExpectedBitsPixel);
200         ok_int(bitmap.bmWidthBytes, cjWidthBytes);
201         ok_ptr(bitmap.bmBits, 0);
202         DeleteObject(hbmp);
203 
204     }
205 
206     hbmp = CreateBitmap(1, 2, 1, 33, NULL);
207     ok(hbmp == 0, "Expected failure for 33 bpp\n");
208 
209 
210 
211 }
212 
213 START_TEST(CreateBitmap)
214 {
215     Test_CreateBitmap_Params();
216     Test_CreateBitmap();
217 
218 }
219