2 * Copyright (C) 2009-2010 Tony Wasserka
3 * Copyright (C) 2012 Józef Kucia
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
24 #include "d3dx9_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
33 /* Wine-specific WIC GUIDs */
34 DEFINE_GUID(GUID_WineContainerFormatTga
, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
40 } wic_pixel_formats
[] = {
41 { &GUID_WICPixelFormat8bppIndexed
, D3DFMT_P8
},
42 { &GUID_WICPixelFormat1bppIndexed
, D3DFMT_P8
},
43 { &GUID_WICPixelFormat4bppIndexed
, D3DFMT_P8
},
44 { &GUID_WICPixelFormat8bppGray
, D3DFMT_L8
},
45 { &GUID_WICPixelFormat16bppBGR555
, D3DFMT_X1R5G5B5
},
46 { &GUID_WICPixelFormat16bppBGR565
, D3DFMT_R5G6B5
},
47 { &GUID_WICPixelFormat24bppBGR
, D3DFMT_R8G8B8
},
48 { &GUID_WICPixelFormat32bppBGR
, D3DFMT_X8R8G8B8
},
49 { &GUID_WICPixelFormat32bppBGRA
, D3DFMT_A8R8G8B8
}
52 static D3DFORMAT
wic_guid_to_d3dformat(const GUID
*guid
)
56 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
58 if (IsEqualGUID(wic_pixel_formats
[i
].wic_guid
, guid
))
59 return wic_pixel_formats
[i
].d3dformat
;
62 return D3DFMT_UNKNOWN
;
65 static const GUID
*d3dformat_to_wic_guid(D3DFORMAT format
)
69 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
71 if (wic_pixel_formats
[i
].d3dformat
== format
)
72 return wic_pixel_formats
[i
].wic_guid
;
78 /* dds_header.flags */
80 #define DDS_HEIGHT 0x2
83 #define DDS_PIXELFORMAT 0x1000
84 #define DDS_MIPMAPCOUNT 0x20000
85 #define DDS_LINEARSIZE 0x80000
86 #define DDS_DEPTH 0x800000
89 #define DDS_CAPS_COMPLEX 0x8
90 #define DDS_CAPS_TEXTURE 0x1000
91 #define DDS_CAPS_MIPMAP 0x400000
93 /* dds_header.caps2 */
94 #define DDS_CAPS2_CUBEMAP 0x200
95 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
96 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
97 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
98 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
99 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
100 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
101 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
102 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
103 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
104 #define DDS_CAPS2_VOLUME 0x200000
106 /* dds_pixel_format.flags */
107 #define DDS_PF_ALPHA 0x1
108 #define DDS_PF_ALPHA_ONLY 0x2
109 #define DDS_PF_FOURCC 0x4
110 #define DDS_PF_RGB 0x40
111 #define DDS_PF_YUV 0x200
112 #define DDS_PF_LUMINANCE 0x20000
113 #define DDS_PF_BUMPDUDV 0x80000
115 struct dds_pixel_format
134 DWORD pitch_or_linear_size
;
138 struct dds_pixel_format pixel_format
;
146 static D3DFORMAT
dds_fourcc_to_d3dformat(DWORD fourcc
)
149 static const DWORD known_fourcc
[] = {
161 D3DFMT_A16B16G16R16F
,
164 D3DFMT_A32B32G32R32F
,
167 for (i
= 0; i
< sizeof(known_fourcc
) / sizeof(known_fourcc
[0]); i
++)
169 if (known_fourcc
[i
] == fourcc
)
173 WARN("Unknown FourCC %#x\n", fourcc
);
174 return D3DFMT_UNKNOWN
;
177 static const struct {
184 } rgb_pixel_formats
[] = {
185 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2
},
186 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5
},
187 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5
},
188 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5
},
189 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4
},
190 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4
},
191 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2
},
192 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8
},
193 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8
},
194 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8
},
195 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10
},
196 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10
},
197 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16
},
198 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8
},
199 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8
},
202 static D3DFORMAT
dds_rgb_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
206 for (i
= 0; i
< sizeof(rgb_pixel_formats
) / sizeof(rgb_pixel_formats
[0]); i
++)
208 if (rgb_pixel_formats
[i
].bpp
== pixel_format
->bpp
209 && rgb_pixel_formats
[i
].rmask
== pixel_format
->rmask
210 && rgb_pixel_formats
[i
].gmask
== pixel_format
->gmask
211 && rgb_pixel_formats
[i
].bmask
== pixel_format
->bmask
)
213 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && rgb_pixel_formats
[i
].amask
== pixel_format
->amask
)
214 return rgb_pixel_formats
[i
].format
;
215 if (rgb_pixel_formats
[i
].amask
== 0)
216 return rgb_pixel_formats
[i
].format
;
220 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
221 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
222 return D3DFMT_UNKNOWN
;
225 static D3DFORMAT
dds_luminance_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
227 if (pixel_format
->bpp
== 8)
229 if (pixel_format
->rmask
== 0xff)
231 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x0f && pixel_format
->amask
== 0xf0)
234 if (pixel_format
->bpp
== 16)
236 if (pixel_format
->rmask
== 0xffff)
238 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x00ff && pixel_format
->amask
== 0xff00)
242 WARN("Unknown luminance pixel format (bpp %u, l %#x, a %#x)\n",
243 pixel_format
->bpp
, pixel_format
->rmask
, pixel_format
->amask
);
244 return D3DFMT_UNKNOWN
;
247 static D3DFORMAT
dds_alpha_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
249 if (pixel_format
->bpp
== 8 && pixel_format
->amask
== 0xff)
252 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format
->bpp
, pixel_format
->rmask
);
253 return D3DFMT_UNKNOWN
;
256 static D3DFORMAT
dds_bump_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
258 if (pixel_format
->bpp
== 16 && pixel_format
->rmask
== 0x00ff && pixel_format
->gmask
== 0xff00)
260 if (pixel_format
->bpp
== 32 && pixel_format
->rmask
== 0x0000ffff && pixel_format
->gmask
== 0xffff0000)
261 return D3DFMT_V16U16
;
263 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format
->bpp
,
264 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
265 return D3DFMT_UNKNOWN
;
268 static D3DFORMAT
dds_pixel_format_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
270 TRACE("pixel_format: size %u, flags %#x, fourcc %#x, bpp %u.\n", pixel_format
->size
,
271 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
);
272 TRACE("rmask %#x, gmask %#x, bmask %#x, amask %#x.\n", pixel_format
->rmask
, pixel_format
->gmask
,
273 pixel_format
->bmask
, pixel_format
->amask
);
275 if (pixel_format
->flags
& DDS_PF_FOURCC
)
276 return dds_fourcc_to_d3dformat(pixel_format
->fourcc
);
277 if (pixel_format
->flags
& DDS_PF_RGB
)
278 return dds_rgb_to_d3dformat(pixel_format
);
279 if (pixel_format
->flags
& DDS_PF_LUMINANCE
)
280 return dds_luminance_to_d3dformat(pixel_format
);
281 if (pixel_format
->flags
& DDS_PF_ALPHA_ONLY
)
282 return dds_alpha_to_d3dformat(pixel_format
);
283 if (pixel_format
->flags
& DDS_PF_BUMPDUDV
)
284 return dds_bump_to_d3dformat(pixel_format
);
286 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n",
287 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
,
288 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
289 return D3DFMT_UNKNOWN
;
292 static HRESULT
d3dformat_to_dds_pixel_format(struct dds_pixel_format
*pixel_format
, D3DFORMAT d3dformat
)
296 memset(pixel_format
, 0, sizeof(*pixel_format
));
298 pixel_format
->size
= sizeof(*pixel_format
);
300 for (i
= 0; i
< sizeof(rgb_pixel_formats
) / sizeof(rgb_pixel_formats
[0]); i
++)
302 if (rgb_pixel_formats
[i
].format
== d3dformat
)
304 pixel_format
->flags
|= DDS_PF_RGB
;
305 pixel_format
->bpp
= rgb_pixel_formats
[i
].bpp
;
306 pixel_format
->rmask
= rgb_pixel_formats
[i
].rmask
;
307 pixel_format
->gmask
= rgb_pixel_formats
[i
].gmask
;
308 pixel_format
->bmask
= rgb_pixel_formats
[i
].bmask
;
309 pixel_format
->amask
= rgb_pixel_formats
[i
].amask
;
310 if (pixel_format
->amask
) pixel_format
->flags
|= DDS_PF_ALPHA
;
315 WARN("Unknown pixel format %#x\n", d3dformat
);
319 static HRESULT
calculate_dds_surface_size(D3DFORMAT format
, UINT width
, UINT height
,
320 UINT
*pitch
, UINT
*size
)
322 const struct pixel_format_desc
*format_desc
= get_format_info(format
);
323 if (format_desc
->type
== FORMAT_UNKNOWN
)
326 if (format_desc
->block_width
!= 1 || format_desc
->block_height
!= 1)
328 *pitch
= format_desc
->block_byte_count
329 * max(1, (width
+ format_desc
->block_width
- 1) / format_desc
->block_width
);
331 * max(1, (height
+ format_desc
->block_height
- 1) / format_desc
->block_height
);
335 *pitch
= width
* format_desc
->bytes_per_pixel
;
336 *size
= *pitch
* height
;
342 static UINT
calculate_dds_file_size(D3DFORMAT format
, UINT width
, UINT height
, UINT depth
,
343 UINT miplevels
, UINT faces
)
345 UINT i
, file_size
= 0;
347 for (i
= 0; i
< miplevels
; i
++)
349 UINT pitch
, size
= 0;
350 calculate_dds_surface_size(format
, width
, height
, &pitch
, &size
);
353 width
= max(1, width
/ 2);
354 height
= max(1, height
/ 2);
355 depth
= max(1, depth
/ 2);
359 file_size
+= sizeof(struct dds_header
);
363 /************************************************************
364 * get_image_info_from_dds
366 * Fills a D3DXIMAGE_INFO structure with information
367 * about a DDS file stored in the memory.
370 * buffer [I] pointer to DDS data
371 * length [I] size of DDS data
372 * info [O] pointer to D3DXIMAGE_INFO structure
376 * Failure: D3DXERR_INVALIDDATA
379 static HRESULT
get_image_info_from_dds(const void *buffer
, UINT length
, D3DXIMAGE_INFO
*info
)
382 UINT expected_length
;
383 const struct dds_header
*header
= buffer
;
385 if (length
< sizeof(*header
) || !info
)
386 return D3DXERR_INVALIDDATA
;
388 if (header
->pixel_format
.size
!= sizeof(header
->pixel_format
))
389 return D3DXERR_INVALIDDATA
;
391 info
->Width
= header
->width
;
392 info
->Height
= header
->height
;
394 info
->MipLevels
= header
->miplevels
? header
->miplevels
: 1;
396 info
->Format
= dds_pixel_format_to_d3dformat(&header
->pixel_format
);
397 if (info
->Format
== D3DFMT_UNKNOWN
)
398 return D3DXERR_INVALIDDATA
;
400 TRACE("Pixel format is %#x\n", info
->Format
);
402 if (header
->caps2
& DDS_CAPS2_VOLUME
)
404 info
->Depth
= header
->depth
;
405 info
->ResourceType
= D3DRTYPE_VOLUMETEXTURE
;
407 else if (header
->caps2
& DDS_CAPS2_CUBEMAP
)
411 for (face
= DDS_CAPS2_CUBEMAP_POSITIVEX
; face
<= DDS_CAPS2_CUBEMAP_NEGATIVEZ
; face
<<= 1)
413 if (header
->caps2
& face
)
416 info
->ResourceType
= D3DRTYPE_CUBETEXTURE
;
420 info
->ResourceType
= D3DRTYPE_TEXTURE
;
423 expected_length
= calculate_dds_file_size(info
->Format
, info
->Width
, info
->Height
, info
->Depth
,
424 info
->MipLevels
, faces
);
425 if (length
< expected_length
)
427 WARN("File is too short %u, expected at least %u bytes\n", length
, expected_length
);
428 return D3DXERR_INVALIDDATA
;
431 info
->ImageFileFormat
= D3DXIFF_DDS
;
435 static HRESULT
load_surface_from_dds(IDirect3DSurface9
*dst_surface
, const PALETTEENTRY
*dst_palette
,
436 const RECT
*dst_rect
, const void *src_data
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
,
437 const D3DXIMAGE_INFO
*src_info
)
441 const struct dds_header
*header
= src_data
;
442 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
444 if (src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
445 return D3DXERR_INVALIDDATA
;
447 if (FAILED(calculate_dds_surface_size(src_info
->Format
, src_info
->Width
, src_info
->Height
, &src_pitch
, &size
)))
450 return D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
, pixels
, src_info
->Format
,
451 src_pitch
, NULL
, src_rect
, filter
, color_key
);
454 static HRESULT
save_dds_surface_to_memory(ID3DXBuffer
**dst_buffer
, IDirect3DSurface9
*src_surface
, const RECT
*src_rect
)
457 UINT dst_pitch
, surface_size
, file_size
;
458 D3DSURFACE_DESC src_desc
;
459 D3DLOCKED_RECT locked_rect
;
461 struct dds_header
*header
;
463 struct volume volume
;
464 const struct pixel_format_desc
*pixel_format
;
468 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
472 hr
= IDirect3DSurface9_GetDesc(src_surface
, &src_desc
);
473 if (FAILED(hr
)) return hr
;
475 pixel_format
= get_format_info(src_desc
.Format
);
476 if (pixel_format
->type
== FORMAT_UNKNOWN
) return E_NOTIMPL
;
478 file_size
= calculate_dds_file_size(src_desc
.Format
, src_desc
.Width
, src_desc
.Height
, 1, 1, 1);
480 hr
= calculate_dds_surface_size(src_desc
.Format
, src_desc
.Width
, src_desc
.Height
, &dst_pitch
, &surface_size
);
481 if (FAILED(hr
)) return hr
;
483 hr
= D3DXCreateBuffer(file_size
, &buffer
);
484 if (FAILED(hr
)) return hr
;
486 header
= ID3DXBuffer_GetBufferPointer(buffer
);
487 pixels
= (BYTE
*)(header
+ 1);
489 memset(header
, 0, sizeof(*header
));
490 header
->signature
= MAKEFOURCC('D','D','S',' ');
491 /* The signature is not really part of the DDS header */
492 header
->size
= sizeof(*header
) - FIELD_OFFSET(struct dds_header
, size
);
493 header
->flags
= DDS_CAPS
| DDS_HEIGHT
| DDS_WIDTH
| DDS_PIXELFORMAT
;
494 header
->height
= src_desc
.Height
;
495 header
->width
= src_desc
.Width
;
496 header
->caps
= DDS_CAPS_TEXTURE
;
497 hr
= d3dformat_to_dds_pixel_format(&header
->pixel_format
, src_desc
.Format
);
500 ID3DXBuffer_Release(buffer
);
504 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, NULL
, D3DLOCK_READONLY
);
507 ID3DXBuffer_Release(buffer
);
511 volume
.width
= src_desc
.Width
;
512 volume
.height
= src_desc
.Height
;
514 copy_pixels(locked_rect
.pBits
, locked_rect
.Pitch
, 0, pixels
, dst_pitch
, 0,
515 &volume
, pixel_format
);
517 IDirect3DSurface9_UnlockRect(src_surface
);
519 *dst_buffer
= buffer
;
523 HRESULT
load_volume_from_dds(IDirect3DVolume9
*dst_volume
, const PALETTEENTRY
*dst_palette
,
524 const D3DBOX
*dst_box
, const void *src_data
, const D3DBOX
*src_box
, DWORD filter
, D3DCOLOR color_key
,
525 const D3DXIMAGE_INFO
*src_info
)
527 UINT row_pitch
, slice_pitch
;
528 const struct dds_header
*header
= src_data
;
529 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
531 if (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
)
532 return D3DXERR_INVALIDDATA
;
534 if (FAILED(calculate_dds_surface_size(src_info
->Format
, src_info
->Width
, src_info
->Height
, &row_pitch
, &slice_pitch
)))
537 return D3DXLoadVolumeFromMemory(dst_volume
, dst_palette
, dst_box
, pixels
, src_info
->Format
,
538 row_pitch
, slice_pitch
, NULL
, src_box
, filter
, color_key
);
541 HRESULT
load_texture_from_dds(IDirect3DTexture9
*texture
, const void *src_data
, const PALETTEENTRY
*palette
,
542 DWORD filter
, D3DCOLOR color_key
, const D3DXIMAGE_INFO
*src_info
, unsigned int skip_levels
,
543 unsigned int *loaded_miplevels
)
552 IDirect3DSurface9
*surface
;
553 const struct dds_header
*header
= src_data
;
554 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
556 /* Loading a cube texture as a simple texture is also supported
557 * (only first face texture is taken). Same with volume textures. */
558 if ((src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
559 && (src_info
->ResourceType
!= D3DRTYPE_CUBETEXTURE
)
560 && (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
))
562 WARN("Trying to load a %u resource as a 2D texture, returning failure.\n", src_info
->ResourceType
);
563 return D3DXERR_INVALIDDATA
;
566 width
= src_info
->Width
;
567 height
= src_info
->Height
;
568 mip_levels
= min(src_info
->MipLevels
, IDirect3DTexture9_GetLevelCount(texture
));
569 if (src_info
->ResourceType
== D3DRTYPE_VOLUMETEXTURE
)
571 for (mip_level
= 0; mip_level
< mip_levels
+ skip_levels
; ++mip_level
)
573 hr
= calculate_dds_surface_size(src_info
->Format
, width
, height
, &src_pitch
, &mip_level_size
);
574 if (FAILED(hr
)) return hr
;
576 if (mip_level
>= skip_levels
)
578 SetRect(&src_rect
, 0, 0, width
, height
);
580 IDirect3DTexture9_GetSurfaceLevel(texture
, mip_level
- skip_levels
, &surface
);
581 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
582 NULL
, &src_rect
, filter
, color_key
);
583 IDirect3DSurface9_Release(surface
);
588 pixels
+= mip_level_size
;
589 width
= max(1, width
/ 2);
590 height
= max(1, height
/ 2);
593 *loaded_miplevels
= mip_levels
- skip_levels
;
598 HRESULT
load_cube_texture_from_dds(IDirect3DCubeTexture9
*cube_texture
, const void *src_data
,
599 const PALETTEENTRY
*palette
, DWORD filter
, DWORD color_key
, const D3DXIMAGE_INFO
*src_info
)
609 IDirect3DSurface9
*surface
;
610 const struct dds_header
*header
= src_data
;
611 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
613 if (src_info
->ResourceType
!= D3DRTYPE_CUBETEXTURE
)
614 return D3DXERR_INVALIDDATA
;
616 if ((header
->caps2
& DDS_CAPS2_CUBEMAP_ALL_FACES
) != DDS_CAPS2_CUBEMAP_ALL_FACES
)
618 WARN("Only full cubemaps are supported\n");
619 return D3DXERR_INVALIDDATA
;
622 mip_levels
= min(src_info
->MipLevels
, IDirect3DCubeTexture9_GetLevelCount(cube_texture
));
623 for (face
= D3DCUBEMAP_FACE_POSITIVE_X
; face
<= D3DCUBEMAP_FACE_NEGATIVE_Z
; face
++)
625 size
= src_info
->Width
;
626 for (mip_level
= 0; mip_level
< src_info
->MipLevels
; mip_level
++)
628 hr
= calculate_dds_surface_size(src_info
->Format
, size
, size
, &src_pitch
, &mip_level_size
);
629 if (FAILED(hr
)) return hr
;
631 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
632 if (mip_level
< mip_levels
)
634 SetRect(&src_rect
, 0, 0, size
, size
);
636 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture
, face
, mip_level
, &surface
);
637 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
638 NULL
, &src_rect
, filter
, color_key
);
639 IDirect3DSurface9_Release(surface
);
640 if (FAILED(hr
)) return hr
;
643 pixels
+= mip_level_size
;
644 size
= max(1, size
/ 2);
651 HRESULT
load_volume_texture_from_dds(IDirect3DVolumeTexture9
*volume_texture
, const void *src_data
,
652 const PALETTEENTRY
*palette
, DWORD filter
, DWORD color_key
, const D3DXIMAGE_INFO
*src_info
)
657 UINT src_slice_pitch
;
660 UINT width
, height
, depth
;
661 IDirect3DVolume9
*volume
;
662 const struct dds_header
*header
= src_data
;
663 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
665 if (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
)
666 return D3DXERR_INVALIDDATA
;
668 width
= src_info
->Width
;
669 height
= src_info
->Height
;
670 depth
= src_info
->Depth
;
671 mip_levels
= min(src_info
->MipLevels
, IDirect3DVolumeTexture9_GetLevelCount(volume_texture
));
673 for (mip_level
= 0; mip_level
< mip_levels
; mip_level
++)
675 hr
= calculate_dds_surface_size(src_info
->Format
, width
, height
, &src_row_pitch
, &src_slice_pitch
);
676 if (FAILED(hr
)) return hr
;
678 hr
= IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, mip_level
, &volume
);
679 if (FAILED(hr
)) return hr
;
683 src_box
.Right
= width
;
684 src_box
.Bottom
= height
;
686 src_box
.Back
= depth
;
688 hr
= D3DXLoadVolumeFromMemory(volume
, palette
, NULL
, pixels
, src_info
->Format
,
689 src_row_pitch
, src_slice_pitch
, NULL
, &src_box
, filter
, color_key
);
691 IDirect3DVolume9_Release(volume
);
692 if (FAILED(hr
)) return hr
;
694 pixels
+= depth
* src_slice_pitch
;
695 width
= max(1, width
/ 2);
696 height
= max(1, height
/ 2);
697 depth
= max(1, depth
/ 2);
703 static BOOL
convert_dib_to_bmp(void **data
, UINT
*size
)
708 BITMAPFILEHEADER
*header
;
712 if ((*size
< 4) || (*size
< (header_size
= *(ULONG
*)*data
)))
715 if ((header_size
== sizeof(BITMAPINFOHEADER
)) ||
716 (header_size
== sizeof(BITMAPV4HEADER
)) ||
717 (header_size
== sizeof(BITMAPV5HEADER
)) ||
718 (header_size
== 64 /* sizeof(BITMAPCOREHEADER2) */))
720 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
721 BITMAPINFOHEADER
*info_header
= (BITMAPINFOHEADER
*)*data
;
722 count
= info_header
->biClrUsed
;
724 if (!count
&& info_header
->biBitCount
<= 8)
725 count
= 1 << info_header
->biBitCount
;
727 offset
= sizeof(BITMAPFILEHEADER
) + header_size
+ sizeof(RGBQUAD
) * count
;
729 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
730 if ((info_header
->biSize
== sizeof(BITMAPINFOHEADER
)) && (info_header
->biCompression
== BI_BITFIELDS
))
731 offset
+= 3 * sizeof(DWORD
);
733 else if (header_size
== sizeof(BITMAPCOREHEADER
))
735 BITMAPCOREHEADER
*core_header
= (BITMAPCOREHEADER
*)*data
;
737 if (core_header
->bcBitCount
<= 8)
738 count
= 1 << core_header
->bcBitCount
;
740 offset
= sizeof(BITMAPFILEHEADER
) + header_size
+ sizeof(RGBTRIPLE
) * count
;
747 TRACE("Converting DIB file to BMP\n");
749 new_size
= *size
+ sizeof(BITMAPFILEHEADER
);
750 new_data
= HeapAlloc(GetProcessHeap(), 0, new_size
);
751 CopyMemory(new_data
+ sizeof(BITMAPFILEHEADER
), *data
, *size
);
754 header
= (BITMAPFILEHEADER
*)new_data
;
755 header
->bfType
= 0x4d42; /* BM */
756 header
->bfSize
= new_size
;
757 header
->bfReserved1
= 0;
758 header
->bfReserved2
= 0;
759 header
->bfOffBits
= offset
;
761 /* Update input data */
768 /************************************************************
769 * D3DXGetImageInfoFromFileInMemory
771 * Fills a D3DXIMAGE_INFO structure with info about an image
774 * data [I] pointer to the image file data
775 * datasize [I] size of the passed data
776 * info [O] pointer to the destination structure
779 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
780 * if info is NULL and data and datasize are not NULL
781 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
782 * D3DERR_INVALIDCALL, if data is NULL or
786 * datasize may be bigger than the actual file size
789 HRESULT WINAPI
D3DXGetImageInfoFromFileInMemory(const void *data
, UINT datasize
, D3DXIMAGE_INFO
*info
)
791 IWICImagingFactory
*factory
;
792 IWICBitmapDecoder
*decoder
= NULL
;
798 TRACE("(%p, %d, %p)\n", data
, datasize
, info
);
800 if (!data
|| !datasize
)
801 return D3DERR_INVALIDCALL
;
806 if ((datasize
>= 4) && !strncmp(data
, "DDS ", 4)) {
807 TRACE("File type is DDS\n");
808 return get_image_info_from_dds(data
, datasize
, info
);
811 /* In case of DIB file, convert it to BMP */
812 dib
= convert_dib_to_bmp((void**)&data
, &datasize
);
814 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
816 hr
= CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
);
819 IWICImagingFactory_CreateStream(factory
, &stream
);
820 IWICStream_InitializeFromMemory(stream
, (BYTE
*)data
, datasize
);
821 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
822 IWICStream_Release(stream
);
823 IWICImagingFactory_Release(factory
);
827 if ((datasize
>= 2) && (!strncmp(data
, "P3", 2) || !strncmp(data
, "P6", 2)))
828 FIXME("File type PPM is not supported yet\n");
829 else if ((datasize
>= 10) && !strncmp(data
, "#?RADIANCE", 10))
830 FIXME("File type HDR is not supported yet\n");
831 else if ((datasize
>= 2) && (!strncmp(data
, "PF", 2) || !strncmp(data
, "Pf", 2)))
832 FIXME("File type PFM is not supported yet\n");
836 GUID container_format
;
839 hr
= IWICBitmapDecoder_GetContainerFormat(decoder
, &container_format
);
841 if (IsEqualGUID(&container_format
, &GUID_ContainerFormatBmp
)) {
843 TRACE("File type is DIB\n");
844 info
->ImageFileFormat
= D3DXIFF_DIB
;
846 TRACE("File type is BMP\n");
847 info
->ImageFileFormat
= D3DXIFF_BMP
;
849 } else if (IsEqualGUID(&container_format
, &GUID_ContainerFormatPng
)) {
850 TRACE("File type is PNG\n");
851 info
->ImageFileFormat
= D3DXIFF_PNG
;
852 } else if(IsEqualGUID(&container_format
, &GUID_ContainerFormatJpeg
)) {
853 TRACE("File type is JPG\n");
854 info
->ImageFileFormat
= D3DXIFF_JPG
;
855 } else if(IsEqualGUID(&container_format
, &GUID_WineContainerFormatTga
)) {
856 TRACE("File type is TGA\n");
857 info
->ImageFileFormat
= D3DXIFF_TGA
;
859 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format
));
860 hr
= D3DXERR_INVALIDDATA
;
865 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
866 if (SUCCEEDED(hr
) && !frame_count
)
867 hr
= D3DXERR_INVALIDDATA
;
870 IWICBitmapFrameDecode
*frame
= NULL
;
872 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
875 hr
= IWICBitmapFrameDecode_GetSize(frame
, &info
->Width
, &info
->Height
);
878 WICPixelFormatGUID pixel_format
;
880 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &pixel_format
);
882 info
->Format
= wic_guid_to_d3dformat(&pixel_format
);
883 if (info
->Format
== D3DFMT_UNKNOWN
) {
884 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format
));
885 hr
= D3DXERR_INVALIDDATA
;
891 IWICBitmapFrameDecode_Release(frame
);
895 info
->ResourceType
= D3DRTYPE_TEXTURE
;
900 IWICBitmapDecoder_Release(decoder
);
902 if (SUCCEEDED(initresult
))
906 HeapFree(GetProcessHeap(), 0, (void*)data
);
909 TRACE("Invalid or unsupported image file\n");
910 return D3DXERR_INVALIDDATA
;
916 /************************************************************
917 * D3DXGetImageInfoFromFile
920 * Success: D3D_OK, if we successfully load a valid image file or
921 * if we successfully load a file which is no valid image and info is NULL
922 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
923 * if file is not a valid image file and info is not NULL
924 * D3DERR_INVALIDCALL, if file is NULL
927 HRESULT WINAPI
D3DXGetImageInfoFromFileA(const char *file
, D3DXIMAGE_INFO
*info
)
933 TRACE("file %s, info %p.\n", debugstr_a(file
), info
);
935 if( !file
) return D3DERR_INVALIDCALL
;
937 strlength
= MultiByteToWideChar(CP_ACP
, 0, file
, -1, NULL
, 0);
938 widename
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*widename
));
939 MultiByteToWideChar(CP_ACP
, 0, file
, -1, widename
, strlength
);
941 hr
= D3DXGetImageInfoFromFileW(widename
, info
);
942 HeapFree(GetProcessHeap(), 0, widename
);
947 HRESULT WINAPI
D3DXGetImageInfoFromFileW(const WCHAR
*file
, D3DXIMAGE_INFO
*info
)
953 TRACE("file %s, info %p.\n", debugstr_w(file
), info
);
956 return D3DERR_INVALIDCALL
;
958 if (FAILED(map_view_of_file(file
, &buffer
, &size
)))
959 return D3DXERR_INVALIDDATA
;
961 hr
= D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
962 UnmapViewOfFile(buffer
);
967 /************************************************************
968 * D3DXGetImageInfoFromResource
971 * Success: D3D_OK, if resource is a valid image file
972 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
973 * if we fail to load resource
976 HRESULT WINAPI
D3DXGetImageInfoFromResourceA(HMODULE module
, const char *resource
, D3DXIMAGE_INFO
*info
)
982 TRACE("module %p, resource %s, info %p.\n", module
, debugstr_a(resource
), info
);
984 if (!(resinfo
= FindResourceA(module
, resource
, (const char *)RT_RCDATA
))
985 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
986 && !(resinfo
= FindResourceA(module
, resource
, (const char *)RT_BITMAP
)))
987 return D3DXERR_INVALIDDATA
;
989 if (FAILED(load_resource_into_memory(module
, resinfo
, &buffer
, &size
)))
990 return D3DXERR_INVALIDDATA
;
992 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
995 HRESULT WINAPI
D3DXGetImageInfoFromResourceW(HMODULE module
, const WCHAR
*resource
, D3DXIMAGE_INFO
*info
)
1001 TRACE("module %p, resource %s, info %p.\n", module
, debugstr_w(resource
), info
);
1003 if (!(resinfo
= FindResourceW(module
, resource
, (const WCHAR
*)RT_RCDATA
))
1004 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1005 && !(resinfo
= FindResourceW(module
, resource
, (const WCHAR
*)RT_BITMAP
)))
1006 return D3DXERR_INVALIDDATA
;
1008 if (FAILED(load_resource_into_memory(module
, resinfo
, &buffer
, &size
)))
1009 return D3DXERR_INVALIDDATA
;
1011 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
1014 /************************************************************
1015 * D3DXLoadSurfaceFromFileInMemory
1017 * Loads data from a given buffer into a surface and fills a given
1018 * D3DXIMAGE_INFO structure with info about the source data.
1021 * pDestSurface [I] pointer to the surface
1022 * pDestPalette [I] palette to use
1023 * pDestRect [I] to be filled area of the surface
1024 * pSrcData [I] pointer to the source data
1025 * SrcDataSize [I] size of the source data in bytes
1026 * pSrcRect [I] area of the source data to load
1027 * dwFilter [I] filter to apply on stretching
1028 * Colorkey [I] colorkey
1029 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1033 * Failure: D3DERR_INVALIDCALL, if pDestSurface, pSrcData or SrcDataSize is NULL
1034 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1037 HRESULT WINAPI
D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9
*pDestSurface
,
1038 const PALETTEENTRY
*pDestPalette
, const RECT
*pDestRect
, const void *pSrcData
, UINT SrcDataSize
,
1039 const RECT
*pSrcRect
, DWORD dwFilter
, D3DCOLOR Colorkey
, D3DXIMAGE_INFO
*pSrcInfo
)
1041 D3DXIMAGE_INFO imginfo
;
1042 HRESULT hr
, com_init
;
1044 IWICImagingFactory
*factory
= NULL
;
1045 IWICBitmapDecoder
*decoder
;
1046 IWICBitmapFrameDecode
*bitmapframe
;
1049 const struct pixel_format_desc
*formatdesc
;
1053 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1054 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1055 pDestSurface
, pDestPalette
, wine_dbgstr_rect(pDestRect
), pSrcData
, SrcDataSize
,
1056 wine_dbgstr_rect(pSrcRect
), dwFilter
, Colorkey
, pSrcInfo
);
1058 if (!pDestSurface
|| !pSrcData
|| !SrcDataSize
)
1059 return D3DERR_INVALIDCALL
;
1061 hr
= D3DXGetImageInfoFromFileInMemory(pSrcData
, SrcDataSize
, &imginfo
);
1068 wicrect
.X
= pSrcRect
->left
;
1069 wicrect
.Y
= pSrcRect
->top
;
1070 wicrect
.Width
= pSrcRect
->right
- pSrcRect
->left
;
1071 wicrect
.Height
= pSrcRect
->bottom
- pSrcRect
->top
;
1077 wicrect
.Width
= imginfo
.Width
;
1078 wicrect
.Height
= imginfo
.Height
;
1081 SetRect(&rect
, 0, 0, wicrect
.Width
, wicrect
.Height
);
1083 if (imginfo
.ImageFileFormat
== D3DXIFF_DDS
)
1085 hr
= load_surface_from_dds(pDestSurface
, pDestPalette
, pDestRect
, pSrcData
, &rect
,
1086 dwFilter
, Colorkey
, &imginfo
);
1087 if (SUCCEEDED(hr
) && pSrcInfo
)
1088 *pSrcInfo
= imginfo
;
1092 if (imginfo
.ImageFileFormat
== D3DXIFF_DIB
)
1093 convert_dib_to_bmp((void**)&pSrcData
, &SrcDataSize
);
1095 com_init
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1097 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
)))
1100 if (FAILED(IWICImagingFactory_CreateStream(factory
, &stream
)))
1102 IWICImagingFactory_Release(factory
);
1107 IWICStream_InitializeFromMemory(stream
, (BYTE
*)pSrcData
, SrcDataSize
);
1109 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
1111 IWICStream_Release(stream
);
1116 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &bitmapframe
);
1121 formatdesc
= get_format_info(imginfo
.Format
);
1123 if (formatdesc
->type
== FORMAT_UNKNOWN
)
1125 FIXME("Unsupported pixel format\n");
1126 hr
= D3DXERR_INVALIDDATA
;
1132 PALETTEENTRY
*palette
= NULL
;
1133 WICColor
*colors
= NULL
;
1135 pitch
= formatdesc
->bytes_per_pixel
* wicrect
.Width
;
1136 buffer
= HeapAlloc(GetProcessHeap(), 0, pitch
* wicrect
.Height
);
1138 hr
= IWICBitmapFrameDecode_CopyPixels(bitmapframe
, &wicrect
, pitch
,
1139 pitch
* wicrect
.Height
, buffer
);
1141 if (SUCCEEDED(hr
) && (formatdesc
->type
== FORMAT_INDEX
))
1143 IWICPalette
*wic_palette
= NULL
;
1146 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
1148 hr
= IWICBitmapFrameDecode_CopyPalette(bitmapframe
, wic_palette
);
1150 hr
= IWICPalette_GetColorCount(wic_palette
, &nb_colors
);
1153 colors
= HeapAlloc(GetProcessHeap(), 0, nb_colors
* sizeof(colors
[0]));
1154 palette
= HeapAlloc(GetProcessHeap(), 0, nb_colors
* sizeof(palette
[0]));
1155 if (!colors
|| !palette
)
1159 hr
= IWICPalette_GetColors(wic_palette
, nb_colors
, colors
, &nb_colors
);
1164 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1165 for (i
= 0; i
< nb_colors
; i
++)
1167 palette
[i
].peRed
= (colors
[i
] >> 16) & 0xff;
1168 palette
[i
].peGreen
= (colors
[i
] >> 8) & 0xff;
1169 palette
[i
].peBlue
= colors
[i
] & 0xff;
1170 palette
[i
].peFlags
= (colors
[i
] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1174 IWICPalette_Release(wic_palette
);
1179 hr
= D3DXLoadSurfaceFromMemory(pDestSurface
, pDestPalette
, pDestRect
,
1180 buffer
, imginfo
.Format
, pitch
,
1181 palette
, &rect
, dwFilter
, Colorkey
);
1184 HeapFree(GetProcessHeap(), 0, colors
);
1185 HeapFree(GetProcessHeap(), 0, palette
);
1186 HeapFree(GetProcessHeap(), 0, buffer
);
1189 IWICBitmapFrameDecode_Release(bitmapframe
);
1192 IWICBitmapDecoder_Release(decoder
);
1196 IWICImagingFactory_Release(factory
);
1198 if (SUCCEEDED(com_init
))
1201 if (imginfo
.ImageFileFormat
== D3DXIFF_DIB
)
1202 HeapFree(GetProcessHeap(), 0, (void*)pSrcData
);
1205 return D3DXERR_INVALIDDATA
;
1208 *pSrcInfo
= imginfo
;
1213 HRESULT WINAPI
D3DXLoadSurfaceFromFileA(IDirect3DSurface9
*dst_surface
,
1214 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const char *src_file
,
1215 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1221 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1222 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1223 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), debugstr_a(src_file
),
1224 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1226 if (!src_file
|| !dst_surface
)
1227 return D3DERR_INVALIDCALL
;
1229 strlength
= MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, NULL
, 0);
1230 src_file_w
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*src_file_w
));
1231 MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, src_file_w
, strlength
);
1233 hr
= D3DXLoadSurfaceFromFileW(dst_surface
, dst_palette
, dst_rect
,
1234 src_file_w
, src_rect
, filter
, color_key
, src_info
);
1235 HeapFree(GetProcessHeap(), 0, src_file_w
);
1240 HRESULT WINAPI
D3DXLoadSurfaceFromFileW(IDirect3DSurface9
*dst_surface
,
1241 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const WCHAR
*src_file
,
1242 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1248 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1249 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1250 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), debugstr_w(src_file
),
1251 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1253 if (!src_file
|| !dst_surface
)
1254 return D3DERR_INVALIDCALL
;
1256 if (FAILED(map_view_of_file(src_file
, &data
, &data_size
)))
1257 return D3DXERR_INVALIDDATA
;
1259 hr
= D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1260 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1261 UnmapViewOfFile(data
);
1266 HRESULT WINAPI
D3DXLoadSurfaceFromResourceA(IDirect3DSurface9
*dst_surface
,
1267 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const char *resource
,
1268 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1274 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1275 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1276 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_a(resource
),
1277 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1280 return D3DERR_INVALIDCALL
;
1282 if (!(resinfo
= FindResourceA(src_module
, resource
, (const char *)RT_RCDATA
))
1283 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1284 && !(resinfo
= FindResourceA(src_module
, resource
, (const char *)RT_BITMAP
)))
1285 return D3DXERR_INVALIDDATA
;
1287 if (FAILED(load_resource_into_memory(src_module
, resinfo
, &data
, &data_size
)))
1288 return D3DXERR_INVALIDDATA
;
1290 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1291 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1294 HRESULT WINAPI
D3DXLoadSurfaceFromResourceW(IDirect3DSurface9
*dst_surface
,
1295 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const WCHAR
*resource
,
1296 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1302 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1303 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1304 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_w(resource
),
1305 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1308 return D3DERR_INVALIDCALL
;
1310 if (!(resinfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_RCDATA
))
1311 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1312 && !(resinfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_BITMAP
)))
1313 return D3DXERR_INVALIDDATA
;
1315 if (FAILED(load_resource_into_memory(src_module
, resinfo
, &data
, &data_size
)))
1316 return D3DXERR_INVALIDDATA
;
1318 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1319 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1323 /************************************************************
1324 * helper functions for D3DXLoadSurfaceFromMemory
1326 struct argb_conversion_info
1328 const struct pixel_format_desc
*srcformat
;
1329 const struct pixel_format_desc
*destformat
;
1330 DWORD srcshift
[4], destshift
[4];
1331 DWORD srcmask
[4], destmask
[4];
1332 BOOL process_channel
[4];
1336 static void init_argb_conversion_info(const struct pixel_format_desc
*srcformat
, const struct pixel_format_desc
*destformat
, struct argb_conversion_info
*info
)
1339 ZeroMemory(info
->process_channel
, 4 * sizeof(BOOL
));
1340 info
->channelmask
= 0;
1342 info
->srcformat
= srcformat
;
1343 info
->destformat
= destformat
;
1345 for(i
= 0;i
< 4;i
++) {
1346 /* srcshift is used to extract the _relevant_ components */
1347 info
->srcshift
[i
] = srcformat
->shift
[i
] + max( srcformat
->bits
[i
] - destformat
->bits
[i
], 0);
1349 /* destshift is used to move the components to the correct position */
1350 info
->destshift
[i
] = destformat
->shift
[i
] + max(destformat
->bits
[i
] - srcformat
->bits
[i
], 0);
1352 info
->srcmask
[i
] = ((1 << srcformat
->bits
[i
]) - 1) << srcformat
->shift
[i
];
1353 info
->destmask
[i
] = ((1 << destformat
->bits
[i
]) - 1) << destformat
->shift
[i
];
1355 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1356 if(destformat
->bits
[i
]) {
1357 if(srcformat
->bits
[i
]) info
->process_channel
[i
] = TRUE
;
1358 else info
->channelmask
|= info
->destmask
[i
];
1363 /************************************************************
1364 * get_relevant_argb_components
1366 * Extracts the relevant components from the source color and
1367 * drops the less significant bits if they aren't used by the destination format.
1369 static void get_relevant_argb_components(const struct argb_conversion_info
*info
, const BYTE
*col
, DWORD
*out
)
1372 unsigned int component
, mask
;
1374 for (i
= 0; i
< 4; ++i
)
1376 if (!info
->process_channel
[i
])
1380 mask
= info
->srcmask
[i
];
1381 for (j
= 0; j
< 4 && mask
; ++j
)
1383 if (info
->srcshift
[i
] < j
* 8)
1384 component
|= (col
[j
] & mask
) << (j
* 8 - info
->srcshift
[i
]);
1386 component
|= (col
[j
] & mask
) >> (info
->srcshift
[i
] - j
* 8);
1393 /************************************************************
1396 * Recombines the output of get_relevant_argb_components and converts
1397 * it to the destination format.
1399 static DWORD
make_argb_color(const struct argb_conversion_info
*info
, const DWORD
*in
)
1404 for(i
= 0;i
< 4;i
++) {
1405 if(info
->process_channel
[i
]) {
1406 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1408 for(shift
= info
->destshift
[i
]; shift
> info
->destformat
->shift
[i
]; shift
-= info
->srcformat
->bits
[i
]) val
|= in
[i
] << shift
;
1409 val
|= (in
[i
] >> (info
->destformat
->shift
[i
] - shift
)) << info
->destformat
->shift
[i
];
1412 val
|= info
->channelmask
; /* new channels are set to their maximal value */
1416 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */
1417 static void format_to_vec4(const struct pixel_format_desc
*format
, const BYTE
*src
, struct vec4
*dst
)
1422 for (c
= 0; c
< 4; ++c
)
1424 static const unsigned int component_offsets
[4] = {3, 0, 1, 2};
1425 float *dst_component
= (float *)dst
+ component_offsets
[c
];
1427 if (format
->bits
[c
])
1429 mask
= ~0u >> (32 - format
->bits
[c
]);
1431 memcpy(&tmp
, src
+ format
->shift
[c
] / 8,
1432 min(sizeof(DWORD
), (format
->shift
[c
] % 8 + format
->bits
[c
] + 7) / 8));
1434 if (format
->type
== FORMAT_ARGBF16
)
1435 *dst_component
= float_16_to_32(tmp
);
1436 else if (format
->type
== FORMAT_ARGBF
)
1437 *dst_component
= *(float *)&tmp
;
1439 *dst_component
= (float)((tmp
>> format
->shift
[c
] % 8) & mask
) / mask
;
1442 *dst_component
= 1.0f
;
1446 /* It doesn't work for components bigger than 32 bits. */
1447 static void format_from_vec4(const struct pixel_format_desc
*format
, const struct vec4
*src
, BYTE
*dst
)
1452 memset(dst
, 0, format
->bytes_per_pixel
);
1454 for (c
= 0; c
< 4; ++c
)
1456 static const unsigned int component_offsets
[4] = {3, 0, 1, 2};
1457 const float src_component
= *((const float *)src
+ component_offsets
[c
]);
1459 if (!format
->bits
[c
])
1462 mask32
= ~0u >> (32 - format
->bits
[c
]);
1464 if (format
->type
== FORMAT_ARGBF16
)
1465 v
= float_32_to_16(src_component
);
1466 else if (format
->type
== FORMAT_ARGBF
)
1467 v
= *(DWORD
*)&src_component
;
1469 v
= (DWORD
)(src_component
* ((1 << format
->bits
[c
]) - 1) + 0.5f
);
1471 for (i
= format
->shift
[c
] / 8 * 8; i
< format
->shift
[c
] + format
->bits
[c
]; i
+= 8)
1475 if (format
->shift
[c
] > i
)
1477 mask
= mask32
<< (format
->shift
[c
] - i
);
1478 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1482 mask
= mask32
>> (i
- format
->shift
[c
]);
1483 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1490 /************************************************************
1493 * Copies the source buffer to the destination buffer.
1494 * Works for any pixel format.
1495 * The source and the destination must be block-aligned.
1497 void copy_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
,
1498 BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
, const struct volume
*size
,
1499 const struct pixel_format_desc
*format
)
1503 const BYTE
*src_addr
;
1504 UINT row_block_count
= (size
->width
+ format
->block_width
- 1) / format
->block_width
;
1505 UINT row_count
= (size
->height
+ format
->block_height
- 1) / format
->block_height
;
1507 for (slice
= 0; slice
< size
->depth
; slice
++)
1509 src_addr
= src
+ slice
* src_slice_pitch
;
1510 dst_addr
= dst
+ slice
* dst_slice_pitch
;
1512 for (row
= 0; row
< row_count
; row
++)
1514 memcpy(dst_addr
, src_addr
, row_block_count
* format
->block_byte_count
);
1515 src_addr
+= src_row_pitch
;
1516 dst_addr
+= dst_row_pitch
;
1521 /************************************************************
1522 * convert_argb_pixels
1524 * Copies the source buffer to the destination buffer, performing
1525 * any necessary format conversion and color keying.
1526 * Pixels outsize the source rect are blacked out.
1528 void convert_argb_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
, const struct volume
*src_size
,
1529 const struct pixel_format_desc
*src_format
, BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
,
1530 const struct volume
*dst_size
, const struct pixel_format_desc
*dst_format
, D3DCOLOR color_key
,
1531 const PALETTEENTRY
*palette
)
1533 struct argb_conversion_info conv_info
, ck_conv_info
;
1534 const struct pixel_format_desc
*ck_format
= NULL
;
1536 UINT min_width
, min_height
, min_depth
;
1539 ZeroMemory(channels
, sizeof(channels
));
1540 init_argb_conversion_info(src_format
, dst_format
, &conv_info
);
1542 min_width
= min(src_size
->width
, dst_size
->width
);
1543 min_height
= min(src_size
->height
, dst_size
->height
);
1544 min_depth
= min(src_size
->depth
, dst_size
->depth
);
1548 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1549 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1550 init_argb_conversion_info(src_format
, ck_format
, &ck_conv_info
);
1553 for (z
= 0; z
< min_depth
; z
++) {
1554 const BYTE
*src_slice_ptr
= src
+ z
* src_slice_pitch
;
1555 BYTE
*dst_slice_ptr
= dst
+ z
* dst_slice_pitch
;
1557 for (y
= 0; y
< min_height
; y
++) {
1558 const BYTE
*src_ptr
= src_slice_ptr
+ y
* src_row_pitch
;
1559 BYTE
*dst_ptr
= dst_slice_ptr
+ y
* dst_row_pitch
;
1561 for (x
= 0; x
< min_width
; x
++) {
1562 if (!src_format
->to_rgba
&& !dst_format
->from_rgba
1563 && src_format
->type
== dst_format
->type
1564 && src_format
->bytes_per_pixel
<= 4 && dst_format
->bytes_per_pixel
<= 4)
1568 get_relevant_argb_components(&conv_info
, src_ptr
, channels
);
1569 val
= make_argb_color(&conv_info
, channels
);
1575 get_relevant_argb_components(&ck_conv_info
, src_ptr
, channels
);
1576 ck_pixel
= make_argb_color(&ck_conv_info
, channels
);
1577 if (ck_pixel
== color_key
)
1578 val
&= ~conv_info
.destmask
[0];
1580 memcpy(dst_ptr
, &val
, dst_format
->bytes_per_pixel
);
1584 struct vec4 color
, tmp
;
1586 format_to_vec4(src_format
, src_ptr
, &color
);
1587 if (src_format
->to_rgba
)
1588 src_format
->to_rgba(&color
, &tmp
, palette
);
1596 format_from_vec4(ck_format
, &tmp
, (BYTE
*)&ck_pixel
);
1597 if (ck_pixel
== color_key
)
1601 if (dst_format
->from_rgba
)
1602 dst_format
->from_rgba(&tmp
, &color
);
1606 format_from_vec4(dst_format
, &color
, dst_ptr
);
1609 src_ptr
+= src_format
->bytes_per_pixel
;
1610 dst_ptr
+= dst_format
->bytes_per_pixel
;
1613 if (src_size
->width
< dst_size
->width
) /* black out remaining pixels */
1614 memset(dst_ptr
, 0, dst_format
->bytes_per_pixel
* (dst_size
->width
- src_size
->width
));
1617 if (src_size
->height
< dst_size
->height
) /* black out remaining pixels */
1618 memset(dst
+ src_size
->height
* dst_row_pitch
, 0, dst_row_pitch
* (dst_size
->height
- src_size
->height
));
1620 if (src_size
->depth
< dst_size
->depth
) /* black out remaining pixels */
1621 memset(dst
+ src_size
->depth
* dst_slice_pitch
, 0, dst_slice_pitch
* (dst_size
->depth
- src_size
->depth
));
1624 /************************************************************
1625 * point_filter_argb_pixels
1627 * Copies the source buffer to the destination buffer, performing
1628 * any necessary format conversion, color keying and stretching
1629 * using a point filter.
1631 void point_filter_argb_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
, const struct volume
*src_size
,
1632 const struct pixel_format_desc
*src_format
, BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
,
1633 const struct volume
*dst_size
, const struct pixel_format_desc
*dst_format
, D3DCOLOR color_key
,
1634 const PALETTEENTRY
*palette
)
1636 struct argb_conversion_info conv_info
, ck_conv_info
;
1637 const struct pixel_format_desc
*ck_format
= NULL
;
1641 ZeroMemory(channels
, sizeof(channels
));
1642 init_argb_conversion_info(src_format
, dst_format
, &conv_info
);
1646 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1647 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1648 init_argb_conversion_info(src_format
, ck_format
, &ck_conv_info
);
1651 for (z
= 0; z
< dst_size
->depth
; z
++)
1653 BYTE
*dst_slice_ptr
= dst
+ z
* dst_slice_pitch
;
1654 const BYTE
*src_slice_ptr
= src
+ src_slice_pitch
* (z
* src_size
->depth
/ dst_size
->depth
);
1656 for (y
= 0; y
< dst_size
->height
; y
++)
1658 BYTE
*dst_ptr
= dst_slice_ptr
+ y
* dst_row_pitch
;
1659 const BYTE
*src_row_ptr
= src_slice_ptr
+ src_row_pitch
* (y
* src_size
->height
/ dst_size
->height
);
1661 for (x
= 0; x
< dst_size
->width
; x
++)
1663 const BYTE
*src_ptr
= src_row_ptr
+ (x
* src_size
->width
/ dst_size
->width
) * src_format
->bytes_per_pixel
;
1665 if (!src_format
->to_rgba
&& !dst_format
->from_rgba
1666 && src_format
->type
== dst_format
->type
1667 && src_format
->bytes_per_pixel
<= 4 && dst_format
->bytes_per_pixel
<= 4)
1671 get_relevant_argb_components(&conv_info
, src_ptr
, channels
);
1672 val
= make_argb_color(&conv_info
, channels
);
1678 get_relevant_argb_components(&ck_conv_info
, src_ptr
, channels
);
1679 ck_pixel
= make_argb_color(&ck_conv_info
, channels
);
1680 if (ck_pixel
== color_key
)
1681 val
&= ~conv_info
.destmask
[0];
1683 memcpy(dst_ptr
, &val
, dst_format
->bytes_per_pixel
);
1687 struct vec4 color
, tmp
;
1689 format_to_vec4(src_format
, src_ptr
, &color
);
1690 if (src_format
->to_rgba
)
1691 src_format
->to_rgba(&color
, &tmp
, palette
);
1699 format_from_vec4(ck_format
, &tmp
, (BYTE
*)&ck_pixel
);
1700 if (ck_pixel
== color_key
)
1704 if (dst_format
->from_rgba
)
1705 dst_format
->from_rgba(&tmp
, &color
);
1709 format_from_vec4(dst_format
, &color
, dst_ptr
);
1712 dst_ptr
+= dst_format
->bytes_per_pixel
;
1718 /************************************************************
1719 * D3DXLoadSurfaceFromMemory
1721 * Loads data from a given memory chunk into a surface,
1722 * applying any of the specified filters.
1725 * pDestSurface [I] pointer to the surface
1726 * pDestPalette [I] palette to use
1727 * pDestRect [I] to be filled area of the surface
1728 * pSrcMemory [I] pointer to the source data
1729 * SrcFormat [I] format of the source pixel data
1730 * SrcPitch [I] number of bytes in a row
1731 * pSrcPalette [I] palette used in the source image
1732 * pSrcRect [I] area of the source data to load
1733 * dwFilter [I] filter to apply on stretching
1734 * Colorkey [I] colorkey
1737 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1738 * if pSrcMemory is NULL but the other parameters are valid
1739 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect is NULL or
1740 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1741 * if DestRect is invalid
1742 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1743 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1746 * pSrcRect specifies the dimensions of the source data;
1747 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1750 HRESULT WINAPI
D3DXLoadSurfaceFromMemory(IDirect3DSurface9
*dst_surface
,
1751 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const void *src_memory
,
1752 D3DFORMAT src_format
, UINT src_pitch
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
,
1753 DWORD filter
, D3DCOLOR color_key
)
1755 const struct pixel_format_desc
*srcformatdesc
, *destformatdesc
;
1756 D3DSURFACE_DESC surfdesc
;
1757 D3DLOCKED_RECT lockrect
;
1758 struct volume src_size
, dst_size
;
1760 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s, %#x, 0x%08x)\n",
1761 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_memory
, src_format
,
1762 src_pitch
, src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1764 if (!dst_surface
|| !src_memory
|| !src_rect
)
1766 WARN("Invalid argument specified.\n");
1767 return D3DERR_INVALIDCALL
;
1769 if (src_format
== D3DFMT_UNKNOWN
1770 || src_rect
->left
>= src_rect
->right
1771 || src_rect
->top
>= src_rect
->bottom
)
1773 WARN("Invalid src_format or src_rect.\n");
1777 if (filter
== D3DX_DEFAULT
)
1778 filter
= D3DX_FILTER_TRIANGLE
| D3DX_FILTER_DITHER
;
1780 IDirect3DSurface9_GetDesc(dst_surface
, &surfdesc
);
1782 src_size
.width
= src_rect
->right
- src_rect
->left
;
1783 src_size
.height
= src_rect
->bottom
- src_rect
->top
;
1787 dst_size
.width
= surfdesc
.Width
;
1788 dst_size
.height
= surfdesc
.Height
;
1792 if (dst_rect
->left
> dst_rect
->right
|| dst_rect
->right
> surfdesc
.Width
1793 || dst_rect
->top
> dst_rect
->bottom
|| dst_rect
->bottom
> surfdesc
.Height
1794 || dst_rect
->left
< 0 || dst_rect
->top
< 0)
1796 WARN("Invalid dst_rect specified.\n");
1797 return D3DERR_INVALIDCALL
;
1799 dst_size
.width
= dst_rect
->right
- dst_rect
->left
;
1800 dst_size
.height
= dst_rect
->bottom
- dst_rect
->top
;
1801 if (!dst_size
.width
|| !dst_size
.height
)
1806 srcformatdesc
= get_format_info(src_format
);
1807 destformatdesc
= get_format_info(surfdesc
.Format
);
1808 if (srcformatdesc
->type
== FORMAT_UNKNOWN
|| destformatdesc
->type
== FORMAT_UNKNOWN
)
1810 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format
, surfdesc
.Format
);
1814 if (src_format
== surfdesc
.Format
1815 && dst_size
.width
== src_size
.width
1816 && dst_size
.height
== src_size
.height
1817 && color_key
== 0) /* Simple copy. */
1819 if (src_rect
->left
& (srcformatdesc
->block_width
- 1)
1820 || src_rect
->top
& (srcformatdesc
->block_height
- 1)
1821 || (src_rect
->right
& (srcformatdesc
->block_width
- 1)
1822 && src_size
.width
!= surfdesc
.Width
)
1823 || (src_rect
->bottom
& (srcformatdesc
->block_height
- 1)
1824 && src_size
.height
!= surfdesc
.Height
))
1826 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect
));
1827 return D3DXERR_INVALIDDATA
;
1830 if (FAILED(IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1831 return D3DXERR_INVALIDDATA
;
1833 copy_pixels(src_memory
, src_pitch
, 0, lockrect
.pBits
, lockrect
.Pitch
, 0,
1834 &src_size
, srcformatdesc
);
1836 IDirect3DSurface9_UnlockRect(dst_surface
);
1838 else /* Stretching or format conversion. */
1840 if (((srcformatdesc
->type
!= FORMAT_ARGB
) && (srcformatdesc
->type
!= FORMAT_INDEX
)) ||
1841 (destformatdesc
->type
!= FORMAT_ARGB
))
1843 FIXME("Format conversion missing %#x -> %#x\n", src_format
, surfdesc
.Format
);
1847 if (FAILED(IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1848 return D3DXERR_INVALIDDATA
;
1850 if ((filter
& 0xf) == D3DX_FILTER_NONE
)
1852 convert_argb_pixels(src_memory
, src_pitch
, 0, &src_size
, srcformatdesc
,
1853 lockrect
.pBits
, lockrect
.Pitch
, 0, &dst_size
, destformatdesc
, color_key
, src_palette
);
1855 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1857 if ((filter
& 0xf) != D3DX_FILTER_POINT
)
1858 FIXME("Unhandled filter %#x.\n", filter
);
1860 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1861 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1862 point_filter_argb_pixels(src_memory
, src_pitch
, 0, &src_size
, srcformatdesc
,
1863 lockrect
.pBits
, lockrect
.Pitch
, 0, &dst_size
, destformatdesc
, color_key
, src_palette
);
1866 IDirect3DSurface9_UnlockRect(dst_surface
);
1872 /************************************************************
1873 * D3DXLoadSurfaceFromSurface
1875 * Copies the contents from one surface to another, performing any required
1876 * format conversion, resizing or filtering.
1879 * pDestSurface [I] pointer to the destination surface
1880 * pDestPalette [I] palette to use
1881 * pDestRect [I] to be filled area of the surface
1882 * pSrcSurface [I] pointer to the source surface
1883 * pSrcPalette [I] palette used for the source surface
1884 * pSrcRect [I] area of the source data to load
1885 * dwFilter [I] filter to apply on resizing
1886 * Colorkey [I] any ARGB value or 0 to disable color-keying
1890 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface is NULL
1891 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1894 HRESULT WINAPI
D3DXLoadSurfaceFromSurface(IDirect3DSurface9
*dst_surface
,
1895 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, IDirect3DSurface9
*src_surface
,
1896 const PALETTEENTRY
*src_palette
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
)
1899 D3DLOCKED_RECT lock
;
1900 D3DSURFACE_DESC SrcDesc
;
1903 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1904 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1905 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_surface
,
1906 src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1908 if (!dst_surface
|| !src_surface
)
1909 return D3DERR_INVALIDCALL
;
1911 IDirect3DSurface9_GetDesc(src_surface
, &SrcDesc
);
1914 SetRect(&rect
, 0, 0, SrcDesc
.Width
, SrcDesc
.Height
);
1918 if (FAILED(IDirect3DSurface9_LockRect(src_surface
, &lock
, NULL
, D3DLOCK_READONLY
)))
1919 return D3DXERR_INVALIDDATA
;
1921 hr
= D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
,
1922 lock
.pBits
, SrcDesc
.Format
, lock
.Pitch
, src_palette
, &rect
, filter
, color_key
);
1924 IDirect3DSurface9_UnlockRect(src_surface
);
1930 HRESULT WINAPI
D3DXSaveSurfaceToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1931 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1936 ID3DXBuffer
*buffer
;
1938 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1939 wine_dbgstr_a(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
1941 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1943 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1944 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1945 if (!filename
) return E_OUTOFMEMORY
;
1946 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1948 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
1951 hr
= write_buffer_to_file(filename
, buffer
);
1952 ID3DXBuffer_Release(buffer
);
1955 HeapFree(GetProcessHeap(), 0, filename
);
1959 HRESULT WINAPI
D3DXSaveSurfaceToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1960 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1963 ID3DXBuffer
*buffer
;
1965 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1966 wine_dbgstr_w(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
1968 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1970 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
1973 hr
= write_buffer_to_file(dst_filename
, buffer
);
1974 ID3DXBuffer_Release(buffer
);
1980 HRESULT WINAPI
D3DXSaveSurfaceToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1981 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1983 IWICBitmapEncoder
*encoder
= NULL
;
1984 IWICBitmapFrameEncode
*frame
= NULL
;
1985 IPropertyBag2
*encoder_options
= NULL
;
1986 IStream
*stream
= NULL
;
1989 const CLSID
*encoder_clsid
;
1990 const GUID
*pixel_format_guid
;
1991 WICPixelFormatGUID wic_pixel_format
;
1992 D3DFORMAT d3d_pixel_format
;
1993 D3DSURFACE_DESC src_surface_desc
;
1994 D3DLOCKED_RECT locked_rect
;
1996 STATSTG stream_stats
;
1997 HGLOBAL stream_hglobal
;
1998 ID3DXBuffer
*buffer
;
2001 TRACE("(%p, %#x, %p, %p, %s)\n",
2002 dst_buffer
, file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
2004 if (!dst_buffer
|| !src_surface
) return D3DERR_INVALIDCALL
;
2008 FIXME("Saving surfaces with palettized pixel formats is not implemented yet\n");
2009 return D3DERR_INVALIDCALL
;
2012 switch (file_format
)
2016 encoder_clsid
= &CLSID_WICBmpEncoder
;
2019 encoder_clsid
= &CLSID_WICPngEncoder
;
2022 encoder_clsid
= &CLSID_WICJpegEncoder
;
2025 return save_dds_surface_to_memory(dst_buffer
, src_surface
, src_rect
);
2030 FIXME("File format %#x is not supported yet\n", file_format
);
2033 return D3DERR_INVALIDCALL
;
2036 IDirect3DSurface9_GetDesc(src_surface
, &src_surface_desc
);
2039 if (src_rect
->left
== src_rect
->right
|| src_rect
->top
== src_rect
->bottom
)
2041 WARN("Invalid rectangle with 0 area\n");
2042 return D3DXCreateBuffer(64, dst_buffer
);
2044 if (src_rect
->left
< 0 || src_rect
->top
< 0)
2045 return D3DERR_INVALIDCALL
;
2046 if (src_rect
->left
> src_rect
->right
|| src_rect
->top
> src_rect
->bottom
)
2047 return D3DERR_INVALIDCALL
;
2048 if (src_rect
->right
> src_surface_desc
.Width
|| src_rect
->bottom
> src_surface_desc
.Height
)
2049 return D3DERR_INVALIDCALL
;
2051 width
= src_rect
->right
- src_rect
->left
;
2052 height
= src_rect
->bottom
- src_rect
->top
;
2056 width
= src_surface_desc
.Width
;
2057 height
= src_surface_desc
.Height
;
2060 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
2062 hr
= CoCreateInstance(encoder_clsid
, NULL
, CLSCTX_INPROC_SERVER
,
2063 &IID_IWICBitmapEncoder
, (void **)&encoder
);
2064 if (FAILED(hr
)) goto cleanup_err
;
2066 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &stream
);
2067 if (FAILED(hr
)) goto cleanup_err
;
2069 hr
= IWICBitmapEncoder_Initialize(encoder
, stream
, WICBitmapEncoderNoCache
);
2070 if (FAILED(hr
)) goto cleanup_err
;
2072 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frame
, &encoder_options
);
2073 if (FAILED(hr
)) goto cleanup_err
;
2075 hr
= IWICBitmapFrameEncode_Initialize(frame
, encoder_options
);
2076 if (FAILED(hr
)) goto cleanup_err
;
2078 hr
= IWICBitmapFrameEncode_SetSize(frame
, width
, height
);
2079 if (FAILED(hr
)) goto cleanup_err
;
2081 pixel_format_guid
= d3dformat_to_wic_guid(src_surface_desc
.Format
);
2082 if (!pixel_format_guid
)
2084 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc
.Format
);
2089 memcpy(&wic_pixel_format
, pixel_format_guid
, sizeof(GUID
));
2090 hr
= IWICBitmapFrameEncode_SetPixelFormat(frame
, &wic_pixel_format
);
2091 d3d_pixel_format
= wic_guid_to_d3dformat(&wic_pixel_format
);
2092 if (SUCCEEDED(hr
) && d3d_pixel_format
!= D3DFMT_UNKNOWN
)
2094 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format
), d3d_pixel_format
);
2096 if (src_surface_desc
.Format
== d3d_pixel_format
) /* Simple copy */
2098 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
2101 IWICBitmapFrameEncode_WritePixels(frame
, height
,
2102 locked_rect
.Pitch
, height
* locked_rect
.Pitch
, locked_rect
.pBits
);
2103 IDirect3DSurface9_UnlockRect(src_surface
);
2106 else /* Pixel format conversion */
2108 const struct pixel_format_desc
*src_format_desc
, *dst_format_desc
;
2113 src_format_desc
= get_format_info(src_surface_desc
.Format
);
2114 dst_format_desc
= get_format_info(d3d_pixel_format
);
2115 if (src_format_desc
->type
!= FORMAT_ARGB
|| dst_format_desc
->type
!= FORMAT_ARGB
)
2117 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
2118 src_surface_desc
.Format
, d3d_pixel_format
);
2124 size
.height
= height
;
2126 dst_pitch
= width
* dst_format_desc
->bytes_per_pixel
;
2127 dst_data
= HeapAlloc(GetProcessHeap(), 0, dst_pitch
* height
);
2134 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
2137 convert_argb_pixels(locked_rect
.pBits
, locked_rect
.Pitch
, 0, &size
, src_format_desc
,
2138 dst_data
, dst_pitch
, 0, &size
, dst_format_desc
, 0, NULL
);
2139 IDirect3DSurface9_UnlockRect(src_surface
);
2142 IWICBitmapFrameEncode_WritePixels(frame
, height
, dst_pitch
, dst_pitch
* height
, dst_data
);
2143 HeapFree(GetProcessHeap(), 0, dst_data
);
2146 hr
= IWICBitmapFrameEncode_Commit(frame
);
2147 if (SUCCEEDED(hr
)) hr
= IWICBitmapEncoder_Commit(encoder
);
2149 else WARN("Unsupported pixel format %#x\n", src_surface_desc
.Format
);
2151 /* copy data from stream to ID3DXBuffer */
2152 hr
= IStream_Stat(stream
, &stream_stats
, STATFLAG_NONAME
);
2153 if (FAILED(hr
)) goto cleanup_err
;
2155 if (stream_stats
.cbSize
.u
.HighPart
!= 0)
2157 hr
= D3DXERR_INVALIDDATA
;
2160 size
= stream_stats
.cbSize
.u
.LowPart
;
2162 /* Remove BMP header for DIB */
2163 if (file_format
== D3DXIFF_DIB
)
2164 size
-= sizeof(BITMAPFILEHEADER
);
2166 hr
= D3DXCreateBuffer(size
, &buffer
);
2167 if (FAILED(hr
)) goto cleanup
;
2169 hr
= GetHGlobalFromStream(stream
, &stream_hglobal
);
2172 void *buffer_pointer
= ID3DXBuffer_GetBufferPointer(buffer
);
2173 void *stream_data
= GlobalLock(stream_hglobal
);
2174 /* Remove BMP header for DIB */
2175 if (file_format
== D3DXIFF_DIB
)
2176 stream_data
= (void*)((BYTE
*)stream_data
+ sizeof(BITMAPFILEHEADER
));
2177 memcpy(buffer_pointer
, stream_data
, size
);
2178 GlobalUnlock(stream_hglobal
);
2179 *dst_buffer
= buffer
;
2181 else ID3DXBuffer_Release(buffer
);
2184 if (FAILED(hr
) && hr
!= E_OUTOFMEMORY
)
2185 hr
= D3DERR_INVALIDCALL
;
2188 if (stream
) IStream_Release(stream
);
2190 if (frame
) IWICBitmapFrameEncode_Release(frame
);
2191 if (encoder_options
) IPropertyBag2_Release(encoder_options
);
2193 if (encoder
) IWICBitmapEncoder_Release(encoder
);
2195 if (SUCCEEDED(initresult
)) CoUninitialize();