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 #define check_release(obj, exp) _check_release(__LINE__, obj, exp)
28 static inline void _check_release(unsigned int line, IUnknown *obj, int exp)
29 {
30     int ref = IUnknown_Release(obj);
31     ok_(__FILE__, line)(ref == exp, "Invalid refcount. Expected %d, got %d\n", exp, ref);
32 }
33 
34 /* 1x1 bmp (1 bpp) */
35 static const unsigned char bmp_1bpp[] = {
36 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
37 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x00,0x00,
38 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
39 0x00,0x00,0x02,0x00,0x00,0x00,0xf1,0xf2,0xf3,0x80,0xf4,0xf5,0xf6,0x81,0x00,0x00,
40 0x00,0x00
41 };
42 
43 /* 1x1 bmp (2 bpp) */
44 static const unsigned char bmp_2bpp[] = {
45 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
46 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x00,0x00,
47 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
48 0x00,0x00,0x02,0x00,0x00,0x00,0xf1,0xf2,0xf3,0x80,0xf4,0xf5,0xf6,0x81,0x00,0x00,
49 0x00,0x00
50 };
51 
52 /* 1x1 bmp (4 bpp) */
53 static const unsigned char bmp_4bpp[] = {
54 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
55 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x04,0x00,0x00,0x00,
56 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
57 0x00,0x00,0x02,0x00,0x00,0x00,0xf1,0xf2,0xf3,0x80,0xf4,0xf5,0xf6,0x81,0x00,0x00,
58 0x00,0x00
59 };
60 
61 /* 1x1 bmp (8 bpp) */
62 static const unsigned char bmp_8bpp[] = {
63 0x42,0x4d,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x28,0x00,
64 0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x08,0x00,0x00,0x00,
65 0x00,0x00,0x04,0x00,0x00,0x00,0x12,0x0b,0x00,0x00,0x12,0x0b,0x00,0x00,0x02,0x00,
66 0x00,0x00,0x02,0x00,0x00,0x00,0xf1,0xf2,0xf3,0x80,0xf4,0xf5,0xf6,0x81,0x00,0x00,
67 0x00,0x00
68 };
69 
70 static const unsigned char png_grayscale[] =
71 {
72     0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49,
73     0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00,
74     0x00, 0x00, 0x00, 0x3a, 0x7e, 0x9b, 0x55, 0x00, 0x00, 0x00, 0x0a, 0x49, 0x44,
75     0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0x0f, 0x00, 0x01, 0x01, 0x01, 0x00, 0x1b,
76     0xb6, 0xee, 0x56, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42,
77     0x60, 0x82
78 };
79 
80 /* 2x2 A8R8G8B8 pixel data */
81 static const unsigned char pixdata[] = {
82 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
83 };
84 
85 /* invalid image file */
86 static const unsigned char noimage[4] = {
87 0x11,0x22,0x33,0x44
88 };
89 
90 /* 2x2 24-bit dds, 2 mipmaps */
91 static const unsigned char dds_24bit[] = {
92 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x0a,0x00,0x02,0x00,0x00,0x00,
93 0x02,0x00,0x00,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,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 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0xff,0x00,
98 0x00,0xff,0x00,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x40,0x00,
99 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
100 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
101 };
102 
103 /* 2x2 16-bit dds, no mipmaps */
104 static const unsigned char dds_16bit[] = {
105 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x08,0x00,0x02,0x00,0x00,0x00,
106 0x02,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
107 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
108 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
109 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
110 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x7c,0x00,0x00,
111 0xe0,0x03,0x00,0x00,0x1f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,
112 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
113 0xff,0x7f,0xff,0x7f,0xff,0x7f,0xff,0x7f
114 };
115 
116 /* 4x4 cube map dds */
117 static const unsigned char dds_cube_map[] = {
118 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x08,0x00,0x04,0x00,0x00,0x00,
119 0x04,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
120 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
121 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
122 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
123 0x04,0x00,0x00,0x00,0x44,0x58,0x54,0x35,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
124 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x00,0x00,
125 0x00,0xfe,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
126 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
127 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
128 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
129 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
130 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
131 0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50
132 };
133 
134 /* 4x4x2 volume map dds, 2 mipmaps */
135 static const unsigned char dds_volume_map[] = {
136 0x44,0x44,0x53,0x20,0x7c,0x00,0x00,0x00,0x07,0x10,0x8a,0x00,0x04,0x00,0x00,0x00,
137 0x04,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x03,0x00,0x00,0x00,
138 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
139 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
140 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
141 0x04,0x00,0x00,0x00,0x44,0x58,0x54,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
142 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x40,0x00,
143 0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
144 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
145 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xef,0x87,0x0f,0x78,0x05,0x05,0x50,0x50,
146 0xff,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x2f,0x7e,0xcf,0x79,0x01,0x54,0x5c,0x5c,
147 0x0f,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x84,0xef,0x7b,0xaa,0xab,0xab,0xab
148 };
149 
150 static HRESULT create_file(const char *filename, const unsigned char *data, const unsigned int size)
151 {
152     DWORD received;
153     HANDLE hfile;
154 
155     hfile = CreateFileA(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
156     if(hfile == INVALID_HANDLE_VALUE) return HRESULT_FROM_WIN32(GetLastError());
157 
158     if(WriteFile(hfile, data, size, &received, NULL))
159     {
160         CloseHandle(hfile);
161         return D3D_OK;
162     }
163 
164     CloseHandle(hfile);
165     return D3DERR_INVALIDCALL;
166 }
167 
168 /* dds_header.flags */
169 #define DDS_CAPS 0x00000001
170 #define DDS_HEIGHT 0x00000002
171 #define DDS_WIDTH 0x00000004
172 #define DDS_PITCH 0x00000008
173 #define DDS_PIXELFORMAT 0x00001000
174 #define DDS_MIPMAPCOUNT 0x00020000
175 #define DDS_LINEARSIZE 0x00080000
176 
177 /* dds_header.caps */
178 #define DDSCAPS_ALPHA    0x00000002
179 #define DDS_CAPS_TEXTURE 0x00001000
180 
181 /* dds_pixel_format.flags */
182 #define DDS_PF_ALPHA 0x00000001
183 #define DDS_PF_ALPHA_ONLY 0x00000002
184 #define DDS_PF_FOURCC 0x00000004
185 #define DDS_PF_RGB 0x00000040
186 #define DDS_PF_LUMINANCE 0x00020000
187 #define DDS_PF_BUMPDUDV 0x00080000
188 
189 struct dds_pixel_format
190 {
191     DWORD size;
192     DWORD flags;
193     DWORD fourcc;
194     DWORD bpp;
195     DWORD rmask;
196     DWORD gmask;
197     DWORD bmask;
198     DWORD amask;
199 };
200 
201 struct dds_header
202 {
203     DWORD size;
204     DWORD flags;
205     DWORD height;
206     DWORD width;
207     DWORD pitch_or_linear_size;
208     DWORD depth;
209     DWORD miplevels;
210     DWORD reserved[11];
211     struct dds_pixel_format pixel_format;
212     DWORD caps;
213     DWORD caps2;
214     DWORD caps3;
215     DWORD caps4;
216     DWORD reserved2;
217 };
218 
219 /* fills dds_header with reasonable default values */
220 static void fill_dds_header(struct dds_header *header)
221 {
222     memset(header, 0, sizeof(*header));
223 
224     header->size = sizeof(*header);
225     header->flags = DDS_CAPS | DDS_WIDTH | DDS_HEIGHT | DDS_PIXELFORMAT;
226     header->height = 4;
227     header->width = 4;
228     header->pixel_format.size = sizeof(header->pixel_format);
229     /* X8R8G8B8 */
230     header->pixel_format.flags = DDS_PF_RGB;
231     header->pixel_format.fourcc = 0;
232     header->pixel_format.bpp = 32;
233     header->pixel_format.rmask = 0xff0000;
234     header->pixel_format.gmask = 0x00ff00;
235     header->pixel_format.bmask = 0x0000ff;
236     header->pixel_format.amask = 0;
237     header->caps = DDS_CAPS_TEXTURE;
238 }
239 
240 #define check_dds_pixel_format(flags, fourcc, bpp, rmask, gmask, bmask, amask, format) \
241         check_dds_pixel_format_(__LINE__, flags, fourcc, bpp, rmask, gmask, bmask, amask, format)
242 static void check_dds_pixel_format_(unsigned int line,
243                                     DWORD flags, DWORD fourcc, DWORD bpp,
244                                     DWORD rmask, DWORD gmask, DWORD bmask, DWORD amask,
245                                     D3DFORMAT expected_format)
246 {
247     HRESULT hr;
248     D3DXIMAGE_INFO info;
249     struct
250     {
251         DWORD magic;
252         struct dds_header header;
253         BYTE data[256];
254     } dds;
255 
256     dds.magic = MAKEFOURCC('D','D','S',' ');
257     fill_dds_header(&dds.header);
258     dds.header.pixel_format.flags = flags;
259     dds.header.pixel_format.fourcc = fourcc;
260     dds.header.pixel_format.bpp = bpp;
261     dds.header.pixel_format.rmask = rmask;
262     dds.header.pixel_format.gmask = gmask;
263     dds.header.pixel_format.bmask = bmask;
264     dds.header.pixel_format.amask = amask;
265     memset(dds.data, 0, sizeof(dds.data));
266 
267     hr = D3DXGetImageInfoFromFileInMemory(&dds, sizeof(dds), &info);
268     ok_(__FILE__, line)(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x for pixel format %#x, expected %#x\n",
269             hr, expected_format, D3D_OK);
270     if (SUCCEEDED(hr))
271     {
272         ok_(__FILE__, line)(info.Format == expected_format, "D3DXGetImageInfoFromFileInMemory returned format %#x, expected %#x\n",
273                 info.Format, expected_format);
274     }
275 }
276 
277 static void test_dds_header_handling(void)
278 {
279     int i;
280     HRESULT hr;
281     D3DXIMAGE_INFO info;
282     struct
283     {
284         DWORD magic;
285         struct dds_header header;
286         BYTE data[4096 * 1024];
287     } *dds;
288 
289     struct
290     {
291         struct dds_pixel_format pixel_format;
292         DWORD flags;
293         DWORD width;
294         DWORD height;
295         DWORD pitch;
296         DWORD miplevels;
297         DWORD pixel_data_size;
298         struct
299         {
300             HRESULT hr;
301             UINT miplevels;
302         }
303         expected;
304     } tests[] = {
305         /* pitch is ignored */
306         { { 32, DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, 0, 4, 4, 0, 0,
307           63 /* pixel data size */, { D3DXERR_INVALIDDATA, 0 } },
308         { { 32, DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, DDS_PITCH, 4, 4, 0 /* pitch */, 0,
309           64, { D3D_OK, 1 } },
310         { { 32, DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, DDS_PITCH, 4, 4, 1 /* pitch */, 0,
311           64, { D3D_OK, 1 } },
312         { { 32, DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, DDS_PITCH, 4, 4, 2 /* pitch */, 0,
313           64, { D3D_OK, 1 } },
314         { { 32, DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, DDS_PITCH, 4, 4, 3 /* pitch */, 0,
315           64, { D3D_OK, 1 } },
316         { { 32, DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, DDS_PITCH, 4, 4, 4 /* pitch */, 0,
317           64, { D3D_OK, 1 } },
318         { { 32, DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, DDS_PITCH, 4, 4, 16 /* pitch */, 0,
319           64, { D3D_OK, 1 } },
320         { { 32, DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, DDS_PITCH, 4, 4, 1024 /* pitch */, 0,
321           64, { D3D_OK, 1 } },
322         { { 32, DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, DDS_PITCH, 4, 4, -1 /* pitch */, 0,
323           64, { D3D_OK, 1 } },
324         /* linear size is ignored */
325         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, 0, 4, 4, 0, 0,
326           7 /* pixel data size */, { D3DXERR_INVALIDDATA, 1 } },
327         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_LINEARSIZE, 4, 4, 0 /* linear size */, 0,
328           8, { D3D_OK, 1 } },
329         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_LINEARSIZE, 4, 4, 1 /* linear size */, 0,
330           8, { D3D_OK, 1 } },
331         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_LINEARSIZE, 4, 4, 2 /* linear size */, 0,
332           8, { D3D_OK, 1 } },
333         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_LINEARSIZE, 4, 4, 9 /* linear size */, 0,
334           8, { D3D_OK, 1 } },
335         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_LINEARSIZE, 4, 4, 16 /* linear size */, 0,
336           8, { D3D_OK, 1 } },
337         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_LINEARSIZE, 4, 4, -1 /* linear size */, 0,
338           8, { D3D_OK, 1 } },
339         /* integer overflows */
340         { { 32, DDS_PF_RGB, 0, 32, 0xff0000, 0x00ff00, 0x0000ff, 0 }, 0, 0x80000000, 0x80000000 /* 0x80000000 * 0x80000000 * 4 = 0 */, 0, 0,
341           64, { D3D_OK, 1 } },
342         { { 32, DDS_PF_RGB, 0, 32, 0xff0000, 0x00ff00, 0x0000ff, 0 }, 0, 0x8000100, 0x800100 /* 0x8000100 * 0x800100 * 4 = 262144 */, 0, 0,
343           64, { D3DXERR_INVALIDDATA, 0 } },
344         { { 32, DDS_PF_RGB, 0, 32, 0xff0000, 0x00ff00, 0x0000ff, 0 }, 0, 0x80000001, 0x80000001 /* 0x80000001 * 0x80000001 * 4 = 4 */, 0, 0,
345           4, { D3D_OK, 1 } },
346         { { 32, DDS_PF_RGB, 0, 32, 0xff0000, 0x00ff00, 0x0000ff, 0 }, 0, 0x80000001, 0x80000001 /* 0x80000001 * 0x80000001 * 4 = 4 */, 0, 0,
347           3 /* pixel data size */, { D3DXERR_INVALIDDATA, 0 } },
348         /* file size is validated */
349         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, 0, 256, 64, 0, 0, 49151, { D3DXERR_INVALIDDATA, 0 } },
350         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, 0, 256, 64, 0, 0, 49152, { D3D_OK, 1 } },
351         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 64, 0, 4, 65279, { D3DXERR_INVALIDDATA, 0 } },
352         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 64, 0, 4, 65280, { D3D_OK, 4 } },
353         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 64, 0, 9, 65540, { D3DXERR_INVALIDDATA, 0 } },
354         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 64, 0, 9, 65541, { D3D_OK, 9 } },
355         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, 0, 256, 256, 0, 0, 196607, { D3DXERR_INVALIDDATA, 0 } },
356         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, 0, 256, 256, 0, 0, 196608, { D3D_OK, 1 } },
357         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, 0, 256, 256, 0, 0, 196609, { D3D_OK, 1 } },
358         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 1, 196607, { D3DXERR_INVALIDDATA, 0 } },
359         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 1, 196608, { D3D_OK, 1 } },
360         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 0, 196607, { D3DXERR_INVALIDDATA, 0 } },
361         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 0, 196608, { D3D_OK, 1 } },
362         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 0, 400000, { D3D_OK, 1 } },
363         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 9, 262142, { D3DXERR_INVALIDDATA, 0 } },
364         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 9, 262143, { D3D_OK, 9 } },
365         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 10, 262145, { D3DXERR_INVALIDDATA, 0 } },
366         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 10, 262146, { D3D_OK, 10 } },
367         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 20, 262175, { D3DXERR_INVALIDDATA, 0 } },
368         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, DDS_MIPMAPCOUNT, 256, 256, 0, 20, 262176, { D3D_OK, 20 } },
369         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, 0, 256, 256, 0, 0, 32767, { D3DXERR_INVALIDDATA, 0 } },
370         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, 0, 256, 256, 0, 0, 32768, { D3D_OK, 1 } },
371         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 0, 32767, { D3DXERR_INVALIDDATA, 0 } },
372         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 0, 32768, { D3D_OK, 1 } },
373         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 9, 43703, { D3DXERR_INVALIDDATA, 0 } },
374         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 9, 43704, { D3D_OK, 9 } },
375         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 20, 43791, { D3DXERR_INVALIDDATA, 0 } },
376         { { 32, DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 20, 43792, { D3D_OK, 20 } },
377         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, 0, 256, 256, 0, 0, 65535, { D3DXERR_INVALIDDATA, 0 } },
378         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, 0, 256, 256, 0, 0, 65536, { D3D_OK, 1 } },
379         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 0, 65535, { D3DXERR_INVALIDDATA, 0 } },
380         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 0, 65536, { D3D_OK, 1 } },
381         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 9, 87407, { D3DXERR_INVALIDDATA, 0 } },
382         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 9, 87408, { D3D_OK, 9 } },
383         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 20, 87583, { D3DXERR_INVALIDDATA, 0 } },
384         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 256, 0, 20, 87584, { D3D_OK, 20 } },
385         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 64, 0, 4, 21759, { D3DXERR_INVALIDDATA, 0 } },
386         { { 32, DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0 }, DDS_MIPMAPCOUNT, 256, 64, 0, 4, 21760, { D3D_OK, 4 } },
387         /* DDS_MIPMAPCOUNT is ignored */
388         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, 0, 256, 256, 0, 0, 262146, { D3D_OK, 1 } },
389         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, 0, 256, 256, 0, 2, 262146, { D3D_OK, 2 } },
390         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, 0, 256, 256, 0, 9, 262146, { D3D_OK, 9 } },
391         { { 32, DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000 }, 0, 256, 256, 0, 10, 262146, { D3D_OK, 10 } },
392     };
393 
394     dds = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dds));
395     if (!dds)
396     {
397         skip("Failed to allocate memory.\n");
398         return;
399     }
400 
401     for (i = 0; i < ARRAY_SIZE(tests); i++)
402     {
403         DWORD file_size = sizeof(dds->magic) + sizeof(dds->header) + tests[i].pixel_data_size;
404         assert(file_size <= sizeof(*dds));
405 
406         dds->magic = MAKEFOURCC('D','D','S',' ');
407         fill_dds_header(&dds->header);
408         dds->header.flags |= tests[i].flags;
409         dds->header.width = tests[i].width;
410         dds->header.height = tests[i].height;
411         dds->header.pitch_or_linear_size = tests[i].pitch;
412         dds->header.miplevels = tests[i].miplevels;
413         dds->header.pixel_format = tests[i].pixel_format;
414 
415         hr = D3DXGetImageInfoFromFileInMemory(dds, file_size, &info);
416         ok(hr == tests[i].expected.hr, "%d: D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n",
417                 i, hr, tests[i].expected.hr);
418         if (SUCCEEDED(hr))
419         {
420             ok(info.MipLevels == tests[i].expected.miplevels, "%d: Got MipLevels %u, expected %u\n",
421                     i, info.MipLevels, tests[i].expected.miplevels);
422         }
423     }
424 
425     HeapFree(GetProcessHeap(), 0, dds);
426 }
427 
428 static void test_D3DXGetImageInfo(void)
429 {
430     HRESULT hr;
431     D3DXIMAGE_INFO info;
432     BOOL testdummy_ok, testbitmap_ok;
433 
434     hr = create_file("testdummy.bmp", noimage, sizeof(noimage));  /* invalid image */
435     testdummy_ok = SUCCEEDED(hr);
436 
437     hr = create_file("testbitmap.bmp", bmp_1bpp, sizeof(bmp_1bpp));  /* valid image */
438     testbitmap_ok = SUCCEEDED(hr);
439 
440     /* D3DXGetImageInfoFromFile */
441     if(testbitmap_ok) {
442         hr = D3DXGetImageInfoFromFileA("testbitmap.bmp", &info);
443         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
444 
445         hr = D3DXGetImageInfoFromFileA("testbitmap.bmp", NULL); /* valid image, second parameter is NULL */
446         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
447     } else skip("Couldn't create \"testbitmap.bmp\"\n");
448 
449     if(testdummy_ok) {
450         hr = D3DXGetImageInfoFromFileA("testdummy.bmp", NULL); /* invalid image, second parameter is NULL */
451         ok(hr == D3D_OK, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3D_OK);
452 
453         hr = D3DXGetImageInfoFromFileA("testdummy.bmp", &info);
454         ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
455     } else skip("Couldn't create \"testdummy.bmp\"\n");
456 
457     hr = D3DXGetImageInfoFromFileA("filedoesnotexist.bmp", &info);
458     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
459 
460     hr = D3DXGetImageInfoFromFileA("filedoesnotexist.bmp", NULL);
461     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
462 
463     hr = D3DXGetImageInfoFromFileA("", &info);
464     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
465 
466     hr = D3DXGetImageInfoFromFileA(NULL, &info);
467     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
468 
469     hr = D3DXGetImageInfoFromFileA(NULL, NULL);
470     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
471 
472 
473     /* D3DXGetImageInfoFromResource */
474     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), &info); /* RT_BITMAP */
475     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
476 
477     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDB_BITMAP_1x1), NULL);
478     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
479 
480     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), &info); /* RT_RCDATA */
481     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
482 
483     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDS_STRING), &info);
484     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
485 
486     hr = D3DXGetImageInfoFromResourceA(NULL, MAKEINTRESOURCEA(IDS_STRING), NULL);
487     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
488 
489     hr = D3DXGetImageInfoFromResourceA(NULL, "resourcedoesnotexist", &info);
490     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
491 
492     hr = D3DXGetImageInfoFromResourceA(NULL, "resourcedoesnotexist", NULL);
493     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
494 
495     hr = D3DXGetImageInfoFromResourceA(NULL, NULL, NULL);
496     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
497 
498 
499     /* D3DXGetImageInfoFromFileInMemory */
500     hr = D3DXGetImageInfoFromFileInMemory(bmp_1bpp, sizeof(bmp_1bpp), &info);
501     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
502 
503     hr = D3DXGetImageInfoFromFileInMemory(bmp_1bpp, sizeof(bmp_1bpp)+5, &info); /* too large size */
504     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
505 
506     hr = D3DXGetImageInfoFromFileInMemory(bmp_1bpp, sizeof(bmp_1bpp), NULL);
507     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
508 
509     hr = D3DXGetImageInfoFromFileInMemory(noimage, sizeof(noimage), NULL);
510     ok(hr == D3D_OK, "D3DXGetImageInfoFromResource returned %#x, expected %#x\n", hr, D3D_OK);
511 
512     hr = D3DXGetImageInfoFromFileInMemory(noimage, sizeof(noimage), &info);
513     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
514 
515     todo_wine {
516         hr = D3DXGetImageInfoFromFileInMemory(bmp_1bpp, sizeof(bmp_1bpp)-1, &info);
517         ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
518     }
519 
520     hr = D3DXGetImageInfoFromFileInMemory(bmp_1bpp+1, sizeof(bmp_1bpp)-1, &info);
521     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
522 
523     hr = D3DXGetImageInfoFromFileInMemory(bmp_1bpp, 0, &info);
524     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
525 
526     hr = D3DXGetImageInfoFromFileInMemory(bmp_1bpp, 0, NULL);
527     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
528 
529     hr = D3DXGetImageInfoFromFileInMemory(noimage, 0, &info);
530     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
531 
532     hr = D3DXGetImageInfoFromFileInMemory(noimage, 0, NULL);
533     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
534 
535     hr = D3DXGetImageInfoFromFileInMemory(NULL, 0, &info);
536     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
537 
538     hr = D3DXGetImageInfoFromFileInMemory(NULL, 4, NULL);
539     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
540 
541     hr = D3DXGetImageInfoFromFileInMemory(NULL, 4, &info);
542     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
543 
544     hr = D3DXGetImageInfoFromFileInMemory(NULL, 0, NULL);
545     ok(hr == D3DERR_INVALIDCALL, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
546 
547     /* test BMP support */
548     hr = D3DXGetImageInfoFromFileInMemory(bmp_1bpp, sizeof(bmp_1bpp), &info);
549     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
550     ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
551     ok(info.Format == D3DFMT_P8, "Got format %u, expected %u\n", info.Format, D3DFMT_P8);
552     hr = D3DXGetImageInfoFromFileInMemory(bmp_2bpp, sizeof(bmp_2bpp), &info);
553     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
554     hr = D3DXGetImageInfoFromFileInMemory(bmp_4bpp, sizeof(bmp_4bpp), &info);
555     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
556     ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
557     ok(info.Format == D3DFMT_P8, "Got format %u, expected %u\n", info.Format, D3DFMT_P8);
558     hr = D3DXGetImageInfoFromFileInMemory(bmp_8bpp, sizeof(bmp_8bpp), &info);
559     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
560     ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
561     ok(info.Format == D3DFMT_P8, "Got format %u, expected %u\n", info.Format, D3DFMT_P8);
562 
563     /* Grayscale PNG */
564     hr = D3DXGetImageInfoFromFileInMemory(png_grayscale, sizeof(png_grayscale), &info);
565     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
566     ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
567     ok(info.Format == D3DFMT_L8, "Got format %u, expected %u\n", info.Format, D3DFMT_L8);
568 
569     /* test DDS support */
570     hr = D3DXGetImageInfoFromFileInMemory(dds_24bit, sizeof(dds_24bit), &info);
571     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
572     if (hr == D3D_OK) {
573         ok(info.Width == 2, "Got width %u, expected 2\n", info.Width);
574         ok(info.Height == 2, "Got height %u, expected 2\n", info.Height);
575         ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
576         ok(info.MipLevels == 2, "Got miplevels %u, expected 2\n", info.MipLevels);
577         ok(info.Format == D3DFMT_R8G8B8, "Got format %#x, expected %#x\n", info.Format, D3DFMT_R8G8B8);
578         ok(info.ResourceType == D3DRTYPE_TEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_TEXTURE);
579         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
580     } else skip("Couldn't get image info from 24-bit DDS file in memory\n");
581 
582     hr = D3DXGetImageInfoFromFileInMemory(dds_16bit, sizeof(dds_16bit), &info);
583     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
584     if (hr == D3D_OK) {
585         ok(info.Width == 2, "Got width %u, expected 2\n", info.Width);
586         ok(info.Height == 2, "Got height %u, expected 2\n", info.Height);
587         ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
588         ok(info.MipLevels == 1, "Got miplevels %u, expected 1\n", info.MipLevels);
589         ok(info.Format == D3DFMT_X1R5G5B5, "Got format %#x, expected %#x\n", info.Format, D3DFMT_X1R5G5B5);
590         ok(info.ResourceType == D3DRTYPE_TEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_TEXTURE);
591         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
592     } else skip("Couldn't get image info from 16-bit DDS file in memory\n");
593 
594     hr = D3DXGetImageInfoFromFileInMemory(dds_cube_map, sizeof(dds_cube_map), &info);
595     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
596     if (hr == D3D_OK) {
597         ok(info.Width == 4, "Got width %u, expected 4\n", info.Width);
598         ok(info.Height == 4, "Got height %u, expected 4\n", info.Height);
599         ok(info.Depth == 1, "Got depth %u, expected 1\n", info.Depth);
600         ok(info.MipLevels == 1, "Got miplevels %u, expected 1\n", info.MipLevels);
601         ok(info.Format == D3DFMT_DXT5, "Got format %#x, expected %#x\n", info.Format, D3DFMT_DXT5);
602         ok(info.ResourceType == D3DRTYPE_CUBETEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_CUBETEXTURE);
603         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
604     } else skip("Couldn't get image info from cube map in memory\n");
605 
606     hr = D3DXGetImageInfoFromFileInMemory(dds_volume_map, sizeof(dds_volume_map), &info);
607     ok(hr == D3D_OK, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
608     if (hr == D3D_OK) {
609         ok(info.Width == 4, "Got width %u, expected 4\n", info.Width);
610         ok(info.Height == 4, "Got height %u, expected 4\n", info.Height);
611         ok(info.Depth == 2, "Got depth %u, expected 2\n", info.Depth);
612         ok(info.MipLevels == 3, "Got miplevels %u, expected 3\n", info.MipLevels);
613         ok(info.Format == D3DFMT_DXT3, "Got format %#x, expected %#x\n", info.Format, D3DFMT_DXT3);
614         ok(info.ResourceType == D3DRTYPE_VOLUMETEXTURE, "Got resource type %#x, expected %#x\n", info.ResourceType, D3DRTYPE_VOLUMETEXTURE);
615         ok(info.ImageFileFormat == D3DXIFF_DDS, "Got image file format %#x, expected %#x\n", info.ImageFileFormat, D3DXIFF_DDS);
616     } else skip("Couldn't get image info from volume map in memory\n");
617 
618     check_dds_pixel_format(DDS_PF_FOURCC, D3DFMT_DXT1, 0, 0, 0, 0, 0, D3DFMT_DXT1);
619     check_dds_pixel_format(DDS_PF_FOURCC, D3DFMT_DXT2, 0, 0, 0, 0, 0, D3DFMT_DXT2);
620     check_dds_pixel_format(DDS_PF_FOURCC, D3DFMT_DXT3, 0, 0, 0, 0, 0, D3DFMT_DXT3);
621     check_dds_pixel_format(DDS_PF_FOURCC, D3DFMT_DXT4, 0, 0, 0, 0, 0, D3DFMT_DXT4);
622     check_dds_pixel_format(DDS_PF_FOURCC, D3DFMT_DXT5, 0, 0, 0, 0, 0, D3DFMT_DXT5);
623     check_dds_pixel_format(DDS_PF_FOURCC, D3DFMT_R8G8_B8G8, 0, 0, 0, 0, 0, D3DFMT_R8G8_B8G8);
624     check_dds_pixel_format(DDS_PF_FOURCC, D3DFMT_G8R8_G8B8, 0, 0, 0, 0, 0, D3DFMT_G8R8_G8B8);
625     check_dds_pixel_format(DDS_PF_FOURCC, D3DFMT_UYVY, 0, 0, 0, 0, 0, D3DFMT_UYVY);
626     check_dds_pixel_format(DDS_PF_FOURCC, D3DFMT_YUY2, 0, 0, 0, 0, 0, D3DFMT_YUY2);
627     check_dds_pixel_format(DDS_PF_RGB, 0, 16, 0xf800, 0x07e0, 0x001f, 0, D3DFMT_R5G6B5);
628     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5);
629     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4);
630     check_dds_pixel_format(DDS_PF_RGB, 0, 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2);
631     check_dds_pixel_format(DDS_PF_ALPHA_ONLY, 0, 8, 0, 0, 0, 0xff, D3DFMT_A8);
632     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2);
633     check_dds_pixel_format(DDS_PF_RGB, 0, 16, 0xf00, 0x0f0, 0x00f, 0, D3DFMT_X4R4G4B4);
634     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10);
635     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10);
636     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8);
637     check_dds_pixel_format(DDS_PF_RGB | DDS_PF_ALPHA, 0, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8);
638     check_dds_pixel_format(DDS_PF_RGB, 0, 32, 0xff0000, 0x00ff00, 0x0000ff, 0, D3DFMT_X8R8G8B8);
639     check_dds_pixel_format(DDS_PF_RGB, 0, 32, 0x0000ff, 0x00ff00, 0xff0000, 0, D3DFMT_X8B8G8R8);
640     check_dds_pixel_format(DDS_PF_RGB, 0, 24, 0xff0000, 0x00ff00, 0x0000ff, 0, D3DFMT_R8G8B8);
641     check_dds_pixel_format(DDS_PF_RGB, 0, 32, 0x0000ffff, 0xffff0000, 0, 0, D3DFMT_G16R16);
642     check_dds_pixel_format(DDS_PF_LUMINANCE, 0, 8, 0xff, 0, 0, 0, D3DFMT_L8);
643     check_dds_pixel_format(DDS_PF_LUMINANCE, 0, 16, 0xffff, 0, 0, 0, D3DFMT_L16);
644     check_dds_pixel_format(DDS_PF_LUMINANCE | DDS_PF_ALPHA, 0, 16, 0x00ff, 0, 0, 0xff00, D3DFMT_A8L8);
645     check_dds_pixel_format(DDS_PF_LUMINANCE | DDS_PF_ALPHA, 0, 8, 0x0f, 0, 0, 0xf0, D3DFMT_A4L4);
646     check_dds_pixel_format(DDS_PF_BUMPDUDV, 0, 16, 0x00ff, 0xff00, 0, 0, D3DFMT_V8U8);
647     check_dds_pixel_format(DDS_PF_BUMPDUDV, 0, 32, 0x0000ffff, 0xffff0000, 0, 0, D3DFMT_V16U16);
648 
649     test_dds_header_handling();
650 
651     hr = D3DXGetImageInfoFromFileInMemory(dds_16bit, sizeof(dds_16bit) - 1, &info);
652     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
653 
654     hr = D3DXGetImageInfoFromFileInMemory(dds_24bit, sizeof(dds_24bit) - 1, &info);
655     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
656 
657     hr = D3DXGetImageInfoFromFileInMemory(dds_cube_map, sizeof(dds_cube_map) - 1, &info);
658     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
659 
660     hr = D3DXGetImageInfoFromFileInMemory(dds_volume_map, sizeof(dds_volume_map) - 1, &info);
661     ok(hr == D3DXERR_INVALIDDATA, "D3DXGetImageInfoFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
662 
663 
664     /* cleanup */
665     if(testdummy_ok) DeleteFileA("testdummy.bmp");
666     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
667 }
668 
669 #define check_pixel_2bpp(lockrect, x, y, color) _check_pixel_2bpp(__LINE__, lockrect, x, y, color)
670 static inline void _check_pixel_2bpp(unsigned int line, const D3DLOCKED_RECT *lockrect, int x, int y, WORD expected_color)
671 {
672     WORD color = ((WORD*)lockrect->pBits)[x + y * lockrect->Pitch / 2];
673     ok_(__FILE__, line)(color == expected_color, "Got color 0x%04x, expected 0x%04x\n", color, expected_color);
674 }
675 
676 #define check_pixel_4bpp(lockrect, x, y, color) _check_pixel_4bpp(__LINE__, lockrect, x, y, color)
677 static inline void _check_pixel_4bpp(unsigned int line, const D3DLOCKED_RECT *lockrect, int x, int y, DWORD expected_color)
678 {
679    DWORD color = ((DWORD*)lockrect->pBits)[x + y * lockrect->Pitch / 4];
680    ok_(__FILE__, line)(color == expected_color, "Got color 0x%08x, expected 0x%08x\n", color, expected_color);
681 }
682 
683 static void test_D3DXLoadSurface(IDirect3DDevice9 *device)
684 {
685     HRESULT hr;
686     BOOL testdummy_ok, testbitmap_ok;
687     IDirect3DTexture9 *tex;
688     IDirect3DSurface9 *surf, *newsurf;
689     RECT rect, destrect;
690     D3DLOCKED_RECT lockrect;
691     const WORD pixdata_a8r3g3b2[] = { 0x57df, 0x98fc, 0xacdd, 0xc891 };
692     const WORD pixdata_a1r5g5b5[] = { 0x46b5, 0x99c8, 0x06a2, 0x9431 };
693     const WORD pixdata_r5g6b5[] = { 0x9ef6, 0x658d, 0x0aee, 0x42ee };
694     const WORD pixdata_a8l8[] = { 0xff00, 0x00ff, 0xff30, 0x7f7f };
695     const DWORD pixdata_g16r16[] = { 0x07d23fbe, 0xdc7f44a4, 0xe4d8976b, 0x9a84fe89 };
696     const DWORD pixdata_a8b8g8r8[] = { 0xc3394cf0, 0x235ae892, 0x09b197fd, 0x8dc32bf6 };
697     const DWORD pixdata_a2r10g10b10[] = { 0x57395aff, 0x5b7668fd, 0xb0d856b5, 0xff2c61d6 };
698 
699     hr = create_file("testdummy.bmp", noimage, sizeof(noimage));  /* invalid image */
700     testdummy_ok = SUCCEEDED(hr);
701 
702     hr = create_file("testbitmap.bmp", bmp_1bpp, sizeof(bmp_1bpp));  /* valid image */
703     testbitmap_ok = SUCCEEDED(hr);
704 
705     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
706     if(FAILED(hr)) {
707         skip("Failed to create a surface (%#x)\n", hr);
708         if(testdummy_ok) DeleteFileA("testdummy.bmp");
709         if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
710         return;
711     }
712 
713     /* D3DXLoadSurfaceFromFile */
714     if(testbitmap_ok) {
715         hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "testbitmap.bmp", NULL, D3DX_DEFAULT, 0, NULL);
716         ok(hr == D3D_OK, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3D_OK);
717 
718         hr = D3DXLoadSurfaceFromFileA(NULL, NULL, NULL, "testbitmap.bmp", NULL, D3DX_DEFAULT, 0, NULL);
719         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
720     } else skip("Couldn't create \"testbitmap.bmp\"\n");
721 
722     if(testdummy_ok) {
723         hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "testdummy.bmp", NULL, D3DX_DEFAULT, 0, NULL);
724         ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
725     } else skip("Couldn't create \"testdummy.bmp\"\n");
726 
727     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
728     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
729 
730     hr = D3DXLoadSurfaceFromFileA(surf, NULL, NULL, "", NULL, D3DX_DEFAULT, 0, NULL);
731     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFile returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
732 
733 
734     /* D3DXLoadSurfaceFromResource */
735     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL,
736             MAKEINTRESOURCEA(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
737     ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
738 
739     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL,
740             MAKEINTRESOURCEA(IDD_BITMAPDATA_1x1), NULL, D3DX_DEFAULT, 0, NULL);
741     ok(hr == D3D_OK, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3D_OK);
742 
743     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0, NULL);
744     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
745 
746     hr = D3DXLoadSurfaceFromResourceA(NULL, NULL, NULL, NULL,
747             MAKEINTRESOURCEA(IDB_BITMAP_1x1), NULL, D3DX_DEFAULT, 0, NULL);
748     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
749 
750     hr = D3DXLoadSurfaceFromResourceA(surf, NULL, NULL, NULL,
751             MAKEINTRESOURCEA(IDS_STRING), NULL, D3DX_DEFAULT, 0, NULL);
752     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromResource returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
753 
754 
755     /* D3DXLoadSurfaceFromFileInMemory */
756     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp_1bpp, sizeof(bmp_1bpp), NULL, D3DX_DEFAULT, 0, NULL);
757     ok(hr == D3D_OK, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
758 
759     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, noimage, sizeof(noimage), NULL, D3DX_DEFAULT, 0, NULL);
760     ok(hr == D3DXERR_INVALIDDATA, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DXERR_INVALIDDATA);
761 
762     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp_1bpp, 0, NULL, D3DX_DEFAULT, 0, NULL);
763     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
764 
765     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, bmp_1bpp, sizeof(bmp_1bpp), NULL, D3DX_DEFAULT, 0, NULL);
766     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
767 
768     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 8, NULL, D3DX_DEFAULT, 0, NULL);
769     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
770 
771     hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
772     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
773 
774     hr = D3DXLoadSurfaceFromFileInMemory(NULL, NULL, NULL, NULL, 0, NULL, D3DX_DEFAULT, 0, NULL);
775     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
776 
777 
778     /* D3DXLoadSurfaceFromMemory */
779     SetRect(&rect, 0, 0, 2, 2);
780 
781     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
782     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
783 
784     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, 0, NULL, &rect, D3DX_FILTER_NONE, 0);
785     ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
786 
787     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, NULL, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
788     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
789 
790     hr = D3DXLoadSurfaceFromMemory(NULL, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
791     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
792 
793     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, NULL, D3DX_DEFAULT, 0);
794     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
795 
796     hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata, D3DFMT_UNKNOWN, sizeof(pixdata), NULL, &rect, D3DX_DEFAULT, 0);
797     ok(hr == E_FAIL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, E_FAIL);
798 
799     SetRect(&destrect, -1, -1, 1, 1); /* destination rect is partially outside texture boundaries */
800     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
801     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
802 
803     SetRect(&destrect, 255, 255, 257, 257); /* destination rect is partially outside texture boundaries */
804     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
805     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
806 
807     SetRect(&destrect, 1, 1, 0, 0); /* left > right, top > bottom */
808     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
809     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
810 
811     SetRect(&destrect, 1, 2, 1, 2); /* left = right, top = bottom */
812     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
813     /* fails when debug version of d3d9 is used */
814     ok(hr == D3D_OK || broken(hr == D3DERR_INVALIDCALL), "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
815 
816     SetRect(&destrect, 257, 257, 257, 257); /* left = right, top = bottom, but invalid values */
817     hr = D3DXLoadSurfaceFromMemory(surf, NULL, &destrect, pixdata, D3DFMT_A8R8G8B8, sizeof(pixdata), NULL, &rect, D3DX_FILTER_NONE, 0);
818     ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
819 
820 
821     /* D3DXLoadSurfaceFromSurface */
822     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 256, 256, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &newsurf, NULL);
823     if(SUCCEEDED(hr)) {
824         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
825         ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3D_OK);
826 
827         hr = D3DXLoadSurfaceFromSurface(NULL, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
828         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
829 
830         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, NULL, NULL, NULL, D3DX_DEFAULT, 0);
831         ok(hr == D3DERR_INVALIDCALL, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
832 
833         check_release((IUnknown*)newsurf, 0);
834     } else skip("Failed to create a second surface\n");
835 
836     hr = IDirect3DDevice9_CreateTexture(device, 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tex, NULL);
837     if (SUCCEEDED(hr))
838     {
839         IDirect3DTexture9_GetSurfaceLevel(tex, 0, &newsurf);
840 
841         hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_DEFAULT, 0);
842         ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x\n", hr, D3D_OK);
843 
844         IDirect3DSurface9_Release(newsurf);
845         IDirect3DTexture9_Release(tex);
846     } else skip("Failed to create texture\n");
847 
848     /* non-lockable render target */
849     hr = IDirect3DDevice9_CreateRenderTarget(device, 256, 256, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &newsurf, NULL);
850     ok(hr == D3D_OK, "Unexpected hr %#x.\n", hr);
851     hr = D3DXLoadSurfaceFromSurface(surf, NULL, NULL, newsurf, NULL, NULL, D3DX_FILTER_NONE, 0);
852     ok(hr == D3D_OK, "Unexpected hr %#x.\n", hr);
853     IDirect3DSurface9_Release(newsurf);
854 
855     /* non-lockable multisampled render target */
856     hr = IDirect3DDevice9_CreateRenderTarget(device, 256, 256, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_2_SAMPLES, 0, FALSE, &newsurf, NULL);
857     if (SUCCEEDED(hr))
858     {
859        hr = D3DXLoadSurfaceFromSurface(surf, NULL, NULL, newsurf, NULL, NULL, D3DX_FILTER_NONE, 0);
860        ok(hr == D3D_OK, "Unexpected hr %#x.\n", hr);
861 
862        IDirect3DSurface9_Release(newsurf);
863     }
864     else
865     {
866         skip("Failed to create multisampled render target.\n");
867     }
868 
869     hr = IDirect3DDevice9_GetRenderTarget(device, 0, &newsurf);
870     ok(hr == D3D_OK, "IDirect3DDevice9_GetRenderTarget returned %#x, expected %#x.\n", hr, D3D_OK);
871 
872     hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
873     ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x.\n", hr, D3D_OK);
874     hr = D3DXLoadSurfaceFromSurface(surf, NULL, NULL, newsurf, NULL, NULL, D3DX_FILTER_NONE, 0);
875     ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x.\n", hr, D3D_OK);
876     hr = D3DXLoadSurfaceFromSurface(surf, NULL, NULL, newsurf, NULL, NULL, D3DX_FILTER_NONE, 0xff000000);
877     ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x.\n", hr, D3D_OK);
878     hr = D3DXLoadSurfaceFromSurface(surf, NULL, NULL, newsurf, NULL, NULL, D3DX_FILTER_TRIANGLE | D3DX_FILTER_MIRROR, 0);
879     ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x.\n", hr, D3D_OK);
880     hr = D3DXLoadSurfaceFromSurface(surf, NULL, NULL, newsurf, NULL, NULL, D3DX_FILTER_LINEAR, 0);
881     ok(hr == D3D_OK, "D3DXLoadSurfaceFromSurface returned %#x, expected %#x.\n", hr, D3D_OK);
882 
883     IDirect3DSurface9_Release(newsurf);
884 
885     check_release((IUnknown*)surf, 0);
886 
887 
888     /* test color conversion */
889     /* A8R8G8B8 */
890     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surf, NULL);
891     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
892     else {
893         PALETTEENTRY palette;
894 
895         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
896         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
897         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
898         check_pixel_4bpp(&lockrect, 0, 0, 0x57dbffff);
899         check_pixel_4bpp(&lockrect, 1, 0, 0x98ffff00);
900         check_pixel_4bpp(&lockrect, 0, 1, 0xacdbff55);
901         check_pixel_4bpp(&lockrect, 1, 1, 0xc8929255);
902         IDirect3DSurface9_UnlockRect(surf);
903 
904         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
905         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
906         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
907         check_pixel_4bpp(&lockrect, 0, 0, 0x008cadad);
908         check_pixel_4bpp(&lockrect, 1, 0, 0xff317342);
909         check_pixel_4bpp(&lockrect, 0, 1, 0x0008ad10);
910         check_pixel_4bpp(&lockrect, 1, 1, 0xff29088c);
911         IDirect3DSurface9_UnlockRect(surf);
912 
913         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
914         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
915         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
916         check_pixel_4bpp(&lockrect, 0, 0, 0xff9cdfb5);
917         check_pixel_4bpp(&lockrect, 1, 0, 0xff63b26b);
918         check_pixel_4bpp(&lockrect, 0, 1, 0xff085d73);
919         check_pixel_4bpp(&lockrect, 1, 1, 0xff425d73);
920         IDirect3DSurface9_UnlockRect(surf);
921 
922         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
923         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
924         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
925         todo_wine {
926             check_pixel_4bpp(&lockrect, 0, 0, 0xff3f08ff);
927         }
928         check_pixel_4bpp(&lockrect, 1, 0, 0xff44dcff);
929         check_pixel_4bpp(&lockrect, 0, 1, 0xff97e4ff);
930         check_pixel_4bpp(&lockrect, 1, 1, 0xfffe9aff);
931         IDirect3DSurface9_UnlockRect(surf);
932 
933         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
934         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
935         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
936         check_pixel_4bpp(&lockrect, 0, 0, 0xc3f04c39);
937         check_pixel_4bpp(&lockrect, 1, 0, 0x2392e85a);
938         check_pixel_4bpp(&lockrect, 0, 1, 0x09fd97b1);
939         check_pixel_4bpp(&lockrect, 1, 1, 0x8df62bc3);
940         IDirect3DSurface9_UnlockRect(surf);
941 
942         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
943         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
944         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
945         check_pixel_4bpp(&lockrect, 0, 0, 0x555c95bf);
946         check_pixel_4bpp(&lockrect, 1, 0, 0x556d663f);
947         check_pixel_4bpp(&lockrect, 0, 1, 0xaac385ad);
948         todo_wine {
949             check_pixel_4bpp(&lockrect, 1, 1, 0xfffcc575);
950         }
951         IDirect3DSurface9_UnlockRect(surf);
952 
953         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
954                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
955         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
956         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
957         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
958         check_pixel_4bpp(&lockrect, 0, 0, 0xff000000);
959         check_pixel_4bpp(&lockrect, 1, 0, 0x00ffffff);
960         check_pixel_4bpp(&lockrect, 0, 1, 0xff303030);
961         check_pixel_4bpp(&lockrect, 1, 1, 0x7f7f7f7f);
962         hr = IDirect3DSurface9_UnlockRect(surf);
963         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
964 
965         /* Test D3DXLoadSurfaceFromMemory with indexed color image */
966         if (0)
967         {
968         /* Crashes on Nvidia Win10. */
969         palette.peRed   = bmp_1bpp[56];
970         palette.peGreen = bmp_1bpp[55];
971         palette.peBlue  = bmp_1bpp[54];
972         palette.peFlags = bmp_1bpp[57]; /* peFlags is the alpha component in DX8 and higher */
973         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, &bmp_1bpp[62],
974                 D3DFMT_P8, 1, (const PALETTEENTRY *)&palette, &rect, D3DX_FILTER_NONE, 0);
975         ok(hr == D3D_OK, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
976         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
977         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x\n", hr);
978         ok(*(DWORD*)lockrect.pBits == 0x80f3f2f1, "Pixel color mismatch: got %#x, expected 0x80f3f2f1\n", *(DWORD*)lockrect.pBits);
979         hr = IDirect3DSurface9_UnlockRect(surf);
980         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x\n", hr);
981         }
982 
983         /* Test D3DXLoadSurfaceFromFileInMemory with indexed color image (alpha is not taken into account for bmp file) */
984         hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, bmp_1bpp, sizeof(bmp_1bpp), NULL, D3DX_FILTER_NONE, 0, NULL);
985         ok(hr == D3D_OK, "D3DXLoadSurfaceFromFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
986         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
987         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x\n", hr);
988         ok(*(DWORD*)lockrect.pBits == 0xfff3f2f1, "Pixel color mismatch: got %#x, expected 0xfff3f2f1\n", *(DWORD*)lockrect.pBits);
989         hr = IDirect3DSurface9_UnlockRect(surf);
990         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x\n", hr);
991 
992         check_release((IUnknown*)surf, 0);
993     }
994 
995     /* A1R5G5B5 */
996     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_A1R5G5B5, D3DPOOL_DEFAULT, &surf, NULL);
997     if(FAILED(hr)) skip("Failed to create a surface (%#x)\n", hr);
998     else {
999         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2, D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
1000         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
1001         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1002         check_pixel_2bpp(&lockrect, 0, 0, 0x6fff);
1003         check_pixel_2bpp(&lockrect, 1, 0, 0xffe0);
1004         check_pixel_2bpp(&lockrect, 0, 1, 0xefea);
1005         check_pixel_2bpp(&lockrect, 1, 1, 0xca4a);
1006         IDirect3DSurface9_UnlockRect(surf);
1007 
1008         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5, D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
1009         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
1010         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1011         check_pixel_2bpp(&lockrect, 0, 0, 0x46b5);
1012         check_pixel_2bpp(&lockrect, 1, 0, 0x99c8);
1013         check_pixel_2bpp(&lockrect, 0, 1, 0x06a2);
1014         check_pixel_2bpp(&lockrect, 1, 1, 0x9431);
1015         IDirect3DSurface9_UnlockRect(surf);
1016 
1017         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5, D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
1018         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
1019         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1020         check_pixel_2bpp(&lockrect, 0, 0, 0xcf76);
1021         check_pixel_2bpp(&lockrect, 1, 0, 0xb2cd);
1022         check_pixel_2bpp(&lockrect, 0, 1, 0x856e);
1023         check_pixel_2bpp(&lockrect, 1, 1, 0xa16e);
1024         IDirect3DSurface9_UnlockRect(surf);
1025 
1026         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16, D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
1027         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
1028         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1029         todo_wine {
1030             check_pixel_2bpp(&lockrect, 0, 0, 0xa03f);
1031         }
1032         check_pixel_2bpp(&lockrect, 1, 0, 0xa37f);
1033         check_pixel_2bpp(&lockrect, 0, 1, 0xcb9f);
1034         check_pixel_2bpp(&lockrect, 1, 1, 0xfe7f);
1035         IDirect3DSurface9_UnlockRect(surf);
1036 
1037         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8, D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
1038         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
1039         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1040         todo_wine {
1041             check_pixel_2bpp(&lockrect, 0, 0, 0xf527);
1042             check_pixel_2bpp(&lockrect, 1, 0, 0x4b8b);
1043         }
1044         check_pixel_2bpp(&lockrect, 0, 1, 0x7e56);
1045         check_pixel_2bpp(&lockrect, 1, 1, 0xf8b8);
1046         IDirect3DSurface9_UnlockRect(surf);
1047 
1048         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10, D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
1049         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
1050         IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1051         check_pixel_2bpp(&lockrect, 0, 0, 0x2e57);
1052         todo_wine {
1053             check_pixel_2bpp(&lockrect, 1, 0, 0x3588);
1054         }
1055         check_pixel_2bpp(&lockrect, 0, 1, 0xe215);
1056         check_pixel_2bpp(&lockrect, 1, 1, 0xff0e);
1057         IDirect3DSurface9_UnlockRect(surf);
1058 
1059         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
1060                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
1061         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
1062         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1063         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
1064         check_pixel_2bpp(&lockrect, 0, 0, 0x8000);
1065         check_pixel_2bpp(&lockrect, 1, 0, 0x7fff);
1066         check_pixel_2bpp(&lockrect, 0, 1, 0x98c6);
1067         check_pixel_2bpp(&lockrect, 1, 1, 0x3def);
1068         hr = IDirect3DSurface9_UnlockRect(surf);
1069         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
1070 
1071         check_release((IUnknown*)surf, 0);
1072     }
1073 
1074     /* A8L8 */
1075     hr = IDirect3DDevice9_CreateTexture(device, 2, 2, 1, 0, D3DFMT_A8L8, D3DPOOL_MANAGED, &tex, NULL);
1076     if (FAILED(hr))
1077         skip("Failed to create A8L8 texture, hr %#x.\n", hr);
1078     else
1079     {
1080         hr = IDirect3DTexture9_GetSurfaceLevel(tex, 0, &surf);
1081         ok(SUCCEEDED(hr), "Failed to get the surface, hr %#x.\n", hr);
1082 
1083         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8r3g3b2,
1084                 D3DFMT_A8R3G3B2, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
1085         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
1086         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1087         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
1088         check_pixel_2bpp(&lockrect, 0, 0, 0x57f7);
1089         check_pixel_2bpp(&lockrect, 1, 0, 0x98ed);
1090         check_pixel_2bpp(&lockrect, 0, 1, 0xaceb);
1091         check_pixel_2bpp(&lockrect, 1, 1, 0xc88d);
1092         hr = IDirect3DSurface9_UnlockRect(surf);
1093         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
1094 
1095         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a1r5g5b5,
1096                 D3DFMT_A1R5G5B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
1097         ok(hr == D3D_OK, "D3DXLoadSurfaceFromMemory returned %#x, expected %#x\n", hr, D3D_OK);
1098         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1099         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
1100         check_pixel_2bpp(&lockrect, 0, 0, 0x00a6);
1101         check_pixel_2bpp(&lockrect, 1, 0, 0xff62);
1102         check_pixel_2bpp(&lockrect, 0, 1, 0x007f);
1103         check_pixel_2bpp(&lockrect, 1, 1, 0xff19);
1104         hr = IDirect3DSurface9_UnlockRect(surf);
1105         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
1106 
1107         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_r5g6b5,
1108                 D3DFMT_R5G6B5, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
1109         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
1110         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1111         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
1112         check_pixel_2bpp(&lockrect, 0, 0, 0xffce);
1113         check_pixel_2bpp(&lockrect, 1, 0, 0xff9c);
1114         check_pixel_2bpp(&lockrect, 0, 1, 0xff4d);
1115         check_pixel_2bpp(&lockrect, 1, 1, 0xff59);
1116         hr = IDirect3DSurface9_UnlockRect(surf);
1117         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
1118 
1119         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_g16r16,
1120                 D3DFMT_G16R16, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
1121         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
1122         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1123         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
1124         check_pixel_2bpp(&lockrect, 0, 0, 0xff25);
1125         check_pixel_2bpp(&lockrect, 1, 0, 0xffbe);
1126         check_pixel_2bpp(&lockrect, 0, 1, 0xffd6);
1127         check_pixel_2bpp(&lockrect, 1, 1, 0xffb6);
1128         hr = IDirect3DSurface9_UnlockRect(surf);
1129         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
1130 
1131         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8b8g8r8,
1132                 D3DFMT_A8B8G8R8, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
1133         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
1134         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1135         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
1136         check_pixel_2bpp(&lockrect, 0, 0, 0xc36d);
1137         check_pixel_2bpp(&lockrect, 1, 0, 0x23cb);
1138         check_pixel_2bpp(&lockrect, 0, 1, 0x09af);
1139         check_pixel_2bpp(&lockrect, 1, 1, 0x8d61);
1140         hr = IDirect3DSurface9_UnlockRect(surf);
1141         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
1142 
1143         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a2r10g10b10,
1144                 D3DFMT_A2R10G10B10, 8, NULL, &rect, D3DX_FILTER_NONE, 0);
1145         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
1146         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1147         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
1148         check_pixel_2bpp(&lockrect, 0, 0, 0x558c);
1149         check_pixel_2bpp(&lockrect, 1, 0, 0x5565);
1150         check_pixel_2bpp(&lockrect, 0, 1, 0xaa95);
1151         check_pixel_2bpp(&lockrect, 1, 1, 0xffcb);
1152         hr = IDirect3DSurface9_UnlockRect(surf);
1153         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
1154 
1155         hr = D3DXLoadSurfaceFromMemory(surf, NULL, NULL, pixdata_a8l8,
1156                 D3DFMT_A8L8, 4, NULL, &rect, D3DX_FILTER_NONE, 0);
1157         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
1158         hr = IDirect3DSurface9_LockRect(surf, &lockrect, NULL, D3DLOCK_READONLY);
1159         ok(SUCCEEDED(hr), "Failed to lock surface, hr %#x.\n", hr);
1160         check_pixel_2bpp(&lockrect, 0, 0, 0xff00);
1161         check_pixel_2bpp(&lockrect, 1, 0, 0x00ff);
1162         check_pixel_2bpp(&lockrect, 0, 1, 0xff30);
1163         check_pixel_2bpp(&lockrect, 1, 1, 0x7f7f);
1164         hr = IDirect3DSurface9_UnlockRect(surf);
1165         ok(SUCCEEDED(hr), "Failed to unlock surface, hr %#x.\n", hr);
1166 
1167         check_release((IUnknown*)surf, 1);
1168         check_release((IUnknown*)tex, 0);
1169     }
1170 
1171     /* DXT1, DXT2, DXT3, DXT4, DXT5 */
1172     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surf, NULL);
1173     if (FAILED(hr))
1174         skip("Failed to create R8G8B8 surface, hr %#x.\n", hr);
1175     else
1176     {
1177         hr = D3DXLoadSurfaceFromFileInMemory(surf, NULL, NULL, dds_24bit, sizeof(dds_24bit), NULL, D3DX_FILTER_NONE, 0, NULL);
1178         ok(SUCCEEDED(hr), "Failed to load surface, hr %#x.\n", hr);
1179 
1180         hr = IDirect3DDevice9_CreateTexture(device, 4, 4, 1, 0, D3DFMT_DXT2, D3DPOOL_SYSTEMMEM, &tex, NULL);
1181         if (FAILED(hr))
1182             skip("Failed to create DXT2 texture, hr %#x.\n", hr);
1183         else
1184         {
1185             hr = IDirect3DTexture9_GetSurfaceLevel(tex, 0, &newsurf);
1186             ok(SUCCEEDED(hr), "Failed to get the surface, hr %#x.\n", hr);
1187             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
1188             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT2 format.\n");
1189             check_release((IUnknown*)newsurf, 1);
1190             check_release((IUnknown*)tex, 0);
1191         }
1192 
1193         hr = IDirect3DDevice9_CreateTexture(device, 4, 4, 1, 0, D3DFMT_DXT3, D3DPOOL_SYSTEMMEM, &tex, NULL);
1194         if (FAILED(hr))
1195             skip("Failed to create DXT3 texture, hr %#x.\n", hr);
1196         else
1197         {
1198             hr = IDirect3DTexture9_GetSurfaceLevel(tex, 0, &newsurf);
1199             ok(SUCCEEDED(hr), "Failed to get the surface, hr %#x.\n", hr);
1200             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
1201             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT3 format.\n");
1202             check_release((IUnknown*)newsurf, 1);
1203             check_release((IUnknown*)tex, 0);
1204         }
1205 
1206         hr = IDirect3DDevice9_CreateTexture(device, 4, 4, 1, 0, D3DFMT_DXT4, D3DPOOL_SYSTEMMEM, &tex, NULL);
1207         if (FAILED(hr))
1208             skip("Failed to create DXT4 texture, hr %#x.\n", hr);
1209         else
1210         {
1211             hr = IDirect3DTexture9_GetSurfaceLevel(tex, 0, &newsurf);
1212             ok(SUCCEEDED(hr), "Failed to get the surface, hr %#x.\n", hr);
1213             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
1214             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT4 format.\n");
1215             check_release((IUnknown*)newsurf, 1);
1216             check_release((IUnknown*)tex, 0);
1217         }
1218 
1219         hr = IDirect3DDevice9_CreateTexture(device, 4, 4, 1, 0, D3DFMT_DXT5, D3DPOOL_SYSTEMMEM, &tex, NULL);
1220         if (FAILED(hr))
1221             skip("Failed to create DXT5 texture, hr %#x.\n", hr);
1222         else
1223         {
1224             hr = IDirect3DTexture9_GetSurfaceLevel(tex, 0, &newsurf);
1225             ok(SUCCEEDED(hr), "Failed to get the surface, hr %#x.\n", hr);
1226             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
1227             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT5 format.\n");
1228             check_release((IUnknown*)newsurf, 1);
1229             check_release((IUnknown*)tex, 0);
1230         }
1231 
1232         hr = IDirect3DDevice9_CreateTexture(device, 4, 4, 1, 0, D3DFMT_DXT1, D3DPOOL_SYSTEMMEM, &tex, NULL);
1233         if (FAILED(hr))
1234             skip("Failed to create DXT1 texture, hr %#x.\n", hr);
1235         else
1236         {
1237             hr = IDirect3DTexture9_GetSurfaceLevel(tex, 0, &newsurf);
1238             ok(SUCCEEDED(hr), "Failed to get the surface, hr %#x.\n", hr);
1239             hr = D3DXLoadSurfaceFromSurface(newsurf, NULL, NULL, surf, NULL, NULL, D3DX_FILTER_NONE, 0);
1240             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels to DXT1 format.\n");
1241 
1242             hr = D3DXLoadSurfaceFromSurface(surf, NULL, NULL, newsurf, NULL, NULL, D3DX_FILTER_NONE, 0);
1243             todo_wine ok(SUCCEEDED(hr), "Failed to convert pixels from DXT1 format.\n");
1244 
1245             check_release((IUnknown*)newsurf, 1);
1246             check_release((IUnknown*)tex, 0);
1247         }
1248 
1249         check_release((IUnknown*)surf, 0);
1250     }
1251 
1252     /* cleanup */
1253     if(testdummy_ok) DeleteFileA("testdummy.bmp");
1254     if(testbitmap_ok) DeleteFileA("testbitmap.bmp");
1255 }
1256 
1257 static void test_D3DXSaveSurfaceToFileInMemory(IDirect3DDevice9 *device)
1258 {
1259     static const struct
1260     {
1261         DWORD usage;
1262         D3DPOOL pool;
1263     }
1264     test_access_types[] =
1265     {
1266         {0,  D3DPOOL_MANAGED},
1267         {0,  D3DPOOL_DEFAULT},
1268         {D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT},
1269     };
1270 
1271     struct
1272     {
1273          DWORD magic;
1274          struct dds_header header;
1275          BYTE *data;
1276     } *dds;
1277     IDirect3DSurface9 *surface;
1278     IDirect3DTexture9 *texture;
1279     ID3DXBuffer *buffer;
1280     unsigned int i;
1281     HRESULT hr;
1282     RECT rect;
1283 
1284     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 4, 4, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
1285     if (FAILED(hr)) {
1286        skip("Couldn't create surface\n");
1287        return;
1288     }
1289 
1290     SetRectEmpty(&rect);
1291     hr = D3DXSaveSurfaceToFileInMemory(&buffer, D3DXIFF_BMP, surface, NULL, &rect);
1292     /* fails with the debug version of d3d9 */
1293     ok(hr == D3D_OK || broken(hr == D3DERR_INVALIDCALL), "D3DXSaveSurfaceToFileInMemory returned %#x, expected %#x\n", hr, D3D_OK);
1294     if (SUCCEEDED(hr)) {
1295         DWORD size = ID3DXBuffer_GetBufferSize(buffer);
1296         ok(size > 0, "ID3DXBuffer_GetBufferSize returned %u, expected > 0\n", size);
1297         ID3DXBuffer_Release(buffer);
1298     }
1299 
1300     SetRectEmpty(&rect);
1301     hr = D3DXSaveSurfaceToFileInMemory(&buffer, D3DXIFF_DDS, surface, NULL, &rect);
1302     todo_wine ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1303     if (SUCCEEDED(hr))
1304     {
1305         dds = ID3DXBuffer_GetBufferPointer(buffer);
1306 
1307         ok(dds->magic == MAKEFOURCC('D','D','S',' '), "Got unexpected DDS signature %#x.\n", dds->magic);
1308         ok(dds->header.size == sizeof(dds->header), "Got unexpected DDS size %u.\n", dds->header.size);
1309         ok(!dds->header.height, "Got unexpected height %u.\n", dds->header.height);
1310         ok(!dds->header.width, "Got unexpected width %u.\n", dds->header.width);
1311         ok(!dds->header.depth, "Got unexpected depth %u.\n", dds->header.depth);
1312         ok(!dds->header.miplevels, "Got unexpected miplevels %u.\n", dds->header.miplevels);
1313         ok(!dds->header.pitch_or_linear_size, "Got unexpected pitch_or_linear_size %u.\n", dds->header.pitch_or_linear_size);
1314         ok(dds->header.caps == (DDS_CAPS_TEXTURE | DDSCAPS_ALPHA), "Got unexpected caps %#x.\n", dds->header.caps);
1315         ok(dds->header.flags == (DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PIXELFORMAT),
1316                 "Got unexpected flags %#x.\n", dds->header.flags);
1317         ID3DXBuffer_Release(buffer);
1318     }
1319 
1320     hr = D3DXSaveSurfaceToFileInMemory(&buffer, D3DXIFF_DDS, surface, NULL, NULL);
1321     ok(hr == D3D_OK, "Got unexpected hr %#x.\n", hr);
1322 
1323     dds = ID3DXBuffer_GetBufferPointer(buffer);
1324     ok(dds->magic == MAKEFOURCC('D','D','S',' '), "Got unexpected DDS signature %#x.\n", dds->magic);
1325     ok(dds->header.size == sizeof(dds->header), "Got unexpected DDS size %u.\n", dds->header.size);
1326     ok(dds->header.height == 4, "Got unexpected height %u.\n", dds->header.height);
1327     ok(dds->header.width == 4, "Got unexpected width %u.\n", dds->header.width);
1328     ok(!dds->header.depth, "Got unexpected depth %u.\n", dds->header.depth);
1329     ok(!dds->header.miplevels, "Got unexpected miplevels %u.\n", dds->header.miplevels);
1330     ok(!dds->header.pitch_or_linear_size, "Got unexpected pitch_or_linear_size %u.\n", dds->header.pitch_or_linear_size);
1331     todo_wine ok(dds->header.caps == (DDS_CAPS_TEXTURE | DDSCAPS_ALPHA), "Got unexpected caps %#x.\n", dds->header.caps);
1332     ok(dds->header.flags == (DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PIXELFORMAT),
1333             "Got unexpected flags %#x.\n", dds->header.flags);
1334     ID3DXBuffer_Release(buffer);
1335 
1336     IDirect3DSurface9_Release(surface);
1337 
1338     for (i = 0; i < ARRAY_SIZE(test_access_types); ++i)
1339     {
1340         hr = IDirect3DDevice9_CreateTexture(device, 4, 4, 0, test_access_types[i].usage,
1341                 D3DFMT_A8R8G8B8, test_access_types[i].pool, &texture, NULL);
1342         ok(hr == D3D_OK, "Unexpected hr %#x, i %u.\n", hr, i);
1343 
1344         hr = IDirect3DTexture9_GetSurfaceLevel(texture, 0, &surface);
1345         ok(hr == D3D_OK, "Unexpected hr %#x, i %u.\n", hr, i);
1346 
1347         hr = D3DXSaveSurfaceToFileInMemory(&buffer, D3DXIFF_DDS, surface, NULL, NULL);
1348         ok(hr == D3D_OK, "Unexpected hr %#x, i %u.\n", hr, i);
1349         ID3DXBuffer_Release(buffer);
1350 
1351         hr = D3DXSaveSurfaceToFileInMemory(&buffer, D3DXIFF_BMP, surface, NULL, NULL);
1352         ok(hr == D3D_OK, "Unexpected hr %#x, i %u.\n", hr, i);
1353         ID3DXBuffer_Release(buffer);
1354 
1355         IDirect3DSurface9_Release(surface);
1356         IDirect3DTexture9_Release(texture);
1357     }
1358 }
1359 
1360 static void test_D3DXSaveSurfaceToFile(IDirect3DDevice9 *device)
1361 {
1362     HRESULT hr;
1363     IDirect3DSurface9 *surface;
1364     RECT rect;
1365     D3DLOCKED_RECT lock_rect;
1366     D3DXIMAGE_INFO image_info;
1367     const BYTE pixels[] = { 0xff, 0x00, 0x00, 0x00, 0xff, 0x00,
1368                             0x00, 0x00, 0xff, 0x00, 0x00, 0xff };
1369     DWORD pitch = sizeof(pixels) / 2;
1370 
1371     hr = IDirect3DDevice9_CreateOffscreenPlainSurface(device, 2, 2, D3DFMT_R8G8B8, D3DPOOL_SCRATCH, &surface, NULL);
1372     if (FAILED(hr)) {
1373        skip("Couldn't create surface\n");
1374        return;
1375     }
1376 
1377     SetRect(&rect, 0, 0, 2, 2);
1378     hr = D3DXLoadSurfaceFromMemory(surface, NULL, NULL, pixels, D3DFMT_R8G8B8, pitch, NULL, &rect, D3DX_FILTER_NONE, 0);
1379     if (SUCCEEDED(hr)) {
1380         hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, NULL);
1381         ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
1382 
1383         hr = D3DXLoadSurfaceFromFileA(surface, NULL, NULL, "saved_surface.bmp", NULL, D3DX_FILTER_NONE, 0, &image_info);
1384         ok(hr == D3D_OK, "Couldn't load saved surface %#x\n", hr);
1385         if (FAILED(hr)) goto next_tests;
1386 
1387         ok(image_info.Width == 2, "Wrong width %u\n", image_info.Width);
1388         ok(image_info.Height == 2, "Wrong height %u\n", image_info.Height);
1389         ok(image_info.Format == D3DFMT_R8G8B8, "Wrong format %#x\n", image_info.Format);
1390         ok(image_info.ImageFileFormat == D3DXIFF_BMP, "Wrong file format %u\n", image_info.ImageFileFormat);
1391 
1392         hr = IDirect3DSurface9_LockRect(surface, &lock_rect, NULL, D3DLOCK_READONLY);
1393         ok(hr == D3D_OK, "Couldn't lock surface %#x\n", hr);
1394         if (FAILED(hr)) goto next_tests;
1395 
1396         ok(!memcmp(lock_rect.pBits, pixels, pitch), "Pixel data mismatch in first row\n");
1397         ok(!memcmp((BYTE *)lock_rect.pBits + lock_rect.Pitch, pixels + pitch, pitch), "Pixel data mismatch in second row\n");
1398 
1399         IDirect3DSurface9_UnlockRect(surface);
1400     } else skip("Couldn't fill surface\n");
1401 
1402 next_tests:
1403     hr = D3DXSaveSurfaceToFileA(NULL, D3DXIFF_BMP, surface, NULL, NULL);
1404     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1405 
1406     /* PPM and TGA are supported, even though MSDN claims they aren't */
1407     todo_wine {
1408     hr = D3DXSaveSurfaceToFileA("saved_surface.ppm", D3DXIFF_PPM, surface, NULL, NULL);
1409     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
1410     hr = D3DXSaveSurfaceToFileA("saved_surface.tga", D3DXIFF_TGA, surface, NULL, NULL);
1411     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
1412     }
1413 
1414     hr = D3DXSaveSurfaceToFileA("saved_surface.dds", D3DXIFF_DDS, surface, NULL, NULL);
1415     ok(hr == D3D_OK, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
1416     if (SUCCEEDED(hr)) {
1417         hr = D3DXLoadSurfaceFromFileA(surface, NULL, NULL, "saved_surface.dds", NULL, D3DX_FILTER_NONE, 0, &image_info);
1418         ok(hr == D3D_OK, "Couldn't load saved surface %#x\n", hr);
1419 
1420         if (SUCCEEDED(hr)) {
1421             ok(image_info.Width == 2, "Wrong width %u\n", image_info.Width);
1422             ok(image_info.Format == D3DFMT_R8G8B8, "Wrong format %#x\n", image_info.Format);
1423             ok(image_info.ImageFileFormat == D3DXIFF_DDS, "Wrong file format %u\n", image_info.ImageFileFormat);
1424 
1425             hr = IDirect3DSurface9_LockRect(surface, &lock_rect, NULL, D3DLOCK_READONLY);
1426             ok(hr == D3D_OK, "Couldn't lock surface %#x\n", hr);
1427             if (SUCCEEDED(hr)) {
1428                 ok(!memcmp(lock_rect.pBits, pixels, pitch), "Pixel data mismatch in first row\n");
1429                 ok(!memcmp((BYTE *)lock_rect.pBits + lock_rect.Pitch, pixels + pitch, pitch), "Pixel data mismatch in second row\n");
1430                 IDirect3DSurface9_UnlockRect(surface);
1431             }
1432         }
1433     } else skip("Couldn't save surface\n");
1434 
1435     hr = D3DXSaveSurfaceToFileA("saved_surface", D3DXIFF_PFM + 1, surface, NULL, NULL);
1436     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1437 
1438     SetRect(&rect, 0, 0, 4, 4);
1439     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
1440     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1441     SetRect(&rect, 2, 0, 1, 4);
1442     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
1443     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1444     SetRect(&rect, 0, 2, 4, 1);
1445     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
1446     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1447     SetRect(&rect, -1, -1, 2, 2);
1448     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
1449     ok(hr == D3DERR_INVALIDCALL, "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3DERR_INVALIDCALL);
1450     SetRectEmpty(&rect);
1451     hr = D3DXSaveSurfaceToFileA("saved_surface.bmp", D3DXIFF_BMP, surface, NULL, &rect);
1452     /* fails when debug version of d3d9 is used */
1453     ok(hr == D3D_OK || broken(hr == D3DERR_INVALIDCALL), "D3DXSaveSurfaceToFileA returned %#x, expected %#x\n", hr, D3D_OK);
1454 
1455     DeleteFileA("saved_surface.bmp");
1456     DeleteFileA("saved_surface.ppm");
1457     DeleteFileA("saved_surface.tga");
1458     DeleteFileA("saved_surface.dds");
1459 
1460     IDirect3DSurface9_Release(surface);
1461 }
1462 
1463 START_TEST(surface)
1464 {
1465     HWND wnd;
1466     IDirect3D9 *d3d;
1467     IDirect3DDevice9 *device;
1468     D3DPRESENT_PARAMETERS d3dpp;
1469     HRESULT hr;
1470 
1471     if (!(wnd = CreateWindowA("static", "d3dx9_test", WS_OVERLAPPEDWINDOW, 0, 0,
1472             640, 480, NULL, NULL, NULL, NULL)))
1473     {
1474         skip("Couldn't create application window\n");
1475         return;
1476     }
1477     d3d = Direct3DCreate9(D3D_SDK_VERSION);
1478     if (!d3d) {
1479         skip("Couldn't create IDirect3D9 object\n");
1480         DestroyWindow(wnd);
1481         return;
1482     }
1483 
1484     ZeroMemory(&d3dpp, sizeof(d3dpp));
1485     d3dpp.Windowed   = TRUE;
1486     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
1487     hr = IDirect3D9_CreateDevice(d3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device);
1488     if(FAILED(hr)) {
1489         skip("Failed to create IDirect3DDevice9 object %#x\n", hr);
1490         IDirect3D9_Release(d3d);
1491         DestroyWindow(wnd);
1492         return;
1493     }
1494 
1495     test_D3DXGetImageInfo();
1496     test_D3DXLoadSurface(device);
1497     test_D3DXSaveSurfaceToFileInMemory(device);
1498     test_D3DXSaveSurfaceToFile(device);
1499 
1500     check_release((IUnknown*)device, 0);
1501     check_release((IUnknown*)d3d, 0);
1502     DestroyWindow(wnd);
1503 }
1504