wbemprox: Win32_NetworkAdapter.InterfaceIndex is unsigned.
[wine/multimedia.git] / dlls / d3dx9_36 / surface.c
blob7be3ebe65cb52c7e32508c148c72a01b1daf7298
1 /*
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"
25 #include "initguid.h"
26 #include "ole2.h"
27 #include "wincodec.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);
35 static const struct
37 const GUID *wic_guid;
38 D3DFORMAT d3dformat;
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)
52 int i;
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)
65 int i;
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;
73 return NULL;
76 /* dds_header.flags */
77 #define DDS_CAPS 0x1
78 #define DDS_HEIGHT 0x2
79 #define DDS_WIDTH 0x2
80 #define DDS_PITCH 0x8
81 #define DDS_PIXELFORMAT 0x1000
82 #define DDS_MIPMAPCOUNT 0x20000
83 #define DDS_LINEARSIZE 0x80000
84 #define DDS_DEPTH 0x800000
86 /* dds_header.caps */
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
115 DWORD size;
116 DWORD flags;
117 DWORD fourcc;
118 DWORD bpp;
119 DWORD rmask;
120 DWORD gmask;
121 DWORD bmask;
122 DWORD amask;
125 struct dds_header
127 DWORD signature;
128 DWORD size;
129 DWORD flags;
130 DWORD height;
131 DWORD width;
132 DWORD pitch_or_linear_size;
133 DWORD depth;
134 DWORD miplevels;
135 DWORD reserved[11];
136 struct dds_pixel_format pixel_format;
137 DWORD caps;
138 DWORD caps2;
139 DWORD caps3;
140 DWORD caps4;
141 DWORD reserved2;
144 static D3DFORMAT dds_fourcc_to_d3dformat(DWORD fourcc)
146 int i;
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)
162 return 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)
171 int i;
172 static const struct {
173 DWORD bpp;
174 DWORD rmask;
175 DWORD gmask;
176 DWORD bmask;
177 DWORD amask;
178 D3DFORMAT format;
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)
221 return D3DFMT_L8;
222 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
223 return D3DFMT_A4L4;
225 if (pixel_format->bpp == 16)
227 if (pixel_format->rmask == 0xffff)
228 return D3DFMT_L16;
229 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
230 return D3DFMT_A8L8;
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)
241 return D3DFMT_A8;
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)
250 return D3DFMT_V8U8;
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)
283 return E_NOTIMPL;
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);
289 *size = *pitch
290 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
292 else
294 *pitch = width * format_desc->bytes_per_pixel;
295 *size = *pitch * height;
298 return D3D_OK;
301 /************************************************************
302 * get_image_info_from_dds
304 * Fills a D3DXIMAGE_INFO structure with information
305 * about a DDS file stored in the memory.
307 * PARAMS
308 * buffer [I] pointer to DDS data
309 * length [I] size of DDS data
310 * info [O] pointer to D3DXIMAGE_INFO structure
312 * RETURNS
313 * Success: D3D_OK
314 * Failure: D3DXERR_INVALIDDATA
317 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
319 UINT i;
320 UINT faces = 0;
321 UINT width, height;
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;
333 info->Depth = 1;
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)
349 DWORD face;
350 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
352 if (header->caps2 & face)
353 faces++;
355 info->ResourceType = D3DRTYPE_CUBETEXTURE;
357 else
359 faces = 1;
360 info->ResourceType = D3DRTYPE_TEXTURE;
363 /* calculate the expected length */
364 width = info->Width;
365 height = info->Height;
366 for (i = 0; i < info->MipLevels; i++)
368 UINT pitch, size = 0;
369 calculate_dds_surface_size(info, width, height, &pitch, &size);
370 expected_length += size;
371 width = max(1, width / 2);
372 height = max(1, height / 2);
375 expected_length *= faces;
376 expected_length += sizeof(*header);
377 if (length < expected_length)
379 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
380 return D3DXERR_INVALIDDATA;
383 info->ImageFileFormat = D3DXIFF_DDS;
385 return D3D_OK;
388 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
389 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
390 const D3DXIMAGE_INFO *src_info)
392 UINT size;
393 UINT src_pitch;
394 const struct dds_header *header = src_data;
395 const BYTE *pixels = (BYTE *)(header + 1);
397 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
398 return D3DXERR_INVALIDDATA;
400 if (FAILED(calculate_dds_surface_size(src_info, src_info->Width, src_info->Height, &src_pitch, &size)))
401 return E_NOTIMPL;
403 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
404 src_pitch, NULL, src_rect, filter, color_key);
407 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
408 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info)
411 HRESULT hr;
412 RECT src_rect;
413 UINT src_pitch;
414 UINT mip_level;
415 UINT mip_levels;
416 UINT mip_level_size;
417 UINT width, height;
418 IDirect3DSurface9 *surface;
419 const struct dds_header *header = src_data;
420 const BYTE *pixels = (BYTE *)(header + 1);
422 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
423 return D3DXERR_INVALIDDATA;
425 width = src_info->Width;
426 height = src_info->Height;
427 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
428 for (mip_level = 0; mip_level < mip_levels; mip_level++)
430 hr = calculate_dds_surface_size(src_info, width, height, &src_pitch, &mip_level_size);
431 if (FAILED(hr)) return hr;
433 SetRect(&src_rect, 0, 0, width, height);
435 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level, &surface);
436 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
437 NULL, &src_rect, filter, color_key);
438 IDirect3DSurface9_Release(surface);
439 if (FAILED(hr)) return hr;
441 pixels += mip_level_size;
442 width = max(1, width / 2);
443 height = max(1, height / 2);
446 return D3D_OK;
449 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
450 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
452 HRESULT hr;
453 int face;
454 int mip_level;
455 UINT size;
456 RECT src_rect;
457 UINT src_pitch;
458 UINT mip_levels;
459 UINT mip_level_size;
460 IDirect3DSurface9 *surface;
461 const struct dds_header *header = src_data;
462 const BYTE *pixels = (BYTE *)(header + 1);
464 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
465 return D3DXERR_INVALIDDATA;
467 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
469 WARN("Only full cubemaps are supported\n");
470 return D3DXERR_INVALIDDATA;
473 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
474 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
476 size = src_info->Width;
477 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
479 hr = calculate_dds_surface_size(src_info, size, size, &src_pitch, &mip_level_size);
480 if (FAILED(hr)) return hr;
482 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
483 if (mip_level < mip_levels)
485 SetRect(&src_rect, 0, 0, size, size);
487 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
488 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
489 NULL, &src_rect, filter, color_key);
490 IDirect3DSurface9_Release(surface);
491 if (FAILED(hr)) return hr;
494 pixels += mip_level_size;
495 size = max(1, size / 2);
499 return D3D_OK;
502 /************************************************************
503 * D3DXGetImageInfoFromFileInMemory
505 * Fills a D3DXIMAGE_INFO structure with info about an image
507 * PARAMS
508 * data [I] pointer to the image file data
509 * datasize [I] size of the passed data
510 * info [O] pointer to the destination structure
512 * RETURNS
513 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
514 * if info is NULL and data and datasize are not NULL
515 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
516 * D3DERR_INVALIDCALL, if data is NULL or
517 * if datasize is 0
519 * NOTES
520 * datasize may be bigger than the actual file size
523 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
525 IWICImagingFactory *factory;
526 IWICBitmapDecoder *decoder = NULL;
527 IWICStream *stream;
528 HRESULT hr;
529 HRESULT initresult;
531 TRACE("(%p, %d, %p)\n", data, datasize, info);
533 if (!data || !datasize)
534 return D3DERR_INVALIDCALL;
536 if (!info)
537 return D3D_OK;
539 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
540 TRACE("File type is DDS\n");
541 return get_image_info_from_dds(data, datasize, info);
544 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
546 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
548 if (SUCCEEDED(hr)) {
549 IWICImagingFactory_CreateStream(factory, &stream);
550 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
551 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
552 IStream_Release(stream);
553 IWICImagingFactory_Release(factory);
556 if (FAILED(hr)) {
557 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
558 FIXME("File type PPM is not supported yet\n");
559 else if ((datasize >= 2) && !strncmp(data, "BM", 2))
560 FIXME("File type DIB is not supported yet\n");
561 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
562 FIXME("File type HDR is not supported yet\n");
563 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
564 FIXME("File type PFM is not supported yet\n");
567 if (SUCCEEDED(hr)) {
568 GUID container_format;
569 UINT frame_count;
571 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
572 if (SUCCEEDED(hr)) {
573 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
574 TRACE("File type is BMP\n");
575 info->ImageFileFormat = D3DXIFF_BMP;
576 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
577 TRACE("File type is PNG\n");
578 info->ImageFileFormat = D3DXIFF_PNG;
579 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
580 TRACE("File type is JPG\n");
581 info->ImageFileFormat = D3DXIFF_JPG;
582 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
583 TRACE("File type is TGA\n");
584 info->ImageFileFormat = D3DXIFF_TGA;
585 } else {
586 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
587 hr = D3DXERR_INVALIDDATA;
591 if (SUCCEEDED(hr))
592 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
593 if (SUCCEEDED(hr) && !frame_count)
594 hr = D3DXERR_INVALIDDATA;
596 if (SUCCEEDED(hr)) {
597 IWICBitmapFrameDecode *frame = NULL;
599 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
601 if (SUCCEEDED(hr))
602 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
604 if (SUCCEEDED(hr)) {
605 WICPixelFormatGUID pixel_format;
607 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
608 if (SUCCEEDED(hr)) {
609 info->Format = wic_guid_to_d3dformat(&pixel_format);
610 if (info->Format == D3DFMT_UNKNOWN) {
611 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
612 hr = D3DXERR_INVALIDDATA;
617 if (frame)
618 IWICBitmapFrameDecode_Release(frame);
620 info->Depth = 1;
621 info->MipLevels = 1;
622 info->ResourceType = D3DRTYPE_TEXTURE;
626 if (decoder)
627 IWICBitmapDecoder_Release(decoder);
629 if (SUCCEEDED(initresult))
630 CoUninitialize();
632 if (FAILED(hr)) {
633 TRACE("Invalid or unsupported image file\n");
634 return D3DXERR_INVALIDDATA;
637 return D3D_OK;
640 /************************************************************
641 * D3DXGetImageInfoFromFile
643 * RETURNS
644 * Success: D3D_OK, if we successfully load a valid image file or
645 * if we successfully load a file which is no valid image and info is NULL
646 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
647 * if file is not a valid image file and info is not NULL
648 * D3DERR_INVALIDCALL, if file is NULL
651 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
653 LPWSTR widename;
654 HRESULT hr;
655 int strlength;
657 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
659 if( !file ) return D3DERR_INVALIDCALL;
661 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
662 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
663 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
665 hr = D3DXGetImageInfoFromFileW(widename, info);
666 HeapFree(GetProcessHeap(), 0, widename);
668 return hr;
671 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
673 HRESULT hr;
674 DWORD size;
675 LPVOID buffer;
677 TRACE("(%s, %p): relay\n", debugstr_w(file), info);
679 if( !file ) return D3DERR_INVALIDCALL;
681 hr = map_view_of_file(file, &buffer, &size);
682 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
684 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
685 UnmapViewOfFile(buffer);
687 return hr;
690 /************************************************************
691 * D3DXGetImageInfoFromResource
693 * RETURNS
694 * Success: D3D_OK, if resource is a valid image file
695 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
696 * if we fail to load resource
699 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
701 HRSRC resinfo;
703 TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
705 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
706 if(resinfo) {
707 LPVOID buffer;
708 HRESULT hr;
709 DWORD size;
711 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
712 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
713 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
716 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
717 if(resinfo) {
718 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
719 return E_NOTIMPL;
721 return D3DXERR_INVALIDDATA;
724 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
726 HRSRC resinfo;
728 TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
730 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
731 if(resinfo) {
732 LPVOID buffer;
733 HRESULT hr;
734 DWORD size;
736 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
737 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
738 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
741 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
742 if(resinfo) {
743 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
744 return E_NOTIMPL;
746 return D3DXERR_INVALIDDATA;
749 /************************************************************
750 * D3DXLoadSurfaceFromFileInMemory
752 * Loads data from a given buffer into a surface and fills a given
753 * D3DXIMAGE_INFO structure with info about the source data.
755 * PARAMS
756 * pDestSurface [I] pointer to the surface
757 * pDestPalette [I] palette to use
758 * pDestRect [I] to be filled area of the surface
759 * pSrcData [I] pointer to the source data
760 * SrcDataSize [I] size of the source data in bytes
761 * pSrcRect [I] area of the source data to load
762 * dwFilter [I] filter to apply on stretching
763 * Colorkey [I] colorkey
764 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
766 * RETURNS
767 * Success: D3D_OK
768 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
769 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
772 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
773 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
774 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
776 D3DXIMAGE_INFO imginfo;
777 HRESULT hr;
779 IWICImagingFactory *factory;
780 IWICBitmapDecoder *decoder;
781 IWICBitmapFrameDecode *bitmapframe;
782 IWICStream *stream;
784 const PixelFormatDesc *formatdesc;
785 WICRect wicrect;
786 RECT rect;
788 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
789 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
790 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
791 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
793 if (!pDestSurface || !pSrcData || !SrcDataSize)
794 return D3DERR_INVALIDCALL;
796 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
798 if (FAILED(hr))
799 return hr;
801 if (pSrcRect)
803 wicrect.X = pSrcRect->left;
804 wicrect.Y = pSrcRect->top;
805 wicrect.Width = pSrcRect->right - pSrcRect->left;
806 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
808 else
810 wicrect.X = 0;
811 wicrect.Y = 0;
812 wicrect.Width = imginfo.Width;
813 wicrect.Height = imginfo.Height;
816 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
818 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
820 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
821 dwFilter, Colorkey, &imginfo);
822 if (SUCCEEDED(hr) && pSrcInfo)
823 *pSrcInfo = imginfo;
824 return hr;
827 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
829 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
830 goto cleanup_err;
832 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
834 IWICImagingFactory_Release(factory);
835 goto cleanup_err;
838 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
840 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
842 IStream_Release(stream);
843 IWICImagingFactory_Release(factory);
845 if (FAILED(hr))
846 goto cleanup_err;
848 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
850 if (FAILED(hr))
851 goto cleanup_bmp;
853 formatdesc = get_format_info(imginfo.Format);
855 if (formatdesc->format == D3DFMT_UNKNOWN)
857 FIXME("Unsupported pixel format\n");
858 hr = D3DXERR_INVALIDDATA;
860 else
862 BYTE *buffer;
863 DWORD pitch;
865 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
866 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
868 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
869 pitch * wicrect.Height, buffer);
871 if (SUCCEEDED(hr))
873 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
874 buffer, imginfo.Format, pitch,
875 NULL, &rect, dwFilter, Colorkey);
878 HeapFree(GetProcessHeap(), 0, buffer);
881 IWICBitmapFrameDecode_Release(bitmapframe);
883 cleanup_bmp:
884 IWICBitmapDecoder_Release(decoder);
886 cleanup_err:
887 CoUninitialize();
889 if (FAILED(hr))
890 return D3DXERR_INVALIDDATA;
892 if (pSrcInfo)
893 *pSrcInfo = imginfo;
895 return D3D_OK;
898 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
899 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
900 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
902 LPWSTR pWidename;
903 HRESULT hr;
904 int strlength;
906 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
907 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
908 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
909 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
911 if (!src_file || !dst_surface)
912 return D3DERR_INVALIDCALL;
914 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
915 pWidename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*pWidename));
916 MultiByteToWideChar(CP_ACP, 0, src_file, -1, pWidename, strlength);
918 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
919 pWidename, src_rect, filter, color_key, src_info);
920 HeapFree(GetProcessHeap(), 0, pWidename);
922 return hr;
925 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
926 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
927 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
929 UINT data_size;
930 void *data;
931 HRESULT hr;
933 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
934 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
935 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
936 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
938 if (!src_file || !dst_surface)
939 return D3DERR_INVALIDCALL;
941 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
942 return D3DXERR_INVALIDDATA;
944 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
945 data, data_size, src_rect, filter, color_key, src_info);
946 UnmapViewOfFile(data);
948 return hr;
951 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
952 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
953 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
955 HRSRC hResInfo;
957 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
958 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
959 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
960 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
962 if (!dst_surface)
963 return D3DERR_INVALIDCALL;
965 if ((hResInfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA)))
967 UINT data_size;
968 void *data;
970 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
971 return D3DXERR_INVALIDDATA;
973 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
974 data, data_size, src_rect, filter, color_key, src_info);
977 if ((hResInfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
979 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
980 return E_NOTIMPL;
983 return D3DXERR_INVALIDDATA;
986 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
987 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
988 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
990 HRSRC hResInfo;
992 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
993 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
994 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
995 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
997 if (!dst_surface)
998 return D3DERR_INVALIDCALL;
1000 if ((hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA)))
1002 UINT data_size;
1003 void *data;
1005 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
1006 return D3DXERR_INVALIDDATA;
1008 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1009 data, data_size, src_rect, filter, color_key, src_info);
1012 if ((hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1014 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
1015 return E_NOTIMPL;
1018 return D3DXERR_INVALIDDATA;
1022 /************************************************************
1023 * helper functions for D3DXLoadSurfaceFromMemory
1025 struct argb_conversion_info
1027 CONST PixelFormatDesc *srcformat;
1028 CONST PixelFormatDesc *destformat;
1029 DWORD srcshift[4], destshift[4];
1030 DWORD srcmask[4], destmask[4];
1031 BOOL process_channel[4];
1032 DWORD channelmask;
1035 static void init_argb_conversion_info(CONST PixelFormatDesc *srcformat, CONST PixelFormatDesc *destformat, struct argb_conversion_info *info)
1037 UINT i;
1038 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1039 info->channelmask = 0;
1041 info->srcformat = srcformat;
1042 info->destformat = destformat;
1044 for(i = 0;i < 4;i++) {
1045 /* srcshift is used to extract the _relevant_ components */
1046 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1048 /* destshift is used to move the components to the correct position */
1049 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1051 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1052 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1054 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1055 if(destformat->bits[i]) {
1056 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1057 else info->channelmask |= info->destmask[i];
1062 static DWORD dword_from_bytes(CONST BYTE *src, UINT bytes_per_pixel)
1064 DWORD ret = 0;
1065 static BOOL fixme_once;
1067 if(bytes_per_pixel > sizeof(DWORD)) {
1068 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1069 bytes_per_pixel = sizeof(DWORD);
1072 memcpy(&ret, src, bytes_per_pixel);
1073 return ret;
1076 static void dword_to_bytes(BYTE *dst, DWORD dword, UINT bytes_per_pixel)
1078 static BOOL fixme_once;
1080 if(bytes_per_pixel > sizeof(DWORD)) {
1081 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1082 ZeroMemory(dst, bytes_per_pixel);
1083 bytes_per_pixel = sizeof(DWORD);
1086 memcpy(dst, &dword, bytes_per_pixel);
1089 /************************************************************
1090 * get_relevant_argb_components
1092 * Extracts the relevant components from the source color and
1093 * drops the less significant bits if they aren't used by the destination format.
1095 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
1097 UINT i = 0;
1098 for(;i < 4;i++)
1099 if(info->process_channel[i])
1100 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
1103 /************************************************************
1104 * make_argb_color
1106 * Recombines the output of get_relevant_argb_components and converts
1107 * it to the destination format.
1109 static DWORD make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in)
1111 UINT i;
1112 DWORD val = 0;
1114 for(i = 0;i < 4;i++) {
1115 if(info->process_channel[i]) {
1116 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1117 signed int shift;
1118 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1119 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1122 val |= info->channelmask; /* new channels are set to their maximal value */
1123 return val;
1126 static void format_to_vec4(const PixelFormatDesc *format, const DWORD *src, struct vec4 *dst)
1128 DWORD mask;
1130 if (format->bits[1])
1132 mask = (1 << format->bits[1]) - 1;
1133 dst->x = (float)((*src >> format->shift[1]) & mask) / mask;
1135 else
1136 dst->x = 1.0f;
1138 if (format->bits[2])
1140 mask = (1 << format->bits[2]) - 1;
1141 dst->y = (float)((*src >> format->shift[2]) & mask) / mask;
1143 else
1144 dst->y = 1.0f;
1146 if (format->bits[3])
1148 mask = (1 << format->bits[3]) - 1;
1149 dst->z = (float)((*src >> format->shift[3]) & mask) / mask;
1151 else
1152 dst->z = 1.0f;
1154 if (format->bits[0])
1156 mask = (1 << format->bits[0]) - 1;
1157 dst->w = (float)((*src >> format->shift[0]) & mask) / mask;
1159 else
1160 dst->w = 1.0f;
1163 static void format_from_vec4(const PixelFormatDesc *format, const struct vec4 *src, DWORD *dst)
1165 *dst = 0;
1167 if (format->bits[1])
1168 *dst |= (DWORD)(src->x * ((1 << format->bits[1]) - 1) + 0.5f) << format->shift[1];
1169 if (format->bits[2])
1170 *dst |= (DWORD)(src->y * ((1 << format->bits[2]) - 1) + 0.5f) << format->shift[2];
1171 if (format->bits[3])
1172 *dst |= (DWORD)(src->z * ((1 << format->bits[3]) - 1) + 0.5f) << format->shift[3];
1173 if (format->bits[0])
1174 *dst |= (DWORD)(src->w * ((1 << format->bits[0]) - 1) + 0.5f) << format->shift[0];
1177 /************************************************************
1178 * copy_simple_data
1180 * Copies the source buffer to the destination buffer, performing
1181 * any necessary format conversion and color keying.
1182 * Pixels outsize the source rect are blacked out.
1183 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1185 static void copy_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
1186 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
1188 struct argb_conversion_info conv_info, ck_conv_info;
1189 const PixelFormatDesc *ck_format = NULL;
1190 DWORD channels[4], pixel;
1191 UINT minwidth, minheight;
1192 UINT x, y;
1194 ZeroMemory(channels, sizeof(channels));
1195 init_argb_conversion_info(srcformat, destformat, &conv_info);
1197 minwidth = (src_size.cx < dst_size.cx) ? src_size.cx : dst_size.cx;
1198 minheight = (src_size.cy < dst_size.cy) ? src_size.cy : dst_size.cy;
1200 if (colorkey)
1202 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1203 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1204 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1207 for(y = 0;y < minheight;y++) {
1208 const BYTE *srcptr = src + y * srcpitch;
1209 BYTE *destptr = dest + y * destpitch;
1210 DWORD val;
1212 for(x = 0;x < minwidth;x++) {
1213 /* extract source color components */
1214 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1216 if (!srcformat->to_rgba && !destformat->from_rgba)
1218 get_relevant_argb_components(&conv_info, pixel, channels);
1219 val = make_argb_color(&conv_info, channels);
1221 if (colorkey)
1223 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1224 pixel = make_argb_color(&ck_conv_info, channels);
1225 if (pixel == colorkey)
1226 val &= ~conv_info.destmask[0];
1229 else
1231 struct vec4 color, tmp;
1233 format_to_vec4(srcformat, &pixel, &color);
1234 if (srcformat->to_rgba)
1235 srcformat->to_rgba(&color, &tmp);
1236 else
1237 tmp = color;
1239 if (ck_format)
1241 format_from_vec4(ck_format, &tmp, &pixel);
1242 if (pixel == colorkey)
1243 tmp.w = 0.0f;
1246 if (destformat->from_rgba)
1247 destformat->from_rgba(&tmp, &color);
1248 else
1249 color = tmp;
1251 format_from_vec4(destformat, &color, &val);
1254 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1255 srcptr += srcformat->bytes_per_pixel;
1256 destptr += destformat->bytes_per_pixel;
1259 if (src_size.cx < dst_size.cx) /* black out remaining pixels */
1260 memset(destptr, 0, destformat->bytes_per_pixel * (dst_size.cx - src_size.cx));
1262 if (src_size.cy < dst_size.cy) /* black out remaining pixels */
1263 memset(dest + src_size.cy * destpitch, 0, destpitch * (dst_size.cy - src_size.cy));
1266 /************************************************************
1267 * point_filter_simple_data
1269 * Copies the source buffer to the destination buffer, performing
1270 * any necessary format conversion, color keying and stretching
1271 * using a point filter.
1272 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1274 static void point_filter_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
1275 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
1277 struct argb_conversion_info conv_info, ck_conv_info;
1278 const PixelFormatDesc *ck_format = NULL;
1279 DWORD channels[4], pixel;
1281 UINT x, y;
1283 ZeroMemory(channels, sizeof(channels));
1284 init_argb_conversion_info(srcformat, destformat, &conv_info);
1286 if (colorkey)
1288 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1289 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1290 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1293 for (y = 0; y < dst_size.cy; ++y)
1295 BYTE *destptr = dest + y * destpitch;
1296 const BYTE *bufptr = src + srcpitch * (y * src_size.cy / dst_size.cy);
1298 for (x = 0; x < dst_size.cx; ++x)
1300 const BYTE *srcptr = bufptr + (x * src_size.cx / dst_size.cx) * srcformat->bytes_per_pixel;
1301 DWORD val;
1303 /* extract source color components */
1304 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1306 if (!srcformat->to_rgba && !destformat->from_rgba)
1308 get_relevant_argb_components(&conv_info, pixel, channels);
1309 val = make_argb_color(&conv_info, channels);
1311 if (colorkey)
1313 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1314 pixel = make_argb_color(&ck_conv_info, channels);
1315 if (pixel == colorkey)
1316 val &= ~conv_info.destmask[0];
1319 else
1321 struct vec4 color, tmp;
1323 format_to_vec4(srcformat, &pixel, &color);
1324 if (srcformat->to_rgba)
1325 srcformat->to_rgba(&color, &tmp);
1326 else
1327 tmp = color;
1329 if (ck_format)
1331 format_from_vec4(ck_format, &tmp, &pixel);
1332 if (pixel == colorkey)
1333 tmp.w = 0.0f;
1336 if (destformat->from_rgba)
1337 destformat->from_rgba(&tmp, &color);
1338 else
1339 color = tmp;
1341 format_from_vec4(destformat, &color, &val);
1344 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1345 destptr += destformat->bytes_per_pixel;
1350 /************************************************************
1351 * D3DXLoadSurfaceFromMemory
1353 * Loads data from a given memory chunk into a surface,
1354 * applying any of the specified filters.
1356 * PARAMS
1357 * pDestSurface [I] pointer to the surface
1358 * pDestPalette [I] palette to use
1359 * pDestRect [I] to be filled area of the surface
1360 * pSrcMemory [I] pointer to the source data
1361 * SrcFormat [I] format of the source pixel data
1362 * SrcPitch [I] number of bytes in a row
1363 * pSrcPalette [I] palette used in the source image
1364 * pSrcRect [I] area of the source data to load
1365 * dwFilter [I] filter to apply on stretching
1366 * Colorkey [I] colorkey
1368 * RETURNS
1369 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1370 * if pSrcMemory is NULL but the other parameters are valid
1371 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1372 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1373 * if DestRect is invalid
1374 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1375 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1377 * NOTES
1378 * pSrcRect specifies the dimensions of the source data;
1379 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1382 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1383 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1384 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1385 DWORD filter, D3DCOLOR color_key)
1387 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
1388 D3DSURFACE_DESC surfdesc;
1389 D3DLOCKED_RECT lockrect;
1390 SIZE src_size, dst_size;
1391 HRESULT hr;
1393 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1394 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1395 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1397 if (!dst_surface || !src_memory || !src_rect)
1398 return D3DERR_INVALIDCALL;
1399 if (src_format == D3DFMT_UNKNOWN
1400 || src_rect->left >= src_rect->right
1401 || src_rect->top >= src_rect->bottom)
1402 return E_FAIL;
1404 if (filter == D3DX_DEFAULT)
1405 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1407 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1409 src_size.cx = src_rect->right - src_rect->left;
1410 src_size.cy = src_rect->bottom - src_rect->top;
1411 if (!dst_rect)
1413 dst_size.cx = surfdesc.Width;
1414 dst_size.cy = surfdesc.Height;
1416 else
1418 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width)
1419 return D3DERR_INVALIDCALL;
1420 if (dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height)
1421 return D3DERR_INVALIDCALL;
1422 if (dst_rect->left < 0 || dst_rect->top < 0)
1423 return D3DERR_INVALIDCALL;
1424 dst_size.cx = dst_rect->right - dst_rect->left;
1425 dst_size.cy = dst_rect->bottom - dst_rect->top;
1426 if (!dst_size.cx || !dst_size.cy)
1427 return D3D_OK;
1430 srcformatdesc = get_format_info(src_format);
1431 if (srcformatdesc->type == FORMAT_UNKNOWN)
1432 return E_NOTIMPL;
1434 destformatdesc = get_format_info(surfdesc.Format);
1435 if (destformatdesc->type == FORMAT_UNKNOWN)
1436 return E_NOTIMPL;
1438 if (src_format == surfdesc.Format
1439 && dst_size.cx == src_size.cx
1440 && dst_size.cy == src_size.cy) /* Simple copy. */
1442 UINT row_block_count = ((src_size.cx + srcformatdesc->block_width - 1) / srcformatdesc->block_width);
1443 UINT row_count = (src_size.cy + srcformatdesc->block_height - 1) / srcformatdesc->block_height;
1444 const BYTE *src_addr;
1445 BYTE *dst_addr;
1446 UINT row;
1448 if (src_rect->left & (srcformatdesc->block_width - 1)
1449 || src_rect->top & (srcformatdesc->block_height - 1)
1450 || (src_rect->right & (srcformatdesc->block_width - 1)
1451 && src_size.cx != surfdesc.Width)
1452 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1453 && src_size.cy != surfdesc.Height))
1455 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1456 return D3DXERR_INVALIDDATA;
1459 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1460 return D3DXERR_INVALIDDATA;
1462 src_addr = src_memory;
1463 src_addr += (src_rect->top / srcformatdesc->block_height) * src_pitch;
1464 src_addr += (src_rect->left / srcformatdesc->block_width) * srcformatdesc->block_byte_count;
1465 dst_addr = lockrect.pBits;
1467 for (row = 0; row < row_count; ++row)
1469 memcpy(dst_addr, src_addr, row_block_count * srcformatdesc->block_byte_count);
1470 src_addr += src_pitch;
1471 dst_addr += lockrect.Pitch;
1474 IDirect3DSurface9_UnlockRect(dst_surface);
1476 else /* Stretching or format conversion. */
1478 if (srcformatdesc->bytes_per_pixel > 4)
1479 return E_NOTIMPL;
1480 if (destformatdesc->bytes_per_pixel > 4)
1481 return E_NOTIMPL;
1482 if (srcformatdesc->block_height != 1 || srcformatdesc->block_width != 1)
1483 return E_NOTIMPL;
1484 if (destformatdesc->block_height != 1 || destformatdesc->block_width != 1)
1485 return E_NOTIMPL;
1487 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1488 return D3DXERR_INVALIDDATA;
1490 if ((filter & 0xf) == D3DX_FILTER_NONE)
1492 copy_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1493 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1495 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1497 if ((filter & 0xf) != D3DX_FILTER_POINT)
1498 FIXME("Unhandled filter %#x.\n", filter);
1500 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1501 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1502 point_filter_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1503 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1506 IDirect3DSurface9_UnlockRect(dst_surface);
1509 return D3D_OK;
1512 /************************************************************
1513 * D3DXLoadSurfaceFromSurface
1515 * Copies the contents from one surface to another, performing any required
1516 * format conversion, resizing or filtering.
1518 * PARAMS
1519 * pDestSurface [I] pointer to the destination surface
1520 * pDestPalette [I] palette to use
1521 * pDestRect [I] to be filled area of the surface
1522 * pSrcSurface [I] pointer to the source surface
1523 * pSrcPalette [I] palette used for the source surface
1524 * pSrcRect [I] area of the source data to load
1525 * dwFilter [I] filter to apply on resizing
1526 * Colorkey [I] any ARGB value or 0 to disable color-keying
1528 * RETURNS
1529 * Success: D3D_OK
1530 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1531 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1534 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
1535 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
1536 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
1538 RECT rect;
1539 D3DLOCKED_RECT lock;
1540 D3DSURFACE_DESC SrcDesc;
1541 HRESULT hr;
1543 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1544 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1545 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
1546 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1548 if (!dst_surface || !src_surface)
1549 return D3DERR_INVALIDCALL;
1551 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
1553 if (!src_rect)
1554 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1555 else
1556 rect = *src_rect;
1558 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
1559 return D3DXERR_INVALIDDATA;
1561 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
1562 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
1564 IDirect3DSurface9_UnlockRect(src_surface);
1566 return hr;
1570 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1571 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1573 int len;
1574 WCHAR *filename;
1575 HRESULT hr;
1576 ID3DXBuffer *buffer;
1578 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1579 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1581 if (!dst_filename) return D3DERR_INVALIDCALL;
1583 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1584 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1585 if (!filename) return E_OUTOFMEMORY;
1586 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1588 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1589 if (SUCCEEDED(hr))
1591 hr = write_buffer_to_file(filename, buffer);
1592 ID3DXBuffer_Release(buffer);
1595 HeapFree(GetProcessHeap(), 0, filename);
1596 return hr;
1599 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1600 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1602 HRESULT hr;
1603 ID3DXBuffer *buffer;
1605 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1606 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1608 if (!dst_filename) return D3DERR_INVALIDCALL;
1610 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1611 if (SUCCEEDED(hr))
1613 hr = write_buffer_to_file(dst_filename, buffer);
1614 ID3DXBuffer_Release(buffer);
1617 return hr;
1620 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1621 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1623 IWICBitmapEncoder *encoder = NULL;
1624 IWICBitmapFrameEncode *frame = NULL;
1625 IPropertyBag2 *encoder_options = NULL;
1626 IStream *stream = NULL;
1627 HRESULT hr;
1628 HRESULT initresult;
1629 const CLSID *encoder_clsid;
1630 const GUID *pixel_format_guid;
1631 WICPixelFormatGUID wic_pixel_format;
1632 D3DFORMAT d3d_pixel_format;
1633 D3DSURFACE_DESC src_surface_desc;
1634 D3DLOCKED_RECT locked_rect;
1635 int width, height;
1636 STATSTG stream_stats;
1637 HGLOBAL stream_hglobal;
1638 ID3DXBuffer *buffer;
1639 DWORD size;
1641 TRACE("(%p, %#x, %p, %p, %s)\n",
1642 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1644 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
1646 if (src_palette)
1648 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1649 return D3DERR_INVALIDCALL;
1652 switch (file_format)
1654 case D3DXIFF_BMP:
1655 encoder_clsid = &CLSID_WICBmpEncoder;
1656 break;
1657 case D3DXIFF_PNG:
1658 encoder_clsid = &CLSID_WICPngEncoder;
1659 break;
1660 case D3DXIFF_JPG:
1661 encoder_clsid = &CLSID_WICJpegEncoder;
1662 break;
1663 case D3DXIFF_DDS:
1664 case D3DXIFF_DIB:
1665 case D3DXIFF_HDR:
1666 case D3DXIFF_PFM:
1667 case D3DXIFF_TGA:
1668 case D3DXIFF_PPM:
1669 FIXME("File format %#x is not supported yet\n", file_format);
1670 return E_NOTIMPL;
1671 default:
1672 return D3DERR_INVALIDCALL;
1675 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
1676 if (src_rect)
1678 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
1680 WARN("Invalid rectangle with 0 area\n");
1681 return D3DXCreateBuffer(64, dst_buffer);
1683 if (src_rect->left < 0 || src_rect->top < 0)
1684 return D3DERR_INVALIDCALL;
1685 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
1686 return D3DERR_INVALIDCALL;
1687 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
1688 return D3DERR_INVALIDCALL;
1690 width = src_rect->right - src_rect->left;
1691 height = src_rect->bottom - src_rect->top;
1693 else
1695 width = src_surface_desc.Width;
1696 height = src_surface_desc.Height;
1699 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1701 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
1702 &IID_IWICBitmapEncoder, (void **)&encoder);
1703 if (FAILED(hr)) goto cleanup_err;
1705 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
1706 if (FAILED(hr)) goto cleanup_err;
1708 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
1709 if (FAILED(hr)) goto cleanup_err;
1711 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
1712 if (FAILED(hr)) goto cleanup_err;
1714 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
1715 if (FAILED(hr)) goto cleanup_err;
1717 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
1718 if (FAILED(hr)) goto cleanup_err;
1720 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
1721 if (!pixel_format_guid)
1723 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
1724 hr = E_NOTIMPL;
1725 goto cleanup;
1728 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
1729 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
1730 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
1731 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
1733 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
1735 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
1737 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1738 if (SUCCEEDED(hr))
1740 IWICBitmapFrameEncode_WritePixels(frame, height,
1741 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
1742 IDirect3DSurface9_UnlockRect(src_surface);
1745 else /* Pixel format conversion */
1747 const PixelFormatDesc *src_format_desc, *dst_format_desc;
1748 SIZE size;
1749 DWORD dst_pitch;
1750 void *dst_data;
1752 src_format_desc = get_format_info(src_surface_desc.Format);
1753 dst_format_desc = get_format_info(d3d_pixel_format);
1754 if (src_format_desc->format == D3DFMT_UNKNOWN || dst_format_desc->format == D3DFMT_UNKNOWN)
1756 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
1757 src_surface_desc.Format, d3d_pixel_format);
1758 hr = E_NOTIMPL;
1759 goto cleanup;
1762 if (src_format_desc->bytes_per_pixel > 4
1763 || dst_format_desc->bytes_per_pixel > 4
1764 || src_format_desc->block_height != 1 || src_format_desc->block_width != 1
1765 || dst_format_desc->block_height != 1 || dst_format_desc->block_width != 1)
1767 hr = E_NOTIMPL;
1768 goto cleanup;
1771 size.cx = width;
1772 size.cy = height;
1773 dst_pitch = width * dst_format_desc->bytes_per_pixel;
1774 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
1775 if (!dst_data)
1777 hr = E_OUTOFMEMORY;
1778 goto cleanup;
1781 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1782 if (SUCCEEDED(hr))
1784 copy_simple_data(locked_rect.pBits, locked_rect.Pitch, size, src_format_desc,
1785 dst_data, dst_pitch, size, dst_format_desc, 0);
1786 IDirect3DSurface9_UnlockRect(src_surface);
1789 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
1790 HeapFree(GetProcessHeap(), 0, dst_data);
1793 hr = IWICBitmapFrameEncode_Commit(frame);
1794 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
1796 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
1798 /* copy data from stream to ID3DXBuffer */
1799 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
1800 if (FAILED(hr)) goto cleanup_err;
1802 if (stream_stats.cbSize.u.HighPart != 0)
1804 hr = D3DXERR_INVALIDDATA;
1805 goto cleanup;
1807 size = stream_stats.cbSize.u.LowPart;
1809 hr = D3DXCreateBuffer(size, &buffer);
1810 if (FAILED(hr)) goto cleanup;
1812 hr = GetHGlobalFromStream(stream, &stream_hglobal);
1813 if (SUCCEEDED(hr))
1815 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
1816 void *stream_data = GlobalLock(stream_hglobal);
1817 memcpy(buffer_pointer, stream_data, size);
1818 GlobalUnlock(stream_hglobal);
1819 *dst_buffer = buffer;
1821 else ID3DXBuffer_Release(buffer);
1823 cleanup_err:
1824 if (FAILED(hr) && hr != E_OUTOFMEMORY)
1825 hr = D3DERR_INVALIDCALL;
1827 cleanup:
1828 if (stream) IStream_Release(stream);
1830 if (frame) IWICBitmapFrameEncode_Release(frame);
1831 if (encoder_options) IPropertyBag2_Release(encoder_options);
1833 if (encoder) IWICBitmapEncoder_Release(encoder);
1835 if (SUCCEEDED(initresult)) CoUninitialize();
1837 return hr;