d3dx9_36: Implement D3DXGetImageInfoFromFileInMemory using WindowsCodecs (based on...
[wine/multimedia.git] / dlls / d3dx9_36 / surface.c
blob0a882b54827f25c9d8bde9b958c1006a566e22f7
1 /*
2 * Copyright (C) 2009 Tony Wasserka
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/debug.h"
21 #include "wine/unicode.h"
22 #include "d3dx9_36_private.h"
24 #include "initguid.h"
25 #include "wincodec.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
30 /************************************************************
31 * D3DXGetImageInfoFromFileInMemory
33 * Fills a D3DXIMAGE_INFO structure with info about an image
35 * PARAMS
36 * data [I] pointer to the image file data
37 * datasize [I] size of the passed data
38 * info [O] pointer to the destination structure
40 * RETURNS
41 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
42 * if info is NULL and data and datasize are not NULL
43 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
44 * D3DERR_INVALIDCALL, if data is NULL or
45 * if datasize is 0
47 * NOTES
48 * datasize may be bigger than the actual file size
51 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
53 IWICImagingFactory *factory;
54 IWICBitmapDecoder *decoder = NULL;
55 IWICStream *stream;
56 HRESULT hr;
57 HRESULT initresult;
59 FIXME("(%p, %d, %p): partially implemented\n", data, datasize, info);
61 /* TODO: Add support for (or at least detect) TGA, DDS, PPM and DIB */
63 if (!data || !datasize)
64 return D3DERR_INVALIDCALL;
66 if (!info)
67 return D3D_OK;
69 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
71 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
73 if (SUCCEEDED(hr)) {
74 IWICImagingFactory_CreateStream(factory, &stream);
75 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
76 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
77 IStream_Release(stream);
78 IWICImagingFactory_Release(factory);
81 if (SUCCEEDED(hr)) {
82 GUID container_format;
83 UINT frame_count;
85 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
86 if (SUCCEEDED(hr)) {
87 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
88 TRACE("File type is BMP\n");
89 info->ImageFileFormat = D3DXIFF_BMP;
90 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
91 TRACE("File type is PNG\n");
92 info->ImageFileFormat = D3DXIFF_PNG;
93 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
94 TRACE("File type is JPG\n");
95 info->ImageFileFormat = D3DXIFF_JPG;
96 } else {
97 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
98 hr = D3DXERR_INVALIDDATA;
102 if (SUCCEEDED(hr))
103 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
104 if (SUCCEEDED(hr) && !frame_count)
105 hr = D3DXERR_INVALIDDATA;
107 if (SUCCEEDED(hr)) {
108 IWICBitmapFrameDecode *frame = NULL;
110 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
112 if (SUCCEEDED(hr))
113 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
115 if (SUCCEEDED(hr)) {
116 WICPixelFormatGUID pixel_format;
118 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
119 if (SUCCEEDED(hr)) {
120 if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat1bppIndexed))
121 info->Format = D3DFMT_L8;
122 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat4bppIndexed))
123 info->Format = D3DFMT_L8;
124 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat8bppIndexed))
125 info->Format = D3DFMT_L8;
126 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat16bppBGR555))
127 info->Format = D3DFMT_X1R5G5B5;
128 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat24bppBGR))
129 info->Format = D3DFMT_R8G8B8;
130 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat32bppBGR))
131 info->Format = D3DFMT_X8R8G8B8;
132 else {
133 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
134 hr = D3DXERR_INVALIDDATA;
139 if (frame)
140 IWICBitmapFrameDecode_Release(frame);
142 info->Depth = 1;
143 info->MipLevels = 1;
144 info->ResourceType = D3DRTYPE_TEXTURE;
148 if (decoder)
149 IWICBitmapDecoder_Release(decoder);
151 if (SUCCEEDED(initresult))
152 CoUninitialize();
154 if (FAILED(hr)) {
155 /* Missing formats are not detected yet and will fail silently without the FIXME */
156 FIXME("Invalid or unsupported image file\n");
157 return D3DXERR_INVALIDDATA;
160 return D3D_OK;
163 /************************************************************
164 * D3DXGetImageInfoFromFile
166 * RETURNS
167 * Success: D3D_OK, if we successfully load a valid image file or
168 * if we successfully load a file which is no valid image and info is NULL
169 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
170 * if file is not a valid image file and info is not NULL
171 * D3DERR_INVALIDCALL, if file is NULL
174 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
176 LPWSTR widename;
177 HRESULT hr;
178 int strlength;
180 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
182 if( !file ) return D3DERR_INVALIDCALL;
184 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
185 widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
186 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
188 hr = D3DXGetImageInfoFromFileW(widename, info);
189 HeapFree(GetProcessHeap(), 0, widename);
191 return hr;
194 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
196 HRESULT hr;
197 DWORD size;
198 LPVOID buffer;
200 TRACE("(%s, %p): relay\n", debugstr_w(file), info);
202 if( !file ) return D3DERR_INVALIDCALL;
204 hr = map_view_of_file(file, &buffer, &size);
205 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
207 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
208 UnmapViewOfFile(buffer);
210 return hr;
213 /************************************************************
214 * D3DXGetImageInfoFromResource
216 * RETURNS
217 * Success: D3D_OK, if resource is a valid image file
218 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
219 * if we fail to load resource
222 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
224 HRSRC resinfo;
226 TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
228 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
229 if(resinfo) {
230 LPVOID buffer;
231 HRESULT hr;
232 DWORD size;
234 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
235 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
236 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
239 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
240 if(resinfo) {
241 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
242 return E_NOTIMPL;
244 return D3DXERR_INVALIDDATA;
247 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
249 HRSRC resinfo;
251 TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
253 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
254 if(resinfo) {
255 LPVOID buffer;
256 HRESULT hr;
257 DWORD size;
259 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
260 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
261 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
264 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
265 if(resinfo) {
266 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
267 return E_NOTIMPL;
269 return D3DXERR_INVALIDDATA;
272 /************************************************************
273 * D3DXLoadSurfaceFromFileInMemory
275 * Loads data from a given buffer into a surface and fills a given
276 * D3DXIMAGE_INFO structure with info about the source data.
278 * PARAMS
279 * pDestSurface [I] pointer to the surface
280 * pDestPalette [I] palette to use
281 * pDestRect [I] to be filled area of the surface
282 * pSrcData [I] pointer to the source data
283 * SrcDataSize [I] size of the source data in bytes
284 * pSrcRect [I] area of the source data to load
285 * dwFilter [I] filter to apply on stretching
286 * Colorkey [I] colorkey
287 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
289 * RETURNS
290 * Success: D3D_OK
291 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
292 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
295 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(LPDIRECT3DSURFACE9 pDestSurface,
296 CONST PALETTEENTRY *pDestPalette,
297 CONST RECT *pDestRect,
298 LPCVOID pSrcData,
299 UINT SrcDataSize,
300 CONST RECT *pSrcRect,
301 DWORD dwFilter,
302 D3DCOLOR Colorkey,
303 D3DXIMAGE_INFO *pSrcInfo)
305 FIXME("(%p, %p, %p, %p, %d, %p, %d, %x, %p): stub\n", pDestSurface, pDestPalette,
306 pDestRect, pSrcData, SrcDataSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
308 if( !pDestSurface || !pSrcData | !SrcDataSize ) return D3DERR_INVALIDCALL;
309 return E_NOTIMPL;
312 /************************************************************
313 * D3DXLoadSurfaceFromFile
315 HRESULT WINAPI D3DXLoadSurfaceFromFileA(LPDIRECT3DSURFACE9 pDestSurface,
316 CONST PALETTEENTRY *pDestPalette,
317 CONST RECT *pDestRect,
318 LPCSTR pSrcFile,
319 CONST RECT *pSrcRect,
320 DWORD dwFilter,
321 D3DCOLOR Colorkey,
322 D3DXIMAGE_INFO *pSrcInfo)
324 LPWSTR pWidename;
325 HRESULT hr;
326 int strlength;
328 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_a(pSrcFile),
329 pSrcRect, dwFilter, Colorkey, pSrcInfo);
331 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
333 strlength = MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, NULL, 0);
334 pWidename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
335 MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, pWidename, strlength);
337 hr = D3DXLoadSurfaceFromFileW(pDestSurface, pDestPalette, pDestRect, pWidename, pSrcRect, dwFilter, Colorkey, pSrcInfo);
338 HeapFree(GetProcessHeap(), 0, pWidename);
340 return hr;
343 HRESULT WINAPI D3DXLoadSurfaceFromFileW(LPDIRECT3DSURFACE9 pDestSurface,
344 CONST PALETTEENTRY *pDestPalette,
345 CONST RECT *pDestRect,
346 LPCWSTR pSrcFile,
347 CONST RECT *pSrcRect,
348 DWORD Filter,
349 D3DCOLOR Colorkey,
350 D3DXIMAGE_INFO *pSrcInfo)
352 HRESULT hr;
353 DWORD dwSize;
354 LPVOID pBuffer;
356 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_w(pSrcFile),
357 pSrcRect, Filter, Colorkey, pSrcInfo);
359 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
361 hr = map_view_of_file(pSrcFile, &pBuffer, &dwSize);
362 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
364 hr = D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, Filter, Colorkey, pSrcInfo);
365 UnmapViewOfFile(pBuffer);
367 return hr;
370 /************************************************************
371 * D3DXLoadSurfaceFromResource
373 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(LPDIRECT3DSURFACE9 pDestSurface,
374 CONST PALETTEENTRY *pDestPalette,
375 CONST RECT *pDestRect,
376 HMODULE hSrcModule,
377 LPCSTR pResource,
378 CONST RECT *pSrcRect,
379 DWORD dwFilter,
380 D3DCOLOR Colorkey,
381 D3DXIMAGE_INFO *pSrcInfo)
383 HRSRC hResInfo;
385 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
386 debugstr_a(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
388 if( !pDestSurface ) return D3DERR_INVALIDCALL;
390 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_RCDATA);
391 if(hResInfo) {
392 LPVOID pBuffer;
393 HRESULT hr;
394 DWORD dwSize;
396 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
397 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
398 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
401 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_BITMAP);
402 if(hResInfo) {
403 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
404 return E_NOTIMPL;
406 return D3DXERR_INVALIDDATA;
409 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(LPDIRECT3DSURFACE9 pDestSurface,
410 CONST PALETTEENTRY *pDestPalette,
411 CONST RECT *pDestRect,
412 HMODULE hSrcModule,
413 LPCWSTR pResource,
414 CONST RECT *pSrcRect,
415 DWORD dwFilter,
416 D3DCOLOR Colorkey,
417 D3DXIMAGE_INFO *pSrcInfo)
419 HRSRC hResInfo;
421 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
422 debugstr_w(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
424 if( !pDestSurface ) return D3DERR_INVALIDCALL;
426 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_RCDATA);
427 if(hResInfo) {
428 LPVOID pBuffer;
429 HRESULT hr;
430 DWORD dwSize;
432 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
433 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
434 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
437 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_BITMAP);
438 if(hResInfo) {
439 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
440 return E_NOTIMPL;
442 return D3DXERR_INVALIDDATA;
446 /************************************************************
447 * copy_simple_data
449 * Copies the source buffer to the destination buffer, performing
450 * any necessary format conversion and color keying.
451 * Works only for ARGB formats with 1 - 4 bytes per pixel.
453 static void copy_simple_data(CONST BYTE *src, UINT srcpitch, POINT srcsize, CONST PixelFormatDesc *srcformat,
454 CONST BYTE *dest, UINT destpitch, POINT destsize, CONST PixelFormatDesc *destformat,
455 DWORD dwFilter)
457 DWORD srcshift[4], destshift[4];
458 DWORD srcmask[4], destmask[4];
459 BOOL process_channel[4];
460 DWORD channels[4];
461 DWORD channelmask = 0;
463 UINT minwidth, minheight;
464 BYTE *srcptr, *destptr;
465 UINT i, x, y;
467 ZeroMemory(channels, sizeof(channels));
468 ZeroMemory(process_channel, sizeof(process_channel));
470 for(i = 0;i < 4;i++) {
471 /* srcshift is used to extract the _relevant_ components */
472 srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
474 /* destshift is used to move the components to the correct position */
475 destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
477 srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
478 destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
480 /* channelmask specifies bits which aren't used in the source format but in the destination one */
481 if(destformat->bits[i]) {
482 if(srcformat->bits[i]) process_channel[i] = TRUE;
483 else channelmask |= destmask[i];
487 minwidth = (srcsize.x < destsize.x) ? srcsize.x : destsize.x;
488 minheight = (srcsize.y < destsize.y) ? srcsize.y : destsize.y;
490 for(y = 0;y < minheight;y++) {
491 srcptr = (BYTE*)( src + y * srcpitch);
492 destptr = (BYTE*)(dest + y * destpitch);
493 for(x = 0;x < minwidth;x++) {
494 /* extract source color components */
495 if(srcformat->type == FORMAT_ARGB) {
496 const DWORD col = *(DWORD*)srcptr;
497 for(i = 0;i < 4;i++)
498 if(process_channel[i])
499 channels[i] = (col & srcmask[i]) >> srcshift[i];
502 /* recombine the components */
503 if(destformat->type == FORMAT_ARGB) {
504 DWORD* const pixel = (DWORD*)destptr;
505 *pixel = 0;
507 for(i = 0;i < 4;i++) {
508 if(process_channel[i]) {
509 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
510 signed int shift;
511 for(shift = destshift[i]; shift > destformat->shift[i]; shift -= srcformat->bits[i]) *pixel |= channels[i] << shift;
512 *pixel |= (channels[i] >> (destformat->shift[i] - shift)) << destformat->shift[i];
515 *pixel |= channelmask; /* new channels are set to their maximal value */
517 srcptr += srcformat->bytes_per_pixel;
518 destptr += destformat->bytes_per_pixel;
523 /************************************************************
524 * D3DXLoadSurfaceFromMemory
526 * Loads data from a given memory chunk into a surface,
527 * applying any of the specified filters.
529 * PARAMS
530 * pDestSurface [I] pointer to the surface
531 * pDestPalette [I] palette to use
532 * pDestRect [I] to be filled area of the surface
533 * pSrcMemory [I] pointer to the source data
534 * SrcFormat [I] format of the source pixel data
535 * SrcPitch [I] number of bytes in a row
536 * pSrcPalette [I] palette used in the source image
537 * pSrcRect [I] area of the source data to load
538 * dwFilter [I] filter to apply on stretching
539 * Colorkey [I] colorkey
541 * RETURNS
542 * Success: D3D_OK, if we successfully load the pixel data into our surface or
543 * if pSrcMemory is NULL but the other parameters are valid
544 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
545 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN)
546 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
547 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
549 * NOTES
550 * pSrcRect specifies the dimensions of the source data;
551 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
554 HRESULT WINAPI D3DXLoadSurfaceFromMemory(LPDIRECT3DSURFACE9 pDestSurface,
555 CONST PALETTEENTRY *pDestPalette,
556 CONST RECT *pDestRect,
557 LPCVOID pSrcMemory,
558 D3DFORMAT SrcFormat,
559 UINT SrcPitch,
560 CONST PALETTEENTRY *pSrcPalette,
561 CONST RECT *pSrcRect,
562 DWORD dwFilter,
563 D3DCOLOR Colorkey)
565 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
566 D3DSURFACE_DESC surfdesc;
567 D3DLOCKED_RECT lockrect;
568 POINT srcsize, destsize;
569 HRESULT hr;
571 TRACE("(%p, %p, %p, %p, %x, %u, %p, %p %u, %#x)\n", pDestSurface, pDestPalette, pDestRect, pSrcMemory,
572 SrcFormat, SrcPitch, pSrcPalette, pSrcRect, dwFilter, Colorkey);
574 if( !pDestSurface || !pSrcMemory || !pSrcRect ) return D3DERR_INVALIDCALL;
575 if(SrcFormat == D3DFMT_UNKNOWN || pSrcRect->left >= pSrcRect->right || pSrcRect->top >= pSrcRect->bottom) return E_FAIL;
577 if(dwFilter != D3DX_FILTER_NONE) return E_NOTIMPL;
579 IDirect3DSurface9_GetDesc(pDestSurface, &surfdesc);
581 srcformatdesc = get_format_info(SrcFormat);
582 destformatdesc = get_format_info(surfdesc.Format);
583 if( srcformatdesc->type == FORMAT_UNKNOWN || srcformatdesc->bytes_per_pixel > 4) return E_NOTIMPL;
584 if(destformatdesc->type == FORMAT_UNKNOWN || destformatdesc->bytes_per_pixel > 4) return E_NOTIMPL;
586 srcsize.x = pSrcRect->right - pSrcRect->left;
587 srcsize.y = pSrcRect->bottom - pSrcRect->top;
588 if( !pDestRect ) {
589 destsize.x = surfdesc.Width;
590 destsize.y = surfdesc.Height;
591 } else {
592 destsize.x = pDestRect->right - pDestRect->left;
593 destsize.y = pDestRect->bottom - pDestRect->top;
596 hr = IDirect3DSurface9_LockRect(pDestSurface, &lockrect, pDestRect, 0);
597 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
599 copy_simple_data((CONST BYTE*)pSrcMemory, SrcPitch, srcsize, srcformatdesc,
600 (CONST BYTE*)lockrect.pBits, lockrect.Pitch, destsize, destformatdesc,
601 dwFilter);
603 IDirect3DSurface9_UnlockRect(pDestSurface);
604 return D3D_OK;
607 /************************************************************
608 * D3DXLoadSurfaceFromSurface
610 * Copies the contents from one surface to another, performing any required
611 * format conversion, resizing or filtering.
613 * PARAMS
614 * pDestSurface [I] pointer to the destination surface
615 * pDestPalette [I] palette to use
616 * pDestRect [I] to be filled area of the surface
617 * pSrcSurface [I] pointer to the source surface
618 * pSrcPalette [I] palette used for the source surface
619 * pSrcRect [I] area of the source data to load
620 * dwFilter [I] filter to apply on resizing
621 * Colorkey [I] any ARGB value or 0 to disable color-keying
623 * RETURNS
624 * Success: D3D_OK
625 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
626 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
629 HRESULT WINAPI D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface,
630 CONST PALETTEENTRY *pDestPalette,
631 CONST RECT *pDestRect,
632 LPDIRECT3DSURFACE9 pSrcSurface,
633 CONST PALETTEENTRY *pSrcPalette,
634 CONST RECT *pSrcRect,
635 DWORD dwFilter,
636 D3DCOLOR Colorkey)
638 RECT rect;
639 D3DLOCKED_RECT lock;
640 D3DSURFACE_DESC SrcDesc;
641 HRESULT hr;
643 TRACE("(%p, %p, %p, %p, %p, %p, %u, %#x): relay\n", pDestSurface, pDestPalette, pDestRect,
644 pSrcSurface, pSrcPalette, pSrcRect, dwFilter, Colorkey);
646 if( !pDestSurface || !pSrcSurface ) return D3DERR_INVALIDCALL;
648 IDirect3DSurface9_GetDesc(pSrcSurface, &SrcDesc);
650 if( !pSrcRect ) SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
651 else rect = *pSrcRect;
653 hr = IDirect3DSurface9_LockRect(pSrcSurface, &lock, NULL, D3DLOCK_READONLY);
654 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
656 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
657 lock.pBits, SrcDesc.Format, lock.Pitch,
658 pSrcPalette, &rect, dwFilter, Colorkey);
660 IDirect3DSurface9_UnlockRect(pSrcSurface);
661 return hr;