d3dx9: Forward D3DXSaveSurfaceToFileA to D3DXSaveSurfaceToFileW.
[wine/multimedia.git] / dlls / d3dx9_36 / surface.c
blobab316cf1a0620d193e27135eea237a247461c18e
1 /*
2 * Copyright (C) 2009-2010 Tony Wasserka
3 * Copyright (C) 2012 Józef Kucia
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/debug.h"
22 #include "wine/unicode.h"
23 #include "d3dx9_36_private.h"
25 #include "initguid.h"
26 #include "wincodec.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
31 /* Wine-specific WIC GUIDs */
32 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
34 static const struct
36 const GUID *wic_guid;
37 D3DFORMAT d3dformat;
38 } wic_pixel_formats[] = {
39 { &GUID_WICPixelFormat8bppIndexed, D3DFMT_L8 },
40 { &GUID_WICPixelFormat1bppIndexed, D3DFMT_L8 },
41 { &GUID_WICPixelFormat4bppIndexed, D3DFMT_L8 },
42 { &GUID_WICPixelFormat16bppBGR555, D3DFMT_X1R5G5B5 },
43 { &GUID_WICPixelFormat16bppBGR565, D3DFMT_R5G6B5 },
44 { &GUID_WICPixelFormat24bppBGR, D3DFMT_R8G8B8 },
45 { &GUID_WICPixelFormat32bppBGR, D3DFMT_X8R8G8B8 },
46 { &GUID_WICPixelFormat32bppBGRA, D3DFMT_A8R8G8B8 }
49 static D3DFORMAT wic_guid_to_d3dformat(const GUID *guid)
51 int i;
53 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
55 if (IsEqualGUID(wic_pixel_formats[i].wic_guid, guid))
56 return wic_pixel_formats[i].d3dformat;
59 return D3DFMT_UNKNOWN;
62 /* dds_header.flags */
63 #define DDS_CAPS 0x1
64 #define DDS_HEIGHT 0x2
65 #define DDS_WIDTH 0x2
66 #define DDS_PITCH 0x8
67 #define DDS_PIXELFORMAT 0x1000
68 #define DDS_MIPMAPCOUNT 0x20000
69 #define DDS_LINEARSIZE 0x80000
70 #define DDS_DEPTH 0x800000
72 /* dds_header.caps */
73 #define DDS_CAPS_COMPLEX 0x8
74 #define DDS_CAPS_TEXTURE 0x1000
75 #define DDS_CAPS_MIPMAP 0x400000
77 /* dds_header.caps2 */
78 #define DDS_CAPS2_CUBEMAP 0x200
79 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
80 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
81 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
82 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
83 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
84 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
85 #define DDS_CAPS2_VOLUME 0x200000
87 /* dds_pixel_format.flags */
88 #define DDS_PF_ALPHA 0x1
89 #define DDS_PF_ALPHA_ONLY 0x2
90 #define DDS_PF_FOURCC 0x4
91 #define DDS_PF_RGB 0x40
92 #define DDS_PF_YUV 0x200
93 #define DDS_PF_LUMINANCE 0x20000
95 struct dds_pixel_format
97 DWORD size;
98 DWORD flags;
99 DWORD fourcc;
100 DWORD bpp;
101 DWORD rmask;
102 DWORD gmask;
103 DWORD bmask;
104 DWORD amask;
107 struct dds_header
109 DWORD signature;
110 DWORD size;
111 DWORD flags;
112 DWORD height;
113 DWORD width;
114 DWORD pitch_or_linear_size;
115 DWORD depth;
116 DWORD miplevels;
117 DWORD reserved[11];
118 struct dds_pixel_format pixel_format;
119 DWORD caps;
120 DWORD caps2;
121 DWORD caps3;
122 DWORD caps4;
123 DWORD reserved2;
126 static D3DFORMAT dds_fourcc_to_d3dformat(DWORD fourcc)
128 int i;
129 static const DWORD known_fourcc[] = {
130 MAKEFOURCC('U','Y','V','Y'),
131 MAKEFOURCC('Y','U','Y','2'),
132 MAKEFOURCC('R','G','B','G'),
133 MAKEFOURCC('G','R','G','B'),
134 MAKEFOURCC('D','X','T','1'),
135 MAKEFOURCC('D','X','T','2'),
136 MAKEFOURCC('D','X','T','3'),
137 MAKEFOURCC('D','X','T','4'),
138 MAKEFOURCC('D','X','T','5')
141 for (i = 0; i < sizeof(known_fourcc) / sizeof(known_fourcc[0]); i++)
143 if (known_fourcc[i] == fourcc)
144 return fourcc;
147 WARN("Unknown FourCC %#x\n", fourcc);
148 return D3DFMT_UNKNOWN;
151 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
153 int i;
154 static const struct {
155 DWORD bpp;
156 DWORD rmask;
157 DWORD gmask;
158 DWORD bmask;
159 DWORD amask;
160 D3DFORMAT format;
161 } rgb_pixel_formats[] = {
162 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 },
163 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 },
164 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 },
165 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 },
166 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 },
167 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 },
168 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 },
169 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 },
170 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 },
171 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 },
172 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 },
173 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 },
174 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 },
177 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
179 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
180 && rgb_pixel_formats[i].rmask == pixel_format->rmask
181 && rgb_pixel_formats[i].gmask == pixel_format->gmask
182 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
184 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
185 return rgb_pixel_formats[i].format;
186 if (rgb_pixel_formats[i].amask == 0)
187 return rgb_pixel_formats[i].format;
191 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
192 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
193 return D3DFMT_UNKNOWN;
196 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
198 if (pixel_format->bpp == 8)
200 if (pixel_format->rmask == 0xff)
201 return D3DFMT_L8;
202 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
203 return D3DFMT_A4L4;
205 if (pixel_format->bpp == 16)
207 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
208 return D3DFMT_A8L8;
211 WARN("Unknown luminance pixel format (bpp %#x, l %#x, a %#x)\n",
212 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
213 return D3DFMT_UNKNOWN;
216 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
218 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
219 return D3DFMT_A8;
221 WARN("Unknown Alpha pixel format (%#x, %#x)\n", pixel_format->bpp, pixel_format->rmask);
222 return D3DFMT_UNKNOWN;
225 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
227 if (pixel_format->flags & DDS_PF_FOURCC)
228 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
229 if (pixel_format->flags & DDS_PF_RGB)
230 return dds_rgb_to_d3dformat(pixel_format);
231 if (pixel_format->flags & DDS_PF_LUMINANCE)
232 return dds_luminance_to_d3dformat(pixel_format);
233 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
234 return dds_alpha_to_d3dformat(pixel_format);
236 WARN("Unknown pixel format (fourcc %#x, bpp %#x, r %#x, g %#x, b %#x, a %#x)\n",
237 pixel_format->fourcc, pixel_format->bpp, pixel_format->rmask, pixel_format->gmask,
238 pixel_format->bmask, pixel_format->amask);
239 return D3DFMT_UNKNOWN;
242 /************************************************************
243 * get_image_info_from_dds
245 * Fills a D3DXIMAGE_INFO structure with information
246 * about a DDS file stored in the memory.
248 * PARAMS
249 * buffer [I] pointer to DDS data
250 * length [I] size of DDS data
251 * info [O] pointer to D3DXIMAGE_INFO structure
253 * RETURNS
254 * Success: D3D_OK
255 * Failure: D3DXERR_INVALIDDATA
258 static HRESULT get_image_info_from_dds(const void *buffer, DWORD length, D3DXIMAGE_INFO *info)
260 const struct dds_header *header = buffer;
262 if (length < sizeof(*header) || !info)
263 return D3DXERR_INVALIDDATA;
265 if (header->pixel_format.size != sizeof(header->pixel_format))
266 return D3DXERR_INVALIDDATA;
268 info->Width = header->width;
269 info->Height = header->height;
270 info->Depth = 1;
271 info->MipLevels = (header->flags & DDS_MIPMAPCOUNT) ? header->miplevels : 1;
273 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
274 if (info->Format == D3DFMT_UNKNOWN)
275 return D3DXERR_INVALIDDATA;
277 if (header->caps2 & DDS_CAPS2_VOLUME)
279 info->Depth = header->depth;
280 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
282 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
284 info->ResourceType = D3DRTYPE_CUBETEXTURE;
286 else
288 info->ResourceType = D3DRTYPE_TEXTURE;
291 info->ImageFileFormat = D3DXIFF_DDS;
293 return D3D_OK;
296 /************************************************************
297 * D3DXGetImageInfoFromFileInMemory
299 * Fills a D3DXIMAGE_INFO structure with info about an image
301 * PARAMS
302 * data [I] pointer to the image file data
303 * datasize [I] size of the passed data
304 * info [O] pointer to the destination structure
306 * RETURNS
307 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
308 * if info is NULL and data and datasize are not NULL
309 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
310 * D3DERR_INVALIDCALL, if data is NULL or
311 * if datasize is 0
313 * NOTES
314 * datasize may be bigger than the actual file size
317 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
319 IWICImagingFactory *factory;
320 IWICBitmapDecoder *decoder = NULL;
321 IWICStream *stream;
322 HRESULT hr;
323 HRESULT initresult;
325 TRACE("(%p, %d, %p)\n", data, datasize, info);
327 if (!data || !datasize)
328 return D3DERR_INVALIDCALL;
330 if (!info)
331 return D3D_OK;
333 if ((datasize >= 4) && !strncmp(data, "DDS ", 4))
334 return get_image_info_from_dds(data, datasize, info);
336 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
338 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
340 if (SUCCEEDED(hr)) {
341 IWICImagingFactory_CreateStream(factory, &stream);
342 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
343 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
344 IStream_Release(stream);
345 IWICImagingFactory_Release(factory);
348 if (FAILED(hr)) {
349 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
350 FIXME("File type PPM is not supported yet\n");
351 else if ((datasize >= 2) && !strncmp(data, "BM", 2))
352 FIXME("File type DIB is not supported yet\n");
353 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
354 FIXME("File type HDR is not supported yet\n");
355 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
356 FIXME("File type PFM is not supported yet\n");
359 if (SUCCEEDED(hr)) {
360 GUID container_format;
361 UINT frame_count;
363 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
364 if (SUCCEEDED(hr)) {
365 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
366 TRACE("File type is BMP\n");
367 info->ImageFileFormat = D3DXIFF_BMP;
368 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
369 TRACE("File type is PNG\n");
370 info->ImageFileFormat = D3DXIFF_PNG;
371 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
372 TRACE("File type is JPG\n");
373 info->ImageFileFormat = D3DXIFF_JPG;
374 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
375 TRACE("File type is TGA\n");
376 info->ImageFileFormat = D3DXIFF_TGA;
377 } else {
378 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
379 hr = D3DXERR_INVALIDDATA;
383 if (SUCCEEDED(hr))
384 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
385 if (SUCCEEDED(hr) && !frame_count)
386 hr = D3DXERR_INVALIDDATA;
388 if (SUCCEEDED(hr)) {
389 IWICBitmapFrameDecode *frame = NULL;
391 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
393 if (SUCCEEDED(hr))
394 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
396 if (SUCCEEDED(hr)) {
397 WICPixelFormatGUID pixel_format;
399 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
400 if (SUCCEEDED(hr)) {
401 info->Format = wic_guid_to_d3dformat(&pixel_format);
402 if (info->Format == D3DFMT_UNKNOWN) {
403 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
404 hr = D3DXERR_INVALIDDATA;
409 if (frame)
410 IWICBitmapFrameDecode_Release(frame);
412 info->Depth = 1;
413 info->MipLevels = 1;
414 info->ResourceType = D3DRTYPE_TEXTURE;
418 if (decoder)
419 IWICBitmapDecoder_Release(decoder);
421 if (SUCCEEDED(initresult))
422 CoUninitialize();
424 if (FAILED(hr)) {
425 TRACE("Invalid or unsupported image file\n");
426 return D3DXERR_INVALIDDATA;
429 return D3D_OK;
432 /************************************************************
433 * D3DXGetImageInfoFromFile
435 * RETURNS
436 * Success: D3D_OK, if we successfully load a valid image file or
437 * if we successfully load a file which is no valid image and info is NULL
438 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
439 * if file is not a valid image file and info is not NULL
440 * D3DERR_INVALIDCALL, if file is NULL
443 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
445 LPWSTR widename;
446 HRESULT hr;
447 int strlength;
449 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
451 if( !file ) return D3DERR_INVALIDCALL;
453 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
454 widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
455 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
457 hr = D3DXGetImageInfoFromFileW(widename, info);
458 HeapFree(GetProcessHeap(), 0, widename);
460 return hr;
463 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
465 HRESULT hr;
466 DWORD size;
467 LPVOID buffer;
469 TRACE("(%s, %p): relay\n", debugstr_w(file), info);
471 if( !file ) return D3DERR_INVALIDCALL;
473 hr = map_view_of_file(file, &buffer, &size);
474 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
476 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
477 UnmapViewOfFile(buffer);
479 return hr;
482 /************************************************************
483 * D3DXGetImageInfoFromResource
485 * RETURNS
486 * Success: D3D_OK, if resource is a valid image file
487 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
488 * if we fail to load resource
491 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
493 HRSRC resinfo;
495 TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
497 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
498 if(resinfo) {
499 LPVOID buffer;
500 HRESULT hr;
501 DWORD size;
503 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
504 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
505 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
508 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
509 if(resinfo) {
510 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
511 return E_NOTIMPL;
513 return D3DXERR_INVALIDDATA;
516 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
518 HRSRC resinfo;
520 TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
522 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
523 if(resinfo) {
524 LPVOID buffer;
525 HRESULT hr;
526 DWORD size;
528 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
529 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
530 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
533 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
534 if(resinfo) {
535 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
536 return E_NOTIMPL;
538 return D3DXERR_INVALIDDATA;
541 /************************************************************
542 * D3DXLoadSurfaceFromFileInMemory
544 * Loads data from a given buffer into a surface and fills a given
545 * D3DXIMAGE_INFO structure with info about the source data.
547 * PARAMS
548 * pDestSurface [I] pointer to the surface
549 * pDestPalette [I] palette to use
550 * pDestRect [I] to be filled area of the surface
551 * pSrcData [I] pointer to the source data
552 * SrcDataSize [I] size of the source data in bytes
553 * pSrcRect [I] area of the source data to load
554 * dwFilter [I] filter to apply on stretching
555 * Colorkey [I] colorkey
556 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
558 * RETURNS
559 * Success: D3D_OK
560 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
561 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
564 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(LPDIRECT3DSURFACE9 pDestSurface,
565 CONST PALETTEENTRY *pDestPalette,
566 CONST RECT *pDestRect,
567 LPCVOID pSrcData,
568 UINT SrcDataSize,
569 CONST RECT *pSrcRect,
570 DWORD dwFilter,
571 D3DCOLOR Colorkey,
572 D3DXIMAGE_INFO *pSrcInfo)
574 D3DXIMAGE_INFO imginfo;
575 HRESULT hr;
577 IWICImagingFactory *factory;
578 IWICBitmapDecoder *decoder;
579 IWICBitmapFrameDecode *bitmapframe;
580 IWICStream *stream;
582 const PixelFormatDesc *formatdesc;
583 WICRect wicrect;
584 RECT rect;
586 TRACE("(%p, %p, %p, %p, %d, %p, %d, %x, %p)\n", pDestSurface, pDestPalette, pDestRect, pSrcData,
587 SrcDataSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
589 if (!pDestSurface || !pSrcData || !SrcDataSize)
590 return D3DERR_INVALIDCALL;
592 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
594 if (FAILED(hr))
595 return hr;
597 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
599 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
600 goto cleanup_err;
602 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
604 IWICImagingFactory_Release(factory);
605 goto cleanup_err;
608 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
610 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
612 IStream_Release(stream);
613 IWICImagingFactory_Release(factory);
615 if (FAILED(hr))
616 goto cleanup_err;
618 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
620 if (FAILED(hr))
621 goto cleanup_bmp;
623 if (pSrcRect)
625 wicrect.X = pSrcRect->left;
626 wicrect.Y = pSrcRect->top;
627 wicrect.Width = pSrcRect->right - pSrcRect->left;
628 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
630 else
632 wicrect.X = 0;
633 wicrect.Y = 0;
634 wicrect.Width = imginfo.Width;
635 wicrect.Height = imginfo.Height;
638 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
640 formatdesc = get_format_info(imginfo.Format);
642 if (formatdesc->format == D3DFMT_UNKNOWN)
644 FIXME("Unsupported pixel format\n");
645 hr = D3DXERR_INVALIDDATA;
647 else
649 BYTE *buffer;
650 DWORD pitch;
652 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
653 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
655 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
656 pitch * wicrect.Height, buffer);
658 if (SUCCEEDED(hr))
660 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
661 buffer, imginfo.Format, pitch,
662 NULL, &rect, dwFilter, Colorkey);
665 HeapFree(GetProcessHeap(), 0, buffer);
668 IWICBitmapFrameDecode_Release(bitmapframe);
670 cleanup_bmp:
671 IWICBitmapDecoder_Release(decoder);
673 cleanup_err:
674 CoUninitialize();
676 if (FAILED(hr))
677 return D3DXERR_INVALIDDATA;
679 if (pSrcInfo)
680 *pSrcInfo = imginfo;
682 return D3D_OK;
685 /************************************************************
686 * D3DXLoadSurfaceFromFile
688 HRESULT WINAPI D3DXLoadSurfaceFromFileA(LPDIRECT3DSURFACE9 pDestSurface,
689 CONST PALETTEENTRY *pDestPalette,
690 CONST RECT *pDestRect,
691 LPCSTR pSrcFile,
692 CONST RECT *pSrcRect,
693 DWORD dwFilter,
694 D3DCOLOR Colorkey,
695 D3DXIMAGE_INFO *pSrcInfo)
697 LPWSTR pWidename;
698 HRESULT hr;
699 int strlength;
701 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_a(pSrcFile),
702 pSrcRect, dwFilter, Colorkey, pSrcInfo);
704 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
706 strlength = MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, NULL, 0);
707 pWidename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
708 MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, pWidename, strlength);
710 hr = D3DXLoadSurfaceFromFileW(pDestSurface, pDestPalette, pDestRect, pWidename, pSrcRect, dwFilter, Colorkey, pSrcInfo);
711 HeapFree(GetProcessHeap(), 0, pWidename);
713 return hr;
716 HRESULT WINAPI D3DXLoadSurfaceFromFileW(LPDIRECT3DSURFACE9 pDestSurface,
717 CONST PALETTEENTRY *pDestPalette,
718 CONST RECT *pDestRect,
719 LPCWSTR pSrcFile,
720 CONST RECT *pSrcRect,
721 DWORD Filter,
722 D3DCOLOR Colorkey,
723 D3DXIMAGE_INFO *pSrcInfo)
725 HRESULT hr;
726 DWORD dwSize;
727 LPVOID pBuffer;
729 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_w(pSrcFile),
730 pSrcRect, Filter, Colorkey, pSrcInfo);
732 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
734 hr = map_view_of_file(pSrcFile, &pBuffer, &dwSize);
735 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
737 hr = D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, Filter, Colorkey, pSrcInfo);
738 UnmapViewOfFile(pBuffer);
740 return hr;
743 /************************************************************
744 * D3DXLoadSurfaceFromResource
746 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(LPDIRECT3DSURFACE9 pDestSurface,
747 CONST PALETTEENTRY *pDestPalette,
748 CONST RECT *pDestRect,
749 HMODULE hSrcModule,
750 LPCSTR pResource,
751 CONST RECT *pSrcRect,
752 DWORD dwFilter,
753 D3DCOLOR Colorkey,
754 D3DXIMAGE_INFO *pSrcInfo)
756 HRSRC hResInfo;
758 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
759 debugstr_a(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
761 if( !pDestSurface ) return D3DERR_INVALIDCALL;
763 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_RCDATA);
764 if(hResInfo) {
765 LPVOID pBuffer;
766 HRESULT hr;
767 DWORD dwSize;
769 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
770 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
771 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
774 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_BITMAP);
775 if(hResInfo) {
776 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
777 return E_NOTIMPL;
779 return D3DXERR_INVALIDDATA;
782 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(LPDIRECT3DSURFACE9 pDestSurface,
783 CONST PALETTEENTRY *pDestPalette,
784 CONST RECT *pDestRect,
785 HMODULE hSrcModule,
786 LPCWSTR pResource,
787 CONST RECT *pSrcRect,
788 DWORD dwFilter,
789 D3DCOLOR Colorkey,
790 D3DXIMAGE_INFO *pSrcInfo)
792 HRSRC hResInfo;
794 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
795 debugstr_w(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
797 if( !pDestSurface ) return D3DERR_INVALIDCALL;
799 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_RCDATA);
800 if(hResInfo) {
801 LPVOID pBuffer;
802 HRESULT hr;
803 DWORD dwSize;
805 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
806 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
807 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
810 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_BITMAP);
811 if(hResInfo) {
812 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
813 return E_NOTIMPL;
815 return D3DXERR_INVALIDDATA;
819 /************************************************************
820 * helper functions for D3DXLoadSurfaceFromMemory
822 struct argb_conversion_info
824 CONST PixelFormatDesc *srcformat;
825 CONST PixelFormatDesc *destformat;
826 DWORD srcshift[4], destshift[4];
827 DWORD srcmask[4], destmask[4];
828 BOOL process_channel[4];
829 DWORD channelmask;
832 static void init_argb_conversion_info(CONST PixelFormatDesc *srcformat, CONST PixelFormatDesc *destformat, struct argb_conversion_info *info)
834 UINT i;
835 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
836 info->channelmask = 0;
838 info->srcformat = srcformat;
839 info->destformat = destformat;
841 for(i = 0;i < 4;i++) {
842 /* srcshift is used to extract the _relevant_ components */
843 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
845 /* destshift is used to move the components to the correct position */
846 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
848 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
849 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
851 /* channelmask specifies bits which aren't used in the source format but in the destination one */
852 if(destformat->bits[i]) {
853 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
854 else info->channelmask |= info->destmask[i];
859 static DWORD dword_from_bytes(CONST BYTE *src, UINT bytes_per_pixel)
861 DWORD ret = 0;
862 static BOOL fixme_once;
864 if(bytes_per_pixel > sizeof(DWORD)) {
865 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
866 bytes_per_pixel = sizeof(DWORD);
869 memcpy(&ret, src, bytes_per_pixel);
870 return ret;
873 static void dword_to_bytes(BYTE *dst, DWORD dword, UINT bytes_per_pixel)
875 static BOOL fixme_once;
877 if(bytes_per_pixel > sizeof(DWORD)) {
878 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
879 ZeroMemory(dst, bytes_per_pixel);
880 bytes_per_pixel = sizeof(DWORD);
883 memcpy(dst, &dword, bytes_per_pixel);
886 /************************************************************
887 * get_relevant_argb_components
889 * Extracts the relevant components from the source color and
890 * drops the less significant bits if they aren't used by the destination format.
892 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
894 UINT i = 0;
895 for(;i < 4;i++)
896 if(info->process_channel[i])
897 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
900 /************************************************************
901 * make_argb_color
903 * Recombines the output of get_relevant_argb_components and converts
904 * it to the destination format.
906 static DWORD make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in)
908 UINT i;
909 DWORD val = 0;
911 for(i = 0;i < 4;i++) {
912 if(info->process_channel[i]) {
913 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
914 signed int shift;
915 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
916 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
919 val |= info->channelmask; /* new channels are set to their maximal value */
920 return val;
923 static void format_to_vec4(const PixelFormatDesc *format, const DWORD *src, struct vec4 *dst)
925 DWORD mask;
927 if (format->bits[1])
929 mask = (1 << format->bits[1]) - 1;
930 dst->x = (float)((*src >> format->shift[1]) & mask) / mask;
932 else
933 dst->x = 1.0f;
935 if (format->bits[2])
937 mask = (1 << format->bits[2]) - 1;
938 dst->y = (float)((*src >> format->shift[2]) & mask) / mask;
940 else
941 dst->y = 1.0f;
943 if (format->bits[3])
945 mask = (1 << format->bits[3]) - 1;
946 dst->z = (float)((*src >> format->shift[3]) & mask) / mask;
948 else
949 dst->z = 1.0f;
951 if (format->bits[0])
953 mask = (1 << format->bits[0]) - 1;
954 dst->w = (float)((*src >> format->shift[0]) & mask) / mask;
956 else
957 dst->w = 1.0f;
960 static void format_from_vec4(const PixelFormatDesc *format, const struct vec4 *src, DWORD *dst)
962 *dst = 0;
964 if (format->bits[1])
965 *dst |= (DWORD)(src->x * ((1 << format->bits[1]) - 1) + 0.5f) << format->shift[1];
966 if (format->bits[2])
967 *dst |= (DWORD)(src->y * ((1 << format->bits[2]) - 1) + 0.5f) << format->shift[2];
968 if (format->bits[3])
969 *dst |= (DWORD)(src->z * ((1 << format->bits[3]) - 1) + 0.5f) << format->shift[3];
970 if (format->bits[0])
971 *dst |= (DWORD)(src->w * ((1 << format->bits[0]) - 1) + 0.5f) << format->shift[0];
974 /************************************************************
975 * copy_simple_data
977 * Copies the source buffer to the destination buffer, performing
978 * any necessary format conversion and color keying.
979 * Pixels outsize the source rect are blacked out.
980 * Works only for ARGB formats with 1 - 4 bytes per pixel.
982 static void copy_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
983 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
985 struct argb_conversion_info conv_info, ck_conv_info;
986 const PixelFormatDesc *ck_format = NULL;
987 DWORD channels[4], pixel;
988 UINT minwidth, minheight;
989 UINT x, y;
991 ZeroMemory(channels, sizeof(channels));
992 init_argb_conversion_info(srcformat, destformat, &conv_info);
994 minwidth = (src_size.cx < dst_size.cx) ? src_size.cx : dst_size.cx;
995 minheight = (src_size.cy < dst_size.cy) ? src_size.cy : dst_size.cy;
997 if (colorkey)
999 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1000 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1001 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1004 for(y = 0;y < minheight;y++) {
1005 const BYTE *srcptr = src + y * srcpitch;
1006 BYTE *destptr = dest + y * destpitch;
1007 DWORD val;
1009 for(x = 0;x < minwidth;x++) {
1010 /* extract source color components */
1011 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1013 if (!srcformat->to_rgba && !destformat->from_rgba)
1015 get_relevant_argb_components(&conv_info, pixel, channels);
1016 val = make_argb_color(&conv_info, channels);
1018 if (colorkey)
1020 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1021 pixel = make_argb_color(&ck_conv_info, channels);
1022 if (pixel == colorkey)
1023 val &= ~conv_info.destmask[0];
1026 else
1028 struct vec4 color, tmp;
1030 format_to_vec4(srcformat, &pixel, &color);
1031 if (srcformat->to_rgba)
1032 srcformat->to_rgba(&color, &tmp);
1033 else
1034 tmp = color;
1036 if (ck_format)
1038 format_from_vec4(ck_format, &tmp, &pixel);
1039 if (pixel == colorkey)
1040 tmp.w = 0.0f;
1043 if (destformat->from_rgba)
1044 destformat->from_rgba(&tmp, &color);
1045 else
1046 color = tmp;
1048 format_from_vec4(destformat, &color, &val);
1051 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1052 srcptr += srcformat->bytes_per_pixel;
1053 destptr += destformat->bytes_per_pixel;
1056 if (src_size.cx < dst_size.cx) /* black out remaining pixels */
1057 memset(destptr, 0, destformat->bytes_per_pixel * (dst_size.cx - src_size.cx));
1059 if (src_size.cy < dst_size.cy) /* black out remaining pixels */
1060 memset(dest + src_size.cy * destpitch, 0, destpitch * (dst_size.cy - src_size.cy));
1063 /************************************************************
1064 * point_filter_simple_data
1066 * Copies the source buffer to the destination buffer, performing
1067 * any necessary format conversion, color keying and stretching
1068 * using a point filter.
1069 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1071 static void point_filter_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
1072 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
1074 struct argb_conversion_info conv_info, ck_conv_info;
1075 const PixelFormatDesc *ck_format = NULL;
1076 DWORD channels[4], pixel;
1078 UINT x, y;
1080 ZeroMemory(channels, sizeof(channels));
1081 init_argb_conversion_info(srcformat, destformat, &conv_info);
1083 if (colorkey)
1085 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1086 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1087 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1090 for (y = 0; y < dst_size.cy; ++y)
1092 BYTE *destptr = dest + y * destpitch;
1093 const BYTE *bufptr = src + srcpitch * (y * src_size.cy / dst_size.cy);
1095 for (x = 0; x < dst_size.cx; ++x)
1097 const BYTE *srcptr = bufptr + (x * src_size.cx / dst_size.cx) * srcformat->bytes_per_pixel;
1098 DWORD val;
1100 /* extract source color components */
1101 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1103 if (!srcformat->to_rgba && !destformat->from_rgba)
1105 get_relevant_argb_components(&conv_info, pixel, channels);
1106 val = make_argb_color(&conv_info, channels);
1108 if (colorkey)
1110 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1111 pixel = make_argb_color(&ck_conv_info, channels);
1112 if (pixel == colorkey)
1113 val &= ~conv_info.destmask[0];
1116 else
1118 struct vec4 color, tmp;
1120 format_to_vec4(srcformat, &pixel, &color);
1121 if (srcformat->to_rgba)
1122 srcformat->to_rgba(&color, &tmp);
1123 else
1124 tmp = color;
1126 if (ck_format)
1128 format_from_vec4(ck_format, &tmp, &pixel);
1129 if (pixel == colorkey)
1130 tmp.w = 0.0f;
1133 if (destformat->from_rgba)
1134 destformat->from_rgba(&tmp, &color);
1135 else
1136 color = tmp;
1138 format_from_vec4(destformat, &color, &val);
1141 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1142 destptr += destformat->bytes_per_pixel;
1147 /************************************************************
1148 * D3DXLoadSurfaceFromMemory
1150 * Loads data from a given memory chunk into a surface,
1151 * applying any of the specified filters.
1153 * PARAMS
1154 * pDestSurface [I] pointer to the surface
1155 * pDestPalette [I] palette to use
1156 * pDestRect [I] to be filled area of the surface
1157 * pSrcMemory [I] pointer to the source data
1158 * SrcFormat [I] format of the source pixel data
1159 * SrcPitch [I] number of bytes in a row
1160 * pSrcPalette [I] palette used in the source image
1161 * pSrcRect [I] area of the source data to load
1162 * dwFilter [I] filter to apply on stretching
1163 * Colorkey [I] colorkey
1165 * RETURNS
1166 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1167 * if pSrcMemory is NULL but the other parameters are valid
1168 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1169 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1170 * if DestRect is invalid
1171 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1172 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1174 * NOTES
1175 * pSrcRect specifies the dimensions of the source data;
1176 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1179 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1180 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1181 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1182 DWORD filter, D3DCOLOR color_key)
1184 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
1185 D3DSURFACE_DESC surfdesc;
1186 D3DLOCKED_RECT lockrect;
1187 SIZE src_size, dst_size;
1188 HRESULT hr;
1190 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1191 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1192 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1194 if (!dst_surface || !src_memory || !src_rect)
1195 return D3DERR_INVALIDCALL;
1196 if (src_format == D3DFMT_UNKNOWN
1197 || src_rect->left >= src_rect->right
1198 || src_rect->top >= src_rect->bottom)
1199 return E_FAIL;
1201 if (filter == D3DX_DEFAULT)
1202 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1204 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1206 src_size.cx = src_rect->right - src_rect->left;
1207 src_size.cy = src_rect->bottom - src_rect->top;
1208 if (!dst_rect)
1210 dst_size.cx = surfdesc.Width;
1211 dst_size.cy = surfdesc.Height;
1213 else
1215 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width)
1216 return D3DERR_INVALIDCALL;
1217 if (dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height)
1218 return D3DERR_INVALIDCALL;
1219 if (dst_rect->left < 0 || dst_rect->top < 0)
1220 return D3DERR_INVALIDCALL;
1221 dst_size.cx = dst_rect->right - dst_rect->left;
1222 dst_size.cy = dst_rect->bottom - dst_rect->top;
1223 if (!dst_size.cx || !dst_size.cy)
1224 return D3D_OK;
1227 srcformatdesc = get_format_info(src_format);
1228 if (srcformatdesc->type == FORMAT_UNKNOWN)
1229 return E_NOTIMPL;
1231 destformatdesc = get_format_info(surfdesc.Format);
1232 if (destformatdesc->type == FORMAT_UNKNOWN)
1233 return E_NOTIMPL;
1235 if (src_format == surfdesc.Format
1236 && dst_size.cx == src_size.cx
1237 && dst_size.cy == src_size.cy) /* Simple copy. */
1239 UINT row_block_count = ((src_size.cx + srcformatdesc->block_width - 1) / srcformatdesc->block_width);
1240 UINT row_count = (src_size.cy + srcformatdesc->block_height - 1) / srcformatdesc->block_height;
1241 const BYTE *src_addr;
1242 BYTE *dst_addr;
1243 UINT row;
1245 if (src_rect->left & (srcformatdesc->block_width - 1)
1246 || src_rect->top & (srcformatdesc->block_height - 1)
1247 || (src_rect->right & (srcformatdesc->block_width - 1)
1248 && src_size.cx != surfdesc.Width)
1249 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1250 && src_size.cy != surfdesc.Height))
1252 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1253 return D3DXERR_INVALIDDATA;
1256 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1257 return D3DXERR_INVALIDDATA;
1259 src_addr = src_memory;
1260 src_addr += (src_rect->top / srcformatdesc->block_height) * src_pitch;
1261 src_addr += (src_rect->left / srcformatdesc->block_width) * srcformatdesc->block_byte_count;
1262 dst_addr = lockrect.pBits;
1264 for (row = 0; row < row_count; ++row)
1266 memcpy(dst_addr, src_addr, row_block_count * srcformatdesc->block_byte_count);
1267 src_addr += src_pitch;
1268 dst_addr += lockrect.Pitch;
1271 IDirect3DSurface9_UnlockRect(dst_surface);
1273 else /* Stretching or format conversion. */
1275 if (srcformatdesc->bytes_per_pixel > 4)
1276 return E_NOTIMPL;
1277 if (destformatdesc->bytes_per_pixel > 4)
1278 return E_NOTIMPL;
1279 if (srcformatdesc->block_height != 1 || srcformatdesc->block_width != 1)
1280 return E_NOTIMPL;
1281 if (destformatdesc->block_height != 1 || destformatdesc->block_width != 1)
1282 return E_NOTIMPL;
1284 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1285 return D3DXERR_INVALIDDATA;
1287 if ((filter & 0xf) == D3DX_FILTER_NONE)
1289 copy_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1290 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1292 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1294 if ((filter & 0xf) != D3DX_FILTER_POINT)
1295 FIXME("Unhandled filter %#x.\n", filter);
1297 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1298 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1299 point_filter_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1300 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1303 IDirect3DSurface9_UnlockRect(dst_surface);
1306 return D3D_OK;
1309 /************************************************************
1310 * D3DXLoadSurfaceFromSurface
1312 * Copies the contents from one surface to another, performing any required
1313 * format conversion, resizing or filtering.
1315 * PARAMS
1316 * pDestSurface [I] pointer to the destination surface
1317 * pDestPalette [I] palette to use
1318 * pDestRect [I] to be filled area of the surface
1319 * pSrcSurface [I] pointer to the source surface
1320 * pSrcPalette [I] palette used for the source surface
1321 * pSrcRect [I] area of the source data to load
1322 * dwFilter [I] filter to apply on resizing
1323 * Colorkey [I] any ARGB value or 0 to disable color-keying
1325 * RETURNS
1326 * Success: D3D_OK
1327 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1328 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1331 HRESULT WINAPI D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface,
1332 CONST PALETTEENTRY *pDestPalette,
1333 CONST RECT *pDestRect,
1334 LPDIRECT3DSURFACE9 pSrcSurface,
1335 CONST PALETTEENTRY *pSrcPalette,
1336 CONST RECT *pSrcRect,
1337 DWORD dwFilter,
1338 D3DCOLOR Colorkey)
1340 RECT rect;
1341 D3DLOCKED_RECT lock;
1342 D3DSURFACE_DESC SrcDesc;
1343 HRESULT hr;
1345 TRACE("(%p, %p, %p, %p, %p, %p, %u, %#x): relay\n", pDestSurface, pDestPalette, pDestRect,
1346 pSrcSurface, pSrcPalette, pSrcRect, dwFilter, Colorkey);
1348 if( !pDestSurface || !pSrcSurface ) return D3DERR_INVALIDCALL;
1350 IDirect3DSurface9_GetDesc(pSrcSurface, &SrcDesc);
1352 if( !pSrcRect ) SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1353 else rect = *pSrcRect;
1355 hr = IDirect3DSurface9_LockRect(pSrcSurface, &lock, NULL, D3DLOCK_READONLY);
1356 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
1358 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1359 lock.pBits, SrcDesc.Format, lock.Pitch,
1360 pSrcPalette, &rect, dwFilter, Colorkey);
1362 IDirect3DSurface9_UnlockRect(pSrcSurface);
1363 return hr;
1367 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1368 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1370 int len;
1371 WCHAR *filename;
1372 HRESULT hr;
1374 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1375 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1377 if (!dst_filename) return D3DERR_INVALIDCALL;
1379 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1380 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1381 if (!filename) return E_OUTOFMEMORY;
1382 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1384 hr = D3DXSaveSurfaceToFileW(filename, file_format, src_surface, src_palette, src_rect);
1386 HeapFree(GetProcessHeap(), 0, filename);
1387 return hr;
1390 HRESULT WINAPI D3DXSaveSurfaceToFileW(LPCWSTR pDestFile, D3DXIMAGE_FILEFORMAT DestFormat,
1391 LPDIRECT3DSURFACE9 pSrcSurface, const PALETTEENTRY* pSrcPalette, const RECT* pSrcRect)
1393 FIXME("(%p, %d, %p, %p, %p): stub\n", pDestFile, DestFormat, pSrcSurface, pSrcPalette, pSrcRect);
1394 return D3DERR_INVALIDCALL;