2 * Copyright (C) 2009-2010 Tony Wasserka
3 * Copyright (C) 2012 Józef Kucia
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/debug.h"
22 #include "wine/unicode.h"
23 #include "d3dx9_36_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
32 /* Wine-specific WIC GUIDs */
33 DEFINE_GUID(GUID_WineContainerFormatTga
, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
39 } wic_pixel_formats
[] = {
40 { &GUID_WICPixelFormat8bppIndexed
, D3DFMT_P8
},
41 { &GUID_WICPixelFormat1bppIndexed
, D3DFMT_P8
},
42 { &GUID_WICPixelFormat4bppIndexed
, D3DFMT_P8
},
43 { &GUID_WICPixelFormat8bppGray
, D3DFMT_L8
},
44 { &GUID_WICPixelFormat16bppBGR555
, D3DFMT_X1R5G5B5
},
45 { &GUID_WICPixelFormat16bppBGR565
, D3DFMT_R5G6B5
},
46 { &GUID_WICPixelFormat24bppBGR
, D3DFMT_R8G8B8
},
47 { &GUID_WICPixelFormat32bppBGR
, D3DFMT_X8R8G8B8
},
48 { &GUID_WICPixelFormat32bppBGRA
, D3DFMT_A8R8G8B8
}
51 static D3DFORMAT
wic_guid_to_d3dformat(const GUID
*guid
)
55 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
57 if (IsEqualGUID(wic_pixel_formats
[i
].wic_guid
, guid
))
58 return wic_pixel_formats
[i
].d3dformat
;
61 return D3DFMT_UNKNOWN
;
64 static const GUID
*d3dformat_to_wic_guid(D3DFORMAT format
)
68 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
70 if (wic_pixel_formats
[i
].d3dformat
== format
)
71 return wic_pixel_formats
[i
].wic_guid
;
77 /* dds_header.flags */
79 #define DDS_HEIGHT 0x2
82 #define DDS_PIXELFORMAT 0x1000
83 #define DDS_MIPMAPCOUNT 0x20000
84 #define DDS_LINEARSIZE 0x80000
85 #define DDS_DEPTH 0x800000
88 #define DDS_CAPS_COMPLEX 0x8
89 #define DDS_CAPS_TEXTURE 0x1000
90 #define DDS_CAPS_MIPMAP 0x400000
92 /* dds_header.caps2 */
93 #define DDS_CAPS2_CUBEMAP 0x200
94 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
95 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
96 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
97 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
98 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
99 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
100 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
101 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
102 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
103 #define DDS_CAPS2_VOLUME 0x200000
105 /* dds_pixel_format.flags */
106 #define DDS_PF_ALPHA 0x1
107 #define DDS_PF_ALPHA_ONLY 0x2
108 #define DDS_PF_FOURCC 0x4
109 #define DDS_PF_RGB 0x40
110 #define DDS_PF_YUV 0x200
111 #define DDS_PF_LUMINANCE 0x20000
112 #define DDS_PF_BUMPDUDV 0x80000
114 struct dds_pixel_format
133 DWORD pitch_or_linear_size
;
137 struct dds_pixel_format pixel_format
;
145 static D3DFORMAT
dds_fourcc_to_d3dformat(DWORD fourcc
)
148 static const DWORD known_fourcc
[] = {
149 MAKEFOURCC('U','Y','V','Y'),
150 MAKEFOURCC('Y','U','Y','2'),
151 MAKEFOURCC('R','G','B','G'),
152 MAKEFOURCC('G','R','G','B'),
153 MAKEFOURCC('D','X','T','1'),
154 MAKEFOURCC('D','X','T','2'),
155 MAKEFOURCC('D','X','T','3'),
156 MAKEFOURCC('D','X','T','4'),
157 MAKEFOURCC('D','X','T','5'),
160 D3DFMT_A16B16G16R16F
,
163 D3DFMT_A32B32G32R32F
,
166 for (i
= 0; i
< sizeof(known_fourcc
) / sizeof(known_fourcc
[0]); i
++)
168 if (known_fourcc
[i
] == fourcc
)
172 WARN("Unknown FourCC %#x\n", fourcc
);
173 return D3DFMT_UNKNOWN
;
176 static const struct {
183 } rgb_pixel_formats
[] = {
184 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2
},
185 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5
},
186 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5
},
187 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5
},
188 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4
},
189 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4
},
190 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2
},
191 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8
},
192 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8
},
193 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8
},
194 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10
},
195 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10
},
196 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16
},
197 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8
},
198 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8
},
201 static D3DFORMAT
dds_rgb_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
205 for (i
= 0; i
< sizeof(rgb_pixel_formats
) / sizeof(rgb_pixel_formats
[0]); i
++)
207 if (rgb_pixel_formats
[i
].bpp
== pixel_format
->bpp
208 && rgb_pixel_formats
[i
].rmask
== pixel_format
->rmask
209 && rgb_pixel_formats
[i
].gmask
== pixel_format
->gmask
210 && rgb_pixel_formats
[i
].bmask
== pixel_format
->bmask
)
212 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && rgb_pixel_formats
[i
].amask
== pixel_format
->amask
)
213 return rgb_pixel_formats
[i
].format
;
214 if (rgb_pixel_formats
[i
].amask
== 0)
215 return rgb_pixel_formats
[i
].format
;
219 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
220 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
221 return D3DFMT_UNKNOWN
;
224 static D3DFORMAT
dds_luminance_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
226 if (pixel_format
->bpp
== 8)
228 if (pixel_format
->rmask
== 0xff)
230 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x0f && pixel_format
->amask
== 0xf0)
233 if (pixel_format
->bpp
== 16)
235 if (pixel_format
->rmask
== 0xffff)
237 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x00ff && pixel_format
->amask
== 0xff00)
241 WARN("Unknown luminance pixel format (bpp %u, l %#x, a %#x)\n",
242 pixel_format
->bpp
, pixel_format
->rmask
, pixel_format
->amask
);
243 return D3DFMT_UNKNOWN
;
246 static D3DFORMAT
dds_alpha_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
248 if (pixel_format
->bpp
== 8 && pixel_format
->amask
== 0xff)
251 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format
->bpp
, pixel_format
->rmask
);
252 return D3DFMT_UNKNOWN
;
255 static D3DFORMAT
dds_bump_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
257 if (pixel_format
->bpp
== 16 && pixel_format
->rmask
== 0x00ff && pixel_format
->gmask
== 0xff00)
259 if (pixel_format
->bpp
== 32 && pixel_format
->rmask
== 0x0000ffff && pixel_format
->gmask
== 0xffff0000)
260 return D3DFMT_V16U16
;
262 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format
->bpp
,
263 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
264 return D3DFMT_UNKNOWN
;
267 static D3DFORMAT
dds_pixel_format_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
269 TRACE("pixel_format: size %u, flags %#x, fourcc %#x, bpp %u.\n", pixel_format
->size
,
270 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
);
271 TRACE("rmask %#x, gmask %#x, bmask %#x, amask %#x.\n", pixel_format
->rmask
, pixel_format
->gmask
,
272 pixel_format
->bmask
, pixel_format
->amask
);
274 if (pixel_format
->flags
& DDS_PF_FOURCC
)
275 return dds_fourcc_to_d3dformat(pixel_format
->fourcc
);
276 if (pixel_format
->flags
& DDS_PF_RGB
)
277 return dds_rgb_to_d3dformat(pixel_format
);
278 if (pixel_format
->flags
& DDS_PF_LUMINANCE
)
279 return dds_luminance_to_d3dformat(pixel_format
);
280 if (pixel_format
->flags
& DDS_PF_ALPHA_ONLY
)
281 return dds_alpha_to_d3dformat(pixel_format
);
282 if (pixel_format
->flags
& DDS_PF_BUMPDUDV
)
283 return dds_bump_to_d3dformat(pixel_format
);
285 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n",
286 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
,
287 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
288 return D3DFMT_UNKNOWN
;
291 static HRESULT
d3dformat_to_dds_pixel_format(struct dds_pixel_format
*pixel_format
, D3DFORMAT d3dformat
)
295 memset(pixel_format
, 0, sizeof(*pixel_format
));
297 pixel_format
->size
= sizeof(*pixel_format
);
299 for (i
= 0; i
< sizeof(rgb_pixel_formats
) / sizeof(rgb_pixel_formats
[0]); i
++)
301 if (rgb_pixel_formats
[i
].format
== d3dformat
)
303 pixel_format
->flags
|= DDS_PF_RGB
;
304 pixel_format
->bpp
= rgb_pixel_formats
[i
].bpp
;
305 pixel_format
->rmask
= rgb_pixel_formats
[i
].rmask
;
306 pixel_format
->gmask
= rgb_pixel_formats
[i
].gmask
;
307 pixel_format
->bmask
= rgb_pixel_formats
[i
].bmask
;
308 pixel_format
->amask
= rgb_pixel_formats
[i
].amask
;
309 if (pixel_format
->amask
) pixel_format
->flags
|= DDS_PF_ALPHA
;
314 WARN("Unknown pixel format %#x\n", d3dformat
);
318 static HRESULT
calculate_dds_surface_size(D3DFORMAT format
, UINT width
, UINT height
,
319 UINT
*pitch
, UINT
*size
)
321 const struct pixel_format_desc
*format_desc
= get_format_info(format
);
322 if (format_desc
->type
== FORMAT_UNKNOWN
)
325 if (format_desc
->block_width
!= 1 || format_desc
->block_height
!= 1)
327 *pitch
= format_desc
->block_byte_count
328 * max(1, (width
+ format_desc
->block_width
- 1) / format_desc
->block_width
);
330 * max(1, (height
+ format_desc
->block_height
- 1) / format_desc
->block_height
);
334 *pitch
= width
* format_desc
->bytes_per_pixel
;
335 *size
= *pitch
* height
;
341 static UINT
calculate_dds_file_size(D3DFORMAT format
, UINT width
, UINT height
, UINT depth
,
342 UINT miplevels
, UINT faces
)
344 UINT i
, file_size
= 0;
346 for (i
= 0; i
< miplevels
; i
++)
348 UINT pitch
, size
= 0;
349 calculate_dds_surface_size(format
, width
, height
, &pitch
, &size
);
352 width
= max(1, width
/ 2);
353 height
= max(1, height
/ 2);
354 depth
= max(1, depth
/ 2);
358 file_size
+= sizeof(struct dds_header
);
362 /************************************************************
363 * get_image_info_from_dds
365 * Fills a D3DXIMAGE_INFO structure with information
366 * about a DDS file stored in the memory.
369 * buffer [I] pointer to DDS data
370 * length [I] size of DDS data
371 * info [O] pointer to D3DXIMAGE_INFO structure
375 * Failure: D3DXERR_INVALIDDATA
378 static HRESULT
get_image_info_from_dds(const void *buffer
, UINT length
, D3DXIMAGE_INFO
*info
)
381 UINT expected_length
;
382 const struct dds_header
*header
= buffer
;
384 if (length
< sizeof(*header
) || !info
)
385 return D3DXERR_INVALIDDATA
;
387 if (header
->pixel_format
.size
!= sizeof(header
->pixel_format
))
388 return D3DXERR_INVALIDDATA
;
390 info
->Width
= header
->width
;
391 info
->Height
= header
->height
;
393 info
->MipLevels
= (header
->flags
& DDS_MIPMAPCOUNT
) ? header
->miplevels
: 1;
395 info
->Format
= dds_pixel_format_to_d3dformat(&header
->pixel_format
);
396 if (info
->Format
== D3DFMT_UNKNOWN
)
397 return D3DXERR_INVALIDDATA
;
399 TRACE("Pixel format is %#x\n", info
->Format
);
401 if (header
->caps2
& DDS_CAPS2_VOLUME
)
403 info
->Depth
= header
->depth
;
404 info
->ResourceType
= D3DRTYPE_VOLUMETEXTURE
;
406 else if (header
->caps2
& DDS_CAPS2_CUBEMAP
)
410 for (face
= DDS_CAPS2_CUBEMAP_POSITIVEX
; face
<= DDS_CAPS2_CUBEMAP_NEGATIVEZ
; face
<<= 1)
412 if (header
->caps2
& face
)
415 info
->ResourceType
= D3DRTYPE_CUBETEXTURE
;
419 info
->ResourceType
= D3DRTYPE_TEXTURE
;
422 expected_length
= calculate_dds_file_size(info
->Format
, info
->Width
, info
->Height
, info
->Depth
,
423 info
->MipLevels
, faces
);
424 if (length
< expected_length
)
426 WARN("File is too short %u, expected at least %u bytes\n", length
, expected_length
);
427 return D3DXERR_INVALIDDATA
;
430 info
->ImageFileFormat
= D3DXIFF_DDS
;
434 static HRESULT
load_surface_from_dds(IDirect3DSurface9
*dst_surface
, const PALETTEENTRY
*dst_palette
,
435 const RECT
*dst_rect
, const void *src_data
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
,
436 const D3DXIMAGE_INFO
*src_info
)
440 const struct dds_header
*header
= src_data
;
441 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
443 if (src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
444 return D3DXERR_INVALIDDATA
;
446 if (FAILED(calculate_dds_surface_size(src_info
->Format
, src_info
->Width
, src_info
->Height
, &src_pitch
, &size
)))
449 return D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
, pixels
, src_info
->Format
,
450 src_pitch
, NULL
, src_rect
, filter
, color_key
);
453 static HRESULT
save_dds_surface_to_memory(ID3DXBuffer
**dst_buffer
, IDirect3DSurface9
*src_surface
, const RECT
*src_rect
)
456 UINT dst_pitch
, surface_size
, file_size
;
457 D3DSURFACE_DESC src_desc
;
458 D3DLOCKED_RECT locked_rect
;
460 struct dds_header
*header
;
462 struct volume volume
;
463 const struct pixel_format_desc
*pixel_format
;
467 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
471 hr
= IDirect3DSurface9_GetDesc(src_surface
, &src_desc
);
472 if (FAILED(hr
)) return hr
;
474 pixel_format
= get_format_info(src_desc
.Format
);
475 if (pixel_format
->type
== FORMAT_UNKNOWN
) return E_NOTIMPL
;
477 file_size
= calculate_dds_file_size(src_desc
.Format
, src_desc
.Width
, src_desc
.Height
, 1, 1, 1);
479 hr
= calculate_dds_surface_size(src_desc
.Format
, src_desc
.Width
, src_desc
.Height
, &dst_pitch
, &surface_size
);
480 if (FAILED(hr
)) return hr
;
482 hr
= D3DXCreateBuffer(file_size
, &buffer
);
483 if (FAILED(hr
)) return hr
;
485 header
= ID3DXBuffer_GetBufferPointer(buffer
);
486 pixels
= (BYTE
*)(header
+ 1);
488 memset(header
, 0, sizeof(*header
));
489 header
->signature
= MAKEFOURCC('D','D','S',' ');
490 header
->size
= sizeof(*header
);
491 header
->flags
= DDS_CAPS
| DDS_HEIGHT
| DDS_WIDTH
| DDS_PITCH
| DDS_PIXELFORMAT
| DDS_MIPMAPCOUNT
;
492 header
->height
= src_desc
.Height
;
493 header
->width
= src_desc
.Width
;
494 header
->pitch_or_linear_size
= dst_pitch
;
496 header
->miplevels
= 1;
497 header
->caps
= DDS_CAPS_TEXTURE
;
498 hr
= d3dformat_to_dds_pixel_format(&header
->pixel_format
, src_desc
.Format
);
501 ID3DXBuffer_Release(buffer
);
505 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, NULL
, D3DLOCK_READONLY
);
508 ID3DXBuffer_Release(buffer
);
512 volume
.width
= src_desc
.Width
;
513 volume
.height
= src_desc
.Height
;
515 copy_pixels(locked_rect
.pBits
, locked_rect
.Pitch
, 0, pixels
, dst_pitch
, 0,
516 &volume
, pixel_format
);
518 IDirect3DSurface9_UnlockRect(src_surface
);
520 *dst_buffer
= buffer
;
524 HRESULT
load_volume_from_dds(IDirect3DVolume9
*dst_volume
, const PALETTEENTRY
*dst_palette
,
525 const D3DBOX
*dst_box
, const void *src_data
, const D3DBOX
*src_box
, DWORD filter
, D3DCOLOR color_key
,
526 const D3DXIMAGE_INFO
*src_info
)
528 UINT row_pitch
, slice_pitch
;
529 const struct dds_header
*header
= src_data
;
530 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
532 if (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
)
533 return D3DXERR_INVALIDDATA
;
535 if (FAILED(calculate_dds_surface_size(src_info
->Format
, src_info
->Width
, src_info
->Height
, &row_pitch
, &slice_pitch
)))
538 return D3DXLoadVolumeFromMemory(dst_volume
, dst_palette
, dst_box
, pixels
, src_info
->Format
,
539 row_pitch
, slice_pitch
, NULL
, src_box
, filter
, color_key
);
542 HRESULT
load_texture_from_dds(IDirect3DTexture9
*texture
, const void *src_data
, const PALETTEENTRY
*palette
,
543 DWORD filter
, D3DCOLOR color_key
, const D3DXIMAGE_INFO
*src_info
, unsigned int skip_levels
,
544 unsigned int *loaded_miplevels
)
553 IDirect3DSurface9
*surface
;
554 const struct dds_header
*header
= src_data
;
555 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
557 /* Loading a cube texture as a simple texture is also supported
558 * (only first face texture is taken). Same with volume textures. */
559 if ((src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
560 && (src_info
->ResourceType
!= D3DRTYPE_CUBETEXTURE
)
561 && (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
))
563 WARN("Trying to load a %u resource as a 2D texture, returning failure.\n", src_info
->ResourceType
);
564 return D3DXERR_INVALIDDATA
;
567 width
= src_info
->Width
;
568 height
= src_info
->Height
;
569 mip_levels
= min(src_info
->MipLevels
, IDirect3DTexture9_GetLevelCount(texture
));
570 if (src_info
->ResourceType
== D3DRTYPE_VOLUMETEXTURE
)
572 for (mip_level
= 0; mip_level
< mip_levels
+ skip_levels
; ++mip_level
)
574 hr
= calculate_dds_surface_size(src_info
->Format
, width
, height
, &src_pitch
, &mip_level_size
);
575 if (FAILED(hr
)) return hr
;
577 if (mip_level
>= skip_levels
)
579 SetRect(&src_rect
, 0, 0, width
, height
);
581 IDirect3DTexture9_GetSurfaceLevel(texture
, mip_level
- skip_levels
, &surface
);
582 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
583 NULL
, &src_rect
, filter
, color_key
);
584 IDirect3DSurface9_Release(surface
);
589 pixels
+= mip_level_size
;
590 width
= max(1, width
/ 2);
591 height
= max(1, height
/ 2);
594 *loaded_miplevels
= mip_levels
- skip_levels
;
599 HRESULT
load_cube_texture_from_dds(IDirect3DCubeTexture9
*cube_texture
, const void *src_data
,
600 const PALETTEENTRY
*palette
, DWORD filter
, DWORD color_key
, const D3DXIMAGE_INFO
*src_info
)
610 IDirect3DSurface9
*surface
;
611 const struct dds_header
*header
= src_data
;
612 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
614 if (src_info
->ResourceType
!= D3DRTYPE_CUBETEXTURE
)
615 return D3DXERR_INVALIDDATA
;
617 if ((header
->caps2
& DDS_CAPS2_CUBEMAP_ALL_FACES
) != DDS_CAPS2_CUBEMAP_ALL_FACES
)
619 WARN("Only full cubemaps are supported\n");
620 return D3DXERR_INVALIDDATA
;
623 mip_levels
= min(src_info
->MipLevels
, IDirect3DCubeTexture9_GetLevelCount(cube_texture
));
624 for (face
= D3DCUBEMAP_FACE_POSITIVE_X
; face
<= D3DCUBEMAP_FACE_NEGATIVE_Z
; face
++)
626 size
= src_info
->Width
;
627 for (mip_level
= 0; mip_level
< src_info
->MipLevels
; mip_level
++)
629 hr
= calculate_dds_surface_size(src_info
->Format
, size
, size
, &src_pitch
, &mip_level_size
);
630 if (FAILED(hr
)) return hr
;
632 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
633 if (mip_level
< mip_levels
)
635 SetRect(&src_rect
, 0, 0, size
, size
);
637 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture
, face
, mip_level
, &surface
);
638 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
639 NULL
, &src_rect
, filter
, color_key
);
640 IDirect3DSurface9_Release(surface
);
641 if (FAILED(hr
)) return hr
;
644 pixels
+= mip_level_size
;
645 size
= max(1, size
/ 2);
652 HRESULT
load_volume_texture_from_dds(IDirect3DVolumeTexture9
*volume_texture
, const void *src_data
,
653 const PALETTEENTRY
*palette
, DWORD filter
, DWORD color_key
, const D3DXIMAGE_INFO
*src_info
)
658 UINT src_slice_pitch
;
661 UINT width
, height
, depth
;
662 IDirect3DVolume9
*volume
;
663 const struct dds_header
*header
= src_data
;
664 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
666 if (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
)
667 return D3DXERR_INVALIDDATA
;
669 width
= src_info
->Width
;
670 height
= src_info
->Height
;
671 depth
= src_info
->Depth
;
672 mip_levels
= min(src_info
->MipLevels
, IDirect3DVolumeTexture9_GetLevelCount(volume_texture
));
674 for (mip_level
= 0; mip_level
< mip_levels
; mip_level
++)
676 hr
= calculate_dds_surface_size(src_info
->Format
, width
, height
, &src_row_pitch
, &src_slice_pitch
);
677 if (FAILED(hr
)) return hr
;
679 hr
= IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, mip_level
, &volume
);
680 if (FAILED(hr
)) return hr
;
684 src_box
.Right
= width
;
685 src_box
.Bottom
= height
;
687 src_box
.Back
= depth
;
689 hr
= D3DXLoadVolumeFromMemory(volume
, palette
, NULL
, pixels
, src_info
->Format
,
690 src_row_pitch
, src_slice_pitch
, NULL
, &src_box
, filter
, color_key
);
692 IDirect3DVolume9_Release(volume
);
693 if (FAILED(hr
)) return hr
;
695 pixels
+= depth
* src_slice_pitch
;
696 width
= max(1, width
/ 2);
697 height
= max(1, height
/ 2);
698 depth
= max(1, depth
/ 2);
704 static BOOL
convert_dib_to_bmp(void **data
, UINT
*size
)
709 BITMAPFILEHEADER
*header
;
713 if ((*size
< 4) || (*size
< (header_size
= *(ULONG
*)*data
)))
716 if ((header_size
== sizeof(BITMAPINFOHEADER
)) ||
717 (header_size
== sizeof(BITMAPV4HEADER
)) ||
718 (header_size
== sizeof(BITMAPV5HEADER
)) ||
719 (header_size
== 64 /* sizeof(BITMAPCOREHEADER2) */))
721 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
722 BITMAPINFOHEADER
*info_header
= (BITMAPINFOHEADER
*)*data
;
723 count
= info_header
->biClrUsed
;
725 if (!count
&& info_header
->biBitCount
<= 8)
726 count
= 1 << info_header
->biBitCount
;
728 offset
= sizeof(BITMAPFILEHEADER
) + header_size
+ sizeof(RGBQUAD
) * count
;
730 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
731 if ((info_header
->biSize
== sizeof(BITMAPINFOHEADER
)) && (info_header
->biCompression
== BI_BITFIELDS
))
732 offset
+= 3 * sizeof(DWORD
);
734 else if (header_size
== sizeof(BITMAPCOREHEADER
))
736 BITMAPCOREHEADER
*core_header
= (BITMAPCOREHEADER
*)*data
;
738 if (core_header
->bcBitCount
<= 8)
739 count
= 1 << core_header
->bcBitCount
;
741 offset
= sizeof(BITMAPFILEHEADER
) + header_size
+ sizeof(RGBTRIPLE
) * count
;
748 TRACE("Converting DIB file to BMP\n");
750 new_size
= *size
+ sizeof(BITMAPFILEHEADER
);
751 new_data
= HeapAlloc(GetProcessHeap(), 0, new_size
);
752 CopyMemory(new_data
+ sizeof(BITMAPFILEHEADER
), *data
, *size
);
755 header
= (BITMAPFILEHEADER
*)new_data
;
756 header
->bfType
= 0x4d42; /* BM */
757 header
->bfSize
= new_size
;
758 header
->bfReserved1
= 0;
759 header
->bfReserved2
= 0;
760 header
->bfOffBits
= offset
;
762 /* Update input data */
769 /************************************************************
770 * D3DXGetImageInfoFromFileInMemory
772 * Fills a D3DXIMAGE_INFO structure with info about an image
775 * data [I] pointer to the image file data
776 * datasize [I] size of the passed data
777 * info [O] pointer to the destination structure
780 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
781 * if info is NULL and data and datasize are not NULL
782 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
783 * D3DERR_INVALIDCALL, if data is NULL or
787 * datasize may be bigger than the actual file size
790 HRESULT WINAPI
D3DXGetImageInfoFromFileInMemory(const void *data
, UINT datasize
, D3DXIMAGE_INFO
*info
)
792 IWICImagingFactory
*factory
;
793 IWICBitmapDecoder
*decoder
= NULL
;
799 TRACE("(%p, %d, %p)\n", data
, datasize
, info
);
801 if (!data
|| !datasize
)
802 return D3DERR_INVALIDCALL
;
807 if ((datasize
>= 4) && !strncmp(data
, "DDS ", 4)) {
808 TRACE("File type is DDS\n");
809 return get_image_info_from_dds(data
, datasize
, info
);
812 /* In case of DIB file, convert it to BMP */
813 dib
= convert_dib_to_bmp((void**)&data
, &datasize
);
815 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
817 hr
= CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
);
820 IWICImagingFactory_CreateStream(factory
, &stream
);
821 IWICStream_InitializeFromMemory(stream
, (BYTE
*)data
, datasize
);
822 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
823 IWICStream_Release(stream
);
824 IWICImagingFactory_Release(factory
);
828 if ((datasize
>= 2) && (!strncmp(data
, "P3", 2) || !strncmp(data
, "P6", 2)))
829 FIXME("File type PPM is not supported yet\n");
830 else if ((datasize
>= 10) && !strncmp(data
, "#?RADIANCE", 10))
831 FIXME("File type HDR is not supported yet\n");
832 else if ((datasize
>= 2) && (!strncmp(data
, "PF", 2) || !strncmp(data
, "Pf", 2)))
833 FIXME("File type PFM is not supported yet\n");
837 GUID container_format
;
840 hr
= IWICBitmapDecoder_GetContainerFormat(decoder
, &container_format
);
842 if (IsEqualGUID(&container_format
, &GUID_ContainerFormatBmp
)) {
844 TRACE("File type is DIB\n");
845 info
->ImageFileFormat
= D3DXIFF_DIB
;
847 TRACE("File type is BMP\n");
848 info
->ImageFileFormat
= D3DXIFF_BMP
;
850 } else if (IsEqualGUID(&container_format
, &GUID_ContainerFormatPng
)) {
851 TRACE("File type is PNG\n");
852 info
->ImageFileFormat
= D3DXIFF_PNG
;
853 } else if(IsEqualGUID(&container_format
, &GUID_ContainerFormatJpeg
)) {
854 TRACE("File type is JPG\n");
855 info
->ImageFileFormat
= D3DXIFF_JPG
;
856 } else if(IsEqualGUID(&container_format
, &GUID_WineContainerFormatTga
)) {
857 TRACE("File type is TGA\n");
858 info
->ImageFileFormat
= D3DXIFF_TGA
;
860 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format
));
861 hr
= D3DXERR_INVALIDDATA
;
866 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
867 if (SUCCEEDED(hr
) && !frame_count
)
868 hr
= D3DXERR_INVALIDDATA
;
871 IWICBitmapFrameDecode
*frame
= NULL
;
873 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
876 hr
= IWICBitmapFrameDecode_GetSize(frame
, &info
->Width
, &info
->Height
);
879 WICPixelFormatGUID pixel_format
;
881 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &pixel_format
);
883 info
->Format
= wic_guid_to_d3dformat(&pixel_format
);
884 if (info
->Format
== D3DFMT_UNKNOWN
) {
885 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format
));
886 hr
= D3DXERR_INVALIDDATA
;
892 IWICBitmapFrameDecode_Release(frame
);
896 info
->ResourceType
= D3DRTYPE_TEXTURE
;
901 IWICBitmapDecoder_Release(decoder
);
903 if (SUCCEEDED(initresult
))
907 HeapFree(GetProcessHeap(), 0, (void*)data
);
910 TRACE("Invalid or unsupported image file\n");
911 return D3DXERR_INVALIDDATA
;
917 /************************************************************
918 * D3DXGetImageInfoFromFile
921 * Success: D3D_OK, if we successfully load a valid image file or
922 * if we successfully load a file which is no valid image and info is NULL
923 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
924 * if file is not a valid image file and info is not NULL
925 * D3DERR_INVALIDCALL, if file is NULL
928 HRESULT WINAPI
D3DXGetImageInfoFromFileA(const char *file
, D3DXIMAGE_INFO
*info
)
934 TRACE("file %s, info %p.\n", debugstr_a(file
), info
);
936 if( !file
) return D3DERR_INVALIDCALL
;
938 strlength
= MultiByteToWideChar(CP_ACP
, 0, file
, -1, NULL
, 0);
939 widename
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*widename
));
940 MultiByteToWideChar(CP_ACP
, 0, file
, -1, widename
, strlength
);
942 hr
= D3DXGetImageInfoFromFileW(widename
, info
);
943 HeapFree(GetProcessHeap(), 0, widename
);
948 HRESULT WINAPI
D3DXGetImageInfoFromFileW(const WCHAR
*file
, D3DXIMAGE_INFO
*info
)
954 TRACE("file %s, info %p.\n", debugstr_w(file
), info
);
957 return D3DERR_INVALIDCALL
;
959 if (FAILED(map_view_of_file(file
, &buffer
, &size
)))
960 return D3DXERR_INVALIDDATA
;
962 hr
= D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
963 UnmapViewOfFile(buffer
);
968 /************************************************************
969 * D3DXGetImageInfoFromResource
972 * Success: D3D_OK, if resource is a valid image file
973 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
974 * if we fail to load resource
977 HRESULT WINAPI
D3DXGetImageInfoFromResourceA(HMODULE module
, const char *resource
, D3DXIMAGE_INFO
*info
)
983 TRACE("module %p, resource %s, info %p.\n", module
, debugstr_a(resource
), info
);
985 if (!(resinfo
= FindResourceA(module
, resource
, (const char *)RT_RCDATA
))
986 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
987 && !(resinfo
= FindResourceA(module
, resource
, (const char *)RT_BITMAP
)))
988 return D3DXERR_INVALIDDATA
;
990 if (FAILED(load_resource_into_memory(module
, resinfo
, &buffer
, &size
)))
991 return D3DXERR_INVALIDDATA
;
993 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
996 HRESULT WINAPI
D3DXGetImageInfoFromResourceW(HMODULE module
, const WCHAR
*resource
, D3DXIMAGE_INFO
*info
)
1002 TRACE("module %p, resource %s, info %p.\n", module
, debugstr_w(resource
), info
);
1004 if (!(resinfo
= FindResourceW(module
, resource
, (const WCHAR
*)RT_RCDATA
))
1005 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1006 && !(resinfo
= FindResourceW(module
, resource
, (const WCHAR
*)RT_BITMAP
)))
1007 return D3DXERR_INVALIDDATA
;
1009 if (FAILED(load_resource_into_memory(module
, resinfo
, &buffer
, &size
)))
1010 return D3DXERR_INVALIDDATA
;
1012 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
1015 /************************************************************
1016 * D3DXLoadSurfaceFromFileInMemory
1018 * Loads data from a given buffer into a surface and fills a given
1019 * D3DXIMAGE_INFO structure with info about the source data.
1022 * pDestSurface [I] pointer to the surface
1023 * pDestPalette [I] palette to use
1024 * pDestRect [I] to be filled area of the surface
1025 * pSrcData [I] pointer to the source data
1026 * SrcDataSize [I] size of the source data in bytes
1027 * pSrcRect [I] area of the source data to load
1028 * dwFilter [I] filter to apply on stretching
1029 * Colorkey [I] colorkey
1030 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1034 * Failure: D3DERR_INVALIDCALL, if pDestSurface, pSrcData or SrcDataSize is NULL
1035 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1038 HRESULT WINAPI
D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9
*pDestSurface
,
1039 const PALETTEENTRY
*pDestPalette
, const RECT
*pDestRect
, const void *pSrcData
, UINT SrcDataSize
,
1040 const RECT
*pSrcRect
, DWORD dwFilter
, D3DCOLOR Colorkey
, D3DXIMAGE_INFO
*pSrcInfo
)
1042 D3DXIMAGE_INFO imginfo
;
1043 HRESULT hr
, com_init
;
1045 IWICImagingFactory
*factory
= NULL
;
1046 IWICBitmapDecoder
*decoder
;
1047 IWICBitmapFrameDecode
*bitmapframe
;
1050 const struct pixel_format_desc
*formatdesc
;
1054 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1055 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1056 pDestSurface
, pDestPalette
, wine_dbgstr_rect(pDestRect
), pSrcData
, SrcDataSize
,
1057 wine_dbgstr_rect(pSrcRect
), dwFilter
, Colorkey
, pSrcInfo
);
1059 if (!pDestSurface
|| !pSrcData
|| !SrcDataSize
)
1060 return D3DERR_INVALIDCALL
;
1062 hr
= D3DXGetImageInfoFromFileInMemory(pSrcData
, SrcDataSize
, &imginfo
);
1069 wicrect
.X
= pSrcRect
->left
;
1070 wicrect
.Y
= pSrcRect
->top
;
1071 wicrect
.Width
= pSrcRect
->right
- pSrcRect
->left
;
1072 wicrect
.Height
= pSrcRect
->bottom
- pSrcRect
->top
;
1078 wicrect
.Width
= imginfo
.Width
;
1079 wicrect
.Height
= imginfo
.Height
;
1082 SetRect(&rect
, 0, 0, wicrect
.Width
, wicrect
.Height
);
1084 if (imginfo
.ImageFileFormat
== D3DXIFF_DDS
)
1086 hr
= load_surface_from_dds(pDestSurface
, pDestPalette
, pDestRect
, pSrcData
, &rect
,
1087 dwFilter
, Colorkey
, &imginfo
);
1088 if (SUCCEEDED(hr
) && pSrcInfo
)
1089 *pSrcInfo
= imginfo
;
1093 if (imginfo
.ImageFileFormat
== D3DXIFF_DIB
)
1094 convert_dib_to_bmp((void**)&pSrcData
, &SrcDataSize
);
1096 com_init
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1098 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
)))
1101 if (FAILED(IWICImagingFactory_CreateStream(factory
, &stream
)))
1103 IWICImagingFactory_Release(factory
);
1108 IWICStream_InitializeFromMemory(stream
, (BYTE
*)pSrcData
, SrcDataSize
);
1110 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
1112 IWICStream_Release(stream
);
1117 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &bitmapframe
);
1122 formatdesc
= get_format_info(imginfo
.Format
);
1124 if (formatdesc
->type
== FORMAT_UNKNOWN
)
1126 FIXME("Unsupported pixel format\n");
1127 hr
= D3DXERR_INVALIDDATA
;
1133 PALETTEENTRY
*palette
= NULL
;
1134 WICColor
*colors
= NULL
;
1136 pitch
= formatdesc
->bytes_per_pixel
* wicrect
.Width
;
1137 buffer
= HeapAlloc(GetProcessHeap(), 0, pitch
* wicrect
.Height
);
1139 hr
= IWICBitmapFrameDecode_CopyPixels(bitmapframe
, &wicrect
, pitch
,
1140 pitch
* wicrect
.Height
, buffer
);
1142 if (SUCCEEDED(hr
) && (formatdesc
->type
== FORMAT_INDEX
))
1144 IWICPalette
*wic_palette
= NULL
;
1147 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
1149 hr
= IWICBitmapFrameDecode_CopyPalette(bitmapframe
, wic_palette
);
1151 hr
= IWICPalette_GetColorCount(wic_palette
, &nb_colors
);
1154 colors
= HeapAlloc(GetProcessHeap(), 0, nb_colors
* sizeof(colors
[0]));
1155 palette
= HeapAlloc(GetProcessHeap(), 0, nb_colors
* sizeof(palette
[0]));
1156 if (!colors
|| !palette
)
1160 hr
= IWICPalette_GetColors(wic_palette
, nb_colors
, colors
, &nb_colors
);
1165 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1166 for (i
= 0; i
< nb_colors
; i
++)
1168 palette
[i
].peRed
= (colors
[i
] >> 16) & 0xff;
1169 palette
[i
].peGreen
= (colors
[i
] >> 8) & 0xff;
1170 palette
[i
].peBlue
= colors
[i
] & 0xff;
1171 palette
[i
].peFlags
= (colors
[i
] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1175 IWICPalette_Release(wic_palette
);
1180 hr
= D3DXLoadSurfaceFromMemory(pDestSurface
, pDestPalette
, pDestRect
,
1181 buffer
, imginfo
.Format
, pitch
,
1182 palette
, &rect
, dwFilter
, Colorkey
);
1185 HeapFree(GetProcessHeap(), 0, colors
);
1186 HeapFree(GetProcessHeap(), 0, palette
);
1187 HeapFree(GetProcessHeap(), 0, buffer
);
1190 IWICBitmapFrameDecode_Release(bitmapframe
);
1193 IWICBitmapDecoder_Release(decoder
);
1197 IWICImagingFactory_Release(factory
);
1199 if (SUCCEEDED(com_init
))
1202 if (imginfo
.ImageFileFormat
== D3DXIFF_DIB
)
1203 HeapFree(GetProcessHeap(), 0, (void*)pSrcData
);
1206 return D3DXERR_INVALIDDATA
;
1209 *pSrcInfo
= imginfo
;
1214 HRESULT WINAPI
D3DXLoadSurfaceFromFileA(IDirect3DSurface9
*dst_surface
,
1215 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const char *src_file
,
1216 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
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_a(src_file
),
1225 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1227 if (!src_file
|| !dst_surface
)
1228 return D3DERR_INVALIDCALL
;
1230 strlength
= MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, NULL
, 0);
1231 src_file_w
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*src_file_w
));
1232 MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, src_file_w
, strlength
);
1234 hr
= D3DXLoadSurfaceFromFileW(dst_surface
, dst_palette
, dst_rect
,
1235 src_file_w
, src_rect
, filter
, color_key
, src_info
);
1236 HeapFree(GetProcessHeap(), 0, src_file_w
);
1241 HRESULT WINAPI
D3DXLoadSurfaceFromFileW(IDirect3DSurface9
*dst_surface
,
1242 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const WCHAR
*src_file
,
1243 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1249 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1250 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1251 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), debugstr_w(src_file
),
1252 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1254 if (!src_file
|| !dst_surface
)
1255 return D3DERR_INVALIDCALL
;
1257 if (FAILED(map_view_of_file(src_file
, &data
, &data_size
)))
1258 return D3DXERR_INVALIDDATA
;
1260 hr
= D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1261 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1262 UnmapViewOfFile(data
);
1267 HRESULT WINAPI
D3DXLoadSurfaceFromResourceA(IDirect3DSurface9
*dst_surface
,
1268 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const char *resource
,
1269 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1275 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1276 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1277 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_a(resource
),
1278 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1281 return D3DERR_INVALIDCALL
;
1283 if (!(resinfo
= FindResourceA(src_module
, resource
, (const char *)RT_RCDATA
))
1284 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1285 && !(resinfo
= FindResourceA(src_module
, resource
, (const char *)RT_BITMAP
)))
1286 return D3DXERR_INVALIDDATA
;
1288 if (FAILED(load_resource_into_memory(src_module
, resinfo
, &data
, &data_size
)))
1289 return D3DXERR_INVALIDDATA
;
1291 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1292 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1295 HRESULT WINAPI
D3DXLoadSurfaceFromResourceW(IDirect3DSurface9
*dst_surface
,
1296 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const WCHAR
*resource
,
1297 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1303 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1304 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1305 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_w(resource
),
1306 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1309 return D3DERR_INVALIDCALL
;
1311 if (!(resinfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_RCDATA
))
1312 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1313 && !(resinfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_BITMAP
)))
1314 return D3DXERR_INVALIDDATA
;
1316 if (FAILED(load_resource_into_memory(src_module
, resinfo
, &data
, &data_size
)))
1317 return D3DXERR_INVALIDDATA
;
1319 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1320 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1324 /************************************************************
1325 * helper functions for D3DXLoadSurfaceFromMemory
1327 struct argb_conversion_info
1329 const struct pixel_format_desc
*srcformat
;
1330 const struct pixel_format_desc
*destformat
;
1331 DWORD srcshift
[4], destshift
[4];
1332 DWORD srcmask
[4], destmask
[4];
1333 BOOL process_channel
[4];
1337 static void init_argb_conversion_info(const struct pixel_format_desc
*srcformat
, const struct pixel_format_desc
*destformat
, struct argb_conversion_info
*info
)
1340 ZeroMemory(info
->process_channel
, 4 * sizeof(BOOL
));
1341 info
->channelmask
= 0;
1343 info
->srcformat
= srcformat
;
1344 info
->destformat
= destformat
;
1346 for(i
= 0;i
< 4;i
++) {
1347 /* srcshift is used to extract the _relevant_ components */
1348 info
->srcshift
[i
] = srcformat
->shift
[i
] + max( srcformat
->bits
[i
] - destformat
->bits
[i
], 0);
1350 /* destshift is used to move the components to the correct position */
1351 info
->destshift
[i
] = destformat
->shift
[i
] + max(destformat
->bits
[i
] - srcformat
->bits
[i
], 0);
1353 info
->srcmask
[i
] = ((1 << srcformat
->bits
[i
]) - 1) << srcformat
->shift
[i
];
1354 info
->destmask
[i
] = ((1 << destformat
->bits
[i
]) - 1) << destformat
->shift
[i
];
1356 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1357 if(destformat
->bits
[i
]) {
1358 if(srcformat
->bits
[i
]) info
->process_channel
[i
] = TRUE
;
1359 else info
->channelmask
|= info
->destmask
[i
];
1364 /************************************************************
1365 * get_relevant_argb_components
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.
1370 static void get_relevant_argb_components(const struct argb_conversion_info
*info
, const BYTE
*col
, DWORD
*out
)
1373 unsigned int component
, mask
;
1375 for (i
= 0; i
< 4; ++i
)
1377 if (!info
->process_channel
[i
])
1381 mask
= info
->srcmask
[i
];
1382 for (j
= 0; j
< 4 && mask
; ++j
)
1384 if (info
->srcshift
[i
] < j
* 8)
1385 component
|= (col
[j
] & mask
) << (j
* 8 - info
->srcshift
[i
]);
1387 component
|= (col
[j
] & mask
) >> (info
->srcshift
[i
] - j
* 8);
1394 /************************************************************
1397 * Recombines the output of get_relevant_argb_components and converts
1398 * it to the destination format.
1400 static DWORD
make_argb_color(const struct argb_conversion_info
*info
, const DWORD
*in
)
1405 for(i
= 0;i
< 4;i
++) {
1406 if(info
->process_channel
[i
]) {
1407 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1409 for(shift
= info
->destshift
[i
]; shift
> info
->destformat
->shift
[i
]; shift
-= info
->srcformat
->bits
[i
]) val
|= in
[i
] << shift
;
1410 val
|= (in
[i
] >> (info
->destformat
->shift
[i
] - shift
)) << info
->destformat
->shift
[i
];
1413 val
|= info
->channelmask
; /* new channels are set to their maximal value */
1417 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */
1418 static void format_to_vec4(const struct pixel_format_desc
*format
, const BYTE
*src
, struct vec4
*dst
)
1423 for (c
= 0; c
< 4; ++c
)
1425 static const unsigned int component_offsets
[4] = {3, 0, 1, 2};
1426 float *dst_component
= (float *)dst
+ component_offsets
[c
];
1428 if (format
->bits
[c
])
1430 mask
= ~0u >> (32 - format
->bits
[c
]);
1432 memcpy(&tmp
, src
+ format
->shift
[c
] / 8,
1433 min(sizeof(DWORD
), (format
->shift
[c
] % 8 + format
->bits
[c
] + 7) / 8));
1435 if (format
->type
== FORMAT_ARGBF16
)
1436 *dst_component
= float_16_to_32(tmp
);
1437 else if (format
->type
== FORMAT_ARGBF
)
1438 *dst_component
= *(float *)&tmp
;
1440 *dst_component
= (float)((tmp
>> format
->shift
[c
] % 8) & mask
) / mask
;
1443 *dst_component
= 1.0f
;
1447 /* It doesn't work for components bigger than 32 bits. */
1448 static void format_from_vec4(const struct pixel_format_desc
*format
, const struct vec4
*src
, BYTE
*dst
)
1453 memset(dst
, 0, format
->bytes_per_pixel
);
1455 for (c
= 0; c
< 4; ++c
)
1457 static const unsigned int component_offsets
[4] = {3, 0, 1, 2};
1458 const float src_component
= *((const float *)src
+ component_offsets
[c
]);
1460 if (!format
->bits
[c
])
1463 mask32
= ~0u >> (32 - format
->bits
[c
]);
1465 if (format
->type
== FORMAT_ARGBF16
)
1466 v
= float_32_to_16(src_component
);
1467 else if (format
->type
== FORMAT_ARGBF
)
1468 v
= *(DWORD
*)&src_component
;
1470 v
= (DWORD
)(src_component
* ((1 << format
->bits
[c
]) - 1) + 0.5f
);
1472 for (i
= format
->shift
[c
] / 8 * 8; i
< format
->shift
[c
] + format
->bits
[c
]; i
+= 8)
1476 if (format
->shift
[c
] > i
)
1478 mask
= mask32
<< (format
->shift
[c
] - i
);
1479 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1483 mask
= mask32
>> (i
- format
->shift
[c
]);
1484 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1491 /************************************************************
1494 * Copies the source buffer to the destination buffer.
1495 * Works for any pixel format.
1496 * The source and the destination must be block-aligned.
1498 void copy_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
,
1499 BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
, const struct volume
*size
,
1500 const struct pixel_format_desc
*format
)
1504 const BYTE
*src_addr
;
1505 UINT row_block_count
= (size
->width
+ format
->block_width
- 1) / format
->block_width
;
1506 UINT row_count
= (size
->height
+ format
->block_height
- 1) / format
->block_height
;
1508 for (slice
= 0; slice
< size
->depth
; slice
++)
1510 src_addr
= src
+ slice
* src_slice_pitch
;
1511 dst_addr
= dst
+ slice
* dst_slice_pitch
;
1513 for (row
= 0; row
< row_count
; row
++)
1515 memcpy(dst_addr
, src_addr
, row_block_count
* format
->block_byte_count
);
1516 src_addr
+= src_row_pitch
;
1517 dst_addr
+= dst_row_pitch
;
1522 /************************************************************
1523 * convert_argb_pixels
1525 * Copies the source buffer to the destination buffer, performing
1526 * any necessary format conversion and color keying.
1527 * Pixels outsize the source rect are blacked out.
1528 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1530 void convert_argb_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
, const struct volume
*src_size
,
1531 const struct pixel_format_desc
*src_format
, BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
,
1532 const struct volume
*dst_size
, const struct pixel_format_desc
*dst_format
, D3DCOLOR color_key
,
1533 const PALETTEENTRY
*palette
)
1535 struct argb_conversion_info conv_info
, ck_conv_info
;
1536 const struct pixel_format_desc
*ck_format
= NULL
;
1538 UINT min_width
, min_height
, min_depth
;
1541 ZeroMemory(channels
, sizeof(channels
));
1542 init_argb_conversion_info(src_format
, dst_format
, &conv_info
);
1544 min_width
= min(src_size
->width
, dst_size
->width
);
1545 min_height
= min(src_size
->height
, dst_size
->height
);
1546 min_depth
= min(src_size
->depth
, dst_size
->depth
);
1550 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1551 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1552 init_argb_conversion_info(src_format
, ck_format
, &ck_conv_info
);
1555 for (z
= 0; z
< min_depth
; z
++) {
1556 const BYTE
*src_slice_ptr
= src
+ z
* src_slice_pitch
;
1557 BYTE
*dst_slice_ptr
= dst
+ z
* dst_slice_pitch
;
1559 for (y
= 0; y
< min_height
; y
++) {
1560 const BYTE
*src_ptr
= src_slice_ptr
+ y
* src_row_pitch
;
1561 BYTE
*dst_ptr
= dst_slice_ptr
+ y
* dst_row_pitch
;
1563 for (x
= 0; x
< min_width
; x
++) {
1564 if (!src_format
->to_rgba
&& !dst_format
->from_rgba
1565 && src_format
->bytes_per_pixel
<= 4 && dst_format
->bytes_per_pixel
<= 4)
1569 get_relevant_argb_components(&conv_info
, src_ptr
, channels
);
1570 val
= make_argb_color(&conv_info
, channels
);
1576 get_relevant_argb_components(&ck_conv_info
, src_ptr
, channels
);
1577 ck_pixel
= make_argb_color(&ck_conv_info
, channels
);
1578 if (ck_pixel
== color_key
)
1579 val
&= ~conv_info
.destmask
[0];
1581 memcpy(dst_ptr
, &val
, dst_format
->bytes_per_pixel
);
1585 struct vec4 color
, tmp
;
1587 format_to_vec4(src_format
, src_ptr
, &color
);
1588 if (src_format
->to_rgba
)
1589 src_format
->to_rgba(&color
, &tmp
, palette
);
1597 format_from_vec4(ck_format
, &tmp
, (BYTE
*)&ck_pixel
);
1598 if (ck_pixel
== color_key
)
1602 if (dst_format
->from_rgba
)
1603 dst_format
->from_rgba(&tmp
, &color
);
1607 format_from_vec4(dst_format
, &color
, dst_ptr
);
1610 src_ptr
+= src_format
->bytes_per_pixel
;
1611 dst_ptr
+= dst_format
->bytes_per_pixel
;
1614 if (src_size
->width
< dst_size
->width
) /* black out remaining pixels */
1615 memset(dst_ptr
, 0, dst_format
->bytes_per_pixel
* (dst_size
->width
- src_size
->width
));
1618 if (src_size
->height
< dst_size
->height
) /* black out remaining pixels */
1619 memset(dst
+ src_size
->height
* dst_row_pitch
, 0, dst_row_pitch
* (dst_size
->height
- src_size
->height
));
1621 if (src_size
->depth
< dst_size
->depth
) /* black out remaining pixels */
1622 memset(dst
+ src_size
->depth
* dst_slice_pitch
, 0, dst_slice_pitch
* (dst_size
->depth
- src_size
->depth
));
1625 /************************************************************
1626 * point_filter_argb_pixels
1628 * Copies the source buffer to the destination buffer, performing
1629 * any necessary format conversion, color keying and stretching
1630 * using a point filter.
1631 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1633 void point_filter_argb_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
, const struct volume
*src_size
,
1634 const struct pixel_format_desc
*src_format
, BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
,
1635 const struct volume
*dst_size
, const struct pixel_format_desc
*dst_format
, D3DCOLOR color_key
,
1636 const PALETTEENTRY
*palette
)
1638 struct argb_conversion_info conv_info
, ck_conv_info
;
1639 const struct pixel_format_desc
*ck_format
= NULL
;
1643 ZeroMemory(channels
, sizeof(channels
));
1644 init_argb_conversion_info(src_format
, dst_format
, &conv_info
);
1648 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1649 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1650 init_argb_conversion_info(src_format
, ck_format
, &ck_conv_info
);
1653 for (z
= 0; z
< dst_size
->depth
; z
++)
1655 BYTE
*dst_slice_ptr
= dst
+ z
* dst_slice_pitch
;
1656 const BYTE
*src_slice_ptr
= src
+ src_slice_pitch
* (z
* src_size
->depth
/ dst_size
->depth
);
1658 for (y
= 0; y
< dst_size
->height
; y
++)
1660 BYTE
*dst_ptr
= dst_slice_ptr
+ y
* dst_row_pitch
;
1661 const BYTE
*src_row_ptr
= src_slice_ptr
+ src_row_pitch
* (y
* src_size
->height
/ dst_size
->height
);
1663 for (x
= 0; x
< dst_size
->width
; x
++)
1665 const BYTE
*src_ptr
= src_row_ptr
+ (x
* src_size
->width
/ dst_size
->width
) * src_format
->bytes_per_pixel
;
1667 if (!src_format
->to_rgba
&& !dst_format
->from_rgba
1668 && src_format
->bytes_per_pixel
<= 4 && dst_format
->bytes_per_pixel
<= 4)
1672 get_relevant_argb_components(&conv_info
, src_ptr
, channels
);
1673 val
= make_argb_color(&conv_info
, channels
);
1679 get_relevant_argb_components(&ck_conv_info
, src_ptr
, channels
);
1680 ck_pixel
= make_argb_color(&ck_conv_info
, channels
);
1681 if (ck_pixel
== color_key
)
1682 val
&= ~conv_info
.destmask
[0];
1684 memcpy(dst_ptr
, &val
, dst_format
->bytes_per_pixel
);
1688 struct vec4 color
, tmp
;
1690 format_to_vec4(src_format
, src_ptr
, &color
);
1691 if (src_format
->to_rgba
)
1692 src_format
->to_rgba(&color
, &tmp
, palette
);
1700 format_from_vec4(ck_format
, &tmp
, (BYTE
*)&ck_pixel
);
1701 if (ck_pixel
== color_key
)
1705 if (dst_format
->from_rgba
)
1706 dst_format
->from_rgba(&tmp
, &color
);
1710 format_from_vec4(dst_format
, &color
, dst_ptr
);
1713 dst_ptr
+= dst_format
->bytes_per_pixel
;
1719 /************************************************************
1720 * D3DXLoadSurfaceFromMemory
1722 * Loads data from a given memory chunk into a surface,
1723 * applying any of the specified filters.
1726 * pDestSurface [I] pointer to the surface
1727 * pDestPalette [I] palette to use
1728 * pDestRect [I] to be filled area of the surface
1729 * pSrcMemory [I] pointer to the source data
1730 * SrcFormat [I] format of the source pixel data
1731 * SrcPitch [I] number of bytes in a row
1732 * pSrcPalette [I] palette used in the source image
1733 * pSrcRect [I] area of the source data to load
1734 * dwFilter [I] filter to apply on stretching
1735 * Colorkey [I] colorkey
1738 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1739 * if pSrcMemory is NULL but the other parameters are valid
1740 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect is NULL or
1741 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1742 * if DestRect is invalid
1743 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1744 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1747 * pSrcRect specifies the dimensions of the source data;
1748 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1751 HRESULT WINAPI
D3DXLoadSurfaceFromMemory(IDirect3DSurface9
*dst_surface
,
1752 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const void *src_memory
,
1753 D3DFORMAT src_format
, UINT src_pitch
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
,
1754 DWORD filter
, D3DCOLOR color_key
)
1756 const struct pixel_format_desc
*srcformatdesc
, *destformatdesc
;
1757 D3DSURFACE_DESC surfdesc
;
1758 D3DLOCKED_RECT lockrect
;
1759 struct volume src_size
, dst_size
;
1761 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1762 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_memory
, src_format
,
1763 src_pitch
, src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1765 if (!dst_surface
|| !src_memory
|| !src_rect
)
1767 WARN("Invalid argument specified.\n");
1768 return D3DERR_INVALIDCALL
;
1770 if (src_format
== D3DFMT_UNKNOWN
1771 || src_rect
->left
>= src_rect
->right
1772 || src_rect
->top
>= src_rect
->bottom
)
1774 WARN("Invalid src_format or src_rect.\n");
1778 if (filter
== D3DX_DEFAULT
)
1779 filter
= D3DX_FILTER_TRIANGLE
| D3DX_FILTER_DITHER
;
1781 IDirect3DSurface9_GetDesc(dst_surface
, &surfdesc
);
1783 src_size
.width
= src_rect
->right
- src_rect
->left
;
1784 src_size
.height
= src_rect
->bottom
- src_rect
->top
;
1788 dst_size
.width
= surfdesc
.Width
;
1789 dst_size
.height
= surfdesc
.Height
;
1793 if (dst_rect
->left
> dst_rect
->right
|| dst_rect
->right
> surfdesc
.Width
1794 || dst_rect
->top
> dst_rect
->bottom
|| dst_rect
->bottom
> surfdesc
.Height
1795 || dst_rect
->left
< 0 || dst_rect
->top
< 0)
1797 WARN("Invalid dst_rect specified.\n");
1798 return D3DERR_INVALIDCALL
;
1800 dst_size
.width
= dst_rect
->right
- dst_rect
->left
;
1801 dst_size
.height
= dst_rect
->bottom
- dst_rect
->top
;
1802 if (!dst_size
.width
|| !dst_size
.height
)
1807 srcformatdesc
= get_format_info(src_format
);
1808 destformatdesc
= get_format_info(surfdesc
.Format
);
1809 if (srcformatdesc
->type
== FORMAT_UNKNOWN
|| destformatdesc
->type
== FORMAT_UNKNOWN
)
1811 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format
, surfdesc
.Format
);
1815 if (src_format
== surfdesc
.Format
1816 && dst_size
.width
== src_size
.width
1817 && dst_size
.height
== src_size
.height
1818 && color_key
== 0) /* Simple copy. */
1820 if (src_rect
->left
& (srcformatdesc
->block_width
- 1)
1821 || src_rect
->top
& (srcformatdesc
->block_height
- 1)
1822 || (src_rect
->right
& (srcformatdesc
->block_width
- 1)
1823 && src_size
.width
!= surfdesc
.Width
)
1824 || (src_rect
->bottom
& (srcformatdesc
->block_height
- 1)
1825 && src_size
.height
!= surfdesc
.Height
))
1827 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect
));
1828 return D3DXERR_INVALIDDATA
;
1831 if (FAILED(IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1832 return D3DXERR_INVALIDDATA
;
1834 copy_pixels(src_memory
, src_pitch
, 0, lockrect
.pBits
, lockrect
.Pitch
, 0,
1835 &src_size
, srcformatdesc
);
1837 IDirect3DSurface9_UnlockRect(dst_surface
);
1839 else /* Stretching or format conversion. */
1841 if (((srcformatdesc
->type
!= FORMAT_ARGB
) && (srcformatdesc
->type
!= FORMAT_INDEX
)) ||
1842 (destformatdesc
->type
!= FORMAT_ARGB
))
1844 FIXME("Format conversion missing %#x -> %#x\n", src_format
, surfdesc
.Format
);
1848 if (FAILED(IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1849 return D3DXERR_INVALIDDATA
;
1851 if ((filter
& 0xf) == D3DX_FILTER_NONE
)
1853 convert_argb_pixels(src_memory
, src_pitch
, 0, &src_size
, srcformatdesc
,
1854 lockrect
.pBits
, lockrect
.Pitch
, 0, &dst_size
, destformatdesc
, color_key
, src_palette
);
1856 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1858 if ((filter
& 0xf) != D3DX_FILTER_POINT
)
1859 FIXME("Unhandled filter %#x.\n", filter
);
1861 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1862 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1863 point_filter_argb_pixels(src_memory
, src_pitch
, 0, &src_size
, srcformatdesc
,
1864 lockrect
.pBits
, lockrect
.Pitch
, 0, &dst_size
, destformatdesc
, color_key
, src_palette
);
1867 IDirect3DSurface9_UnlockRect(dst_surface
);
1873 /************************************************************
1874 * D3DXLoadSurfaceFromSurface
1876 * Copies the contents from one surface to another, performing any required
1877 * format conversion, resizing or filtering.
1880 * pDestSurface [I] pointer to the destination surface
1881 * pDestPalette [I] palette to use
1882 * pDestRect [I] to be filled area of the surface
1883 * pSrcSurface [I] pointer to the source surface
1884 * pSrcPalette [I] palette used for the source surface
1885 * pSrcRect [I] area of the source data to load
1886 * dwFilter [I] filter to apply on resizing
1887 * Colorkey [I] any ARGB value or 0 to disable color-keying
1891 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface is NULL
1892 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1895 HRESULT WINAPI
D3DXLoadSurfaceFromSurface(IDirect3DSurface9
*dst_surface
,
1896 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, IDirect3DSurface9
*src_surface
,
1897 const PALETTEENTRY
*src_palette
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
)
1900 D3DLOCKED_RECT lock
;
1901 D3DSURFACE_DESC SrcDesc
;
1904 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1905 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1906 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_surface
,
1907 src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1909 if (!dst_surface
|| !src_surface
)
1910 return D3DERR_INVALIDCALL
;
1912 IDirect3DSurface9_GetDesc(src_surface
, &SrcDesc
);
1915 SetRect(&rect
, 0, 0, SrcDesc
.Width
, SrcDesc
.Height
);
1919 if (FAILED(IDirect3DSurface9_LockRect(src_surface
, &lock
, NULL
, D3DLOCK_READONLY
)))
1920 return D3DXERR_INVALIDDATA
;
1922 hr
= D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
,
1923 lock
.pBits
, SrcDesc
.Format
, lock
.Pitch
, src_palette
, &rect
, filter
, color_key
);
1925 IDirect3DSurface9_UnlockRect(src_surface
);
1931 HRESULT WINAPI
D3DXSaveSurfaceToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1932 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1937 ID3DXBuffer
*buffer
;
1939 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1940 wine_dbgstr_a(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
1942 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1944 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1945 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1946 if (!filename
) return E_OUTOFMEMORY
;
1947 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1949 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
1952 hr
= write_buffer_to_file(filename
, buffer
);
1953 ID3DXBuffer_Release(buffer
);
1956 HeapFree(GetProcessHeap(), 0, filename
);
1960 HRESULT WINAPI
D3DXSaveSurfaceToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1961 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1964 ID3DXBuffer
*buffer
;
1966 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1967 wine_dbgstr_w(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
1969 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1971 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
1974 hr
= write_buffer_to_file(dst_filename
, buffer
);
1975 ID3DXBuffer_Release(buffer
);
1981 HRESULT WINAPI
D3DXSaveSurfaceToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1982 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1984 IWICBitmapEncoder
*encoder
= NULL
;
1985 IWICBitmapFrameEncode
*frame
= NULL
;
1986 IPropertyBag2
*encoder_options
= NULL
;
1987 IStream
*stream
= NULL
;
1990 const CLSID
*encoder_clsid
;
1991 const GUID
*pixel_format_guid
;
1992 WICPixelFormatGUID wic_pixel_format
;
1993 D3DFORMAT d3d_pixel_format
;
1994 D3DSURFACE_DESC src_surface_desc
;
1995 D3DLOCKED_RECT locked_rect
;
1997 STATSTG stream_stats
;
1998 HGLOBAL stream_hglobal
;
1999 ID3DXBuffer
*buffer
;
2002 TRACE("(%p, %#x, %p, %p, %s)\n",
2003 dst_buffer
, file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
2005 if (!dst_buffer
|| !src_surface
) return D3DERR_INVALIDCALL
;
2009 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
2010 return D3DERR_INVALIDCALL
;
2013 switch (file_format
)
2017 encoder_clsid
= &CLSID_WICBmpEncoder
;
2020 encoder_clsid
= &CLSID_WICPngEncoder
;
2023 encoder_clsid
= &CLSID_WICJpegEncoder
;
2026 return save_dds_surface_to_memory(dst_buffer
, src_surface
, src_rect
);
2031 FIXME("File format %#x is not supported yet\n", file_format
);
2034 return D3DERR_INVALIDCALL
;
2037 IDirect3DSurface9_GetDesc(src_surface
, &src_surface_desc
);
2040 if (src_rect
->left
== src_rect
->right
|| src_rect
->top
== src_rect
->bottom
)
2042 WARN("Invalid rectangle with 0 area\n");
2043 return D3DXCreateBuffer(64, dst_buffer
);
2045 if (src_rect
->left
< 0 || src_rect
->top
< 0)
2046 return D3DERR_INVALIDCALL
;
2047 if (src_rect
->left
> src_rect
->right
|| src_rect
->top
> src_rect
->bottom
)
2048 return D3DERR_INVALIDCALL
;
2049 if (src_rect
->right
> src_surface_desc
.Width
|| src_rect
->bottom
> src_surface_desc
.Height
)
2050 return D3DERR_INVALIDCALL
;
2052 width
= src_rect
->right
- src_rect
->left
;
2053 height
= src_rect
->bottom
- src_rect
->top
;
2057 width
= src_surface_desc
.Width
;
2058 height
= src_surface_desc
.Height
;
2061 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
2063 hr
= CoCreateInstance(encoder_clsid
, NULL
, CLSCTX_INPROC_SERVER
,
2064 &IID_IWICBitmapEncoder
, (void **)&encoder
);
2065 if (FAILED(hr
)) goto cleanup_err
;
2067 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &stream
);
2068 if (FAILED(hr
)) goto cleanup_err
;
2070 hr
= IWICBitmapEncoder_Initialize(encoder
, stream
, WICBitmapEncoderNoCache
);
2071 if (FAILED(hr
)) goto cleanup_err
;
2073 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frame
, &encoder_options
);
2074 if (FAILED(hr
)) goto cleanup_err
;
2076 hr
= IWICBitmapFrameEncode_Initialize(frame
, encoder_options
);
2077 if (FAILED(hr
)) goto cleanup_err
;
2079 hr
= IWICBitmapFrameEncode_SetSize(frame
, width
, height
);
2080 if (FAILED(hr
)) goto cleanup_err
;
2082 pixel_format_guid
= d3dformat_to_wic_guid(src_surface_desc
.Format
);
2083 if (!pixel_format_guid
)
2085 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc
.Format
);
2090 memcpy(&wic_pixel_format
, pixel_format_guid
, sizeof(GUID
));
2091 hr
= IWICBitmapFrameEncode_SetPixelFormat(frame
, &wic_pixel_format
);
2092 d3d_pixel_format
= wic_guid_to_d3dformat(&wic_pixel_format
);
2093 if (SUCCEEDED(hr
) && d3d_pixel_format
!= D3DFMT_UNKNOWN
)
2095 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format
), d3d_pixel_format
);
2097 if (src_surface_desc
.Format
== d3d_pixel_format
) /* Simple copy */
2099 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
2102 IWICBitmapFrameEncode_WritePixels(frame
, height
,
2103 locked_rect
.Pitch
, height
* locked_rect
.Pitch
, locked_rect
.pBits
);
2104 IDirect3DSurface9_UnlockRect(src_surface
);
2107 else /* Pixel format conversion */
2109 const struct pixel_format_desc
*src_format_desc
, *dst_format_desc
;
2114 src_format_desc
= get_format_info(src_surface_desc
.Format
);
2115 dst_format_desc
= get_format_info(d3d_pixel_format
);
2116 if (src_format_desc
->type
!= FORMAT_ARGB
|| dst_format_desc
->type
!= FORMAT_ARGB
)
2118 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
2119 src_surface_desc
.Format
, d3d_pixel_format
);
2125 size
.height
= height
;
2127 dst_pitch
= width
* dst_format_desc
->bytes_per_pixel
;
2128 dst_data
= HeapAlloc(GetProcessHeap(), 0, dst_pitch
* height
);
2135 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
2138 convert_argb_pixels(locked_rect
.pBits
, locked_rect
.Pitch
, 0, &size
, src_format_desc
,
2139 dst_data
, dst_pitch
, 0, &size
, dst_format_desc
, 0, NULL
);
2140 IDirect3DSurface9_UnlockRect(src_surface
);
2143 IWICBitmapFrameEncode_WritePixels(frame
, height
, dst_pitch
, dst_pitch
* height
, dst_data
);
2144 HeapFree(GetProcessHeap(), 0, dst_data
);
2147 hr
= IWICBitmapFrameEncode_Commit(frame
);
2148 if (SUCCEEDED(hr
)) hr
= IWICBitmapEncoder_Commit(encoder
);
2150 else WARN("Unsupported pixel format %#x\n", src_surface_desc
.Format
);
2152 /* copy data from stream to ID3DXBuffer */
2153 hr
= IStream_Stat(stream
, &stream_stats
, STATFLAG_NONAME
);
2154 if (FAILED(hr
)) goto cleanup_err
;
2156 if (stream_stats
.cbSize
.u
.HighPart
!= 0)
2158 hr
= D3DXERR_INVALIDDATA
;
2161 size
= stream_stats
.cbSize
.u
.LowPart
;
2163 /* Remove BMP header for DIB */
2164 if (file_format
== D3DXIFF_DIB
)
2165 size
-= sizeof(BITMAPFILEHEADER
);
2167 hr
= D3DXCreateBuffer(size
, &buffer
);
2168 if (FAILED(hr
)) goto cleanup
;
2170 hr
= GetHGlobalFromStream(stream
, &stream_hglobal
);
2173 void *buffer_pointer
= ID3DXBuffer_GetBufferPointer(buffer
);
2174 void *stream_data
= GlobalLock(stream_hglobal
);
2175 /* Remove BMP header for DIB */
2176 if (file_format
== D3DXIFF_DIB
)
2177 stream_data
= (void*)((BYTE
*)stream_data
+ sizeof(BITMAPFILEHEADER
));
2178 memcpy(buffer_pointer
, stream_data
, size
);
2179 GlobalUnlock(stream_hglobal
);
2180 *dst_buffer
= buffer
;
2182 else ID3DXBuffer_Release(buffer
);
2185 if (FAILED(hr
) && hr
!= E_OUTOFMEMORY
)
2186 hr
= D3DERR_INVALIDCALL
;
2189 if (stream
) IStream_Release(stream
);
2191 if (frame
) IWICBitmapFrameEncode_Release(frame
);
2192 if (encoder_options
) IPropertyBag2_Release(encoder_options
);
2194 if (encoder
) IWICBitmapEncoder_Release(encoder
);
2196 if (SUCCEEDED(initresult
)) CoUninitialize();