1 /* 2 * Copyright (C) 2009-2010 Tony Wasserka 3 * Copyright (C) 2012 Józef Kucia 4 * 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. 9 * 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. 14 * 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 18 * 19 */ 20 21 #include "config.h" 22 #include "wine/port.h" 23 24 #include "d3dx9_private.h" 25 26 #include "initguid.h" 27 #include "ole2.h" 28 #include "wincodec.h" 29 30 WINE_DEFAULT_DEBUG_CHANNEL(d3dx); 31 32 HRESULT WINAPI WICCreateImagingFactory_Proxy(UINT, IWICImagingFactory**); 33 34 /* Wine-specific WIC GUIDs */ 35 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22); 36 37 static const struct 38 { 39 const GUID *wic_guid; 40 D3DFORMAT d3dformat; 41 } wic_pixel_formats[] = { 42 { &GUID_WICPixelFormat8bppIndexed, D3DFMT_P8 }, 43 { &GUID_WICPixelFormat1bppIndexed, D3DFMT_P8 }, 44 { &GUID_WICPixelFormat4bppIndexed, D3DFMT_P8 }, 45 { &GUID_WICPixelFormat8bppGray, D3DFMT_L8 }, 46 { &GUID_WICPixelFormat16bppBGR555, D3DFMT_X1R5G5B5 }, 47 { &GUID_WICPixelFormat16bppBGR565, D3DFMT_R5G6B5 }, 48 { &GUID_WICPixelFormat24bppBGR, D3DFMT_R8G8B8 }, 49 { &GUID_WICPixelFormat32bppBGR, D3DFMT_X8R8G8B8 }, 50 { &GUID_WICPixelFormat32bppBGRA, D3DFMT_A8R8G8B8 } 51 }; 52 53 static D3DFORMAT wic_guid_to_d3dformat(const GUID *guid) 54 { 55 unsigned int i; 56 57 for (i = 0; i < ARRAY_SIZE(wic_pixel_formats); i++) 58 { 59 if (IsEqualGUID(wic_pixel_formats[i].wic_guid, guid)) 60 return wic_pixel_formats[i].d3dformat; 61 } 62 63 return D3DFMT_UNKNOWN; 64 } 65 66 static const GUID *d3dformat_to_wic_guid(D3DFORMAT format) 67 { 68 unsigned int i; 69 70 for (i = 0; i < ARRAY_SIZE(wic_pixel_formats); i++) 71 { 72 if (wic_pixel_formats[i].d3dformat == format) 73 return wic_pixel_formats[i].wic_guid; 74 } 75 76 return NULL; 77 } 78 79 /* dds_header.flags */ 80 #define DDS_CAPS 0x1 81 #define DDS_HEIGHT 0x2 82 #define DDS_WIDTH 0x4 83 #define DDS_PITCH 0x8 84 #define DDS_PIXELFORMAT 0x1000 85 #define DDS_MIPMAPCOUNT 0x20000 86 #define DDS_LINEARSIZE 0x80000 87 #define DDS_DEPTH 0x800000 88 89 /* dds_header.caps */ 90 #define DDS_CAPS_COMPLEX 0x8 91 #define DDS_CAPS_TEXTURE 0x1000 92 #define DDS_CAPS_MIPMAP 0x400000 93 94 /* dds_header.caps2 */ 95 #define DDS_CAPS2_CUBEMAP 0x200 96 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400 97 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800 98 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000 99 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000 100 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000 101 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000 102 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \ 103 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \ 104 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ ) 105 #define DDS_CAPS2_VOLUME 0x200000 106 107 /* dds_pixel_format.flags */ 108 #define DDS_PF_ALPHA 0x1 109 #define DDS_PF_ALPHA_ONLY 0x2 110 #define DDS_PF_FOURCC 0x4 111 #define DDS_PF_RGB 0x40 112 #define DDS_PF_YUV 0x200 113 #define DDS_PF_LUMINANCE 0x20000 114 #define DDS_PF_BUMPDUDV 0x80000 115 116 struct dds_pixel_format 117 { 118 DWORD size; 119 DWORD flags; 120 DWORD fourcc; 121 DWORD bpp; 122 DWORD rmask; 123 DWORD gmask; 124 DWORD bmask; 125 DWORD amask; 126 }; 127 128 struct dds_header 129 { 130 DWORD signature; 131 DWORD size; 132 DWORD flags; 133 DWORD height; 134 DWORD width; 135 DWORD pitch_or_linear_size; 136 DWORD depth; 137 DWORD miplevels; 138 DWORD reserved[11]; 139 struct dds_pixel_format pixel_format; 140 DWORD caps; 141 DWORD caps2; 142 DWORD caps3; 143 DWORD caps4; 144 DWORD reserved2; 145 }; 146 147 static D3DFORMAT dds_fourcc_to_d3dformat(DWORD fourcc) 148 { 149 unsigned int i; 150 static const DWORD known_fourcc[] = { 151 D3DFMT_UYVY, 152 D3DFMT_YUY2, 153 D3DFMT_R8G8_B8G8, 154 D3DFMT_G8R8_G8B8, 155 D3DFMT_DXT1, 156 D3DFMT_DXT2, 157 D3DFMT_DXT3, 158 D3DFMT_DXT4, 159 D3DFMT_DXT5, 160 D3DFMT_R16F, 161 D3DFMT_G16R16F, 162 D3DFMT_A16B16G16R16F, 163 D3DFMT_R32F, 164 D3DFMT_G32R32F, 165 D3DFMT_A32B32G32R32F, 166 }; 167 168 for (i = 0; i < ARRAY_SIZE(known_fourcc); i++) 169 { 170 if (known_fourcc[i] == fourcc) 171 return fourcc; 172 } 173 174 WARN("Unknown FourCC %#x\n", fourcc); 175 return D3DFMT_UNKNOWN; 176 } 177 178 static const struct { 179 DWORD bpp; 180 DWORD rmask; 181 DWORD gmask; 182 DWORD bmask; 183 DWORD amask; 184 D3DFORMAT format; 185 } rgb_pixel_formats[] = { 186 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 }, 187 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 }, 188 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 }, 189 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 }, 190 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 }, 191 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 }, 192 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 }, 193 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 }, 194 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 }, 195 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 }, 196 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 }, 197 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 }, 198 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 }, 199 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000, D3DFMT_A8B8G8R8 }, 200 { 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000, D3DFMT_X8B8G8R8 }, 201 }; 202 203 HRESULT lock_surface(IDirect3DSurface9 *surface, D3DLOCKED_RECT *lock, 204 IDirect3DSurface9 **temp_surface, BOOL write) 205 { 206 IDirect3DDevice9 *device; 207 D3DSURFACE_DESC desc; 208 DWORD lock_flag; 209 HRESULT hr; 210 211 lock_flag = write ? D3DLOCK_DISCARD : D3DLOCK_READONLY; 212 *temp_surface = NULL; 213 if (FAILED(hr = IDirect3DSurface9_LockRect(surface, lock, NULL, lock_flag))) 214 { 215 IDirect3DSurface9_GetDevice(surface, &device); 216 IDirect3DSurface9_GetDesc(surface, &desc); 217 218 hr = write ? IDirect3DDevice9_CreateOffscreenPlainSurface(device, desc.Width, desc.Height, 219 desc.Format, D3DPOOL_SYSTEMMEM, temp_surface, NULL) 220 : IDirect3DDevice9_CreateRenderTarget(device, desc.Width, desc.Height, 221 desc.Format, D3DMULTISAMPLE_NONE, 0, TRUE, temp_surface, NULL); 222 if (FAILED(hr)) 223 { 224 WARN("Failed to create temporary surface, surface %p, format %#x," 225 " usage %#x, pool %#x, write %#x, width %u, height %u.\n", 226 surface, desc.Format, desc.Usage, desc.Pool, write, desc.Width, desc.Height); 227 IDirect3DDevice9_Release(device); 228 return hr; 229 } 230 231 if (write || SUCCEEDED(hr = IDirect3DDevice9_StretchRect(device, surface, NULL, 232 *temp_surface, NULL, D3DTEXF_NONE))) 233 hr = IDirect3DSurface9_LockRect(*temp_surface, lock, NULL, lock_flag); 234 235 IDirect3DDevice9_Release(device); 236 if (FAILED(hr)) 237 { 238 WARN("Failed to lock surface %p, write %#x, usage %#x, pool %#x.\n", 239 surface, write, desc.Usage, desc.Pool); 240 IDirect3DSurface9_Release(*temp_surface); 241 *temp_surface = NULL; 242 return hr; 243 } 244 TRACE("Created temporary surface %p.\n", surface); 245 } 246 return hr; 247 } 248 249 HRESULT unlock_surface(IDirect3DSurface9 *surface, D3DLOCKED_RECT *lock, 250 IDirect3DSurface9 *temp_surface, BOOL update) 251 { 252 IDirect3DDevice9 *device; 253 HRESULT hr; 254 255 if (!temp_surface) 256 return IDirect3DSurface9_UnlockRect(surface); 257 258 hr = IDirect3DSurface9_UnlockRect(temp_surface); 259 if (update) 260 { 261 IDirect3DSurface9_GetDevice(surface, &device); 262 if (FAILED(hr = IDirect3DDevice9_UpdateSurface(device, temp_surface, NULL, surface, NULL))) 263 WARN("Updating surface failed, hr %#x, surface %p, temp_surface %p.\n", 264 hr, surface, temp_surface); 265 IDirect3DDevice9_Release(device); 266 } 267 IDirect3DSurface9_Release(temp_surface); 268 return hr; 269 } 270 271 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format) 272 { 273 unsigned int i; 274 275 for (i = 0; i < ARRAY_SIZE(rgb_pixel_formats); i++) 276 { 277 if (rgb_pixel_formats[i].bpp == pixel_format->bpp 278 && rgb_pixel_formats[i].rmask == pixel_format->rmask 279 && rgb_pixel_formats[i].gmask == pixel_format->gmask 280 && rgb_pixel_formats[i].bmask == pixel_format->bmask) 281 { 282 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask) 283 return rgb_pixel_formats[i].format; 284 if (rgb_pixel_formats[i].amask == 0) 285 return rgb_pixel_formats[i].format; 286 } 287 } 288 289 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n", 290 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask); 291 return D3DFMT_UNKNOWN; 292 } 293 294 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format) 295 { 296 if (pixel_format->bpp == 8) 297 { 298 if (pixel_format->rmask == 0xff) 299 return D3DFMT_L8; 300 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0) 301 return D3DFMT_A4L4; 302 } 303 if (pixel_format->bpp == 16) 304 { 305 if (pixel_format->rmask == 0xffff) 306 return D3DFMT_L16; 307 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00) 308 return D3DFMT_A8L8; 309 } 310 311 WARN("Unknown luminance pixel format (bpp %u, l %#x, a %#x)\n", 312 pixel_format->bpp, pixel_format->rmask, pixel_format->amask); 313 return D3DFMT_UNKNOWN; 314 } 315 316 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format) 317 { 318 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff) 319 return D3DFMT_A8; 320 321 WARN("Unknown Alpha pixel format (%u, %#x)\n", pixel_format->bpp, pixel_format->rmask); 322 return D3DFMT_UNKNOWN; 323 } 324 325 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format) 326 { 327 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00) 328 return D3DFMT_V8U8; 329 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000) 330 return D3DFMT_V16U16; 331 332 WARN("Unknown bump pixel format (%u, %#x, %#x, %#x, %#x)\n", pixel_format->bpp, 333 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask); 334 return D3DFMT_UNKNOWN; 335 } 336 337 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format) 338 { 339 TRACE("pixel_format: size %u, flags %#x, fourcc %#x, bpp %u.\n", pixel_format->size, 340 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp); 341 TRACE("rmask %#x, gmask %#x, bmask %#x, amask %#x.\n", pixel_format->rmask, pixel_format->gmask, 342 pixel_format->bmask, pixel_format->amask); 343 344 if (pixel_format->flags & DDS_PF_FOURCC) 345 return dds_fourcc_to_d3dformat(pixel_format->fourcc); 346 if (pixel_format->flags & DDS_PF_RGB) 347 return dds_rgb_to_d3dformat(pixel_format); 348 if (pixel_format->flags & DDS_PF_LUMINANCE) 349 return dds_luminance_to_d3dformat(pixel_format); 350 if (pixel_format->flags & DDS_PF_ALPHA_ONLY) 351 return dds_alpha_to_d3dformat(pixel_format); 352 if (pixel_format->flags & DDS_PF_BUMPDUDV) 353 return dds_bump_to_d3dformat(pixel_format); 354 355 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %u, r %#x, g %#x, b %#x, a %#x)\n", 356 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp, 357 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask); 358 return D3DFMT_UNKNOWN; 359 } 360 361 static HRESULT d3dformat_to_dds_pixel_format(struct dds_pixel_format *pixel_format, D3DFORMAT d3dformat) 362 { 363 unsigned int i; 364 365 memset(pixel_format, 0, sizeof(*pixel_format)); 366 367 pixel_format->size = sizeof(*pixel_format); 368 369 for (i = 0; i < ARRAY_SIZE(rgb_pixel_formats); i++) 370 { 371 if (rgb_pixel_formats[i].format == d3dformat) 372 { 373 pixel_format->flags |= DDS_PF_RGB; 374 pixel_format->bpp = rgb_pixel_formats[i].bpp; 375 pixel_format->rmask = rgb_pixel_formats[i].rmask; 376 pixel_format->gmask = rgb_pixel_formats[i].gmask; 377 pixel_format->bmask = rgb_pixel_formats[i].bmask; 378 pixel_format->amask = rgb_pixel_formats[i].amask; 379 if (pixel_format->amask) pixel_format->flags |= DDS_PF_ALPHA; 380 return D3D_OK; 381 } 382 } 383 384 WARN("Unknown pixel format %#x\n", d3dformat); 385 return E_NOTIMPL; 386 } 387 388 static HRESULT calculate_dds_surface_size(D3DFORMAT format, UINT width, UINT height, 389 UINT *pitch, UINT *size) 390 { 391 const struct pixel_format_desc *format_desc = get_format_info(format); 392 if (format_desc->type == FORMAT_UNKNOWN) 393 return E_NOTIMPL; 394 395 if (format_desc->block_width != 1 || format_desc->block_height != 1) 396 { 397 *pitch = format_desc->block_byte_count 398 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width); 399 *size = *pitch 400 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height); 401 } 402 else 403 { 404 *pitch = width * format_desc->bytes_per_pixel; 405 *size = *pitch * height; 406 } 407 408 return D3D_OK; 409 } 410 411 static UINT calculate_dds_file_size(D3DFORMAT format, UINT width, UINT height, UINT depth, 412 UINT miplevels, UINT faces) 413 { 414 UINT i, file_size = 0; 415 416 for (i = 0; i < miplevels; i++) 417 { 418 UINT pitch, size = 0; 419 calculate_dds_surface_size(format, width, height, &pitch, &size); 420 size *= depth; 421 file_size += size; 422 width = max(1, width / 2); 423 height = max(1, height / 2); 424 depth = max(1, depth / 2); 425 } 426 427 file_size *= faces; 428 file_size += sizeof(struct dds_header); 429 return file_size; 430 } 431 432 /************************************************************ 433 * get_image_info_from_dds 434 * 435 * Fills a D3DXIMAGE_INFO structure with information 436 * about a DDS file stored in the memory. 437 * 438 * PARAMS 439 * buffer [I] pointer to DDS data 440 * length [I] size of DDS data 441 * info [O] pointer to D3DXIMAGE_INFO structure 442 * 443 * RETURNS 444 * Success: D3D_OK 445 * Failure: D3DXERR_INVALIDDATA 446 * 447 */ 448 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info) 449 { 450 UINT faces = 1; 451 UINT expected_length; 452 const struct dds_header *header = buffer; 453 454 if (length < sizeof(*header) || !info) 455 return D3DXERR_INVALIDDATA; 456 457 if (header->pixel_format.size != sizeof(header->pixel_format)) 458 return D3DXERR_INVALIDDATA; 459 460 info->Width = header->width; 461 info->Height = header->height; 462 info->Depth = 1; 463 info->MipLevels = header->miplevels ? header->miplevels : 1; 464 465 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format); 466 if (info->Format == D3DFMT_UNKNOWN) 467 return D3DXERR_INVALIDDATA; 468 469 TRACE("Pixel format is %#x\n", info->Format); 470 471 if (header->caps2 & DDS_CAPS2_VOLUME) 472 { 473 info->Depth = header->depth; 474 info->ResourceType = D3DRTYPE_VOLUMETEXTURE; 475 } 476 else if (header->caps2 & DDS_CAPS2_CUBEMAP) 477 { 478 DWORD face; 479 faces = 0; 480 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1) 481 { 482 if (header->caps2 & face) 483 faces++; 484 } 485 info->ResourceType = D3DRTYPE_CUBETEXTURE; 486 } 487 else 488 { 489 info->ResourceType = D3DRTYPE_TEXTURE; 490 } 491 492 expected_length = calculate_dds_file_size(info->Format, info->Width, info->Height, info->Depth, 493 info->MipLevels, faces); 494 if (length < expected_length) 495 { 496 WARN("File is too short %u, expected at least %u bytes\n", length, expected_length); 497 return D3DXERR_INVALIDDATA; 498 } 499 500 info->ImageFileFormat = D3DXIFF_DDS; 501 return D3D_OK; 502 } 503 504 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette, 505 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key, 506 const D3DXIMAGE_INFO *src_info) 507 { 508 UINT size; 509 UINT src_pitch; 510 const struct dds_header *header = src_data; 511 const BYTE *pixels = (BYTE *)(header + 1); 512 513 if (src_info->ResourceType != D3DRTYPE_TEXTURE) 514 return D3DXERR_INVALIDDATA; 515 516 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &src_pitch, &size))) 517 return E_NOTIMPL; 518 519 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format, 520 src_pitch, NULL, src_rect, filter, color_key); 521 } 522 523 static HRESULT save_dds_surface_to_memory(ID3DXBuffer **dst_buffer, IDirect3DSurface9 *src_surface, const RECT *src_rect) 524 { 525 HRESULT hr; 526 UINT dst_pitch, surface_size, file_size; 527 D3DSURFACE_DESC src_desc; 528 D3DLOCKED_RECT locked_rect; 529 ID3DXBuffer *buffer; 530 struct dds_header *header; 531 BYTE *pixels; 532 struct volume volume; 533 const struct pixel_format_desc *pixel_format; 534 IDirect3DSurface9 *temp_surface; 535 536 if (src_rect) 537 { 538 FIXME("Saving a part of a surface to a DDS file is not implemented yet\n"); 539 return E_NOTIMPL; 540 } 541 542 hr = IDirect3DSurface9_GetDesc(src_surface, &src_desc); 543 if (FAILED(hr)) return hr; 544 545 pixel_format = get_format_info(src_desc.Format); 546 if (pixel_format->type == FORMAT_UNKNOWN) return E_NOTIMPL; 547 548 file_size = calculate_dds_file_size(src_desc.Format, src_desc.Width, src_desc.Height, 1, 1, 1); 549 550 hr = calculate_dds_surface_size(src_desc.Format, src_desc.Width, src_desc.Height, &dst_pitch, &surface_size); 551 if (FAILED(hr)) return hr; 552 553 hr = D3DXCreateBuffer(file_size, &buffer); 554 if (FAILED(hr)) return hr; 555 556 header = ID3DXBuffer_GetBufferPointer(buffer); 557 pixels = (BYTE *)(header + 1); 558 559 memset(header, 0, sizeof(*header)); 560 header->signature = MAKEFOURCC('D','D','S',' '); 561 /* The signature is not really part of the DDS header */ 562 header->size = sizeof(*header) - FIELD_OFFSET(struct dds_header, size); 563 header->flags = DDS_CAPS | DDS_HEIGHT | DDS_WIDTH | DDS_PIXELFORMAT; 564 header->height = src_desc.Height; 565 header->width = src_desc.Width; 566 header->caps = DDS_CAPS_TEXTURE; 567 hr = d3dformat_to_dds_pixel_format(&header->pixel_format, src_desc.Format); 568 if (FAILED(hr)) 569 { 570 ID3DXBuffer_Release(buffer); 571 return hr; 572 } 573 574 hr = lock_surface(src_surface, &locked_rect, &temp_surface, FALSE); 575 if (FAILED(hr)) 576 { 577 ID3DXBuffer_Release(buffer); 578 return hr; 579 } 580 581 volume.width = src_desc.Width; 582 volume.height = src_desc.Height; 583 volume.depth = 1; 584 copy_pixels(locked_rect.pBits, locked_rect.Pitch, 0, pixels, dst_pitch, 0, 585 &volume, pixel_format); 586 587 unlock_surface(src_surface, &locked_rect, temp_surface, FALSE); 588 589 *dst_buffer = buffer; 590 return D3D_OK; 591 } 592 593 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette, 594 const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key, 595 const D3DXIMAGE_INFO *src_info) 596 { 597 UINT row_pitch, slice_pitch; 598 const struct dds_header *header = src_data; 599 const BYTE *pixels = (BYTE *)(header + 1); 600 601 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE) 602 return D3DXERR_INVALIDDATA; 603 604 if (FAILED(calculate_dds_surface_size(src_info->Format, src_info->Width, src_info->Height, &row_pitch, &slice_pitch))) 605 return E_NOTIMPL; 606 607 return D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box, pixels, src_info->Format, 608 row_pitch, slice_pitch, NULL, src_box, filter, color_key); 609 } 610 611 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette, 612 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info, unsigned int skip_levels, 613 unsigned int *loaded_miplevels) 614 { 615 HRESULT hr; 616 RECT src_rect; 617 UINT src_pitch; 618 UINT mip_level; 619 UINT mip_levels; 620 UINT mip_level_size; 621 UINT width, height; 622 IDirect3DSurface9 *surface; 623 const struct dds_header *header = src_data; 624 const BYTE *pixels = (BYTE *)(header + 1); 625 626 /* Loading a cube texture as a simple texture is also supported 627 * (only first face texture is taken). Same with volume textures. */ 628 if ((src_info->ResourceType != D3DRTYPE_TEXTURE) 629 && (src_info->ResourceType != D3DRTYPE_CUBETEXTURE) 630 && (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE)) 631 { 632 WARN("Trying to load a %u resource as a 2D texture, returning failure.\n", src_info->ResourceType); 633 return D3DXERR_INVALIDDATA; 634 } 635 636 width = src_info->Width; 637 height = src_info->Height; 638 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture)); 639 if (src_info->ResourceType == D3DRTYPE_VOLUMETEXTURE) 640 mip_levels = 1; 641 for (mip_level = 0; mip_level < mip_levels + skip_levels; ++mip_level) 642 { 643 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_pitch, &mip_level_size); 644 if (FAILED(hr)) return hr; 645 646 if (mip_level >= skip_levels) 647 { 648 SetRect(&src_rect, 0, 0, width, height); 649 650 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level - skip_levels, &surface); 651 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch, 652 NULL, &src_rect, filter, color_key); 653 IDirect3DSurface9_Release(surface); 654 if (FAILED(hr)) 655 return hr; 656 } 657 658 pixels += mip_level_size; 659 width = max(1, width / 2); 660 height = max(1, height / 2); 661 } 662 663 *loaded_miplevels = mip_levels - skip_levels; 664 665 return D3D_OK; 666 } 667 668 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data, 669 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info) 670 { 671 HRESULT hr; 672 int face; 673 UINT mip_level; 674 UINT size; 675 RECT src_rect; 676 UINT src_pitch; 677 UINT mip_levels; 678 UINT mip_level_size; 679 IDirect3DSurface9 *surface; 680 const struct dds_header *header = src_data; 681 const BYTE *pixels = (BYTE *)(header + 1); 682 683 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE) 684 return D3DXERR_INVALIDDATA; 685 686 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES) 687 { 688 WARN("Only full cubemaps are supported\n"); 689 return D3DXERR_INVALIDDATA; 690 } 691 692 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture)); 693 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++) 694 { 695 size = src_info->Width; 696 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++) 697 { 698 hr = calculate_dds_surface_size(src_info->Format, size, size, &src_pitch, &mip_level_size); 699 if (FAILED(hr)) return hr; 700 701 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */ 702 if (mip_level < mip_levels) 703 { 704 SetRect(&src_rect, 0, 0, size, size); 705 706 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface); 707 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch, 708 NULL, &src_rect, filter, color_key); 709 IDirect3DSurface9_Release(surface); 710 if (FAILED(hr)) return hr; 711 } 712 713 pixels += mip_level_size; 714 size = max(1, size / 2); 715 } 716 } 717 718 return D3D_OK; 719 } 720 721 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data, 722 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info) 723 { 724 HRESULT hr; 725 UINT mip_level; 726 UINT mip_levels; 727 UINT src_slice_pitch; 728 UINT src_row_pitch; 729 D3DBOX src_box; 730 UINT width, height, depth; 731 IDirect3DVolume9 *volume; 732 const struct dds_header *header = src_data; 733 const BYTE *pixels = (BYTE *)(header + 1); 734 735 if (src_info->ResourceType != D3DRTYPE_VOLUMETEXTURE) 736 return D3DXERR_INVALIDDATA; 737 738 width = src_info->Width; 739 height = src_info->Height; 740 depth = src_info->Depth; 741 mip_levels = min(src_info->MipLevels, IDirect3DVolumeTexture9_GetLevelCount(volume_texture)); 742 743 for (mip_level = 0; mip_level < mip_levels; mip_level++) 744 { 745 hr = calculate_dds_surface_size(src_info->Format, width, height, &src_row_pitch, &src_slice_pitch); 746 if (FAILED(hr)) return hr; 747 748 hr = IDirect3DVolumeTexture9_GetVolumeLevel(volume_texture, mip_level, &volume); 749 if (FAILED(hr)) return hr; 750 751 src_box.Left = 0; 752 src_box.Top = 0; 753 src_box.Right = width; 754 src_box.Bottom = height; 755 src_box.Front = 0; 756 src_box.Back = depth; 757 758 hr = D3DXLoadVolumeFromMemory(volume, palette, NULL, pixels, src_info->Format, 759 src_row_pitch, src_slice_pitch, NULL, &src_box, filter, color_key); 760 761 IDirect3DVolume9_Release(volume); 762 if (FAILED(hr)) return hr; 763 764 pixels += depth * src_slice_pitch; 765 width = max(1, width / 2); 766 height = max(1, height / 2); 767 depth = max(1, depth / 2); 768 } 769 770 return D3D_OK; 771 } 772 773 static BOOL convert_dib_to_bmp(void **data, UINT *size) 774 { 775 ULONG header_size; 776 ULONG count = 0; 777 ULONG offset; 778 BITMAPFILEHEADER *header; 779 BYTE *new_data; 780 UINT new_size; 781 782 if ((*size < 4) || (*size < (header_size = *(ULONG*)*data))) 783 return FALSE; 784 785 if ((header_size == sizeof(BITMAPINFOHEADER)) || 786 (header_size == sizeof(BITMAPV4HEADER)) || 787 (header_size == sizeof(BITMAPV5HEADER)) || 788 (header_size == 64 /* sizeof(BITMAPCOREHEADER2) */)) 789 { 790 /* All structures begin with the same memory layout as BITMAPINFOHEADER */ 791 BITMAPINFOHEADER *info_header = (BITMAPINFOHEADER*)*data; 792 count = info_header->biClrUsed; 793 794 if (!count && info_header->biBitCount <= 8) 795 count = 1 << info_header->biBitCount; 796 797 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBQUAD) * count; 798 799 /* For BITMAPINFOHEADER with BI_BITFIELDS compression, there are 3 additional color masks after header */ 800 if ((info_header->biSize == sizeof(BITMAPINFOHEADER)) && (info_header->biCompression == BI_BITFIELDS)) 801 offset += 3 * sizeof(DWORD); 802 } 803 else if (header_size == sizeof(BITMAPCOREHEADER)) 804 { 805 BITMAPCOREHEADER *core_header = (BITMAPCOREHEADER*)*data; 806 807 if (core_header->bcBitCount <= 8) 808 count = 1 << core_header->bcBitCount; 809 810 offset = sizeof(BITMAPFILEHEADER) + header_size + sizeof(RGBTRIPLE) * count; 811 } 812 else 813 { 814 return FALSE; 815 } 816 817 TRACE("Converting DIB file to BMP\n"); 818 819 new_size = *size + sizeof(BITMAPFILEHEADER); 820 new_data = HeapAlloc(GetProcessHeap(), 0, new_size); 821 CopyMemory(new_data + sizeof(BITMAPFILEHEADER), *data, *size); 822 823 /* Add BMP header */ 824 header = (BITMAPFILEHEADER*)new_data; 825 header->bfType = 0x4d42; /* BM */ 826 header->bfSize = new_size; 827 header->bfReserved1 = 0; 828 header->bfReserved2 = 0; 829 header->bfOffBits = offset; 830 831 /* Update input data */ 832 *data = new_data; 833 *size = new_size; 834 835 return TRUE; 836 } 837 838 /************************************************************ 839 * D3DXGetImageInfoFromFileInMemory 840 * 841 * Fills a D3DXIMAGE_INFO structure with info about an image 842 * 843 * PARAMS 844 * data [I] pointer to the image file data 845 * datasize [I] size of the passed data 846 * info [O] pointer to the destination structure 847 * 848 * RETURNS 849 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or 850 * if info is NULL and data and datasize are not NULL 851 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL 852 * D3DERR_INVALIDCALL, if data is NULL or 853 * if datasize is 0 854 * 855 * NOTES 856 * datasize may be bigger than the actual file size 857 * 858 */ 859 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(const void *data, UINT datasize, D3DXIMAGE_INFO *info) 860 { 861 IWICImagingFactory *factory; 862 IWICBitmapDecoder *decoder = NULL; 863 IWICStream *stream; 864 HRESULT hr; 865 BOOL dib; 866 867 TRACE("(%p, %d, %p)\n", data, datasize, info); 868 869 if (!data || !datasize) 870 return D3DERR_INVALIDCALL; 871 872 if (!info) 873 return D3D_OK; 874 875 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) { 876 TRACE("File type is DDS\n"); 877 return get_image_info_from_dds(data, datasize, info); 878 } 879 880 /* In case of DIB file, convert it to BMP */ 881 dib = convert_dib_to_bmp((void**)&data, &datasize); 882 883 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory); 884 885 if (SUCCEEDED(hr)) { 886 IWICImagingFactory_CreateStream(factory, &stream); 887 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize); 888 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder); 889 IWICStream_Release(stream); 890 IWICImagingFactory_Release(factory); 891 } 892 893 if (FAILED(hr)) { 894 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2))) 895 FIXME("File type PPM is not supported yet\n"); 896 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10)) 897 FIXME("File type HDR is not supported yet\n"); 898 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2))) 899 FIXME("File type PFM is not supported yet\n"); 900 } 901 902 if (SUCCEEDED(hr)) { 903 GUID container_format; 904 UINT frame_count; 905 906 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format); 907 if (SUCCEEDED(hr)) { 908 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) { 909 if (dib) { 910 TRACE("File type is DIB\n"); 911 info->ImageFileFormat = D3DXIFF_DIB; 912 } else { 913 TRACE("File type is BMP\n"); 914 info->ImageFileFormat = D3DXIFF_BMP; 915 } 916 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) { 917 TRACE("File type is PNG\n"); 918 info->ImageFileFormat = D3DXIFF_PNG; 919 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) { 920 TRACE("File type is JPG\n"); 921 info->ImageFileFormat = D3DXIFF_JPG; 922 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) { 923 TRACE("File type is TGA\n"); 924 info->ImageFileFormat = D3DXIFF_TGA; 925 } else { 926 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format)); 927 hr = D3DXERR_INVALIDDATA; 928 } 929 } 930 931 if (SUCCEEDED(hr)) 932 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count); 933 if (SUCCEEDED(hr) && !frame_count) 934 hr = D3DXERR_INVALIDDATA; 935 936 if (SUCCEEDED(hr)) { 937 IWICBitmapFrameDecode *frame = NULL; 938 939 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame); 940 941 if (SUCCEEDED(hr)) 942 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height); 943 944 if (SUCCEEDED(hr)) { 945 WICPixelFormatGUID pixel_format; 946 947 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format); 948 if (SUCCEEDED(hr)) { 949 info->Format = wic_guid_to_d3dformat(&pixel_format); 950 if (info->Format == D3DFMT_UNKNOWN) { 951 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format)); 952 hr = D3DXERR_INVALIDDATA; 953 } 954 } 955 } 956 957 if (frame) 958 IWICBitmapFrameDecode_Release(frame); 959 960 info->Depth = 1; 961 info->MipLevels = 1; 962 info->ResourceType = D3DRTYPE_TEXTURE; 963 } 964 } 965 966 if (decoder) 967 IWICBitmapDecoder_Release(decoder); 968 969 if (dib) 970 HeapFree(GetProcessHeap(), 0, (void*)data); 971 972 if (FAILED(hr)) { 973 TRACE("Invalid or unsupported image file\n"); 974 return D3DXERR_INVALIDDATA; 975 } 976 977 return D3D_OK; 978 } 979 980 /************************************************************ 981 * D3DXGetImageInfoFromFile 982 * 983 * RETURNS 984 * Success: D3D_OK, if we successfully load a valid image file or 985 * if we successfully load a file which is no valid image and info is NULL 986 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or 987 * if file is not a valid image file and info is not NULL 988 * D3DERR_INVALIDCALL, if file is NULL 989 * 990 */ 991 HRESULT WINAPI D3DXGetImageInfoFromFileA(const char *file, D3DXIMAGE_INFO *info) 992 { 993 WCHAR *widename; 994 HRESULT hr; 995 int strlength; 996 997 TRACE("file %s, info %p.\n", debugstr_a(file), info); 998 999 if( !file ) return D3DERR_INVALIDCALL; 1000 1001 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0); 1002 widename = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*widename)); 1003 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength); 1004 1005 hr = D3DXGetImageInfoFromFileW(widename, info); 1006 HeapFree(GetProcessHeap(), 0, widename); 1007 1008 return hr; 1009 } 1010 1011 HRESULT WINAPI D3DXGetImageInfoFromFileW(const WCHAR *file, D3DXIMAGE_INFO *info) 1012 { 1013 void *buffer; 1014 HRESULT hr; 1015 DWORD size; 1016 1017 TRACE("file %s, info %p.\n", debugstr_w(file), info); 1018 1019 if (!file) 1020 return D3DERR_INVALIDCALL; 1021 1022 if (FAILED(map_view_of_file(file, &buffer, &size))) 1023 return D3DXERR_INVALIDDATA; 1024 1025 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info); 1026 UnmapViewOfFile(buffer); 1027 1028 return hr; 1029 } 1030 1031 /************************************************************ 1032 * D3DXGetImageInfoFromResource 1033 * 1034 * RETURNS 1035 * Success: D3D_OK, if resource is a valid image file 1036 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or 1037 * if we fail to load resource 1038 * 1039 */ 1040 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, const char *resource, D3DXIMAGE_INFO *info) 1041 { 1042 HRSRC resinfo; 1043 void *buffer; 1044 DWORD size; 1045 1046 TRACE("module %p, resource %s, info %p.\n", module, debugstr_a(resource), info); 1047 1048 if (!(resinfo = FindResourceA(module, resource, (const char *)RT_RCDATA)) 1049 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */ 1050 && !(resinfo = FindResourceA(module, resource, (const char *)RT_BITMAP))) 1051 return D3DXERR_INVALIDDATA; 1052 1053 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size))) 1054 return D3DXERR_INVALIDDATA; 1055 1056 return D3DXGetImageInfoFromFileInMemory(buffer, size, info); 1057 } 1058 1059 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, const WCHAR *resource, D3DXIMAGE_INFO *info) 1060 { 1061 HRSRC resinfo; 1062 void *buffer; 1063 DWORD size; 1064 1065 TRACE("module %p, resource %s, info %p.\n", module, debugstr_w(resource), info); 1066 1067 if (!(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_RCDATA)) 1068 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */ 1069 && !(resinfo = FindResourceW(module, resource, (const WCHAR *)RT_BITMAP))) 1070 return D3DXERR_INVALIDDATA; 1071 1072 if (FAILED(load_resource_into_memory(module, resinfo, &buffer, &size))) 1073 return D3DXERR_INVALIDDATA; 1074 1075 return D3DXGetImageInfoFromFileInMemory(buffer, size, info); 1076 } 1077 1078 /************************************************************ 1079 * D3DXLoadSurfaceFromFileInMemory 1080 * 1081 * Loads data from a given buffer into a surface and fills a given 1082 * D3DXIMAGE_INFO structure with info about the source data. 1083 * 1084 * PARAMS 1085 * pDestSurface [I] pointer to the surface 1086 * pDestPalette [I] palette to use 1087 * pDestRect [I] to be filled area of the surface 1088 * pSrcData [I] pointer to the source data 1089 * SrcDataSize [I] size of the source data in bytes 1090 * pSrcRect [I] area of the source data to load 1091 * dwFilter [I] filter to apply on stretching 1092 * Colorkey [I] colorkey 1093 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure 1094 * 1095 * RETURNS 1096 * Success: D3D_OK 1097 * Failure: D3DERR_INVALIDCALL, if pDestSurface, pSrcData or SrcDataSize is NULL 1098 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file 1099 * 1100 */ 1101 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(IDirect3DSurface9 *pDestSurface, 1102 const PALETTEENTRY *pDestPalette, const RECT *pDestRect, const void *pSrcData, UINT SrcDataSize, 1103 const RECT *pSrcRect, DWORD dwFilter, D3DCOLOR Colorkey, D3DXIMAGE_INFO *pSrcInfo) 1104 { 1105 D3DXIMAGE_INFO imginfo; 1106 HRESULT hr; 1107 1108 IWICImagingFactory *factory = NULL; 1109 IWICBitmapDecoder *decoder; 1110 IWICBitmapFrameDecode *bitmapframe; 1111 IWICStream *stream; 1112 1113 const struct pixel_format_desc *formatdesc; 1114 WICRect wicrect; 1115 RECT rect; 1116 1117 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_data %p, src_data_size %u, " 1118 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n", 1119 pDestSurface, pDestPalette, wine_dbgstr_rect(pDestRect), pSrcData, SrcDataSize, 1120 wine_dbgstr_rect(pSrcRect), dwFilter, Colorkey, pSrcInfo); 1121 1122 if (!pDestSurface || !pSrcData || !SrcDataSize) 1123 return D3DERR_INVALIDCALL; 1124 1125 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo); 1126 1127 if (FAILED(hr)) 1128 return hr; 1129 1130 if (pSrcRect) 1131 { 1132 wicrect.X = pSrcRect->left; 1133 wicrect.Y = pSrcRect->top; 1134 wicrect.Width = pSrcRect->right - pSrcRect->left; 1135 wicrect.Height = pSrcRect->bottom - pSrcRect->top; 1136 } 1137 else 1138 { 1139 wicrect.X = 0; 1140 wicrect.Y = 0; 1141 wicrect.Width = imginfo.Width; 1142 wicrect.Height = imginfo.Height; 1143 } 1144 1145 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height); 1146 1147 if (imginfo.ImageFileFormat == D3DXIFF_DDS) 1148 { 1149 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect, 1150 dwFilter, Colorkey, &imginfo); 1151 if (SUCCEEDED(hr) && pSrcInfo) 1152 *pSrcInfo = imginfo; 1153 return hr; 1154 } 1155 1156 if (imginfo.ImageFileFormat == D3DXIFF_DIB) 1157 convert_dib_to_bmp((void**)&pSrcData, &SrcDataSize); 1158 1159 if (FAILED(WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory))) 1160 goto cleanup_err; 1161 1162 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream))) 1163 { 1164 IWICImagingFactory_Release(factory); 1165 factory = NULL; 1166 goto cleanup_err; 1167 } 1168 1169 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize); 1170 1171 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder); 1172 1173 IWICStream_Release(stream); 1174 1175 if (FAILED(hr)) 1176 goto cleanup_err; 1177 1178 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe); 1179 1180 if (FAILED(hr)) 1181 goto cleanup_bmp; 1182 1183 formatdesc = get_format_info(imginfo.Format); 1184 1185 if (formatdesc->type == FORMAT_UNKNOWN) 1186 { 1187 FIXME("Unsupported pixel format\n"); 1188 hr = D3DXERR_INVALIDDATA; 1189 } 1190 else 1191 { 1192 BYTE *buffer; 1193 DWORD pitch; 1194 PALETTEENTRY *palette = NULL; 1195 WICColor *colors = NULL; 1196 1197 pitch = formatdesc->bytes_per_pixel * wicrect.Width; 1198 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height); 1199 1200 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch, 1201 pitch * wicrect.Height, buffer); 1202 1203 if (SUCCEEDED(hr) && (formatdesc->type == FORMAT_INDEX)) 1204 { 1205 IWICPalette *wic_palette = NULL; 1206 UINT nb_colors; 1207 1208 hr = IWICImagingFactory_CreatePalette(factory, &wic_palette); 1209 if (SUCCEEDED(hr)) 1210 hr = IWICBitmapFrameDecode_CopyPalette(bitmapframe, wic_palette); 1211 if (SUCCEEDED(hr)) 1212 hr = IWICPalette_GetColorCount(wic_palette, &nb_colors); 1213 if (SUCCEEDED(hr)) 1214 { 1215 colors = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(colors[0])); 1216 palette = HeapAlloc(GetProcessHeap(), 0, nb_colors * sizeof(palette[0])); 1217 if (!colors || !palette) 1218 hr = E_OUTOFMEMORY; 1219 } 1220 if (SUCCEEDED(hr)) 1221 hr = IWICPalette_GetColors(wic_palette, nb_colors, colors, &nb_colors); 1222 if (SUCCEEDED(hr)) 1223 { 1224 UINT i; 1225 1226 /* Convert colors from WICColor (ARGB) to PALETTEENTRY (ABGR) */ 1227 for (i = 0; i < nb_colors; i++) 1228 { 1229 palette[i].peRed = (colors[i] >> 16) & 0xff; 1230 palette[i].peGreen = (colors[i] >> 8) & 0xff; 1231 palette[i].peBlue = colors[i] & 0xff; 1232 palette[i].peFlags = (colors[i] >> 24) & 0xff; /* peFlags is the alpha component in DX8 and higher */ 1233 } 1234 } 1235 if (wic_palette) 1236 IWICPalette_Release(wic_palette); 1237 } 1238 1239 if (SUCCEEDED(hr)) 1240 { 1241 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect, 1242 buffer, imginfo.Format, pitch, 1243 palette, &rect, dwFilter, Colorkey); 1244 } 1245 1246 HeapFree(GetProcessHeap(), 0, colors); 1247 HeapFree(GetProcessHeap(), 0, palette); 1248 HeapFree(GetProcessHeap(), 0, buffer); 1249 } 1250 1251 IWICBitmapFrameDecode_Release(bitmapframe); 1252 1253 cleanup_bmp: 1254 IWICBitmapDecoder_Release(decoder); 1255 1256 cleanup_err: 1257 if (factory) 1258 IWICImagingFactory_Release(factory); 1259 1260 if (imginfo.ImageFileFormat == D3DXIFF_DIB) 1261 HeapFree(GetProcessHeap(), 0, (void*)pSrcData); 1262 1263 if (FAILED(hr)) 1264 return D3DXERR_INVALIDDATA; 1265 1266 if (pSrcInfo) 1267 *pSrcInfo = imginfo; 1268 1269 return D3D_OK; 1270 } 1271 1272 HRESULT WINAPI D3DXLoadSurfaceFromFileA(IDirect3DSurface9 *dst_surface, 1273 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const char *src_file, 1274 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info) 1275 { 1276 WCHAR *src_file_w; 1277 HRESULT hr; 1278 int strlength; 1279 1280 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, " 1281 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n", 1282 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_a(src_file), 1283 wine_dbgstr_rect(src_rect), filter, color_key, src_info); 1284 1285 if (!src_file || !dst_surface) 1286 return D3DERR_INVALIDCALL; 1287 1288 strlength = MultiByteToWideChar(CP_ACP, 0, src_file, -1, NULL, 0); 1289 src_file_w = HeapAlloc(GetProcessHeap(), 0, strlength * sizeof(*src_file_w)); 1290 MultiByteToWideChar(CP_ACP, 0, src_file, -1, src_file_w, strlength); 1291 1292 hr = D3DXLoadSurfaceFromFileW(dst_surface, dst_palette, dst_rect, 1293 src_file_w, src_rect, filter, color_key, src_info); 1294 HeapFree(GetProcessHeap(), 0, src_file_w); 1295 1296 return hr; 1297 } 1298 1299 HRESULT WINAPI D3DXLoadSurfaceFromFileW(IDirect3DSurface9 *dst_surface, 1300 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const WCHAR *src_file, 1301 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info) 1302 { 1303 UINT data_size; 1304 void *data; 1305 HRESULT hr; 1306 1307 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_file %s, " 1308 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n", 1309 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), debugstr_w(src_file), 1310 wine_dbgstr_rect(src_rect), filter, color_key, src_info); 1311 1312 if (!src_file || !dst_surface) 1313 return D3DERR_INVALIDCALL; 1314 1315 if (FAILED(map_view_of_file(src_file, &data, &data_size))) 1316 return D3DXERR_INVALIDDATA; 1317 1318 hr = D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect, 1319 data, data_size, src_rect, filter, color_key, src_info); 1320 UnmapViewOfFile(data); 1321 1322 return hr; 1323 } 1324 1325 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(IDirect3DSurface9 *dst_surface, 1326 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const char *resource, 1327 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info) 1328 { 1329 UINT data_size; 1330 HRSRC resinfo; 1331 void *data; 1332 1333 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, " 1334 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n", 1335 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_a(resource), 1336 wine_dbgstr_rect(src_rect), filter, color_key, src_info); 1337 1338 if (!dst_surface) 1339 return D3DERR_INVALIDCALL; 1340 1341 if (!(resinfo = FindResourceA(src_module, resource, (const char *)RT_RCDATA)) 1342 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */ 1343 && !(resinfo = FindResourceA(src_module, resource, (const char *)RT_BITMAP))) 1344 return D3DXERR_INVALIDDATA; 1345 1346 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size))) 1347 return D3DXERR_INVALIDDATA; 1348 1349 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect, 1350 data, data_size, src_rect, filter, color_key, src_info); 1351 } 1352 1353 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(IDirect3DSurface9 *dst_surface, 1354 const PALETTEENTRY *dst_palette, const RECT *dst_rect, HMODULE src_module, const WCHAR *resource, 1355 const RECT *src_rect, DWORD filter, D3DCOLOR color_key, D3DXIMAGE_INFO *src_info) 1356 { 1357 UINT data_size; 1358 HRSRC resinfo; 1359 void *data; 1360 1361 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_module %p, resource %s, " 1362 "src_rect %s, filter %#x, color_key 0x%08x, src_info %p.\n", 1363 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_module, debugstr_w(resource), 1364 wine_dbgstr_rect(src_rect), filter, color_key, src_info); 1365 1366 if (!dst_surface) 1367 return D3DERR_INVALIDCALL; 1368 1369 if (!(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_RCDATA)) 1370 /* Try loading the resource as bitmap data (which is in DIB format D3DXIFF_DIB) */ 1371 && !(resinfo = FindResourceW(src_module, resource, (const WCHAR *)RT_BITMAP))) 1372 return D3DXERR_INVALIDDATA; 1373 1374 if (FAILED(load_resource_into_memory(src_module, resinfo, &data, &data_size))) 1375 return D3DXERR_INVALIDDATA; 1376 1377 return D3DXLoadSurfaceFromFileInMemory(dst_surface, dst_palette, dst_rect, 1378 data, data_size, src_rect, filter, color_key, src_info); 1379 } 1380 1381 1382 /************************************************************ 1383 * helper functions for D3DXLoadSurfaceFromMemory 1384 */ 1385 struct argb_conversion_info 1386 { 1387 const struct pixel_format_desc *srcformat; 1388 const struct pixel_format_desc *destformat; 1389 DWORD srcshift[4], destshift[4]; 1390 DWORD srcmask[4], destmask[4]; 1391 BOOL process_channel[4]; 1392 DWORD channelmask; 1393 }; 1394 1395 static void init_argb_conversion_info(const struct pixel_format_desc *srcformat, const struct pixel_format_desc *destformat, struct argb_conversion_info *info) 1396 { 1397 UINT i; 1398 ZeroMemory(info->process_channel, 4 * sizeof(BOOL)); 1399 info->channelmask = 0; 1400 1401 info->srcformat = srcformat; 1402 info->destformat = destformat; 1403 1404 for(i = 0;i < 4;i++) { 1405 /* srcshift is used to extract the _relevant_ components */ 1406 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0); 1407 1408 /* destshift is used to move the components to the correct position */ 1409 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0); 1410 1411 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i]; 1412 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i]; 1413 1414 /* channelmask specifies bits which aren't used in the source format but in the destination one */ 1415 if(destformat->bits[i]) { 1416 if(srcformat->bits[i]) info->process_channel[i] = TRUE; 1417 else info->channelmask |= info->destmask[i]; 1418 } 1419 } 1420 } 1421 1422 /************************************************************ 1423 * get_relevant_argb_components 1424 * 1425 * Extracts the relevant components from the source color and 1426 * drops the less significant bits if they aren't used by the destination format. 1427 */ 1428 static void get_relevant_argb_components(const struct argb_conversion_info *info, const BYTE *col, DWORD *out) 1429 { 1430 unsigned int i, j; 1431 unsigned int component, mask; 1432 1433 for (i = 0; i < 4; ++i) 1434 { 1435 if (!info->process_channel[i]) 1436 continue; 1437 1438 component = 0; 1439 mask = info->srcmask[i]; 1440 for (j = 0; j < 4 && mask; ++j) 1441 { 1442 if (info->srcshift[i] < j * 8) 1443 component |= (col[j] & mask) << (j * 8 - info->srcshift[i]); 1444 else 1445 component |= (col[j] & mask) >> (info->srcshift[i] - j * 8); 1446 mask >>= 8; 1447 } 1448 out[i] = component; 1449 } 1450 } 1451 1452 /************************************************************ 1453 * make_argb_color 1454 * 1455 * Recombines the output of get_relevant_argb_components and converts 1456 * it to the destination format. 1457 */ 1458 static DWORD make_argb_color(const struct argb_conversion_info *info, const DWORD *in) 1459 { 1460 UINT i; 1461 DWORD val = 0; 1462 1463 for(i = 0;i < 4;i++) { 1464 if(info->process_channel[i]) { 1465 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */ 1466 signed int shift; 1467 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift; 1468 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i]; 1469 } 1470 } 1471 val |= info->channelmask; /* new channels are set to their maximal value */ 1472 return val; 1473 } 1474 1475 /* It doesn't work for components bigger than 32 bits (or somewhat smaller but unaligned). */ 1476 static void format_to_vec4(const struct pixel_format_desc *format, const BYTE *src, struct vec4 *dst) 1477 { 1478 DWORD mask, tmp; 1479 unsigned int c; 1480 1481 for (c = 0; c < 4; ++c) 1482 { 1483 static const unsigned int component_offsets[4] = {3, 0, 1, 2}; 1484 float *dst_component = (float *)dst + component_offsets[c]; 1485 1486 if (format->bits[c]) 1487 { 1488 mask = ~0u >> (32 - format->bits[c]); 1489 1490 memcpy(&tmp, src + format->shift[c] / 8, 1491 min(sizeof(DWORD), (format->shift[c] % 8 + format->bits[c] + 7) / 8)); 1492 1493 if (format->type == FORMAT_ARGBF16) 1494 *dst_component = float_16_to_32(tmp); 1495 else if (format->type == FORMAT_ARGBF) 1496 *dst_component = *(float *)&tmp; 1497 else 1498 *dst_component = (float)((tmp >> format->shift[c] % 8) & mask) / mask; 1499 } 1500 else 1501 *dst_component = 1.0f; 1502 } 1503 } 1504 1505 /* It doesn't work for components bigger than 32 bits. */ 1506 static void format_from_vec4(const struct pixel_format_desc *format, const struct vec4 *src, BYTE *dst) 1507 { 1508 DWORD v, mask32; 1509 unsigned int c, i; 1510 1511 memset(dst, 0, format->bytes_per_pixel); 1512 1513 for (c = 0; c < 4; ++c) 1514 { 1515 static const unsigned int component_offsets[4] = {3, 0, 1, 2}; 1516 const float src_component = *((const float *)src + component_offsets[c]); 1517 1518 if (!format->bits[c]) 1519 continue; 1520 1521 mask32 = ~0u >> (32 - format->bits[c]); 1522 1523 if (format->type == FORMAT_ARGBF16) 1524 v = float_32_to_16(src_component); 1525 else if (format->type == FORMAT_ARGBF) 1526 v = *(DWORD *)&src_component; 1527 else 1528 v = (DWORD)(src_component * ((1 << format->bits[c]) - 1) + 0.5f); 1529 1530 for (i = format->shift[c] / 8 * 8; i < format->shift[c] + format->bits[c]; i += 8) 1531 { 1532 BYTE mask, byte; 1533 1534 if (format->shift[c] > i) 1535 { 1536 mask = mask32 << (format->shift[c] - i); 1537 byte = (v << (format->shift[c] - i)) & mask; 1538 } 1539 else 1540 { 1541 mask = mask32 >> (i - format->shift[c]); 1542 byte = (v >> (i - format->shift[c])) & mask; 1543 } 1544 dst[i / 8] |= byte; 1545 } 1546 } 1547 } 1548 1549 /************************************************************ 1550 * copy_pixels 1551 * 1552 * Copies the source buffer to the destination buffer. 1553 * Works for any pixel format. 1554 * The source and the destination must be block-aligned. 1555 */ 1556 void copy_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, 1557 BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *size, 1558 const struct pixel_format_desc *format) 1559 { 1560 UINT row, slice; 1561 BYTE *dst_addr; 1562 const BYTE *src_addr; 1563 UINT row_block_count = (size->width + format->block_width - 1) / format->block_width; 1564 UINT row_count = (size->height + format->block_height - 1) / format->block_height; 1565 1566 for (slice = 0; slice < size->depth; slice++) 1567 { 1568 src_addr = src + slice * src_slice_pitch; 1569 dst_addr = dst + slice * dst_slice_pitch; 1570 1571 for (row = 0; row < row_count; row++) 1572 { 1573 memcpy(dst_addr, src_addr, row_block_count * format->block_byte_count); 1574 src_addr += src_row_pitch; 1575 dst_addr += dst_row_pitch; 1576 } 1577 } 1578 } 1579 1580 /************************************************************ 1581 * convert_argb_pixels 1582 * 1583 * Copies the source buffer to the destination buffer, performing 1584 * any necessary format conversion and color keying. 1585 * Pixels outsize the source rect are blacked out. 1586 */ 1587 void convert_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size, 1588 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, 1589 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key, 1590 const PALETTEENTRY *palette) 1591 { 1592 struct argb_conversion_info conv_info, ck_conv_info; 1593 const struct pixel_format_desc *ck_format = NULL; 1594 DWORD channels[4]; 1595 UINT min_width, min_height, min_depth; 1596 UINT x, y, z; 1597 1598 ZeroMemory(channels, sizeof(channels)); 1599 init_argb_conversion_info(src_format, dst_format, &conv_info); 1600 1601 min_width = min(src_size->width, dst_size->width); 1602 min_height = min(src_size->height, dst_size->height); 1603 min_depth = min(src_size->depth, dst_size->depth); 1604 1605 if (color_key) 1606 { 1607 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */ 1608 ck_format = get_format_info(D3DFMT_A8R8G8B8); 1609 init_argb_conversion_info(src_format, ck_format, &ck_conv_info); 1610 } 1611 1612 for (z = 0; z < min_depth; z++) { 1613 const BYTE *src_slice_ptr = src + z * src_slice_pitch; 1614 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch; 1615 1616 for (y = 0; y < min_height; y++) { 1617 const BYTE *src_ptr = src_slice_ptr + y * src_row_pitch; 1618 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch; 1619 1620 for (x = 0; x < min_width; x++) { 1621 if (!src_format->to_rgba && !dst_format->from_rgba 1622 && src_format->type == dst_format->type 1623 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4) 1624 { 1625 DWORD val; 1626 1627 get_relevant_argb_components(&conv_info, src_ptr, channels); 1628 val = make_argb_color(&conv_info, channels); 1629 1630 if (color_key) 1631 { 1632 DWORD ck_pixel; 1633 1634 get_relevant_argb_components(&ck_conv_info, src_ptr, channels); 1635 ck_pixel = make_argb_color(&ck_conv_info, channels); 1636 if (ck_pixel == color_key) 1637 val &= ~conv_info.destmask[0]; 1638 } 1639 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel); 1640 } 1641 else 1642 { 1643 struct vec4 color, tmp; 1644 1645 format_to_vec4(src_format, src_ptr, &color); 1646 if (src_format->to_rgba) 1647 src_format->to_rgba(&color, &tmp, palette); 1648 else 1649 tmp = color; 1650 1651 if (ck_format) 1652 { 1653 DWORD ck_pixel; 1654 1655 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel); 1656 if (ck_pixel == color_key) 1657 tmp.w = 0.0f; 1658 } 1659 1660 if (dst_format->from_rgba) 1661 dst_format->from_rgba(&tmp, &color); 1662 else 1663 color = tmp; 1664 1665 format_from_vec4(dst_format, &color, dst_ptr); 1666 } 1667 1668 src_ptr += src_format->bytes_per_pixel; 1669 dst_ptr += dst_format->bytes_per_pixel; 1670 } 1671 1672 if (src_size->width < dst_size->width) /* black out remaining pixels */ 1673 memset(dst_ptr, 0, dst_format->bytes_per_pixel * (dst_size->width - src_size->width)); 1674 } 1675 1676 if (src_size->height < dst_size->height) /* black out remaining pixels */ 1677 memset(dst + src_size->height * dst_row_pitch, 0, dst_row_pitch * (dst_size->height - src_size->height)); 1678 } 1679 if (src_size->depth < dst_size->depth) /* black out remaining pixels */ 1680 memset(dst + src_size->depth * dst_slice_pitch, 0, dst_slice_pitch * (dst_size->depth - src_size->depth)); 1681 } 1682 1683 /************************************************************ 1684 * point_filter_argb_pixels 1685 * 1686 * Copies the source buffer to the destination buffer, performing 1687 * any necessary format conversion, color keying and stretching 1688 * using a point filter. 1689 */ 1690 void point_filter_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch, const struct volume *src_size, 1691 const struct pixel_format_desc *src_format, BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, 1692 const struct volume *dst_size, const struct pixel_format_desc *dst_format, D3DCOLOR color_key, 1693 const PALETTEENTRY *palette) 1694 { 1695 struct argb_conversion_info conv_info, ck_conv_info; 1696 const struct pixel_format_desc *ck_format = NULL; 1697 DWORD channels[4]; 1698 UINT x, y, z; 1699 1700 ZeroMemory(channels, sizeof(channels)); 1701 init_argb_conversion_info(src_format, dst_format, &conv_info); 1702 1703 if (color_key) 1704 { 1705 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */ 1706 ck_format = get_format_info(D3DFMT_A8R8G8B8); 1707 init_argb_conversion_info(src_format, ck_format, &ck_conv_info); 1708 } 1709 1710 for (z = 0; z < dst_size->depth; z++) 1711 { 1712 BYTE *dst_slice_ptr = dst + z * dst_slice_pitch; 1713 const BYTE *src_slice_ptr = src + src_slice_pitch * (z * src_size->depth / dst_size->depth); 1714 1715 for (y = 0; y < dst_size->height; y++) 1716 { 1717 BYTE *dst_ptr = dst_slice_ptr + y * dst_row_pitch; 1718 const BYTE *src_row_ptr = src_slice_ptr + src_row_pitch * (y * src_size->height / dst_size->height); 1719 1720 for (x = 0; x < dst_size->width; x++) 1721 { 1722 const BYTE *src_ptr = src_row_ptr + (x * src_size->width / dst_size->width) * src_format->bytes_per_pixel; 1723 1724 if (!src_format->to_rgba && !dst_format->from_rgba 1725 && src_format->type == dst_format->type 1726 && src_format->bytes_per_pixel <= 4 && dst_format->bytes_per_pixel <= 4) 1727 { 1728 DWORD val; 1729 1730 get_relevant_argb_components(&conv_info, src_ptr, channels); 1731 val = make_argb_color(&conv_info, channels); 1732 1733 if (color_key) 1734 { 1735 DWORD ck_pixel; 1736 1737 get_relevant_argb_components(&ck_conv_info, src_ptr, channels); 1738 ck_pixel = make_argb_color(&ck_conv_info, channels); 1739 if (ck_pixel == color_key) 1740 val &= ~conv_info.destmask[0]; 1741 } 1742 memcpy(dst_ptr, &val, dst_format->bytes_per_pixel); 1743 } 1744 else 1745 { 1746 struct vec4 color, tmp; 1747 1748 format_to_vec4(src_format, src_ptr, &color); 1749 if (src_format->to_rgba) 1750 src_format->to_rgba(&color, &tmp, palette); 1751 else 1752 tmp = color; 1753 1754 if (ck_format) 1755 { 1756 DWORD ck_pixel; 1757 1758 format_from_vec4(ck_format, &tmp, (BYTE *)&ck_pixel); 1759 if (ck_pixel == color_key) 1760 tmp.w = 0.0f; 1761 } 1762 1763 if (dst_format->from_rgba) 1764 dst_format->from_rgba(&tmp, &color); 1765 else 1766 color = tmp; 1767 1768 format_from_vec4(dst_format, &color, dst_ptr); 1769 } 1770 1771 dst_ptr += dst_format->bytes_per_pixel; 1772 } 1773 } 1774 } 1775 } 1776 1777 /************************************************************ 1778 * D3DXLoadSurfaceFromMemory 1779 * 1780 * Loads data from a given memory chunk into a surface, 1781 * applying any of the specified filters. 1782 * 1783 * PARAMS 1784 * pDestSurface [I] pointer to the surface 1785 * pDestPalette [I] palette to use 1786 * pDestRect [I] to be filled area of the surface 1787 * pSrcMemory [I] pointer to the source data 1788 * SrcFormat [I] format of the source pixel data 1789 * SrcPitch [I] number of bytes in a row 1790 * pSrcPalette [I] palette used in the source image 1791 * pSrcRect [I] area of the source data to load 1792 * dwFilter [I] filter to apply on stretching 1793 * Colorkey [I] colorkey 1794 * 1795 * RETURNS 1796 * Success: D3D_OK, if we successfully load the pixel data into our surface or 1797 * if pSrcMemory is NULL but the other parameters are valid 1798 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect is NULL or 1799 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or 1800 * if DestRect is invalid 1801 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface 1802 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid 1803 * 1804 * NOTES 1805 * pSrcRect specifies the dimensions of the source data; 1806 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway. 1807 * 1808 */ 1809 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface, 1810 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory, 1811 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect, 1812 DWORD filter, D3DCOLOR color_key) 1813 { 1814 const struct pixel_format_desc *srcformatdesc, *destformatdesc; 1815 IDirect3DSurface9 *surface; 1816 D3DSURFACE_DESC surfdesc; 1817 D3DLOCKED_RECT lockrect; 1818 struct volume src_size, dst_size; 1819 HRESULT hr; 1820 1821 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s, %#x, 0x%08x)\n", 1822 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format, 1823 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key); 1824 1825 if (!dst_surface || !src_memory || !src_rect) 1826 { 1827 WARN("Invalid argument specified.\n"); 1828 return D3DERR_INVALIDCALL; 1829 } 1830 if (src_format == D3DFMT_UNKNOWN 1831 || src_rect->left >= src_rect->right 1832 || src_rect->top >= src_rect->bottom) 1833 { 1834 WARN("Invalid src_format or src_rect.\n"); 1835 return E_FAIL; 1836 } 1837 1838 if (filter == D3DX_DEFAULT) 1839 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER; 1840 1841 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc); 1842 1843 src_size.width = src_rect->right - src_rect->left; 1844 src_size.height = src_rect->bottom - src_rect->top; 1845 src_size.depth = 1; 1846 if (!dst_rect) 1847 { 1848 dst_size.width = surfdesc.Width; 1849 dst_size.height = surfdesc.Height; 1850 } 1851 else 1852 { 1853 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width 1854 || dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height 1855 || dst_rect->left < 0 || dst_rect->top < 0) 1856 { 1857 WARN("Invalid dst_rect specified.\n"); 1858 return D3DERR_INVALIDCALL; 1859 } 1860 dst_size.width = dst_rect->right - dst_rect->left; 1861 dst_size.height = dst_rect->bottom - dst_rect->top; 1862 if (!dst_size.width || !dst_size.height) 1863 return D3D_OK; 1864 } 1865 dst_size.depth = 1; 1866 1867 srcformatdesc = get_format_info(src_format); 1868 destformatdesc = get_format_info(surfdesc.Format); 1869 if (srcformatdesc->type == FORMAT_UNKNOWN || destformatdesc->type == FORMAT_UNKNOWN) 1870 { 1871 FIXME("Unsupported pixel format conversion %#x -> %#x\n", src_format, surfdesc.Format); 1872 return E_NOTIMPL; 1873 } 1874 1875 if (FAILED(hr = lock_surface(dst_surface, &lockrect, &surface, TRUE))) 1876 return hr; 1877 1878 if (src_format == surfdesc.Format 1879 && dst_size.width == src_size.width 1880 && dst_size.height == src_size.height 1881 && color_key == 0) /* Simple copy. */ 1882 { 1883 if (src_rect->left & (srcformatdesc->block_width - 1) 1884 || src_rect->top & (srcformatdesc->block_height - 1) 1885 || (src_rect->right & (srcformatdesc->block_width - 1) 1886 && src_size.width != surfdesc.Width) 1887 || (src_rect->bottom & (srcformatdesc->block_height - 1) 1888 && src_size.height != surfdesc.Height)) 1889 { 1890 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect)); 1891 unlock_surface(dst_surface, &lockrect, surface, FALSE); 1892 return D3DXERR_INVALIDDATA; 1893 } 1894 1895 copy_pixels(src_memory, src_pitch, 0, lockrect.pBits, lockrect.Pitch, 0, 1896 &src_size, srcformatdesc); 1897 } 1898 else /* Stretching or format conversion. */ 1899 { 1900 if (!is_conversion_from_supported(srcformatdesc) 1901 || !is_conversion_to_supported(destformatdesc)) 1902 { 1903 FIXME("Unsupported format conversion %#x -> %#x.\n", src_format, surfdesc.Format); 1904 unlock_surface(dst_surface, &lockrect, surface, FALSE); 1905 return E_NOTIMPL; 1906 } 1907 1908 if ((filter & 0xf) == D3DX_FILTER_NONE) 1909 { 1910 convert_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc, 1911 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette); 1912 } 1913 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */ 1914 { 1915 if ((filter & 0xf) != D3DX_FILTER_POINT) 1916 FIXME("Unhandled filter %#x.\n", filter); 1917 1918 /* Always apply a point filter until D3DX_FILTER_LINEAR, 1919 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */ 1920 point_filter_argb_pixels(src_memory, src_pitch, 0, &src_size, srcformatdesc, 1921 lockrect.pBits, lockrect.Pitch, 0, &dst_size, destformatdesc, color_key, src_palette); 1922 } 1923 } 1924 1925 return unlock_surface(dst_surface, &lockrect, surface, TRUE); 1926 } 1927 1928 /************************************************************ 1929 * D3DXLoadSurfaceFromSurface 1930 * 1931 * Copies the contents from one surface to another, performing any required 1932 * format conversion, resizing or filtering. 1933 * 1934 * PARAMS 1935 * pDestSurface [I] pointer to the destination surface 1936 * pDestPalette [I] palette to use 1937 * pDestRect [I] to be filled area of the surface 1938 * pSrcSurface [I] pointer to the source surface 1939 * pSrcPalette [I] palette used for the source surface 1940 * pSrcRect [I] area of the source data to load 1941 * dwFilter [I] filter to apply on resizing 1942 * Colorkey [I] any ARGB value or 0 to disable color-keying 1943 * 1944 * RETURNS 1945 * Success: D3D_OK 1946 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface is NULL 1947 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable 1948 * 1949 */ 1950 HRESULT WINAPI D3DXLoadSurfaceFromSurface(IDirect3DSurface9 *dst_surface, 1951 const PALETTEENTRY *dst_palette, const RECT *dst_rect, IDirect3DSurface9 *src_surface, 1952 const PALETTEENTRY *src_palette, const RECT *src_rect, DWORD filter, D3DCOLOR color_key) 1953 { 1954 IDirect3DSurface9 *temp_surface; 1955 D3DTEXTUREFILTERTYPE d3d_filter; 1956 IDirect3DDevice9 *device; 1957 D3DSURFACE_DESC src_desc; 1958 D3DLOCKED_RECT lock; 1959 HRESULT hr; 1960 RECT s; 1961 1962 TRACE("dst_surface %p, dst_palette %p, dst_rect %s, src_surface %p, " 1963 "src_palette %p, src_rect %s, filter %#x, color_key 0x%08x.\n", 1964 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_surface, 1965 src_palette, wine_dbgstr_rect(src_rect), filter, color_key); 1966 1967 if (!dst_surface || !src_surface) 1968 return D3DERR_INVALIDCALL; 1969 1970 if (!dst_palette && !src_palette && !color_key) 1971 { 1972 switch (filter) 1973 { 1974 case D3DX_FILTER_NONE: 1975 d3d_filter = D3DTEXF_NONE; 1976 break; 1977 1978 case D3DX_FILTER_POINT: 1979 d3d_filter = D3DTEXF_POINT; 1980 break; 1981 1982 case D3DX_FILTER_LINEAR: 1983 d3d_filter = D3DTEXF_LINEAR; 1984 break; 1985 1986 default: 1987 d3d_filter = ~0u; 1988 break; 1989 } 1990 1991 if (d3d_filter != ~0u) 1992 { 1993 IDirect3DSurface9_GetDevice(src_surface, &device); 1994 hr = IDirect3DDevice9_StretchRect(device, src_surface, src_rect, dst_surface, dst_rect, d3d_filter); 1995 IDirect3DDevice9_Release(device); 1996 if (SUCCEEDED(hr)) 1997 return D3D_OK; 1998 } 1999 } 2000 2001 IDirect3DSurface9_GetDesc(src_surface, &src_desc); 2002 2003 if (!src_rect) 2004 { 2005 SetRect(&s, 0, 0, src_desc.Width, src_desc.Height); 2006 src_rect = &s; 2007 } 2008 2009 if (FAILED(lock_surface(src_surface, &lock, &temp_surface, FALSE))) 2010 return D3DXERR_INVALIDDATA; 2011 2012 hr = D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, lock.pBits, 2013 src_desc.Format, lock.Pitch, src_palette, src_rect, filter, color_key); 2014 2015 if (FAILED(unlock_surface(src_surface, &lock, temp_surface, FALSE))) 2016 return D3DXERR_INVALIDDATA; 2017 2018 return hr; 2019 } 2020 2021 2022 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format, 2023 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect) 2024 { 2025 int len; 2026 WCHAR *filename; 2027 HRESULT hr; 2028 ID3DXBuffer *buffer; 2029 2030 TRACE("(%s, %#x, %p, %p, %s): relay\n", 2031 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect)); 2032 2033 if (!dst_filename) return D3DERR_INVALIDCALL; 2034 2035 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0); 2036 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); 2037 if (!filename) return E_OUTOFMEMORY; 2038 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len); 2039 2040 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect); 2041 if (SUCCEEDED(hr)) 2042 { 2043 hr = write_buffer_to_file(filename, buffer); 2044 ID3DXBuffer_Release(buffer); 2045 } 2046 2047 HeapFree(GetProcessHeap(), 0, filename); 2048 return hr; 2049 } 2050 2051 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format, 2052 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect) 2053 { 2054 HRESULT hr; 2055 ID3DXBuffer *buffer; 2056 2057 TRACE("(%s, %#x, %p, %p, %s): relay\n", 2058 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect)); 2059 2060 if (!dst_filename) return D3DERR_INVALIDCALL; 2061 2062 hr = D3DXSaveSurfaceToFileInMemory(&buffer, file_format, src_surface, src_palette, src_rect); 2063 if (SUCCEEDED(hr)) 2064 { 2065 hr = write_buffer_to_file(dst_filename, buffer); 2066 ID3DXBuffer_Release(buffer); 2067 } 2068 2069 return hr; 2070 } 2071 2072 HRESULT WINAPI D3DXSaveSurfaceToFileInMemory(ID3DXBuffer **dst_buffer, D3DXIMAGE_FILEFORMAT file_format, 2073 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect) 2074 { 2075 IWICBitmapEncoder *encoder = NULL; 2076 IWICBitmapFrameEncode *frame = NULL; 2077 IPropertyBag2 *encoder_options = NULL; 2078 IStream *stream = NULL; 2079 HRESULT hr; 2080 const GUID *container_format; 2081 const GUID *pixel_format_guid; 2082 WICPixelFormatGUID wic_pixel_format; 2083 IWICImagingFactory *factory; 2084 D3DFORMAT d3d_pixel_format; 2085 D3DSURFACE_DESC src_surface_desc; 2086 IDirect3DSurface9 *temp_surface; 2087 D3DLOCKED_RECT locked_rect; 2088 int width, height; 2089 STATSTG stream_stats; 2090 HGLOBAL stream_hglobal; 2091 ID3DXBuffer *buffer; 2092 DWORD size; 2093 2094 TRACE("dst_buffer %p, file_format %#x, src_surface %p, src_palette %p, src_rect %s.\n", 2095 dst_buffer, file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect)); 2096 2097 if (!dst_buffer || !src_surface) return D3DERR_INVALIDCALL; 2098 2099 if (src_palette) 2100 { 2101 FIXME("Saving surfaces with palettized pixel formats is not implemented yet\n"); 2102 return D3DERR_INVALIDCALL; 2103 } 2104 2105 switch (file_format) 2106 { 2107 case D3DXIFF_BMP: 2108 case D3DXIFF_DIB: 2109 container_format = &GUID_ContainerFormatBmp; 2110 break; 2111 case D3DXIFF_PNG: 2112 container_format = &GUID_ContainerFormatPng; 2113 break; 2114 case D3DXIFF_JPG: 2115 container_format = &GUID_ContainerFormatJpeg; 2116 break; 2117 case D3DXIFF_DDS: 2118 return save_dds_surface_to_memory(dst_buffer, src_surface, src_rect); 2119 case D3DXIFF_HDR: 2120 case D3DXIFF_PFM: 2121 case D3DXIFF_TGA: 2122 case D3DXIFF_PPM: 2123 FIXME("File format %#x is not supported yet\n", file_format); 2124 return E_NOTIMPL; 2125 default: 2126 return D3DERR_INVALIDCALL; 2127 } 2128 2129 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc); 2130 if (src_rect) 2131 { 2132 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom) 2133 { 2134 WARN("Invalid rectangle with 0 area\n"); 2135 return D3DXCreateBuffer(64, dst_buffer); 2136 } 2137 if (src_rect->left < 0 || src_rect->top < 0) 2138 return D3DERR_INVALIDCALL; 2139 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom) 2140 return D3DERR_INVALIDCALL; 2141 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height) 2142 return D3DERR_INVALIDCALL; 2143 2144 width = src_rect->right - src_rect->left; 2145 height = src_rect->bottom - src_rect->top; 2146 } 2147 else 2148 { 2149 width = src_surface_desc.Width; 2150 height = src_surface_desc.Height; 2151 } 2152 2153 hr = WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION, &factory); 2154 if (FAILED(hr)) goto cleanup_err; 2155 2156 hr = IWICImagingFactory_CreateEncoder(factory, container_format, NULL, &encoder); 2157 IWICImagingFactory_Release(factory); 2158 if (FAILED(hr)) goto cleanup_err; 2159 2160 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream); 2161 if (FAILED(hr)) goto cleanup_err; 2162 2163 hr = IWICBitmapEncoder_Initialize(encoder, stream, WICBitmapEncoderNoCache); 2164 if (FAILED(hr)) goto cleanup_err; 2165 2166 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options); 2167 if (FAILED(hr)) goto cleanup_err; 2168 2169 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options); 2170 if (FAILED(hr)) goto cleanup_err; 2171 2172 hr = IWICBitmapFrameEncode_SetSize(frame, width, height); 2173 if (FAILED(hr)) goto cleanup_err; 2174 2175 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format); 2176 if (!pixel_format_guid) 2177 { 2178 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format); 2179 hr = E_NOTIMPL; 2180 goto cleanup; 2181 } 2182 2183 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID)); 2184 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format); 2185 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format); 2186 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN) 2187 { 2188 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format); 2189 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */ 2190 { 2191 if (FAILED(hr = lock_surface(src_surface, &locked_rect, &temp_surface, FALSE))) 2192 goto cleanup; 2193 2194 IWICBitmapFrameEncode_WritePixels(frame, height, 2195 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits); 2196 unlock_surface(src_surface, &locked_rect, temp_surface, FALSE); 2197 } 2198 else /* Pixel format conversion */ 2199 { 2200 const struct pixel_format_desc *src_format_desc, *dst_format_desc; 2201 struct volume size; 2202 DWORD dst_pitch; 2203 void *dst_data; 2204 2205 src_format_desc = get_format_info(src_surface_desc.Format); 2206 dst_format_desc = get_format_info(d3d_pixel_format); 2207 if (!is_conversion_from_supported(src_format_desc) 2208 || !is_conversion_to_supported(dst_format_desc)) 2209 { 2210 FIXME("Unsupported format conversion %#x -> %#x.\n", 2211 src_surface_desc.Format, d3d_pixel_format); 2212 hr = E_NOTIMPL; 2213 goto cleanup; 2214 } 2215 2216 size.width = width; 2217 size.height = height; 2218 size.depth = 1; 2219 dst_pitch = width * dst_format_desc->bytes_per_pixel; 2220 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height); 2221 if (!dst_data) 2222 { 2223 hr = E_OUTOFMEMORY; 2224 goto cleanup; 2225 } 2226 if (FAILED(hr = lock_surface(src_surface, &locked_rect, &temp_surface, FALSE))) 2227 { 2228 HeapFree(GetProcessHeap(), 0, dst_data); 2229 goto cleanup; 2230 } 2231 convert_argb_pixels(locked_rect.pBits, locked_rect.Pitch, 0, &size, src_format_desc, 2232 dst_data, dst_pitch, 0, &size, dst_format_desc, 0, NULL); 2233 unlock_surface(src_surface, &locked_rect, temp_surface, FALSE); 2234 2235 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data); 2236 HeapFree(GetProcessHeap(), 0, dst_data); 2237 } 2238 2239 hr = IWICBitmapFrameEncode_Commit(frame); 2240 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder); 2241 } 2242 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format); 2243 2244 /* copy data from stream to ID3DXBuffer */ 2245 hr = IStream_Stat(stream, &stream_stats, STATFLAG_NONAME); 2246 if (FAILED(hr)) goto cleanup_err; 2247 2248 if (stream_stats.cbSize.u.HighPart != 0) 2249 { 2250 hr = D3DXERR_INVALIDDATA; 2251 goto cleanup; 2252 } 2253 size = stream_stats.cbSize.u.LowPart; 2254 2255 /* Remove BMP header for DIB */ 2256 if (file_format == D3DXIFF_DIB) 2257 size -= sizeof(BITMAPFILEHEADER); 2258 2259 hr = D3DXCreateBuffer(size, &buffer); 2260 if (FAILED(hr)) goto cleanup; 2261 2262 hr = GetHGlobalFromStream(stream, &stream_hglobal); 2263 if (SUCCEEDED(hr)) 2264 { 2265 void *buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer); 2266 void *stream_data = GlobalLock(stream_hglobal); 2267 /* Remove BMP header for DIB */ 2268 if (file_format == D3DXIFF_DIB) 2269 stream_data = (void*)((BYTE*)stream_data + sizeof(BITMAPFILEHEADER)); 2270 memcpy(buffer_pointer, stream_data, size); 2271 GlobalUnlock(stream_hglobal); 2272 *dst_buffer = buffer; 2273 } 2274 else ID3DXBuffer_Release(buffer); 2275 2276 cleanup_err: 2277 if (FAILED(hr) && hr != E_OUTOFMEMORY) 2278 hr = D3DERR_INVALIDCALL; 2279 2280 cleanup: 2281 if (stream) IStream_Release(stream); 2282 2283 if (frame) IWICBitmapFrameEncode_Release(frame); 2284 if (encoder_options) IPropertyBag2_Release(encoder_options); 2285 2286 if (encoder) IWICBitmapEncoder_Release(encoder); 2287 2288 return hr; 2289 } 2290