d3dx9: Fix the expected_length computation for non-square DDS files.
[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     /* fails when debug version of d3d9 is used */
569     ok(hr == D3D_OK || broken(hr == D3DERR_INVALIDCALL), "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
570
571     SetRect(&destrect, 257, 257, 257, 257); /* left = right, top = bottom, but invalid values */
572     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
573     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
574
575
576     /* D3DXLoadSurfaceFromSurface */
577     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &newsurf, NULL);
578     if(SUCCEEDED(hr)) {
579         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
580         ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3D_OK);
581
582         hr = D3DXLoadSurfaceFromSurface(NULL, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
583         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
584
585         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0);
586         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
587
588         check_release((IUnknown*)newsurf, 0);
589     } else skip("Failed to create a second surface\n");
590
591     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex, NULL);
592     if (SUCCEEDED(hr))
593     {
594         IDirect3DTexture9_GetSurfaceLevel(tex, 0, &newsurf);
595
596         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
597         ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3D_OK);
598
599         IDirect3DSurface9_Release(newsurf);
600         IDirect3DTexture9_Release(tex);
601     } else skip("Failed to create texture\n");
602
603     check_release((IUnknown*)surf, 0);
604
605
606     /* test color conversion */
607     /* A8R8G8B8 */
608     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
609     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
610     else {
611         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
612         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
613         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
614         check_pixel_4bpp(lockrect, 0, 0, 0x57dbffff);
615         check_pixel_4bpp(lockrect, 1, 0, 0x98ffff00);
616         check_pixel_4bpp(lockrect, 0, 1, 0xacdbff55);
617         check_pixel_4bpp(lockrect, 1, 1, 0xc8929255);
618         IDirect3DSurface9_UnlockRect(surf);
619
620         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
621         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
622         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
623         check_pixel_4bpp(lockrect, 0, 0, 0x008cadad);
624         check_pixel_4bpp(lockrect, 1, 0, 0xff317342);
625         check_pixel_4bpp(lockrect, 0, 1, 0x0008ad10);
626         check_pixel_4bpp(lockrect, 1, 1, 0xff29088c);
627         IDirect3DSurface9_UnlockRect(surf);
628
629         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
630         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
631         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
632         check_pixel_4bpp(lockrect, 0, 0, 0xff9cdfb5);
633         check_pixel_4bpp(lockrect, 1, 0, 0xff63b26b);
634         check_pixel_4bpp(lockrect, 0, 1, 0xff085d73);
635         check_pixel_4bpp(lockrect, 1, 1, 0xff425d73);
636         IDirect3DSurface9_UnlockRect(surf);
637
638         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
639         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
640         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
641         todo_wine {
642             check_pixel_4bpp(lockrect, 0, 0, 0xff3f08ff);
643         }
644         check_pixel_4bpp(lockrect, 1, 0, 0xff44dcff);
645         check_pixel_4bpp(lockrect, 0, 1, 0xff97e4ff);
646         check_pixel_4bpp(lockrect, 1, 1, 0xfffe9aff);
647         IDirect3DSurface9_UnlockRect(surf);
648
649         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
650         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
651         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
652         check_pixel_4bpp(lockrect, 0, 0, 0xc3f04c39);
653         check_pixel_4bpp(lockrect, 1, 0, 0x2392e85a);
654         check_pixel_4bpp(lockrect, 0, 1, 0x09fd97b1);
655         check_pixel_4bpp(lockrect, 1, 1, 0x8df62bc3);
656         IDirect3DSurface9_UnlockRect(surf);
657
658         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
659         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
660         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
661         check_pixel_4bpp(lockrect, 0, 0, 0x555c95bf);
662         check_pixel_4bpp(lockrect, 1, 0, 0x556d663f);
663         check_pixel_4bpp(lockrect, 0, 1, 0xaac385ad);
664         todo_wine {
665             check_pixel_4bpp(lockrect, 1, 1, 0xfffcc575);
666         }
667         IDirect3DSurface9_UnlockRect(surf);
668
669         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
670                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
671         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
672         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
673         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
674         check_pixel_4bpp(lockrect, 0, 0, 0xff000000);
675         check_pixel_4bpp(lockrect, 1, 0, 0x00ffffff);
676         check_pixel_4bpp(lockrect, 0, 1, 0xff303030);
677         check_pixel_4bpp(lockrect, 1, 1, 0x7f7f7f7f);
678         hr = IDirect3DSurface9_UnlockRect(surf);
679         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
680
681         check_release((IUnknown*)surf, 0);
682     }
683
684     /* A1R5G5B5 */
685     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A1R5G5B5, D3DPOOL_DEFAULT, &surf, NULL);
686     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
687     else {
688         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
689         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
690         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
691         check_pixel_2bpp(lockrect, 0, 0, 0x6fff);
692         check_pixel_2bpp(lockrect, 1, 0, 0xffe0);
693         check_pixel_2bpp(lockrect, 0, 1, 0xefea);
694         check_pixel_2bpp(lockrect, 1, 1, 0xca4a);
695         IDirect3DSurface9_UnlockRect(surf);
696
697         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
698         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
699         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
700         check_pixel_2bpp(lockrect, 0, 0, 0x46b5);
701         check_pixel_2bpp(lockrect, 1, 0, 0x99c8);
702         check_pixel_2bpp(lockrect, 0, 1, 0x06a2);
703         check_pixel_2bpp(lockrect, 1, 1, 0x9431);
704         IDirect3DSurface9_UnlockRect(surf);
705
706         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
707         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
708         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
709         check_pixel_2bpp(lockrect, 0, 0, 0xcf76);
710         check_pixel_2bpp(lockrect, 1, 0, 0xb2cd);
711         check_pixel_2bpp(lockrect, 0, 1, 0x856e);
712         check_pixel_2bpp(lockrect, 1, 1, 0xa16e);
713         IDirect3DSurface9_UnlockRect(surf);
714
715         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
716         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
717         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
718         todo_wine {
719             check_pixel_2bpp(lockrect, 0, 0, 0xa03f);
720         }
721         check_pixel_2bpp(lockrect, 1, 0, 0xa37f);
722         check_pixel_2bpp(lockrect, 0, 1, 0xcb9f);
723         check_pixel_2bpp(lockrect, 1, 1, 0xfe7f);
724         IDirect3DSurface9_UnlockRect(surf);
725
726         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
727         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
728         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
729         todo_wine {
730             check_pixel_2bpp(lockrect, 0, 0, 0xf527);
731             check_pixel_2bpp(lockrect, 1, 0, 0x4b8b);
732         }
733         check_pixel_2bpp(lockrect, 0, 1, 0x7e56);
734         check_pixel_2bpp(lockrect, 1, 1, 0xf8b8);
735         IDirect3DSurface9_UnlockRect(surf);
736
737         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
738         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
739         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
740         check_pixel_2bpp(lockrect, 0, 0, 0x2e57);
741         todo_wine {
742             check_pixel_2bpp(lockrect, 1, 0, 0x3588);
743         }
744         check_pixel_2bpp(lockrect, 0, 1, 0xe215);
745         check_pixel_2bpp(lockrect, 1, 1, 0xff0e);
746         IDirect3DSurface9_UnlockRect(surf);
747
748         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
749                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
750         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
751         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
752         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
753         check_pixel_2bpp(lockrect, 0, 0, 0x8000);
754         check_pixel_2bpp(lockrect, 1, 0, 0x7fff);
755         check_pixel_2bpp(lockrect, 0, 1, 0x98c6);
756         check_pixel_2bpp(lockrect, 1, 1, 0x3def);
757         hr = IDirect3DSurface9_UnlockRect(surf);
758         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
759
760         check_release((IUnknown*)surf, 0);
761     }
762
763     /* A8L8 */
764     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A8L8, D3DPOOL_DEFAULT, &surf, NULL);
765     if (FAILED(hr))
766         skip("Failed to create A8L8 surface, hr %#x.\n", hr);
767     else
768     {
769         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2,
770                 D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
771         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
772         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
773         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
774         check_pixel_2bpp(lockrect, 0, 0, 0x57f7);
775         check_pixel_2bpp(lockrect, 1, 0, 0x98ed);
776         check_pixel_2bpp(lockrect, 0, 1, 0xaceb);
777         check_pixel_2bpp(lockrect, 1, 1, 0xc88d);
778         hr = IDirect3DSurface9_UnlockRect(surf);
779         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
780
781         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5,
782                 D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
783         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
784         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
785         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
786         check_pixel_2bpp(lockrect, 0, 0, 0x00a6);
787         check_pixel_2bpp(lockrect, 1, 0, 0xff62);
788         check_pixel_2bpp(lockrect, 0, 1, 0x007f);
789         check_pixel_2bpp(lockrect, 1, 1, 0xff19);
790         hr = IDirect3DSurface9_UnlockRect(surf);
791         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
792
793         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5,
794                 D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
795         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
796         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
797         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
798         check_pixel_2bpp(lockrect, 0, 0, 0xffce);
799         check_pixel_2bpp(lockrect, 1, 0, 0xff9c);
800         check_pixel_2bpp(lockrect, 0, 1, 0xff4d);
801         check_pixel_2bpp(lockrect, 1, 1, 0xff59);
802         hr = IDirect3DSurface9_UnlockRect(surf);
803         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
804
805         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16,
806                 D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
807         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
808         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
809         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
810         check_pixel_2bpp(lockrect, 0, 0, 0xff25);
811         check_pixel_2bpp(lockrect, 1, 0, 0xffbe);
812         check_pixel_2bpp(lockrect, 0, 1, 0xffd6);
813         check_pixel_2bpp(lockrect, 1, 1, 0xffb6);
814         hr = IDirect3DSurface9_UnlockRect(surf);
815         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
816
817         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8,
818                 D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
819         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
820         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
821         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
822         check_pixel_2bpp(lockrect, 0, 0, 0xc36d);
823         check_pixel_2bpp(lockrect, 1, 0, 0x23cb);
824         check_pixel_2bpp(lockrect, 0, 1, 0x09af);
825         check_pixel_2bpp(lockrect, 1, 1, 0x8d61);
826         hr = IDirect3DSurface9_UnlockRect(surf);
827         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
828
829         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10,
830                 D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
831         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
832         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
833         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
834         check_pixel_2bpp(lockrect, 0, 0, 0x558c);
835         check_pixel_2bpp(lockrect, 1, 0, 0x5565);
836         check_pixel_2bpp(lockrect, 0, 1, 0xaa95);
837         check_pixel_2bpp(lockrect, 1, 1, 0xffcb);
838         hr = IDirect3DSurface9_UnlockRect(surf);
839         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
840
841         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
842                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
843         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
844         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
845         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
846         check_pixel_2bpp(lockrect, 0, 0, 0xff00);
847         check_pixel_2bpp(lockrect, 1, 0, 0x00ff);
848         check_pixel_2bpp(lockrect, 0, 1, 0xff30);
849         check_pixel_2bpp(lockrect, 1, 1, 0x7f7f);
850         hr = IDirect3DSurface9_UnlockRect(surf);
851         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
852
853         check_release((IUnknown*)surf, 0);
854     }
855
856     /* DXT1, DXT2, DXT3, DXT4, DXT5 */
857     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surf, NULL);
858     if (FAILED(hr))
859         skip("Failed to create R8G8B8 surface, hr %#x.\n", hr);
860     else
861     {
862         hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, dds_24bit, sizeof(dds_24bit), NULL, D3DX_FILTER_NONE, 0, NULL);
863         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
864
865         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_DXT2, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
866         if (FAILED(hr))
867             skip("Failed to create DXT2 surface, hr %#x.\n", hr);
868         else
869         {
870             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
871             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT2 format.\n");
872             check_release((IUnknown*)newsurf, 0);
873         }
874
875         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_DXT3, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
876         if (FAILED(hr))
877             skip("Failed to create DXT3 surface, hr %#x.\n", hr);
878         else
879         {
880             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
881             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT3 format.\n");
882             check_release((IUnknown*)newsurf, 0);
883         }
884
885         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_DXT4, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
886         if (FAILED(hr))
887             skip("Failed to create DXT4 surface, hr %#x.\n", hr);
888         else
889         {
890             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
891             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT4 format.\n");
892             check_release((IUnknown*)newsurf, 0);
893         }
894
895         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_DXT5, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
896         if (FAILED(hr))
897             skip("Failed to create DXT5 surface, hr %#x.\n", hr);
898         else
899         {
900             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
901             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT5 format.\n");
902             check_release((IUnknown*)newsurf, 0);
903         }
904
905         hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_DXT1, D3DPOOL_SYSTEMMEM, &newsurf, NULL);
906         if (FAILED(hr))
907             skip("Failed to create DXT1 surface, hr %#x.\n", hr);
908         else
909         {
910             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
911             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT1 format.\n");
912
913             hr = D3DXLoadSurfaceFromSurface(surf, NULL, NULL, newsurf, NULL, NULL, D3DX_FILTER_NONE, 0);
914             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels from DXT1 format.\n");
915
916             check_release((IUnknown*)newsurf, 0);
917         }
918
919         check_release((IUnknown*)surf, 0);
920     }
921
922     /* cleanup */
923     if(testdummy_ok) DeleteFileA("testdummy.bmp");
924     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
925 }
926
927 static void test_D3DXSaveSurfaceToFile(IDirect3DDevice9 *device)
928 {
929     HRESULT hr;
930     IDirect3DSurface9 *surface;
931     RECT rect;
932     D3DLOCKED_RECT lock_rect;
933     D3DXIMAGE_INFO image_info;
934     const BYTE pixels[] = { 0xff, 0x00, 0x00, 0x00, 0xff, 0x00,
935                             0x00, 0x00, 0xff, 0x00, 0x00, 0xff };
936     DWORD pitch = sizeof(pixels) / 2;
937
938     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
939     if (FAILED(hr)) {
940        skip("Couldn't create surface\n");
941        return;
942     }
943
944     SetRect(&rect, 0, 0, 2, 2);
945     hr = D3DXLoadSurfaceFromMemory(surface, NULL, NULL, pixels, D3DFMT_R8G8B8, pitch, NULL, &rect, D3DX_FILTER_NONE, 0);
946     if (SUCCEEDED(hr)) {
947         hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, NULL);
948         ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
949
950         hr = D3DXLoadSurfaceFromFileA(surface, NULL, NULL, "saved_surface.bmp", NULL, D3DX_FILTER_NONE, 0, &image_info);
951         ok(hr == D3D_OK, "Couldn't load saved surface %#x\n", hr);
952         if (FAILED(hr)) goto next_tests;
953
954         ok(image_info.Width == 2, "Wrong width %u\n", image_info.Width);
955         ok(image_info.Height == 2, "Wrong height %u\n", image_info.Height);
956         ok(image_info.Format == D3DFMT_R8G8B8, "Wrong format %#x\n", image_info.Format);
957         ok(image_info.ImageFileFormat == D3DXIFF_BMP, "Wrong file format %u\n", image_info.ImageFileFormat);
958
959         hr = IDirect3DSurface9_LockRect(surface, &lock_rect, NULL, D3DLOCK_READONLY);
960         ok(hr == D3D_OK, "Couldn't lock surface %#x\n", hr);
961         if (FAILED(hr)) goto next_tests;
962
963         ok(!memcmp(lock_rect.pBits, pixels, pitch), "Pixel data mismatch in first row\n");
964         ok(!memcmp((BYTE *)lock_rect.pBits + lock_rect.Pitch, pixels + pitch, pitch), "Pixel data mismatch in second row\n");
965
966         IDirect3DSurface9_UnlockRect(surface);
967     } else skip("Couldn't fill surface\n");
968
969 next_tests:
970     hr = D3DXSaveSurfaceToFileA(NULL, D3DXIFF_BMP, surface, NULL, NULL);
971     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
972
973     /* PPM and TGA are supported, even though MSDN claims they aren't */
974     todo_wine {
975     hr = D3DXSaveSurfaceToFileA("saved_surface.ppm", D3DXIFF_PPM, surface, NULL, NULL);
976     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
977     hr = D3DXSaveSurfaceToFileA("saved_surface.tga", D3DXIFF_TGA, surface, NULL, NULL);
978     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
979     hr = D3DXSaveSurfaceToFileA("saved_surface.dds", D3DXIFF_DDS, surface, NULL, NULL);
980     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
981     }
982
983     hr = D3DXSaveSurfaceToFileA("saved_surface", D3DXIFF_PFM + 1, surface, NULL, NULL);
984     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
985
986     SetRect(&rect, 0, 0, 4, 4);
987     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
988     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
989     SetRect(&rect, 2, 0, 1, 4);
990     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
991     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
992     SetRect(&rect, 0, 2, 4, 1);
993     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
994     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
995     SetRect(&rect, -1, -1, 2, 2);
996     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
997     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
998     SetRect(&rect, 0, 0, 0, 0);
999     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
1000     /* fails when debug version of d3d9 is used */
1001     ok(hr == D3D_OK || broken(hr == D3DERR_INVALIDCALL), "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
1002
1003     DeleteFileA("saved_surface.bmp");
1004     DeleteFileA("saved_surface.ppm");
1005     DeleteFileA("saved_surface.tga");
1006     DeleteFileA("saved_surface.dds");
1007
1008     IDirect3DSurface9_Release(surface);
1009 }
1010
1011 START_TEST(surface)
1012 {
1013     HWND wnd;
1014     IDirect3D9 *d3d;
1015     IDirect3DDevice9 *device;
1016     D3DPRESENT_PARAMETERS d3dpp;
1017     HRESULT hr;
1018
1019     wnd = CreateWindow("static", "d3dx9_test", 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
1020     if (!wnd) {
1021         skip("Couldn't create application window\n");
1022         return;
1023     }
1024     d3d = Direct3DCreate9(D3D_SDK_VERSION);
1025     if (!d3d) {
1026         skip("Couldn't create IDirect3D9 object\n");
1027         DestroyWindow(wnd);
1028         return;
1029     }
1030
1031     ZeroMemory(&d3dpp, sizeof(d3dpp));
1032     d3dpp.Windowed   = TRUE;
1033     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1034     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_MIXED_VERTEXPROCESSING, &d3dpp, &device);
1035     if(FAILED(hr)) {
1036         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
1037         IDirect3D9_Release(d3d);
1038         DestroyWindow(wnd);
1039         return;
1040     }
1041
1042     test_D3DXGetImageInfo();
1043     test_D3DXLoadSurface(device);
1044     test_D3DXSaveSurfaceToFile(device);
1045
1046     check_release((IUnknown*)device, 0);
1047     check_release((IUnknown*)d3d, 0);
1048     DestroyWindow(wnd);
1049 }