f9c4b579f55474b031ca9bb4b551f1ddd6a90fd4
[reactos.git] / dll / directx / wine / d3dx9_36 / surface.c
1 /*
2 * Copyright (C) 2009-2010 Tony Wasserka
3 * Copyright (C) 2012 Józef Kucia
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 *
19 */
20
21 #include "d3dx9_36_private.h"
22
23 #include <ole2.h>
24 #include <wine/wined3d.h>
25
26 #include <initguid.h>
27 #include <wincodec.h>
28
29 /* Wine-specific WIC GUIDs */
30 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
31
32 static const struct
33 {
34 const GUID *wic_guid;
35 D3DFORMAT d3dformat;
36 } wic_pixel_formats[] = {
37 { &GUID_WICPixelFormat8bppIndexed, D3DFMT_P8 },
38 { &GUID_WICPixelFormat1bppIndexed, D3DFMT_P8 },
39 { &GUID_WICPixelFormat4bppIndexed, D3DFMT_P8 },
40 { &GUID_WICPixelFormat8bppGray, D3DFMT_L8 },
41 { &GUID_WICPixelFormat16bppBGR555, D3DFMT_X1R5G5B5 },
42 { &GUID_WICPixelFormat16bppBGR565, D3DFMT_R5G6B5 },
43 { &GUID_WICPixelFormat24bppBGR, D3DFMT_R8G8B8 },
44 { &GUID_WICPixelFormat32bppBGR, D3DFMT_X8R8G8B8 },
45 { &GUID_WICPixelFormat32bppBGRA, D3DFMT_A8R8G8B8 }
46 };
47
48 static D3DFORMAT wic_guid_to_d3dformat(const GUID *guid)
49 {
50 unsigned int i;
51
52 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
53 {
54 if (IsEqualGUID(wic_pixel_formats[i].wic_guid, guid))
55 return wic_pixel_formats[i].d3dformat;
56 }
57
58 return D3DFMT_UNKNOWN;
59 }
60
61 static const GUID *d3dformat_to_wic_guid(D3DFORMAT format)
62 {
63 unsigned int i;
64
65 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
66 {
67 if (wic_pixel_formats[i].d3dformat == format)
68 return wic_pixel_formats[i].wic_guid;
69 }
70
71 return NULL;
72 }
73
74 /* dds_header.flags */
75 #define DDS_CAPS 0x1
76 #define DDS_HEIGHT 0x2
77 #define DDS_WIDTH 0x4
78 #define DDS_PITCH 0x8
79 #define DDS_PIXELFORMAT 0x1000
80 #define DDS_MIPMAPCOUNT 0x20000
81 #define DDS_LINEARSIZE 0x80000
82 #define DDS_DEPTH 0x800000
83
84 /* dds_header.caps */
85 #define DDS_CAPS_COMPLEX 0x8
86 #define DDS_CAPS_TEXTURE 0x1000
87 #define DDS_CAPS_MIPMAP 0x400000
88
89 /* dds_header.caps2 */
90 #define DDS_CAPS2_CUBEMAP 0x200
91 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
92 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
93 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
94 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
95 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
96 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
97 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
98 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
99 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
100 #define DDS_CAPS2_VOLUME 0x200000
101
102 /* dds_pixel_format.flags */
103 #define DDS_PF_ALPHA 0x1
104 #define DDS_PF_ALPHA_ONLY 0x2
105 #define DDS_PF_FOURCC 0x4
106 #define DDS_PF_RGB 0x40
107 #define DDS_PF_YUV 0x200
108 #define DDS_PF_LUMINANCE 0x20000
109 #define DDS_PF_BUMPLUMINANCE 0x40000
110 #define DDS_PF_BUMPDUDV 0x80000
111
112 struct dds_pixel_format
113 {
114 DWORD size;
115 DWORD flags;
116 DWORD fourcc;
117 DWORD bpp;
118 DWORD rmask;
119 DWORD gmask;
120 DWORD bmask;
121 DWORD amask;
122 };
123
124 struct dds_header
125 {
126 DWORD signature;
127 DWORD size;
128 DWORD flags;
129 DWORD height;
130 DWORD width;
131 DWORD pitch_or_linear_size;
132 DWORD depth;
133 DWORD miplevels;
134 DWORD reserved[11];
135 struct dds_pixel_format pixel_format;
136 DWORD caps;
137 DWORD caps2;
138 DWORD caps3;
139 DWORD caps4;
140 DWORD reserved2;
141 };
142
143 static D3DFORMAT dds_fourcc_to_d3dformat(DWORD fourcc)
144 {
145 unsigned int i;
146 static const DWORD known_fourcc[] = {
147 D3DFMT_UYVY,
148 D3DFMT_YUY2,
149 D3DFMT_R8G8_B8G8,
150 D3DFMT_G8R8_G8B8,
151 D3DFMT_DXT1,
152 D3DFMT_DXT2,
153 D3DFMT_DXT3,
154 D3DFMT_DXT4,
155 D3DFMT_DXT5,
156 D3DFMT_R16F,
157 D3DFMT_G16R16F,
158 D3DFMT_A16B16G16R16F,
159 D3DFMT_R32F,
160 D3DFMT_G32R32F,
161 D3DFMT_A32B32G32R32F,
162 };
163
164 for (i = 0; i < sizeof(known_fourcc) / sizeof(known_fourcc[0]); i++)
165 {
166 if (known_fourcc[i] == fourcc)
167 return fourcc;
168 }
169
170 WARN("Unknown FourCC %#x\n", fourcc);
171 return D3DFMT_UNKNOWN;
172 }
173
174 static const struct {
175 DWORD bpp;
176 DWORD rmask;
177 DWORD gmask;
178 DWORD bmask;
179 DWORD amask;
180 D3DFORMAT format;
181 } rgb_pixel_formats[] = {
182 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 },
183 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 },
184 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 },
185 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 },
186 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 },
187 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 },
188 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 },
189 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 },
190 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 },
191 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 },
192 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 },
193 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 },
194 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 },
195 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8 },
196 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8 },
197 };
198
199 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
200 {
201 unsigned int i;
202
203 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
204 {
205 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
206 && rgb_pixel_formats[i].rmask == pixel_format->rmask
207 && rgb_pixel_formats[i].gmask == pixel_format->gmask
208 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
209 {
210 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
211 return rgb_pixel_formats[i].format;
212 if (rgb_pixel_formats[i].amask == 0)
213 return rgb_pixel_formats[i].format;
214 }
215 }
216
217 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
218 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
219 return D3DFMT_UNKNOWN;
220 }
221
222 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
223 {
224 if (pixel_format->bpp == 8)
225 {
226 if (pixel_format->rmask == 0xff)
227 return D3DFMT_L8;
228 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
229 return D3DFMT_A4L4;
230 }
231 if (pixel_format->bpp == 16)
232 {
233 if (pixel_format->rmask == 0xffff)
234 return D3DFMT_L16;
235 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
236 return D3DFMT_A8L8;
237 }
238
239 WARN("Unknown luminance pixel format (bpp %u, l %#x, a %#x)\n",
240 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
241 return D3DFMT_UNKNOWN;
242 }
243
244 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
245 {
246 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
247 return D3DFMT_A8;
248
249 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format->bpp, pixel_format->rmask);
250 return D3DFMT_UNKNOWN;
251 }
252
253 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
254 {
255 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00)
256 return D3DFMT_V8U8;
257 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000)
258 return D3DFMT_V16U16;
259
260 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format->bpp,
261 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
262 return D3DFMT_UNKNOWN;
263 }
264
265 static D3DFORMAT dds_bump_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
266 {
267 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x000000ff && pixel_format->gmask == 0x0000ff00
268 && pixel_format->bmask == 0x00ff0000)
269 return D3DFMT_X8L8V8U8;
270
271 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format->bpp,
272 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
273 return D3DFMT_UNKNOWN;
274 }
275
276 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
277 {
278 TRACE("pixel_format: size %u, flags %#x, fourcc %#x, bpp %u.\n", pixel_format->size,
279 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp);
280 TRACE("rmask %#x, gmask %#x, bmask %#x, amask %#x.\n", pixel_format->rmask, pixel_format->gmask,
281 pixel_format->bmask, pixel_format->amask);
282
283 if (pixel_format->flags & DDS_PF_FOURCC)
284 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
285 if (pixel_format->flags & DDS_PF_RGB)
286 return dds_rgb_to_d3dformat(pixel_format);
287 if (pixel_format->flags & DDS_PF_LUMINANCE)
288 return dds_luminance_to_d3dformat(pixel_format);
289 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
290 return dds_alpha_to_d3dformat(pixel_format);
291 if (pixel_format->flags & DDS_PF_BUMPDUDV)
292 return dds_bump_to_d3dformat(pixel_format);
293 if (pixel_format->flags & DDS_PF_BUMPLUMINANCE)
294 return dds_bump_luminance_to_d3dformat(pixel_format);
295
296 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n",
297 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
298 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
299 return D3DFMT_UNKNOWN;
300 }
301
302 static HRESULT d3dformat_to_dds_pixel_format(struct dds_pixel_format *pixel_format, D3DFORMAT d3dformat)
303 {
304 unsigned int i;
305
306 memset(pixel_format, 0, sizeof(*pixel_format));
307
308 pixel_format->size = sizeof(*pixel_format);
309
310 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
311 {
312 if (rgb_pixel_formats[i].format == d3dformat)
313 {
314 pixel_format->flags |= DDS_PF_RGB;
315 pixel_format->bpp = rgb_pixel_formats[i].bpp;
316 pixel_format->rmask = rgb_pixel_formats[i].rmask;
317 pixel_format->gmask = rgb_pixel_formats[i].gmask;
318 pixel_format->bmask = rgb_pixel_formats[i].bmask;
319 pixel_format->amask = rgb_pixel_formats[i].amask;
320 if (pixel_format->amask) pixel_format->flags |= DDS_PF_ALPHA;
321 return D3D_OK;
322 }
323 }
324
325 /* Reuse dds_fourcc_to_d3dformat as D3DFORMAT and FOURCC are DWORD with same values */
326 if (dds_fourcc_to_d3dformat(d3dformat) != D3DFMT_UNKNOWN)
327 {
328 pixel_format->flags |= DDS_PF_FOURCC;
329 pixel_format->fourcc = d3dformat;
330 return D3D_OK;
331 }
332
333 WARN("Unknown pixel format %#x\n", d3dformat);
334 return E_NOTIMPL;
335 }
336
337 static HRESULT calculate_dds_surface_size(D3DFORMAT format, UINT width, UINT height,
338 UINT *pitch, UINT *size)
339 {
340 const struct pixel_format_desc *format_desc = get_format_info(format);
341 if (format_desc->type == FORMAT_UNKNOWN)
342 return E_NOTIMPL;
343
344 if (format_desc->block_width != 1 || format_desc->block_height != 1)
345 {
346 *pitch = format_desc->block_byte_count
347 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
348 *size = *pitch
349 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
350 }
351 else
352 {
353 *pitch = width * format_desc->bytes_per_pixel;
354 *size = *pitch * height;
355 }
356
357 return D3D_OK;
358 }
359
360 static UINT calculate_dds_file_size(D3DFORMAT format, UINT width, UINT height, UINT depth,
361 UINT miplevels, UINT faces)
362 {
363 UINT i, file_size = 0;
364
365 for (i = 0; i < miplevels; i++)
366 {
367 UINT pitch, size = 0;
368 calculate_dds_surface_size(format, width, height, &pitch, &size);
369 size *= depth;
370 file_size += size;
371 width = max(1, width / 2);
372 height = max(1, height / 2);
373 depth = max(1, depth / 2);
374 }
375
376 file_size *= faces;
377 file_size += sizeof(struct dds_header);
378 return file_size;
379 }
380
381 /************************************************************
382 * get_image_info_from_dds
383 *
384 * Fills a D3DXIMAGE_INFO structure with information
385 * about a DDS file stored in the memory.
386 *
387 * PARAMS
388 * buffer [I] pointer to DDS data
389 * length [I] size of DDS data
390 * info [O] pointer to D3DXIMAGE_INFO structure
391 *
392 * RETURNS
393 * Success: D3D_OK
394 * Failure: D3DXERR_INVALIDDATA
395 *
396 */
397 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
398 {
399 UINT faces = 1;
400 UINT expected_length;
401 const struct dds_header *header = buffer;
402
403 if (length < sizeof(*header) || !info)
404 return D3DXERR_INVALIDDATA;
405
406 if (header->pixel_format.size != sizeof(header->pixel_format))
407 return D3DXERR_INVALIDDATA;
408
409 info->Width = header->width;
410 info->Height = header->height;
411 info->Depth = 1;
412 info->MipLevels = header->miplevels ? header->miplevels : 1;
413
414 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
415 if (info->Format == D3DFMT_UNKNOWN)
416 return D3DXERR_INVALIDDATA;
417
418 TRACE("Pixel format is %#x\n", info->Format);
419
420 if (header->caps2 & DDS_CAPS2_VOLUME)
421 {
422 info->Depth = header->depth;
423 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
424 }
425 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
426 {
427 DWORD face;
428 faces = 0;
429 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
430 {
431 if (header->caps2 & face)
432 faces++;
433 }
434 info->ResourceType = D3DRTYPE_CUBETEXTURE;
435 }
436 else
437 {
438 info->ResourceType = D3DRTYPE_TEXTURE;
439 }
440
441 expected_length = calculate_dds_file_size(info->Format, info->Width, info->Height, info->Depth,
442 info->MipLevels, faces);
443 if (length < expected_length)
444 {
445 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
446 return D3DXERR_INVALIDDATA;
447 }
448
449 info->ImageFileFormat = D3DXIFF_DDS;
450 return D3D_OK;
451 }
452
453 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
454 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
455 const D3DXIMAGE_INFO *src_info)
456 {
457 UINT size;
458 UINT src_pitch;
459 const struct dds_header *header = src_data;
460 const BYTE *pixels = (BYTE *)(header + 1);
461
462 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
463 return D3DXERR_INVALIDDATA;
464
465 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &src_pitch, &size)))
466 return E_NOTIMPL;
467
468 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
469 src_pitch, NULL, src_rect, filter, color_key);
470 }
471
472 static HRESULT save_dds_surface_to_memory(ID3DXBuffer **dst_buffer, IDirect3DSurface9 *src_surface, const RECT *src_rect)
473 {
474 HRESULT hr;
475 UINT dst_pitch, surface_size, file_size;
476 D3DSURFACE_DESC src_desc;
477 D3DLOCKED_RECT locked_rect;
478 ID3DXBuffer *buffer;
479 struct dds_header *header;
480 BYTE *pixels;
481 struct volume volume;
482 const struct pixel_format_desc *pixel_format;
483
484 if (src_rect)
485 {
486 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
487 return E_NOTIMPL;
488 }
489
490 hr = IDirect3DSurface9_GetDesc(src_surface, &src_desc);
491 if (FAILED(hr)) return hr;
492
493 pixel_format = get_format_info(src_desc.Format);
494 if (pixel_format->type == FORMAT_UNKNOWN) return E_NOTIMPL;
495
496 file_size = calculate_dds_file_size(src_desc.Format, src_desc.Width, src_desc.Height, 1, 1, 1);
497
498 hr = calculate_dds_surface_size(src_desc.Format, src_desc.Width, src_desc.Height, &dst_pitch, &surface_size);
499 if (FAILED(hr)) return hr;
500
501 hr = D3DXCreateBuffer(file_size, &buffer);
502 if (FAILED(hr)) return hr;
503
504 header = ID3DXBuffer_GetBufferPointer(buffer);
505 pixels = (BYTE *)(header + 1);
506
507 memset(header, 0, sizeof(*header));
508 header->signature = MAKEFOURCC('D','D','S',' ');
509 /* The signature is not really part of the DDS header */
510 header->size = sizeof(*header) - FIELD_OFFSET(struct dds_header, size);
511 header->flags = DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PIXELFORMAT;
512 header->height = src_desc.Height;
513 header->width = src_desc.Width;
514 header->caps = DDS_CAPS_TEXTURE;
515 hr = d3dformat_to_dds_pixel_format(&header->pixel_format, src_desc.Format);
516 if (FAILED(hr))
517 {
518 ID3DXBuffer_Release(buffer);
519 return hr;
520 }
521
522 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, NULL, D3DLOCK_READONLY);
523 if (FAILED(hr))
524 {
525 ID3DXBuffer_Release(buffer);
526 return hr;
527 }
528
529 volume.width = src_desc.Width;
530 volume.height = src_desc.Height;
531 volume.depth = 1;
532 copy_pixels(locked_rect.pBits, locked_rect.Pitch, 0, pixels, dst_pitch, 0,
533 &volume, pixel_format);
534
535 IDirect3DSurface9_UnlockRect(src_surface);
536
537 *dst_buffer = buffer;
538 return D3D_OK;
539 }
540
541 static HRESULT get_surface(D3DRESOURCETYPE type, struct IDirect3DBaseTexture9 *tex,
542 int face, UINT level, struct IDirect3DSurface9 **surf)
543 {
544 switch (type)
545 {
546 case D3DRTYPE_TEXTURE:
547 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9*) tex, level, surf);
548 case D3DRTYPE_CUBETEXTURE:
549 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9*) tex, face, level, surf);
550 default:
551 ERR("Unexpected texture type\n");
552 return E_NOTIMPL;
553 }
554 }
555
556 HRESULT save_dds_texture_to_memory(ID3DXBuffer **dst_buffer, IDirect3DBaseTexture9 *src_texture, const PALETTEENTRY *src_palette)
557 {
558 HRESULT hr;
559 D3DRESOURCETYPE type;
560 UINT mip_levels;
561 IDirect3DSurface9 *surface;
562
563 type = IDirect3DBaseTexture9_GetType(src_texture);
564
565 if ((type != D3DRTYPE_TEXTURE) && (type != D3DRTYPE_CUBETEXTURE) && (type != D3DRTYPE_VOLUMETEXTURE))
566 return D3DERR_INVALIDCALL;
567
568 if (type == D3DRTYPE_CUBETEXTURE)
569 {
570 FIXME("Cube texture not supported yet\n");
571 return E_NOTIMPL;
572 }
573 else if (type == D3DRTYPE_VOLUMETEXTURE)
574 {
575 FIXME("Volume texture not supported yet\n");
576 return E_NOTIMPL;
577 }
578
579 mip_levels = IDirect3DTexture9_GetLevelCount(src_texture);
580
581 if (mip_levels > 1)
582 {
583 FIXME("Mipmap not supported yet\n");
584 return E_NOTIMPL;
585 }
586
587 if (src_palette)
588 {
589 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
590 return E_NOTIMPL;
591 }
592
593 hr = get_surface(type, src_texture, D3DCUBEMAP_FACE_POSITIVE_X, 0, &surface);
594
595 if (SUCCEEDED(hr))
596 {
597 hr = save_dds_surface_to_memory(dst_buffer, surface, NULL);
598 IDirect3DSurface9_Release(surface);
599 }
600
601 return hr;
602 }
603 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette,
604 const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key,
605 const D3DXIMAGE_INFO *src_info)
606 {
607 UINT row_pitch, slice_pitch;
608 const struct dds_header *header = src_data;
609 const BYTE *pixels = (BYTE *)(header + 1);
610
611 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
612 return D3DXERR_INVALIDDATA;
613
614 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &row_pitch, &slice_pitch)))
615 return E_NOTIMPL;
616
617 return D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box, pixels, src_info->Format,
618 row_pitch, slice_pitch, NULL, src_box, filter, color_key);
619 }
620
621 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
622 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info, unsigned int skip_levels,
623 unsigned int *loaded_miplevels)
624 {
625 HRESULT hr;
626 RECT src_rect;
627 UINT src_pitch;
628 UINT mip_level;
629 UINT mip_levels;
630 UINT mip_level_size;
631 UINT width, height;
632 IDirect3DSurface9 *surface;
633 const struct dds_header *header = src_data;
634 const BYTE *pixels = (BYTE *)(header + 1);
635
636 /* Loading a cube texture as a simple texture is also supported
637 * (only first face texture is taken). Same with volume textures. */
638 if ((src_info->ResourceType != D3DRTYPE_TEXTURE)
639 && (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
640 && (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE))
641 {
642 WARN("Trying to load a %u resource as a 2D texture, returning failure.\n", src_info->ResourceType);
643 return D3DXERR_INVALIDDATA;
644 }
645
646 width = src_info->Width;
647 height = src_info->Height;
648 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
649 if (src_info->ResourceType == D3DRTYPE_VOLUMETEXTURE)
650 mip_levels = 1;
651 for (mip_level = 0; mip_level < mip_levels + skip_levels; ++mip_level)
652 {
653 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_pitch, &mip_level_size);
654 if (FAILED(hr)) return hr;
655
656 if (mip_level >= skip_levels)
657 {
658 SetRect(&src_rect, 0, 0, width, height);
659
660 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level - skip_levels, &surface);
661 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
662 NULL, &src_rect, filter, color_key);
663 IDirect3DSurface9_Release(surface);
664 if (FAILED(hr))
665 return hr;
666 }
667
668 pixels += mip_level_size;
669 width = max(1, width / 2);
670 height = max(1, height / 2);
671 }
672
673 *loaded_miplevels = mip_levels - skip_levels;
674
675 return D3D_OK;
676 }
677
678 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
679 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
680 {
681 HRESULT hr;
682 int face;
683 UINT mip_level;
684 UINT size;
685 RECT src_rect;
686 UINT src_pitch;
687 UINT mip_levels;
688 UINT mip_level_size;
689 IDirect3DSurface9 *surface;
690 const struct dds_header *header = src_data;
691 const BYTE *pixels = (BYTE *)(header + 1);
692
693 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
694 return D3DXERR_INVALIDDATA;
695
696 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
697 {
698 WARN("Only full cubemaps are supported\n");
699 return D3DXERR_INVALIDDATA;
700 }
701
702 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
703 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
704 {
705 size = src_info->Width;
706 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
707 {
708 hr = calculate_dds_surface_size(src_info->Format, size, size, &src_pitch, &mip_level_size);
709 if (FAILED(hr)) return hr;
710
711 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
712 if (mip_level < mip_levels)
713 {
714 SetRect(&src_rect, 0, 0, size, size);
715
716 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
717 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
718 NULL, &src_rect, filter, color_key);
719 IDirect3DSurface9_Release(surface);
720 if (FAILED(hr)) return hr;
721 }
722
723 pixels += mip_level_size;
724 size = max(1, size / 2);
725 }
726 }
727
728 return D3D_OK;
729 }
730
731 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
732 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
733 {
734 HRESULT hr;
735 UINT mip_level;
736 UINT mip_levels;
737 UINT src_slice_pitch;
738 UINT src_row_pitch;
739 D3DBOX src_box;
740 UINT width, height, depth;
741 IDirect3DVolume9 *volume;
742 const struct dds_header *header = src_data;
743 const BYTE *pixels = (BYTE *)(header + 1);
744
745 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
746 return D3DXERR_INVALIDDATA;
747
748 width = src_info->Width;
749 height = src_info->Height;
750 depth = src_info->Depth;
751 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture));
752
753 for (mip_level = 0; mip_level < mip_levels; mip_level++)
754 {
755 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_row_pitch, &src_slice_pitch);
756 if (FAILED(hr)) return hr;
757
758 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume);
759 if (FAILED(hr)) return hr;
760
761 src_box.Left = 0;
762 src_box.Top = 0;
763 src_box.Right = width;
764 src_box.Bottom = height;
765 src_box.Front = 0;
766 src_box.Back = depth;
767
768 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format,
769 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key);
770
771 IDirect3DVolume9_Release(volume);
772 if (FAILED(hr)) return hr;
773
774 pixels += depth * src_slice_pitch;
775 width = max(1, width / 2);
776 height = max(1, height / 2);
777 depth = max(1, depth / 2);
778 }
779
780 return D3D_OK;
781 }
782
783 static BOOL convert_dib_to_bmp(void **data, UINT *size)
784 {
785 ULONG header_size;
786 ULONG count = 0;
787 ULONG offset;
788 BITMAPFILEHEADER *header;
789 BYTE *new_data;
790 UINT new_size;
791
792 if ((*size < 4) || (*size < (header_size = *(ULONG*)*data)))
793 return FALSE;
794
795 if ((header_size == sizeof(BITMAPINFOHEADER)) ||
796 (header_size == sizeof(BITMAPV4HEADER)) ||
797 (header_size == sizeof(BITMAPV5HEADER)) ||
798 (header_size == 64 /* sizeof(BITMAPCOREHEADER2) */))
799 {
800 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
801 BITMAPINFOHEADER *info_header = (BITMAPINFOHEADER*)*data;
802 count = info_header->biClrUsed;
803
804 if (!count && info_header->biBitCount <= 8)
805 count = 1 << info_header->biBitCount;
806
807 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBQUAD) * count;
808
809 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
810 if ((info_header->biSize == sizeof(BITMAPINFOHEADER)) && (info_header->biCompression == BI_BITFIELDS))
811 offset += 3 * sizeof(DWORD);
812 }
813 else if (header_size == sizeof(BITMAPCOREHEADER))
814 {
815 BITMAPCOREHEADER *core_header = (BITMAPCOREHEADER*)*data;
816
817 if (core_header->bcBitCount <= 8)
818 count = 1 << core_header->bcBitCount;
819
820 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBTRIPLE) * count;
821 }
822 else
823 {
824 return FALSE;
825 }
826
827 TRACE("Converting DIB file to BMP\n");
828
829 new_size = *size + sizeof(BITMAPFILEHEADER);
830 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
831 CopyMemory(new_data + sizeof(BITMAPFILEHEADER), *data, *size);
832
833 /* Add BMP header */
834 header = (BITMAPFILEHEADER*)new_data;
835 header->bfType = 0x4d42; /* BM */
836 header->bfSize = new_size;
837 header->bfReserved1 = 0;
838 header->bfReserved2 = 0;
839 header->bfOffBits = offset;
840
841 /* Update input data */
842 *data = new_data;
843 *size = new_size;
844
845 return TRUE;
846 }
847
848 /************************************************************
849 * D3DXGetImageInfoFromFileInMemory
850 *
851 * Fills a D3DXIMAGE_INFO structure with info about an image
852 *
853 * PARAMS
854 * data [I] pointer to the image file data
855 * datasize [I] size of the passed data
856 * info [O] pointer to the destination structure
857 *
858 * RETURNS
859 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
860 * if info is NULL and data and datasize are not NULL
861 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
862 * D3DERR_INVALIDCALL, if data is NULL or
863 * if datasize is 0
864 *
865 * NOTES
866 * datasize may be bigger than the actual file size
867 *
868 */
869 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(const void *data, UINT datasize, D3DXIMAGE_INFO *info)
870 {
871 IWICImagingFactory *factory;
872 IWICBitmapDecoder *decoder = NULL;
873 IWICStream *stream;
874 HRESULT hr;
875 HRESULT initresult;
876 BOOL dib;
877
878 TRACE("(%p, %d, %p)\n", data, datasize, info);
879
880 if (!data || !datasize)
881 return D3DERR_INVALIDCALL;
882
883 if (!info)
884 return D3D_OK;
885
886 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
887 TRACE("File type is DDS\n");
888 return get_image_info_from_dds(data, datasize, info);
889 }
890
891 /* In case of DIB file, convert it to BMP */
892 dib = convert_dib_to_bmp((void**)&data, &datasize);
893
894 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
895
896 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
897
898 if (SUCCEEDED(hr)) {
899 IWICImagingFactory_CreateStream(factory, &stream);
900 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
901 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
902 IWICStream_Release(stream);
903 IWICImagingFactory_Release(factory);
904 }
905
906 if (FAILED(hr)) {
907 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
908 FIXME("File type PPM is not supported yet\n");
909 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
910 FIXME("File type HDR is not supported yet\n");
911 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
912 FIXME("File type PFM is not supported yet\n");
913 }
914
915 if (SUCCEEDED(hr)) {
916 GUID container_format;
917 UINT frame_count;
918
919 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
920 if (SUCCEEDED(hr)) {
921 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
922 if (dib) {
923 TRACE("File type is DIB\n");
924 info->ImageFileFormat = D3DXIFF_DIB;
925 } else {
926 TRACE("File type is BMP\n");
927 info->ImageFileFormat = D3DXIFF_BMP;
928 }
929 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
930 TRACE("File type is PNG\n");
931 info->ImageFileFormat = D3DXIFF_PNG;
932 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
933 TRACE("File type is JPG\n");
934 info->ImageFileFormat = D3DXIFF_JPG;
935 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
936 TRACE("File type is TGA\n");
937 info->ImageFileFormat = D3DXIFF_TGA;
938 } else {
939 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
940 hr = D3DXERR_INVALIDDATA;
941 }
942 }
943
944 if (SUCCEEDED(hr))
945 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
946 if (SUCCEEDED(hr) && !frame_count)
947 hr = D3DXERR_INVALIDDATA;
948
949 if (SUCCEEDED(hr)) {
950 IWICBitmapFrameDecode *frame = NULL;
951
952 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
953
954 if (SUCCEEDED(hr))
955 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
956
957 if (SUCCEEDED(hr)) {
958 WICPixelFormatGUID pixel_format;
959
960 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
961 if (SUCCEEDED(hr)) {
962 info->Format = wic_guid_to_d3dformat(&pixel_format);
963 if (info->Format == D3DFMT_UNKNOWN) {
964 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
965 hr = D3DXERR_INVALIDDATA;
966 }
967 }
968 }
969
970 /* For 32 bpp BMP, windowscodecs.dll never returns a format with alpha while
971 * d3dx9_xx.dll returns one if at least 1 pixel has a non zero alpha component */
972 if (SUCCEEDED(hr) && (info->Format == D3DFMT_X8R8G8B8) && (info->ImageFileFormat == D3DXIFF_BMP)) {
973 DWORD size = sizeof(DWORD) * info->Width * info->Height;
974 BYTE *buffer = HeapAlloc(GetProcessHeap(), 0, size);
975 hr = IWICBitmapFrameDecode_CopyPixels(frame, NULL, sizeof(DWORD) * info->Width, size, buffer);
976 if (SUCCEEDED(hr)) {
977 DWORD i;
978 for (i = 0; i < info->Width * info->Height; i++) {
979 if (buffer[i*4+3]) {
980 info->Format = D3DFMT_A8R8G8B8;
981 break;
982 }
983 }
984 }
985 HeapFree(GetProcessHeap(), 0, buffer);
986 }
987
988 if (frame)
989 IWICBitmapFrameDecode_Release(frame);
990
991 info->Depth = 1;
992 info->MipLevels = 1;
993 info->ResourceType = D3DRTYPE_TEXTURE;
994 }
995 }
996
997 if (decoder)
998 IWICBitmapDecoder_Release(decoder);
999
1000 if (SUCCEEDED(initresult))
1001 CoUninitialize();
1002
1003 if (dib)
1004 HeapFree(GetProcessHeap(), 0, (void*)data);
1005
1006 if (FAILED(hr)) {
1007 TRACE("Invalid or unsupported image file\n");
1008 return D3DXERR_INVALIDDATA;
1009 }
1010
1011 return D3D_OK;
1012 }
1013
1014 /************************************************************
1015 * D3DXGetImageInfoFromFile
1016 *
1017 * RETURNS
1018 * Success: D3D_OK, if we successfully load a valid image file or
1019 * if we successfully load a file which is no valid image and info is NULL
1020 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
1021 * if file is not a valid image file and info is not NULL
1022 * D3DERR_INVALIDCALL, if file is NULL
1023 *
1024 */
1025 HRESULT WINAPI D3DXGetImageInfoFromFileA(const char *file, D3DXIMAGE_INFO *info)
1026 {
1027 WCHAR *widename;
1028 HRESULT hr;
1029 int strlength;
1030
1031 TRACE("file %s, info %p.\n", debugstr_a(file), info);
1032
1033 if( !file ) return D3DERR_INVALIDCALL;
1034
1035 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
1036 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
1037 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
1038
1039 hr = D3DXGetImageInfoFromFileW(widename, info);
1040 HeapFree(GetProcessHeap(), 0, widename);
1041
1042 return hr;
1043 }
1044
1045 HRESULT WINAPI D3DXGetImageInfoFromFileW(const WCHAR *file, D3DXIMAGE_INFO *info)
1046 {
1047 void *buffer;
1048 HRESULT hr;
1049 DWORD size;
1050
1051 TRACE("file %s, info %p.\n", debugstr_w(file), info);
1052
1053 if (!file)
1054 return D3DERR_INVALIDCALL;
1055
1056 if (FAILED(map_view_of_file(file, &buffer, &size)))
1057 return D3DXERR_INVALIDDATA;
1058
1059 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
1060 UnmapViewOfFile(buffer);
1061
1062 return hr;
1063 }
1064
1065 /************************************************************
1066 * D3DXGetImageInfoFromResource
1067 *
1068 * RETURNS
1069 * Success: D3D_OK, if resource is a valid image file
1070 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
1071 * if we fail to load resource
1072 *
1073 */
1074 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, const char *resource, D3DXIMAGE_INFO *info)
1075 {
1076 HRSRC resinfo;
1077 void *buffer;
1078 DWORD size;
1079
1080 TRACE("module %p, resource %s, info %p.\n", module, debugstr_a(resource), info);
1081
1082 if (!(resinfo = FindResourceA(module, resource, (const char *)RT_RCDATA))
1083 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1084 && !(resinfo = FindResourceA(module, resource, (const char *)RT_BITMAP)))
1085 return D3DXERR_INVALIDDATA;
1086
1087 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
1088 return D3DXERR_INVALIDDATA;
1089
1090 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
1091 }
1092
1093 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, const WCHAR *resource, D3DXIMAGE_INFO *info)
1094 {
1095 HRSRC resinfo;
1096 void *buffer;
1097 DWORD size;
1098
1099 TRACE("module %p, resource %s, info %p.\n", module, debugstr_w(resource), info);
1100
1101 if (!(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA))
1102 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1103 && !(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_BITMAP)))
1104 return D3DXERR_INVALIDDATA;
1105
1106 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
1107 return D3DXERR_INVALIDDATA;
1108
1109 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
1110 }
1111
1112 /************************************************************
1113 * D3DXLoadSurfaceFromFileInMemory
1114 *
1115 * Loads data from a given buffer into a surface and fills a given
1116 * D3DXIMAGE_INFO structure with info about the source data.
1117 *
1118 * PARAMS
1119 * pDestSurface [I] pointer to the surface
1120 * pDestPalette [I] palette to use
1121 * pDestRect [I] to be filled area of the surface
1122 * pSrcData [I] pointer to the source data
1123 * SrcDataSize [I] size of the source data in bytes
1124 * pSrcRect [I] area of the source data to load
1125 * dwFilter [I] filter to apply on stretching
1126 * Colorkey [I] colorkey
1127 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1128 *
1129 * RETURNS
1130 * Success: D3D_OK
1131 * Failure: D3DERR_INVALIDCALL, if pDestSurface, pSrcData or SrcDataSize is NULL
1132 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1133 *
1134 */
1135 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
1136 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
1137 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
1138 {
1139 D3DXIMAGE_INFO imginfo;
1140 HRESULT hr, com_init;
1141
1142 IWICImagingFactory *factory = NULL;
1143 IWICBitmapDecoder *decoder;
1144 IWICBitmapFrameDecode *bitmapframe;
1145 IWICStream *stream;
1146
1147 const struct pixel_format_desc *formatdesc;
1148 WICRect wicrect;
1149 RECT rect;
1150
1151 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1152 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1153 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
1154 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
1155
1156 if (!pDestSurface || !pSrcData || !SrcDataSize)
1157 return D3DERR_INVALIDCALL;
1158
1159 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
1160
1161 if (FAILED(hr))
1162 return hr;
1163
1164 if (pSrcRect)
1165 {
1166 wicrect.X = pSrcRect->left;
1167 wicrect.Y = pSrcRect->top;
1168 wicrect.Width = pSrcRect->right - pSrcRect->left;
1169 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
1170 }
1171 else
1172 {
1173 wicrect.X = 0;
1174 wicrect.Y = 0;
1175 wicrect.Width = imginfo.Width;
1176 wicrect.Height = imginfo.Height;
1177 }
1178
1179 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
1180
1181 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
1182 {
1183 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
1184 dwFilter, Colorkey, &imginfo);
1185 if (SUCCEEDED(hr) && pSrcInfo)
1186 *pSrcInfo = imginfo;
1187 return hr;
1188 }
1189
1190 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1191 convert_dib_to_bmp((void**)&pSrcData, &SrcDataSize);
1192
1193 com_init = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1194
1195 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
1196 goto cleanup_err;
1197
1198 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
1199 {
1200 IWICImagingFactory_Release(factory);
1201 factory = NULL;
1202 goto cleanup_err;
1203 }
1204
1205 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
1206
1207 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
1208
1209 IWICStream_Release(stream);
1210
1211 if (FAILED(hr))
1212 goto cleanup_err;
1213
1214 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
1215
1216 if (FAILED(hr))
1217 goto cleanup_bmp;
1218
1219 formatdesc = get_format_info(imginfo.Format);
1220
1221 if (formatdesc->type == FORMAT_UNKNOWN)
1222 {
1223 FIXME("Unsupported pixel format\n");
1224 hr = D3DXERR_INVALIDDATA;
1225 }
1226 else
1227 {
1228 BYTE *buffer;
1229 DWORD pitch;
1230 PALETTEENTRY *palette = NULL;
1231 WICColor *colors = NULL;
1232
1233 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
1234 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
1235
1236 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
1237 pitch * wicrect.Height, buffer);
1238
1239 if (SUCCEEDED(hr) && (formatdesc->type == FORMAT_INDEX))
1240 {
1241 IWICPalette *wic_palette = NULL;
1242 UINT nb_colors;
1243
1244 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
1245 if (SUCCEEDED(hr))
1246 hr = IWICBitmapFrameDecode_CopyPalette(bitmapframe, wic_palette);
1247 if (SUCCEEDED(hr))
1248 hr = IWICPalette_GetColorCount(wic_palette, &nb_colors);
1249 if (SUCCEEDED(hr))
1250 {
1251 colors = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(colors[0]));
1252 palette = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(palette[0]));
1253 if (!colors || !palette)
1254 hr = E_OUTOFMEMORY;
1255 }
1256 if (SUCCEEDED(hr))
1257 hr = IWICPalette_GetColors(wic_palette, nb_colors, colors, &nb_colors);
1258 if (SUCCEEDED(hr))
1259 {
1260 UINT i;
1261
1262 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1263 for (i = 0; i < nb_colors; i++)
1264 {
1265 palette[i].peRed = (colors[i] >> 16) & 0xff;
1266 palette[i].peGreen = (colors[i] >> 8) & 0xff;
1267 palette[i].peBlue = colors[i] & 0xff;
1268 palette[i].peFlags = (colors[i] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1269 }
1270 }
1271 if (wic_palette)
1272 IWICPalette_Release(wic_palette);
1273 }
1274
1275 if (SUCCEEDED(hr))
1276 {
1277 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1278 buffer, imginfo.Format, pitch,
1279 palette, &rect, dwFilter, Colorkey);
1280 }
1281
1282 HeapFree(GetProcessHeap(), 0, colors);
1283 HeapFree(GetProcessHeap(), 0, palette);
1284 HeapFree(GetProcessHeap(), 0, buffer);
1285 }
1286
1287 IWICBitmapFrameDecode_Release(bitmapframe);
1288
1289 cleanup_bmp:
1290 IWICBitmapDecoder_Release(decoder);
1291
1292 cleanup_err:
1293 if (factory)
1294 IWICImagingFactory_Release(factory);
1295
1296 if (SUCCEEDED(com_init))
1297 CoUninitialize();
1298
1299 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1300 HeapFree(GetProcessHeap(), 0, (void*)pSrcData);
1301
1302 if (FAILED(hr))
1303 return D3DXERR_INVALIDDATA;
1304
1305 if (pSrcInfo)
1306 *pSrcInfo = imginfo;
1307
1308 return D3D_OK;
1309 }
1310
1311 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
1312 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
1313 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1314 {
1315 WCHAR *src_file_w;
1316 HRESULT hr;
1317 int strlength;
1318
1319 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1320 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1321 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
1322 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1323
1324 if (!src_file || !dst_surface)
1325 return D3DERR_INVALIDCALL;
1326
1327 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
1328 src_file_w = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*src_file_w));
1329 MultiByteToWideChar(CP_ACP, 0, src_file, -1, src_file_w, strlength);
1330
1331 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
1332 src_file_w, src_rect, filter, color_key, src_info);
1333 HeapFree(GetProcessHeap(), 0, src_file_w);
1334
1335 return hr;
1336 }
1337
1338 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
1339 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
1340 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1341 {
1342 UINT data_size;
1343 void *data;
1344 HRESULT hr;
1345
1346 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1347 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1348 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
1349 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1350
1351 if (!src_file || !dst_surface)
1352 return D3DERR_INVALIDCALL;
1353
1354 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
1355 return D3DXERR_INVALIDDATA;
1356
1357 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1358 data, data_size, src_rect, filter, color_key, src_info);
1359 UnmapViewOfFile(data);
1360
1361 return hr;
1362 }
1363
1364 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
1365 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
1366 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1367 {
1368 UINT data_size;
1369 HRSRC resinfo;
1370 void *data;
1371
1372 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1373 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1374 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
1375 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1376
1377 if (!dst_surface)
1378 return D3DERR_INVALIDCALL;
1379
1380 if (!(resinfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA))
1381 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1382 && !(resinfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
1383 return D3DXERR_INVALIDDATA;
1384
1385 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1386 return D3DXERR_INVALIDDATA;
1387
1388 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1389 data, data_size, src_rect, filter, color_key, src_info);
1390 }
1391
1392 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
1393 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
1394 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1395 {
1396 UINT data_size;
1397 HRSRC resinfo;
1398 void *data;
1399
1400 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1401 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1402 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
1403 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1404
1405 if (!dst_surface)
1406 return D3DERR_INVALIDCALL;
1407
1408 if (!(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA))
1409 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1410 && !(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1411 return D3DXERR_INVALIDDATA;
1412
1413 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1414 return D3DXERR_INVALIDDATA;
1415
1416 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1417 data, data_size, src_rect, filter, color_key, src_info);
1418 }
1419
1420
1421 /************************************************************
1422 * helper functions for D3DXLoadSurfaceFromMemory
1423 */
1424 struct argb_conversion_info
1425 {
1426 const struct pixel_format_desc *srcformat;
1427 const struct pixel_format_desc *destformat;
1428 DWORD srcshift[4], destshift[4];
1429 DWORD srcmask[4], destmask[4];
1430 BOOL process_channel[4];
1431 DWORD channelmask;
1432 };
1433
1434 static void init_argb_conversion_info(const struct pixel_format_desc *srcformat, const struct pixel_format_desc *destformat, struct argb_conversion_info *info)
1435 {
1436 UINT i;
1437 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1438 info->channelmask = 0;
1439
1440 info->srcformat = srcformat;
1441 info->destformat = destformat;
1442
1443 for(i = 0;i < 4;i++) {
1444 /* srcshift is used to extract the _relevant_ components */
1445 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1446
1447 /* destshift is used to move the components to the correct position */
1448 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1449
1450 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1451 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1452
1453 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1454 if(destformat->bits[i]) {
1455 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1456 else info->channelmask |= info->destmask[i];
1457 }
1458 }
1459 }
1460
1461 /************************************************************
1462 * get_relevant_argb_components
1463 *
1464 * Extracts the relevant components from the source color and
1465 * drops the less significant bits if they aren't used by the destination format.
1466 */
1467 static void get_relevant_argb_components(const struct argb_conversion_info *info, const BYTE *col, DWORD *out)
1468 {
1469 unsigned int i, j;
1470 unsigned int component, mask;
1471
1472 for (i = 0; i < 4; ++i)
1473 {
1474 if (!info->process_channel[i])
1475 continue;
1476
1477 component = 0;
1478 mask = info->srcmask[i];
1479 for (j = 0; j < 4 && mask; ++j)
1480 {
1481 if (info->srcshift[i] < j * 8)
1482 component |= (col[j] & mask) << (j * 8 - info->srcshift[i]);
1483 else
1484 component |= (col[j] & mask) >> (info->srcshift[i] - j * 8);
1485 mask >>= 8;
1486 }
1487 out[i] = component;
1488 }
1489 }
1490
1491 /************************************************************
1492 * make_argb_color
1493 *
1494 * Recombines the output of get_relevant_argb_components and converts
1495 * it to the destination format.
1496 */
1497 static DWORD make_argb_color(const struct argb_conversion_info *info, const DWORD *in)
1498 {
1499 UINT i;
1500 DWORD val = 0;
1501
1502 for(i = 0;i < 4;i++) {
1503 if(info->process_channel[i]) {
1504 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1505 signed int shift;
1506 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1507 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1508 }
1509 }
1510 val |= info->channelmask; /* new channels are set to their maximal value */
1511 return val;
1512 }
1513
1514 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */
1515 static void format_to_vec4(const struct pixel_format_desc *format, const BYTE *src, struct vec4 *dst)
1516 {
1517 DWORD mask, tmp;
1518 unsigned int c;
1519
1520 for (c = 0; c < 4; ++c)
1521 {
1522 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1523 float *dst_component = (float *)dst + component_offsets[c];
1524
1525 if (format->bits[c])
1526 {
1527 mask = ~0u >> (32 - format->bits[c]);
1528
1529 memcpy(&tmp, src + format->shift[c] / 8,
1530 min(sizeof(DWORD), (format->shift[c] % 8 + format->bits[c] + 7) / 8));
1531
1532 if (format->type == FORMAT_ARGBF16)
1533 *dst_component = float_16_to_32(tmp);
1534 else if (format->type == FORMAT_ARGBF)
1535 *dst_component = *(float *)&tmp;
1536 else
1537 *dst_component = (float)((tmp >> format->shift[c] % 8) & mask) / mask;
1538 }
1539 else
1540 *dst_component = 1.0f;
1541 }
1542 }
1543
1544 /* It doesn't work for components bigger than 32 bits. */
1545 static void format_from_vec4(const struct pixel_format_desc *format, const struct vec4 *src, BYTE *dst)
1546 {
1547 DWORD v, mask32;
1548 unsigned int c, i;
1549
1550 memset(dst, 0, format->bytes_per_pixel);
1551
1552 for (c = 0; c < 4; ++c)
1553 {
1554 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1555 const float src_component = *((const float *)src + component_offsets[c]);
1556
1557 if (!format->bits[c])
1558 continue;
1559
1560 mask32 = ~0u >> (32 - format->bits[c]);
1561
1562 if (format->type == FORMAT_ARGBF16)
1563 v = float_32_to_16(src_component);
1564 else if (format->type == FORMAT_ARGBF)
1565 v = *(DWORD *)&src_component;
1566 else
1567 v = (DWORD)(src_component * ((1 << format->bits[c]) - 1) + 0.5f);
1568
1569 for (i = format->shift[c] / 8 * 8; i < format->shift[c] + format->bits[c]; i += 8)
1570 {
1571 BYTE mask, byte;
1572
1573 if (format->shift[c] > i)
1574 {
1575 mask = mask32 << (format->shift[c] - i);
1576 byte = (v << (format->shift[c] - i)) & mask;
1577 }
1578 else
1579 {
1580 mask = mask32 >> (i - format->shift[c]);
1581 byte = (v >> (i - format->shift[c])) & mask;
1582 }
1583 dst[i / 8] |= byte;
1584 }
1585 }
1586 }
1587
1588 /************************************************************
1589 * copy_pixels
1590 *
1591 * Copies the source buffer to the destination buffer.
1592 * Works for any pixel format.
1593 * The source and the destination must be block-aligned.
1594 */
1595 void copy_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
1596 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *size,
1597 const struct pixel_format_desc *format)
1598 {
1599 UINT row, slice;
1600 BYTE *dst_addr;
1601 const BYTE *src_addr;
1602 UINT row_block_count = (size->width + format->block_width - 1) / format->block_width;
1603 UINT row_count = (size->height + format->block_height - 1) / format->block_height;
1604
1605 for (slice = 0; slice < size->depth; slice++)
1606 {
1607 src_addr = src + slice * src_slice_pitch;
1608 dst_addr = dst + slice * dst_slice_pitch;
1609
1610 for (row = 0; row < row_count; row++)
1611 {
1612 memcpy(dst_addr, src_addr, row_block_count * format->block_byte_count);
1613 src_addr += src_row_pitch;
1614 dst_addr += dst_row_pitch;
1615 }
1616 }
1617 }
1618
1619 /************************************************************
1620 * convert_argb_pixels
1621 *
1622 * Copies the source buffer to the destination buffer, performing
1623 * any necessary format conversion and color keying.
1624 * Pixels outsize the source rect are blacked out.
1625 */
1626 void convert_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1627 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1628 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1629 const PALETTEENTRY *palette)
1630 {
1631 struct argb_conversion_info conv_info, ck_conv_info;
1632 const struct pixel_format_desc *ck_format = NULL;
1633 DWORD channels[4];
1634 UINT min_width, min_height, min_depth;
1635 UINT x, y, z;
1636
1637 ZeroMemory(channels, sizeof(channels));
1638 init_argb_conversion_info(src_format, dst_format, &conv_info);
1639
1640 min_width = min(src_size->width, dst_size->width);
1641 min_height = min(src_size->height, dst_size->height);
1642 min_depth = min(src_size->depth, dst_size->depth);
1643
1644 if (color_key)
1645 {
1646 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1647 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1648 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1649 }
1650
1651 for (z = 0; z < min_depth; z++) {
1652 const BYTE *src_slice_ptr = src + z * src_slice_pitch;
1653 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1654
1655 for (y = 0; y < min_height; y++) {
1656 const BYTE *src_ptr = src_slice_ptr + y * src_row_pitch;
1657 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1658
1659 for (x = 0; x < min_width; x++) {
1660 if (!src_format->to_rgba && !dst_format->from_rgba
1661 && src_format->type == dst_format->type
1662 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1663 {
1664 DWORD val;
1665
1666 get_relevant_argb_components(&conv_info, src_ptr, channels);
1667 val = make_argb_color(&conv_info, channels);
1668
1669 if (color_key)
1670 {
1671 DWORD ck_pixel;
1672
1673 get_relevant_argb_components(&ck_conv_info, src_ptr, channels);
1674 ck_pixel = make_argb_color(&ck_conv_info, channels);
1675 if (ck_pixel == color_key)
1676 val &= ~conv_info.destmask[0];
1677 }
1678 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1679 }
1680 else
1681 {
1682 struct vec4 color, tmp;
1683
1684 format_to_vec4(src_format, src_ptr, &color);
1685 if (src_format->to_rgba)
1686 src_format->to_rgba(&color, &tmp, palette);
1687 else
1688 tmp = color;
1689
1690 if (ck_format)
1691 {
1692 DWORD ck_pixel;
1693
1694 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1695 if (ck_pixel == color_key)
1696 tmp.w = 0.0f;
1697 }
1698
1699 if (dst_format->from_rgba)
1700 dst_format->from_rgba(&tmp, &color);
1701 else
1702 color = tmp;
1703
1704 format_from_vec4(dst_format, &color, dst_ptr);
1705 }
1706
1707 src_ptr += src_format->bytes_per_pixel;
1708 dst_ptr += dst_format->bytes_per_pixel;
1709 }
1710
1711 if (src_size->width < dst_size->width) /* black out remaining pixels */
1712 memset(dst_ptr, 0, dst_format->bytes_per_pixel * (dst_size->width - src_size->width));
1713 }
1714
1715 if (src_size->height < dst_size->height) /* black out remaining pixels */
1716 memset(dst + src_size->height * dst_row_pitch, 0, dst_row_pitch * (dst_size->height - src_size->height));
1717 }
1718 if (src_size->depth < dst_size->depth) /* black out remaining pixels */
1719 memset(dst + src_size->depth * dst_slice_pitch, 0, dst_slice_pitch * (dst_size->depth - src_size->depth));
1720 }
1721
1722 /************************************************************
1723 * point_filter_argb_pixels
1724 *
1725 * Copies the source buffer to the destination buffer, performing
1726 * any necessary format conversion, color keying and stretching
1727 * using a point filter.
1728 */
1729 void point_filter_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1730 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1731 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1732 const PALETTEENTRY *palette)
1733 {
1734 struct argb_conversion_info conv_info, ck_conv_info;
1735 const struct pixel_format_desc *ck_format = NULL;
1736 DWORD channels[4];
1737 UINT x, y, z;
1738
1739 ZeroMemory(channels, sizeof(channels));
1740 init_argb_conversion_info(src_format, dst_format, &conv_info);
1741
1742 if (color_key)
1743 {
1744 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1745 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1746 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1747 }
1748
1749 for (z = 0; z < dst_size->depth; z++)
1750 {
1751 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1752 const BYTE *src_slice_ptr = src + src_slice_pitch * (z * src_size->depth / dst_size->depth);
1753
1754 for (y = 0; y < dst_size->height; y++)
1755 {
1756 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1757 const BYTE *src_row_ptr = src_slice_ptr + src_row_pitch * (y * src_size->height / dst_size->height);
1758
1759 for (x = 0; x < dst_size->width; x++)
1760 {
1761 const BYTE *src_ptr = src_row_ptr + (x * src_size->width / dst_size->width) * src_format->bytes_per_pixel;
1762
1763 if (!src_format->to_rgba && !dst_format->from_rgba
1764 && src_format->type == dst_format->type
1765 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1766 {
1767 DWORD val;
1768
1769 get_relevant_argb_components(&conv_info, src_ptr, channels);
1770 val = make_argb_color(&conv_info, channels);
1771
1772 if (color_key)
1773 {
1774 DWORD ck_pixel;
1775
1776 get_relevant_argb_components(&ck_conv_info, src_ptr, channels);
1777 ck_pixel = make_argb_color(&ck_conv_info, channels);
1778 if (ck_pixel == color_key)
1779 val &= ~conv_info.destmask[0];
1780 }
1781 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1782 }
1783 else
1784 {
1785 struct vec4 color, tmp;
1786
1787 format_to_vec4(src_format, src_ptr, &color);
1788 if (src_format->to_rgba)
1789 src_format->to_rgba(&color, &tmp, palette);
1790 else
1791 tmp = color;
1792
1793 if (ck_format)
1794 {
1795 DWORD ck_pixel;
1796
1797 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1798 if (ck_pixel == color_key)
1799 tmp.w = 0.0f;
1800 }
1801
1802 if (dst_format->from_rgba)
1803 dst_format->from_rgba(&tmp, &color);
1804 else
1805 color = tmp;
1806
1807 format_from_vec4(dst_format, &color, dst_ptr);
1808 }
1809
1810 dst_ptr += dst_format->bytes_per_pixel;
1811 }
1812 }
1813 }
1814 }
1815
1816 typedef BOOL (*dxtn_conversion_func)(const BYTE *src, BYTE *dst, DWORD pitch_in, DWORD pitch_out,
1817 enum wined3d_format_id format, unsigned int w, unsigned int h);
1818
1819 static dxtn_conversion_func get_dxtn_conversion_func(D3DFORMAT format, BOOL encode)
1820 {
1821 switch (format)
1822 {
1823 case D3DFMT_DXT1:
1824 if (!wined3d_dxtn_supported()) return NULL;
1825 return encode ? wined3d_dxt1_encode : wined3d_dxt1_decode;
1826 case D3DFMT_DXT3:
1827 if (!wined3d_dxtn_supported()) return NULL;
1828 return encode ? wined3d_dxt3_encode : wined3d_dxt3_decode;
1829 case D3DFMT_DXT5:
1830 if (!wined3d_dxtn_supported()) return NULL;
1831 return encode ? wined3d_dxt5_encode : wined3d_dxt5_decode;
1832 default:
1833 return NULL;
1834 }
1835 }
1836
1837 /************************************************************
1838 * D3DXLoadSurfaceFromMemory
1839 *
1840 * Loads data from a given memory chunk into a surface,
1841 * applying any of the specified filters.
1842 *
1843 * PARAMS
1844 * pDestSurface [I] pointer to the surface
1845 * pDestPalette [I] palette to use
1846 * pDestRect [I] to be filled area of the surface
1847 * pSrcMemory [I] pointer to the source data
1848 * SrcFormat [I] format of the source pixel data
1849 * SrcPitch [I] number of bytes in a row
1850 * pSrcPalette [I] palette used in the source image
1851 * pSrcRect [I] area of the source data to load
1852 * dwFilter [I] filter to apply on stretching
1853 * Colorkey [I] colorkey
1854 *
1855 * RETURNS
1856 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1857 * if pSrcMemory is NULL but the other parameters are valid
1858 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect is NULL or
1859 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1860 * if DestRect is invalid
1861 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1862 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1863 *
1864 * NOTES
1865 * pSrcRect specifies the dimensions of the source data;
1866 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1867 *
1868 */
1869 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1870 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1871 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1872 DWORD filter, D3DCOLOR color_key)
1873 {
1874 const struct pixel_format_desc *srcformatdesc, *destformatdesc;
1875 D3DSURFACE_DESC surfdesc;
1876 D3DLOCKED_RECT lockrect;
1877 struct volume src_size, dst_size;
1878 HRESULT ret = D3D_OK;
1879
1880 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s, %#x, 0x%08x)\n",
1881 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1882 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1883
1884 if (!dst_surface || !src_memory || !src_rect)
1885 {
1886 WARN("Invalid argument specified.\n");
1887 return D3DERR_INVALIDCALL;
1888 }
1889 if (src_format == D3DFMT_UNKNOWN
1890 || src_rect->left >= src_rect->right
1891 || src_rect->top >= src_rect->bottom)
1892 {
1893 WARN("Invalid src_format or src_rect.\n");
1894 return E_FAIL;
1895 }
1896
1897 if (filter == D3DX_DEFAULT)
1898 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1899
1900 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1901
1902 src_size.width = src_rect->right - src_rect->left;
1903 src_size.height = src_rect->bottom - src_rect->top;
1904 src_size.depth = 1;
1905 if (!dst_rect)
1906 {
1907 dst_size.width = surfdesc.Width;
1908 dst_size.height = surfdesc.Height;
1909 }
1910 else
1911 {
1912 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width
1913 || dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height
1914 || dst_rect->left < 0 || dst_rect->top < 0)
1915 {
1916 WARN("Invalid dst_rect specified.\n");
1917 return D3DERR_INVALIDCALL;
1918 }
1919 dst_size.width = dst_rect->right - dst_rect->left;
1920 dst_size.height = dst_rect->bottom - dst_rect->top;
1921 if (!dst_size.width || !dst_size.height)
1922 return D3D_OK;
1923 }
1924 dst_size.depth = 1;
1925
1926 srcformatdesc = get_format_info(src_format);
1927 destformatdesc = get_format_info(surfdesc.Format);
1928 if (srcformatdesc->type == FORMAT_UNKNOWN || destformatdesc->type == FORMAT_UNKNOWN)
1929 {
1930 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format, surfdesc.Format);
1931 return E_NOTIMPL;
1932 }
1933
1934 if (src_format == surfdesc.Format
1935 && dst_size.width == src_size.width
1936 && dst_size.height == src_size.height
1937 && color_key == 0) /* Simple copy. */
1938 {
1939 if (src_rect->left & (srcformatdesc->block_width - 1)
1940 || src_rect->top & (srcformatdesc->block_height - 1)
1941 || (src_rect->right & (srcformatdesc->block_width - 1)
1942 && src_size.width != surfdesc.Width)
1943 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1944 && src_size.height != surfdesc.Height))
1945 {
1946 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1947 return D3DXERR_INVALIDDATA;
1948 }
1949
1950 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1951 return D3DXERR_INVALIDDATA;
1952
1953 copy_pixels(src_memory, src_pitch, 0, lockrect.pBits, lockrect.Pitch, 0,
1954 &src_size, srcformatdesc);
1955
1956 IDirect3DSurface9_UnlockRect(dst_surface);
1957 }
1958 else /* Stretching or format conversion. */
1959 {
1960 dxtn_conversion_func pre_convert, post_convert;
1961 void *tmp_src_memory = NULL, *tmp_dst_memory = NULL;
1962 UINT tmp_src_pitch, tmp_dst_pitch;
1963
1964 pre_convert = get_dxtn_conversion_func(srcformatdesc->format, FALSE);
1965 post_convert = get_dxtn_conversion_func(destformatdesc->format, TRUE);
1966
1967 if ((!pre_convert && !is_conversion_from_supported(srcformatdesc)) ||
1968 (!post_convert && !is_conversion_to_supported(destformatdesc)))
1969 {
1970 FIXME("Unsupported format conversion %#x -> %#x.\n", src_format, surfdesc.Format);
1971 return E_NOTIMPL;
1972 }
1973
1974 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1975 return D3DXERR_INVALIDDATA;
1976
1977 /* handle pre-conversion */
1978 if (pre_convert)
1979 {
1980 tmp_src_memory = HeapAlloc(GetProcessHeap(), 0, src_size.width * src_size.height * sizeof(DWORD));
1981 if (!tmp_src_memory)
1982 {
1983 ret = E_OUTOFMEMORY;
1984 goto error;
1985 }
1986 tmp_src_pitch = src_size.width * sizeof(DWORD);
1987 if (!pre_convert(src_memory, tmp_src_memory, src_pitch, tmp_src_pitch,
1988 WINED3DFMT_B8G8R8A8_UNORM, src_size.width, src_size.height))
1989 {
1990 ret = E_FAIL;
1991 goto error;
1992 }
1993 srcformatdesc = get_format_info(D3DFMT_A8R8G8B8);
1994 }
1995 else
1996 {
1997 tmp_src_memory = (void *)src_memory;
1998 tmp_src_pitch = src_pitch;
1999 }
2000
2001 /* handle post-conversion */
2002 if (post_convert)
2003 {
2004 tmp_dst_memory = HeapAlloc(GetProcessHeap(), 0, dst_size.width * dst_size.height * sizeof(DWORD));
2005 if (!tmp_dst_memory)
2006 {
2007 ret = E_OUTOFMEMORY;
2008 goto error;
2009 }
2010 tmp_dst_pitch = dst_size.width * sizeof(DWORD);
2011 destformatdesc = get_format_info(D3DFMT_A8R8G8B8);
2012 }
2013 else
2014 {
2015 tmp_dst_memory = lockrect.pBits;
2016 tmp_dst_pitch = lockrect.Pitch;
2017 }
2018
2019 if ((filter & 0xf) == D3DX_FILTER_NONE)
2020 {
2021 convert_argb_pixels(tmp_src_memory, tmp_src_pitch, 0, &src_size, srcformatdesc,
2022 tmp_dst_memory, tmp_dst_pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
2023 }
2024 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
2025 {
2026 if ((filter & 0xf) != D3DX_FILTER_POINT)
2027 FIXME("Unhandled filter %#x.\n", filter);
2028
2029 /* Always apply a point filter until D3DX_FILTER_LINEAR,
2030 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
2031 point_filter_argb_pixels(tmp_src_memory, tmp_src_pitch, 0, &src_size, srcformatdesc,
2032 tmp_dst_memory, tmp_dst_pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
2033 }
2034
2035 /* handle post-conversion */
2036 if (post_convert)
2037 {
2038 if (!post_convert(tmp_dst_memory, lockrect.pBits, tmp_dst_pitch, lockrect.Pitch,
2039 WINED3DFMT_B8G8R8A8_UNORM, dst_size.width, dst_size.height))
2040 {
2041 ret = E_FAIL;
2042 goto error;
2043 }
2044 }
2045
2046 error:
2047 if (pre_convert)
2048 HeapFree(GetProcessHeap(), 0, tmp_src_memory);
2049 if (post_convert)
2050 HeapFree(GetProcessHeap(), 0, tmp_dst_memory);
2051 IDirect3DSurface9_UnlockRect(dst_surface);
2052 }
2053
2054 return ret;
2055 }
2056
2057 /************************************************************
2058 * D3DXLoadSurfaceFromSurface
2059 *
2060 * Copies the contents from one surface to another, performing any required
2061 * format conversion, resizing or filtering.
2062 *
2063 * PARAMS
2064 * pDestSurface [I] pointer to the destination surface
2065 * pDestPalette [I] palette to use
2066 * pDestRect [I] to be filled area of the surface
2067 * pSrcSurface [I] pointer to the source surface
2068 * pSrcPalette [I] palette used for the source surface
2069 * pSrcRect [I] area of the source data to load
2070 * dwFilter [I] filter to apply on resizing
2071 * Colorkey [I] any ARGB value or 0 to disable color-keying
2072 *
2073 * RETURNS
2074 * Success: D3D_OK
2075 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface is NULL
2076 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
2077 *
2078 */
2079 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
2080 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
2081 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
2082 {
2083 RECT rect;
2084 D3DLOCKED_RECT lock;
2085 D3DSURFACE_DESC SrcDesc;
2086 HRESULT hr;
2087
2088 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
2089 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
2090 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
2091 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
2092
2093 if (!dst_surface || !src_surface)
2094 return D3DERR_INVALIDCALL;
2095
2096 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
2097
2098 if (!src_rect)
2099 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
2100 else
2101 rect = *src_rect;
2102
2103 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
2104 return D3DXERR_INVALIDDATA;
2105
2106 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
2107 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
2108
2109 IDirect3DSurface9_UnlockRect(src_surface);
2110
2111 return hr;
2112 }
2113
2114
2115 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
2116 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
2117 {
2118 int len;
2119 WCHAR *filename;
2120 HRESULT hr;
2121 ID3DXBuffer *buffer;
2122
2123 TRACE("(%s, %#x, %p, %p, %s): relay\n",
2124 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
2125
2126 if (!dst_filename) return D3DERR_INVALIDCALL;
2127
2128 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
2129 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2130 if (!filename) return E_OUTOFMEMORY;
2131 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
2132
2133 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
2134 if (SUCCEEDED(hr))
2135 {
2136 hr = write_buffer_to_file(filename, buffer);
2137 ID3DXBuffer_Release(buffer);
2138 }
2139
2140 HeapFree(GetProcessHeap(), 0, filename);
2141 return hr;
2142 }
2143
2144 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
2145 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
2146 {
2147 HRESULT hr;
2148 ID3DXBuffer *buffer;
2149
2150 TRACE("(%s, %#x, %p, %p, %s): relay\n",
2151 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
2152
2153 if (!dst_filename) return D3DERR_INVALIDCALL;
2154
2155 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
2156 if (SUCCEEDED(hr))
2157 {
2158 hr = write_buffer_to_file(dst_filename, buffer);
2159 ID3DXBuffer_Release(buffer);
2160 }
2161
2162 return hr;
2163 }
2164
2165 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
2166 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
2167 {
2168 IWICBitmapEncoder *encoder = NULL;
2169 IWICBitmapFrameEncode *frame = NULL;
2170 IPropertyBag2 *encoder_options = NULL;
2171 IStream *stream = NULL;
2172 HRESULT hr;
2173 HRESULT initresult;
2174 const CLSID *encoder_clsid;
2175 const GUID *pixel_format_guid;
2176 WICPixelFormatGUID wic_pixel_format;
2177 D3DFORMAT d3d_pixel_format;
2178 D3DSURFACE_DESC src_surface_desc;
2179 D3DLOCKED_RECT locked_rect;
2180 int width, height;
2181 STATSTG stream_stats;
2182 HGLOBAL stream_hglobal;
2183 ID3DXBuffer *buffer;
2184 DWORD size;
2185
2186 TRACE("(%p, %#x, %p, %p, %s)\n",
2187 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
2188
2189 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
2190
2191 if (src_palette)
2192 {
2193 FIXME("Saving surfaces with palettized pixel formats is not implemented yet\n");
2194 return D3DERR_INVALIDCALL;
2195 }
2196
2197 switch (file_format)
2198 {
2199 case D3DXIFF_BMP:
2200 case D3DXIFF_DIB:
2201 encoder_clsid = &CLSID_WICBmpEncoder;
2202 break;
2203 case D3DXIFF_PNG:
2204 encoder_clsid = &CLSID_WICPngEncoder;
2205 break;
2206 case D3DXIFF_JPG:
2207 encoder_clsid = &CLSID_WICJpegEncoder;
2208 break;
2209 case D3DXIFF_DDS:
2210 return save_dds_surface_to_memory(dst_buffer, src_surface, src_rect);
2211 case D3DXIFF_HDR:
2212 case D3DXIFF_PFM:
2213 case D3DXIFF_TGA:
2214 case D3DXIFF_PPM:
2215 FIXME("File format %#x is not supported yet\n", file_format);
2216 return E_NOTIMPL;
2217 default:
2218 return D3DERR_INVALIDCALL;
2219 }
2220
2221 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
2222 if (src_rect)
2223 {
2224 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
2225 {
2226 WARN("Invalid rectangle with 0 area\n");
2227 return D3DXCreateBuffer(64, dst_buffer);
2228 }
2229 if (src_rect->left < 0 || src_rect->top < 0)
2230 return D3DERR_INVALIDCALL;
2231 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
2232 return D3DERR_INVALIDCALL;
2233 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
2234 return D3DERR_INVALIDCALL;
2235
2236 width = src_rect->right - src_rect->left;
2237 height = src_rect->bottom - src_rect->top;
2238 }
2239 else
2240 {
2241 width = src_surface_desc.Width;
2242 height = src_surface_desc.Height;
2243 }
2244
2245 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2246
2247 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
2248 &IID_IWICBitmapEncoder, (void **)&encoder);
2249 if (FAILED(hr)) goto cleanup_err;
2250
2251 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
2252 if (FAILED(hr)) goto cleanup_err;
2253
2254 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2255 if (FAILED(hr)) goto cleanup_err;
2256
2257 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
2258 if (FAILED(hr)) goto cleanup_err;
2259
2260 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
2261 if (FAILED(hr)) goto cleanup_err;
2262
2263 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
2264 if (FAILED(hr)) goto cleanup_err;
2265
2266 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
2267 if (!pixel_format_guid)
2268 {
2269 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
2270 hr = E_NOTIMPL;
2271 goto cleanup;
2272 }
2273
2274 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
2275 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
2276 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
2277 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
2278 {
2279 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
2280
2281 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
2282 {
2283 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2284 if (SUCCEEDED(hr))
2285 {
2286 IWICBitmapFrameEncode_WritePixels(frame, height,
2287 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
2288 IDirect3DSurface9_UnlockRect(src_surface);
2289 }
2290 }
2291 else /* Pixel format conversion */
2292 {
2293 const struct pixel_format_desc *src_format_desc, *dst_format_desc;
2294 struct volume size;
2295 DWORD dst_pitch;
2296 void *dst_data;
2297
2298 src_format_desc = get_format_info(src_surface_desc.Format);
2299 dst_format_desc = get_format_info(d3d_pixel_format);
2300 if (!is_conversion_from_supported(src_format_desc)
2301 || !is_conversion_to_supported(dst_format_desc))
2302 {
2303 FIXME("Unsupported format conversion %#x -> %#x.\n",
2304 src_surface_desc.Format, d3d_pixel_format);
2305 hr = E_NOTIMPL;
2306 goto cleanup;
2307 }
2308
2309 size.width = width;
2310 size.height = height;
2311 size.depth = 1;
2312 dst_pitch = width * dst_format_desc->bytes_per_pixel;
2313 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
2314 if (!dst_data)
2315 {
2316 hr = E_OUTOFMEMORY;
2317 goto cleanup;
2318 }
2319
2320 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2321 if (SUCCEEDED(hr))
2322 {
2323 convert_argb_pixels(locked_rect.pBits, locked_rect.Pitch, 0, &size, src_format_desc,
2324 dst_data, dst_pitch, 0, &size, dst_format_desc, 0, NULL);
2325 IDirect3DSurface9_UnlockRect(src_surface);
2326 }
2327
2328 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
2329 HeapFree(GetProcessHeap(), 0, dst_data);
2330 }
2331
2332 hr = IWICBitmapFrameEncode_Commit(frame);
2333 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
2334 }
2335 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
2336
2337 /* copy data from stream to ID3DXBuffer */
2338 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
2339 if (FAILED(hr)) goto cleanup_err;
2340
2341 if (stream_stats.cbSize.u.HighPart != 0)
2342 {
2343 hr = D3DXERR_INVALIDDATA;
2344 goto cleanup;
2345 }
2346 size = stream_stats.cbSize.u.LowPart;
2347
2348 /* Remove BMP header for DIB */
2349 if (file_format == D3DXIFF_DIB)
2350 size -= sizeof(BITMAPFILEHEADER);
2351
2352 hr = D3DXCreateBuffer(size, &buffer);
2353 if (FAILED(hr)) goto cleanup;
2354
2355 hr = GetHGlobalFromStream(stream, &stream_hglobal);
2356 if (SUCCEEDED(hr))
2357 {
2358 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
2359 void *stream_data = GlobalLock(stream_hglobal);
2360 /* Remove BMP header for DIB */
2361 if (file_format == D3DXIFF_DIB)
2362 stream_data = (void*)((BYTE*)stream_data + sizeof(BITMAPFILEHEADER));
2363 memcpy(buffer_pointer, stream_data, size);
2364 GlobalUnlock(stream_hglobal);
2365 *dst_buffer = buffer;
2366 }
2367 else ID3DXBuffer_Release(buffer);
2368
2369 cleanup_err:
2370 if (FAILED(hr) && hr != E_OUTOFMEMORY)
2371 hr = D3DERR_INVALIDCALL;
2372
2373 cleanup:
2374 if (stream) IStream_Release(stream);
2375
2376 if (frame) IWICBitmapFrameEncode_Release(frame);
2377 if (encoder_options) IPropertyBag2_Release(encoder_options);
2378
2379 if (encoder) IWICBitmapEncoder_Release(encoder);
2380
2381 if (SUCCEEDED(initresult)) CoUninitialize();
2382
2383 return hr;
2384 }