a3ba192e4e6f62f1307e573d2816582025a4ad2e
[reactos.git] / reactos / dll / directx / wine / d3dx9_36 / surface.c
1 /*
2 * Copyright (C) 2009-2010 Tony Wasserka
3 * Copyright (C) 2012 Jأ³zef Kucia
4 *
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.
9 *
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.
14 *
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
18 *
19 */
20
21 #include <wine/debug.h>
22 //#include "wine/unicode.h"
23 #include "d3dx9_36_private.h"
24
25 #include <initguid.h>
26 #include <ole2.h>
27 #include <wincodec.h>
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
30
31
32 /* Wine-specific WIC GUIDs */
33 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
34
35 static const struct
36 {
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 }
48 };
49
50 static D3DFORMAT wic_guid_to_d3dformat(const GUID *guid)
51 {
52 unsigned int i;
53
54 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
55 {
56 if (IsEqualGUID(wic_pixel_formats[i].wic_guid, guid))
57 return wic_pixel_formats[i].d3dformat;
58 }
59
60 return D3DFMT_UNKNOWN;
61 }
62
63 static const GUID *d3dformat_to_wic_guid(D3DFORMAT format)
64 {
65 unsigned int i;
66
67 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
68 {
69 if (wic_pixel_formats[i].d3dformat == format)
70 return wic_pixel_formats[i].wic_guid;
71 }
72
73 return NULL;
74 }
75
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
85
86 /* dds_header.caps */
87 #define DDS_CAPS_COMPLEX 0x8
88 #define DDS_CAPS_TEXTURE 0x1000
89 #define DDS_CAPS_MIPMAP 0x400000
90
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
103
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
112
113 struct dds_pixel_format
114 {
115 DWORD size;
116 DWORD flags;
117 DWORD fourcc;
118 DWORD bpp;
119 DWORD rmask;
120 DWORD gmask;
121 DWORD bmask;
122 DWORD amask;
123 };
124
125 struct dds_header
126 {
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;
142 };
143
144 static D3DFORMAT dds_fourcc_to_d3dformat(DWORD fourcc)
145 {
146 unsigned int i;
147 static const DWORD known_fourcc[] = {
148 MAKEFOURCC('U','Y','V','Y'),
149 MAKEFOURCC('Y','U','Y','2'),
150 MAKEFOURCC('R','G','B','G'),
151 MAKEFOURCC('G','R','G','B'),
152 MAKEFOURCC('D','X','T','1'),
153 MAKEFOURCC('D','X','T','2'),
154 MAKEFOURCC('D','X','T','3'),
155 MAKEFOURCC('D','X','T','4'),
156 MAKEFOURCC('D','X','T','5')
157 };
158
159 for (i = 0; i < sizeof(known_fourcc) / sizeof(known_fourcc[0]); i++)
160 {
161 if (known_fourcc[i] == fourcc)
162 return fourcc;
163 }
164
165 WARN("Unknown FourCC %#x\n", fourcc);
166 return D3DFMT_UNKNOWN;
167 }
168
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 },
192 };
193
194 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
195 {
196 unsigned int i;
197
198 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
199 {
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)
204 {
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;
209 }
210 }
211
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;
215 }
216
217 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
218 {
219 if (pixel_format->bpp == 8)
220 {
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;
225 }
226 if (pixel_format->bpp == 16)
227 {
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;
232 }
233
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;
237 }
238
239 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
240 {
241 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
242 return D3DFMT_A8;
243
244 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format->bpp, pixel_format->rmask);
245 return D3DFMT_UNKNOWN;
246 }
247
248 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
249 {
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;
254
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;
258 }
259
260 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
261 {
262 if (pixel_format->flags & DDS_PF_FOURCC)
263 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
264 if (pixel_format->flags & DDS_PF_RGB)
265 return dds_rgb_to_d3dformat(pixel_format);
266 if (pixel_format->flags & DDS_PF_LUMINANCE)
267 return dds_luminance_to_d3dformat(pixel_format);
268 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
269 return dds_alpha_to_d3dformat(pixel_format);
270 if (pixel_format->flags & DDS_PF_BUMPDUDV)
271 return dds_bump_to_d3dformat(pixel_format);
272
273 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n",
274 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
275 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
276 return D3DFMT_UNKNOWN;
277 }
278
279 static HRESULT d3dformat_to_dds_pixel_format(struct dds_pixel_format *pixel_format, D3DFORMAT d3dformat)
280 {
281 unsigned int i;
282
283 memset(pixel_format, 0, sizeof(*pixel_format));
284
285 pixel_format->size = sizeof(*pixel_format);
286
287 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
288 {
289 if (rgb_pixel_formats[i].format == d3dformat)
290 {
291 pixel_format->flags |= DDS_PF_RGB;
292 pixel_format->bpp = rgb_pixel_formats[i].bpp;
293 pixel_format->rmask = rgb_pixel_formats[i].rmask;
294 pixel_format->gmask = rgb_pixel_formats[i].gmask;
295 pixel_format->bmask = rgb_pixel_formats[i].bmask;
296 pixel_format->amask = rgb_pixel_formats[i].amask;
297 if (pixel_format->amask) pixel_format->flags |= DDS_PF_ALPHA;
298 return D3D_OK;
299 }
300 }
301
302 WARN("Unknown pixel format %#x\n", d3dformat);
303 return E_NOTIMPL;
304 }
305
306 static HRESULT calculate_dds_surface_size(D3DFORMAT format, UINT width, UINT height,
307 UINT *pitch, UINT *size)
308 {
309 const struct pixel_format_desc *format_desc = get_format_info(format);
310 if (format_desc->type == FORMAT_UNKNOWN)
311 return E_NOTIMPL;
312
313 if (format_desc->block_width != 1 || format_desc->block_height != 1)
314 {
315 *pitch = format_desc->block_byte_count
316 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
317 *size = *pitch
318 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
319 }
320 else
321 {
322 *pitch = width * format_desc->bytes_per_pixel;
323 *size = *pitch * height;
324 }
325
326 return D3D_OK;
327 }
328
329 static UINT calculate_dds_file_size(D3DFORMAT format, UINT width, UINT height, UINT depth,
330 UINT miplevels, UINT faces)
331 {
332 UINT i, file_size = 0;
333
334 for (i = 0; i < miplevels; i++)
335 {
336 UINT pitch, size = 0;
337 calculate_dds_surface_size(format, width, height, &pitch, &size);
338 size *= depth;
339 file_size += size;
340 width = max(1, width / 2);
341 height = max(1, height / 2);
342 depth = max(1, depth / 2);
343 }
344
345 file_size *= faces;
346 file_size += sizeof(struct dds_header);
347 return file_size;
348 }
349
350 /************************************************************
351 * get_image_info_from_dds
352 *
353 * Fills a D3DXIMAGE_INFO structure with information
354 * about a DDS file stored in the memory.
355 *
356 * PARAMS
357 * buffer [I] pointer to DDS data
358 * length [I] size of DDS data
359 * info [O] pointer to D3DXIMAGE_INFO structure
360 *
361 * RETURNS
362 * Success: D3D_OK
363 * Failure: D3DXERR_INVALIDDATA
364 *
365 */
366 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
367 {
368 UINT faces = 1;
369 UINT expected_length;
370 const struct dds_header *header = buffer;
371
372 if (length < sizeof(*header) || !info)
373 return D3DXERR_INVALIDDATA;
374
375 if (header->pixel_format.size != sizeof(header->pixel_format))
376 return D3DXERR_INVALIDDATA;
377
378 info->Width = header->width;
379 info->Height = header->height;
380 info->Depth = 1;
381 info->MipLevels = (header->flags & DDS_MIPMAPCOUNT) ? header->miplevels : 1;
382
383 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
384 if (info->Format == D3DFMT_UNKNOWN)
385 return D3DXERR_INVALIDDATA;
386
387 TRACE("Pixel format is %#x\n", info->Format);
388
389 if (header->caps2 & DDS_CAPS2_VOLUME)
390 {
391 info->Depth = header->depth;
392 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
393 }
394 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
395 {
396 DWORD face;
397 faces = 0;
398 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
399 {
400 if (header->caps2 & face)
401 faces++;
402 }
403 info->ResourceType = D3DRTYPE_CUBETEXTURE;
404 }
405 else
406 {
407 info->ResourceType = D3DRTYPE_TEXTURE;
408 }
409
410 expected_length = calculate_dds_file_size(info->Format, info->Width, info->Height, info->Depth,
411 info->MipLevels, faces);
412 if (length < expected_length)
413 {
414 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length);
415 return D3DXERR_INVALIDDATA;
416 }
417
418 info->ImageFileFormat = D3DXIFF_DDS;
419 return D3D_OK;
420 }
421
422 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
423 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
424 const D3DXIMAGE_INFO *src_info)
425 {
426 UINT size;
427 UINT src_pitch;
428 const struct dds_header *header = src_data;
429 const BYTE *pixels = (BYTE *)(header + 1);
430
431 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
432 return D3DXERR_INVALIDDATA;
433
434 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &src_pitch, &size)))
435 return E_NOTIMPL;
436
437 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
438 src_pitch, NULL, src_rect, filter, color_key);
439 }
440
441 static HRESULT save_dds_surface_to_memory(ID3DXBuffer **dst_buffer, IDirect3DSurface9 *src_surface, const RECT *src_rect)
442 {
443 HRESULT hr;
444 UINT dst_pitch, surface_size, file_size;
445 D3DSURFACE_DESC src_desc;
446 D3DLOCKED_RECT locked_rect;
447 ID3DXBuffer *buffer;
448 struct dds_header *header;
449 BYTE *pixels;
450 struct volume volume;
451 const struct pixel_format_desc *pixel_format;
452
453 if (src_rect)
454 {
455 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
456 return E_NOTIMPL;
457 }
458
459 hr = IDirect3DSurface9_GetDesc(src_surface, &src_desc);
460 if (FAILED(hr)) return hr;
461
462 pixel_format = get_format_info(src_desc.Format);
463 if (pixel_format->type == FORMAT_UNKNOWN) return E_NOTIMPL;
464
465 file_size = calculate_dds_file_size(src_desc.Format, src_desc.Width, src_desc.Height, 1, 1, 1);
466
467 hr = calculate_dds_surface_size(src_desc.Format, src_desc.Width, src_desc.Height, &dst_pitch, &surface_size);
468 if (FAILED(hr)) return hr;
469
470 hr = D3DXCreateBuffer(file_size, &buffer);
471 if (FAILED(hr)) return hr;
472
473 header = ID3DXBuffer_GetBufferPointer(buffer);
474 pixels = (BYTE *)(header + 1);
475
476 memset(header, 0, sizeof(*header));
477 header->signature = MAKEFOURCC('D','D','S',' ');
478 header->size = sizeof(*header);
479 header->flags = DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PITCH | DDS_PIXELFORMAT | DDS_MIPMAPCOUNT;
480 header->height = src_desc.Height;
481 header->width = src_desc.Width;
482 header->pitch_or_linear_size = dst_pitch;
483 header->depth = 1;
484 header->miplevels = 1;
485 header->caps = DDS_CAPS_TEXTURE;
486 hr = d3dformat_to_dds_pixel_format(&header->pixel_format, src_desc.Format);
487 if (FAILED(hr))
488 {
489 ID3DXBuffer_Release(buffer);
490 return hr;
491 }
492
493 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, NULL, D3DLOCK_READONLY);
494 if (FAILED(hr))
495 {
496 ID3DXBuffer_Release(buffer);
497 return hr;
498 }
499
500 volume.width = src_desc.Width;
501 volume.height = src_desc.Height;
502 volume.depth = 1;
503 copy_pixels(locked_rect.pBits, locked_rect.Pitch, 0, pixels, dst_pitch, 0,
504 &volume, pixel_format);
505
506 IDirect3DSurface9_UnlockRect(src_surface);
507
508 *dst_buffer = buffer;
509 return D3D_OK;
510 }
511
512 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette,
513 const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key,
514 const D3DXIMAGE_INFO *src_info)
515 {
516 UINT row_pitch, slice_pitch;
517 const struct dds_header *header = src_data;
518 const BYTE *pixels = (BYTE *)(header + 1);
519
520 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
521 return D3DXERR_INVALIDDATA;
522
523 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &row_pitch, &slice_pitch)))
524 return E_NOTIMPL;
525
526 return D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box, pixels, src_info->Format,
527 row_pitch, slice_pitch, NULL, src_box, filter, color_key);
528 }
529
530 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
531 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info)
532
533 {
534 HRESULT hr;
535 RECT src_rect;
536 UINT src_pitch;
537 UINT mip_level;
538 UINT mip_levels;
539 UINT mip_level_size;
540 UINT width, height;
541 IDirect3DSurface9 *surface;
542 const struct dds_header *header = src_data;
543 const BYTE *pixels = (BYTE *)(header + 1);
544
545 /* Loading a cube texture as a simple texture is also supported (only first face texture is taken) */
546 if ((src_info->ResourceType != D3DRTYPE_TEXTURE) && (src_info->ResourceType != D3DRTYPE_CUBETEXTURE))
547 return D3DXERR_INVALIDDATA;
548
549 width = src_info->Width;
550 height = src_info->Height;
551 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
552 for (mip_level = 0; mip_level < mip_levels; mip_level++)
553 {
554 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_pitch, &mip_level_size);
555 if (FAILED(hr)) return hr;
556
557 SetRect(&src_rect, 0, 0, width, height);
558
559 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level, &surface);
560 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
561 NULL, &src_rect, filter, color_key);
562 IDirect3DSurface9_Release(surface);
563 if (FAILED(hr)) return hr;
564
565 pixels += mip_level_size;
566 width = max(1, width / 2);
567 height = max(1, height / 2);
568 }
569
570 return D3D_OK;
571 }
572
573 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
574 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
575 {
576 HRESULT hr;
577 int face;
578 UINT mip_level;
579 UINT size;
580 RECT src_rect;
581 UINT src_pitch;
582 UINT mip_levels;
583 UINT mip_level_size;
584 IDirect3DSurface9 *surface;
585 const struct dds_header *header = src_data;
586 const BYTE *pixels = (BYTE *)(header + 1);
587
588 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
589 return D3DXERR_INVALIDDATA;
590
591 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
592 {
593 WARN("Only full cubemaps are supported\n");
594 return D3DXERR_INVALIDDATA;
595 }
596
597 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
598 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
599 {
600 size = src_info->Width;
601 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
602 {
603 hr = calculate_dds_surface_size(src_info->Format, size, size, &src_pitch, &mip_level_size);
604 if (FAILED(hr)) return hr;
605
606 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
607 if (mip_level < mip_levels)
608 {
609 SetRect(&src_rect, 0, 0, size, size);
610
611 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
612 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
613 NULL, &src_rect, filter, color_key);
614 IDirect3DSurface9_Release(surface);
615 if (FAILED(hr)) return hr;
616 }
617
618 pixels += mip_level_size;
619 size = max(1, size / 2);
620 }
621 }
622
623 return D3D_OK;
624 }
625
626 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
627 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
628 {
629 HRESULT hr;
630 UINT mip_level;
631 UINT mip_levels;
632 UINT src_slice_pitch;
633 UINT src_row_pitch;
634 D3DBOX src_box;
635 UINT width, height, depth;
636 IDirect3DVolume9 *volume;
637 const struct dds_header *header = src_data;
638 const BYTE *pixels = (BYTE *)(header + 1);
639
640 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)
641 return D3DXERR_INVALIDDATA;
642
643 width = src_info->Width;
644 height = src_info->Height;
645 depth = src_info->Depth;
646 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture));
647
648 for (mip_level = 0; mip_level < mip_levels; mip_level++)
649 {
650 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_row_pitch, &src_slice_pitch);
651 if (FAILED(hr)) return hr;
652
653 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume);
654 if (FAILED(hr)) return hr;
655
656 src_box.Left = 0;
657 src_box.Top = 0;
658 src_box.Right = width;
659 src_box.Bottom = height;
660 src_box.Front = 0;
661 src_box.Back = depth;
662
663 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format,
664 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key);
665
666 IDirect3DVolume9_Release(volume);
667 if (FAILED(hr)) return hr;
668
669 pixels += depth * src_slice_pitch;
670 width = max(1, width / 2);
671 height = max(1, height / 2);
672 depth = max(1, depth / 2);
673 }
674
675 return D3D_OK;
676 }
677
678 static BOOL convert_dib_to_bmp(void **data, UINT *size)
679 {
680 ULONG header_size;
681 ULONG count = 0;
682 ULONG offset;
683 BITMAPFILEHEADER *header;
684 BYTE *new_data;
685 UINT new_size;
686
687 if ((*size < 4) || (*size < (header_size = *(ULONG*)*data)))
688 return FALSE;
689
690 if ((header_size == sizeof(BITMAPINFOHEADER)) ||
691 (header_size == sizeof(BITMAPV4HEADER)) ||
692 (header_size == sizeof(BITMAPV5HEADER)) ||
693 (header_size == 64 /* sizeof(BITMAPCOREHEADER2) */))
694 {
695 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
696 BITMAPINFOHEADER *info_header = (BITMAPINFOHEADER*)*data;
697 count = info_header->biClrUsed;
698
699 if (!count && info_header->biBitCount <= 8)
700 count = 1 << info_header->biBitCount;
701
702 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBQUAD) * count;
703
704 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
705 if ((info_header->biSize == sizeof(BITMAPINFOHEADER)) && (info_header->biCompression == BI_BITFIELDS))
706 offset += 3 * sizeof(DWORD);
707 }
708 else if (header_size == sizeof(BITMAPCOREHEADER))
709 {
710 BITMAPCOREHEADER *core_header = (BITMAPCOREHEADER*)*data;
711
712 if (core_header->bcBitCount <= 8)
713 count = 1 << core_header->bcBitCount;
714
715 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBTRIPLE) * count;
716 }
717 else
718 {
719 return FALSE;
720 }
721
722 TRACE("Converting DIB file to BMP\n");
723
724 new_size = *size + sizeof(BITMAPFILEHEADER);
725 new_data = HeapAlloc(GetProcessHeap(), 0, new_size);
726 CopyMemory(new_data + sizeof(BITMAPFILEHEADER), *data, *size);
727
728 /* Add BMP header */
729 header = (BITMAPFILEHEADER*)new_data;
730 header->bfType = 0x4d42; /* BM */
731 header->bfSize = new_size;
732 header->bfReserved1 = 0;
733 header->bfReserved2 = 0;
734 header->bfOffBits = offset;
735
736 /* Update input data */
737 *data = new_data;
738 *size = new_size;
739
740 return TRUE;
741 }
742
743 /************************************************************
744 * D3DXGetImageInfoFromFileInMemory
745 *
746 * Fills a D3DXIMAGE_INFO structure with info about an image
747 *
748 * PARAMS
749 * data [I] pointer to the image file data
750 * datasize [I] size of the passed data
751 * info [O] pointer to the destination structure
752 *
753 * RETURNS
754 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
755 * if info is NULL and data and datasize are not NULL
756 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
757 * D3DERR_INVALIDCALL, if data is NULL or
758 * if datasize is 0
759 *
760 * NOTES
761 * datasize may be bigger than the actual file size
762 *
763 */
764 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(const void *data, UINT datasize, D3DXIMAGE_INFO *info)
765 {
766 IWICImagingFactory *factory;
767 IWICBitmapDecoder *decoder = NULL;
768 IWICStream *stream;
769 HRESULT hr;
770 HRESULT initresult;
771 BOOL dib;
772
773 TRACE("(%p, %d, %p)\n", data, datasize, info);
774
775 if (!data || !datasize)
776 return D3DERR_INVALIDCALL;
777
778 if (!info)
779 return D3D_OK;
780
781 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
782 TRACE("File type is DDS\n");
783 return get_image_info_from_dds(data, datasize, info);
784 }
785
786 /* In case of DIB file, convert it to BMP */
787 dib = convert_dib_to_bmp((void**)&data, &datasize);
788
789 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
790
791 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
792
793 if (SUCCEEDED(hr)) {
794 IWICImagingFactory_CreateStream(factory, &stream);
795 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
796 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
797 IWICStream_Release(stream);
798 IWICImagingFactory_Release(factory);
799 }
800
801 if (FAILED(hr)) {
802 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
803 FIXME("File type PPM is not supported yet\n");
804 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
805 FIXME("File type HDR is not supported yet\n");
806 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
807 FIXME("File type PFM is not supported yet\n");
808 }
809
810 if (SUCCEEDED(hr)) {
811 GUID container_format;
812 UINT frame_count;
813
814 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
815 if (SUCCEEDED(hr)) {
816 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
817 if (dib) {
818 TRACE("File type is DIB\n");
819 info->ImageFileFormat = D3DXIFF_DIB;
820 } else {
821 TRACE("File type is BMP\n");
822 info->ImageFileFormat = D3DXIFF_BMP;
823 }
824 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
825 TRACE("File type is PNG\n");
826 info->ImageFileFormat = D3DXIFF_PNG;
827 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
828 TRACE("File type is JPG\n");
829 info->ImageFileFormat = D3DXIFF_JPG;
830 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
831 TRACE("File type is TGA\n");
832 info->ImageFileFormat = D3DXIFF_TGA;
833 } else {
834 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
835 hr = D3DXERR_INVALIDDATA;
836 }
837 }
838
839 if (SUCCEEDED(hr))
840 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
841 if (SUCCEEDED(hr) && !frame_count)
842 hr = D3DXERR_INVALIDDATA;
843
844 if (SUCCEEDED(hr)) {
845 IWICBitmapFrameDecode *frame = NULL;
846
847 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
848
849 if (SUCCEEDED(hr))
850 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
851
852 if (SUCCEEDED(hr)) {
853 WICPixelFormatGUID pixel_format;
854
855 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
856 if (SUCCEEDED(hr)) {
857 info->Format = wic_guid_to_d3dformat(&pixel_format);
858 if (info->Format == D3DFMT_UNKNOWN) {
859 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
860 hr = D3DXERR_INVALIDDATA;
861 }
862 }
863 }
864
865 if (frame)
866 IWICBitmapFrameDecode_Release(frame);
867
868 info->Depth = 1;
869 info->MipLevels = 1;
870 info->ResourceType = D3DRTYPE_TEXTURE;
871 }
872 }
873
874 if (decoder)
875 IWICBitmapDecoder_Release(decoder);
876
877 if (SUCCEEDED(initresult))
878 CoUninitialize();
879
880 if (dib)
881 HeapFree(GetProcessHeap(), 0, (void*)data);
882
883 if (FAILED(hr)) {
884 TRACE("Invalid or unsupported image file\n");
885 return D3DXERR_INVALIDDATA;
886 }
887
888 return D3D_OK;
889 }
890
891 /************************************************************
892 * D3DXGetImageInfoFromFile
893 *
894 * RETURNS
895 * Success: D3D_OK, if we successfully load a valid image file or
896 * if we successfully load a file which is no valid image and info is NULL
897 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
898 * if file is not a valid image file and info is not NULL
899 * D3DERR_INVALIDCALL, if file is NULL
900 *
901 */
902 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
903 {
904 LPWSTR widename;
905 HRESULT hr;
906 int strlength;
907
908 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
909
910 if( !file ) return D3DERR_INVALIDCALL;
911
912 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
913 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename));
914 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
915
916 hr = D3DXGetImageInfoFromFileW(widename, info);
917 HeapFree(GetProcessHeap(), 0, widename);
918
919 return hr;
920 }
921
922 HRESULT WINAPI D3DXGetImageInfoFromFileW(const WCHAR *file, D3DXIMAGE_INFO *info)
923 {
924 void *buffer;
925 HRESULT hr;
926 DWORD size;
927
928 TRACE("file %s, info %p.\n", debugstr_w(file), info);
929
930 if (!file)
931 return D3DERR_INVALIDCALL;
932
933 if (FAILED(map_view_of_file(file, &buffer, &size)))
934 return D3DXERR_INVALIDDATA;
935
936 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
937 UnmapViewOfFile(buffer);
938
939 return hr;
940 }
941
942 /************************************************************
943 * D3DXGetImageInfoFromResource
944 *
945 * RETURNS
946 * Success: D3D_OK, if resource is a valid image file
947 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
948 * if we fail to load resource
949 *
950 */
951 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, const char *resource, D3DXIMAGE_INFO *info)
952 {
953 HRSRC resinfo;
954 void *buffer;
955 DWORD size;
956
957 TRACE("module %p, resource %s, info %p.\n", module, debugstr_a(resource), info);
958
959 if (!(resinfo = FindResourceA(module, resource, (const char *)RT_RCDATA))
960 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
961 && !(resinfo = FindResourceA(module, resource, (const char *)RT_BITMAP)))
962 return D3DXERR_INVALIDDATA;
963
964 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
965 return D3DXERR_INVALIDDATA;
966
967 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
968 }
969
970 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, const WCHAR *resource, D3DXIMAGE_INFO *info)
971 {
972 HRSRC resinfo;
973 void *buffer;
974 DWORD size;
975
976 TRACE("module %p, resource %s, info %p.\n", module, debugstr_w(resource), info);
977
978 if (!(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA))
979 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
980 && !(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_BITMAP)))
981 return D3DXERR_INVALIDDATA;
982
983 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size)))
984 return D3DXERR_INVALIDDATA;
985
986 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
987 }
988
989 /************************************************************
990 * D3DXLoadSurfaceFromFileInMemory
991 *
992 * Loads data from a given buffer into a surface and fills a given
993 * D3DXIMAGE_INFO structure with info about the source data.
994 *
995 * PARAMS
996 * pDestSurface [I] pointer to the surface
997 * pDestPalette [I] palette to use
998 * pDestRect [I] to be filled area of the surface
999 * pSrcData [I] pointer to the source data
1000 * SrcDataSize [I] size of the source data in bytes
1001 * pSrcRect [I] area of the source data to load
1002 * dwFilter [I] filter to apply on stretching
1003 * Colorkey [I] colorkey
1004 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1005 *
1006 * RETURNS
1007 * Success: D3D_OK
1008 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
1009 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1010 *
1011 */
1012 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface,
1013 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize,
1014 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo)
1015 {
1016 D3DXIMAGE_INFO imginfo;
1017 HRESULT hr;
1018
1019 IWICImagingFactory *factory = NULL;
1020 IWICBitmapDecoder *decoder;
1021 IWICBitmapFrameDecode *bitmapframe;
1022 IWICStream *stream;
1023
1024 const struct pixel_format_desc *formatdesc;
1025 WICRect wicrect;
1026 RECT rect;
1027
1028 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1029 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1030 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize,
1031 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo);
1032
1033 if (!pDestSurface || !pSrcData || !SrcDataSize)
1034 return D3DERR_INVALIDCALL;
1035
1036 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
1037
1038 if (FAILED(hr))
1039 return hr;
1040
1041 if (pSrcRect)
1042 {
1043 wicrect.X = pSrcRect->left;
1044 wicrect.Y = pSrcRect->top;
1045 wicrect.Width = pSrcRect->right - pSrcRect->left;
1046 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
1047 }
1048 else
1049 {
1050 wicrect.X = 0;
1051 wicrect.Y = 0;
1052 wicrect.Width = imginfo.Width;
1053 wicrect.Height = imginfo.Height;
1054 }
1055
1056 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
1057
1058 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
1059 {
1060 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
1061 dwFilter, Colorkey, &imginfo);
1062 if (SUCCEEDED(hr) && pSrcInfo)
1063 *pSrcInfo = imginfo;
1064 return hr;
1065 }
1066
1067 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1068 convert_dib_to_bmp((void**)&pSrcData, &SrcDataSize);
1069
1070 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1071
1072 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
1073 goto cleanup_err;
1074
1075 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
1076 {
1077 IWICImagingFactory_Release(factory);
1078 factory = NULL;
1079 goto cleanup_err;
1080 }
1081
1082 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
1083
1084 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
1085
1086 IWICStream_Release(stream);
1087
1088 if (FAILED(hr))
1089 goto cleanup_err;
1090
1091 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
1092
1093 if (FAILED(hr))
1094 goto cleanup_bmp;
1095
1096 formatdesc = get_format_info(imginfo.Format);
1097
1098 if (formatdesc->type == FORMAT_UNKNOWN)
1099 {
1100 FIXME("Unsupported pixel format\n");
1101 hr = D3DXERR_INVALIDDATA;
1102 }
1103 else
1104 {
1105 BYTE *buffer;
1106 DWORD pitch;
1107 PALETTEENTRY *palette = NULL;
1108 WICColor *colors = NULL;
1109
1110 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
1111 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
1112
1113 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
1114 pitch * wicrect.Height, buffer);
1115
1116 if (SUCCEEDED(hr) && (formatdesc->type == FORMAT_INDEX))
1117 {
1118 IWICPalette *wic_palette = NULL;
1119 UINT nb_colors;
1120
1121 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette);
1122 if (SUCCEEDED(hr))
1123 hr = IWICBitmapFrameDecode_CopyPalette(bitmapframe, wic_palette);
1124 if (SUCCEEDED(hr))
1125 hr = IWICPalette_GetColorCount(wic_palette, &nb_colors);
1126 if (SUCCEEDED(hr))
1127 {
1128 colors = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(colors[0]));
1129 palette = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(palette[0]));
1130 if (!colors || !palette)
1131 hr = E_OUTOFMEMORY;
1132 }
1133 if (SUCCEEDED(hr))
1134 hr = IWICPalette_GetColors(wic_palette, nb_colors, colors, &nb_colors);
1135 if (SUCCEEDED(hr))
1136 {
1137 UINT i;
1138
1139 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1140 for (i = 0; i < nb_colors; i++)
1141 {
1142 palette[i].peRed = (colors[i] >> 16) & 0xff;
1143 palette[i].peGreen = (colors[i] >> 8) & 0xff;
1144 palette[i].peBlue = colors[i] & 0xff;
1145 palette[i].peFlags = (colors[i] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1146 }
1147 }
1148 if (wic_palette)
1149 IWICPalette_Release(wic_palette);
1150 }
1151
1152 if (SUCCEEDED(hr))
1153 {
1154 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1155 buffer, imginfo.Format, pitch,
1156 palette, &rect, dwFilter, Colorkey);
1157 }
1158
1159 HeapFree(GetProcessHeap(), 0, colors);
1160 HeapFree(GetProcessHeap(), 0, palette);
1161 HeapFree(GetProcessHeap(), 0, buffer);
1162 }
1163
1164 IWICBitmapFrameDecode_Release(bitmapframe);
1165
1166 cleanup_bmp:
1167 IWICBitmapDecoder_Release(decoder);
1168
1169 cleanup_err:
1170 if (factory)
1171 IWICImagingFactory_Release(factory);
1172
1173 CoUninitialize();
1174
1175 if (imginfo.ImageFileFormat == D3DXIFF_DIB)
1176 HeapFree(GetProcessHeap(), 0, (void*)pSrcData);
1177
1178 if (FAILED(hr))
1179 return D3DXERR_INVALIDDATA;
1180
1181 if (pSrcInfo)
1182 *pSrcInfo = imginfo;
1183
1184 return D3D_OK;
1185 }
1186
1187 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface,
1188 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file,
1189 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1190 {
1191 LPWSTR pWidename;
1192 HRESULT hr;
1193 int strlength;
1194
1195 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1196 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1197 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file),
1198 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1199
1200 if (!src_file || !dst_surface)
1201 return D3DERR_INVALIDCALL;
1202
1203 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0);
1204 pWidename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*pWidename));
1205 MultiByteToWideChar(CP_ACP, 0, src_file, -1, pWidename, strlength);
1206
1207 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect,
1208 pWidename, src_rect, filter, color_key, src_info);
1209 HeapFree(GetProcessHeap(), 0, pWidename);
1210
1211 return hr;
1212 }
1213
1214 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface,
1215 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file,
1216 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1217 {
1218 UINT data_size;
1219 void *data;
1220 HRESULT hr;
1221
1222 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1223 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1224 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file),
1225 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1226
1227 if (!src_file || !dst_surface)
1228 return D3DERR_INVALIDCALL;
1229
1230 if (FAILED(map_view_of_file(src_file, &data, &data_size)))
1231 return D3DXERR_INVALIDDATA;
1232
1233 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1234 data, data_size, src_rect, filter, color_key, src_info);
1235 UnmapViewOfFile(data);
1236
1237 return hr;
1238 }
1239
1240 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface,
1241 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource,
1242 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1243 {
1244 UINT data_size;
1245 HRSRC resinfo;
1246 void *data;
1247
1248 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1249 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1250 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource),
1251 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1252
1253 if (!dst_surface)
1254 return D3DERR_INVALIDCALL;
1255
1256 if (!(resinfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA))
1257 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1258 && !(resinfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP)))
1259 return D3DXERR_INVALIDDATA;
1260
1261 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1262 return D3DXERR_INVALIDDATA;
1263
1264 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1265 data, data_size, src_rect, filter, color_key, src_info);
1266 }
1267
1268 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface,
1269 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource,
1270 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info)
1271 {
1272 UINT data_size;
1273 HRSRC resinfo;
1274 void *data;
1275
1276 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1277 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1278 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource),
1279 wine_dbgstr_rect(src_rect), filter, color_key, src_info);
1280
1281 if (!dst_surface)
1282 return D3DERR_INVALIDCALL;
1283
1284 if (!(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA))
1285 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1286 && !(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP)))
1287 return D3DXERR_INVALIDDATA;
1288
1289 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size)))
1290 return D3DXERR_INVALIDDATA;
1291
1292 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect,
1293 data, data_size, src_rect, filter, color_key, src_info);
1294 }
1295
1296
1297 /************************************************************
1298 * helper functions for D3DXLoadSurfaceFromMemory
1299 */
1300 struct argb_conversion_info
1301 {
1302 const struct pixel_format_desc *srcformat;
1303 const struct pixel_format_desc *destformat;
1304 DWORD srcshift[4], destshift[4];
1305 DWORD srcmask[4], destmask[4];
1306 BOOL process_channel[4];
1307 DWORD channelmask;
1308 };
1309
1310 static void init_argb_conversion_info(const struct pixel_format_desc *srcformat, const struct pixel_format_desc *destformat, struct argb_conversion_info *info)
1311 {
1312 UINT i;
1313 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1314 info->channelmask = 0;
1315
1316 info->srcformat = srcformat;
1317 info->destformat = destformat;
1318
1319 for(i = 0;i < 4;i++) {
1320 /* srcshift is used to extract the _relevant_ components */
1321 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1322
1323 /* destshift is used to move the components to the correct position */
1324 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1325
1326 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1327 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1328
1329 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1330 if(destformat->bits[i]) {
1331 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1332 else info->channelmask |= info->destmask[i];
1333 }
1334 }
1335 }
1336
1337 static DWORD dword_from_bytes(CONST BYTE *src, UINT bytes_per_pixel)
1338 {
1339 DWORD ret = 0;
1340 static BOOL fixme_once;
1341
1342 if(bytes_per_pixel > sizeof(DWORD)) {
1343 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1344 bytes_per_pixel = sizeof(DWORD);
1345 }
1346
1347 memcpy(&ret, src, bytes_per_pixel);
1348 return ret;
1349 }
1350
1351 static void dword_to_bytes(BYTE *dst, DWORD dword, UINT bytes_per_pixel)
1352 {
1353 static BOOL fixme_once;
1354
1355 if(bytes_per_pixel > sizeof(DWORD)) {
1356 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1357 ZeroMemory(dst, bytes_per_pixel);
1358 bytes_per_pixel = sizeof(DWORD);
1359 }
1360
1361 memcpy(dst, &dword, bytes_per_pixel);
1362 }
1363
1364 /************************************************************
1365 * get_relevant_argb_components
1366 *
1367 * Extracts the relevant components from the source color and
1368 * drops the less significant bits if they aren't used by the destination format.
1369 */
1370 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
1371 {
1372 UINT i = 0;
1373 for(;i < 4;i++)
1374 if(info->process_channel[i])
1375 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
1376 }
1377
1378 /************************************************************
1379 * make_argb_color
1380 *
1381 * Recombines the output of get_relevant_argb_components and converts
1382 * it to the destination format.
1383 */
1384 static DWORD make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in)
1385 {
1386 UINT i;
1387 DWORD val = 0;
1388
1389 for(i = 0;i < 4;i++) {
1390 if(info->process_channel[i]) {
1391 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1392 signed int shift;
1393 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1394 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1395 }
1396 }
1397 val |= info->channelmask; /* new channels are set to their maximal value */
1398 return val;
1399 }
1400
1401 static void format_to_vec4(const struct pixel_format_desc *format, const DWORD *src, struct vec4 *dst)
1402 {
1403 DWORD mask;
1404
1405 if (format->bits[1])
1406 {
1407 mask = (1 << format->bits[1]) - 1;
1408 dst->x = (float)((*src >> format->shift[1]) & mask) / mask;
1409 }
1410 else
1411 dst->x = 1.0f;
1412
1413 if (format->bits[2])
1414 {
1415 mask = (1 << format->bits[2]) - 1;
1416 dst->y = (float)((*src >> format->shift[2]) & mask) / mask;
1417 }
1418 else
1419 dst->y = 1.0f;
1420
1421 if (format->bits[3])
1422 {
1423 mask = (1 << format->bits[3]) - 1;
1424 dst->z = (float)((*src >> format->shift[3]) & mask) / mask;
1425 }
1426 else
1427 dst->z = 1.0f;
1428
1429 if (format->bits[0])
1430 {
1431 mask = (1 << format->bits[0]) - 1;
1432 dst->w = (float)((*src >> format->shift[0]) & mask) / mask;
1433 }
1434 else
1435 dst->w = 1.0f;
1436 }
1437
1438 static void format_from_vec4(const struct pixel_format_desc *format, const struct vec4 *src, DWORD *dst)
1439 {
1440 *dst = 0;
1441
1442 if (format->bits[1])
1443 *dst |= (DWORD)(src->x * ((1 << format->bits[1]) - 1) + 0.5f) << format->shift[1];
1444 if (format->bits[2])
1445 *dst |= (DWORD)(src->y * ((1 << format->bits[2]) - 1) + 0.5f) << format->shift[2];
1446 if (format->bits[3])
1447 *dst |= (DWORD)(src->z * ((1 << format->bits[3]) - 1) + 0.5f) << format->shift[3];
1448 if (format->bits[0])
1449 *dst |= (DWORD)(src->w * ((1 << format->bits[0]) - 1) + 0.5f) << format->shift[0];
1450 }
1451
1452 /************************************************************
1453 * copy_pixels
1454 *
1455 * Copies the source buffer to the destination buffer.
1456 * Works for any pixel format.
1457 * The source and the destination must be block-aligned.
1458 */
1459 void copy_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
1460 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *size,
1461 const struct pixel_format_desc *format)
1462 {
1463 UINT row, slice;
1464 BYTE *dst_addr;
1465 const BYTE *src_addr;
1466 UINT row_block_count = (size->width + format->block_width - 1) / format->block_width;
1467 UINT row_count = (size->height + format->block_height - 1) / format->block_height;
1468
1469 for (slice = 0; slice < size->depth; slice++)
1470 {
1471 src_addr = src + slice * src_slice_pitch;
1472 dst_addr = dst + slice * dst_slice_pitch;
1473
1474 for (row = 0; row < row_count; row++)
1475 {
1476 memcpy(dst_addr, src_addr, row_block_count * format->block_byte_count);
1477 src_addr += src_row_pitch;
1478 dst_addr += dst_row_pitch;
1479 }
1480 }
1481 }
1482
1483 /************************************************************
1484 * convert_argb_pixels
1485 *
1486 * Copies the source buffer to the destination buffer, performing
1487 * any necessary format conversion and color keying.
1488 * Pixels outsize the source rect are blacked out.
1489 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1490 */
1491 void convert_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1492 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1493 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1494 const PALETTEENTRY *palette)
1495 {
1496 struct argb_conversion_info conv_info, ck_conv_info;
1497 const struct pixel_format_desc *ck_format = NULL;
1498 DWORD channels[4], pixel;
1499 UINT min_width, min_height, min_depth;
1500 UINT x, y, z;
1501
1502 ZeroMemory(channels, sizeof(channels));
1503 init_argb_conversion_info(src_format, dst_format, &conv_info);
1504
1505 min_width = min(src_size->width, dst_size->width);
1506 min_height = min(src_size->height, dst_size->height);
1507 min_depth = min(src_size->depth, dst_size->depth);
1508
1509 if (color_key)
1510 {
1511 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1512 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1513 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1514 }
1515
1516 for (z = 0; z < min_depth; z++) {
1517 const BYTE *src_slice_ptr = src + z * src_slice_pitch;
1518 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1519
1520 for (y = 0; y < min_height; y++) {
1521 const BYTE *src_ptr = src_slice_ptr + y * src_row_pitch;
1522 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1523 DWORD val;
1524
1525 for (x = 0; x < min_width; x++) {
1526 /* extract source color components */
1527 pixel = dword_from_bytes(src_ptr, src_format->bytes_per_pixel);
1528
1529 if (!src_format->to_rgba && !dst_format->from_rgba)
1530 {
1531 get_relevant_argb_components(&conv_info, pixel, channels);
1532 val = make_argb_color(&conv_info, channels);
1533
1534 if (color_key)
1535 {
1536 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1537 pixel = make_argb_color(&ck_conv_info, channels);
1538 if (pixel == color_key)
1539 val &= ~conv_info.destmask[0];
1540 }
1541 }
1542 else
1543 {
1544 struct vec4 color, tmp;
1545
1546 format_to_vec4(src_format, &pixel, &color);
1547 if (src_format->to_rgba)
1548 src_format->to_rgba(&color, &tmp, palette);
1549 else
1550 tmp = color;
1551
1552 if (ck_format)
1553 {
1554 format_from_vec4(ck_format, &tmp, &pixel);
1555 if (pixel == color_key)
1556 tmp.w = 0.0f;
1557 }
1558
1559 if (dst_format->from_rgba)
1560 dst_format->from_rgba(&tmp, &color);
1561 else
1562 color = tmp;
1563
1564 format_from_vec4(dst_format, &color, &val);
1565 }
1566
1567 dword_to_bytes(dst_ptr, val, dst_format->bytes_per_pixel);
1568 src_ptr += src_format->bytes_per_pixel;
1569 dst_ptr += dst_format->bytes_per_pixel;
1570 }
1571
1572 if (src_size->width < dst_size->width) /* black out remaining pixels */
1573 memset(dst_ptr, 0, dst_format->bytes_per_pixel * (dst_size->width - src_size->width));
1574 }
1575
1576 if (src_size->height < dst_size->height) /* black out remaining pixels */
1577 memset(dst + src_size->height * dst_row_pitch, 0, dst_row_pitch * (dst_size->height - src_size->height));
1578 }
1579 if (src_size->depth < dst_size->depth) /* black out remaining pixels */
1580 memset(dst + src_size->depth * dst_slice_pitch, 0, dst_slice_pitch * (dst_size->depth - src_size->depth));
1581 }
1582
1583 /************************************************************
1584 * point_filter_argb_pixels
1585 *
1586 * Copies the source buffer to the destination buffer, performing
1587 * any necessary format conversion, color keying and stretching
1588 * using a point filter.
1589 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1590 */
1591 void point_filter_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size,
1592 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch,
1593 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key,
1594 const PALETTEENTRY *palette)
1595 {
1596 struct argb_conversion_info conv_info, ck_conv_info;
1597 const struct pixel_format_desc *ck_format = NULL;
1598 DWORD channels[4], pixel;
1599 UINT x, y, z;
1600
1601 ZeroMemory(channels, sizeof(channels));
1602 init_argb_conversion_info(src_format, dst_format, &conv_info);
1603
1604 if (color_key)
1605 {
1606 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1607 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1608 init_argb_conversion_info(src_format, ck_format, &ck_conv_info);
1609 }
1610
1611 for (z = 0; z < dst_size->depth; z++)
1612 {
1613 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch;
1614 const BYTE *src_slice_ptr = src + src_slice_pitch * (z * src_size->depth / dst_size->depth);
1615
1616 for (y = 0; y < dst_size->height; y++)
1617 {
1618 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch;
1619 const BYTE *src_row_ptr = src_slice_ptr + src_row_pitch * (y * src_size->height / dst_size->height);
1620
1621 for (x = 0; x < dst_size->width; x++)
1622 {
1623 const BYTE *src_ptr = src_row_ptr + (x * src_size->width / dst_size->width) * src_format->bytes_per_pixel;
1624 DWORD val;
1625
1626 /* extract source color components */
1627 pixel = dword_from_bytes(src_ptr, src_format->bytes_per_pixel);
1628
1629 if (!src_format->to_rgba && !dst_format->from_rgba)
1630 {
1631 get_relevant_argb_components(&conv_info, pixel, channels);
1632 val = make_argb_color(&conv_info, channels);
1633
1634 if (color_key)
1635 {
1636 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1637 pixel = make_argb_color(&ck_conv_info, channels);
1638 if (pixel == color_key)
1639 val &= ~conv_info.destmask[0];
1640 }
1641 }
1642 else
1643 {
1644 struct vec4 color, tmp;
1645
1646 format_to_vec4(src_format, &pixel, &color);
1647 if (src_format->to_rgba)
1648 src_format->to_rgba(&color, &tmp, palette);
1649 else
1650 tmp = color;
1651
1652 if (ck_format)
1653 {
1654 format_from_vec4(ck_format, &tmp, &pixel);
1655 if (pixel == color_key)
1656 tmp.w = 0.0f;
1657 }
1658
1659 if (dst_format->from_rgba)
1660 dst_format->from_rgba(&tmp, &color);
1661 else
1662 color = tmp;
1663
1664 format_from_vec4(dst_format, &color, &val);
1665 }
1666
1667 dword_to_bytes(dst_ptr, val, dst_format->bytes_per_pixel);
1668 dst_ptr += dst_format->bytes_per_pixel;
1669 }
1670 }
1671 }
1672 }
1673
1674 /************************************************************
1675 * D3DXLoadSurfaceFromMemory
1676 *
1677 * Loads data from a given memory chunk into a surface,
1678 * applying any of the specified filters.
1679 *
1680 * PARAMS
1681 * pDestSurface [I] pointer to the surface
1682 * pDestPalette [I] palette to use
1683 * pDestRect [I] to be filled area of the surface
1684 * pSrcMemory [I] pointer to the source data
1685 * SrcFormat [I] format of the source pixel data
1686 * SrcPitch [I] number of bytes in a row
1687 * pSrcPalette [I] palette used in the source image
1688 * pSrcRect [I] area of the source data to load
1689 * dwFilter [I] filter to apply on stretching
1690 * Colorkey [I] colorkey
1691 *
1692 * RETURNS
1693 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1694 * if pSrcMemory is NULL but the other parameters are valid
1695 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1696 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1697 * if DestRect is invalid
1698 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1699 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1700 *
1701 * NOTES
1702 * pSrcRect specifies the dimensions of the source data;
1703 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1704 *
1705 */
1706 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1707 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1708 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1709 DWORD filter, D3DCOLOR color_key)
1710 {
1711 const struct pixel_format_desc *srcformatdesc, *destformatdesc;
1712 D3DSURFACE_DESC surfdesc;
1713 D3DLOCKED_RECT lockrect;
1714 struct volume src_size, dst_size;
1715
1716 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1717 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1718 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1719
1720 if (!dst_surface || !src_memory || !src_rect)
1721 {
1722 WARN("Invalid argument specified.\n");
1723 return D3DERR_INVALIDCALL;
1724 }
1725 if (src_format == D3DFMT_UNKNOWN
1726 || src_rect->left >= src_rect->right
1727 || src_rect->top >= src_rect->bottom)
1728 {
1729 WARN("Invalid src_format or src_rect.\n");
1730 return E_FAIL;
1731 }
1732
1733 if (filter == D3DX_DEFAULT)
1734 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1735
1736 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1737
1738 src_size.width = src_rect->right - src_rect->left;
1739 src_size.height = src_rect->bottom - src_rect->top;
1740 src_size.depth = 1;
1741 if (!dst_rect)
1742 {
1743 dst_size.width = surfdesc.Width;
1744 dst_size.height = surfdesc.Height;
1745 }
1746 else
1747 {
1748 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width
1749 || dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height
1750 || dst_rect->left < 0 || dst_rect->top < 0)
1751 {
1752 WARN("Invalid dst_rect specified.\n");
1753 return D3DERR_INVALIDCALL;
1754 }
1755 dst_size.width = dst_rect->right - dst_rect->left;
1756 dst_size.height = dst_rect->bottom - dst_rect->top;
1757 if (!dst_size.width || !dst_size.height)
1758 return D3D_OK;
1759 }
1760 dst_size.depth = 1;
1761
1762 srcformatdesc = get_format_info(src_format);
1763 destformatdesc = get_format_info(surfdesc.Format);
1764 if (srcformatdesc->type == FORMAT_UNKNOWN || destformatdesc->type == FORMAT_UNKNOWN)
1765 {
1766 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format, surfdesc.Format);
1767 return E_NOTIMPL;
1768 }
1769
1770 if (src_format == surfdesc.Format
1771 && dst_size.width == src_size.width
1772 && dst_size.height == src_size.height
1773 && color_key == 0) /* Simple copy. */
1774 {
1775 if (src_rect->left & (srcformatdesc->block_width - 1)
1776 || src_rect->top & (srcformatdesc->block_height - 1)
1777 || (src_rect->right & (srcformatdesc->block_width - 1)
1778 && src_size.width != surfdesc.Width)
1779 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1780 && src_size.height != surfdesc.Height))
1781 {
1782 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1783 return D3DXERR_INVALIDDATA;
1784 }
1785
1786 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1787 return D3DXERR_INVALIDDATA;
1788
1789 copy_pixels(src_memory, src_pitch, 0, lockrect.pBits, lockrect.Pitch, 0,
1790 &src_size, srcformatdesc);
1791
1792 IDirect3DSurface9_UnlockRect(dst_surface);
1793 }
1794 else /* Stretching or format conversion. */
1795 {
1796 if (((srcformatdesc->type != FORMAT_ARGB) && (srcformatdesc->type != FORMAT_INDEX)) ||
1797 (destformatdesc->type != FORMAT_ARGB))
1798 {
1799 FIXME("Format conversion missing %#x -> %#x\n", src_format, surfdesc.Format);
1800 return E_NOTIMPL;
1801 }
1802
1803 if (FAILED(IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1804 return D3DXERR_INVALIDDATA;
1805
1806 if ((filter & 0xf) == D3DX_FILTER_NONE)
1807 {
1808 convert_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1809 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
1810 }
1811 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1812 {
1813 if ((filter & 0xf) != D3DX_FILTER_POINT)
1814 FIXME("Unhandled filter %#x.\n", filter);
1815
1816 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1817 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1818 point_filter_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc,
1819 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette);
1820 }
1821
1822 IDirect3DSurface9_UnlockRect(dst_surface);
1823 }
1824
1825 return D3D_OK;
1826 }
1827
1828 /************************************************************
1829 * D3DXLoadSurfaceFromSurface
1830 *
1831 * Copies the contents from one surface to another, performing any required
1832 * format conversion, resizing or filtering.
1833 *
1834 * PARAMS
1835 * pDestSurface [I] pointer to the destination surface
1836 * pDestPalette [I] palette to use
1837 * pDestRect [I] to be filled area of the surface
1838 * pSrcSurface [I] pointer to the source surface
1839 * pSrcPalette [I] palette used for the source surface
1840 * pSrcRect [I] area of the source data to load
1841 * dwFilter [I] filter to apply on resizing
1842 * Colorkey [I] any ARGB value or 0 to disable color-keying
1843 *
1844 * RETURNS
1845 * Success: D3D_OK
1846 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1847 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1848 *
1849 */
1850 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface,
1851 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface,
1852 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key)
1853 {
1854 RECT rect;
1855 D3DLOCKED_RECT lock;
1856 D3DSURFACE_DESC SrcDesc;
1857 HRESULT hr;
1858
1859 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1860 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1861 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface,
1862 src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1863
1864 if (!dst_surface || !src_surface)
1865 return D3DERR_INVALIDCALL;
1866
1867 IDirect3DSurface9_GetDesc(src_surface, &SrcDesc);
1868
1869 if (!src_rect)
1870 SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1871 else
1872 rect = *src_rect;
1873
1874 if (FAILED(IDirect3DSurface9_LockRect(src_surface, &lock, NULL, D3DLOCK_READONLY)))
1875 return D3DXERR_INVALIDDATA;
1876
1877 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect,
1878 lock.pBits, SrcDesc.Format, lock.Pitch, src_palette, &rect, filter, color_key);
1879
1880 IDirect3DSurface9_UnlockRect(src_surface);
1881
1882 return hr;
1883 }
1884
1885
1886 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1887 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1888 {
1889 int len;
1890 WCHAR *filename;
1891 HRESULT hr;
1892 ID3DXBuffer *buffer;
1893
1894 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1895 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1896
1897 if (!dst_filename) return D3DERR_INVALIDCALL;
1898
1899 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1900 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1901 if (!filename) return E_OUTOFMEMORY;
1902 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1903
1904 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1905 if (SUCCEEDED(hr))
1906 {
1907 hr = write_buffer_to_file(filename, buffer);
1908 ID3DXBuffer_Release(buffer);
1909 }
1910
1911 HeapFree(GetProcessHeap(), 0, filename);
1912 return hr;
1913 }
1914
1915 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1916 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1917 {
1918 HRESULT hr;
1919 ID3DXBuffer *buffer;
1920
1921 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1922 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1923
1924 if (!dst_filename) return D3DERR_INVALIDCALL;
1925
1926 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect);
1927 if (SUCCEEDED(hr))
1928 {
1929 hr = write_buffer_to_file(dst_filename, buffer);
1930 ID3DXBuffer_Release(buffer);
1931 }
1932
1933 return hr;
1934 }
1935
1936 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format,
1937 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1938 {
1939 IWICBitmapEncoder *encoder = NULL;
1940 IWICBitmapFrameEncode *frame = NULL;
1941 IPropertyBag2 *encoder_options = NULL;
1942 IStream *stream = NULL;
1943 HRESULT hr;
1944 HRESULT initresult;
1945 const CLSID *encoder_clsid;
1946 const GUID *pixel_format_guid;
1947 WICPixelFormatGUID wic_pixel_format;
1948 D3DFORMAT d3d_pixel_format;
1949 D3DSURFACE_DESC src_surface_desc;
1950 D3DLOCKED_RECT locked_rect;
1951 int width, height;
1952 STATSTG stream_stats;
1953 HGLOBAL stream_hglobal;
1954 ID3DXBuffer *buffer;
1955 DWORD size;
1956
1957 TRACE("(%p, %#x, %p, %p, %s)\n",
1958 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1959
1960 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL;
1961
1962 if (src_palette)
1963 {
1964 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1965 return D3DERR_INVALIDCALL;
1966 }
1967
1968 switch (file_format)
1969 {
1970 case D3DXIFF_BMP:
1971 case D3DXIFF_DIB:
1972 encoder_clsid = &CLSID_WICBmpEncoder;
1973 break;
1974 case D3DXIFF_PNG:
1975 encoder_clsid = &CLSID_WICPngEncoder;
1976 break;
1977 case D3DXIFF_JPG:
1978 encoder_clsid = &CLSID_WICJpegEncoder;
1979 break;
1980 case D3DXIFF_DDS:
1981 return save_dds_surface_to_memory(dst_buffer, src_surface, src_rect);
1982 case D3DXIFF_HDR:
1983 case D3DXIFF_PFM:
1984 case D3DXIFF_TGA:
1985 case D3DXIFF_PPM:
1986 FIXME("File format %#x is not supported yet\n", file_format);
1987 return E_NOTIMPL;
1988 default:
1989 return D3DERR_INVALIDCALL;
1990 }
1991
1992 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
1993 if (src_rect)
1994 {
1995 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
1996 {
1997 WARN("Invalid rectangle with 0 area\n");
1998 return D3DXCreateBuffer(64, dst_buffer);
1999 }
2000 if (src_rect->left < 0 || src_rect->top < 0)
2001 return D3DERR_INVALIDCALL;
2002 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
2003 return D3DERR_INVALIDCALL;
2004 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
2005 return D3DERR_INVALIDCALL;
2006
2007 width = src_rect->right - src_rect->left;
2008 height = src_rect->bottom - src_rect->top;
2009 }
2010 else
2011 {
2012 width = src_surface_desc.Width;
2013 height = src_surface_desc.Height;
2014 }
2015
2016 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
2017
2018 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
2019 &IID_IWICBitmapEncoder, (void **)&encoder);
2020 if (FAILED(hr)) goto cleanup_err;
2021
2022 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
2023 if (FAILED(hr)) goto cleanup_err;
2024
2025 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache);
2026 if (FAILED(hr)) goto cleanup_err;
2027
2028 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
2029 if (FAILED(hr)) goto cleanup_err;
2030
2031 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
2032 if (FAILED(hr)) goto cleanup_err;
2033
2034 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
2035 if (FAILED(hr)) goto cleanup_err;
2036
2037 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
2038 if (!pixel_format_guid)
2039 {
2040 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
2041 hr = E_NOTIMPL;
2042 goto cleanup;
2043 }
2044
2045 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
2046 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
2047 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
2048 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
2049 {
2050 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
2051
2052 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
2053 {
2054 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2055 if (SUCCEEDED(hr))
2056 {
2057 IWICBitmapFrameEncode_WritePixels(frame, height,
2058 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
2059 IDirect3DSurface9_UnlockRect(src_surface);
2060 }
2061 }
2062 else /* Pixel format conversion */
2063 {
2064 const struct pixel_format_desc *src_format_desc, *dst_format_desc;
2065 struct volume size;
2066 DWORD dst_pitch;
2067 void *dst_data;
2068
2069 src_format_desc = get_format_info(src_surface_desc.Format);
2070 dst_format_desc = get_format_info(d3d_pixel_format);
2071 if (src_format_desc->type != FORMAT_ARGB || dst_format_desc->type != FORMAT_ARGB)
2072 {
2073 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
2074 src_surface_desc.Format, d3d_pixel_format);
2075 hr = E_NOTIMPL;
2076 goto cleanup;
2077 }
2078
2079 size.width = width;
2080 size.height = height;
2081 size.depth = 1;
2082 dst_pitch = width * dst_format_desc->bytes_per_pixel;
2083 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
2084 if (!dst_data)
2085 {
2086 hr = E_OUTOFMEMORY;
2087 goto cleanup;
2088 }
2089
2090 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
2091 if (SUCCEEDED(hr))
2092 {
2093 convert_argb_pixels(locked_rect.pBits, locked_rect.Pitch, 0, &size, src_format_desc,
2094 dst_data, dst_pitch, 0, &size, dst_format_desc, 0, NULL);
2095 IDirect3DSurface9_UnlockRect(src_surface);
2096 }
2097
2098 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
2099 HeapFree(GetProcessHeap(), 0, dst_data);
2100 }
2101
2102 hr = IWICBitmapFrameEncode_Commit(frame);
2103 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
2104 }
2105 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
2106
2107 /* copy data from stream to ID3DXBuffer */
2108 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME);
2109 if (FAILED(hr)) goto cleanup_err;
2110
2111 if (stream_stats.cbSize.u.HighPart != 0)
2112 {
2113 hr = D3DXERR_INVALIDDATA;
2114 goto cleanup;
2115 }
2116 size = stream_stats.cbSize.u.LowPart;
2117
2118 /* Remove BMP header for DIB */
2119 if (file_format == D3DXIFF_DIB)
2120 size -= sizeof(BITMAPFILEHEADER);
2121
2122 hr = D3DXCreateBuffer(size, &buffer);
2123 if (FAILED(hr)) goto cleanup;
2124
2125 hr = GetHGlobalFromStream(stream, &stream_hglobal);
2126 if (SUCCEEDED(hr))
2127 {
2128 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
2129 void *stream_data = GlobalLock(stream_hglobal);
2130 /* Remove BMP header for DIB */
2131 if (file_format == D3DXIFF_DIB)
2132 stream_data = (void*)((BYTE*)stream_data + sizeof(BITMAPFILEHEADER));
2133 memcpy(buffer_pointer, stream_data, size);
2134 GlobalUnlock(stream_hglobal);
2135 *dst_buffer = buffer;
2136 }
2137 else ID3DXBuffer_Release(buffer);
2138
2139 cleanup_err:
2140 if (FAILED(hr) && hr != E_OUTOFMEMORY)
2141 hr = D3DERR_INVALIDCALL;
2142
2143 cleanup:
2144 if (stream) IStream_Release(stream);
2145
2146 if (frame) IWICBitmapFrameEncode_Release(frame);
2147 if (encoder_options) IPropertyBag2_Release(encoder_options);
2148
2149 if (encoder) IWICBitmapEncoder_Release(encoder);
2150
2151 if (SUCCEEDED(initresult)) CoUninitialize();
2152
2153 return hr;
2154 }