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 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
27 /************************************************************
28 * D3DXGetImageInfoFromFileInMemory
30 * Fills a D3DXIMAGE_INFO structure with info about an image
33 * data [I] pointer to the image file data
34 * datasize [I] size of the passed data
35 * info [O] pointer to the destination structure
38 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
39 * if info is NULL and data and datasize are not NULL
40 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
41 * D3DERR_INVALIDCALL, if data is NULL or
45 * datasize may be bigger than the actual file size
48 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
52 if(data && datasize && !info) return D3D_OK;
53 if( !data || !datasize ) return D3DERR_INVALIDCALL;
58 /************************************************************
59 * D3DXGetImageInfoFromFile
62 * Success: D3D_OK, if we successfully load a valid image file or
63 * if we successfully load a file which is no valid image and info is NULL
64 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
65 * if file is not a valid image file and info is not NULL
66 * D3DERR_INVALIDCALL, if file is NULL
69 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
74 TRACE("(void): relay\n");
76 if( !file ) return D3DERR_INVALIDCALL;
78 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
79 widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
80 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
82 hr = D3DXGetImageInfoFromFileW(widename, info);
83 HeapFree(GetProcessHeap(), 0, widename);
88 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
93 TRACE("(void): relay\n");
95 if( !file ) return D3DERR_INVALIDCALL;
97 hr = map_view_of_file(file, &buffer, &size);
98 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
100 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
101 UnmapViewOfFile(buffer);
106 /************************************************************
107 * D3DXGetImageInfoFromResource
110 * Success: D3D_OK, if resource is a valid image file
111 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
112 * if we fail to load resource
115 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
120 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
126 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
127 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
128 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
131 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
133 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
136 return D3DXERR_INVALIDDATA;
139 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
144 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
150 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
151 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
152 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
155 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
157 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
160 return D3DXERR_INVALIDDATA;
163 /************************************************************
164 * D3DXLoadSurfaceFromFileInMemory
166 * Loads data from a given buffer into a surface and fills a given
167 * D3DXIMAGE_INFO structure with info about the source data.
170 * pDestSurface [I] pointer to the surface
171 * pDestPalette [I] palette to use
172 * pDestRect [I] to be filled area of the surface
173 * pSrcData [I] pointer to the source data
174 * SrcDataSize [I] size of the source data in bytes
175 * pSrcRect [I] area of the source data to load
176 * dwFilter [I] filter to apply on stretching
177 * Colorkey [I] colorkey
178 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
182 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
183 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
186 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(LPDIRECT3DSURFACE9 pDestSurface,
187 CONST PALETTEENTRY *pDestPalette,
188 CONST RECT *pDestRect,
191 CONST RECT *pSrcRect,
194 D3DXIMAGE_INFO *pSrcInfo)
197 if( !pDestSurface || !pSrcData | !SrcDataSize ) return D3DERR_INVALIDCALL;
201 /************************************************************
202 * D3DXLoadSurfaceFromFile
204 HRESULT WINAPI D3DXLoadSurfaceFromFileA(LPDIRECT3DSURFACE9 pDestSurface,
205 CONST PALETTEENTRY *pDestPalette,
206 CONST RECT *pDestRect,
208 CONST RECT *pSrcRect,
211 D3DXIMAGE_INFO *pSrcInfo)
216 TRACE("(void): relay\n");
218 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
220 strlength = MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, NULL, 0);
221 pWidename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
222 MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, pWidename, strlength);
224 hr = D3DXLoadSurfaceFromFileW(pDestSurface, pDestPalette, pDestRect, pWidename, pSrcRect, dwFilter, Colorkey, pSrcInfo);
225 HeapFree(GetProcessHeap(), 0, pWidename);
230 HRESULT WINAPI D3DXLoadSurfaceFromFileW(LPDIRECT3DSURFACE9 pDestSurface,
231 CONST PALETTEENTRY *pDestPalette,
232 CONST RECT *pDestRect,
234 CONST RECT *pSrcRect,
237 D3DXIMAGE_INFO *pSrcInfo)
242 TRACE("(void): relay\n");
244 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
246 hr = map_view_of_file(pSrcFile, &pBuffer, &dwSize);
247 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
249 hr = D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, Filter, Colorkey, pSrcInfo);
250 UnmapViewOfFile(pBuffer);
255 /************************************************************
256 * D3DXLoadSurfaceFromResource
258 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(LPDIRECT3DSURFACE9 pDestSurface,
259 CONST PALETTEENTRY *pDestPalette,
260 CONST RECT *pDestRect,
263 CONST RECT *pSrcRect,
266 D3DXIMAGE_INFO *pSrcInfo)
269 TRACE("(void): relay\n");
271 if( !pDestSurface ) return D3DERR_INVALIDCALL;
273 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_RCDATA);
279 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
280 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
281 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
284 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_BITMAP);
286 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
289 return D3DXERR_INVALIDDATA;
292 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(LPDIRECT3DSURFACE9 pDestSurface,
293 CONST PALETTEENTRY *pDestPalette,
294 CONST RECT *pDestRect,
297 CONST RECT *pSrcRect,
300 D3DXIMAGE_INFO *pSrcInfo)
303 TRACE("(void): relay\n");
305 if( !pDestSurface ) return D3DERR_INVALIDCALL;
307 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_RCDATA);
313 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
314 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
315 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
318 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_BITMAP);
320 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
323 return D3DXERR_INVALIDDATA;
327 /************************************************************
330 * Copies the source buffer to the destination buffer, performing
331 * any necessary format conversion and color keying.
332 * Works only for ARGB formats with 1 - 4 bytes per pixel.
334 static void copy_simple_data(CONST BYTE *src, UINT srcpitch, POINT srcsize, CONST PixelFormatDesc *srcformat,
335 CONST BYTE *dest, UINT destpitch, POINT destsize, CONST PixelFormatDesc *destformat,
338 DWORD srcshift[4], destshift[4];
339 DWORD srcmask[4], destmask[4];
340 BOOL process_channel[4];
342 DWORD channelmask = 0;
344 UINT minwidth, minheight;
345 BYTE *srcptr, *destptr;
348 ZeroMemory(channels, sizeof(channels));
349 ZeroMemory(process_channel, sizeof(process_channel));
351 for(i = 0;i < 4;i++) {
352 /* srcshift is used to extract the _relevant_ components */
353 srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
355 /* destshift is used to move the components to the correct position */
356 destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
358 srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
359 destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
361 /* channelmask specifies bits which aren't used in the source format but in the destination one */
362 if(destformat->bits[i]) {
363 if(srcformat->bits[i]) process_channel[i] = TRUE;
364 else channelmask |= destmask[i];
368 minwidth = (srcsize.x < destsize.x) ? srcsize.x : destsize.x;
369 minheight = (srcsize.y < destsize.y) ? srcsize.y : destsize.y;
371 for(y = 0;y < minheight;y++) {
372 srcptr = (BYTE*)( src + y * srcpitch);
373 destptr = (BYTE*)(dest + y * destpitch);
374 for(x = 0;x < minwidth;x++) {
375 /* extract source color components */
376 if(srcformat->type == FORMAT_ARGB) {
377 const DWORD col = *(DWORD*)srcptr;
379 if(process_channel[i])
380 channels[i] = (col & srcmask[i]) >> srcshift[i];
383 /* recombine the components */
384 if(destformat->type == FORMAT_ARGB) {
385 DWORD* const pixel = (DWORD*)destptr;
388 for(i = 0;i < 4;i++) {
389 if(process_channel[i]) {
390 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
392 for(shift = destshift[i]; shift > destformat->shift[i]; shift -= srcformat->bits[i]) *pixel |= channels[i] << shift;
393 *pixel |= (channels[i] >> (destformat->shift[i] - shift)) << destformat->shift[i];
396 *pixel |= channelmask; /* new channels are set to their maximal value */
398 srcptr += srcformat->bytes_per_pixel;
399 destptr += destformat->bytes_per_pixel;
404 /************************************************************
405 * D3DXLoadSurfaceFromMemory
407 * Loads data from a given memory chunk into a surface,
408 * applying any of the specified filters.
411 * pDestSurface [I] pointer to the surface
412 * pDestPalette [I] palette to use
413 * pDestRect [I] to be filled area of the surface
414 * pSrcMemory [I] pointer to the source data
415 * SrcFormat [I] format of the source pixel data
416 * SrcPitch [I] number of bytes in a row
417 * pSrcPalette [I] palette used in the source image
418 * pSrcRect [I] area of the source data to load
419 * dwFilter [I] filter to apply on stretching
420 * Colorkey [I] colorkey
423 * Success: D3D_OK, if we successfully load the pixel data into our surface or
424 * if pSrcMemory is NULL but the other parameters are valid
425 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
426 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN)
427 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
428 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
431 * pSrcRect specifies the dimensions of the source data;
432 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
435 HRESULT WINAPI D3DXLoadSurfaceFromMemory(LPDIRECT3DSURFACE9 pDestSurface,
436 CONST PALETTEENTRY *pDestPalette,
437 CONST RECT *pDestRect,
441 CONST PALETTEENTRY *pSrcPalette,
442 CONST RECT *pSrcRect,
446 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
447 D3DSURFACE_DESC surfdesc;
448 D3DLOCKED_RECT lockrect;
449 POINT srcsize, destsize;
453 if( !pDestSurface || !pSrcMemory || !pSrcRect ) return D3DERR_INVALIDCALL;
454 if(SrcFormat == D3DFMT_UNKNOWN || pSrcRect->left >= pSrcRect->right || pSrcRect->top >= pSrcRect->bottom) return E_FAIL;
456 if(dwFilter != D3DX_FILTER_NONE) return E_NOTIMPL;
458 IDirect3DSurface9_GetDesc(pDestSurface, &surfdesc);
460 srcformatdesc = get_format_info(SrcFormat);
461 destformatdesc = get_format_info(surfdesc.Format);
462 if( srcformatdesc->type == FORMAT_UNKNOWN || srcformatdesc->bytes_per_pixel > 4) return E_NOTIMPL;
463 if(destformatdesc->type == FORMAT_UNKNOWN || destformatdesc->bytes_per_pixel > 4) return E_NOTIMPL;
465 srcsize.x = pSrcRect->right - pSrcRect->left;
466 srcsize.y = pSrcRect->bottom - pSrcRect->top;
468 destsize.x = surfdesc.Width;
469 destsize.y = surfdesc.Height;
471 destsize.x = pDestRect->right - pDestRect->left;
472 destsize.y = pDestRect->bottom - pDestRect->top;
475 hr = IDirect3DSurface9_LockRect(pDestSurface, &lockrect, pDestRect, 0);
476 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
478 copy_simple_data((CONST BYTE*)pSrcMemory, SrcPitch, srcsize, srcformatdesc,
479 (CONST BYTE*)lockrect.pBits, lockrect.Pitch, destsize, destformatdesc,
482 IDirect3DSurface9_UnlockRect(pDestSurface);
486 /************************************************************
487 * D3DXLoadSurfaceFromSurface
489 * Copies the contents from one surface to another, performing any required
490 * format conversion, resizing or filtering.
493 * pDestSurface [I] pointer to the destination surface
494 * pDestPalette [I] palette to use
495 * pDestRect [I] to be filled area of the surface
496 * pSrcSurface [I] pointer to the source surface
497 * pSrcPalette [I] palette used for the source surface
498 * pSrcRect [I] area of the source data to load
499 * dwFilter [I] filter to apply on resizing
500 * Colorkey [I] any ARGB value or 0 to disable color-keying
504 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
505 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
508 HRESULT WINAPI D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface,
509 CONST PALETTEENTRY *pDestPalette,
510 CONST RECT *pDestRect,
511 LPDIRECT3DSURFACE9 pSrcSurface,
512 CONST PALETTEENTRY *pSrcPalette,
513 CONST RECT *pSrcRect,
519 D3DSURFACE_DESC SrcDesc;
521 TRACE("(void): relay\n");
523 if( !pDestSurface || !pSrcSurface ) return D3DERR_INVALIDCALL;
525 IDirect3DSurface9_GetDesc(pSrcSurface, &SrcDesc);
527 if( !pSrcRect ) SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
528 else rect = *pSrcRect;
530 hr = IDirect3DSurface9_LockRect(pSrcSurface, &lock, NULL, D3DLOCK_READONLY);
531 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
533 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
534 lock.pBits, SrcDesc.Format, lock.Pitch,
535 pSrcPalette, &rect, dwFilter, Colorkey);
537 IDirect3DSurface9_UnlockRect(pSrcSurface);