2 * Copyright (C) 2009-2010 Tony Wasserka
3 * Copyright (C) 2012 Józef Kucia
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.
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.
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
21 #include "wine/debug.h"
22 #include "wine/unicode.h"
23 #include "d3dx9_36_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
32 /* Wine-specific WIC GUIDs */
33 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
39 } wic_pixel_formats[] = {
40 { &GUID_WICPixelFormat8bppIndexed, D3DFMT_L8 },
41 { &GUID_WICPixelFormat1bppIndexed, D3DFMT_L8 },
42 { &GUID_WICPixelFormat4bppIndexed, D3DFMT_L8 },
43 { &GUID_WICPixelFormat16bppBGR555, D3DFMT_X1R5G5B5 },
44 { &GUID_WICPixelFormat16bppBGR565, D3DFMT_R5G6B5 },
45 { &GUID_WICPixelFormat24bppBGR, D3DFMT_R8G8B8 },
46 { &GUID_WICPixelFormat32bppBGR, D3DFMT_X8R8G8B8 },
47 { &GUID_WICPixelFormat32bppBGRA, D3DFMT_A8R8G8B8 }
50 static D3DFORMAT wic_guid_to_d3dformat(const GUID *guid)
54 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
56 if (IsEqualGUID(wic_pixel_formats[i].wic_guid, guid))
57 return wic_pixel_formats[i].d3dformat;
60 return D3DFMT_UNKNOWN;
63 static const GUID *d3dformat_to_wic_guid(D3DFORMAT format)
67 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
69 if (wic_pixel_formats[i].d3dformat == format)
70 return wic_pixel_formats[i].wic_guid;
76 /* dds_header.flags */
78 #define DDS_HEIGHT 0x2
81 #define DDS_PIXELFORMAT 0x1000
82 #define DDS_MIPMAPCOUNT 0x20000
83 #define DDS_LINEARSIZE 0x80000
84 #define DDS_DEPTH 0x800000
87 #define DDS_CAPS_COMPLEX 0x8
88 #define DDS_CAPS_TEXTURE 0x1000
89 #define DDS_CAPS_MIPMAP 0x400000
91 /* dds_header.caps2 */
92 #define DDS_CAPS2_CUBEMAP 0x200
93 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
94 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
95 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
96 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
97 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
98 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
99 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
100 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
101 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
102 #define DDS_CAPS2_VOLUME 0x200000
104 /* dds_pixel_format.flags */
105 #define DDS_PF_ALPHA 0x1
106 #define DDS_PF_ALPHA_ONLY 0x2
107 #define DDS_PF_FOURCC 0x4
108 #define DDS_PF_RGB 0x40
109 #define DDS_PF_YUV 0x200
110 #define DDS_PF_LUMINANCE 0x20000
111 #define DDS_PF_BUMPDUDV 0x80000
113 struct dds_pixel_format
132 DWORD pitch_or_linear_size;
136 struct dds_pixel_format pixel_format;
144 static D3DFORMAT dds_fourcc_to_d3dformat(DWORD fourcc)
147 static const DWORD known_fourcc[] = {
148 MAKEFOURCC('U','Y','V','Y'),
149 MAKEFOURCC('Y','U','Y','2'),
150 MAKEFOURCC('R','G','B','G'),
151 MAKEFOURCC('G','R','G','B'),
152 MAKEFOURCC('D','X','T','1'),
153 MAKEFOURCC('D','X','T','2'),
154 MAKEFOURCC('D','X','T','3'),
155 MAKEFOURCC('D','X','T','4'),
156 MAKEFOURCC('D','X','T','5')
159 for (i = 0; i < sizeof(known_fourcc) / sizeof(known_fourcc[0]); i++)
161 if (known_fourcc[i] == fourcc)
165 WARN("Unknown FourCC %#x\n", fourcc);
166 return D3DFMT_UNKNOWN;
169 static const struct {
176 } rgb_pixel_formats[] = {
177 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 },
178 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 },
179 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 },
180 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 },
181 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 },
182 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 },
183 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 },
184 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 },
185 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 },
186 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 },
187 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 },
188 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 },
189 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 },
190 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8 },
191 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8 },
194 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
198 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
200 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
201 && rgb_pixel_formats[i].rmask == pixel_format->rmask
202 && rgb_pixel_formats[i].gmask == pixel_format->gmask
203 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
205 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
206 return rgb_pixel_formats[i].format;
207 if (rgb_pixel_formats[i].amask == 0)
208 return rgb_pixel_formats[i].format;
212 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
213 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
214 return D3DFMT_UNKNOWN;
217 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
219 if (pixel_format->bpp == 8)
221 if (pixel_format->rmask == 0xff)
223 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
226 if (pixel_format->bpp == 16)
228 if (pixel_format->rmask == 0xffff)
230 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
234 WARN("Unknown luminance pixel format (bpp %#x, l %#x, a %#x)\n",
235 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
236 return D3DFMT_UNKNOWN;
239 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
241 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
244 WARN("Unknown Alpha pixel format (%#x, %#x)\n", pixel_format->bpp, pixel_format->rmask);
245 return D3DFMT_UNKNOWN;
248 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
250 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00)
252 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000)
253 return D3DFMT_V16U16;
255 WARN("Unknown bump pixel format (%#x, %#x, %#x, %#x, %#x)\n", pixel_format->bpp,
256 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
257 return D3DFMT_UNKNOWN;
260 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
262 if (pixel_format->flags & DDS_PF_FOURCC)
263 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
264 if (pixel_format->flags & DDS_PF_RGB)
265 return dds_rgb_to_d3dformat(pixel_format);
266 if (pixel_format->flags & DDS_PF_LUMINANCE)
267 return dds_luminance_to_d3dformat(pixel_format);
268 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
269 return dds_alpha_to_d3dformat(pixel_format);
270 if (pixel_format->flags & DDS_PF_BUMPDUDV)
271 return dds_bump_to_d3dformat(pixel_format);
273 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %#x, r %#x, g %#x, b %#x, a %#x)\n",
274 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
275 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
276 return D3DFMT_UNKNOWN;
279 static HRESULT d3dformat_to_dds_pixel_format(struct dds_pixel_format *pixel_format, D3DFORMAT d3dformat)
283 memset(pixel_format, 0, sizeof(*pixel_format));
285 pixel_format->size = sizeof(*pixel_format);
287 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
289 if (rgb_pixel_formats[i].format == d3dformat)
291 pixel_format->flags |= DDS_PF_RGB;
292 pixel_format->bpp = rgb_pixel_formats[i].bpp;
293 pixel_format->rmask = rgb_pixel_formats[i].rmask;
294 pixel_format->gmask = rgb_pixel_formats[i].gmask;
295 pixel_format->bmask = rgb_pixel_formats[i].bmask;
296 pixel_format->amask = rgb_pixel_formats[i].amask;
297 if (pixel_format->amask) pixel_format->flags |= DDS_PF_ALPHA;
302 WARN("Unknown pixel format %#x\n", d3dformat);
306 static HRESULT calculate_dds_surface_size(D3DFORMAT format, UINT width, UINT height,
307 UINT *pitch, UINT *size)
309 const struct pixel_format_desc *format_desc = get_format_info(format);
310 if (format_desc->type == FORMAT_UNKNOWN)
313 if (format_desc->block_width != 1 || format_desc->block_height != 1)
315 *pitch = format_desc->block_byte_count
316 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
318 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
322 *pitch = width * format_desc->bytes_per_pixel;
323 *size = *pitch * height;
329 static UINT calculate_dds_file_size(D3DFORMAT format, UINT width, UINT height, UINT depth,
330 UINT miplevels, UINT faces)
332 UINT i, file_size = 0;
334 for (i = 0; i < miplevels; i++)
336 UINT pitch, size = 0;
337 calculate_dds_surface_size(format, width, height, &pitch, &size);
340 width = max(1, width / 2);
341 height = max(1, height / 2);
342 depth = max(1, depth / 2);
346 file_size += sizeof(struct dds_header);
350 /************************************************************
351 * get_image_info_from_dds
353 * Fills a D3DXIMAGE_INFO structure with information
354 * about a DDS file stored in the memory.
357 * buffer [I] pointer to DDS data
358 * length [I] size of DDS data
359 * info [O] pointer to D3DXIMAGE_INFO structure
363 * Failure: D3DXERR_INVALIDDATA
366 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
369 UINT expected_length;
370 const struct dds_header *header = buffer;
372 if (length < sizeof(*header) || !info)
373 return D3DXERR_INVALIDDATA;
375 if (header->pixel_format.size != sizeof(header->pixel_format))
376 return D3DXERR_INVALIDDATA;
378 info->Width = header->width;
379 info->Height = header->height;
381 info->MipLevels = (header->flags & DDS_MIPMAPCOUNT) ? header->miplevels : 1;
383 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
384 if (info->Format == D3DFMT_UNKNOWN)
385 return D3DXERR_INVALIDDATA;
387 TRACE("Pixel format is %#x\n", info->Format);
389 if (header->caps2 & DDS_CAPS2_VOLUME)
391 info->Depth = header->depth;
392 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
394 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
398 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
400 if (header->caps2 & face)
403 info->ResourceType = D3DRTYPE_CUBETEXTURE;
407 info->ResourceType = D3DRTYPE_TEXTURE;
410 expected_length = calculate_dds_file_size(info->Format, info->Width, info->Height, info->Depth,
411 info->MipLevels, faces);
412 if (length < expected_length)
414 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
415 return D3DXERR_INVALIDDATA;
418 info->ImageFileFormat = D3DXIFF_DDS;
422 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
423 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
424 const D3DXIMAGE_INFO *src_info)
428 const struct dds_header *header = src_data;
429 const BYTE *pixels = (BYTE *)(header + 1);
431 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
432 return D3DXERR_INVALIDDATA;
434 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &src_pitch, &size)))
437 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
438 src_pitch, NULL, src_rect, filter, color_key);
441 static HRESULT save_dds_surface_to_memory(ID3DXBuffer **dst_buffer, IDirect3DSurface9 *src_surface, const RECT *src_rect)
444 UINT dst_pitch, surface_size, file_size;
445 D3DSURFACE_DESC src_desc;
446 D3DLOCKED_RECT locked_rect;
448 struct dds_header *header;
450 struct volume volume;
451 const struct pixel_format_desc *pixel_format;
455 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
459 hr = IDirect3DSurface9_GetDesc(src_surface, &src_desc);
460 if (FAILED(hr)) return hr;
462 pixel_format = get_format_info(src_desc.Format);
463 if (pixel_format->type == FORMAT_UNKNOWN) return E_NOTIMPL;
465 file_size = calculate_dds_file_size(src_desc.Format, src_desc.Width, src_desc.Height, 1, 1, 1);
467 hr = calculate_dds_surface_size(src_desc.Format, src_desc.Width, src_desc.Height, &dst_pitch, &surface_size);
468 if (FAILED(hr)) return hr;
470 hr = D3DXCreateBuffer(file_size, &buffer);
471 if (FAILED(hr)) return hr;
473 header = ID3DXBuffer_GetBufferPointer(buffer);
474 pixels = (BYTE *)(header + 1);
476 memset(header, 0, sizeof(*header));
477 header->signature = MAKEFOURCC('D','D','S',' ');
478 header->size = sizeof(*header);
479 header->flags = DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PITCH | DDS_PIXELFORMAT | DDS_MIPMAPCOUNT;
480 header->height = src_desc.Height;
481 header->width = src_desc.Width;
482 header->pitch_or_linear_size = dst_pitch;
484 header->miplevels = 1;
485 header->caps = DDS_CAPS_TEXTURE;
486 hr = d3dformat_to_dds_pixel_format(&header->pixel_format, src_desc.Format);
489 ID3DXBuffer_Release(buffer);
493 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, NULL, D3DLOCK_READONLY);
496 ID3DXBuffer_Release(buffer);
500 volume.width = src_desc.Width;
501 volume.height = src_desc.Height;
503 copy_pixels(locked_rect.pBits, locked_rect.Pitch, 0, pixels, dst_pitch, 0,
504 &volume, pixel_format);
506 IDirect3DSurface9_UnlockRect(src_surface);
508 *dst_buffer = buffer;
512 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette,
513 const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key,
514 const D3DXIMAGE_INFO *src_info)
516 UINT row_pitch, slice_pitch;
517 const struct dds_header *header = src_data;
518 const BYTE *pixels = (BYTE *)(header + 1);
520 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
521 return D3DXERR_INVALIDDATA;
523 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &row_pitch, &slice_pitch)))
526 return D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box, pixels, src_info->Format,
527 row_pitch, slice_pitch, NULL, src_box, filter, color_key);
530 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
531 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info)
541 IDirect3DSurface9 *surface;
542 const struct dds_header *header = src_data;
543 const BYTE *pixels = (BYTE *)(header + 1);
545 /* Loading a cube texture as a simple texture is also supported (only first face texture is taken) */
546 if ((src_info->ResourceType != D3DRTYPE_TEXTURE) && (src_info->ResourceType != D3DRTYPE_CUBETEXTURE))
547 return D3DXERR_INVALIDDATA;
549 width = src_info->Width;
550 height = src_info->Height;
551 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
552 for (mip_level = 0; mip_level < mip_levels; mip_level++)
554 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_pitch, &mip_level_size);
555 if (FAILED(hr)) return hr;
557 SetRect(&src_rect, 0, 0, width, height);
559 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level, &surface);
560 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
561 NULL, &src_rect, filter, color_key);
562 IDirect3DSurface9_Release(surface);
563 if (FAILED(hr)) return hr;
565 pixels += mip_level_size;
566 width = max(1, width / 2);
567 height = max(1, height / 2);
573 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
574 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
584 IDirect3DSurface9 *surface;
585 const struct dds_header *header = src_data;
586 const BYTE *pixels = (BYTE *)(header + 1);
588 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
589 return D3DXERR_INVALIDDATA;
591 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
593 WARN("Only full cubemaps are supported\n");
594 return D3DXERR_INVALIDDATA;
597 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
598 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
600 size = src_info->Width;
601 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
603 hr = calculate_dds_surface_size(src_info->Format, size, size, &src_pitch, &mip_level_size);
604 if (FAILED(hr)) return hr;
606 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
607 if (mip_level < mip_levels)
609 SetRect(&src_rect, 0, 0, size, size);
611 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
612 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
613 NULL, &src_rect, filter, color_key);
614 IDirect3DSurface9_Release(surface);
615 if (FAILED(hr)) return hr;
618 pixels += mip_level_size;
619 size = max(1, size / 2);
626 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
627 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
632 UINT src_slice_pitch;
635 UINT width, height, depth;
636 IDirect3DVolume9 *volume;
637 const struct dds_header *header = src_data;
638 const BYTE *pixels = (BYTE *)(header + 1);
640 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
641 return D3DXERR_INVALIDDATA;
643 width = src_info->Width;
644 height = src_info->Height;
645 depth = src_info->Depth;
646 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture));
648 for (mip_level = 0; mip_level < mip_levels; mip_level++)
650 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_row_pitch, &src_slice_pitch);
651 if (FAILED(hr)) return hr;
653 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume);
654 if (FAILED(hr)) return hr;
658 src_box.Right = width;
659 src_box.Bottom = height;
661 src_box.Back = depth;
663 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format,
664 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key);
666 IDirect3DVolume9_Release(volume);
667 if (FAILED(hr)) return hr;
669 pixels += depth * src_slice_pitch;
670 width = max(1, width / 2);
671 height = max(1, height / 2);
672 depth = max(1, depth / 2);
678 static BOOL convert_dib_to_bmp(void **data, UINT *size)
683 BITMAPFILEHEADER *header;
687 if ((*size < 4) || (*size < (header_size = *(ULONG*)*data)))
690 if ((header_size == sizeof(BITMAPINFOHEADER)) ||
691 (header_size == sizeof(BITMAPV4HEADER)) ||
692 (header_size == sizeof(BITMAPV5HEADER)) ||
693 (header_size == 64 /* sizeof(BITMAPCOREHEADER2) */))
695 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
696 BITMAPINFOHEADER *info_header = (BITMAPINFOHEADER*)*data;
697 count = info_header->biClrUsed;
699 if (!count && info_header->biBitCount <= 8)
700 count = 1 << info_header->biBitCount;
702 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBQUAD) * count;
704 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
705 if ((info_header->biSize == sizeof(BITMAPINFOHEADER)) && (info_header->biCompression == BI_BITFIELDS))
706 offset += 3 * sizeof(DWORD);
708 else if (header_size == sizeof(BITMAPCOREHEADER))
710 BITMAPCOREHEADER *core_header = (BITMAPCOREHEADER*)*data;
712 if (core_header->bcBitCount <= 8)
713 count = 1 << core_header->bcBitCount;
715 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBTRIPLE) * count;
722 TRACE("Converting DIB file to BMP\n");
724 new_size = *size + sizeof(BITMAPFILEHEADER);
725 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
726 CopyMemory(new_data + sizeof(BITMAPFILEHEADER), *data, *size);
729 header = (BITMAPFILEHEADER*)new_data;
730 header->bfType = 0x4d42; /* BM */
731 header->bfSize = new_size;
732 header->bfReserved1 = 0;
733 header->bfReserved2 = 0;
734 header->bfOffBits = offset;
736 /* Update input data */
743 /************************************************************
744 * D3DXGetImageInfoFromFileInMemory
746 * Fills a D3DXIMAGE_INFO structure with info about an image
749 * data [I] pointer to the image file data
750 * datasize [I] size of the passed data
751 * info [O] pointer to the destination structure
754 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
755 * if info is NULL and data and datasize are not NULL
756 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
757 * D3DERR_INVALIDCALL, if data is NULL or
761 * datasize may be bigger than the actual file size
764 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(const void *data, UINT datasize, D3DXIMAGE_INFO *info)
766 IWICImagingFactory *factory;
767 IWICBitmapDecoder *decoder = NULL;
773 TRACE("(%p, %d, %p)\n", data, datasize, info);
775 if (!data || !datasize)
776 return D3DERR_INVALIDCALL;
781 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
782 TRACE("File type is DDS\n");
783 return get_image_info_from_dds(data, datasize, info);
786 /* In case of DIB file, convert it to BMP */
787 dib = convert_dib_to_bmp((void**)&data, &datasize);
789 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
791 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
794 IWICImagingFactory_CreateStream(factory, &stream);
795 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
796 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
797 IWICStream_Release(stream);
798 IWICImagingFactory_Release(factory);
802 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
803 FIXME("File type PPM is not supported yet\n");
804 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
805 FIXME("File type HDR is not supported yet\n");
806 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
807 FIXME("File type PFM is not supported yet\n");
811 GUID container_format;
814 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
816 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
818 TRACE("File type is DIB\n");
819 info->ImageFileFormat = D3DXIFF_DIB;
821 TRACE("File type is BMP\n");
822 info->ImageFileFormat = D3DXIFF_BMP;
824 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
825 TRACE("File type is PNG\n");
826 info->ImageFileFormat = D3DXIFF_PNG;
827 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
828 TRACE("File type is JPG\n");
829 info->ImageFileFormat = D3DXIFF_JPG;
830 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
831 TRACE("File type is TGA\n");
832 info->ImageFileFormat = D3DXIFF_TGA;
834 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
835 hr = D3DXERR_INVALIDDATA;
840 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
841 if (SUCCEEDED(hr) && !frame_count)
842 hr = D3DXERR_INVALIDDATA;
845 IWICBitmapFrameDecode *frame = NULL;
847 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
850 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
853 WICPixelFormatGUID pixel_format;
855 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
857 info->Format = wic_guid_to_d3dformat(&pixel_format);
858 if (info->Format == D3DFMT_UNKNOWN) {
859 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
860 hr = D3DXERR_INVALIDDATA;
866 IWICBitmapFrameDecode_Release(frame);
870 info->ResourceType = D3DRTYPE_TEXTURE;
875 IWICBitmapDecoder_Release(decoder);
877 if (SUCCEEDED(initresult))
881 HeapFree(GetProcessHeap(), 0, (void*)data);
884 TRACE("Invalid or unsupported image file\n");
885 return D3DXERR_INVALIDDATA;
891 /************************************************************
892 * D3DXGetImageInfoFromFile
895 * Success: D3D_OK, if we successfully load a valid image file or
896 * if we successfully load a file which is no valid image and info is NULL
897 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
898 * if file is not a valid image file and info is not NULL
899 * D3DERR_INVALIDCALL, if file is NULL
902 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
908 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
910 if( !file ) return D3DERR_INVALIDCALL;
912 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
913 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
914 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
916 hr = D3DXGetImageInfoFromFileW(widename, info);
917 HeapFree(GetProcessHeap(), 0, widename);
922 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
928 TRACE("(%s, %p): relay\n", debugstr_w(file), info);
930 if( !file ) return D3DERR_INVALIDCALL;
932 hr = map_view_of_file(file, &buffer, &size);
933 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
935 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
936 UnmapViewOfFile(buffer);
941 /************************************************************
942 * D3DXGetImageInfoFromResource
945 * Success: D3D_OK, if resource is a valid image file
946 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
947 * if we fail to load resource
950 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
954 TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
956 resinfo = FindResourceA(module, resource, (const char *)RT_RCDATA);
957 if (!resinfo) /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
958 resinfo = FindResourceA(module, resource, (const char *)RT_BITMAP);
966 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
967 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
968 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
971 return D3DXERR_INVALIDDATA;
974 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
978 TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
980 resinfo = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA);
981 if (!resinfo) /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
982 resinfo = FindResourceW(module, resource, (const WCHAR *)RT_BITMAP);
990 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
991 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
992 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
995 return D3DXERR_INVALIDDATA;
998 /************************************************************
999 * D3DXLoadSurfaceFromFileInMemory
1001 * Loads data from a given buffer into a surface and fills a given
1002 * D3DXIMAGE_INFO structure with info about the source data.
1005 * pDestSurface [I] pointer to the surface
1006 * pDestPalette [I] palette to use
1007 * pDestRect [I] to be filled area of the surface
1008 * pSrcData [I] pointer to the source data
1009 * SrcDataSize [I] size of the source data in bytes
1010 * pSrcRect [I] area of the source data to load
1011 * dwFilter [I] filter to apply on stretching
1012 * Colorkey [I] colorkey
1013 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1017 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
1018 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1021 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
1022 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
1023 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
1025 D3DXIMAGE_INFO imginfo;
1028 IWICImagingFactory *factory;
1029 IWICBitmapDecoder *decoder;
1030 IWICBitmapFrameDecode *bitmapframe;
1033 const struct pixel_format_desc *formatdesc;
1037 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1038 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1039 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
1040 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
1042 if (!pDestSurface || !pSrcData || !SrcDataSize)
1043 return D3DERR_INVALIDCALL;
1045 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
1052 wicrect.X = pSrcRect->left;
1053 wicrect.Y = pSrcRect->top;
1054 wicrect.Width = pSrcRect->right - pSrcRect->left;
1055 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
1061 wicrect.Width = imginfo.Width;
1062 wicrect.Height = imginfo.Height;
1065 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
1067 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
1069 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
1070 dwFilter, Colorkey, &imginfo);
1071 if (SUCCEEDED(hr) && pSrcInfo)
1072 *pSrcInfo = imginfo;
1076 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1078 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
1081 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
1083 IWICImagingFactory_Release(factory);
1087 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
1089 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
1091 IWICStream_Release(stream);
1092 IWICImagingFactory_Release(factory);
1097 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
1102 formatdesc = get_format_info(imginfo.Format);
1104 if (formatdesc->type == FORMAT_UNKNOWN)
1106 FIXME("Unsupported pixel format\n");
1107 hr = D3DXERR_INVALIDDATA;
1114 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
1115 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
1117 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
1118 pitch * wicrect.Height, buffer);
1122 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1123 buffer, imginfo.Format, pitch,
1124 NULL, &rect, dwFilter, Colorkey);
1127 HeapFree(GetProcessHeap(), 0, buffer);
1130 IWICBitmapFrameDecode_Release(bitmapframe);
1133 IWICBitmapDecoder_Release(decoder);
1139 return D3DXERR_INVALIDDATA;
1142 *pSrcInfo = imginfo;
1147 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
1148 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
1149 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1155 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1156 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1157 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
1158 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1160 if (!src_file || !dst_surface)
1161 return D3DERR_INVALIDCALL;
1163 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
1164 pWidename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*pWidename));
1165 MultiByteToWideChar(CP_ACP, 0, src_file, -1, pWidename, strlength);
1167 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
1168 pWidename, src_rect, filter, color_key, src_info);
1169 HeapFree(GetProcessHeap(), 0, pWidename);
1174 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
1175 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
1176 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1182 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1183 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1184 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
1185 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1187 if (!src_file || !dst_surface)
1188 return D3DERR_INVALIDCALL;
1190 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
1191 return D3DXERR_INVALIDDATA;
1193 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1194 data, data_size, src_rect, filter, color_key, src_info);
1195 UnmapViewOfFile(data);
1200 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
1201 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
1202 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1206 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1207 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1208 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
1209 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1212 return D3DERR_INVALIDCALL;
1214 hResInfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA);
1215 if (!hResInfo) /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1216 hResInfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP);
1223 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
1224 return D3DXERR_INVALIDDATA;
1226 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1227 data, data_size, src_rect, filter, color_key, src_info);
1230 return D3DXERR_INVALIDDATA;
1233 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
1234 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
1235 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1239 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1240 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1241 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
1242 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1245 return D3DERR_INVALIDCALL;
1247 hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA);
1248 if (!hResInfo) /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1249 hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP);
1256 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
1257 return D3DXERR_INVALIDDATA;
1259 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1260 data, data_size, src_rect, filter, color_key, src_info);
1263 return D3DXERR_INVALIDDATA;
1267 /************************************************************
1268 * helper functions for D3DXLoadSurfaceFromMemory
1270 struct argb_conversion_info
1272 const struct pixel_format_desc *srcformat;
1273 const struct pixel_format_desc *destformat;
1274 DWORD srcshift[4], destshift[4];
1275 DWORD srcmask[4], destmask[4];
1276 BOOL process_channel[4];
1280 static void init_argb_conversion_info(const struct pixel_format_desc *srcformat, const struct pixel_format_desc *destformat, struct argb_conversion_info *info)
1283 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1284 info->channelmask = 0;
1286 info->srcformat = srcformat;
1287 info->destformat = destformat;
1289 for(i = 0;i < 4;i++) {
1290 /* srcshift is used to extract the _relevant_ components */
1291 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1293 /* destshift is used to move the components to the correct position */
1294 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1296 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1297 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1299 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1300 if(destformat->bits[i]) {
1301 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1302 else info->channelmask |= info->destmask[i];
1307 static DWORD dword_from_bytes(CONST BYTE *src, UINT bytes_per_pixel)
1310 static BOOL fixme_once;
1312 if(bytes_per_pixel > sizeof(DWORD)) {
1313 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1314 bytes_per_pixel = sizeof(DWORD);
1317 memcpy(&ret, src, bytes_per_pixel);
1321 static void dword_to_bytes(BYTE *dst, DWORD dword, UINT bytes_per_pixel)
1323 static BOOL fixme_once;
1325 if(bytes_per_pixel > sizeof(DWORD)) {
1326 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1327 ZeroMemory(dst, bytes_per_pixel);
1328 bytes_per_pixel = sizeof(DWORD);
1331 memcpy(dst, &dword, bytes_per_pixel);
1334 /************************************************************
1335 * get_relevant_argb_components
1337 * Extracts the relevant components from the source color and
1338 * drops the less significant bits if they aren't used by the destination format.
1340 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
1344 if(info->process_channel[i])
1345 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
1348 /************************************************************
1351 * Recombines the output of get_relevant_argb_components and converts
1352 * it to the destination format.
1354 static DWORD make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in)
1359 for(i = 0;i < 4;i++) {
1360 if(info->process_channel[i]) {
1361 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1363 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1364 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1367 val |= info->channelmask; /* new channels are set to their maximal value */
1371 static void format_to_vec4(const struct pixel_format_desc *format, const DWORD *src, struct vec4 *dst)
1375 if (format->bits[1])
1377 mask = (1 << format->bits[1]) - 1;
1378 dst->x = (float)((*src >> format->shift[1]) & mask) / mask;
1383 if (format->bits[2])
1385 mask = (1 << format->bits[2]) - 1;
1386 dst->y = (float)((*src >> format->shift[2]) & mask) / mask;
1391 if (format->bits[3])
1393 mask = (1 << format->bits[3]) - 1;
1394 dst->z = (float)((*src >> format->shift[3]) & mask) / mask;
1399 if (format->bits[0])
1401 mask = (1 << format->bits[0]) - 1;
1402 dst->w = (float)((*src >> format->shift[0]) & mask) / mask;
1408 static void format_from_vec4(const struct pixel_format_desc *format, const struct vec4 *src, DWORD *dst)
1412 if (format->bits[1])
1413 *dst |= (DWORD)(src->x * ((1 << format->bits[1]) - 1) + 0.5f) << format->shift[1];
1414 if (format->bits[2])
1415 *dst |= (DWORD)(src->y * ((1 << format->bits[2]) - 1) + 0.5f) << format->shift[2];
1416 if (format->bits[3])
1417 *dst |= (DWORD)(src->z * ((1 << format->bits[3]) - 1) + 0.5f) << format->shift[3];
1418 if (format->bits[0])
1419 *dst |= (DWORD)(src->w * ((1 << format->bits[0]) - 1) + 0.5f) << format->shift[0];
1422 /************************************************************
1425 * Copies the source buffer to the destination buffer.
1426 * Works for any pixel format.
1427 * The source and the destination must be block-aligned.
1429 void copy_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
1430 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *size,
1431 const struct pixel_format_desc *format)
1435 const BYTE *src_addr;
1436 UINT row_block_count = (size->width + format->block_width - 1) / format->block_width;
1437 UINT row_count = (size->height + format->block_height - 1) / format->block_height;
1439 for (slice = 0; slice < size->depth; slice++)
1441 src_addr = src + slice * src_slice_pitch;
1442 dst_addr = dst + slice * dst_slice_pitch;
1444 for (row = 0; row < row_count; row++)
1446 memcpy(dst_addr, src_addr, row_block_count * format->block_byte_count);
1447 src_addr += src_row_pitch;
1448 dst_addr += dst_row_pitch;
1453 /************************************************************
1454 * convert_argb_pixels
1456 * Copies the source buffer to the destination buffer, performing
1457 * any necessary format conversion and color keying.
1458 * Pixels outsize the source rect are blacked out.
1459 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1461 void convert_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1462 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1463 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key)
1465 struct argb_conversion_info conv_info, ck_conv_info;
1466 const struct pixel_format_desc *ck_format = NULL;
1467 DWORD channels[4], pixel;
1468 UINT min_width, min_height, min_depth;
1471 ZeroMemory(channels, sizeof(channels));
1472 init_argb_conversion_info(src_format, dst_format, &conv_info);
1474 min_width = min(src_size->width, dst_size->width);
1475 min_height = min(src_size->height, dst_size->height);
1476 min_depth = min(src_size->depth, dst_size->depth);
1480 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1481 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1482 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1485 for (z = 0; z < min_depth; z++) {
1486 const BYTE *src_slice_ptr = src + z * src_slice_pitch;
1487 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1489 for (y = 0; y < min_height; y++) {
1490 const BYTE *src_ptr = src_slice_ptr + y * src_row_pitch;
1491 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1494 for (x = 0; x < min_width; x++) {
1495 /* extract source color components */
1496 pixel = dword_from_bytes(src_ptr, src_format->bytes_per_pixel);
1498 if (!src_format->to_rgba && !dst_format->from_rgba)
1500 get_relevant_argb_components(&conv_info, pixel, channels);
1501 val = make_argb_color(&conv_info, channels);
1505 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1506 pixel = make_argb_color(&ck_conv_info, channels);
1507 if (pixel == color_key)
1508 val &= ~conv_info.destmask[0];
1513 struct vec4 color, tmp;
1515 format_to_vec4(src_format, &pixel, &color);
1516 if (src_format->to_rgba)
1517 src_format->to_rgba(&color, &tmp);
1523 format_from_vec4(ck_format, &tmp, &pixel);
1524 if (pixel == color_key)
1528 if (dst_format->from_rgba)
1529 dst_format->from_rgba(&tmp, &color);
1533 format_from_vec4(dst_format, &color, &val);
1536 dword_to_bytes(dst_ptr, val, dst_format->bytes_per_pixel);
1537 src_ptr += src_format->bytes_per_pixel;
1538 dst_ptr += dst_format->bytes_per_pixel;
1541 if (src_size->width < dst_size->width) /* black out remaining pixels */
1542 memset(dst_ptr, 0, dst_format->bytes_per_pixel * (dst_size->width - src_size->width));
1545 if (src_size->height < dst_size->height) /* black out remaining pixels */
1546 memset(dst + src_size->height * dst_row_pitch, 0, dst_row_pitch * (dst_size->height - src_size->height));
1548 if (src_size->depth < dst_size->depth) /* black out remaining pixels */
1549 memset(dst + src_size->depth * dst_slice_pitch, 0, dst_slice_pitch * (dst_size->depth - src_size->depth));
1552 /************************************************************
1553 * point_filter_argb_pixels
1555 * Copies the source buffer to the destination buffer, performing
1556 * any necessary format conversion, color keying and stretching
1557 * using a point filter.
1558 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1560 void point_filter_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1561 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1562 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key)
1564 struct argb_conversion_info conv_info, ck_conv_info;
1565 const struct pixel_format_desc *ck_format = NULL;
1566 DWORD channels[4], pixel;
1569 ZeroMemory(channels, sizeof(channels));
1570 init_argb_conversion_info(src_format, dst_format, &conv_info);
1574 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1575 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1576 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1579 for (z = 0; z < dst_size->depth; z++)
1581 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1582 const BYTE *src_slice_ptr = src + src_slice_pitch * (z * src_size->depth / dst_size->depth);
1584 for (y = 0; y < dst_size->height; y++)
1586 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1587 const BYTE *src_row_ptr = src_slice_ptr + src_row_pitch * (y * src_size->height / dst_size->height);
1589 for (x = 0; x < dst_size->width; x++)
1591 const BYTE *src_ptr = src_row_ptr + (x * src_size->width / dst_size->width) * src_format->bytes_per_pixel;
1594 /* extract source color components */
1595 pixel = dword_from_bytes(src_ptr, src_format->bytes_per_pixel);
1597 if (!src_format->to_rgba && !dst_format->from_rgba)
1599 get_relevant_argb_components(&conv_info, pixel, channels);
1600 val = make_argb_color(&conv_info, channels);
1604 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1605 pixel = make_argb_color(&ck_conv_info, channels);
1606 if (pixel == color_key)
1607 val &= ~conv_info.destmask[0];
1612 struct vec4 color, tmp;
1614 format_to_vec4(src_format, &pixel, &color);
1615 if (src_format->to_rgba)
1616 src_format->to_rgba(&color, &tmp);
1622 format_from_vec4(ck_format, &tmp, &pixel);
1623 if (pixel == color_key)
1627 if (dst_format->from_rgba)
1628 dst_format->from_rgba(&tmp, &color);
1632 format_from_vec4(dst_format, &color, &val);
1635 dword_to_bytes(dst_ptr, val, dst_format->bytes_per_pixel);
1636 dst_ptr += dst_format->bytes_per_pixel;
1642 /************************************************************
1643 * D3DXLoadSurfaceFromMemory
1645 * Loads data from a given memory chunk into a surface,
1646 * applying any of the specified filters.
1649 * pDestSurface [I] pointer to the surface
1650 * pDestPalette [I] palette to use
1651 * pDestRect [I] to be filled area of the surface
1652 * pSrcMemory [I] pointer to the source data
1653 * SrcFormat [I] format of the source pixel data
1654 * SrcPitch [I] number of bytes in a row
1655 * pSrcPalette [I] palette used in the source image
1656 * pSrcRect [I] area of the source data to load
1657 * dwFilter [I] filter to apply on stretching
1658 * Colorkey [I] colorkey
1661 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1662 * if pSrcMemory is NULL but the other parameters are valid
1663 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1664 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1665 * if DestRect is invalid
1666 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1667 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1670 * pSrcRect specifies the dimensions of the source data;
1671 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1674 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1675 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1676 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1677 DWORD filter, D3DCOLOR color_key)
1679 const struct pixel_format_desc *srcformatdesc, *destformatdesc;
1680 D3DSURFACE_DESC surfdesc;
1681 D3DLOCKED_RECT lockrect;
1682 struct volume src_size, dst_size;
1684 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1685 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1686 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1688 if (!dst_surface || !src_memory || !src_rect)
1690 WARN("Invalid argument specified.\n");
1691 return D3DERR_INVALIDCALL;
1693 if (src_format == D3DFMT_UNKNOWN
1694 || src_rect->left >= src_rect->right
1695 || src_rect->top >= src_rect->bottom)
1697 WARN("Invalid src_format or src_rect.\n");
1701 if (filter == D3DX_DEFAULT)
1702 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1704 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1706 src_size.width = src_rect->right - src_rect->left;
1707 src_size.height = src_rect->bottom - src_rect->top;
1711 dst_size.width = surfdesc.Width;
1712 dst_size.height = surfdesc.Height;
1716 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width
1717 || dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height
1718 || dst_rect->left < 0 || dst_rect->top < 0)
1720 WARN("Invalid dst_rect specified.\n");
1721 return D3DERR_INVALIDCALL;
1723 dst_size.width = dst_rect->right - dst_rect->left;
1724 dst_size.height = dst_rect->bottom - dst_rect->top;
1725 if (!dst_size.width || !dst_size.height)
1730 srcformatdesc = get_format_info(src_format);
1731 destformatdesc = get_format_info(surfdesc.Format);
1732 if (srcformatdesc->type == FORMAT_UNKNOWN || destformatdesc->type == FORMAT_UNKNOWN)
1734 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format, surfdesc.Format);
1738 if (src_format == surfdesc.Format
1739 && dst_size.width == src_size.width
1740 && dst_size.height == src_size.height
1741 && color_key == 0) /* Simple copy. */
1743 if (src_rect->left & (srcformatdesc->block_width - 1)
1744 || src_rect->top & (srcformatdesc->block_height - 1)
1745 || (src_rect->right & (srcformatdesc->block_width - 1)
1746 && src_size.width != surfdesc.Width)
1747 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1748 && src_size.height != surfdesc.Height))
1750 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1751 return D3DXERR_INVALIDDATA;
1754 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1755 return D3DXERR_INVALIDDATA;
1757 copy_pixels(src_memory, src_pitch, 0, lockrect.pBits, lockrect.Pitch, 0,
1758 &src_size, srcformatdesc);
1760 IDirect3DSurface9_UnlockRect(dst_surface);
1762 else /* Stretching or format conversion. */
1764 if (srcformatdesc->type != FORMAT_ARGB || destformatdesc->type != FORMAT_ARGB)
1766 FIXME("Format conversion missing %#x -> %#x\n", src_format, surfdesc.Format);
1770 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1771 return D3DXERR_INVALIDDATA;
1773 if ((filter & 0xf) == D3DX_FILTER_NONE)
1775 convert_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1776 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key);
1778 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1780 if ((filter & 0xf) != D3DX_FILTER_POINT)
1781 FIXME("Unhandled filter %#x.\n", filter);
1783 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1784 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1785 point_filter_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1786 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key);
1789 IDirect3DSurface9_UnlockRect(dst_surface);
1795 /************************************************************
1796 * D3DXLoadSurfaceFromSurface
1798 * Copies the contents from one surface to another, performing any required
1799 * format conversion, resizing or filtering.
1802 * pDestSurface [I] pointer to the destination surface
1803 * pDestPalette [I] palette to use
1804 * pDestRect [I] to be filled area of the surface
1805 * pSrcSurface [I] pointer to the source surface
1806 * pSrcPalette [I] palette used for the source surface
1807 * pSrcRect [I] area of the source data to load
1808 * dwFilter [I] filter to apply on resizing
1809 * Colorkey [I] any ARGB value or 0 to disable color-keying
1813 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1814 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1817 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
1818 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
1819 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
1822 D3DLOCKED_RECT lock;
1823 D3DSURFACE_DESC SrcDesc;
1826 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1827 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1828 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
1829 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1831 if (!dst_surface || !src_surface)
1832 return D3DERR_INVALIDCALL;
1834 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
1837 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1841 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
1842 return D3DXERR_INVALIDDATA;
1844 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
1845 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
1847 IDirect3DSurface9_UnlockRect(src_surface);
1853 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1854 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1859 ID3DXBuffer *buffer;
1861 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1862 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1864 if (!dst_filename) return D3DERR_INVALIDCALL;
1866 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1867 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1868 if (!filename) return E_OUTOFMEMORY;
1869 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1871 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1874 hr = write_buffer_to_file(filename, buffer);
1875 ID3DXBuffer_Release(buffer);
1878 HeapFree(GetProcessHeap(), 0, filename);
1882 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1883 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1886 ID3DXBuffer *buffer;
1888 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1889 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1891 if (!dst_filename) return D3DERR_INVALIDCALL;
1893 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1896 hr = write_buffer_to_file(dst_filename, buffer);
1897 ID3DXBuffer_Release(buffer);
1903 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1904 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1906 IWICBitmapEncoder *encoder = NULL;
1907 IWICBitmapFrameEncode *frame = NULL;
1908 IPropertyBag2 *encoder_options = NULL;
1909 IStream *stream = NULL;
1912 const CLSID *encoder_clsid;
1913 const GUID *pixel_format_guid;
1914 WICPixelFormatGUID wic_pixel_format;
1915 D3DFORMAT d3d_pixel_format;
1916 D3DSURFACE_DESC src_surface_desc;
1917 D3DLOCKED_RECT locked_rect;
1919 STATSTG stream_stats;
1920 HGLOBAL stream_hglobal;
1921 ID3DXBuffer *buffer;
1924 TRACE("(%p, %#x, %p, %p, %s)\n",
1925 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1927 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
1931 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1932 return D3DERR_INVALIDCALL;
1935 switch (file_format)
1938 encoder_clsid = &CLSID_WICBmpEncoder;
1941 encoder_clsid = &CLSID_WICPngEncoder;
1944 encoder_clsid = &CLSID_WICJpegEncoder;
1947 return save_dds_surface_to_memory(dst_buffer, src_surface, src_rect);
1953 FIXME("File format %#x is not supported yet\n", file_format);
1956 return D3DERR_INVALIDCALL;
1959 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
1962 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
1964 WARN("Invalid rectangle with 0 area\n");
1965 return D3DXCreateBuffer(64, dst_buffer);
1967 if (src_rect->left < 0 || src_rect->top < 0)
1968 return D3DERR_INVALIDCALL;
1969 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
1970 return D3DERR_INVALIDCALL;
1971 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
1972 return D3DERR_INVALIDCALL;
1974 width = src_rect->right - src_rect->left;
1975 height = src_rect->bottom - src_rect->top;
1979 width = src_surface_desc.Width;
1980 height = src_surface_desc.Height;
1983 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1985 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
1986 &IID_IWICBitmapEncoder, (void **)&encoder);
1987 if (FAILED(hr)) goto cleanup_err;
1989 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
1990 if (FAILED(hr)) goto cleanup_err;
1992 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
1993 if (FAILED(hr)) goto cleanup_err;
1995 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
1996 if (FAILED(hr)) goto cleanup_err;
1998 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
1999 if (FAILED(hr)) goto cleanup_err;
2001 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
2002 if (FAILED(hr)) goto cleanup_err;
2004 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
2005 if (!pixel_format_guid)
2007 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
2012 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
2013 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
2014 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
2015 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
2017 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
2019 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
2021 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2024 IWICBitmapFrameEncode_WritePixels(frame, height,
2025 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
2026 IDirect3DSurface9_UnlockRect(src_surface);
2029 else /* Pixel format conversion */
2031 const struct pixel_format_desc *src_format_desc, *dst_format_desc;
2036 src_format_desc = get_format_info(src_surface_desc.Format);
2037 dst_format_desc = get_format_info(d3d_pixel_format);
2038 if (src_format_desc->type != FORMAT_ARGB || dst_format_desc->type != FORMAT_ARGB)
2040 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
2041 src_surface_desc.Format, d3d_pixel_format);
2047 size.height = height;
2049 dst_pitch = width * dst_format_desc->bytes_per_pixel;
2050 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
2057 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2060 convert_argb_pixels(locked_rect.pBits, locked_rect.Pitch, 0, &size, src_format_desc,
2061 dst_data, dst_pitch, 0, &size, dst_format_desc, 0);
2062 IDirect3DSurface9_UnlockRect(src_surface);
2065 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
2066 HeapFree(GetProcessHeap(), 0, dst_data);
2069 hr = IWICBitmapFrameEncode_Commit(frame);
2070 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
2072 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
2074 /* copy data from stream to ID3DXBuffer */
2075 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
2076 if (FAILED(hr)) goto cleanup_err;
2078 if (stream_stats.cbSize.u.HighPart != 0)
2080 hr = D3DXERR_INVALIDDATA;
2083 size = stream_stats.cbSize.u.LowPart;
2085 hr = D3DXCreateBuffer(size, &buffer);
2086 if (FAILED(hr)) goto cleanup;
2088 hr = GetHGlobalFromStream(stream, &stream_hglobal);
2091 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
2092 void *stream_data = GlobalLock(stream_hglobal);
2093 memcpy(buffer_pointer, stream_data, size);
2094 GlobalUnlock(stream_hglobal);
2095 *dst_buffer = buffer;
2097 else ID3DXBuffer_Release(buffer);
2100 if (FAILED(hr) && hr != E_OUTOFMEMORY)
2101 hr = D3DERR_INVALIDCALL;
2104 if (stream) IStream_Release(stream);
2106 if (frame) IWICBitmapFrameEncode_Release(frame);
2107 if (encoder_options) IPropertyBag2_Release(encoder_options);
2109 if (encoder) IWICBitmapEncoder_Release(encoder);
2111 if (SUCCEEDED(initresult)) CoUninitialize();