d3dx9: Handle pixel formats larger than 32 bits in surface loading / filtering functions.
[wine.git] / dlls / d3dx9_36 / surface.c
blob71c43bdf66a84a4cf504d49d7cd812cd4d881a5d
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')
159 for (i = 0; i < sizeof(known_fourcc) / sizeof(known_fourcc[0]); i++)
161 if (known_fourcc[i] == fourcc)
162 return fourcc;
165 WARN("Unknown FourCC %#x\n", fourcc);
166 return D3DFMT_UNKNOWN;
169 static const struct {
170 DWORD bpp;
171 DWORD rmask;
172 DWORD gmask;
173 DWORD bmask;
174 DWORD amask;
175 D3DFORMAT format;
176 } rgb_pixel_formats[] = {
177 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 },
178 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 },
179 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 },
180 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 },
181 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 },
182 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 },
183 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 },
184 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 },
185 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 },
186 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 },
187 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 },
188 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 },
189 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 },
190 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8 },
191 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8 },
194 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
196 unsigned int i;
198 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
200 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
201 && rgb_pixel_formats[i].rmask == pixel_format->rmask
202 && rgb_pixel_formats[i].gmask == pixel_format->gmask
203 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
205 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
206 return rgb_pixel_formats[i].format;
207 if (rgb_pixel_formats[i].amask == 0)
208 return rgb_pixel_formats[i].format;
212 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
213 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
214 return D3DFMT_UNKNOWN;
217 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
219 if (pixel_format->bpp == 8)
221 if (pixel_format->rmask == 0xff)
222 return D3DFMT_L8;
223 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
224 return D3DFMT_A4L4;
226 if (pixel_format->bpp == 16)
228 if (pixel_format->rmask == 0xffff)
229 return D3DFMT_L16;
230 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
231 return D3DFMT_A8L8;
234 WARN("Unknown luminance pixel format (bpp %u, l %#x, a %#x)\n",
235 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
236 return D3DFMT_UNKNOWN;
239 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
241 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
242 return D3DFMT_A8;
244 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format->bpp, pixel_format->rmask);
245 return D3DFMT_UNKNOWN;
248 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
250 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00)
251 return D3DFMT_V8U8;
252 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000)
253 return D3DFMT_V16U16;
255 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format->bpp,
256 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
257 return D3DFMT_UNKNOWN;
260 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
262 TRACE("pixel_format: size %u, flags %#x, fourcc %#x, bpp %u.\n", pixel_format->size,
263 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp);
264 TRACE("rmask %#x, gmask %#x, bmask %#x, amask %#x.\n", pixel_format->rmask, pixel_format->gmask,
265 pixel_format->bmask, pixel_format->amask);
267 if (pixel_format->flags & DDS_PF_FOURCC)
268 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
269 if (pixel_format->flags & DDS_PF_RGB)
270 return dds_rgb_to_d3dformat(pixel_format);
271 if (pixel_format->flags & DDS_PF_LUMINANCE)
272 return dds_luminance_to_d3dformat(pixel_format);
273 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
274 return dds_alpha_to_d3dformat(pixel_format);
275 if (pixel_format->flags & DDS_PF_BUMPDUDV)
276 return dds_bump_to_d3dformat(pixel_format);
278 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n",
279 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
280 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
281 return D3DFMT_UNKNOWN;
284 static HRESULT d3dformat_to_dds_pixel_format(struct dds_pixel_format *pixel_format, D3DFORMAT d3dformat)
286 unsigned int i;
288 memset(pixel_format, 0, sizeof(*pixel_format));
290 pixel_format->size = sizeof(*pixel_format);
292 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
294 if (rgb_pixel_formats[i].format == d3dformat)
296 pixel_format->flags |= DDS_PF_RGB;
297 pixel_format->bpp = rgb_pixel_formats[i].bpp;
298 pixel_format->rmask = rgb_pixel_formats[i].rmask;
299 pixel_format->gmask = rgb_pixel_formats[i].gmask;
300 pixel_format->bmask = rgb_pixel_formats[i].bmask;
301 pixel_format->amask = rgb_pixel_formats[i].amask;
302 if (pixel_format->amask) pixel_format->flags |= DDS_PF_ALPHA;
303 return D3D_OK;
307 WARN("Unknown pixel format %#x\n", d3dformat);
308 return E_NOTIMPL;
311 static HRESULT calculate_dds_surface_size(D3DFORMAT format, UINT width, UINT height,
312 UINT *pitch, UINT *size)
314 const struct pixel_format_desc *format_desc = get_format_info(format);
315 if (format_desc->type == FORMAT_UNKNOWN)
316 return E_NOTIMPL;
318 if (format_desc->block_width != 1 || format_desc->block_height != 1)
320 *pitch = format_desc->block_byte_count
321 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
322 *size = *pitch
323 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
325 else
327 *pitch = width * format_desc->bytes_per_pixel;
328 *size = *pitch * height;
331 return D3D_OK;
334 static UINT calculate_dds_file_size(D3DFORMAT format, UINT width, UINT height, UINT depth,
335 UINT miplevels, UINT faces)
337 UINT i, file_size = 0;
339 for (i = 0; i < miplevels; i++)
341 UINT pitch, size = 0;
342 calculate_dds_surface_size(format, width, height, &pitch, &size);
343 size *= depth;
344 file_size += size;
345 width = max(1, width / 2);
346 height = max(1, height / 2);
347 depth = max(1, depth / 2);
350 file_size *= faces;
351 file_size += sizeof(struct dds_header);
352 return file_size;
355 /************************************************************
356 * get_image_info_from_dds
358 * Fills a D3DXIMAGE_INFO structure with information
359 * about a DDS file stored in the memory.
361 * PARAMS
362 * buffer [I] pointer to DDS data
363 * length [I] size of DDS data
364 * info [O] pointer to D3DXIMAGE_INFO structure
366 * RETURNS
367 * Success: D3D_OK
368 * Failure: D3DXERR_INVALIDDATA
371 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
373 UINT faces = 1;
374 UINT expected_length;
375 const struct dds_header *header = buffer;
377 if (length < sizeof(*header) || !info)
378 return D3DXERR_INVALIDDATA;
380 if (header->pixel_format.size != sizeof(header->pixel_format))
381 return D3DXERR_INVALIDDATA;
383 info->Width = header->width;
384 info->Height = header->height;
385 info->Depth = 1;
386 info->MipLevels = (header->flags & DDS_MIPMAPCOUNT) ? header->miplevels : 1;
388 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
389 if (info->Format == D3DFMT_UNKNOWN)
390 return D3DXERR_INVALIDDATA;
392 TRACE("Pixel format is %#x\n", info->Format);
394 if (header->caps2 & DDS_CAPS2_VOLUME)
396 info->Depth = header->depth;
397 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
399 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
401 DWORD face;
402 faces = 0;
403 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
405 if (header->caps2 & face)
406 faces++;
408 info->ResourceType = D3DRTYPE_CUBETEXTURE;
410 else
412 info->ResourceType = D3DRTYPE_TEXTURE;
415 expected_length = calculate_dds_file_size(info->Format, info->Width, info->Height, info->Depth,
416 info->MipLevels, faces);
417 if (length < expected_length)
419 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
420 return D3DXERR_INVALIDDATA;
423 info->ImageFileFormat = D3DXIFF_DDS;
424 return D3D_OK;
427 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
428 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
429 const D3DXIMAGE_INFO *src_info)
431 UINT size;
432 UINT src_pitch;
433 const struct dds_header *header = src_data;
434 const BYTE *pixels = (BYTE *)(header + 1);
436 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
437 return D3DXERR_INVALIDDATA;
439 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &src_pitch, &size)))
440 return E_NOTIMPL;
442 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
443 src_pitch, NULL, src_rect, filter, color_key);
446 static HRESULT save_dds_surface_to_memory(ID3DXBuffer **dst_buffer, IDirect3DSurface9 *src_surface, const RECT *src_rect)
448 HRESULT hr;
449 UINT dst_pitch, surface_size, file_size;
450 D3DSURFACE_DESC src_desc;
451 D3DLOCKED_RECT locked_rect;
452 ID3DXBuffer *buffer;
453 struct dds_header *header;
454 BYTE *pixels;
455 struct volume volume;
456 const struct pixel_format_desc *pixel_format;
458 if (src_rect)
460 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
461 return E_NOTIMPL;
464 hr = IDirect3DSurface9_GetDesc(src_surface, &src_desc);
465 if (FAILED(hr)) return hr;
467 pixel_format = get_format_info(src_desc.Format);
468 if (pixel_format->type == FORMAT_UNKNOWN) return E_NOTIMPL;
470 file_size = calculate_dds_file_size(src_desc.Format, src_desc.Width, src_desc.Height, 1, 1, 1);
472 hr = calculate_dds_surface_size(src_desc.Format, src_desc.Width, src_desc.Height, &dst_pitch, &surface_size);
473 if (FAILED(hr)) return hr;
475 hr = D3DXCreateBuffer(file_size, &buffer);
476 if (FAILED(hr)) return hr;
478 header = ID3DXBuffer_GetBufferPointer(buffer);
479 pixels = (BYTE *)(header + 1);
481 memset(header, 0, sizeof(*header));
482 header->signature = MAKEFOURCC('D','D','S',' ');
483 header->size = sizeof(*header);
484 header->flags = DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PITCH | DDS_PIXELFORMAT | DDS_MIPMAPCOUNT;
485 header->height = src_desc.Height;
486 header->width = src_desc.Width;
487 header->pitch_or_linear_size = dst_pitch;
488 header->depth = 1;
489 header->miplevels = 1;
490 header->caps = DDS_CAPS_TEXTURE;
491 hr = d3dformat_to_dds_pixel_format(&header->pixel_format, src_desc.Format);
492 if (FAILED(hr))
494 ID3DXBuffer_Release(buffer);
495 return hr;
498 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, NULL, D3DLOCK_READONLY);
499 if (FAILED(hr))
501 ID3DXBuffer_Release(buffer);
502 return hr;
505 volume.width = src_desc.Width;
506 volume.height = src_desc.Height;
507 volume.depth = 1;
508 copy_pixels(locked_rect.pBits, locked_rect.Pitch, 0, pixels, dst_pitch, 0,
509 &volume, pixel_format);
511 IDirect3DSurface9_UnlockRect(src_surface);
513 *dst_buffer = buffer;
514 return D3D_OK;
517 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette,
518 const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key,
519 const D3DXIMAGE_INFO *src_info)
521 UINT row_pitch, slice_pitch;
522 const struct dds_header *header = src_data;
523 const BYTE *pixels = (BYTE *)(header + 1);
525 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
526 return D3DXERR_INVALIDDATA;
528 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &row_pitch, &slice_pitch)))
529 return E_NOTIMPL;
531 return D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box, pixels, src_info->Format,
532 row_pitch, slice_pitch, NULL, src_box, filter, color_key);
535 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
536 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info)
539 HRESULT hr;
540 RECT src_rect;
541 UINT src_pitch;
542 UINT mip_level;
543 UINT mip_levels;
544 UINT mip_level_size;
545 UINT width, height;
546 IDirect3DSurface9 *surface;
547 const struct dds_header *header = src_data;
548 const BYTE *pixels = (BYTE *)(header + 1);
550 /* Loading a cube texture as a simple texture is also supported (only first face texture is taken) */
551 if ((src_info->ResourceType != D3DRTYPE_TEXTURE) && (src_info->ResourceType != D3DRTYPE_CUBETEXTURE))
552 return D3DXERR_INVALIDDATA;
554 width = src_info->Width;
555 height = src_info->Height;
556 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
557 for (mip_level = 0; mip_level < mip_levels; mip_level++)
559 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_pitch, &mip_level_size);
560 if (FAILED(hr)) return hr;
562 SetRect(&src_rect, 0, 0, width, height);
564 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level, &surface);
565 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
566 NULL, &src_rect, filter, color_key);
567 IDirect3DSurface9_Release(surface);
568 if (FAILED(hr)) return hr;
570 pixels += mip_level_size;
571 width = max(1, width / 2);
572 height = max(1, height / 2);
575 return D3D_OK;
578 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
579 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
581 HRESULT hr;
582 int face;
583 UINT mip_level;
584 UINT size;
585 RECT src_rect;
586 UINT src_pitch;
587 UINT mip_levels;
588 UINT mip_level_size;
589 IDirect3DSurface9 *surface;
590 const struct dds_header *header = src_data;
591 const BYTE *pixels = (BYTE *)(header + 1);
593 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
594 return D3DXERR_INVALIDDATA;
596 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
598 WARN("Only full cubemaps are supported\n");
599 return D3DXERR_INVALIDDATA;
602 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
603 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
605 size = src_info->Width;
606 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
608 hr = calculate_dds_surface_size(src_info->Format, size, size, &src_pitch, &mip_level_size);
609 if (FAILED(hr)) return hr;
611 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
612 if (mip_level < mip_levels)
614 SetRect(&src_rect, 0, 0, size, size);
616 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
617 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
618 NULL, &src_rect, filter, color_key);
619 IDirect3DSurface9_Release(surface);
620 if (FAILED(hr)) return hr;
623 pixels += mip_level_size;
624 size = max(1, size / 2);
628 return D3D_OK;
631 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
632 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
634 HRESULT hr;
635 UINT mip_level;
636 UINT mip_levels;
637 UINT src_slice_pitch;
638 UINT src_row_pitch;
639 D3DBOX src_box;
640 UINT width, height, depth;
641 IDirect3DVolume9 *volume;
642 const struct dds_header *header = src_data;
643 const BYTE *pixels = (BYTE *)(header + 1);
645 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
646 return D3DXERR_INVALIDDATA;
648 width = src_info->Width;
649 height = src_info->Height;
650 depth = src_info->Depth;
651 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture));
653 for (mip_level = 0; mip_level < mip_levels; mip_level++)
655 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_row_pitch, &src_slice_pitch);
656 if (FAILED(hr)) return hr;
658 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume);
659 if (FAILED(hr)) return hr;
661 src_box.Left = 0;
662 src_box.Top = 0;
663 src_box.Right = width;
664 src_box.Bottom = height;
665 src_box.Front = 0;
666 src_box.Back = depth;
668 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format,
669 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key);
671 IDirect3DVolume9_Release(volume);
672 if (FAILED(hr)) return hr;
674 pixels += depth * src_slice_pitch;
675 width = max(1, width / 2);
676 height = max(1, height / 2);
677 depth = max(1, depth / 2);
680 return D3D_OK;
683 static BOOL convert_dib_to_bmp(void **data, UINT *size)
685 ULONG header_size;
686 ULONG count = 0;
687 ULONG offset;
688 BITMAPFILEHEADER *header;
689 BYTE *new_data;
690 UINT new_size;
692 if ((*size < 4) || (*size < (header_size = *(ULONG*)*data)))
693 return FALSE;
695 if ((header_size == sizeof(BITMAPINFOHEADER)) ||
696 (header_size == sizeof(BITMAPV4HEADER)) ||
697 (header_size == sizeof(BITMAPV5HEADER)) ||
698 (header_size == 64 /* sizeof(BITMAPCOREHEADER2) */))
700 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
701 BITMAPINFOHEADER *info_header = (BITMAPINFOHEADER*)*data;
702 count = info_header->biClrUsed;
704 if (!count && info_header->biBitCount <= 8)
705 count = 1 << info_header->biBitCount;
707 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBQUAD) * count;
709 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
710 if ((info_header->biSize == sizeof(BITMAPINFOHEADER)) && (info_header->biCompression == BI_BITFIELDS))
711 offset += 3 * sizeof(DWORD);
713 else if (header_size == sizeof(BITMAPCOREHEADER))
715 BITMAPCOREHEADER *core_header = (BITMAPCOREHEADER*)*data;
717 if (core_header->bcBitCount <= 8)
718 count = 1 << core_header->bcBitCount;
720 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBTRIPLE) * count;
722 else
724 return FALSE;
727 TRACE("Converting DIB file to BMP\n");
729 new_size = *size + sizeof(BITMAPFILEHEADER);
730 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
731 CopyMemory(new_data + sizeof(BITMAPFILEHEADER), *data, *size);
733 /* Add BMP header */
734 header = (BITMAPFILEHEADER*)new_data;
735 header->bfType = 0x4d42; /* BM */
736 header->bfSize = new_size;
737 header->bfReserved1 = 0;
738 header->bfReserved2 = 0;
739 header->bfOffBits = offset;
741 /* Update input data */
742 *data = new_data;
743 *size = new_size;
745 return TRUE;
748 /************************************************************
749 * D3DXGetImageInfoFromFileInMemory
751 * Fills a D3DXIMAGE_INFO structure with info about an image
753 * PARAMS
754 * data [I] pointer to the image file data
755 * datasize [I] size of the passed data
756 * info [O] pointer to the destination structure
758 * RETURNS
759 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
760 * if info is NULL and data and datasize are not NULL
761 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
762 * D3DERR_INVALIDCALL, if data is NULL or
763 * if datasize is 0
765 * NOTES
766 * datasize may be bigger than the actual file size
769 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(const void *data, UINT datasize, D3DXIMAGE_INFO *info)
771 IWICImagingFactory *factory;
772 IWICBitmapDecoder *decoder = NULL;
773 IWICStream *stream;
774 HRESULT hr;
775 HRESULT initresult;
776 BOOL dib;
778 TRACE("(%p, %d, %p)\n", data, datasize, info);
780 if (!data || !datasize)
781 return D3DERR_INVALIDCALL;
783 if (!info)
784 return D3D_OK;
786 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
787 TRACE("File type is DDS\n");
788 return get_image_info_from_dds(data, datasize, info);
791 /* In case of DIB file, convert it to BMP */
792 dib = convert_dib_to_bmp((void**)&data, &datasize);
794 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
796 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
798 if (SUCCEEDED(hr)) {
799 IWICImagingFactory_CreateStream(factory, &stream);
800 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
801 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
802 IWICStream_Release(stream);
803 IWICImagingFactory_Release(factory);
806 if (FAILED(hr)) {
807 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
808 FIXME("File type PPM is not supported yet\n");
809 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
810 FIXME("File type HDR is not supported yet\n");
811 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
812 FIXME("File type PFM is not supported yet\n");
815 if (SUCCEEDED(hr)) {
816 GUID container_format;
817 UINT frame_count;
819 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
820 if (SUCCEEDED(hr)) {
821 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
822 if (dib) {
823 TRACE("File type is DIB\n");
824 info->ImageFileFormat = D3DXIFF_DIB;
825 } else {
826 TRACE("File type is BMP\n");
827 info->ImageFileFormat = D3DXIFF_BMP;
829 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
830 TRACE("File type is PNG\n");
831 info->ImageFileFormat = D3DXIFF_PNG;
832 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
833 TRACE("File type is JPG\n");
834 info->ImageFileFormat = D3DXIFF_JPG;
835 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
836 TRACE("File type is TGA\n");
837 info->ImageFileFormat = D3DXIFF_TGA;
838 } else {
839 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
840 hr = D3DXERR_INVALIDDATA;
844 if (SUCCEEDED(hr))
845 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
846 if (SUCCEEDED(hr) && !frame_count)
847 hr = D3DXERR_INVALIDDATA;
849 if (SUCCEEDED(hr)) {
850 IWICBitmapFrameDecode *frame = NULL;
852 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
854 if (SUCCEEDED(hr))
855 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
857 if (SUCCEEDED(hr)) {
858 WICPixelFormatGUID pixel_format;
860 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
861 if (SUCCEEDED(hr)) {
862 info->Format = wic_guid_to_d3dformat(&pixel_format);
863 if (info->Format == D3DFMT_UNKNOWN) {
864 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
865 hr = D3DXERR_INVALIDDATA;
870 if (frame)
871 IWICBitmapFrameDecode_Release(frame);
873 info->Depth = 1;
874 info->MipLevels = 1;
875 info->ResourceType = D3DRTYPE_TEXTURE;
879 if (decoder)
880 IWICBitmapDecoder_Release(decoder);
882 if (SUCCEEDED(initresult))
883 CoUninitialize();
885 if (dib)
886 HeapFree(GetProcessHeap(), 0, (void*)data);
888 if (FAILED(hr)) {
889 TRACE("Invalid or unsupported image file\n");
890 return D3DXERR_INVALIDDATA;
893 return D3D_OK;
896 /************************************************************
897 * D3DXGetImageInfoFromFile
899 * RETURNS
900 * Success: D3D_OK, if we successfully load a valid image file or
901 * if we successfully load a file which is no valid image and info is NULL
902 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
903 * if file is not a valid image file and info is not NULL
904 * D3DERR_INVALIDCALL, if file is NULL
907 HRESULT WINAPI D3DXGetImageInfoFromFileA(const char *file, D3DXIMAGE_INFO *info)
909 WCHAR *widename;
910 HRESULT hr;
911 int strlength;
913 TRACE("file %s, info %p.\n", debugstr_a(file), info);
915 if( !file ) return D3DERR_INVALIDCALL;
917 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
918 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
919 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
921 hr = D3DXGetImageInfoFromFileW(widename, info);
922 HeapFree(GetProcessHeap(), 0, widename);
924 return hr;
927 HRESULT WINAPI D3DXGetImageInfoFromFileW(const WCHAR *file, D3DXIMAGE_INFO *info)
929 void *buffer;
930 HRESULT hr;
931 DWORD size;
933 TRACE("file %s, info %p.\n", debugstr_w(file), info);
935 if (!file)
936 return D3DERR_INVALIDCALL;
938 if (FAILED(map_view_of_file(file, &buffer, &size)))
939 return D3DXERR_INVALIDDATA;
941 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
942 UnmapViewOfFile(buffer);
944 return hr;
947 /************************************************************
948 * D3DXGetImageInfoFromResource
950 * RETURNS
951 * Success: D3D_OK, if resource is a valid image file
952 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
953 * if we fail to load resource
956 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, const char *resource, D3DXIMAGE_INFO *info)
958 HRSRC resinfo;
959 void *buffer;
960 DWORD size;
962 TRACE("module %p, resource %s, info %p.\n", module, debugstr_a(resource), info);
964 if (!(resinfo = FindResourceA(module, resource, (const char *)RT_RCDATA))
965 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
966 && !(resinfo = FindResourceA(module, resource, (const char *)RT_BITMAP)))
967 return D3DXERR_INVALIDDATA;
969 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
970 return D3DXERR_INVALIDDATA;
972 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
975 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, const WCHAR *resource, D3DXIMAGE_INFO *info)
977 HRSRC resinfo;
978 void *buffer;
979 DWORD size;
981 TRACE("module %p, resource %s, info %p.\n", module, debugstr_w(resource), info);
983 if (!(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA))
984 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
985 && !(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_BITMAP)))
986 return D3DXERR_INVALIDDATA;
988 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
989 return D3DXERR_INVALIDDATA;
991 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
994 /************************************************************
995 * D3DXLoadSurfaceFromFileInMemory
997 * Loads data from a given buffer into a surface and fills a given
998 * D3DXIMAGE_INFO structure with info about the source data.
1000 * PARAMS
1001 * pDestSurface [I] pointer to the surface
1002 * pDestPalette [I] palette to use
1003 * pDestRect [I] to be filled area of the surface
1004 * pSrcData [I] pointer to the source data
1005 * SrcDataSize [I] size of the source data in bytes
1006 * pSrcRect [I] area of the source data to load
1007 * dwFilter [I] filter to apply on stretching
1008 * Colorkey [I] colorkey
1009 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1011 * RETURNS
1012 * Success: D3D_OK
1013 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
1014 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1017 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
1018 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
1019 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
1021 D3DXIMAGE_INFO imginfo;
1022 HRESULT hr;
1024 IWICImagingFactory *factory = NULL;
1025 IWICBitmapDecoder *decoder;
1026 IWICBitmapFrameDecode *bitmapframe;
1027 IWICStream *stream;
1029 const struct pixel_format_desc *formatdesc;
1030 WICRect wicrect;
1031 RECT rect;
1033 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1034 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1035 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
1036 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
1038 if (!pDestSurface || !pSrcData || !SrcDataSize)
1039 return D3DERR_INVALIDCALL;
1041 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
1043 if (FAILED(hr))
1044 return hr;
1046 if (pSrcRect)
1048 wicrect.X = pSrcRect->left;
1049 wicrect.Y = pSrcRect->top;
1050 wicrect.Width = pSrcRect->right - pSrcRect->left;
1051 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
1053 else
1055 wicrect.X = 0;
1056 wicrect.Y = 0;
1057 wicrect.Width = imginfo.Width;
1058 wicrect.Height = imginfo.Height;
1061 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
1063 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
1065 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
1066 dwFilter, Colorkey, &imginfo);
1067 if (SUCCEEDED(hr) && pSrcInfo)
1068 *pSrcInfo = imginfo;
1069 return hr;
1072 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1073 convert_dib_to_bmp((void**)&pSrcData, &SrcDataSize);
1075 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1077 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
1078 goto cleanup_err;
1080 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
1082 IWICImagingFactory_Release(factory);
1083 factory = NULL;
1084 goto cleanup_err;
1087 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
1089 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
1091 IWICStream_Release(stream);
1093 if (FAILED(hr))
1094 goto cleanup_err;
1096 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
1098 if (FAILED(hr))
1099 goto cleanup_bmp;
1101 formatdesc = get_format_info(imginfo.Format);
1103 if (formatdesc->type == FORMAT_UNKNOWN)
1105 FIXME("Unsupported pixel format\n");
1106 hr = D3DXERR_INVALIDDATA;
1108 else
1110 BYTE *buffer;
1111 DWORD pitch;
1112 PALETTEENTRY *palette = NULL;
1113 WICColor *colors = NULL;
1115 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
1116 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
1118 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
1119 pitch * wicrect.Height, buffer);
1121 if (SUCCEEDED(hr) && (formatdesc->type == FORMAT_INDEX))
1123 IWICPalette *wic_palette = NULL;
1124 UINT nb_colors;
1126 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
1127 if (SUCCEEDED(hr))
1128 hr = IWICBitmapFrameDecode_CopyPalette(bitmapframe, wic_palette);
1129 if (SUCCEEDED(hr))
1130 hr = IWICPalette_GetColorCount(wic_palette, &nb_colors);
1131 if (SUCCEEDED(hr))
1133 colors = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(colors[0]));
1134 palette = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(palette[0]));
1135 if (!colors || !palette)
1136 hr = E_OUTOFMEMORY;
1138 if (SUCCEEDED(hr))
1139 hr = IWICPalette_GetColors(wic_palette, nb_colors, colors, &nb_colors);
1140 if (SUCCEEDED(hr))
1142 UINT i;
1144 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1145 for (i = 0; i < nb_colors; i++)
1147 palette[i].peRed = (colors[i] >> 16) & 0xff;
1148 palette[i].peGreen = (colors[i] >> 8) & 0xff;
1149 palette[i].peBlue = colors[i] & 0xff;
1150 palette[i].peFlags = (colors[i] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1153 if (wic_palette)
1154 IWICPalette_Release(wic_palette);
1157 if (SUCCEEDED(hr))
1159 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1160 buffer, imginfo.Format, pitch,
1161 palette, &rect, dwFilter, Colorkey);
1164 HeapFree(GetProcessHeap(), 0, colors);
1165 HeapFree(GetProcessHeap(), 0, palette);
1166 HeapFree(GetProcessHeap(), 0, buffer);
1169 IWICBitmapFrameDecode_Release(bitmapframe);
1171 cleanup_bmp:
1172 IWICBitmapDecoder_Release(decoder);
1174 cleanup_err:
1175 if (factory)
1176 IWICImagingFactory_Release(factory);
1178 CoUninitialize();
1180 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1181 HeapFree(GetProcessHeap(), 0, (void*)pSrcData);
1183 if (FAILED(hr))
1184 return D3DXERR_INVALIDDATA;
1186 if (pSrcInfo)
1187 *pSrcInfo = imginfo;
1189 return D3D_OK;
1192 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
1193 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
1194 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1196 WCHAR *src_file_w;
1197 HRESULT hr;
1198 int strlength;
1200 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1201 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1202 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
1203 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1205 if (!src_file || !dst_surface)
1206 return D3DERR_INVALIDCALL;
1208 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
1209 src_file_w = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*src_file_w));
1210 MultiByteToWideChar(CP_ACP, 0, src_file, -1, src_file_w, strlength);
1212 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
1213 src_file_w, src_rect, filter, color_key, src_info);
1214 HeapFree(GetProcessHeap(), 0, src_file_w);
1216 return hr;
1219 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
1220 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
1221 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1223 UINT data_size;
1224 void *data;
1225 HRESULT hr;
1227 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1228 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1229 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
1230 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1232 if (!src_file || !dst_surface)
1233 return D3DERR_INVALIDCALL;
1235 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
1236 return D3DXERR_INVALIDDATA;
1238 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1239 data, data_size, src_rect, filter, color_key, src_info);
1240 UnmapViewOfFile(data);
1242 return hr;
1245 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
1246 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
1247 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1249 UINT data_size;
1250 HRSRC resinfo;
1251 void *data;
1253 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1254 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1255 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
1256 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1258 if (!dst_surface)
1259 return D3DERR_INVALIDCALL;
1261 if (!(resinfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA))
1262 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1263 && !(resinfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
1264 return D3DXERR_INVALIDDATA;
1266 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1267 return D3DXERR_INVALIDDATA;
1269 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1270 data, data_size, src_rect, filter, color_key, src_info);
1273 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
1274 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
1275 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1277 UINT data_size;
1278 HRSRC resinfo;
1279 void *data;
1281 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1282 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1283 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
1284 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1286 if (!dst_surface)
1287 return D3DERR_INVALIDCALL;
1289 if (!(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA))
1290 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1291 && !(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1292 return D3DXERR_INVALIDDATA;
1294 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1295 return D3DXERR_INVALIDDATA;
1297 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1298 data, data_size, src_rect, filter, color_key, src_info);
1302 /************************************************************
1303 * helper functions for D3DXLoadSurfaceFromMemory
1305 struct argb_conversion_info
1307 const struct pixel_format_desc *srcformat;
1308 const struct pixel_format_desc *destformat;
1309 DWORD srcshift[4], destshift[4];
1310 DWORD srcmask[4], destmask[4];
1311 BOOL process_channel[4];
1312 DWORD channelmask;
1315 static void init_argb_conversion_info(const struct pixel_format_desc *srcformat, const struct pixel_format_desc *destformat, struct argb_conversion_info *info)
1317 UINT i;
1318 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1319 info->channelmask = 0;
1321 info->srcformat = srcformat;
1322 info->destformat = destformat;
1324 for(i = 0;i < 4;i++) {
1325 /* srcshift is used to extract the _relevant_ components */
1326 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1328 /* destshift is used to move the components to the correct position */
1329 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1331 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1332 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1334 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1335 if(destformat->bits[i]) {
1336 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1337 else info->channelmask |= info->destmask[i];
1342 /************************************************************
1343 * get_relevant_argb_components
1345 * Extracts the relevant components from the source color and
1346 * drops the less significant bits if they aren't used by the destination format.
1348 static void get_relevant_argb_components(const struct argb_conversion_info *info, DWORD col, DWORD *out)
1350 UINT i = 0;
1351 for(;i < 4;i++)
1352 if(info->process_channel[i])
1353 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
1356 /************************************************************
1357 * make_argb_color
1359 * Recombines the output of get_relevant_argb_components and converts
1360 * it to the destination format.
1362 static DWORD make_argb_color(const struct argb_conversion_info *info, const DWORD *in)
1364 UINT i;
1365 DWORD val = 0;
1367 for(i = 0;i < 4;i++) {
1368 if(info->process_channel[i]) {
1369 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1370 signed int shift;
1371 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1372 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1375 val |= info->channelmask; /* new channels are set to their maximal value */
1376 return val;
1379 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */
1380 static void format_to_vec4(const struct pixel_format_desc *format, const BYTE *src, struct vec4 *dst)
1382 DWORD mask, tmp;
1383 unsigned int c;
1385 for (c = 0; c < 4; ++c)
1387 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1388 float *dst_component = (float *)dst + component_offsets[c];
1390 if (format->bits[c])
1392 mask = ~0u >> (32 - format->bits[c]);
1394 memcpy(&tmp, src + format->shift[c] / 8,
1395 min(sizeof(DWORD), (format->shift[c] % 8 + format->bits[c] + 7) / 8));
1397 if (format->type == FORMAT_ARGBF16)
1398 *dst_component = float_16_to_32(tmp);
1399 else if (format->type == FORMAT_ARGBF)
1400 *dst_component = *(float *)&tmp;
1401 else
1402 *dst_component = (float)((tmp >> format->shift[c] % 8) & mask) / mask;
1404 else
1405 *dst_component = 1.0f;
1409 /* It doesn't work for components bigger than 32 bits. */
1410 static void format_from_vec4(const struct pixel_format_desc *format, const struct vec4 *src, BYTE *dst)
1412 DWORD v, mask32;
1413 unsigned int c, i;
1415 memset(dst, 0, format->bytes_per_pixel);
1417 for (c = 0; c < 4; ++c)
1419 static const unsigned int component_offsets[4] = {3, 0, 1, 2};
1420 const float src_component = *((const float *)src + component_offsets[c]);
1422 if (!format->bits[c])
1423 continue;
1425 mask32 = ~0u >> (32 - format->bits[c]);
1427 if (format->type == FORMAT_ARGBF16)
1428 v = float_32_to_16(src_component);
1429 else if (format->type == FORMAT_ARGBF)
1430 v = *(DWORD *)&src_component;
1431 else
1432 v = (DWORD)(src_component * ((1 << format->bits[c]) - 1) + 0.5f);
1434 for (i = format->shift[c] / 8 * 8; i < format->shift[c] + format->bits[c]; i += 8)
1436 BYTE mask, byte;
1438 if (format->shift[c] > i)
1440 mask = mask32 << (format->shift[c] - i);
1441 byte = (v << (format->shift[c] - i)) & mask;
1443 else
1445 mask = mask32 >> (i - format->shift[c]);
1446 byte = (v >> (i - format->shift[c])) & mask;
1448 dst[i / 8] |= byte;
1453 /************************************************************
1454 * copy_pixels
1456 * Copies the source buffer to the destination buffer.
1457 * Works for any pixel format.
1458 * The source and the destination must be block-aligned.
1460 void copy_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
1461 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *size,
1462 const struct pixel_format_desc *format)
1464 UINT row, slice;
1465 BYTE *dst_addr;
1466 const BYTE *src_addr;
1467 UINT row_block_count = (size->width + format->block_width - 1) / format->block_width;
1468 UINT row_count = (size->height + format->block_height - 1) / format->block_height;
1470 for (slice = 0; slice < size->depth; slice++)
1472 src_addr = src + slice * src_slice_pitch;
1473 dst_addr = dst + slice * dst_slice_pitch;
1475 for (row = 0; row < row_count; row++)
1477 memcpy(dst_addr, src_addr, row_block_count * format->block_byte_count);
1478 src_addr += src_row_pitch;
1479 dst_addr += dst_row_pitch;
1484 /************************************************************
1485 * convert_argb_pixels
1487 * Copies the source buffer to the destination buffer, performing
1488 * any necessary format conversion and color keying.
1489 * Pixels outsize the source rect are blacked out.
1490 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1492 void convert_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1493 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1494 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1495 const PALETTEENTRY *palette)
1497 struct argb_conversion_info conv_info, ck_conv_info;
1498 const struct pixel_format_desc *ck_format = NULL;
1499 DWORD channels[4];
1500 UINT min_width, min_height, min_depth;
1501 UINT x, y, z;
1503 ZeroMemory(channels, sizeof(channels));
1504 init_argb_conversion_info(src_format, dst_format, &conv_info);
1506 min_width = min(src_size->width, dst_size->width);
1507 min_height = min(src_size->height, dst_size->height);
1508 min_depth = min(src_size->depth, dst_size->depth);
1510 if (color_key)
1512 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1513 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1514 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1517 for (z = 0; z < min_depth; z++) {
1518 const BYTE *src_slice_ptr = src + z * src_slice_pitch;
1519 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1521 for (y = 0; y < min_height; y++) {
1522 const BYTE *src_ptr = src_slice_ptr + y * src_row_pitch;
1523 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1525 for (x = 0; x < min_width; x++) {
1526 if (!src_format->to_rgba && !dst_format->from_rgba
1527 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1529 DWORD val;
1531 get_relevant_argb_components(&conv_info, *(DWORD *)src_ptr, channels);
1532 val = make_argb_color(&conv_info, channels);
1534 if (color_key)
1536 DWORD ck_pixel;
1538 get_relevant_argb_components(&ck_conv_info, *(DWORD *)src_ptr, channels);
1539 ck_pixel = make_argb_color(&ck_conv_info, channels);
1540 if (ck_pixel == color_key)
1541 val &= ~conv_info.destmask[0];
1543 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1545 else
1547 struct vec4 color, tmp;
1549 format_to_vec4(src_format, src_ptr, &color);
1550 if (src_format->to_rgba)
1551 src_format->to_rgba(&color, &tmp, palette);
1552 else
1553 tmp = color;
1555 if (ck_format)
1557 DWORD ck_pixel;
1559 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1560 if (ck_pixel == color_key)
1561 tmp.w = 0.0f;
1564 if (dst_format->from_rgba)
1565 dst_format->from_rgba(&tmp, &color);
1566 else
1567 color = tmp;
1569 format_from_vec4(dst_format, &color, dst_ptr);
1572 src_ptr += src_format->bytes_per_pixel;
1573 dst_ptr += dst_format->bytes_per_pixel;
1576 if (src_size->width < dst_size->width) /* black out remaining pixels */
1577 memset(dst_ptr, 0, dst_format->bytes_per_pixel * (dst_size->width - src_size->width));
1580 if (src_size->height < dst_size->height) /* black out remaining pixels */
1581 memset(dst + src_size->height * dst_row_pitch, 0, dst_row_pitch * (dst_size->height - src_size->height));
1583 if (src_size->depth < dst_size->depth) /* black out remaining pixels */
1584 memset(dst + src_size->depth * dst_slice_pitch, 0, dst_slice_pitch * (dst_size->depth - src_size->depth));
1587 /************************************************************
1588 * point_filter_argb_pixels
1590 * Copies the source buffer to the destination buffer, performing
1591 * any necessary format conversion, color keying and stretching
1592 * using a point filter.
1593 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1595 void point_filter_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1596 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1597 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1598 const PALETTEENTRY *palette)
1600 struct argb_conversion_info conv_info, ck_conv_info;
1601 const struct pixel_format_desc *ck_format = NULL;
1602 DWORD channels[4];
1603 UINT x, y, z;
1605 ZeroMemory(channels, sizeof(channels));
1606 init_argb_conversion_info(src_format, dst_format, &conv_info);
1608 if (color_key)
1610 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1611 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1612 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1615 for (z = 0; z < dst_size->depth; z++)
1617 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1618 const BYTE *src_slice_ptr = src + src_slice_pitch * (z * src_size->depth / dst_size->depth);
1620 for (y = 0; y < dst_size->height; y++)
1622 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1623 const BYTE *src_row_ptr = src_slice_ptr + src_row_pitch * (y * src_size->height / dst_size->height);
1625 for (x = 0; x < dst_size->width; x++)
1627 const BYTE *src_ptr = src_row_ptr + (x * src_size->width / dst_size->width) * src_format->bytes_per_pixel;
1629 if (!src_format->to_rgba && !dst_format->from_rgba
1630 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4)
1632 DWORD val;
1634 get_relevant_argb_components(&conv_info, *(DWORD *)src_ptr, channels);
1635 val = make_argb_color(&conv_info, channels);
1637 if (color_key)
1639 DWORD ck_pixel;
1641 get_relevant_argb_components(&ck_conv_info, *(DWORD *)src_ptr, channels);
1642 ck_pixel = make_argb_color(&ck_conv_info, channels);
1643 if (ck_pixel == color_key)
1644 val &= ~conv_info.destmask[0];
1646 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel);
1648 else
1650 struct vec4 color, tmp;
1652 format_to_vec4(src_format, src_ptr, &color);
1653 if (src_format->to_rgba)
1654 src_format->to_rgba(&color, &tmp, palette);
1655 else
1656 tmp = color;
1658 if (ck_format)
1660 DWORD ck_pixel;
1662 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel);
1663 if (ck_pixel == color_key)
1664 tmp.w = 0.0f;
1667 if (dst_format->from_rgba)
1668 dst_format->from_rgba(&tmp, &color);
1669 else
1670 color = tmp;
1672 format_from_vec4(dst_format, &color, dst_ptr);
1675 dst_ptr += dst_format->bytes_per_pixel;
1681 /************************************************************
1682 * D3DXLoadSurfaceFromMemory
1684 * Loads data from a given memory chunk into a surface,
1685 * applying any of the specified filters.
1687 * PARAMS
1688 * pDestSurface [I] pointer to the surface
1689 * pDestPalette [I] palette to use
1690 * pDestRect [I] to be filled area of the surface
1691 * pSrcMemory [I] pointer to the source data
1692 * SrcFormat [I] format of the source pixel data
1693 * SrcPitch [I] number of bytes in a row
1694 * pSrcPalette [I] palette used in the source image
1695 * pSrcRect [I] area of the source data to load
1696 * dwFilter [I] filter to apply on stretching
1697 * Colorkey [I] colorkey
1699 * RETURNS
1700 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1701 * if pSrcMemory is NULL but the other parameters are valid
1702 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1703 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1704 * if DestRect is invalid
1705 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1706 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1708 * NOTES
1709 * pSrcRect specifies the dimensions of the source data;
1710 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1713 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1714 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1715 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1716 DWORD filter, D3DCOLOR color_key)
1718 const struct pixel_format_desc *srcformatdesc, *destformatdesc;
1719 D3DSURFACE_DESC surfdesc;
1720 D3DLOCKED_RECT lockrect;
1721 struct volume src_size, dst_size;
1723 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1724 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1725 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1727 if (!dst_surface || !src_memory || !src_rect)
1729 WARN("Invalid argument specified.\n");
1730 return D3DERR_INVALIDCALL;
1732 if (src_format == D3DFMT_UNKNOWN
1733 || src_rect->left >= src_rect->right
1734 || src_rect->top >= src_rect->bottom)
1736 WARN("Invalid src_format or src_rect.\n");
1737 return E_FAIL;
1740 if (filter == D3DX_DEFAULT)
1741 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1743 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1745 src_size.width = src_rect->right - src_rect->left;
1746 src_size.height = src_rect->bottom - src_rect->top;
1747 src_size.depth = 1;
1748 if (!dst_rect)
1750 dst_size.width = surfdesc.Width;
1751 dst_size.height = surfdesc.Height;
1753 else
1755 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width
1756 || dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height
1757 || dst_rect->left < 0 || dst_rect->top < 0)
1759 WARN("Invalid dst_rect specified.\n");
1760 return D3DERR_INVALIDCALL;
1762 dst_size.width = dst_rect->right - dst_rect->left;
1763 dst_size.height = dst_rect->bottom - dst_rect->top;
1764 if (!dst_size.width || !dst_size.height)
1765 return D3D_OK;
1767 dst_size.depth = 1;
1769 srcformatdesc = get_format_info(src_format);
1770 destformatdesc = get_format_info(surfdesc.Format);
1771 if (srcformatdesc->type == FORMAT_UNKNOWN || destformatdesc->type == FORMAT_UNKNOWN)
1773 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format, surfdesc.Format);
1774 return E_NOTIMPL;
1777 if (src_format == surfdesc.Format
1778 && dst_size.width == src_size.width
1779 && dst_size.height == src_size.height
1780 && color_key == 0) /* Simple copy. */
1782 if (src_rect->left & (srcformatdesc->block_width - 1)
1783 || src_rect->top & (srcformatdesc->block_height - 1)
1784 || (src_rect->right & (srcformatdesc->block_width - 1)
1785 && src_size.width != surfdesc.Width)
1786 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1787 && src_size.height != surfdesc.Height))
1789 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1790 return D3DXERR_INVALIDDATA;
1793 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1794 return D3DXERR_INVALIDDATA;
1796 copy_pixels(src_memory, src_pitch, 0, lockrect.pBits, lockrect.Pitch, 0,
1797 &src_size, srcformatdesc);
1799 IDirect3DSurface9_UnlockRect(dst_surface);
1801 else /* Stretching or format conversion. */
1803 if (((srcformatdesc->type != FORMAT_ARGB) && (srcformatdesc->type != FORMAT_INDEX)) ||
1804 (destformatdesc->type != FORMAT_ARGB))
1806 FIXME("Format conversion missing %#x -> %#x\n", src_format, surfdesc.Format);
1807 return E_NOTIMPL;
1810 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1811 return D3DXERR_INVALIDDATA;
1813 if ((filter & 0xf) == D3DX_FILTER_NONE)
1815 convert_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1816 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
1818 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1820 if ((filter & 0xf) != D3DX_FILTER_POINT)
1821 FIXME("Unhandled filter %#x.\n", filter);
1823 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1824 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1825 point_filter_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1826 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
1829 IDirect3DSurface9_UnlockRect(dst_surface);
1832 return D3D_OK;
1835 /************************************************************
1836 * D3DXLoadSurfaceFromSurface
1838 * Copies the contents from one surface to another, performing any required
1839 * format conversion, resizing or filtering.
1841 * PARAMS
1842 * pDestSurface [I] pointer to the destination surface
1843 * pDestPalette [I] palette to use
1844 * pDestRect [I] to be filled area of the surface
1845 * pSrcSurface [I] pointer to the source surface
1846 * pSrcPalette [I] palette used for the source surface
1847 * pSrcRect [I] area of the source data to load
1848 * dwFilter [I] filter to apply on resizing
1849 * Colorkey [I] any ARGB value or 0 to disable color-keying
1851 * RETURNS
1852 * Success: D3D_OK
1853 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1854 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1857 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
1858 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
1859 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
1861 RECT rect;
1862 D3DLOCKED_RECT lock;
1863 D3DSURFACE_DESC SrcDesc;
1864 HRESULT hr;
1866 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1867 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1868 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
1869 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1871 if (!dst_surface || !src_surface)
1872 return D3DERR_INVALIDCALL;
1874 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
1876 if (!src_rect)
1877 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1878 else
1879 rect = *src_rect;
1881 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
1882 return D3DXERR_INVALIDDATA;
1884 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
1885 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
1887 IDirect3DSurface9_UnlockRect(src_surface);
1889 return hr;
1893 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1894 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1896 int len;
1897 WCHAR *filename;
1898 HRESULT hr;
1899 ID3DXBuffer *buffer;
1901 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1902 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1904 if (!dst_filename) return D3DERR_INVALIDCALL;
1906 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1907 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1908 if (!filename) return E_OUTOFMEMORY;
1909 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1911 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1912 if (SUCCEEDED(hr))
1914 hr = write_buffer_to_file(filename, buffer);
1915 ID3DXBuffer_Release(buffer);
1918 HeapFree(GetProcessHeap(), 0, filename);
1919 return hr;
1922 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1923 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1925 HRESULT hr;
1926 ID3DXBuffer *buffer;
1928 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1929 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1931 if (!dst_filename) return D3DERR_INVALIDCALL;
1933 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1934 if (SUCCEEDED(hr))
1936 hr = write_buffer_to_file(dst_filename, buffer);
1937 ID3DXBuffer_Release(buffer);
1940 return hr;
1943 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1944 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1946 IWICBitmapEncoder *encoder = NULL;
1947 IWICBitmapFrameEncode *frame = NULL;
1948 IPropertyBag2 *encoder_options = NULL;
1949 IStream *stream = NULL;
1950 HRESULT hr;
1951 HRESULT initresult;
1952 const CLSID *encoder_clsid;
1953 const GUID *pixel_format_guid;
1954 WICPixelFormatGUID wic_pixel_format;
1955 D3DFORMAT d3d_pixel_format;
1956 D3DSURFACE_DESC src_surface_desc;
1957 D3DLOCKED_RECT locked_rect;
1958 int width, height;
1959 STATSTG stream_stats;
1960 HGLOBAL stream_hglobal;
1961 ID3DXBuffer *buffer;
1962 DWORD size;
1964 TRACE("(%p, %#x, %p, %p, %s)\n",
1965 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1967 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
1969 if (src_palette)
1971 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1972 return D3DERR_INVALIDCALL;
1975 switch (file_format)
1977 case D3DXIFF_BMP:
1978 case D3DXIFF_DIB:
1979 encoder_clsid = &CLSID_WICBmpEncoder;
1980 break;
1981 case D3DXIFF_PNG:
1982 encoder_clsid = &CLSID_WICPngEncoder;
1983 break;
1984 case D3DXIFF_JPG:
1985 encoder_clsid = &CLSID_WICJpegEncoder;
1986 break;
1987 case D3DXIFF_DDS:
1988 return save_dds_surface_to_memory(dst_buffer, src_surface, src_rect);
1989 case D3DXIFF_HDR:
1990 case D3DXIFF_PFM:
1991 case D3DXIFF_TGA:
1992 case D3DXIFF_PPM:
1993 FIXME("File format %#x is not supported yet\n", file_format);
1994 return E_NOTIMPL;
1995 default:
1996 return D3DERR_INVALIDCALL;
1999 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
2000 if (src_rect)
2002 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
2004 WARN("Invalid rectangle with 0 area\n");
2005 return D3DXCreateBuffer(64, dst_buffer);
2007 if (src_rect->left < 0 || src_rect->top < 0)
2008 return D3DERR_INVALIDCALL;
2009 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
2010 return D3DERR_INVALIDCALL;
2011 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
2012 return D3DERR_INVALIDCALL;
2014 width = src_rect->right - src_rect->left;
2015 height = src_rect->bottom - src_rect->top;
2017 else
2019 width = src_surface_desc.Width;
2020 height = src_surface_desc.Height;
2023 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2025 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
2026 &IID_IWICBitmapEncoder, (void **)&encoder);
2027 if (FAILED(hr)) goto cleanup_err;
2029 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
2030 if (FAILED(hr)) goto cleanup_err;
2032 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2033 if (FAILED(hr)) goto cleanup_err;
2035 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
2036 if (FAILED(hr)) goto cleanup_err;
2038 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
2039 if (FAILED(hr)) goto cleanup_err;
2041 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
2042 if (FAILED(hr)) goto cleanup_err;
2044 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
2045 if (!pixel_format_guid)
2047 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
2048 hr = E_NOTIMPL;
2049 goto cleanup;
2052 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
2053 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
2054 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
2055 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
2057 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
2059 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
2061 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2062 if (SUCCEEDED(hr))
2064 IWICBitmapFrameEncode_WritePixels(frame, height,
2065 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
2066 IDirect3DSurface9_UnlockRect(src_surface);
2069 else /* Pixel format conversion */
2071 const struct pixel_format_desc *src_format_desc, *dst_format_desc;
2072 struct volume size;
2073 DWORD dst_pitch;
2074 void *dst_data;
2076 src_format_desc = get_format_info(src_surface_desc.Format);
2077 dst_format_desc = get_format_info(d3d_pixel_format);
2078 if (src_format_desc->type != FORMAT_ARGB || dst_format_desc->type != FORMAT_ARGB)
2080 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
2081 src_surface_desc.Format, d3d_pixel_format);
2082 hr = E_NOTIMPL;
2083 goto cleanup;
2086 size.width = width;
2087 size.height = height;
2088 size.depth = 1;
2089 dst_pitch = width * dst_format_desc->bytes_per_pixel;
2090 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
2091 if (!dst_data)
2093 hr = E_OUTOFMEMORY;
2094 goto cleanup;
2097 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2098 if (SUCCEEDED(hr))
2100 convert_argb_pixels(locked_rect.pBits, locked_rect.Pitch, 0, &size, src_format_desc,
2101 dst_data, dst_pitch, 0, &size, dst_format_desc, 0, NULL);
2102 IDirect3DSurface9_UnlockRect(src_surface);
2105 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
2106 HeapFree(GetProcessHeap(), 0, dst_data);
2109 hr = IWICBitmapFrameEncode_Commit(frame);
2110 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
2112 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
2114 /* copy data from stream to ID3DXBuffer */
2115 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
2116 if (FAILED(hr)) goto cleanup_err;
2118 if (stream_stats.cbSize.u.HighPart != 0)
2120 hr = D3DXERR_INVALIDDATA;
2121 goto cleanup;
2123 size = stream_stats.cbSize.u.LowPart;
2125 /* Remove BMP header for DIB */
2126 if (file_format == D3DXIFF_DIB)
2127 size -= sizeof(BITMAPFILEHEADER);
2129 hr = D3DXCreateBuffer(size, &buffer);
2130 if (FAILED(hr)) goto cleanup;
2132 hr = GetHGlobalFromStream(stream, &stream_hglobal);
2133 if (SUCCEEDED(hr))
2135 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
2136 void *stream_data = GlobalLock(stream_hglobal);
2137 /* Remove BMP header for DIB */
2138 if (file_format == D3DXIFF_DIB)
2139 stream_data = (void*)((BYTE*)stream_data + sizeof(BITMAPFILEHEADER));
2140 memcpy(buffer_pointer, stream_data, size);
2141 GlobalUnlock(stream_hglobal);
2142 *dst_buffer = buffer;
2144 else ID3DXBuffer_Release(buffer);
2146 cleanup_err:
2147 if (FAILED(hr) && hr != E_OUTOFMEMORY)
2148 hr = D3DERR_INVALIDCALL;
2150 cleanup:
2151 if (stream) IStream_Release(stream);
2153 if (frame) IWICBitmapFrameEncode_Release(frame);
2154 if (encoder_options) IPropertyBag2_Release(encoder_options);
2156 if (encoder) IWICBitmapEncoder_Release(encoder);
2158 if (SUCCEEDED(initresult)) CoUninitialize();
2160 return hr;