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 D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
172 static const struct {
179 } rgb_pixel_formats[] = {
180 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 },
181 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 },
182 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 },
183 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 },
184 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 },
185 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 },
186 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 },
187 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 },
188 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 },
189 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 },
190 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 },
191 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 },
192 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 },
193 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8 },
194 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8 },
197 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
199 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
200 && rgb_pixel_formats[i].rmask == pixel_format->rmask
201 && rgb_pixel_formats[i].gmask == pixel_format->gmask
202 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
204 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
205 return rgb_pixel_formats[i].format;
206 if (rgb_pixel_formats[i].amask == 0)
207 return rgb_pixel_formats[i].format;
211 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
212 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
213 return D3DFMT_UNKNOWN;
216 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
218 if (pixel_format->bpp == 8)
220 if (pixel_format->rmask == 0xff)
222 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
225 if (pixel_format->bpp == 16)
227 if (pixel_format->rmask == 0xffff)
229 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
233 WARN("Unknown luminance pixel format (bpp %#x, l %#x, a %#x)\n",
234 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
235 return D3DFMT_UNKNOWN;
238 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
240 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
243 WARN("Unknown Alpha pixel format (%#x, %#x)\n", pixel_format->bpp, pixel_format->rmask);
244 return D3DFMT_UNKNOWN;
247 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
249 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00)
251 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000)
252 return D3DFMT_V16U16;
254 WARN("Unknown bump pixel format (%#x, %#x, %#x, %#x, %#x)\n", pixel_format->bpp,
255 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
256 return D3DFMT_UNKNOWN;
259 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
261 if (pixel_format->flags & DDS_PF_FOURCC)
262 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
263 if (pixel_format->flags & DDS_PF_RGB)
264 return dds_rgb_to_d3dformat(pixel_format);
265 if (pixel_format->flags & DDS_PF_LUMINANCE)
266 return dds_luminance_to_d3dformat(pixel_format);
267 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
268 return dds_alpha_to_d3dformat(pixel_format);
269 if (pixel_format->flags & DDS_PF_BUMPDUDV)
270 return dds_bump_to_d3dformat(pixel_format);
272 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %#x, r %#x, g %#x, b %#x, a %#x)\n",
273 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
274 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
275 return D3DFMT_UNKNOWN;
278 static HRESULT calculate_dds_surface_size(const D3DXIMAGE_INFO *img_info,
279 UINT width, UINT height, UINT *pitch, UINT *size)
281 const PixelFormatDesc *format_desc = get_format_info(img_info->Format);
282 if (format_desc->format == D3DFMT_UNKNOWN)
285 if (format_desc->block_width != 1 || format_desc->block_height != 1)
287 *pitch = format_desc->block_byte_count
288 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
290 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
294 *pitch = width * format_desc->bytes_per_pixel;
295 *size = *pitch * height;
301 /************************************************************
302 * get_image_info_from_dds
304 * Fills a D3DXIMAGE_INFO structure with information
305 * about a DDS file stored in the memory.
308 * buffer [I] pointer to DDS data
309 * length [I] size of DDS data
310 * info [O] pointer to D3DXIMAGE_INFO structure
314 * Failure: D3DXERR_INVALIDDATA
317 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
321 UINT width, height, depth;
322 const struct dds_header *header = buffer;
323 UINT expected_length = 0;
325 if (length < sizeof(*header) || !info)
326 return D3DXERR_INVALIDDATA;
328 if (header->pixel_format.size != sizeof(header->pixel_format))
329 return D3DXERR_INVALIDDATA;
331 info->Width = header->width;
332 info->Height = header->height;
334 info->MipLevels = (header->flags & DDS_MIPMAPCOUNT) ? header->miplevels : 1;
336 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
337 if (info->Format == D3DFMT_UNKNOWN)
338 return D3DXERR_INVALIDDATA;
340 TRACE("Pixel format is %#x\n", info->Format);
342 if (header->caps2 & DDS_CAPS2_VOLUME)
344 info->Depth = header->depth;
345 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
347 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
351 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
353 if (header->caps2 & face)
356 info->ResourceType = D3DRTYPE_CUBETEXTURE;
360 info->ResourceType = D3DRTYPE_TEXTURE;
363 /* calculate the expected length */
365 height = info->Height;
367 for (i = 0; i < info->MipLevels; i++)
369 UINT pitch, size = 0;
370 calculate_dds_surface_size(info, width, height, &pitch, &size);
372 expected_length += size;
373 width = max(1, width / 2);
374 height = max(1, height / 2);
375 depth = max(1, depth / 2);
378 expected_length *= faces;
379 expected_length += sizeof(*header);
380 if (length < expected_length)
382 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
383 return D3DXERR_INVALIDDATA;
386 info->ImageFileFormat = D3DXIFF_DDS;
391 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
392 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
393 const D3DXIMAGE_INFO *src_info)
397 const struct dds_header *header = src_data;
398 const BYTE *pixels = (BYTE *)(header + 1);
400 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
401 return D3DXERR_INVALIDDATA;
403 if (FAILED(calculate_dds_surface_size(src_info, src_info->Width, src_info->Height, &src_pitch, &size)))
406 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
407 src_pitch, NULL, src_rect, filter, color_key);
410 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette,
411 const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key,
412 const D3DXIMAGE_INFO *src_info)
414 UINT row_pitch, slice_pitch;
415 const struct dds_header *header = src_data;
416 const BYTE *pixels = (BYTE *)(header + 1);
418 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
419 return D3DXERR_INVALIDDATA;
421 if (FAILED(calculate_dds_surface_size(src_info, src_info->Width, src_info->Height, &row_pitch, &slice_pitch)))
424 return D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box, pixels, src_info->Format,
425 row_pitch, slice_pitch, NULL, src_box, filter, color_key);
428 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
429 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info)
439 IDirect3DSurface9 *surface;
440 const struct dds_header *header = src_data;
441 const BYTE *pixels = (BYTE *)(header + 1);
443 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
444 return D3DXERR_INVALIDDATA;
446 width = src_info->Width;
447 height = src_info->Height;
448 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
449 for (mip_level = 0; mip_level < mip_levels; mip_level++)
451 hr = calculate_dds_surface_size(src_info, width, height, &src_pitch, &mip_level_size);
452 if (FAILED(hr)) return hr;
454 SetRect(&src_rect, 0, 0, width, height);
456 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level, &surface);
457 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
458 NULL, &src_rect, filter, color_key);
459 IDirect3DSurface9_Release(surface);
460 if (FAILED(hr)) return hr;
462 pixels += mip_level_size;
463 width = max(1, width / 2);
464 height = max(1, height / 2);
470 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
471 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
481 IDirect3DSurface9 *surface;
482 const struct dds_header *header = src_data;
483 const BYTE *pixels = (BYTE *)(header + 1);
485 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
486 return D3DXERR_INVALIDDATA;
488 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
490 WARN("Only full cubemaps are supported\n");
491 return D3DXERR_INVALIDDATA;
494 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
495 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
497 size = src_info->Width;
498 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
500 hr = calculate_dds_surface_size(src_info, size, size, &src_pitch, &mip_level_size);
501 if (FAILED(hr)) return hr;
503 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
504 if (mip_level < mip_levels)
506 SetRect(&src_rect, 0, 0, size, size);
508 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
509 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
510 NULL, &src_rect, filter, color_key);
511 IDirect3DSurface9_Release(surface);
512 if (FAILED(hr)) return hr;
515 pixels += mip_level_size;
516 size = max(1, size / 2);
523 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
524 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
529 UINT src_slice_pitch;
532 UINT width, height, depth;
533 IDirect3DVolume9 *volume;
534 const struct dds_header *header = src_data;
535 const BYTE *pixels = (BYTE *)(header + 1);
537 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
538 return D3DXERR_INVALIDDATA;
540 width = src_info->Width;
541 height = src_info->Height;
542 depth = src_info->Depth;
543 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture));
545 for (mip_level = 0; mip_level < mip_levels; mip_level++)
547 hr = calculate_dds_surface_size(src_info, width, height, &src_row_pitch, &src_slice_pitch);
548 if (FAILED(hr)) return hr;
550 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume);
551 if (FAILED(hr)) return hr;
555 src_box.Right = width;
556 src_box.Bottom = height;
558 src_box.Back = depth;
560 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format,
561 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key);
563 IDirect3DVolume9_Release(volume);
564 if (FAILED(hr)) return hr;
566 pixels += depth * src_slice_pitch;
567 width = max(1, width / 2);
568 height = max(1, height / 2);
569 depth = max(1, depth / 2);
575 /************************************************************
576 * D3DXGetImageInfoFromFileInMemory
578 * Fills a D3DXIMAGE_INFO structure with info about an image
581 * data [I] pointer to the image file data
582 * datasize [I] size of the passed data
583 * info [O] pointer to the destination structure
586 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
587 * if info is NULL and data and datasize are not NULL
588 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
589 * D3DERR_INVALIDCALL, if data is NULL or
593 * datasize may be bigger than the actual file size
596 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
598 IWICImagingFactory *factory;
599 IWICBitmapDecoder *decoder = NULL;
604 TRACE("(%p, %d, %p)\n", data, datasize, info);
606 if (!data || !datasize)
607 return D3DERR_INVALIDCALL;
612 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
613 TRACE("File type is DDS\n");
614 return get_image_info_from_dds(data, datasize, info);
617 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
619 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
622 IWICImagingFactory_CreateStream(factory, &stream);
623 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
624 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
625 IStream_Release(stream);
626 IWICImagingFactory_Release(factory);
630 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
631 FIXME("File type PPM is not supported yet\n");
632 else if ((datasize >= 2) && !strncmp(data, "BM", 2))
633 FIXME("File type DIB is not supported yet\n");
634 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
635 FIXME("File type HDR is not supported yet\n");
636 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
637 FIXME("File type PFM is not supported yet\n");
641 GUID container_format;
644 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
646 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
647 TRACE("File type is BMP\n");
648 info->ImageFileFormat = D3DXIFF_BMP;
649 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
650 TRACE("File type is PNG\n");
651 info->ImageFileFormat = D3DXIFF_PNG;
652 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
653 TRACE("File type is JPG\n");
654 info->ImageFileFormat = D3DXIFF_JPG;
655 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
656 TRACE("File type is TGA\n");
657 info->ImageFileFormat = D3DXIFF_TGA;
659 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
660 hr = D3DXERR_INVALIDDATA;
665 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
666 if (SUCCEEDED(hr) && !frame_count)
667 hr = D3DXERR_INVALIDDATA;
670 IWICBitmapFrameDecode *frame = NULL;
672 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
675 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
678 WICPixelFormatGUID pixel_format;
680 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
682 info->Format = wic_guid_to_d3dformat(&pixel_format);
683 if (info->Format == D3DFMT_UNKNOWN) {
684 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
685 hr = D3DXERR_INVALIDDATA;
691 IWICBitmapFrameDecode_Release(frame);
695 info->ResourceType = D3DRTYPE_TEXTURE;
700 IWICBitmapDecoder_Release(decoder);
702 if (SUCCEEDED(initresult))
706 TRACE("Invalid or unsupported image file\n");
707 return D3DXERR_INVALIDDATA;
713 /************************************************************
714 * D3DXGetImageInfoFromFile
717 * Success: D3D_OK, if we successfully load a valid image file or
718 * if we successfully load a file which is no valid image and info is NULL
719 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
720 * if file is not a valid image file and info is not NULL
721 * D3DERR_INVALIDCALL, if file is NULL
724 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
730 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
732 if( !file ) return D3DERR_INVALIDCALL;
734 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
735 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
736 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
738 hr = D3DXGetImageInfoFromFileW(widename, info);
739 HeapFree(GetProcessHeap(), 0, widename);
744 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
750 TRACE("(%s, %p): relay\n", debugstr_w(file), info);
752 if( !file ) return D3DERR_INVALIDCALL;
754 hr = map_view_of_file(file, &buffer, &size);
755 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
757 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
758 UnmapViewOfFile(buffer);
763 /************************************************************
764 * D3DXGetImageInfoFromResource
767 * Success: D3D_OK, if resource is a valid image file
768 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
769 * if we fail to load resource
772 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
776 TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
778 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
784 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
785 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
786 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
789 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
791 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
794 return D3DXERR_INVALIDDATA;
797 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
801 TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
803 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
809 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
810 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
811 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
814 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
816 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
819 return D3DXERR_INVALIDDATA;
822 /************************************************************
823 * D3DXLoadSurfaceFromFileInMemory
825 * Loads data from a given buffer into a surface and fills a given
826 * D3DXIMAGE_INFO structure with info about the source data.
829 * pDestSurface [I] pointer to the surface
830 * pDestPalette [I] palette to use
831 * pDestRect [I] to be filled area of the surface
832 * pSrcData [I] pointer to the source data
833 * SrcDataSize [I] size of the source data in bytes
834 * pSrcRect [I] area of the source data to load
835 * dwFilter [I] filter to apply on stretching
836 * Colorkey [I] colorkey
837 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
841 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
842 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
845 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
846 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
847 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
849 D3DXIMAGE_INFO imginfo;
852 IWICImagingFactory *factory;
853 IWICBitmapDecoder *decoder;
854 IWICBitmapFrameDecode *bitmapframe;
857 const PixelFormatDesc *formatdesc;
861 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
862 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
863 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
864 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
866 if (!pDestSurface || !pSrcData || !SrcDataSize)
867 return D3DERR_INVALIDCALL;
869 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
876 wicrect.X = pSrcRect->left;
877 wicrect.Y = pSrcRect->top;
878 wicrect.Width = pSrcRect->right - pSrcRect->left;
879 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
885 wicrect.Width = imginfo.Width;
886 wicrect.Height = imginfo.Height;
889 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
891 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
893 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
894 dwFilter, Colorkey, &imginfo);
895 if (SUCCEEDED(hr) && pSrcInfo)
900 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
902 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
905 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
907 IWICImagingFactory_Release(factory);
911 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
913 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
915 IStream_Release(stream);
916 IWICImagingFactory_Release(factory);
921 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
926 formatdesc = get_format_info(imginfo.Format);
928 if (formatdesc->format == D3DFMT_UNKNOWN)
930 FIXME("Unsupported pixel format\n");
931 hr = D3DXERR_INVALIDDATA;
938 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
939 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
941 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
942 pitch * wicrect.Height, buffer);
946 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
947 buffer, imginfo.Format, pitch,
948 NULL, &rect, dwFilter, Colorkey);
951 HeapFree(GetProcessHeap(), 0, buffer);
954 IWICBitmapFrameDecode_Release(bitmapframe);
957 IWICBitmapDecoder_Release(decoder);
963 return D3DXERR_INVALIDDATA;
971 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
972 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
973 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
979 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
980 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
981 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
982 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
984 if (!src_file || !dst_surface)
985 return D3DERR_INVALIDCALL;
987 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
988 pWidename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*pWidename));
989 MultiByteToWideChar(CP_ACP, 0, src_file, -1, pWidename, strlength);
991 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
992 pWidename, src_rect, filter, color_key, src_info);
993 HeapFree(GetProcessHeap(), 0, pWidename);
998 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
999 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
1000 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1006 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1007 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1008 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
1009 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1011 if (!src_file || !dst_surface)
1012 return D3DERR_INVALIDCALL;
1014 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
1015 return D3DXERR_INVALIDDATA;
1017 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1018 data, data_size, src_rect, filter, color_key, src_info);
1019 UnmapViewOfFile(data);
1024 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
1025 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
1026 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1030 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1031 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1032 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
1033 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1036 return D3DERR_INVALIDCALL;
1038 if ((hResInfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA)))
1043 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
1044 return D3DXERR_INVALIDDATA;
1046 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1047 data, data_size, src_rect, filter, color_key, src_info);
1050 if ((hResInfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
1052 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
1056 return D3DXERR_INVALIDDATA;
1059 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
1060 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
1061 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1065 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1066 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1067 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
1068 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1071 return D3DERR_INVALIDCALL;
1073 if ((hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA)))
1078 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
1079 return D3DXERR_INVALIDDATA;
1081 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1082 data, data_size, src_rect, filter, color_key, src_info);
1085 if ((hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1087 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
1091 return D3DXERR_INVALIDDATA;
1095 /************************************************************
1096 * helper functions for D3DXLoadSurfaceFromMemory
1098 struct argb_conversion_info
1100 CONST PixelFormatDesc *srcformat;
1101 CONST PixelFormatDesc *destformat;
1102 DWORD srcshift[4], destshift[4];
1103 DWORD srcmask[4], destmask[4];
1104 BOOL process_channel[4];
1108 static void init_argb_conversion_info(CONST PixelFormatDesc *srcformat, CONST PixelFormatDesc *destformat, struct argb_conversion_info *info)
1111 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1112 info->channelmask = 0;
1114 info->srcformat = srcformat;
1115 info->destformat = destformat;
1117 for(i = 0;i < 4;i++) {
1118 /* srcshift is used to extract the _relevant_ components */
1119 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1121 /* destshift is used to move the components to the correct position */
1122 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1124 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1125 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1127 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1128 if(destformat->bits[i]) {
1129 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1130 else info->channelmask |= info->destmask[i];
1135 static DWORD dword_from_bytes(CONST BYTE *src, UINT bytes_per_pixel)
1138 static BOOL fixme_once;
1140 if(bytes_per_pixel > sizeof(DWORD)) {
1141 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1142 bytes_per_pixel = sizeof(DWORD);
1145 memcpy(&ret, src, bytes_per_pixel);
1149 static void dword_to_bytes(BYTE *dst, DWORD dword, UINT bytes_per_pixel)
1151 static BOOL fixme_once;
1153 if(bytes_per_pixel > sizeof(DWORD)) {
1154 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1155 ZeroMemory(dst, bytes_per_pixel);
1156 bytes_per_pixel = sizeof(DWORD);
1159 memcpy(dst, &dword, bytes_per_pixel);
1162 /************************************************************
1163 * get_relevant_argb_components
1165 * Extracts the relevant components from the source color and
1166 * drops the less significant bits if they aren't used by the destination format.
1168 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
1172 if(info->process_channel[i])
1173 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
1176 /************************************************************
1179 * Recombines the output of get_relevant_argb_components and converts
1180 * it to the destination format.
1182 static DWORD make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in)
1187 for(i = 0;i < 4;i++) {
1188 if(info->process_channel[i]) {
1189 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1191 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1192 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1195 val |= info->channelmask; /* new channels are set to their maximal value */
1199 static void format_to_vec4(const PixelFormatDesc *format, const DWORD *src, struct vec4 *dst)
1203 if (format->bits[1])
1205 mask = (1 << format->bits[1]) - 1;
1206 dst->x = (float)((*src >> format->shift[1]) & mask) / mask;
1211 if (format->bits[2])
1213 mask = (1 << format->bits[2]) - 1;
1214 dst->y = (float)((*src >> format->shift[2]) & mask) / mask;
1219 if (format->bits[3])
1221 mask = (1 << format->bits[3]) - 1;
1222 dst->z = (float)((*src >> format->shift[3]) & mask) / mask;
1227 if (format->bits[0])
1229 mask = (1 << format->bits[0]) - 1;
1230 dst->w = (float)((*src >> format->shift[0]) & mask) / mask;
1236 static void format_from_vec4(const PixelFormatDesc *format, const struct vec4 *src, DWORD *dst)
1240 if (format->bits[1])
1241 *dst |= (DWORD)(src->x * ((1 << format->bits[1]) - 1) + 0.5f) << format->shift[1];
1242 if (format->bits[2])
1243 *dst |= (DWORD)(src->y * ((1 << format->bits[2]) - 1) + 0.5f) << format->shift[2];
1244 if (format->bits[3])
1245 *dst |= (DWORD)(src->z * ((1 << format->bits[3]) - 1) + 0.5f) << format->shift[3];
1246 if (format->bits[0])
1247 *dst |= (DWORD)(src->w * ((1 << format->bits[0]) - 1) + 0.5f) << format->shift[0];
1250 /************************************************************
1253 * Copies the source buffer to the destination buffer, performing
1254 * any necessary format conversion and color keying.
1255 * Pixels outsize the source rect are blacked out.
1256 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1258 void copy_simple_data(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, struct volume *src_size, const PixelFormatDesc *src_format,
1259 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, struct volume *dst_size, const PixelFormatDesc *dst_format, D3DCOLOR color_key)
1261 struct argb_conversion_info conv_info, ck_conv_info;
1262 const PixelFormatDesc *ck_format = NULL;
1263 DWORD channels[4], pixel;
1264 UINT min_width, min_height, min_depth;
1267 ZeroMemory(channels, sizeof(channels));
1268 init_argb_conversion_info(src_format, dst_format, &conv_info);
1270 min_width = min(src_size->width, dst_size->width);
1271 min_height = min(src_size->height, dst_size->height);
1272 min_depth = min(src_size->depth, dst_size->depth);
1276 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1277 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1278 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1281 for (z = 0; z < min_depth; z++) {
1282 const BYTE *src_slice_ptr = src + z * src_slice_pitch;
1283 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1285 for (y = 0; y < min_height; y++) {
1286 const BYTE *src_ptr = src_slice_ptr + y * src_row_pitch;
1287 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1290 for (x = 0; x < min_width; x++) {
1291 /* extract source color components */
1292 pixel = dword_from_bytes(src_ptr, src_format->bytes_per_pixel);
1294 if (!src_format->to_rgba && !dst_format->from_rgba)
1296 get_relevant_argb_components(&conv_info, pixel, channels);
1297 val = make_argb_color(&conv_info, channels);
1301 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1302 pixel = make_argb_color(&ck_conv_info, channels);
1303 if (pixel == color_key)
1304 val &= ~conv_info.destmask[0];
1309 struct vec4 color, tmp;
1311 format_to_vec4(src_format, &pixel, &color);
1312 if (src_format->to_rgba)
1313 src_format->to_rgba(&color, &tmp);
1319 format_from_vec4(ck_format, &tmp, &pixel);
1320 if (pixel == color_key)
1324 if (dst_format->from_rgba)
1325 dst_format->from_rgba(&tmp, &color);
1329 format_from_vec4(dst_format, &color, &val);
1332 dword_to_bytes(dst_ptr, val, dst_format->bytes_per_pixel);
1333 src_ptr += src_format->bytes_per_pixel;
1334 dst_ptr += dst_format->bytes_per_pixel;
1337 if (src_size->width < dst_size->width) /* black out remaining pixels */
1338 memset(dst_ptr, 0, dst_format->bytes_per_pixel * (dst_size->width - src_size->width));
1341 if (src_size->height < dst_size->height) /* black out remaining pixels */
1342 memset(dst + src_size->height * dst_row_pitch, 0, dst_row_pitch * (dst_size->height - src_size->height));
1344 if (src_size->depth < dst_size->depth) /* black out remaining pixels */
1345 memset(dst + src_size->depth * dst_slice_pitch, 0, dst_slice_pitch * (dst_size->depth - src_size->depth));
1348 /************************************************************
1349 * point_filter_simple_data
1351 * Copies the source buffer to the destination buffer, performing
1352 * any necessary format conversion, color keying and stretching
1353 * using a point filter.
1354 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1356 void point_filter_simple_data(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, struct volume *src_size, const PixelFormatDesc *src_format,
1357 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, struct volume *dst_size, const PixelFormatDesc *dst_format, D3DCOLOR color_key)
1359 struct argb_conversion_info conv_info, ck_conv_info;
1360 const PixelFormatDesc *ck_format = NULL;
1361 DWORD channels[4], pixel;
1364 ZeroMemory(channels, sizeof(channels));
1365 init_argb_conversion_info(src_format, dst_format, &conv_info);
1369 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1370 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1371 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1374 for (z = 0; z < dst_size->depth; z++)
1376 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1377 const BYTE *src_slice_ptr = src + src_slice_pitch * (z * src_size->depth / dst_size->depth);
1379 for (y = 0; y < dst_size->height; y++)
1381 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1382 const BYTE *src_row_ptr = src_slice_ptr + src_row_pitch * (y * src_size->height / dst_size->height);
1384 for (x = 0; x < dst_size->width; x++)
1386 const BYTE *src_ptr = src_row_ptr + (x * src_size->width / dst_size->width) * src_format->bytes_per_pixel;
1389 /* extract source color components */
1390 pixel = dword_from_bytes(src_ptr, src_format->bytes_per_pixel);
1392 if (!src_format->to_rgba && !dst_format->from_rgba)
1394 get_relevant_argb_components(&conv_info, pixel, channels);
1395 val = make_argb_color(&conv_info, channels);
1399 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1400 pixel = make_argb_color(&ck_conv_info, channels);
1401 if (pixel == color_key)
1402 val &= ~conv_info.destmask[0];
1407 struct vec4 color, tmp;
1409 format_to_vec4(src_format, &pixel, &color);
1410 if (src_format->to_rgba)
1411 src_format->to_rgba(&color, &tmp);
1417 format_from_vec4(ck_format, &tmp, &pixel);
1418 if (pixel == color_key)
1422 if (dst_format->from_rgba)
1423 dst_format->from_rgba(&tmp, &color);
1427 format_from_vec4(dst_format, &color, &val);
1430 dword_to_bytes(dst_ptr, val, dst_format->bytes_per_pixel);
1431 dst_ptr += dst_format->bytes_per_pixel;
1437 /************************************************************
1438 * D3DXLoadSurfaceFromMemory
1440 * Loads data from a given memory chunk into a surface,
1441 * applying any of the specified filters.
1444 * pDestSurface [I] pointer to the surface
1445 * pDestPalette [I] palette to use
1446 * pDestRect [I] to be filled area of the surface
1447 * pSrcMemory [I] pointer to the source data
1448 * SrcFormat [I] format of the source pixel data
1449 * SrcPitch [I] number of bytes in a row
1450 * pSrcPalette [I] palette used in the source image
1451 * pSrcRect [I] area of the source data to load
1452 * dwFilter [I] filter to apply on stretching
1453 * Colorkey [I] colorkey
1456 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1457 * if pSrcMemory is NULL but the other parameters are valid
1458 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1459 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1460 * if DestRect is invalid
1461 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1462 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1465 * pSrcRect specifies the dimensions of the source data;
1466 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1469 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1470 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1471 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1472 DWORD filter, D3DCOLOR color_key)
1474 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
1475 D3DSURFACE_DESC surfdesc;
1476 D3DLOCKED_RECT lockrect;
1477 struct volume src_size, dst_size;
1480 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1481 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1482 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1484 if (!dst_surface || !src_memory || !src_rect)
1485 return D3DERR_INVALIDCALL;
1486 if (src_format == D3DFMT_UNKNOWN
1487 || src_rect->left >= src_rect->right
1488 || src_rect->top >= src_rect->bottom)
1491 if (filter == D3DX_DEFAULT)
1492 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1494 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1496 src_size.width = src_rect->right - src_rect->left;
1497 src_size.height = src_rect->bottom - src_rect->top;
1501 dst_size.width = surfdesc.Width;
1502 dst_size.height = surfdesc.Height;
1506 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width)
1507 return D3DERR_INVALIDCALL;
1508 if (dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height)
1509 return D3DERR_INVALIDCALL;
1510 if (dst_rect->left < 0 || dst_rect->top < 0)
1511 return D3DERR_INVALIDCALL;
1512 dst_size.width = dst_rect->right - dst_rect->left;
1513 dst_size.height = dst_rect->bottom - dst_rect->top;
1514 if (!dst_size.width || !dst_size.height)
1519 srcformatdesc = get_format_info(src_format);
1520 if (srcformatdesc->type == FORMAT_UNKNOWN)
1523 destformatdesc = get_format_info(surfdesc.Format);
1524 if (destformatdesc->type == FORMAT_UNKNOWN)
1527 if (src_format == surfdesc.Format
1528 && dst_size.width == src_size.width
1529 && dst_size.height == src_size.height) /* Simple copy. */
1531 UINT row_block_count = ((src_size.width + srcformatdesc->block_width - 1) / srcformatdesc->block_width);
1532 UINT row_count = (src_size.height + srcformatdesc->block_height - 1) / srcformatdesc->block_height;
1533 const BYTE *src_addr;
1537 if (src_rect->left & (srcformatdesc->block_width - 1)
1538 || src_rect->top & (srcformatdesc->block_height - 1)
1539 || (src_rect->right & (srcformatdesc->block_width - 1)
1540 && src_size.width != surfdesc.Width)
1541 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1542 && src_size.height != surfdesc.Height))
1544 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1545 return D3DXERR_INVALIDDATA;
1548 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1549 return D3DXERR_INVALIDDATA;
1551 src_addr = src_memory;
1552 src_addr += (src_rect->top / srcformatdesc->block_height) * src_pitch;
1553 src_addr += (src_rect->left / srcformatdesc->block_width) * srcformatdesc->block_byte_count;
1554 dst_addr = lockrect.pBits;
1556 for (row = 0; row < row_count; ++row)
1558 memcpy(dst_addr, src_addr, row_block_count * srcformatdesc->block_byte_count);
1559 src_addr += src_pitch;
1560 dst_addr += lockrect.Pitch;
1563 IDirect3DSurface9_UnlockRect(dst_surface);
1565 else /* Stretching or format conversion. */
1567 if (srcformatdesc->bytes_per_pixel > 4)
1569 if (destformatdesc->bytes_per_pixel > 4)
1571 if (srcformatdesc->block_height != 1 || srcformatdesc->block_width != 1)
1573 if (destformatdesc->block_height != 1 || destformatdesc->block_width != 1)
1576 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1577 return D3DXERR_INVALIDDATA;
1579 if ((filter & 0xf) == D3DX_FILTER_NONE)
1581 copy_simple_data(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1582 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key);
1584 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1586 if ((filter & 0xf) != D3DX_FILTER_POINT)
1587 FIXME("Unhandled filter %#x.\n", filter);
1589 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1590 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1591 point_filter_simple_data(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1592 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key);
1595 IDirect3DSurface9_UnlockRect(dst_surface);
1601 /************************************************************
1602 * D3DXLoadSurfaceFromSurface
1604 * Copies the contents from one surface to another, performing any required
1605 * format conversion, resizing or filtering.
1608 * pDestSurface [I] pointer to the destination surface
1609 * pDestPalette [I] palette to use
1610 * pDestRect [I] to be filled area of the surface
1611 * pSrcSurface [I] pointer to the source surface
1612 * pSrcPalette [I] palette used for the source surface
1613 * pSrcRect [I] area of the source data to load
1614 * dwFilter [I] filter to apply on resizing
1615 * Colorkey [I] any ARGB value or 0 to disable color-keying
1619 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1620 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1623 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
1624 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
1625 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
1628 D3DLOCKED_RECT lock;
1629 D3DSURFACE_DESC SrcDesc;
1632 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1633 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1634 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
1635 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1637 if (!dst_surface || !src_surface)
1638 return D3DERR_INVALIDCALL;
1640 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
1643 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1647 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
1648 return D3DXERR_INVALIDDATA;
1650 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
1651 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
1653 IDirect3DSurface9_UnlockRect(src_surface);
1659 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1660 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1665 ID3DXBuffer *buffer;
1667 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1668 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1670 if (!dst_filename) return D3DERR_INVALIDCALL;
1672 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1673 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1674 if (!filename) return E_OUTOFMEMORY;
1675 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1677 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1680 hr = write_buffer_to_file(filename, buffer);
1681 ID3DXBuffer_Release(buffer);
1684 HeapFree(GetProcessHeap(), 0, filename);
1688 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1689 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1692 ID3DXBuffer *buffer;
1694 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1695 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1697 if (!dst_filename) return D3DERR_INVALIDCALL;
1699 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1702 hr = write_buffer_to_file(dst_filename, buffer);
1703 ID3DXBuffer_Release(buffer);
1709 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1710 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1712 IWICBitmapEncoder *encoder = NULL;
1713 IWICBitmapFrameEncode *frame = NULL;
1714 IPropertyBag2 *encoder_options = NULL;
1715 IStream *stream = NULL;
1718 const CLSID *encoder_clsid;
1719 const GUID *pixel_format_guid;
1720 WICPixelFormatGUID wic_pixel_format;
1721 D3DFORMAT d3d_pixel_format;
1722 D3DSURFACE_DESC src_surface_desc;
1723 D3DLOCKED_RECT locked_rect;
1725 STATSTG stream_stats;
1726 HGLOBAL stream_hglobal;
1727 ID3DXBuffer *buffer;
1730 TRACE("(%p, %#x, %p, %p, %s)\n",
1731 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1733 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
1737 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1738 return D3DERR_INVALIDCALL;
1741 switch (file_format)
1744 encoder_clsid = &CLSID_WICBmpEncoder;
1747 encoder_clsid = &CLSID_WICPngEncoder;
1750 encoder_clsid = &CLSID_WICJpegEncoder;
1758 FIXME("File format %#x is not supported yet\n", file_format);
1761 return D3DERR_INVALIDCALL;
1764 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
1767 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
1769 WARN("Invalid rectangle with 0 area\n");
1770 return D3DXCreateBuffer(64, dst_buffer);
1772 if (src_rect->left < 0 || src_rect->top < 0)
1773 return D3DERR_INVALIDCALL;
1774 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
1775 return D3DERR_INVALIDCALL;
1776 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
1777 return D3DERR_INVALIDCALL;
1779 width = src_rect->right - src_rect->left;
1780 height = src_rect->bottom - src_rect->top;
1784 width = src_surface_desc.Width;
1785 height = src_surface_desc.Height;
1788 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1790 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
1791 &IID_IWICBitmapEncoder, (void **)&encoder);
1792 if (FAILED(hr)) goto cleanup_err;
1794 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
1795 if (FAILED(hr)) goto cleanup_err;
1797 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
1798 if (FAILED(hr)) goto cleanup_err;
1800 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
1801 if (FAILED(hr)) goto cleanup_err;
1803 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
1804 if (FAILED(hr)) goto cleanup_err;
1806 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
1807 if (FAILED(hr)) goto cleanup_err;
1809 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
1810 if (!pixel_format_guid)
1812 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
1817 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
1818 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
1819 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
1820 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
1822 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
1824 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
1826 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1829 IWICBitmapFrameEncode_WritePixels(frame, height,
1830 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
1831 IDirect3DSurface9_UnlockRect(src_surface);
1834 else /* Pixel format conversion */
1836 const PixelFormatDesc *src_format_desc, *dst_format_desc;
1841 src_format_desc = get_format_info(src_surface_desc.Format);
1842 dst_format_desc = get_format_info(d3d_pixel_format);
1843 if (src_format_desc->format == D3DFMT_UNKNOWN || dst_format_desc->format == D3DFMT_UNKNOWN)
1845 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
1846 src_surface_desc.Format, d3d_pixel_format);
1851 if (src_format_desc->bytes_per_pixel > 4
1852 || dst_format_desc->bytes_per_pixel > 4
1853 || src_format_desc->block_height != 1 || src_format_desc->block_width != 1
1854 || dst_format_desc->block_height != 1 || dst_format_desc->block_width != 1)
1861 size.height = height;
1863 dst_pitch = width * dst_format_desc->bytes_per_pixel;
1864 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
1871 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1874 copy_simple_data(locked_rect.pBits, locked_rect.Pitch, 0, &size, src_format_desc,
1875 dst_data, dst_pitch, 0, &size, dst_format_desc, 0);
1876 IDirect3DSurface9_UnlockRect(src_surface);
1879 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
1880 HeapFree(GetProcessHeap(), 0, dst_data);
1883 hr = IWICBitmapFrameEncode_Commit(frame);
1884 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
1886 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
1888 /* copy data from stream to ID3DXBuffer */
1889 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
1890 if (FAILED(hr)) goto cleanup_err;
1892 if (stream_stats.cbSize.u.HighPart != 0)
1894 hr = D3DXERR_INVALIDDATA;
1897 size = stream_stats.cbSize.u.LowPart;
1899 hr = D3DXCreateBuffer(size, &buffer);
1900 if (FAILED(hr)) goto cleanup;
1902 hr = GetHGlobalFromStream(stream, &stream_hglobal);
1905 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
1906 void *stream_data = GlobalLock(stream_hglobal);
1907 memcpy(buffer_pointer, stream_data, size);
1908 GlobalUnlock(stream_hglobal);
1909 *dst_buffer = buffer;
1911 else ID3DXBuffer_Release(buffer);
1914 if (FAILED(hr) && hr != E_OUTOFMEMORY)
1915 hr = D3DERR_INVALIDCALL;
1918 if (stream) IStream_Release(stream);
1920 if (frame) IWICBitmapFrameEncode_Release(frame);
1921 if (encoder_options) IPropertyBag2_Release(encoder_options);
1923 if (encoder) IWICBitmapEncoder_Release(encoder);
1925 if (SUCCEEDED(initresult)) CoUninitialize();