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_L8
},
41 { &GUID_WICPixelFormat1bppIndexed
, D3DFMT_L8
},
42 { &GUID_WICPixelFormat4bppIndexed
, D3DFMT_L8
},
43 { &GUID_WICPixelFormat16bppBGR555
, D3DFMT_X1R5G5B5
},
44 { &GUID_WICPixelFormat16bppBGR565
, D3DFMT_R5G6B5
},
45 { &GUID_WICPixelFormat24bppBGR
, D3DFMT_R8G8B8
},
46 { &GUID_WICPixelFormat32bppBGR
, D3DFMT_X8R8G8B8
},
47 { &GUID_WICPixelFormat32bppBGRA
, D3DFMT_A8R8G8B8
}
50 static D3DFORMAT
wic_guid_to_d3dformat(const GUID
*guid
)
54 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
56 if (IsEqualGUID(wic_pixel_formats
[i
].wic_guid
, guid
))
57 return wic_pixel_formats
[i
].d3dformat
;
60 return D3DFMT_UNKNOWN
;
63 static const GUID
*d3dformat_to_wic_guid(D3DFORMAT format
)
67 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
69 if (wic_pixel_formats
[i
].d3dformat
== format
)
70 return wic_pixel_formats
[i
].wic_guid
;
76 /* dds_header.flags */
78 #define DDS_HEIGHT 0x2
81 #define DDS_PIXELFORMAT 0x1000
82 #define DDS_MIPMAPCOUNT 0x20000
83 #define DDS_LINEARSIZE 0x80000
84 #define DDS_DEPTH 0x800000
87 #define DDS_CAPS_COMPLEX 0x8
88 #define DDS_CAPS_TEXTURE 0x1000
89 #define DDS_CAPS_MIPMAP 0x400000
91 /* dds_header.caps2 */
92 #define DDS_CAPS2_CUBEMAP 0x200
93 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
94 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
95 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
96 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
97 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
98 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
99 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
100 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
101 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
102 #define DDS_CAPS2_VOLUME 0x200000
104 /* dds_pixel_format.flags */
105 #define DDS_PF_ALPHA 0x1
106 #define DDS_PF_ALPHA_ONLY 0x2
107 #define DDS_PF_FOURCC 0x4
108 #define DDS_PF_RGB 0x40
109 #define DDS_PF_YUV 0x200
110 #define DDS_PF_LUMINANCE 0x20000
111 #define DDS_PF_BUMPDUDV 0x80000
113 struct dds_pixel_format
132 DWORD pitch_or_linear_size
;
136 struct dds_pixel_format pixel_format
;
144 static D3DFORMAT
dds_fourcc_to_d3dformat(DWORD fourcc
)
147 static const DWORD known_fourcc
[] = {
148 MAKEFOURCC('U','Y','V','Y'),
149 MAKEFOURCC('Y','U','Y','2'),
150 MAKEFOURCC('R','G','B','G'),
151 MAKEFOURCC('G','R','G','B'),
152 MAKEFOURCC('D','X','T','1'),
153 MAKEFOURCC('D','X','T','2'),
154 MAKEFOURCC('D','X','T','3'),
155 MAKEFOURCC('D','X','T','4'),
156 MAKEFOURCC('D','X','T','5')
159 for (i
= 0; i
< sizeof(known_fourcc
) / sizeof(known_fourcc
[0]); i
++)
161 if (known_fourcc
[i
] == fourcc
)
165 WARN("Unknown FourCC %#x\n", fourcc
);
166 return D3DFMT_UNKNOWN
;
169 static D3DFORMAT
dds_rgb_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
172 static const struct {
179 } rgb_pixel_formats
[] = {
180 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2
},
181 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5
},
182 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5
},
183 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5
},
184 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4
},
185 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4
},
186 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2
},
187 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8
},
188 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8
},
189 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8
},
190 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10
},
191 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10
},
192 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16
},
193 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8
},
194 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8
},
197 for (i
= 0; i
< sizeof(rgb_pixel_formats
) / sizeof(rgb_pixel_formats
[0]); i
++)
199 if (rgb_pixel_formats
[i
].bpp
== pixel_format
->bpp
200 && rgb_pixel_formats
[i
].rmask
== pixel_format
->rmask
201 && rgb_pixel_formats
[i
].gmask
== pixel_format
->gmask
202 && rgb_pixel_formats
[i
].bmask
== pixel_format
->bmask
)
204 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && rgb_pixel_formats
[i
].amask
== pixel_format
->amask
)
205 return rgb_pixel_formats
[i
].format
;
206 if (rgb_pixel_formats
[i
].amask
== 0)
207 return rgb_pixel_formats
[i
].format
;
211 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
212 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
213 return D3DFMT_UNKNOWN
;
216 static D3DFORMAT
dds_luminance_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
218 if (pixel_format
->bpp
== 8)
220 if (pixel_format
->rmask
== 0xff)
222 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x0f && pixel_format
->amask
== 0xf0)
225 if (pixel_format
->bpp
== 16)
227 if (pixel_format
->rmask
== 0xffff)
229 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x00ff && pixel_format
->amask
== 0xff00)
233 WARN("Unknown luminance pixel format (bpp %#x, l %#x, a %#x)\n",
234 pixel_format
->bpp
, pixel_format
->rmask
, pixel_format
->amask
);
235 return D3DFMT_UNKNOWN
;
238 static D3DFORMAT
dds_alpha_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
240 if (pixel_format
->bpp
== 8 && pixel_format
->amask
== 0xff)
243 WARN("Unknown Alpha pixel format (%#x, %#x)\n", pixel_format
->bpp
, pixel_format
->rmask
);
244 return D3DFMT_UNKNOWN
;
247 static D3DFORMAT
dds_bump_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
249 if (pixel_format
->bpp
== 16 && pixel_format
->rmask
== 0x00ff && pixel_format
->gmask
== 0xff00)
251 if (pixel_format
->bpp
== 32 && pixel_format
->rmask
== 0x0000ffff && pixel_format
->gmask
== 0xffff0000)
252 return D3DFMT_V16U16
;
254 WARN("Unknown bump pixel format (%#x, %#x, %#x, %#x, %#x)\n", pixel_format
->bpp
,
255 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
256 return D3DFMT_UNKNOWN
;
259 static D3DFORMAT
dds_pixel_format_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
261 if (pixel_format
->flags
& DDS_PF_FOURCC
)
262 return dds_fourcc_to_d3dformat(pixel_format
->fourcc
);
263 if (pixel_format
->flags
& DDS_PF_RGB
)
264 return dds_rgb_to_d3dformat(pixel_format
);
265 if (pixel_format
->flags
& DDS_PF_LUMINANCE
)
266 return dds_luminance_to_d3dformat(pixel_format
);
267 if (pixel_format
->flags
& DDS_PF_ALPHA_ONLY
)
268 return dds_alpha_to_d3dformat(pixel_format
);
269 if (pixel_format
->flags
& DDS_PF_BUMPDUDV
)
270 return dds_bump_to_d3dformat(pixel_format
);
272 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %#x, r %#x, g %#x, b %#x, a %#x)\n",
273 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
,
274 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
275 return D3DFMT_UNKNOWN
;
278 static HRESULT
calculate_dds_surface_size(const D3DXIMAGE_INFO
*img_info
,
279 UINT width
, UINT height
, UINT
*pitch
, UINT
*size
)
281 const PixelFormatDesc
*format_desc
= get_format_info(img_info
->Format
);
282 if (format_desc
->format
== D3DFMT_UNKNOWN
)
285 if (format_desc
->block_width
!= 1 || format_desc
->block_height
!= 1)
287 *pitch
= format_desc
->block_byte_count
288 * max(1, (width
+ format_desc
->block_width
- 1) / format_desc
->block_width
);
290 * max(1, (height
+ format_desc
->block_height
- 1) / format_desc
->block_height
);
294 *pitch
= width
* format_desc
->bytes_per_pixel
;
295 *size
= *pitch
* height
;
301 /************************************************************
302 * get_image_info_from_dds
304 * Fills a D3DXIMAGE_INFO structure with information
305 * about a DDS file stored in the memory.
308 * buffer [I] pointer to DDS data
309 * length [I] size of DDS data
310 * info [O] pointer to D3DXIMAGE_INFO structure
314 * Failure: D3DXERR_INVALIDDATA
317 static HRESULT
get_image_info_from_dds(const void *buffer
, UINT length
, D3DXIMAGE_INFO
*info
)
322 const struct dds_header
*header
= buffer
;
323 UINT expected_length
= 0;
325 if (length
< sizeof(*header
) || !info
)
326 return D3DXERR_INVALIDDATA
;
328 if (header
->pixel_format
.size
!= sizeof(header
->pixel_format
))
329 return D3DXERR_INVALIDDATA
;
331 info
->Width
= header
->width
;
332 info
->Height
= header
->height
;
334 info
->MipLevels
= (header
->flags
& DDS_MIPMAPCOUNT
) ? header
->miplevels
: 1;
336 info
->Format
= dds_pixel_format_to_d3dformat(&header
->pixel_format
);
337 if (info
->Format
== D3DFMT_UNKNOWN
)
338 return D3DXERR_INVALIDDATA
;
340 TRACE("Pixel format is %#x\n", info
->Format
);
342 if (header
->caps2
& DDS_CAPS2_VOLUME
)
344 info
->Depth
= header
->depth
;
345 info
->ResourceType
= D3DRTYPE_VOLUMETEXTURE
;
347 else if (header
->caps2
& DDS_CAPS2_CUBEMAP
)
350 for (face
= DDS_CAPS2_CUBEMAP_POSITIVEX
; face
<= DDS_CAPS2_CUBEMAP_NEGATIVEZ
; face
<<= 1)
352 if (header
->caps2
& face
)
355 info
->ResourceType
= D3DRTYPE_CUBETEXTURE
;
360 info
->ResourceType
= D3DRTYPE_TEXTURE
;
363 /* calculate the expected length */
365 height
= info
->Height
;
366 for (i
= 0; i
< info
->MipLevels
; i
++)
368 UINT pitch
, size
= 0;
369 calculate_dds_surface_size(info
, width
, height
, &pitch
, &size
);
370 expected_length
+= size
;
371 width
= max(1, width
/ 2);
372 height
= max(1, height
/ 2);
375 expected_length
*= faces
;
376 expected_length
+= sizeof(*header
);
377 if (length
< expected_length
)
379 WARN("File is too short %u, expected at least %u bytes\n", length
, expected_length
);
380 return D3DXERR_INVALIDDATA
;
383 info
->ImageFileFormat
= D3DXIFF_DDS
;
388 static HRESULT
load_surface_from_dds(IDirect3DSurface9
*dst_surface
, const PALETTEENTRY
*dst_palette
,
389 const RECT
*dst_rect
, const void *src_data
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
,
390 const D3DXIMAGE_INFO
*src_info
)
394 const struct dds_header
*header
= src_data
;
395 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
397 if (src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
398 return D3DXERR_INVALIDDATA
;
400 if (FAILED(calculate_dds_surface_size(src_info
, src_info
->Width
, src_info
->Height
, &src_pitch
, &size
)))
403 return D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
, pixels
, src_info
->Format
,
404 src_pitch
, NULL
, src_rect
, filter
, color_key
);
407 HRESULT
load_texture_from_dds(IDirect3DTexture9
*texture
, const void *src_data
, const PALETTEENTRY
*palette
,
408 DWORD filter
, D3DCOLOR color_key
, const D3DXIMAGE_INFO
*src_info
)
418 IDirect3DSurface9
*surface
;
419 const struct dds_header
*header
= src_data
;
420 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
422 if (src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
423 return D3DXERR_INVALIDDATA
;
425 width
= src_info
->Width
;
426 height
= src_info
->Height
;
427 mip_levels
= min(src_info
->MipLevels
, IDirect3DTexture9_GetLevelCount(texture
));
428 for (mip_level
= 0; mip_level
< mip_levels
; mip_level
++)
430 hr
= calculate_dds_surface_size(src_info
, width
, height
, &src_pitch
, &mip_level_size
);
431 if (FAILED(hr
)) return hr
;
433 SetRect(&src_rect
, 0, 0, width
, height
);
435 IDirect3DTexture9_GetSurfaceLevel(texture
, mip_level
, &surface
);
436 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
437 NULL
, &src_rect
, filter
, color_key
);
438 IDirect3DSurface9_Release(surface
);
439 if (FAILED(hr
)) return hr
;
441 pixels
+= mip_level_size
;
442 width
= max(1, width
/ 2);
443 height
= max(1, height
/ 2);
449 HRESULT
load_cube_texture_from_dds(IDirect3DCubeTexture9
*cube_texture
, const void *src_data
,
450 const PALETTEENTRY
*palette
, DWORD filter
, DWORD color_key
, const D3DXIMAGE_INFO
*src_info
)
460 IDirect3DSurface9
*surface
;
461 const struct dds_header
*header
= src_data
;
462 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
464 if (src_info
->ResourceType
!= D3DRTYPE_CUBETEXTURE
)
465 return D3DXERR_INVALIDDATA
;
467 if ((header
->caps2
& DDS_CAPS2_CUBEMAP_ALL_FACES
) != DDS_CAPS2_CUBEMAP_ALL_FACES
)
469 WARN("Only full cubemaps are supported\n");
470 return D3DXERR_INVALIDDATA
;
473 mip_levels
= min(src_info
->MipLevels
, IDirect3DCubeTexture9_GetLevelCount(cube_texture
));
474 for (face
= D3DCUBEMAP_FACE_POSITIVE_X
; face
<= D3DCUBEMAP_FACE_NEGATIVE_Z
; face
++)
476 size
= src_info
->Width
;
477 for (mip_level
= 0; mip_level
< src_info
->MipLevels
; mip_level
++)
479 hr
= calculate_dds_surface_size(src_info
, size
, size
, &src_pitch
, &mip_level_size
);
480 if (FAILED(hr
)) return hr
;
482 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
483 if (mip_level
< mip_levels
)
485 SetRect(&src_rect
, 0, 0, size
, size
);
487 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture
, face
, mip_level
, &surface
);
488 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
489 NULL
, &src_rect
, filter
, color_key
);
490 IDirect3DSurface9_Release(surface
);
491 if (FAILED(hr
)) return hr
;
494 pixels
+= mip_level_size
;
495 size
= max(1, size
/ 2);
502 /************************************************************
503 * D3DXGetImageInfoFromFileInMemory
505 * Fills a D3DXIMAGE_INFO structure with info about an image
508 * data [I] pointer to the image file data
509 * datasize [I] size of the passed data
510 * info [O] pointer to the destination structure
513 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
514 * if info is NULL and data and datasize are not NULL
515 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
516 * D3DERR_INVALIDCALL, if data is NULL or
520 * datasize may be bigger than the actual file size
523 HRESULT WINAPI
D3DXGetImageInfoFromFileInMemory(LPCVOID data
, UINT datasize
, D3DXIMAGE_INFO
*info
)
525 IWICImagingFactory
*factory
;
526 IWICBitmapDecoder
*decoder
= NULL
;
531 TRACE("(%p, %d, %p)\n", data
, datasize
, info
);
533 if (!data
|| !datasize
)
534 return D3DERR_INVALIDCALL
;
539 if ((datasize
>= 4) && !strncmp(data
, "DDS ", 4)) {
540 TRACE("File type is DDS\n");
541 return get_image_info_from_dds(data
, datasize
, info
);
544 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
546 hr
= CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
);
549 IWICImagingFactory_CreateStream(factory
, &stream
);
550 IWICStream_InitializeFromMemory(stream
, (BYTE
*)data
, datasize
);
551 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
552 IStream_Release(stream
);
553 IWICImagingFactory_Release(factory
);
557 if ((datasize
>= 2) && (!strncmp(data
, "P3", 2) || !strncmp(data
, "P6", 2)))
558 FIXME("File type PPM is not supported yet\n");
559 else if ((datasize
>= 2) && !strncmp(data
, "BM", 2))
560 FIXME("File type DIB is not supported yet\n");
561 else if ((datasize
>= 10) && !strncmp(data
, "#?RADIANCE", 10))
562 FIXME("File type HDR is not supported yet\n");
563 else if ((datasize
>= 2) && (!strncmp(data
, "PF", 2) || !strncmp(data
, "Pf", 2)))
564 FIXME("File type PFM is not supported yet\n");
568 GUID container_format
;
571 hr
= IWICBitmapDecoder_GetContainerFormat(decoder
, &container_format
);
573 if (IsEqualGUID(&container_format
, &GUID_ContainerFormatBmp
)) {
574 TRACE("File type is BMP\n");
575 info
->ImageFileFormat
= D3DXIFF_BMP
;
576 } else if (IsEqualGUID(&container_format
, &GUID_ContainerFormatPng
)) {
577 TRACE("File type is PNG\n");
578 info
->ImageFileFormat
= D3DXIFF_PNG
;
579 } else if(IsEqualGUID(&container_format
, &GUID_ContainerFormatJpeg
)) {
580 TRACE("File type is JPG\n");
581 info
->ImageFileFormat
= D3DXIFF_JPG
;
582 } else if(IsEqualGUID(&container_format
, &GUID_WineContainerFormatTga
)) {
583 TRACE("File type is TGA\n");
584 info
->ImageFileFormat
= D3DXIFF_TGA
;
586 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format
));
587 hr
= D3DXERR_INVALIDDATA
;
592 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
593 if (SUCCEEDED(hr
) && !frame_count
)
594 hr
= D3DXERR_INVALIDDATA
;
597 IWICBitmapFrameDecode
*frame
= NULL
;
599 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
602 hr
= IWICBitmapFrameDecode_GetSize(frame
, &info
->Width
, &info
->Height
);
605 WICPixelFormatGUID pixel_format
;
607 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &pixel_format
);
609 info
->Format
= wic_guid_to_d3dformat(&pixel_format
);
610 if (info
->Format
== D3DFMT_UNKNOWN
) {
611 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format
));
612 hr
= D3DXERR_INVALIDDATA
;
618 IWICBitmapFrameDecode_Release(frame
);
622 info
->ResourceType
= D3DRTYPE_TEXTURE
;
627 IWICBitmapDecoder_Release(decoder
);
629 if (SUCCEEDED(initresult
))
633 TRACE("Invalid or unsupported image file\n");
634 return D3DXERR_INVALIDDATA
;
640 /************************************************************
641 * D3DXGetImageInfoFromFile
644 * Success: D3D_OK, if we successfully load a valid image file or
645 * if we successfully load a file which is no valid image and info is NULL
646 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
647 * if file is not a valid image file and info is not NULL
648 * D3DERR_INVALIDCALL, if file is NULL
651 HRESULT WINAPI
D3DXGetImageInfoFromFileA(LPCSTR file
, D3DXIMAGE_INFO
*info
)
657 TRACE("(%s, %p): relay\n", debugstr_a(file
), info
);
659 if( !file
) return D3DERR_INVALIDCALL
;
661 strlength
= MultiByteToWideChar(CP_ACP
, 0, file
, -1, NULL
, 0);
662 widename
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*widename
));
663 MultiByteToWideChar(CP_ACP
, 0, file
, -1, widename
, strlength
);
665 hr
= D3DXGetImageInfoFromFileW(widename
, info
);
666 HeapFree(GetProcessHeap(), 0, widename
);
671 HRESULT WINAPI
D3DXGetImageInfoFromFileW(LPCWSTR file
, D3DXIMAGE_INFO
*info
)
677 TRACE("(%s, %p): relay\n", debugstr_w(file
), info
);
679 if( !file
) return D3DERR_INVALIDCALL
;
681 hr
= map_view_of_file(file
, &buffer
, &size
);
682 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
684 hr
= D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
685 UnmapViewOfFile(buffer
);
690 /************************************************************
691 * D3DXGetImageInfoFromResource
694 * Success: D3D_OK, if resource is a valid image file
695 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
696 * if we fail to load resource
699 HRESULT WINAPI
D3DXGetImageInfoFromResourceA(HMODULE module
, LPCSTR resource
, D3DXIMAGE_INFO
*info
)
703 TRACE("(%p, %s, %p)\n", module
, debugstr_a(resource
), info
);
705 resinfo
= FindResourceA(module
, resource
, (LPCSTR
)RT_RCDATA
);
711 hr
= load_resource_into_memory(module
, resinfo
, &buffer
, &size
);
712 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
713 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
716 resinfo
= FindResourceA(module
, resource
, (LPCSTR
)RT_BITMAP
);
718 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
721 return D3DXERR_INVALIDDATA
;
724 HRESULT WINAPI
D3DXGetImageInfoFromResourceW(HMODULE module
, LPCWSTR resource
, D3DXIMAGE_INFO
*info
)
728 TRACE("(%p, %s, %p)\n", module
, debugstr_w(resource
), info
);
730 resinfo
= FindResourceW(module
, resource
, (LPCWSTR
)RT_RCDATA
);
736 hr
= load_resource_into_memory(module
, resinfo
, &buffer
, &size
);
737 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
738 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
741 resinfo
= FindResourceW(module
, resource
, (LPCWSTR
)RT_BITMAP
);
743 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
746 return D3DXERR_INVALIDDATA
;
749 /************************************************************
750 * D3DXLoadSurfaceFromFileInMemory
752 * Loads data from a given buffer into a surface and fills a given
753 * D3DXIMAGE_INFO structure with info about the source data.
756 * pDestSurface [I] pointer to the surface
757 * pDestPalette [I] palette to use
758 * pDestRect [I] to be filled area of the surface
759 * pSrcData [I] pointer to the source data
760 * SrcDataSize [I] size of the source data in bytes
761 * pSrcRect [I] area of the source data to load
762 * dwFilter [I] filter to apply on stretching
763 * Colorkey [I] colorkey
764 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
768 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
769 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
772 HRESULT WINAPI
D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9
*pDestSurface
,
773 const PALETTEENTRY
*pDestPalette
, const RECT
*pDestRect
, const void *pSrcData
, UINT SrcDataSize
,
774 const RECT
*pSrcRect
, DWORD dwFilter
, D3DCOLOR Colorkey
, D3DXIMAGE_INFO
*pSrcInfo
)
776 D3DXIMAGE_INFO imginfo
;
779 IWICImagingFactory
*factory
;
780 IWICBitmapDecoder
*decoder
;
781 IWICBitmapFrameDecode
*bitmapframe
;
784 const PixelFormatDesc
*formatdesc
;
788 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
789 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
790 pDestSurface
, pDestPalette
, wine_dbgstr_rect(pDestRect
), pSrcData
, SrcDataSize
,
791 wine_dbgstr_rect(pSrcRect
), dwFilter
, Colorkey
, pSrcInfo
);
793 if (!pDestSurface
|| !pSrcData
|| !SrcDataSize
)
794 return D3DERR_INVALIDCALL
;
796 hr
= D3DXGetImageInfoFromFileInMemory(pSrcData
, SrcDataSize
, &imginfo
);
803 wicrect
.X
= pSrcRect
->left
;
804 wicrect
.Y
= pSrcRect
->top
;
805 wicrect
.Width
= pSrcRect
->right
- pSrcRect
->left
;
806 wicrect
.Height
= pSrcRect
->bottom
- pSrcRect
->top
;
812 wicrect
.Width
= imginfo
.Width
;
813 wicrect
.Height
= imginfo
.Height
;
816 SetRect(&rect
, 0, 0, wicrect
.Width
, wicrect
.Height
);
818 if (imginfo
.ImageFileFormat
== D3DXIFF_DDS
)
820 hr
= load_surface_from_dds(pDestSurface
, pDestPalette
, pDestRect
, pSrcData
, &rect
,
821 dwFilter
, Colorkey
, &imginfo
);
822 if (SUCCEEDED(hr
) && pSrcInfo
)
827 CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
829 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
)))
832 if (FAILED(IWICImagingFactory_CreateStream(factory
, &stream
)))
834 IWICImagingFactory_Release(factory
);
838 IWICStream_InitializeFromMemory(stream
, (BYTE
*)pSrcData
, SrcDataSize
);
840 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
842 IStream_Release(stream
);
843 IWICImagingFactory_Release(factory
);
848 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &bitmapframe
);
853 formatdesc
= get_format_info(imginfo
.Format
);
855 if (formatdesc
->format
== D3DFMT_UNKNOWN
)
857 FIXME("Unsupported pixel format\n");
858 hr
= D3DXERR_INVALIDDATA
;
865 pitch
= formatdesc
->bytes_per_pixel
* wicrect
.Width
;
866 buffer
= HeapAlloc(GetProcessHeap(), 0, pitch
* wicrect
.Height
);
868 hr
= IWICBitmapFrameDecode_CopyPixels(bitmapframe
, &wicrect
, pitch
,
869 pitch
* wicrect
.Height
, buffer
);
873 hr
= D3DXLoadSurfaceFromMemory(pDestSurface
, pDestPalette
, pDestRect
,
874 buffer
, imginfo
.Format
, pitch
,
875 NULL
, &rect
, dwFilter
, Colorkey
);
878 HeapFree(GetProcessHeap(), 0, buffer
);
881 IWICBitmapFrameDecode_Release(bitmapframe
);
884 IWICBitmapDecoder_Release(decoder
);
890 return D3DXERR_INVALIDDATA
;
898 HRESULT WINAPI
D3DXLoadSurfaceFromFileA(IDirect3DSurface9
*dst_surface
,
899 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const char *src_file
,
900 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
906 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
907 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
908 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), debugstr_a(src_file
),
909 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
911 if (!src_file
|| !dst_surface
)
912 return D3DERR_INVALIDCALL
;
914 strlength
= MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, NULL
, 0);
915 pWidename
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*pWidename
));
916 MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, pWidename
, strlength
);
918 hr
= D3DXLoadSurfaceFromFileW(dst_surface
, dst_palette
, dst_rect
,
919 pWidename
, src_rect
, filter
, color_key
, src_info
);
920 HeapFree(GetProcessHeap(), 0, pWidename
);
925 HRESULT WINAPI
D3DXLoadSurfaceFromFileW(IDirect3DSurface9
*dst_surface
,
926 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const WCHAR
*src_file
,
927 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
933 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
934 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
935 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), debugstr_w(src_file
),
936 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
938 if (!src_file
|| !dst_surface
)
939 return D3DERR_INVALIDCALL
;
941 if (FAILED(map_view_of_file(src_file
, &data
, &data_size
)))
942 return D3DXERR_INVALIDDATA
;
944 hr
= D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
945 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
946 UnmapViewOfFile(data
);
951 HRESULT WINAPI
D3DXLoadSurfaceFromResourceA(IDirect3DSurface9
*dst_surface
,
952 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const char *resource
,
953 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
957 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
958 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
959 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_a(resource
),
960 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
963 return D3DERR_INVALIDCALL
;
965 if ((hResInfo
= FindResourceA(src_module
, resource
, (const char *)RT_RCDATA
)))
970 if (FAILED(load_resource_into_memory(src_module
, hResInfo
, &data
, &data_size
)))
971 return D3DXERR_INVALIDDATA
;
973 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
974 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
977 if ((hResInfo
= FindResourceA(src_module
, resource
, (const char *)RT_BITMAP
)))
979 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
983 return D3DXERR_INVALIDDATA
;
986 HRESULT WINAPI
D3DXLoadSurfaceFromResourceW(IDirect3DSurface9
*dst_surface
,
987 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const WCHAR
*resource
,
988 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
992 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
993 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
994 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_w(resource
),
995 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
998 return D3DERR_INVALIDCALL
;
1000 if ((hResInfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_RCDATA
)))
1005 if (FAILED(load_resource_into_memory(src_module
, hResInfo
, &data
, &data_size
)))
1006 return D3DXERR_INVALIDDATA
;
1008 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1009 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1012 if ((hResInfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_BITMAP
)))
1014 FIXME("Implement loading bitmaps from resource type RT_BITMAP.\n");
1018 return D3DXERR_INVALIDDATA
;
1022 /************************************************************
1023 * helper functions for D3DXLoadSurfaceFromMemory
1025 struct argb_conversion_info
1027 CONST PixelFormatDesc
*srcformat
;
1028 CONST PixelFormatDesc
*destformat
;
1029 DWORD srcshift
[4], destshift
[4];
1030 DWORD srcmask
[4], destmask
[4];
1031 BOOL process_channel
[4];
1035 static void init_argb_conversion_info(CONST PixelFormatDesc
*srcformat
, CONST PixelFormatDesc
*destformat
, struct argb_conversion_info
*info
)
1038 ZeroMemory(info
->process_channel
, 4 * sizeof(BOOL
));
1039 info
->channelmask
= 0;
1041 info
->srcformat
= srcformat
;
1042 info
->destformat
= destformat
;
1044 for(i
= 0;i
< 4;i
++) {
1045 /* srcshift is used to extract the _relevant_ components */
1046 info
->srcshift
[i
] = srcformat
->shift
[i
] + max( srcformat
->bits
[i
] - destformat
->bits
[i
], 0);
1048 /* destshift is used to move the components to the correct position */
1049 info
->destshift
[i
] = destformat
->shift
[i
] + max(destformat
->bits
[i
] - srcformat
->bits
[i
], 0);
1051 info
->srcmask
[i
] = ((1 << srcformat
->bits
[i
]) - 1) << srcformat
->shift
[i
];
1052 info
->destmask
[i
] = ((1 << destformat
->bits
[i
]) - 1) << destformat
->shift
[i
];
1054 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1055 if(destformat
->bits
[i
]) {
1056 if(srcformat
->bits
[i
]) info
->process_channel
[i
] = TRUE
;
1057 else info
->channelmask
|= info
->destmask
[i
];
1062 static DWORD
dword_from_bytes(CONST BYTE
*src
, UINT bytes_per_pixel
)
1065 static BOOL fixme_once
;
1067 if(bytes_per_pixel
> sizeof(DWORD
)) {
1068 if(!fixme_once
++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel
);
1069 bytes_per_pixel
= sizeof(DWORD
);
1072 memcpy(&ret
, src
, bytes_per_pixel
);
1076 static void dword_to_bytes(BYTE
*dst
, DWORD dword
, UINT bytes_per_pixel
)
1078 static BOOL fixme_once
;
1080 if(bytes_per_pixel
> sizeof(DWORD
)) {
1081 if(!fixme_once
++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel
);
1082 ZeroMemory(dst
, bytes_per_pixel
);
1083 bytes_per_pixel
= sizeof(DWORD
);
1086 memcpy(dst
, &dword
, bytes_per_pixel
);
1089 /************************************************************
1090 * get_relevant_argb_components
1092 * Extracts the relevant components from the source color and
1093 * drops the less significant bits if they aren't used by the destination format.
1095 static void get_relevant_argb_components(CONST
struct argb_conversion_info
*info
, CONST DWORD col
, DWORD
*out
)
1099 if(info
->process_channel
[i
])
1100 out
[i
] = (col
& info
->srcmask
[i
]) >> info
->srcshift
[i
];
1103 /************************************************************
1106 * Recombines the output of get_relevant_argb_components and converts
1107 * it to the destination format.
1109 static DWORD
make_argb_color(CONST
struct argb_conversion_info
*info
, CONST DWORD
*in
)
1114 for(i
= 0;i
< 4;i
++) {
1115 if(info
->process_channel
[i
]) {
1116 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1118 for(shift
= info
->destshift
[i
]; shift
> info
->destformat
->shift
[i
]; shift
-= info
->srcformat
->bits
[i
]) val
|= in
[i
] << shift
;
1119 val
|= (in
[i
] >> (info
->destformat
->shift
[i
] - shift
)) << info
->destformat
->shift
[i
];
1122 val
|= info
->channelmask
; /* new channels are set to their maximal value */
1126 static void format_to_vec4(const PixelFormatDesc
*format
, const DWORD
*src
, struct vec4
*dst
)
1130 if (format
->bits
[1])
1132 mask
= (1 << format
->bits
[1]) - 1;
1133 dst
->x
= (float)((*src
>> format
->shift
[1]) & mask
) / mask
;
1138 if (format
->bits
[2])
1140 mask
= (1 << format
->bits
[2]) - 1;
1141 dst
->y
= (float)((*src
>> format
->shift
[2]) & mask
) / mask
;
1146 if (format
->bits
[3])
1148 mask
= (1 << format
->bits
[3]) - 1;
1149 dst
->z
= (float)((*src
>> format
->shift
[3]) & mask
) / mask
;
1154 if (format
->bits
[0])
1156 mask
= (1 << format
->bits
[0]) - 1;
1157 dst
->w
= (float)((*src
>> format
->shift
[0]) & mask
) / mask
;
1163 static void format_from_vec4(const PixelFormatDesc
*format
, const struct vec4
*src
, DWORD
*dst
)
1167 if (format
->bits
[1])
1168 *dst
|= (DWORD
)(src
->x
* ((1 << format
->bits
[1]) - 1) + 0.5f
) << format
->shift
[1];
1169 if (format
->bits
[2])
1170 *dst
|= (DWORD
)(src
->y
* ((1 << format
->bits
[2]) - 1) + 0.5f
) << format
->shift
[2];
1171 if (format
->bits
[3])
1172 *dst
|= (DWORD
)(src
->z
* ((1 << format
->bits
[3]) - 1) + 0.5f
) << format
->shift
[3];
1173 if (format
->bits
[0])
1174 *dst
|= (DWORD
)(src
->w
* ((1 << format
->bits
[0]) - 1) + 0.5f
) << format
->shift
[0];
1177 /************************************************************
1180 * Copies the source buffer to the destination buffer, performing
1181 * any necessary format conversion and color keying.
1182 * Pixels outsize the source rect are blacked out.
1183 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1185 static void copy_simple_data(const BYTE
*src
, UINT srcpitch
, SIZE src_size
, const PixelFormatDesc
*srcformat
,
1186 BYTE
*dest
, UINT destpitch
, SIZE dst_size
, const PixelFormatDesc
*destformat
, D3DCOLOR colorkey
)
1188 struct argb_conversion_info conv_info
, ck_conv_info
;
1189 const PixelFormatDesc
*ck_format
= NULL
;
1190 DWORD channels
[4], pixel
;
1191 UINT minwidth
, minheight
;
1194 ZeroMemory(channels
, sizeof(channels
));
1195 init_argb_conversion_info(srcformat
, destformat
, &conv_info
);
1197 minwidth
= (src_size
.cx
< dst_size
.cx
) ? src_size
.cx
: dst_size
.cx
;
1198 minheight
= (src_size
.cy
< dst_size
.cy
) ? src_size
.cy
: dst_size
.cy
;
1202 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1203 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1204 init_argb_conversion_info(srcformat
, ck_format
, &ck_conv_info
);
1207 for(y
= 0;y
< minheight
;y
++) {
1208 const BYTE
*srcptr
= src
+ y
* srcpitch
;
1209 BYTE
*destptr
= dest
+ y
* destpitch
;
1212 for(x
= 0;x
< minwidth
;x
++) {
1213 /* extract source color components */
1214 pixel
= dword_from_bytes(srcptr
, srcformat
->bytes_per_pixel
);
1216 if (!srcformat
->to_rgba
&& !destformat
->from_rgba
)
1218 get_relevant_argb_components(&conv_info
, pixel
, channels
);
1219 val
= make_argb_color(&conv_info
, channels
);
1223 get_relevant_argb_components(&ck_conv_info
, pixel
, channels
);
1224 pixel
= make_argb_color(&ck_conv_info
, channels
);
1225 if (pixel
== colorkey
)
1226 val
&= ~conv_info
.destmask
[0];
1231 struct vec4 color
, tmp
;
1233 format_to_vec4(srcformat
, &pixel
, &color
);
1234 if (srcformat
->to_rgba
)
1235 srcformat
->to_rgba(&color
, &tmp
);
1241 format_from_vec4(ck_format
, &tmp
, &pixel
);
1242 if (pixel
== colorkey
)
1246 if (destformat
->from_rgba
)
1247 destformat
->from_rgba(&tmp
, &color
);
1251 format_from_vec4(destformat
, &color
, &val
);
1254 dword_to_bytes(destptr
, val
, destformat
->bytes_per_pixel
);
1255 srcptr
+= srcformat
->bytes_per_pixel
;
1256 destptr
+= destformat
->bytes_per_pixel
;
1259 if (src_size
.cx
< dst_size
.cx
) /* black out remaining pixels */
1260 memset(destptr
, 0, destformat
->bytes_per_pixel
* (dst_size
.cx
- src_size
.cx
));
1262 if (src_size
.cy
< dst_size
.cy
) /* black out remaining pixels */
1263 memset(dest
+ src_size
.cy
* destpitch
, 0, destpitch
* (dst_size
.cy
- src_size
.cy
));
1266 /************************************************************
1267 * point_filter_simple_data
1269 * Copies the source buffer to the destination buffer, performing
1270 * any necessary format conversion, color keying and stretching
1271 * using a point filter.
1272 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1274 static void point_filter_simple_data(const BYTE
*src
, UINT srcpitch
, SIZE src_size
, const PixelFormatDesc
*srcformat
,
1275 BYTE
*dest
, UINT destpitch
, SIZE dst_size
, const PixelFormatDesc
*destformat
, D3DCOLOR colorkey
)
1277 struct argb_conversion_info conv_info
, ck_conv_info
;
1278 const PixelFormatDesc
*ck_format
= NULL
;
1279 DWORD channels
[4], pixel
;
1283 ZeroMemory(channels
, sizeof(channels
));
1284 init_argb_conversion_info(srcformat
, destformat
, &conv_info
);
1288 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1289 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1290 init_argb_conversion_info(srcformat
, ck_format
, &ck_conv_info
);
1293 for (y
= 0; y
< dst_size
.cy
; ++y
)
1295 BYTE
*destptr
= dest
+ y
* destpitch
;
1296 const BYTE
*bufptr
= src
+ srcpitch
* (y
* src_size
.cy
/ dst_size
.cy
);
1298 for (x
= 0; x
< dst_size
.cx
; ++x
)
1300 const BYTE
*srcptr
= bufptr
+ (x
* src_size
.cx
/ dst_size
.cx
) * srcformat
->bytes_per_pixel
;
1303 /* extract source color components */
1304 pixel
= dword_from_bytes(srcptr
, srcformat
->bytes_per_pixel
);
1306 if (!srcformat
->to_rgba
&& !destformat
->from_rgba
)
1308 get_relevant_argb_components(&conv_info
, pixel
, channels
);
1309 val
= make_argb_color(&conv_info
, channels
);
1313 get_relevant_argb_components(&ck_conv_info
, pixel
, channels
);
1314 pixel
= make_argb_color(&ck_conv_info
, channels
);
1315 if (pixel
== colorkey
)
1316 val
&= ~conv_info
.destmask
[0];
1321 struct vec4 color
, tmp
;
1323 format_to_vec4(srcformat
, &pixel
, &color
);
1324 if (srcformat
->to_rgba
)
1325 srcformat
->to_rgba(&color
, &tmp
);
1331 format_from_vec4(ck_format
, &tmp
, &pixel
);
1332 if (pixel
== colorkey
)
1336 if (destformat
->from_rgba
)
1337 destformat
->from_rgba(&tmp
, &color
);
1341 format_from_vec4(destformat
, &color
, &val
);
1344 dword_to_bytes(destptr
, val
, destformat
->bytes_per_pixel
);
1345 destptr
+= destformat
->bytes_per_pixel
;
1350 /************************************************************
1351 * D3DXLoadSurfaceFromMemory
1353 * Loads data from a given memory chunk into a surface,
1354 * applying any of the specified filters.
1357 * pDestSurface [I] pointer to the surface
1358 * pDestPalette [I] palette to use
1359 * pDestRect [I] to be filled area of the surface
1360 * pSrcMemory [I] pointer to the source data
1361 * SrcFormat [I] format of the source pixel data
1362 * SrcPitch [I] number of bytes in a row
1363 * pSrcPalette [I] palette used in the source image
1364 * pSrcRect [I] area of the source data to load
1365 * dwFilter [I] filter to apply on stretching
1366 * Colorkey [I] colorkey
1369 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1370 * if pSrcMemory is NULL but the other parameters are valid
1371 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1372 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1373 * if DestRect is invalid
1374 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1375 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1378 * pSrcRect specifies the dimensions of the source data;
1379 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1382 HRESULT WINAPI
D3DXLoadSurfaceFromMemory(IDirect3DSurface9
*dst_surface
,
1383 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const void *src_memory
,
1384 D3DFORMAT src_format
, UINT src_pitch
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
,
1385 DWORD filter
, D3DCOLOR color_key
)
1387 CONST PixelFormatDesc
*srcformatdesc
, *destformatdesc
;
1388 D3DSURFACE_DESC surfdesc
;
1389 D3DLOCKED_RECT lockrect
;
1390 SIZE src_size
, dst_size
;
1393 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1394 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_memory
, src_format
,
1395 src_pitch
, src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1397 if (!dst_surface
|| !src_memory
|| !src_rect
)
1398 return D3DERR_INVALIDCALL
;
1399 if (src_format
== D3DFMT_UNKNOWN
1400 || src_rect
->left
>= src_rect
->right
1401 || src_rect
->top
>= src_rect
->bottom
)
1404 if (filter
== D3DX_DEFAULT
)
1405 filter
= D3DX_FILTER_TRIANGLE
| D3DX_FILTER_DITHER
;
1407 IDirect3DSurface9_GetDesc(dst_surface
, &surfdesc
);
1409 src_size
.cx
= src_rect
->right
- src_rect
->left
;
1410 src_size
.cy
= src_rect
->bottom
- src_rect
->top
;
1413 dst_size
.cx
= surfdesc
.Width
;
1414 dst_size
.cy
= surfdesc
.Height
;
1418 if (dst_rect
->left
> dst_rect
->right
|| dst_rect
->right
> surfdesc
.Width
)
1419 return D3DERR_INVALIDCALL
;
1420 if (dst_rect
->top
> dst_rect
->bottom
|| dst_rect
->bottom
> surfdesc
.Height
)
1421 return D3DERR_INVALIDCALL
;
1422 if (dst_rect
->left
< 0 || dst_rect
->top
< 0)
1423 return D3DERR_INVALIDCALL
;
1424 dst_size
.cx
= dst_rect
->right
- dst_rect
->left
;
1425 dst_size
.cy
= dst_rect
->bottom
- dst_rect
->top
;
1426 if (!dst_size
.cx
|| !dst_size
.cy
)
1430 srcformatdesc
= get_format_info(src_format
);
1431 if (srcformatdesc
->type
== FORMAT_UNKNOWN
)
1434 destformatdesc
= get_format_info(surfdesc
.Format
);
1435 if (destformatdesc
->type
== FORMAT_UNKNOWN
)
1438 if (src_format
== surfdesc
.Format
1439 && dst_size
.cx
== src_size
.cx
1440 && dst_size
.cy
== src_size
.cy
) /* Simple copy. */
1442 UINT row_block_count
= ((src_size
.cx
+ srcformatdesc
->block_width
- 1) / srcformatdesc
->block_width
);
1443 UINT row_count
= (src_size
.cy
+ srcformatdesc
->block_height
- 1) / srcformatdesc
->block_height
;
1444 const BYTE
*src_addr
;
1448 if (src_rect
->left
& (srcformatdesc
->block_width
- 1)
1449 || src_rect
->top
& (srcformatdesc
->block_height
- 1)
1450 || (src_rect
->right
& (srcformatdesc
->block_width
- 1)
1451 && src_size
.cx
!= surfdesc
.Width
)
1452 || (src_rect
->bottom
& (srcformatdesc
->block_height
- 1)
1453 && src_size
.cy
!= surfdesc
.Height
))
1455 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect
));
1456 return D3DXERR_INVALIDDATA
;
1459 if (FAILED(hr
= IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1460 return D3DXERR_INVALIDDATA
;
1462 src_addr
= src_memory
;
1463 src_addr
+= (src_rect
->top
/ srcformatdesc
->block_height
) * src_pitch
;
1464 src_addr
+= (src_rect
->left
/ srcformatdesc
->block_width
) * srcformatdesc
->block_byte_count
;
1465 dst_addr
= lockrect
.pBits
;
1467 for (row
= 0; row
< row_count
; ++row
)
1469 memcpy(dst_addr
, src_addr
, row_block_count
* srcformatdesc
->block_byte_count
);
1470 src_addr
+= src_pitch
;
1471 dst_addr
+= lockrect
.Pitch
;
1474 IDirect3DSurface9_UnlockRect(dst_surface
);
1476 else /* Stretching or format conversion. */
1478 if (srcformatdesc
->bytes_per_pixel
> 4)
1480 if (destformatdesc
->bytes_per_pixel
> 4)
1482 if (srcformatdesc
->block_height
!= 1 || srcformatdesc
->block_width
!= 1)
1484 if (destformatdesc
->block_height
!= 1 || destformatdesc
->block_width
!= 1)
1487 if (FAILED(hr
= IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1488 return D3DXERR_INVALIDDATA
;
1490 if ((filter
& 0xf) == D3DX_FILTER_NONE
)
1492 copy_simple_data(src_memory
, src_pitch
, src_size
, srcformatdesc
,
1493 lockrect
.pBits
, lockrect
.Pitch
, dst_size
, destformatdesc
, color_key
);
1495 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1497 if ((filter
& 0xf) != D3DX_FILTER_POINT
)
1498 FIXME("Unhandled filter %#x.\n", filter
);
1500 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1501 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1502 point_filter_simple_data(src_memory
, src_pitch
, src_size
, srcformatdesc
,
1503 lockrect
.pBits
, lockrect
.Pitch
, dst_size
, destformatdesc
, color_key
);
1506 IDirect3DSurface9_UnlockRect(dst_surface
);
1512 /************************************************************
1513 * D3DXLoadSurfaceFromSurface
1515 * Copies the contents from one surface to another, performing any required
1516 * format conversion, resizing or filtering.
1519 * pDestSurface [I] pointer to the destination surface
1520 * pDestPalette [I] palette to use
1521 * pDestRect [I] to be filled area of the surface
1522 * pSrcSurface [I] pointer to the source surface
1523 * pSrcPalette [I] palette used for the source surface
1524 * pSrcRect [I] area of the source data to load
1525 * dwFilter [I] filter to apply on resizing
1526 * Colorkey [I] any ARGB value or 0 to disable color-keying
1530 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1531 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1534 HRESULT WINAPI
D3DXLoadSurfaceFromSurface(IDirect3DSurface9
*dst_surface
,
1535 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, IDirect3DSurface9
*src_surface
,
1536 const PALETTEENTRY
*src_palette
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
)
1539 D3DLOCKED_RECT lock
;
1540 D3DSURFACE_DESC SrcDesc
;
1543 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
1544 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
1545 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_surface
,
1546 src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1548 if (!dst_surface
|| !src_surface
)
1549 return D3DERR_INVALIDCALL
;
1551 IDirect3DSurface9_GetDesc(src_surface
, &SrcDesc
);
1554 SetRect(&rect
, 0, 0, SrcDesc
.Width
, SrcDesc
.Height
);
1558 if (FAILED(IDirect3DSurface9_LockRect(src_surface
, &lock
, NULL
, D3DLOCK_READONLY
)))
1559 return D3DXERR_INVALIDDATA
;
1561 hr
= D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
,
1562 lock
.pBits
, SrcDesc
.Format
, lock
.Pitch
, src_palette
, &rect
, filter
, color_key
);
1564 IDirect3DSurface9_UnlockRect(src_surface
);
1570 HRESULT WINAPI
D3DXSaveSurfaceToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1571 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1576 ID3DXBuffer
*buffer
;
1578 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1579 wine_dbgstr_a(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
1581 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1583 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1584 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1585 if (!filename
) return E_OUTOFMEMORY
;
1586 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1588 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
1591 hr
= write_buffer_to_file(filename
, buffer
);
1592 ID3DXBuffer_Release(buffer
);
1595 HeapFree(GetProcessHeap(), 0, filename
);
1599 HRESULT WINAPI
D3DXSaveSurfaceToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1600 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1603 ID3DXBuffer
*buffer
;
1605 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1606 wine_dbgstr_w(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
1608 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1610 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
1613 hr
= write_buffer_to_file(dst_filename
, buffer
);
1614 ID3DXBuffer_Release(buffer
);
1620 HRESULT WINAPI
D3DXSaveSurfaceToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
1621 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1623 IWICBitmapEncoder
*encoder
= NULL
;
1624 IWICBitmapFrameEncode
*frame
= NULL
;
1625 IPropertyBag2
*encoder_options
= NULL
;
1626 IStream
*stream
= NULL
;
1629 const CLSID
*encoder_clsid
;
1630 const GUID
*pixel_format_guid
;
1631 WICPixelFormatGUID wic_pixel_format
;
1632 D3DFORMAT d3d_pixel_format
;
1633 D3DSURFACE_DESC src_surface_desc
;
1634 D3DLOCKED_RECT locked_rect
;
1636 STATSTG stream_stats
;
1637 HGLOBAL stream_hglobal
;
1638 ID3DXBuffer
*buffer
;
1641 TRACE("(%p, %#x, %p, %p, %s)\n",
1642 dst_buffer
, file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
1644 if (!dst_buffer
|| !src_surface
) return D3DERR_INVALIDCALL
;
1648 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1649 return D3DERR_INVALIDCALL
;
1652 switch (file_format
)
1655 encoder_clsid
= &CLSID_WICBmpEncoder
;
1658 encoder_clsid
= &CLSID_WICPngEncoder
;
1661 encoder_clsid
= &CLSID_WICJpegEncoder
;
1669 FIXME("File format %#x is not supported yet\n", file_format
);
1672 return D3DERR_INVALIDCALL
;
1675 IDirect3DSurface9_GetDesc(src_surface
, &src_surface_desc
);
1678 if (src_rect
->left
== src_rect
->right
|| src_rect
->top
== src_rect
->bottom
)
1680 WARN("Invalid rectangle with 0 area\n");
1681 return D3DXCreateBuffer(64, dst_buffer
);
1683 if (src_rect
->left
< 0 || src_rect
->top
< 0)
1684 return D3DERR_INVALIDCALL
;
1685 if (src_rect
->left
> src_rect
->right
|| src_rect
->top
> src_rect
->bottom
)
1686 return D3DERR_INVALIDCALL
;
1687 if (src_rect
->right
> src_surface_desc
.Width
|| src_rect
->bottom
> src_surface_desc
.Height
)
1688 return D3DERR_INVALIDCALL
;
1690 width
= src_rect
->right
- src_rect
->left
;
1691 height
= src_rect
->bottom
- src_rect
->top
;
1695 width
= src_surface_desc
.Width
;
1696 height
= src_surface_desc
.Height
;
1699 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1701 hr
= CoCreateInstance(encoder_clsid
, NULL
, CLSCTX_INPROC_SERVER
,
1702 &IID_IWICBitmapEncoder
, (void **)&encoder
);
1703 if (FAILED(hr
)) goto cleanup_err
;
1705 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &stream
);
1706 if (FAILED(hr
)) goto cleanup_err
;
1708 hr
= IWICBitmapEncoder_Initialize(encoder
, stream
, WICBitmapEncoderNoCache
);
1709 if (FAILED(hr
)) goto cleanup_err
;
1711 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frame
, &encoder_options
);
1712 if (FAILED(hr
)) goto cleanup_err
;
1714 hr
= IWICBitmapFrameEncode_Initialize(frame
, encoder_options
);
1715 if (FAILED(hr
)) goto cleanup_err
;
1717 hr
= IWICBitmapFrameEncode_SetSize(frame
, width
, height
);
1718 if (FAILED(hr
)) goto cleanup_err
;
1720 pixel_format_guid
= d3dformat_to_wic_guid(src_surface_desc
.Format
);
1721 if (!pixel_format_guid
)
1723 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc
.Format
);
1728 memcpy(&wic_pixel_format
, pixel_format_guid
, sizeof(GUID
));
1729 hr
= IWICBitmapFrameEncode_SetPixelFormat(frame
, &wic_pixel_format
);
1730 d3d_pixel_format
= wic_guid_to_d3dformat(&wic_pixel_format
);
1731 if (SUCCEEDED(hr
) && d3d_pixel_format
!= D3DFMT_UNKNOWN
)
1733 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format
), d3d_pixel_format
);
1735 if (src_surface_desc
.Format
== d3d_pixel_format
) /* Simple copy */
1737 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
1740 IWICBitmapFrameEncode_WritePixels(frame
, height
,
1741 locked_rect
.Pitch
, height
* locked_rect
.Pitch
, locked_rect
.pBits
);
1742 IDirect3DSurface9_UnlockRect(src_surface
);
1745 else /* Pixel format conversion */
1747 const PixelFormatDesc
*src_format_desc
, *dst_format_desc
;
1752 src_format_desc
= get_format_info(src_surface_desc
.Format
);
1753 dst_format_desc
= get_format_info(d3d_pixel_format
);
1754 if (src_format_desc
->format
== D3DFMT_UNKNOWN
|| dst_format_desc
->format
== D3DFMT_UNKNOWN
)
1756 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
1757 src_surface_desc
.Format
, d3d_pixel_format
);
1762 if (src_format_desc
->bytes_per_pixel
> 4
1763 || dst_format_desc
->bytes_per_pixel
> 4
1764 || src_format_desc
->block_height
!= 1 || src_format_desc
->block_width
!= 1
1765 || dst_format_desc
->block_height
!= 1 || dst_format_desc
->block_width
!= 1)
1773 dst_pitch
= width
* dst_format_desc
->bytes_per_pixel
;
1774 dst_data
= HeapAlloc(GetProcessHeap(), 0, dst_pitch
* height
);
1781 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
1784 copy_simple_data(locked_rect
.pBits
, locked_rect
.Pitch
, size
, src_format_desc
,
1785 dst_data
, dst_pitch
, size
, dst_format_desc
, 0);
1786 IDirect3DSurface9_UnlockRect(src_surface
);
1789 IWICBitmapFrameEncode_WritePixels(frame
, height
, dst_pitch
, dst_pitch
* height
, dst_data
);
1790 HeapFree(GetProcessHeap(), 0, dst_data
);
1793 hr
= IWICBitmapFrameEncode_Commit(frame
);
1794 if (SUCCEEDED(hr
)) hr
= IWICBitmapEncoder_Commit(encoder
);
1796 else WARN("Unsupported pixel format %#x\n", src_surface_desc
.Format
);
1798 /* copy data from stream to ID3DXBuffer */
1799 hr
= IStream_Stat(stream
, &stream_stats
, STATFLAG_NONAME
);
1800 if (FAILED(hr
)) goto cleanup_err
;
1802 if (stream_stats
.cbSize
.u
.HighPart
!= 0)
1804 hr
= D3DXERR_INVALIDDATA
;
1807 size
= stream_stats
.cbSize
.u
.LowPart
;
1809 hr
= D3DXCreateBuffer(size
, &buffer
);
1810 if (FAILED(hr
)) goto cleanup
;
1812 hr
= GetHGlobalFromStream(stream
, &stream_hglobal
);
1815 void *buffer_pointer
= ID3DXBuffer_GetBufferPointer(buffer
);
1816 void *stream_data
= GlobalLock(stream_hglobal
);
1817 memcpy(buffer_pointer
, stream_data
, size
);
1818 GlobalUnlock(stream_hglobal
);
1819 *dst_buffer
= buffer
;
1821 else ID3DXBuffer_Release(buffer
);
1824 if (FAILED(hr
) && hr
!= E_OUTOFMEMORY
)
1825 hr
= D3DERR_INVALIDCALL
;
1828 if (stream
) IStream_Release(stream
);
1830 if (frame
) IWICBitmapFrameEncode_Release(frame
);
1831 if (encoder_options
) IPropertyBag2_Release(encoder_options
);
1833 if (encoder
) IWICBitmapEncoder_Release(encoder
);
1835 if (SUCCEEDED(initresult
)) CoUninitialize();