5 * Copyright (C) 2009-2010 Tony Wasserka
6 * Copyright (C) 2012 Józef Kucia
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "d3dx9_private.h"
32 #endif /* __REACTOS__ */
34 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
36 HRESULT WINAPI
WICCreateImagingFactory_Proxy(UINT
, IWICImagingFactory
**);
38 /* Wine-specific WIC GUIDs */
39 DEFINE_GUID(GUID_WineContainerFormatTga
, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
45 } wic_pixel_formats
[] = {
46 { &GUID_WICPixelFormat8bppIndexed
, D3DFMT_P8
},
47 { &GUID_WICPixelFormat1bppIndexed
, D3DFMT_P8
},
48 { &GUID_WICPixelFormat4bppIndexed
, D3DFMT_P8
},
49 { &GUID_WICPixelFormat8bppGray
, D3DFMT_L8
},
50 { &GUID_WICPixelFormat16bppBGR555
, D3DFMT_X1R5G5B5
},
51 { &GUID_WICPixelFormat16bppBGR565
, D3DFMT_R5G6B5
},
52 { &GUID_WICPixelFormat24bppBGR
, D3DFMT_R8G8B8
},
53 { &GUID_WICPixelFormat32bppBGR
, D3DFMT_X8R8G8B8
},
54 { &GUID_WICPixelFormat32bppBGRA
, D3DFMT_A8R8G8B8
}
57 static D3DFORMAT
wic_guid_to_d3dformat(const GUID
*guid
)
61 for (i
= 0; i
< ARRAY_SIZE(wic_pixel_formats
); i
++)
63 if (IsEqualGUID(wic_pixel_formats
[i
].wic_guid
, guid
))
64 return wic_pixel_formats
[i
].d3dformat
;
67 return D3DFMT_UNKNOWN
;
70 static const GUID
*d3dformat_to_wic_guid(D3DFORMAT format
)
74 for (i
= 0; i
< ARRAY_SIZE(wic_pixel_formats
); i
++)
76 if (wic_pixel_formats
[i
].d3dformat
== format
)
77 return wic_pixel_formats
[i
].wic_guid
;
83 /* dds_header.flags */
85 #define DDS_HEIGHT 0x2
88 #define DDS_PIXELFORMAT 0x1000
89 #define DDS_MIPMAPCOUNT 0x20000
90 #define DDS_LINEARSIZE 0x80000
91 #define DDS_DEPTH 0x800000
94 #define DDS_CAPS_COMPLEX 0x8
95 #define DDS_CAPS_TEXTURE 0x1000
96 #define DDS_CAPS_MIPMAP 0x400000
98 /* dds_header.caps2 */
99 #define DDS_CAPS2_CUBEMAP 0x200
100 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
101 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
102 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
103 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
104 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
105 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
106 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
107 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
108 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
109 #define DDS_CAPS2_VOLUME 0x200000
111 /* dds_pixel_format.flags */
112 #define DDS_PF_ALPHA 0x1
113 #define DDS_PF_ALPHA_ONLY 0x2
114 #define DDS_PF_FOURCC 0x4
115 #define DDS_PF_INDEXED 0x20
116 #define DDS_PF_RGB 0x40
117 #define DDS_PF_YUV 0x200
118 #define DDS_PF_LUMINANCE 0x20000
119 #define DDS_PF_BUMPLUMINANCE 0x40000
120 #define DDS_PF_BUMPDUDV 0x80000
122 struct dds_pixel_format
141 DWORD pitch_or_linear_size
;
145 struct dds_pixel_format pixel_format
;
153 static D3DFORMAT
dds_fourcc_to_d3dformat(DWORD fourcc
)
156 static const DWORD known_fourcc
[] = {
168 D3DFMT_A16B16G16R16F
,
171 D3DFMT_A32B32G32R32F
,
174 for (i
= 0; i
< ARRAY_SIZE(known_fourcc
); i
++)
176 if (known_fourcc
[i
] == fourcc
)
180 WARN("Unknown FourCC %#x\n", fourcc
);
181 return D3DFMT_UNKNOWN
;
184 static const struct {
191 } rgb_pixel_formats
[] = {
192 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2
},
193 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5
},
194 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5
},
195 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5
},
196 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4
},
197 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4
},
198 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2
},
199 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8
},
200 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8
},
201 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8
},
202 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10
},
203 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10
},
204 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16
},
205 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8
},
206 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8
},
209 HRESULT
lock_surface(IDirect3DSurface9
*surface
, const RECT
*surface_rect
, D3DLOCKED_RECT
*lock
,
210 IDirect3DSurface9
**temp_surface
, BOOL write
)
212 unsigned int width
, height
;
213 IDirect3DDevice9
*device
;
214 D3DSURFACE_DESC desc
;
218 lock_flag
= write
? 0 : D3DLOCK_READONLY
;
219 *temp_surface
= NULL
;
220 if (FAILED(hr
= IDirect3DSurface9_LockRect(surface
, lock
, surface_rect
, lock_flag
)))
222 IDirect3DSurface9_GetDevice(surface
, &device
);
223 IDirect3DSurface9_GetDesc(surface
, &desc
);
227 width
= surface_rect
->right
- surface_rect
->left
;
228 height
= surface_rect
->bottom
- surface_rect
->top
;
233 height
= desc
.Height
;
236 hr
= write
? IDirect3DDevice9_CreateOffscreenPlainSurface(device
, width
, height
,
237 desc
.Format
, D3DPOOL_SYSTEMMEM
, temp_surface
, NULL
)
238 : IDirect3DDevice9_CreateRenderTarget(device
, width
, height
,
239 desc
.Format
, D3DMULTISAMPLE_NONE
, 0, TRUE
, temp_surface
, NULL
);
242 WARN("Failed to create temporary surface, surface %p, format %#x,"
243 " usage %#x, pool %#x, write %#x, width %u, height %u.\n",
244 surface
, desc
.Format
, desc
.Usage
, desc
.Pool
, write
, width
, height
);
245 IDirect3DDevice9_Release(device
);
249 if (write
|| SUCCEEDED(hr
= IDirect3DDevice9_StretchRect(device
, surface
, surface_rect
,
250 *temp_surface
, NULL
, D3DTEXF_NONE
)))
251 hr
= IDirect3DSurface9_LockRect(*temp_surface
, lock
, NULL
, lock_flag
);
253 IDirect3DDevice9_Release(device
);
256 WARN("Failed to lock surface %p, write %#x, usage %#x, pool %#x.\n",
257 surface
, write
, desc
.Usage
, desc
.Pool
);
258 IDirect3DSurface9_Release(*temp_surface
);
259 *temp_surface
= NULL
;
262 TRACE("Created temporary surface %p.\n", surface
);
267 HRESULT
unlock_surface(IDirect3DSurface9
*surface
, const RECT
*surface_rect
,
268 IDirect3DSurface9
*temp_surface
, BOOL update
)
270 IDirect3DDevice9
*device
;
276 hr
= IDirect3DSurface9_UnlockRect(surface
);
280 hr
= IDirect3DSurface9_UnlockRect(temp_surface
);
285 surface_point
.x
= surface_rect
->left
;
286 surface_point
.y
= surface_rect
->top
;
293 IDirect3DSurface9_GetDevice(surface
, &device
);
294 if (FAILED(hr
= IDirect3DDevice9_UpdateSurface(device
, temp_surface
, NULL
, surface
, &surface_point
)))
295 WARN("Updating surface failed, hr %#x, surface %p, temp_surface %p.\n",
296 hr
, surface
, temp_surface
);
297 IDirect3DDevice9_Release(device
);
299 IDirect3DSurface9_Release(temp_surface
);
303 static D3DFORMAT
dds_rgb_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
307 for (i
= 0; i
< ARRAY_SIZE(rgb_pixel_formats
); i
++)
309 if (rgb_pixel_formats
[i
].bpp
== pixel_format
->bpp
310 && rgb_pixel_formats
[i
].rmask
== pixel_format
->rmask
311 && rgb_pixel_formats
[i
].gmask
== pixel_format
->gmask
312 && rgb_pixel_formats
[i
].bmask
== pixel_format
->bmask
)
314 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && rgb_pixel_formats
[i
].amask
== pixel_format
->amask
)
315 return rgb_pixel_formats
[i
].format
;
316 if (rgb_pixel_formats
[i
].amask
== 0)
317 return rgb_pixel_formats
[i
].format
;
321 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
322 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
323 return D3DFMT_UNKNOWN
;
326 static D3DFORMAT
dds_luminance_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
328 if (pixel_format
->bpp
== 8)
330 if (pixel_format
->rmask
== 0xff)
332 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x0f && pixel_format
->amask
== 0xf0)
335 if (pixel_format
->bpp
== 16)
337 if (pixel_format
->rmask
== 0xffff)
339 if ((pixel_format
->flags
& DDS_PF_ALPHA
) && pixel_format
->rmask
== 0x00ff && pixel_format
->amask
== 0xff00)
343 WARN("Unknown luminance pixel format (bpp %u, l %#x, a %#x)\n",
344 pixel_format
->bpp
, pixel_format
->rmask
, pixel_format
->amask
);
345 return D3DFMT_UNKNOWN
;
348 static D3DFORMAT
dds_alpha_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
350 if (pixel_format
->bpp
== 8 && pixel_format
->amask
== 0xff)
353 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format
->bpp
, pixel_format
->rmask
);
354 return D3DFMT_UNKNOWN
;
357 static D3DFORMAT
dds_indexed_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
359 if (pixel_format
->bpp
== 8)
362 WARN("Unknown indexed pixel format (%u).\n", pixel_format
->bpp
);
363 return D3DFMT_UNKNOWN
;
366 static D3DFORMAT
dds_bump_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
368 if (pixel_format
->bpp
== 16 && pixel_format
->rmask
== 0x00ff && pixel_format
->gmask
== 0xff00)
370 if (pixel_format
->bpp
== 32 && pixel_format
->rmask
== 0x0000ffff && pixel_format
->gmask
== 0xffff0000)
371 return D3DFMT_V16U16
;
373 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format
->bpp
,
374 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
375 return D3DFMT_UNKNOWN
;
378 static D3DFORMAT
dds_bump_luminance_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
380 if (pixel_format
->bpp
== 32 && pixel_format
->rmask
== 0x000000ff && pixel_format
->gmask
== 0x0000ff00
381 && pixel_format
->bmask
== 0x00ff0000)
382 return D3DFMT_X8L8V8U8
;
384 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x).\n", pixel_format
->bpp
,
385 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
386 return D3DFMT_UNKNOWN
;
389 static D3DFORMAT
dds_pixel_format_to_d3dformat(const struct dds_pixel_format
*pixel_format
)
391 TRACE("pixel_format: size %u, flags %#x, fourcc %#x, bpp %u.\n", pixel_format
->size
,
392 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
);
393 TRACE("rmask %#x, gmask %#x, bmask %#x, amask %#x.\n", pixel_format
->rmask
, pixel_format
->gmask
,
394 pixel_format
->bmask
, pixel_format
->amask
);
396 if (pixel_format
->flags
& DDS_PF_FOURCC
)
397 return dds_fourcc_to_d3dformat(pixel_format
->fourcc
);
398 if (pixel_format
->flags
& DDS_PF_INDEXED
)
399 return dds_indexed_to_d3dformat(pixel_format
);
400 if (pixel_format
->flags
& DDS_PF_RGB
)
401 return dds_rgb_to_d3dformat(pixel_format
);
402 if (pixel_format
->flags
& DDS_PF_LUMINANCE
)
403 return dds_luminance_to_d3dformat(pixel_format
);
404 if (pixel_format
->flags
& DDS_PF_ALPHA_ONLY
)
405 return dds_alpha_to_d3dformat(pixel_format
);
406 if (pixel_format
->flags
& DDS_PF_BUMPDUDV
)
407 return dds_bump_to_d3dformat(pixel_format
);
408 if (pixel_format
->flags
& DDS_PF_BUMPLUMINANCE
)
409 return dds_bump_luminance_to_d3dformat(pixel_format
);
411 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n",
412 pixel_format
->flags
, pixel_format
->fourcc
, pixel_format
->bpp
,
413 pixel_format
->rmask
, pixel_format
->gmask
, pixel_format
->bmask
, pixel_format
->amask
);
414 return D3DFMT_UNKNOWN
;
417 static HRESULT
d3dformat_to_dds_pixel_format(struct dds_pixel_format
*pixel_format
, D3DFORMAT d3dformat
)
421 memset(pixel_format
, 0, sizeof(*pixel_format
));
423 pixel_format
->size
= sizeof(*pixel_format
);
425 for (i
= 0; i
< ARRAY_SIZE(rgb_pixel_formats
); i
++)
427 if (rgb_pixel_formats
[i
].format
== d3dformat
)
429 pixel_format
->flags
|= DDS_PF_RGB
;
430 pixel_format
->bpp
= rgb_pixel_formats
[i
].bpp
;
431 pixel_format
->rmask
= rgb_pixel_formats
[i
].rmask
;
432 pixel_format
->gmask
= rgb_pixel_formats
[i
].gmask
;
433 pixel_format
->bmask
= rgb_pixel_formats
[i
].bmask
;
434 pixel_format
->amask
= rgb_pixel_formats
[i
].amask
;
435 if (pixel_format
->amask
) pixel_format
->flags
|= DDS_PF_ALPHA
;
440 /* Reuse dds_fourcc_to_d3dformat as D3DFORMAT and FOURCC are DWORD with same values */
441 if (dds_fourcc_to_d3dformat(d3dformat
) != D3DFMT_UNKNOWN
)
443 pixel_format
->flags
|= DDS_PF_FOURCC
;
444 pixel_format
->fourcc
= d3dformat
;
448 WARN("Unknown pixel format %#x\n", d3dformat
);
452 static HRESULT
calculate_dds_surface_size(D3DFORMAT format
, UINT width
, UINT height
,
453 UINT
*pitch
, UINT
*size
)
455 const struct pixel_format_desc
*format_desc
= get_format_info(format
);
456 if (format_desc
->type
== FORMAT_UNKNOWN
)
459 if (format_desc
->block_width
!= 1 || format_desc
->block_height
!= 1)
461 *pitch
= format_desc
->block_byte_count
462 * max(1, (width
+ format_desc
->block_width
- 1) / format_desc
->block_width
);
464 * max(1, (height
+ format_desc
->block_height
- 1) / format_desc
->block_height
);
468 *pitch
= width
* format_desc
->bytes_per_pixel
;
469 *size
= *pitch
* height
;
475 static UINT
calculate_dds_file_size(D3DFORMAT format
, UINT width
, UINT height
, UINT depth
,
476 UINT miplevels
, UINT faces
)
478 UINT i
, file_size
= 0;
480 for (i
= 0; i
< miplevels
; i
++)
482 UINT pitch
, size
= 0;
483 calculate_dds_surface_size(format
, width
, height
, &pitch
, &size
);
486 width
= max(1, width
/ 2);
487 height
= max(1, height
/ 2);
488 depth
= max(1, depth
/ 2);
492 file_size
+= sizeof(struct dds_header
);
496 /************************************************************
497 * get_image_info_from_dds
499 * Fills a D3DXIMAGE_INFO structure with information
500 * about a DDS file stored in the memory.
503 * buffer [I] pointer to DDS data
504 * length [I] size of DDS data
505 * info [O] pointer to D3DXIMAGE_INFO structure
509 * Failure: D3DXERR_INVALIDDATA
512 static HRESULT
get_image_info_from_dds(const void *buffer
, UINT length
, D3DXIMAGE_INFO
*info
)
515 UINT expected_length
;
516 const struct dds_header
*header
= buffer
;
518 if (length
< sizeof(*header
) || !info
)
519 return D3DXERR_INVALIDDATA
;
521 if (header
->pixel_format
.size
!= sizeof(header
->pixel_format
))
522 return D3DXERR_INVALIDDATA
;
524 info
->Width
= header
->width
;
525 info
->Height
= header
->height
;
527 info
->MipLevels
= header
->miplevels
? header
->miplevels
: 1;
529 info
->Format
= dds_pixel_format_to_d3dformat(&header
->pixel_format
);
530 if (info
->Format
== D3DFMT_UNKNOWN
)
531 return D3DXERR_INVALIDDATA
;
533 TRACE("Pixel format is %#x\n", info
->Format
);
535 if (header
->caps2
& DDS_CAPS2_VOLUME
)
537 info
->Depth
= header
->depth
;
538 info
->ResourceType
= D3DRTYPE_VOLUMETEXTURE
;
540 else if (header
->caps2
& DDS_CAPS2_CUBEMAP
)
544 for (face
= DDS_CAPS2_CUBEMAP_POSITIVEX
; face
<= DDS_CAPS2_CUBEMAP_NEGATIVEZ
; face
<<= 1)
546 if (header
->caps2
& face
)
549 info
->ResourceType
= D3DRTYPE_CUBETEXTURE
;
553 info
->ResourceType
= D3DRTYPE_TEXTURE
;
556 expected_length
= calculate_dds_file_size(info
->Format
, info
->Width
, info
->Height
, info
->Depth
,
557 info
->MipLevels
, faces
);
558 if (length
< expected_length
)
560 WARN("File is too short %u, expected at least %u bytes\n", length
, expected_length
);
561 return D3DXERR_INVALIDDATA
;
564 info
->ImageFileFormat
= D3DXIFF_DDS
;
568 static HRESULT
load_surface_from_dds(IDirect3DSurface9
*dst_surface
, const PALETTEENTRY
*dst_palette
,
569 const RECT
*dst_rect
, const void *src_data
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
,
570 const D3DXIMAGE_INFO
*src_info
)
574 const struct dds_header
*header
= src_data
;
575 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
577 if (src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
578 return D3DXERR_INVALIDDATA
;
580 if (FAILED(calculate_dds_surface_size(src_info
->Format
, src_info
->Width
, src_info
->Height
, &src_pitch
, &size
)))
583 return D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
, pixels
, src_info
->Format
,
584 src_pitch
, NULL
, src_rect
, filter
, color_key
);
587 static HRESULT
save_dds_surface_to_memory(ID3DXBuffer
**dst_buffer
, IDirect3DSurface9
*src_surface
, const RECT
*src_rect
)
590 UINT dst_pitch
, surface_size
, file_size
;
591 D3DSURFACE_DESC src_desc
;
592 D3DLOCKED_RECT locked_rect
;
594 struct dds_header
*header
;
596 struct volume volume
;
597 const struct pixel_format_desc
*pixel_format
;
598 IDirect3DSurface9
*temp_surface
;
602 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n");
606 hr
= IDirect3DSurface9_GetDesc(src_surface
, &src_desc
);
607 if (FAILED(hr
)) return hr
;
609 pixel_format
= get_format_info(src_desc
.Format
);
610 if (pixel_format
->type
== FORMAT_UNKNOWN
) return E_NOTIMPL
;
612 file_size
= calculate_dds_file_size(src_desc
.Format
, src_desc
.Width
, src_desc
.Height
, 1, 1, 1);
614 hr
= calculate_dds_surface_size(src_desc
.Format
, src_desc
.Width
, src_desc
.Height
, &dst_pitch
, &surface_size
);
615 if (FAILED(hr
)) return hr
;
617 hr
= D3DXCreateBuffer(file_size
, &buffer
);
618 if (FAILED(hr
)) return hr
;
620 header
= ID3DXBuffer_GetBufferPointer(buffer
);
621 pixels
= (BYTE
*)(header
+ 1);
623 memset(header
, 0, sizeof(*header
));
624 header
->signature
= MAKEFOURCC('D','D','S',' ');
625 /* The signature is not really part of the DDS header */
626 header
->size
= sizeof(*header
) - FIELD_OFFSET(struct dds_header
, size
);
627 header
->flags
= DDS_CAPS
| DDS_HEIGHT
| DDS_WIDTH
| DDS_PIXELFORMAT
;
628 header
->height
= src_desc
.Height
;
629 header
->width
= src_desc
.Width
;
630 header
->caps
= DDS_CAPS_TEXTURE
;
631 hr
= d3dformat_to_dds_pixel_format(&header
->pixel_format
, src_desc
.Format
);
634 ID3DXBuffer_Release(buffer
);
638 hr
= lock_surface(src_surface
, NULL
, &locked_rect
, &temp_surface
, FALSE
);
641 ID3DXBuffer_Release(buffer
);
645 volume
.width
= src_desc
.Width
;
646 volume
.height
= src_desc
.Height
;
648 copy_pixels(locked_rect
.pBits
, locked_rect
.Pitch
, 0, pixels
, dst_pitch
, 0,
649 &volume
, pixel_format
);
651 unlock_surface(src_surface
, NULL
, temp_surface
, FALSE
);
653 *dst_buffer
= buffer
;
657 static HRESULT
get_surface(D3DRESOURCETYPE type
, struct IDirect3DBaseTexture9
*tex
,
658 int face
, UINT level
, struct IDirect3DSurface9
**surf
)
662 case D3DRTYPE_TEXTURE
:
663 return IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9
*) tex
, level
, surf
);
664 case D3DRTYPE_CUBETEXTURE
:
665 return IDirect3DCubeTexture9_GetCubeMapSurface((IDirect3DCubeTexture9
*) tex
, face
, level
, surf
);
667 ERR("Unexpected texture type\n");
672 HRESULT
save_dds_texture_to_memory(ID3DXBuffer
**dst_buffer
, IDirect3DBaseTexture9
*src_texture
, const PALETTEENTRY
*src_palette
)
675 D3DRESOURCETYPE type
;
677 IDirect3DSurface9
*surface
;
679 type
= IDirect3DBaseTexture9_GetType(src_texture
);
681 if ((type
!= D3DRTYPE_TEXTURE
) && (type
!= D3DRTYPE_CUBETEXTURE
) && (type
!= D3DRTYPE_VOLUMETEXTURE
))
682 return D3DERR_INVALIDCALL
;
684 if (type
== D3DRTYPE_CUBETEXTURE
)
686 FIXME("Cube texture not supported yet\n");
689 else if (type
== D3DRTYPE_VOLUMETEXTURE
)
691 FIXME("Volume texture not supported yet\n");
695 mip_levels
= IDirect3DTexture9_GetLevelCount(src_texture
);
699 FIXME("Mipmap not supported yet\n");
705 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
709 hr
= get_surface(type
, src_texture
, D3DCUBEMAP_FACE_POSITIVE_X
, 0, &surface
);
713 hr
= save_dds_surface_to_memory(dst_buffer
, surface
, NULL
);
714 IDirect3DSurface9_Release(surface
);
719 HRESULT
load_volume_from_dds(IDirect3DVolume9
*dst_volume
, const PALETTEENTRY
*dst_palette
,
720 const D3DBOX
*dst_box
, const void *src_data
, const D3DBOX
*src_box
, DWORD filter
, D3DCOLOR color_key
,
721 const D3DXIMAGE_INFO
*src_info
)
723 UINT row_pitch
, slice_pitch
;
724 const struct dds_header
*header
= src_data
;
725 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
727 if (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
)
728 return D3DXERR_INVALIDDATA
;
730 if (FAILED(calculate_dds_surface_size(src_info
->Format
, src_info
->Width
, src_info
->Height
, &row_pitch
, &slice_pitch
)))
733 return D3DXLoadVolumeFromMemory(dst_volume
, dst_palette
, dst_box
, pixels
, src_info
->Format
,
734 row_pitch
, slice_pitch
, NULL
, src_box
, filter
, color_key
);
737 HRESULT
load_texture_from_dds(IDirect3DTexture9
*texture
, const void *src_data
, const PALETTEENTRY
*palette
,
738 DWORD filter
, D3DCOLOR color_key
, const D3DXIMAGE_INFO
*src_info
, unsigned int skip_levels
,
739 unsigned int *loaded_miplevels
)
748 IDirect3DSurface9
*surface
;
749 const struct dds_header
*header
= src_data
;
750 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
752 /* Loading a cube texture as a simple texture is also supported
753 * (only first face texture is taken). Same with volume textures. */
754 if ((src_info
->ResourceType
!= D3DRTYPE_TEXTURE
)
755 && (src_info
->ResourceType
!= D3DRTYPE_CUBETEXTURE
)
756 && (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
))
758 WARN("Trying to load a %u resource as a 2D texture, returning failure.\n", src_info
->ResourceType
);
759 return D3DXERR_INVALIDDATA
;
762 width
= src_info
->Width
;
763 height
= src_info
->Height
;
764 mip_levels
= min(src_info
->MipLevels
, IDirect3DTexture9_GetLevelCount(texture
));
765 if (src_info
->ResourceType
== D3DRTYPE_VOLUMETEXTURE
)
767 for (mip_level
= 0; mip_level
< mip_levels
+ skip_levels
; ++mip_level
)
769 hr
= calculate_dds_surface_size(src_info
->Format
, width
, height
, &src_pitch
, &mip_level_size
);
770 if (FAILED(hr
)) return hr
;
772 if (mip_level
>= skip_levels
)
774 SetRect(&src_rect
, 0, 0, width
, height
);
776 IDirect3DTexture9_GetSurfaceLevel(texture
, mip_level
- skip_levels
, &surface
);
777 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
778 NULL
, &src_rect
, filter
, color_key
);
779 IDirect3DSurface9_Release(surface
);
784 pixels
+= mip_level_size
;
785 width
= max(1, width
/ 2);
786 height
= max(1, height
/ 2);
789 *loaded_miplevels
= mip_levels
- skip_levels
;
794 HRESULT
load_cube_texture_from_dds(IDirect3DCubeTexture9
*cube_texture
, const void *src_data
,
795 const PALETTEENTRY
*palette
, DWORD filter
, DWORD color_key
, const D3DXIMAGE_INFO
*src_info
)
805 IDirect3DSurface9
*surface
;
806 const struct dds_header
*header
= src_data
;
807 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
809 if (src_info
->ResourceType
!= D3DRTYPE_CUBETEXTURE
)
810 return D3DXERR_INVALIDDATA
;
812 if ((header
->caps2
& DDS_CAPS2_CUBEMAP_ALL_FACES
) != DDS_CAPS2_CUBEMAP_ALL_FACES
)
814 WARN("Only full cubemaps are supported\n");
815 return D3DXERR_INVALIDDATA
;
818 mip_levels
= min(src_info
->MipLevels
, IDirect3DCubeTexture9_GetLevelCount(cube_texture
));
819 for (face
= D3DCUBEMAP_FACE_POSITIVE_X
; face
<= D3DCUBEMAP_FACE_NEGATIVE_Z
; face
++)
821 size
= src_info
->Width
;
822 for (mip_level
= 0; mip_level
< src_info
->MipLevels
; mip_level
++)
824 hr
= calculate_dds_surface_size(src_info
->Format
, size
, size
, &src_pitch
, &mip_level_size
);
825 if (FAILED(hr
)) return hr
;
827 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
828 if (mip_level
< mip_levels
)
830 SetRect(&src_rect
, 0, 0, size
, size
);
832 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture
, face
, mip_level
, &surface
);
833 hr
= D3DXLoadSurfaceFromMemory(surface
, palette
, NULL
, pixels
, src_info
->Format
, src_pitch
,
834 NULL
, &src_rect
, filter
, color_key
);
835 IDirect3DSurface9_Release(surface
);
836 if (FAILED(hr
)) return hr
;
839 pixels
+= mip_level_size
;
840 size
= max(1, size
/ 2);
847 HRESULT
load_volume_texture_from_dds(IDirect3DVolumeTexture9
*volume_texture
, const void *src_data
,
848 const PALETTEENTRY
*palette
, DWORD filter
, DWORD color_key
, const D3DXIMAGE_INFO
*src_info
)
853 UINT src_slice_pitch
;
856 UINT width
, height
, depth
;
857 IDirect3DVolume9
*volume
;
858 const struct dds_header
*header
= src_data
;
859 const BYTE
*pixels
= (BYTE
*)(header
+ 1);
861 if (src_info
->ResourceType
!= D3DRTYPE_VOLUMETEXTURE
)
862 return D3DXERR_INVALIDDATA
;
864 width
= src_info
->Width
;
865 height
= src_info
->Height
;
866 depth
= src_info
->Depth
;
867 mip_levels
= min(src_info
->MipLevels
, IDirect3DVolumeTexture9_GetLevelCount(volume_texture
));
869 for (mip_level
= 0; mip_level
< mip_levels
; mip_level
++)
871 hr
= calculate_dds_surface_size(src_info
->Format
, width
, height
, &src_row_pitch
, &src_slice_pitch
);
872 if (FAILED(hr
)) return hr
;
874 hr
= IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture
, mip_level
, &volume
);
875 if (FAILED(hr
)) return hr
;
879 src_box
.Right
= width
;
880 src_box
.Bottom
= height
;
882 src_box
.Back
= depth
;
884 hr
= D3DXLoadVolumeFromMemory(volume
, palette
, NULL
, pixels
, src_info
->Format
,
885 src_row_pitch
, src_slice_pitch
, NULL
, &src_box
, filter
, color_key
);
887 IDirect3DVolume9_Release(volume
);
888 if (FAILED(hr
)) return hr
;
890 pixels
+= depth
* src_slice_pitch
;
891 width
= max(1, width
/ 2);
892 height
= max(1, height
/ 2);
893 depth
= max(1, depth
/ 2);
899 static BOOL
convert_dib_to_bmp(const void **data
, unsigned int *size
)
904 BITMAPFILEHEADER
*header
;
908 if ((*size
< 4) || (*size
< (header_size
= *(ULONG
*)*data
)))
911 if ((header_size
== sizeof(BITMAPINFOHEADER
)) ||
912 (header_size
== sizeof(BITMAPV4HEADER
)) ||
913 (header_size
== sizeof(BITMAPV5HEADER
)) ||
914 (header_size
== 64 /* sizeof(BITMAPCOREHEADER2) */))
916 /* All structures begin with the same memory layout as BITMAPINFOHEADER */
917 BITMAPINFOHEADER
*info_header
= (BITMAPINFOHEADER
*)*data
;
918 count
= info_header
->biClrUsed
;
920 if (!count
&& info_header
->biBitCount
<= 8)
921 count
= 1 << info_header
->biBitCount
;
923 offset
= sizeof(BITMAPFILEHEADER
) + header_size
+ sizeof(RGBQUAD
) * count
;
925 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */
926 if ((info_header
->biSize
== sizeof(BITMAPINFOHEADER
)) && (info_header
->biCompression
== BI_BITFIELDS
))
927 offset
+= 3 * sizeof(DWORD
);
929 else if (header_size
== sizeof(BITMAPCOREHEADER
))
931 BITMAPCOREHEADER
*core_header
= (BITMAPCOREHEADER
*)*data
;
933 if (core_header
->bcBitCount
<= 8)
934 count
= 1 << core_header
->bcBitCount
;
936 offset
= sizeof(BITMAPFILEHEADER
) + header_size
+ sizeof(RGBTRIPLE
) * count
;
943 TRACE("Converting DIB file to BMP\n");
945 new_size
= *size
+ sizeof(BITMAPFILEHEADER
);
946 new_data
= HeapAlloc(GetProcessHeap(), 0, new_size
);
947 CopyMemory(new_data
+ sizeof(BITMAPFILEHEADER
), *data
, *size
);
950 header
= (BITMAPFILEHEADER
*)new_data
;
951 header
->bfType
= 0x4d42; /* BM */
952 header
->bfSize
= new_size
;
953 header
->bfReserved1
= 0;
954 header
->bfReserved2
= 0;
955 header
->bfOffBits
= offset
;
957 /* Update input data */
964 /************************************************************
965 * D3DXGetImageInfoFromFileInMemory
967 * Fills a D3DXIMAGE_INFO structure with info about an image
970 * data [I] pointer to the image file data
971 * datasize [I] size of the passed data
972 * info [O] pointer to the destination structure
975 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
976 * if info is NULL and data and datasize are not NULL
977 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
978 * D3DERR_INVALIDCALL, if data is NULL or
982 * datasize may be bigger than the actual file size
985 HRESULT WINAPI
D3DXGetImageInfoFromFileInMemory(const void *data
, UINT datasize
, D3DXIMAGE_INFO
*info
)
987 IWICImagingFactory
*factory
;
988 IWICBitmapDecoder
*decoder
= NULL
;
993 TRACE("(%p, %d, %p)\n", data
, datasize
, info
);
995 if (!data
|| !datasize
)
996 return D3DERR_INVALIDCALL
;
1001 if ((datasize
>= 4) && !strncmp(data
, "DDS ", 4)) {
1002 TRACE("File type is DDS\n");
1003 return get_image_info_from_dds(data
, datasize
, info
);
1006 /* In case of DIB file, convert it to BMP */
1007 dib
= convert_dib_to_bmp(&data
, &datasize
);
1009 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
1011 if (SUCCEEDED(hr
)) {
1012 IWICImagingFactory_CreateStream(factory
, &stream
);
1013 IWICStream_InitializeFromMemory(stream
, (BYTE
*)data
, datasize
);
1014 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
1015 IWICStream_Release(stream
);
1016 IWICImagingFactory_Release(factory
);
1020 if ((datasize
>= 2) && (!strncmp(data
, "P3", 2) || !strncmp(data
, "P6", 2)))
1021 FIXME("File type PPM is not supported yet\n");
1022 else if ((datasize
>= 10) && !strncmp(data
, "#?RADIANCE", 10))
1023 FIXME("File type HDR is not supported yet\n");
1024 else if ((datasize
>= 2) && (!strncmp(data
, "PF", 2) || !strncmp(data
, "Pf", 2)))
1025 FIXME("File type PFM is not supported yet\n");
1028 if (SUCCEEDED(hr
)) {
1029 GUID container_format
;
1032 hr
= IWICBitmapDecoder_GetContainerFormat(decoder
, &container_format
);
1033 if (SUCCEEDED(hr
)) {
1034 if (IsEqualGUID(&container_format
, &GUID_ContainerFormatBmp
)) {
1036 TRACE("File type is DIB\n");
1037 info
->ImageFileFormat
= D3DXIFF_DIB
;
1039 TRACE("File type is BMP\n");
1040 info
->ImageFileFormat
= D3DXIFF_BMP
;
1042 } else if (IsEqualGUID(&container_format
, &GUID_ContainerFormatPng
)) {
1043 TRACE("File type is PNG\n");
1044 info
->ImageFileFormat
= D3DXIFF_PNG
;
1045 } else if(IsEqualGUID(&container_format
, &GUID_ContainerFormatJpeg
)) {
1046 TRACE("File type is JPG\n");
1047 info
->ImageFileFormat
= D3DXIFF_JPG
;
1048 } else if(IsEqualGUID(&container_format
, &GUID_WineContainerFormatTga
)) {
1049 TRACE("File type is TGA\n");
1050 info
->ImageFileFormat
= D3DXIFF_TGA
;
1052 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format
));
1053 hr
= D3DXERR_INVALIDDATA
;
1058 hr
= IWICBitmapDecoder_GetFrameCount(decoder
, &frame_count
);
1059 if (SUCCEEDED(hr
) && !frame_count
)
1060 hr
= D3DXERR_INVALIDDATA
;
1062 if (SUCCEEDED(hr
)) {
1063 IWICBitmapFrameDecode
*frame
= NULL
;
1065 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &frame
);
1068 hr
= IWICBitmapFrameDecode_GetSize(frame
, &info
->Width
, &info
->Height
);
1070 if (SUCCEEDED(hr
)) {
1071 WICPixelFormatGUID pixel_format
;
1073 hr
= IWICBitmapFrameDecode_GetPixelFormat(frame
, &pixel_format
);
1074 if (SUCCEEDED(hr
)) {
1075 info
->Format
= wic_guid_to_d3dformat(&pixel_format
);
1076 if (info
->Format
== D3DFMT_UNKNOWN
) {
1077 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format
));
1078 hr
= D3DXERR_INVALIDDATA
;
1083 /* For 32 bpp BMP, windowscodecs.dll never returns a format with alpha while
1084 * d3dx9_xx.dll returns one if at least 1 pixel has a non zero alpha component */
1085 if (SUCCEEDED(hr
) && (info
->Format
== D3DFMT_X8R8G8B8
) && (info
->ImageFileFormat
== D3DXIFF_BMP
)) {
1086 DWORD size
= sizeof(DWORD
) * info
->Width
* info
->Height
;
1087 BYTE
*buffer
= HeapAlloc(GetProcessHeap(), 0, size
);
1088 hr
= IWICBitmapFrameDecode_CopyPixels(frame
, NULL
, sizeof(DWORD
) * info
->Width
, size
, buffer
);
1089 if (SUCCEEDED(hr
)) {
1091 for (i
= 0; i
< info
->Width
* info
->Height
; i
++) {
1092 if (buffer
[i
*4+3]) {
1093 info
->Format
= D3DFMT_A8R8G8B8
;
1098 HeapFree(GetProcessHeap(), 0, buffer
);
1102 IWICBitmapFrameDecode_Release(frame
);
1105 info
->MipLevels
= 1;
1106 info
->ResourceType
= D3DRTYPE_TEXTURE
;
1111 IWICBitmapDecoder_Release(decoder
);
1114 HeapFree(GetProcessHeap(), 0, (void*)data
);
1117 TRACE("Invalid or unsupported image file\n");
1118 return D3DXERR_INVALIDDATA
;
1124 /************************************************************
1125 * D3DXGetImageInfoFromFile
1128 * Success: D3D_OK, if we successfully load a valid image file or
1129 * if we successfully load a file which is no valid image and info is NULL
1130 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
1131 * if file is not a valid image file and info is not NULL
1132 * D3DERR_INVALIDCALL, if file is NULL
1135 HRESULT WINAPI
D3DXGetImageInfoFromFileA(const char *file
, D3DXIMAGE_INFO
*info
)
1141 TRACE("file %s, info %p.\n", debugstr_a(file
), info
);
1143 if( !file
) return D3DERR_INVALIDCALL
;
1145 strlength
= MultiByteToWideChar(CP_ACP
, 0, file
, -1, NULL
, 0);
1146 widename
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*widename
));
1147 MultiByteToWideChar(CP_ACP
, 0, file
, -1, widename
, strlength
);
1149 hr
= D3DXGetImageInfoFromFileW(widename
, info
);
1150 HeapFree(GetProcessHeap(), 0, widename
);
1155 HRESULT WINAPI
D3DXGetImageInfoFromFileW(const WCHAR
*file
, D3DXIMAGE_INFO
*info
)
1161 TRACE("file %s, info %p.\n", debugstr_w(file
), info
);
1164 return D3DERR_INVALIDCALL
;
1166 if (FAILED(map_view_of_file(file
, &buffer
, &size
)))
1167 return D3DXERR_INVALIDDATA
;
1169 hr
= D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
1170 UnmapViewOfFile(buffer
);
1175 /************************************************************
1176 * D3DXGetImageInfoFromResource
1179 * Success: D3D_OK, if resource is a valid image file
1180 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
1181 * if we fail to load resource
1184 HRESULT WINAPI
D3DXGetImageInfoFromResourceA(HMODULE module
, const char *resource
, D3DXIMAGE_INFO
*info
)
1190 TRACE("module %p, resource %s, info %p.\n", module
, debugstr_a(resource
), info
);
1192 if (!(resinfo
= FindResourceA(module
, resource
, (const char *)RT_RCDATA
))
1193 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1194 && !(resinfo
= FindResourceA(module
, resource
, (const char *)RT_BITMAP
)))
1195 return D3DXERR_INVALIDDATA
;
1197 if (FAILED(load_resource_into_memory(module
, resinfo
, &buffer
, &size
)))
1198 return D3DXERR_INVALIDDATA
;
1200 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
1203 HRESULT WINAPI
D3DXGetImageInfoFromResourceW(HMODULE module
, const WCHAR
*resource
, D3DXIMAGE_INFO
*info
)
1209 TRACE("module %p, resource %s, info %p.\n", module
, debugstr_w(resource
), info
);
1211 if (!(resinfo
= FindResourceW(module
, resource
, (const WCHAR
*)RT_RCDATA
))
1212 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1213 && !(resinfo
= FindResourceW(module
, resource
, (const WCHAR
*)RT_BITMAP
)))
1214 return D3DXERR_INVALIDDATA
;
1216 if (FAILED(load_resource_into_memory(module
, resinfo
, &buffer
, &size
)))
1217 return D3DXERR_INVALIDDATA
;
1219 return D3DXGetImageInfoFromFileInMemory(buffer
, size
, info
);
1222 /************************************************************
1223 * D3DXLoadSurfaceFromFileInMemory
1225 * Loads data from a given buffer into a surface and fills a given
1226 * D3DXIMAGE_INFO structure with info about the source data.
1229 * pDestSurface [I] pointer to the surface
1230 * pDestPalette [I] palette to use
1231 * pDestRect [I] to be filled area of the surface
1232 * pSrcData [I] pointer to the source data
1233 * SrcDataSize [I] size of the source data in bytes
1234 * pSrcRect [I] area of the source data to load
1235 * dwFilter [I] filter to apply on stretching
1236 * Colorkey [I] colorkey
1237 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
1241 * Failure: D3DERR_INVALIDCALL, if pDestSurface, pSrcData or SrcDataSize is NULL
1242 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
1245 HRESULT WINAPI
D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9
*pDestSurface
,
1246 const PALETTEENTRY
*pDestPalette
, const RECT
*pDestRect
, const void *pSrcData
, UINT SrcDataSize
,
1247 const RECT
*pSrcRect
, DWORD dwFilter
, D3DCOLOR Colorkey
, D3DXIMAGE_INFO
*pSrcInfo
)
1249 D3DXIMAGE_INFO imginfo
;
1252 IWICImagingFactory
*factory
= NULL
;
1253 IWICBitmapDecoder
*decoder
;
1254 IWICBitmapFrameDecode
*bitmapframe
;
1257 const struct pixel_format_desc
*formatdesc
;
1261 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, "
1262 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1263 pDestSurface
, pDestPalette
, wine_dbgstr_rect(pDestRect
), pSrcData
, SrcDataSize
,
1264 wine_dbgstr_rect(pSrcRect
), dwFilter
, Colorkey
, pSrcInfo
);
1266 if (!pDestSurface
|| !pSrcData
|| !SrcDataSize
)
1267 return D3DERR_INVALIDCALL
;
1269 hr
= D3DXGetImageInfoFromFileInMemory(pSrcData
, SrcDataSize
, &imginfo
);
1276 wicrect
.X
= pSrcRect
->left
;
1277 wicrect
.Y
= pSrcRect
->top
;
1278 wicrect
.Width
= pSrcRect
->right
- pSrcRect
->left
;
1279 wicrect
.Height
= pSrcRect
->bottom
- pSrcRect
->top
;
1285 wicrect
.Width
= imginfo
.Width
;
1286 wicrect
.Height
= imginfo
.Height
;
1289 SetRect(&rect
, wicrect
.X
, wicrect
.Y
, wicrect
.X
+ wicrect
.Width
, wicrect
.Y
+ wicrect
.Height
);
1291 if (imginfo
.ImageFileFormat
== D3DXIFF_DDS
)
1293 hr
= load_surface_from_dds(pDestSurface
, pDestPalette
, pDestRect
, pSrcData
, &rect
,
1294 dwFilter
, Colorkey
, &imginfo
);
1295 if (SUCCEEDED(hr
) && pSrcInfo
)
1296 *pSrcInfo
= imginfo
;
1300 if (imginfo
.ImageFileFormat
== D3DXIFF_DIB
)
1301 convert_dib_to_bmp(&pSrcData
, &SrcDataSize
);
1303 if (FAILED(WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
)))
1306 if (FAILED(IWICImagingFactory_CreateStream(factory
, &stream
)))
1308 IWICImagingFactory_Release(factory
);
1313 IWICStream_InitializeFromMemory(stream
, (BYTE
*)pSrcData
, SrcDataSize
);
1315 hr
= IWICImagingFactory_CreateDecoderFromStream(factory
, (IStream
*)stream
, NULL
, 0, &decoder
);
1317 IWICStream_Release(stream
);
1322 hr
= IWICBitmapDecoder_GetFrame(decoder
, 0, &bitmapframe
);
1327 formatdesc
= get_format_info(imginfo
.Format
);
1329 if (formatdesc
->type
== FORMAT_UNKNOWN
)
1331 FIXME("Unsupported pixel format\n");
1332 hr
= D3DXERR_INVALIDDATA
;
1338 PALETTEENTRY
*palette
= NULL
;
1339 WICColor
*colors
= NULL
;
1341 pitch
= formatdesc
->bytes_per_pixel
* wicrect
.Width
;
1342 buffer
= HeapAlloc(GetProcessHeap(), 0, pitch
* wicrect
.Height
);
1344 hr
= IWICBitmapFrameDecode_CopyPixels(bitmapframe
, &wicrect
, pitch
,
1345 pitch
* wicrect
.Height
, buffer
);
1347 if (SUCCEEDED(hr
) && (formatdesc
->type
== FORMAT_INDEX
))
1349 IWICPalette
*wic_palette
= NULL
;
1352 hr
= IWICImagingFactory_CreatePalette(factory
, &wic_palette
);
1354 hr
= IWICBitmapFrameDecode_CopyPalette(bitmapframe
, wic_palette
);
1356 hr
= IWICPalette_GetColorCount(wic_palette
, &nb_colors
);
1359 colors
= HeapAlloc(GetProcessHeap(), 0, nb_colors
* sizeof(colors
[0]));
1360 palette
= HeapAlloc(GetProcessHeap(), 0, nb_colors
* sizeof(palette
[0]));
1361 if (!colors
|| !palette
)
1365 hr
= IWICPalette_GetColors(wic_palette
, nb_colors
, colors
, &nb_colors
);
1370 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */
1371 for (i
= 0; i
< nb_colors
; i
++)
1373 palette
[i
].peRed
= (colors
[i
] >> 16) & 0xff;
1374 palette
[i
].peGreen
= (colors
[i
] >> 8) & 0xff;
1375 palette
[i
].peBlue
= colors
[i
] & 0xff;
1376 palette
[i
].peFlags
= (colors
[i
] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */
1380 IWICPalette_Release(wic_palette
);
1385 hr
= D3DXLoadSurfaceFromMemory(pDestSurface
, pDestPalette
, pDestRect
,
1386 buffer
, imginfo
.Format
, pitch
,
1387 palette
, &rect
, dwFilter
, Colorkey
);
1390 HeapFree(GetProcessHeap(), 0, colors
);
1391 HeapFree(GetProcessHeap(), 0, palette
);
1392 HeapFree(GetProcessHeap(), 0, buffer
);
1395 IWICBitmapFrameDecode_Release(bitmapframe
);
1398 IWICBitmapDecoder_Release(decoder
);
1402 IWICImagingFactory_Release(factory
);
1404 if (imginfo
.ImageFileFormat
== D3DXIFF_DIB
)
1405 HeapFree(GetProcessHeap(), 0, (void*)pSrcData
);
1408 return D3DXERR_INVALIDDATA
;
1411 *pSrcInfo
= imginfo
;
1416 HRESULT WINAPI
D3DXLoadSurfaceFromFileA(IDirect3DSurface9
*dst_surface
,
1417 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const char *src_file
,
1418 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1424 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1425 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1426 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), debugstr_a(src_file
),
1427 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1429 if (!src_file
|| !dst_surface
)
1430 return D3DERR_INVALIDCALL
;
1432 strlength
= MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, NULL
, 0);
1433 src_file_w
= HeapAlloc(GetProcessHeap(), 0, strlength
* sizeof(*src_file_w
));
1434 MultiByteToWideChar(CP_ACP
, 0, src_file
, -1, src_file_w
, strlength
);
1436 hr
= D3DXLoadSurfaceFromFileW(dst_surface
, dst_palette
, dst_rect
,
1437 src_file_w
, src_rect
, filter
, color_key
, src_info
);
1438 HeapFree(GetProcessHeap(), 0, src_file_w
);
1443 HRESULT WINAPI
D3DXLoadSurfaceFromFileW(IDirect3DSurface9
*dst_surface
,
1444 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const WCHAR
*src_file
,
1445 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1451 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, "
1452 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1453 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), debugstr_w(src_file
),
1454 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1456 if (!src_file
|| !dst_surface
)
1457 return D3DERR_INVALIDCALL
;
1459 if (FAILED(map_view_of_file(src_file
, &data
, &data_size
)))
1460 return D3DXERR_INVALIDDATA
;
1462 hr
= D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1463 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1464 UnmapViewOfFile(data
);
1469 HRESULT WINAPI
D3DXLoadSurfaceFromResourceA(IDirect3DSurface9
*dst_surface
,
1470 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const char *resource
,
1471 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1477 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1478 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1479 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_a(resource
),
1480 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1483 return D3DERR_INVALIDCALL
;
1485 if (!(resinfo
= FindResourceA(src_module
, resource
, (const char *)RT_RCDATA
))
1486 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1487 && !(resinfo
= FindResourceA(src_module
, resource
, (const char *)RT_BITMAP
)))
1488 return D3DXERR_INVALIDDATA
;
1490 if (FAILED(load_resource_into_memory(src_module
, resinfo
, &data
, &data_size
)))
1491 return D3DXERR_INVALIDDATA
;
1493 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1494 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1497 HRESULT WINAPI
D3DXLoadSurfaceFromResourceW(IDirect3DSurface9
*dst_surface
,
1498 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, HMODULE src_module
, const WCHAR
*resource
,
1499 const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
, D3DXIMAGE_INFO
*src_info
)
1505 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, "
1506 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n",
1507 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_module
, debugstr_w(resource
),
1508 wine_dbgstr_rect(src_rect
), filter
, color_key
, src_info
);
1511 return D3DERR_INVALIDCALL
;
1513 if (!(resinfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_RCDATA
))
1514 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */
1515 && !(resinfo
= FindResourceW(src_module
, resource
, (const WCHAR
*)RT_BITMAP
)))
1516 return D3DXERR_INVALIDDATA
;
1518 if (FAILED(load_resource_into_memory(src_module
, resinfo
, &data
, &data_size
)))
1519 return D3DXERR_INVALIDDATA
;
1521 return D3DXLoadSurfaceFromFileInMemory(dst_surface
, dst_palette
, dst_rect
,
1522 data
, data_size
, src_rect
, filter
, color_key
, src_info
);
1526 /************************************************************
1527 * helper functions for D3DXLoadSurfaceFromMemory
1529 struct argb_conversion_info
1531 const struct pixel_format_desc
*srcformat
;
1532 const struct pixel_format_desc
*destformat
;
1533 DWORD srcshift
[4], destshift
[4];
1534 DWORD srcmask
[4], destmask
[4];
1535 BOOL process_channel
[4];
1539 static void init_argb_conversion_info(const struct pixel_format_desc
*srcformat
, const struct pixel_format_desc
*destformat
, struct argb_conversion_info
*info
)
1542 ZeroMemory(info
->process_channel
, 4 * sizeof(BOOL
));
1543 info
->channelmask
= 0;
1545 info
->srcformat
= srcformat
;
1546 info
->destformat
= destformat
;
1548 for(i
= 0;i
< 4;i
++) {
1549 /* srcshift is used to extract the _relevant_ components */
1550 info
->srcshift
[i
] = srcformat
->shift
[i
] + max( srcformat
->bits
[i
] - destformat
->bits
[i
], 0);
1552 /* destshift is used to move the components to the correct position */
1553 info
->destshift
[i
] = destformat
->shift
[i
] + max(destformat
->bits
[i
] - srcformat
->bits
[i
], 0);
1555 info
->srcmask
[i
] = ((1 << srcformat
->bits
[i
]) - 1) << srcformat
->shift
[i
];
1556 info
->destmask
[i
] = ((1 << destformat
->bits
[i
]) - 1) << destformat
->shift
[i
];
1558 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1559 if(destformat
->bits
[i
]) {
1560 if(srcformat
->bits
[i
]) info
->process_channel
[i
] = TRUE
;
1561 else info
->channelmask
|= info
->destmask
[i
];
1566 /************************************************************
1567 * get_relevant_argb_components
1569 * Extracts the relevant components from the source color and
1570 * drops the less significant bits if they aren't used by the destination format.
1572 static void get_relevant_argb_components(const struct argb_conversion_info
*info
, const BYTE
*col
, DWORD
*out
)
1575 unsigned int component
, mask
;
1577 for (i
= 0; i
< 4; ++i
)
1579 if (!info
->process_channel
[i
])
1583 mask
= info
->srcmask
[i
];
1584 for (j
= 0; j
< 4 && mask
; ++j
)
1586 if (info
->srcshift
[i
] < j
* 8)
1587 component
|= (col
[j
] & mask
) << (j
* 8 - info
->srcshift
[i
]);
1589 component
|= (col
[j
] & mask
) >> (info
->srcshift
[i
] - j
* 8);
1596 /************************************************************
1599 * Recombines the output of get_relevant_argb_components and converts
1600 * it to the destination format.
1602 static DWORD
make_argb_color(const struct argb_conversion_info
*info
, const DWORD
*in
)
1607 for(i
= 0;i
< 4;i
++) {
1608 if(info
->process_channel
[i
]) {
1609 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1611 for(shift
= info
->destshift
[i
]; shift
> info
->destformat
->shift
[i
]; shift
-= info
->srcformat
->bits
[i
]) val
|= in
[i
] << shift
;
1612 val
|= (in
[i
] >> (info
->destformat
->shift
[i
] - shift
)) << info
->destformat
->shift
[i
];
1615 val
|= info
->channelmask
; /* new channels are set to their maximal value */
1619 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */
1620 static void format_to_vec4(const struct pixel_format_desc
*format
, const BYTE
*src
, struct vec4
*dst
)
1625 for (c
= 0; c
< 4; ++c
)
1627 static const unsigned int component_offsets
[4] = {3, 0, 1, 2};
1628 float *dst_component
= (float *)dst
+ component_offsets
[c
];
1630 if (format
->bits
[c
])
1632 mask
= ~0u >> (32 - format
->bits
[c
]);
1634 memcpy(&tmp
, src
+ format
->shift
[c
] / 8,
1635 min(sizeof(DWORD
), (format
->shift
[c
] % 8 + format
->bits
[c
] + 7) / 8));
1637 if (format
->type
== FORMAT_ARGBF16
)
1638 *dst_component
= float_16_to_32(tmp
);
1639 else if (format
->type
== FORMAT_ARGBF
)
1640 *dst_component
= *(float *)&tmp
;
1642 *dst_component
= (float)((tmp
>> format
->shift
[c
] % 8) & mask
) / mask
;
1645 *dst_component
= 1.0f
;
1649 /* It doesn't work for components bigger than 32 bits. */
1650 static void format_from_vec4(const struct pixel_format_desc
*format
, const struct vec4
*src
, BYTE
*dst
)
1655 memset(dst
, 0, format
->bytes_per_pixel
);
1657 for (c
= 0; c
< 4; ++c
)
1659 static const unsigned int component_offsets
[4] = {3, 0, 1, 2};
1660 const float src_component
= *((const float *)src
+ component_offsets
[c
]);
1662 if (!format
->bits
[c
])
1665 mask32
= ~0u >> (32 - format
->bits
[c
]);
1667 if (format
->type
== FORMAT_ARGBF16
)
1668 v
= float_32_to_16(src_component
);
1669 else if (format
->type
== FORMAT_ARGBF
)
1670 v
= *(DWORD
*)&src_component
;
1672 v
= (DWORD
)(src_component
* ((1 << format
->bits
[c
]) - 1) + 0.5f
);
1674 for (i
= format
->shift
[c
] / 8 * 8; i
< format
->shift
[c
] + format
->bits
[c
]; i
+= 8)
1678 if (format
->shift
[c
] > i
)
1680 mask
= mask32
<< (format
->shift
[c
] - i
);
1681 byte
= (v
<< (format
->shift
[c
] - i
)) & mask
;
1685 mask
= mask32
>> (i
- format
->shift
[c
]);
1686 byte
= (v
>> (i
- format
->shift
[c
])) & mask
;
1693 /************************************************************
1696 * Copies the source buffer to the destination buffer.
1697 * Works for any pixel format.
1698 * The source and the destination must be block-aligned.
1700 void copy_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
,
1701 BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
, const struct volume
*size
,
1702 const struct pixel_format_desc
*format
)
1706 const BYTE
*src_addr
;
1707 UINT row_block_count
= (size
->width
+ format
->block_width
- 1) / format
->block_width
;
1708 UINT row_count
= (size
->height
+ format
->block_height
- 1) / format
->block_height
;
1710 for (slice
= 0; slice
< size
->depth
; slice
++)
1712 src_addr
= src
+ slice
* src_slice_pitch
;
1713 dst_addr
= dst
+ slice
* dst_slice_pitch
;
1715 for (row
= 0; row
< row_count
; row
++)
1717 memcpy(dst_addr
, src_addr
, row_block_count
* format
->block_byte_count
);
1718 src_addr
+= src_row_pitch
;
1719 dst_addr
+= dst_row_pitch
;
1724 /************************************************************
1725 * convert_argb_pixels
1727 * Copies the source buffer to the destination buffer, performing
1728 * any necessary format conversion and color keying.
1729 * Pixels outsize the source rect are blacked out.
1731 void convert_argb_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
, const struct volume
*src_size
,
1732 const struct pixel_format_desc
*src_format
, BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
,
1733 const struct volume
*dst_size
, const struct pixel_format_desc
*dst_format
, D3DCOLOR color_key
,
1734 const PALETTEENTRY
*palette
)
1736 struct argb_conversion_info conv_info
, ck_conv_info
;
1737 const struct pixel_format_desc
*ck_format
= NULL
;
1739 UINT min_width
, min_height
, min_depth
;
1742 TRACE("src %p, src_row_pitch %u, src_slice_pitch %u, src_size %p, src_format %p, dst %p, "
1743 "dst_row_pitch %u, dst_slice_pitch %u, dst_size %p, dst_format %p, color_key 0x%08x, palette %p.\n",
1744 src
, src_row_pitch
, src_slice_pitch
, src_size
, src_format
, dst
, dst_row_pitch
, dst_slice_pitch
, dst_size
,
1745 dst_format
, color_key
, palette
);
1747 ZeroMemory(channels
, sizeof(channels
));
1748 init_argb_conversion_info(src_format
, dst_format
, &conv_info
);
1750 min_width
= min(src_size
->width
, dst_size
->width
);
1751 min_height
= min(src_size
->height
, dst_size
->height
);
1752 min_depth
= min(src_size
->depth
, dst_size
->depth
);
1756 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1757 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1758 init_argb_conversion_info(src_format
, ck_format
, &ck_conv_info
);
1761 for (z
= 0; z
< min_depth
; z
++) {
1762 const BYTE
*src_slice_ptr
= src
+ z
* src_slice_pitch
;
1763 BYTE
*dst_slice_ptr
= dst
+ z
* dst_slice_pitch
;
1765 for (y
= 0; y
< min_height
; y
++) {
1766 const BYTE
*src_ptr
= src_slice_ptr
+ y
* src_row_pitch
;
1767 BYTE
*dst_ptr
= dst_slice_ptr
+ y
* dst_row_pitch
;
1769 for (x
= 0; x
< min_width
; x
++) {
1770 if (!src_format
->to_rgba
&& !dst_format
->from_rgba
1771 && src_format
->type
== dst_format
->type
1772 && src_format
->bytes_per_pixel
<= 4 && dst_format
->bytes_per_pixel
<= 4)
1776 get_relevant_argb_components(&conv_info
, src_ptr
, channels
);
1777 val
= make_argb_color(&conv_info
, channels
);
1783 get_relevant_argb_components(&ck_conv_info
, src_ptr
, channels
);
1784 ck_pixel
= make_argb_color(&ck_conv_info
, channels
);
1785 if (ck_pixel
== color_key
)
1786 val
&= ~conv_info
.destmask
[0];
1788 memcpy(dst_ptr
, &val
, dst_format
->bytes_per_pixel
);
1792 struct vec4 color
, tmp
;
1794 format_to_vec4(src_format
, src_ptr
, &color
);
1795 if (src_format
->to_rgba
)
1796 src_format
->to_rgba(&color
, &tmp
, palette
);
1804 format_from_vec4(ck_format
, &tmp
, (BYTE
*)&ck_pixel
);
1805 if (ck_pixel
== color_key
)
1809 if (dst_format
->from_rgba
)
1810 dst_format
->from_rgba(&tmp
, &color
);
1814 format_from_vec4(dst_format
, &color
, dst_ptr
);
1817 src_ptr
+= src_format
->bytes_per_pixel
;
1818 dst_ptr
+= dst_format
->bytes_per_pixel
;
1821 if (src_size
->width
< dst_size
->width
) /* black out remaining pixels */
1822 memset(dst_ptr
, 0, dst_format
->bytes_per_pixel
* (dst_size
->width
- src_size
->width
));
1825 if (src_size
->height
< dst_size
->height
) /* black out remaining pixels */
1826 memset(dst
+ src_size
->height
* dst_row_pitch
, 0, dst_row_pitch
* (dst_size
->height
- src_size
->height
));
1828 if (src_size
->depth
< dst_size
->depth
) /* black out remaining pixels */
1829 memset(dst
+ src_size
->depth
* dst_slice_pitch
, 0, dst_slice_pitch
* (dst_size
->depth
- src_size
->depth
));
1832 /************************************************************
1833 * point_filter_argb_pixels
1835 * Copies the source buffer to the destination buffer, performing
1836 * any necessary format conversion, color keying and stretching
1837 * using a point filter.
1839 void point_filter_argb_pixels(const BYTE
*src
, UINT src_row_pitch
, UINT src_slice_pitch
, const struct volume
*src_size
,
1840 const struct pixel_format_desc
*src_format
, BYTE
*dst
, UINT dst_row_pitch
, UINT dst_slice_pitch
,
1841 const struct volume
*dst_size
, const struct pixel_format_desc
*dst_format
, D3DCOLOR color_key
,
1842 const PALETTEENTRY
*palette
)
1844 struct argb_conversion_info conv_info
, ck_conv_info
;
1845 const struct pixel_format_desc
*ck_format
= NULL
;
1849 TRACE("src %p, src_row_pitch %u, src_slice_pitch %u, src_size %p, src_format %p, dst %p, "
1850 "dst_row_pitch %u, dst_slice_pitch %u, dst_size %p, dst_format %p, color_key 0x%08x, palette %p.\n",
1851 src
, src_row_pitch
, src_slice_pitch
, src_size
, src_format
, dst
, dst_row_pitch
, dst_slice_pitch
, dst_size
,
1852 dst_format
, color_key
, palette
);
1854 ZeroMemory(channels
, sizeof(channels
));
1855 init_argb_conversion_info(src_format
, dst_format
, &conv_info
);
1859 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1860 ck_format
= get_format_info(D3DFMT_A8R8G8B8
);
1861 init_argb_conversion_info(src_format
, ck_format
, &ck_conv_info
);
1864 for (z
= 0; z
< dst_size
->depth
; z
++)
1866 BYTE
*dst_slice_ptr
= dst
+ z
* dst_slice_pitch
;
1867 const BYTE
*src_slice_ptr
= src
+ src_slice_pitch
* (z
* src_size
->depth
/ dst_size
->depth
);
1869 for (y
= 0; y
< dst_size
->height
; y
++)
1871 BYTE
*dst_ptr
= dst_slice_ptr
+ y
* dst_row_pitch
;
1872 const BYTE
*src_row_ptr
= src_slice_ptr
+ src_row_pitch
* (y
* src_size
->height
/ dst_size
->height
);
1874 for (x
= 0; x
< dst_size
->width
; x
++)
1876 const BYTE
*src_ptr
= src_row_ptr
+ (x
* src_size
->width
/ dst_size
->width
) * src_format
->bytes_per_pixel
;
1878 if (!src_format
->to_rgba
&& !dst_format
->from_rgba
1879 && src_format
->type
== dst_format
->type
1880 && src_format
->bytes_per_pixel
<= 4 && dst_format
->bytes_per_pixel
<= 4)
1884 get_relevant_argb_components(&conv_info
, src_ptr
, channels
);
1885 val
= make_argb_color(&conv_info
, channels
);
1891 get_relevant_argb_components(&ck_conv_info
, src_ptr
, channels
);
1892 ck_pixel
= make_argb_color(&ck_conv_info
, channels
);
1893 if (ck_pixel
== color_key
)
1894 val
&= ~conv_info
.destmask
[0];
1896 memcpy(dst_ptr
, &val
, dst_format
->bytes_per_pixel
);
1900 struct vec4 color
, tmp
;
1902 format_to_vec4(src_format
, src_ptr
, &color
);
1903 if (src_format
->to_rgba
)
1904 src_format
->to_rgba(&color
, &tmp
, palette
);
1912 format_from_vec4(ck_format
, &tmp
, (BYTE
*)&ck_pixel
);
1913 if (ck_pixel
== color_key
)
1917 if (dst_format
->from_rgba
)
1918 dst_format
->from_rgba(&tmp
, &color
);
1922 format_from_vec4(dst_format
, &color
, dst_ptr
);
1925 dst_ptr
+= dst_format
->bytes_per_pixel
;
1931 /************************************************************
1932 * D3DXLoadSurfaceFromMemory
1934 * Loads data from a given memory chunk into a surface,
1935 * applying any of the specified filters.
1938 * pDestSurface [I] pointer to the surface
1939 * pDestPalette [I] palette to use
1940 * pDestRect [I] to be filled area of the surface
1941 * pSrcMemory [I] pointer to the source data
1942 * SrcFormat [I] format of the source pixel data
1943 * SrcPitch [I] number of bytes in a row
1944 * pSrcPalette [I] palette used in the source image
1945 * pSrcRect [I] area of the source data to load
1946 * dwFilter [I] filter to apply on stretching
1947 * Colorkey [I] colorkey
1950 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1951 * if pSrcMemory is NULL but the other parameters are valid
1952 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect is NULL or
1953 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1954 * if DestRect is invalid
1955 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1956 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1959 * pSrcRect specifies the dimensions of the source data;
1960 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1963 HRESULT WINAPI
D3DXLoadSurfaceFromMemory(IDirect3DSurface9
*dst_surface
,
1964 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, const void *src_memory
,
1965 D3DFORMAT src_format
, UINT src_pitch
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
,
1966 DWORD filter
, D3DCOLOR color_key
)
1968 const struct pixel_format_desc
*srcformatdesc
, *destformatdesc
;
1969 struct volume src_size
, dst_size
, dst_size_aligned
;
1970 RECT dst_rect_temp
, dst_rect_aligned
;
1971 IDirect3DSurface9
*surface
;
1972 D3DSURFACE_DESC surfdesc
;
1973 D3DLOCKED_RECT lockrect
;
1976 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s, %#x, 0x%08x)\n",
1977 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_memory
, src_format
,
1978 src_pitch
, src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
1980 if (!dst_surface
|| !src_memory
|| !src_rect
)
1982 WARN("Invalid argument specified.\n");
1983 return D3DERR_INVALIDCALL
;
1985 if (src_format
== D3DFMT_UNKNOWN
1986 || src_rect
->left
>= src_rect
->right
1987 || src_rect
->top
>= src_rect
->bottom
)
1989 WARN("Invalid src_format or src_rect.\n");
1993 srcformatdesc
= get_format_info(src_format
);
1994 if (srcformatdesc
->type
== FORMAT_UNKNOWN
)
1996 FIXME("Unsupported format %#x.\n", src_format
);
2000 src_size
.width
= src_rect
->right
- src_rect
->left
;
2001 src_size
.height
= src_rect
->bottom
- src_rect
->top
;
2004 IDirect3DSurface9_GetDesc(dst_surface
, &surfdesc
);
2005 destformatdesc
= get_format_info(surfdesc
.Format
);
2008 dst_rect
= &dst_rect_temp
;
2009 dst_rect_temp
.left
= 0;
2010 dst_rect_temp
.top
= 0;
2011 dst_rect_temp
.right
= surfdesc
.Width
;
2012 dst_rect_temp
.bottom
= surfdesc
.Height
;
2016 if (dst_rect
->left
> dst_rect
->right
|| dst_rect
->right
> surfdesc
.Width
2017 || dst_rect
->top
> dst_rect
->bottom
|| dst_rect
->bottom
> surfdesc
.Height
2018 || dst_rect
->left
< 0 || dst_rect
->top
< 0)
2020 WARN("Invalid dst_rect specified.\n");
2021 return D3DERR_INVALIDCALL
;
2023 if (dst_rect
->left
== dst_rect
->right
|| dst_rect
->top
== dst_rect
->bottom
)
2025 WARN("Empty dst_rect specified.\n");
2030 dst_rect_aligned
= *dst_rect
;
2031 if (dst_rect_aligned
.left
& (destformatdesc
->block_width
- 1))
2032 dst_rect_aligned
.left
= dst_rect_aligned
.left
& ~(destformatdesc
->block_width
- 1);
2033 if (dst_rect_aligned
.top
& (destformatdesc
->block_height
- 1))
2034 dst_rect_aligned
.top
= dst_rect_aligned
.top
& ~(destformatdesc
->block_height
- 1);
2035 if (dst_rect_aligned
.right
& (destformatdesc
->block_width
- 1) && dst_rect_aligned
.right
!= surfdesc
.Width
)
2036 dst_rect_aligned
.right
= min((dst_rect_aligned
.right
+ destformatdesc
->block_width
- 1)
2037 & ~(destformatdesc
->block_width
- 1), surfdesc
.Width
);
2038 if (dst_rect_aligned
.bottom
& (destformatdesc
->block_height
- 1) && dst_rect_aligned
.bottom
!= surfdesc
.Height
)
2039 dst_rect_aligned
.bottom
= min((dst_rect_aligned
.bottom
+ destformatdesc
->block_height
- 1)
2040 & ~(destformatdesc
->block_height
- 1), surfdesc
.Height
);
2042 dst_size
.width
= dst_rect
->right
- dst_rect
->left
;
2043 dst_size
.height
= dst_rect
->bottom
- dst_rect
->top
;
2045 dst_size_aligned
.width
= dst_rect_aligned
.right
- dst_rect_aligned
.left
;
2046 dst_size_aligned
.height
= dst_rect_aligned
.bottom
- dst_rect_aligned
.top
;
2047 dst_size_aligned
.depth
= 1;
2049 if (filter
== D3DX_DEFAULT
)
2050 filter
= D3DX_FILTER_TRIANGLE
| D3DX_FILTER_DITHER
;
2052 if (FAILED(hr
= lock_surface(dst_surface
, &dst_rect_aligned
, &lockrect
, &surface
, TRUE
)))
2055 src_memory
= (BYTE
*)src_memory
+ src_rect
->top
/ srcformatdesc
->block_height
* src_pitch
2056 + src_rect
->left
/ srcformatdesc
->block_width
* srcformatdesc
->block_byte_count
;
2058 if (src_format
== surfdesc
.Format
2059 && dst_size
.width
== src_size
.width
2060 && dst_size
.height
== src_size
.height
2062 && !(src_rect
->left
& (srcformatdesc
->block_width
- 1))
2063 && !(src_rect
->top
& (srcformatdesc
->block_height
- 1))
2064 && !(dst_rect
->left
& (destformatdesc
->block_width
- 1))
2065 && !(dst_rect
->top
& (destformatdesc
->block_height
- 1)))
2067 TRACE("Simple copy.\n");
2068 copy_pixels(src_memory
, src_pitch
, 0, lockrect
.pBits
, lockrect
.Pitch
, 0,
2069 &src_size
, srcformatdesc
);
2071 else /* Stretching or format conversion. */
2073 const struct pixel_format_desc
*dst_format
;
2074 DWORD
*src_uncompressed
= NULL
;
2075 BYTE
*dst_uncompressed
= NULL
;
2076 unsigned int dst_pitch
;
2079 if (!is_conversion_from_supported(srcformatdesc
)
2080 || !is_conversion_to_supported(destformatdesc
))
2082 FIXME("Unsupported format conversion %#x -> %#x.\n", src_format
, surfdesc
.Format
);
2083 unlock_surface(dst_surface
, &dst_rect_aligned
, surface
, FALSE
);
2087 if (srcformatdesc
->type
== FORMAT_DXT
)
2089 void (*fetch_dxt_texel
)(int srcRowStride
, const BYTE
*pixdata
,
2090 int i
, int j
, void *texel
);
2093 src_pitch
= src_pitch
* srcformatdesc
->block_width
/ srcformatdesc
->block_byte_count
;
2095 src_uncompressed
= heap_alloc(src_size
.width
* src_size
.height
* sizeof(DWORD
));
2096 if (!src_uncompressed
)
2098 unlock_surface(dst_surface
, &dst_rect_aligned
, surface
, FALSE
);
2099 return E_OUTOFMEMORY
;
2105 fetch_dxt_texel
= fetch_2d_texel_rgba_dxt1
;
2109 fetch_dxt_texel
= fetch_2d_texel_rgba_dxt3
;
2113 fetch_dxt_texel
= fetch_2d_texel_rgba_dxt5
;
2116 FIXME("Unexpected compressed texture format %u.\n", src_format
);
2117 fetch_dxt_texel
= NULL
;
2120 TRACE("Uncompressing DXTn surface.\n");
2121 for (y
= 0; y
< src_size
.height
; ++y
)
2123 DWORD
*ptr
= &src_uncompressed
[y
* src_size
.width
];
2124 for (x
= 0; x
< src_size
.width
; ++x
)
2126 fetch_dxt_texel(src_pitch
, src_memory
, x
+ src_rect
->left
, y
+ src_rect
->top
, ptr
);
2130 src_memory
= src_uncompressed
;
2131 src_pitch
= src_size
.width
* sizeof(DWORD
);
2132 srcformatdesc
= get_format_info(D3DFMT_A8B8G8R8
);
2135 if (destformatdesc
->type
== FORMAT_DXT
)
2137 BOOL dst_misaligned
= dst_rect
->left
!= dst_rect_aligned
.left
2138 || dst_rect
->top
!= dst_rect_aligned
.top
2139 || dst_rect
->right
!= dst_rect_aligned
.right
2140 || dst_rect
->bottom
!= dst_rect_aligned
.bottom
;
2142 dst_uncompressed
= HeapAlloc(GetProcessHeap(), dst_misaligned
? HEAP_ZERO_MEMORY
: 0,
2143 dst_size_aligned
.width
* dst_size_aligned
.height
* sizeof(DWORD
));
2144 if (!dst_uncompressed
)
2146 heap_free(src_uncompressed
);
2147 unlock_surface(dst_surface
, &dst_rect_aligned
, surface
, FALSE
);
2148 return E_OUTOFMEMORY
;
2150 dst_pitch
= dst_size_aligned
.width
* sizeof(DWORD
);
2151 dst_format
= get_format_info(D3DFMT_A8B8G8R8
);
2152 dst_mem
= dst_uncompressed
+ (dst_rect
->top
- dst_rect_aligned
.top
) * dst_pitch
2153 + (dst_rect
->left
- dst_rect_aligned
.left
) * sizeof(DWORD
);
2157 dst_mem
= lockrect
.pBits
;
2158 dst_pitch
= lockrect
.Pitch
;
2159 dst_format
= destformatdesc
;
2162 if ((filter
& 0xf) == D3DX_FILTER_NONE
)
2164 convert_argb_pixels(src_memory
, src_pitch
, 0, &src_size
, srcformatdesc
,
2165 dst_mem
, dst_pitch
, 0, &dst_size
, dst_format
, color_key
, src_palette
);
2167 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
2169 if ((filter
& 0xf) != D3DX_FILTER_POINT
)
2170 FIXME("Unhandled filter %#x.\n", filter
);
2172 /* Always apply a point filter until D3DX_FILTER_LINEAR,
2173 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
2174 point_filter_argb_pixels(src_memory
, src_pitch
, 0, &src_size
, srcformatdesc
,
2175 dst_mem
, dst_pitch
, 0, &dst_size
, dst_format
, color_key
, src_palette
);
2178 heap_free(src_uncompressed
);
2180 if (dst_uncompressed
)
2182 GLenum gl_format
= 0;
2184 TRACE("Compressing DXTn surface.\n");
2185 switch(surfdesc
.Format
)
2188 gl_format
= GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
;
2192 gl_format
= GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
;
2196 gl_format
= GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
;
2199 ERR("Unexpected destination compressed format %u.\n", surfdesc
.Format
);
2201 tx_compress_dxtn(4, dst_size_aligned
.width
, dst_size_aligned
.height
,
2202 dst_uncompressed
, gl_format
, lockrect
.pBits
,
2203 lockrect
.Pitch
* destformatdesc
->block_width
/ destformatdesc
->block_byte_count
);
2204 heap_free(dst_uncompressed
);
2208 return unlock_surface(dst_surface
, &dst_rect_aligned
, surface
, TRUE
);
2211 /************************************************************
2212 * D3DXLoadSurfaceFromSurface
2214 * Copies the contents from one surface to another, performing any required
2215 * format conversion, resizing or filtering.
2218 * pDestSurface [I] pointer to the destination surface
2219 * pDestPalette [I] palette to use
2220 * pDestRect [I] to be filled area of the surface
2221 * pSrcSurface [I] pointer to the source surface
2222 * pSrcPalette [I] palette used for the source surface
2223 * pSrcRect [I] area of the source data to load
2224 * dwFilter [I] filter to apply on resizing
2225 * Colorkey [I] any ARGB value or 0 to disable color-keying
2229 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface is NULL
2230 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
2233 HRESULT WINAPI
D3DXLoadSurfaceFromSurface(IDirect3DSurface9
*dst_surface
,
2234 const PALETTEENTRY
*dst_palette
, const RECT
*dst_rect
, IDirect3DSurface9
*src_surface
,
2235 const PALETTEENTRY
*src_palette
, const RECT
*src_rect
, DWORD filter
, D3DCOLOR color_key
)
2237 const struct pixel_format_desc
*src_format_desc
, *dst_format_desc
;
2238 D3DSURFACE_DESC src_desc
, dst_desc
;
2239 struct volume src_size
, dst_size
;
2240 IDirect3DSurface9
*temp_surface
;
2241 D3DTEXTUREFILTERTYPE d3d_filter
;
2242 IDirect3DDevice9
*device
;
2243 D3DLOCKED_RECT lock
;
2248 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, "
2249 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n",
2250 dst_surface
, dst_palette
, wine_dbgstr_rect(dst_rect
), src_surface
,
2251 src_palette
, wine_dbgstr_rect(src_rect
), filter
, color_key
);
2253 if (!dst_surface
|| !src_surface
)
2254 return D3DERR_INVALIDCALL
;
2256 IDirect3DSurface9_GetDesc(src_surface
, &src_desc
);
2257 src_format_desc
= get_format_info(src_desc
.Format
);
2260 SetRect(&s
, 0, 0, src_desc
.Width
, src_desc
.Height
);
2263 else if (src_rect
->left
== src_rect
->right
|| src_rect
->top
== src_rect
->bottom
)
2265 WARN("Empty src_rect specified.\n");
2266 return filter
== D3DX_FILTER_NONE
? D3D_OK
: E_FAIL
;
2268 else if (src_rect
->left
> src_rect
->right
|| src_rect
->right
> src_desc
.Width
2269 || src_rect
->left
< 0 || src_rect
->left
> src_desc
.Width
2270 || src_rect
->top
> src_rect
->bottom
|| src_rect
->bottom
> src_desc
.Height
2271 || src_rect
->top
< 0 || src_rect
->top
> src_desc
.Height
)
2273 WARN("Invalid src_rect specified.\n");
2274 return D3DERR_INVALIDCALL
;
2277 src_size
.width
= src_rect
->right
- src_rect
->left
;
2278 src_size
.height
= src_rect
->bottom
- src_rect
->top
;
2281 IDirect3DSurface9_GetDesc(dst_surface
, &dst_desc
);
2282 dst_format_desc
= get_format_info(dst_desc
.Format
);
2285 SetRect(&dst_rect_temp
, 0, 0, dst_desc
.Width
, dst_desc
.Height
);
2286 dst_rect
= &dst_rect_temp
;
2288 else if (dst_rect
->left
== dst_rect
->right
|| dst_rect
->top
== dst_rect
->bottom
)
2290 WARN("Empty dst_rect specified.\n");
2291 return filter
== D3DX_FILTER_NONE
? D3D_OK
: E_FAIL
;
2293 else if (dst_rect
->left
> dst_rect
->right
|| dst_rect
->right
> dst_desc
.Width
2294 || dst_rect
->left
< 0 || dst_rect
->left
> dst_desc
.Width
2295 || dst_rect
->top
> dst_rect
->bottom
|| dst_rect
->bottom
> dst_desc
.Height
2296 || dst_rect
->top
< 0 || dst_rect
->top
> dst_desc
.Height
)
2298 WARN("Invalid dst_rect specified.\n");
2299 return D3DERR_INVALIDCALL
;
2302 dst_size
.width
= dst_rect
->right
- dst_rect
->left
;
2303 dst_size
.height
= dst_rect
->bottom
- dst_rect
->top
;
2306 if (!dst_palette
&& !src_palette
&& !color_key
)
2308 if (src_desc
.Format
== dst_desc
.Format
2309 && dst_size
.width
== src_size
.width
2310 && dst_size
.height
== src_size
.height
2312 && !(src_rect
->left
& (src_format_desc
->block_width
- 1))
2313 && !(src_rect
->top
& (src_format_desc
->block_height
- 1))
2314 && !(dst_rect
->left
& (dst_format_desc
->block_width
- 1))
2315 && !(dst_rect
->top
& (dst_format_desc
->block_height
- 1)))
2317 d3d_filter
= D3DTEXF_NONE
;
2323 case D3DX_FILTER_NONE
:
2324 d3d_filter
= D3DTEXF_NONE
;
2327 case D3DX_FILTER_POINT
:
2328 d3d_filter
= D3DTEXF_POINT
;
2331 case D3DX_FILTER_LINEAR
:
2332 d3d_filter
= D3DTEXF_LINEAR
;
2336 d3d_filter
= D3DTEXF_FORCE_DWORD
;
2341 if (d3d_filter
!= D3DTEXF_FORCE_DWORD
)
2343 IDirect3DSurface9_GetDevice(src_surface
, &device
);
2344 hr
= IDirect3DDevice9_StretchRect(device
, src_surface
, src_rect
, dst_surface
, dst_rect
, d3d_filter
);
2345 IDirect3DDevice9_Release(device
);
2351 if (FAILED(lock_surface(src_surface
, NULL
, &lock
, &temp_surface
, FALSE
)))
2352 return D3DXERR_INVALIDDATA
;
2354 hr
= D3DXLoadSurfaceFromMemory(dst_surface
, dst_palette
, dst_rect
, lock
.pBits
,
2355 src_desc
.Format
, lock
.Pitch
, src_palette
, src_rect
, filter
, color_key
);
2357 if (FAILED(unlock_surface(src_surface
, NULL
, temp_surface
, FALSE
)))
2358 return D3DXERR_INVALIDDATA
;
2364 HRESULT WINAPI
D3DXSaveSurfaceToFileA(const char *dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
2365 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
2370 ID3DXBuffer
*buffer
;
2372 TRACE("(%s, %#x, %p, %p, %s): relay\n",
2373 wine_dbgstr_a(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
2375 if (!dst_filename
) return D3DERR_INVALIDCALL
;
2377 len
= MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, NULL
, 0);
2378 filename
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2379 if (!filename
) return E_OUTOFMEMORY
;
2380 MultiByteToWideChar(CP_ACP
, 0, dst_filename
, -1, filename
, len
);
2382 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
2385 hr
= write_buffer_to_file(filename
, buffer
);
2386 ID3DXBuffer_Release(buffer
);
2389 HeapFree(GetProcessHeap(), 0, filename
);
2393 HRESULT WINAPI
D3DXSaveSurfaceToFileW(const WCHAR
*dst_filename
, D3DXIMAGE_FILEFORMAT file_format
,
2394 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
2397 ID3DXBuffer
*buffer
;
2399 TRACE("(%s, %#x, %p, %p, %s): relay\n",
2400 wine_dbgstr_w(dst_filename
), file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
2402 if (!dst_filename
) return D3DERR_INVALIDCALL
;
2404 hr
= D3DXSaveSurfaceToFileInMemory(&buffer
, file_format
, src_surface
, src_palette
, src_rect
);
2407 hr
= write_buffer_to_file(dst_filename
, buffer
);
2408 ID3DXBuffer_Release(buffer
);
2414 HRESULT WINAPI
D3DXSaveSurfaceToFileInMemory(ID3DXBuffer
**dst_buffer
, D3DXIMAGE_FILEFORMAT file_format
,
2415 IDirect3DSurface9
*src_surface
, const PALETTEENTRY
*src_palette
, const RECT
*src_rect
)
2417 IWICBitmapEncoder
*encoder
= NULL
;
2418 IWICBitmapFrameEncode
*frame
= NULL
;
2419 IPropertyBag2
*encoder_options
= NULL
;
2420 IStream
*stream
= NULL
;
2422 const GUID
*container_format
;
2423 const GUID
*pixel_format_guid
;
2424 WICPixelFormatGUID wic_pixel_format
;
2425 IWICImagingFactory
*factory
;
2426 D3DFORMAT d3d_pixel_format
;
2427 D3DSURFACE_DESC src_surface_desc
;
2428 IDirect3DSurface9
*temp_surface
;
2429 D3DLOCKED_RECT locked_rect
;
2431 STATSTG stream_stats
;
2432 HGLOBAL stream_hglobal
;
2433 ID3DXBuffer
*buffer
;
2436 TRACE("dst_buffer %p, file_format %#x, src_surface %p, src_palette %p, src_rect %s.\n",
2437 dst_buffer
, file_format
, src_surface
, src_palette
, wine_dbgstr_rect(src_rect
));
2439 if (!dst_buffer
|| !src_surface
) return D3DERR_INVALIDCALL
;
2443 FIXME("Saving surfaces with palettized pixel formats is not implemented yet\n");
2444 return D3DERR_INVALIDCALL
;
2447 switch (file_format
)
2451 container_format
= &GUID_ContainerFormatBmp
;
2454 container_format
= &GUID_ContainerFormatPng
;
2457 container_format
= &GUID_ContainerFormatJpeg
;
2460 return save_dds_surface_to_memory(dst_buffer
, src_surface
, src_rect
);
2465 FIXME("File format %#x is not supported yet\n", file_format
);
2468 return D3DERR_INVALIDCALL
;
2471 IDirect3DSurface9_GetDesc(src_surface
, &src_surface_desc
);
2474 if (src_rect
->left
== src_rect
->right
|| src_rect
->top
== src_rect
->bottom
)
2476 WARN("Invalid rectangle with 0 area\n");
2477 return D3DXCreateBuffer(64, dst_buffer
);
2479 if (src_rect
->left
< 0 || src_rect
->top
< 0)
2480 return D3DERR_INVALIDCALL
;
2481 if (src_rect
->left
> src_rect
->right
|| src_rect
->top
> src_rect
->bottom
)
2482 return D3DERR_INVALIDCALL
;
2483 if (src_rect
->right
> src_surface_desc
.Width
|| src_rect
->bottom
> src_surface_desc
.Height
)
2484 return D3DERR_INVALIDCALL
;
2486 width
= src_rect
->right
- src_rect
->left
;
2487 height
= src_rect
->bottom
- src_rect
->top
;
2491 width
= src_surface_desc
.Width
;
2492 height
= src_surface_desc
.Height
;
2495 hr
= WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION
, &factory
);
2496 if (FAILED(hr
)) goto cleanup_err
;
2498 hr
= IWICImagingFactory_CreateEncoder(factory
, container_format
, NULL
, &encoder
);
2499 IWICImagingFactory_Release(factory
);
2500 if (FAILED(hr
)) goto cleanup_err
;
2502 hr
= CreateStreamOnHGlobal(NULL
, TRUE
, &stream
);
2503 if (FAILED(hr
)) goto cleanup_err
;
2505 hr
= IWICBitmapEncoder_Initialize(encoder
, stream
, WICBitmapEncoderNoCache
);
2506 if (FAILED(hr
)) goto cleanup_err
;
2508 hr
= IWICBitmapEncoder_CreateNewFrame(encoder
, &frame
, &encoder_options
);
2509 if (FAILED(hr
)) goto cleanup_err
;
2511 hr
= IWICBitmapFrameEncode_Initialize(frame
, encoder_options
);
2512 if (FAILED(hr
)) goto cleanup_err
;
2514 hr
= IWICBitmapFrameEncode_SetSize(frame
, width
, height
);
2515 if (FAILED(hr
)) goto cleanup_err
;
2517 pixel_format_guid
= d3dformat_to_wic_guid(src_surface_desc
.Format
);
2518 if (!pixel_format_guid
)
2520 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc
.Format
);
2525 memcpy(&wic_pixel_format
, pixel_format_guid
, sizeof(GUID
));
2526 hr
= IWICBitmapFrameEncode_SetPixelFormat(frame
, &wic_pixel_format
);
2527 d3d_pixel_format
= wic_guid_to_d3dformat(&wic_pixel_format
);
2528 if (SUCCEEDED(hr
) && d3d_pixel_format
!= D3DFMT_UNKNOWN
)
2530 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format
), d3d_pixel_format
);
2531 if (src_surface_desc
.Format
== d3d_pixel_format
) /* Simple copy */
2533 if (FAILED(hr
= lock_surface(src_surface
, src_rect
, &locked_rect
, &temp_surface
, FALSE
)))
2536 IWICBitmapFrameEncode_WritePixels(frame
, height
,
2537 locked_rect
.Pitch
, height
* locked_rect
.Pitch
, locked_rect
.pBits
);
2538 unlock_surface(src_surface
, src_rect
, temp_surface
, FALSE
);
2540 else /* Pixel format conversion */
2542 const struct pixel_format_desc
*src_format_desc
, *dst_format_desc
;
2547 src_format_desc
= get_format_info(src_surface_desc
.Format
);
2548 dst_format_desc
= get_format_info(d3d_pixel_format
);
2549 if (!is_conversion_from_supported(src_format_desc
)
2550 || !is_conversion_to_supported(dst_format_desc
))
2552 FIXME("Unsupported format conversion %#x -> %#x.\n",
2553 src_surface_desc
.Format
, d3d_pixel_format
);
2559 size
.height
= height
;
2561 dst_pitch
= width
* dst_format_desc
->bytes_per_pixel
;
2562 dst_data
= HeapAlloc(GetProcessHeap(), 0, dst_pitch
* height
);
2568 if (FAILED(hr
= lock_surface(src_surface
, src_rect
, &locked_rect
, &temp_surface
, FALSE
)))
2570 HeapFree(GetProcessHeap(), 0, dst_data
);
2573 convert_argb_pixels(locked_rect
.pBits
, locked_rect
.Pitch
, 0, &size
, src_format_desc
,
2574 dst_data
, dst_pitch
, 0, &size
, dst_format_desc
, 0, NULL
);
2575 unlock_surface(src_surface
, src_rect
, temp_surface
, FALSE
);
2577 IWICBitmapFrameEncode_WritePixels(frame
, height
, dst_pitch
, dst_pitch
* height
, dst_data
);
2578 HeapFree(GetProcessHeap(), 0, dst_data
);
2581 hr
= IWICBitmapFrameEncode_Commit(frame
);
2582 if (SUCCEEDED(hr
)) hr
= IWICBitmapEncoder_Commit(encoder
);
2584 else WARN("Unsupported pixel format %#x\n", src_surface_desc
.Format
);
2586 /* copy data from stream to ID3DXBuffer */
2587 hr
= IStream_Stat(stream
, &stream_stats
, STATFLAG_NONAME
);
2588 if (FAILED(hr
)) goto cleanup_err
;
2590 if (stream_stats
.cbSize
.u
.HighPart
!= 0)
2592 hr
= D3DXERR_INVALIDDATA
;
2595 size
= stream_stats
.cbSize
.u
.LowPart
;
2597 /* Remove BMP header for DIB */
2598 if (file_format
== D3DXIFF_DIB
)
2599 size
-= sizeof(BITMAPFILEHEADER
);
2601 hr
= D3DXCreateBuffer(size
, &buffer
);
2602 if (FAILED(hr
)) goto cleanup
;
2604 hr
= GetHGlobalFromStream(stream
, &stream_hglobal
);
2607 void *buffer_pointer
= ID3DXBuffer_GetBufferPointer(buffer
);
2608 void *stream_data
= GlobalLock(stream_hglobal
);
2609 /* Remove BMP header for DIB */
2610 if (file_format
== D3DXIFF_DIB
)
2611 stream_data
= (void*)((BYTE
*)stream_data
+ sizeof(BITMAPFILEHEADER
));
2612 memcpy(buffer_pointer
, stream_data
, size
);
2613 GlobalUnlock(stream_hglobal
);
2614 *dst_buffer
= buffer
;
2616 else ID3DXBuffer_Release(buffer
);
2619 if (FAILED(hr
) && hr
!= E_OUTOFMEMORY
)
2620 hr
= D3DERR_INVALIDCALL
;
2623 if (stream
) IStream_Release(stream
);
2625 if (frame
) IWICBitmapFrameEncode_Release(frame
);
2626 if (encoder_options
) IPropertyBag2_Release(encoder_options
);
2628 if (encoder
) IWICBitmapEncoder_Release(encoder
);