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 },
195 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
197 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
198 && rgb_pixel_formats[i].rmask == pixel_format->rmask
199 && rgb_pixel_formats[i].gmask == pixel_format->gmask
200 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
202 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
203 return rgb_pixel_formats[i].format;
204 if (rgb_pixel_formats[i].amask == 0)
205 return rgb_pixel_formats[i].format;
209 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
210 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
211 return D3DFMT_UNKNOWN;
214 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
216 if (pixel_format->bpp == 8)
218 if (pixel_format->rmask == 0xff)
220 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
223 if (pixel_format->bpp == 16)
225 if (pixel_format->rmask == 0xffff)
227 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
231 WARN("Unknown luminance pixel format (bpp %#x, l %#x, a %#x)\n",
232 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
233 return D3DFMT_UNKNOWN;
236 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
238 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
241 WARN("Unknown Alpha pixel format (%#x, %#x)\n", pixel_format->bpp, pixel_format->rmask);
242 return D3DFMT_UNKNOWN;
245 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
247 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00)
249 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000)
250 return D3DFMT_V16U16;
252 WARN("Unknown bump pixel format (%#x, %#x, %#x, %#x, %#x)\n", pixel_format->bpp,
253 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
254 return D3DFMT_UNKNOWN;
257 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
259 if (pixel_format->flags & DDS_PF_FOURCC)
260 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
261 if (pixel_format->flags & DDS_PF_RGB)
262 return dds_rgb_to_d3dformat(pixel_format);
263 if (pixel_format->flags & DDS_PF_LUMINANCE)
264 return dds_luminance_to_d3dformat(pixel_format);
265 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
266 return dds_alpha_to_d3dformat(pixel_format);
267 if (pixel_format->flags & DDS_PF_BUMPDUDV)
268 return dds_bump_to_d3dformat(pixel_format);
270 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %#x, r %#x, g %#x, b %#x, a %#x)\n",
271 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
272 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
273 return D3DFMT_UNKNOWN;
276 static HRESULT calculate_dds_surface_size(const D3DXIMAGE_INFO *img_info,
277 UINT width, UINT height, UINT *pitch, UINT *size)
279 const PixelFormatDesc *format_desc = get_format_info(img_info->Format);
280 if (format_desc->format == D3DFMT_UNKNOWN)
283 if (format_desc->block_width != 1 || format_desc->block_height != 1)
285 *pitch = format_desc->block_byte_count
286 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
288 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
292 *pitch = width * format_desc->bytes_per_pixel;
293 *size = *pitch * height;
299 /************************************************************
300 * get_image_info_from_dds
302 * Fills a D3DXIMAGE_INFO structure with information
303 * about a DDS file stored in the memory.
306 * buffer [I] pointer to DDS data
307 * length [I] size of DDS data
308 * info [O] pointer to D3DXIMAGE_INFO structure
312 * Failure: D3DXERR_INVALIDDATA
315 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
320 const struct dds_header *header = buffer;
321 UINT expected_length = 0;
323 if (length < sizeof(*header) || !info)
324 return D3DXERR_INVALIDDATA;
326 if (header->pixel_format.size != sizeof(header->pixel_format))
327 return D3DXERR_INVALIDDATA;
329 info->Width = header->width;
330 info->Height = header->height;
332 info->MipLevels = (header->flags & DDS_MIPMAPCOUNT) ? header->miplevels : 1;
334 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
335 if (info->Format == D3DFMT_UNKNOWN)
336 return D3DXERR_INVALIDDATA;
338 TRACE("Pixel format is %#x\n", info->Format);
340 if (header->caps2 & DDS_CAPS2_VOLUME)
342 info->Depth = header->depth;
343 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
345 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
348 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
350 if (header->caps2 & face)
353 info->ResourceType = D3DRTYPE_CUBETEXTURE;
358 info->ResourceType = D3DRTYPE_TEXTURE;
361 /* calculate the expected length */
363 height = info->Height;
364 for (i = 0; i < info->MipLevels; i++)
366 UINT pitch, size = 0;
367 calculate_dds_surface_size(info, width, height, &pitch, &size);
368 expected_length += size;
369 width = max(1, width / 2);
370 height = max(1, height / 2);
373 expected_length *= faces;
374 expected_length += sizeof(*header);
375 if (length < expected_length)
377 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
378 return D3DXERR_INVALIDDATA;
381 info->ImageFileFormat = D3DXIFF_DDS;
386 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
387 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
388 const D3DXIMAGE_INFO *src_info)
392 const struct dds_header *header = src_data;
393 const BYTE *pixels = (BYTE *)(header + 1);
395 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
396 return D3DXERR_INVALIDDATA;
398 if (FAILED(calculate_dds_surface_size(src_info, src_info->Width, src_info->Height, &src_pitch, &size)))
401 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
402 src_pitch, NULL, src_rect, filter, color_key);
405 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
406 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info)
416 IDirect3DSurface9 *surface;
417 const struct dds_header *header = src_data;
418 const BYTE *pixels = (BYTE *)(header + 1);
420 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
421 return D3DXERR_INVALIDDATA;
423 width = src_info->Width;
424 height = src_info->Height;
425 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
426 for (mip_level = 0; mip_level < mip_levels; mip_level++)
428 hr = calculate_dds_surface_size(src_info, width, height, &src_pitch, &mip_level_size);
429 if (FAILED(hr)) return hr;
431 SetRect(&src_rect, 0, 0, width, height);
433 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level, &surface);
434 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
435 NULL, &src_rect, filter, color_key);
436 IDirect3DSurface9_Release(surface);
437 if (FAILED(hr)) return hr;
439 pixels += mip_level_size;
440 width = max(1, width / 2);
441 height = max(1, height / 2);
447 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
448 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
458 IDirect3DSurface9 *surface;
459 const struct dds_header *header = src_data;
460 const BYTE *pixels = (BYTE *)(header + 1);
462 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
463 return D3DXERR_INVALIDDATA;
465 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
467 WARN("Only full cubemaps are supported\n");
468 return D3DXERR_INVALIDDATA;
471 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
472 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
474 size = src_info->Width;
475 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
477 hr = calculate_dds_surface_size(src_info, size, size, &src_pitch, &mip_level_size);
478 if (FAILED(hr)) return hr;
480 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
481 if (mip_level < mip_levels)
483 SetRect(&src_rect, 0, 0, size, size);
485 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
486 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
487 NULL, &src_rect, filter, color_key);
488 IDirect3DSurface9_Release(surface);
489 if (FAILED(hr)) return hr;
492 pixels += mip_level_size;
493 size = max(1, size / 2);
500 /************************************************************
501 * D3DXGetImageInfoFromFileInMemory
503 * Fills a D3DXIMAGE_INFO structure with info about an image
506 * data [I] pointer to the image file data
507 * datasize [I] size of the passed data
508 * info [O] pointer to the destination structure
511 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
512 * if info is NULL and data and datasize are not NULL
513 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
514 * D3DERR_INVALIDCALL, if data is NULL or
518 * datasize may be bigger than the actual file size
521 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
523 IWICImagingFactory *factory;
524 IWICBitmapDecoder *decoder = NULL;
529 TRACE("(%p, %d, %p)\n", data, datasize, info);
531 if (!data || !datasize)
532 return D3DERR_INVALIDCALL;
537 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
538 TRACE("File type is DDS\n");
539 return get_image_info_from_dds(data, datasize, info);
542 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
544 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
547 IWICImagingFactory_CreateStream(factory, &stream);
548 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
549 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
550 IStream_Release(stream);
551 IWICImagingFactory_Release(factory);
555 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
556 FIXME("File type PPM is not supported yet\n");
557 else if ((datasize >= 2) && !strncmp(data, "BM", 2))
558 FIXME("File type DIB is not supported yet\n");
559 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
560 FIXME("File type HDR is not supported yet\n");
561 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
562 FIXME("File type PFM is not supported yet\n");
566 GUID container_format;
569 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
571 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
572 TRACE("File type is BMP\n");
573 info->ImageFileFormat = D3DXIFF_BMP;
574 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
575 TRACE("File type is PNG\n");
576 info->ImageFileFormat = D3DXIFF_PNG;
577 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
578 TRACE("File type is JPG\n");
579 info->ImageFileFormat = D3DXIFF_JPG;
580 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
581 TRACE("File type is TGA\n");
582 info->ImageFileFormat = D3DXIFF_TGA;
584 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
585 hr = D3DXERR_INVALIDDATA;
590 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
591 if (SUCCEEDED(hr) && !frame_count)
592 hr = D3DXERR_INVALIDDATA;
595 IWICBitmapFrameDecode *frame = NULL;
597 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
600 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
603 WICPixelFormatGUID pixel_format;
605 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
607 info->Format = wic_guid_to_d3dformat(&pixel_format);
608 if (info->Format == D3DFMT_UNKNOWN) {
609 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
610 hr = D3DXERR_INVALIDDATA;
616 IWICBitmapFrameDecode_Release(frame);
620 info->ResourceType = D3DRTYPE_TEXTURE;
625 IWICBitmapDecoder_Release(decoder);
627 if (SUCCEEDED(initresult))
631 TRACE("Invalid or unsupported image file\n");
632 return D3DXERR_INVALIDDATA;
638 /************************************************************
639 * D3DXGetImageInfoFromFile
642 * Success: D3D_OK, if we successfully load a valid image file or
643 * if we successfully load a file which is no valid image and info is NULL
644 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
645 * if file is not a valid image file and info is not NULL
646 * D3DERR_INVALIDCALL, if file is NULL
649 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
655 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
657 if( !file ) return D3DERR_INVALIDCALL;
659 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
660 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
661 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
663 hr = D3DXGetImageInfoFromFileW(widename, info);
664 HeapFree(GetProcessHeap(), 0, widename);
669 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
675 TRACE("(%s, %p): relay\n", debugstr_w(file), info);
677 if( !file ) return D3DERR_INVALIDCALL;
679 hr = map_view_of_file(file, &buffer, &size);
680 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
682 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
683 UnmapViewOfFile(buffer);
688 /************************************************************
689 * D3DXGetImageInfoFromResource
692 * Success: D3D_OK, if resource is a valid image file
693 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
694 * if we fail to load resource
697 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
701 TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
703 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
709 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
710 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
711 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
714 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
716 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
719 return D3DXERR_INVALIDDATA;
722 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
726 TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
728 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
734 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
735 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
736 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
739 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
741 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
744 return D3DXERR_INVALIDDATA;
747 /************************************************************
748 * D3DXLoadSurfaceFromFileInMemory
750 * Loads data from a given buffer into a surface and fills a given
751 * D3DXIMAGE_INFO structure with info about the source data.
754 * pDestSurface [I] pointer to the surface
755 * pDestPalette [I] palette to use
756 * pDestRect [I] to be filled area of the surface
757 * pSrcData [I] pointer to the source data
758 * SrcDataSize [I] size of the source data in bytes
759 * pSrcRect [I] area of the source data to load
760 * dwFilter [I] filter to apply on stretching
761 * Colorkey [I] colorkey
762 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
766 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
767 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
770 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
771 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
772 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
774 D3DXIMAGE_INFO imginfo;
777 IWICImagingFactory *factory;
778 IWICBitmapDecoder *decoder;
779 IWICBitmapFrameDecode *bitmapframe;
782 const PixelFormatDesc *formatdesc;
786 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
787 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
788 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
789 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
791 if (!pDestSurface || !pSrcData || !SrcDataSize)
792 return D3DERR_INVALIDCALL;
794 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
801 wicrect.X = pSrcRect->left;
802 wicrect.Y = pSrcRect->top;
803 wicrect.Width = pSrcRect->right - pSrcRect->left;
804 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
810 wicrect.Width = imginfo.Width;
811 wicrect.Height = imginfo.Height;
814 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
816 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
818 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
819 dwFilter, Colorkey, &imginfo);
820 if (SUCCEEDED(hr) && pSrcInfo)
825 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
827 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
830 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
832 IWICImagingFactory_Release(factory);
836 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
838 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
840 IStream_Release(stream);
841 IWICImagingFactory_Release(factory);
846 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
851 formatdesc = get_format_info(imginfo.Format);
853 if (formatdesc->format == D3DFMT_UNKNOWN)
855 FIXME("Unsupported pixel format\n");
856 hr = D3DXERR_INVALIDDATA;
863 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
864 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
866 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
867 pitch * wicrect.Height, buffer);
871 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
872 buffer, imginfo.Format, pitch,
873 NULL, &rect, dwFilter, Colorkey);
876 HeapFree(GetProcessHeap(), 0, buffer);
879 IWICBitmapFrameDecode_Release(bitmapframe);
882 IWICBitmapDecoder_Release(decoder);
888 return D3DXERR_INVALIDDATA;
896 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
897 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
898 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
904 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
905 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
906 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
907 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
909 if (!src_file || !dst_surface)
910 return D3DERR_INVALIDCALL;
912 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
913 pWidename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*pWidename));
914 MultiByteToWideChar(CP_ACP, 0, src_file, -1, pWidename, strlength);
916 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
917 pWidename, src_rect, filter, color_key, src_info);
918 HeapFree(GetProcessHeap(), 0, pWidename);
923 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
924 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
925 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
931 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
932 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
933 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
934 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
936 if (!src_file || !dst_surface)
937 return D3DERR_INVALIDCALL;
939 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
940 return D3DXERR_INVALIDDATA;
942 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
943 data, data_size, src_rect, filter, color_key, src_info);
944 UnmapViewOfFile(data);
949 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
950 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
951 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
955 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
956 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
957 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
958 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
961 return D3DERR_INVALIDCALL;
963 if ((hResInfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA)))
968 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
969 return D3DXERR_INVALIDDATA;
971 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
972 data, data_size, src_rect, filter, color_key, src_info);
975 if ((hResInfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
977 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
981 return D3DXERR_INVALIDDATA;
984 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
985 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
986 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
990 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
991 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
992 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
993 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
996 return D3DERR_INVALIDCALL;
998 if ((hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA)))
1003 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
1004 return D3DXERR_INVALIDDATA;
1006 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1007 data, data_size, src_rect, filter, color_key, src_info);
1010 if ((hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1012 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
1016 return D3DXERR_INVALIDDATA;
1020 /************************************************************
1021 * helper functions for D3DXLoadSurfaceFromMemory
1023 struct argb_conversion_info
1025 CONST PixelFormatDesc *srcformat;
1026 CONST PixelFormatDesc *destformat;
1027 DWORD srcshift[4], destshift[4];
1028 DWORD srcmask[4], destmask[4];
1029 BOOL process_channel[4];
1033 static void init_argb_conversion_info(CONST PixelFormatDesc *srcformat, CONST PixelFormatDesc *destformat, struct argb_conversion_info *info)
1036 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1037 info->channelmask = 0;
1039 info->srcformat = srcformat;
1040 info->destformat = destformat;
1042 for(i = 0;i < 4;i++) {
1043 /* srcshift is used to extract the _relevant_ components */
1044 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1046 /* destshift is used to move the components to the correct position */
1047 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1049 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1050 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1052 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1053 if(destformat->bits[i]) {
1054 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1055 else info->channelmask |= info->destmask[i];
1060 static DWORD dword_from_bytes(CONST BYTE *src, UINT bytes_per_pixel)
1063 static BOOL fixme_once;
1065 if(bytes_per_pixel > sizeof(DWORD)) {
1066 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1067 bytes_per_pixel = sizeof(DWORD);
1070 memcpy(&ret, src, bytes_per_pixel);
1074 static void dword_to_bytes(BYTE *dst, DWORD dword, UINT bytes_per_pixel)
1076 static BOOL fixme_once;
1078 if(bytes_per_pixel > sizeof(DWORD)) {
1079 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1080 ZeroMemory(dst, bytes_per_pixel);
1081 bytes_per_pixel = sizeof(DWORD);
1084 memcpy(dst, &dword, bytes_per_pixel);
1087 /************************************************************
1088 * get_relevant_argb_components
1090 * Extracts the relevant components from the source color and
1091 * drops the less significant bits if they aren't used by the destination format.
1093 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
1097 if(info->process_channel[i])
1098 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
1101 /************************************************************
1104 * Recombines the output of get_relevant_argb_components and converts
1105 * it to the destination format.
1107 static DWORD make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in)
1112 for(i = 0;i < 4;i++) {
1113 if(info->process_channel[i]) {
1114 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1116 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1117 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1120 val |= info->channelmask; /* new channels are set to their maximal value */
1124 static void format_to_vec4(const PixelFormatDesc *format, const DWORD *src, struct vec4 *dst)
1128 if (format->bits[1])
1130 mask = (1 << format->bits[1]) - 1;
1131 dst->x = (float)((*src >> format->shift[1]) & mask) / mask;
1136 if (format->bits[2])
1138 mask = (1 << format->bits[2]) - 1;
1139 dst->y = (float)((*src >> format->shift[2]) & mask) / mask;
1144 if (format->bits[3])
1146 mask = (1 << format->bits[3]) - 1;
1147 dst->z = (float)((*src >> format->shift[3]) & mask) / mask;
1152 if (format->bits[0])
1154 mask = (1 << format->bits[0]) - 1;
1155 dst->w = (float)((*src >> format->shift[0]) & mask) / mask;
1161 static void format_from_vec4(const PixelFormatDesc *format, const struct vec4 *src, DWORD *dst)
1165 if (format->bits[1])
1166 *dst |= (DWORD)(src->x * ((1 << format->bits[1]) - 1) + 0.5f) << format->shift[1];
1167 if (format->bits[2])
1168 *dst |= (DWORD)(src->y * ((1 << format->bits[2]) - 1) + 0.5f) << format->shift[2];
1169 if (format->bits[3])
1170 *dst |= (DWORD)(src->z * ((1 << format->bits[3]) - 1) + 0.5f) << format->shift[3];
1171 if (format->bits[0])
1172 *dst |= (DWORD)(src->w * ((1 << format->bits[0]) - 1) + 0.5f) << format->shift[0];
1175 /************************************************************
1178 * Copies the source buffer to the destination buffer, performing
1179 * any necessary format conversion and color keying.
1180 * Pixels outsize the source rect are blacked out.
1181 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1183 static void copy_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
1184 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
1186 struct argb_conversion_info conv_info, ck_conv_info;
1187 const PixelFormatDesc *ck_format = NULL;
1188 DWORD channels[4], pixel;
1189 UINT minwidth, minheight;
1192 ZeroMemory(channels, sizeof(channels));
1193 init_argb_conversion_info(srcformat, destformat, &conv_info);
1195 minwidth = (src_size.cx < dst_size.cx) ? src_size.cx : dst_size.cx;
1196 minheight = (src_size.cy < dst_size.cy) ? src_size.cy : dst_size.cy;
1200 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1201 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1202 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1205 for(y = 0;y < minheight;y++) {
1206 const BYTE *srcptr = src + y * srcpitch;
1207 BYTE *destptr = dest + y * destpitch;
1210 for(x = 0;x < minwidth;x++) {
1211 /* extract source color components */
1212 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1214 if (!srcformat->to_rgba && !destformat->from_rgba)
1216 get_relevant_argb_components(&conv_info, pixel, channels);
1217 val = make_argb_color(&conv_info, channels);
1221 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1222 pixel = make_argb_color(&ck_conv_info, channels);
1223 if (pixel == colorkey)
1224 val &= ~conv_info.destmask[0];
1229 struct vec4 color, tmp;
1231 format_to_vec4(srcformat, &pixel, &color);
1232 if (srcformat->to_rgba)
1233 srcformat->to_rgba(&color, &tmp);
1239 format_from_vec4(ck_format, &tmp, &pixel);
1240 if (pixel == colorkey)
1244 if (destformat->from_rgba)
1245 destformat->from_rgba(&tmp, &color);
1249 format_from_vec4(destformat, &color, &val);
1252 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1253 srcptr += srcformat->bytes_per_pixel;
1254 destptr += destformat->bytes_per_pixel;
1257 if (src_size.cx < dst_size.cx) /* black out remaining pixels */
1258 memset(destptr, 0, destformat->bytes_per_pixel * (dst_size.cx - src_size.cx));
1260 if (src_size.cy < dst_size.cy) /* black out remaining pixels */
1261 memset(dest + src_size.cy * destpitch, 0, destpitch * (dst_size.cy - src_size.cy));
1264 /************************************************************
1265 * point_filter_simple_data
1267 * Copies the source buffer to the destination buffer, performing
1268 * any necessary format conversion, color keying and stretching
1269 * using a point filter.
1270 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1272 static void point_filter_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
1273 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
1275 struct argb_conversion_info conv_info, ck_conv_info;
1276 const PixelFormatDesc *ck_format = NULL;
1277 DWORD channels[4], pixel;
1281 ZeroMemory(channels, sizeof(channels));
1282 init_argb_conversion_info(srcformat, destformat, &conv_info);
1286 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1287 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1288 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1291 for (y = 0; y < dst_size.cy; ++y)
1293 BYTE *destptr = dest + y * destpitch;
1294 const BYTE *bufptr = src + srcpitch * (y * src_size.cy / dst_size.cy);
1296 for (x = 0; x < dst_size.cx; ++x)
1298 const BYTE *srcptr = bufptr + (x * src_size.cx / dst_size.cx) * srcformat->bytes_per_pixel;
1301 /* extract source color components */
1302 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1304 if (!srcformat->to_rgba && !destformat->from_rgba)
1306 get_relevant_argb_components(&conv_info, pixel, channels);
1307 val = make_argb_color(&conv_info, channels);
1311 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1312 pixel = make_argb_color(&ck_conv_info, channels);
1313 if (pixel == colorkey)
1314 val &= ~conv_info.destmask[0];
1319 struct vec4 color, tmp;
1321 format_to_vec4(srcformat, &pixel, &color);
1322 if (srcformat->to_rgba)
1323 srcformat->to_rgba(&color, &tmp);
1329 format_from_vec4(ck_format, &tmp, &pixel);
1330 if (pixel == colorkey)
1334 if (destformat->from_rgba)
1335 destformat->from_rgba(&tmp, &color);
1339 format_from_vec4(destformat, &color, &val);
1342 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1343 destptr += destformat->bytes_per_pixel;
1348 /************************************************************
1349 * D3DXLoadSurfaceFromMemory
1351 * Loads data from a given memory chunk into a surface,
1352 * applying any of the specified filters.
1355 * pDestSurface [I] pointer to the surface
1356 * pDestPalette [I] palette to use
1357 * pDestRect [I] to be filled area of the surface
1358 * pSrcMemory [I] pointer to the source data
1359 * SrcFormat [I] format of the source pixel data
1360 * SrcPitch [I] number of bytes in a row
1361 * pSrcPalette [I] palette used in the source image
1362 * pSrcRect [I] area of the source data to load
1363 * dwFilter [I] filter to apply on stretching
1364 * Colorkey [I] colorkey
1367 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1368 * if pSrcMemory is NULL but the other parameters are valid
1369 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1370 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1371 * if DestRect is invalid
1372 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1373 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1376 * pSrcRect specifies the dimensions of the source data;
1377 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1380 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1381 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1382 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1383 DWORD filter, D3DCOLOR color_key)
1385 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
1386 D3DSURFACE_DESC surfdesc;
1387 D3DLOCKED_RECT lockrect;
1388 SIZE src_size, dst_size;
1391 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1392 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1393 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1395 if (!dst_surface || !src_memory || !src_rect)
1396 return D3DERR_INVALIDCALL;
1397 if (src_format == D3DFMT_UNKNOWN
1398 || src_rect->left >= src_rect->right
1399 || src_rect->top >= src_rect->bottom)
1402 if (filter == D3DX_DEFAULT)
1403 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1405 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1407 src_size.cx = src_rect->right - src_rect->left;
1408 src_size.cy = src_rect->bottom - src_rect->top;
1411 dst_size.cx = surfdesc.Width;
1412 dst_size.cy = surfdesc.Height;
1416 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width)
1417 return D3DERR_INVALIDCALL;
1418 if (dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height)
1419 return D3DERR_INVALIDCALL;
1420 if (dst_rect->left < 0 || dst_rect->top < 0)
1421 return D3DERR_INVALIDCALL;
1422 dst_size.cx = dst_rect->right - dst_rect->left;
1423 dst_size.cy = dst_rect->bottom - dst_rect->top;
1424 if (!dst_size.cx || !dst_size.cy)
1428 srcformatdesc = get_format_info(src_format);
1429 if (srcformatdesc->type == FORMAT_UNKNOWN)
1432 destformatdesc = get_format_info(surfdesc.Format);
1433 if (destformatdesc->type == FORMAT_UNKNOWN)
1436 if (src_format == surfdesc.Format
1437 && dst_size.cx == src_size.cx
1438 && dst_size.cy == src_size.cy) /* Simple copy. */
1440 UINT row_block_count = ((src_size.cx + srcformatdesc->block_width - 1) / srcformatdesc->block_width);
1441 UINT row_count = (src_size.cy + srcformatdesc->block_height - 1) / srcformatdesc->block_height;
1442 const BYTE *src_addr;
1446 if (src_rect->left & (srcformatdesc->block_width - 1)
1447 || src_rect->top & (srcformatdesc->block_height - 1)
1448 || (src_rect->right & (srcformatdesc->block_width - 1)
1449 && src_size.cx != surfdesc.Width)
1450 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1451 && src_size.cy != surfdesc.Height))
1453 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1454 return D3DXERR_INVALIDDATA;
1457 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1458 return D3DXERR_INVALIDDATA;
1460 src_addr = src_memory;
1461 src_addr += (src_rect->top / srcformatdesc->block_height) * src_pitch;
1462 src_addr += (src_rect->left / srcformatdesc->block_width) * srcformatdesc->block_byte_count;
1463 dst_addr = lockrect.pBits;
1465 for (row = 0; row < row_count; ++row)
1467 memcpy(dst_addr, src_addr, row_block_count * srcformatdesc->block_byte_count);
1468 src_addr += src_pitch;
1469 dst_addr += lockrect.Pitch;
1472 IDirect3DSurface9_UnlockRect(dst_surface);
1474 else /* Stretching or format conversion. */
1476 if (srcformatdesc->bytes_per_pixel > 4)
1478 if (destformatdesc->bytes_per_pixel > 4)
1480 if (srcformatdesc->block_height != 1 || srcformatdesc->block_width != 1)
1482 if (destformatdesc->block_height != 1 || destformatdesc->block_width != 1)
1485 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1486 return D3DXERR_INVALIDDATA;
1488 if ((filter & 0xf) == D3DX_FILTER_NONE)
1490 copy_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1491 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1493 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1495 if ((filter & 0xf) != D3DX_FILTER_POINT)
1496 FIXME("Unhandled filter %#x.\n", filter);
1498 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1499 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1500 point_filter_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1501 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1504 IDirect3DSurface9_UnlockRect(dst_surface);
1510 /************************************************************
1511 * D3DXLoadSurfaceFromSurface
1513 * Copies the contents from one surface to another, performing any required
1514 * format conversion, resizing or filtering.
1517 * pDestSurface [I] pointer to the destination surface
1518 * pDestPalette [I] palette to use
1519 * pDestRect [I] to be filled area of the surface
1520 * pSrcSurface [I] pointer to the source surface
1521 * pSrcPalette [I] palette used for the source surface
1522 * pSrcRect [I] area of the source data to load
1523 * dwFilter [I] filter to apply on resizing
1524 * Colorkey [I] any ARGB value or 0 to disable color-keying
1528 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1529 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1532 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
1533 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
1534 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
1537 D3DLOCKED_RECT lock;
1538 D3DSURFACE_DESC SrcDesc;
1541 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1542 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1543 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
1544 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1546 if (!dst_surface || !src_surface)
1547 return D3DERR_INVALIDCALL;
1549 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
1552 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1556 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
1557 return D3DXERR_INVALIDDATA;
1559 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
1560 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
1562 IDirect3DSurface9_UnlockRect(src_surface);
1568 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1569 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1574 ID3DXBuffer *buffer;
1576 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1577 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1579 if (!dst_filename) return D3DERR_INVALIDCALL;
1581 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1582 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1583 if (!filename) return E_OUTOFMEMORY;
1584 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1586 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1589 hr = write_buffer_to_file(filename, buffer);
1590 ID3DXBuffer_Release(buffer);
1593 HeapFree(GetProcessHeap(), 0, filename);
1597 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1598 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1601 ID3DXBuffer *buffer;
1603 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1604 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1606 if (!dst_filename) return D3DERR_INVALIDCALL;
1608 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1611 hr = write_buffer_to_file(dst_filename, buffer);
1612 ID3DXBuffer_Release(buffer);
1618 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1619 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1621 IWICBitmapEncoder *encoder = NULL;
1622 IWICBitmapFrameEncode *frame = NULL;
1623 IPropertyBag2 *encoder_options = NULL;
1624 IStream *stream = NULL;
1627 const CLSID *encoder_clsid;
1628 const GUID *pixel_format_guid;
1629 WICPixelFormatGUID wic_pixel_format;
1630 D3DFORMAT d3d_pixel_format;
1631 D3DSURFACE_DESC src_surface_desc;
1632 D3DLOCKED_RECT locked_rect;
1634 STATSTG stream_stats;
1635 HGLOBAL stream_hglobal;
1636 ID3DXBuffer *buffer;
1639 TRACE("(%p, %#x, %p, %p, %s)\n",
1640 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1642 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
1646 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1647 return D3DERR_INVALIDCALL;
1650 switch (file_format)
1653 encoder_clsid = &CLSID_WICBmpEncoder;
1656 encoder_clsid = &CLSID_WICPngEncoder;
1659 encoder_clsid = &CLSID_WICJpegEncoder;
1667 FIXME("File format %#x is not supported yet\n", file_format);
1670 return D3DERR_INVALIDCALL;
1673 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
1676 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
1678 WARN("Invalid rectangle with 0 area\n");
1679 return D3DXCreateBuffer(64, dst_buffer);
1681 if (src_rect->left < 0 || src_rect->top < 0)
1682 return D3DERR_INVALIDCALL;
1683 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
1684 return D3DERR_INVALIDCALL;
1685 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
1686 return D3DERR_INVALIDCALL;
1688 width = src_rect->right - src_rect->left;
1689 height = src_rect->bottom - src_rect->top;
1693 width = src_surface_desc.Width;
1694 height = src_surface_desc.Height;
1697 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1699 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
1700 &IID_IWICBitmapEncoder, (void **)&encoder);
1701 if (FAILED(hr)) goto cleanup_err;
1703 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
1704 if (FAILED(hr)) goto cleanup_err;
1706 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
1707 if (FAILED(hr)) goto cleanup_err;
1709 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
1710 if (FAILED(hr)) goto cleanup_err;
1712 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
1713 if (FAILED(hr)) goto cleanup_err;
1715 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
1716 if (FAILED(hr)) goto cleanup_err;
1718 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
1719 if (!pixel_format_guid)
1721 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
1726 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
1727 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
1728 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
1729 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
1731 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
1733 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
1735 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1738 IWICBitmapFrameEncode_WritePixels(frame, height,
1739 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
1740 IDirect3DSurface9_UnlockRect(src_surface);
1743 else /* Pixel format conversion */
1745 const PixelFormatDesc *src_format_desc, *dst_format_desc;
1750 src_format_desc = get_format_info(src_surface_desc.Format);
1751 dst_format_desc = get_format_info(d3d_pixel_format);
1752 if (src_format_desc->format == D3DFMT_UNKNOWN || dst_format_desc->format == D3DFMT_UNKNOWN)
1754 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
1755 src_surface_desc.Format, d3d_pixel_format);
1760 if (src_format_desc->bytes_per_pixel > 4
1761 || dst_format_desc->bytes_per_pixel > 4
1762 || src_format_desc->block_height != 1 || src_format_desc->block_width != 1
1763 || dst_format_desc->block_height != 1 || dst_format_desc->block_width != 1)
1771 dst_pitch = width * dst_format_desc->bytes_per_pixel;
1772 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
1779 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1782 copy_simple_data(locked_rect.pBits, locked_rect.Pitch, size, src_format_desc,
1783 dst_data, dst_pitch, size, dst_format_desc, 0);
1784 IDirect3DSurface9_UnlockRect(src_surface);
1787 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
1788 HeapFree(GetProcessHeap(), 0, dst_data);
1791 hr = IWICBitmapFrameEncode_Commit(frame);
1792 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
1794 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
1796 /* copy data from stream to ID3DXBuffer */
1797 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
1798 if (FAILED(hr)) goto cleanup_err;
1800 if (stream_stats.cbSize.u.HighPart != 0)
1802 hr = D3DXERR_INVALIDDATA;
1805 size = stream_stats.cbSize.u.LowPart;
1807 hr = D3DXCreateBuffer(size, &buffer);
1808 if (FAILED(hr)) goto cleanup;
1810 hr = GetHGlobalFromStream(stream, &stream_hglobal);
1813 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
1814 void *stream_data = GlobalLock(stream_hglobal);
1815 memcpy(buffer_pointer, stream_data, size);
1816 GlobalUnlock(stream_hglobal);
1817 *dst_buffer = buffer;
1819 else ID3DXBuffer_Release(buffer);
1822 if (FAILED(hr) && hr != E_OUTOFMEMORY)
1823 hr = D3DERR_INVALIDCALL;
1826 if (stream) IStream_Release(stream);
1828 if (frame) IWICBitmapFrameEncode_Release(frame);
1829 if (encoder_options) IPropertyBag2_Release(encoder_options);
1831 if (encoder) IWICBitmapEncoder_Release(encoder);
1833 if (SUCCEEDED(initresult)) CoUninitialize();