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