TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / d3dx9_36 / surface.c
blobb14f5e785e6ab9eed159f3d7ebe4c546ba571ce3
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_P8 },
41 { &GUID_WICPixelFormat1bppIndexed, D3DFMT_P8 },
42 { &GUID_WICPixelFormat4bppIndexed, D3DFMT_P8 },
43 { &GUID_WICPixelFormat8bppGray, D3DFMT_L8 },
44 { &GUID_WICPixelFormat16bppBGR555, D3DFMT_X1R5G5B5 },
45 { &GUID_WICPixelFormat16bppBGR565, D3DFMT_R5G6B5 },
46 { &GUID_WICPixelFormat24bppBGR, D3DFMT_R8G8B8 },
47 { &GUID_WICPixelFormat32bppBGR, D3DFMT_X8R8G8B8 },
48 { &GUID_WICPixelFormat32bppBGRA, D3DFMT_A8R8G8B8 }
51 static D3DFORMAT wic_guid_to_d3dformat(const GUID *guid)
53 unsigned int i;
55 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
57 if (IsEqualGUID(wic_pixel_formats[i].wic_guid, guid))
58 return wic_pixel_formats[i].d3dformat;
61 return D3DFMT_UNKNOWN;
64 static const GUID *d3dformat_to_wic_guid(D3DFORMAT format)
66 unsigned int i;
68 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
70 if (wic_pixel_formats[i].d3dformat == format)
71 return wic_pixel_formats[i].wic_guid;
74 return NULL;
77 /* dds_header.flags */
78 #define DDS_CAPS 0x1
79 #define DDS_HEIGHT 0x2
80 #define DDS_WIDTH 0x4
81 #define DDS_PITCH 0x8
82 #define DDS_PIXELFORMAT 0x1000
83 #define DDS_MIPMAPCOUNT 0x20000
84 #define DDS_LINEARSIZE 0x80000
85 #define DDS_DEPTH 0x800000
87 /* dds_header.caps */
88 #define DDS_CAPS_COMPLEX 0x8
89 #define DDS_CAPS_TEXTURE 0x1000
90 #define DDS_CAPS_MIPMAP 0x400000
92 /* dds_header.caps2 */
93 #define DDS_CAPS2_CUBEMAP 0x200
94 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
95 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
96 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
97 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
98 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
99 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
100 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
101 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
102 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
103 #define DDS_CAPS2_VOLUME 0x200000
105 /* dds_pixel_format.flags */
106 #define DDS_PF_ALPHA 0x1
107 #define DDS_PF_ALPHA_ONLY 0x2
108 #define DDS_PF_FOURCC 0x4
109 #define DDS_PF_RGB 0x40
110 #define DDS_PF_YUV 0x200
111 #define DDS_PF_LUMINANCE 0x20000
112 #define DDS_PF_BUMPDUDV 0x80000
114 struct dds_pixel_format
116 DWORD size;
117 DWORD flags;
118 DWORD fourcc;
119 DWORD bpp;
120 DWORD rmask;
121 DWORD gmask;
122 DWORD bmask;
123 DWORD amask;
126 struct dds_header
128 DWORD signature;
129 DWORD size;
130 DWORD flags;
131 DWORD height;
132 DWORD width;
133 DWORD pitch_or_linear_size;
134 DWORD depth;
135 DWORD miplevels;
136 DWORD reserved[11];
137 struct dds_pixel_format pixel_format;
138 DWORD caps;
139 DWORD caps2;
140 DWORD caps3;
141 DWORD caps4;
142 DWORD reserved2;
145 static D3DFORMAT dds_fourcc_to_d3dformat(DWORD fourcc)
147 unsigned int i;
148 static const DWORD known_fourcc[] = {
149 D3DFMT_UYVY,
150 D3DFMT_YUY2,
151 D3DFMT_R8G8_B8G8,
152 D3DFMT_G8R8_G8B8,
153 D3DFMT_DXT1,
154 D3DFMT_DXT2,
155 D3DFMT_DXT3,
156 D3DFMT_DXT4,
157 D3DFMT_DXT5,
158 D3DFMT_R16F,
159 D3DFMT_G16R16F,
160 D3DFMT_A16B16G16R16F,
161 D3DFMT_R32F,
162 D3DFMT_G32R32F,
163 D3DFMT_A32B32G32R32F,
166 for (i = 0; i < sizeof(known_fourcc) / sizeof(known_fourcc[0]); i++)
168 if (known_fourcc[i] == fourcc)
169 return fourcc;
172 WARN("Unknown FourCC %#x\n", fourcc);
173 return D3DFMT_UNKNOWN;
176 static const struct {
177 DWORD bpp;
178 DWORD rmask;
179 DWORD gmask;
180 DWORD bmask;
181 DWORD amask;
182 D3DFORMAT format;
183 } rgb_pixel_formats[] = {
184 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 },
185 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 },
186 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 },
187 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 },
188 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 },
189 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 },
190 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 },
191 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 },
192 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 },
193 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 },
194 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 },
195 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 },
196 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 },
197 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8 },
198 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8 },
201 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
203 unsigned int i;
205 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
207 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
208 && rgb_pixel_formats[i].rmask == pixel_format->rmask
209 && rgb_pixel_formats[i].gmask == pixel_format->gmask
210 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
212 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
213 return rgb_pixel_formats[i].format;
214 if (rgb_pixel_formats[i].amask == 0)
215 return rgb_pixel_formats[i].format;
219 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
220 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
221 return D3DFMT_UNKNOWN;
224 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
226 if (pixel_format->bpp == 8)
228 if (pixel_format->rmask == 0xff)
229 return D3DFMT_L8;
230 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
231 return D3DFMT_A4L4;
233 if (pixel_format->bpp == 16)
235 if (pixel_format->rmask == 0xffff)
236 return D3DFMT_L16;
237 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
238 return D3DFMT_A8L8;
241 WARN("Unknown luminance pixel format (bpp %u, l %#x, a %#x)\n",
242 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
243 return D3DFMT_UNKNOWN;
246 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
248 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
249 return D3DFMT_A8;
251 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format->bpp, pixel_format->rmask);
252 return D3DFMT_UNKNOWN;
255 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
257 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00)
258 return D3DFMT_V8U8;
259 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000)
260 return D3DFMT_V16U16;
262 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format->bpp,
263 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
264 return D3DFMT_UNKNOWN;
267 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
269 TRACE("pixel_format: size %u, flags %#x, fourcc %#x, bpp %u.\n", pixel_format->size,
270 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp);
271 TRACE("rmask %#x, gmask %#x, bmask %#x, amask %#x.\n", pixel_format->rmask, pixel_format->gmask,
272 pixel_format->bmask, pixel_format->amask);
274 if (pixel_format->flags & DDS_PF_FOURCC)
275 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
276 if (pixel_format->flags & DDS_PF_RGB)
277 return dds_rgb_to_d3dformat(pixel_format);
278 if (pixel_format->flags & DDS_PF_LUMINANCE)
279 return dds_luminance_to_d3dformat(pixel_format);
280 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
281 return dds_alpha_to_d3dformat(pixel_format);
282 if (pixel_format->flags & DDS_PF_BUMPDUDV)
283 return dds_bump_to_d3dformat(pixel_format);
285 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n",
286 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
287 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
288 return D3DFMT_UNKNOWN;
291 static HRESULT d3dformat_to_dds_pixel_format(struct dds_pixel_format *pixel_format, D3DFORMAT d3dformat)
293 unsigned int i;
295 memset(pixel_format, 0, sizeof(*pixel_format));
297 pixel_format->size = sizeof(*pixel_format);
299 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
301 if (rgb_pixel_formats[i].format == d3dformat)
303 pixel_format->flags |= DDS_PF_RGB;
304 pixel_format->bpp = rgb_pixel_formats[i].bpp;
305 pixel_format->rmask = rgb_pixel_formats[i].rmask;
306 pixel_format->gmask = rgb_pixel_formats[i].gmask;
307 pixel_format->bmask = rgb_pixel_formats[i].bmask;
308 pixel_format->amask = rgb_pixel_formats[i].amask;
309 if (pixel_format->amask) pixel_format->flags |= DDS_PF_ALPHA;
310 return D3D_OK;
314 WARN("Unknown pixel format %#x\n", d3dformat);
315 return E_NOTIMPL;
318 static HRESULT calculate_dds_surface_size(D3DFORMAT format, UINT width, UINT height,
319 UINT *pitch, UINT *size)
321 const struct pixel_format_desc *format_desc = get_format_info(format);
322 if (format_desc->type == FORMAT_UNKNOWN)
323 return E_NOTIMPL;
325 if (format_desc->block_width != 1 || format_desc->block_height != 1)
327 *pitch = format_desc->block_byte_count
328 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
329 *size = *pitch
330 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
332 else
334 *pitch = width * format_desc->bytes_per_pixel;
335 *size = *pitch * height;
338 return D3D_OK;
341 static UINT calculate_dds_file_size(D3DFORMAT format, UINT width, UINT height, UINT depth,
342 UINT miplevels, UINT faces)
344 UINT i, file_size = 0;
346 for (i = 0; i < miplevels; i++)
348 UINT pitch, size = 0;
349 calculate_dds_surface_size(format, width, height, &pitch, &size);
350 size *= depth;
351 file_size += size;
352 width = max(1, width / 2);
353 height = max(1, height / 2);
354 depth = max(1, depth / 2);
357 file_size *= faces;
358 file_size += sizeof(struct dds_header);
359 return file_size;
362 /************************************************************
363 * get_image_info_from_dds
365 * Fills a D3DXIMAGE_INFO structure with information
366 * about a DDS file stored in the memory.
368 * PARAMS
369 * buffer [I] pointer to DDS data
370 * length [I] size of DDS data
371 * info [O] pointer to D3DXIMAGE_INFO structure
373 * RETURNS
374 * Success: D3D_OK
375 * Failure: D3DXERR_INVALIDDATA
378 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
380 UINT faces = 1;
381 UINT expected_length;
382 const struct dds_header *header = buffer;
384 if (length < sizeof(*header) || !info)
385 return D3DXERR_INVALIDDATA;
387 if (header->pixel_format.size != sizeof(header->pixel_format))
388 return D3DXERR_INVALIDDATA;
390 info->Width = header->width;
391 info->Height = header->height;
392 info->Depth = 1;
393 info->MipLevels = header->miplevels ? header->miplevels : 1;
395 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
396 if (info->Format == D3DFMT_UNKNOWN)
397 return D3DXERR_INVALIDDATA;
399 TRACE("Pixel format is %#x\n", info->Format);
401 if (header->caps2 & DDS_CAPS2_VOLUME)
403 info->Depth = header->depth;
404 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
406 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
408 DWORD face;
409 faces = 0;
410 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
412 if (header->caps2 & face)
413 faces++;
415 info->ResourceType = D3DRTYPE_CUBETEXTURE;
417 else
419 info->ResourceType = D3DRTYPE_TEXTURE;
422 expected_length = calculate_dds_file_size(info->Format, info->Width, info->Height, info->Depth,
423 info->MipLevels, faces);
424 if (length < expected_length)
426 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
427 return D3DXERR_INVALIDDATA;
430 info->ImageFileFormat = D3DXIFF_DDS;
431 return D3D_OK;
434 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
435 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
436 const D3DXIMAGE_INFO *src_info)
438 UINT size;
439 UINT src_pitch;
440 const struct dds_header *header = src_data;
441 const BYTE *pixels = (BYTE *)(header + 1);
443 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
444 return D3DXERR_INVALIDDATA;
446 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &src_pitch, &size)))
447 return E_NOTIMPL;
449 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
450 src_pitch, NULL, src_rect, filter, color_key);
453 static HRESULT save_dds_surface_to_memory(ID3DXBuffer **dst_buffer, IDirect3DSurface9 *src_surface, const RECT *src_rect)
455 HRESULT hr;
456 UINT dst_pitch, surface_size, file_size;
457 D3DSURFACE_DESC src_desc;
458 D3DLOCKED_RECT locked_rect;
459 ID3DXBuffer *buffer;
460 struct dds_header *header;
461 BYTE *pixels;
462 struct volume volume;
463 const struct pixel_format_desc *pixel_format;
465 if (src_rect)
467 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
468 return E_NOTIMPL;
471 hr = IDirect3DSurface9_GetDesc(src_surface, &src_desc);
472 if (FAILED(hr)) return hr;
474 pixel_format = get_format_info(src_desc.Format);
475 if (pixel_format->type == FORMAT_UNKNOWN) return E_NOTIMPL;
477 file_size = calculate_dds_file_size(src_desc.Format, src_desc.Width, src_desc.Height, 1, 1, 1);
479 hr = calculate_dds_surface_size(src_desc.Format, src_desc.Width, src_desc.Height, &dst_pitch, &surface_size);
480 if (FAILED(hr)) return hr;
482 hr = D3DXCreateBuffer(file_size, &buffer);
483 if (FAILED(hr)) return hr;
485 header = ID3DXBuffer_GetBufferPointer(buffer);
486 pixels = (BYTE *)(header + 1);
488 memset(header, 0, sizeof(*header));
489 header->signature = MAKEFOURCC('D','D','S',' ');
490 header->size = sizeof(*header);
491 header->flags = DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PITCH | DDS_PIXELFORMAT | DDS_MIPMAPCOUNT;
492 header->height = src_desc.Height;
493 header->width = src_desc.Width;
494 header->pitch_or_linear_size = dst_pitch;
495 header->depth = 1;
496 header->miplevels = 1;
497 header->caps = DDS_CAPS_TEXTURE;
498 hr = d3dformat_to_dds_pixel_format(&header->pixel_format, src_desc.Format);
499 if (FAILED(hr))
501 ID3DXBuffer_Release(buffer);
502 return hr;
505 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, NULL, D3DLOCK_READONLY);
506 if (FAILED(hr))
508 ID3DXBuffer_Release(buffer);
509 return hr;
512 volume.width = src_desc.Width;
513 volume.height = src_desc.Height;
514 volume.depth = 1;
515 copy_pixels(locked_rect.pBits, locked_rect.Pitch, 0, pixels, dst_pitch, 0,
516 &volume, pixel_format);
518 IDirect3DSurface9_UnlockRect(src_surface);
520 *dst_buffer = buffer;
521 return D3D_OK;
524 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette,
525 const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key,
526 const D3DXIMAGE_INFO *src_info)
528 UINT row_pitch, slice_pitch;
529 const struct dds_header *header = src_data;
530 const BYTE *pixels = (BYTE *)(header + 1);
532 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
533 return D3DXERR_INVALIDDATA;
535 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &row_pitch, &slice_pitch)))
536 return E_NOTIMPL;
538 return D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box, pixels, src_info->Format,
539 row_pitch, slice_pitch, NULL, src_box, filter, color_key);
542 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
543 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info, unsigned int skip_levels,
544 unsigned int *loaded_miplevels)
546 HRESULT hr;
547 RECT src_rect;
548 UINT src_pitch;
549 UINT mip_level;
550 UINT mip_levels;
551 UINT mip_level_size;
552 UINT width, height;
553 IDirect3DSurface9 *surface;
554 const struct dds_header *header = src_data;
555 const BYTE *pixels = (BYTE *)(header + 1);
557 /* Loading a cube texture as a simple texture is also supported
558 * (only first face texture is taken). Same with volume textures. */
559 if ((src_info->ResourceType != D3DRTYPE_TEXTURE)
560 && (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
561 && (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE))
563 WARN("Trying to load a %u resource as a 2D texture, returning failure.\n", src_info->ResourceType);
564 return D3DXERR_INVALIDDATA;
567 width = src_info->Width;
568 height = src_info->Height;
569 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
570 if (src_info->ResourceType == D3DRTYPE_VOLUMETEXTURE)
571 mip_levels = 1;
572 for (mip_level = 0; mip_level < mip_levels + skip_levels; ++mip_level)
574 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_pitch, &mip_level_size);
575 if (FAILED(hr)) return hr;
577 if (mip_level >= skip_levels)
579 SetRect(&src_rect, 0, 0, width, height);
581 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level - skip_levels, &surface);
582 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
583 NULL, &src_rect, filter, color_key);
584 IDirect3DSurface9_Release(surface);
585 if (FAILED(hr))
586 return hr;
589 pixels += mip_level_size;
590 width = max(1, width / 2);
591 height = max(1, height / 2);
594 *loaded_miplevels = mip_levels - skip_levels;
596 return D3D_OK;
599 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
600 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
602 HRESULT hr;
603 int face;
604 UINT mip_level;
605 UINT size;
606 RECT src_rect;
607 UINT src_pitch;
608 UINT mip_levels;
609 UINT mip_level_size;
610 IDirect3DSurface9 *surface;
611 const struct dds_header *header = src_data;
612 const BYTE *pixels = (BYTE *)(header + 1);
614 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
615 return D3DXERR_INVALIDDATA;
617 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
619 WARN("Only full cubemaps are supported\n");
620 return D3DXERR_INVALIDDATA;
623 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
624 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
626 size = src_info->Width;
627 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
629 hr = calculate_dds_surface_size(src_info->Format, size, size, &src_pitch, &mip_level_size);
630 if (FAILED(hr)) return hr;
632 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
633 if (mip_level < mip_levels)
635 SetRect(&src_rect, 0, 0, size, size);
637 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
638 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
639 NULL, &src_rect, filter, color_key);
640 IDirect3DSurface9_Release(surface);
641 if (FAILED(hr)) return hr;
644 pixels += mip_level_size;
645 size = max(1, size / 2);
649 return D3D_OK;
652 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
653 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
655 HRESULT hr;
656 UINT mip_level;
657 UINT mip_levels;
658 UINT src_slice_pitch;
659 UINT src_row_pitch;
660 D3DBOX src_box;
661 UINT width, height, depth;
662 IDirect3DVolume9 *volume;
663 const struct dds_header *header = src_data;
664 const BYTE *pixels = (BYTE *)(header + 1);
666 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
667 return D3DXERR_INVALIDDATA;
669 width = src_info->Width;
670 height = src_info->Height;
671 depth = src_info->Depth;
672 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture));
674 for (mip_level = 0; mip_level < mip_levels; mip_level++)
676 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_row_pitch, &src_slice_pitch);
677 if (FAILED(hr)) return hr;
679 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume);
680 if (FAILED(hr)) return hr;
682 src_box.Left = 0;
683 src_box.Top = 0;
684 src_box.Right = width;
685 src_box.Bottom = height;
686 src_box.Front = 0;
687 src_box.Back = depth;
689 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format,
690 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key);
692 IDirect3DVolume9_Release(volume);
693 if (FAILED(hr)) return hr;
695 pixels += depth * src_slice_pitch;
696 width = max(1, width / 2);
697 height = max(1, height / 2);
698 depth = max(1, depth / 2);
701 return D3D_OK;
704 static BOOL convert_dib_to_bmp(void **data, UINT *size)
706 ULONG header_size;
707 ULONG count = 0;
708 ULONG offset;
709 BITMAPFILEHEADER *header;
710 BYTE *new_data;
711 UINT new_size;
713 if ((*size < 4) || (*size < (header_size = *(ULONG*)*data)))
714 return FALSE;
716 if ((header_size == sizeof(BITMAPINFOHEADER)) ||
717 (header_size == sizeof(BITMAPV4HEADER)) ||
718 (header_size == sizeof(BITMAPV5HEADER)) ||
719 (header_size == 64 /* sizeof(BITMAPCOREHEADER2) */))
721 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
722 BITMAPINFOHEADER *info_header = (BITMAPINFOHEADER*)*data;
723 count = info_header->biClrUsed;
725 if (!count && info_header->biBitCount <= 8)
726 count = 1 << info_header->biBitCount;
728 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBQUAD) * count;
730 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
731 if ((info_header->biSize == sizeof(BITMAPINFOHEADER)) && (info_header->biCompression == BI_BITFIELDS))
732 offset += 3 * sizeof(DWORD);
734 else if (header_size == sizeof(BITMAPCOREHEADER))
736 BITMAPCOREHEADER *core_header = (BITMAPCOREHEADER*)*data;
738 if (core_header->bcBitCount <= 8)
739 count = 1 << core_header->bcBitCount;
741 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBTRIPLE) * count;
743 else
745 return FALSE;
748 TRACE("Converting DIB file to BMP\n");
750 new_size = *size + sizeof(BITMAPFILEHEADER);
751 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
752 CopyMemory(new_data + sizeof(BITMAPFILEHEADER), *data, *size);
754 /* Add BMP header */
755 header = (BITMAPFILEHEADER*)new_data;
756 header->bfType = 0x4d42; /* BM */
757 header->bfSize = new_size;
758 header->bfReserved1 = 0;
759 header->bfReserved2 = 0;
760 header->bfOffBits = offset;
762 /* Update input data */
763 *data = new_data;
764 *size = new_size;
766 return TRUE;
769 /************************************************************
770 * D3DXGetImageInfoFromFileInMemory
772 * Fills a D3DXIMAGE_INFO structure with info about an image
774 * PARAMS
775 * data [I] pointer to the image file data
776 * datasize [I] size of the passed data
777 * info [O] pointer to the destination structure
779 * RETURNS
780 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
781 * if info is NULL and data and datasize are not NULL
782 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
783 * D3DERR_INVALIDCALL, if data is NULL or
784 * if datasize is 0
786 * NOTES
787 * datasize may be bigger than the actual file size
790 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(const void *data, UINT datasize, D3DXIMAGE_INFO *info)
792 IWICImagingFactory *factory;
793 IWICBitmapDecoder *decoder = NULL;
794 IWICStream *stream;
795 HRESULT hr;
796 HRESULT initresult;
797 BOOL dib;
799 TRACE("(%p, %d, %p)\n", data, datasize, info);
801 if (!data || !datasize)
802 return D3DERR_INVALIDCALL;
804 if (!info)
805 return D3D_OK;
807 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
808 TRACE("File type is DDS\n");
809 return get_image_info_from_dds(data, datasize, info);
812 /* In case of DIB file, convert it to BMP */
813 dib = convert_dib_to_bmp((void**)&data, &datasize);
815 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
817 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
819 if (SUCCEEDED(hr)) {
820 IWICImagingFactory_CreateStream(factory, &stream);
821 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
822 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
823 IWICStream_Release(stream);
824 IWICImagingFactory_Release(factory);
827 if (FAILED(hr)) {
828 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
829 FIXME("File type PPM is not supported yet\n");
830 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
831 FIXME("File type HDR is not supported yet\n");
832 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
833 FIXME("File type PFM is not supported yet\n");
836 if (SUCCEEDED(hr)) {
837 GUID container_format;
838 UINT frame_count;
840 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
841 if (SUCCEEDED(hr)) {
842 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
843 if (dib) {
844 TRACE("File type is DIB\n");
845 info->ImageFileFormat = D3DXIFF_DIB;
846 } else {
847 TRACE("File type is BMP\n");
848 info->ImageFileFormat = D3DXIFF_BMP;
850 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
851 TRACE("File type is PNG\n");
852 info->ImageFileFormat = D3DXIFF_PNG;
853 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
854 TRACE("File type is JPG\n");
855 info->ImageFileFormat = D3DXIFF_JPG;
856 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
857 TRACE("File type is TGA\n");
858 info->ImageFileFormat = D3DXIFF_TGA;
859 } else {
860 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
861 hr = D3DXERR_INVALIDDATA;
865 if (SUCCEEDED(hr))
866 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
867 if (SUCCEEDED(hr) && !frame_count)
868 hr = D3DXERR_INVALIDDATA;
870 if (SUCCEEDED(hr)) {
871 IWICBitmapFrameDecode *frame = NULL;
873 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
875 if (SUCCEEDED(hr))
876 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
878 if (SUCCEEDED(hr)) {
879 WICPixelFormatGUID pixel_format;
881 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
882 if (SUCCEEDED(hr)) {
883 info->Format = wic_guid_to_d3dformat(&pixel_format);
884 if (info->Format == D3DFMT_UNKNOWN) {
885 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
886 hr = D3DXERR_INVALIDDATA;
891 if (frame)
892 IWICBitmapFrameDecode_Release(frame);
894 info->Depth = 1;
895 info->MipLevels = 1;
896 info->ResourceType = D3DRTYPE_TEXTURE;
900 if (decoder)
901 IWICBitmapDecoder_Release(decoder);
903 if (SUCCEEDED(initresult))
904 CoUninitialize();
906 if (dib)
907 HeapFree(GetProcessHeap(), 0, (void*)data);
909 if (FAILED(hr)) {
910 TRACE("Invalid or unsupported image file\n");
911 return D3DXERR_INVALIDDATA;
914 return D3D_OK;
917 /************************************************************
918 * D3DXGetImageInfoFromFile
920 * RETURNS
921 * Success: D3D_OK, if we successfully load a valid image file or
922 * if we successfully load a file which is no valid image and info is NULL
923 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
924 * if file is not a valid image file and info is not NULL
925 * D3DERR_INVALIDCALL, if file is NULL
928 HRESULT WINAPI D3DXGetImageInfoFromFileA(const char *file, D3DXIMAGE_INFO *info)
930 WCHAR *widename;
931 HRESULT hr;
932 int strlength;
934 TRACE("file %s, info %p.\n", debugstr_a(file), info);
936 if( !file ) return D3DERR_INVALIDCALL;
938 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
939 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
940 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
942 hr = D3DXGetImageInfoFromFileW(widename, info);
943 HeapFree(GetProcessHeap(), 0, widename);
945 return hr;
948 HRESULT WINAPI D3DXGetImageInfoFromFileW(const WCHAR *file, D3DXIMAGE_INFO *info)
950 void *buffer;
951 HRESULT hr;
952 DWORD size;
954 TRACE("file %s, info %p.\n", debugstr_w(file), info);
956 if (!file)
957 return D3DERR_INVALIDCALL;
959 if (FAILED(map_view_of_file(file, &buffer, &size)))
960 return D3DXERR_INVALIDDATA;
962 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
963 UnmapViewOfFile(buffer);
965 return hr;
968 /************************************************************
969 * D3DXGetImageInfoFromResource
971 * RETURNS
972 * Success: D3D_OK, if resource is a valid image file
973 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
974 * if we fail to load resource
977 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, const char *resource, D3DXIMAGE_INFO *info)
979 HRSRC resinfo;
980 void *buffer;
981 DWORD size;
983 TRACE("module %p, resource %s, info %p.\n", module, debugstr_a(resource), info);
985 if (!(resinfo = FindResourceA(module, resource, (const char *)RT_RCDATA))
986 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
987 && !(resinfo = FindResourceA(module, resource, (const char *)RT_BITMAP)))
988 return D3DXERR_INVALIDDATA;
990 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
991 return D3DXERR_INVALIDDATA;
993 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
996 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, const WCHAR *resource, D3DXIMAGE_INFO *info)
998 HRSRC resinfo;
999 void *buffer;
1000 DWORD size;
1002 TRACE("module %p, resource %s, info %p.\n", module, debugstr_w(resource), info);
1004 if (!(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA))
1005 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1006 && !(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_BITMAP)))
1007 return D3DXERR_INVALIDDATA;
1009 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
1010 return D3DXERR_INVALIDDATA;
1012 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
1015 /************************************************************
1016 * D3DXLoadSurfaceFromFileInMemory
1018 * Loads data from a given buffer into a surface and fills a given
1019 * D3DXIMAGE_INFO structure with info about the source data.
1021 * PARAMS
1022 * pDestSurface [I] pointer to the surface
1023 * pDestPalette [I] palette to use
1024 * pDestRect [I] to be filled area of the surface
1025 * pSrcData [I] pointer to the source data
1026 * SrcDataSize [I] size of the source data in bytes
1027 * pSrcRect [I] area of the source data to load
1028 * dwFilter [I] filter to apply on stretching
1029 * Colorkey [I] colorkey
1030 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1032 * RETURNS
1033 * Success: D3D_OK
1034 * Failure: D3DERR_INVALIDCALL, if pDestSurface, pSrcData or SrcDataSize is NULL
1035 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1038 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
1039 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
1040 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
1042 D3DXIMAGE_INFO imginfo;
1043 HRESULT hr, com_init;
1045 IWICImagingFactory *factory = NULL;
1046 IWICBitmapDecoder *decoder;
1047 IWICBitmapFrameDecode *bitmapframe;
1048 IWICStream *stream;
1050 const struct pixel_format_desc *formatdesc;
1051 WICRect wicrect;
1052 RECT rect;
1054 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1055 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1056 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
1057 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
1059 if (!pDestSurface || !pSrcData || !SrcDataSize)
1060 return D3DERR_INVALIDCALL;
1062 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
1064 if (FAILED(hr))
1065 return hr;
1067 if (pSrcRect)
1069 wicrect.X = pSrcRect->left;
1070 wicrect.Y = pSrcRect->top;
1071 wicrect.Width = pSrcRect->right - pSrcRect->left;
1072 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
1074 else
1076 wicrect.X = 0;
1077 wicrect.Y = 0;
1078 wicrect.Width = imginfo.Width;
1079 wicrect.Height = imginfo.Height;
1082 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
1084 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
1086 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
1087 dwFilter, Colorkey, &imginfo);
1088 if (SUCCEEDED(hr) && pSrcInfo)
1089 *pSrcInfo = imginfo;
1090 return hr;
1093 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1094 convert_dib_to_bmp((void**)&pSrcData, &SrcDataSize);
1096 com_init = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1098 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
1099 goto cleanup_err;
1101 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
1103 IWICImagingFactory_Release(factory);
1104 factory = NULL;
1105 goto cleanup_err;
1108 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
1110 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
1112 IWICStream_Release(stream);
1114 if (FAILED(hr))
1115 goto cleanup_err;
1117 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
1119 if (FAILED(hr))
1120 goto cleanup_bmp;
1122 formatdesc = get_format_info(imginfo.Format);
1124 if (formatdesc->type == FORMAT_UNKNOWN)
1126 FIXME("Unsupported pixel format\n");
1127 hr = D3DXERR_INVALIDDATA;
1129 else
1131 BYTE *buffer;
1132 DWORD pitch;
1133 PALETTEENTRY *palette = NULL;
1134 WICColor *colors = NULL;
1136 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
1137 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
1139 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
1140 pitch * wicrect.Height, buffer);
1142 if (SUCCEEDED(hr) && (formatdesc->type == FORMAT_INDEX))
1144 IWICPalette *wic_palette = NULL;
1145 UINT nb_colors;
1147 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
1148 if (SUCCEEDED(hr))
1149 hr = IWICBitmapFrameDecode_CopyPalette(bitmapframe, wic_palette);
1150 if (SUCCEEDED(hr))
1151 hr = IWICPalette_GetColorCount(wic_palette, &nb_colors);
1152 if (SUCCEEDED(hr))
1154 colors = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(colors[0]));
1155 palette = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(palette[0]));
1156 if (!colors || !palette)
1157 hr = E_OUTOFMEMORY;
1159 if (SUCCEEDED(hr))
1160 hr = IWICPalette_GetColors(wic_palette, nb_colors, colors, &nb_colors);
1161 if (SUCCEEDED(hr))
1163 UINT i;
1165 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1166 for (i = 0; i < nb_colors; i++)
1168 palette[i].peRed = (colors[i] >> 16) & 0xff;
1169 palette[i].peGreen = (colors[i] >> 8) & 0xff;
1170 palette[i].peBlue = colors[i] & 0xff;
1171 palette[i].peFlags = (colors[i] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1174 if (wic_palette)
1175 IWICPalette_Release(wic_palette);
1178 if (SUCCEEDED(hr))
1180 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1181 buffer, imginfo.Format, pitch,
1182 palette, &rect, dwFilter, Colorkey);
1185 HeapFree(GetProcessHeap(), 0, colors);
1186 HeapFree(GetProcessHeap(), 0, palette);
1187 HeapFree(GetProcessHeap(), 0, buffer);
1190 IWICBitmapFrameDecode_Release(bitmapframe);
1192 cleanup_bmp:
1193 IWICBitmapDecoder_Release(decoder);
1195 cleanup_err:
1196 if (factory)
1197 IWICImagingFactory_Release(factory);
1199 if (SUCCEEDED(com_init))
1200 CoUninitialize();
1202 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1203 HeapFree(GetProcessHeap(), 0, (void*)pSrcData);
1205 if (FAILED(hr))
1206 return D3DXERR_INVALIDDATA;
1208 if (pSrcInfo)
1209 *pSrcInfo = imginfo;
1211 return D3D_OK;
1214 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
1215 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
1216 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1218 WCHAR *src_file_w;
1219 HRESULT hr;
1220 int strlength;
1222 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1223 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1224 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
1225 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1227 if (!src_file || !dst_surface)
1228 return D3DERR_INVALIDCALL;
1230 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
1231 src_file_w = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*src_file_w));
1232 MultiByteToWideChar(CP_ACP, 0, src_file, -1, src_file_w, strlength);
1234 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
1235 src_file_w, src_rect, filter, color_key, src_info);
1236 HeapFree(GetProcessHeap(), 0, src_file_w);
1238 return hr;
1241 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
1242 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
1243 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1245 UINT data_size;
1246 void *data;
1247 HRESULT hr;
1249 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1250 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1251 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
1252 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1254 if (!src_file || !dst_surface)
1255 return D3DERR_INVALIDCALL;
1257 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
1258 return D3DXERR_INVALIDDATA;
1260 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1261 data, data_size, src_rect, filter, color_key, src_info);
1262 UnmapViewOfFile(data);
1264 return hr;
1267 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
1268 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
1269 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1271 UINT data_size;
1272 HRSRC resinfo;
1273 void *data;
1275 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1276 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1277 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
1278 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1280 if (!dst_surface)
1281 return D3DERR_INVALIDCALL;
1283 if (!(resinfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA))
1284 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1285 && !(resinfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
1286 return D3DXERR_INVALIDDATA;
1288 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1289 return D3DXERR_INVALIDDATA;
1291 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1292 data, data_size, src_rect, filter, color_key, src_info);
1295 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
1296 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
1297 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1299 UINT data_size;
1300 HRSRC resinfo;
1301 void *data;
1303 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1304 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1305 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
1306 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1308 if (!dst_surface)
1309 return D3DERR_INVALIDCALL;
1311 if (!(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA))
1312 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1313 && !(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1314 return D3DXERR_INVALIDDATA;
1316 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1317 return D3DXERR_INVALIDDATA;
1319 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1320 data, data_size, src_rect, filter, color_key, src_info);
1324 /************************************************************
1325 * helper functions for D3DXLoadSurfaceFromMemory
1327 struct argb_conversion_info
1329 const struct pixel_format_desc *srcformat;
1330 const struct pixel_format_desc *destformat;
1331 DWORD srcshift[4], destshift[4];
1332 DWORD srcmask[4], destmask[4];
1333 BOOL process_channel[4];
1334 DWORD channelmask;
1337 static void init_argb_conversion_info(const struct pixel_format_desc *srcformat, const struct pixel_format_desc *destformat, struct argb_conversion_info *info)
1339 UINT i;
1340 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1341 info->channelmask = 0;
1343 info->srcformat = srcformat;
1344 info->destformat = destformat;
1346 for(i = 0;i < 4;i++) {
1347 /* srcshift is used to extract the _relevant_ components */
1348 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1350 /* destshift is used to move the components to the correct position */
1351 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1353 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1354 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1356 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1357 if(destformat->bits[i]) {
1358 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1359 else info->channelmask |= info->destmask[i];
1364 /************************************************************
1365 * get_relevant_argb_components
1367 * Extracts the relevant components from the source color and
1368 * drops the less significant bits if they aren't used by the destination format.
1370 static void get_relevant_argb_components(const struct argb_conversion_info *info, const BYTE *col, DWORD *out)
1372 unsigned int i, j;
1373 unsigned int component, mask;
1375 for (i = 0; i < 4; ++i)
1377 if (!info->process_channel[i])
1378 continue;
1380 component = 0;
1381 mask = info->srcmask[i];
1382 for (j = 0; j < 4 && mask; ++j)
1384 if (info->srcshift[i] < j * 8)
1385 component |= (col[j] & mask) << (j * 8 - info->srcshift[i]);
1386 else
1387 component |= (col[j] & mask) >> (info->srcshift[i] - j * 8);
1388 mask >>= 8;
1390 out[i] = component;
1394 /************************************************************
1395 * make_argb_color
1397 * Recombines the output of get_relevant_argb_components and converts
1398 * it to the destination format.
1400 static DWORD make_argb_color(const struct argb_conversion_info *info, const DWORD *in)
1402 UINT i;
1403 DWORD val = 0;
1405 for(i = 0;i < 4;i++) {
1406 if(info->process_channel[i]) {
1407 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1408 signed int shift;
1409 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1410 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1413 val |= info->channelmask; /* new channels are set to their maximal value */
1414 return val;
1417 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */
1418 static void format_to_vec4(const struct pixel_format_desc *format, const BYTE *src, struct vec4 *dst)
1420 DWORD mask, tmp;
1421 unsigned int c;
1423 for (c = 0; c < 4; ++c)
1425 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1426 float *dst_component = (float *)dst + component_offsets[c];
1428 if (format->bits[c])
1430 mask = ~0u >> (32 - format->bits[c]);
1432 memcpy(&tmp, src + format->shift[c] / 8,
1433 min(sizeof(DWORD), (format->shift[c] % 8 + format->bits[c] + 7) / 8));
1435 if (format->type == FORMAT_ARGBF16)
1436 *dst_component = float_16_to_32(tmp);
1437 else if (format->type == FORMAT_ARGBF)
1438 *dst_component = *(float *)&tmp;
1439 else
1440 *dst_component = (float)((tmp >> format->shift[c] % 8) & mask) / mask;
1442 else
1443 *dst_component = 1.0f;
1447 /* It doesn't work for components bigger than 32 bits. */
1448 static void format_from_vec4(const struct pixel_format_desc *format, const struct vec4 *src, BYTE *dst)
1450 DWORD v, mask32;
1451 unsigned int c, i;
1453 memset(dst, 0, format->bytes_per_pixel);
1455 for (c = 0; c < 4; ++c)
1457 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1458 const float src_component = *((const float *)src + component_offsets[c]);
1460 if (!format->bits[c])
1461 continue;
1463 mask32 = ~0u >> (32 - format->bits[c]);
1465 if (format->type == FORMAT_ARGBF16)
1466 v = float_32_to_16(src_component);
1467 else if (format->type == FORMAT_ARGBF)
1468 v = *(DWORD *)&src_component;
1469 else
1470 v = (DWORD)(src_component * ((1 << format->bits[c]) - 1) + 0.5f);
1472 for (i = format->shift[c] / 8 * 8; i < format->shift[c] + format->bits[c]; i += 8)
1474 BYTE mask, byte;
1476 if (format->shift[c] > i)
1478 mask = mask32 << (format->shift[c] - i);
1479 byte = (v << (format->shift[c] - i)) & mask;
1481 else
1483 mask = mask32 >> (i - format->shift[c]);
1484 byte = (v >> (i - format->shift[c])) & mask;
1486 dst[i / 8] |= byte;
1491 /************************************************************
1492 * copy_pixels
1494 * Copies the source buffer to the destination buffer.
1495 * Works for any pixel format.
1496 * The source and the destination must be block-aligned.
1498 void copy_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
1499 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *size,
1500 const struct pixel_format_desc *format)
1502 UINT row, slice;
1503 BYTE *dst_addr;
1504 const BYTE *src_addr;
1505 UINT row_block_count = (size->width + format->block_width - 1) / format->block_width;
1506 UINT row_count = (size->height + format->block_height - 1) / format->block_height;
1508 for (slice = 0; slice < size->depth; slice++)
1510 src_addr = src + slice * src_slice_pitch;
1511 dst_addr = dst + slice * dst_slice_pitch;
1513 for (row = 0; row < row_count; row++)
1515 memcpy(dst_addr, src_addr, row_block_count * format->block_byte_count);
1516 src_addr += src_row_pitch;
1517 dst_addr += dst_row_pitch;
1522 /************************************************************
1523 * convert_argb_pixels
1525 * Copies the source buffer to the destination buffer, performing
1526 * any necessary format conversion and color keying.
1527 * Pixels outsize the source rect are blacked out.
1529 void convert_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1530 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1531 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1532 const PALETTEENTRY *palette)
1534 struct argb_conversion_info conv_info, ck_conv_info;
1535 const struct pixel_format_desc *ck_format = NULL;
1536 DWORD channels[4];
1537 UINT min_width, min_height, min_depth;
1538 UINT x, y, z;
1540 ZeroMemory(channels, sizeof(channels));
1541 init_argb_conversion_info(src_format, dst_format, &conv_info);
1543 min_width = min(src_size->width, dst_size->width);
1544 min_height = min(src_size->height, dst_size->height);
1545 min_depth = min(src_size->depth, dst_size->depth);
1547 if (color_key)
1549 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1550 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1551 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1554 for (z = 0; z < min_depth; z++) {
1555 const BYTE *src_slice_ptr = src + z * src_slice_pitch;
1556 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1558 for (y = 0; y < min_height; y++) {
1559 const BYTE *src_ptr = src_slice_ptr + y * src_row_pitch;
1560 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1562 for (x = 0; x < min_width; x++) {
1563 if (!src_format->to_rgba && !dst_format->from_rgba
1564 && src_format->type == dst_format->type
1565 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1567 DWORD val;
1569 get_relevant_argb_components(&conv_info, src_ptr, channels);
1570 val = make_argb_color(&conv_info, channels);
1572 if (color_key)
1574 DWORD ck_pixel;
1576 get_relevant_argb_components(&ck_conv_info, src_ptr, channels);
1577 ck_pixel = make_argb_color(&ck_conv_info, channels);
1578 if (ck_pixel == color_key)
1579 val &= ~conv_info.destmask[0];
1581 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1583 else
1585 struct vec4 color, tmp;
1587 format_to_vec4(src_format, src_ptr, &color);
1588 if (src_format->to_rgba)
1589 src_format->to_rgba(&color, &tmp, palette);
1590 else
1591 tmp = color;
1593 if (ck_format)
1595 DWORD ck_pixel;
1597 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1598 if (ck_pixel == color_key)
1599 tmp.w = 0.0f;
1602 if (dst_format->from_rgba)
1603 dst_format->from_rgba(&tmp, &color);
1604 else
1605 color = tmp;
1607 format_from_vec4(dst_format, &color, dst_ptr);
1610 src_ptr += src_format->bytes_per_pixel;
1611 dst_ptr += dst_format->bytes_per_pixel;
1614 if (src_size->width < dst_size->width) /* black out remaining pixels */
1615 memset(dst_ptr, 0, dst_format->bytes_per_pixel * (dst_size->width - src_size->width));
1618 if (src_size->height < dst_size->height) /* black out remaining pixels */
1619 memset(dst + src_size->height * dst_row_pitch, 0, dst_row_pitch * (dst_size->height - src_size->height));
1621 if (src_size->depth < dst_size->depth) /* black out remaining pixels */
1622 memset(dst + src_size->depth * dst_slice_pitch, 0, dst_slice_pitch * (dst_size->depth - src_size->depth));
1625 /************************************************************
1626 * point_filter_argb_pixels
1628 * Copies the source buffer to the destination buffer, performing
1629 * any necessary format conversion, color keying and stretching
1630 * using a point filter.
1632 void point_filter_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1633 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1634 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1635 const PALETTEENTRY *palette)
1637 struct argb_conversion_info conv_info, ck_conv_info;
1638 const struct pixel_format_desc *ck_format = NULL;
1639 DWORD channels[4];
1640 UINT x, y, z;
1642 ZeroMemory(channels, sizeof(channels));
1643 init_argb_conversion_info(src_format, dst_format, &conv_info);
1645 if (color_key)
1647 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1648 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1649 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1652 for (z = 0; z < dst_size->depth; z++)
1654 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1655 const BYTE *src_slice_ptr = src + src_slice_pitch * (z * src_size->depth / dst_size->depth);
1657 for (y = 0; y < dst_size->height; y++)
1659 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1660 const BYTE *src_row_ptr = src_slice_ptr + src_row_pitch * (y * src_size->height / dst_size->height);
1662 for (x = 0; x < dst_size->width; x++)
1664 const BYTE *src_ptr = src_row_ptr + (x * src_size->width / dst_size->width) * src_format->bytes_per_pixel;
1666 if (!src_format->to_rgba && !dst_format->from_rgba
1667 && src_format->type == dst_format->type
1668 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1670 DWORD val;
1672 get_relevant_argb_components(&conv_info, src_ptr, channels);
1673 val = make_argb_color(&conv_info, channels);
1675 if (color_key)
1677 DWORD ck_pixel;
1679 get_relevant_argb_components(&ck_conv_info, src_ptr, channels);
1680 ck_pixel = make_argb_color(&ck_conv_info, channels);
1681 if (ck_pixel == color_key)
1682 val &= ~conv_info.destmask[0];
1684 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1686 else
1688 struct vec4 color, tmp;
1690 format_to_vec4(src_format, src_ptr, &color);
1691 if (src_format->to_rgba)
1692 src_format->to_rgba(&color, &tmp, palette);
1693 else
1694 tmp = color;
1696 if (ck_format)
1698 DWORD ck_pixel;
1700 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1701 if (ck_pixel == color_key)
1702 tmp.w = 0.0f;
1705 if (dst_format->from_rgba)
1706 dst_format->from_rgba(&tmp, &color);
1707 else
1708 color = tmp;
1710 format_from_vec4(dst_format, &color, dst_ptr);
1713 dst_ptr += dst_format->bytes_per_pixel;
1719 /************************************************************
1720 * D3DXLoadSurfaceFromMemory
1722 * Loads data from a given memory chunk into a surface,
1723 * applying any of the specified filters.
1725 * PARAMS
1726 * pDestSurface [I] pointer to the surface
1727 * pDestPalette [I] palette to use
1728 * pDestRect [I] to be filled area of the surface
1729 * pSrcMemory [I] pointer to the source data
1730 * SrcFormat [I] format of the source pixel data
1731 * SrcPitch [I] number of bytes in a row
1732 * pSrcPalette [I] palette used in the source image
1733 * pSrcRect [I] area of the source data to load
1734 * dwFilter [I] filter to apply on stretching
1735 * Colorkey [I] colorkey
1737 * RETURNS
1738 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1739 * if pSrcMemory is NULL but the other parameters are valid
1740 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect is NULL or
1741 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1742 * if DestRect is invalid
1743 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1744 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1746 * NOTES
1747 * pSrcRect specifies the dimensions of the source data;
1748 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1751 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1752 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1753 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1754 DWORD filter, D3DCOLOR color_key)
1756 const struct pixel_format_desc *srcformatdesc, *destformatdesc;
1757 D3DSURFACE_DESC surfdesc;
1758 D3DLOCKED_RECT lockrect;
1759 struct volume src_size, dst_size;
1761 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s, %#x, 0x%08x)\n",
1762 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1763 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1765 if (!dst_surface || !src_memory || !src_rect)
1767 WARN("Invalid argument specified.\n");
1768 return D3DERR_INVALIDCALL;
1770 if (src_format == D3DFMT_UNKNOWN
1771 || src_rect->left >= src_rect->right
1772 || src_rect->top >= src_rect->bottom)
1774 WARN("Invalid src_format or src_rect.\n");
1775 return E_FAIL;
1778 if (filter == D3DX_DEFAULT)
1779 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1781 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1783 src_size.width = src_rect->right - src_rect->left;
1784 src_size.height = src_rect->bottom - src_rect->top;
1785 src_size.depth = 1;
1786 if (!dst_rect)
1788 dst_size.width = surfdesc.Width;
1789 dst_size.height = surfdesc.Height;
1791 else
1793 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width
1794 || dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height
1795 || dst_rect->left < 0 || dst_rect->top < 0)
1797 WARN("Invalid dst_rect specified.\n");
1798 return D3DERR_INVALIDCALL;
1800 dst_size.width = dst_rect->right - dst_rect->left;
1801 dst_size.height = dst_rect->bottom - dst_rect->top;
1802 if (!dst_size.width || !dst_size.height)
1803 return D3D_OK;
1805 dst_size.depth = 1;
1807 srcformatdesc = get_format_info(src_format);
1808 destformatdesc = get_format_info(surfdesc.Format);
1809 if (srcformatdesc->type == FORMAT_UNKNOWN || destformatdesc->type == FORMAT_UNKNOWN)
1811 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format, surfdesc.Format);
1812 return E_NOTIMPL;
1815 if (src_format == surfdesc.Format
1816 && dst_size.width == src_size.width
1817 && dst_size.height == src_size.height
1818 && color_key == 0) /* Simple copy. */
1820 if (src_rect->left & (srcformatdesc->block_width - 1)
1821 || src_rect->top & (srcformatdesc->block_height - 1)
1822 || (src_rect->right & (srcformatdesc->block_width - 1)
1823 && src_size.width != surfdesc.Width)
1824 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1825 && src_size.height != surfdesc.Height))
1827 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1828 return D3DXERR_INVALIDDATA;
1831 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1832 return D3DXERR_INVALIDDATA;
1834 copy_pixels(src_memory, src_pitch, 0, lockrect.pBits, lockrect.Pitch, 0,
1835 &src_size, srcformatdesc);
1837 IDirect3DSurface9_UnlockRect(dst_surface);
1839 else /* Stretching or format conversion. */
1841 if (((srcformatdesc->type != FORMAT_ARGB) && (srcformatdesc->type != FORMAT_INDEX)) ||
1842 (destformatdesc->type != FORMAT_ARGB))
1844 FIXME("Format conversion missing %#x -> %#x\n", src_format, surfdesc.Format);
1845 return E_NOTIMPL;
1848 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1849 return D3DXERR_INVALIDDATA;
1851 if ((filter & 0xf) == D3DX_FILTER_NONE)
1853 convert_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1854 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
1856 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1858 if ((filter & 0xf) != D3DX_FILTER_POINT)
1859 FIXME("Unhandled filter %#x.\n", filter);
1861 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1862 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1863 point_filter_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1864 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
1867 IDirect3DSurface9_UnlockRect(dst_surface);
1870 return D3D_OK;
1873 /************************************************************
1874 * D3DXLoadSurfaceFromSurface
1876 * Copies the contents from one surface to another, performing any required
1877 * format conversion, resizing or filtering.
1879 * PARAMS
1880 * pDestSurface [I] pointer to the destination surface
1881 * pDestPalette [I] palette to use
1882 * pDestRect [I] to be filled area of the surface
1883 * pSrcSurface [I] pointer to the source surface
1884 * pSrcPalette [I] palette used for the source surface
1885 * pSrcRect [I] area of the source data to load
1886 * dwFilter [I] filter to apply on resizing
1887 * Colorkey [I] any ARGB value or 0 to disable color-keying
1889 * RETURNS
1890 * Success: D3D_OK
1891 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface is NULL
1892 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1895 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
1896 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
1897 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
1899 RECT rect;
1900 D3DLOCKED_RECT lock;
1901 D3DSURFACE_DESC SrcDesc;
1902 HRESULT hr;
1904 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1905 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1906 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
1907 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1909 if (!dst_surface || !src_surface)
1910 return D3DERR_INVALIDCALL;
1912 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
1914 if (!src_rect)
1915 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1916 else
1917 rect = *src_rect;
1919 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
1920 return D3DXERR_INVALIDDATA;
1922 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
1923 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
1925 IDirect3DSurface9_UnlockRect(src_surface);
1927 return hr;
1931 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1932 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1934 int len;
1935 WCHAR *filename;
1936 HRESULT hr;
1937 ID3DXBuffer *buffer;
1939 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1940 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1942 if (!dst_filename) return D3DERR_INVALIDCALL;
1944 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1945 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1946 if (!filename) return E_OUTOFMEMORY;
1947 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1949 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1950 if (SUCCEEDED(hr))
1952 hr = write_buffer_to_file(filename, buffer);
1953 ID3DXBuffer_Release(buffer);
1956 HeapFree(GetProcessHeap(), 0, filename);
1957 return hr;
1960 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1961 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1963 HRESULT hr;
1964 ID3DXBuffer *buffer;
1966 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1967 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1969 if (!dst_filename) return D3DERR_INVALIDCALL;
1971 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1972 if (SUCCEEDED(hr))
1974 hr = write_buffer_to_file(dst_filename, buffer);
1975 ID3DXBuffer_Release(buffer);
1978 return hr;
1981 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1982 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1984 IWICBitmapEncoder *encoder = NULL;
1985 IWICBitmapFrameEncode *frame = NULL;
1986 IPropertyBag2 *encoder_options = NULL;
1987 IStream *stream = NULL;
1988 HRESULT hr;
1989 HRESULT initresult;
1990 const CLSID *encoder_clsid;
1991 const GUID *pixel_format_guid;
1992 WICPixelFormatGUID wic_pixel_format;
1993 D3DFORMAT d3d_pixel_format;
1994 D3DSURFACE_DESC src_surface_desc;
1995 D3DLOCKED_RECT locked_rect;
1996 int width, height;
1997 STATSTG stream_stats;
1998 HGLOBAL stream_hglobal;
1999 ID3DXBuffer *buffer;
2000 DWORD size;
2002 TRACE("(%p, %#x, %p, %p, %s)\n",
2003 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
2005 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
2007 if (src_palette)
2009 FIXME("Saving surfaces with palettized pixel formats is not implemented yet\n");
2010 return D3DERR_INVALIDCALL;
2013 switch (file_format)
2015 case D3DXIFF_BMP:
2016 case D3DXIFF_DIB:
2017 encoder_clsid = &CLSID_WICBmpEncoder;
2018 break;
2019 case D3DXIFF_PNG:
2020 encoder_clsid = &CLSID_WICPngEncoder;
2021 break;
2022 case D3DXIFF_JPG:
2023 encoder_clsid = &CLSID_WICJpegEncoder;
2024 break;
2025 case D3DXIFF_DDS:
2026 return save_dds_surface_to_memory(dst_buffer, src_surface, src_rect);
2027 case D3DXIFF_HDR:
2028 case D3DXIFF_PFM:
2029 case D3DXIFF_TGA:
2030 case D3DXIFF_PPM:
2031 FIXME("File format %#x is not supported yet\n", file_format);
2032 return E_NOTIMPL;
2033 default:
2034 return D3DERR_INVALIDCALL;
2037 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
2038 if (src_rect)
2040 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
2042 WARN("Invalid rectangle with 0 area\n");
2043 return D3DXCreateBuffer(64, dst_buffer);
2045 if (src_rect->left < 0 || src_rect->top < 0)
2046 return D3DERR_INVALIDCALL;
2047 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
2048 return D3DERR_INVALIDCALL;
2049 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
2050 return D3DERR_INVALIDCALL;
2052 width = src_rect->right - src_rect->left;
2053 height = src_rect->bottom - src_rect->top;
2055 else
2057 width = src_surface_desc.Width;
2058 height = src_surface_desc.Height;
2061 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2063 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
2064 &IID_IWICBitmapEncoder, (void **)&encoder);
2065 if (FAILED(hr)) goto cleanup_err;
2067 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
2068 if (FAILED(hr)) goto cleanup_err;
2070 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2071 if (FAILED(hr)) goto cleanup_err;
2073 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
2074 if (FAILED(hr)) goto cleanup_err;
2076 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
2077 if (FAILED(hr)) goto cleanup_err;
2079 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
2080 if (FAILED(hr)) goto cleanup_err;
2082 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
2083 if (!pixel_format_guid)
2085 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
2086 hr = E_NOTIMPL;
2087 goto cleanup;
2090 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
2091 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
2092 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
2093 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
2095 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
2097 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
2099 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2100 if (SUCCEEDED(hr))
2102 IWICBitmapFrameEncode_WritePixels(frame, height,
2103 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
2104 IDirect3DSurface9_UnlockRect(src_surface);
2107 else /* Pixel format conversion */
2109 const struct pixel_format_desc *src_format_desc, *dst_format_desc;
2110 struct volume size;
2111 DWORD dst_pitch;
2112 void *dst_data;
2114 src_format_desc = get_format_info(src_surface_desc.Format);
2115 dst_format_desc = get_format_info(d3d_pixel_format);
2116 if (src_format_desc->type != FORMAT_ARGB || dst_format_desc->type != FORMAT_ARGB)
2118 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
2119 src_surface_desc.Format, d3d_pixel_format);
2120 hr = E_NOTIMPL;
2121 goto cleanup;
2124 size.width = width;
2125 size.height = height;
2126 size.depth = 1;
2127 dst_pitch = width * dst_format_desc->bytes_per_pixel;
2128 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
2129 if (!dst_data)
2131 hr = E_OUTOFMEMORY;
2132 goto cleanup;
2135 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2136 if (SUCCEEDED(hr))
2138 convert_argb_pixels(locked_rect.pBits, locked_rect.Pitch, 0, &size, src_format_desc,
2139 dst_data, dst_pitch, 0, &size, dst_format_desc, 0, NULL);
2140 IDirect3DSurface9_UnlockRect(src_surface);
2143 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
2144 HeapFree(GetProcessHeap(), 0, dst_data);
2147 hr = IWICBitmapFrameEncode_Commit(frame);
2148 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
2150 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
2152 /* copy data from stream to ID3DXBuffer */
2153 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
2154 if (FAILED(hr)) goto cleanup_err;
2156 if (stream_stats.cbSize.u.HighPart != 0)
2158 hr = D3DXERR_INVALIDDATA;
2159 goto cleanup;
2161 size = stream_stats.cbSize.u.LowPart;
2163 /* Remove BMP header for DIB */
2164 if (file_format == D3DXIFF_DIB)
2165 size -= sizeof(BITMAPFILEHEADER);
2167 hr = D3DXCreateBuffer(size, &buffer);
2168 if (FAILED(hr)) goto cleanup;
2170 hr = GetHGlobalFromStream(stream, &stream_hglobal);
2171 if (SUCCEEDED(hr))
2173 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
2174 void *stream_data = GlobalLock(stream_hglobal);
2175 /* Remove BMP header for DIB */
2176 if (file_format == D3DXIFF_DIB)
2177 stream_data = (void*)((BYTE*)stream_data + sizeof(BITMAPFILEHEADER));
2178 memcpy(buffer_pointer, stream_data, size);
2179 GlobalUnlock(stream_hglobal);
2180 *dst_buffer = buffer;
2182 else ID3DXBuffer_Release(buffer);
2184 cleanup_err:
2185 if (FAILED(hr) && hr != E_OUTOFMEMORY)
2186 hr = D3DERR_INVALIDCALL;
2188 cleanup:
2189 if (stream) IStream_Release(stream);
2191 if (frame) IWICBitmapFrameEncode_Release(frame);
2192 if (encoder_options) IPropertyBag2_Release(encoder_options);
2194 if (encoder) IWICBitmapEncoder_Release(encoder);
2196 if (SUCCEEDED(initresult)) CoUninitialize();
2198 return hr;