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