windowscodecs: Silence fixme for IID_CMetaBitmapRenderTarget.
[wine.git] / dlls / d3dx9_36 / surface.c
blobbbeba9c8b236c7d23bd14677ebe0e633fc4e5cdf
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
22 #include "d3dx9_private.h"
24 #include "initguid.h"
25 #include "ole2.h"
26 #include "wincodec.h"
28 #include "txc_dxtn.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
32 HRESULT WINAPI WICCreateImagingFactory_Proxy(UINT, IWICImagingFactory**);
34 /* Wine-specific WIC GUIDs */
35 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
37 static const struct
39 const GUID *wic_guid;
40 D3DFORMAT d3dformat;
41 } wic_pixel_formats[] = {
42 { &GUID_WICPixelFormat8bppIndexed, D3DFMT_P8 },
43 { &GUID_WICPixelFormat1bppIndexed, D3DFMT_P8 },
44 { &GUID_WICPixelFormat4bppIndexed, D3DFMT_P8 },
45 { &GUID_WICPixelFormat8bppGray, D3DFMT_L8 },
46 { &GUID_WICPixelFormat16bppBGR555, D3DFMT_X1R5G5B5 },
47 { &GUID_WICPixelFormat16bppBGR565, D3DFMT_R5G6B5 },
48 { &GUID_WICPixelFormat24bppBGR, D3DFMT_R8G8B8 },
49 { &GUID_WICPixelFormat32bppBGR, D3DFMT_X8R8G8B8 },
50 { &GUID_WICPixelFormat32bppBGRA, D3DFMT_A8R8G8B8 }
53 static D3DFORMAT wic_guid_to_d3dformat(const GUID *guid)
55 unsigned int i;
57 for (i = 0; i < ARRAY_SIZE(wic_pixel_formats); i++)
59 if (IsEqualGUID(wic_pixel_formats[i].wic_guid, guid))
60 return wic_pixel_formats[i].d3dformat;
63 return D3DFMT_UNKNOWN;
66 static const GUID *d3dformat_to_wic_guid(D3DFORMAT format)
68 unsigned int i;
70 for (i = 0; i < ARRAY_SIZE(wic_pixel_formats); i++)
72 if (wic_pixel_formats[i].d3dformat == format)
73 return wic_pixel_formats[i].wic_guid;
76 return NULL;
79 /* dds_header.flags */
80 #define DDS_CAPS 0x1
81 #define DDS_HEIGHT 0x2
82 #define DDS_WIDTH 0x4
83 #define DDS_PITCH 0x8
84 #define DDS_PIXELFORMAT 0x1000
85 #define DDS_MIPMAPCOUNT 0x20000
86 #define DDS_LINEARSIZE 0x80000
87 #define DDS_DEPTH 0x800000
89 /* dds_header.caps */
90 #define DDS_CAPS_COMPLEX 0x8
91 #define DDS_CAPS_TEXTURE 0x1000
92 #define DDS_CAPS_MIPMAP 0x400000
94 /* dds_header.caps2 */
95 #define DDS_CAPS2_CUBEMAP 0x200
96 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
97 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
98 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
99 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
100 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
101 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
102 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
103 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
104 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
105 #define DDS_CAPS2_VOLUME 0x200000
107 /* dds_pixel_format.flags */
108 #define DDS_PF_ALPHA 0x1
109 #define DDS_PF_ALPHA_ONLY 0x2
110 #define DDS_PF_FOURCC 0x4
111 #define DDS_PF_INDEXED 0x20
112 #define DDS_PF_RGB 0x40
113 #define DDS_PF_YUV 0x200
114 #define DDS_PF_LUMINANCE 0x20000
115 #define DDS_PF_BUMPLUMINANCE 0x40000
116 #define DDS_PF_BUMPDUDV 0x80000
118 struct dds_pixel_format
120 DWORD size;
121 DWORD flags;
122 DWORD fourcc;
123 DWORD bpp;
124 DWORD rmask;
125 DWORD gmask;
126 DWORD bmask;
127 DWORD amask;
130 struct dds_header
132 DWORD signature;
133 DWORD size;
134 DWORD flags;
135 DWORD height;
136 DWORD width;
137 DWORD pitch_or_linear_size;
138 DWORD depth;
139 DWORD miplevels;
140 DWORD reserved[11];
141 struct dds_pixel_format pixel_format;
142 DWORD caps;
143 DWORD caps2;
144 DWORD caps3;
145 DWORD caps4;
146 DWORD reserved2;
149 static D3DFORMAT dds_fourcc_to_d3dformat(DWORD fourcc)
151 unsigned int i;
152 static const DWORD known_fourcc[] = {
153 D3DFMT_UYVY,
154 D3DFMT_YUY2,
155 D3DFMT_R8G8_B8G8,
156 D3DFMT_G8R8_G8B8,
157 D3DFMT_DXT1,
158 D3DFMT_DXT2,
159 D3DFMT_DXT3,
160 D3DFMT_DXT4,
161 D3DFMT_DXT5,
162 D3DFMT_R16F,
163 D3DFMT_G16R16F,
164 D3DFMT_A16B16G16R16F,
165 D3DFMT_R32F,
166 D3DFMT_G32R32F,
167 D3DFMT_A32B32G32R32F,
170 for (i = 0; i < ARRAY_SIZE(known_fourcc); i++)
172 if (known_fourcc[i] == fourcc)
173 return fourcc;
176 WARN("Unknown FourCC %#lx.\n", fourcc);
177 return D3DFMT_UNKNOWN;
180 static const struct {
181 DWORD bpp;
182 DWORD rmask;
183 DWORD gmask;
184 DWORD bmask;
185 DWORD amask;
186 D3DFORMAT format;
187 } rgb_pixel_formats[] = {
188 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 },
189 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 },
190 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 },
191 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 },
192 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 },
193 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 },
194 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 },
195 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 },
196 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 },
197 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 },
198 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 },
199 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 },
200 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 },
201 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8 },
202 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8 },
205 HRESULT lock_surface(IDirect3DSurface9 *surface, const RECT *surface_rect, D3DLOCKED_RECT *lock,
206 IDirect3DSurface9 **temp_surface, BOOL write)
208 unsigned int width, height;
209 IDirect3DDevice9 *device;
210 D3DSURFACE_DESC desc;
211 DWORD lock_flag;
212 HRESULT hr;
214 lock_flag = write ? 0 : D3DLOCK_READONLY;
215 *temp_surface = NULL;
216 if (FAILED(hr = IDirect3DSurface9_LockRect(surface, lock, surface_rect, lock_flag)))
218 IDirect3DSurface9_GetDevice(surface, &device);
219 IDirect3DSurface9_GetDesc(surface, &desc);
221 if (surface_rect)
223 width = surface_rect->right - surface_rect->left;
224 height = surface_rect->bottom - surface_rect->top;
226 else
228 width = desc.Width;
229 height = desc.Height;
232 hr = write ? IDirect3DDevice9_CreateOffscreenPlainSurface(device, width, height,
233 desc.Format, D3DPOOL_SYSTEMMEM, temp_surface, NULL)
234 : IDirect3DDevice9_CreateRenderTarget(device, width, height,
235 desc.Format, D3DMULTISAMPLE_NONE, 0, TRUE, temp_surface, NULL);
236 if (FAILED(hr))
238 WARN("Failed to create temporary surface, surface %p, format %#x, "
239 "usage %#lx, pool %#x, write %#x, width %u, height %u.\n",
240 surface, desc.Format, desc.Usage, desc.Pool, write, width, height);
241 IDirect3DDevice9_Release(device);
242 return hr;
245 if (write || SUCCEEDED(hr = IDirect3DDevice9_StretchRect(device, surface, surface_rect,
246 *temp_surface, NULL, D3DTEXF_NONE)))
247 hr = IDirect3DSurface9_LockRect(*temp_surface, lock, NULL, lock_flag);
249 IDirect3DDevice9_Release(device);
250 if (FAILED(hr))
252 WARN("Failed to lock surface %p, write %#x, usage %#lx, pool %#x.\n",
253 surface, write, desc.Usage, desc.Pool);
254 IDirect3DSurface9_Release(*temp_surface);
255 *temp_surface = NULL;
256 return hr;
258 TRACE("Created temporary surface %p.\n", surface);
260 return hr;
263 HRESULT unlock_surface(IDirect3DSurface9 *surface, const RECT *surface_rect,
264 IDirect3DSurface9 *temp_surface, BOOL update)
266 IDirect3DDevice9 *device;
267 POINT surface_point;
268 HRESULT hr;
270 if (!temp_surface)
272 hr = IDirect3DSurface9_UnlockRect(surface);
273 return hr;
276 hr = IDirect3DSurface9_UnlockRect(temp_surface);
277 if (update)
279 if (surface_rect)
281 surface_point.x = surface_rect->left;
282 surface_point.y = surface_rect->top;
284 else
286 surface_point.x = 0;
287 surface_point.y = 0;
289 IDirect3DSurface9_GetDevice(surface, &device);
290 if (FAILED(hr = IDirect3DDevice9_UpdateSurface(device, temp_surface, NULL, surface, &surface_point)))
291 WARN("Updating surface failed, hr %#lx, surface %p, temp_surface %p.\n",
292 hr, surface, temp_surface);
293 IDirect3DDevice9_Release(device);
295 IDirect3DSurface9_Release(temp_surface);
296 return hr;
299 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
301 unsigned int i;
303 for (i = 0; i < ARRAY_SIZE(rgb_pixel_formats); i++)
305 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
306 && rgb_pixel_formats[i].rmask == pixel_format->rmask
307 && rgb_pixel_formats[i].gmask == pixel_format->gmask
308 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
310 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
311 return rgb_pixel_formats[i].format;
312 if (rgb_pixel_formats[i].amask == 0)
313 return rgb_pixel_formats[i].format;
317 WARN("Unknown RGB pixel format (r %#lx, g %#lx, b %#lx, a %#lx).\n",
318 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
319 return D3DFMT_UNKNOWN;
322 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
324 if (pixel_format->bpp == 8)
326 if (pixel_format->rmask == 0xff)
327 return D3DFMT_L8;
328 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
329 return D3DFMT_A4L4;
331 if (pixel_format->bpp == 16)
333 if (pixel_format->rmask == 0xffff)
334 return D3DFMT_L16;
335 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
336 return D3DFMT_A8L8;
339 WARN("Unknown luminance pixel format (bpp %lu, l %#lx, a %#lx).\n",
340 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
341 return D3DFMT_UNKNOWN;
344 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
346 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
347 return D3DFMT_A8;
349 WARN("Unknown alpha pixel format (bpp %lu, a %#lx).\n", pixel_format->bpp, pixel_format->rmask);
350 return D3DFMT_UNKNOWN;
353 static D3DFORMAT dds_indexed_to_d3dformat(const struct dds_pixel_format *pixel_format)
355 if (pixel_format->bpp == 8)
356 return D3DFMT_P8;
358 WARN("Unknown indexed pixel format (bpp %lu).\n", pixel_format->bpp);
359 return D3DFMT_UNKNOWN;
362 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
364 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00)
365 return D3DFMT_V8U8;
366 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000)
367 return D3DFMT_V16U16;
369 WARN("Unknown bump pixel format (bpp %lu, r %#lx, g %#lx, b %#lx, a %#lx).\n", pixel_format->bpp,
370 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
371 return D3DFMT_UNKNOWN;
374 static D3DFORMAT dds_bump_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
376 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x000000ff && pixel_format->gmask == 0x0000ff00
377 && pixel_format->bmask == 0x00ff0000)
378 return D3DFMT_X8L8V8U8;
380 WARN("Unknown bump pixel format (bpp %lu, r %#lx, g %#lx, b %#lx, a %#lx).\n", pixel_format->bpp,
381 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
382 return D3DFMT_UNKNOWN;
385 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
387 TRACE("pixel_format: size %lu, flags %#lx, fourcc %#lx, bpp %lu.\n", pixel_format->size,
388 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp);
389 TRACE("rmask %#lx, gmask %#lx, bmask %#lx, amask %#lx.\n", pixel_format->rmask, pixel_format->gmask,
390 pixel_format->bmask, pixel_format->amask);
392 if (pixel_format->flags & DDS_PF_FOURCC)
393 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
394 if (pixel_format->flags & DDS_PF_INDEXED)
395 return dds_indexed_to_d3dformat(pixel_format);
396 if (pixel_format->flags & DDS_PF_RGB)
397 return dds_rgb_to_d3dformat(pixel_format);
398 if (pixel_format->flags & DDS_PF_LUMINANCE)
399 return dds_luminance_to_d3dformat(pixel_format);
400 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
401 return dds_alpha_to_d3dformat(pixel_format);
402 if (pixel_format->flags & DDS_PF_BUMPDUDV)
403 return dds_bump_to_d3dformat(pixel_format);
404 if (pixel_format->flags & DDS_PF_BUMPLUMINANCE)
405 return dds_bump_luminance_to_d3dformat(pixel_format);
407 WARN("Unknown pixel format (flags %#lx, fourcc %#lx, bpp %lu, r %#lx, g %#lx, b %#lx, a %#lx).\n",
408 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
409 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
410 return D3DFMT_UNKNOWN;
413 static HRESULT d3dformat_to_dds_pixel_format(struct dds_pixel_format *pixel_format, D3DFORMAT d3dformat)
415 unsigned int i;
417 memset(pixel_format, 0, sizeof(*pixel_format));
419 pixel_format->size = sizeof(*pixel_format);
421 for (i = 0; i < ARRAY_SIZE(rgb_pixel_formats); i++)
423 if (rgb_pixel_formats[i].format == d3dformat)
425 pixel_format->flags |= DDS_PF_RGB;
426 pixel_format->bpp = rgb_pixel_formats[i].bpp;
427 pixel_format->rmask = rgb_pixel_formats[i].rmask;
428 pixel_format->gmask = rgb_pixel_formats[i].gmask;
429 pixel_format->bmask = rgb_pixel_formats[i].bmask;
430 pixel_format->amask = rgb_pixel_formats[i].amask;
431 if (pixel_format->amask) pixel_format->flags |= DDS_PF_ALPHA;
432 return D3D_OK;
436 WARN("Unknown pixel format %#x.\n", d3dformat);
437 return E_NOTIMPL;
440 static HRESULT calculate_dds_surface_size(D3DFORMAT format, UINT width, UINT height,
441 UINT *pitch, UINT *size)
443 const struct pixel_format_desc *format_desc = get_format_info(format);
444 if (format_desc->type == FORMAT_UNKNOWN)
445 return E_NOTIMPL;
447 if (format_desc->block_width != 1 || format_desc->block_height != 1)
449 *pitch = format_desc->block_byte_count
450 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
451 *size = *pitch
452 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
454 else
456 *pitch = width * format_desc->bytes_per_pixel;
457 *size = *pitch * height;
460 return D3D_OK;
463 static UINT calculate_dds_file_size(D3DFORMAT format, UINT width, UINT height, UINT depth,
464 UINT miplevels, UINT faces)
466 UINT i, file_size = 0;
468 for (i = 0; i < miplevels; i++)
470 UINT pitch, size = 0;
471 calculate_dds_surface_size(format, width, height, &pitch, &size);
472 size *= depth;
473 file_size += size;
474 width = max(1, width / 2);
475 height = max(1, height / 2);
476 depth = max(1, depth / 2);
479 file_size *= faces;
480 file_size += sizeof(struct dds_header);
481 return file_size;
484 /************************************************************
485 * get_image_info_from_dds
487 * Fills a D3DXIMAGE_INFO structure with information
488 * about a DDS file stored in the memory.
490 * PARAMS
491 * buffer [I] pointer to DDS data
492 * length [I] size of DDS data
493 * info [O] pointer to D3DXIMAGE_INFO structure
495 * RETURNS
496 * Success: D3D_OK
497 * Failure: D3DXERR_INVALIDDATA
500 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
502 UINT faces = 1;
503 UINT expected_length;
504 const struct dds_header *header = buffer;
506 if (length < sizeof(*header) || !info)
507 return D3DXERR_INVALIDDATA;
509 if (header->pixel_format.size != sizeof(header->pixel_format))
510 return D3DXERR_INVALIDDATA;
512 info->Width = header->width;
513 info->Height = header->height;
514 info->Depth = 1;
515 info->MipLevels = header->miplevels ? header->miplevels : 1;
517 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
518 if (info->Format == D3DFMT_UNKNOWN)
519 return D3DXERR_INVALIDDATA;
521 TRACE("Pixel format is %#x\n", info->Format);
523 if (header->caps2 & DDS_CAPS2_VOLUME)
525 info->Depth = header->depth;
526 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
528 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
530 DWORD face;
531 faces = 0;
532 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
534 if (header->caps2 & face)
535 faces++;
537 info->ResourceType = D3DRTYPE_CUBETEXTURE;
539 else
541 info->ResourceType = D3DRTYPE_TEXTURE;
544 expected_length = calculate_dds_file_size(info->Format, info->Width, info->Height, info->Depth,
545 info->MipLevels, faces);
546 if (length < expected_length)
548 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
549 return D3DXERR_INVALIDDATA;
552 info->ImageFileFormat = D3DXIFF_DDS;
553 return D3D_OK;
556 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
557 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
558 const D3DXIMAGE_INFO *src_info)
560 UINT size;
561 UINT src_pitch;
562 const struct dds_header *header = src_data;
563 const BYTE *pixels = (BYTE *)(header + 1);
565 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
566 return D3DXERR_INVALIDDATA;
568 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &src_pitch, &size)))
569 return E_NOTIMPL;
571 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
572 src_pitch, NULL, src_rect, filter, color_key);
575 static HRESULT save_dds_surface_to_memory(ID3DXBuffer **dst_buffer, IDirect3DSurface9 *src_surface, const RECT *src_rect)
577 HRESULT hr;
578 UINT dst_pitch, surface_size, file_size;
579 D3DSURFACE_DESC src_desc;
580 D3DLOCKED_RECT locked_rect;
581 ID3DXBuffer *buffer;
582 struct dds_header *header;
583 BYTE *pixels;
584 struct volume volume;
585 const struct pixel_format_desc *pixel_format;
586 IDirect3DSurface9 *temp_surface;
588 if (src_rect)
590 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
591 return E_NOTIMPL;
594 hr = IDirect3DSurface9_GetDesc(src_surface, &src_desc);
595 if (FAILED(hr)) return hr;
597 pixel_format = get_format_info(src_desc.Format);
598 if (pixel_format->type == FORMAT_UNKNOWN) return E_NOTIMPL;
600 file_size = calculate_dds_file_size(src_desc.Format, src_desc.Width, src_desc.Height, 1, 1, 1);
602 hr = calculate_dds_surface_size(src_desc.Format, src_desc.Width, src_desc.Height, &dst_pitch, &surface_size);
603 if (FAILED(hr)) return hr;
605 hr = D3DXCreateBuffer(file_size, &buffer);
606 if (FAILED(hr)) return hr;
608 header = ID3DXBuffer_GetBufferPointer(buffer);
609 pixels = (BYTE *)(header + 1);
611 memset(header, 0, sizeof(*header));
612 header->signature = MAKEFOURCC('D','D','S',' ');
613 /* The signature is not really part of the DDS header */
614 header->size = sizeof(*header) - FIELD_OFFSET(struct dds_header, size);
615 header->flags = DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PIXELFORMAT;
616 header->height = src_desc.Height;
617 header->width = src_desc.Width;
618 header->caps = DDS_CAPS_TEXTURE;
619 hr = d3dformat_to_dds_pixel_format(&header->pixel_format, src_desc.Format);
620 if (FAILED(hr))
622 ID3DXBuffer_Release(buffer);
623 return hr;
626 hr = lock_surface(src_surface, NULL, &locked_rect, &temp_surface, FALSE);
627 if (FAILED(hr))
629 ID3DXBuffer_Release(buffer);
630 return hr;
633 volume.width = src_desc.Width;
634 volume.height = src_desc.Height;
635 volume.depth = 1;
636 copy_pixels(locked_rect.pBits, locked_rect.Pitch, 0, pixels, dst_pitch, 0,
637 &volume, pixel_format);
639 unlock_surface(src_surface, NULL, temp_surface, FALSE);
641 *dst_buffer = buffer;
642 return D3D_OK;
645 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette,
646 const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key,
647 const D3DXIMAGE_INFO *src_info)
649 UINT row_pitch, slice_pitch;
650 const struct dds_header *header = src_data;
651 const BYTE *pixels = (BYTE *)(header + 1);
653 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
654 return D3DXERR_INVALIDDATA;
656 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &row_pitch, &slice_pitch)))
657 return E_NOTIMPL;
659 return D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box, pixels, src_info->Format,
660 row_pitch, slice_pitch, NULL, src_box, filter, color_key);
663 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
664 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info, unsigned int skip_levels,
665 unsigned int *loaded_miplevels)
667 HRESULT hr;
668 RECT src_rect;
669 UINT src_pitch;
670 UINT mip_level;
671 UINT mip_levels;
672 UINT mip_level_size;
673 UINT width, height;
674 IDirect3DSurface9 *surface;
675 const struct dds_header *header = src_data;
676 const BYTE *pixels = (BYTE *)(header + 1);
678 /* Loading a cube texture as a simple texture is also supported
679 * (only first face texture is taken). Same with volume textures. */
680 if ((src_info->ResourceType != D3DRTYPE_TEXTURE)
681 && (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
682 && (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE))
684 WARN("Trying to load a %u resource as a 2D texture, returning failure.\n", src_info->ResourceType);
685 return D3DXERR_INVALIDDATA;
688 width = src_info->Width;
689 height = src_info->Height;
690 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
691 if (src_info->ResourceType == D3DRTYPE_VOLUMETEXTURE)
692 mip_levels = 1;
693 for (mip_level = 0; mip_level < mip_levels + skip_levels; ++mip_level)
695 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_pitch, &mip_level_size);
696 if (FAILED(hr)) return hr;
698 if (mip_level >= skip_levels)
700 SetRect(&src_rect, 0, 0, width, height);
702 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level - skip_levels, &surface);
703 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
704 NULL, &src_rect, filter, color_key);
705 IDirect3DSurface9_Release(surface);
706 if (FAILED(hr))
707 return hr;
710 pixels += mip_level_size;
711 width = max(1, width / 2);
712 height = max(1, height / 2);
715 *loaded_miplevels = mip_levels - skip_levels;
717 return D3D_OK;
720 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
721 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
723 HRESULT hr;
724 int face;
725 UINT mip_level;
726 UINT size;
727 RECT src_rect;
728 UINT src_pitch;
729 UINT mip_levels;
730 UINT mip_level_size;
731 IDirect3DSurface9 *surface;
732 const struct dds_header *header = src_data;
733 const BYTE *pixels = (BYTE *)(header + 1);
735 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
736 return D3DXERR_INVALIDDATA;
738 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
740 WARN("Only full cubemaps are supported\n");
741 return D3DXERR_INVALIDDATA;
744 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
745 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
747 size = src_info->Width;
748 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
750 hr = calculate_dds_surface_size(src_info->Format, size, size, &src_pitch, &mip_level_size);
751 if (FAILED(hr)) return hr;
753 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
754 if (mip_level < mip_levels)
756 SetRect(&src_rect, 0, 0, size, size);
758 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
759 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
760 NULL, &src_rect, filter, color_key);
761 IDirect3DSurface9_Release(surface);
762 if (FAILED(hr)) return hr;
765 pixels += mip_level_size;
766 size = max(1, size / 2);
770 return D3D_OK;
773 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
774 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
776 HRESULT hr;
777 UINT mip_level;
778 UINT mip_levels;
779 UINT src_slice_pitch;
780 UINT src_row_pitch;
781 D3DBOX src_box;
782 UINT width, height, depth;
783 IDirect3DVolume9 *volume;
784 const struct dds_header *header = src_data;
785 const BYTE *pixels = (BYTE *)(header + 1);
787 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
788 return D3DXERR_INVALIDDATA;
790 width = src_info->Width;
791 height = src_info->Height;
792 depth = src_info->Depth;
793 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture));
795 for (mip_level = 0; mip_level < mip_levels; mip_level++)
797 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_row_pitch, &src_slice_pitch);
798 if (FAILED(hr)) return hr;
800 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume);
801 if (FAILED(hr)) return hr;
803 src_box.Left = 0;
804 src_box.Top = 0;
805 src_box.Right = width;
806 src_box.Bottom = height;
807 src_box.Front = 0;
808 src_box.Back = depth;
810 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format,
811 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key);
813 IDirect3DVolume9_Release(volume);
814 if (FAILED(hr)) return hr;
816 pixels += depth * src_slice_pitch;
817 width = max(1, width / 2);
818 height = max(1, height / 2);
819 depth = max(1, depth / 2);
822 return D3D_OK;
825 static BOOL convert_dib_to_bmp(const void **data, unsigned int *size)
827 ULONG header_size;
828 ULONG count = 0;
829 ULONG offset;
830 BITMAPFILEHEADER *header;
831 BYTE *new_data;
832 UINT new_size;
834 if ((*size < 4) || (*size < (header_size = *(ULONG*)*data)))
835 return FALSE;
837 if ((header_size == sizeof(BITMAPINFOHEADER)) ||
838 (header_size == sizeof(BITMAPV4HEADER)) ||
839 (header_size == sizeof(BITMAPV5HEADER)) ||
840 (header_size == 64 /* sizeof(BITMAPCOREHEADER2) */))
842 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
843 BITMAPINFOHEADER *info_header = (BITMAPINFOHEADER*)*data;
844 count = info_header->biClrUsed;
846 if (!count && info_header->biBitCount <= 8)
847 count = 1 << info_header->biBitCount;
849 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBQUAD) * count;
851 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
852 if ((info_header->biSize == sizeof(BITMAPINFOHEADER)) && (info_header->biCompression == BI_BITFIELDS))
853 offset += 3 * sizeof(DWORD);
855 else if (header_size == sizeof(BITMAPCOREHEADER))
857 BITMAPCOREHEADER *core_header = (BITMAPCOREHEADER*)*data;
859 if (core_header->bcBitCount <= 8)
860 count = 1 << core_header->bcBitCount;
862 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBTRIPLE) * count;
864 else
866 return FALSE;
869 TRACE("Converting DIB file to BMP\n");
871 new_size = *size + sizeof(BITMAPFILEHEADER);
872 new_data = malloc(new_size);
873 CopyMemory(new_data + sizeof(BITMAPFILEHEADER), *data, *size);
875 /* Add BMP header */
876 header = (BITMAPFILEHEADER*)new_data;
877 header->bfType = 0x4d42; /* BM */
878 header->bfSize = new_size;
879 header->bfReserved1 = 0;
880 header->bfReserved2 = 0;
881 header->bfOffBits = offset;
883 /* Update input data */
884 *data = new_data;
885 *size = new_size;
887 return TRUE;
890 /* windowscodecs always returns xRGB, but we should return ARGB if and only if
891 * at least one pixel has a non-zero alpha component. */
892 static BOOL image_is_argb(IWICBitmapFrameDecode *frame, const D3DXIMAGE_INFO *info)
894 unsigned int size, i;
895 BYTE *buffer;
896 HRESULT hr;
898 if (info->Format != D3DFMT_X8R8G8B8 || (info->ImageFileFormat != D3DXIFF_BMP
899 && info->ImageFileFormat != D3DXIFF_TGA))
900 return FALSE;
902 size = info->Width * info->Height * 4;
903 if (!(buffer = malloc(size)))
904 return FALSE;
906 if (FAILED(hr = IWICBitmapFrameDecode_CopyPixels(frame, NULL, info->Width * 4, size, buffer)))
908 ERR("Failed to copy pixels, hr %#lx.\n", hr);
909 free(buffer);
910 return FALSE;
913 for (i = 0; i < info->Width * info->Height; ++i)
915 if (buffer[i * 4 + 3])
917 free(buffer);
918 return TRUE;
922 free(buffer);
923 return FALSE;
926 /************************************************************
927 * D3DXGetImageInfoFromFileInMemory
929 * Fills a D3DXIMAGE_INFO structure with info about an image
931 * PARAMS
932 * data [I] pointer to the image file data
933 * datasize [I] size of the passed data
934 * info [O] pointer to the destination structure
936 * RETURNS
937 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
938 * if info is NULL and data and datasize are not NULL
939 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
940 * D3DERR_INVALIDCALL, if data is NULL or
941 * if datasize is 0
943 * NOTES
944 * datasize may be bigger than the actual file size
947 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(const void *data, UINT datasize, D3DXIMAGE_INFO *info)
949 IWICImagingFactory *factory;
950 IWICBitmapDecoder *decoder = NULL;
951 IWICStream *stream;
952 HRESULT hr;
953 BOOL dib;
955 TRACE("(%p, %d, %p)\n", data, datasize, info);
957 if (!data || !datasize)
958 return D3DERR_INVALIDCALL;
960 if (!info)
961 return D3D_OK;
963 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
964 TRACE("File type is DDS\n");
965 return get_image_info_from_dds(data, datasize, info);
968 /* In case of DIB file, convert it to BMP */
969 dib = convert_dib_to_bmp(&data, &datasize);
971 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
973 if (SUCCEEDED(hr)) {
974 IWICImagingFactory_CreateStream(factory, &stream);
975 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
976 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
977 IWICStream_Release(stream);
978 IWICImagingFactory_Release(factory);
981 if (FAILED(hr)) {
982 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
983 FIXME("File type PPM is not supported yet\n");
984 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
985 FIXME("File type HDR is not supported yet\n");
986 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
987 FIXME("File type PFM is not supported yet\n");
990 if (SUCCEEDED(hr)) {
991 GUID container_format;
992 UINT frame_count;
994 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
995 if (SUCCEEDED(hr)) {
996 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
997 if (dib) {
998 TRACE("File type is DIB\n");
999 info->ImageFileFormat = D3DXIFF_DIB;
1000 } else {
1001 TRACE("File type is BMP\n");
1002 info->ImageFileFormat = D3DXIFF_BMP;
1004 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
1005 TRACE("File type is PNG\n");
1006 info->ImageFileFormat = D3DXIFF_PNG;
1007 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
1008 TRACE("File type is JPG\n");
1009 info->ImageFileFormat = D3DXIFF_JPG;
1010 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
1011 TRACE("File type is TGA\n");
1012 info->ImageFileFormat = D3DXIFF_TGA;
1013 } else {
1014 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
1015 hr = D3DXERR_INVALIDDATA;
1019 if (SUCCEEDED(hr))
1020 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
1021 if (SUCCEEDED(hr) && !frame_count)
1022 hr = D3DXERR_INVALIDDATA;
1024 if (SUCCEEDED(hr)) {
1025 IWICBitmapFrameDecode *frame = NULL;
1027 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
1029 if (SUCCEEDED(hr))
1030 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
1032 if (SUCCEEDED(hr)) {
1033 WICPixelFormatGUID pixel_format;
1035 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
1036 if (SUCCEEDED(hr)) {
1037 info->Format = wic_guid_to_d3dformat(&pixel_format);
1038 if (info->Format == D3DFMT_UNKNOWN) {
1039 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
1040 hr = D3DXERR_INVALIDDATA;
1045 if (SUCCEEDED(hr) && image_is_argb(frame, info))
1046 info->Format = D3DFMT_A8R8G8B8;
1048 if (frame)
1049 IWICBitmapFrameDecode_Release(frame);
1051 info->Depth = 1;
1052 info->MipLevels = 1;
1053 info->ResourceType = D3DRTYPE_TEXTURE;
1057 if (decoder)
1058 IWICBitmapDecoder_Release(decoder);
1060 if (dib)
1061 free((void*)data);
1063 if (FAILED(hr)) {
1064 TRACE("Invalid or unsupported image file\n");
1065 return D3DXERR_INVALIDDATA;
1068 return D3D_OK;
1071 /************************************************************
1072 * D3DXGetImageInfoFromFile
1074 * RETURNS
1075 * Success: D3D_OK, if we successfully load a valid image file or
1076 * if we successfully load a file which is no valid image and info is NULL
1077 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
1078 * if file is not a valid image file and info is not NULL
1079 * D3DERR_INVALIDCALL, if file is NULL
1082 HRESULT WINAPI D3DXGetImageInfoFromFileA(const char *file, D3DXIMAGE_INFO *info)
1084 WCHAR *widename;
1085 HRESULT hr;
1086 int strlength;
1088 TRACE("file %s, info %p.\n", debugstr_a(file), info);
1090 if( !file ) return D3DERR_INVALIDCALL;
1092 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
1093 widename = malloc(strlength * sizeof(*widename));
1094 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
1096 hr = D3DXGetImageInfoFromFileW(widename, info);
1097 free(widename);
1099 return hr;
1102 HRESULT WINAPI D3DXGetImageInfoFromFileW(const WCHAR *file, D3DXIMAGE_INFO *info)
1104 void *buffer;
1105 HRESULT hr;
1106 DWORD size;
1108 TRACE("file %s, info %p.\n", debugstr_w(file), info);
1110 if (!file)
1111 return D3DERR_INVALIDCALL;
1113 if (FAILED(map_view_of_file(file, &buffer, &size)))
1114 return D3DXERR_INVALIDDATA;
1116 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
1117 UnmapViewOfFile(buffer);
1119 return hr;
1122 /************************************************************
1123 * D3DXGetImageInfoFromResource
1125 * RETURNS
1126 * Success: D3D_OK, if resource is a valid image file
1127 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
1128 * if we fail to load resource
1131 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, const char *resource, D3DXIMAGE_INFO *info)
1133 HRSRC resinfo;
1134 void *buffer;
1135 DWORD size;
1137 TRACE("module %p, resource %s, info %p.\n", module, debugstr_a(resource), info);
1139 if (!(resinfo = FindResourceA(module, resource, (const char *)RT_RCDATA))
1140 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1141 && !(resinfo = FindResourceA(module, resource, (const char *)RT_BITMAP)))
1142 return D3DXERR_INVALIDDATA;
1144 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
1145 return D3DXERR_INVALIDDATA;
1147 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
1150 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, const WCHAR *resource, D3DXIMAGE_INFO *info)
1152 HRSRC resinfo;
1153 void *buffer;
1154 DWORD size;
1156 TRACE("module %p, resource %s, info %p.\n", module, debugstr_w(resource), info);
1158 if (!(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA))
1159 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1160 && !(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_BITMAP)))
1161 return D3DXERR_INVALIDDATA;
1163 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
1164 return D3DXERR_INVALIDDATA;
1166 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
1169 /************************************************************
1170 * D3DXLoadSurfaceFromFileInMemory
1172 * Loads data from a given buffer into a surface and fills a given
1173 * D3DXIMAGE_INFO structure with info about the source data.
1175 * PARAMS
1176 * pDestSurface [I] pointer to the surface
1177 * pDestPalette [I] palette to use
1178 * pDestRect [I] to be filled area of the surface
1179 * pSrcData [I] pointer to the source data
1180 * SrcDataSize [I] size of the source data in bytes
1181 * pSrcRect [I] area of the source data to load
1182 * dwFilter [I] filter to apply on stretching
1183 * Colorkey [I] colorkey
1184 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1186 * RETURNS
1187 * Success: D3D_OK
1188 * Failure: D3DERR_INVALIDCALL, if pDestSurface, pSrcData or SrcDataSize is NULL
1189 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1192 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
1193 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
1194 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
1196 D3DXIMAGE_INFO imginfo;
1197 HRESULT hr;
1199 IWICImagingFactory *factory = NULL;
1200 IWICBitmapDecoder *decoder;
1201 IWICBitmapFrameDecode *bitmapframe;
1202 IWICStream *stream;
1204 const struct pixel_format_desc *formatdesc;
1205 WICRect wicrect;
1206 RECT rect;
1208 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1209 "src_rect %s, filter %#lx, color_key 0x%08lx, src_info %p.\n",
1210 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
1211 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
1213 if (!pDestSurface || !pSrcData || !SrcDataSize)
1214 return D3DERR_INVALIDCALL;
1216 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
1218 if (FAILED(hr))
1219 return hr;
1221 if (pSrcRect)
1223 wicrect.X = pSrcRect->left;
1224 wicrect.Y = pSrcRect->top;
1225 wicrect.Width = pSrcRect->right - pSrcRect->left;
1226 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
1228 else
1230 wicrect.X = 0;
1231 wicrect.Y = 0;
1232 wicrect.Width = imginfo.Width;
1233 wicrect.Height = imginfo.Height;
1236 SetRect(&rect, wicrect.X, wicrect.Y, wicrect.X + wicrect.Width, wicrect.Y + wicrect.Height);
1238 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
1240 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
1241 dwFilter, Colorkey, &imginfo);
1242 if (SUCCEEDED(hr) && pSrcInfo)
1243 *pSrcInfo = imginfo;
1244 return hr;
1247 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1248 convert_dib_to_bmp(&pSrcData, &SrcDataSize);
1250 if (FAILED(WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory)))
1251 goto cleanup_err;
1253 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
1255 IWICImagingFactory_Release(factory);
1256 factory = NULL;
1257 goto cleanup_err;
1260 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
1262 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
1264 IWICStream_Release(stream);
1266 if (FAILED(hr))
1267 goto cleanup_err;
1269 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
1271 if (FAILED(hr))
1272 goto cleanup_bmp;
1274 formatdesc = get_format_info(imginfo.Format);
1276 if (formatdesc->type == FORMAT_UNKNOWN)
1278 FIXME("Unsupported pixel format\n");
1279 hr = D3DXERR_INVALIDDATA;
1281 else
1283 BYTE *buffer;
1284 DWORD pitch;
1285 PALETTEENTRY *palette = NULL;
1286 WICColor *colors = NULL;
1288 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
1289 buffer = malloc(pitch * wicrect.Height);
1291 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
1292 pitch * wicrect.Height, buffer);
1294 if (SUCCEEDED(hr) && (formatdesc->type == FORMAT_INDEX))
1296 IWICPalette *wic_palette = NULL;
1297 UINT nb_colors;
1299 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
1300 if (SUCCEEDED(hr))
1301 hr = IWICBitmapFrameDecode_CopyPalette(bitmapframe, wic_palette);
1302 if (SUCCEEDED(hr))
1303 hr = IWICPalette_GetColorCount(wic_palette, &nb_colors);
1304 if (SUCCEEDED(hr))
1306 colors = malloc(nb_colors * sizeof(colors[0]));
1307 palette = malloc(nb_colors * sizeof(palette[0]));
1308 if (!colors || !palette)
1309 hr = E_OUTOFMEMORY;
1311 if (SUCCEEDED(hr))
1312 hr = IWICPalette_GetColors(wic_palette, nb_colors, colors, &nb_colors);
1313 if (SUCCEEDED(hr))
1315 UINT i;
1317 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1318 for (i = 0; i < nb_colors; i++)
1320 palette[i].peRed = (colors[i] >> 16) & 0xff;
1321 palette[i].peGreen = (colors[i] >> 8) & 0xff;
1322 palette[i].peBlue = colors[i] & 0xff;
1323 palette[i].peFlags = (colors[i] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1326 if (wic_palette)
1327 IWICPalette_Release(wic_palette);
1330 if (SUCCEEDED(hr))
1332 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1333 buffer, imginfo.Format, pitch,
1334 palette, &rect, dwFilter, Colorkey);
1337 free(colors);
1338 free(palette);
1339 free(buffer);
1342 IWICBitmapFrameDecode_Release(bitmapframe);
1344 cleanup_bmp:
1345 IWICBitmapDecoder_Release(decoder);
1347 cleanup_err:
1348 if (factory)
1349 IWICImagingFactory_Release(factory);
1351 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1352 free((void*)pSrcData);
1354 if (FAILED(hr))
1355 return D3DXERR_INVALIDDATA;
1357 if (pSrcInfo)
1358 *pSrcInfo = imginfo;
1360 return D3D_OK;
1363 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
1364 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
1365 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1367 WCHAR *src_file_w;
1368 HRESULT hr;
1369 int strlength;
1371 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1372 "src_rect %s, filter %#lx, color_key 0x%08lx, src_info %p.\n",
1373 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
1374 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1376 if (!src_file || !dst_surface)
1377 return D3DERR_INVALIDCALL;
1379 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
1380 src_file_w = malloc(strlength * sizeof(*src_file_w));
1381 MultiByteToWideChar(CP_ACP, 0, src_file, -1, src_file_w, strlength);
1383 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
1384 src_file_w, src_rect, filter, color_key, src_info);
1385 free(src_file_w);
1387 return hr;
1390 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
1391 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
1392 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1394 DWORD data_size;
1395 void *data;
1396 HRESULT hr;
1398 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1399 "src_rect %s, filter %#lx, color_key 0x%08lx, src_info %p.\n",
1400 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
1401 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1403 if (!src_file || !dst_surface)
1404 return D3DERR_INVALIDCALL;
1406 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
1407 return D3DXERR_INVALIDDATA;
1409 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1410 data, data_size, src_rect, filter, color_key, src_info);
1411 UnmapViewOfFile(data);
1413 return hr;
1416 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
1417 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
1418 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1420 DWORD data_size;
1421 HRSRC resinfo;
1422 void *data;
1424 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1425 "src_rect %s, filter %#lx, color_key 0x%08lx, src_info %p.\n",
1426 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
1427 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1429 if (!dst_surface)
1430 return D3DERR_INVALIDCALL;
1432 if (!(resinfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA))
1433 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1434 && !(resinfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
1435 return D3DXERR_INVALIDDATA;
1437 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1438 return D3DXERR_INVALIDDATA;
1440 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1441 data, data_size, src_rect, filter, color_key, src_info);
1444 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
1445 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
1446 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1448 DWORD data_size;
1449 HRSRC resinfo;
1450 void *data;
1452 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1453 "src_rect %s, filter %#lx, color_key 0x%08lx, src_info %p.\n",
1454 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
1455 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1457 if (!dst_surface)
1458 return D3DERR_INVALIDCALL;
1460 if (!(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA))
1461 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1462 && !(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1463 return D3DXERR_INVALIDDATA;
1465 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1466 return D3DXERR_INVALIDDATA;
1468 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1469 data, data_size, src_rect, filter, color_key, src_info);
1473 /************************************************************
1474 * helper functions for D3DXLoadSurfaceFromMemory
1476 struct argb_conversion_info
1478 const struct pixel_format_desc *srcformat;
1479 const struct pixel_format_desc *destformat;
1480 DWORD srcshift[4], destshift[4];
1481 DWORD srcmask[4], destmask[4];
1482 BOOL process_channel[4];
1483 DWORD channelmask;
1486 static void init_argb_conversion_info(const struct pixel_format_desc *srcformat, const struct pixel_format_desc *destformat, struct argb_conversion_info *info)
1488 UINT i;
1489 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1490 info->channelmask = 0;
1492 info->srcformat = srcformat;
1493 info->destformat = destformat;
1495 for(i = 0;i < 4;i++) {
1496 /* srcshift is used to extract the _relevant_ components */
1497 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1499 /* destshift is used to move the components to the correct position */
1500 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1502 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1503 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1505 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1506 if(destformat->bits[i]) {
1507 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1508 else info->channelmask |= info->destmask[i];
1513 /************************************************************
1514 * get_relevant_argb_components
1516 * Extracts the relevant components from the source color and
1517 * drops the less significant bits if they aren't used by the destination format.
1519 static void get_relevant_argb_components(const struct argb_conversion_info *info, const BYTE *col, DWORD *out)
1521 unsigned int i, j;
1522 unsigned int component, mask;
1524 for (i = 0; i < 4; ++i)
1526 if (!info->process_channel[i])
1527 continue;
1529 component = 0;
1530 mask = info->srcmask[i];
1531 for (j = 0; j < 4 && mask; ++j)
1533 if (info->srcshift[i] < j * 8)
1534 component |= (col[j] & mask) << (j * 8 - info->srcshift[i]);
1535 else
1536 component |= (col[j] & mask) >> (info->srcshift[i] - j * 8);
1537 mask >>= 8;
1539 out[i] = component;
1543 /************************************************************
1544 * make_argb_color
1546 * Recombines the output of get_relevant_argb_components and converts
1547 * it to the destination format.
1549 static DWORD make_argb_color(const struct argb_conversion_info *info, const DWORD *in)
1551 UINT i;
1552 DWORD val = 0;
1554 for(i = 0;i < 4;i++) {
1555 if(info->process_channel[i]) {
1556 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1557 signed int shift;
1558 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1559 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1562 val |= info->channelmask; /* new channels are set to their maximal value */
1563 return val;
1566 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */
1567 void format_to_vec4(const struct pixel_format_desc *format, const BYTE *src, struct vec4 *dst)
1569 DWORD mask, tmp;
1570 unsigned int c;
1572 for (c = 0; c < 4; ++c)
1574 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1575 float *dst_component = (float *)dst + component_offsets[c];
1577 if (format->bits[c])
1579 mask = ~0u >> (32 - format->bits[c]);
1581 memcpy(&tmp, src + format->shift[c] / 8,
1582 min(sizeof(DWORD), (format->shift[c] % 8 + format->bits[c] + 7) / 8));
1584 if (format->type == FORMAT_ARGBF16)
1585 *dst_component = float_16_to_32(tmp);
1586 else if (format->type == FORMAT_ARGBF)
1587 *dst_component = *(float *)&tmp;
1588 else
1589 *dst_component = (float)((tmp >> format->shift[c] % 8) & mask) / mask;
1591 else
1592 *dst_component = 1.0f;
1596 /* It doesn't work for components bigger than 32 bits. */
1597 static void format_from_vec4(const struct pixel_format_desc *format, const struct vec4 *src, BYTE *dst)
1599 DWORD v, mask32;
1600 unsigned int c, i;
1602 memset(dst, 0, format->bytes_per_pixel);
1604 for (c = 0; c < 4; ++c)
1606 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1607 const float src_component = *((const float *)src + component_offsets[c]);
1609 if (!format->bits[c])
1610 continue;
1612 mask32 = ~0u >> (32 - format->bits[c]);
1614 if (format->type == FORMAT_ARGBF16)
1615 v = float_32_to_16(src_component);
1616 else if (format->type == FORMAT_ARGBF)
1617 v = *(DWORD *)&src_component;
1618 else
1619 v = (DWORD)(src_component * ((1 << format->bits[c]) - 1) + 0.5f);
1621 for (i = format->shift[c] / 8 * 8; i < format->shift[c] + format->bits[c]; i += 8)
1623 BYTE mask, byte;
1625 if (format->shift[c] > i)
1627 mask = mask32 << (format->shift[c] - i);
1628 byte = (v << (format->shift[c] - i)) & mask;
1630 else
1632 mask = mask32 >> (i - format->shift[c]);
1633 byte = (v >> (i - format->shift[c])) & mask;
1635 dst[i / 8] |= byte;
1640 /************************************************************
1641 * copy_pixels
1643 * Copies the source buffer to the destination buffer.
1644 * Works for any pixel format.
1645 * The source and the destination must be block-aligned.
1647 void copy_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
1648 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *size,
1649 const struct pixel_format_desc *format)
1651 UINT row, slice;
1652 BYTE *dst_addr;
1653 const BYTE *src_addr;
1654 UINT row_block_count = (size->width + format->block_width - 1) / format->block_width;
1655 UINT row_count = (size->height + format->block_height - 1) / format->block_height;
1657 for (slice = 0; slice < size->depth; slice++)
1659 src_addr = src + slice * src_slice_pitch;
1660 dst_addr = dst + slice * dst_slice_pitch;
1662 for (row = 0; row < row_count; row++)
1664 memcpy(dst_addr, src_addr, row_block_count * format->block_byte_count);
1665 src_addr += src_row_pitch;
1666 dst_addr += dst_row_pitch;
1671 /************************************************************
1672 * convert_argb_pixels
1674 * Copies the source buffer to the destination buffer, performing
1675 * any necessary format conversion and color keying.
1676 * Pixels outsize the source rect are blacked out.
1678 void convert_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1679 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1680 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1681 const PALETTEENTRY *palette)
1683 struct argb_conversion_info conv_info, ck_conv_info;
1684 const struct pixel_format_desc *ck_format = NULL;
1685 DWORD channels[4];
1686 UINT min_width, min_height, min_depth;
1687 UINT x, y, z;
1689 TRACE("src %p, src_row_pitch %u, src_slice_pitch %u, src_size %p, src_format %p, dst %p, "
1690 "dst_row_pitch %u, dst_slice_pitch %u, dst_size %p, dst_format %p, color_key 0x%08lx, palette %p.\n",
1691 src, src_row_pitch, src_slice_pitch, src_size, src_format, dst, dst_row_pitch, dst_slice_pitch, dst_size,
1692 dst_format, color_key, palette);
1694 ZeroMemory(channels, sizeof(channels));
1695 init_argb_conversion_info(src_format, dst_format, &conv_info);
1697 min_width = min(src_size->width, dst_size->width);
1698 min_height = min(src_size->height, dst_size->height);
1699 min_depth = min(src_size->depth, dst_size->depth);
1701 if (color_key)
1703 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1704 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1705 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1708 for (z = 0; z < min_depth; z++) {
1709 const BYTE *src_slice_ptr = src + z * src_slice_pitch;
1710 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1712 for (y = 0; y < min_height; y++) {
1713 const BYTE *src_ptr = src_slice_ptr + y * src_row_pitch;
1714 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1716 for (x = 0; x < min_width; x++) {
1717 if (!src_format->to_rgba && !dst_format->from_rgba
1718 && src_format->type == dst_format->type
1719 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1721 DWORD val;
1723 get_relevant_argb_components(&conv_info, src_ptr, channels);
1724 val = make_argb_color(&conv_info, channels);
1726 if (color_key)
1728 DWORD ck_pixel;
1730 get_relevant_argb_components(&ck_conv_info, src_ptr, channels);
1731 ck_pixel = make_argb_color(&ck_conv_info, channels);
1732 if (ck_pixel == color_key)
1733 val &= ~conv_info.destmask[0];
1735 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1737 else
1739 struct vec4 color, tmp;
1741 format_to_vec4(src_format, src_ptr, &color);
1742 if (src_format->to_rgba)
1743 src_format->to_rgba(&color, &tmp, palette);
1744 else
1745 tmp = color;
1747 if (ck_format)
1749 DWORD ck_pixel;
1751 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1752 if (ck_pixel == color_key)
1753 tmp.w = 0.0f;
1756 if (dst_format->from_rgba)
1757 dst_format->from_rgba(&tmp, &color);
1758 else
1759 color = tmp;
1761 format_from_vec4(dst_format, &color, dst_ptr);
1764 src_ptr += src_format->bytes_per_pixel;
1765 dst_ptr += dst_format->bytes_per_pixel;
1768 if (src_size->width < dst_size->width) /* black out remaining pixels */
1769 memset(dst_ptr, 0, dst_format->bytes_per_pixel * (dst_size->width - src_size->width));
1772 if (src_size->height < dst_size->height) /* black out remaining pixels */
1773 memset(dst + src_size->height * dst_row_pitch, 0, dst_row_pitch * (dst_size->height - src_size->height));
1775 if (src_size->depth < dst_size->depth) /* black out remaining pixels */
1776 memset(dst + src_size->depth * dst_slice_pitch, 0, dst_slice_pitch * (dst_size->depth - src_size->depth));
1779 /************************************************************
1780 * point_filter_argb_pixels
1782 * Copies the source buffer to the destination buffer, performing
1783 * any necessary format conversion, color keying and stretching
1784 * using a point filter.
1786 void point_filter_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1787 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1788 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1789 const PALETTEENTRY *palette)
1791 struct argb_conversion_info conv_info, ck_conv_info;
1792 const struct pixel_format_desc *ck_format = NULL;
1793 DWORD channels[4];
1794 UINT x, y, z;
1796 TRACE("src %p, src_row_pitch %u, src_slice_pitch %u, src_size %p, src_format %p, dst %p, "
1797 "dst_row_pitch %u, dst_slice_pitch %u, dst_size %p, dst_format %p, color_key 0x%08lx, palette %p.\n",
1798 src, src_row_pitch, src_slice_pitch, src_size, src_format, dst, dst_row_pitch, dst_slice_pitch, dst_size,
1799 dst_format, color_key, palette);
1801 ZeroMemory(channels, sizeof(channels));
1802 init_argb_conversion_info(src_format, dst_format, &conv_info);
1804 if (color_key)
1806 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1807 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1808 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1811 for (z = 0; z < dst_size->depth; z++)
1813 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1814 const BYTE *src_slice_ptr = src + src_slice_pitch * (z * src_size->depth / dst_size->depth);
1816 for (y = 0; y < dst_size->height; y++)
1818 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1819 const BYTE *src_row_ptr = src_slice_ptr + src_row_pitch * (y * src_size->height / dst_size->height);
1821 for (x = 0; x < dst_size->width; x++)
1823 const BYTE *src_ptr = src_row_ptr + (x * src_size->width / dst_size->width) * src_format->bytes_per_pixel;
1825 if (!src_format->to_rgba && !dst_format->from_rgba
1826 && src_format->type == dst_format->type
1827 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1829 DWORD val;
1831 get_relevant_argb_components(&conv_info, src_ptr, channels);
1832 val = make_argb_color(&conv_info, channels);
1834 if (color_key)
1836 DWORD ck_pixel;
1838 get_relevant_argb_components(&ck_conv_info, src_ptr, channels);
1839 ck_pixel = make_argb_color(&ck_conv_info, channels);
1840 if (ck_pixel == color_key)
1841 val &= ~conv_info.destmask[0];
1843 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1845 else
1847 struct vec4 color, tmp;
1849 format_to_vec4(src_format, src_ptr, &color);
1850 if (src_format->to_rgba)
1851 src_format->to_rgba(&color, &tmp, palette);
1852 else
1853 tmp = color;
1855 if (ck_format)
1857 DWORD ck_pixel;
1859 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1860 if (ck_pixel == color_key)
1861 tmp.w = 0.0f;
1864 if (dst_format->from_rgba)
1865 dst_format->from_rgba(&tmp, &color);
1866 else
1867 color = tmp;
1869 format_from_vec4(dst_format, &color, dst_ptr);
1872 dst_ptr += dst_format->bytes_per_pixel;
1878 static HRESULT d3dx_image_decompress(const void *memory, uint32_t row_pitch, const RECT *rect,
1879 const RECT *unaligned_rect, const struct volume *size, const struct pixel_format_desc *desc,
1880 void **out_memory, uint32_t *out_row_pitch, RECT *out_rect, const struct pixel_format_desc **out_desc)
1882 void (*fetch_dxt_texel)(int srcRowStride, const BYTE *pixdata, int i, int j, void *texel);
1883 const struct pixel_format_desc *uncompressed_desc = NULL;
1884 uint32_t x, y, tmp_pitch;
1885 BYTE *uncompressed_mem;
1887 switch (desc->format)
1889 case D3DFMT_DXT1:
1890 uncompressed_desc = get_format_info(D3DFMT_A8B8G8R8);
1891 fetch_dxt_texel = fetch_2d_texel_rgba_dxt1;
1892 break;
1893 case D3DFMT_DXT2:
1894 case D3DFMT_DXT3:
1895 uncompressed_desc = get_format_info(D3DFMT_A8B8G8R8);
1896 fetch_dxt_texel = fetch_2d_texel_rgba_dxt3;
1897 break;
1898 case D3DFMT_DXT4:
1899 case D3DFMT_DXT5:
1900 uncompressed_desc = get_format_info(D3DFMT_A8B8G8R8);
1901 fetch_dxt_texel = fetch_2d_texel_rgba_dxt5;
1902 break;
1903 default:
1904 FIXME("Unexpected compressed texture format %u.\n", desc->format);
1905 return E_NOTIMPL;
1908 if (!(uncompressed_mem = malloc(size->width * size->height * size->depth * uncompressed_desc->bytes_per_pixel)))
1909 return E_OUTOFMEMORY;
1911 if (unaligned_rect && EqualRect(rect, unaligned_rect))
1912 goto exit;
1914 TRACE("Decompressing image.\n");
1915 tmp_pitch = row_pitch * desc->block_width / desc->block_byte_count;
1916 for (y = 0; y < size->height; ++y)
1918 BYTE *ptr = &uncompressed_mem[y * size->width * uncompressed_desc->bytes_per_pixel];
1919 for (x = 0; x < size->width; ++x)
1921 const POINT pt = { x, y };
1923 if (!PtInRect(unaligned_rect, pt))
1924 fetch_dxt_texel(tmp_pitch, (BYTE *)memory, x + rect->left, y + rect->top, ptr);
1925 ptr += uncompressed_desc->bytes_per_pixel;
1929 exit:
1930 *out_memory = uncompressed_mem;
1931 *out_row_pitch = size->width * uncompressed_desc->bytes_per_pixel;
1932 if (unaligned_rect)
1933 *out_rect = *unaligned_rect;
1934 else
1935 SetRect(out_rect, 0, 0, size->width, size->height);
1936 *out_desc = uncompressed_desc;
1938 return S_OK;
1941 static void set_volume_struct(struct volume *volume, uint32_t width, uint32_t height, uint32_t depth)
1943 volume->width = width;
1944 volume->height = height;
1945 volume->depth = depth;
1948 static HRESULT d3dx_load_image_from_memory(void *dst_memory, uint32_t dst_row_pitch, const struct pixel_format_desc *dst_desc,
1949 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const RECT *dst_rect_aligned, const void *src_memory,
1950 uint32_t src_row_pitch, const struct pixel_format_desc *src_desc, const PALETTEENTRY *src_palette, const RECT *src_rect,
1951 uint32_t filter_flags, uint32_t color_key)
1953 struct volume src_size, dst_size, dst_size_aligned;
1954 const BYTE *src_memory_offset = src_memory;
1955 HRESULT hr = S_OK;
1957 TRACE("dst_memory %p, dst_row_pitch %d, dst_desc %p, dst_palette %p, dst_rect %s, dst_rect_aligned %s, src_memory %p, "
1958 "src_row_pitch %d, src_desc %p, src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1959 dst_memory, dst_row_pitch, dst_desc, dst_palette, wine_dbgstr_rect(dst_rect), wine_dbgstr_rect(dst_rect_aligned),
1960 src_memory, src_row_pitch, src_desc, src_palette, wine_dbgstr_rect(src_rect), filter_flags, color_key);
1962 set_volume_struct(&src_size, (src_rect->right - src_rect->left), (src_rect->bottom - src_rect->top), 1);
1963 set_volume_struct(&dst_size, (dst_rect->right - dst_rect->left), (dst_rect->bottom - dst_rect->top), 1);
1964 set_volume_struct(&dst_size_aligned, (dst_rect_aligned->right - dst_rect_aligned->left),
1965 (dst_rect_aligned->bottom - dst_rect_aligned->top), 1);
1967 src_memory_offset += (src_rect->top / src_desc->block_height) * src_row_pitch;
1968 src_memory_offset += (src_rect->left / src_desc->block_width) * src_desc->block_byte_count;
1970 /* Everything matches, simply copy the pixels. */
1971 if (src_desc->format == dst_desc->format
1972 && dst_size.width == src_size.width
1973 && dst_size.height == src_size.height
1974 && color_key == 0
1975 && !(src_rect->left & (src_desc->block_width - 1))
1976 && !(src_rect->top & (src_desc->block_height - 1))
1977 && !(dst_rect->left & (dst_desc->block_width - 1))
1978 && !(dst_rect->top & (dst_desc->block_height - 1)))
1980 TRACE("Simple copy.\n");
1981 copy_pixels(src_memory_offset, src_row_pitch, 0, dst_memory, dst_row_pitch, 0, &src_size, src_desc);
1982 return S_OK;
1985 /* Stretching or format conversion. */
1986 if (!is_conversion_from_supported(src_desc)
1987 || !is_conversion_to_supported(dst_desc))
1989 FIXME("Unsupported format conversion %#x -> %#x.\n", src_desc->format, dst_desc->format);
1990 return E_NOTIMPL;
1994 * If the source is a compressed image, we need to decompress it first
1995 * before doing any modifications.
1997 if (src_desc->type == FORMAT_DXT)
1999 const struct pixel_format_desc *uncompressed_desc;
2000 uint32_t uncompressed_row_pitch;
2001 void *uncompressed_mem = NULL;
2002 RECT uncompressed_rect;
2004 hr = d3dx_image_decompress(src_memory, src_row_pitch, src_rect, NULL, &src_size, src_desc,
2005 &uncompressed_mem, &uncompressed_row_pitch, &uncompressed_rect, &uncompressed_desc);
2006 if (SUCCEEDED(hr))
2008 hr = d3dx_load_image_from_memory(dst_memory, dst_row_pitch, dst_desc, dst_palette, dst_rect, dst_rect_aligned,
2009 uncompressed_mem, uncompressed_row_pitch, uncompressed_desc, src_palette, &uncompressed_rect,
2010 filter_flags, color_key);
2012 free(uncompressed_mem);
2013 return hr;
2016 /* Same as the above, need to decompress the destination prior to modifying. */
2017 if (dst_desc->type == FORMAT_DXT)
2019 const struct pixel_format_desc *uncompressed_desc;
2020 uint32_t uncompressed_row_pitch;
2021 void *uncompressed_mem = NULL;
2022 BYTE *uncompressed_mem_offset;
2023 RECT uncompressed_rect;
2025 hr = d3dx_image_decompress(dst_memory, dst_row_pitch, dst_rect_aligned, dst_rect, &dst_size_aligned, dst_desc,
2026 &uncompressed_mem, &uncompressed_row_pitch, &uncompressed_rect, &uncompressed_desc);
2027 if (FAILED(hr))
2028 return hr;
2030 uncompressed_mem_offset = (BYTE *)uncompressed_mem + (dst_rect->top - dst_rect_aligned->top) * uncompressed_row_pitch
2031 + (dst_rect->left - dst_rect_aligned->left) * uncompressed_desc->bytes_per_pixel;
2032 hr = d3dx_load_image_from_memory(uncompressed_mem_offset, uncompressed_row_pitch, uncompressed_desc, dst_palette,
2033 &uncompressed_rect, &uncompressed_rect, src_memory, src_row_pitch, src_desc, src_palette,
2034 src_rect, filter_flags, color_key);
2035 if (SUCCEEDED(hr))
2037 GLenum gl_format = 0;
2039 TRACE("Compressing DXTn surface.\n");
2040 switch (dst_desc->format)
2042 case D3DFMT_DXT1:
2043 gl_format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
2044 break;
2045 case D3DFMT_DXT2:
2046 case D3DFMT_DXT3:
2047 gl_format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
2048 break;
2049 case D3DFMT_DXT4:
2050 case D3DFMT_DXT5:
2051 gl_format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
2052 break;
2053 default:
2054 ERR("Unexpected destination compressed format %u.\n", dst_desc->format);
2056 tx_compress_dxtn(4, dst_size_aligned.width, dst_size_aligned.height, uncompressed_mem, gl_format,
2057 dst_memory, dst_row_pitch);
2059 free(uncompressed_mem);
2060 return hr;
2063 if ((filter_flags & 0xf) == D3DX_FILTER_NONE)
2065 convert_argb_pixels(src_memory_offset, src_row_pitch, 0, &src_size, src_desc,
2066 dst_memory, dst_row_pitch, 0, &dst_size, dst_desc, color_key, src_palette);
2068 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
2070 if ((filter_flags & 0xf) != D3DX_FILTER_POINT)
2071 FIXME("Unhandled filter %#x.\n", filter_flags);
2073 /* Always apply a point filter until D3DX_FILTER_LINEAR,
2074 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
2075 point_filter_argb_pixels(src_memory_offset, src_row_pitch, 0, &src_size, src_desc, dst_memory, dst_row_pitch, 0,
2076 &dst_size, dst_desc, color_key, src_palette);
2079 return hr;
2082 /************************************************************
2083 * D3DXLoadSurfaceFromMemory
2085 * Loads data from a given memory chunk into a surface,
2086 * applying any of the specified filters.
2088 * PARAMS
2089 * pDestSurface [I] pointer to the surface
2090 * pDestPalette [I] palette to use
2091 * pDestRect [I] to be filled area of the surface
2092 * pSrcMemory [I] pointer to the source data
2093 * SrcFormat [I] format of the source pixel data
2094 * SrcPitch [I] number of bytes in a row
2095 * pSrcPalette [I] palette used in the source image
2096 * pSrcRect [I] area of the source data to load
2097 * dwFilter [I] filter to apply on stretching
2098 * Colorkey [I] colorkey
2100 * RETURNS
2101 * Success: D3D_OK, if we successfully load the pixel data into our surface or
2102 * if pSrcMemory is NULL but the other parameters are valid
2103 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect is NULL or
2104 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
2105 * if DestRect is invalid
2106 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
2107 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
2109 * NOTES
2110 * pSrcRect specifies the dimensions of the source data;
2111 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
2114 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
2115 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
2116 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
2117 DWORD filter, D3DCOLOR color_key)
2119 const struct pixel_format_desc *srcformatdesc, *destformatdesc;
2120 RECT dst_rect_temp, dst_rect_aligned;
2121 IDirect3DSurface9 *surface;
2122 D3DSURFACE_DESC surfdesc;
2123 D3DLOCKED_RECT lockrect;
2124 HRESULT hr;
2126 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_memory %p, src_format %#x, "
2127 "src_pitch %u, src_palette %p, src_rect %s, filter %#lx, color_key 0x%08lx.\n",
2128 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
2129 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
2131 if (!dst_surface || !src_memory || !src_rect)
2133 WARN("Invalid argument specified.\n");
2134 return D3DERR_INVALIDCALL;
2136 if (src_format == D3DFMT_UNKNOWN
2137 || src_rect->left >= src_rect->right
2138 || src_rect->top >= src_rect->bottom)
2140 WARN("Invalid src_format or src_rect.\n");
2141 return E_FAIL;
2144 srcformatdesc = get_format_info(src_format);
2145 if (srcformatdesc->type == FORMAT_UNKNOWN)
2147 FIXME("Unsupported format %#x.\n", src_format);
2148 return E_NOTIMPL;
2151 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
2152 destformatdesc = get_format_info(surfdesc.Format);
2153 if (!dst_rect)
2155 dst_rect = &dst_rect_temp;
2156 dst_rect_temp.left = 0;
2157 dst_rect_temp.top = 0;
2158 dst_rect_temp.right = surfdesc.Width;
2159 dst_rect_temp.bottom = surfdesc.Height;
2161 else
2163 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width
2164 || dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height
2165 || dst_rect->left < 0 || dst_rect->top < 0)
2167 WARN("Invalid dst_rect specified.\n");
2168 return D3DERR_INVALIDCALL;
2170 if (dst_rect->left == dst_rect->right || dst_rect->top == dst_rect->bottom)
2172 WARN("Empty dst_rect specified.\n");
2173 return D3D_OK;
2177 dst_rect_aligned = *dst_rect;
2178 if (dst_rect_aligned.left & (destformatdesc->block_width - 1))
2179 dst_rect_aligned.left = dst_rect_aligned.left & ~(destformatdesc->block_width - 1);
2180 if (dst_rect_aligned.top & (destformatdesc->block_height - 1))
2181 dst_rect_aligned.top = dst_rect_aligned.top & ~(destformatdesc->block_height - 1);
2182 if (dst_rect_aligned.right & (destformatdesc->block_width - 1) && dst_rect_aligned.right != surfdesc.Width)
2183 dst_rect_aligned.right = min((dst_rect_aligned.right + destformatdesc->block_width - 1)
2184 & ~(destformatdesc->block_width - 1), surfdesc.Width);
2185 if (dst_rect_aligned.bottom & (destformatdesc->block_height - 1) && dst_rect_aligned.bottom != surfdesc.Height)
2186 dst_rect_aligned.bottom = min((dst_rect_aligned.bottom + destformatdesc->block_height - 1)
2187 & ~(destformatdesc->block_height - 1), surfdesc.Height);
2189 if (filter == D3DX_DEFAULT)
2190 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
2192 if (FAILED(hr = lock_surface(dst_surface, &dst_rect_aligned, &lockrect, &surface, TRUE)))
2193 return hr;
2195 hr = d3dx_load_image_from_memory(lockrect.pBits, lockrect.Pitch, destformatdesc, dst_palette, dst_rect,
2196 &dst_rect_aligned, src_memory, src_pitch, srcformatdesc, src_palette, src_rect, filter, color_key);
2197 if (FAILED(hr))
2198 WARN("d3dx_load_image_from_memory failed with hr %#lx\n", hr);
2200 return unlock_surface(dst_surface, &dst_rect_aligned, surface, TRUE);
2203 /************************************************************
2204 * D3DXLoadSurfaceFromSurface
2206 * Copies the contents from one surface to another, performing any required
2207 * format conversion, resizing or filtering.
2209 * PARAMS
2210 * pDestSurface [I] pointer to the destination surface
2211 * pDestPalette [I] palette to use
2212 * pDestRect [I] to be filled area of the surface
2213 * pSrcSurface [I] pointer to the source surface
2214 * pSrcPalette [I] palette used for the source surface
2215 * pSrcRect [I] area of the source data to load
2216 * dwFilter [I] filter to apply on resizing
2217 * Colorkey [I] any ARGB value or 0 to disable color-keying
2219 * RETURNS
2220 * Success: D3D_OK
2221 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface is NULL
2222 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
2225 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
2226 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
2227 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
2229 const struct pixel_format_desc *src_format_desc, *dst_format_desc;
2230 D3DSURFACE_DESC src_desc, dst_desc;
2231 struct volume src_size, dst_size;
2232 IDirect3DSurface9 *temp_surface;
2233 D3DTEXTUREFILTERTYPE d3d_filter;
2234 IDirect3DDevice9 *device;
2235 D3DLOCKED_RECT lock;
2236 RECT dst_rect_temp;
2237 HRESULT hr;
2238 RECT s;
2240 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
2241 "src_palette %p, src_rect %s, filter %#lx, color_key 0x%08lx.\n",
2242 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
2243 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
2245 if (!dst_surface || !src_surface)
2246 return D3DERR_INVALIDCALL;
2248 IDirect3DSurface9_GetDesc(src_surface, &src_desc);
2249 src_format_desc = get_format_info(src_desc.Format);
2250 if (!src_rect)
2252 SetRect(&s, 0, 0, src_desc.Width, src_desc.Height);
2253 src_rect = &s;
2255 else if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
2257 WARN("Empty src_rect specified.\n");
2258 return filter == D3DX_FILTER_NONE ? D3D_OK : E_FAIL;
2260 else if (src_rect->left > src_rect->right || src_rect->right > src_desc.Width
2261 || src_rect->left < 0 || src_rect->left > src_desc.Width
2262 || src_rect->top > src_rect->bottom || src_rect->bottom > src_desc.Height
2263 || src_rect->top < 0 || src_rect->top > src_desc.Height)
2265 WARN("Invalid src_rect specified.\n");
2266 return D3DERR_INVALIDCALL;
2269 src_size.width = src_rect->right - src_rect->left;
2270 src_size.height = src_rect->bottom - src_rect->top;
2271 src_size.depth = 1;
2273 IDirect3DSurface9_GetDesc(dst_surface, &dst_desc);
2274 dst_format_desc = get_format_info(dst_desc.Format);
2275 if (!dst_rect)
2277 SetRect(&dst_rect_temp, 0, 0, dst_desc.Width, dst_desc.Height);
2278 dst_rect = &dst_rect_temp;
2280 else if (dst_rect->left == dst_rect->right || dst_rect->top == dst_rect->bottom)
2282 WARN("Empty dst_rect specified.\n");
2283 return filter == D3DX_FILTER_NONE ? D3D_OK : E_FAIL;
2285 else if (dst_rect->left > dst_rect->right || dst_rect->right > dst_desc.Width
2286 || dst_rect->left < 0 || dst_rect->left > dst_desc.Width
2287 || dst_rect->top > dst_rect->bottom || dst_rect->bottom > dst_desc.Height
2288 || dst_rect->top < 0 || dst_rect->top > dst_desc.Height)
2290 WARN("Invalid dst_rect specified.\n");
2291 return D3DERR_INVALIDCALL;
2294 dst_size.width = dst_rect->right - dst_rect->left;
2295 dst_size.height = dst_rect->bottom - dst_rect->top;
2296 dst_size.depth = 1;
2298 if (!dst_palette && !src_palette && !color_key)
2300 if (src_desc.Format == dst_desc.Format
2301 && dst_size.width == src_size.width
2302 && dst_size.height == src_size.height
2303 && color_key == 0
2304 && !(src_rect->left & (src_format_desc->block_width - 1))
2305 && !(src_rect->top & (src_format_desc->block_height - 1))
2306 && !(dst_rect->left & (dst_format_desc->block_width - 1))
2307 && !(dst_rect->top & (dst_format_desc->block_height - 1)))
2309 d3d_filter = D3DTEXF_NONE;
2311 else
2313 switch (filter)
2315 case D3DX_FILTER_NONE:
2316 d3d_filter = D3DTEXF_NONE;
2317 break;
2319 case D3DX_FILTER_POINT:
2320 d3d_filter = D3DTEXF_POINT;
2321 break;
2323 case D3DX_FILTER_LINEAR:
2324 d3d_filter = D3DTEXF_LINEAR;
2325 break;
2327 default:
2328 d3d_filter = D3DTEXF_FORCE_DWORD;
2329 break;
2333 if (d3d_filter != D3DTEXF_FORCE_DWORD)
2335 IDirect3DSurface9_GetDevice(src_surface, &device);
2336 hr = IDirect3DDevice9_StretchRect(device, src_surface, src_rect, dst_surface, dst_rect, d3d_filter);
2337 IDirect3DDevice9_Release(device);
2338 if (SUCCEEDED(hr))
2339 return D3D_OK;
2343 if (FAILED(lock_surface(src_surface, NULL, &lock, &temp_surface, FALSE)))
2344 return D3DXERR_INVALIDDATA;
2346 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, lock.pBits,
2347 src_desc.Format, lock.Pitch, src_palette, src_rect, filter, color_key);
2349 if (FAILED(unlock_surface(src_surface, NULL, temp_surface, FALSE)))
2350 return D3DXERR_INVALIDDATA;
2352 return hr;
2356 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
2357 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
2359 int len;
2360 WCHAR *filename;
2361 HRESULT hr;
2362 ID3DXBuffer *buffer;
2364 TRACE("(%s, %#x, %p, %p, %s): relay\n",
2365 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
2367 if (!dst_filename) return D3DERR_INVALIDCALL;
2369 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
2370 filename = malloc(len * sizeof(WCHAR));
2371 if (!filename) return E_OUTOFMEMORY;
2372 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
2374 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
2375 if (SUCCEEDED(hr))
2377 hr = write_buffer_to_file(filename, buffer);
2378 ID3DXBuffer_Release(buffer);
2381 free(filename);
2382 return hr;
2385 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
2386 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
2388 HRESULT hr;
2389 ID3DXBuffer *buffer;
2391 TRACE("(%s, %#x, %p, %p, %s): relay\n",
2392 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
2394 if (!dst_filename) return D3DERR_INVALIDCALL;
2396 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
2397 if (SUCCEEDED(hr))
2399 hr = write_buffer_to_file(dst_filename, buffer);
2400 ID3DXBuffer_Release(buffer);
2403 return hr;
2406 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
2407 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
2409 IWICBitmapEncoder *encoder = NULL;
2410 IWICBitmapFrameEncode *frame = NULL;
2411 IPropertyBag2 *encoder_options = NULL;
2412 IStream *stream = NULL;
2413 HRESULT hr;
2414 const GUID *container_format;
2415 const GUID *pixel_format_guid;
2416 WICPixelFormatGUID wic_pixel_format;
2417 IWICImagingFactory *factory;
2418 D3DFORMAT d3d_pixel_format;
2419 D3DSURFACE_DESC src_surface_desc;
2420 IDirect3DSurface9 *temp_surface;
2421 D3DLOCKED_RECT locked_rect;
2422 int width, height;
2423 STATSTG stream_stats;
2424 HGLOBAL stream_hglobal;
2425 ID3DXBuffer *buffer;
2426 DWORD size;
2428 TRACE("dst_buffer %p, file_format %#x, src_surface %p, src_palette %p, src_rect %s.\n",
2429 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
2431 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
2433 if (src_palette)
2435 FIXME("Saving surfaces with palettized pixel formats is not implemented yet\n");
2436 return D3DERR_INVALIDCALL;
2439 switch (file_format)
2441 case D3DXIFF_BMP:
2442 case D3DXIFF_DIB:
2443 container_format = &GUID_ContainerFormatBmp;
2444 break;
2445 case D3DXIFF_PNG:
2446 container_format = &GUID_ContainerFormatPng;
2447 break;
2448 case D3DXIFF_JPG:
2449 container_format = &GUID_ContainerFormatJpeg;
2450 break;
2451 case D3DXIFF_DDS:
2452 return save_dds_surface_to_memory(dst_buffer, src_surface, src_rect);
2453 case D3DXIFF_HDR:
2454 case D3DXIFF_PFM:
2455 case D3DXIFF_TGA:
2456 case D3DXIFF_PPM:
2457 FIXME("File format %#x is not supported yet\n", file_format);
2458 return E_NOTIMPL;
2459 default:
2460 return D3DERR_INVALIDCALL;
2463 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
2464 if (src_rect)
2466 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
2468 WARN("Invalid rectangle with 0 area\n");
2469 return D3DXCreateBuffer(64, dst_buffer);
2471 if (src_rect->left < 0 || src_rect->top < 0)
2472 return D3DERR_INVALIDCALL;
2473 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
2474 return D3DERR_INVALIDCALL;
2475 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
2476 return D3DERR_INVALIDCALL;
2478 width = src_rect->right - src_rect->left;
2479 height = src_rect->bottom - src_rect->top;
2481 else
2483 width = src_surface_desc.Width;
2484 height = src_surface_desc.Height;
2487 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory);
2488 if (FAILED(hr)) goto cleanup_err;
2490 hr = IWICImagingFactory_CreateEncoder(factory, container_format, NULL, &encoder);
2491 IWICImagingFactory_Release(factory);
2492 if (FAILED(hr)) goto cleanup_err;
2494 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
2495 if (FAILED(hr)) goto cleanup_err;
2497 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2498 if (FAILED(hr)) goto cleanup_err;
2500 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
2501 if (FAILED(hr)) goto cleanup_err;
2503 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
2504 if (FAILED(hr)) goto cleanup_err;
2506 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
2507 if (FAILED(hr)) goto cleanup_err;
2509 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
2510 if (!pixel_format_guid)
2512 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
2513 hr = E_NOTIMPL;
2514 goto cleanup;
2517 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
2518 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
2519 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
2520 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
2522 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
2523 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
2525 if (FAILED(hr = lock_surface(src_surface, src_rect, &locked_rect, &temp_surface, FALSE)))
2526 goto cleanup;
2528 IWICBitmapFrameEncode_WritePixels(frame, height,
2529 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
2530 unlock_surface(src_surface, src_rect, temp_surface, FALSE);
2532 else /* Pixel format conversion */
2534 const struct pixel_format_desc *src_format_desc, *dst_format_desc;
2535 struct volume size;
2536 DWORD dst_pitch;
2537 void *dst_data;
2539 src_format_desc = get_format_info(src_surface_desc.Format);
2540 dst_format_desc = get_format_info(d3d_pixel_format);
2541 if (!is_conversion_from_supported(src_format_desc)
2542 || !is_conversion_to_supported(dst_format_desc))
2544 FIXME("Unsupported format conversion %#x -> %#x.\n",
2545 src_surface_desc.Format, d3d_pixel_format);
2546 hr = E_NOTIMPL;
2547 goto cleanup;
2550 size.width = width;
2551 size.height = height;
2552 size.depth = 1;
2553 dst_pitch = width * dst_format_desc->bytes_per_pixel;
2554 dst_data = malloc(dst_pitch * height);
2555 if (!dst_data)
2557 hr = E_OUTOFMEMORY;
2558 goto cleanup;
2560 if (FAILED(hr = lock_surface(src_surface, src_rect, &locked_rect, &temp_surface, FALSE)))
2562 free(dst_data);
2563 goto cleanup;
2565 convert_argb_pixels(locked_rect.pBits, locked_rect.Pitch, 0, &size, src_format_desc,
2566 dst_data, dst_pitch, 0, &size, dst_format_desc, 0, NULL);
2567 unlock_surface(src_surface, src_rect, temp_surface, FALSE);
2569 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
2570 free(dst_data);
2573 hr = IWICBitmapFrameEncode_Commit(frame);
2574 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
2576 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
2578 /* copy data from stream to ID3DXBuffer */
2579 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
2580 if (FAILED(hr)) goto cleanup_err;
2582 if (stream_stats.cbSize.u.HighPart != 0)
2584 hr = D3DXERR_INVALIDDATA;
2585 goto cleanup;
2587 size = stream_stats.cbSize.u.LowPart;
2589 /* Remove BMP header for DIB */
2590 if (file_format == D3DXIFF_DIB)
2591 size -= sizeof(BITMAPFILEHEADER);
2593 hr = D3DXCreateBuffer(size, &buffer);
2594 if (FAILED(hr)) goto cleanup;
2596 hr = GetHGlobalFromStream(stream, &stream_hglobal);
2597 if (SUCCEEDED(hr))
2599 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
2600 void *stream_data = GlobalLock(stream_hglobal);
2601 /* Remove BMP header for DIB */
2602 if (file_format == D3DXIFF_DIB)
2603 stream_data = (void*)((BYTE*)stream_data + sizeof(BITMAPFILEHEADER));
2604 memcpy(buffer_pointer, stream_data, size);
2605 GlobalUnlock(stream_hglobal);
2606 *dst_buffer = buffer;
2608 else ID3DXBuffer_Release(buffer);
2610 cleanup_err:
2611 if (FAILED(hr) && hr != E_OUTOFMEMORY)
2612 hr = D3DERR_INVALIDCALL;
2614 cleanup:
2615 if (stream) IStream_Release(stream);
2617 if (frame) IWICBitmapFrameEncode_Release(frame);
2618 if (encoder_options) IPropertyBag2_Release(encoder_options);
2620 if (encoder) IWICBitmapEncoder_Release(encoder);
2622 return hr;