d3dx9/tests: Add a test for D3DXLoadSurfaceFromSurface.
[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 /* 2x2 24-bit dds, 2 mipmaps */
64 static const unsigned char dds_24bit[] = {
65 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x0a,0x00,0x02,0x00,0x00,0x00,
66 0x02,0x00,0x00,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
67 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
68 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
69 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
70 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0xff,0x00,
71 0x00,0xff,0x00,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x40,0x00,
72 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
73 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
74 };
75
76 /* 2x2 16-bit dds, no mipmaps */
77 static const unsigned char dds_16bit[] = {
78 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x08,0x00,0x02,0x00,0x00,0x00,
79 0x02,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
80 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
81 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
82 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
83 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x7c,0x00,0x00,
84 0xe0,0x03,0x00,0x00,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,
85 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
86 0xff,0x7f,0xff,0x7f,0xff,0x7f,0xff,0x7f
87 };
88
89 /* 4x4 cube map dds */
90 static const unsigned char dds_cube_map[] = {
91 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x08,0x00,0x04,0x00,0x00,0x00,
92 0x04,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
93 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
94 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
95 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
96 0x04,0x00,0x00,0x00,0x44,0x58,0x54,0x35,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
97 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x00,0x00,
98 0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
99 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
100 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
101 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
102 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
103 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
104 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50
105 };
106
107 /* 4x4x2 volume map dds, 2 mipmaps */
108 static const unsigned char dds_volume_map[] = {
109 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x8a,0x00,0x04,0x00,0x00,0x00,
110 0x04,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
111 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
112 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
113 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
114 0x04,0x00,0x00,0x00,0x44,0x58,0x54,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
115 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x40,0x00,
116 0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
117 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
118 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
119 0xff,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x2f,0x7e,0xcf,0x79,0x01,0x54,0x5c,0x5c,
120 0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x84,0xef,0x7b,0xaa,0xab,0xab,0xab
121 };
122
123 static HRESULT create_file(const char *filename, const unsigned char *data, const unsigned int size)
124 {
125     DWORD received;
126     HANDLE hfile;
127
128     hfile = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
129     if(hfile == INVALID_HANDLE_VALUE) return HRESULT_FROM_WIN32(GetLastError());
130
131     if(WriteFile(hfile, data, size, &received, NULL))
132     {
133         CloseHandle(hfile);
134         return D3D_OK;
135     }
136
137     CloseHandle(hfile);
138     return D3DERR_INVALIDCALL;
139 }
140
141 #define DDS_CAPS 0x00000001
142 #define DDS_HEIGHT 0x00000002
143 #define DDS_WIDTH 0x00000004
144 #define DDS_PIXELFORMAT 0x00001000
145
146 #define DDS_CAPS_TEXTURE 0x00001000
147
148 #define DDS_PF_ALPHA 0x00000001
149 #define DDS_PF_ALPHA_ONLY 0x00000002
150 #define DDS_PF_FOURCC 0x00000004
151 #define DDS_PF_RGB 0x00000040
152 #define DDS_PF_LUMINANCE 0x00020000
153 #define DDS_PF_BUMPDUDV 0x00080000
154
155 static void check_dds_pixel_format(DWORD flags, DWORD fourcc, DWORD bpp,
156                                    DWORD rmask, DWORD gmask, DWORD bmask, DWORD amask,
157                                    D3DFORMAT expected_format)
158 {
159     HRESULT hr;
160     D3DXIMAGE_INFO info;
161     struct
162     {
163         DWORD magic;
164         DWORD size;
165         DWORD flags;
166         DWORD height;
167         DWORD width;
168         DWORD padding[14];
169         struct
170         {
171             DWORD size;
172             DWORD flags;
173             DWORD fourcc;
174             DWORD bpp;
175             DWORD rmask;
176             DWORD gmask;
177             DWORD bmask;
178             DWORD amask;
179         } pixel_format;
180         DWORD caps;
181         DWORD padding2[4];
182         BYTE data[256];
183     } dds;
184
185     memset(&dds, 0, sizeof(dds));
186
187     dds.magic = MAKEFOURCC('D','D','S',' ');
188     dds.size = 124;
189     dds.flags = DDS_CAPS | DDS_WIDTH | DDS_HEIGHT | DDS_PIXELFORMAT;
190     dds.height = 4;
191     dds.width = 4;
192     dds.pixel_format.size = sizeof(dds.pixel_format);
193     dds.pixel_format.flags = flags;
194     dds.pixel_format.fourcc = fourcc;
195     dds.pixel_format.bpp = bpp;
196     dds.pixel_format.rmask = rmask;
197     dds.pixel_format.gmask = gmask;
198     dds.pixel_format.bmask = bmask;
199     dds.pixel_format.amask = amask;
200     dds.caps = DDS_CAPS_TEXTURE;
201
202     hr = D3DXGetImageInfoFromFileInMemory(&dds, sizeof(dds), &info);
203     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x for pixel format %#x, expected %#x\n", hr, expected_format, D3D_OK);
204     if (SUCCEEDED(hr))
205         ok(info.Format == expected_format, "D3DXGetImageInfoFromFileInMemory returned format %#x, expected %#x\n", info.Format, expected_format);
206 }
207
208 static void test_D3DXGetImageInfo(void)
209 {
210     HRESULT hr;
211     D3DXIMAGE_INFO info;
212     BOOL testdummy_ok, testbitmap_ok;
213
214     hr = create_file("testdummy.bmp", noimage, sizeof(noimage));  /* invalid image */
215     testdummy_ok = SUCCEEDED(hr);
216
217     hr = create_file("testbitmap.bmp", bmp01, sizeof(bmp01));  /* valid image */
218     testbitmap_ok = SUCCEEDED(hr);
219
220     /* D3DXGetImageInfoFromFile */
221     if(testbitmap_ok) {
222         hr = D3DXGetImageInfoFromFileA("testbitmap.bmp", &info);
223         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
224
225         hr = D3DXGetImageInfoFromFileA("testbitmap.bmp", NULL); /* valid image, second parameter is NULL */
226         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
227     } else skip("Couldn't create \"testbitmap.bmp\"\n");
228
229     if(testdummy_ok) {
230         hr = D3DXGetImageInfoFromFileA("testdummy.bmp", NULL); /* invalid image, second parameter is NULL */
231         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
232
233         hr = D3DXGetImageInfoFromFileA("testdummy.bmp", &info);
234         ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
235     } else skip("Couldn't create \"testdummy.bmp\"\n");
236
237     hr = D3DXGetImageInfoFromFileA("filedoesnotexist.bmp", &info);
238     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
239
240     hr = D3DXGetImageInfoFromFileA("filedoesnotexist.bmp", NULL);
241     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
242
243     hr = D3DXGetImageInfoFromFileA("", &info);
244     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
245
246     hr = D3DXGetImageInfoFromFileA(NULL, &info);
247     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
248
249     hr = D3DXGetImageInfoFromFileA(NULL, NULL);
250     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
251
252
253     /* D3DXGetImageInfoFromResource */
254     todo_wine {
255         hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), &info); /* RT_BITMAP */
256         ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
257
258         hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), NULL);
259         ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
260     }
261
262     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), &info); /* RT_RCDATA */
263     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
264
265     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDS_STRING), &info);
266     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
267
268     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDS_STRING), NULL);
269     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
270
271     hr = D3DXGetImageInfoFromResourceA(NULL, "resourcedoesnotexist", &info);
272     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
273
274     hr = D3DXGetImageInfoFromResourceA(NULL, "resourcedoesnotexist", NULL);
275     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
276
277     hr = D3DXGetImageInfoFromResourceA(NULL, NULL, NULL);
278     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
279
280
281     /* D3DXGetImageInfoFromFileInMemory */
282     hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01), &info);
283     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
284
285     hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01)+5, &info); /* too large size */
286     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
287
288     hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01), NULL);
289     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
290
291     hr = D3DXGetImageInfoFromFileInMemory(noimage, sizeof(noimage), NULL);
292     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
293
294     hr = D3DXGetImageInfoFromFileInMemory(noimage, sizeof(noimage), &info);
295     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
296
297     todo_wine {
298         hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01)-1, &info);
299         ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
300     }
301
302     hr = D3DXGetImageInfoFromFileInMemory(bmp01+1, sizeof(bmp01)-1, &info);
303     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
304
305     hr = D3DXGetImageInfoFromFileInMemory(bmp01, 0, &info);
306     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
307
308     hr = D3DXGetImageInfoFromFileInMemory(bmp01, 0, NULL);
309     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
310
311     hr = D3DXGetImageInfoFromFileInMemory(noimage, 0, &info);
312     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
313
314     hr = D3DXGetImageInfoFromFileInMemory(noimage, 0, NULL);
315     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
316
317     hr = D3DXGetImageInfoFromFileInMemory(NULL, 0, &info);
318     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
319
320     hr = D3DXGetImageInfoFromFileInMemory(NULL, 4, NULL);
321     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
322
323     hr = D3DXGetImageInfoFromFileInMemory(NULL, 4, &info);
324     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
325
326     hr = D3DXGetImageInfoFromFileInMemory(NULL, 0, NULL);
327     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
328
329
330     /* test DDS support */
331     hr = D3DXGetImageInfoFromFileInMemory(dds_24bit, sizeof(dds_24bit), &info);
332     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
333     if (hr == D3D_OK) {
334         ok(info.Width == 2, "Got width %u, expected 2\n", info.Width);
335         ok(info.Height == 2, "Got height %u, expected 2\n", info.Height);
336         ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
337         ok(info.MipLevels == 2, "Got miplevels %u, expected 2\n", info.MipLevels);
338         ok(info.Format == D3DFMT_R8G8B8, "Got format %#x, expected %#x\n", info.Format, D3DFMT_R8G8B8);
339         ok(info.ResourceType == D3DRTYPE_TEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_TEXTURE);
340         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
341     } else skip("Couldn't get image info from 24-bit DDS file in memory\n");
342
343     hr = D3DXGetImageInfoFromFileInMemory(dds_16bit, sizeof(dds_16bit), &info);
344     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
345     if (hr == D3D_OK) {
346         ok(info.Width == 2, "Got width %u, expected 2\n", info.Width);
347         ok(info.Height == 2, "Got height %u, expected 2\n", info.Height);
348         ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
349         ok(info.MipLevels == 1, "Got miplevels %u, expected 1\n", info.MipLevels);
350         ok(info.Format == D3DFMT_X1R5G5B5, "Got format %#x, expected %#x\n", info.Format, D3DFMT_X1R5G5B5);
351         ok(info.ResourceType == D3DRTYPE_TEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_TEXTURE);
352         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
353     } else skip("Couldn't get image info from 16-bit DDS file in memory\n");
354
355     hr = D3DXGetImageInfoFromFileInMemory(dds_cube_map, sizeof(dds_cube_map), &info);
356     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
357     if (hr == D3D_OK) {
358         ok(info.Width == 4, "Got width %u, expected 4\n", info.Width);
359         ok(info.Height == 4, "Got height %u, expected 4\n", info.Height);
360         ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
361         ok(info.MipLevels == 1, "Got miplevels %u, expected 1\n", info.MipLevels);
362         ok(info.Format == D3DFMT_DXT5, "Got format %#x, expected %#x\n", info.Format, D3DFMT_DXT5);
363         ok(info.ResourceType == D3DRTYPE_CUBETEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_CUBETEXTURE);
364         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
365     } else skip("Couldn't get image info from cube map in memory\n");
366
367     hr = D3DXGetImageInfoFromFileInMemory(dds_volume_map, sizeof(dds_volume_map), &info);
368     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
369     if (hr == D3D_OK) {
370         ok(info.Width == 4, "Got width %u, expected 4\n", info.Width);
371         ok(info.Height == 4, "Got height %u, expected 4\n", info.Height);
372         ok(info.Depth == 2, "Got depth %u, expected 2\n", info.Depth);
373         ok(info.MipLevels == 3, "Got miplevels %u, expected 3\n", info.MipLevels);
374         ok(info.Format == D3DFMT_DXT3, "Got format %#x, expected %#x\n", info.Format, D3DFMT_DXT3);
375         ok(info.ResourceType == D3DRTYPE_VOLUMETEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_VOLUMETEXTURE);
376         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
377     } else skip("Couldn't get image info from volume map in memory\n");
378
379     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','1'), 0, 0, 0, 0, 0, D3DFMT_DXT1);
380     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','2'), 0, 0, 0, 0, 0, D3DFMT_DXT2);
381     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','3'), 0, 0, 0, 0, 0, D3DFMT_DXT3);
382     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','4'), 0, 0, 0, 0, 0, D3DFMT_DXT4);
383     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','5'), 0, 0, 0, 0, 0, D3DFMT_DXT5);
384     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('R','G','B','G'), 0, 0, 0, 0, 0, D3DFMT_R8G8_B8G8);
385     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('G','R','G','B'), 0, 0, 0, 0, 0, D3DFMT_G8R8_G8B8);
386     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('U','Y','V','Y'), 0, 0, 0, 0, 0, D3DFMT_UYVY);
387     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('Y','U','Y','2'), 0, 0, 0, 0, 0, D3DFMT_YUY2);
388     check_dds_pixel_format(DDS_PF_RGB, 0, 16, 0xf800, 0x07e0, 0x001f, 0, D3DFMT_R5G6B5);
389     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5);
390     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4);
391     check_dds_pixel_format(DDS_PF_RGB, 0, 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2);
392     check_dds_pixel_format(DDS_PF_ALPHA_ONLY, 0, 8, 0, 0, 0, 0xff, D3DFMT_A8);
393     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2);
394     check_dds_pixel_format(DDS_PF_RGB, 0, 16, 0xf00, 0x0f0, 0x00f, 0, D3DFMT_X4R4G4B4);
395     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10);
396     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10);
397     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8);
398     check_dds_pixel_format(DDS_PF_RGB, 0, 32, 0xff0000, 0x00ff00, 0x0000ff, 0, D3DFMT_X8R8G8B8);
399     check_dds_pixel_format(DDS_PF_RGB, 0, 32, 0x0000ffff, 0xffff0000, 0, 0, D3DFMT_G16R16);
400     check_dds_pixel_format(DDS_PF_LUMINANCE, 0, 8, 0xff, 0, 0, 0, D3DFMT_L8);
401     check_dds_pixel_format(DDS_PF_LUMINANCE, 0, 16, 0xffff, 0, 0, 0, D3DFMT_L16);
402     check_dds_pixel_format(DDS_PF_LUMINANCE | DDS_PF_ALPHA, 0, 16, 0x00ff, 0, 0, 0xff00, D3DFMT_A8L8);
403     check_dds_pixel_format(DDS_PF_LUMINANCE | DDS_PF_ALPHA, 0, 8, 0x0f, 0, 0, 0xf0, D3DFMT_A4L4);
404     check_dds_pixel_format(DDS_PF_BUMPDUDV, 0, 16, 0x00ff, 0xff00, 0, 0, D3DFMT_V8U8);
405     check_dds_pixel_format(DDS_PF_BUMPDUDV, 0, 32, 0x0000ffff, 0xffff0000, 0, 0, D3DFMT_V16U16);
406
407     hr = D3DXGetImageInfoFromFileInMemory(dds_16bit, sizeof(dds_16bit) - 1, &info);
408     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
409
410     hr = D3DXGetImageInfoFromFileInMemory(dds_24bit, sizeof(dds_24bit) - 1, &info);
411     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
412
413     hr = D3DXGetImageInfoFromFileInMemory(dds_cube_map, sizeof(dds_cube_map) - 1, &info);
414     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
415
416     hr = D3DXGetImageInfoFromFileInMemory(dds_volume_map, sizeof(dds_volume_map) - 1, &info);
417     todo_wine ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
418
419
420     /* cleanup */
421     if(testdummy_ok) DeleteFileA("testdummy.bmp");
422     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
423 }
424
425 #define check_pixel_1bpp(lockrect, x, y, color) \
426 ok(((BYTE*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch] == color, "Got color 0x%02x, expected 0x%02x.\n", \
427 ((BYTE*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch], color)
428
429 #define check_pixel_2bpp(lockrect, x, y, color) \
430 ok(((WORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 2] == color, "Got color 0x%04x, expected 0x%04x.\n", \
431 ((WORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 2], color)
432
433 #define check_pixel_4bpp(lockrect, x, y, color) \
434 ok(((DWORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 4] == color, "Got color 0x%08x, expected 0x%08x.\n", \
435 ((DWORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 4], color)
436 static void test_D3DXLoadSurface(IDirect3DDevice9 *device)
437 {
438     HRESULT hr;
439     BOOL testdummy_ok, testbitmap_ok;
440     IDirect3DTexture9 *tex;
441     IDirect3DSurface9 *surf, *newsurf;
442     RECT rect, destrect;
443     D3DLOCKED_RECT lockrect;
444     const WORD pixdata_a8r3g3b2[] = { 0x57df, 0x98fc, 0xacdd, 0xc891 };
445     const WORD pixdata_a1r5g5b5[] = { 0x46b5, 0x99c8, 0x06a2, 0x9431 };
446     const WORD pixdata_r5g6b5[] = { 0x9ef6, 0x658d, 0x0aee, 0x42ee };
447     const WORD pixdata_a8l8[] = { 0xff00, 0x00ff, 0xff30, 0x7f7f };
448     const DWORD pixdata_g16r16[] = { 0x07d23fbe, 0xdc7f44a4, 0xe4d8976b, 0x9a84fe89 };
449     const DWORD pixdata_a8b8g8r8[] = { 0xc3394cf0, 0x235ae892, 0x09b197fd, 0x8dc32bf6 };
450     const DWORD pixdata_a2r10g10b10[] = { 0x57395aff, 0x5b7668fd, 0xb0d856b5, 0xff2c61d6 };
451
452     hr = create_file("testdummy.bmp", noimage, sizeof(noimage));  /* invalid image */
453     testdummy_ok = SUCCEEDED(hr);
454
455     hr = create_file("testbitmap.bmp", bmp01, sizeof(bmp01));  /* valid image */
456     testbitmap_ok = SUCCEEDED(hr);
457
458     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
459     if(FAILED(hr)) {
460         skip("Failed to create a surface (%#x)\n", hr);
461         if(testdummy_ok) DeleteFileA("testdummy.bmp");
462         if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
463         return;
464     }
465
466     /* D3DXLoadSurfaceFromFile */
467     if(testbitmap_ok) {
468         todo_wine {
469             hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "testbitmap.bmp", NULL, D3DX_DEFAULT, 0, NULL);
470             ok(hr == D3D_OK, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3D_OK);
471         }
472
473         hr = D3DXLoadSurfaceFromFileA(NULL, NULL, NULL, "testbitmap.bmp", NULL, D3DX_DEFAULT, 0, NULL);
474         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
475     } else skip("Couldn't create \"testbitmap.bmp\"\n");
476
477     if(testdummy_ok) {
478         hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "testdummy.bmp", NULL, D3DX_DEFAULT, 0, NULL);
479         ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
480     } else skip("Couldn't create \"testdummy.bmp\"\n");
481
482     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
483     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
484
485     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "", NULL, D3DX_DEFAULT, 0, NULL);
486     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
487
488
489     /* D3DXLoadSurfaceFromResource */
490     todo_wine {
491         hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
492         ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
493     }
494
495     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDD_BITMAPDATA_1x1), NULL, D3DX_DEFAULT, 0, NULL);
496     ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
497
498     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
499     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
500
501     hr = D3DXLoadSurfaceFromResourceA(NULL, NULL, NULL, NULL, MAKEINTRESOURCE(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
502     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
503
504     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDS_STRING), NULL, D3DX_DEFAULT, 0, NULL);
505     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
506
507
508     /* D3DXLoadSurfaceFromFileInMemory */
509     todo_wine {
510         hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp01, sizeof(bmp01), NULL, D3DX_DEFAULT, 0, NULL);
511         ok(hr == D3D_OK, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
512     }
513
514     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, noimage, sizeof(noimage), NULL, D3DX_DEFAULT, 0, NULL);
515     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
516
517     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp01, 0, NULL, D3DX_DEFAULT, 0, NULL);
518     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
519
520     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, bmp01, sizeof(bmp01), NULL, D3DX_DEFAULT, 0, NULL);
521     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
522
523     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 8, NULL, D3DX_DEFAULT, 0, NULL);
524     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
525
526     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
527     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
528
529     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
530     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
531
532
533     /* D3DXLoadSurfaceFromMemory */
534     SetRect(&rect, 0, 0, 2, 2);
535
536     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
537     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
538
539     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, 0, NULL, &rect, D3DX_FILTER_NONE, 0);
540     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
541
542     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, NULL, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
543     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
544
545     hr = D3DXLoadSurfaceFromMemory(NULL, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
546     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
547
548     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, NULL, D3DX_DEFAULT, 0);
549     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
550
551     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_UNKNOWN, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
552     ok(hr == E_FAIL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, E_FAIL);
553
554     SetRect(&destrect, -1, -1, 1, 1); /* destination rect is partially outside texture boundaries */
555     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
556     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
557
558     SetRect(&destrect, 255, 255, 257, 257); /* destination rect is partially outside texture boundaries */
559     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
560     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
561
562     SetRect(&destrect, 1, 1, 0, 0); /* left > right, top > bottom */
563     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
564     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
565
566     SetRect(&destrect, 0, 0, 0, 0); /* left = right, top = bottom */
567     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
568     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
569
570     SetRect(&destrect, 257, 257, 257, 257); /* left = right, top = bottom, but invalid values */
571     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
572     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
573
574
575     /* D3DXLoadSurfaceFromSurface */
576     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &newsurf, NULL);
577     if(SUCCEEDED(hr)) {
578         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
579         ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3D_OK);
580
581         hr = D3DXLoadSurfaceFromSurface(NULL, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
582         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
583
584         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0);
585         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
586
587         check_release((IUnknown*)newsurf, 0);
588     } else skip("Failed to create a second surface\n");
589
590     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex, NULL);
591     if (SUCCEEDED(hr))
592     {
593         IDirect3DTexture9_GetSurfaceLevel(tex, 0, &newsurf);
594
595         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
596         ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3D_OK);
597
598         IDirect3DSurface9_Release(newsurf);
599         IDirect3DTexture9_Release(tex);
600     } else skip("Failed to create texture\n");
601
602     check_release((IUnknown*)surf, 0);
603
604
605     /* test color conversion */
606     /* A8R8G8B8 */
607     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
608     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
609     else {
610         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
611         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
612         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
613         check_pixel_4bpp(lockrect, 0, 0, 0x57dbffff);
614         check_pixel_4bpp(lockrect, 1, 0, 0x98ffff00);
615         check_pixel_4bpp(lockrect, 0, 1, 0xacdbff55);
616         check_pixel_4bpp(lockrect, 1, 1, 0xc8929255);
617         IDirect3DSurface9_UnlockRect(surf);
618
619         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
620         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
621         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
622         check_pixel_4bpp(lockrect, 0, 0, 0x008cadad);
623         check_pixel_4bpp(lockrect, 1, 0, 0xff317342);
624         check_pixel_4bpp(lockrect, 0, 1, 0x0008ad10);
625         check_pixel_4bpp(lockrect, 1, 1, 0xff29088c);
626         IDirect3DSurface9_UnlockRect(surf);
627
628         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
629         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
630         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
631         check_pixel_4bpp(lockrect, 0, 0, 0xff9cdfb5);
632         check_pixel_4bpp(lockrect, 1, 0, 0xff63b26b);
633         check_pixel_4bpp(lockrect, 0, 1, 0xff085d73);
634         check_pixel_4bpp(lockrect, 1, 1, 0xff425d73);
635         IDirect3DSurface9_UnlockRect(surf);
636
637         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
638         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
639         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
640         todo_wine {
641             check_pixel_4bpp(lockrect, 0, 0, 0xff3f08ff);
642         }
643         check_pixel_4bpp(lockrect, 1, 0, 0xff44dcff);
644         check_pixel_4bpp(lockrect, 0, 1, 0xff97e4ff);
645         check_pixel_4bpp(lockrect, 1, 1, 0xfffe9aff);
646         IDirect3DSurface9_UnlockRect(surf);
647
648         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
649         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
650         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
651         check_pixel_4bpp(lockrect, 0, 0, 0xc3f04c39);
652         check_pixel_4bpp(lockrect, 1, 0, 0x2392e85a);
653         check_pixel_4bpp(lockrect, 0, 1, 0x09fd97b1);
654         check_pixel_4bpp(lockrect, 1, 1, 0x8df62bc3);
655         IDirect3DSurface9_UnlockRect(surf);
656
657         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
658         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
659         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
660         check_pixel_4bpp(lockrect, 0, 0, 0x555c95bf);
661         check_pixel_4bpp(lockrect, 1, 0, 0x556d663f);
662         check_pixel_4bpp(lockrect, 0, 1, 0xaac385ad);
663         todo_wine {
664             check_pixel_4bpp(lockrect, 1, 1, 0xfffcc575);
665         }
666         IDirect3DSurface9_UnlockRect(surf);
667
668         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
669                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
670         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
671         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
672         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
673         check_pixel_4bpp(lockrect, 0, 0, 0xff000000);
674         check_pixel_4bpp(lockrect, 1, 0, 0x00ffffff);
675         check_pixel_4bpp(lockrect, 0, 1, 0xff303030);
676         check_pixel_4bpp(lockrect, 1, 1, 0x7f7f7f7f);
677         hr = IDirect3DSurface9_UnlockRect(surf);
678         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
679
680         check_release((IUnknown*)surf, 0);
681     }
682
683     /* A1R5G5B5 */
684     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A1R5G5B5, D3DPOOL_DEFAULT, &surf, NULL);
685     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
686     else {
687         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
688         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
689         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
690         check_pixel_2bpp(lockrect, 0, 0, 0x6fff);
691         check_pixel_2bpp(lockrect, 1, 0, 0xffe0);
692         check_pixel_2bpp(lockrect, 0, 1, 0xefea);
693         check_pixel_2bpp(lockrect, 1, 1, 0xca4a);
694         IDirect3DSurface9_UnlockRect(surf);
695
696         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
697         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
698         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
699         check_pixel_2bpp(lockrect, 0, 0, 0x46b5);
700         check_pixel_2bpp(lockrect, 1, 0, 0x99c8);
701         check_pixel_2bpp(lockrect, 0, 1, 0x06a2);
702         check_pixel_2bpp(lockrect, 1, 1, 0x9431);
703         IDirect3DSurface9_UnlockRect(surf);
704
705         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
706         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
707         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
708         check_pixel_2bpp(lockrect, 0, 0, 0xcf76);
709         check_pixel_2bpp(lockrect, 1, 0, 0xb2cd);
710         check_pixel_2bpp(lockrect, 0, 1, 0x856e);
711         check_pixel_2bpp(lockrect, 1, 1, 0xa16e);
712         IDirect3DSurface9_UnlockRect(surf);
713
714         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
715         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
716         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
717         todo_wine {
718             check_pixel_2bpp(lockrect, 0, 0, 0xa03f);
719         }
720         check_pixel_2bpp(lockrect, 1, 0, 0xa37f);
721         check_pixel_2bpp(lockrect, 0, 1, 0xcb9f);
722         check_pixel_2bpp(lockrect, 1, 1, 0xfe7f);
723         IDirect3DSurface9_UnlockRect(surf);
724
725         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
726         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
727         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
728         todo_wine {
729             check_pixel_2bpp(lockrect, 0, 0, 0xf527);
730             check_pixel_2bpp(lockrect, 1, 0, 0x4b8b);
731         }
732         check_pixel_2bpp(lockrect, 0, 1, 0x7e56);
733         check_pixel_2bpp(lockrect, 1, 1, 0xf8b8);
734         IDirect3DSurface9_UnlockRect(surf);
735
736         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
737         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
738         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
739         check_pixel_2bpp(lockrect, 0, 0, 0x2e57);
740         todo_wine {
741             check_pixel_2bpp(lockrect, 1, 0, 0x3588);
742         }
743         check_pixel_2bpp(lockrect, 0, 1, 0xe215);
744         check_pixel_2bpp(lockrect, 1, 1, 0xff0e);
745         IDirect3DSurface9_UnlockRect(surf);
746
747         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
748                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
749         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
750         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
751         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
752         check_pixel_2bpp(lockrect, 0, 0, 0x8000);
753         check_pixel_2bpp(lockrect, 1, 0, 0x7fff);
754         check_pixel_2bpp(lockrect, 0, 1, 0x98c6);
755         check_pixel_2bpp(lockrect, 1, 1, 0x3def);
756         hr = IDirect3DSurface9_UnlockRect(surf);
757         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
758
759         check_release((IUnknown*)surf, 0);
760     }
761
762     /* A8L8 */
763     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A8L8, D3DPOOL_DEFAULT, &surf, NULL);
764     if (FAILED(hr))
765         skip("Failed to create A8L8 surface, hr %#x.\n", hr);
766     else
767     {
768         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2,
769                 D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
770         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
771         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
772         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
773         check_pixel_2bpp(lockrect, 0, 0, 0x57f7);
774         check_pixel_2bpp(lockrect, 1, 0, 0x98ed);
775         check_pixel_2bpp(lockrect, 0, 1, 0xaceb);
776         check_pixel_2bpp(lockrect, 1, 1, 0xc88d);
777         hr = IDirect3DSurface9_UnlockRect(surf);
778         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
779
780         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5,
781                 D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
782         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
783         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
784         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
785         check_pixel_2bpp(lockrect, 0, 0, 0x00a6);
786         check_pixel_2bpp(lockrect, 1, 0, 0xff62);
787         check_pixel_2bpp(lockrect, 0, 1, 0x007f);
788         check_pixel_2bpp(lockrect, 1, 1, 0xff19);
789         hr = IDirect3DSurface9_UnlockRect(surf);
790         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
791
792         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5,
793                 D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
794         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
795         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
796         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
797         check_pixel_2bpp(lockrect, 0, 0, 0xffce);
798         check_pixel_2bpp(lockrect, 1, 0, 0xff9c);
799         check_pixel_2bpp(lockrect, 0, 1, 0xff4d);
800         check_pixel_2bpp(lockrect, 1, 1, 0xff59);
801         hr = IDirect3DSurface9_UnlockRect(surf);
802         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
803
804         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16,
805                 D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
806         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
807         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
808         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
809         check_pixel_2bpp(lockrect, 0, 0, 0xff25);
810         check_pixel_2bpp(lockrect, 1, 0, 0xffbe);
811         check_pixel_2bpp(lockrect, 0, 1, 0xffd6);
812         check_pixel_2bpp(lockrect, 1, 1, 0xffb6);
813         hr = IDirect3DSurface9_UnlockRect(surf);
814         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
815
816         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8,
817                 D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
818         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
819         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
820         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
821         check_pixel_2bpp(lockrect, 0, 0, 0xc36d);
822         check_pixel_2bpp(lockrect, 1, 0, 0x23cb);
823         check_pixel_2bpp(lockrect, 0, 1, 0x09af);
824         check_pixel_2bpp(lockrect, 1, 1, 0x8d61);
825         hr = IDirect3DSurface9_UnlockRect(surf);
826         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
827
828         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10,
829                 D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
830         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
831         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
832         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
833         check_pixel_2bpp(lockrect, 0, 0, 0x558c);
834         check_pixel_2bpp(lockrect, 1, 0, 0x5565);
835         check_pixel_2bpp(lockrect, 0, 1, 0xaa95);
836         check_pixel_2bpp(lockrect, 1, 1, 0xffcb);
837         hr = IDirect3DSurface9_UnlockRect(surf);
838         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
839
840         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
841                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
842         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
843         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
844         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
845         check_pixel_2bpp(lockrect, 0, 0, 0xff00);
846         check_pixel_2bpp(lockrect, 1, 0, 0x00ff);
847         check_pixel_2bpp(lockrect, 0, 1, 0xff30);
848         check_pixel_2bpp(lockrect, 1, 1, 0x7f7f);
849         hr = IDirect3DSurface9_UnlockRect(surf);
850         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
851
852         check_release((IUnknown*)surf, 0);
853     }
854
855     /* DXT1, DXT2, DXT3, DXT4, DXT5 */
856     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_R8G8B8, D3DPOOL_SYSTEMMEM, &surf, NULL);
857     if (FAILED(hr))
858         skip("Failed to create R8G8B8 surface, hr %#x.\n", hr);
859     else
860     {
861         hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, dds_24bit, sizeof(dds_24bit), NULL, D3DX_FILTER_NONE, 0, NULL);
862         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
863
864         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_DXT2, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
865         if (FAILED(hr))
866             skip("Failed to create DXT2 surface, hr %#x.\n", hr);
867         else
868         {
869             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
870             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT2 format.\n");
871             check_release((IUnknown*)newsurf, 0);
872         }
873
874         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_DXT3, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
875         if (FAILED(hr))
876             skip("Failed to create DXT3 surface, hr %#x.\n", hr);
877         else
878         {
879             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
880             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT3 format.\n");
881             check_release((IUnknown*)newsurf, 0);
882         }
883
884         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_DXT4, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
885         if (FAILED(hr))
886             skip("Failed to create DXT4 surface, hr %#x.\n", hr);
887         else
888         {
889             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
890             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT4 format.\n");
891             check_release((IUnknown*)newsurf, 0);
892         }
893
894         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_DXT5, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
895         if (FAILED(hr))
896             skip("Failed to create DXT5 surface, hr %#x.\n", hr);
897         else
898         {
899             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
900             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT5 format.\n");
901             check_release((IUnknown*)newsurf, 0);
902         }
903
904         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_DXT1, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
905         if (FAILED(hr))
906             skip("Failed to create DXT1 surface, hr %#x.\n", hr);
907         else
908         {
909             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
910             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT1 format.\n");
911
912             hr = D3DXLoadSurfaceFromSurface(surf, NULL, NULL, newsurf, NULL, NULL, D3DX_FILTER_NONE, 0);
913             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels from DXT1 format.\n");
914
915             check_release((IUnknown*)newsurf, 0);
916         }
917
918         check_release((IUnknown*)surf, 0);
919     }
920
921     /* cleanup */
922     if(testdummy_ok) DeleteFileA("testdummy.bmp");
923     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
924 }
925
926 static void test_D3DXSaveSurfaceToFile(IDirect3DDevice9 *device)
927 {
928     HRESULT hr;
929     IDirect3DSurface9 *surface;
930     RECT rect;
931     D3DLOCKED_RECT lock_rect;
932     D3DXIMAGE_INFO image_info;
933     const BYTE pixels[] = { 0xff, 0x00, 0x00, 0x00, 0xff, 0x00,
934                             0x00, 0x00, 0xff, 0x00, 0x00, 0xff };
935     DWORD pitch = sizeof(pixels) / 2;
936
937     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
938     if (FAILED(hr)) {
939        skip("Couldn't create surface\n");
940        return;
941     }
942
943     SetRect(&rect, 0, 0, 2, 2);
944     hr = D3DXLoadSurfaceFromMemory(surface, NULL, NULL, pixels, D3DFMT_R8G8B8, pitch, NULL, &rect, D3DX_FILTER_NONE, 0);
945     if (SUCCEEDED(hr)) {
946         hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, NULL);
947         ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
948
949         hr = D3DXLoadSurfaceFromFileA(surface, NULL, NULL, "saved_surface.bmp", NULL, D3DX_FILTER_NONE, 0, &image_info);
950         ok(hr == D3D_OK, "Couldn't load saved surface %#x\n", hr);
951         if (FAILED(hr)) goto next_tests;
952
953         ok(image_info.Width == 2, "Wrong width %u\n", image_info.Width);
954         ok(image_info.Height == 2, "Wrong height %u\n", image_info.Height);
955         ok(image_info.Format == D3DFMT_R8G8B8, "Wrong format %#x\n", image_info.Format);
956         ok(image_info.ImageFileFormat == D3DXIFF_BMP, "Wrong file format %u\n", image_info.ImageFileFormat);
957
958         hr = IDirect3DSurface9_LockRect(surface, &lock_rect, NULL, D3DLOCK_READONLY);
959         ok(hr == D3D_OK, "Couldn't lock surface %#x\n", hr);
960         if (FAILED(hr)) goto next_tests;
961
962         ok(!memcmp(lock_rect.pBits, pixels, pitch), "Pixel data mismatch in first row\n");
963         ok(!memcmp((BYTE *)lock_rect.pBits + lock_rect.Pitch, pixels + pitch, pitch), "Pixel data mismatch in second row\n");
964
965         IDirect3DSurface9_UnlockRect(surface);
966     } else skip("Couldn't fill surface\n");
967
968 next_tests:
969     hr = D3DXSaveSurfaceToFileA(NULL, D3DXIFF_BMP, surface, NULL, NULL);
970     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
971
972     /* PPM and TGA are supported, even though MSDN claims they aren't */
973     todo_wine {
974     hr = D3DXSaveSurfaceToFileA("saved_surface.ppm", D3DXIFF_PPM, surface, NULL, NULL);
975     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
976     hr = D3DXSaveSurfaceToFileA("saved_surface.tga", D3DXIFF_TGA, surface, NULL, NULL);
977     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
978     hr = D3DXSaveSurfaceToFileA("saved_surface.dds", D3DXIFF_DDS, surface, NULL, NULL);
979     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
980     }
981
982     hr = D3DXSaveSurfaceToFileA("saved_surface", D3DXIFF_PFM + 1, surface, NULL, NULL);
983     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
984
985     SetRect(&rect, 0, 0, 4, 4);
986     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
987     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
988     SetRect(&rect, 2, 0, 1, 4);
989     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
990     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
991     SetRect(&rect, 0, 2, 4, 1);
992     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
993     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
994     SetRect(&rect, -1, -1, 2, 2);
995     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
996     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
997     SetRect(&rect, 0, 0, 0, 0);
998     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
999     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
1000
1001     DeleteFileA("saved_surface.bmp");
1002     DeleteFileA("saved_surface.ppm");
1003     DeleteFileA("saved_surface.tga");
1004     DeleteFileA("saved_surface.dds");
1005
1006     IDirect3DSurface9_Release(surface);
1007 }
1008
1009 START_TEST(surface)
1010 {
1011     HWND wnd;
1012     IDirect3D9 *d3d;
1013     IDirect3DDevice9 *device;
1014     D3DPRESENT_PARAMETERS d3dpp;
1015     HRESULT hr;
1016
1017     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
1018     if (!wnd) {
1019         skip("Couldn't create application window\n");
1020         return;
1021     }
1022     d3d = Direct3DCreate9(D3D_SDK_VERSION);
1023     if (!d3d) {
1024         skip("Couldn't create IDirect3D9 object\n");
1025         DestroyWindow(wnd);
1026         return;
1027     }
1028
1029     ZeroMemory(&d3dpp, sizeof(d3dpp));
1030     d3dpp.Windowed   = TRUE;
1031     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1032     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
1033     if(FAILED(hr)) {
1034         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
1035         IDirect3D9_Release(d3d);
1036         DestroyWindow(wnd);
1037         return;
1038     }
1039
1040     test_D3DXGetImageInfo();
1041     test_D3DXLoadSurface(device);
1042     test_D3DXSaveSurfaceToFile(device);
1043
1044     check_release((IUnknown*)device, 0);
1045     check_release((IUnknown*)d3d, 0);
1046     DestroyWindow(wnd);
1047 }