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 
121     hbmp = CreateBitmap(0, 0, 0, 0, NULL);
122     ok(hbmp != 0, "should get a 1x1 bitmap\n");
123     ok(hbmp == GetStockObject(DEFAULT_BITMAP), "\n");
124     ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
125     ok_int(bitmap.bmType, 0);
126     ok_int(bitmap.bmWidth, 1);
127     ok_int(bitmap.bmHeight, 1);
128     ok_int(bitmap.bmWidthBytes, 2);
129     ok_int(bitmap.bmPlanes, 1);
130     ok_int(bitmap.bmBitsPixel, 1);
131     ok_ptr(bitmap.bmBits, 0);
132     DeleteObject(hbmp);
133 
134     /* Above 32 bpp should fail */
135     hbmp = CreateBitmap(1, 2, 1, 33, NULL);
136     ok(hbmp == 0, "should fail\n");
137 
138     hbmp = CreateBitmap(1, 2, 1, 1, NULL);
139     ok(hbmp != 0, "should get a 1x2 bitmap\n");
140     ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
141     ok_int(bitmap.bmType, 0);
142     ok_int(bitmap.bmWidth, 1);
143     ok_int(bitmap.bmHeight, 2);
144     ok_int(bitmap.bmWidthBytes, 2);
145     ok_int(bitmap.bmPlanes, 1);
146     ok_int(bitmap.bmBitsPixel, 1);
147     ok_ptr(bitmap.bmBits, 0);
148     DeleteObject(hbmp);
149 
150     for (cBitsPixel = 0; cBitsPixel <= 32; cBitsPixel++)
151     {
152         /* CreateBitmap API accepts any number as BitsPixels param.
153            but it can only create 1, 4, 8, 16, 24, 32 bpp bitmaps */
154         if (cBitsPixel <= 1)       cExpectedBitsPixel = 1;
155         else if (cBitsPixel <= 4)  cExpectedBitsPixel = 4;
156         else if (cBitsPixel <= 8)  cExpectedBitsPixel = 8;
157         else if (cBitsPixel <= 16) cExpectedBitsPixel = 16;
158         else if (cBitsPixel <= 24) cExpectedBitsPixel = 24;
159         else if (cBitsPixel <= 32) cExpectedBitsPixel = 32;
160 
161         hbmp = CreateBitmap(1, 2, 1, cBitsPixel, NULL);
162         ok(hbmp != 0, "should get a 1x2 bitmap %ld\n", cBitsPixel);
163         ok_int(GetObject(hbmp, sizeof(bitmap), &bitmap), sizeof(BITMAP));
164 
165         /* Calculate expected line width */
166         cjWidthBytes = ((bitmap.bmWidth * bitmap.bmBitsPixel + 15) & ~15) >> 3;
167 
168         ok_int(bitmap.bmType, 0);
169         ok_int(bitmap.bmWidth, 1);
170         ok_int(bitmap.bmHeight, 2);
171         ok_int(bitmap.bmPlanes, 1);
172         ok_int(bitmap.bmBitsPixel, cExpectedBitsPixel);
173         ok_int(bitmap.bmWidthBytes, cjWidthBytes);
174         ok_ptr(bitmap.bmBits, 0);
175         DeleteObject(hbmp);
176     }
177 
178     hbmp = CreateBitmap(1, 2, 1, 33, NULL);
179     ok(hbmp == 0, "Expected failure for 33 bpp\n");
180 }
181 
182 START_TEST(CreateBitmap)
183 {
184     Test_CreateBitmap_Params();
185     Test_CreateBitmap();
186 
187 }
188