makedep: Add dependency for ttf font files when necessary.
[wine.git] / dlls / d3dx9_36 / surface.c
blob09c2fce1e2be77840e0b7511e09775e615219fb3
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_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 unsigned 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 unsigned 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 unsigned 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'),
157 D3DFMT_R16F,
158 D3DFMT_G16R16F,
159 D3DFMT_A16B16G16R16F,
160 D3DFMT_R32F,
161 D3DFMT_G32R32F,
162 D3DFMT_A32B32G32R32F,
165 for (i = 0; i < sizeof(known_fourcc) / sizeof(known_fourcc[0]); i++)
167 if (known_fourcc[i] == fourcc)
168 return fourcc;
171 WARN("Unknown FourCC %#x\n", fourcc);
172 return D3DFMT_UNKNOWN;
175 static const struct {
176 DWORD bpp;
177 DWORD rmask;
178 DWORD gmask;
179 DWORD bmask;
180 DWORD amask;
181 D3DFORMAT format;
182 } rgb_pixel_formats[] = {
183 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 },
184 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 },
185 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 },
186 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 },
187 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 },
188 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 },
189 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 },
190 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 },
191 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 },
192 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 },
193 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 },
194 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 },
195 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 },
196 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8 },
197 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8 },
200 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
202 unsigned int i;
204 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
206 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
207 && rgb_pixel_formats[i].rmask == pixel_format->rmask
208 && rgb_pixel_formats[i].gmask == pixel_format->gmask
209 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
211 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
212 return rgb_pixel_formats[i].format;
213 if (rgb_pixel_formats[i].amask == 0)
214 return rgb_pixel_formats[i].format;
218 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
219 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
220 return D3DFMT_UNKNOWN;
223 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
225 if (pixel_format->bpp == 8)
227 if (pixel_format->rmask == 0xff)
228 return D3DFMT_L8;
229 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
230 return D3DFMT_A4L4;
232 if (pixel_format->bpp == 16)
234 if (pixel_format->rmask == 0xffff)
235 return D3DFMT_L16;
236 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
237 return D3DFMT_A8L8;
240 WARN("Unknown luminance pixel format (bpp %u, l %#x, a %#x)\n",
241 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
242 return D3DFMT_UNKNOWN;
245 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
247 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
248 return D3DFMT_A8;
250 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format->bpp, pixel_format->rmask);
251 return D3DFMT_UNKNOWN;
254 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
256 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00)
257 return D3DFMT_V8U8;
258 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000)
259 return D3DFMT_V16U16;
261 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format->bpp,
262 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
263 return D3DFMT_UNKNOWN;
266 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
268 TRACE("pixel_format: size %u, flags %#x, fourcc %#x, bpp %u.\n", pixel_format->size,
269 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp);
270 TRACE("rmask %#x, gmask %#x, bmask %#x, amask %#x.\n", pixel_format->rmask, pixel_format->gmask,
271 pixel_format->bmask, pixel_format->amask);
273 if (pixel_format->flags & DDS_PF_FOURCC)
274 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
275 if (pixel_format->flags & DDS_PF_RGB)
276 return dds_rgb_to_d3dformat(pixel_format);
277 if (pixel_format->flags & DDS_PF_LUMINANCE)
278 return dds_luminance_to_d3dformat(pixel_format);
279 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
280 return dds_alpha_to_d3dformat(pixel_format);
281 if (pixel_format->flags & DDS_PF_BUMPDUDV)
282 return dds_bump_to_d3dformat(pixel_format);
284 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n",
285 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
286 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
287 return D3DFMT_UNKNOWN;
290 static HRESULT d3dformat_to_dds_pixel_format(struct dds_pixel_format *pixel_format, D3DFORMAT d3dformat)
292 unsigned int i;
294 memset(pixel_format, 0, sizeof(*pixel_format));
296 pixel_format->size = sizeof(*pixel_format);
298 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
300 if (rgb_pixel_formats[i].format == d3dformat)
302 pixel_format->flags |= DDS_PF_RGB;
303 pixel_format->bpp = rgb_pixel_formats[i].bpp;
304 pixel_format->rmask = rgb_pixel_formats[i].rmask;
305 pixel_format->gmask = rgb_pixel_formats[i].gmask;
306 pixel_format->bmask = rgb_pixel_formats[i].bmask;
307 pixel_format->amask = rgb_pixel_formats[i].amask;
308 if (pixel_format->amask) pixel_format->flags |= DDS_PF_ALPHA;
309 return D3D_OK;
313 WARN("Unknown pixel format %#x\n", d3dformat);
314 return E_NOTIMPL;
317 static HRESULT calculate_dds_surface_size(D3DFORMAT format, UINT width, UINT height,
318 UINT *pitch, UINT *size)
320 const struct pixel_format_desc *format_desc = get_format_info(format);
321 if (format_desc->type == FORMAT_UNKNOWN)
322 return E_NOTIMPL;
324 if (format_desc->block_width != 1 || format_desc->block_height != 1)
326 *pitch = format_desc->block_byte_count
327 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
328 *size = *pitch
329 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
331 else
333 *pitch = width * format_desc->bytes_per_pixel;
334 *size = *pitch * height;
337 return D3D_OK;
340 static UINT calculate_dds_file_size(D3DFORMAT format, UINT width, UINT height, UINT depth,
341 UINT miplevels, UINT faces)
343 UINT i, file_size = 0;
345 for (i = 0; i < miplevels; i++)
347 UINT pitch, size = 0;
348 calculate_dds_surface_size(format, width, height, &pitch, &size);
349 size *= depth;
350 file_size += size;
351 width = max(1, width / 2);
352 height = max(1, height / 2);
353 depth = max(1, depth / 2);
356 file_size *= faces;
357 file_size += sizeof(struct dds_header);
358 return file_size;
361 /************************************************************
362 * get_image_info_from_dds
364 * Fills a D3DXIMAGE_INFO structure with information
365 * about a DDS file stored in the memory.
367 * PARAMS
368 * buffer [I] pointer to DDS data
369 * length [I] size of DDS data
370 * info [O] pointer to D3DXIMAGE_INFO structure
372 * RETURNS
373 * Success: D3D_OK
374 * Failure: D3DXERR_INVALIDDATA
377 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
379 UINT faces = 1;
380 UINT expected_length;
381 const struct dds_header *header = buffer;
383 if (length < sizeof(*header) || !info)
384 return D3DXERR_INVALIDDATA;
386 if (header->pixel_format.size != sizeof(header->pixel_format))
387 return D3DXERR_INVALIDDATA;
389 info->Width = header->width;
390 info->Height = header->height;
391 info->Depth = 1;
392 info->MipLevels = (header->flags & DDS_MIPMAPCOUNT) ? header->miplevels : 1;
394 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
395 if (info->Format == D3DFMT_UNKNOWN)
396 return D3DXERR_INVALIDDATA;
398 TRACE("Pixel format is %#x\n", info->Format);
400 if (header->caps2 & DDS_CAPS2_VOLUME)
402 info->Depth = header->depth;
403 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
405 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
407 DWORD face;
408 faces = 0;
409 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
411 if (header->caps2 & face)
412 faces++;
414 info->ResourceType = D3DRTYPE_CUBETEXTURE;
416 else
418 info->ResourceType = D3DRTYPE_TEXTURE;
421 expected_length = calculate_dds_file_size(info->Format, info->Width, info->Height, info->Depth,
422 info->MipLevels, faces);
423 if (length < expected_length)
425 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
426 return D3DXERR_INVALIDDATA;
429 info->ImageFileFormat = D3DXIFF_DDS;
430 return D3D_OK;
433 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
434 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
435 const D3DXIMAGE_INFO *src_info)
437 UINT size;
438 UINT src_pitch;
439 const struct dds_header *header = src_data;
440 const BYTE *pixels = (BYTE *)(header + 1);
442 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
443 return D3DXERR_INVALIDDATA;
445 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &src_pitch, &size)))
446 return E_NOTIMPL;
448 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
449 src_pitch, NULL, src_rect, filter, color_key);
452 static HRESULT save_dds_surface_to_memory(ID3DXBuffer **dst_buffer, IDirect3DSurface9 *src_surface, const RECT *src_rect)
454 HRESULT hr;
455 UINT dst_pitch, surface_size, file_size;
456 D3DSURFACE_DESC src_desc;
457 D3DLOCKED_RECT locked_rect;
458 ID3DXBuffer *buffer;
459 struct dds_header *header;
460 BYTE *pixels;
461 struct volume volume;
462 const struct pixel_format_desc *pixel_format;
464 if (src_rect)
466 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
467 return E_NOTIMPL;
470 hr = IDirect3DSurface9_GetDesc(src_surface, &src_desc);
471 if (FAILED(hr)) return hr;
473 pixel_format = get_format_info(src_desc.Format);
474 if (pixel_format->type == FORMAT_UNKNOWN) return E_NOTIMPL;
476 file_size = calculate_dds_file_size(src_desc.Format, src_desc.Width, src_desc.Height, 1, 1, 1);
478 hr = calculate_dds_surface_size(src_desc.Format, src_desc.Width, src_desc.Height, &dst_pitch, &surface_size);
479 if (FAILED(hr)) return hr;
481 hr = D3DXCreateBuffer(file_size, &buffer);
482 if (FAILED(hr)) return hr;
484 header = ID3DXBuffer_GetBufferPointer(buffer);
485 pixels = (BYTE *)(header + 1);
487 memset(header, 0, sizeof(*header));
488 header->signature = MAKEFOURCC('D','D','S',' ');
489 header->size = sizeof(*header);
490 header->flags = DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PITCH | DDS_PIXELFORMAT | DDS_MIPMAPCOUNT;
491 header->height = src_desc.Height;
492 header->width = src_desc.Width;
493 header->pitch_or_linear_size = dst_pitch;
494 header->depth = 1;
495 header->miplevels = 1;
496 header->caps = DDS_CAPS_TEXTURE;
497 hr = d3dformat_to_dds_pixel_format(&header->pixel_format, src_desc.Format);
498 if (FAILED(hr))
500 ID3DXBuffer_Release(buffer);
501 return hr;
504 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, NULL, D3DLOCK_READONLY);
505 if (FAILED(hr))
507 ID3DXBuffer_Release(buffer);
508 return hr;
511 volume.width = src_desc.Width;
512 volume.height = src_desc.Height;
513 volume.depth = 1;
514 copy_pixels(locked_rect.pBits, locked_rect.Pitch, 0, pixels, dst_pitch, 0,
515 &volume, pixel_format);
517 IDirect3DSurface9_UnlockRect(src_surface);
519 *dst_buffer = buffer;
520 return D3D_OK;
523 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette,
524 const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key,
525 const D3DXIMAGE_INFO *src_info)
527 UINT row_pitch, slice_pitch;
528 const struct dds_header *header = src_data;
529 const BYTE *pixels = (BYTE *)(header + 1);
531 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
532 return D3DXERR_INVALIDDATA;
534 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &row_pitch, &slice_pitch)))
535 return E_NOTIMPL;
537 return D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box, pixels, src_info->Format,
538 row_pitch, slice_pitch, NULL, src_box, filter, color_key);
541 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
542 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info, unsigned int skip_levels,
543 unsigned int *loaded_miplevels)
545 HRESULT hr;
546 RECT src_rect;
547 UINT src_pitch;
548 UINT mip_level;
549 UINT mip_levels;
550 UINT mip_level_size;
551 UINT width, height;
552 IDirect3DSurface9 *surface;
553 const struct dds_header *header = src_data;
554 const BYTE *pixels = (BYTE *)(header + 1);
556 /* Loading a cube texture as a simple texture is also supported
557 * (only first face texture is taken). Same with volume textures. */
558 if ((src_info->ResourceType != D3DRTYPE_TEXTURE)
559 && (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
560 && (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE))
562 WARN("Trying to load a %u resource as a 2D texture, returning failure.\n", src_info->ResourceType);
563 return D3DXERR_INVALIDDATA;
566 width = src_info->Width;
567 height = src_info->Height;
568 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
569 if (src_info->ResourceType == D3DRTYPE_VOLUMETEXTURE)
570 mip_levels = 1;
571 for (mip_level = 0; mip_level < mip_levels + skip_levels; ++mip_level)
573 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_pitch, &mip_level_size);
574 if (FAILED(hr)) return hr;
576 if (mip_level >= skip_levels)
578 SetRect(&src_rect, 0, 0, width, height);
580 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level - skip_levels, &surface);
581 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
582 NULL, &src_rect, filter, color_key);
583 IDirect3DSurface9_Release(surface);
584 if (FAILED(hr))
585 return hr;
588 pixels += mip_level_size;
589 width = max(1, width / 2);
590 height = max(1, height / 2);
593 *loaded_miplevels = mip_levels - skip_levels;
595 return D3D_OK;
598 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
599 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
601 HRESULT hr;
602 int face;
603 UINT mip_level;
604 UINT size;
605 RECT src_rect;
606 UINT src_pitch;
607 UINT mip_levels;
608 UINT mip_level_size;
609 IDirect3DSurface9 *surface;
610 const struct dds_header *header = src_data;
611 const BYTE *pixels = (BYTE *)(header + 1);
613 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
614 return D3DXERR_INVALIDDATA;
616 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
618 WARN("Only full cubemaps are supported\n");
619 return D3DXERR_INVALIDDATA;
622 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
623 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
625 size = src_info->Width;
626 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
628 hr = calculate_dds_surface_size(src_info->Format, size, size, &src_pitch, &mip_level_size);
629 if (FAILED(hr)) return hr;
631 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
632 if (mip_level < mip_levels)
634 SetRect(&src_rect, 0, 0, size, size);
636 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
637 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
638 NULL, &src_rect, filter, color_key);
639 IDirect3DSurface9_Release(surface);
640 if (FAILED(hr)) return hr;
643 pixels += mip_level_size;
644 size = max(1, size / 2);
648 return D3D_OK;
651 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
652 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
654 HRESULT hr;
655 UINT mip_level;
656 UINT mip_levels;
657 UINT src_slice_pitch;
658 UINT src_row_pitch;
659 D3DBOX src_box;
660 UINT width, height, depth;
661 IDirect3DVolume9 *volume;
662 const struct dds_header *header = src_data;
663 const BYTE *pixels = (BYTE *)(header + 1);
665 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
666 return D3DXERR_INVALIDDATA;
668 width = src_info->Width;
669 height = src_info->Height;
670 depth = src_info->Depth;
671 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture));
673 for (mip_level = 0; mip_level < mip_levels; mip_level++)
675 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_row_pitch, &src_slice_pitch);
676 if (FAILED(hr)) return hr;
678 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume);
679 if (FAILED(hr)) return hr;
681 src_box.Left = 0;
682 src_box.Top = 0;
683 src_box.Right = width;
684 src_box.Bottom = height;
685 src_box.Front = 0;
686 src_box.Back = depth;
688 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format,
689 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key);
691 IDirect3DVolume9_Release(volume);
692 if (FAILED(hr)) return hr;
694 pixels += depth * src_slice_pitch;
695 width = max(1, width / 2);
696 height = max(1, height / 2);
697 depth = max(1, depth / 2);
700 return D3D_OK;
703 static BOOL convert_dib_to_bmp(void **data, UINT *size)
705 ULONG header_size;
706 ULONG count = 0;
707 ULONG offset;
708 BITMAPFILEHEADER *header;
709 BYTE *new_data;
710 UINT new_size;
712 if ((*size < 4) || (*size < (header_size = *(ULONG*)*data)))
713 return FALSE;
715 if ((header_size == sizeof(BITMAPINFOHEADER)) ||
716 (header_size == sizeof(BITMAPV4HEADER)) ||
717 (header_size == sizeof(BITMAPV5HEADER)) ||
718 (header_size == 64 /* sizeof(BITMAPCOREHEADER2) */))
720 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
721 BITMAPINFOHEADER *info_header = (BITMAPINFOHEADER*)*data;
722 count = info_header->biClrUsed;
724 if (!count && info_header->biBitCount <= 8)
725 count = 1 << info_header->biBitCount;
727 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBQUAD) * count;
729 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
730 if ((info_header->biSize == sizeof(BITMAPINFOHEADER)) && (info_header->biCompression == BI_BITFIELDS))
731 offset += 3 * sizeof(DWORD);
733 else if (header_size == sizeof(BITMAPCOREHEADER))
735 BITMAPCOREHEADER *core_header = (BITMAPCOREHEADER*)*data;
737 if (core_header->bcBitCount <= 8)
738 count = 1 << core_header->bcBitCount;
740 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBTRIPLE) * count;
742 else
744 return FALSE;
747 TRACE("Converting DIB file to BMP\n");
749 new_size = *size + sizeof(BITMAPFILEHEADER);
750 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
751 CopyMemory(new_data + sizeof(BITMAPFILEHEADER), *data, *size);
753 /* Add BMP header */
754 header = (BITMAPFILEHEADER*)new_data;
755 header->bfType = 0x4d42; /* BM */
756 header->bfSize = new_size;
757 header->bfReserved1 = 0;
758 header->bfReserved2 = 0;
759 header->bfOffBits = offset;
761 /* Update input data */
762 *data = new_data;
763 *size = new_size;
765 return TRUE;
768 /************************************************************
769 * D3DXGetImageInfoFromFileInMemory
771 * Fills a D3DXIMAGE_INFO structure with info about an image
773 * PARAMS
774 * data [I] pointer to the image file data
775 * datasize [I] size of the passed data
776 * info [O] pointer to the destination structure
778 * RETURNS
779 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
780 * if info is NULL and data and datasize are not NULL
781 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
782 * D3DERR_INVALIDCALL, if data is NULL or
783 * if datasize is 0
785 * NOTES
786 * datasize may be bigger than the actual file size
789 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(const void *data, UINT datasize, D3DXIMAGE_INFO *info)
791 IWICImagingFactory *factory;
792 IWICBitmapDecoder *decoder = NULL;
793 IWICStream *stream;
794 HRESULT hr;
795 HRESULT initresult;
796 BOOL dib;
798 TRACE("(%p, %d, %p)\n", data, datasize, info);
800 if (!data || !datasize)
801 return D3DERR_INVALIDCALL;
803 if (!info)
804 return D3D_OK;
806 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
807 TRACE("File type is DDS\n");
808 return get_image_info_from_dds(data, datasize, info);
811 /* In case of DIB file, convert it to BMP */
812 dib = convert_dib_to_bmp((void**)&data, &datasize);
814 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
816 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
818 if (SUCCEEDED(hr)) {
819 IWICImagingFactory_CreateStream(factory, &stream);
820 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
821 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
822 IWICStream_Release(stream);
823 IWICImagingFactory_Release(factory);
826 if (FAILED(hr)) {
827 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
828 FIXME("File type PPM is not supported yet\n");
829 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
830 FIXME("File type HDR is not supported yet\n");
831 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
832 FIXME("File type PFM is not supported yet\n");
835 if (SUCCEEDED(hr)) {
836 GUID container_format;
837 UINT frame_count;
839 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
840 if (SUCCEEDED(hr)) {
841 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
842 if (dib) {
843 TRACE("File type is DIB\n");
844 info->ImageFileFormat = D3DXIFF_DIB;
845 } else {
846 TRACE("File type is BMP\n");
847 info->ImageFileFormat = D3DXIFF_BMP;
849 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
850 TRACE("File type is PNG\n");
851 info->ImageFileFormat = D3DXIFF_PNG;
852 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
853 TRACE("File type is JPG\n");
854 info->ImageFileFormat = D3DXIFF_JPG;
855 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
856 TRACE("File type is TGA\n");
857 info->ImageFileFormat = D3DXIFF_TGA;
858 } else {
859 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
860 hr = D3DXERR_INVALIDDATA;
864 if (SUCCEEDED(hr))
865 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
866 if (SUCCEEDED(hr) && !frame_count)
867 hr = D3DXERR_INVALIDDATA;
869 if (SUCCEEDED(hr)) {
870 IWICBitmapFrameDecode *frame = NULL;
872 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
874 if (SUCCEEDED(hr))
875 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
877 if (SUCCEEDED(hr)) {
878 WICPixelFormatGUID pixel_format;
880 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
881 if (SUCCEEDED(hr)) {
882 info->Format = wic_guid_to_d3dformat(&pixel_format);
883 if (info->Format == D3DFMT_UNKNOWN) {
884 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
885 hr = D3DXERR_INVALIDDATA;
890 if (frame)
891 IWICBitmapFrameDecode_Release(frame);
893 info->Depth = 1;
894 info->MipLevels = 1;
895 info->ResourceType = D3DRTYPE_TEXTURE;
899 if (decoder)
900 IWICBitmapDecoder_Release(decoder);
902 if (SUCCEEDED(initresult))
903 CoUninitialize();
905 if (dib)
906 HeapFree(GetProcessHeap(), 0, (void*)data);
908 if (FAILED(hr)) {
909 TRACE("Invalid or unsupported image file\n");
910 return D3DXERR_INVALIDDATA;
913 return D3D_OK;
916 /************************************************************
917 * D3DXGetImageInfoFromFile
919 * RETURNS
920 * Success: D3D_OK, if we successfully load a valid image file or
921 * if we successfully load a file which is no valid image and info is NULL
922 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
923 * if file is not a valid image file and info is not NULL
924 * D3DERR_INVALIDCALL, if file is NULL
927 HRESULT WINAPI D3DXGetImageInfoFromFileA(const char *file, D3DXIMAGE_INFO *info)
929 WCHAR *widename;
930 HRESULT hr;
931 int strlength;
933 TRACE("file %s, info %p.\n", debugstr_a(file), info);
935 if( !file ) return D3DERR_INVALIDCALL;
937 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
938 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
939 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
941 hr = D3DXGetImageInfoFromFileW(widename, info);
942 HeapFree(GetProcessHeap(), 0, widename);
944 return hr;
947 HRESULT WINAPI D3DXGetImageInfoFromFileW(const WCHAR *file, D3DXIMAGE_INFO *info)
949 void *buffer;
950 HRESULT hr;
951 DWORD size;
953 TRACE("file %s, info %p.\n", debugstr_w(file), info);
955 if (!file)
956 return D3DERR_INVALIDCALL;
958 if (FAILED(map_view_of_file(file, &buffer, &size)))
959 return D3DXERR_INVALIDDATA;
961 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
962 UnmapViewOfFile(buffer);
964 return hr;
967 /************************************************************
968 * D3DXGetImageInfoFromResource
970 * RETURNS
971 * Success: D3D_OK, if resource is a valid image file
972 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
973 * if we fail to load resource
976 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, const char *resource, D3DXIMAGE_INFO *info)
978 HRSRC resinfo;
979 void *buffer;
980 DWORD size;
982 TRACE("module %p, resource %s, info %p.\n", module, debugstr_a(resource), info);
984 if (!(resinfo = FindResourceA(module, resource, (const char *)RT_RCDATA))
985 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
986 && !(resinfo = FindResourceA(module, resource, (const char *)RT_BITMAP)))
987 return D3DXERR_INVALIDDATA;
989 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
990 return D3DXERR_INVALIDDATA;
992 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
995 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, const WCHAR *resource, D3DXIMAGE_INFO *info)
997 HRSRC resinfo;
998 void *buffer;
999 DWORD size;
1001 TRACE("module %p, resource %s, info %p.\n", module, debugstr_w(resource), info);
1003 if (!(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA))
1004 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1005 && !(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_BITMAP)))
1006 return D3DXERR_INVALIDDATA;
1008 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
1009 return D3DXERR_INVALIDDATA;
1011 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
1014 /************************************************************
1015 * D3DXLoadSurfaceFromFileInMemory
1017 * Loads data from a given buffer into a surface and fills a given
1018 * D3DXIMAGE_INFO structure with info about the source data.
1020 * PARAMS
1021 * pDestSurface [I] pointer to the surface
1022 * pDestPalette [I] palette to use
1023 * pDestRect [I] to be filled area of the surface
1024 * pSrcData [I] pointer to the source data
1025 * SrcDataSize [I] size of the source data in bytes
1026 * pSrcRect [I] area of the source data to load
1027 * dwFilter [I] filter to apply on stretching
1028 * Colorkey [I] colorkey
1029 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1031 * RETURNS
1032 * Success: D3D_OK
1033 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
1034 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1037 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
1038 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
1039 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
1041 D3DXIMAGE_INFO imginfo;
1042 HRESULT hr, com_init;
1044 IWICImagingFactory *factory = NULL;
1045 IWICBitmapDecoder *decoder;
1046 IWICBitmapFrameDecode *bitmapframe;
1047 IWICStream *stream;
1049 const struct pixel_format_desc *formatdesc;
1050 WICRect wicrect;
1051 RECT rect;
1053 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1054 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1055 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
1056 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
1058 if (!pDestSurface || !pSrcData || !SrcDataSize)
1059 return D3DERR_INVALIDCALL;
1061 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
1063 if (FAILED(hr))
1064 return hr;
1066 if (pSrcRect)
1068 wicrect.X = pSrcRect->left;
1069 wicrect.Y = pSrcRect->top;
1070 wicrect.Width = pSrcRect->right - pSrcRect->left;
1071 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
1073 else
1075 wicrect.X = 0;
1076 wicrect.Y = 0;
1077 wicrect.Width = imginfo.Width;
1078 wicrect.Height = imginfo.Height;
1081 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
1083 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
1085 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
1086 dwFilter, Colorkey, &imginfo);
1087 if (SUCCEEDED(hr) && pSrcInfo)
1088 *pSrcInfo = imginfo;
1089 return hr;
1092 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1093 convert_dib_to_bmp((void**)&pSrcData, &SrcDataSize);
1095 com_init = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1097 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
1098 goto cleanup_err;
1100 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
1102 IWICImagingFactory_Release(factory);
1103 factory = NULL;
1104 goto cleanup_err;
1107 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
1109 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
1111 IWICStream_Release(stream);
1113 if (FAILED(hr))
1114 goto cleanup_err;
1116 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
1118 if (FAILED(hr))
1119 goto cleanup_bmp;
1121 formatdesc = get_format_info(imginfo.Format);
1123 if (formatdesc->type == FORMAT_UNKNOWN)
1125 FIXME("Unsupported pixel format\n");
1126 hr = D3DXERR_INVALIDDATA;
1128 else
1130 BYTE *buffer;
1131 DWORD pitch;
1132 PALETTEENTRY *palette = NULL;
1133 WICColor *colors = NULL;
1135 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
1136 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
1138 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
1139 pitch * wicrect.Height, buffer);
1141 if (SUCCEEDED(hr) && (formatdesc->type == FORMAT_INDEX))
1143 IWICPalette *wic_palette = NULL;
1144 UINT nb_colors;
1146 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
1147 if (SUCCEEDED(hr))
1148 hr = IWICBitmapFrameDecode_CopyPalette(bitmapframe, wic_palette);
1149 if (SUCCEEDED(hr))
1150 hr = IWICPalette_GetColorCount(wic_palette, &nb_colors);
1151 if (SUCCEEDED(hr))
1153 colors = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(colors[0]));
1154 palette = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(palette[0]));
1155 if (!colors || !palette)
1156 hr = E_OUTOFMEMORY;
1158 if (SUCCEEDED(hr))
1159 hr = IWICPalette_GetColors(wic_palette, nb_colors, colors, &nb_colors);
1160 if (SUCCEEDED(hr))
1162 UINT i;
1164 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1165 for (i = 0; i < nb_colors; i++)
1167 palette[i].peRed = (colors[i] >> 16) & 0xff;
1168 palette[i].peGreen = (colors[i] >> 8) & 0xff;
1169 palette[i].peBlue = colors[i] & 0xff;
1170 palette[i].peFlags = (colors[i] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1173 if (wic_palette)
1174 IWICPalette_Release(wic_palette);
1177 if (SUCCEEDED(hr))
1179 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1180 buffer, imginfo.Format, pitch,
1181 palette, &rect, dwFilter, Colorkey);
1184 HeapFree(GetProcessHeap(), 0, colors);
1185 HeapFree(GetProcessHeap(), 0, palette);
1186 HeapFree(GetProcessHeap(), 0, buffer);
1189 IWICBitmapFrameDecode_Release(bitmapframe);
1191 cleanup_bmp:
1192 IWICBitmapDecoder_Release(decoder);
1194 cleanup_err:
1195 if (factory)
1196 IWICImagingFactory_Release(factory);
1198 if (SUCCEEDED(com_init))
1199 CoUninitialize();
1201 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1202 HeapFree(GetProcessHeap(), 0, (void*)pSrcData);
1204 if (FAILED(hr))
1205 return D3DXERR_INVALIDDATA;
1207 if (pSrcInfo)
1208 *pSrcInfo = imginfo;
1210 return D3D_OK;
1213 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
1214 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
1215 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1217 WCHAR *src_file_w;
1218 HRESULT hr;
1219 int strlength;
1221 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1222 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1223 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
1224 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1226 if (!src_file || !dst_surface)
1227 return D3DERR_INVALIDCALL;
1229 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
1230 src_file_w = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*src_file_w));
1231 MultiByteToWideChar(CP_ACP, 0, src_file, -1, src_file_w, strlength);
1233 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
1234 src_file_w, src_rect, filter, color_key, src_info);
1235 HeapFree(GetProcessHeap(), 0, src_file_w);
1237 return hr;
1240 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
1241 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
1242 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1244 UINT data_size;
1245 void *data;
1246 HRESULT hr;
1248 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1249 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1250 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
1251 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1253 if (!src_file || !dst_surface)
1254 return D3DERR_INVALIDCALL;
1256 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
1257 return D3DXERR_INVALIDDATA;
1259 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1260 data, data_size, src_rect, filter, color_key, src_info);
1261 UnmapViewOfFile(data);
1263 return hr;
1266 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
1267 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
1268 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1270 UINT data_size;
1271 HRSRC resinfo;
1272 void *data;
1274 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1275 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1276 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
1277 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1279 if (!dst_surface)
1280 return D3DERR_INVALIDCALL;
1282 if (!(resinfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA))
1283 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1284 && !(resinfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
1285 return D3DXERR_INVALIDDATA;
1287 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1288 return D3DXERR_INVALIDDATA;
1290 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1291 data, data_size, src_rect, filter, color_key, src_info);
1294 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
1295 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
1296 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1298 UINT data_size;
1299 HRSRC resinfo;
1300 void *data;
1302 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1303 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1304 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
1305 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1307 if (!dst_surface)
1308 return D3DERR_INVALIDCALL;
1310 if (!(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA))
1311 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1312 && !(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1313 return D3DXERR_INVALIDDATA;
1315 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1316 return D3DXERR_INVALIDDATA;
1318 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1319 data, data_size, src_rect, filter, color_key, src_info);
1323 /************************************************************
1324 * helper functions for D3DXLoadSurfaceFromMemory
1326 struct argb_conversion_info
1328 const struct pixel_format_desc *srcformat;
1329 const struct pixel_format_desc *destformat;
1330 DWORD srcshift[4], destshift[4];
1331 DWORD srcmask[4], destmask[4];
1332 BOOL process_channel[4];
1333 DWORD channelmask;
1336 static void init_argb_conversion_info(const struct pixel_format_desc *srcformat, const struct pixel_format_desc *destformat, struct argb_conversion_info *info)
1338 UINT i;
1339 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1340 info->channelmask = 0;
1342 info->srcformat = srcformat;
1343 info->destformat = destformat;
1345 for(i = 0;i < 4;i++) {
1346 /* srcshift is used to extract the _relevant_ components */
1347 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1349 /* destshift is used to move the components to the correct position */
1350 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1352 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1353 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1355 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1356 if(destformat->bits[i]) {
1357 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1358 else info->channelmask |= info->destmask[i];
1363 /************************************************************
1364 * get_relevant_argb_components
1366 * Extracts the relevant components from the source color and
1367 * drops the less significant bits if they aren't used by the destination format.
1369 static void get_relevant_argb_components(const struct argb_conversion_info *info, DWORD col, DWORD *out)
1371 UINT i = 0;
1372 for(;i < 4;i++)
1373 if(info->process_channel[i])
1374 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
1377 /************************************************************
1378 * make_argb_color
1380 * Recombines the output of get_relevant_argb_components and converts
1381 * it to the destination format.
1383 static DWORD make_argb_color(const struct argb_conversion_info *info, const DWORD *in)
1385 UINT i;
1386 DWORD val = 0;
1388 for(i = 0;i < 4;i++) {
1389 if(info->process_channel[i]) {
1390 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1391 signed int shift;
1392 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1393 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1396 val |= info->channelmask; /* new channels are set to their maximal value */
1397 return val;
1400 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */
1401 static void format_to_vec4(const struct pixel_format_desc *format, const BYTE *src, struct vec4 *dst)
1403 DWORD mask, tmp;
1404 unsigned int c;
1406 for (c = 0; c < 4; ++c)
1408 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1409 float *dst_component = (float *)dst + component_offsets[c];
1411 if (format->bits[c])
1413 mask = ~0u >> (32 - format->bits[c]);
1415 memcpy(&tmp, src + format->shift[c] / 8,
1416 min(sizeof(DWORD), (format->shift[c] % 8 + format->bits[c] + 7) / 8));
1418 if (format->type == FORMAT_ARGBF16)
1419 *dst_component = float_16_to_32(tmp);
1420 else if (format->type == FORMAT_ARGBF)
1421 *dst_component = *(float *)&tmp;
1422 else
1423 *dst_component = (float)((tmp >> format->shift[c] % 8) & mask) / mask;
1425 else
1426 *dst_component = 1.0f;
1430 /* It doesn't work for components bigger than 32 bits. */
1431 static void format_from_vec4(const struct pixel_format_desc *format, const struct vec4 *src, BYTE *dst)
1433 DWORD v, mask32;
1434 unsigned int c, i;
1436 memset(dst, 0, format->bytes_per_pixel);
1438 for (c = 0; c < 4; ++c)
1440 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1441 const float src_component = *((const float *)src + component_offsets[c]);
1443 if (!format->bits[c])
1444 continue;
1446 mask32 = ~0u >> (32 - format->bits[c]);
1448 if (format->type == FORMAT_ARGBF16)
1449 v = float_32_to_16(src_component);
1450 else if (format->type == FORMAT_ARGBF)
1451 v = *(DWORD *)&src_component;
1452 else
1453 v = (DWORD)(src_component * ((1 << format->bits[c]) - 1) + 0.5f);
1455 for (i = format->shift[c] / 8 * 8; i < format->shift[c] + format->bits[c]; i += 8)
1457 BYTE mask, byte;
1459 if (format->shift[c] > i)
1461 mask = mask32 << (format->shift[c] - i);
1462 byte = (v << (format->shift[c] - i)) & mask;
1464 else
1466 mask = mask32 >> (i - format->shift[c]);
1467 byte = (v >> (i - format->shift[c])) & mask;
1469 dst[i / 8] |= byte;
1474 /************************************************************
1475 * copy_pixels
1477 * Copies the source buffer to the destination buffer.
1478 * Works for any pixel format.
1479 * The source and the destination must be block-aligned.
1481 void copy_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
1482 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *size,
1483 const struct pixel_format_desc *format)
1485 UINT row, slice;
1486 BYTE *dst_addr;
1487 const BYTE *src_addr;
1488 UINT row_block_count = (size->width + format->block_width - 1) / format->block_width;
1489 UINT row_count = (size->height + format->block_height - 1) / format->block_height;
1491 for (slice = 0; slice < size->depth; slice++)
1493 src_addr = src + slice * src_slice_pitch;
1494 dst_addr = dst + slice * dst_slice_pitch;
1496 for (row = 0; row < row_count; row++)
1498 memcpy(dst_addr, src_addr, row_block_count * format->block_byte_count);
1499 src_addr += src_row_pitch;
1500 dst_addr += dst_row_pitch;
1505 /************************************************************
1506 * convert_argb_pixels
1508 * Copies the source buffer to the destination buffer, performing
1509 * any necessary format conversion and color keying.
1510 * Pixels outsize the source rect are blacked out.
1511 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1513 void convert_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1514 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1515 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1516 const PALETTEENTRY *palette)
1518 struct argb_conversion_info conv_info, ck_conv_info;
1519 const struct pixel_format_desc *ck_format = NULL;
1520 DWORD channels[4];
1521 UINT min_width, min_height, min_depth;
1522 UINT x, y, z;
1524 ZeroMemory(channels, sizeof(channels));
1525 init_argb_conversion_info(src_format, dst_format, &conv_info);
1527 min_width = min(src_size->width, dst_size->width);
1528 min_height = min(src_size->height, dst_size->height);
1529 min_depth = min(src_size->depth, dst_size->depth);
1531 if (color_key)
1533 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1534 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1535 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1538 for (z = 0; z < min_depth; z++) {
1539 const BYTE *src_slice_ptr = src + z * src_slice_pitch;
1540 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1542 for (y = 0; y < min_height; y++) {
1543 const BYTE *src_ptr = src_slice_ptr + y * src_row_pitch;
1544 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1546 for (x = 0; x < min_width; x++) {
1547 if (!src_format->to_rgba && !dst_format->from_rgba
1548 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1550 DWORD val;
1552 get_relevant_argb_components(&conv_info, *(DWORD *)src_ptr, channels);
1553 val = make_argb_color(&conv_info, channels);
1555 if (color_key)
1557 DWORD ck_pixel;
1559 get_relevant_argb_components(&ck_conv_info, *(DWORD *)src_ptr, channels);
1560 ck_pixel = make_argb_color(&ck_conv_info, channels);
1561 if (ck_pixel == color_key)
1562 val &= ~conv_info.destmask[0];
1564 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1566 else
1568 struct vec4 color, tmp;
1570 format_to_vec4(src_format, src_ptr, &color);
1571 if (src_format->to_rgba)
1572 src_format->to_rgba(&color, &tmp, palette);
1573 else
1574 tmp = color;
1576 if (ck_format)
1578 DWORD ck_pixel;
1580 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1581 if (ck_pixel == color_key)
1582 tmp.w = 0.0f;
1585 if (dst_format->from_rgba)
1586 dst_format->from_rgba(&tmp, &color);
1587 else
1588 color = tmp;
1590 format_from_vec4(dst_format, &color, dst_ptr);
1593 src_ptr += src_format->bytes_per_pixel;
1594 dst_ptr += dst_format->bytes_per_pixel;
1597 if (src_size->width < dst_size->width) /* black out remaining pixels */
1598 memset(dst_ptr, 0, dst_format->bytes_per_pixel * (dst_size->width - src_size->width));
1601 if (src_size->height < dst_size->height) /* black out remaining pixels */
1602 memset(dst + src_size->height * dst_row_pitch, 0, dst_row_pitch * (dst_size->height - src_size->height));
1604 if (src_size->depth < dst_size->depth) /* black out remaining pixels */
1605 memset(dst + src_size->depth * dst_slice_pitch, 0, dst_slice_pitch * (dst_size->depth - src_size->depth));
1608 /************************************************************
1609 * point_filter_argb_pixels
1611 * Copies the source buffer to the destination buffer, performing
1612 * any necessary format conversion, color keying and stretching
1613 * using a point filter.
1614 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1616 void point_filter_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1617 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1618 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1619 const PALETTEENTRY *palette)
1621 struct argb_conversion_info conv_info, ck_conv_info;
1622 const struct pixel_format_desc *ck_format = NULL;
1623 DWORD channels[4];
1624 UINT x, y, z;
1626 ZeroMemory(channels, sizeof(channels));
1627 init_argb_conversion_info(src_format, dst_format, &conv_info);
1629 if (color_key)
1631 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1632 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1633 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1636 for (z = 0; z < dst_size->depth; z++)
1638 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1639 const BYTE *src_slice_ptr = src + src_slice_pitch * (z * src_size->depth / dst_size->depth);
1641 for (y = 0; y < dst_size->height; y++)
1643 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1644 const BYTE *src_row_ptr = src_slice_ptr + src_row_pitch * (y * src_size->height / dst_size->height);
1646 for (x = 0; x < dst_size->width; x++)
1648 const BYTE *src_ptr = src_row_ptr + (x * src_size->width / dst_size->width) * src_format->bytes_per_pixel;
1650 if (!src_format->to_rgba && !dst_format->from_rgba
1651 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1653 DWORD val;
1655 get_relevant_argb_components(&conv_info, *(DWORD *)src_ptr, channels);
1656 val = make_argb_color(&conv_info, channels);
1658 if (color_key)
1660 DWORD ck_pixel;
1662 get_relevant_argb_components(&ck_conv_info, *(DWORD *)src_ptr, channels);
1663 ck_pixel = make_argb_color(&ck_conv_info, channels);
1664 if (ck_pixel == color_key)
1665 val &= ~conv_info.destmask[0];
1667 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1669 else
1671 struct vec4 color, tmp;
1673 format_to_vec4(src_format, src_ptr, &color);
1674 if (src_format->to_rgba)
1675 src_format->to_rgba(&color, &tmp, palette);
1676 else
1677 tmp = color;
1679 if (ck_format)
1681 DWORD ck_pixel;
1683 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1684 if (ck_pixel == color_key)
1685 tmp.w = 0.0f;
1688 if (dst_format->from_rgba)
1689 dst_format->from_rgba(&tmp, &color);
1690 else
1691 color = tmp;
1693 format_from_vec4(dst_format, &color, dst_ptr);
1696 dst_ptr += dst_format->bytes_per_pixel;
1702 /************************************************************
1703 * D3DXLoadSurfaceFromMemory
1705 * Loads data from a given memory chunk into a surface,
1706 * applying any of the specified filters.
1708 * PARAMS
1709 * pDestSurface [I] pointer to the surface
1710 * pDestPalette [I] palette to use
1711 * pDestRect [I] to be filled area of the surface
1712 * pSrcMemory [I] pointer to the source data
1713 * SrcFormat [I] format of the source pixel data
1714 * SrcPitch [I] number of bytes in a row
1715 * pSrcPalette [I] palette used in the source image
1716 * pSrcRect [I] area of the source data to load
1717 * dwFilter [I] filter to apply on stretching
1718 * Colorkey [I] colorkey
1720 * RETURNS
1721 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1722 * if pSrcMemory is NULL but the other parameters are valid
1723 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1724 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1725 * if DestRect is invalid
1726 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1727 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1729 * NOTES
1730 * pSrcRect specifies the dimensions of the source data;
1731 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1734 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1735 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1736 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1737 DWORD filter, D3DCOLOR color_key)
1739 const struct pixel_format_desc *srcformatdesc, *destformatdesc;
1740 D3DSURFACE_DESC surfdesc;
1741 D3DLOCKED_RECT lockrect;
1742 struct volume src_size, dst_size;
1744 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1745 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1746 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1748 if (!dst_surface || !src_memory || !src_rect)
1750 WARN("Invalid argument specified.\n");
1751 return D3DERR_INVALIDCALL;
1753 if (src_format == D3DFMT_UNKNOWN
1754 || src_rect->left >= src_rect->right
1755 || src_rect->top >= src_rect->bottom)
1757 WARN("Invalid src_format or src_rect.\n");
1758 return E_FAIL;
1761 if (filter == D3DX_DEFAULT)
1762 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1764 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1766 src_size.width = src_rect->right - src_rect->left;
1767 src_size.height = src_rect->bottom - src_rect->top;
1768 src_size.depth = 1;
1769 if (!dst_rect)
1771 dst_size.width = surfdesc.Width;
1772 dst_size.height = surfdesc.Height;
1774 else
1776 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width
1777 || dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height
1778 || dst_rect->left < 0 || dst_rect->top < 0)
1780 WARN("Invalid dst_rect specified.\n");
1781 return D3DERR_INVALIDCALL;
1783 dst_size.width = dst_rect->right - dst_rect->left;
1784 dst_size.height = dst_rect->bottom - dst_rect->top;
1785 if (!dst_size.width || !dst_size.height)
1786 return D3D_OK;
1788 dst_size.depth = 1;
1790 srcformatdesc = get_format_info(src_format);
1791 destformatdesc = get_format_info(surfdesc.Format);
1792 if (srcformatdesc->type == FORMAT_UNKNOWN || destformatdesc->type == FORMAT_UNKNOWN)
1794 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format, surfdesc.Format);
1795 return E_NOTIMPL;
1798 if (src_format == surfdesc.Format
1799 && dst_size.width == src_size.width
1800 && dst_size.height == src_size.height
1801 && color_key == 0) /* Simple copy. */
1803 if (src_rect->left & (srcformatdesc->block_width - 1)
1804 || src_rect->top & (srcformatdesc->block_height - 1)
1805 || (src_rect->right & (srcformatdesc->block_width - 1)
1806 && src_size.width != surfdesc.Width)
1807 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1808 && src_size.height != surfdesc.Height))
1810 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1811 return D3DXERR_INVALIDDATA;
1814 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1815 return D3DXERR_INVALIDDATA;
1817 copy_pixels(src_memory, src_pitch, 0, lockrect.pBits, lockrect.Pitch, 0,
1818 &src_size, srcformatdesc);
1820 IDirect3DSurface9_UnlockRect(dst_surface);
1822 else /* Stretching or format conversion. */
1824 if (((srcformatdesc->type != FORMAT_ARGB) && (srcformatdesc->type != FORMAT_INDEX)) ||
1825 (destformatdesc->type != FORMAT_ARGB))
1827 FIXME("Format conversion missing %#x -> %#x\n", src_format, surfdesc.Format);
1828 return E_NOTIMPL;
1831 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1832 return D3DXERR_INVALIDDATA;
1834 if ((filter & 0xf) == D3DX_FILTER_NONE)
1836 convert_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1837 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
1839 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1841 if ((filter & 0xf) != D3DX_FILTER_POINT)
1842 FIXME("Unhandled filter %#x.\n", filter);
1844 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1845 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1846 point_filter_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1847 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
1850 IDirect3DSurface9_UnlockRect(dst_surface);
1853 return D3D_OK;
1856 /************************************************************
1857 * D3DXLoadSurfaceFromSurface
1859 * Copies the contents from one surface to another, performing any required
1860 * format conversion, resizing or filtering.
1862 * PARAMS
1863 * pDestSurface [I] pointer to the destination surface
1864 * pDestPalette [I] palette to use
1865 * pDestRect [I] to be filled area of the surface
1866 * pSrcSurface [I] pointer to the source surface
1867 * pSrcPalette [I] palette used for the source surface
1868 * pSrcRect [I] area of the source data to load
1869 * dwFilter [I] filter to apply on resizing
1870 * Colorkey [I] any ARGB value or 0 to disable color-keying
1872 * RETURNS
1873 * Success: D3D_OK
1874 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1875 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1878 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
1879 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
1880 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
1882 RECT rect;
1883 D3DLOCKED_RECT lock;
1884 D3DSURFACE_DESC SrcDesc;
1885 HRESULT hr;
1887 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1888 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1889 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
1890 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1892 if (!dst_surface || !src_surface)
1893 return D3DERR_INVALIDCALL;
1895 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
1897 if (!src_rect)
1898 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1899 else
1900 rect = *src_rect;
1902 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
1903 return D3DXERR_INVALIDDATA;
1905 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
1906 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
1908 IDirect3DSurface9_UnlockRect(src_surface);
1910 return hr;
1914 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1915 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1917 int len;
1918 WCHAR *filename;
1919 HRESULT hr;
1920 ID3DXBuffer *buffer;
1922 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1923 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1925 if (!dst_filename) return D3DERR_INVALIDCALL;
1927 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1928 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1929 if (!filename) return E_OUTOFMEMORY;
1930 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1932 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1933 if (SUCCEEDED(hr))
1935 hr = write_buffer_to_file(filename, buffer);
1936 ID3DXBuffer_Release(buffer);
1939 HeapFree(GetProcessHeap(), 0, filename);
1940 return hr;
1943 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1944 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1946 HRESULT hr;
1947 ID3DXBuffer *buffer;
1949 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1950 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1952 if (!dst_filename) return D3DERR_INVALIDCALL;
1954 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1955 if (SUCCEEDED(hr))
1957 hr = write_buffer_to_file(dst_filename, buffer);
1958 ID3DXBuffer_Release(buffer);
1961 return hr;
1964 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1965 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1967 IWICBitmapEncoder *encoder = NULL;
1968 IWICBitmapFrameEncode *frame = NULL;
1969 IPropertyBag2 *encoder_options = NULL;
1970 IStream *stream = NULL;
1971 HRESULT hr;
1972 HRESULT initresult;
1973 const CLSID *encoder_clsid;
1974 const GUID *pixel_format_guid;
1975 WICPixelFormatGUID wic_pixel_format;
1976 D3DFORMAT d3d_pixel_format;
1977 D3DSURFACE_DESC src_surface_desc;
1978 D3DLOCKED_RECT locked_rect;
1979 int width, height;
1980 STATSTG stream_stats;
1981 HGLOBAL stream_hglobal;
1982 ID3DXBuffer *buffer;
1983 DWORD size;
1985 TRACE("(%p, %#x, %p, %p, %s)\n",
1986 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1988 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
1990 if (src_palette)
1992 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1993 return D3DERR_INVALIDCALL;
1996 switch (file_format)
1998 case D3DXIFF_BMP:
1999 case D3DXIFF_DIB:
2000 encoder_clsid = &CLSID_WICBmpEncoder;
2001 break;
2002 case D3DXIFF_PNG:
2003 encoder_clsid = &CLSID_WICPngEncoder;
2004 break;
2005 case D3DXIFF_JPG:
2006 encoder_clsid = &CLSID_WICJpegEncoder;
2007 break;
2008 case D3DXIFF_DDS:
2009 return save_dds_surface_to_memory(dst_buffer, src_surface, src_rect);
2010 case D3DXIFF_HDR:
2011 case D3DXIFF_PFM:
2012 case D3DXIFF_TGA:
2013 case D3DXIFF_PPM:
2014 FIXME("File format %#x is not supported yet\n", file_format);
2015 return E_NOTIMPL;
2016 default:
2017 return D3DERR_INVALIDCALL;
2020 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
2021 if (src_rect)
2023 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
2025 WARN("Invalid rectangle with 0 area\n");
2026 return D3DXCreateBuffer(64, dst_buffer);
2028 if (src_rect->left < 0 || src_rect->top < 0)
2029 return D3DERR_INVALIDCALL;
2030 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
2031 return D3DERR_INVALIDCALL;
2032 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
2033 return D3DERR_INVALIDCALL;
2035 width = src_rect->right - src_rect->left;
2036 height = src_rect->bottom - src_rect->top;
2038 else
2040 width = src_surface_desc.Width;
2041 height = src_surface_desc.Height;
2044 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2046 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
2047 &IID_IWICBitmapEncoder, (void **)&encoder);
2048 if (FAILED(hr)) goto cleanup_err;
2050 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
2051 if (FAILED(hr)) goto cleanup_err;
2053 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2054 if (FAILED(hr)) goto cleanup_err;
2056 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
2057 if (FAILED(hr)) goto cleanup_err;
2059 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
2060 if (FAILED(hr)) goto cleanup_err;
2062 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
2063 if (FAILED(hr)) goto cleanup_err;
2065 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
2066 if (!pixel_format_guid)
2068 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
2069 hr = E_NOTIMPL;
2070 goto cleanup;
2073 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
2074 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
2075 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
2076 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
2078 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
2080 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
2082 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2083 if (SUCCEEDED(hr))
2085 IWICBitmapFrameEncode_WritePixels(frame, height,
2086 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
2087 IDirect3DSurface9_UnlockRect(src_surface);
2090 else /* Pixel format conversion */
2092 const struct pixel_format_desc *src_format_desc, *dst_format_desc;
2093 struct volume size;
2094 DWORD dst_pitch;
2095 void *dst_data;
2097 src_format_desc = get_format_info(src_surface_desc.Format);
2098 dst_format_desc = get_format_info(d3d_pixel_format);
2099 if (src_format_desc->type != FORMAT_ARGB || dst_format_desc->type != FORMAT_ARGB)
2101 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
2102 src_surface_desc.Format, d3d_pixel_format);
2103 hr = E_NOTIMPL;
2104 goto cleanup;
2107 size.width = width;
2108 size.height = height;
2109 size.depth = 1;
2110 dst_pitch = width * dst_format_desc->bytes_per_pixel;
2111 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
2112 if (!dst_data)
2114 hr = E_OUTOFMEMORY;
2115 goto cleanup;
2118 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2119 if (SUCCEEDED(hr))
2121 convert_argb_pixels(locked_rect.pBits, locked_rect.Pitch, 0, &size, src_format_desc,
2122 dst_data, dst_pitch, 0, &size, dst_format_desc, 0, NULL);
2123 IDirect3DSurface9_UnlockRect(src_surface);
2126 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
2127 HeapFree(GetProcessHeap(), 0, dst_data);
2130 hr = IWICBitmapFrameEncode_Commit(frame);
2131 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
2133 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
2135 /* copy data from stream to ID3DXBuffer */
2136 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
2137 if (FAILED(hr)) goto cleanup_err;
2139 if (stream_stats.cbSize.u.HighPart != 0)
2141 hr = D3DXERR_INVALIDDATA;
2142 goto cleanup;
2144 size = stream_stats.cbSize.u.LowPart;
2146 /* Remove BMP header for DIB */
2147 if (file_format == D3DXIFF_DIB)
2148 size -= sizeof(BITMAPFILEHEADER);
2150 hr = D3DXCreateBuffer(size, &buffer);
2151 if (FAILED(hr)) goto cleanup;
2153 hr = GetHGlobalFromStream(stream, &stream_hglobal);
2154 if (SUCCEEDED(hr))
2156 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
2157 void *stream_data = GlobalLock(stream_hglobal);
2158 /* Remove BMP header for DIB */
2159 if (file_format == D3DXIFF_DIB)
2160 stream_data = (void*)((BYTE*)stream_data + sizeof(BITMAPFILEHEADER));
2161 memcpy(buffer_pointer, stream_data, size);
2162 GlobalUnlock(stream_hglobal);
2163 *dst_buffer = buffer;
2165 else ID3DXBuffer_Release(buffer);
2167 cleanup_err:
2168 if (FAILED(hr) && hr != E_OUTOFMEMORY)
2169 hr = D3DERR_INVALIDCALL;
2171 cleanup:
2172 if (stream) IStream_Release(stream);
2174 if (frame) IWICBitmapFrameEncode_Release(frame);
2175 if (encoder_options) IPropertyBag2_Release(encoder_options);
2177 if (encoder) IWICBitmapEncoder_Release(encoder);
2179 if (SUCCEEDED(initresult)) CoUninitialize();
2181 return hr;