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"
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 /* dds_header.flags */
36 #define DDS_HEIGHT 0x2
39 #define DDS_PIXELFORMAT 0x1000
40 #define DDS_MIPMAPCOUNT 0x20000
41 #define DDS_LINEARSIZE 0x80000
42 #define DDS_DEPTH 0x800000
45 #define DDS_CAPS_COMPLEX 0x8
46 #define DDS_CAPS_TEXTURE 0x1000
47 #define DDS_CAPS_MIPMAP 0x400000
49 /* dds_header.caps2 */
50 #define DDS_CAPS2_CUBEMAP 0x200
51 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
52 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
53 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
54 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
55 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
56 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
57 #define DDS_CAPS2_VOLUME 0x200000
59 /* dds_pixel_format.flags */
60 #define DDS_PF_ALPHA 0x1
61 #define DDS_PF_ALPHA_ONLY 0x2
62 #define DDS_PF_FOURCC 0x4
63 #define DDS_PF_RGB 0x40
64 #define DDS_PF_YUV 0x200
65 #define DDS_PF_LUMINANCE 0x20000
67 struct dds_pixel_format
86 DWORD pitch_or_linear_size
;
90 struct dds_pixel_format pixel_format
;
98 static D3DFORMAT
dds_pixel_format_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
100 FIXME("Pixel format conversion not implemented.\n");
101 return D3DFMT_UNKNOWN
;
104 /************************************************************
105 * get_image_info_from_dds
107 * Fills a D3DXIMAGE_INFO structure with information
108 * about a DDS file stored in the memory.
111 * buffer [I] pointer to DDS data
112 * length [I] size of DDS data
113 * info [O] pointer to D3DXIMAGE_INFO structure
117 * Failure: D3DXERR_INVALIDDATA
120 static HRESULT
get_image_info_from_dds(const void *buffer
, DWORD length
, D3DXIMAGE_INFO
*info
)
122 const struct dds_header
*header
= buffer
;
124 if (length
< sizeof(*header
) || !info
)
125 return D3DXERR_INVALIDDATA
;
127 if (header
->pixel_format
.size
!= sizeof(header
->pixel_format
))
128 return D3DXERR_INVALIDDATA
;
130 info
->Width
= header
->width
;
131 info
->Height
= header
->height
;
133 info
->MipLevels
= (header
->flags
& DDS_MIPMAPCOUNT
) ? header
->miplevels
: 1;
135 info
->Format
= dds_pixel_format_to_d3dformat(&header
->pixel_format
);
136 if (info
->Format
== D3DFMT_UNKNOWN
)
137 return D3DXERR_INVALIDDATA
;
139 if (header
->caps2
& DDS_CAPS2_VOLUME
)
141 info
->Depth
= header
->depth
;
142 info
->ResourceType
= D3DRTYPE_VOLUMETEXTURE
;
144 else if (header
->caps2
& DDS_CAPS2_CUBEMAP
)
146 info
->ResourceType
= D3DRTYPE_CUBETEXTURE
;
150 info
->ResourceType
= D3DRTYPE_TEXTURE
;
153 info
->ImageFileFormat
= D3DXIFF_DDS
;
158 /************************************************************
159 * D3DXGetImageInfoFromFileInMemory
161 * Fills a D3DXIMAGE_INFO structure with info about an image
164 * data [I] pointer to the image file data
165 * datasize [I] size of the passed data
166 * info [O] pointer to the destination structure
169 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
170 * if info is NULL and data and datasize are not NULL
171 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
172 * D3DERR_INVALIDCALL, if data is NULL or
176 * datasize may be bigger than the actual file size
179 HRESULT WINAPI
D3DXGetImageInfoFromFileInMemory(LPCVOID data
, UINT datasize
, D3DXIMAGE_INFO
*info
)
181 IWICImagingFactory
*factory
;
182 IWICBitmapDecoder
*decoder
= NULL
;
187 TRACE("(%p, %d, %p)\n", data
, datasize
, info
);
189 if (!data
|| !datasize
)
190 return D3DERR_INVALIDCALL
;
195 if ((datasize
>= 4) && !strncmp(data
, "DDS ", 4))
196 return get_image_info_from_dds(data
, datasize
, info
);
198 initresult
= CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
200 hr
= CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
);
203 IWICImagingFactory_CreateStream(factory
, &stream
);
204 IWICStream_InitializeFromMemory(stream
, (BYTE
*)data
, datasize
);
205 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
206 IStream_Release(stream
);
207 IWICImagingFactory_Release(factory
);
211 if ((datasize
>= 2) && (!strncmp(data
, "P3", 2) || !strncmp(data
, "P6", 2)))
212 FIXME("File type PPM is not supported yet\n");
213 else if ((datasize
>= 2) && !strncmp(data
, "BM", 2))
214 FIXME("File type DIB is not supported yet\n");
215 else if ((datasize
>= 10) && !strncmp(data
, "#?RADIANCE", 10))
216 FIXME("File type HDR is not supported yet\n");
217 else if ((datasize
>= 2) && (!strncmp(data
, "PF", 2) || !strncmp(data
, "Pf", 2)))
218 FIXME("File type PFM is not supported yet\n");
222 GUID container_format
;
225 hr
= IWICBitmapDecoder_GetContainerFormat(decoder
, &container_format
);
227 if (IsEqualGUID(&container_format
, &GUID_ContainerFormatBmp
)) {
228 TRACE("File type is BMP\n");
229 info
->ImageFileFormat
= D3DXIFF_BMP
;
230 } else if (IsEqualGUID(&container_format
, &GUID_ContainerFormatPng
)) {
231 TRACE("File type is PNG\n");
232 info
->ImageFileFormat
= D3DXIFF_PNG
;
233 } else if(IsEqualGUID(&container_format
, &GUID_ContainerFormatJpeg
)) {
234 TRACE("File type is JPG\n");
235 info
->ImageFileFormat
= D3DXIFF_JPG
;
236 } else if(IsEqualGUID(&container_format
, &GUID_WineContainerFormatTga
)) {
237 TRACE("File type is TGA\n");
238 info
->ImageFileFormat
= D3DXIFF_TGA
;
240 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format
));
241 hr
= D3DXERR_INVALIDDATA
;
246 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
247 if (SUCCEEDED(hr
) && !frame_count
)
248 hr
= D3DXERR_INVALIDDATA
;
251 IWICBitmapFrameDecode
*frame
= NULL
;
253 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
256 hr
= IWICBitmapFrameDecode_GetSize(frame
, &info
->Width
, &info
->Height
);
259 WICPixelFormatGUID pixel_format
;
261 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &pixel_format
);
263 if (IsEqualGUID(&pixel_format
, &GUID_WICPixelFormat1bppIndexed
))
264 info
->Format
= D3DFMT_L8
;
265 else if (IsEqualGUID(&pixel_format
, &GUID_WICPixelFormat4bppIndexed
))
266 info
->Format
= D3DFMT_L8
;
267 else if (IsEqualGUID(&pixel_format
, &GUID_WICPixelFormat8bppIndexed
))
268 info
->Format
= D3DFMT_L8
;
269 else if (IsEqualGUID(&pixel_format
, &GUID_WICPixelFormat16bppBGR555
))
270 info
->Format
= D3DFMT_X1R5G5B5
;
271 else if (IsEqualGUID(&pixel_format
, &GUID_WICPixelFormat24bppBGR
))
272 info
->Format
= D3DFMT_R8G8B8
;
273 else if (IsEqualGUID(&pixel_format
, &GUID_WICPixelFormat32bppBGR
))
274 info
->Format
= D3DFMT_X8R8G8B8
;
275 else if (IsEqualGUID(&pixel_format
, &GUID_WICPixelFormat32bppBGRA
))
276 info
->Format
= D3DFMT_A8R8G8B8
;
278 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format
));
279 hr
= D3DXERR_INVALIDDATA
;
285 IWICBitmapFrameDecode_Release(frame
);
289 info
->ResourceType
= D3DRTYPE_TEXTURE
;
294 IWICBitmapDecoder_Release(decoder
);
296 if (SUCCEEDED(initresult
))
300 TRACE("Invalid or unsupported image file\n");
301 return D3DXERR_INVALIDDATA
;
307 /************************************************************
308 * D3DXGetImageInfoFromFile
311 * Success: D3D_OK, if we successfully load a valid image file or
312 * if we successfully load a file which is no valid image and info is NULL
313 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
314 * if file is not a valid image file and info is not NULL
315 * D3DERR_INVALIDCALL, if file is NULL
318 HRESULT WINAPI
D3DXGetImageInfoFromFileA(LPCSTR file
, D3DXIMAGE_INFO
*info
)
324 TRACE("(%s, %p): relay\n", debugstr_a(file
), info
);
326 if( !file
) return D3DERR_INVALIDCALL
;
328 strlength
= MultiByteToWideChar(CP_ACP
, 0, file
, -1, NULL
, 0);
329 widename
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, strlength
* sizeof(WCHAR
));
330 MultiByteToWideChar(CP_ACP
, 0, file
, -1, widename
, strlength
);
332 hr
= D3DXGetImageInfoFromFileW(widename
, info
);
333 HeapFree(GetProcessHeap(), 0, widename
);
338 HRESULT WINAPI
D3DXGetImageInfoFromFileW(LPCWSTR file
, D3DXIMAGE_INFO
*info
)
344 TRACE("(%s, %p): relay\n", debugstr_w(file
), info
);
346 if( !file
) return D3DERR_INVALIDCALL
;
348 hr
= map_view_of_file(file
, &buffer
, &size
);
349 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
351 hr
= D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
352 UnmapViewOfFile(buffer
);
357 /************************************************************
358 * D3DXGetImageInfoFromResource
361 * Success: D3D_OK, if resource is a valid image file
362 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
363 * if we fail to load resource
366 HRESULT WINAPI
D3DXGetImageInfoFromResourceA(HMODULE module
, LPCSTR resource
, D3DXIMAGE_INFO
*info
)
370 TRACE("(%p, %s, %p)\n", module
, debugstr_a(resource
), info
);
372 resinfo
= FindResourceA(module
, resource
, (LPCSTR
)RT_RCDATA
);
378 hr
= load_resource_into_memory(module
, resinfo
, &buffer
, &size
);
379 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
380 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
383 resinfo
= FindResourceA(module
, resource
, (LPCSTR
)RT_BITMAP
);
385 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
388 return D3DXERR_INVALIDDATA
;
391 HRESULT WINAPI
D3DXGetImageInfoFromResourceW(HMODULE module
, LPCWSTR resource
, D3DXIMAGE_INFO
*info
)
395 TRACE("(%p, %s, %p)\n", module
, debugstr_w(resource
), info
);
397 resinfo
= FindResourceW(module
, resource
, (LPCWSTR
)RT_RCDATA
);
403 hr
= load_resource_into_memory(module
, resinfo
, &buffer
, &size
);
404 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
405 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
408 resinfo
= FindResourceW(module
, resource
, (LPCWSTR
)RT_BITMAP
);
410 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
413 return D3DXERR_INVALIDDATA
;
416 /************************************************************
417 * D3DXLoadSurfaceFromFileInMemory
419 * Loads data from a given buffer into a surface and fills a given
420 * D3DXIMAGE_INFO structure with info about the source data.
423 * pDestSurface [I] pointer to the surface
424 * pDestPalette [I] palette to use
425 * pDestRect [I] to be filled area of the surface
426 * pSrcData [I] pointer to the source data
427 * SrcDataSize [I] size of the source data in bytes
428 * pSrcRect [I] area of the source data to load
429 * dwFilter [I] filter to apply on stretching
430 * Colorkey [I] colorkey
431 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
435 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
436 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
439 HRESULT WINAPI
D3DXLoadSurfaceFromFileInMemory(LPDIRECT3DSURFACE9 pDestSurface
,
440 CONST PALETTEENTRY
*pDestPalette
,
441 CONST RECT
*pDestRect
,
444 CONST RECT
*pSrcRect
,
447 D3DXIMAGE_INFO
*pSrcInfo
)
449 D3DXIMAGE_INFO imginfo
;
452 IWICImagingFactory
*factory
;
453 IWICBitmapDecoder
*decoder
;
454 IWICBitmapFrameDecode
*bitmapframe
;
457 const PixelFormatDesc
*formatdesc
;
461 TRACE("(%p, %p, %p, %p, %d, %p, %d, %x, %p)\n", pDestSurface
, pDestPalette
, pDestRect
, pSrcData
,
462 SrcDataSize
, pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
464 if (!pDestSurface
|| !pSrcData
|| !SrcDataSize
)
465 return D3DERR_INVALIDCALL
;
467 hr
= D3DXGetImageInfoFromFileInMemory(pSrcData
, SrcDataSize
, &imginfo
);
472 CoInitializeEx(NULL
, COINIT_APARTMENTTHREADED
);
474 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory
, NULL
, CLSCTX_INPROC_SERVER
, &IID_IWICImagingFactory
, (void**)&factory
)))
477 if (FAILED(IWICImagingFactory_CreateStream(factory
, &stream
)))
479 IWICImagingFactory_Release(factory
);
483 IWICStream_InitializeFromMemory(stream
, (BYTE
*)pSrcData
, SrcDataSize
);
485 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
487 IStream_Release(stream
);
488 IWICImagingFactory_Release(factory
);
493 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &bitmapframe
);
500 wicrect
.X
= pSrcRect
->left
;
501 wicrect
.Y
= pSrcRect
->top
;
502 wicrect
.Width
= pSrcRect
->right
- pSrcRect
->left
;
503 wicrect
.Height
= pSrcRect
->bottom
- pSrcRect
->top
;
509 wicrect
.Width
= imginfo
.Width
;
510 wicrect
.Height
= imginfo
.Height
;
513 SetRect(&rect
, 0, 0, wicrect
.Width
, wicrect
.Height
);
515 formatdesc
= get_format_info(imginfo
.Format
);
517 if (formatdesc
->format
== D3DFMT_UNKNOWN
)
519 FIXME("Unsupported pixel format\n");
520 hr
= D3DXERR_INVALIDDATA
;
527 pitch
= formatdesc
->bytes_per_pixel
* wicrect
.Width
;
528 buffer
= HeapAlloc(GetProcessHeap(), 0, pitch
* wicrect
.Height
);
530 hr
= IWICBitmapFrameDecode_CopyPixels(bitmapframe
, &wicrect
, pitch
,
531 pitch
* wicrect
.Height
, buffer
);
535 hr
= D3DXLoadSurfaceFromMemory(pDestSurface
, pDestPalette
, pDestRect
,
536 buffer
, imginfo
.Format
, pitch
,
537 NULL
, &rect
, dwFilter
, Colorkey
);
540 HeapFree(GetProcessHeap(), 0, buffer
);
543 IWICBitmapFrameDecode_Release(bitmapframe
);
546 IWICBitmapDecoder_Release(decoder
);
552 return D3DXERR_INVALIDDATA
;
560 /************************************************************
561 * D3DXLoadSurfaceFromFile
563 HRESULT WINAPI
D3DXLoadSurfaceFromFileA(LPDIRECT3DSURFACE9 pDestSurface
,
564 CONST PALETTEENTRY
*pDestPalette
,
565 CONST RECT
*pDestRect
,
567 CONST RECT
*pSrcRect
,
570 D3DXIMAGE_INFO
*pSrcInfo
)
576 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface
, pDestPalette
, pDestRect
, debugstr_a(pSrcFile
),
577 pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
579 if( !pSrcFile
|| !pDestSurface
) return D3DERR_INVALIDCALL
;
581 strlength
= MultiByteToWideChar(CP_ACP
, 0, pSrcFile
, -1, NULL
, 0);
582 pWidename
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, strlength
* sizeof(WCHAR
));
583 MultiByteToWideChar(CP_ACP
, 0, pSrcFile
, -1, pWidename
, strlength
);
585 hr
= D3DXLoadSurfaceFromFileW(pDestSurface
, pDestPalette
, pDestRect
, pWidename
, pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
586 HeapFree(GetProcessHeap(), 0, pWidename
);
591 HRESULT WINAPI
D3DXLoadSurfaceFromFileW(LPDIRECT3DSURFACE9 pDestSurface
,
592 CONST PALETTEENTRY
*pDestPalette
,
593 CONST RECT
*pDestRect
,
595 CONST RECT
*pSrcRect
,
598 D3DXIMAGE_INFO
*pSrcInfo
)
604 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface
, pDestPalette
, pDestRect
, debugstr_w(pSrcFile
),
605 pSrcRect
, Filter
, Colorkey
, pSrcInfo
);
607 if( !pSrcFile
|| !pDestSurface
) return D3DERR_INVALIDCALL
;
609 hr
= map_view_of_file(pSrcFile
, &pBuffer
, &dwSize
);
610 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
612 hr
= D3DXLoadSurfaceFromFileInMemory(pDestSurface
, pDestPalette
, pDestRect
, pBuffer
, dwSize
, pSrcRect
, Filter
, Colorkey
, pSrcInfo
);
613 UnmapViewOfFile(pBuffer
);
618 /************************************************************
619 * D3DXLoadSurfaceFromResource
621 HRESULT WINAPI
D3DXLoadSurfaceFromResourceA(LPDIRECT3DSURFACE9 pDestSurface
,
622 CONST PALETTEENTRY
*pDestPalette
,
623 CONST RECT
*pDestRect
,
626 CONST RECT
*pSrcRect
,
629 D3DXIMAGE_INFO
*pSrcInfo
)
633 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface
, pDestPalette
, pDestRect
, hSrcModule
,
634 debugstr_a(pResource
), pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
636 if( !pDestSurface
) return D3DERR_INVALIDCALL
;
638 hResInfo
= FindResourceA(hSrcModule
, pResource
, (LPCSTR
)RT_RCDATA
);
644 hr
= load_resource_into_memory(hSrcModule
, hResInfo
, &pBuffer
, &dwSize
);
645 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
646 return D3DXLoadSurfaceFromFileInMemory(pDestSurface
, pDestPalette
, pDestRect
, pBuffer
, dwSize
, pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
649 hResInfo
= FindResourceA(hSrcModule
, pResource
, (LPCSTR
)RT_BITMAP
);
651 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
654 return D3DXERR_INVALIDDATA
;
657 HRESULT WINAPI
D3DXLoadSurfaceFromResourceW(LPDIRECT3DSURFACE9 pDestSurface
,
658 CONST PALETTEENTRY
*pDestPalette
,
659 CONST RECT
*pDestRect
,
662 CONST RECT
*pSrcRect
,
665 D3DXIMAGE_INFO
*pSrcInfo
)
669 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface
, pDestPalette
, pDestRect
, hSrcModule
,
670 debugstr_w(pResource
), pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
672 if( !pDestSurface
) return D3DERR_INVALIDCALL
;
674 hResInfo
= FindResourceW(hSrcModule
, pResource
, (LPCWSTR
)RT_RCDATA
);
680 hr
= load_resource_into_memory(hSrcModule
, hResInfo
, &pBuffer
, &dwSize
);
681 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
682 return D3DXLoadSurfaceFromFileInMemory(pDestSurface
, pDestPalette
, pDestRect
, pBuffer
, dwSize
, pSrcRect
, dwFilter
, Colorkey
, pSrcInfo
);
685 hResInfo
= FindResourceW(hSrcModule
, pResource
, (LPCWSTR
)RT_BITMAP
);
687 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
690 return D3DXERR_INVALIDDATA
;
694 /************************************************************
695 * helper functions for D3DXLoadSurfaceFromMemory
697 struct argb_conversion_info
699 CONST PixelFormatDesc
*srcformat
;
700 CONST PixelFormatDesc
*destformat
;
701 DWORD srcshift
[4], destshift
[4];
702 DWORD srcmask
[4], destmask
[4];
703 BOOL process_channel
[4];
707 static void init_argb_conversion_info(CONST PixelFormatDesc
*srcformat
, CONST PixelFormatDesc
*destformat
, struct argb_conversion_info
*info
)
710 ZeroMemory(info
->process_channel
, 4 * sizeof(BOOL
));
711 info
->channelmask
= 0;
713 info
->srcformat
= srcformat
;
714 info
->destformat
= destformat
;
716 for(i
= 0;i
< 4;i
++) {
717 /* srcshift is used to extract the _relevant_ components */
718 info
->srcshift
[i
] = srcformat
->shift
[i
] + max( srcformat
->bits
[i
] - destformat
->bits
[i
], 0);
720 /* destshift is used to move the components to the correct position */
721 info
->destshift
[i
] = destformat
->shift
[i
] + max(destformat
->bits
[i
] - srcformat
->bits
[i
], 0);
723 info
->srcmask
[i
] = ((1 << srcformat
->bits
[i
]) - 1) << srcformat
->shift
[i
];
724 info
->destmask
[i
] = ((1 << destformat
->bits
[i
]) - 1) << destformat
->shift
[i
];
726 /* channelmask specifies bits which aren't used in the source format but in the destination one */
727 if(destformat
->bits
[i
]) {
728 if(srcformat
->bits
[i
]) info
->process_channel
[i
] = TRUE
;
729 else info
->channelmask
|= info
->destmask
[i
];
734 static DWORD
dword_from_bytes(CONST BYTE
*src
, UINT bytes_per_pixel
)
737 static BOOL fixme_once
;
739 if(bytes_per_pixel
> sizeof(DWORD
)) {
740 if(!fixme_once
++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel
);
741 bytes_per_pixel
= sizeof(DWORD
);
744 memcpy(&ret
, src
, bytes_per_pixel
);
748 static void dword_to_bytes(BYTE
*dst
, DWORD dword
, UINT bytes_per_pixel
)
750 static BOOL fixme_once
;
752 if(bytes_per_pixel
> sizeof(DWORD
)) {
753 if(!fixme_once
++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel
);
754 ZeroMemory(dst
, bytes_per_pixel
);
755 bytes_per_pixel
= sizeof(DWORD
);
758 memcpy(dst
, &dword
, bytes_per_pixel
);
761 /************************************************************
762 * get_relevant_argb_components
764 * Extracts the relevant components from the source color and
765 * drops the less significant bits if they aren't used by the destination format.
767 static void get_relevant_argb_components(CONST
struct argb_conversion_info
*info
, CONST DWORD col
, DWORD
*out
)
771 if(info
->process_channel
[i
])
772 out
[i
] = (col
& info
->srcmask
[i
]) >> info
->srcshift
[i
];
775 /************************************************************
778 * Recombines the output of get_relevant_argb_components and converts
779 * it to the destination format.
781 static DWORD
make_argb_color(CONST
struct argb_conversion_info
*info
, CONST DWORD
*in
)
786 for(i
= 0;i
< 4;i
++) {
787 if(info
->process_channel
[i
]) {
788 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
790 for(shift
= info
->destshift
[i
]; shift
> info
->destformat
->shift
[i
]; shift
-= info
->srcformat
->bits
[i
]) val
|= in
[i
] << shift
;
791 val
|= (in
[i
] >> (info
->destformat
->shift
[i
] - shift
)) << info
->destformat
->shift
[i
];
794 val
|= info
->channelmask
; /* new channels are set to their maximal value */
798 static void format_to_vec4(const PixelFormatDesc
*format
, const DWORD
*src
, struct vec4
*dst
)
804 mask
= (1 << format
->bits
[1]) - 1;
805 dst
->x
= (float)((*src
>> format
->shift
[1]) & mask
) / mask
;
812 mask
= (1 << format
->bits
[2]) - 1;
813 dst
->y
= (float)((*src
>> format
->shift
[2]) & mask
) / mask
;
820 mask
= (1 << format
->bits
[3]) - 1;
821 dst
->z
= (float)((*src
>> format
->shift
[3]) & mask
) / mask
;
828 mask
= (1 << format
->bits
[0]) - 1;
829 dst
->w
= (float)((*src
>> format
->shift
[0]) & mask
) / mask
;
835 static void format_from_vec4(const PixelFormatDesc
*format
, const struct vec4
*src
, DWORD
*dst
)
840 *dst
|= (DWORD
)(src
->x
* ((1 << format
->bits
[1]) - 1) + 0.5f
) << format
->shift
[1];
842 *dst
|= (DWORD
)(src
->y
* ((1 << format
->bits
[2]) - 1) + 0.5f
) << format
->shift
[2];
844 *dst
|= (DWORD
)(src
->z
* ((1 << format
->bits
[3]) - 1) + 0.5f
) << format
->shift
[3];
846 *dst
|= (DWORD
)(src
->w
* ((1 << format
->bits
[0]) - 1) + 0.5f
) << format
->shift
[0];
849 /************************************************************
852 * Copies the source buffer to the destination buffer, performing
853 * any necessary format conversion and color keying.
854 * Pixels outsize the source rect are blacked out.
855 * Works only for ARGB formats with 1 - 4 bytes per pixel.
857 static void copy_simple_data(const BYTE
*src
, UINT srcpitch
, SIZE src_size
, const PixelFormatDesc
*srcformat
,
858 BYTE
*dest
, UINT destpitch
, SIZE dst_size
, const PixelFormatDesc
*destformat
, D3DCOLOR colorkey
)
860 struct argb_conversion_info conv_info
, ck_conv_info
;
861 const PixelFormatDesc
*ck_format
= NULL
;
862 DWORD channels
[4], pixel
;
863 UINT minwidth
, minheight
;
866 ZeroMemory(channels
, sizeof(channels
));
867 init_argb_conversion_info(srcformat
, destformat
, &conv_info
);
869 minwidth
= (src_size
.cx
< dst_size
.cx
) ? src_size
.cx
: dst_size
.cx
;
870 minheight
= (src_size
.cy
< dst_size
.cy
) ? src_size
.cy
: dst_size
.cy
;
874 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
875 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
876 init_argb_conversion_info(srcformat
, ck_format
, &ck_conv_info
);
879 for(y
= 0;y
< minheight
;y
++) {
880 const BYTE
*srcptr
= src
+ y
* srcpitch
;
881 BYTE
*destptr
= dest
+ y
* destpitch
;
884 for(x
= 0;x
< minwidth
;x
++) {
885 /* extract source color components */
886 pixel
= dword_from_bytes(srcptr
, srcformat
->bytes_per_pixel
);
888 if (!srcformat
->to_rgba
&& !destformat
->from_rgba
)
890 get_relevant_argb_components(&conv_info
, pixel
, channels
);
891 val
= make_argb_color(&conv_info
, channels
);
895 get_relevant_argb_components(&ck_conv_info
, pixel
, channels
);
896 pixel
= make_argb_color(&ck_conv_info
, channels
);
897 if (pixel
== colorkey
)
898 val
&= ~conv_info
.destmask
[0];
903 struct vec4 color
, tmp
;
905 format_to_vec4(srcformat
, &pixel
, &color
);
906 if (srcformat
->to_rgba
)
907 srcformat
->to_rgba(&color
, &tmp
);
913 format_from_vec4(ck_format
, &tmp
, &pixel
);
914 if (pixel
== colorkey
)
918 if (destformat
->from_rgba
)
919 destformat
->from_rgba(&tmp
, &color
);
923 format_from_vec4(destformat
, &color
, &val
);
926 dword_to_bytes(destptr
, val
, destformat
->bytes_per_pixel
);
927 srcptr
+= srcformat
->bytes_per_pixel
;
928 destptr
+= destformat
->bytes_per_pixel
;
931 if (src_size
.cx
< dst_size
.cx
) /* black out remaining pixels */
932 memset(destptr
, 0, destformat
->bytes_per_pixel
* (dst_size
.cx
- src_size
.cx
));
934 if (src_size
.cy
< dst_size
.cy
) /* black out remaining pixels */
935 memset(dest
+ src_size
.cy
* destpitch
, 0, destpitch
* (dst_size
.cy
- src_size
.cy
));
938 /************************************************************
939 * point_filter_simple_data
941 * Copies the source buffer to the destination buffer, performing
942 * any necessary format conversion, color keying and stretching
943 * using a point filter.
944 * Works only for ARGB formats with 1 - 4 bytes per pixel.
946 static void point_filter_simple_data(const BYTE
*src
, UINT srcpitch
, SIZE src_size
, const PixelFormatDesc
*srcformat
,
947 BYTE
*dest
, UINT destpitch
, SIZE dst_size
, const PixelFormatDesc
*destformat
, D3DCOLOR colorkey
)
949 struct argb_conversion_info conv_info
, ck_conv_info
;
950 const PixelFormatDesc
*ck_format
= NULL
;
951 DWORD channels
[4], pixel
;
955 ZeroMemory(channels
, sizeof(channels
));
956 init_argb_conversion_info(srcformat
, destformat
, &conv_info
);
960 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
961 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
962 init_argb_conversion_info(srcformat
, ck_format
, &ck_conv_info
);
965 for (y
= 0; y
< dst_size
.cy
; ++y
)
967 BYTE
*destptr
= dest
+ y
* destpitch
;
968 const BYTE
*bufptr
= src
+ srcpitch
* (y
* src_size
.cy
/ dst_size
.cy
);
970 for (x
= 0; x
< dst_size
.cx
; ++x
)
972 const BYTE
*srcptr
= bufptr
+ (x
* src_size
.cx
/ dst_size
.cx
) * srcformat
->bytes_per_pixel
;
975 /* extract source color components */
976 pixel
= dword_from_bytes(srcptr
, srcformat
->bytes_per_pixel
);
978 if (!srcformat
->to_rgba
&& !destformat
->from_rgba
)
980 get_relevant_argb_components(&conv_info
, pixel
, channels
);
981 val
= make_argb_color(&conv_info
, channels
);
985 get_relevant_argb_components(&ck_conv_info
, pixel
, channels
);
986 pixel
= make_argb_color(&ck_conv_info
, channels
);
987 if (pixel
== colorkey
)
988 val
&= ~conv_info
.destmask
[0];
993 struct vec4 color
, tmp
;
995 format_to_vec4(srcformat
, &pixel
, &color
);
996 if (srcformat
->to_rgba
)
997 srcformat
->to_rgba(&color
, &tmp
);
1003 format_from_vec4(ck_format
, &tmp
, &pixel
);
1004 if (pixel
== colorkey
)
1008 if (destformat
->from_rgba
)
1009 destformat
->from_rgba(&tmp
, &color
);
1013 format_from_vec4(destformat
, &color
, &val
);
1016 dword_to_bytes(destptr
, val
, destformat
->bytes_per_pixel
);
1017 destptr
+= destformat
->bytes_per_pixel
;
1022 /************************************************************
1023 * D3DXLoadSurfaceFromMemory
1025 * Loads data from a given memory chunk into a surface,
1026 * applying any of the specified filters.
1029 * pDestSurface [I] pointer to the surface
1030 * pDestPalette [I] palette to use
1031 * pDestRect [I] to be filled area of the surface
1032 * pSrcMemory [I] pointer to the source data
1033 * SrcFormat [I] format of the source pixel data
1034 * SrcPitch [I] number of bytes in a row
1035 * pSrcPalette [I] palette used in the source image
1036 * pSrcRect [I] area of the source data to load
1037 * dwFilter [I] filter to apply on stretching
1038 * Colorkey [I] colorkey
1041 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1042 * if pSrcMemory is NULL but the other parameters are valid
1043 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1044 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1045 * if DestRect is invalid
1046 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1047 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1050 * pSrcRect specifies the dimensions of the source data;
1051 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1054 HRESULT WINAPI
D3DXLoadSurfaceFromMemory(IDirect3DSurface9
*dst_surface
,
1055 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const void *src_memory
,
1056 D3DFORMAT src_format
, UINT src_pitch
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
,
1057 DWORD filter
, D3DCOLOR color_key
)
1059 CONST PixelFormatDesc
*srcformatdesc
, *destformatdesc
;
1060 D3DSURFACE_DESC surfdesc
;
1061 D3DLOCKED_RECT lockrect
;
1062 SIZE src_size
, dst_size
;
1065 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1066 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_memory
, src_format
,
1067 src_pitch
, src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1069 if (!dst_surface
|| !src_memory
|| !src_rect
)
1070 return D3DERR_INVALIDCALL
;
1071 if (src_format
== D3DFMT_UNKNOWN
1072 || src_rect
->left
>= src_rect
->right
1073 || src_rect
->top
>= src_rect
->bottom
)
1076 if (filter
== D3DX_DEFAULT
)
1077 filter
= D3DX_FILTER_TRIANGLE
| D3DX_FILTER_DITHER
;
1079 IDirect3DSurface9_GetDesc(dst_surface
, &surfdesc
);
1081 src_size
.cx
= src_rect
->right
- src_rect
->left
;
1082 src_size
.cy
= src_rect
->bottom
- src_rect
->top
;
1085 dst_size
.cx
= surfdesc
.Width
;
1086 dst_size
.cy
= surfdesc
.Height
;
1090 if (dst_rect
->left
> dst_rect
->right
|| dst_rect
->right
> surfdesc
.Width
)
1091 return D3DERR_INVALIDCALL
;
1092 if (dst_rect
->top
> dst_rect
->bottom
|| dst_rect
->bottom
> surfdesc
.Height
)
1093 return D3DERR_INVALIDCALL
;
1094 if (dst_rect
->left
< 0 || dst_rect
->top
< 0)
1095 return D3DERR_INVALIDCALL
;
1096 dst_size
.cx
= dst_rect
->right
- dst_rect
->left
;
1097 dst_size
.cy
= dst_rect
->bottom
- dst_rect
->top
;
1098 if (!dst_size
.cx
|| !dst_size
.cy
)
1102 srcformatdesc
= get_format_info(src_format
);
1103 if (srcformatdesc
->type
== FORMAT_UNKNOWN
)
1106 destformatdesc
= get_format_info(surfdesc
.Format
);
1107 if (destformatdesc
->type
== FORMAT_UNKNOWN
)
1110 if (src_format
== surfdesc
.Format
1111 && dst_size
.cx
== src_size
.cx
1112 && dst_size
.cy
== src_size
.cy
) /* Simple copy. */
1114 UINT row_block_count
= ((src_size
.cx
+ srcformatdesc
->block_width
- 1) / srcformatdesc
->block_width
);
1115 UINT row_count
= (src_size
.cy
+ srcformatdesc
->block_height
- 1) / srcformatdesc
->block_height
;
1116 const BYTE
*src_addr
;
1120 if (src_rect
->left
& (srcformatdesc
->block_width
- 1)
1121 || src_rect
->top
& (srcformatdesc
->block_height
- 1)
1122 || (src_rect
->right
& (srcformatdesc
->block_width
- 1)
1123 && src_size
.cx
!= surfdesc
.Width
)
1124 || (src_rect
->bottom
& (srcformatdesc
->block_height
- 1)
1125 && src_size
.cy
!= surfdesc
.Height
))
1127 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect
));
1128 return D3DXERR_INVALIDDATA
;
1131 if (FAILED(hr
= IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1132 return D3DXERR_INVALIDDATA
;
1134 src_addr
= src_memory
;
1135 src_addr
+= (src_rect
->top
/ srcformatdesc
->block_height
) * src_pitch
;
1136 src_addr
+= (src_rect
->left
/ srcformatdesc
->block_width
) * srcformatdesc
->block_byte_count
;
1137 dst_addr
= lockrect
.pBits
;
1139 for (row
= 0; row
< row_count
; ++row
)
1141 memcpy(dst_addr
, src_addr
, row_block_count
* srcformatdesc
->block_byte_count
);
1142 src_addr
+= src_pitch
;
1143 dst_addr
+= lockrect
.Pitch
;
1146 IDirect3DSurface9_UnlockRect(dst_surface
);
1148 else /* Stretching or format conversion. */
1150 if (srcformatdesc
->bytes_per_pixel
> 4)
1152 if (destformatdesc
->bytes_per_pixel
> 4)
1154 if (srcformatdesc
->block_height
!= 1 || srcformatdesc
->block_width
!= 1)
1156 if (destformatdesc
->block_height
!= 1 || destformatdesc
->block_width
!= 1)
1159 if (FAILED(hr
= IDirect3DSurface9_LockRect(dst_surface
, &lockrect
, dst_rect
, 0)))
1160 return D3DXERR_INVALIDDATA
;
1162 if ((filter
& 0xf) == D3DX_FILTER_NONE
)
1164 copy_simple_data(src_memory
, src_pitch
, src_size
, srcformatdesc
,
1165 lockrect
.pBits
, lockrect
.Pitch
, dst_size
, destformatdesc
, color_key
);
1167 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1169 if ((filter
& 0xf) != D3DX_FILTER_POINT
)
1170 FIXME("Unhandled filter %#x.\n", filter
);
1172 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1173 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1174 point_filter_simple_data(src_memory
, src_pitch
, src_size
, srcformatdesc
,
1175 lockrect
.pBits
, lockrect
.Pitch
, dst_size
, destformatdesc
, color_key
);
1178 IDirect3DSurface9_UnlockRect(dst_surface
);
1184 /************************************************************
1185 * D3DXLoadSurfaceFromSurface
1187 * Copies the contents from one surface to another, performing any required
1188 * format conversion, resizing or filtering.
1191 * pDestSurface [I] pointer to the destination surface
1192 * pDestPalette [I] palette to use
1193 * pDestRect [I] to be filled area of the surface
1194 * pSrcSurface [I] pointer to the source surface
1195 * pSrcPalette [I] palette used for the source surface
1196 * pSrcRect [I] area of the source data to load
1197 * dwFilter [I] filter to apply on resizing
1198 * Colorkey [I] any ARGB value or 0 to disable color-keying
1202 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1203 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1206 HRESULT WINAPI
D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface
,
1207 CONST PALETTEENTRY
*pDestPalette
,
1208 CONST RECT
*pDestRect
,
1209 LPDIRECT3DSURFACE9 pSrcSurface
,
1210 CONST PALETTEENTRY
*pSrcPalette
,
1211 CONST RECT
*pSrcRect
,
1216 D3DLOCKED_RECT lock
;
1217 D3DSURFACE_DESC SrcDesc
;
1220 TRACE("(%p, %p, %p, %p, %p, %p, %u, %#x): relay\n", pDestSurface
, pDestPalette
, pDestRect
,
1221 pSrcSurface
, pSrcPalette
, pSrcRect
, dwFilter
, Colorkey
);
1223 if( !pDestSurface
|| !pSrcSurface
) return D3DERR_INVALIDCALL
;
1225 IDirect3DSurface9_GetDesc(pSrcSurface
, &SrcDesc
);
1227 if( !pSrcRect
) SetRect(&rect
, 0, 0, SrcDesc
.Width
, SrcDesc
.Height
);
1228 else rect
= *pSrcRect
;
1230 hr
= IDirect3DSurface9_LockRect(pSrcSurface
, &lock
, NULL
, D3DLOCK_READONLY
);
1231 if(FAILED(hr
)) return D3DXERR_INVALIDDATA
;
1233 hr
= D3DXLoadSurfaceFromMemory(pDestSurface
, pDestPalette
, pDestRect
,
1234 lock
.pBits
, SrcDesc
.Format
, lock
.Pitch
,
1235 pSrcPalette
, &rect
, dwFilter
, Colorkey
);
1237 IDirect3DSurface9_UnlockRect(pSrcSurface
);
1242 HRESULT WINAPI
D3DXSaveSurfaceToFileA(LPCSTR pDestFile
, D3DXIMAGE_FILEFORMAT DestFormat
,
1243 LPDIRECT3DSURFACE9 pSrcSurface
, const PALETTEENTRY
* pSrcPalette
, const RECT
* pSrcRect
)
1245 FIXME("(%p, %d, %p, %p, %p): stub\n", pDestFile
, DestFormat
, pSrcSurface
, pSrcPalette
, pSrcRect
);
1246 return D3DERR_INVALIDCALL
;
1249 HRESULT WINAPI
D3DXSaveSurfaceToFileW(LPCWSTR pDestFile
, D3DXIMAGE_FILEFORMAT DestFormat
,
1250 LPDIRECT3DSURFACE9 pSrcSurface
, const PALETTEENTRY
* pSrcPalette
, const RECT
* pSrcRect
)
1252 FIXME("(%p, %d, %p, %p, %p): stub\n", pDestFile
, DestFormat
, pSrcSurface
, pSrcPalette
, pSrcRect
);
1253 return D3DERR_INVALIDCALL
;