strmbase: do not lock in BaseOutputPinImpl_GetDeliveryBuffer the MemInputPin will...
[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
154 static void check_dds_pixel_format(DWORD flags, DWORD fourcc, DWORD bpp,
155                                    DWORD rmask, DWORD gmask, DWORD bmask, DWORD amask,
156                                    D3DFORMAT expected_format)
157 {
158     HRESULT hr;
159     D3DXIMAGE_INFO info;
160     struct
161     {
162         DWORD magic;
163         DWORD size;
164         DWORD flags;
165         DWORD height;
166         DWORD width;
167         DWORD padding[14];
168         struct
169         {
170             DWORD size;
171             DWORD flags;
172             DWORD fourcc;
173             DWORD bpp;
174             DWORD rmask;
175             DWORD gmask;
176             DWORD bmask;
177             DWORD amask;
178         } pixel_format;
179         DWORD caps;
180         DWORD padding2[4];
181         BYTE data[256];
182     } dds;
183
184     memset(&dds, 0, sizeof(dds));
185
186     dds.magic = MAKEFOURCC('D','D','S',' ');
187     dds.size = 124;
188     dds.flags = DDS_CAPS | DDS_WIDTH | DDS_HEIGHT | DDS_PIXELFORMAT;
189     dds.height = 4;
190     dds.width = 4;
191     dds.pixel_format.size = sizeof(dds.pixel_format);
192     dds.pixel_format.flags = flags;
193     dds.pixel_format.fourcc = fourcc;
194     dds.pixel_format.bpp = bpp;
195     dds.pixel_format.rmask = rmask;
196     dds.pixel_format.gmask = gmask;
197     dds.pixel_format.bmask = bmask;
198     dds.pixel_format.amask = amask;
199     dds.caps = DDS_CAPS_TEXTURE;
200
201     hr = D3DXGetImageInfoFromFileInMemory(&dds, sizeof(dds), &info);
202     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x for pixel format %#x, expected %#x\n", hr, expected_format, D3D_OK);
203     if (SUCCEEDED(hr))
204         ok(info.Format == expected_format, "D3DXGetImageInfoFromFileInMemory returned format %#x, expected %#x\n", info.Format, expected_format);
205 }
206
207 static void test_D3DXGetImageInfo(void)
208 {
209     HRESULT hr;
210     D3DXIMAGE_INFO info;
211     BOOL testdummy_ok, testbitmap_ok;
212
213     hr = create_file("testdummy.bmp", noimage, sizeof(noimage));  /* invalid image */
214     testdummy_ok = SUCCEEDED(hr);
215
216     hr = create_file("testbitmap.bmp", bmp01, sizeof(bmp01));  /* valid image */
217     testbitmap_ok = SUCCEEDED(hr);
218
219     /* D3DXGetImageInfoFromFile */
220     if(testbitmap_ok) {
221         hr = D3DXGetImageInfoFromFileA("testbitmap.bmp", &info);
222         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
223
224         hr = D3DXGetImageInfoFromFileA("testbitmap.bmp", NULL); /* valid image, second parameter is NULL */
225         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
226     } else skip("Couldn't create \"testbitmap.bmp\"\n");
227
228     if(testdummy_ok) {
229         hr = D3DXGetImageInfoFromFileA("testdummy.bmp", NULL); /* invalid image, second parameter is NULL */
230         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
231
232         hr = D3DXGetImageInfoFromFileA("testdummy.bmp", &info);
233         ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
234     } else skip("Couldn't create \"testdummy.bmp\"\n");
235
236     hr = D3DXGetImageInfoFromFileA("filedoesnotexist.bmp", &info);
237     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
238
239     hr = D3DXGetImageInfoFromFileA("filedoesnotexist.bmp", NULL);
240     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
241
242     hr = D3DXGetImageInfoFromFileA("", &info);
243     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
244
245     hr = D3DXGetImageInfoFromFileA(NULL, &info);
246     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
247
248     hr = D3DXGetImageInfoFromFileA(NULL, NULL);
249     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
250
251
252     /* D3DXGetImageInfoFromResource */
253     todo_wine {
254         hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), &info); /* RT_BITMAP */
255         ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
256
257         hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), NULL);
258         ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
259     }
260
261     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), &info); /* RT_RCDATA */
262     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
263
264     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDS_STRING), &info);
265     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
266
267     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDS_STRING), NULL);
268     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
269
270     hr = D3DXGetImageInfoFromResourceA(NULL, "resourcedoesnotexist", &info);
271     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
272
273     hr = D3DXGetImageInfoFromResourceA(NULL, "resourcedoesnotexist", NULL);
274     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
275
276     hr = D3DXGetImageInfoFromResourceA(NULL, NULL, NULL);
277     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
278
279
280     /* D3DXGetImageInfoFromFileInMemory */
281     hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01), &info);
282     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
283
284     hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01)+5, &info); /* too large size */
285     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
286
287     hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01), NULL);
288     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
289
290     hr = D3DXGetImageInfoFromFileInMemory(noimage, sizeof(noimage), NULL);
291     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
292
293     hr = D3DXGetImageInfoFromFileInMemory(noimage, sizeof(noimage), &info);
294     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
295
296     todo_wine {
297         hr = D3DXGetImageInfoFromFileInMemory(bmp01, sizeof(bmp01)-1, &info);
298         ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
299     }
300
301     hr = D3DXGetImageInfoFromFileInMemory(bmp01+1, sizeof(bmp01)-1, &info);
302     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
303
304     hr = D3DXGetImageInfoFromFileInMemory(bmp01, 0, &info);
305     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
306
307     hr = D3DXGetImageInfoFromFileInMemory(bmp01, 0, NULL);
308     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
309
310     hr = D3DXGetImageInfoFromFileInMemory(noimage, 0, &info);
311     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
312
313     hr = D3DXGetImageInfoFromFileInMemory(noimage, 0, NULL);
314     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
315
316     hr = D3DXGetImageInfoFromFileInMemory(NULL, 0, &info);
317     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
318
319     hr = D3DXGetImageInfoFromFileInMemory(NULL, 4, NULL);
320     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
321
322     hr = D3DXGetImageInfoFromFileInMemory(NULL, 4, &info);
323     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
324
325     hr = D3DXGetImageInfoFromFileInMemory(NULL, 0, NULL);
326     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
327
328
329     /* test DDS support */
330     hr = D3DXGetImageInfoFromFileInMemory(dds_24bit, sizeof(dds_24bit), &info);
331     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
332     if (hr == D3D_OK) {
333         ok(info.Width == 2, "Got width %u, expected 2\n", info.Width);
334         ok(info.Height == 2, "Got height %u, expected 2\n", info.Height);
335         ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
336         ok(info.MipLevels == 2, "Got miplevels %u, expected 2\n", info.MipLevels);
337         ok(info.Format == D3DFMT_R8G8B8, "Got format %#x, expected %#x\n", info.Format, D3DFMT_R8G8B8);
338         ok(info.ResourceType == D3DRTYPE_TEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_TEXTURE);
339         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
340     } else skip("Couldn't get image info from 24-bit DDS file in memory\n");
341
342     hr = D3DXGetImageInfoFromFileInMemory(dds_16bit, sizeof(dds_16bit), &info);
343     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
344     if (hr == D3D_OK) {
345         ok(info.Width == 2, "Got width %u, expected 2\n", info.Width);
346         ok(info.Height == 2, "Got height %u, expected 2\n", info.Height);
347         ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
348         ok(info.MipLevels == 1, "Got miplevels %u, expected 1\n", info.MipLevels);
349         ok(info.Format == D3DFMT_X1R5G5B5, "Got format %#x, expected %#x\n", info.Format, D3DFMT_X1R5G5B5);
350         ok(info.ResourceType == D3DRTYPE_TEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_TEXTURE);
351         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
352     } else skip("Couldn't get image info from 16-bit DDS file in memory\n");
353
354     hr = D3DXGetImageInfoFromFileInMemory(dds_cube_map, sizeof(dds_cube_map), &info);
355     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
356     if (hr == D3D_OK) {
357         ok(info.Width == 4, "Got width %u, expected 4\n", info.Width);
358         ok(info.Height == 4, "Got height %u, expected 4\n", info.Height);
359         ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
360         ok(info.MipLevels == 1, "Got miplevels %u, expected 1\n", info.MipLevels);
361         ok(info.Format == D3DFMT_DXT5, "Got format %#x, expected %#x\n", info.Format, D3DFMT_DXT5);
362         ok(info.ResourceType == D3DRTYPE_CUBETEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_CUBETEXTURE);
363         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
364     } else skip("Couldn't get image info from cube map in memory\n");
365
366     hr = D3DXGetImageInfoFromFileInMemory(dds_volume_map, sizeof(dds_volume_map), &info);
367     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
368     if (hr == D3D_OK) {
369         ok(info.Width == 4, "Got width %u, expected 4\n", info.Width);
370         ok(info.Height == 4, "Got height %u, expected 4\n", info.Height);
371         ok(info.Depth == 2, "Got depth %u, expected 2\n", info.Depth);
372         ok(info.MipLevels == 3, "Got miplevels %u, expected 3\n", info.MipLevels);
373         ok(info.Format == D3DFMT_DXT3, "Got format %#x, expected %#x\n", info.Format, D3DFMT_DXT3);
374         ok(info.ResourceType == D3DRTYPE_VOLUMETEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_VOLUMETEXTURE);
375         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
376     } else skip("Couldn't get image info from volume map in memory\n");
377
378     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','1'), 0, 0, 0, 0, 0, D3DFMT_DXT1);
379     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','2'), 0, 0, 0, 0, 0, D3DFMT_DXT2);
380     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','3'), 0, 0, 0, 0, 0, D3DFMT_DXT3);
381     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','4'), 0, 0, 0, 0, 0, D3DFMT_DXT4);
382     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('D','X','T','5'), 0, 0, 0, 0, 0, D3DFMT_DXT5);
383     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('R','G','B','G'), 0, 0, 0, 0, 0, D3DFMT_R8G8_B8G8);
384     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('G','R','G','B'), 0, 0, 0, 0, 0, D3DFMT_G8R8_G8B8);
385     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('U','Y','V','Y'), 0, 0, 0, 0, 0, D3DFMT_UYVY);
386     check_dds_pixel_format(DDS_PF_FOURCC, MAKEFOURCC('Y','U','Y','2'), 0, 0, 0, 0, 0, D3DFMT_YUY2);
387     check_dds_pixel_format(DDS_PF_RGB, 0, 16, 0xf800, 0x07e0, 0x001f, 0, D3DFMT_R5G6B5);
388     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5);
389     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4);
390     check_dds_pixel_format(DDS_PF_RGB, 0, 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2);
391     check_dds_pixel_format(DDS_PF_ALPHA_ONLY, 0, 8, 0, 0, 0, 0xff, D3DFMT_A8);
392     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2);
393     check_dds_pixel_format(DDS_PF_RGB, 0, 16, 0xf00, 0x0f0, 0x00f, 0, D3DFMT_X4R4G4B4);
394     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10);
395     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10);
396     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8);
397     check_dds_pixel_format(DDS_PF_RGB, 0, 32, 0xff0000, 0x00ff00, 0x0000ff, 0, D3DFMT_X8R8G8B8);
398     check_dds_pixel_format(DDS_PF_RGB, 0, 32, 0x0000ffff, 0xffff0000, 0, 0, D3DFMT_G16R16);
399     check_dds_pixel_format(DDS_PF_LUMINANCE, 0, 8, 0xff, 0, 0, 0, D3DFMT_L8);
400     check_dds_pixel_format(DDS_PF_LUMINANCE | DDS_PF_ALPHA, 0, 16, 0x00ff, 0, 0, 0xff00, D3DFMT_A8L8);
401     check_dds_pixel_format(DDS_PF_LUMINANCE | DDS_PF_ALPHA, 0, 8, 0x0f, 0, 0, 0xf0, D3DFMT_A4L4);
402
403     todo_wine {
404         hr = D3DXGetImageInfoFromFileInMemory(dds_16bit, sizeof(dds_16bit) - 1, &info);
405         ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
406     }
407
408
409     /* cleanup */
410     if(testdummy_ok) DeleteFileA("testdummy.bmp");
411     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
412 }
413
414 #define check_pixel_1bpp(lockrect, x, y, color) \
415 ok(((BYTE*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch] == color, "Got color 0x%02x, expected 0x%02x.\n", \
416 ((BYTE*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch], color)
417
418 #define check_pixel_2bpp(lockrect, x, y, color) \
419 ok(((WORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 2] == color, "Got color 0x%04x, expected 0x%04x.\n", \
420 ((WORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 2], color)
421
422 #define check_pixel_4bpp(lockrect, x, y, color) \
423 ok(((DWORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 4] == color, "Got color 0x%08x, expected 0x%08x.\n", \
424 ((DWORD*)(lockrect).pBits)[(x) + (y) * (lockrect).Pitch / 4], color)
425 static void test_D3DXLoadSurface(IDirect3DDevice9 *device)
426 {
427     HRESULT hr;
428     BOOL testdummy_ok, testbitmap_ok;
429     IDirect3DSurface9 *surf, *newsurf;
430     RECT rect, destrect;
431     D3DLOCKED_RECT lockrect;
432     const WORD pixdata_a8r3g3b2[] = { 0x57df, 0x98fc, 0xacdd, 0xc891 };
433     const WORD pixdata_a1r5g5b5[] = { 0x46b5, 0x99c8, 0x06a2, 0x9431 };
434     const WORD pixdata_r5g6b5[] = { 0x9ef6, 0x658d, 0x0aee, 0x42ee };
435     const WORD pixdata_a8l8[] = { 0xff00, 0x00ff, 0xff30, 0x7f7f };
436     const DWORD pixdata_g16r16[] = { 0x07d23fbe, 0xdc7f44a4, 0xe4d8976b, 0x9a84fe89 };
437     const DWORD pixdata_a8b8g8r8[] = { 0xc3394cf0, 0x235ae892, 0x09b197fd, 0x8dc32bf6 };
438     const DWORD pixdata_a2r10g10b10[] = { 0x57395aff, 0x5b7668fd, 0xb0d856b5, 0xff2c61d6 };
439
440     hr = create_file("testdummy.bmp", noimage, sizeof(noimage));  /* invalid image */
441     testdummy_ok = SUCCEEDED(hr);
442
443     hr = create_file("testbitmap.bmp", bmp01, sizeof(bmp01));  /* valid image */
444     testbitmap_ok = SUCCEEDED(hr);
445
446     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
447     if(FAILED(hr)) {
448         skip("Failed to create a surface (%#x)\n", hr);
449         if(testdummy_ok) DeleteFileA("testdummy.bmp");
450         if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
451         return;
452     }
453
454     /* D3DXLoadSurfaceFromFile */
455     if(testbitmap_ok) {
456         todo_wine {
457             hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "testbitmap.bmp", NULL, D3DX_DEFAULT, 0, NULL);
458             ok(hr == D3D_OK, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3D_OK);
459         }
460
461         hr = D3DXLoadSurfaceFromFileA(NULL, NULL, NULL, "testbitmap.bmp", NULL, D3DX_DEFAULT, 0, NULL);
462         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
463     } else skip("Couldn't create \"testbitmap.bmp\"\n");
464
465     if(testdummy_ok) {
466         hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "testdummy.bmp", NULL, D3DX_DEFAULT, 0, NULL);
467         ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
468     } else skip("Couldn't create \"testdummy.bmp\"\n");
469
470     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
471     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
472
473     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "", NULL, D3DX_DEFAULT, 0, NULL);
474     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
475
476
477     /* D3DXLoadSurfaceFromResource */
478     todo_wine {
479         hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
480         ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
481     }
482
483     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDD_BITMAPDATA_1x1), NULL, D3DX_DEFAULT, 0, NULL);
484     ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
485
486     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
487     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
488
489     hr = D3DXLoadSurfaceFromResourceA(NULL, NULL, NULL, NULL, MAKEINTRESOURCE(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
490     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
491
492     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, MAKEINTRESOURCE(IDS_STRING), NULL, D3DX_DEFAULT, 0, NULL);
493     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
494
495
496     /* D3DXLoadSurfaceFromFileInMemory */
497     todo_wine {
498         hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp01, sizeof(bmp01), NULL, D3DX_DEFAULT, 0, NULL);
499         ok(hr == D3D_OK, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
500     }
501
502     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, noimage, sizeof(noimage), NULL, D3DX_DEFAULT, 0, NULL);
503     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
504
505     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp01, 0, NULL, D3DX_DEFAULT, 0, NULL);
506     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
507
508     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, bmp01, sizeof(bmp01), NULL, D3DX_DEFAULT, 0, NULL);
509     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
510
511     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 8, NULL, D3DX_DEFAULT, 0, NULL);
512     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
513
514     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
515     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
516
517     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
518     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
519
520
521     /* D3DXLoadSurfaceFromMemory */
522     SetRect(&rect, 0, 0, 2, 2);
523
524     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
525     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
526
527     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, 0, NULL, &rect, D3DX_FILTER_NONE, 0);
528     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
529
530     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, NULL, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
531     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
532
533     hr = D3DXLoadSurfaceFromMemory(NULL, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
534     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
535
536     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, NULL, D3DX_DEFAULT, 0);
537     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
538
539     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_UNKNOWN, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
540     ok(hr == E_FAIL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, E_FAIL);
541
542     SetRect(&destrect, -1, -1, 1, 1); /* destination rect is partially outside texture boundaries */
543     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
544     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
545
546     SetRect(&destrect, 255, 255, 257, 257); /* destination rect is partially outside texture boundaries */
547     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
548     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
549
550     SetRect(&destrect, 1, 1, 0, 0); /* left > right, top > bottom */
551     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
552     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
553
554     SetRect(&destrect, 0, 0, 0, 0); /* left = right, top = bottom */
555     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
556     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
557
558     SetRect(&destrect, 257, 257, 257, 257); /* left = right, top = bottom, but invalid values */
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
563     /* D3DXLoadSurfaceFromSurface */
564     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &newsurf, NULL);
565     if(SUCCEEDED(hr)) {
566         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
567         ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3D_OK);
568
569         hr = D3DXLoadSurfaceFromSurface(NULL, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
570         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
571
572         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0);
573         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
574
575         check_release((IUnknown*)newsurf, 0);
576     } else skip("Failed to create a second surface\n");
577
578     check_release((IUnknown*)surf, 0);
579
580
581     /* test color conversion */
582     /* A8R8G8B8 */
583     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
584     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
585     else {
586         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
587         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
588         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
589         check_pixel_4bpp(lockrect, 0, 0, 0x57dbffff);
590         check_pixel_4bpp(lockrect, 1, 0, 0x98ffff00);
591         check_pixel_4bpp(lockrect, 0, 1, 0xacdbff55);
592         check_pixel_4bpp(lockrect, 1, 1, 0xc8929255);
593         IDirect3DSurface9_UnlockRect(surf);
594
595         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
596         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
597         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
598         check_pixel_4bpp(lockrect, 0, 0, 0x008cadad);
599         check_pixel_4bpp(lockrect, 1, 0, 0xff317342);
600         check_pixel_4bpp(lockrect, 0, 1, 0x0008ad10);
601         check_pixel_4bpp(lockrect, 1, 1, 0xff29088c);
602         IDirect3DSurface9_UnlockRect(surf);
603
604         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
605         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
606         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
607         check_pixel_4bpp(lockrect, 0, 0, 0xff9cdfb5);
608         check_pixel_4bpp(lockrect, 1, 0, 0xff63b26b);
609         check_pixel_4bpp(lockrect, 0, 1, 0xff085d73);
610         check_pixel_4bpp(lockrect, 1, 1, 0xff425d73);
611         IDirect3DSurface9_UnlockRect(surf);
612
613         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
614         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
615         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
616         todo_wine {
617             check_pixel_4bpp(lockrect, 0, 0, 0xff3f08ff);
618         }
619         check_pixel_4bpp(lockrect, 1, 0, 0xff44dcff);
620         check_pixel_4bpp(lockrect, 0, 1, 0xff97e4ff);
621         check_pixel_4bpp(lockrect, 1, 1, 0xfffe9aff);
622         IDirect3DSurface9_UnlockRect(surf);
623
624         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
625         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
626         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
627         check_pixel_4bpp(lockrect, 0, 0, 0xc3f04c39);
628         check_pixel_4bpp(lockrect, 1, 0, 0x2392e85a);
629         check_pixel_4bpp(lockrect, 0, 1, 0x09fd97b1);
630         check_pixel_4bpp(lockrect, 1, 1, 0x8df62bc3);
631         IDirect3DSurface9_UnlockRect(surf);
632
633         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
634         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
635         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
636         check_pixel_4bpp(lockrect, 0, 0, 0x555c95bf);
637         check_pixel_4bpp(lockrect, 1, 0, 0x556d663f);
638         check_pixel_4bpp(lockrect, 0, 1, 0xaac385ad);
639         todo_wine {
640             check_pixel_4bpp(lockrect, 1, 1, 0xfffcc575);
641         }
642         IDirect3DSurface9_UnlockRect(surf);
643
644         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
645                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
646         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
647         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
648         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
649         check_pixel_4bpp(lockrect, 0, 0, 0xff000000);
650         check_pixel_4bpp(lockrect, 1, 0, 0x00ffffff);
651         check_pixel_4bpp(lockrect, 0, 1, 0xff303030);
652         check_pixel_4bpp(lockrect, 1, 1, 0x7f7f7f7f);
653         hr = IDirect3DSurface9_UnlockRect(surf);
654         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
655
656         check_release((IUnknown*)surf, 0);
657     }
658
659     /* A1R5G5B5 */
660     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A1R5G5B5, D3DPOOL_DEFAULT, &surf, NULL);
661     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
662     else {
663         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
664         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
665         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
666         check_pixel_2bpp(lockrect, 0, 0, 0x6fff);
667         check_pixel_2bpp(lockrect, 1, 0, 0xffe0);
668         check_pixel_2bpp(lockrect, 0, 1, 0xefea);
669         check_pixel_2bpp(lockrect, 1, 1, 0xca4a);
670         IDirect3DSurface9_UnlockRect(surf);
671
672         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
673         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
674         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
675         check_pixel_2bpp(lockrect, 0, 0, 0x46b5);
676         check_pixel_2bpp(lockrect, 1, 0, 0x99c8);
677         check_pixel_2bpp(lockrect, 0, 1, 0x06a2);
678         check_pixel_2bpp(lockrect, 1, 1, 0x9431);
679         IDirect3DSurface9_UnlockRect(surf);
680
681         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
682         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
683         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
684         check_pixel_2bpp(lockrect, 0, 0, 0xcf76);
685         check_pixel_2bpp(lockrect, 1, 0, 0xb2cd);
686         check_pixel_2bpp(lockrect, 0, 1, 0x856e);
687         check_pixel_2bpp(lockrect, 1, 1, 0xa16e);
688         IDirect3DSurface9_UnlockRect(surf);
689
690         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
691         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
692         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
693         todo_wine {
694             check_pixel_2bpp(lockrect, 0, 0, 0xa03f);
695         }
696         check_pixel_2bpp(lockrect, 1, 0, 0xa37f);
697         check_pixel_2bpp(lockrect, 0, 1, 0xcb9f);
698         check_pixel_2bpp(lockrect, 1, 1, 0xfe7f);
699         IDirect3DSurface9_UnlockRect(surf);
700
701         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
702         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
703         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
704         todo_wine {
705             check_pixel_2bpp(lockrect, 0, 0, 0xf527);
706             check_pixel_2bpp(lockrect, 1, 0, 0x4b8b);
707         }
708         check_pixel_2bpp(lockrect, 0, 1, 0x7e56);
709         check_pixel_2bpp(lockrect, 1, 1, 0xf8b8);
710         IDirect3DSurface9_UnlockRect(surf);
711
712         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
713         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
714         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
715         check_pixel_2bpp(lockrect, 0, 0, 0x2e57);
716         todo_wine {
717             check_pixel_2bpp(lockrect, 1, 0, 0x3588);
718         }
719         check_pixel_2bpp(lockrect, 0, 1, 0xe215);
720         check_pixel_2bpp(lockrect, 1, 1, 0xff0e);
721         IDirect3DSurface9_UnlockRect(surf);
722
723         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
724                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
725         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
726         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
727         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
728         check_pixel_2bpp(lockrect, 0, 0, 0x8000);
729         check_pixel_2bpp(lockrect, 1, 0, 0x7fff);
730         check_pixel_2bpp(lockrect, 0, 1, 0x98c6);
731         check_pixel_2bpp(lockrect, 1, 1, 0x3def);
732         hr = IDirect3DSurface9_UnlockRect(surf);
733         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
734
735         check_release((IUnknown*)surf, 0);
736     }
737
738     /* A8L8 */
739     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A8L8, D3DPOOL_DEFAULT, &surf, NULL);
740     if (FAILED(hr))
741         skip("Failed to create A8L8 surface, hr %#x.\n", hr);
742     else
743     {
744         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2,
745                 D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
746         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
747         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
748         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
749         check_pixel_2bpp(lockrect, 0, 0, 0x57f7);
750         check_pixel_2bpp(lockrect, 1, 0, 0x98ed);
751         check_pixel_2bpp(lockrect, 0, 1, 0xaceb);
752         check_pixel_2bpp(lockrect, 1, 1, 0xc88d);
753         hr = IDirect3DSurface9_UnlockRect(surf);
754         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
755
756         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5,
757                 D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
758         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
759         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
760         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
761         check_pixel_2bpp(lockrect, 0, 0, 0x00a6);
762         check_pixel_2bpp(lockrect, 1, 0, 0xff62);
763         check_pixel_2bpp(lockrect, 0, 1, 0x007f);
764         check_pixel_2bpp(lockrect, 1, 1, 0xff19);
765         hr = IDirect3DSurface9_UnlockRect(surf);
766         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
767
768         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5,
769                 D3DFMT_R5G6B5, 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, 0xffce);
774         check_pixel_2bpp(lockrect, 1, 0, 0xff9c);
775         check_pixel_2bpp(lockrect, 0, 1, 0xff4d);
776         check_pixel_2bpp(lockrect, 1, 1, 0xff59);
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_g16r16,
781                 D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
782         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
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, 0xff25);
786         check_pixel_2bpp(lockrect, 1, 0, 0xffbe);
787         check_pixel_2bpp(lockrect, 0, 1, 0xffd6);
788         check_pixel_2bpp(lockrect, 1, 1, 0xffb6);
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_a8b8g8r8,
793                 D3DFMT_A8B8G8R8, 8, 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, 0xc36d);
798         check_pixel_2bpp(lockrect, 1, 0, 0x23cb);
799         check_pixel_2bpp(lockrect, 0, 1, 0x09af);
800         check_pixel_2bpp(lockrect, 1, 1, 0x8d61);
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_a2r10g10b10,
805                 D3DFMT_A2R10G10B10, 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, 0x558c);
810         check_pixel_2bpp(lockrect, 1, 0, 0x5565);
811         check_pixel_2bpp(lockrect, 0, 1, 0xaa95);
812         check_pixel_2bpp(lockrect, 1, 1, 0xffcb);
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_a8l8,
817                 D3DFMT_A8L8, 4, 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, 0xff00);
822         check_pixel_2bpp(lockrect, 1, 0, 0x00ff);
823         check_pixel_2bpp(lockrect, 0, 1, 0xff30);
824         check_pixel_2bpp(lockrect, 1, 1, 0x7f7f);
825         hr = IDirect3DSurface9_UnlockRect(surf);
826         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
827
828         check_release((IUnknown*)surf, 0);
829     }
830
831     /* cleanup */
832     if(testdummy_ok) DeleteFileA("testdummy.bmp");
833     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
834 }
835
836 static void test_D3DXSaveSurfaceToFile(IDirect3DDevice9 *device)
837 {
838     HRESULT hr;
839     IDirect3DSurface9 *surface;
840     RECT rect;
841     D3DLOCKED_RECT lock_rect;
842     D3DXIMAGE_INFO image_info;
843     const BYTE pixels[] = { 0xff, 0x00, 0x00, 0x00, 0xff, 0x00,
844                             0x00, 0x00, 0xff, 0x00, 0x00, 0xff };
845     DWORD pitch = sizeof(pixels) / 2;
846
847     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
848     if (FAILED(hr)) {
849        skip("Couldn't create surface\n");
850        return;
851     }
852
853     SetRect(&rect, 0, 0, 2, 2);
854     hr = D3DXLoadSurfaceFromMemory(surface, NULL, NULL, pixels, D3DFMT_R8G8B8, pitch, NULL, &rect, D3DX_FILTER_NONE, 0);
855     if (SUCCEEDED(hr)) {
856         hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, NULL);
857         ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
858
859         hr = D3DXLoadSurfaceFromFileA(surface, NULL, NULL, "saved_surface.bmp", NULL, D3DX_FILTER_NONE, 0, &image_info);
860         ok(hr == D3D_OK, "Couldn't load saved surface %#x\n", hr);
861         if (FAILED(hr)) goto next_tests;
862
863         ok(image_info.Width == 2, "Wrong width %u\n", image_info.Width);
864         ok(image_info.Height == 2, "Wrong height %u\n", image_info.Height);
865         ok(image_info.Format == D3DFMT_R8G8B8, "Wrong format %#x\n", image_info.Format);
866         ok(image_info.ImageFileFormat == D3DXIFF_BMP, "Wrong file format %u\n", image_info.ImageFileFormat);
867
868         hr = IDirect3DSurface9_LockRect(surface, &lock_rect, NULL, D3DLOCK_READONLY);
869         ok(hr == D3D_OK, "Couldn't lock surface %#x\n", hr);
870         if (FAILED(hr)) goto next_tests;
871
872         ok(!memcmp(lock_rect.pBits, pixels, pitch), "Pixel data mismatch in first row\n");
873         ok(!memcmp((BYTE *)lock_rect.pBits + lock_rect.Pitch, pixels + pitch, pitch), "Pixel data mismatch in second row\n");
874
875         IDirect3DSurface9_UnlockRect(surface);
876     } else skip("Couldn't fill surface\n");
877
878 next_tests:
879     hr = D3DXSaveSurfaceToFileA(NULL, D3DXIFF_BMP, surface, NULL, NULL);
880     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
881
882     /* PPM and TGA are supported, even though MSDN claims they aren't */
883     todo_wine {
884     hr = D3DXSaveSurfaceToFileA("saved_surface.ppm", D3DXIFF_PPM, surface, NULL, NULL);
885     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
886     hr = D3DXSaveSurfaceToFileA("saved_surface.tga", D3DXIFF_TGA, surface, NULL, NULL);
887     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
888     hr = D3DXSaveSurfaceToFileA("saved_surface.dds", D3DXIFF_DDS, surface, NULL, NULL);
889     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
890     }
891
892     hr = D3DXSaveSurfaceToFileA("saved_surface", D3DXIFF_PFM + 1, surface, NULL, NULL);
893     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
894
895     SetRect(&rect, 0, 0, 4, 4);
896     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
897     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
898     SetRect(&rect, 2, 0, 1, 4);
899     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
900     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
901     SetRect(&rect, 0, 2, 4, 1);
902     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
903     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
904     SetRect(&rect, -1, -1, 2, 2);
905     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
906     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
907     SetRect(&rect, 0, 0, 0, 0);
908     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
909     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
910
911     DeleteFileA("saved_surface.bmp");
912     DeleteFileA("saved_surface.ppm");
913     DeleteFileA("saved_surface.tga");
914     DeleteFileA("saved_surface.dds");
915
916     IDirect3DSurface9_Release(surface);
917 }
918
919 START_TEST(surface)
920 {
921     HWND wnd;
922     IDirect3D9 *d3d;
923     IDirect3DDevice9 *device;
924     D3DPRESENT_PARAMETERS d3dpp;
925     HRESULT hr;
926
927     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
928     if (!wnd) {
929         skip("Couldn't create application window\n");
930         return;
931     }
932     d3d = Direct3DCreate9(D3D_SDK_VERSION);
933     if (!d3d) {
934         skip("Couldn't create IDirect3D9 object\n");
935         DestroyWindow(wnd);
936         return;
937     }
938
939     ZeroMemory(&d3dpp, sizeof(d3dpp));
940     d3dpp.Windowed   = TRUE;
941     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
942     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
943     if(FAILED(hr)) {
944         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
945         IDirect3D9_Release(d3d);
946         DestroyWindow(wnd);
947         return;
948     }
949
950     test_D3DXGetImageInfo();
951     test_D3DXLoadSurface(device);
952     test_D3DXSaveSurfaceToFile(device);
953
954     check_release((IUnknown*)device, 0);
955     check_release((IUnknown*)d3d, 0);
956     DestroyWindow(wnd);
957 }