Create this branch to work on loading of different Kernel-Debugger DLL providers...
[reactos.git] / dll / directx / wine / d3dx9_36 / surface.c
1 /*
2 * Copyright (C) 2009 Tony Wasserka
3 *
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.
8 *
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.
13 *
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
17 *
18 */
19
20 #include <wine/debug.h>
21 //#include "wine/unicode.h"
22 #include "d3dx9_36_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
25
26
27 /************************************************************
28 * D3DXGetImageInfoFromFileInMemory
29 *
30 * Fills a D3DXIMAGE_INFO structure with info about an image
31 *
32 * PARAMS
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
36 *
37 * RETURNS
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
42 * if datasize is 0
43 *
44 * NOTES
45 * datasize may be bigger than the actual file size
46 *
47 */
48 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
49 {
50 FIXME("(%p, %d, %p): stub\n", data, datasize, info);
51
52 if(data && datasize && !info) return D3D_OK;
53 if( !data || !datasize ) return D3DERR_INVALIDCALL;
54
55 return E_NOTIMPL;
56 }
57
58 /************************************************************
59 * D3DXGetImageInfoFromFile
60 *
61 * RETURNS
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
67 *
68 */
69 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
70 {
71 LPWSTR widename;
72 HRESULT hr;
73 int strlength;
74 TRACE("(void): relay\n");
75
76 if( !file ) return D3DERR_INVALIDCALL;
77
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);
81
82 hr = D3DXGetImageInfoFromFileW(widename, info);
83 HeapFree(GetProcessHeap(), 0, widename);
84
85 return hr;
86 }
87
88 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
89 {
90 HRESULT hr;
91 DWORD size;
92 LPVOID buffer;
93 TRACE("(void): relay\n");
94
95 if( !file ) return D3DERR_INVALIDCALL;
96
97 hr = map_view_of_file(file, &buffer, &size);
98 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
99
100 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
101 UnmapViewOfFile(buffer);
102
103 return hr;
104 }
105
106 /************************************************************
107 * D3DXGetImageInfoFromResource
108 *
109 * RETURNS
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
113 *
114 */
115 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
116 {
117 HRSRC resinfo;
118 TRACE("(void)\n");
119
120 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
121 if(resinfo) {
122 LPVOID buffer;
123 HRESULT hr;
124 DWORD size;
125
126 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
127 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
128 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
129 }
130
131 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
132 if(resinfo) {
133 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
134 return E_NOTIMPL;
135 }
136 return D3DXERR_INVALIDDATA;
137 }
138
139 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
140 {
141 HRSRC resinfo;
142 TRACE("(void)\n");
143
144 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
145 if(resinfo) {
146 LPVOID buffer;
147 HRESULT hr;
148 DWORD size;
149
150 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
151 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
152 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
153 }
154
155 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
156 if(resinfo) {
157 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
158 return E_NOTIMPL;
159 }
160 return D3DXERR_INVALIDDATA;
161 }
162
163 /************************************************************
164 * D3DXLoadSurfaceFromFileInMemory
165 *
166 * Loads data from a given buffer into a surface and fills a given
167 * D3DXIMAGE_INFO structure with info about the source data.
168 *
169 * PARAMS
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
179 *
180 * RETURNS
181 * Success: D3D_OK
182 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
183 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
184 *
185 */
186 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(LPDIRECT3DSURFACE9 pDestSurface,
187 CONST PALETTEENTRY *pDestPalette,
188 CONST RECT *pDestRect,
189 LPCVOID pSrcData,
190 UINT SrcDataSize,
191 CONST RECT *pSrcRect,
192 DWORD dwFilter,
193 D3DCOLOR Colorkey,
194 D3DXIMAGE_INFO *pSrcInfo)
195 {
196 FIXME("(%p, %p, %p, %p, %d, %p, %d, %x, %p): stub\n", pDestSurface, pDestPalette,
197 pDestRect, pSrcData, SrcDataSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
198
199 if( !pDestSurface || !pSrcData | !SrcDataSize ) return D3DERR_INVALIDCALL;
200 return E_NOTIMPL;
201 }
202
203 /************************************************************
204 * D3DXLoadSurfaceFromFile
205 */
206 HRESULT WINAPI D3DXLoadSurfaceFromFileA(LPDIRECT3DSURFACE9 pDestSurface,
207 CONST PALETTEENTRY *pDestPalette,
208 CONST RECT *pDestRect,
209 LPCSTR pSrcFile,
210 CONST RECT *pSrcRect,
211 DWORD dwFilter,
212 D3DCOLOR Colorkey,
213 D3DXIMAGE_INFO *pSrcInfo)
214 {
215 LPWSTR pWidename;
216 HRESULT hr;
217 int strlength;
218 TRACE("(void): relay\n");
219
220 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
221
222 strlength = MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, NULL, 0);
223 pWidename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
224 MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, pWidename, strlength);
225
226 hr = D3DXLoadSurfaceFromFileW(pDestSurface, pDestPalette, pDestRect, pWidename, pSrcRect, dwFilter, Colorkey, pSrcInfo);
227 HeapFree(GetProcessHeap(), 0, pWidename);
228
229 return hr;
230 }
231
232 HRESULT WINAPI D3DXLoadSurfaceFromFileW(LPDIRECT3DSURFACE9 pDestSurface,
233 CONST PALETTEENTRY *pDestPalette,
234 CONST RECT *pDestRect,
235 LPCWSTR pSrcFile,
236 CONST RECT *pSrcRect,
237 DWORD Filter,
238 D3DCOLOR Colorkey,
239 D3DXIMAGE_INFO *pSrcInfo)
240 {
241 HRESULT hr;
242 DWORD dwSize;
243 LPVOID pBuffer;
244 TRACE("(void): relay\n");
245
246 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
247
248 hr = map_view_of_file(pSrcFile, &pBuffer, &dwSize);
249 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
250
251 hr = D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, Filter, Colorkey, pSrcInfo);
252 UnmapViewOfFile(pBuffer);
253
254 return hr;
255 }
256
257 /************************************************************
258 * D3DXLoadSurfaceFromResource
259 */
260 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(LPDIRECT3DSURFACE9 pDestSurface,
261 CONST PALETTEENTRY *pDestPalette,
262 CONST RECT *pDestRect,
263 HMODULE hSrcModule,
264 LPCSTR pResource,
265 CONST RECT *pSrcRect,
266 DWORD dwFilter,
267 D3DCOLOR Colorkey,
268 D3DXIMAGE_INFO *pSrcInfo)
269 {
270 HRSRC hResInfo;
271 TRACE("(void): relay\n");
272
273 if( !pDestSurface ) return D3DERR_INVALIDCALL;
274
275 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_RCDATA);
276 if(hResInfo) {
277 LPVOID pBuffer;
278 HRESULT hr;
279 DWORD dwSize;
280
281 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
282 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
283 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
284 }
285
286 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_BITMAP);
287 if(hResInfo) {
288 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
289 return E_NOTIMPL;
290 }
291 return D3DXERR_INVALIDDATA;
292 }
293
294 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(LPDIRECT3DSURFACE9 pDestSurface,
295 CONST PALETTEENTRY *pDestPalette,
296 CONST RECT *pDestRect,
297 HMODULE hSrcModule,
298 LPCWSTR pResource,
299 CONST RECT *pSrcRect,
300 DWORD dwFilter,
301 D3DCOLOR Colorkey,
302 D3DXIMAGE_INFO *pSrcInfo)
303 {
304 HRSRC hResInfo;
305 TRACE("(void): relay\n");
306
307 if( !pDestSurface ) return D3DERR_INVALIDCALL;
308
309 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_RCDATA);
310 if(hResInfo) {
311 LPVOID pBuffer;
312 HRESULT hr;
313 DWORD dwSize;
314
315 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
316 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
317 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
318 }
319
320 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_BITMAP);
321 if(hResInfo) {
322 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
323 return E_NOTIMPL;
324 }
325 return D3DXERR_INVALIDDATA;
326 }
327
328
329 /************************************************************
330 * copy_simple_data
331 *
332 * Copies the source buffer to the destination buffer, performing
333 * any necessary format conversion and color keying.
334 * Works only for ARGB formats with 1 - 4 bytes per pixel.
335 */
336 static void copy_simple_data(CONST BYTE *src, UINT srcpitch, POINT srcsize, CONST PixelFormatDesc *srcformat,
337 CONST BYTE *dest, UINT destpitch, POINT destsize, CONST PixelFormatDesc *destformat,
338 DWORD dwFilter)
339 {
340 DWORD srcshift[4], destshift[4];
341 DWORD srcmask[4], destmask[4];
342 BOOL process_channel[4];
343 DWORD channels[4];
344 DWORD channelmask = 0;
345
346 UINT minwidth, minheight;
347 BYTE *srcptr, *destptr;
348 UINT i, x, y;
349
350 ZeroMemory(channels, sizeof(channels));
351 ZeroMemory(process_channel, sizeof(process_channel));
352
353 for(i = 0;i < 4;i++) {
354 /* srcshift is used to extract the _relevant_ components */
355 srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
356
357 /* destshift is used to move the components to the correct position */
358 destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
359
360 srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
361 destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
362
363 /* channelmask specifies bits which aren't used in the source format but in the destination one */
364 if(destformat->bits[i]) {
365 if(srcformat->bits[i]) process_channel[i] = TRUE;
366 else channelmask |= destmask[i];
367 }
368 }
369
370 minwidth = (srcsize.x < destsize.x) ? srcsize.x : destsize.x;
371 minheight = (srcsize.y < destsize.y) ? srcsize.y : destsize.y;
372
373 for(y = 0;y < minheight;y++) {
374 srcptr = (BYTE*)( src + y * srcpitch);
375 destptr = (BYTE*)(dest + y * destpitch);
376 for(x = 0;x < minwidth;x++) {
377 /* extract source color components */
378 if(srcformat->type == FORMAT_ARGB) {
379 const DWORD col = *(DWORD*)srcptr;
380 for(i = 0;i < 4;i++)
381 if(process_channel[i])
382 channels[i] = (col & srcmask[i]) >> srcshift[i];
383 }
384
385 /* recombine the components */
386 if(destformat->type == FORMAT_ARGB) {
387 DWORD* const pixel = (DWORD*)destptr;
388 *pixel = 0;
389
390 for(i = 0;i < 4;i++) {
391 if(process_channel[i]) {
392 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
393 signed int shift;
394 for(shift = destshift[i]; shift > destformat->shift[i]; shift -= srcformat->bits[i]) *pixel |= channels[i] << shift;
395 *pixel |= (channels[i] >> (destformat->shift[i] - shift)) << destformat->shift[i];
396 }
397 }
398 *pixel |= channelmask; /* new channels are set to their maximal value */
399 }
400 srcptr += srcformat->bytes_per_pixel;
401 destptr += destformat->bytes_per_pixel;
402 }
403 }
404 }
405
406 /************************************************************
407 * D3DXLoadSurfaceFromMemory
408 *
409 * Loads data from a given memory chunk into a surface,
410 * applying any of the specified filters.
411 *
412 * PARAMS
413 * pDestSurface [I] pointer to the surface
414 * pDestPalette [I] palette to use
415 * pDestRect [I] to be filled area of the surface
416 * pSrcMemory [I] pointer to the source data
417 * SrcFormat [I] format of the source pixel data
418 * SrcPitch [I] number of bytes in a row
419 * pSrcPalette [I] palette used in the source image
420 * pSrcRect [I] area of the source data to load
421 * dwFilter [I] filter to apply on stretching
422 * Colorkey [I] colorkey
423 *
424 * RETURNS
425 * Success: D3D_OK, if we successfully load the pixel data into our surface or
426 * if pSrcMemory is NULL but the other parameters are valid
427 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
428 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN)
429 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
430 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
431 *
432 * NOTES
433 * pSrcRect specifies the dimensions of the source data;
434 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
435 *
436 */
437 HRESULT WINAPI D3DXLoadSurfaceFromMemory(LPDIRECT3DSURFACE9 pDestSurface,
438 CONST PALETTEENTRY *pDestPalette,
439 CONST RECT *pDestRect,
440 LPCVOID pSrcMemory,
441 D3DFORMAT SrcFormat,
442 UINT SrcPitch,
443 CONST PALETTEENTRY *pSrcPalette,
444 CONST RECT *pSrcRect,
445 DWORD dwFilter,
446 D3DCOLOR Colorkey)
447 {
448 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
449 D3DSURFACE_DESC surfdesc;
450 D3DLOCKED_RECT lockrect;
451 POINT srcsize, destsize;
452 HRESULT hr;
453 TRACE("(void)\n");
454
455 if( !pDestSurface || !pSrcMemory || !pSrcRect ) return D3DERR_INVALIDCALL;
456 if(SrcFormat == D3DFMT_UNKNOWN || pSrcRect->left >= pSrcRect->right || pSrcRect->top >= pSrcRect->bottom) return E_FAIL;
457
458 if(dwFilter != D3DX_FILTER_NONE) return E_NOTIMPL;
459
460 IDirect3DSurface9_GetDesc(pDestSurface, &surfdesc);
461
462 srcformatdesc = get_format_info(SrcFormat);
463 destformatdesc = get_format_info(surfdesc.Format);
464 if( srcformatdesc->type == FORMAT_UNKNOWN || srcformatdesc->bytes_per_pixel > 4) return E_NOTIMPL;
465 if(destformatdesc->type == FORMAT_UNKNOWN || destformatdesc->bytes_per_pixel > 4) return E_NOTIMPL;
466
467 srcsize.x = pSrcRect->right - pSrcRect->left;
468 srcsize.y = pSrcRect->bottom - pSrcRect->top;
469 if( !pDestRect ) {
470 destsize.x = surfdesc.Width;
471 destsize.y = surfdesc.Height;
472 } else {
473 destsize.x = pDestRect->right - pDestRect->left;
474 destsize.y = pDestRect->bottom - pDestRect->top;
475 }
476
477 hr = IDirect3DSurface9_LockRect(pDestSurface, &lockrect, pDestRect, 0);
478 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
479
480 copy_simple_data((CONST BYTE*)pSrcMemory, SrcPitch, srcsize, srcformatdesc,
481 (CONST BYTE*)lockrect.pBits, lockrect.Pitch, destsize, destformatdesc,
482 dwFilter);
483
484 IDirect3DSurface9_UnlockRect(pDestSurface);
485 return D3D_OK;
486 }
487
488 /************************************************************
489 * D3DXLoadSurfaceFromSurface
490 *
491 * Copies the contents from one surface to another, performing any required
492 * format conversion, resizing or filtering.
493 *
494 * PARAMS
495 * pDestSurface [I] pointer to the destination surface
496 * pDestPalette [I] palette to use
497 * pDestRect [I] to be filled area of the surface
498 * pSrcSurface [I] pointer to the source surface
499 * pSrcPalette [I] palette used for the source surface
500 * pSrcRect [I] area of the source data to load
501 * dwFilter [I] filter to apply on resizing
502 * Colorkey [I] any ARGB value or 0 to disable color-keying
503 *
504 * RETURNS
505 * Success: D3D_OK
506 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
507 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
508 *
509 */
510 HRESULT WINAPI D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface,
511 CONST PALETTEENTRY *pDestPalette,
512 CONST RECT *pDestRect,
513 LPDIRECT3DSURFACE9 pSrcSurface,
514 CONST PALETTEENTRY *pSrcPalette,
515 CONST RECT *pSrcRect,
516 DWORD dwFilter,
517 D3DCOLOR Colorkey)
518 {
519 RECT rect;
520 D3DLOCKED_RECT lock;
521 D3DSURFACE_DESC SrcDesc;
522 HRESULT hr;
523 TRACE("(void): relay\n");
524
525 if( !pDestSurface || !pSrcSurface ) return D3DERR_INVALIDCALL;
526
527 IDirect3DSurface9_GetDesc(pSrcSurface, &SrcDesc);
528
529 if( !pSrcRect ) SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
530 else rect = *pSrcRect;
531
532 hr = IDirect3DSurface9_LockRect(pSrcSurface, &lock, NULL, D3DLOCK_READONLY);
533 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
534
535 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
536 lock.pBits, SrcDesc.Format, lock.Pitch,
537 pSrcPalette, &rect, dwFilter, Colorkey);
538
539 IDirect3DSurface9_UnlockRect(pSrcSurface);
540 return hr;
541 }