dsound: clean up capture interface and prepare for mmdevapi (v2)
[wine/multimedia.git] / dlls / d3dx9_36 / surface.c
blob845a1f70f3afa3642ea3ea53d706c9a42f1752a8
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 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
503 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
505 HRESULT hr;
506 UINT mip_level;
507 UINT mip_levels;
508 UINT src_slice_pitch;
509 UINT src_row_pitch;
510 D3DBOX src_box;
511 UINT width, height, depth;
512 IDirect3DVolume9 *volume;
513 const struct dds_header *header = src_data;
514 const BYTE *pixels = (BYTE *)(header + 1);
516 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
517 return D3DXERR_INVALIDDATA;
519 width = src_info->Width;
520 height = src_info->Height;
521 depth = src_info->Depth;
522 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture));
524 for (mip_level = 0; mip_level < mip_levels; mip_level++)
526 hr = calculate_dds_surface_size(src_info, width, height, &src_row_pitch, &src_slice_pitch);
527 if (FAILED(hr)) return hr;
529 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume);
530 if (FAILED(hr)) return hr;
532 src_box.Left = 0;
533 src_box.Top = 0;
534 src_box.Right = width;
535 src_box.Bottom = height;
536 src_box.Front = 0;
537 src_box.Back = depth;
539 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format,
540 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key);
542 IDirect3DVolume9_Release(volume);
543 if (FAILED(hr)) return hr;
545 pixels += depth * src_slice_pitch;
546 width = max(1, width / 2);
547 height = max(1, height / 2);
548 depth = max(1, depth / 2);
551 return D3D_OK;
554 /************************************************************
555 * D3DXGetImageInfoFromFileInMemory
557 * Fills a D3DXIMAGE_INFO structure with info about an image
559 * PARAMS
560 * data [I] pointer to the image file data
561 * datasize [I] size of the passed data
562 * info [O] pointer to the destination structure
564 * RETURNS
565 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
566 * if info is NULL and data and datasize are not NULL
567 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
568 * D3DERR_INVALIDCALL, if data is NULL or
569 * if datasize is 0
571 * NOTES
572 * datasize may be bigger than the actual file size
575 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
577 IWICImagingFactory *factory;
578 IWICBitmapDecoder *decoder = NULL;
579 IWICStream *stream;
580 HRESULT hr;
581 HRESULT initresult;
583 TRACE("(%p, %d, %p)\n", data, datasize, info);
585 if (!data || !datasize)
586 return D3DERR_INVALIDCALL;
588 if (!info)
589 return D3D_OK;
591 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
592 TRACE("File type is DDS\n");
593 return get_image_info_from_dds(data, datasize, info);
596 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
598 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
600 if (SUCCEEDED(hr)) {
601 IWICImagingFactory_CreateStream(factory, &stream);
602 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
603 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
604 IStream_Release(stream);
605 IWICImagingFactory_Release(factory);
608 if (FAILED(hr)) {
609 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
610 FIXME("File type PPM is not supported yet\n");
611 else if ((datasize >= 2) && !strncmp(data, "BM", 2))
612 FIXME("File type DIB is not supported yet\n");
613 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
614 FIXME("File type HDR is not supported yet\n");
615 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
616 FIXME("File type PFM is not supported yet\n");
619 if (SUCCEEDED(hr)) {
620 GUID container_format;
621 UINT frame_count;
623 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
624 if (SUCCEEDED(hr)) {
625 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
626 TRACE("File type is BMP\n");
627 info->ImageFileFormat = D3DXIFF_BMP;
628 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
629 TRACE("File type is PNG\n");
630 info->ImageFileFormat = D3DXIFF_PNG;
631 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
632 TRACE("File type is JPG\n");
633 info->ImageFileFormat = D3DXIFF_JPG;
634 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
635 TRACE("File type is TGA\n");
636 info->ImageFileFormat = D3DXIFF_TGA;
637 } else {
638 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
639 hr = D3DXERR_INVALIDDATA;
643 if (SUCCEEDED(hr))
644 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
645 if (SUCCEEDED(hr) && !frame_count)
646 hr = D3DXERR_INVALIDDATA;
648 if (SUCCEEDED(hr)) {
649 IWICBitmapFrameDecode *frame = NULL;
651 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
653 if (SUCCEEDED(hr))
654 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
656 if (SUCCEEDED(hr)) {
657 WICPixelFormatGUID pixel_format;
659 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
660 if (SUCCEEDED(hr)) {
661 info->Format = wic_guid_to_d3dformat(&pixel_format);
662 if (info->Format == D3DFMT_UNKNOWN) {
663 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
664 hr = D3DXERR_INVALIDDATA;
669 if (frame)
670 IWICBitmapFrameDecode_Release(frame);
672 info->Depth = 1;
673 info->MipLevels = 1;
674 info->ResourceType = D3DRTYPE_TEXTURE;
678 if (decoder)
679 IWICBitmapDecoder_Release(decoder);
681 if (SUCCEEDED(initresult))
682 CoUninitialize();
684 if (FAILED(hr)) {
685 TRACE("Invalid or unsupported image file\n");
686 return D3DXERR_INVALIDDATA;
689 return D3D_OK;
692 /************************************************************
693 * D3DXGetImageInfoFromFile
695 * RETURNS
696 * Success: D3D_OK, if we successfully load a valid image file or
697 * if we successfully load a file which is no valid image and info is NULL
698 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
699 * if file is not a valid image file and info is not NULL
700 * D3DERR_INVALIDCALL, if file is NULL
703 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
705 LPWSTR widename;
706 HRESULT hr;
707 int strlength;
709 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
711 if( !file ) return D3DERR_INVALIDCALL;
713 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
714 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
715 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
717 hr = D3DXGetImageInfoFromFileW(widename, info);
718 HeapFree(GetProcessHeap(), 0, widename);
720 return hr;
723 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
725 HRESULT hr;
726 DWORD size;
727 LPVOID buffer;
729 TRACE("(%s, %p): relay\n", debugstr_w(file), info);
731 if( !file ) return D3DERR_INVALIDCALL;
733 hr = map_view_of_file(file, &buffer, &size);
734 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
736 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
737 UnmapViewOfFile(buffer);
739 return hr;
742 /************************************************************
743 * D3DXGetImageInfoFromResource
745 * RETURNS
746 * Success: D3D_OK, if resource is a valid image file
747 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
748 * if we fail to load resource
751 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
753 HRSRC resinfo;
755 TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
757 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
758 if(resinfo) {
759 LPVOID buffer;
760 HRESULT hr;
761 DWORD size;
763 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
764 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
765 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
768 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
769 if(resinfo) {
770 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
771 return E_NOTIMPL;
773 return D3DXERR_INVALIDDATA;
776 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
778 HRSRC resinfo;
780 TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
782 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
783 if(resinfo) {
784 LPVOID buffer;
785 HRESULT hr;
786 DWORD size;
788 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
789 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
790 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
793 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
794 if(resinfo) {
795 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
796 return E_NOTIMPL;
798 return D3DXERR_INVALIDDATA;
801 /************************************************************
802 * D3DXLoadSurfaceFromFileInMemory
804 * Loads data from a given buffer into a surface and fills a given
805 * D3DXIMAGE_INFO structure with info about the source data.
807 * PARAMS
808 * pDestSurface [I] pointer to the surface
809 * pDestPalette [I] palette to use
810 * pDestRect [I] to be filled area of the surface
811 * pSrcData [I] pointer to the source data
812 * SrcDataSize [I] size of the source data in bytes
813 * pSrcRect [I] area of the source data to load
814 * dwFilter [I] filter to apply on stretching
815 * Colorkey [I] colorkey
816 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
818 * RETURNS
819 * Success: D3D_OK
820 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
821 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
824 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
825 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
826 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
828 D3DXIMAGE_INFO imginfo;
829 HRESULT hr;
831 IWICImagingFactory *factory;
832 IWICBitmapDecoder *decoder;
833 IWICBitmapFrameDecode *bitmapframe;
834 IWICStream *stream;
836 const PixelFormatDesc *formatdesc;
837 WICRect wicrect;
838 RECT rect;
840 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
841 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
842 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
843 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
845 if (!pDestSurface || !pSrcData || !SrcDataSize)
846 return D3DERR_INVALIDCALL;
848 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
850 if (FAILED(hr))
851 return hr;
853 if (pSrcRect)
855 wicrect.X = pSrcRect->left;
856 wicrect.Y = pSrcRect->top;
857 wicrect.Width = pSrcRect->right - pSrcRect->left;
858 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
860 else
862 wicrect.X = 0;
863 wicrect.Y = 0;
864 wicrect.Width = imginfo.Width;
865 wicrect.Height = imginfo.Height;
868 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
870 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
872 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
873 dwFilter, Colorkey, &imginfo);
874 if (SUCCEEDED(hr) && pSrcInfo)
875 *pSrcInfo = imginfo;
876 return hr;
879 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
881 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
882 goto cleanup_err;
884 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
886 IWICImagingFactory_Release(factory);
887 goto cleanup_err;
890 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
892 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
894 IStream_Release(stream);
895 IWICImagingFactory_Release(factory);
897 if (FAILED(hr))
898 goto cleanup_err;
900 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
902 if (FAILED(hr))
903 goto cleanup_bmp;
905 formatdesc = get_format_info(imginfo.Format);
907 if (formatdesc->format == D3DFMT_UNKNOWN)
909 FIXME("Unsupported pixel format\n");
910 hr = D3DXERR_INVALIDDATA;
912 else
914 BYTE *buffer;
915 DWORD pitch;
917 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
918 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
920 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
921 pitch * wicrect.Height, buffer);
923 if (SUCCEEDED(hr))
925 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
926 buffer, imginfo.Format, pitch,
927 NULL, &rect, dwFilter, Colorkey);
930 HeapFree(GetProcessHeap(), 0, buffer);
933 IWICBitmapFrameDecode_Release(bitmapframe);
935 cleanup_bmp:
936 IWICBitmapDecoder_Release(decoder);
938 cleanup_err:
939 CoUninitialize();
941 if (FAILED(hr))
942 return D3DXERR_INVALIDDATA;
944 if (pSrcInfo)
945 *pSrcInfo = imginfo;
947 return D3D_OK;
950 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
951 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
952 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
954 LPWSTR pWidename;
955 HRESULT hr;
956 int strlength;
958 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
959 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
960 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
961 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
963 if (!src_file || !dst_surface)
964 return D3DERR_INVALIDCALL;
966 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
967 pWidename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*pWidename));
968 MultiByteToWideChar(CP_ACP, 0, src_file, -1, pWidename, strlength);
970 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
971 pWidename, src_rect, filter, color_key, src_info);
972 HeapFree(GetProcessHeap(), 0, pWidename);
974 return hr;
977 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
978 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
979 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
981 UINT data_size;
982 void *data;
983 HRESULT hr;
985 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
986 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
987 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
988 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
990 if (!src_file || !dst_surface)
991 return D3DERR_INVALIDCALL;
993 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
994 return D3DXERR_INVALIDDATA;
996 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
997 data, data_size, src_rect, filter, color_key, src_info);
998 UnmapViewOfFile(data);
1000 return hr;
1003 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
1004 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
1005 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1007 HRSRC hResInfo;
1009 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1010 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1011 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
1012 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1014 if (!dst_surface)
1015 return D3DERR_INVALIDCALL;
1017 if ((hResInfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA)))
1019 UINT data_size;
1020 void *data;
1022 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
1023 return D3DXERR_INVALIDDATA;
1025 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1026 data, data_size, src_rect, filter, color_key, src_info);
1029 if ((hResInfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
1031 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
1032 return E_NOTIMPL;
1035 return D3DXERR_INVALIDDATA;
1038 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
1039 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
1040 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1042 HRSRC hResInfo;
1044 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1045 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1046 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
1047 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1049 if (!dst_surface)
1050 return D3DERR_INVALIDCALL;
1052 if ((hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA)))
1054 UINT data_size;
1055 void *data;
1057 if (FAILED(load_resource_into_memory(src_module, hResInfo, &data, &data_size)))
1058 return D3DXERR_INVALIDDATA;
1060 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1061 data, data_size, src_rect, filter, color_key, src_info);
1064 if ((hResInfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1066 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
1067 return E_NOTIMPL;
1070 return D3DXERR_INVALIDDATA;
1074 /************************************************************
1075 * helper functions for D3DXLoadSurfaceFromMemory
1077 struct argb_conversion_info
1079 CONST PixelFormatDesc *srcformat;
1080 CONST PixelFormatDesc *destformat;
1081 DWORD srcshift[4], destshift[4];
1082 DWORD srcmask[4], destmask[4];
1083 BOOL process_channel[4];
1084 DWORD channelmask;
1087 static void init_argb_conversion_info(CONST PixelFormatDesc *srcformat, CONST PixelFormatDesc *destformat, struct argb_conversion_info *info)
1089 UINT i;
1090 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1091 info->channelmask = 0;
1093 info->srcformat = srcformat;
1094 info->destformat = destformat;
1096 for(i = 0;i < 4;i++) {
1097 /* srcshift is used to extract the _relevant_ components */
1098 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1100 /* destshift is used to move the components to the correct position */
1101 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1103 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1104 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1106 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1107 if(destformat->bits[i]) {
1108 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1109 else info->channelmask |= info->destmask[i];
1114 static DWORD dword_from_bytes(CONST BYTE *src, UINT bytes_per_pixel)
1116 DWORD ret = 0;
1117 static BOOL fixme_once;
1119 if(bytes_per_pixel > sizeof(DWORD)) {
1120 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1121 bytes_per_pixel = sizeof(DWORD);
1124 memcpy(&ret, src, bytes_per_pixel);
1125 return ret;
1128 static void dword_to_bytes(BYTE *dst, DWORD dword, UINT bytes_per_pixel)
1130 static BOOL fixme_once;
1132 if(bytes_per_pixel > sizeof(DWORD)) {
1133 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1134 ZeroMemory(dst, bytes_per_pixel);
1135 bytes_per_pixel = sizeof(DWORD);
1138 memcpy(dst, &dword, bytes_per_pixel);
1141 /************************************************************
1142 * get_relevant_argb_components
1144 * Extracts the relevant components from the source color and
1145 * drops the less significant bits if they aren't used by the destination format.
1147 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
1149 UINT i = 0;
1150 for(;i < 4;i++)
1151 if(info->process_channel[i])
1152 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
1155 /************************************************************
1156 * make_argb_color
1158 * Recombines the output of get_relevant_argb_components and converts
1159 * it to the destination format.
1161 static DWORD make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in)
1163 UINT i;
1164 DWORD val = 0;
1166 for(i = 0;i < 4;i++) {
1167 if(info->process_channel[i]) {
1168 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1169 signed int shift;
1170 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1171 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1174 val |= info->channelmask; /* new channels are set to their maximal value */
1175 return val;
1178 static void format_to_vec4(const PixelFormatDesc *format, const DWORD *src, struct vec4 *dst)
1180 DWORD mask;
1182 if (format->bits[1])
1184 mask = (1 << format->bits[1]) - 1;
1185 dst->x = (float)((*src >> format->shift[1]) & mask) / mask;
1187 else
1188 dst->x = 1.0f;
1190 if (format->bits[2])
1192 mask = (1 << format->bits[2]) - 1;
1193 dst->y = (float)((*src >> format->shift[2]) & mask) / mask;
1195 else
1196 dst->y = 1.0f;
1198 if (format->bits[3])
1200 mask = (1 << format->bits[3]) - 1;
1201 dst->z = (float)((*src >> format->shift[3]) & mask) / mask;
1203 else
1204 dst->z = 1.0f;
1206 if (format->bits[0])
1208 mask = (1 << format->bits[0]) - 1;
1209 dst->w = (float)((*src >> format->shift[0]) & mask) / mask;
1211 else
1212 dst->w = 1.0f;
1215 static void format_from_vec4(const PixelFormatDesc *format, const struct vec4 *src, DWORD *dst)
1217 *dst = 0;
1219 if (format->bits[1])
1220 *dst |= (DWORD)(src->x * ((1 << format->bits[1]) - 1) + 0.5f) << format->shift[1];
1221 if (format->bits[2])
1222 *dst |= (DWORD)(src->y * ((1 << format->bits[2]) - 1) + 0.5f) << format->shift[2];
1223 if (format->bits[3])
1224 *dst |= (DWORD)(src->z * ((1 << format->bits[3]) - 1) + 0.5f) << format->shift[3];
1225 if (format->bits[0])
1226 *dst |= (DWORD)(src->w * ((1 << format->bits[0]) - 1) + 0.5f) << format->shift[0];
1229 /************************************************************
1230 * copy_simple_data
1232 * Copies the source buffer to the destination buffer, performing
1233 * any necessary format conversion and color keying.
1234 * Pixels outsize the source rect are blacked out.
1235 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1237 static void copy_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
1238 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
1240 struct argb_conversion_info conv_info, ck_conv_info;
1241 const PixelFormatDesc *ck_format = NULL;
1242 DWORD channels[4], pixel;
1243 UINT minwidth, minheight;
1244 UINT x, y;
1246 ZeroMemory(channels, sizeof(channels));
1247 init_argb_conversion_info(srcformat, destformat, &conv_info);
1249 minwidth = (src_size.cx < dst_size.cx) ? src_size.cx : dst_size.cx;
1250 minheight = (src_size.cy < dst_size.cy) ? src_size.cy : dst_size.cy;
1252 if (colorkey)
1254 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1255 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1256 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1259 for(y = 0;y < minheight;y++) {
1260 const BYTE *srcptr = src + y * srcpitch;
1261 BYTE *destptr = dest + y * destpitch;
1262 DWORD val;
1264 for(x = 0;x < minwidth;x++) {
1265 /* extract source color components */
1266 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1268 if (!srcformat->to_rgba && !destformat->from_rgba)
1270 get_relevant_argb_components(&conv_info, pixel, channels);
1271 val = make_argb_color(&conv_info, channels);
1273 if (colorkey)
1275 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1276 pixel = make_argb_color(&ck_conv_info, channels);
1277 if (pixel == colorkey)
1278 val &= ~conv_info.destmask[0];
1281 else
1283 struct vec4 color, tmp;
1285 format_to_vec4(srcformat, &pixel, &color);
1286 if (srcformat->to_rgba)
1287 srcformat->to_rgba(&color, &tmp);
1288 else
1289 tmp = color;
1291 if (ck_format)
1293 format_from_vec4(ck_format, &tmp, &pixel);
1294 if (pixel == colorkey)
1295 tmp.w = 0.0f;
1298 if (destformat->from_rgba)
1299 destformat->from_rgba(&tmp, &color);
1300 else
1301 color = tmp;
1303 format_from_vec4(destformat, &color, &val);
1306 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1307 srcptr += srcformat->bytes_per_pixel;
1308 destptr += destformat->bytes_per_pixel;
1311 if (src_size.cx < dst_size.cx) /* black out remaining pixels */
1312 memset(destptr, 0, destformat->bytes_per_pixel * (dst_size.cx - src_size.cx));
1314 if (src_size.cy < dst_size.cy) /* black out remaining pixels */
1315 memset(dest + src_size.cy * destpitch, 0, destpitch * (dst_size.cy - src_size.cy));
1318 /************************************************************
1319 * point_filter_simple_data
1321 * Copies the source buffer to the destination buffer, performing
1322 * any necessary format conversion, color keying and stretching
1323 * using a point filter.
1324 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1326 static void point_filter_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
1327 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
1329 struct argb_conversion_info conv_info, ck_conv_info;
1330 const PixelFormatDesc *ck_format = NULL;
1331 DWORD channels[4], pixel;
1333 UINT x, y;
1335 ZeroMemory(channels, sizeof(channels));
1336 init_argb_conversion_info(srcformat, destformat, &conv_info);
1338 if (colorkey)
1340 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1341 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1342 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1345 for (y = 0; y < dst_size.cy; ++y)
1347 BYTE *destptr = dest + y * destpitch;
1348 const BYTE *bufptr = src + srcpitch * (y * src_size.cy / dst_size.cy);
1350 for (x = 0; x < dst_size.cx; ++x)
1352 const BYTE *srcptr = bufptr + (x * src_size.cx / dst_size.cx) * srcformat->bytes_per_pixel;
1353 DWORD val;
1355 /* extract source color components */
1356 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1358 if (!srcformat->to_rgba && !destformat->from_rgba)
1360 get_relevant_argb_components(&conv_info, pixel, channels);
1361 val = make_argb_color(&conv_info, channels);
1363 if (colorkey)
1365 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1366 pixel = make_argb_color(&ck_conv_info, channels);
1367 if (pixel == colorkey)
1368 val &= ~conv_info.destmask[0];
1371 else
1373 struct vec4 color, tmp;
1375 format_to_vec4(srcformat, &pixel, &color);
1376 if (srcformat->to_rgba)
1377 srcformat->to_rgba(&color, &tmp);
1378 else
1379 tmp = color;
1381 if (ck_format)
1383 format_from_vec4(ck_format, &tmp, &pixel);
1384 if (pixel == colorkey)
1385 tmp.w = 0.0f;
1388 if (destformat->from_rgba)
1389 destformat->from_rgba(&tmp, &color);
1390 else
1391 color = tmp;
1393 format_from_vec4(destformat, &color, &val);
1396 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1397 destptr += destformat->bytes_per_pixel;
1402 /************************************************************
1403 * D3DXLoadSurfaceFromMemory
1405 * Loads data from a given memory chunk into a surface,
1406 * applying any of the specified filters.
1408 * PARAMS
1409 * pDestSurface [I] pointer to the surface
1410 * pDestPalette [I] palette to use
1411 * pDestRect [I] to be filled area of the surface
1412 * pSrcMemory [I] pointer to the source data
1413 * SrcFormat [I] format of the source pixel data
1414 * SrcPitch [I] number of bytes in a row
1415 * pSrcPalette [I] palette used in the source image
1416 * pSrcRect [I] area of the source data to load
1417 * dwFilter [I] filter to apply on stretching
1418 * Colorkey [I] colorkey
1420 * RETURNS
1421 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1422 * if pSrcMemory is NULL but the other parameters are valid
1423 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1424 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1425 * if DestRect is invalid
1426 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1427 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1429 * NOTES
1430 * pSrcRect specifies the dimensions of the source data;
1431 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1434 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1435 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1436 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1437 DWORD filter, D3DCOLOR color_key)
1439 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
1440 D3DSURFACE_DESC surfdesc;
1441 D3DLOCKED_RECT lockrect;
1442 SIZE src_size, dst_size;
1443 HRESULT hr;
1445 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1446 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1447 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1449 if (!dst_surface || !src_memory || !src_rect)
1450 return D3DERR_INVALIDCALL;
1451 if (src_format == D3DFMT_UNKNOWN
1452 || src_rect->left >= src_rect->right
1453 || src_rect->top >= src_rect->bottom)
1454 return E_FAIL;
1456 if (filter == D3DX_DEFAULT)
1457 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1459 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1461 src_size.cx = src_rect->right - src_rect->left;
1462 src_size.cy = src_rect->bottom - src_rect->top;
1463 if (!dst_rect)
1465 dst_size.cx = surfdesc.Width;
1466 dst_size.cy = surfdesc.Height;
1468 else
1470 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width)
1471 return D3DERR_INVALIDCALL;
1472 if (dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height)
1473 return D3DERR_INVALIDCALL;
1474 if (dst_rect->left < 0 || dst_rect->top < 0)
1475 return D3DERR_INVALIDCALL;
1476 dst_size.cx = dst_rect->right - dst_rect->left;
1477 dst_size.cy = dst_rect->bottom - dst_rect->top;
1478 if (!dst_size.cx || !dst_size.cy)
1479 return D3D_OK;
1482 srcformatdesc = get_format_info(src_format);
1483 if (srcformatdesc->type == FORMAT_UNKNOWN)
1484 return E_NOTIMPL;
1486 destformatdesc = get_format_info(surfdesc.Format);
1487 if (destformatdesc->type == FORMAT_UNKNOWN)
1488 return E_NOTIMPL;
1490 if (src_format == surfdesc.Format
1491 && dst_size.cx == src_size.cx
1492 && dst_size.cy == src_size.cy) /* Simple copy. */
1494 UINT row_block_count = ((src_size.cx + srcformatdesc->block_width - 1) / srcformatdesc->block_width);
1495 UINT row_count = (src_size.cy + srcformatdesc->block_height - 1) / srcformatdesc->block_height;
1496 const BYTE *src_addr;
1497 BYTE *dst_addr;
1498 UINT row;
1500 if (src_rect->left & (srcformatdesc->block_width - 1)
1501 || src_rect->top & (srcformatdesc->block_height - 1)
1502 || (src_rect->right & (srcformatdesc->block_width - 1)
1503 && src_size.cx != surfdesc.Width)
1504 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1505 && src_size.cy != surfdesc.Height))
1507 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1508 return D3DXERR_INVALIDDATA;
1511 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1512 return D3DXERR_INVALIDDATA;
1514 src_addr = src_memory;
1515 src_addr += (src_rect->top / srcformatdesc->block_height) * src_pitch;
1516 src_addr += (src_rect->left / srcformatdesc->block_width) * srcformatdesc->block_byte_count;
1517 dst_addr = lockrect.pBits;
1519 for (row = 0; row < row_count; ++row)
1521 memcpy(dst_addr, src_addr, row_block_count * srcformatdesc->block_byte_count);
1522 src_addr += src_pitch;
1523 dst_addr += lockrect.Pitch;
1526 IDirect3DSurface9_UnlockRect(dst_surface);
1528 else /* Stretching or format conversion. */
1530 if (srcformatdesc->bytes_per_pixel > 4)
1531 return E_NOTIMPL;
1532 if (destformatdesc->bytes_per_pixel > 4)
1533 return E_NOTIMPL;
1534 if (srcformatdesc->block_height != 1 || srcformatdesc->block_width != 1)
1535 return E_NOTIMPL;
1536 if (destformatdesc->block_height != 1 || destformatdesc->block_width != 1)
1537 return E_NOTIMPL;
1539 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1540 return D3DXERR_INVALIDDATA;
1542 if ((filter & 0xf) == D3DX_FILTER_NONE)
1544 copy_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1545 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1547 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1549 if ((filter & 0xf) != D3DX_FILTER_POINT)
1550 FIXME("Unhandled filter %#x.\n", filter);
1552 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1553 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1554 point_filter_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1555 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1558 IDirect3DSurface9_UnlockRect(dst_surface);
1561 return D3D_OK;
1564 /************************************************************
1565 * D3DXLoadSurfaceFromSurface
1567 * Copies the contents from one surface to another, performing any required
1568 * format conversion, resizing or filtering.
1570 * PARAMS
1571 * pDestSurface [I] pointer to the destination surface
1572 * pDestPalette [I] palette to use
1573 * pDestRect [I] to be filled area of the surface
1574 * pSrcSurface [I] pointer to the source surface
1575 * pSrcPalette [I] palette used for the source surface
1576 * pSrcRect [I] area of the source data to load
1577 * dwFilter [I] filter to apply on resizing
1578 * Colorkey [I] any ARGB value or 0 to disable color-keying
1580 * RETURNS
1581 * Success: D3D_OK
1582 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1583 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1586 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
1587 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
1588 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
1590 RECT rect;
1591 D3DLOCKED_RECT lock;
1592 D3DSURFACE_DESC SrcDesc;
1593 HRESULT hr;
1595 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1596 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1597 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
1598 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1600 if (!dst_surface || !src_surface)
1601 return D3DERR_INVALIDCALL;
1603 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
1605 if (!src_rect)
1606 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1607 else
1608 rect = *src_rect;
1610 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
1611 return D3DXERR_INVALIDDATA;
1613 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
1614 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
1616 IDirect3DSurface9_UnlockRect(src_surface);
1618 return hr;
1622 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1623 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1625 int len;
1626 WCHAR *filename;
1627 HRESULT hr;
1628 ID3DXBuffer *buffer;
1630 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1631 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1633 if (!dst_filename) return D3DERR_INVALIDCALL;
1635 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1636 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1637 if (!filename) return E_OUTOFMEMORY;
1638 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1640 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1641 if (SUCCEEDED(hr))
1643 hr = write_buffer_to_file(filename, buffer);
1644 ID3DXBuffer_Release(buffer);
1647 HeapFree(GetProcessHeap(), 0, filename);
1648 return hr;
1651 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1652 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1654 HRESULT hr;
1655 ID3DXBuffer *buffer;
1657 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1658 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1660 if (!dst_filename) return D3DERR_INVALIDCALL;
1662 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1663 if (SUCCEEDED(hr))
1665 hr = write_buffer_to_file(dst_filename, buffer);
1666 ID3DXBuffer_Release(buffer);
1669 return hr;
1672 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1673 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1675 IWICBitmapEncoder *encoder = NULL;
1676 IWICBitmapFrameEncode *frame = NULL;
1677 IPropertyBag2 *encoder_options = NULL;
1678 IStream *stream = NULL;
1679 HRESULT hr;
1680 HRESULT initresult;
1681 const CLSID *encoder_clsid;
1682 const GUID *pixel_format_guid;
1683 WICPixelFormatGUID wic_pixel_format;
1684 D3DFORMAT d3d_pixel_format;
1685 D3DSURFACE_DESC src_surface_desc;
1686 D3DLOCKED_RECT locked_rect;
1687 int width, height;
1688 STATSTG stream_stats;
1689 HGLOBAL stream_hglobal;
1690 ID3DXBuffer *buffer;
1691 DWORD size;
1693 TRACE("(%p, %#x, %p, %p, %s)\n",
1694 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1696 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
1698 if (src_palette)
1700 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1701 return D3DERR_INVALIDCALL;
1704 switch (file_format)
1706 case D3DXIFF_BMP:
1707 encoder_clsid = &CLSID_WICBmpEncoder;
1708 break;
1709 case D3DXIFF_PNG:
1710 encoder_clsid = &CLSID_WICPngEncoder;
1711 break;
1712 case D3DXIFF_JPG:
1713 encoder_clsid = &CLSID_WICJpegEncoder;
1714 break;
1715 case D3DXIFF_DDS:
1716 case D3DXIFF_DIB:
1717 case D3DXIFF_HDR:
1718 case D3DXIFF_PFM:
1719 case D3DXIFF_TGA:
1720 case D3DXIFF_PPM:
1721 FIXME("File format %#x is not supported yet\n", file_format);
1722 return E_NOTIMPL;
1723 default:
1724 return D3DERR_INVALIDCALL;
1727 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
1728 if (src_rect)
1730 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
1732 WARN("Invalid rectangle with 0 area\n");
1733 return D3DXCreateBuffer(64, dst_buffer);
1735 if (src_rect->left < 0 || src_rect->top < 0)
1736 return D3DERR_INVALIDCALL;
1737 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
1738 return D3DERR_INVALIDCALL;
1739 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
1740 return D3DERR_INVALIDCALL;
1742 width = src_rect->right - src_rect->left;
1743 height = src_rect->bottom - src_rect->top;
1745 else
1747 width = src_surface_desc.Width;
1748 height = src_surface_desc.Height;
1751 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1753 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
1754 &IID_IWICBitmapEncoder, (void **)&encoder);
1755 if (FAILED(hr)) goto cleanup_err;
1757 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
1758 if (FAILED(hr)) goto cleanup_err;
1760 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
1761 if (FAILED(hr)) goto cleanup_err;
1763 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
1764 if (FAILED(hr)) goto cleanup_err;
1766 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
1767 if (FAILED(hr)) goto cleanup_err;
1769 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
1770 if (FAILED(hr)) goto cleanup_err;
1772 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
1773 if (!pixel_format_guid)
1775 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
1776 hr = E_NOTIMPL;
1777 goto cleanup;
1780 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
1781 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
1782 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
1783 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
1785 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
1787 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
1789 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1790 if (SUCCEEDED(hr))
1792 IWICBitmapFrameEncode_WritePixels(frame, height,
1793 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
1794 IDirect3DSurface9_UnlockRect(src_surface);
1797 else /* Pixel format conversion */
1799 const PixelFormatDesc *src_format_desc, *dst_format_desc;
1800 SIZE size;
1801 DWORD dst_pitch;
1802 void *dst_data;
1804 src_format_desc = get_format_info(src_surface_desc.Format);
1805 dst_format_desc = get_format_info(d3d_pixel_format);
1806 if (src_format_desc->format == D3DFMT_UNKNOWN || dst_format_desc->format == D3DFMT_UNKNOWN)
1808 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
1809 src_surface_desc.Format, d3d_pixel_format);
1810 hr = E_NOTIMPL;
1811 goto cleanup;
1814 if (src_format_desc->bytes_per_pixel > 4
1815 || dst_format_desc->bytes_per_pixel > 4
1816 || src_format_desc->block_height != 1 || src_format_desc->block_width != 1
1817 || dst_format_desc->block_height != 1 || dst_format_desc->block_width != 1)
1819 hr = E_NOTIMPL;
1820 goto cleanup;
1823 size.cx = width;
1824 size.cy = height;
1825 dst_pitch = width * dst_format_desc->bytes_per_pixel;
1826 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
1827 if (!dst_data)
1829 hr = E_OUTOFMEMORY;
1830 goto cleanup;
1833 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1834 if (SUCCEEDED(hr))
1836 copy_simple_data(locked_rect.pBits, locked_rect.Pitch, size, src_format_desc,
1837 dst_data, dst_pitch, size, dst_format_desc, 0);
1838 IDirect3DSurface9_UnlockRect(src_surface);
1841 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
1842 HeapFree(GetProcessHeap(), 0, dst_data);
1845 hr = IWICBitmapFrameEncode_Commit(frame);
1846 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
1848 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
1850 /* copy data from stream to ID3DXBuffer */
1851 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
1852 if (FAILED(hr)) goto cleanup_err;
1854 if (stream_stats.cbSize.u.HighPart != 0)
1856 hr = D3DXERR_INVALIDDATA;
1857 goto cleanup;
1859 size = stream_stats.cbSize.u.LowPart;
1861 hr = D3DXCreateBuffer(size, &buffer);
1862 if (FAILED(hr)) goto cleanup;
1864 hr = GetHGlobalFromStream(stream, &stream_hglobal);
1865 if (SUCCEEDED(hr))
1867 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
1868 void *stream_data = GlobalLock(stream_hglobal);
1869 memcpy(buffer_pointer, stream_data, size);
1870 GlobalUnlock(stream_hglobal);
1871 *dst_buffer = buffer;
1873 else ID3DXBuffer_Release(buffer);
1875 cleanup_err:
1876 if (FAILED(hr) && hr != E_OUTOFMEMORY)
1877 hr = D3DERR_INVALIDCALL;
1879 cleanup:
1880 if (stream) IStream_Release(stream);
1882 if (frame) IWICBitmapFrameEncode_Release(frame);
1883 if (encoder_options) IPropertyBag2_Release(encoder_options);
1885 if (encoder) IWICBitmapEncoder_Release(encoder);
1887 if (SUCCEEDED(initresult)) CoUninitialize();
1889 return hr;