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 "d3dx9_36_private.h"
24 #include <wine/wined3d.h>
29 /* Wine-specific WIC GUIDs */
30 DEFINE_GUID(GUID_WineContainerFormatTga
, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
36 } wic_pixel_formats
[] = {
37 { &GUID_WICPixelFormat8bppIndexed
, D3DFMT_P8
},
38 { &GUID_WICPixelFormat1bppIndexed
, D3DFMT_P8
},
39 { &GUID_WICPixelFormat4bppIndexed
, D3DFMT_P8
},
40 { &GUID_WICPixelFormat8bppGray
, D3DFMT_L8
},
41 { &GUID_WICPixelFormat16bppBGR555
, D3DFMT_X1R5G5B5
},
42 { &GUID_WICPixelFormat16bppBGR565
, D3DFMT_R5G6B5
},
43 { &GUID_WICPixelFormat24bppBGR
, D3DFMT_R8G8B8
},
44 { &GUID_WICPixelFormat32bppBGR
, D3DFMT_X8R8G8B8
},
45 { &GUID_WICPixelFormat32bppBGRA
, D3DFMT_A8R8G8B8
}
48 static D3DFORMAT
wic_guid_to_d3dformat(const GUID
*guid
)
52 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
54 if (IsEqualGUID(wic_pixel_formats
[i
].wic_guid
, guid
))
55 return wic_pixel_formats
[i
].d3dformat
;
58 return D3DFMT_UNKNOWN
;
61 static const GUID
*d3dformat_to_wic_guid(D3DFORMAT format
)
65 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
67 if (wic_pixel_formats
[i
].d3dformat
== format
)
68 return wic_pixel_formats
[i
].wic_guid
;
74 /* dds_header.flags */
76 #define DDS_HEIGHT 0x2
79 #define DDS_PIXELFORMAT 0x1000
80 #define DDS_MIPMAPCOUNT 0x20000
81 #define DDS_LINEARSIZE 0x80000
82 #define DDS_DEPTH 0x800000
85 #define DDS_CAPS_COMPLEX 0x8
86 #define DDS_CAPS_TEXTURE 0x1000
87 #define DDS_CAPS_MIPMAP 0x400000
89 /* dds_header.caps2 */
90 #define DDS_CAPS2_CUBEMAP 0x200
91 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
92 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
93 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
94 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
95 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
96 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
97 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
98 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
99 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
100 #define DDS_CAPS2_VOLUME 0x200000
102 /* dds_pixel_format.flags */
103 #define DDS_PF_ALPHA 0x1
104 #define DDS_PF_ALPHA_ONLY 0x2
105 #define DDS_PF_FOURCC 0x4
106 #define DDS_PF_RGB 0x40
107 #define DDS_PF_YUV 0x200
108 #define DDS_PF_LUMINANCE 0x20000
109 #define DDS_PF_BUMPLUMINANCE 0x40000
110 #define DDS_PF_BUMPDUDV 0x80000
112 struct dds_pixel_format
131 DWORD pitch_or_linear_size
;
135 struct dds_pixel_format pixel_format
;
143 static D3DFORMAT
dds_fourcc_to_d3dformat(DWORD fourcc
)
146 static const DWORD known_fourcc
[] = {
158 D3DFMT_A16B16G16R16F
,
161 D3DFMT_A32B32G32R32F
,
164 for (i
= 0; i
< sizeof(known_fourcc
) / sizeof(known_fourcc
[0]); i
++)
166 if (known_fourcc
[i
] == fourcc
)
170 WARN("Unknown FourCC %#x\n", fourcc
);
171 return D3DFMT_UNKNOWN
;
174 static const struct {
181 } rgb_pixel_formats
[] = {
182 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2
},
183 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5
},
184 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5
},
185 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5
},
186 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4
},
187 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4
},
188 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2
},
189 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8
},
190 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8
},
191 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8
},
192 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10
},
193 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10
},
194 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16
},
195 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8
},
196 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8
},
199 static D3DFORMAT
dds_rgb_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
203 for (i
= 0; i
< sizeof(rgb_pixel_formats
) / sizeof(rgb_pixel_formats
[0]); i
++)
205 if (rgb_pixel_formats
[i
].bpp
== pixel_format
->bpp
206 && rgb_pixel_formats
[i
].rmask
== pixel_format
->rmask
207 && rgb_pixel_formats
[i
].gmask
== pixel_format
->gmask
208 && rgb_pixel_formats
[i
].bmask
== pixel_format
->bmask
)
210 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && rgb_pixel_formats
[i
].amask
== pixel_format
->amask
)
211 return rgb_pixel_formats
[i
].format
;
212 if (rgb_pixel_formats
[i
].amask
== 0)
213 return rgb_pixel_formats
[i
].format
;
217 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
218 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
219 return D3DFMT_UNKNOWN
;
222 static D3DFORMAT
dds_luminance_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
224 if (pixel_format
->bpp
== 8)
226 if (pixel_format
->rmask
== 0xff)
228 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x0f && pixel_format
->amask
== 0xf0)
231 if (pixel_format
->bpp
== 16)
233 if (pixel_format
->rmask
== 0xffff)
235 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x00ff && pixel_format
->amask
== 0xff00)
239 WARN("Unknown luminance pixel format (bpp %u, l %#x, a %#x)\n",
240 pixel_format
->bpp
, pixel_format
->rmask
, pixel_format
->amask
);
241 return D3DFMT_UNKNOWN
;
244 static D3DFORMAT
dds_alpha_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
246 if (pixel_format
->bpp
== 8 && pixel_format
->amask
== 0xff)
249 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format
->bpp
, pixel_format
->rmask
);
250 return D3DFMT_UNKNOWN
;
253 static D3DFORMAT
dds_bump_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
255 if (pixel_format
->bpp
== 16 && pixel_format
->rmask
== 0x00ff && pixel_format
->gmask
== 0xff00)
257 if (pixel_format
->bpp
== 32 && pixel_format
->rmask
== 0x0000ffff && pixel_format
->gmask
== 0xffff0000)
258 return D3DFMT_V16U16
;
260 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format
->bpp
,
261 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
262 return D3DFMT_UNKNOWN
;
265 static D3DFORMAT
dds_bump_luminance_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
267 if (pixel_format
->bpp
== 32 && pixel_format
->rmask
== 0x000000ff && pixel_format
->gmask
== 0x0000ff00
268 && pixel_format
->bmask
== 0x00ff0000)
269 return D3DFMT_X8L8V8U8
;
271 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format
->bpp
,
272 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
273 return D3DFMT_UNKNOWN
;
276 static D3DFORMAT
dds_pixel_format_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
278 TRACE("pixel_format: size %u, flags %#x, fourcc %#x, bpp %u.\n", pixel_format
->size
,
279 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
);
280 TRACE("rmask %#x, gmask %#x, bmask %#x, amask %#x.\n", pixel_format
->rmask
, pixel_format
->gmask
,
281 pixel_format
->bmask
, pixel_format
->amask
);
283 if (pixel_format
->flags
& DDS_PF_FOURCC
)
284 return dds_fourcc_to_d3dformat(pixel_format
->fourcc
);
285 if (pixel_format
->flags
& DDS_PF_RGB
)
286 return dds_rgb_to_d3dformat(pixel_format
);
287 if (pixel_format
->flags
& DDS_PF_LUMINANCE
)
288 return dds_luminance_to_d3dformat(pixel_format
);
289 if (pixel_format
->flags
& DDS_PF_ALPHA_ONLY
)
290 return dds_alpha_to_d3dformat(pixel_format
);
291 if (pixel_format
->flags
& DDS_PF_BUMPDUDV
)
292 return dds_bump_to_d3dformat(pixel_format
);
293 if (pixel_format
->flags
& DDS_PF_BUMPLUMINANCE
)
294 return dds_bump_luminance_to_d3dformat(pixel_format
);
296 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n",
297 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
,
298 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
299 return D3DFMT_UNKNOWN
;
302 static HRESULT
d3dformat_to_dds_pixel_format(struct dds_pixel_format
*pixel_format
, D3DFORMAT d3dformat
)
306 memset(pixel_format
, 0, sizeof(*pixel_format
));
308 pixel_format
->size
= sizeof(*pixel_format
);
310 for (i
= 0; i
< sizeof(rgb_pixel_formats
) / sizeof(rgb_pixel_formats
[0]); i
++)
312 if (rgb_pixel_formats
[i
].format
== d3dformat
)
314 pixel_format
->flags
|= DDS_PF_RGB
;
315 pixel_format
->bpp
= rgb_pixel_formats
[i
].bpp
;
316 pixel_format
->rmask
= rgb_pixel_formats
[i
].rmask
;
317 pixel_format
->gmask
= rgb_pixel_formats
[i
].gmask
;
318 pixel_format
->bmask
= rgb_pixel_formats
[i
].bmask
;
319 pixel_format
->amask
= rgb_pixel_formats
[i
].amask
;
320 if (pixel_format
->amask
) pixel_format
->flags
|= DDS_PF_ALPHA
;
325 /* Reuse dds_fourcc_to_d3dformat as D3DFORMAT and FOURCC are DWORD with same values */
326 if (dds_fourcc_to_d3dformat(d3dformat
) != D3DFMT_UNKNOWN
)
328 pixel_format
->flags
|= DDS_PF_FOURCC
;
329 pixel_format
->fourcc
= d3dformat
;
333 WARN("Unknown pixel format %#x\n", d3dformat
);
337 static HRESULT
calculate_dds_surface_size(D3DFORMAT format
, UINT width
, UINT height
,
338 UINT
*pitch
, UINT
*size
)
340 const struct pixel_format_desc
*format_desc
= get_format_info(format
);
341 if (format_desc
->type
== FORMAT_UNKNOWN
)
344 if (format_desc
->block_width
!= 1 || format_desc
->block_height
!= 1)
346 *pitch
= format_desc
->block_byte_count
347 * max(1, (width
+ format_desc
->block_width
- 1) / format_desc
->block_width
);
349 * max(1, (height
+ format_desc
->block_height
- 1) / format_desc
->block_height
);
353 *pitch
= width
* format_desc
->bytes_per_pixel
;
354 *size
= *pitch
* height
;
360 static UINT
calculate_dds_file_size(D3DFORMAT format
, UINT width
, UINT height
, UINT depth
,
361 UINT miplevels
, UINT faces
)
363 UINT i
, file_size
= 0;
365 for (i
= 0; i
< miplevels
; i
++)
367 UINT pitch
, size
= 0;
368 calculate_dds_surface_size(format
, width
, height
, &pitch
, &size
);
371 width
= max(1, width
/ 2);
372 height
= max(1, height
/ 2);
373 depth
= max(1, depth
/ 2);
377 file_size
+= sizeof(struct dds_header
);
381 /************************************************************
382 * get_image_info_from_dds
384 * Fills a D3DXIMAGE_INFO structure with information
385 * about a DDS file stored in the memory.
388 * buffer [I] pointer to DDS data
389 * length [I] size of DDS data
390 * info [O] pointer to D3DXIMAGE_INFO structure
394 * Failure: D3DXERR_INVALIDDATA
397 static HRESULT
get_image_info_from_dds(const void *buffer
, UINT length
, D3DXIMAGE_INFO
*info
)
400 UINT expected_length
;
401 const struct dds_header
*header
= buffer
;
403 if (length
< sizeof(*header
) || !info
)
404 return D3DXERR_INVALIDDATA
;
406 if (header
->pixel_format
.size
!= sizeof(header
->pixel_format
))
407 return D3DXERR_INVALIDDATA
;
409 info
->Width
= header
->width
;
410 info
->Height
= header
->height
;
412 info
->MipLevels
= header
->miplevels
? header
->miplevels
: 1;
414 info
->Format
= dds_pixel_format_to_d3dformat(&header
->pixel_format
);
415 if (info
->Format
== D3DFMT_UNKNOWN
)
416 return D3DXERR_INVALIDDATA
;
418 TRACE("Pixel format is %#x\n", info
->Format
);
420 if (header
->caps2
& DDS_CAPS2_VOLUME
)
422 info
->Depth
= header
->depth
;
423 info
->ResourceType
= D3DRTYPE_VOLUMETEXTURE
;
425 else if (header
->caps2
& DDS_CAPS2_CUBEMAP
)
429 for (face
= DDS_CAPS2_CUBEMAP_POSITIVEX
; face
<= DDS_CAPS2_CUBEMAP_NEGATIVEZ
; face
<<= 1)
431 if (header
->caps2
& face
)
434 info
->ResourceType
= D3DRTYPE_CUBETEXTURE
;
438 info
->ResourceType
= D3DRTYPE_TEXTURE
;
441 expected_length
= calculate_dds_file_size(info
->Format
, info
->Width
, info
->Height
, info
->Depth
,
442 info
->MipLevels
, faces
);
443 if (length
< expected_length
)
445 WARN("File is too short %u, expected at least %u bytes\n", length
, expected_length
);
446 return D3DXERR_INVALIDDATA
;
449 info
->ImageFileFormat
= D3DXIFF_DDS
;
453 static HRESULT
load_surface_from_dds(IDirect3DSurface9
*dst_surface
, const PALETTEENTRY
*dst_palette
,
454 const RECT
*dst_rect
, const void *src_data
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
,
455 const D3DXIMAGE_INFO
*src_info
)
459 const struct dds_header
*header
= src_data
;
460 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
462 if (src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
463 return D3DXERR_INVALIDDATA
;
465 if (FAILED(calculate_dds_surface_size(src_info
->Format
, src_info
->Width
, src_info
->Height
, &src_pitch
, &size
)))
468 return D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
, pixels
, src_info
->Format
,
469 src_pitch
, NULL
, src_rect
, filter
, color_key
);
472 static HRESULT
save_dds_surface_to_memory(ID3DXBuffer
**dst_buffer
, IDirect3DSurface9
*src_surface
, const RECT
*src_rect
)
475 UINT dst_pitch
, surface_size
, file_size
;
476 D3DSURFACE_DESC src_desc
;
477 D3DLOCKED_RECT locked_rect
;
479 struct dds_header
*header
;
481 struct volume volume
;
482 const struct pixel_format_desc
*pixel_format
;
486 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
490 hr
= IDirect3DSurface9_GetDesc(src_surface
, &src_desc
);
491 if (FAILED(hr
)) return hr
;
493 pixel_format
= get_format_info(src_desc
.Format
);
494 if (pixel_format
->type
== FORMAT_UNKNOWN
) return E_NOTIMPL
;
496 file_size
= calculate_dds_file_size(src_desc
.Format
, src_desc
.Width
, src_desc
.Height
, 1, 1, 1);
498 hr
= calculate_dds_surface_size(src_desc
.Format
, src_desc
.Width
, src_desc
.Height
, &dst_pitch
, &surface_size
);
499 if (FAILED(hr
)) return hr
;
501 hr
= D3DXCreateBuffer(file_size
, &buffer
);
502 if (FAILED(hr
)) return hr
;
504 header
= ID3DXBuffer_GetBufferPointer(buffer
);
505 pixels
= (BYTE
*)(header
+ 1);
507 memset(header
, 0, sizeof(*header
));
508 header
->signature
= MAKEFOURCC('D','D','S',' ');
509 /* The signature is not really part of the DDS header */
510 header
->size
= sizeof(*header
) - FIELD_OFFSET(struct dds_header
, size
);
511 header
->flags
= DDS_CAPS
| DDS_HEIGHT
| DDS_WIDTH
| DDS_PIXELFORMAT
;
512 header
->height
= src_desc
.Height
;
513 header
->width
= src_desc
.Width
;
514 header
->caps
= DDS_CAPS_TEXTURE
;
515 hr
= d3dformat_to_dds_pixel_format(&header
->pixel_format
, src_desc
.Format
);
518 ID3DXBuffer_Release(buffer
);
522 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, NULL
, D3DLOCK_READONLY
);
525 ID3DXBuffer_Release(buffer
);
529 volume
.width
= src_desc
.Width
;
530 volume
.height
= src_desc
.Height
;
532 copy_pixels(locked_rect
.pBits
, locked_rect
.Pitch
, 0, pixels
, dst_pitch
, 0,
533 &volume
, pixel_format
);
535 IDirect3DSurface9_UnlockRect(src_surface
);
537 *dst_buffer
= buffer
;
541 static HRESULT
get_surface(D3DRESOURCETYPE type
, struct IDirect3DBaseTexture9
*tex
,
542 int face
, UINT level
, struct IDirect3DSurface9
**surf
)
546 case D3DRTYPE_TEXTURE
:
547 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9
*) tex
, level
, surf
);
548 case D3DRTYPE_CUBETEXTURE
:
549 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9
*) tex
, face
, level
, surf
);
551 ERR("Unexpected texture type\n");
556 HRESULT
save_dds_texture_to_memory(ID3DXBuffer
**dst_buffer
, IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
559 D3DRESOURCETYPE type
;
561 IDirect3DSurface9
*surface
;
563 type
= IDirect3DBaseTexture9_GetType(src_texture
);
565 if ((type
!= D3DRTYPE_TEXTURE
) && (type
!= D3DRTYPE_CUBETEXTURE
) && (type
!= D3DRTYPE_VOLUMETEXTURE
))
566 return D3DERR_INVALIDCALL
;
568 if (type
== D3DRTYPE_CUBETEXTURE
)
570 FIXME("Cube texture not supported yet\n");
573 else if (type
== D3DRTYPE_VOLUMETEXTURE
)
575 FIXME("Volume texture not supported yet\n");
579 mip_levels
= IDirect3DTexture9_GetLevelCount(src_texture
);
583 FIXME("Mipmap not supported yet\n");
589 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
593 hr
= get_surface(type
, src_texture
, D3DCUBEMAP_FACE_POSITIVE_X
, 0, &surface
);
597 hr
= save_dds_surface_to_memory(dst_buffer
, surface
, NULL
);
598 IDirect3DSurface9_Release(surface
);
603 HRESULT
load_volume_from_dds(IDirect3DVolume9
*dst_volume
, const PALETTEENTRY
*dst_palette
,
604 const D3DBOX
*dst_box
, const void *src_data
, const D3DBOX
*src_box
, DWORD filter
, D3DCOLOR color_key
,
605 const D3DXIMAGE_INFO
*src_info
)
607 UINT row_pitch
, slice_pitch
;
608 const struct dds_header
*header
= src_data
;
609 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
611 if (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
)
612 return D3DXERR_INVALIDDATA
;
614 if (FAILED(calculate_dds_surface_size(src_info
->Format
, src_info
->Width
, src_info
->Height
, &row_pitch
, &slice_pitch
)))
617 return D3DXLoadVolumeFromMemory(dst_volume
, dst_palette
, dst_box
, pixels
, src_info
->Format
,
618 row_pitch
, slice_pitch
, NULL
, src_box
, filter
, color_key
);
621 HRESULT
load_texture_from_dds(IDirect3DTexture9
*texture
, const void *src_data
, const PALETTEENTRY
*palette
,
622 DWORD filter
, D3DCOLOR color_key
, const D3DXIMAGE_INFO
*src_info
, unsigned int skip_levels
,
623 unsigned int *loaded_miplevels
)
632 IDirect3DSurface9
*surface
;
633 const struct dds_header
*header
= src_data
;
634 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
636 /* Loading a cube texture as a simple texture is also supported
637 * (only first face texture is taken). Same with volume textures. */
638 if ((src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
639 && (src_info
->ResourceType
!= D3DRTYPE_CUBETEXTURE
)
640 && (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
))
642 WARN("Trying to load a %u resource as a 2D texture, returning failure.\n", src_info
->ResourceType
);
643 return D3DXERR_INVALIDDATA
;
646 width
= src_info
->Width
;
647 height
= src_info
->Height
;
648 mip_levels
= min(src_info
->MipLevels
, IDirect3DTexture9_GetLevelCount(texture
));
649 if (src_info
->ResourceType
== D3DRTYPE_VOLUMETEXTURE
)
651 for (mip_level
= 0; mip_level
< mip_levels
+ skip_levels
; ++mip_level
)
653 hr
= calculate_dds_surface_size(src_info
->Format
, width
, height
, &src_pitch
, &mip_level_size
);
654 if (FAILED(hr
)) return hr
;
656 if (mip_level
>= skip_levels
)
658 SetRect(&src_rect
, 0, 0, width
, height
);
660 IDirect3DTexture9_GetSurfaceLevel(texture
, mip_level
- skip_levels
, &surface
);
661 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
662 NULL
, &src_rect
, filter
, color_key
);
663 IDirect3DSurface9_Release(surface
);
668 pixels
+= mip_level_size
;
669 width
= max(1, width
/ 2);
670 height
= max(1, height
/ 2);
673 *loaded_miplevels
= mip_levels
- skip_levels
;
678 HRESULT
load_cube_texture_from_dds(IDirect3DCubeTexture9
*cube_texture
, const void *src_data
,
679 const PALETTEENTRY
*palette
, DWORD filter
, DWORD color_key
, const D3DXIMAGE_INFO
*src_info
)
689 IDirect3DSurface9
*surface
;
690 const struct dds_header
*header
= src_data
;
691 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
693 if (src_info
->ResourceType
!= D3DRTYPE_CUBETEXTURE
)
694 return D3DXERR_INVALIDDATA
;
696 if ((header
->caps2
& DDS_CAPS2_CUBEMAP_ALL_FACES
) != DDS_CAPS2_CUBEMAP_ALL_FACES
)
698 WARN("Only full cubemaps are supported\n");
699 return D3DXERR_INVALIDDATA
;
702 mip_levels
= min(src_info
->MipLevels
, IDirect3DCubeTexture9_GetLevelCount(cube_texture
));
703 for (face
= D3DCUBEMAP_FACE_POSITIVE_X
; face
<= D3DCUBEMAP_FACE_NEGATIVE_Z
; face
++)
705 size
= src_info
->Width
;
706 for (mip_level
= 0; mip_level
< src_info
->MipLevels
; mip_level
++)
708 hr
= calculate_dds_surface_size(src_info
->Format
, size
, size
, &src_pitch
, &mip_level_size
);
709 if (FAILED(hr
)) return hr
;
711 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
712 if (mip_level
< mip_levels
)
714 SetRect(&src_rect
, 0, 0, size
, size
);
716 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture
, face
, mip_level
, &surface
);
717 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
718 NULL
, &src_rect
, filter
, color_key
);
719 IDirect3DSurface9_Release(surface
);
720 if (FAILED(hr
)) return hr
;
723 pixels
+= mip_level_size
;
724 size
= max(1, size
/ 2);
731 HRESULT
load_volume_texture_from_dds(IDirect3DVolumeTexture9
*volume_texture
, const void *src_data
,
732 const PALETTEENTRY
*palette
, DWORD filter
, DWORD color_key
, const D3DXIMAGE_INFO
*src_info
)
737 UINT src_slice_pitch
;
740 UINT width
, height
, depth
;
741 IDirect3DVolume9
*volume
;
742 const struct dds_header
*header
= src_data
;
743 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
745 if (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
)
746 return D3DXERR_INVALIDDATA
;
748 width
= src_info
->Width
;
749 height
= src_info
->Height
;
750 depth
= src_info
->Depth
;
751 mip_levels
= min(src_info
->MipLevels
, IDirect3DVolumeTexture9_GetLevelCount(volume_texture
));
753 for (mip_level
= 0; mip_level
< mip_levels
; mip_level
++)
755 hr
= calculate_dds_surface_size(src_info
->Format
, width
, height
, &src_row_pitch
, &src_slice_pitch
);
756 if (FAILED(hr
)) return hr
;
758 hr
= IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, mip_level
, &volume
);
759 if (FAILED(hr
)) return hr
;
763 src_box
.Right
= width
;
764 src_box
.Bottom
= height
;
766 src_box
.Back
= depth
;
768 hr
= D3DXLoadVolumeFromMemory(volume
, palette
, NULL
, pixels
, src_info
->Format
,
769 src_row_pitch
, src_slice_pitch
, NULL
, &src_box
, filter
, color_key
);
771 IDirect3DVolume9_Release(volume
);
772 if (FAILED(hr
)) return hr
;
774 pixels
+= depth
* src_slice_pitch
;
775 width
= max(1, width
/ 2);
776 height
= max(1, height
/ 2);
777 depth
= max(1, depth
/ 2);
783 static BOOL
convert_dib_to_bmp(void **data
, UINT
*size
)
788 BITMAPFILEHEADER
*header
;
792 if ((*size
< 4) || (*size
< (header_size
= *(ULONG
*)*data
)))
795 if ((header_size
== sizeof(BITMAPINFOHEADER
)) ||
796 (header_size
== sizeof(BITMAPV4HEADER
)) ||
797 (header_size
== sizeof(BITMAPV5HEADER
)) ||
798 (header_size
== 64 /* sizeof(BITMAPCOREHEADER2) */))
800 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
801 BITMAPINFOHEADER
*info_header
= (BITMAPINFOHEADER
*)*data
;
802 count
= info_header
->biClrUsed
;
804 if (!count
&& info_header
->biBitCount
<= 8)
805 count
= 1 << info_header
->biBitCount
;
807 offset
= sizeof(BITMAPFILEHEADER
) + header_size
+ sizeof(RGBQUAD
) * count
;
809 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
810 if ((info_header
->biSize
== sizeof(BITMAPINFOHEADER
)) && (info_header
->biCompression
== BI_BITFIELDS
))
811 offset
+= 3 * sizeof(DWORD
);
813 else if (header_size
== sizeof(BITMAPCOREHEADER
))
815 BITMAPCOREHEADER
*core_header
= (BITMAPCOREHEADER
*)*data
;
817 if (core_header
->bcBitCount
<= 8)
818 count
= 1 << core_header
->bcBitCount
;
820 offset
= sizeof(BITMAPFILEHEADER
) + header_size
+ sizeof(RGBTRIPLE
) * count
;
827 TRACE("Converting DIB file to BMP\n");
829 new_size
= *size
+ sizeof(BITMAPFILEHEADER
);
830 new_data
= HeapAlloc(GetProcessHeap(), 0, new_size
);
831 CopyMemory(new_data
+ sizeof(BITMAPFILEHEADER
), *data
, *size
);
834 header
= (BITMAPFILEHEADER
*)new_data
;
835 header
->bfType
= 0x4d42; /* BM */
836 header
->bfSize
= new_size
;
837 header
->bfReserved1
= 0;
838 header
->bfReserved2
= 0;
839 header
->bfOffBits
= offset
;
841 /* Update input data */
848 /************************************************************
849 * D3DXGetImageInfoFromFileInMemory
851 * Fills a D3DXIMAGE_INFO structure with info about an image
854 * data [I] pointer to the image file data
855 * datasize [I] size of the passed data
856 * info [O] pointer to the destination structure
859 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
860 * if info is NULL and data and datasize are not NULL
861 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
862 * D3DERR_INVALIDCALL, if data is NULL or
866 * datasize may be bigger than the actual file size
869 HRESULT WINAPI
D3DXGetImageInfoFromFileInMemory(const void *data
, UINT datasize
, D3DXIMAGE_INFO
*info
)
871 IWICImagingFactory
*factory
;
872 IWICBitmapDecoder
*decoder
= NULL
;
878 TRACE("(%p, %d, %p)\n", data
, datasize
, info
);
880 if (!data
|| !datasize
)
881 return D3DERR_INVALIDCALL
;
886 if ((datasize
>= 4) && !strncmp(data
, "DDS ", 4)) {
887 TRACE("File type is DDS\n");
888 return get_image_info_from_dds(data
, datasize
, info
);
891 /* In case of DIB file, convert it to BMP */
892 dib
= convert_dib_to_bmp((void**)&data
, &datasize
);
894 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
896 hr
= CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
);
899 IWICImagingFactory_CreateStream(factory
, &stream
);
900 IWICStream_InitializeFromMemory(stream
, (BYTE
*)data
, datasize
);
901 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
902 IWICStream_Release(stream
);
903 IWICImagingFactory_Release(factory
);
907 if ((datasize
>= 2) && (!strncmp(data
, "P3", 2) || !strncmp(data
, "P6", 2)))
908 FIXME("File type PPM is not supported yet\n");
909 else if ((datasize
>= 10) && !strncmp(data
, "#?RADIANCE", 10))
910 FIXME("File type HDR is not supported yet\n");
911 else if ((datasize
>= 2) && (!strncmp(data
, "PF", 2) || !strncmp(data
, "Pf", 2)))
912 FIXME("File type PFM is not supported yet\n");
916 GUID container_format
;
919 hr
= IWICBitmapDecoder_GetContainerFormat(decoder
, &container_format
);
921 if (IsEqualGUID(&container_format
, &GUID_ContainerFormatBmp
)) {
923 TRACE("File type is DIB\n");
924 info
->ImageFileFormat
= D3DXIFF_DIB
;
926 TRACE("File type is BMP\n");
927 info
->ImageFileFormat
= D3DXIFF_BMP
;
929 } else if (IsEqualGUID(&container_format
, &GUID_ContainerFormatPng
)) {
930 TRACE("File type is PNG\n");
931 info
->ImageFileFormat
= D3DXIFF_PNG
;
932 } else if(IsEqualGUID(&container_format
, &GUID_ContainerFormatJpeg
)) {
933 TRACE("File type is JPG\n");
934 info
->ImageFileFormat
= D3DXIFF_JPG
;
935 } else if(IsEqualGUID(&container_format
, &GUID_WineContainerFormatTga
)) {
936 TRACE("File type is TGA\n");
937 info
->ImageFileFormat
= D3DXIFF_TGA
;
939 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format
));
940 hr
= D3DXERR_INVALIDDATA
;
945 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
946 if (SUCCEEDED(hr
) && !frame_count
)
947 hr
= D3DXERR_INVALIDDATA
;
950 IWICBitmapFrameDecode
*frame
= NULL
;
952 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
955 hr
= IWICBitmapFrameDecode_GetSize(frame
, &info
->Width
, &info
->Height
);
958 WICPixelFormatGUID pixel_format
;
960 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &pixel_format
);
962 info
->Format
= wic_guid_to_d3dformat(&pixel_format
);
963 if (info
->Format
== D3DFMT_UNKNOWN
) {
964 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format
));
965 hr
= D3DXERR_INVALIDDATA
;
970 /* For 32 bpp BMP, windowscodecs.dll never returns a format with alpha while
971 * d3dx9_xx.dll returns one if at least 1 pixel has a non zero alpha component */
972 if (SUCCEEDED(hr
) && (info
->Format
== D3DFMT_X8R8G8B8
) && (info
->ImageFileFormat
== D3DXIFF_BMP
)) {
973 DWORD size
= sizeof(DWORD
) * info
->Width
* info
->Height
;
974 BYTE
*buffer
= HeapAlloc(GetProcessHeap(), 0, size
);
975 hr
= IWICBitmapFrameDecode_CopyPixels(frame
, NULL
, sizeof(DWORD
) * info
->Width
, size
, buffer
);
978 for (i
= 0; i
< info
->Width
* info
->Height
; i
++) {
980 info
->Format
= D3DFMT_A8R8G8B8
;
985 HeapFree(GetProcessHeap(), 0, buffer
);
989 IWICBitmapFrameDecode_Release(frame
);
993 info
->ResourceType
= D3DRTYPE_TEXTURE
;
998 IWICBitmapDecoder_Release(decoder
);
1000 if (SUCCEEDED(initresult
))
1004 HeapFree(GetProcessHeap(), 0, (void*)data
);
1007 TRACE("Invalid or unsupported image file\n");
1008 return D3DXERR_INVALIDDATA
;
1014 /************************************************************
1015 * D3DXGetImageInfoFromFile
1018 * Success: D3D_OK, if we successfully load a valid image file or
1019 * if we successfully load a file which is no valid image and info is NULL
1020 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
1021 * if file is not a valid image file and info is not NULL
1022 * D3DERR_INVALIDCALL, if file is NULL
1025 HRESULT WINAPI
D3DXGetImageInfoFromFileA(const char *file
, D3DXIMAGE_INFO
*info
)
1031 TRACE("file %s, info %p.\n", debugstr_a(file
), info
);
1033 if( !file
) return D3DERR_INVALIDCALL
;
1035 strlength
= MultiByteToWideChar(CP_ACP
, 0, file
, -1, NULL
, 0);
1036 widename
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*widename
));
1037 MultiByteToWideChar(CP_ACP
, 0, file
, -1, widename
, strlength
);
1039 hr
= D3DXGetImageInfoFromFileW(widename
, info
);
1040 HeapFree(GetProcessHeap(), 0, widename
);
1045 HRESULT WINAPI
D3DXGetImageInfoFromFileW(const WCHAR
*file
, D3DXIMAGE_INFO
*info
)
1051 TRACE("file %s, info %p.\n", debugstr_w(file
), info
);
1054 return D3DERR_INVALIDCALL
;
1056 if (FAILED(map_view_of_file(file
, &buffer
, &size
)))
1057 return D3DXERR_INVALIDDATA
;
1059 hr
= D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
1060 UnmapViewOfFile(buffer
);
1065 /************************************************************
1066 * D3DXGetImageInfoFromResource
1069 * Success: D3D_OK, if resource is a valid image file
1070 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
1071 * if we fail to load resource
1074 HRESULT WINAPI
D3DXGetImageInfoFromResourceA(HMODULE module
, const char *resource
, D3DXIMAGE_INFO
*info
)
1080 TRACE("module %p, resource %s, info %p.\n", module
, debugstr_a(resource
), info
);
1082 if (!(resinfo
= FindResourceA(module
, resource
, (const char *)RT_RCDATA
))
1083 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1084 && !(resinfo
= FindResourceA(module
, resource
, (const char *)RT_BITMAP
)))
1085 return D3DXERR_INVALIDDATA
;
1087 if (FAILED(load_resource_into_memory(module
, resinfo
, &buffer
, &size
)))
1088 return D3DXERR_INVALIDDATA
;
1090 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
1093 HRESULT WINAPI
D3DXGetImageInfoFromResourceW(HMODULE module
, const WCHAR
*resource
, D3DXIMAGE_INFO
*info
)
1099 TRACE("module %p, resource %s, info %p.\n", module
, debugstr_w(resource
), info
);
1101 if (!(resinfo
= FindResourceW(module
, resource
, (const WCHAR
*)RT_RCDATA
))
1102 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1103 && !(resinfo
= FindResourceW(module
, resource
, (const WCHAR
*)RT_BITMAP
)))
1104 return D3DXERR_INVALIDDATA
;
1106 if (FAILED(load_resource_into_memory(module
, resinfo
, &buffer
, &size
)))
1107 return D3DXERR_INVALIDDATA
;
1109 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
1112 /************************************************************
1113 * D3DXLoadSurfaceFromFileInMemory
1115 * Loads data from a given buffer into a surface and fills a given
1116 * D3DXIMAGE_INFO structure with info about the source data.
1119 * pDestSurface [I] pointer to the surface
1120 * pDestPalette [I] palette to use
1121 * pDestRect [I] to be filled area of the surface
1122 * pSrcData [I] pointer to the source data
1123 * SrcDataSize [I] size of the source data in bytes
1124 * pSrcRect [I] area of the source data to load
1125 * dwFilter [I] filter to apply on stretching
1126 * Colorkey [I] colorkey
1127 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1131 * Failure: D3DERR_INVALIDCALL, if pDestSurface, pSrcData or SrcDataSize is NULL
1132 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1135 HRESULT WINAPI
D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9
*pDestSurface
,
1136 const PALETTEENTRY
*pDestPalette
, const RECT
*pDestRect
, const void *pSrcData
, UINT SrcDataSize
,
1137 const RECT
*pSrcRect
, DWORD dwFilter
, D3DCOLOR Colorkey
, D3DXIMAGE_INFO
*pSrcInfo
)
1139 D3DXIMAGE_INFO imginfo
;
1140 HRESULT hr
, com_init
;
1142 IWICImagingFactory
*factory
= NULL
;
1143 IWICBitmapDecoder
*decoder
;
1144 IWICBitmapFrameDecode
*bitmapframe
;
1147 const struct pixel_format_desc
*formatdesc
;
1151 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1152 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1153 pDestSurface
, pDestPalette
, wine_dbgstr_rect(pDestRect
), pSrcData
, SrcDataSize
,
1154 wine_dbgstr_rect(pSrcRect
), dwFilter
, Colorkey
, pSrcInfo
);
1156 if (!pDestSurface
|| !pSrcData
|| !SrcDataSize
)
1157 return D3DERR_INVALIDCALL
;
1159 hr
= D3DXGetImageInfoFromFileInMemory(pSrcData
, SrcDataSize
, &imginfo
);
1166 wicrect
.X
= pSrcRect
->left
;
1167 wicrect
.Y
= pSrcRect
->top
;
1168 wicrect
.Width
= pSrcRect
->right
- pSrcRect
->left
;
1169 wicrect
.Height
= pSrcRect
->bottom
- pSrcRect
->top
;
1175 wicrect
.Width
= imginfo
.Width
;
1176 wicrect
.Height
= imginfo
.Height
;
1179 SetRect(&rect
, 0, 0, wicrect
.Width
, wicrect
.Height
);
1181 if (imginfo
.ImageFileFormat
== D3DXIFF_DDS
)
1183 hr
= load_surface_from_dds(pDestSurface
, pDestPalette
, pDestRect
, pSrcData
, &rect
,
1184 dwFilter
, Colorkey
, &imginfo
);
1185 if (SUCCEEDED(hr
) && pSrcInfo
)
1186 *pSrcInfo
= imginfo
;
1190 if (imginfo
.ImageFileFormat
== D3DXIFF_DIB
)
1191 convert_dib_to_bmp((void**)&pSrcData
, &SrcDataSize
);
1193 com_init
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1195 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
)))
1198 if (FAILED(IWICImagingFactory_CreateStream(factory
, &stream
)))
1200 IWICImagingFactory_Release(factory
);
1205 IWICStream_InitializeFromMemory(stream
, (BYTE
*)pSrcData
, SrcDataSize
);
1207 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
1209 IWICStream_Release(stream
);
1214 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &bitmapframe
);
1219 formatdesc
= get_format_info(imginfo
.Format
);
1221 if (formatdesc
->type
== FORMAT_UNKNOWN
)
1223 FIXME("Unsupported pixel format\n");
1224 hr
= D3DXERR_INVALIDDATA
;
1230 PALETTEENTRY
*palette
= NULL
;
1231 WICColor
*colors
= NULL
;
1233 pitch
= formatdesc
->bytes_per_pixel
* wicrect
.Width
;
1234 buffer
= HeapAlloc(GetProcessHeap(), 0, pitch
* wicrect
.Height
);
1236 hr
= IWICBitmapFrameDecode_CopyPixels(bitmapframe
, &wicrect
, pitch
,
1237 pitch
* wicrect
.Height
, buffer
);
1239 if (SUCCEEDED(hr
) && (formatdesc
->type
== FORMAT_INDEX
))
1241 IWICPalette
*wic_palette
= NULL
;
1244 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
1246 hr
= IWICBitmapFrameDecode_CopyPalette(bitmapframe
, wic_palette
);
1248 hr
= IWICPalette_GetColorCount(wic_palette
, &nb_colors
);
1251 colors
= HeapAlloc(GetProcessHeap(), 0, nb_colors
* sizeof(colors
[0]));
1252 palette
= HeapAlloc(GetProcessHeap(), 0, nb_colors
* sizeof(palette
[0]));
1253 if (!colors
|| !palette
)
1257 hr
= IWICPalette_GetColors(wic_palette
, nb_colors
, colors
, &nb_colors
);
1262 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1263 for (i
= 0; i
< nb_colors
; i
++)
1265 palette
[i
].peRed
= (colors
[i
] >> 16) & 0xff;
1266 palette
[i
].peGreen
= (colors
[i
] >> 8) & 0xff;
1267 palette
[i
].peBlue
= colors
[i
] & 0xff;
1268 palette
[i
].peFlags
= (colors
[i
] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1272 IWICPalette_Release(wic_palette
);
1277 hr
= D3DXLoadSurfaceFromMemory(pDestSurface
, pDestPalette
, pDestRect
,
1278 buffer
, imginfo
.Format
, pitch
,
1279 palette
, &rect
, dwFilter
, Colorkey
);
1282 HeapFree(GetProcessHeap(), 0, colors
);
1283 HeapFree(GetProcessHeap(), 0, palette
);
1284 HeapFree(GetProcessHeap(), 0, buffer
);
1287 IWICBitmapFrameDecode_Release(bitmapframe
);
1290 IWICBitmapDecoder_Release(decoder
);
1294 IWICImagingFactory_Release(factory
);
1296 if (SUCCEEDED(com_init
))
1299 if (imginfo
.ImageFileFormat
== D3DXIFF_DIB
)
1300 HeapFree(GetProcessHeap(), 0, (void*)pSrcData
);
1303 return D3DXERR_INVALIDDATA
;
1306 *pSrcInfo
= imginfo
;
1311 HRESULT WINAPI
D3DXLoadSurfaceFromFileA(IDirect3DSurface9
*dst_surface
,
1312 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const char *src_file
,
1313 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1319 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1320 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1321 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), debugstr_a(src_file
),
1322 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1324 if (!src_file
|| !dst_surface
)
1325 return D3DERR_INVALIDCALL
;
1327 strlength
= MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, NULL
, 0);
1328 src_file_w
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*src_file_w
));
1329 MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, src_file_w
, strlength
);
1331 hr
= D3DXLoadSurfaceFromFileW(dst_surface
, dst_palette
, dst_rect
,
1332 src_file_w
, src_rect
, filter
, color_key
, src_info
);
1333 HeapFree(GetProcessHeap(), 0, src_file_w
);
1338 HRESULT WINAPI
D3DXLoadSurfaceFromFileW(IDirect3DSurface9
*dst_surface
,
1339 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const WCHAR
*src_file
,
1340 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1346 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1347 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1348 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), debugstr_w(src_file
),
1349 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1351 if (!src_file
|| !dst_surface
)
1352 return D3DERR_INVALIDCALL
;
1354 if (FAILED(map_view_of_file(src_file
, &data
, &data_size
)))
1355 return D3DXERR_INVALIDDATA
;
1357 hr
= D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1358 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1359 UnmapViewOfFile(data
);
1364 HRESULT WINAPI
D3DXLoadSurfaceFromResourceA(IDirect3DSurface9
*dst_surface
,
1365 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const char *resource
,
1366 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1372 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1373 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1374 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_a(resource
),
1375 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1378 return D3DERR_INVALIDCALL
;
1380 if (!(resinfo
= FindResourceA(src_module
, resource
, (const char *)RT_RCDATA
))
1381 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1382 && !(resinfo
= FindResourceA(src_module
, resource
, (const char *)RT_BITMAP
)))
1383 return D3DXERR_INVALIDDATA
;
1385 if (FAILED(load_resource_into_memory(src_module
, resinfo
, &data
, &data_size
)))
1386 return D3DXERR_INVALIDDATA
;
1388 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1389 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1392 HRESULT WINAPI
D3DXLoadSurfaceFromResourceW(IDirect3DSurface9
*dst_surface
,
1393 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const WCHAR
*resource
,
1394 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1400 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1401 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1402 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_w(resource
),
1403 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1406 return D3DERR_INVALIDCALL
;
1408 if (!(resinfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_RCDATA
))
1409 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1410 && !(resinfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_BITMAP
)))
1411 return D3DXERR_INVALIDDATA
;
1413 if (FAILED(load_resource_into_memory(src_module
, resinfo
, &data
, &data_size
)))
1414 return D3DXERR_INVALIDDATA
;
1416 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1417 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1421 /************************************************************
1422 * helper functions for D3DXLoadSurfaceFromMemory
1424 struct argb_conversion_info
1426 const struct pixel_format_desc
*srcformat
;
1427 const struct pixel_format_desc
*destformat
;
1428 DWORD srcshift
[4], destshift
[4];
1429 DWORD srcmask
[4], destmask
[4];
1430 BOOL process_channel
[4];
1434 static void init_argb_conversion_info(const struct pixel_format_desc
*srcformat
, const struct pixel_format_desc
*destformat
, struct argb_conversion_info
*info
)
1437 ZeroMemory(info
->process_channel
, 4 * sizeof(BOOL
));
1438 info
->channelmask
= 0;
1440 info
->srcformat
= srcformat
;
1441 info
->destformat
= destformat
;
1443 for(i
= 0;i
< 4;i
++) {
1444 /* srcshift is used to extract the _relevant_ components */
1445 info
->srcshift
[i
] = srcformat
->shift
[i
] + max( srcformat
->bits
[i
] - destformat
->bits
[i
], 0);
1447 /* destshift is used to move the components to the correct position */
1448 info
->destshift
[i
] = destformat
->shift
[i
] + max(destformat
->bits
[i
] - srcformat
->bits
[i
], 0);
1450 info
->srcmask
[i
] = ((1 << srcformat
->bits
[i
]) - 1) << srcformat
->shift
[i
];
1451 info
->destmask
[i
] = ((1 << destformat
->bits
[i
]) - 1) << destformat
->shift
[i
];
1453 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1454 if(destformat
->bits
[i
]) {
1455 if(srcformat
->bits
[i
]) info
->process_channel
[i
] = TRUE
;
1456 else info
->channelmask
|= info
->destmask
[i
];
1461 /************************************************************
1462 * get_relevant_argb_components
1464 * Extracts the relevant components from the source color and
1465 * drops the less significant bits if they aren't used by the destination format.
1467 static void get_relevant_argb_components(const struct argb_conversion_info
*info
, const BYTE
*col
, DWORD
*out
)
1470 unsigned int component
, mask
;
1472 for (i
= 0; i
< 4; ++i
)
1474 if (!info
->process_channel
[i
])
1478 mask
= info
->srcmask
[i
];
1479 for (j
= 0; j
< 4 && mask
; ++j
)
1481 if (info
->srcshift
[i
] < j
* 8)
1482 component
|= (col
[j
] & mask
) << (j
* 8 - info
->srcshift
[i
]);
1484 component
|= (col
[j
] & mask
) >> (info
->srcshift
[i
] - j
* 8);
1491 /************************************************************
1494 * Recombines the output of get_relevant_argb_components and converts
1495 * it to the destination format.
1497 static DWORD
make_argb_color(const struct argb_conversion_info
*info
, const DWORD
*in
)
1502 for(i
= 0;i
< 4;i
++) {
1503 if(info
->process_channel
[i
]) {
1504 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1506 for(shift
= info
->destshift
[i
]; shift
> info
->destformat
->shift
[i
]; shift
-= info
->srcformat
->bits
[i
]) val
|= in
[i
] << shift
;
1507 val
|= (in
[i
] >> (info
->destformat
->shift
[i
] - shift
)) << info
->destformat
->shift
[i
];
1510 val
|= info
->channelmask
; /* new channels are set to their maximal value */
1514 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */
1515 static void format_to_vec4(const struct pixel_format_desc
*format
, const BYTE
*src
, struct vec4
*dst
)
1520 for (c
= 0; c
< 4; ++c
)
1522 static const unsigned int component_offsets
[4] = {3, 0, 1, 2};
1523 float *dst_component
= (float *)dst
+ component_offsets
[c
];
1525 if (format
->bits
[c
])
1527 mask
= ~0u >> (32 - format
->bits
[c
]);
1529 memcpy(&tmp
, src
+ format
->shift
[c
] / 8,
1530 min(sizeof(DWORD
), (format
->shift
[c
] % 8 + format
->bits
[c
] + 7) / 8));
1532 if (format
->type
== FORMAT_ARGBF16
)
1533 *dst_component
= float_16_to_32(tmp
);
1534 else if (format
->type
== FORMAT_ARGBF
)
1535 *dst_component
= *(float *)&tmp
;
1537 *dst_component
= (float)((tmp
>> format
->shift
[c
] % 8) & mask
) / mask
;
1540 *dst_component
= 1.0f
;
1544 /* It doesn't work for components bigger than 32 bits. */
1545 static void format_from_vec4(const struct pixel_format_desc
*format
, const struct vec4
*src
, BYTE
*dst
)
1550 memset(dst
, 0, format
->bytes_per_pixel
);
1552 for (c
= 0; c
< 4; ++c
)
1554 static const unsigned int component_offsets
[4] = {3, 0, 1, 2};
1555 const float src_component
= *((const float *)src
+ component_offsets
[c
]);
1557 if (!format
->bits
[c
])
1560 mask32
= ~0u >> (32 - format
->bits
[c
]);
1562 if (format
->type
== FORMAT_ARGBF16
)
1563 v
= float_32_to_16(src_component
);
1564 else if (format
->type
== FORMAT_ARGBF
)
1565 v
= *(DWORD
*)&src_component
;
1567 v
= (DWORD
)(src_component
* ((1 << format
->bits
[c
]) - 1) + 0.5f
);
1569 for (i
= format
->shift
[c
] / 8 * 8; i
< format
->shift
[c
] + format
->bits
[c
]; i
+= 8)
1573 if (format
->shift
[c
] > i
)
1575 mask
= mask32
<< (format
->shift
[c
] - i
);
1576 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1580 mask
= mask32
>> (i
- format
->shift
[c
]);
1581 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1588 /************************************************************
1591 * Copies the source buffer to the destination buffer.
1592 * Works for any pixel format.
1593 * The source and the destination must be block-aligned.
1595 void copy_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
,
1596 BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
, const struct volume
*size
,
1597 const struct pixel_format_desc
*format
)
1601 const BYTE
*src_addr
;
1602 UINT row_block_count
= (size
->width
+ format
->block_width
- 1) / format
->block_width
;
1603 UINT row_count
= (size
->height
+ format
->block_height
- 1) / format
->block_height
;
1605 for (slice
= 0; slice
< size
->depth
; slice
++)
1607 src_addr
= src
+ slice
* src_slice_pitch
;
1608 dst_addr
= dst
+ slice
* dst_slice_pitch
;
1610 for (row
= 0; row
< row_count
; row
++)
1612 memcpy(dst_addr
, src_addr
, row_block_count
* format
->block_byte_count
);
1613 src_addr
+= src_row_pitch
;
1614 dst_addr
+= dst_row_pitch
;
1619 /************************************************************
1620 * convert_argb_pixels
1622 * Copies the source buffer to the destination buffer, performing
1623 * any necessary format conversion and color keying.
1624 * Pixels outsize the source rect are blacked out.
1626 void convert_argb_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
, const struct volume
*src_size
,
1627 const struct pixel_format_desc
*src_format
, BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
,
1628 const struct volume
*dst_size
, const struct pixel_format_desc
*dst_format
, D3DCOLOR color_key
,
1629 const PALETTEENTRY
*palette
)
1631 struct argb_conversion_info conv_info
, ck_conv_info
;
1632 const struct pixel_format_desc
*ck_format
= NULL
;
1634 UINT min_width
, min_height
, min_depth
;
1637 ZeroMemory(channels
, sizeof(channels
));
1638 init_argb_conversion_info(src_format
, dst_format
, &conv_info
);
1640 min_width
= min(src_size
->width
, dst_size
->width
);
1641 min_height
= min(src_size
->height
, dst_size
->height
);
1642 min_depth
= min(src_size
->depth
, dst_size
->depth
);
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
< min_depth
; z
++) {
1652 const BYTE
*src_slice_ptr
= src
+ z
* src_slice_pitch
;
1653 BYTE
*dst_slice_ptr
= dst
+ z
* dst_slice_pitch
;
1655 for (y
= 0; y
< min_height
; y
++) {
1656 const BYTE
*src_ptr
= src_slice_ptr
+ y
* src_row_pitch
;
1657 BYTE
*dst_ptr
= dst_slice_ptr
+ y
* dst_row_pitch
;
1659 for (x
= 0; x
< min_width
; x
++) {
1660 if (!src_format
->to_rgba
&& !dst_format
->from_rgba
1661 && src_format
->type
== dst_format
->type
1662 && src_format
->bytes_per_pixel
<= 4 && dst_format
->bytes_per_pixel
<= 4)
1666 get_relevant_argb_components(&conv_info
, src_ptr
, channels
);
1667 val
= make_argb_color(&conv_info
, channels
);
1673 get_relevant_argb_components(&ck_conv_info
, src_ptr
, channels
);
1674 ck_pixel
= make_argb_color(&ck_conv_info
, channels
);
1675 if (ck_pixel
== color_key
)
1676 val
&= ~conv_info
.destmask
[0];
1678 memcpy(dst_ptr
, &val
, dst_format
->bytes_per_pixel
);
1682 struct vec4 color
, tmp
;
1684 format_to_vec4(src_format
, src_ptr
, &color
);
1685 if (src_format
->to_rgba
)
1686 src_format
->to_rgba(&color
, &tmp
, palette
);
1694 format_from_vec4(ck_format
, &tmp
, (BYTE
*)&ck_pixel
);
1695 if (ck_pixel
== color_key
)
1699 if (dst_format
->from_rgba
)
1700 dst_format
->from_rgba(&tmp
, &color
);
1704 format_from_vec4(dst_format
, &color
, dst_ptr
);
1707 src_ptr
+= src_format
->bytes_per_pixel
;
1708 dst_ptr
+= dst_format
->bytes_per_pixel
;
1711 if (src_size
->width
< dst_size
->width
) /* black out remaining pixels */
1712 memset(dst_ptr
, 0, dst_format
->bytes_per_pixel
* (dst_size
->width
- src_size
->width
));
1715 if (src_size
->height
< dst_size
->height
) /* black out remaining pixels */
1716 memset(dst
+ src_size
->height
* dst_row_pitch
, 0, dst_row_pitch
* (dst_size
->height
- src_size
->height
));
1718 if (src_size
->depth
< dst_size
->depth
) /* black out remaining pixels */
1719 memset(dst
+ src_size
->depth
* dst_slice_pitch
, 0, dst_slice_pitch
* (dst_size
->depth
- src_size
->depth
));
1722 /************************************************************
1723 * point_filter_argb_pixels
1725 * Copies the source buffer to the destination buffer, performing
1726 * any necessary format conversion, color keying and stretching
1727 * using a point filter.
1729 void point_filter_argb_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
, const struct volume
*src_size
,
1730 const struct pixel_format_desc
*src_format
, BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
,
1731 const struct volume
*dst_size
, const struct pixel_format_desc
*dst_format
, D3DCOLOR color_key
,
1732 const PALETTEENTRY
*palette
)
1734 struct argb_conversion_info conv_info
, ck_conv_info
;
1735 const struct pixel_format_desc
*ck_format
= NULL
;
1739 ZeroMemory(channels
, sizeof(channels
));
1740 init_argb_conversion_info(src_format
, dst_format
, &conv_info
);
1744 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1745 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1746 init_argb_conversion_info(src_format
, ck_format
, &ck_conv_info
);
1749 for (z
= 0; z
< dst_size
->depth
; z
++)
1751 BYTE
*dst_slice_ptr
= dst
+ z
* dst_slice_pitch
;
1752 const BYTE
*src_slice_ptr
= src
+ src_slice_pitch
* (z
* src_size
->depth
/ dst_size
->depth
);
1754 for (y
= 0; y
< dst_size
->height
; y
++)
1756 BYTE
*dst_ptr
= dst_slice_ptr
+ y
* dst_row_pitch
;
1757 const BYTE
*src_row_ptr
= src_slice_ptr
+ src_row_pitch
* (y
* src_size
->height
/ dst_size
->height
);
1759 for (x
= 0; x
< dst_size
->width
; x
++)
1761 const BYTE
*src_ptr
= src_row_ptr
+ (x
* src_size
->width
/ dst_size
->width
) * src_format
->bytes_per_pixel
;
1763 if (!src_format
->to_rgba
&& !dst_format
->from_rgba
1764 && src_format
->type
== dst_format
->type
1765 && src_format
->bytes_per_pixel
<= 4 && dst_format
->bytes_per_pixel
<= 4)
1769 get_relevant_argb_components(&conv_info
, src_ptr
, channels
);
1770 val
= make_argb_color(&conv_info
, channels
);
1776 get_relevant_argb_components(&ck_conv_info
, src_ptr
, channels
);
1777 ck_pixel
= make_argb_color(&ck_conv_info
, channels
);
1778 if (ck_pixel
== color_key
)
1779 val
&= ~conv_info
.destmask
[0];
1781 memcpy(dst_ptr
, &val
, dst_format
->bytes_per_pixel
);
1785 struct vec4 color
, tmp
;
1787 format_to_vec4(src_format
, src_ptr
, &color
);
1788 if (src_format
->to_rgba
)
1789 src_format
->to_rgba(&color
, &tmp
, palette
);
1797 format_from_vec4(ck_format
, &tmp
, (BYTE
*)&ck_pixel
);
1798 if (ck_pixel
== color_key
)
1802 if (dst_format
->from_rgba
)
1803 dst_format
->from_rgba(&tmp
, &color
);
1807 format_from_vec4(dst_format
, &color
, dst_ptr
);
1810 dst_ptr
+= dst_format
->bytes_per_pixel
;
1816 typedef BOOL (*dxtn_conversion_func
)(const BYTE
*src
, BYTE
*dst
, DWORD pitch_in
, DWORD pitch_out
,
1817 enum wined3d_format_id format
, unsigned int w
, unsigned int h
);
1819 static dxtn_conversion_func
get_dxtn_conversion_func(D3DFORMAT format
, BOOL encode
)
1824 if (!wined3d_dxtn_supported()) return NULL
;
1825 return encode
? wined3d_dxt1_encode
: wined3d_dxt1_decode
;
1827 if (!wined3d_dxtn_supported()) return NULL
;
1828 return encode
? wined3d_dxt3_encode
: wined3d_dxt3_decode
;
1830 if (!wined3d_dxtn_supported()) return NULL
;
1831 return encode
? wined3d_dxt5_encode
: wined3d_dxt5_decode
;
1837 /************************************************************
1838 * D3DXLoadSurfaceFromMemory
1840 * Loads data from a given memory chunk into a surface,
1841 * applying any of the specified filters.
1844 * pDestSurface [I] pointer to the surface
1845 * pDestPalette [I] palette to use
1846 * pDestRect [I] to be filled area of the surface
1847 * pSrcMemory [I] pointer to the source data
1848 * SrcFormat [I] format of the source pixel data
1849 * SrcPitch [I] number of bytes in a row
1850 * pSrcPalette [I] palette used in the source image
1851 * pSrcRect [I] area of the source data to load
1852 * dwFilter [I] filter to apply on stretching
1853 * Colorkey [I] colorkey
1856 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1857 * if pSrcMemory is NULL but the other parameters are valid
1858 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect is NULL or
1859 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1860 * if DestRect is invalid
1861 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1862 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1865 * pSrcRect specifies the dimensions of the source data;
1866 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1869 HRESULT WINAPI
D3DXLoadSurfaceFromMemory(IDirect3DSurface9
*dst_surface
,
1870 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const void *src_memory
,
1871 D3DFORMAT src_format
, UINT src_pitch
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
,
1872 DWORD filter
, D3DCOLOR color_key
)
1874 const struct pixel_format_desc
*srcformatdesc
, *destformatdesc
;
1875 D3DSURFACE_DESC surfdesc
;
1876 D3DLOCKED_RECT lockrect
;
1877 struct volume src_size
, dst_size
;
1878 HRESULT ret
= D3D_OK
;
1880 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s, %#x, 0x%08x)\n",
1881 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_memory
, src_format
,
1882 src_pitch
, src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1884 if (!dst_surface
|| !src_memory
|| !src_rect
)
1886 WARN("Invalid argument specified.\n");
1887 return D3DERR_INVALIDCALL
;
1889 if (src_format
== D3DFMT_UNKNOWN
1890 || src_rect
->left
>= src_rect
->right
1891 || src_rect
->top
>= src_rect
->bottom
)
1893 WARN("Invalid src_format or src_rect.\n");
1897 if (filter
== D3DX_DEFAULT
)
1898 filter
= D3DX_FILTER_TRIANGLE
| D3DX_FILTER_DITHER
;
1900 IDirect3DSurface9_GetDesc(dst_surface
, &surfdesc
);
1902 src_size
.width
= src_rect
->right
- src_rect
->left
;
1903 src_size
.height
= src_rect
->bottom
- src_rect
->top
;
1907 dst_size
.width
= surfdesc
.Width
;
1908 dst_size
.height
= surfdesc
.Height
;
1912 if (dst_rect
->left
> dst_rect
->right
|| dst_rect
->right
> surfdesc
.Width
1913 || dst_rect
->top
> dst_rect
->bottom
|| dst_rect
->bottom
> surfdesc
.Height
1914 || dst_rect
->left
< 0 || dst_rect
->top
< 0)
1916 WARN("Invalid dst_rect specified.\n");
1917 return D3DERR_INVALIDCALL
;
1919 dst_size
.width
= dst_rect
->right
- dst_rect
->left
;
1920 dst_size
.height
= dst_rect
->bottom
- dst_rect
->top
;
1921 if (!dst_size
.width
|| !dst_size
.height
)
1926 srcformatdesc
= get_format_info(src_format
);
1927 destformatdesc
= get_format_info(surfdesc
.Format
);
1928 if (srcformatdesc
->type
== FORMAT_UNKNOWN
|| destformatdesc
->type
== FORMAT_UNKNOWN
)
1930 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format
, surfdesc
.Format
);
1934 if (src_format
== surfdesc
.Format
1935 && dst_size
.width
== src_size
.width
1936 && dst_size
.height
== src_size
.height
1937 && color_key
== 0) /* Simple copy. */
1939 if (src_rect
->left
& (srcformatdesc
->block_width
- 1)
1940 || src_rect
->top
& (srcformatdesc
->block_height
- 1)
1941 || (src_rect
->right
& (srcformatdesc
->block_width
- 1)
1942 && src_size
.width
!= surfdesc
.Width
)
1943 || (src_rect
->bottom
& (srcformatdesc
->block_height
- 1)
1944 && src_size
.height
!= surfdesc
.Height
))
1946 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect
));
1947 return D3DXERR_INVALIDDATA
;
1950 if (FAILED(IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1951 return D3DXERR_INVALIDDATA
;
1953 copy_pixels(src_memory
, src_pitch
, 0, lockrect
.pBits
, lockrect
.Pitch
, 0,
1954 &src_size
, srcformatdesc
);
1956 IDirect3DSurface9_UnlockRect(dst_surface
);
1958 else /* Stretching or format conversion. */
1960 dxtn_conversion_func pre_convert
, post_convert
;
1961 void *tmp_src_memory
= NULL
, *tmp_dst_memory
= NULL
;
1962 UINT tmp_src_pitch
, tmp_dst_pitch
;
1964 pre_convert
= get_dxtn_conversion_func(srcformatdesc
->format
, FALSE
);
1965 post_convert
= get_dxtn_conversion_func(destformatdesc
->format
, TRUE
);
1967 if ((!pre_convert
&& !is_conversion_from_supported(srcformatdesc
)) ||
1968 (!post_convert
&& !is_conversion_to_supported(destformatdesc
)))
1970 FIXME("Unsupported format conversion %#x -> %#x.\n", src_format
, surfdesc
.Format
);
1974 if (FAILED(IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1975 return D3DXERR_INVALIDDATA
;
1977 /* handle pre-conversion */
1980 tmp_src_memory
= HeapAlloc(GetProcessHeap(), 0, src_size
.width
* src_size
.height
* sizeof(DWORD
));
1981 if (!tmp_src_memory
)
1983 ret
= E_OUTOFMEMORY
;
1986 tmp_src_pitch
= src_size
.width
* sizeof(DWORD
);
1987 if (!pre_convert(src_memory
, tmp_src_memory
, src_pitch
, tmp_src_pitch
,
1988 WINED3DFMT_B8G8R8A8_UNORM
, src_size
.width
, src_size
.height
))
1993 srcformatdesc
= get_format_info(D3DFMT_A8R8G8B8
);
1997 tmp_src_memory
= (void *)src_memory
;
1998 tmp_src_pitch
= src_pitch
;
2001 /* handle post-conversion */
2004 tmp_dst_memory
= HeapAlloc(GetProcessHeap(), 0, dst_size
.width
* dst_size
.height
* sizeof(DWORD
));
2005 if (!tmp_dst_memory
)
2007 ret
= E_OUTOFMEMORY
;
2010 tmp_dst_pitch
= dst_size
.width
* sizeof(DWORD
);
2011 destformatdesc
= get_format_info(D3DFMT_A8R8G8B8
);
2015 tmp_dst_memory
= lockrect
.pBits
;
2016 tmp_dst_pitch
= lockrect
.Pitch
;
2019 if ((filter
& 0xf) == D3DX_FILTER_NONE
)
2021 convert_argb_pixels(tmp_src_memory
, tmp_src_pitch
, 0, &src_size
, srcformatdesc
,
2022 tmp_dst_memory
, tmp_dst_pitch
, 0, &dst_size
, destformatdesc
, color_key
, src_palette
);
2024 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
2026 if ((filter
& 0xf) != D3DX_FILTER_POINT
)
2027 FIXME("Unhandled filter %#x.\n", filter
);
2029 /* Always apply a point filter until D3DX_FILTER_LINEAR,
2030 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
2031 point_filter_argb_pixels(tmp_src_memory
, tmp_src_pitch
, 0, &src_size
, srcformatdesc
,
2032 tmp_dst_memory
, tmp_dst_pitch
, 0, &dst_size
, destformatdesc
, color_key
, src_palette
);
2035 /* handle post-conversion */
2038 if (!post_convert(tmp_dst_memory
, lockrect
.pBits
, tmp_dst_pitch
, lockrect
.Pitch
,
2039 WINED3DFMT_B8G8R8A8_UNORM
, dst_size
.width
, dst_size
.height
))
2048 HeapFree(GetProcessHeap(), 0, tmp_src_memory
);
2050 HeapFree(GetProcessHeap(), 0, tmp_dst_memory
);
2051 IDirect3DSurface9_UnlockRect(dst_surface
);
2057 /************************************************************
2058 * D3DXLoadSurfaceFromSurface
2060 * Copies the contents from one surface to another, performing any required
2061 * format conversion, resizing or filtering.
2064 * pDestSurface [I] pointer to the destination surface
2065 * pDestPalette [I] palette to use
2066 * pDestRect [I] to be filled area of the surface
2067 * pSrcSurface [I] pointer to the source surface
2068 * pSrcPalette [I] palette used for the source surface
2069 * pSrcRect [I] area of the source data to load
2070 * dwFilter [I] filter to apply on resizing
2071 * Colorkey [I] any ARGB value or 0 to disable color-keying
2075 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface is NULL
2076 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
2079 HRESULT WINAPI
D3DXLoadSurfaceFromSurface(IDirect3DSurface9
*dst_surface
,
2080 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, IDirect3DSurface9
*src_surface
,
2081 const PALETTEENTRY
*src_palette
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
)
2084 D3DLOCKED_RECT lock
;
2085 D3DSURFACE_DESC SrcDesc
;
2088 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
2089 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
2090 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_surface
,
2091 src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
2093 if (!dst_surface
|| !src_surface
)
2094 return D3DERR_INVALIDCALL
;
2096 IDirect3DSurface9_GetDesc(src_surface
, &SrcDesc
);
2099 SetRect(&rect
, 0, 0, SrcDesc
.Width
, SrcDesc
.Height
);
2103 if (FAILED(IDirect3DSurface9_LockRect(src_surface
, &lock
, NULL
, D3DLOCK_READONLY
)))
2104 return D3DXERR_INVALIDDATA
;
2106 hr
= D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
,
2107 lock
.pBits
, SrcDesc
.Format
, lock
.Pitch
, src_palette
, &rect
, filter
, color_key
);
2109 IDirect3DSurface9_UnlockRect(src_surface
);
2115 HRESULT WINAPI
D3DXSaveSurfaceToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
2116 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
2121 ID3DXBuffer
*buffer
;
2123 TRACE("(%s, %#x, %p, %p, %s): relay\n",
2124 wine_dbgstr_a(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
2126 if (!dst_filename
) return D3DERR_INVALIDCALL
;
2128 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
2129 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2130 if (!filename
) return E_OUTOFMEMORY
;
2131 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
2133 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
2136 hr
= write_buffer_to_file(filename
, buffer
);
2137 ID3DXBuffer_Release(buffer
);
2140 HeapFree(GetProcessHeap(), 0, filename
);
2144 HRESULT WINAPI
D3DXSaveSurfaceToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
2145 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
2148 ID3DXBuffer
*buffer
;
2150 TRACE("(%s, %#x, %p, %p, %s): relay\n",
2151 wine_dbgstr_w(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
2153 if (!dst_filename
) return D3DERR_INVALIDCALL
;
2155 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
2158 hr
= write_buffer_to_file(dst_filename
, buffer
);
2159 ID3DXBuffer_Release(buffer
);
2165 HRESULT WINAPI
D3DXSaveSurfaceToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
2166 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
2168 IWICBitmapEncoder
*encoder
= NULL
;
2169 IWICBitmapFrameEncode
*frame
= NULL
;
2170 IPropertyBag2
*encoder_options
= NULL
;
2171 IStream
*stream
= NULL
;
2174 const CLSID
*encoder_clsid
;
2175 const GUID
*pixel_format_guid
;
2176 WICPixelFormatGUID wic_pixel_format
;
2177 D3DFORMAT d3d_pixel_format
;
2178 D3DSURFACE_DESC src_surface_desc
;
2179 D3DLOCKED_RECT locked_rect
;
2181 STATSTG stream_stats
;
2182 HGLOBAL stream_hglobal
;
2183 ID3DXBuffer
*buffer
;
2186 TRACE("(%p, %#x, %p, %p, %s)\n",
2187 dst_buffer
, file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
2189 if (!dst_buffer
|| !src_surface
) return D3DERR_INVALIDCALL
;
2193 FIXME("Saving surfaces with palettized pixel formats is not implemented yet\n");
2194 return D3DERR_INVALIDCALL
;
2197 switch (file_format
)
2201 encoder_clsid
= &CLSID_WICBmpEncoder
;
2204 encoder_clsid
= &CLSID_WICPngEncoder
;
2207 encoder_clsid
= &CLSID_WICJpegEncoder
;
2210 return save_dds_surface_to_memory(dst_buffer
, src_surface
, src_rect
);
2215 FIXME("File format %#x is not supported yet\n", file_format
);
2218 return D3DERR_INVALIDCALL
;
2221 IDirect3DSurface9_GetDesc(src_surface
, &src_surface_desc
);
2224 if (src_rect
->left
== src_rect
->right
|| src_rect
->top
== src_rect
->bottom
)
2226 WARN("Invalid rectangle with 0 area\n");
2227 return D3DXCreateBuffer(64, dst_buffer
);
2229 if (src_rect
->left
< 0 || src_rect
->top
< 0)
2230 return D3DERR_INVALIDCALL
;
2231 if (src_rect
->left
> src_rect
->right
|| src_rect
->top
> src_rect
->bottom
)
2232 return D3DERR_INVALIDCALL
;
2233 if (src_rect
->right
> src_surface_desc
.Width
|| src_rect
->bottom
> src_surface_desc
.Height
)
2234 return D3DERR_INVALIDCALL
;
2236 width
= src_rect
->right
- src_rect
->left
;
2237 height
= src_rect
->bottom
- src_rect
->top
;
2241 width
= src_surface_desc
.Width
;
2242 height
= src_surface_desc
.Height
;
2245 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
2247 hr
= CoCreateInstance(encoder_clsid
, NULL
, CLSCTX_INPROC_SERVER
,
2248 &IID_IWICBitmapEncoder
, (void **)&encoder
);
2249 if (FAILED(hr
)) goto cleanup_err
;
2251 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &stream
);
2252 if (FAILED(hr
)) goto cleanup_err
;
2254 hr
= IWICBitmapEncoder_Initialize(encoder
, stream
, WICBitmapEncoderNoCache
);
2255 if (FAILED(hr
)) goto cleanup_err
;
2257 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frame
, &encoder_options
);
2258 if (FAILED(hr
)) goto cleanup_err
;
2260 hr
= IWICBitmapFrameEncode_Initialize(frame
, encoder_options
);
2261 if (FAILED(hr
)) goto cleanup_err
;
2263 hr
= IWICBitmapFrameEncode_SetSize(frame
, width
, height
);
2264 if (FAILED(hr
)) goto cleanup_err
;
2266 pixel_format_guid
= d3dformat_to_wic_guid(src_surface_desc
.Format
);
2267 if (!pixel_format_guid
)
2269 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc
.Format
);
2274 memcpy(&wic_pixel_format
, pixel_format_guid
, sizeof(GUID
));
2275 hr
= IWICBitmapFrameEncode_SetPixelFormat(frame
, &wic_pixel_format
);
2276 d3d_pixel_format
= wic_guid_to_d3dformat(&wic_pixel_format
);
2277 if (SUCCEEDED(hr
) && d3d_pixel_format
!= D3DFMT_UNKNOWN
)
2279 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format
), d3d_pixel_format
);
2281 if (src_surface_desc
.Format
== d3d_pixel_format
) /* Simple copy */
2283 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
2286 IWICBitmapFrameEncode_WritePixels(frame
, height
,
2287 locked_rect
.Pitch
, height
* locked_rect
.Pitch
, locked_rect
.pBits
);
2288 IDirect3DSurface9_UnlockRect(src_surface
);
2291 else /* Pixel format conversion */
2293 const struct pixel_format_desc
*src_format_desc
, *dst_format_desc
;
2298 src_format_desc
= get_format_info(src_surface_desc
.Format
);
2299 dst_format_desc
= get_format_info(d3d_pixel_format
);
2300 if (!is_conversion_from_supported(src_format_desc
)
2301 || !is_conversion_to_supported(dst_format_desc
))
2303 FIXME("Unsupported format conversion %#x -> %#x.\n",
2304 src_surface_desc
.Format
, d3d_pixel_format
);
2310 size
.height
= height
;
2312 dst_pitch
= width
* dst_format_desc
->bytes_per_pixel
;
2313 dst_data
= HeapAlloc(GetProcessHeap(), 0, dst_pitch
* height
);
2320 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
2323 convert_argb_pixels(locked_rect
.pBits
, locked_rect
.Pitch
, 0, &size
, src_format_desc
,
2324 dst_data
, dst_pitch
, 0, &size
, dst_format_desc
, 0, NULL
);
2325 IDirect3DSurface9_UnlockRect(src_surface
);
2328 IWICBitmapFrameEncode_WritePixels(frame
, height
, dst_pitch
, dst_pitch
* height
, dst_data
);
2329 HeapFree(GetProcessHeap(), 0, dst_data
);
2332 hr
= IWICBitmapFrameEncode_Commit(frame
);
2333 if (SUCCEEDED(hr
)) hr
= IWICBitmapEncoder_Commit(encoder
);
2335 else WARN("Unsupported pixel format %#x\n", src_surface_desc
.Format
);
2337 /* copy data from stream to ID3DXBuffer */
2338 hr
= IStream_Stat(stream
, &stream_stats
, STATFLAG_NONAME
);
2339 if (FAILED(hr
)) goto cleanup_err
;
2341 if (stream_stats
.cbSize
.u
.HighPart
!= 0)
2343 hr
= D3DXERR_INVALIDDATA
;
2346 size
= stream_stats
.cbSize
.u
.LowPart
;
2348 /* Remove BMP header for DIB */
2349 if (file_format
== D3DXIFF_DIB
)
2350 size
-= sizeof(BITMAPFILEHEADER
);
2352 hr
= D3DXCreateBuffer(size
, &buffer
);
2353 if (FAILED(hr
)) goto cleanup
;
2355 hr
= GetHGlobalFromStream(stream
, &stream_hglobal
);
2358 void *buffer_pointer
= ID3DXBuffer_GetBufferPointer(buffer
);
2359 void *stream_data
= GlobalLock(stream_hglobal
);
2360 /* Remove BMP header for DIB */
2361 if (file_format
== D3DXIFF_DIB
)
2362 stream_data
= (void*)((BYTE
*)stream_data
+ sizeof(BITMAPFILEHEADER
));
2363 memcpy(buffer_pointer
, stream_data
, size
);
2364 GlobalUnlock(stream_hglobal
);
2365 *dst_buffer
= buffer
;
2367 else ID3DXBuffer_Release(buffer
);
2370 if (FAILED(hr
) && hr
!= E_OUTOFMEMORY
)
2371 hr
= D3DERR_INVALIDCALL
;
2374 if (stream
) IStream_Release(stream
);
2376 if (frame
) IWICBitmapFrameEncode_Release(frame
);
2377 if (encoder_options
) IPropertyBag2_Release(encoder_options
);
2379 if (encoder
) IWICBitmapEncoder_Release(encoder
);
2381 if (SUCCEEDED(initresult
)) CoUninitialize();