user32: Use the stored color and mask bitmaps instead of the raw bits in GetIconInfo.
[wine] / dlls / d3dx9_36 / tests / surface.c
1 /*
2  * Tests for the D3DX9 surface functions
3  *
4  * Copyright 2009 Tony Wasserka
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define COBJMACROS
22 #include "wine/test.h"
23 #include "d3dx9tex.h"
24 #include "resources.h"
25
26 static inline int get_ref(IUnknown *obj)
27 {
28     IUnknown_AddRef(obj);
29     return IUnknown_Release(obj);
30 }
31
32 static inline void check_ref(IUnknown *obj, int exp)
33 {
34     int ref = get_ref(obj);
35     ok (exp == ref, "Invalid refcount. Expected %d, got %d\n", exp, ref);
36 }
37
38 static inline void check_release(IUnknown *obj, int exp)
39 {
40     int ref = IUnknown_Release(obj);
41     ok (ref == exp, "Invalid refcount. Expected %d, got %d\n", exp, ref);
42 }
43
44 /* 1x1 bmp (1 bpp) */
45 static const unsigned char bmp01[66] = {
46 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
47 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
48 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
49 0x00,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0x00,0x00,0x00,
50 0x00,0x00
51 };
52
53 /* 2x2 A8R8G8B8 pixel data */
54 static const unsigned char pixdata[] = {
55 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
56 };
57
58 /* invalid image file */
59 static const unsigned char noimage[4] = {
60 0x11,0x22,0x33,0x44
61 };
62
63 static HRESULT create_file(const char *filename, const unsigned char *data, const unsigned int size)
64 {
65     DWORD received;
66     HANDLE hfile;
67
68     hfile = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
69     if(hfile == INVALID_HANDLE_VALUE) return HRESULT_FROM_WIN32(GetLastError());
70
71     if(WriteFile(hfile, data, size, &received, NULL))
72     {
73         CloseHandle(hfile);
74         return D3D_OK;
75     }
76
77     CloseHandle(hfile);
78     return D3DERR_INVALIDCALL;
79 }
80
81 static void test_D3DXGetImageInfo(void)
82 {
83     HRESULT hr;
84     D3DXIMAGE_INFO info;
85     BOOL testdummy_ok, testbitmap_ok;
86
87     hr = create_file("testdummy.bmp", noimage, sizeof(noimage));  /* invalid image */
88     testdummy_ok = SUCCEEDED(hr);
89
90     hr = create_file("testbitmap.bmp", bmp01, sizeof(bmp01));  /* valid image */
91     testbitmap_ok = SUCCEEDED(hr);
92
93     /* D3DXGetImageInfoFromFile */
94     if(testbitmap_ok) {
95         hr = D3DXGetImageInfoFromFileA("testbitmap.bmp", &info);
96         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
97
98         hr = D3DXGetImageInfoFromFileA("testbitmap.bmp", NULL); /* valid image, second parameter is NULL */
99         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
100     } else skip("Couldn't create \"testbitmap.bmp\"\n");
101
102     if(testdummy_ok) {
103         hr = D3DXGetImageInfoFromFileA("testdummy.bmp", NULL); /* invalid image, second parameter is NULL */
104         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
105
106         hr = D3DXGetImageInfoFromFileA("testdummy.bmp", &info);
107         ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
108     } else skip("Couldn't create \"testdummy.bmp\"\n");
109
110     hr = D3DXGetImageInfoFromFileA("filedoesnotexist.bmp", &info);
111     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
112
113     hr = D3DXGetImageInfoFromFileA("filedoesnotexist.bmp", NULL);
114     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
115
116     hr = D3DXGetImageInfoFromFileA("", &info);
117     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
118
119     hr = D3DXGetImageInfoFromFileA(NULL, &info);
120     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
121
122     hr = D3DXGetImageInfoFromFileA(NULL, NULL);
123     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
124
125
126     /* D3DXGetImageInfoFromResource */
127     todo_wine {
128         hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), &info); /* RT_BITMAP */
129         ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
130
131         hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), NULL);
132         ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
133     }
134
135     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), &info); /* RT_RCDATA */
136     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
137
138     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDS_STRING), &info);
139     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
140
141     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDS_STRING), NULL);
142     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
143
144     hr = D3DXGetImageInfoFromResourceA(NULL, "resourcedoesnotexist", &info);
145     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
146
147     hr = D3DXGetImageInfoFromResourceA(NULL, "resourcedoesnotexist", NULL);
148     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
149
150     hr = D3DXGetImageInfoFromResourceA(NULL, NULL, NULL);
151     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
152
153
154     /* D3DXGetImageInfoFromFileInMemory */
155     hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01), &info);
156     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
157
158     hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01)+5, &info); /* too large size */
159     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
160
161     hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01), NULL);
162     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
163
164     hr = D3DXGetImageInfoFromFileInMemory(noimage, sizeof(noimage), NULL);
165     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
166
167     hr = D3DXGetImageInfoFromFileInMemory(noimage, sizeof(noimage), &info);
168     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
169
170     todo_wine {
171         hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01)-1, &info);
172         ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
173     }
174
175     hr = D3DXGetImageInfoFromFileInMemory(bmp01+1, sizeof(bmp01)-1, &info);
176     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
177
178     hr = D3DXGetImageInfoFromFileInMemory(bmp01, 0, &info);
179     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
180
181     hr = D3DXGetImageInfoFromFileInMemory(bmp01, 0, NULL);
182     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
183
184     hr = D3DXGetImageInfoFromFileInMemory(noimage, 0, &info);
185     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
186
187     hr = D3DXGetImageInfoFromFileInMemory(noimage, 0, NULL);
188     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
189
190     hr = D3DXGetImageInfoFromFileInMemory(NULL, 0, &info);
191     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
192
193     hr = D3DXGetImageInfoFromFileInMemory(NULL, 4, NULL);
194     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
195
196     hr = D3DXGetImageInfoFromFileInMemory(NULL, 4, &info);
197     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
198
199     hr = D3DXGetImageInfoFromFileInMemory(NULL, 0, NULL);
200     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
201
202     /* cleanup */
203     if(testdummy_ok) DeleteFileA("testdummy.bmp");
204     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
205 }
206
207 #define check_pixel_1bpp(lockrect, x, y, color) \
208 ok(((BYTE*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch] == color, "Got color %#x, expected %#x\n", ((BYTE*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch], color)
209
210 #define check_pixel_2bpp(lockrect, x, y, color) \
211 ok(((WORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 2] == color, "Got color %#x, expected %#x\n", ((WORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 2], color)
212
213 #define check_pixel_4bpp(lockrect, x, y, color) \
214 ok(((DWORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 4] == color, "Got color %#x, expected %#x\n", ((DWORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 4], color)
215 static void test_D3DXLoadSurface(IDirect3DDevice9 *device)
216 {
217     HRESULT hr;
218     BOOL testdummy_ok, testbitmap_ok;
219     IDirect3DSurface9 *surf, *newsurf;
220     RECT rect;
221     D3DLOCKED_RECT lockrect;
222     const WORD pixdata_a8r3g3b2[] = { 0x57df, 0x98fc, 0xacdd, 0xc891 };
223     const WORD pixdata_a1r5g5b5[] = { 0x46b5, 0x99c8, 0x06a2, 0x9431 };
224     const WORD pixdata_r5g6b5[] = { 0x9ef6, 0x658d, 0x0aee, 0x42ee };
225     const DWORD pixdata_g16r16[] = { 0x07d23fbe, 0xdc7f44a4, 0xe4d8976b, 0x9a84fe89 };
226     const DWORD pixdata_a8b8g8r8[] = { 0xc3394cf0, 0x235ae892, 0x09b197fd, 0x8dc32bf6 };
227     const DWORD pixdata_a2r10g10b10[] = { 0x57395aff, 0x5b7668fd, 0xb0d856b5, 0xff2c61d6 };
228
229     hr = create_file("testdummy.bmp", noimage, sizeof(noimage));  /* invalid image */
230     testdummy_ok = SUCCEEDED(hr);
231
232     hr = create_file("testbitmap.bmp", bmp01, sizeof(bmp01));  /* valid image */
233     testbitmap_ok = SUCCEEDED(hr);
234
235     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
236     if(FAILED(hr)) {
237         skip("Failed to create a surface (%#x)\n", hr);
238         if(testdummy_ok) DeleteFileA("testdummy.bmp");
239         if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
240         return;
241     }
242
243     /* D3DXLoadSurfaceFromFile */
244     if(testbitmap_ok) {
245         todo_wine {
246             hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "testbitmap.bmp", NULL, D3DX_DEFAULT, 0, NULL);
247             ok(hr == D3D_OK, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3D_OK);
248         }
249
250         hr = D3DXLoadSurfaceFromFileA(NULL, NULL, NULL, "testbitmap.bmp", NULL, D3DX_DEFAULT, 0, NULL);
251         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
252     } else skip("Couldn't create \"testbitmap.bmp\"\n");
253
254     if(testdummy_ok) {
255         todo_wine {
256             hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "testdummy.bmp", NULL, D3DX_DEFAULT, 0, NULL);
257             ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
258         }
259     } else skip("Couldn't create \"testdummy.bmp\"\n");
260
261     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
262     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
263
264     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "", NULL, D3DX_DEFAULT, 0, NULL);
265     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
266
267
268     /* D3DXLoadSurfaceFromResource */
269     todo_wine {
270         hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
271         ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
272
273         hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDD_BITMAPDATA_1x1), NULL, D3DX_DEFAULT, 0, NULL);
274         ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
275     }
276
277     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
278     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
279
280     hr = D3DXLoadSurfaceFromResourceA(NULL, NULL, NULL, NULL, MAKEINTRESOURCE(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
281     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
282
283     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDS_STRING), NULL, D3DX_DEFAULT, 0, NULL);
284     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
285
286
287     /* D3DXLoadSurfaceFromFileInMemory */
288     todo_wine {
289         hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp01, sizeof(bmp01), NULL, D3DX_DEFAULT, 0, NULL);
290         ok(hr == D3D_OK, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
291
292         hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, noimage, sizeof(noimage), NULL, D3DX_DEFAULT, 0, NULL);
293         ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
294     }
295
296     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp01, 0, NULL, D3DX_DEFAULT, 0, NULL);
297     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
298
299     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, bmp01, sizeof(bmp01), NULL, D3DX_DEFAULT, 0, NULL);
300     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
301
302     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 8, NULL, D3DX_DEFAULT, 0, NULL);
303     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
304
305     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
306     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
307
308     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
309     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
310
311
312     /* D3DXLoadSurfaceFromMemory */
313     SetRect(&rect, 0, 0, 2, 2);
314
315     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
316     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
317
318     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, 0, NULL, &rect, D3DX_FILTER_NONE, 0);
319     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
320
321     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, NULL, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
322     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
323
324     hr = D3DXLoadSurfaceFromMemory(NULL, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
325     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
326
327     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, NULL, D3DX_DEFAULT, 0);
328     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
329
330     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_UNKNOWN, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
331     ok(hr == E_FAIL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, E_FAIL);
332
333
334     /* D3DXLoadSurfaceFromSurface */
335     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &newsurf, NULL);
336     if(SUCCEEDED(hr)) {
337         todo_wine {
338             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
339             ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3D_OK);
340         }
341
342         hr = D3DXLoadSurfaceFromSurface(NULL, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
343         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
344
345         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0);
346         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
347
348         check_release((IUnknown*)newsurf, 0);
349     } else skip("Failed to create a second surface\n");
350
351     check_release((IUnknown*)surf, 0);
352
353
354     /* test color conversion */
355     /* A8R8G8B8 */
356     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
357     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
358     else {
359         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
360         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
361         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
362         check_pixel_4bpp(lockrect, 0, 0, 0x57dbffff);
363         check_pixel_4bpp(lockrect, 1, 0, 0x98ffff00);
364         check_pixel_4bpp(lockrect, 0, 1, 0xacdbff55);
365         check_pixel_4bpp(lockrect, 1, 1, 0xc8929255);
366         IDirect3DSurface9_UnlockRect(surf);
367
368         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
369         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
370         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
371         check_pixel_4bpp(lockrect, 0, 0, 0x008cadad);
372         check_pixel_4bpp(lockrect, 1, 0, 0xff317342);
373         check_pixel_4bpp(lockrect, 0, 1, 0x0008ad10);
374         check_pixel_4bpp(lockrect, 1, 1, 0xff29088c);
375         IDirect3DSurface9_UnlockRect(surf);
376
377         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
378         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
379         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
380         check_pixel_4bpp(lockrect, 0, 0, 0xff9cdfb5);
381         check_pixel_4bpp(lockrect, 1, 0, 0xff63b26b);
382         check_pixel_4bpp(lockrect, 0, 1, 0xff085d73);
383         check_pixel_4bpp(lockrect, 1, 1, 0xff425d73);
384         IDirect3DSurface9_UnlockRect(surf);
385
386         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
387         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
388         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
389         todo_wine {
390             check_pixel_4bpp(lockrect, 0, 0, 0xff3f08ff);
391         }
392         check_pixel_4bpp(lockrect, 1, 0, 0xff44dcff);
393         check_pixel_4bpp(lockrect, 0, 1, 0xff97e4ff);
394         check_pixel_4bpp(lockrect, 1, 1, 0xfffe9aff);
395         IDirect3DSurface9_UnlockRect(surf);
396
397         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
398         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
399         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
400         check_pixel_4bpp(lockrect, 0, 0, 0xc3f04c39);
401         check_pixel_4bpp(lockrect, 1, 0, 0x2392e85a);
402         check_pixel_4bpp(lockrect, 0, 1, 0x09fd97b1);
403         check_pixel_4bpp(lockrect, 1, 1, 0x8df62bc3);
404         IDirect3DSurface9_UnlockRect(surf);
405
406         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
407         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
408         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
409         check_pixel_4bpp(lockrect, 0, 0, 0x555c95bf);
410         check_pixel_4bpp(lockrect, 1, 0, 0x556d663f);
411         check_pixel_4bpp(lockrect, 0, 1, 0xaac385ad);
412         todo_wine {
413             check_pixel_4bpp(lockrect, 1, 1, 0xfffcc575);
414         }
415         IDirect3DSurface9_UnlockRect(surf);
416
417         check_release((IUnknown*)surf, 0);
418     }
419
420     /* A1R5G5B5 */
421     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A1R5G5B5, D3DPOOL_DEFAULT, &surf, NULL);
422     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
423     else {
424         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
425         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
426         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
427         check_pixel_2bpp(lockrect, 0, 0, 0x6fff);
428         check_pixel_2bpp(lockrect, 1, 0, 0xffe0);
429         check_pixel_2bpp(lockrect, 0, 1, 0xefea);
430         check_pixel_2bpp(lockrect, 1, 1, 0xca4a);
431         IDirect3DSurface9_UnlockRect(surf);
432
433         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
434         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
435         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
436         check_pixel_2bpp(lockrect, 0, 0, 0x46b5);
437         check_pixel_2bpp(lockrect, 1, 0, 0x99c8);
438         check_pixel_2bpp(lockrect, 0, 1, 0x06a2);
439         check_pixel_2bpp(lockrect, 1, 1, 0x9431);
440         IDirect3DSurface9_UnlockRect(surf);
441
442         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
443         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
444         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
445         check_pixel_2bpp(lockrect, 0, 0, 0xcf76);
446         check_pixel_2bpp(lockrect, 1, 0, 0xb2cd);
447         check_pixel_2bpp(lockrect, 0, 1, 0x856e);
448         check_pixel_2bpp(lockrect, 1, 1, 0xa16e);
449         IDirect3DSurface9_UnlockRect(surf);
450
451         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
452         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
453         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
454         todo_wine {
455             check_pixel_2bpp(lockrect, 0, 0, 0xa03f);
456         }
457         check_pixel_2bpp(lockrect, 1, 0, 0xa37f);
458         check_pixel_2bpp(lockrect, 0, 1, 0xcb9f);
459         check_pixel_2bpp(lockrect, 1, 1, 0xfe7f);
460         IDirect3DSurface9_UnlockRect(surf);
461
462         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
463         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
464         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
465         todo_wine {
466             check_pixel_2bpp(lockrect, 0, 0, 0xf527);
467             check_pixel_2bpp(lockrect, 1, 0, 0x4b8b);
468         }
469         check_pixel_2bpp(lockrect, 0, 1, 0x7e56);
470         check_pixel_2bpp(lockrect, 1, 1, 0xf8b8);
471         IDirect3DSurface9_UnlockRect(surf);
472
473         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
474         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
475         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
476         check_pixel_2bpp(lockrect, 0, 0, 0x2e57);
477         todo_wine {
478             check_pixel_2bpp(lockrect, 1, 0, 0x3588);
479         }
480         check_pixel_2bpp(lockrect, 0, 1, 0xe215);
481         check_pixel_2bpp(lockrect, 1, 1, 0xff0e);
482         IDirect3DSurface9_UnlockRect(surf);
483
484         check_release((IUnknown*)surf, 0);
485     }
486
487     /* cleanup */
488     if(testdummy_ok) DeleteFileA("testdummy.bmp");
489     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
490 }
491
492 START_TEST(surface)
493 {
494     HWND wnd;
495     IDirect3D9 *d3d;
496     IDirect3DDevice9 *device;
497     D3DPRESENT_PARAMETERS d3dpp;
498     HRESULT hr;
499
500     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
501     if (!wnd) {
502         skip("Couldn't create application window\n");
503         return;
504     }
505     d3d = Direct3DCreate9(D3D_SDK_VERSION);
506     if (!d3d) {
507         skip("Couldn't create IDirect3D9 object\n");
508         DestroyWindow(wnd);
509         return;
510     }
511
512     ZeroMemory(&d3dpp, sizeof(d3dpp));
513     d3dpp.Windowed   = TRUE;
514     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
515     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
516     if(FAILED(hr)) {
517         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
518         IDirect3D9_Release(d3d);
519         DestroyWindow(wnd);
520         return;
521     }
522
523     test_D3DXGetImageInfo();
524     test_D3DXLoadSurface(device);
525
526     check_release((IUnknown*)device, 0);
527     check_release((IUnknown*)d3d, 0);
528     DestroyWindow(wnd);
529 }