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