2 * Copyright (C) 2009-2010 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"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
30 /* Wine-specific WIC GUIDs */
32 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
34 /************************************************************
35 * D3DXGetImageInfoFromFileInMemory
37 * Fills a D3DXIMAGE_INFO structure with info about an image
40 * data [I] pointer to the image file data
41 * datasize [I] size of the passed data
42 * info [O] pointer to the destination structure
45 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
46 * if info is NULL and data and datasize are not NULL
47 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
48 * D3DERR_INVALIDCALL, if data is NULL or
52 * datasize may be bigger than the actual file size
55 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
57 IWICImagingFactory *factory;
58 IWICBitmapDecoder *decoder = NULL;
63 FIXME("(%p, %d, %p): partially implemented\n", data, datasize, info);
65 /* TODO: Add support for (or at least detect) TGA, DDS, PPM and DIB */
67 if (!data || !datasize)
68 return D3DERR_INVALIDCALL;
73 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
75 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
78 IWICImagingFactory_CreateStream(factory, &stream);
79 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
80 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
81 IStream_Release(stream);
82 IWICImagingFactory_Release(factory);
86 GUID container_format;
89 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
91 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
92 TRACE("File type is BMP\n");
93 info->ImageFileFormat = D3DXIFF_BMP;
94 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
95 TRACE("File type is PNG\n");
96 info->ImageFileFormat = D3DXIFF_PNG;
97 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
98 TRACE("File type is JPG\n");
99 info->ImageFileFormat = D3DXIFF_JPG;
100 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
101 TRACE("File type is TGA\n");
102 info->ImageFileFormat = D3DXIFF_TGA;
104 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
105 hr = D3DXERR_INVALIDDATA;
110 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
111 if (SUCCEEDED(hr) && !frame_count)
112 hr = D3DXERR_INVALIDDATA;
115 IWICBitmapFrameDecode *frame = NULL;
117 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
120 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
123 WICPixelFormatGUID pixel_format;
125 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
127 if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat1bppIndexed))
128 info->Format = D3DFMT_L8;
129 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat4bppIndexed))
130 info->Format = D3DFMT_L8;
131 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat8bppIndexed))
132 info->Format = D3DFMT_L8;
133 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat16bppBGR555))
134 info->Format = D3DFMT_X1R5G5B5;
135 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat24bppBGR))
136 info->Format = D3DFMT_R8G8B8;
137 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat32bppBGR))
138 info->Format = D3DFMT_X8R8G8B8;
139 else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat32bppBGRA))
140 info->Format = D3DFMT_A8R8G8B8;
142 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
143 hr = D3DXERR_INVALIDDATA;
149 IWICBitmapFrameDecode_Release(frame);
153 info->ResourceType = D3DRTYPE_TEXTURE;
158 IWICBitmapDecoder_Release(decoder);
160 if (SUCCEEDED(initresult))
164 /* Missing formats are not detected yet and will fail silently without the FIXME */
165 FIXME("Invalid or unsupported image file\n");
166 return D3DXERR_INVALIDDATA;
172 /************************************************************
173 * D3DXGetImageInfoFromFile
176 * Success: D3D_OK, if we successfully load a valid image file or
177 * if we successfully load a file which is no valid image and info is NULL
178 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
179 * if file is not a valid image file and info is not NULL
180 * D3DERR_INVALIDCALL, if file is NULL
183 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
189 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
191 if( !file ) return D3DERR_INVALIDCALL;
193 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
194 widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
195 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
197 hr = D3DXGetImageInfoFromFileW(widename, info);
198 HeapFree(GetProcessHeap(), 0, widename);
203 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
209 TRACE("(%s, %p): relay\n", debugstr_w(file), info);
211 if( !file ) return D3DERR_INVALIDCALL;
213 hr = map_view_of_file(file, &buffer, &size);
214 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
216 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
217 UnmapViewOfFile(buffer);
222 /************************************************************
223 * D3DXGetImageInfoFromResource
226 * Success: D3D_OK, if resource is a valid image file
227 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
228 * if we fail to load resource
231 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
235 TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
237 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
243 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
244 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
245 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
248 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
250 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
253 return D3DXERR_INVALIDDATA;
256 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
260 TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
262 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
268 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
269 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
270 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
273 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
275 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
278 return D3DXERR_INVALIDDATA;
281 /************************************************************
282 * D3DXLoadSurfaceFromFileInMemory
284 * Loads data from a given buffer into a surface and fills a given
285 * D3DXIMAGE_INFO structure with info about the source data.
288 * pDestSurface [I] pointer to the surface
289 * pDestPalette [I] palette to use
290 * pDestRect [I] to be filled area of the surface
291 * pSrcData [I] pointer to the source data
292 * SrcDataSize [I] size of the source data in bytes
293 * pSrcRect [I] area of the source data to load
294 * dwFilter [I] filter to apply on stretching
295 * Colorkey [I] colorkey
296 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
300 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
301 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
304 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(LPDIRECT3DSURFACE9 pDestSurface,
305 CONST PALETTEENTRY *pDestPalette,
306 CONST RECT *pDestRect,
309 CONST RECT *pSrcRect,
312 D3DXIMAGE_INFO *pSrcInfo)
314 D3DXIMAGE_INFO imginfo;
317 TRACE("(%p, %p, %p, %p, %d, %p, %d, %x, %p)\n", pDestSurface, pDestPalette, pDestRect, pSrcData,
318 SrcDataSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
320 if (!pDestSurface || !pSrcData | !SrcDataSize)
321 return D3DERR_INVALIDCALL;
323 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
328 switch (imginfo.ImageFileFormat)
334 IWICImagingFactory *factory;
335 IWICBitmapDecoder *decoder;
336 IWICBitmapFrameDecode *bitmapframe;
339 const PixelFormatDesc *formatdesc;
343 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
345 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
348 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
350 IWICImagingFactory_Release(factory);
354 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
356 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
358 IStream_Release(stream);
359 IWICImagingFactory_Release(factory);
364 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
371 wicrect.X = pSrcRect->left;
372 wicrect.Y = pSrcRect->top;
373 wicrect.Width = pSrcRect->right - pSrcRect->left;
374 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
380 wicrect.Width = imginfo.Width;
381 wicrect.Height = imginfo.Height;
384 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
386 formatdesc = get_format_info(imginfo.Format);
388 if (formatdesc->format == D3DFMT_UNKNOWN)
390 FIXME("Unsupported pixel format\n");
391 hr = D3DXERR_INVALIDDATA;
398 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
399 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
401 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
402 pitch * wicrect.Height, buffer);
406 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
407 buffer, imginfo.Format, pitch,
408 NULL, &rect, dwFilter, Colorkey);
411 HeapFree(GetProcessHeap(), 0, buffer);
415 IWICBitmapFrameDecode_Release(bitmapframe);
416 IWICBitmapDecoder_Release(decoder);
422 return D3DXERR_INVALIDDATA;
428 FIXME("Unsupported image file format\n");
438 /************************************************************
439 * D3DXLoadSurfaceFromFile
441 HRESULT WINAPI D3DXLoadSurfaceFromFileA(LPDIRECT3DSURFACE9 pDestSurface,
442 CONST PALETTEENTRY *pDestPalette,
443 CONST RECT *pDestRect,
445 CONST RECT *pSrcRect,
448 D3DXIMAGE_INFO *pSrcInfo)
454 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_a(pSrcFile),
455 pSrcRect, dwFilter, Colorkey, pSrcInfo);
457 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
459 strlength = MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, NULL, 0);
460 pWidename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
461 MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, pWidename, strlength);
463 hr = D3DXLoadSurfaceFromFileW(pDestSurface, pDestPalette, pDestRect, pWidename, pSrcRect, dwFilter, Colorkey, pSrcInfo);
464 HeapFree(GetProcessHeap(), 0, pWidename);
469 HRESULT WINAPI D3DXLoadSurfaceFromFileW(LPDIRECT3DSURFACE9 pDestSurface,
470 CONST PALETTEENTRY *pDestPalette,
471 CONST RECT *pDestRect,
473 CONST RECT *pSrcRect,
476 D3DXIMAGE_INFO *pSrcInfo)
482 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_w(pSrcFile),
483 pSrcRect, Filter, Colorkey, pSrcInfo);
485 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
487 hr = map_view_of_file(pSrcFile, &pBuffer, &dwSize);
488 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
490 hr = D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, Filter, Colorkey, pSrcInfo);
491 UnmapViewOfFile(pBuffer);
496 /************************************************************
497 * D3DXLoadSurfaceFromResource
499 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(LPDIRECT3DSURFACE9 pDestSurface,
500 CONST PALETTEENTRY *pDestPalette,
501 CONST RECT *pDestRect,
504 CONST RECT *pSrcRect,
507 D3DXIMAGE_INFO *pSrcInfo)
511 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
512 debugstr_a(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
514 if( !pDestSurface ) return D3DERR_INVALIDCALL;
516 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_RCDATA);
522 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
523 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
524 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
527 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_BITMAP);
529 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
532 return D3DXERR_INVALIDDATA;
535 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(LPDIRECT3DSURFACE9 pDestSurface,
536 CONST PALETTEENTRY *pDestPalette,
537 CONST RECT *pDestRect,
540 CONST RECT *pSrcRect,
543 D3DXIMAGE_INFO *pSrcInfo)
547 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
548 debugstr_w(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
550 if( !pDestSurface ) return D3DERR_INVALIDCALL;
552 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_RCDATA);
558 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
559 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
560 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
563 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_BITMAP);
565 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
568 return D3DXERR_INVALIDDATA;
572 /************************************************************
573 * helper functions for D3DXLoadSurfaceFromMemory
575 struct argb_conversion_info
577 CONST PixelFormatDesc *srcformat;
578 CONST PixelFormatDesc *destformat;
579 DWORD srcshift[4], destshift[4];
580 DWORD srcmask[4], destmask[4];
581 BOOL process_channel[4];
585 static void init_argb_conversion_info(CONST PixelFormatDesc *srcformat, CONST PixelFormatDesc *destformat, struct argb_conversion_info *info)
588 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
589 info->channelmask = 0;
591 info->srcformat = srcformat;
592 info->destformat = destformat;
594 for(i = 0;i < 4;i++) {
595 /* srcshift is used to extract the _relevant_ components */
596 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
598 /* destshift is used to move the components to the correct position */
599 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
601 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
602 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
604 /* channelmask specifies bits which aren't used in the source format but in the destination one */
605 if(destformat->bits[i]) {
606 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
607 else info->channelmask |= info->destmask[i];
612 /************************************************************
613 * get_relevant_argb_components
615 * Extracts the relevant components from the source color and
616 * drops the less significant bits if they aren't used by the destination format.
618 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
622 if(info->process_channel[i])
623 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
626 /************************************************************
629 * Recombines the output of get_relevant_argb_components and converts
630 * it to the destination format.
632 static void make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in, DWORD *out)
637 for(i = 0;i < 4;i++) {
638 if(info->process_channel[i]) {
639 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
641 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) *out |= in[i] << shift;
642 *out |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
645 *out |= info->channelmask; /* new channels are set to their maximal value */
648 /************************************************************
651 * Copies the source buffer to the destination buffer, performing
652 * any necessary format conversion and color keying.
653 * Pixels outsize the source rect are blacked out.
654 * Works only for ARGB formats with 1 - 4 bytes per pixel.
656 static void copy_simple_data(CONST BYTE *src, UINT srcpitch, POINT srcsize, CONST PixelFormatDesc *srcformat,
657 BYTE *dest, UINT destpitch, POINT destsize, CONST PixelFormatDesc *destformat)
659 struct argb_conversion_info conv_info;
661 UINT minwidth, minheight;
664 ZeroMemory(channels, sizeof(channels));
665 init_argb_conversion_info(srcformat, destformat, &conv_info);
667 minwidth = (srcsize.x < destsize.x) ? srcsize.x : destsize.x;
668 minheight = (srcsize.y < destsize.y) ? srcsize.y : destsize.y;
670 for(y = 0;y < minheight;y++) {
671 const BYTE *srcptr = src + y * srcpitch;
672 BYTE *destptr = dest + y * destpitch;
673 for(x = 0;x < minwidth;x++) {
674 /* extract source color components */
675 if(srcformat->type == FORMAT_ARGB) get_relevant_argb_components(&conv_info, *(const DWORD*)srcptr, channels);
677 /* recombine the components */
678 if(destformat->type == FORMAT_ARGB) make_argb_color(&conv_info, channels, (DWORD*)destptr);
680 srcptr += srcformat->bytes_per_pixel;
681 destptr += destformat->bytes_per_pixel;
684 if(srcsize.x < destsize.x) /* black out remaining pixels */
685 ZeroMemory(destptr, destformat->bytes_per_pixel * (destsize.x - srcsize.x));
687 if(srcsize.y < destsize.y) /* black out remaining pixels */
688 ZeroMemory(dest + srcsize.y * destpitch, destpitch * (destsize.y - srcsize.y));
691 /************************************************************
692 * point_filter_simple_data
694 * Copies the source buffer to the destination buffer, performing
695 * any necessary format conversion, color keying and stretching
696 * using a point filter.
697 * Works only for ARGB formats with 1 - 4 bytes per pixel.
699 static void point_filter_simple_data(CONST BYTE *src, UINT srcpitch, POINT srcsize, CONST PixelFormatDesc *srcformat,
700 BYTE *dest, UINT destpitch, POINT destsize, CONST PixelFormatDesc *destformat)
702 struct argb_conversion_info conv_info;
707 ZeroMemory(channels, sizeof(channels));
708 init_argb_conversion_info(srcformat, destformat, &conv_info);
710 for(y = 0;y < destsize.y;y++) {
711 BYTE *destptr = dest + y * destpitch;
712 const BYTE *bufptr = src + srcpitch * (y * srcsize.y / destsize.y);
714 for(x = 0;x < destsize.x;x++) {
715 const BYTE *srcptr = bufptr + (x * srcsize.x / destsize.x) * srcformat->bytes_per_pixel;
717 /* extract source color components */
718 if(srcformat->type == FORMAT_ARGB) get_relevant_argb_components(&conv_info, *(const DWORD*)srcptr, channels);
720 /* recombine the components */
721 if(destformat->type == FORMAT_ARGB) make_argb_color(&conv_info, channels, (DWORD*)destptr);
723 destptr += destformat->bytes_per_pixel;
728 /************************************************************
729 * D3DXLoadSurfaceFromMemory
731 * Loads data from a given memory chunk into a surface,
732 * applying any of the specified filters.
735 * pDestSurface [I] pointer to the surface
736 * pDestPalette [I] palette to use
737 * pDestRect [I] to be filled area of the surface
738 * pSrcMemory [I] pointer to the source data
739 * SrcFormat [I] format of the source pixel data
740 * SrcPitch [I] number of bytes in a row
741 * pSrcPalette [I] palette used in the source image
742 * pSrcRect [I] area of the source data to load
743 * dwFilter [I] filter to apply on stretching
744 * Colorkey [I] colorkey
747 * Success: D3D_OK, if we successfully load the pixel data into our surface or
748 * if pSrcMemory is NULL but the other parameters are valid
749 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
750 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
751 * if DestRect is invalid
752 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
753 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
756 * pSrcRect specifies the dimensions of the source data;
757 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
760 HRESULT WINAPI D3DXLoadSurfaceFromMemory(LPDIRECT3DSURFACE9 pDestSurface,
761 CONST PALETTEENTRY *pDestPalette,
762 CONST RECT *pDestRect,
766 CONST PALETTEENTRY *pSrcPalette,
767 CONST RECT *pSrcRect,
771 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
772 D3DSURFACE_DESC surfdesc;
773 D3DLOCKED_RECT lockrect;
774 POINT srcsize, destsize;
777 TRACE("(%p, %p, %p, %p, %x, %u, %p, %p %u, %#x)\n", pDestSurface, pDestPalette, pDestRect, pSrcMemory,
778 SrcFormat, SrcPitch, pSrcPalette, pSrcRect, dwFilter, Colorkey);
780 if( !pDestSurface || !pSrcMemory || !pSrcRect ) return D3DERR_INVALIDCALL;
781 if(SrcFormat == D3DFMT_UNKNOWN || pSrcRect->left >= pSrcRect->right || pSrcRect->top >= pSrcRect->bottom) return E_FAIL;
783 if(dwFilter == D3DX_DEFAULT) dwFilter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
785 IDirect3DSurface9_GetDesc(pDestSurface, &surfdesc);
787 srcformatdesc = get_format_info(SrcFormat);
788 destformatdesc = get_format_info(surfdesc.Format);
789 if( srcformatdesc->type == FORMAT_UNKNOWN || srcformatdesc->bytes_per_pixel > 4) return E_NOTIMPL;
790 if(destformatdesc->type == FORMAT_UNKNOWN || destformatdesc->bytes_per_pixel > 4) return E_NOTIMPL;
792 srcsize.x = pSrcRect->right - pSrcRect->left;
793 srcsize.y = pSrcRect->bottom - pSrcRect->top;
795 destsize.x = surfdesc.Width;
796 destsize.y = surfdesc.Height;
798 if(pDestRect->left > pDestRect->right || pDestRect->right > surfdesc.Width) return D3DERR_INVALIDCALL;
799 if(pDestRect->top > pDestRect->bottom || pDestRect->bottom > surfdesc.Height) return D3DERR_INVALIDCALL;
800 if(pDestRect->left < 0 || pDestRect->top < 0) return D3DERR_INVALIDCALL;
801 destsize.x = pDestRect->right - pDestRect->left;
802 destsize.y = pDestRect->bottom - pDestRect->top;
803 if(destsize.x == 0 || destsize.y == 0) return D3D_OK;
806 hr = IDirect3DSurface9_LockRect(pDestSurface, &lockrect, pDestRect, 0);
807 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
809 if((dwFilter & 0xF) == D3DX_FILTER_NONE) {
810 copy_simple_data(pSrcMemory, SrcPitch, srcsize, srcformatdesc,
811 lockrect.pBits, lockrect.Pitch, destsize, destformatdesc);
812 } else /*if((dwFilter & 0xF) == D3DX_FILTER_POINT) */ {
813 /* always apply a point filter until D3DX_FILTER_LINEAR, D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented */
814 point_filter_simple_data(pSrcMemory, SrcPitch, srcsize, srcformatdesc,
815 lockrect.pBits, lockrect.Pitch, destsize, destformatdesc);
818 IDirect3DSurface9_UnlockRect(pDestSurface);
822 /************************************************************
823 * D3DXLoadSurfaceFromSurface
825 * Copies the contents from one surface to another, performing any required
826 * format conversion, resizing or filtering.
829 * pDestSurface [I] pointer to the destination surface
830 * pDestPalette [I] palette to use
831 * pDestRect [I] to be filled area of the surface
832 * pSrcSurface [I] pointer to the source surface
833 * pSrcPalette [I] palette used for the source surface
834 * pSrcRect [I] area of the source data to load
835 * dwFilter [I] filter to apply on resizing
836 * Colorkey [I] any ARGB value or 0 to disable color-keying
840 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
841 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
844 HRESULT WINAPI D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface,
845 CONST PALETTEENTRY *pDestPalette,
846 CONST RECT *pDestRect,
847 LPDIRECT3DSURFACE9 pSrcSurface,
848 CONST PALETTEENTRY *pSrcPalette,
849 CONST RECT *pSrcRect,
855 D3DSURFACE_DESC SrcDesc;
858 TRACE("(%p, %p, %p, %p, %p, %p, %u, %#x): relay\n", pDestSurface, pDestPalette, pDestRect,
859 pSrcSurface, pSrcPalette, pSrcRect, dwFilter, Colorkey);
861 if( !pDestSurface || !pSrcSurface ) return D3DERR_INVALIDCALL;
863 IDirect3DSurface9_GetDesc(pSrcSurface, &SrcDesc);
865 if( !pSrcRect ) SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
866 else rect = *pSrcRect;
868 hr = IDirect3DSurface9_LockRect(pSrcSurface, &lock, NULL, D3DLOCK_READONLY);
869 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
871 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
872 lock.pBits, SrcDesc.Format, lock.Pitch,
873 pSrcPalette, &rect, dwFilter, Colorkey);
875 IDirect3DSurface9_UnlockRect(pSrcSurface);