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"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
31 /* Wine-specific WIC GUIDs */
32 DEFINE_GUID(GUID_WineContainerFormatTga
, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
38 } wic_pixel_formats
[] = {
39 { &GUID_WICPixelFormat8bppIndexed
, D3DFMT_L8
},
40 { &GUID_WICPixelFormat1bppIndexed
, D3DFMT_L8
},
41 { &GUID_WICPixelFormat4bppIndexed
, D3DFMT_L8
},
42 { &GUID_WICPixelFormat16bppBGR555
, D3DFMT_X1R5G5B5
},
43 { &GUID_WICPixelFormat16bppBGR565
, D3DFMT_R5G6B5
},
44 { &GUID_WICPixelFormat24bppBGR
, D3DFMT_R8G8B8
},
45 { &GUID_WICPixelFormat32bppBGR
, D3DFMT_X8R8G8B8
},
46 { &GUID_WICPixelFormat32bppBGRA
, D3DFMT_A8R8G8B8
}
49 static D3DFORMAT
wic_guid_to_d3dformat(const GUID
*guid
)
53 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
55 if (IsEqualGUID(wic_pixel_formats
[i
].wic_guid
, guid
))
56 return wic_pixel_formats
[i
].d3dformat
;
59 return D3DFMT_UNKNOWN
;
62 static const GUID
*d3dformat_to_wic_guid(D3DFORMAT format
)
66 for (i
= 0; i
< sizeof(wic_pixel_formats
) / sizeof(wic_pixel_formats
[0]); i
++)
68 if (wic_pixel_formats
[i
].d3dformat
== format
)
69 return wic_pixel_formats
[i
].wic_guid
;
75 /* dds_header.flags */
77 #define DDS_HEIGHT 0x2
80 #define DDS_PIXELFORMAT 0x1000
81 #define DDS_MIPMAPCOUNT 0x20000
82 #define DDS_LINEARSIZE 0x80000
83 #define DDS_DEPTH 0x800000
86 #define DDS_CAPS_COMPLEX 0x8
87 #define DDS_CAPS_TEXTURE 0x1000
88 #define DDS_CAPS_MIPMAP 0x400000
90 /* dds_header.caps2 */
91 #define DDS_CAPS2_CUBEMAP 0x200
92 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
93 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
94 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
95 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
96 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
97 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
98 #define DDS_CAPS2_VOLUME 0x200000
100 /* dds_pixel_format.flags */
101 #define DDS_PF_ALPHA 0x1
102 #define DDS_PF_ALPHA_ONLY 0x2
103 #define DDS_PF_FOURCC 0x4
104 #define DDS_PF_RGB 0x40
105 #define DDS_PF_YUV 0x200
106 #define DDS_PF_LUMINANCE 0x20000
107 #define DDS_PF_BUMPDUDV 0x80000
109 struct dds_pixel_format
128 DWORD pitch_or_linear_size
;
132 struct dds_pixel_format pixel_format
;
140 static D3DFORMAT
dds_fourcc_to_d3dformat(DWORD fourcc
)
143 static const DWORD known_fourcc
[] = {
144 MAKEFOURCC('U','Y','V','Y'),
145 MAKEFOURCC('Y','U','Y','2'),
146 MAKEFOURCC('R','G','B','G'),
147 MAKEFOURCC('G','R','G','B'),
148 MAKEFOURCC('D','X','T','1'),
149 MAKEFOURCC('D','X','T','2'),
150 MAKEFOURCC('D','X','T','3'),
151 MAKEFOURCC('D','X','T','4'),
152 MAKEFOURCC('D','X','T','5')
155 for (i
= 0; i
< sizeof(known_fourcc
) / sizeof(known_fourcc
[0]); i
++)
157 if (known_fourcc
[i
] == fourcc
)
161 WARN("Unknown FourCC %#x\n", fourcc
);
162 return D3DFMT_UNKNOWN
;
165 static D3DFORMAT
dds_rgb_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
168 static const struct {
175 } rgb_pixel_formats
[] = {
176 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2
},
177 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5
},
178 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5
},
179 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5
},
180 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4
},
181 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4
},
182 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2
},
183 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8
},
184 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8
},
185 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8
},
186 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10
},
187 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10
},
188 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16
},
191 for (i
= 0; i
< sizeof(rgb_pixel_formats
) / sizeof(rgb_pixel_formats
[0]); i
++)
193 if (rgb_pixel_formats
[i
].bpp
== pixel_format
->bpp
194 && rgb_pixel_formats
[i
].rmask
== pixel_format
->rmask
195 && rgb_pixel_formats
[i
].gmask
== pixel_format
->gmask
196 && rgb_pixel_formats
[i
].bmask
== pixel_format
->bmask
)
198 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && rgb_pixel_formats
[i
].amask
== pixel_format
->amask
)
199 return rgb_pixel_formats
[i
].format
;
200 if (rgb_pixel_formats
[i
].amask
== 0)
201 return rgb_pixel_formats
[i
].format
;
205 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
206 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
207 return D3DFMT_UNKNOWN
;
210 static D3DFORMAT
dds_luminance_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
212 if (pixel_format
->bpp
== 8)
214 if (pixel_format
->rmask
== 0xff)
216 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x0f && pixel_format
->amask
== 0xf0)
219 if (pixel_format
->bpp
== 16)
221 if (pixel_format
->rmask
== 0xffff)
223 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x00ff && pixel_format
->amask
== 0xff00)
227 WARN("Unknown luminance pixel format (bpp %#x, l %#x, a %#x)\n",
228 pixel_format
->bpp
, pixel_format
->rmask
, pixel_format
->amask
);
229 return D3DFMT_UNKNOWN
;
232 static D3DFORMAT
dds_alpha_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
234 if (pixel_format
->bpp
== 8 && pixel_format
->amask
== 0xff)
237 WARN("Unknown Alpha pixel format (%#x, %#x)\n", pixel_format
->bpp
, pixel_format
->rmask
);
238 return D3DFMT_UNKNOWN
;
241 static D3DFORMAT
dds_bump_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
243 if (pixel_format
->bpp
== 16 && pixel_format
->rmask
== 0x00ff && pixel_format
->gmask
== 0xff00)
245 if (pixel_format
->bpp
== 32 && pixel_format
->rmask
== 0x0000ffff && pixel_format
->gmask
== 0xffff0000)
246 return D3DFMT_V16U16
;
248 WARN("Unknown bump pixel format (%#x, %#x, %#x, %#x, %#x)\n", pixel_format
->bpp
,
249 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
250 return D3DFMT_UNKNOWN
;
253 static D3DFORMAT
dds_pixel_format_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
255 if (pixel_format
->flags
& DDS_PF_FOURCC
)
256 return dds_fourcc_to_d3dformat(pixel_format
->fourcc
);
257 if (pixel_format
->flags
& DDS_PF_RGB
)
258 return dds_rgb_to_d3dformat(pixel_format
);
259 if (pixel_format
->flags
& DDS_PF_LUMINANCE
)
260 return dds_luminance_to_d3dformat(pixel_format
);
261 if (pixel_format
->flags
& DDS_PF_ALPHA_ONLY
)
262 return dds_alpha_to_d3dformat(pixel_format
);
263 if (pixel_format
->flags
& DDS_PF_BUMPDUDV
)
264 return dds_bump_to_d3dformat(pixel_format
);
266 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %#x, r %#x, g %#x, b %#x, a %#x)\n",
267 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
,
268 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
269 return D3DFMT_UNKNOWN
;
272 /************************************************************
273 * get_image_info_from_dds
275 * Fills a D3DXIMAGE_INFO structure with information
276 * about a DDS file stored in the memory.
279 * buffer [I] pointer to DDS data
280 * length [I] size of DDS data
281 * info [O] pointer to D3DXIMAGE_INFO structure
285 * Failure: D3DXERR_INVALIDDATA
288 static HRESULT
get_image_info_from_dds(const void *buffer
, DWORD length
, D3DXIMAGE_INFO
*info
)
290 const struct dds_header
*header
= buffer
;
292 if (length
< sizeof(*header
) || !info
)
293 return D3DXERR_INVALIDDATA
;
295 if (header
->pixel_format
.size
!= sizeof(header
->pixel_format
))
296 return D3DXERR_INVALIDDATA
;
298 info
->Width
= header
->width
;
299 info
->Height
= header
->height
;
301 info
->MipLevels
= (header
->flags
& DDS_MIPMAPCOUNT
) ? header
->miplevels
: 1;
303 info
->Format
= dds_pixel_format_to_d3dformat(&header
->pixel_format
);
304 if (info
->Format
== D3DFMT_UNKNOWN
)
305 return D3DXERR_INVALIDDATA
;
307 if (header
->caps2
& DDS_CAPS2_VOLUME
)
309 info
->Depth
= header
->depth
;
310 info
->ResourceType
= D3DRTYPE_VOLUMETEXTURE
;
312 else if (header
->caps2
& DDS_CAPS2_CUBEMAP
)
314 info
->ResourceType
= D3DRTYPE_CUBETEXTURE
;
318 info
->ResourceType
= D3DRTYPE_TEXTURE
;
321 info
->ImageFileFormat
= D3DXIFF_DDS
;
326 /************************************************************
327 * D3DXGetImageInfoFromFileInMemory
329 * Fills a D3DXIMAGE_INFO structure with info about an image
332 * data [I] pointer to the image file data
333 * datasize [I] size of the passed data
334 * info [O] pointer to the destination structure
337 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
338 * if info is NULL and data and datasize are not NULL
339 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
340 * D3DERR_INVALIDCALL, if data is NULL or
344 * datasize may be bigger than the actual file size
347 HRESULT WINAPI
D3DXGetImageInfoFromFileInMemory(LPCVOID data
, UINT datasize
, D3DXIMAGE_INFO
*info
)
349 IWICImagingFactory
*factory
;
350 IWICBitmapDecoder
*decoder
= NULL
;
355 TRACE("(%p, %d, %p)\n", data
, datasize
, info
);
357 if (!data
|| !datasize
)
358 return D3DERR_INVALIDCALL
;
363 if ((datasize
>= 4) && !strncmp(data
, "DDS ", 4))
364 return get_image_info_from_dds(data
, datasize
, info
);
366 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
368 hr
= CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
);
371 IWICImagingFactory_CreateStream(factory
, &stream
);
372 IWICStream_InitializeFromMemory(stream
, (BYTE
*)data
, datasize
);
373 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
374 IStream_Release(stream
);
375 IWICImagingFactory_Release(factory
);
379 if ((datasize
>= 2) && (!strncmp(data
, "P3", 2) || !strncmp(data
, "P6", 2)))
380 FIXME("File type PPM is not supported yet\n");
381 else if ((datasize
>= 2) && !strncmp(data
, "BM", 2))
382 FIXME("File type DIB is not supported yet\n");
383 else if ((datasize
>= 10) && !strncmp(data
, "#?RADIANCE", 10))
384 FIXME("File type HDR is not supported yet\n");
385 else if ((datasize
>= 2) && (!strncmp(data
, "PF", 2) || !strncmp(data
, "Pf", 2)))
386 FIXME("File type PFM is not supported yet\n");
390 GUID container_format
;
393 hr
= IWICBitmapDecoder_GetContainerFormat(decoder
, &container_format
);
395 if (IsEqualGUID(&container_format
, &GUID_ContainerFormatBmp
)) {
396 TRACE("File type is BMP\n");
397 info
->ImageFileFormat
= D3DXIFF_BMP
;
398 } else if (IsEqualGUID(&container_format
, &GUID_ContainerFormatPng
)) {
399 TRACE("File type is PNG\n");
400 info
->ImageFileFormat
= D3DXIFF_PNG
;
401 } else if(IsEqualGUID(&container_format
, &GUID_ContainerFormatJpeg
)) {
402 TRACE("File type is JPG\n");
403 info
->ImageFileFormat
= D3DXIFF_JPG
;
404 } else if(IsEqualGUID(&container_format
, &GUID_WineContainerFormatTga
)) {
405 TRACE("File type is TGA\n");
406 info
->ImageFileFormat
= D3DXIFF_TGA
;
408 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format
));
409 hr
= D3DXERR_INVALIDDATA
;
414 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
415 if (SUCCEEDED(hr
) && !frame_count
)
416 hr
= D3DXERR_INVALIDDATA
;
419 IWICBitmapFrameDecode
*frame
= NULL
;
421 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
424 hr
= IWICBitmapFrameDecode_GetSize(frame
, &info
->Width
, &info
->Height
);
427 WICPixelFormatGUID pixel_format
;
429 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &pixel_format
);
431 info
->Format
= wic_guid_to_d3dformat(&pixel_format
);
432 if (info
->Format
== D3DFMT_UNKNOWN
) {
433 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format
));
434 hr
= D3DXERR_INVALIDDATA
;
440 IWICBitmapFrameDecode_Release(frame
);
444 info
->ResourceType
= D3DRTYPE_TEXTURE
;
449 IWICBitmapDecoder_Release(decoder
);
451 if (SUCCEEDED(initresult
))
455 TRACE("Invalid or unsupported image file\n");
456 return D3DXERR_INVALIDDATA
;
462 /************************************************************
463 * D3DXGetImageInfoFromFile
466 * Success: D3D_OK, if we successfully load a valid image file or
467 * if we successfully load a file which is no valid image and info is NULL
468 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
469 * if file is not a valid image file and info is not NULL
470 * D3DERR_INVALIDCALL, if file is NULL
473 HRESULT WINAPI
D3DXGetImageInfoFromFileA(LPCSTR file
, D3DXIMAGE_INFO
*info
)
479 TRACE("(%s, %p): relay\n", debugstr_a(file
), info
);
481 if( !file
) return D3DERR_INVALIDCALL
;
483 strlength
= MultiByteToWideChar(CP_ACP
, 0, file
, -1, NULL
, 0);
484 widename
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, strlength
* sizeof(WCHAR
));
485 MultiByteToWideChar(CP_ACP
, 0, file
, -1, widename
, strlength
);
487 hr
= D3DXGetImageInfoFromFileW(widename
, info
);
488 HeapFree(GetProcessHeap(), 0, widename
);
493 HRESULT WINAPI
D3DXGetImageInfoFromFileW(LPCWSTR file
, D3DXIMAGE_INFO
*info
)
499 TRACE("(%s, %p): relay\n", debugstr_w(file
), info
);
501 if( !file
) return D3DERR_INVALIDCALL
;
503 hr
= map_view_of_file(file
, &buffer
, &size
);
504 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
506 hr
= D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
507 UnmapViewOfFile(buffer
);
512 /************************************************************
513 * D3DXGetImageInfoFromResource
516 * Success: D3D_OK, if resource is a valid image file
517 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
518 * if we fail to load resource
521 HRESULT WINAPI
D3DXGetImageInfoFromResourceA(HMODULE module
, LPCSTR resource
, D3DXIMAGE_INFO
*info
)
525 TRACE("(%p, %s, %p)\n", module
, debugstr_a(resource
), info
);
527 resinfo
= FindResourceA(module
, resource
, (LPCSTR
)RT_RCDATA
);
533 hr
= load_resource_into_memory(module
, resinfo
, &buffer
, &size
);
534 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
535 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
538 resinfo
= FindResourceA(module
, resource
, (LPCSTR
)RT_BITMAP
);
540 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
543 return D3DXERR_INVALIDDATA
;
546 HRESULT WINAPI
D3DXGetImageInfoFromResourceW(HMODULE module
, LPCWSTR resource
, D3DXIMAGE_INFO
*info
)
550 TRACE("(%p, %s, %p)\n", module
, debugstr_w(resource
), info
);
552 resinfo
= FindResourceW(module
, resource
, (LPCWSTR
)RT_RCDATA
);
558 hr
= load_resource_into_memory(module
, resinfo
, &buffer
, &size
);
559 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
560 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
563 resinfo
= FindResourceW(module
, resource
, (LPCWSTR
)RT_BITMAP
);
565 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
568 return D3DXERR_INVALIDDATA
;
571 /************************************************************
572 * D3DXLoadSurfaceFromFileInMemory
574 * Loads data from a given buffer into a surface and fills a given
575 * D3DXIMAGE_INFO structure with info about the source data.
578 * pDestSurface [I] pointer to the surface
579 * pDestPalette [I] palette to use
580 * pDestRect [I] to be filled area of the surface
581 * pSrcData [I] pointer to the source data
582 * SrcDataSize [I] size of the source data in bytes
583 * pSrcRect [I] area of the source data to load
584 * dwFilter [I] filter to apply on stretching
585 * Colorkey [I] colorkey
586 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
590 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
591 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
594 HRESULT WINAPI
D3DXLoadSurfaceFromFileInMemory(LPDIRECT3DSURFACE9 pDestSurface
,
595 CONST PALETTEENTRY
*pDestPalette
,
596 CONST RECT
*pDestRect
,
599 CONST RECT
*pSrcRect
,
602 D3DXIMAGE_INFO
*pSrcInfo
)
604 D3DXIMAGE_INFO imginfo
;
607 IWICImagingFactory
*factory
;
608 IWICBitmapDecoder
*decoder
;
609 IWICBitmapFrameDecode
*bitmapframe
;
612 const PixelFormatDesc
*formatdesc
;
616 TRACE("(%p, %p, %p, %p, %d, %p, %d, %x, %p)\n", pDestSurface
, pDestPalette
, pDestRect
, pSrcData
,
617 SrcDataSize
, pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
619 if (!pDestSurface
|| !pSrcData
|| !SrcDataSize
)
620 return D3DERR_INVALIDCALL
;
622 hr
= D3DXGetImageInfoFromFileInMemory(pSrcData
, SrcDataSize
, &imginfo
);
627 CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
629 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
)))
632 if (FAILED(IWICImagingFactory_CreateStream(factory
, &stream
)))
634 IWICImagingFactory_Release(factory
);
638 IWICStream_InitializeFromMemory(stream
, (BYTE
*)pSrcData
, SrcDataSize
);
640 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
642 IStream_Release(stream
);
643 IWICImagingFactory_Release(factory
);
648 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &bitmapframe
);
655 wicrect
.X
= pSrcRect
->left
;
656 wicrect
.Y
= pSrcRect
->top
;
657 wicrect
.Width
= pSrcRect
->right
- pSrcRect
->left
;
658 wicrect
.Height
= pSrcRect
->bottom
- pSrcRect
->top
;
664 wicrect
.Width
= imginfo
.Width
;
665 wicrect
.Height
= imginfo
.Height
;
668 SetRect(&rect
, 0, 0, wicrect
.Width
, wicrect
.Height
);
670 formatdesc
= get_format_info(imginfo
.Format
);
672 if (formatdesc
->format
== D3DFMT_UNKNOWN
)
674 FIXME("Unsupported pixel format\n");
675 hr
= D3DXERR_INVALIDDATA
;
682 pitch
= formatdesc
->bytes_per_pixel
* wicrect
.Width
;
683 buffer
= HeapAlloc(GetProcessHeap(), 0, pitch
* wicrect
.Height
);
685 hr
= IWICBitmapFrameDecode_CopyPixels(bitmapframe
, &wicrect
, pitch
,
686 pitch
* wicrect
.Height
, buffer
);
690 hr
= D3DXLoadSurfaceFromMemory(pDestSurface
, pDestPalette
, pDestRect
,
691 buffer
, imginfo
.Format
, pitch
,
692 NULL
, &rect
, dwFilter
, Colorkey
);
695 HeapFree(GetProcessHeap(), 0, buffer
);
698 IWICBitmapFrameDecode_Release(bitmapframe
);
701 IWICBitmapDecoder_Release(decoder
);
707 return D3DXERR_INVALIDDATA
;
715 /************************************************************
716 * D3DXLoadSurfaceFromFile
718 HRESULT WINAPI
D3DXLoadSurfaceFromFileA(LPDIRECT3DSURFACE9 pDestSurface
,
719 CONST PALETTEENTRY
*pDestPalette
,
720 CONST RECT
*pDestRect
,
722 CONST RECT
*pSrcRect
,
725 D3DXIMAGE_INFO
*pSrcInfo
)
731 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface
, pDestPalette
, pDestRect
, debugstr_a(pSrcFile
),
732 pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
734 if( !pSrcFile
|| !pDestSurface
) return D3DERR_INVALIDCALL
;
736 strlength
= MultiByteToWideChar(CP_ACP
, 0, pSrcFile
, -1, NULL
, 0);
737 pWidename
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, strlength
* sizeof(WCHAR
));
738 MultiByteToWideChar(CP_ACP
, 0, pSrcFile
, -1, pWidename
, strlength
);
740 hr
= D3DXLoadSurfaceFromFileW(pDestSurface
, pDestPalette
, pDestRect
, pWidename
, pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
741 HeapFree(GetProcessHeap(), 0, pWidename
);
746 HRESULT WINAPI
D3DXLoadSurfaceFromFileW(LPDIRECT3DSURFACE9 pDestSurface
,
747 CONST PALETTEENTRY
*pDestPalette
,
748 CONST RECT
*pDestRect
,
750 CONST RECT
*pSrcRect
,
753 D3DXIMAGE_INFO
*pSrcInfo
)
759 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface
, pDestPalette
, pDestRect
, debugstr_w(pSrcFile
),
760 pSrcRect
, Filter
, Colorkey
, pSrcInfo
);
762 if( !pSrcFile
|| !pDestSurface
) return D3DERR_INVALIDCALL
;
764 hr
= map_view_of_file(pSrcFile
, &pBuffer
, &dwSize
);
765 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
767 hr
= D3DXLoadSurfaceFromFileInMemory(pDestSurface
, pDestPalette
, pDestRect
, pBuffer
, dwSize
, pSrcRect
, Filter
, Colorkey
, pSrcInfo
);
768 UnmapViewOfFile(pBuffer
);
773 /************************************************************
774 * D3DXLoadSurfaceFromResource
776 HRESULT WINAPI
D3DXLoadSurfaceFromResourceA(LPDIRECT3DSURFACE9 pDestSurface
,
777 CONST PALETTEENTRY
*pDestPalette
,
778 CONST RECT
*pDestRect
,
781 CONST RECT
*pSrcRect
,
784 D3DXIMAGE_INFO
*pSrcInfo
)
788 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface
, pDestPalette
, pDestRect
, hSrcModule
,
789 debugstr_a(pResource
), pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
791 if( !pDestSurface
) return D3DERR_INVALIDCALL
;
793 hResInfo
= FindResourceA(hSrcModule
, pResource
, (LPCSTR
)RT_RCDATA
);
799 hr
= load_resource_into_memory(hSrcModule
, hResInfo
, &pBuffer
, &dwSize
);
800 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
801 return D3DXLoadSurfaceFromFileInMemory(pDestSurface
, pDestPalette
, pDestRect
, pBuffer
, dwSize
, pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
804 hResInfo
= FindResourceA(hSrcModule
, pResource
, (LPCSTR
)RT_BITMAP
);
806 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
809 return D3DXERR_INVALIDDATA
;
812 HRESULT WINAPI
D3DXLoadSurfaceFromResourceW(LPDIRECT3DSURFACE9 pDestSurface
,
813 CONST PALETTEENTRY
*pDestPalette
,
814 CONST RECT
*pDestRect
,
817 CONST RECT
*pSrcRect
,
820 D3DXIMAGE_INFO
*pSrcInfo
)
824 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface
, pDestPalette
, pDestRect
, hSrcModule
,
825 debugstr_w(pResource
), pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
827 if( !pDestSurface
) return D3DERR_INVALIDCALL
;
829 hResInfo
= FindResourceW(hSrcModule
, pResource
, (LPCWSTR
)RT_RCDATA
);
835 hr
= load_resource_into_memory(hSrcModule
, hResInfo
, &pBuffer
, &dwSize
);
836 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
837 return D3DXLoadSurfaceFromFileInMemory(pDestSurface
, pDestPalette
, pDestRect
, pBuffer
, dwSize
, pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
840 hResInfo
= FindResourceW(hSrcModule
, pResource
, (LPCWSTR
)RT_BITMAP
);
842 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
845 return D3DXERR_INVALIDDATA
;
849 /************************************************************
850 * helper functions for D3DXLoadSurfaceFromMemory
852 struct argb_conversion_info
854 CONST PixelFormatDesc
*srcformat
;
855 CONST PixelFormatDesc
*destformat
;
856 DWORD srcshift
[4], destshift
[4];
857 DWORD srcmask
[4], destmask
[4];
858 BOOL process_channel
[4];
862 static void init_argb_conversion_info(CONST PixelFormatDesc
*srcformat
, CONST PixelFormatDesc
*destformat
, struct argb_conversion_info
*info
)
865 ZeroMemory(info
->process_channel
, 4 * sizeof(BOOL
));
866 info
->channelmask
= 0;
868 info
->srcformat
= srcformat
;
869 info
->destformat
= destformat
;
871 for(i
= 0;i
< 4;i
++) {
872 /* srcshift is used to extract the _relevant_ components */
873 info
->srcshift
[i
] = srcformat
->shift
[i
] + max( srcformat
->bits
[i
] - destformat
->bits
[i
], 0);
875 /* destshift is used to move the components to the correct position */
876 info
->destshift
[i
] = destformat
->shift
[i
] + max(destformat
->bits
[i
] - srcformat
->bits
[i
], 0);
878 info
->srcmask
[i
] = ((1 << srcformat
->bits
[i
]) - 1) << srcformat
->shift
[i
];
879 info
->destmask
[i
] = ((1 << destformat
->bits
[i
]) - 1) << destformat
->shift
[i
];
881 /* channelmask specifies bits which aren't used in the source format but in the destination one */
882 if(destformat
->bits
[i
]) {
883 if(srcformat
->bits
[i
]) info
->process_channel
[i
] = TRUE
;
884 else info
->channelmask
|= info
->destmask
[i
];
889 static DWORD
dword_from_bytes(CONST BYTE
*src
, UINT bytes_per_pixel
)
892 static BOOL fixme_once
;
894 if(bytes_per_pixel
> sizeof(DWORD
)) {
895 if(!fixme_once
++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel
);
896 bytes_per_pixel
= sizeof(DWORD
);
899 memcpy(&ret
, src
, bytes_per_pixel
);
903 static void dword_to_bytes(BYTE
*dst
, DWORD dword
, UINT bytes_per_pixel
)
905 static BOOL fixme_once
;
907 if(bytes_per_pixel
> sizeof(DWORD
)) {
908 if(!fixme_once
++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel
);
909 ZeroMemory(dst
, bytes_per_pixel
);
910 bytes_per_pixel
= sizeof(DWORD
);
913 memcpy(dst
, &dword
, bytes_per_pixel
);
916 /************************************************************
917 * get_relevant_argb_components
919 * Extracts the relevant components from the source color and
920 * drops the less significant bits if they aren't used by the destination format.
922 static void get_relevant_argb_components(CONST
struct argb_conversion_info
*info
, CONST DWORD col
, DWORD
*out
)
926 if(info
->process_channel
[i
])
927 out
[i
] = (col
& info
->srcmask
[i
]) >> info
->srcshift
[i
];
930 /************************************************************
933 * Recombines the output of get_relevant_argb_components and converts
934 * it to the destination format.
936 static DWORD
make_argb_color(CONST
struct argb_conversion_info
*info
, CONST DWORD
*in
)
941 for(i
= 0;i
< 4;i
++) {
942 if(info
->process_channel
[i
]) {
943 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
945 for(shift
= info
->destshift
[i
]; shift
> info
->destformat
->shift
[i
]; shift
-= info
->srcformat
->bits
[i
]) val
|= in
[i
] << shift
;
946 val
|= (in
[i
] >> (info
->destformat
->shift
[i
] - shift
)) << info
->destformat
->shift
[i
];
949 val
|= info
->channelmask
; /* new channels are set to their maximal value */
953 static void format_to_vec4(const PixelFormatDesc
*format
, const DWORD
*src
, struct vec4
*dst
)
959 mask
= (1 << format
->bits
[1]) - 1;
960 dst
->x
= (float)((*src
>> format
->shift
[1]) & mask
) / mask
;
967 mask
= (1 << format
->bits
[2]) - 1;
968 dst
->y
= (float)((*src
>> format
->shift
[2]) & mask
) / mask
;
975 mask
= (1 << format
->bits
[3]) - 1;
976 dst
->z
= (float)((*src
>> format
->shift
[3]) & mask
) / mask
;
983 mask
= (1 << format
->bits
[0]) - 1;
984 dst
->w
= (float)((*src
>> format
->shift
[0]) & mask
) / mask
;
990 static void format_from_vec4(const PixelFormatDesc
*format
, const struct vec4
*src
, DWORD
*dst
)
995 *dst
|= (DWORD
)(src
->x
* ((1 << format
->bits
[1]) - 1) + 0.5f
) << format
->shift
[1];
997 *dst
|= (DWORD
)(src
->y
* ((1 << format
->bits
[2]) - 1) + 0.5f
) << format
->shift
[2];
999 *dst
|= (DWORD
)(src
->z
* ((1 << format
->bits
[3]) - 1) + 0.5f
) << format
->shift
[3];
1000 if (format
->bits
[0])
1001 *dst
|= (DWORD
)(src
->w
* ((1 << format
->bits
[0]) - 1) + 0.5f
) << format
->shift
[0];
1004 /************************************************************
1007 * Copies the source buffer to the destination buffer, performing
1008 * any necessary format conversion and color keying.
1009 * Pixels outsize the source rect are blacked out.
1010 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1012 static void copy_simple_data(const BYTE
*src
, UINT srcpitch
, SIZE src_size
, const PixelFormatDesc
*srcformat
,
1013 BYTE
*dest
, UINT destpitch
, SIZE dst_size
, const PixelFormatDesc
*destformat
, D3DCOLOR colorkey
)
1015 struct argb_conversion_info conv_info
, ck_conv_info
;
1016 const PixelFormatDesc
*ck_format
= NULL
;
1017 DWORD channels
[4], pixel
;
1018 UINT minwidth
, minheight
;
1021 ZeroMemory(channels
, sizeof(channels
));
1022 init_argb_conversion_info(srcformat
, destformat
, &conv_info
);
1024 minwidth
= (src_size
.cx
< dst_size
.cx
) ? src_size
.cx
: dst_size
.cx
;
1025 minheight
= (src_size
.cy
< dst_size
.cy
) ? src_size
.cy
: dst_size
.cy
;
1029 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1030 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1031 init_argb_conversion_info(srcformat
, ck_format
, &ck_conv_info
);
1034 for(y
= 0;y
< minheight
;y
++) {
1035 const BYTE
*srcptr
= src
+ y
* srcpitch
;
1036 BYTE
*destptr
= dest
+ y
* destpitch
;
1039 for(x
= 0;x
< minwidth
;x
++) {
1040 /* extract source color components */
1041 pixel
= dword_from_bytes(srcptr
, srcformat
->bytes_per_pixel
);
1043 if (!srcformat
->to_rgba
&& !destformat
->from_rgba
)
1045 get_relevant_argb_components(&conv_info
, pixel
, channels
);
1046 val
= make_argb_color(&conv_info
, channels
);
1050 get_relevant_argb_components(&ck_conv_info
, pixel
, channels
);
1051 pixel
= make_argb_color(&ck_conv_info
, channels
);
1052 if (pixel
== colorkey
)
1053 val
&= ~conv_info
.destmask
[0];
1058 struct vec4 color
, tmp
;
1060 format_to_vec4(srcformat
, &pixel
, &color
);
1061 if (srcformat
->to_rgba
)
1062 srcformat
->to_rgba(&color
, &tmp
);
1068 format_from_vec4(ck_format
, &tmp
, &pixel
);
1069 if (pixel
== colorkey
)
1073 if (destformat
->from_rgba
)
1074 destformat
->from_rgba(&tmp
, &color
);
1078 format_from_vec4(destformat
, &color
, &val
);
1081 dword_to_bytes(destptr
, val
, destformat
->bytes_per_pixel
);
1082 srcptr
+= srcformat
->bytes_per_pixel
;
1083 destptr
+= destformat
->bytes_per_pixel
;
1086 if (src_size
.cx
< dst_size
.cx
) /* black out remaining pixels */
1087 memset(destptr
, 0, destformat
->bytes_per_pixel
* (dst_size
.cx
- src_size
.cx
));
1089 if (src_size
.cy
< dst_size
.cy
) /* black out remaining pixels */
1090 memset(dest
+ src_size
.cy
* destpitch
, 0, destpitch
* (dst_size
.cy
- src_size
.cy
));
1093 /************************************************************
1094 * point_filter_simple_data
1096 * Copies the source buffer to the destination buffer, performing
1097 * any necessary format conversion, color keying and stretching
1098 * using a point filter.
1099 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1101 static void point_filter_simple_data(const BYTE
*src
, UINT srcpitch
, SIZE src_size
, const PixelFormatDesc
*srcformat
,
1102 BYTE
*dest
, UINT destpitch
, SIZE dst_size
, const PixelFormatDesc
*destformat
, D3DCOLOR colorkey
)
1104 struct argb_conversion_info conv_info
, ck_conv_info
;
1105 const PixelFormatDesc
*ck_format
= NULL
;
1106 DWORD channels
[4], pixel
;
1110 ZeroMemory(channels
, sizeof(channels
));
1111 init_argb_conversion_info(srcformat
, destformat
, &conv_info
);
1115 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1116 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1117 init_argb_conversion_info(srcformat
, ck_format
, &ck_conv_info
);
1120 for (y
= 0; y
< dst_size
.cy
; ++y
)
1122 BYTE
*destptr
= dest
+ y
* destpitch
;
1123 const BYTE
*bufptr
= src
+ srcpitch
* (y
* src_size
.cy
/ dst_size
.cy
);
1125 for (x
= 0; x
< dst_size
.cx
; ++x
)
1127 const BYTE
*srcptr
= bufptr
+ (x
* src_size
.cx
/ dst_size
.cx
) * srcformat
->bytes_per_pixel
;
1130 /* extract source color components */
1131 pixel
= dword_from_bytes(srcptr
, srcformat
->bytes_per_pixel
);
1133 if (!srcformat
->to_rgba
&& !destformat
->from_rgba
)
1135 get_relevant_argb_components(&conv_info
, pixel
, channels
);
1136 val
= make_argb_color(&conv_info
, channels
);
1140 get_relevant_argb_components(&ck_conv_info
, pixel
, channels
);
1141 pixel
= make_argb_color(&ck_conv_info
, channels
);
1142 if (pixel
== colorkey
)
1143 val
&= ~conv_info
.destmask
[0];
1148 struct vec4 color
, tmp
;
1150 format_to_vec4(srcformat
, &pixel
, &color
);
1151 if (srcformat
->to_rgba
)
1152 srcformat
->to_rgba(&color
, &tmp
);
1158 format_from_vec4(ck_format
, &tmp
, &pixel
);
1159 if (pixel
== colorkey
)
1163 if (destformat
->from_rgba
)
1164 destformat
->from_rgba(&tmp
, &color
);
1168 format_from_vec4(destformat
, &color
, &val
);
1171 dword_to_bytes(destptr
, val
, destformat
->bytes_per_pixel
);
1172 destptr
+= destformat
->bytes_per_pixel
;
1177 /************************************************************
1178 * D3DXLoadSurfaceFromMemory
1180 * Loads data from a given memory chunk into a surface,
1181 * applying any of the specified filters.
1184 * pDestSurface [I] pointer to the surface
1185 * pDestPalette [I] palette to use
1186 * pDestRect [I] to be filled area of the surface
1187 * pSrcMemory [I] pointer to the source data
1188 * SrcFormat [I] format of the source pixel data
1189 * SrcPitch [I] number of bytes in a row
1190 * pSrcPalette [I] palette used in the source image
1191 * pSrcRect [I] area of the source data to load
1192 * dwFilter [I] filter to apply on stretching
1193 * Colorkey [I] colorkey
1196 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1197 * if pSrcMemory is NULL but the other parameters are valid
1198 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1199 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1200 * if DestRect is invalid
1201 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1202 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1205 * pSrcRect specifies the dimensions of the source data;
1206 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1209 HRESULT WINAPI
D3DXLoadSurfaceFromMemory(IDirect3DSurface9
*dst_surface
,
1210 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const void *src_memory
,
1211 D3DFORMAT src_format
, UINT src_pitch
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
,
1212 DWORD filter
, D3DCOLOR color_key
)
1214 CONST PixelFormatDesc
*srcformatdesc
, *destformatdesc
;
1215 D3DSURFACE_DESC surfdesc
;
1216 D3DLOCKED_RECT lockrect
;
1217 SIZE src_size
, dst_size
;
1220 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1221 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_memory
, src_format
,
1222 src_pitch
, src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1224 if (!dst_surface
|| !src_memory
|| !src_rect
)
1225 return D3DERR_INVALIDCALL
;
1226 if (src_format
== D3DFMT_UNKNOWN
1227 || src_rect
->left
>= src_rect
->right
1228 || src_rect
->top
>= src_rect
->bottom
)
1231 if (filter
== D3DX_DEFAULT
)
1232 filter
= D3DX_FILTER_TRIANGLE
| D3DX_FILTER_DITHER
;
1234 IDirect3DSurface9_GetDesc(dst_surface
, &surfdesc
);
1236 src_size
.cx
= src_rect
->right
- src_rect
->left
;
1237 src_size
.cy
= src_rect
->bottom
- src_rect
->top
;
1240 dst_size
.cx
= surfdesc
.Width
;
1241 dst_size
.cy
= surfdesc
.Height
;
1245 if (dst_rect
->left
> dst_rect
->right
|| dst_rect
->right
> surfdesc
.Width
)
1246 return D3DERR_INVALIDCALL
;
1247 if (dst_rect
->top
> dst_rect
->bottom
|| dst_rect
->bottom
> surfdesc
.Height
)
1248 return D3DERR_INVALIDCALL
;
1249 if (dst_rect
->left
< 0 || dst_rect
->top
< 0)
1250 return D3DERR_INVALIDCALL
;
1251 dst_size
.cx
= dst_rect
->right
- dst_rect
->left
;
1252 dst_size
.cy
= dst_rect
->bottom
- dst_rect
->top
;
1253 if (!dst_size
.cx
|| !dst_size
.cy
)
1257 srcformatdesc
= get_format_info(src_format
);
1258 if (srcformatdesc
->type
== FORMAT_UNKNOWN
)
1261 destformatdesc
= get_format_info(surfdesc
.Format
);
1262 if (destformatdesc
->type
== FORMAT_UNKNOWN
)
1265 if (src_format
== surfdesc
.Format
1266 && dst_size
.cx
== src_size
.cx
1267 && dst_size
.cy
== src_size
.cy
) /* Simple copy. */
1269 UINT row_block_count
= ((src_size
.cx
+ srcformatdesc
->block_width
- 1) / srcformatdesc
->block_width
);
1270 UINT row_count
= (src_size
.cy
+ srcformatdesc
->block_height
- 1) / srcformatdesc
->block_height
;
1271 const BYTE
*src_addr
;
1275 if (src_rect
->left
& (srcformatdesc
->block_width
- 1)
1276 || src_rect
->top
& (srcformatdesc
->block_height
- 1)
1277 || (src_rect
->right
& (srcformatdesc
->block_width
- 1)
1278 && src_size
.cx
!= surfdesc
.Width
)
1279 || (src_rect
->bottom
& (srcformatdesc
->block_height
- 1)
1280 && src_size
.cy
!= surfdesc
.Height
))
1282 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect
));
1283 return D3DXERR_INVALIDDATA
;
1286 if (FAILED(hr
= IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1287 return D3DXERR_INVALIDDATA
;
1289 src_addr
= src_memory
;
1290 src_addr
+= (src_rect
->top
/ srcformatdesc
->block_height
) * src_pitch
;
1291 src_addr
+= (src_rect
->left
/ srcformatdesc
->block_width
) * srcformatdesc
->block_byte_count
;
1292 dst_addr
= lockrect
.pBits
;
1294 for (row
= 0; row
< row_count
; ++row
)
1296 memcpy(dst_addr
, src_addr
, row_block_count
* srcformatdesc
->block_byte_count
);
1297 src_addr
+= src_pitch
;
1298 dst_addr
+= lockrect
.Pitch
;
1301 IDirect3DSurface9_UnlockRect(dst_surface
);
1303 else /* Stretching or format conversion. */
1305 if (srcformatdesc
->bytes_per_pixel
> 4)
1307 if (destformatdesc
->bytes_per_pixel
> 4)
1309 if (srcformatdesc
->block_height
!= 1 || srcformatdesc
->block_width
!= 1)
1311 if (destformatdesc
->block_height
!= 1 || destformatdesc
->block_width
!= 1)
1314 if (FAILED(hr
= IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1315 return D3DXERR_INVALIDDATA
;
1317 if ((filter
& 0xf) == D3DX_FILTER_NONE
)
1319 copy_simple_data(src_memory
, src_pitch
, src_size
, srcformatdesc
,
1320 lockrect
.pBits
, lockrect
.Pitch
, dst_size
, destformatdesc
, color_key
);
1322 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1324 if ((filter
& 0xf) != D3DX_FILTER_POINT
)
1325 FIXME("Unhandled filter %#x.\n", filter
);
1327 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1328 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1329 point_filter_simple_data(src_memory
, src_pitch
, src_size
, srcformatdesc
,
1330 lockrect
.pBits
, lockrect
.Pitch
, dst_size
, destformatdesc
, color_key
);
1333 IDirect3DSurface9_UnlockRect(dst_surface
);
1339 /************************************************************
1340 * D3DXLoadSurfaceFromSurface
1342 * Copies the contents from one surface to another, performing any required
1343 * format conversion, resizing or filtering.
1346 * pDestSurface [I] pointer to the destination surface
1347 * pDestPalette [I] palette to use
1348 * pDestRect [I] to be filled area of the surface
1349 * pSrcSurface [I] pointer to the source surface
1350 * pSrcPalette [I] palette used for the source surface
1351 * pSrcRect [I] area of the source data to load
1352 * dwFilter [I] filter to apply on resizing
1353 * Colorkey [I] any ARGB value or 0 to disable color-keying
1357 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1358 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1361 HRESULT WINAPI
D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface
,
1362 CONST PALETTEENTRY
*pDestPalette
,
1363 CONST RECT
*pDestRect
,
1364 LPDIRECT3DSURFACE9 pSrcSurface
,
1365 CONST PALETTEENTRY
*pSrcPalette
,
1366 CONST RECT
*pSrcRect
,
1371 D3DLOCKED_RECT lock
;
1372 D3DSURFACE_DESC SrcDesc
;
1375 TRACE("(%p, %p, %p, %p, %p, %p, %u, %#x): relay\n", pDestSurface
, pDestPalette
, pDestRect
,
1376 pSrcSurface
, pSrcPalette
, pSrcRect
, dwFilter
, Colorkey
);
1378 if( !pDestSurface
|| !pSrcSurface
) return D3DERR_INVALIDCALL
;
1380 IDirect3DSurface9_GetDesc(pSrcSurface
, &SrcDesc
);
1382 if( !pSrcRect
) SetRect(&rect
, 0, 0, SrcDesc
.Width
, SrcDesc
.Height
);
1383 else rect
= *pSrcRect
;
1385 hr
= IDirect3DSurface9_LockRect(pSrcSurface
, &lock
, NULL
, D3DLOCK_READONLY
);
1386 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1388 hr
= D3DXLoadSurfaceFromMemory(pDestSurface
, pDestPalette
, pDestRect
,
1389 lock
.pBits
, SrcDesc
.Format
, lock
.Pitch
,
1390 pSrcPalette
, &rect
, dwFilter
, Colorkey
);
1392 IDirect3DSurface9_UnlockRect(pSrcSurface
);
1397 HRESULT WINAPI
D3DXSaveSurfaceToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1398 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1404 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1405 wine_dbgstr_a(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
1407 if (!dst_filename
) return D3DERR_INVALIDCALL
;
1409 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
1410 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1411 if (!filename
) return E_OUTOFMEMORY
;
1412 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
1414 hr
= D3DXSaveSurfaceToFileW(filename
, file_format
, src_surface
, src_palette
, src_rect
);
1416 HeapFree(GetProcessHeap(), 0, filename
);
1420 HRESULT WINAPI
D3DXSaveSurfaceToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
1421 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
1423 IWICImagingFactory
*factory
;
1424 IWICBitmapEncoder
*encoder
= NULL
;
1425 IWICBitmapFrameEncode
*frame
= NULL
;
1426 IPropertyBag2
*encoder_options
= NULL
;
1427 IWICStream
*stream
= NULL
;
1430 const CLSID
*encoder_clsid
;
1431 const GUID
*pixel_format_guid
;
1432 WICPixelFormatGUID wic_pixel_format
;
1433 D3DFORMAT d3d_pixel_format
;
1434 D3DSURFACE_DESC src_surface_desc
;
1435 D3DLOCKED_RECT locked_rect
;
1438 TRACE("(%s, %#x, %p, %p, %s)\n",
1439 wine_dbgstr_w(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
1441 if (!dst_filename
|| !src_surface
) return D3DERR_INVALIDCALL
;
1445 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1446 return D3DERR_INVALIDCALL
;
1449 switch (file_format
)
1452 encoder_clsid
= &CLSID_WICBmpEncoder
;
1455 encoder_clsid
= &CLSID_WICPngEncoder
;
1458 encoder_clsid
= &CLSID_WICJpegEncoder
;
1466 FIXME("File format %#x is not supported yet\n", file_format
);
1469 return D3DERR_INVALIDCALL
;
1472 IDirect3DSurface9_GetDesc(src_surface
, &src_surface_desc
);
1475 if (src_rect
->left
== src_rect
->right
|| src_rect
->top
== src_rect
->bottom
)
1477 if (src_rect
->left
< 0 || src_rect
->top
< 0)
1478 return D3DERR_INVALIDCALL
;
1479 if (src_rect
->left
> src_rect
->right
|| src_rect
->top
> src_rect
->bottom
)
1480 return D3DERR_INVALIDCALL
;
1481 if (src_rect
->right
> src_surface_desc
.Width
|| src_rect
->bottom
> src_surface_desc
.Height
)
1482 return D3DERR_INVALIDCALL
;
1484 width
= src_rect
->right
- src_rect
->left
;
1485 height
= src_rect
->bottom
- src_rect
->top
;
1489 width
= src_surface_desc
.Width
;
1490 height
= src_surface_desc
.Height
;
1493 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
1495 hr
= CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
,
1496 &IID_IWICImagingFactory
, (void **)&factory
);
1497 if (FAILED(hr
)) goto cleanup_err
;
1499 hr
= IWICImagingFactory_CreateStream(factory
, &stream
);
1500 IWICImagingFactory_Release(factory
);
1501 if (FAILED(hr
)) goto cleanup_err
;
1503 hr
= IWICStream_InitializeFromFilename(stream
, dst_filename
, GENERIC_WRITE
);
1504 if (FAILED(hr
)) goto cleanup_err
;
1506 hr
= CoCreateInstance(encoder_clsid
, NULL
, CLSCTX_INPROC_SERVER
,
1507 &IID_IWICBitmapEncoder
, (void **)&encoder
);
1508 if (FAILED(hr
)) goto cleanup_err
;
1510 hr
= IWICBitmapEncoder_Initialize(encoder
, (IStream
*)stream
, WICBitmapEncoderNoCache
);
1511 IStream_Release((IStream
*)stream
);
1513 if (FAILED(hr
)) goto cleanup_err
;
1515 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frame
, &encoder_options
);
1516 if (FAILED(hr
)) goto cleanup_err
;
1518 hr
= IWICBitmapFrameEncode_Initialize(frame
, encoder_options
);
1519 if (FAILED(hr
)) goto cleanup_err
;
1521 hr
= IWICBitmapFrameEncode_SetSize(frame
, width
, height
);
1522 if (FAILED(hr
)) goto cleanup_err
;
1524 pixel_format_guid
= d3dformat_to_wic_guid(src_surface_desc
.Format
);
1525 if (!pixel_format_guid
)
1527 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc
.Format
);
1532 memcpy(&wic_pixel_format
, pixel_format_guid
, sizeof(GUID
));
1533 hr
= IWICBitmapFrameEncode_SetPixelFormat(frame
, &wic_pixel_format
);
1534 d3d_pixel_format
= wic_guid_to_d3dformat(&wic_pixel_format
);
1535 if (SUCCEEDED(hr
) && d3d_pixel_format
!= D3DFMT_UNKNOWN
)
1537 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format
), d3d_pixel_format
);
1539 if (src_surface_desc
.Format
== d3d_pixel_format
) /* Simple copy */
1541 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
1544 IWICBitmapFrameEncode_WritePixels(frame
, height
,
1545 locked_rect
.Pitch
, height
* locked_rect
.Pitch
, locked_rect
.pBits
);
1546 IDirect3DSurface9_UnlockRect(src_surface
);
1549 else /* Pixel format conversion */
1551 const PixelFormatDesc
*src_format_desc
, *dst_format_desc
;
1556 src_format_desc
= get_format_info(src_surface_desc
.Format
);
1557 dst_format_desc
= get_format_info(d3d_pixel_format
);
1558 if (src_format_desc
->format
== D3DFMT_UNKNOWN
|| dst_format_desc
->format
== D3DFMT_UNKNOWN
)
1560 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
1561 src_surface_desc
.Format
, d3d_pixel_format
);
1566 if (src_format_desc
->bytes_per_pixel
> 4
1567 || dst_format_desc
->bytes_per_pixel
> 4
1568 || src_format_desc
->block_height
!= 1 || src_format_desc
->block_width
!= 1
1569 || dst_format_desc
->block_height
!= 1 || dst_format_desc
->block_width
!= 1)
1577 dst_pitch
= width
* dst_format_desc
->bytes_per_pixel
;
1578 dst_data
= HeapAlloc(GetProcessHeap(), 0, dst_pitch
* height
);
1585 hr
= IDirect3DSurface9_LockRect(src_surface
, &locked_rect
, src_rect
, D3DLOCK_READONLY
);
1588 copy_simple_data(locked_rect
.pBits
, locked_rect
.Pitch
, size
, src_format_desc
,
1589 dst_data
, dst_pitch
, size
, dst_format_desc
, 0);
1590 IDirect3DSurface9_UnlockRect(src_surface
);
1593 IWICBitmapFrameEncode_WritePixels(frame
, height
, dst_pitch
, dst_pitch
* height
, dst_data
);
1594 HeapFree(GetProcessHeap(), 0, dst_data
);
1597 hr
= IWICBitmapFrameEncode_Commit(frame
);
1598 if (SUCCEEDED(hr
)) hr
= IWICBitmapEncoder_Commit(encoder
);
1600 else WARN("Unsupported pixel format %#x\n", src_surface_desc
.Format
);
1603 if (FAILED(hr
)) hr
= D3DERR_INVALIDCALL
;
1606 if (stream
) IStream_Release((IStream
*)stream
);
1608 if (frame
) IWICBitmapFrameEncode_Release(frame
);
1609 if (encoder_options
) IPropertyBag2_Release(encoder_options
);
1611 if (encoder
) IWICBitmapEncoder_Release(encoder
);
1613 if (SUCCEEDED(initresult
)) CoUninitialize();