d3dx9: Implemented ConvertPointRepsToAdjacency.
[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, destrect;
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         hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "testdummy.bmp", NULL, D3DX_DEFAULT, 0, NULL);
256         ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
257     } else skip("Couldn't create \"testdummy.bmp\"\n");
258
259     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
260     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
261
262     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "", NULL, D3DX_DEFAULT, 0, NULL);
263     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
264
265
266     /* D3DXLoadSurfaceFromResource */
267     todo_wine {
268         hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
269         ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
270     }
271
272     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDD_BITMAPDATA_1x1), NULL, D3DX_DEFAULT, 0, NULL);
273     ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
274
275     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
276     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
277
278     hr = D3DXLoadSurfaceFromResourceA(NULL, NULL, NULL, NULL, MAKEINTRESOURCE(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
279     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
280
281     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDS_STRING), NULL, D3DX_DEFAULT, 0, NULL);
282     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
283
284
285     /* D3DXLoadSurfaceFromFileInMemory */
286     todo_wine {
287         hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp01, sizeof(bmp01), NULL, D3DX_DEFAULT, 0, NULL);
288         ok(hr == D3D_OK, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
289     }
290
291     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, noimage, sizeof(noimage), NULL, D3DX_DEFAULT, 0, NULL);
292     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
293
294     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp01, 0, NULL, D3DX_DEFAULT, 0, NULL);
295     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
296
297     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, bmp01, sizeof(bmp01), NULL, D3DX_DEFAULT, 0, NULL);
298     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
299
300     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 8, NULL, D3DX_DEFAULT, 0, NULL);
301     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
302
303     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
304     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
305
306     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
307     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
308
309
310     /* D3DXLoadSurfaceFromMemory */
311     SetRect(&rect, 0, 0, 2, 2);
312
313     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
314     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
315
316     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, 0, NULL, &rect, D3DX_FILTER_NONE, 0);
317     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
318
319     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, NULL, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
320     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
321
322     hr = D3DXLoadSurfaceFromMemory(NULL, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
323     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
324
325     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, NULL, D3DX_DEFAULT, 0);
326     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
327
328     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_UNKNOWN, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
329     ok(hr == E_FAIL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, E_FAIL);
330
331     SetRect(&destrect, -1, -1, 1, 1); /* destination rect is partially outside texture boundaries */
332     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
333     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
334
335     SetRect(&destrect, 255, 255, 257, 257); /* destination rect is partially outside texture boundaries */
336     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
337     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
338
339     SetRect(&destrect, 1, 1, 0, 0); /* left > right, top > bottom */
340     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
341     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
342
343     SetRect(&destrect, 0, 0, 0, 0); /* left = right, top = bottom */
344     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
345     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
346
347     SetRect(&destrect, 257, 257, 257, 257); /* left = right, top = bottom, but invalid values */
348     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
349     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
350
351
352     /* D3DXLoadSurfaceFromSurface */
353     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &newsurf, NULL);
354     if(SUCCEEDED(hr)) {
355         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
356         ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3D_OK);
357
358         hr = D3DXLoadSurfaceFromSurface(NULL, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
359         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
360
361         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0);
362         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
363
364         check_release((IUnknown*)newsurf, 0);
365     } else skip("Failed to create a second surface\n");
366
367     check_release((IUnknown*)surf, 0);
368
369
370     /* test color conversion */
371     /* A8R8G8B8 */
372     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
373     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
374     else {
375         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
376         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
377         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
378         check_pixel_4bpp(lockrect, 0, 0, 0x57dbffff);
379         check_pixel_4bpp(lockrect, 1, 0, 0x98ffff00);
380         check_pixel_4bpp(lockrect, 0, 1, 0xacdbff55);
381         check_pixel_4bpp(lockrect, 1, 1, 0xc8929255);
382         IDirect3DSurface9_UnlockRect(surf);
383
384         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
385         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
386         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
387         check_pixel_4bpp(lockrect, 0, 0, 0x008cadad);
388         check_pixel_4bpp(lockrect, 1, 0, 0xff317342);
389         check_pixel_4bpp(lockrect, 0, 1, 0x0008ad10);
390         check_pixel_4bpp(lockrect, 1, 1, 0xff29088c);
391         IDirect3DSurface9_UnlockRect(surf);
392
393         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
394         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
395         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
396         check_pixel_4bpp(lockrect, 0, 0, 0xff9cdfb5);
397         check_pixel_4bpp(lockrect, 1, 0, 0xff63b26b);
398         check_pixel_4bpp(lockrect, 0, 1, 0xff085d73);
399         check_pixel_4bpp(lockrect, 1, 1, 0xff425d73);
400         IDirect3DSurface9_UnlockRect(surf);
401
402         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
403         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
404         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
405         todo_wine {
406             check_pixel_4bpp(lockrect, 0, 0, 0xff3f08ff);
407         }
408         check_pixel_4bpp(lockrect, 1, 0, 0xff44dcff);
409         check_pixel_4bpp(lockrect, 0, 1, 0xff97e4ff);
410         check_pixel_4bpp(lockrect, 1, 1, 0xfffe9aff);
411         IDirect3DSurface9_UnlockRect(surf);
412
413         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
414         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
415         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
416         check_pixel_4bpp(lockrect, 0, 0, 0xc3f04c39);
417         check_pixel_4bpp(lockrect, 1, 0, 0x2392e85a);
418         check_pixel_4bpp(lockrect, 0, 1, 0x09fd97b1);
419         check_pixel_4bpp(lockrect, 1, 1, 0x8df62bc3);
420         IDirect3DSurface9_UnlockRect(surf);
421
422         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
423         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
424         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
425         check_pixel_4bpp(lockrect, 0, 0, 0x555c95bf);
426         check_pixel_4bpp(lockrect, 1, 0, 0x556d663f);
427         check_pixel_4bpp(lockrect, 0, 1, 0xaac385ad);
428         todo_wine {
429             check_pixel_4bpp(lockrect, 1, 1, 0xfffcc575);
430         }
431         IDirect3DSurface9_UnlockRect(surf);
432
433         check_release((IUnknown*)surf, 0);
434     }
435
436     /* A1R5G5B5 */
437     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A1R5G5B5, D3DPOOL_DEFAULT, &surf, NULL);
438     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
439     else {
440         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
441         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
442         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
443         check_pixel_2bpp(lockrect, 0, 0, 0x6fff);
444         check_pixel_2bpp(lockrect, 1, 0, 0xffe0);
445         check_pixel_2bpp(lockrect, 0, 1, 0xefea);
446         check_pixel_2bpp(lockrect, 1, 1, 0xca4a);
447         IDirect3DSurface9_UnlockRect(surf);
448
449         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
450         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
451         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
452         check_pixel_2bpp(lockrect, 0, 0, 0x46b5);
453         check_pixel_2bpp(lockrect, 1, 0, 0x99c8);
454         check_pixel_2bpp(lockrect, 0, 1, 0x06a2);
455         check_pixel_2bpp(lockrect, 1, 1, 0x9431);
456         IDirect3DSurface9_UnlockRect(surf);
457
458         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
459         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
460         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
461         check_pixel_2bpp(lockrect, 0, 0, 0xcf76);
462         check_pixel_2bpp(lockrect, 1, 0, 0xb2cd);
463         check_pixel_2bpp(lockrect, 0, 1, 0x856e);
464         check_pixel_2bpp(lockrect, 1, 1, 0xa16e);
465         IDirect3DSurface9_UnlockRect(surf);
466
467         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
468         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
469         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
470         todo_wine {
471             check_pixel_2bpp(lockrect, 0, 0, 0xa03f);
472         }
473         check_pixel_2bpp(lockrect, 1, 0, 0xa37f);
474         check_pixel_2bpp(lockrect, 0, 1, 0xcb9f);
475         check_pixel_2bpp(lockrect, 1, 1, 0xfe7f);
476         IDirect3DSurface9_UnlockRect(surf);
477
478         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
479         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
480         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
481         todo_wine {
482             check_pixel_2bpp(lockrect, 0, 0, 0xf527);
483             check_pixel_2bpp(lockrect, 1, 0, 0x4b8b);
484         }
485         check_pixel_2bpp(lockrect, 0, 1, 0x7e56);
486         check_pixel_2bpp(lockrect, 1, 1, 0xf8b8);
487         IDirect3DSurface9_UnlockRect(surf);
488
489         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
490         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
491         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
492         check_pixel_2bpp(lockrect, 0, 0, 0x2e57);
493         todo_wine {
494             check_pixel_2bpp(lockrect, 1, 0, 0x3588);
495         }
496         check_pixel_2bpp(lockrect, 0, 1, 0xe215);
497         check_pixel_2bpp(lockrect, 1, 1, 0xff0e);
498         IDirect3DSurface9_UnlockRect(surf);
499
500         check_release((IUnknown*)surf, 0);
501     }
502
503     /* cleanup */
504     if(testdummy_ok) DeleteFileA("testdummy.bmp");
505     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
506 }
507
508 START_TEST(surface)
509 {
510     HWND wnd;
511     IDirect3D9 *d3d;
512     IDirect3DDevice9 *device;
513     D3DPRESENT_PARAMETERS d3dpp;
514     HRESULT hr;
515
516     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
517     if (!wnd) {
518         skip("Couldn't create application window\n");
519         return;
520     }
521     d3d = Direct3DCreate9(D3D_SDK_VERSION);
522     if (!d3d) {
523         skip("Couldn't create IDirect3D9 object\n");
524         DestroyWindow(wnd);
525         return;
526     }
527
528     ZeroMemory(&d3dpp, sizeof(d3dpp));
529     d3dpp.Windowed   = TRUE;
530     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
531     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
532     if(FAILED(hr)) {
533         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
534         IDirect3D9_Release(d3d);
535         DestroyWindow(wnd);
536         return;
537     }
538
539     test_D3DXGetImageInfo();
540     test_D3DXLoadSurface(device);
541
542     check_release((IUnknown*)device, 0);
543     check_release((IUnknown*)d3d, 0);
544     DestroyWindow(wnd);
545 }