d3dx9: Add a FIXME for unsupported filter types to D3DXLoadSurfaceFromMemory().
[wine] / dlls / d3dx9_36 / surface.c
1 /*
2  * Copyright (C) 2009-2010 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 #include "initguid.h"
25 #include "wincodec.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
28
29
30 /* Wine-specific WIC GUIDs */
31
32 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
33
34 /************************************************************
35  * D3DXGetImageInfoFromFileInMemory
36  *
37  * Fills a D3DXIMAGE_INFO structure with info about an image
38  *
39  * PARAMS
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
43  *
44  * RETURNS
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
49  *                                if datasize is 0
50  *
51  * NOTES
52  *   datasize may be bigger than the actual file size
53  *
54  */
55 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
56 {
57     IWICImagingFactory *factory;
58     IWICBitmapDecoder *decoder = NULL;
59     IWICStream *stream;
60     HRESULT hr;
61     HRESULT initresult;
62
63     TRACE("(%p, %d, %p)\n", data, datasize, info);
64
65     if (!data || !datasize)
66         return D3DERR_INVALIDCALL;
67
68     if (!info)
69         return D3D_OK;
70
71     initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
72
73     hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
74
75     if (SUCCEEDED(hr)) {
76         IWICImagingFactory_CreateStream(factory, &stream);
77         IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
78         hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
79         IStream_Release(stream);
80         IWICImagingFactory_Release(factory);
81     }
82
83     if (FAILED(hr)) {
84         if ((datasize >= 4) && !strncmp(data, "DDS ", 4))
85             FIXME("File type DDS is not supported yet\n");
86         else if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
87             FIXME("File type PPM is not supported yet\n");
88         else if ((datasize >= 2) && !strncmp(data, "BM", 2))
89             FIXME("File type DIB is not supported yet\n");
90         else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
91             FIXME("File type HDR is not supported yet\n");
92         else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
93             FIXME("File type PFM is not supported yet\n");
94     }
95
96     if (SUCCEEDED(hr)) {
97         GUID container_format;
98         UINT frame_count;
99
100         hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
101         if (SUCCEEDED(hr)) {
102             if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
103                 TRACE("File type is BMP\n");
104                 info->ImageFileFormat = D3DXIFF_BMP;
105             } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
106                 TRACE("File type is PNG\n");
107                 info->ImageFileFormat = D3DXIFF_PNG;
108             } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
109                 TRACE("File type is JPG\n");
110                 info->ImageFileFormat = D3DXIFF_JPG;
111             } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
112                 TRACE("File type is TGA\n");
113                 info->ImageFileFormat = D3DXIFF_TGA;
114             } else {
115                 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
116                 hr = D3DXERR_INVALIDDATA;
117             }
118         }
119
120         if (SUCCEEDED(hr))
121             hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
122         if (SUCCEEDED(hr) && !frame_count)
123             hr = D3DXERR_INVALIDDATA;
124
125         if (SUCCEEDED(hr)) {
126             IWICBitmapFrameDecode *frame = NULL;
127
128             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
129
130             if (SUCCEEDED(hr))
131                 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
132
133             if (SUCCEEDED(hr)) {
134                 WICPixelFormatGUID pixel_format;
135
136                 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
137                 if (SUCCEEDED(hr)) {
138                     if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat1bppIndexed))
139                         info->Format = D3DFMT_L8;
140                     else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat4bppIndexed))
141                         info->Format = D3DFMT_L8;
142                     else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat8bppIndexed))
143                         info->Format = D3DFMT_L8;
144                     else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat16bppBGR555))
145                         info->Format = D3DFMT_X1R5G5B5;
146                     else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat24bppBGR))
147                         info->Format = D3DFMT_R8G8B8;
148                     else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat32bppBGR))
149                         info->Format = D3DFMT_X8R8G8B8;
150                     else if (IsEqualGUID(&pixel_format, &GUID_WICPixelFormat32bppBGRA))
151                         info->Format = D3DFMT_A8R8G8B8;
152                     else {
153                         WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
154                         hr = D3DXERR_INVALIDDATA;
155                     }
156                 }
157             }
158
159             if (frame)
160                  IWICBitmapFrameDecode_Release(frame);
161
162             info->Depth = 1;
163             info->MipLevels = 1;
164             info->ResourceType = D3DRTYPE_TEXTURE;
165         }
166     }
167
168     if (decoder)
169         IWICBitmapDecoder_Release(decoder);
170
171     if (SUCCEEDED(initresult))
172         CoUninitialize();
173
174     if (FAILED(hr)) {
175         TRACE("Invalid or unsupported image file\n");
176         return D3DXERR_INVALIDDATA;
177     }
178
179     return D3D_OK;
180 }
181
182 /************************************************************
183  * D3DXGetImageInfoFromFile
184  *
185  * RETURNS
186  *   Success: D3D_OK, if we successfully load a valid image file or
187  *                    if we successfully load a file which is no valid image and info is NULL
188  *   Failure: D3DXERR_INVALIDDATA, if we fail to load file or
189  *                                 if file is not a valid image file and info is not NULL
190  *            D3DERR_INVALIDCALL, if file is NULL
191  *
192  */
193 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
194 {
195     LPWSTR widename;
196     HRESULT hr;
197     int strlength;
198
199     TRACE("(%s, %p): relay\n", debugstr_a(file), info);
200
201     if( !file ) return D3DERR_INVALIDCALL;
202
203     strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
204     widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
205     MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
206
207     hr = D3DXGetImageInfoFromFileW(widename, info);
208     HeapFree(GetProcessHeap(), 0, widename);
209
210     return hr;
211 }
212
213 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
214 {
215     HRESULT hr;
216     DWORD size;
217     LPVOID buffer;
218
219     TRACE("(%s, %p): relay\n", debugstr_w(file), info);
220
221     if( !file ) return D3DERR_INVALIDCALL;
222
223     hr = map_view_of_file(file, &buffer, &size);
224     if(FAILED(hr)) return D3DXERR_INVALIDDATA;
225
226     hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
227     UnmapViewOfFile(buffer);
228
229     return hr;
230 }
231
232 /************************************************************
233  * D3DXGetImageInfoFromResource
234  *
235  * RETURNS
236  *   Success: D3D_OK, if resource is a valid image file
237  *   Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
238  *                                 if we fail to load resource
239  *
240  */
241 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
242 {
243     HRSRC resinfo;
244
245     TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
246
247     resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
248     if(resinfo) {
249         LPVOID buffer;
250         HRESULT hr;
251         DWORD size;
252
253         hr = load_resource_into_memory(module, resinfo, &buffer, &size);
254         if(FAILED(hr)) return D3DXERR_INVALIDDATA;
255         return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
256     }
257
258     resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
259     if(resinfo) {
260         FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
261         return E_NOTIMPL;
262     }
263     return D3DXERR_INVALIDDATA;
264 }
265
266 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
267 {
268     HRSRC resinfo;
269
270     TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
271
272     resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
273     if(resinfo) {
274         LPVOID buffer;
275         HRESULT hr;
276         DWORD size;
277
278         hr = load_resource_into_memory(module, resinfo, &buffer, &size);
279         if(FAILED(hr)) return D3DXERR_INVALIDDATA;
280         return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
281     }
282
283     resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
284     if(resinfo) {
285         FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
286         return E_NOTIMPL;
287     }
288     return D3DXERR_INVALIDDATA;
289 }
290
291 /************************************************************
292  * D3DXLoadSurfaceFromFileInMemory
293  *
294  * Loads data from a given buffer into a surface and fills a given
295  * D3DXIMAGE_INFO structure with info about the source data.
296  *
297  * PARAMS
298  *   pDestSurface [I] pointer to the surface
299  *   pDestPalette [I] palette to use
300  *   pDestRect    [I] to be filled area of the surface
301  *   pSrcData     [I] pointer to the source data
302  *   SrcDataSize  [I] size of the source data in bytes
303  *   pSrcRect     [I] area of the source data to load
304  *   dwFilter     [I] filter to apply on stretching
305  *   Colorkey     [I] colorkey
306  *   pSrcInfo     [O] pointer to a D3DXIMAGE_INFO structure
307  *
308  * RETURNS
309  *   Success: D3D_OK
310  *   Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
311  *            D3DXERR_INVALIDDATA, if pSrcData is no valid image file
312  *
313  */
314 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(LPDIRECT3DSURFACE9 pDestSurface,
315                                                CONST PALETTEENTRY *pDestPalette,
316                                                CONST RECT *pDestRect,
317                                                LPCVOID pSrcData,
318                                                UINT SrcDataSize,
319                                                CONST RECT *pSrcRect,
320                                                DWORD dwFilter,
321                                                D3DCOLOR Colorkey,
322                                                D3DXIMAGE_INFO *pSrcInfo)
323 {
324     D3DXIMAGE_INFO imginfo;
325     HRESULT hr;
326
327     IWICImagingFactory *factory;
328     IWICBitmapDecoder *decoder;
329     IWICBitmapFrameDecode *bitmapframe;
330     IWICStream *stream;
331
332     const PixelFormatDesc *formatdesc;
333     WICRect wicrect;
334     RECT rect;
335
336     TRACE("(%p, %p, %p, %p, %d, %p, %d, %x, %p)\n", pDestSurface, pDestPalette, pDestRect, pSrcData,
337         SrcDataSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
338
339     if (!pDestSurface || !pSrcData || !SrcDataSize)
340         return D3DERR_INVALIDCALL;
341
342     hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
343
344     if (FAILED(hr))
345         return hr;
346
347     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
348
349     if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
350         goto cleanup_err;
351
352     if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
353     {
354         IWICImagingFactory_Release(factory);
355         goto cleanup_err;
356     }
357
358     IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
359
360     hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
361
362     IStream_Release(stream);
363     IWICImagingFactory_Release(factory);
364
365     if (FAILED(hr))
366         goto cleanup_err;
367
368     hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
369
370     if (FAILED(hr))
371         goto cleanup_bmp;
372
373     if (pSrcRect)
374     {
375         wicrect.X = pSrcRect->left;
376         wicrect.Y = pSrcRect->top;
377         wicrect.Width = pSrcRect->right - pSrcRect->left;
378         wicrect.Height = pSrcRect->bottom - pSrcRect->top;
379     }
380     else
381     {
382         wicrect.X = 0;
383         wicrect.Y = 0;
384         wicrect.Width = imginfo.Width;
385         wicrect.Height = imginfo.Height;
386     }
387
388     SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
389
390     formatdesc = get_format_info(imginfo.Format);
391
392     if (formatdesc->format == D3DFMT_UNKNOWN)
393     {
394         FIXME("Unsupported pixel format\n");
395         hr = D3DXERR_INVALIDDATA;
396     }
397     else
398     {
399         BYTE *buffer;
400         DWORD pitch;
401
402         pitch = formatdesc->bytes_per_pixel * wicrect.Width;
403         buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
404
405         hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
406                                               pitch * wicrect.Height, buffer);
407
408         if (SUCCEEDED(hr))
409         {
410             hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
411                                            buffer, imginfo.Format, pitch,
412                                            NULL, &rect, dwFilter, Colorkey);
413         }
414
415         HeapFree(GetProcessHeap(), 0, buffer);
416     }
417
418     IWICBitmapFrameDecode_Release(bitmapframe);
419
420 cleanup_bmp:
421     IWICBitmapDecoder_Release(decoder);
422
423 cleanup_err:
424     CoUninitialize();
425
426     if (FAILED(hr))
427         return D3DXERR_INVALIDDATA;
428
429     if (pSrcInfo)
430         *pSrcInfo = imginfo;
431
432     return D3D_OK;
433 }
434
435 /************************************************************
436  * D3DXLoadSurfaceFromFile
437  */
438 HRESULT WINAPI D3DXLoadSurfaceFromFileA(LPDIRECT3DSURFACE9 pDestSurface,
439                                         CONST PALETTEENTRY *pDestPalette,
440                                         CONST RECT *pDestRect,
441                                         LPCSTR pSrcFile,
442                                         CONST RECT *pSrcRect,
443                                         DWORD dwFilter,
444                                         D3DCOLOR Colorkey,
445                                         D3DXIMAGE_INFO *pSrcInfo)
446 {
447     LPWSTR pWidename;
448     HRESULT hr;
449     int strlength;
450
451     TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_a(pSrcFile),
452         pSrcRect, dwFilter, Colorkey, pSrcInfo);
453
454     if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
455
456     strlength = MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, NULL, 0);
457     pWidename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
458     MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, pWidename, strlength);
459
460     hr = D3DXLoadSurfaceFromFileW(pDestSurface, pDestPalette, pDestRect, pWidename, pSrcRect, dwFilter, Colorkey, pSrcInfo);
461     HeapFree(GetProcessHeap(), 0, pWidename);
462
463     return hr;
464 }
465
466 HRESULT WINAPI D3DXLoadSurfaceFromFileW(LPDIRECT3DSURFACE9 pDestSurface,
467                                         CONST PALETTEENTRY *pDestPalette,
468                                         CONST RECT *pDestRect,
469                                         LPCWSTR pSrcFile,
470                                         CONST RECT *pSrcRect,
471                                         DWORD Filter,
472                                         D3DCOLOR Colorkey,
473                                         D3DXIMAGE_INFO *pSrcInfo)
474 {
475     HRESULT hr;
476     DWORD dwSize;
477     LPVOID pBuffer;
478
479     TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_w(pSrcFile),
480         pSrcRect, Filter, Colorkey, pSrcInfo);
481
482     if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
483
484     hr = map_view_of_file(pSrcFile, &pBuffer, &dwSize);
485     if(FAILED(hr)) return D3DXERR_INVALIDDATA;
486
487     hr = D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, Filter, Colorkey, pSrcInfo);
488     UnmapViewOfFile(pBuffer);
489
490     return hr;
491 }
492
493 /************************************************************
494  * D3DXLoadSurfaceFromResource
495  */
496 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(LPDIRECT3DSURFACE9 pDestSurface,
497                                             CONST PALETTEENTRY *pDestPalette,
498                                             CONST RECT *pDestRect,
499                                             HMODULE hSrcModule,
500                                             LPCSTR pResource,
501                                             CONST RECT *pSrcRect,
502                                             DWORD dwFilter,
503                                             D3DCOLOR Colorkey,
504                                             D3DXIMAGE_INFO *pSrcInfo)
505 {
506     HRSRC hResInfo;
507
508     TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
509         debugstr_a(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
510
511     if( !pDestSurface ) return D3DERR_INVALIDCALL;
512
513     hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_RCDATA);
514     if(hResInfo) {
515         LPVOID pBuffer;
516         HRESULT hr;
517         DWORD dwSize;
518
519         hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
520         if(FAILED(hr)) return D3DXERR_INVALIDDATA;
521         return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
522     }
523
524     hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_BITMAP);
525     if(hResInfo) {
526         FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
527         return E_NOTIMPL;
528     }
529     return D3DXERR_INVALIDDATA;
530 }
531
532 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(LPDIRECT3DSURFACE9 pDestSurface,
533                                             CONST PALETTEENTRY *pDestPalette,
534                                             CONST RECT *pDestRect,
535                                             HMODULE hSrcModule,
536                                             LPCWSTR pResource,
537                                             CONST RECT *pSrcRect,
538                                             DWORD dwFilter,
539                                             D3DCOLOR Colorkey,
540                                             D3DXIMAGE_INFO *pSrcInfo)
541 {
542     HRSRC hResInfo;
543
544     TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
545         debugstr_w(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
546
547     if( !pDestSurface ) return D3DERR_INVALIDCALL;
548
549     hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_RCDATA);
550     if(hResInfo) {
551         LPVOID pBuffer;
552         HRESULT hr;
553         DWORD dwSize;
554
555         hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
556         if(FAILED(hr)) return D3DXERR_INVALIDDATA;
557         return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
558     }
559
560     hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_BITMAP);
561     if(hResInfo) {
562         FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
563         return E_NOTIMPL;
564     }
565     return D3DXERR_INVALIDDATA;
566 }
567
568
569 /************************************************************
570  * helper functions for D3DXLoadSurfaceFromMemory
571  */
572 struct argb_conversion_info
573 {
574     CONST PixelFormatDesc *srcformat;
575     CONST PixelFormatDesc *destformat;
576     DWORD srcshift[4], destshift[4];
577     DWORD srcmask[4], destmask[4];
578     BOOL process_channel[4];
579     DWORD channelmask;
580 };
581
582 static void init_argb_conversion_info(CONST PixelFormatDesc *srcformat, CONST PixelFormatDesc *destformat, struct argb_conversion_info *info)
583 {
584     UINT i;
585     ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
586     info->channelmask = 0;
587
588     info->srcformat  =  srcformat;
589     info->destformat = destformat;
590
591     for(i = 0;i < 4;i++) {
592         /* srcshift is used to extract the _relevant_ components */
593         info->srcshift[i]  =  srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
594
595         /* destshift is used to move the components to the correct position */
596         info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] -  srcformat->bits[i], 0);
597
598         info->srcmask[i]  = ((1 <<  srcformat->bits[i]) - 1) <<  srcformat->shift[i];
599         info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
600
601         /* channelmask specifies bits which aren't used in the source format but in the destination one */
602         if(destformat->bits[i]) {
603             if(srcformat->bits[i]) info->process_channel[i] = TRUE;
604             else info->channelmask |= info->destmask[i];
605         }
606     }
607 }
608
609 static DWORD dword_from_bytes(CONST BYTE *src, UINT bytes_per_pixel)
610 {
611     DWORD ret = 0;
612     static BOOL fixme_once;
613
614     if(bytes_per_pixel > sizeof(DWORD)) {
615         if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
616         bytes_per_pixel = sizeof(DWORD);
617     }
618
619     memcpy(&ret, src, bytes_per_pixel);
620     return ret;
621 }
622
623 static void dword_to_bytes(BYTE *dst, DWORD dword, UINT bytes_per_pixel)
624 {
625     static BOOL fixme_once;
626
627     if(bytes_per_pixel > sizeof(DWORD)) {
628         if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
629         ZeroMemory(dst, bytes_per_pixel);
630         bytes_per_pixel = sizeof(DWORD);
631     }
632
633     memcpy(dst, &dword, bytes_per_pixel);
634 }
635
636 /************************************************************
637  * get_relevant_argb_components
638  *
639  * Extracts the relevant components from the source color and
640  * drops the less significant bits if they aren't used by the destination format.
641  */
642 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
643 {
644     UINT i = 0;
645     for(;i < 4;i++)
646         if(info->process_channel[i])
647             out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
648 }
649
650 /************************************************************
651  * make_argb_color
652  *
653  * Recombines the output of get_relevant_argb_components and converts
654  * it to the destination format.
655  */
656 static DWORD make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in)
657 {
658     UINT i;
659     DWORD val = 0;
660
661     for(i = 0;i < 4;i++) {
662         if(info->process_channel[i]) {
663             /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
664             signed int shift;
665             for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
666             val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
667         }
668     }
669     val |= info->channelmask;   /* new channels are set to their maximal value */
670     return val;
671 }
672
673 static void format_to_vec4(const PixelFormatDesc *format, const DWORD *src, struct vec4 *dst)
674 {
675     DWORD mask;
676
677     if (format->bits[1])
678     {
679         mask = (1 << format->bits[1]) - 1;
680         dst->x = (float)((*src >> format->shift[1]) & mask) / mask;
681     }
682     else
683         dst->x = 1.0f;
684
685     if (format->bits[2])
686     {
687         mask = (1 << format->bits[2]) - 1;
688         dst->y = (float)((*src >> format->shift[2]) & mask) / mask;
689     }
690     else
691         dst->y = 1.0f;
692
693     if (format->bits[3])
694     {
695         mask = (1 << format->bits[3]) - 1;
696         dst->z = (float)((*src >> format->shift[3]) & mask) / mask;
697     }
698     else
699         dst->z = 1.0f;
700
701     if (format->bits[0])
702     {
703         mask = (1 << format->bits[0]) - 1;
704         dst->w = (float)((*src >> format->shift[0]) & mask) / mask;
705     }
706     else
707         dst->w = 1.0f;
708 }
709
710 static void format_from_vec4(const PixelFormatDesc *format, const struct vec4 *src, DWORD *dst)
711 {
712     *dst = 0;
713
714     if (format->bits[1])
715         *dst |= (DWORD)(src->x * ((1 << format->bits[1]) - 1) + 0.5f) << format->shift[1];
716     if (format->bits[2])
717         *dst |= (DWORD)(src->y * ((1 << format->bits[2]) - 1) + 0.5f) << format->shift[2];
718     if (format->bits[3])
719         *dst |= (DWORD)(src->z * ((1 << format->bits[3]) - 1) + 0.5f) << format->shift[3];
720     if (format->bits[0])
721         *dst |= (DWORD)(src->w * ((1 << format->bits[0]) - 1) + 0.5f) << format->shift[0];
722 }
723
724 /************************************************************
725  * copy_simple_data
726  *
727  * Copies the source buffer to the destination buffer, performing
728  * any necessary format conversion and color keying.
729  * Pixels outsize the source rect are blacked out.
730  * Works only for ARGB formats with 1 - 4 bytes per pixel.
731  */
732 static void copy_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
733         BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
734 {
735     struct argb_conversion_info conv_info, ck_conv_info;
736     const PixelFormatDesc *ck_format = NULL;
737     DWORD channels[4], pixel;
738     UINT minwidth, minheight;
739     UINT x, y;
740
741     ZeroMemory(channels, sizeof(channels));
742     init_argb_conversion_info(srcformat, destformat, &conv_info);
743
744     minwidth = (src_size.cx < dst_size.cx) ? src_size.cx : dst_size.cx;
745     minheight = (src_size.cy < dst_size.cy) ? src_size.cy : dst_size.cy;
746
747     if (colorkey)
748     {
749         /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
750         ck_format = get_format_info(D3DFMT_A8R8G8B8);
751         init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
752     }
753
754     for(y = 0;y < minheight;y++) {
755         const BYTE *srcptr = src + y * srcpitch;
756         BYTE *destptr = dest + y * destpitch;
757         DWORD val;
758
759         for(x = 0;x < minwidth;x++) {
760             /* extract source color components */
761             pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
762
763             if (!srcformat->to_rgba && !destformat->from_rgba)
764             {
765                 get_relevant_argb_components(&conv_info, pixel, channels);
766                 val = make_argb_color(&conv_info, channels);
767
768                 if (colorkey)
769                 {
770                     get_relevant_argb_components(&ck_conv_info, pixel, channels);
771                     pixel = make_argb_color(&ck_conv_info, channels);
772                     if (pixel == colorkey)
773                         val &= ~conv_info.destmask[0];
774                 }
775             }
776             else
777             {
778                 struct vec4 color, tmp;
779
780                 format_to_vec4(srcformat, &pixel, &color);
781                 if (srcformat->to_rgba)
782                     srcformat->to_rgba(&color, &tmp);
783                 else
784                     tmp = color;
785
786                 if (ck_format)
787                 {
788                     format_from_vec4(ck_format, &tmp, &pixel);
789                     if (pixel == colorkey)
790                         tmp.w = 0.0f;
791                 }
792
793                 if (destformat->from_rgba)
794                     destformat->from_rgba(&tmp, &color);
795                 else
796                     color = tmp;
797
798                 format_from_vec4(destformat, &color, &val);
799             }
800
801             dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
802             srcptr  +=  srcformat->bytes_per_pixel;
803             destptr += destformat->bytes_per_pixel;
804         }
805
806         if (src_size.cx < dst_size.cx) /* black out remaining pixels */
807             memset(destptr, 0, destformat->bytes_per_pixel * (dst_size.cx - src_size.cx));
808     }
809     if (src_size.cy < dst_size.cy) /* black out remaining pixels */
810         memset(dest + src_size.cy * destpitch, 0, destpitch * (dst_size.cy - src_size.cy));
811 }
812
813 /************************************************************
814  * point_filter_simple_data
815  *
816  * Copies the source buffer to the destination buffer, performing
817  * any necessary format conversion, color keying and stretching
818  * using a point filter.
819  * Works only for ARGB formats with 1 - 4 bytes per pixel.
820  */
821 static void point_filter_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
822         BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
823 {
824     struct argb_conversion_info conv_info, ck_conv_info;
825     const PixelFormatDesc *ck_format = NULL;
826     DWORD channels[4], pixel;
827
828     UINT x, y;
829
830     ZeroMemory(channels, sizeof(channels));
831     init_argb_conversion_info(srcformat, destformat, &conv_info);
832
833     if (colorkey)
834     {
835         /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
836         ck_format = get_format_info(D3DFMT_A8R8G8B8);
837         init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
838     }
839
840     for (y = 0; y < dst_size.cy; ++y)
841     {
842         BYTE *destptr = dest + y * destpitch;
843         const BYTE *bufptr = src + srcpitch * (y * src_size.cy / dst_size.cy);
844
845         for (x = 0; x < dst_size.cx; ++x)
846         {
847             const BYTE *srcptr = bufptr + (x * src_size.cx / dst_size.cx) * srcformat->bytes_per_pixel;
848             DWORD val;
849
850             /* extract source color components */
851             pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
852
853             if (!srcformat->to_rgba && !destformat->from_rgba)
854             {
855                 get_relevant_argb_components(&conv_info, pixel, channels);
856                 val = make_argb_color(&conv_info, channels);
857
858                 if (colorkey)
859                 {
860                     get_relevant_argb_components(&ck_conv_info, pixel, channels);
861                     pixel = make_argb_color(&ck_conv_info, channels);
862                     if (pixel == colorkey)
863                         val &= ~conv_info.destmask[0];
864                 }
865             }
866             else
867             {
868                 struct vec4 color, tmp;
869
870                 format_to_vec4(srcformat, &pixel, &color);
871                 if (srcformat->to_rgba)
872                     srcformat->to_rgba(&color, &tmp);
873                 else
874                     tmp = color;
875
876                 if (ck_format)
877                 {
878                     format_from_vec4(ck_format, &tmp, &pixel);
879                     if (pixel == colorkey)
880                         tmp.w = 0.0f;
881                 }
882
883                 if (destformat->from_rgba)
884                     destformat->from_rgba(&tmp, &color);
885                 else
886                     color = tmp;
887
888                 format_from_vec4(destformat, &color, &val);
889             }
890
891             dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
892             destptr += destformat->bytes_per_pixel;
893         }
894     }
895 }
896
897 /************************************************************
898  * D3DXLoadSurfaceFromMemory
899  *
900  * Loads data from a given memory chunk into a surface,
901  * applying any of the specified filters.
902  *
903  * PARAMS
904  *   pDestSurface [I] pointer to the surface
905  *   pDestPalette [I] palette to use
906  *   pDestRect    [I] to be filled area of the surface
907  *   pSrcMemory   [I] pointer to the source data
908  *   SrcFormat    [I] format of the source pixel data
909  *   SrcPitch     [I] number of bytes in a row
910  *   pSrcPalette  [I] palette used in the source image
911  *   pSrcRect     [I] area of the source data to load
912  *   dwFilter     [I] filter to apply on stretching
913  *   Colorkey     [I] colorkey
914  *
915  * RETURNS
916  *   Success: D3D_OK, if we successfully load the pixel data into our surface or
917  *                    if pSrcMemory is NULL but the other parameters are valid
918  *   Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
919  *                                if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
920  *                                if DestRect is invalid
921  *            D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
922  *            E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
923  *
924  * NOTES
925  *   pSrcRect specifies the dimensions of the source data;
926  *   negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
927  *
928  */
929 HRESULT WINAPI D3DXLoadSurfaceFromMemory(LPDIRECT3DSURFACE9 pDestSurface,
930                                          CONST PALETTEENTRY *pDestPalette,
931                                          CONST RECT *pDestRect,
932                                          LPCVOID pSrcMemory,
933                                          D3DFORMAT SrcFormat,
934                                          UINT SrcPitch,
935                                          CONST PALETTEENTRY *pSrcPalette,
936                                          CONST RECT *pSrcRect,
937                                          DWORD dwFilter,
938                                          D3DCOLOR Colorkey)
939 {
940     CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
941     D3DSURFACE_DESC surfdesc;
942     D3DLOCKED_RECT lockrect;
943     SIZE src_size, dst_size;
944     HRESULT hr;
945
946     TRACE("(%p, %p, %p, %p, %x, %u, %p, %p %u, %#x)\n", pDestSurface, pDestPalette, pDestRect, pSrcMemory,
947         SrcFormat, SrcPitch, pSrcPalette, pSrcRect, dwFilter, Colorkey);
948
949     if( !pDestSurface || !pSrcMemory || !pSrcRect ) return D3DERR_INVALIDCALL;
950     if(SrcFormat == D3DFMT_UNKNOWN || pSrcRect->left >= pSrcRect->right || pSrcRect->top >= pSrcRect->bottom) return E_FAIL;
951
952     if(dwFilter == D3DX_DEFAULT) dwFilter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
953
954     IDirect3DSurface9_GetDesc(pDestSurface, &surfdesc);
955
956     src_size.cx = pSrcRect->right - pSrcRect->left;
957     src_size.cy = pSrcRect->bottom - pSrcRect->top;
958     if (!pDestRect)
959     {
960         dst_size.cx = surfdesc.Width;
961         dst_size.cy = surfdesc.Height;
962     }
963     else
964     {
965         if(pDestRect->left > pDestRect->right || pDestRect->right > surfdesc.Width) return D3DERR_INVALIDCALL;
966         if(pDestRect->top > pDestRect->bottom || pDestRect->bottom > surfdesc.Height) return D3DERR_INVALIDCALL;
967         if(pDestRect->left < 0 || pDestRect->top < 0) return D3DERR_INVALIDCALL;
968         dst_size.cx = pDestRect->right - pDestRect->left;
969         dst_size.cy = pDestRect->bottom - pDestRect->top;
970         if (!dst_size.cx || !dst_size.cy)
971             return D3D_OK;
972     }
973
974     srcformatdesc = get_format_info(SrcFormat);
975     if (srcformatdesc->type == FORMAT_UNKNOWN)
976         return E_NOTIMPL;
977
978     destformatdesc = get_format_info(surfdesc.Format);
979     if (destformatdesc->type == FORMAT_UNKNOWN)
980         return E_NOTIMPL;
981
982     if (SrcFormat == surfdesc.Format
983             && dst_size.cx == src_size.cx
984             && dst_size.cy == src_size.cy) /* Simple copy. */
985     {
986         UINT row_block_count = ((src_size.cx + srcformatdesc->block_width - 1) / srcformatdesc->block_width);
987         UINT row_count = (src_size.cy + srcformatdesc->block_height - 1) / srcformatdesc->block_height;
988         const BYTE *src_addr;
989         BYTE *dst_addr;
990         UINT row;
991
992         if (pSrcRect->left & (srcformatdesc->block_width - 1)
993                 || pSrcRect->top & (srcformatdesc->block_height - 1)
994                 || (pSrcRect->right & (srcformatdesc->block_width - 1)
995                     && src_size.cx != surfdesc.Width)
996                 || (pSrcRect->bottom & (srcformatdesc->block_height - 1)
997                     && src_size.cy != surfdesc.Height))
998         {
999             WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(pSrcRect));
1000             return D3DXERR_INVALIDDATA;
1001         }
1002
1003         if (FAILED(hr = IDirect3DSurface9_LockRect(pDestSurface, &lockrect, pDestRect, 0)))
1004             return D3DXERR_INVALIDDATA;
1005
1006         src_addr = pSrcMemory;
1007         src_addr += (pSrcRect->top / srcformatdesc->block_height) * SrcPitch;
1008         src_addr += (pSrcRect->left / srcformatdesc->block_width) * srcformatdesc->block_byte_count;
1009         dst_addr = lockrect.pBits;
1010
1011         for (row = 0; row < row_count; ++row)
1012         {
1013             memcpy(dst_addr, src_addr, row_block_count * srcformatdesc->block_byte_count);
1014             src_addr += SrcPitch;
1015             dst_addr += lockrect.Pitch;
1016         }
1017
1018         IDirect3DSurface9_UnlockRect(pDestSurface);
1019     }
1020     else /* Stretching or format conversion. */
1021     {
1022         if (srcformatdesc->bytes_per_pixel > 4)
1023             return E_NOTIMPL;
1024         if (destformatdesc->bytes_per_pixel > 4)
1025             return E_NOTIMPL;
1026         if (srcformatdesc->block_height != 1 || srcformatdesc->block_width != 1)
1027             return E_NOTIMPL;
1028         if (destformatdesc->block_height != 1 || destformatdesc->block_width != 1)
1029             return E_NOTIMPL;
1030
1031         if (FAILED(hr = IDirect3DSurface9_LockRect(pDestSurface, &lockrect, pDestRect, 0)))
1032             return D3DXERR_INVALIDDATA;
1033
1034         if ((dwFilter & 0xf) == D3DX_FILTER_NONE)
1035         {
1036             copy_simple_data(pSrcMemory, SrcPitch, src_size, srcformatdesc,
1037                     lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc,
1038                     Colorkey);
1039         }
1040         else /* if ((dwFilter & 0xf) == D3DX_FILTER_POINT) */
1041         {
1042             if ((dwFilter & 0xf) != D3DX_FILTER_POINT)
1043                 FIXME("Unhandled filter %#x.\n", dwFilter);
1044
1045             /* Always apply a point filter until D3DX_FILTER_LINEAR,
1046              * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1047             point_filter_simple_data(pSrcMemory, SrcPitch, src_size, srcformatdesc,
1048                     lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc,
1049                     Colorkey);
1050         }
1051
1052         IDirect3DSurface9_UnlockRect(pDestSurface);
1053     }
1054
1055     return D3D_OK;
1056 }
1057
1058 /************************************************************
1059  * D3DXLoadSurfaceFromSurface
1060  *
1061  * Copies the contents from one surface to another, performing any required
1062  * format conversion, resizing or filtering.
1063  *
1064  * PARAMS
1065  *   pDestSurface [I] pointer to the destination surface
1066  *   pDestPalette [I] palette to use
1067  *   pDestRect    [I] to be filled area of the surface
1068  *   pSrcSurface  [I] pointer to the source surface
1069  *   pSrcPalette  [I] palette used for the source surface
1070  *   pSrcRect     [I] area of the source data to load
1071  *   dwFilter     [I] filter to apply on resizing
1072  *   Colorkey     [I] any ARGB value or 0 to disable color-keying
1073  *
1074  * RETURNS
1075  *   Success: D3D_OK
1076  *   Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1077  *            D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1078  *
1079  */
1080 HRESULT WINAPI D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface,
1081                                           CONST PALETTEENTRY *pDestPalette,
1082                                           CONST RECT *pDestRect,
1083                                           LPDIRECT3DSURFACE9 pSrcSurface,
1084                                           CONST PALETTEENTRY *pSrcPalette,
1085                                           CONST RECT *pSrcRect,
1086                                           DWORD dwFilter,
1087                                           D3DCOLOR Colorkey)
1088 {
1089     RECT rect;
1090     D3DLOCKED_RECT lock;
1091     D3DSURFACE_DESC SrcDesc;
1092     HRESULT hr;
1093
1094     TRACE("(%p, %p, %p, %p, %p, %p, %u, %#x): relay\n", pDestSurface, pDestPalette, pDestRect,
1095         pSrcSurface, pSrcPalette, pSrcRect, dwFilter, Colorkey);
1096
1097     if( !pDestSurface || !pSrcSurface ) return D3DERR_INVALIDCALL;
1098
1099     IDirect3DSurface9_GetDesc(pSrcSurface, &SrcDesc);
1100
1101     if( !pSrcRect ) SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1102     else rect = *pSrcRect;
1103
1104     hr = IDirect3DSurface9_LockRect(pSrcSurface, &lock, NULL, D3DLOCK_READONLY);
1105     if(FAILED(hr)) return D3DXERR_INVALIDDATA;
1106
1107     hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1108                                    lock.pBits, SrcDesc.Format, lock.Pitch,
1109                                    pSrcPalette, &rect, dwFilter, Colorkey);
1110
1111     IDirect3DSurface9_UnlockRect(pSrcSurface);
1112     return hr;
1113 }
1114
1115
1116 HRESULT WINAPI D3DXSaveSurfaceToFileA(LPCSTR pDestFile, D3DXIMAGE_FILEFORMAT DestFormat,
1117         LPDIRECT3DSURFACE9 pSrcSurface, const PALETTEENTRY* pSrcPalette, const RECT* pSrcRect)
1118 {
1119     FIXME("(%p, %d, %p, %p, %p): stub\n", pDestFile, DestFormat, pSrcSurface, pSrcPalette, pSrcRect);
1120     return D3DERR_INVALIDCALL;
1121 }
1122
1123 HRESULT WINAPI D3DXSaveSurfaceToFileW(LPCWSTR pDestFile, D3DXIMAGE_FILEFORMAT DestFormat,
1124         LPDIRECT3DSURFACE9 pSrcSurface, const PALETTEENTRY* pSrcPalette, const RECT* pSrcRect)
1125 {
1126     FIXME("(%p, %d, %p, %p, %p): stub\n", pDestFile, DestFormat, pSrcSurface, pSrcPalette, pSrcRect);
1127     return D3DERR_INVALIDCALL;
1128 }