d3dx9: Add traces.
[wine/multimedia.git] / dlls / d3dx9_36 / surface.c
blob28975b98b8753330812bbe147c0fa42193288aa4
1 /*
2 * Copyright (C) 2009-2010 Tony Wasserka
3 * Copyright (C) 2012 Józef Kucia
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/debug.h"
22 #include "wine/unicode.h"
23 #include "d3dx9_36_private.h"
25 #include "initguid.h"
26 #include "wincodec.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
31 /* Wine-specific WIC GUIDs */
32 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
34 static const struct
36 const GUID *wic_guid;
37 D3DFORMAT d3dformat;
38 } wic_pixel_formats[] = {
39 { &GUID_WICPixelFormat8bppIndexed, D3DFMT_L8 },
40 { &GUID_WICPixelFormat1bppIndexed, D3DFMT_L8 },
41 { &GUID_WICPixelFormat4bppIndexed, D3DFMT_L8 },
42 { &GUID_WICPixelFormat16bppBGR555, D3DFMT_X1R5G5B5 },
43 { &GUID_WICPixelFormat16bppBGR565, D3DFMT_R5G6B5 },
44 { &GUID_WICPixelFormat24bppBGR, D3DFMT_R8G8B8 },
45 { &GUID_WICPixelFormat32bppBGR, D3DFMT_X8R8G8B8 },
46 { &GUID_WICPixelFormat32bppBGRA, D3DFMT_A8R8G8B8 }
49 static D3DFORMAT wic_guid_to_d3dformat(const GUID *guid)
51 int i;
53 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
55 if (IsEqualGUID(wic_pixel_formats[i].wic_guid, guid))
56 return wic_pixel_formats[i].d3dformat;
59 return D3DFMT_UNKNOWN;
62 static const GUID *d3dformat_to_wic_guid(D3DFORMAT format)
64 int i;
66 for (i = 0; i < sizeof(wic_pixel_formats) / sizeof(wic_pixel_formats[0]); i++)
68 if (wic_pixel_formats[i].d3dformat == format)
69 return wic_pixel_formats[i].wic_guid;
72 return NULL;
75 /* dds_header.flags */
76 #define DDS_CAPS 0x1
77 #define DDS_HEIGHT 0x2
78 #define DDS_WIDTH 0x2
79 #define DDS_PITCH 0x8
80 #define DDS_PIXELFORMAT 0x1000
81 #define DDS_MIPMAPCOUNT 0x20000
82 #define DDS_LINEARSIZE 0x80000
83 #define DDS_DEPTH 0x800000
85 /* dds_header.caps */
86 #define DDS_CAPS_COMPLEX 0x8
87 #define DDS_CAPS_TEXTURE 0x1000
88 #define DDS_CAPS_MIPMAP 0x400000
90 /* dds_header.caps2 */
91 #define DDS_CAPS2_CUBEMAP 0x200
92 #define DDS_CAPS2_CUBEMAP_POSITIVEX 0x400
93 #define DDS_CAPS2_CUBEMAP_NEGATIVEX 0x800
94 #define DDS_CAPS2_CUBEMAP_POSITIVEY 0x1000
95 #define DDS_CAPS2_CUBEMAP_NEGATIVEY 0x2000
96 #define DDS_CAPS2_CUBEMAP_POSITIVEZ 0x4000
97 #define DDS_CAPS2_CUBEMAP_NEGATIVEZ 0x8000
98 #define DDS_CAPS2_CUBEMAP_ALL_FACES ( DDS_CAPS2_CUBEMAP_POSITIVEX | DDS_CAPS2_CUBEMAP_NEGATIVEX \
99 | DDS_CAPS2_CUBEMAP_POSITIVEY | DDS_CAPS2_CUBEMAP_NEGATIVEY \
100 | DDS_CAPS2_CUBEMAP_POSITIVEZ | DDS_CAPS2_CUBEMAP_NEGATIVEZ )
101 #define DDS_CAPS2_VOLUME 0x200000
103 /* dds_pixel_format.flags */
104 #define DDS_PF_ALPHA 0x1
105 #define DDS_PF_ALPHA_ONLY 0x2
106 #define DDS_PF_FOURCC 0x4
107 #define DDS_PF_RGB 0x40
108 #define DDS_PF_YUV 0x200
109 #define DDS_PF_LUMINANCE 0x20000
110 #define DDS_PF_BUMPDUDV 0x80000
112 struct dds_pixel_format
114 DWORD size;
115 DWORD flags;
116 DWORD fourcc;
117 DWORD bpp;
118 DWORD rmask;
119 DWORD gmask;
120 DWORD bmask;
121 DWORD amask;
124 struct dds_header
126 DWORD signature;
127 DWORD size;
128 DWORD flags;
129 DWORD height;
130 DWORD width;
131 DWORD pitch_or_linear_size;
132 DWORD depth;
133 DWORD miplevels;
134 DWORD reserved[11];
135 struct dds_pixel_format pixel_format;
136 DWORD caps;
137 DWORD caps2;
138 DWORD caps3;
139 DWORD caps4;
140 DWORD reserved2;
143 static D3DFORMAT dds_fourcc_to_d3dformat(DWORD fourcc)
145 int i;
146 static const DWORD known_fourcc[] = {
147 MAKEFOURCC('U','Y','V','Y'),
148 MAKEFOURCC('Y','U','Y','2'),
149 MAKEFOURCC('R','G','B','G'),
150 MAKEFOURCC('G','R','G','B'),
151 MAKEFOURCC('D','X','T','1'),
152 MAKEFOURCC('D','X','T','2'),
153 MAKEFOURCC('D','X','T','3'),
154 MAKEFOURCC('D','X','T','4'),
155 MAKEFOURCC('D','X','T','5')
158 for (i = 0; i < sizeof(known_fourcc) / sizeof(known_fourcc[0]); i++)
160 if (known_fourcc[i] == fourcc)
161 return fourcc;
164 WARN("Unknown FourCC %#x\n", fourcc);
165 return D3DFMT_UNKNOWN;
168 static D3DFORMAT dds_rgb_to_d3dformat(const struct dds_pixel_format *pixel_format)
170 int i;
171 static const struct {
172 DWORD bpp;
173 DWORD rmask;
174 DWORD gmask;
175 DWORD bmask;
176 DWORD amask;
177 D3DFORMAT format;
178 } rgb_pixel_formats[] = {
179 { 8, 0xe0, 0x1c, 0x03, 0, D3DFMT_R3G3B2 },
180 { 16, 0xf800, 0x07e0, 0x001f, 0x0000, D3DFMT_R5G6B5 },
181 { 16, 0x7c00, 0x03e0, 0x001f, 0x8000, D3DFMT_A1R5G5B5 },
182 { 16, 0x7c00, 0x03e0, 0x001f, 0x0000, D3DFMT_X1R5G5B5 },
183 { 16, 0x0f00, 0x00f0, 0x000f, 0xf000, D3DFMT_A4R4G4B4 },
184 { 16, 0x0f00, 0x00f0, 0x000f, 0x0000, D3DFMT_X4R4G4B4 },
185 { 16, 0x00e0, 0x001c, 0x0003, 0xff00, D3DFMT_A8R3G3B2 },
186 { 24, 0xff0000, 0x00ff00, 0x0000ff, 0x000000, D3DFMT_R8G8B8 },
187 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000, D3DFMT_A8R8G8B8 },
188 { 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000, D3DFMT_X8R8G8B8 },
189 { 32, 0x3ff00000, 0x000ffc00, 0x000003ff, 0xc0000000, D3DFMT_A2B10G10R10 },
190 { 32, 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000, D3DFMT_A2R10G10B10 },
191 { 32, 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000, D3DFMT_G16R16 },
194 for (i = 0; i < sizeof(rgb_pixel_formats) / sizeof(rgb_pixel_formats[0]); i++)
196 if (rgb_pixel_formats[i].bpp == pixel_format->bpp
197 && rgb_pixel_formats[i].rmask == pixel_format->rmask
198 && rgb_pixel_formats[i].gmask == pixel_format->gmask
199 && rgb_pixel_formats[i].bmask == pixel_format->bmask)
201 if ((pixel_format->flags & DDS_PF_ALPHA) && rgb_pixel_formats[i].amask == pixel_format->amask)
202 return rgb_pixel_formats[i].format;
203 if (rgb_pixel_formats[i].amask == 0)
204 return rgb_pixel_formats[i].format;
208 WARN("Unknown RGB pixel format (%#x, %#x, %#x, %#x)\n",
209 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
210 return D3DFMT_UNKNOWN;
213 static D3DFORMAT dds_luminance_to_d3dformat(const struct dds_pixel_format *pixel_format)
215 if (pixel_format->bpp == 8)
217 if (pixel_format->rmask == 0xff)
218 return D3DFMT_L8;
219 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x0f && pixel_format->amask == 0xf0)
220 return D3DFMT_A4L4;
222 if (pixel_format->bpp == 16)
224 if (pixel_format->rmask == 0xffff)
225 return D3DFMT_L16;
226 if ((pixel_format->flags & DDS_PF_ALPHA) && pixel_format->rmask == 0x00ff && pixel_format->amask == 0xff00)
227 return D3DFMT_A8L8;
230 WARN("Unknown luminance pixel format (bpp %#x, l %#x, a %#x)\n",
231 pixel_format->bpp, pixel_format->rmask, pixel_format->amask);
232 return D3DFMT_UNKNOWN;
235 static D3DFORMAT dds_alpha_to_d3dformat(const struct dds_pixel_format *pixel_format)
237 if (pixel_format->bpp == 8 && pixel_format->amask == 0xff)
238 return D3DFMT_A8;
240 WARN("Unknown Alpha pixel format (%#x, %#x)\n", pixel_format->bpp, pixel_format->rmask);
241 return D3DFMT_UNKNOWN;
244 static D3DFORMAT dds_bump_to_d3dformat(const struct dds_pixel_format *pixel_format)
246 if (pixel_format->bpp == 16 && pixel_format->rmask == 0x00ff && pixel_format->gmask == 0xff00)
247 return D3DFMT_V8U8;
248 if (pixel_format->bpp == 32 && pixel_format->rmask == 0x0000ffff && pixel_format->gmask == 0xffff0000)
249 return D3DFMT_V16U16;
251 WARN("Unknown bump pixel format (%#x, %#x, %#x, %#x, %#x)\n", pixel_format->bpp,
252 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
253 return D3DFMT_UNKNOWN;
256 static D3DFORMAT dds_pixel_format_to_d3dformat(const struct dds_pixel_format *pixel_format)
258 if (pixel_format->flags & DDS_PF_FOURCC)
259 return dds_fourcc_to_d3dformat(pixel_format->fourcc);
260 if (pixel_format->flags & DDS_PF_RGB)
261 return dds_rgb_to_d3dformat(pixel_format);
262 if (pixel_format->flags & DDS_PF_LUMINANCE)
263 return dds_luminance_to_d3dformat(pixel_format);
264 if (pixel_format->flags & DDS_PF_ALPHA_ONLY)
265 return dds_alpha_to_d3dformat(pixel_format);
266 if (pixel_format->flags & DDS_PF_BUMPDUDV)
267 return dds_bump_to_d3dformat(pixel_format);
269 WARN("Unknown pixel format (flags %#x, fourcc %#x, bpp %#x, r %#x, g %#x, b %#x, a %#x)\n",
270 pixel_format->flags, pixel_format->fourcc, pixel_format->bpp,
271 pixel_format->rmask, pixel_format->gmask, pixel_format->bmask, pixel_format->amask);
272 return D3DFMT_UNKNOWN;
275 static HRESULT calculate_dds_surface_size(const D3DXIMAGE_INFO *img_info,
276 UINT width, UINT height, UINT *pitch, UINT *size)
278 const PixelFormatDesc *format_desc = get_format_info(img_info->Format);
279 if (format_desc->format == D3DFMT_UNKNOWN)
280 return E_NOTIMPL;
282 if (format_desc->block_width != 1 || format_desc->block_height != 1)
284 *pitch = format_desc->block_byte_count
285 * max(1, (width + format_desc->block_width - 1) / format_desc->block_width);
286 *size = *pitch
287 * max(1, (height + format_desc->block_height - 1) / format_desc->block_height);
289 else
291 *pitch = width * format_desc->bytes_per_pixel;
292 *size = *pitch * height;
295 return D3D_OK;
298 /************************************************************
299 * get_image_info_from_dds
301 * Fills a D3DXIMAGE_INFO structure with information
302 * about a DDS file stored in the memory.
304 * PARAMS
305 * buffer [I] pointer to DDS data
306 * length [I] size of DDS data
307 * info [O] pointer to D3DXIMAGE_INFO structure
309 * RETURNS
310 * Success: D3D_OK
311 * Failure: D3DXERR_INVALIDDATA
314 static HRESULT get_image_info_from_dds(const void *buffer, UINT length, D3DXIMAGE_INFO *info)
316 UINT i;
317 UINT faces = 0;
318 UINT width, height;
319 const struct dds_header *header = buffer;
320 UINT expected_length = 0;
322 if (length < sizeof(*header) || !info)
323 return D3DXERR_INVALIDDATA;
325 if (header->pixel_format.size != sizeof(header->pixel_format))
326 return D3DXERR_INVALIDDATA;
328 info->Width = header->width;
329 info->Height = header->height;
330 info->Depth = 1;
331 info->MipLevels = (header->flags & DDS_MIPMAPCOUNT) ? header->miplevels : 1;
333 info->Format = dds_pixel_format_to_d3dformat(&header->pixel_format);
334 if (info->Format == D3DFMT_UNKNOWN)
335 return D3DXERR_INVALIDDATA;
337 TRACE("Pixel format is %#x\n", info->Format);
339 if (header->caps2 & DDS_CAPS2_VOLUME)
341 info->Depth = header->depth;
342 info->ResourceType = D3DRTYPE_VOLUMETEXTURE;
344 else if (header->caps2 & DDS_CAPS2_CUBEMAP)
346 DWORD face;
347 for (face = DDS_CAPS2_CUBEMAP_POSITIVEX; face <= DDS_CAPS2_CUBEMAP_NEGATIVEZ; face <<= 1)
349 if (header->caps2 & face)
350 faces++;
352 info->ResourceType = D3DRTYPE_CUBETEXTURE;
354 else
356 faces = 1;
357 info->ResourceType = D3DRTYPE_TEXTURE;
360 /* calculate the expected length */
361 width = info->Width;
362 height = info->Height;
363 for (i = 0; i < info->MipLevels; i++)
365 UINT pitch, size = 0;
366 calculate_dds_surface_size(info, width, height, &pitch, &size);
367 expected_length += size;
368 width = max(1, width / 2);
369 height = max(1, width / 2);
372 expected_length *= faces;
373 expected_length += sizeof(*header);
374 if (length < expected_length)
375 return D3DXERR_INVALIDDATA;
377 info->ImageFileFormat = D3DXIFF_DDS;
379 return D3D_OK;
382 static HRESULT load_surface_from_dds(IDirect3DSurface9 *dst_surface, const PALETTEENTRY *dst_palette,
383 const RECT *dst_rect, const void *src_data, const RECT *src_rect, DWORD filter, D3DCOLOR color_key,
384 const D3DXIMAGE_INFO *src_info)
386 UINT size;
387 UINT src_pitch;
388 const struct dds_header *header = src_data;
389 const BYTE *pixels = (BYTE *)(header + 1);
391 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
392 return D3DXERR_INVALIDDATA;
394 if (FAILED(calculate_dds_surface_size(src_info, src_info->Width, src_info->Height, &src_pitch, &size)))
395 return E_NOTIMPL;
397 return D3DXLoadSurfaceFromMemory(dst_surface, dst_palette, dst_rect, pixels, src_info->Format,
398 src_pitch, NULL, src_rect, filter, color_key);
401 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
402 DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info)
405 HRESULT hr;
406 RECT src_rect;
407 UINT src_pitch;
408 UINT mip_level;
409 UINT mip_levels;
410 UINT mip_level_size;
411 UINT width, height;
412 IDirect3DSurface9 *surface;
413 const struct dds_header *header = src_data;
414 const BYTE *pixels = (BYTE *)(header + 1);
416 if (src_info->ResourceType != D3DRTYPE_TEXTURE)
417 return D3DXERR_INVALIDDATA;
419 width = src_info->Width;
420 height = src_info->Height;
421 mip_levels = min(src_info->MipLevels, IDirect3DTexture9_GetLevelCount(texture));
422 for (mip_level = 0; mip_level < mip_levels; mip_level++)
424 hr = calculate_dds_surface_size(src_info, width, height, &src_pitch, &mip_level_size);
425 if (FAILED(hr)) return hr;
427 SetRect(&src_rect, 0, 0, width, height);
429 IDirect3DTexture9_GetSurfaceLevel(texture, mip_level, &surface);
430 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
431 NULL, &src_rect, filter, color_key);
432 IDirect3DSurface9_Release(surface);
433 if (FAILED(hr)) return hr;
435 pixels += mip_level_size;
436 width = max(1, width / 2);
437 height = max(1, height / 2);
440 return D3D_OK;
443 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
444 const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info)
446 HRESULT hr;
447 int face;
448 int mip_level;
449 UINT size;
450 RECT src_rect;
451 UINT src_pitch;
452 UINT mip_levels;
453 UINT mip_level_size;
454 IDirect3DSurface9 *surface;
455 const struct dds_header *header = src_data;
456 const BYTE *pixels = (BYTE *)(header + 1);
458 if (src_info->ResourceType != D3DRTYPE_CUBETEXTURE)
459 return D3DXERR_INVALIDDATA;
461 if ((header->caps2 & DDS_CAPS2_CUBEMAP_ALL_FACES) != DDS_CAPS2_CUBEMAP_ALL_FACES)
463 WARN("Only full cubemaps are supported\n");
464 return D3DXERR_INVALIDDATA;
467 mip_levels = min(src_info->MipLevels, IDirect3DCubeTexture9_GetLevelCount(cube_texture));
468 for (face = D3DCUBEMAP_FACE_POSITIVE_X; face <= D3DCUBEMAP_FACE_NEGATIVE_Z; face++)
470 size = src_info->Width;
471 for (mip_level = 0; mip_level < src_info->MipLevels; mip_level++)
473 hr = calculate_dds_surface_size(src_info, size, size, &src_pitch, &mip_level_size);
474 if (FAILED(hr)) return hr;
476 /* if texture has fewer mip levels than DDS file, skip excessive mip levels */
477 if (mip_level < mip_levels)
479 SetRect(&src_rect, 0, 0, size, size);
481 IDirect3DCubeTexture9_GetCubeMapSurface(cube_texture, face, mip_level, &surface);
482 hr = D3DXLoadSurfaceFromMemory(surface, palette, NULL, pixels, src_info->Format, src_pitch,
483 NULL, &src_rect, filter, color_key);
484 IDirect3DSurface9_Release(surface);
485 if (FAILED(hr)) return hr;
488 pixels += mip_level_size;
489 size = max(1, size / 2);
493 return D3D_OK;
496 /************************************************************
497 * D3DXGetImageInfoFromFileInMemory
499 * Fills a D3DXIMAGE_INFO structure with info about an image
501 * PARAMS
502 * data [I] pointer to the image file data
503 * datasize [I] size of the passed data
504 * info [O] pointer to the destination structure
506 * RETURNS
507 * Success: D3D_OK, if info is not NULL and data and datasize make up a valid image file or
508 * if info is NULL and data and datasize are not NULL
509 * Failure: D3DXERR_INVALIDDATA, if data is no valid image file and datasize and info are not NULL
510 * D3DERR_INVALIDCALL, if data is NULL or
511 * if datasize is 0
513 * NOTES
514 * datasize may be bigger than the actual file size
517 HRESULT WINAPI D3DXGetImageInfoFromFileInMemory(LPCVOID data, UINT datasize, D3DXIMAGE_INFO *info)
519 IWICImagingFactory *factory;
520 IWICBitmapDecoder *decoder = NULL;
521 IWICStream *stream;
522 HRESULT hr;
523 HRESULT initresult;
525 TRACE("(%p, %d, %p)\n", data, datasize, info);
527 if (!data || !datasize)
528 return D3DERR_INVALIDCALL;
530 if (!info)
531 return D3D_OK;
533 if ((datasize >= 4) && !strncmp(data, "DDS ", 4)) {
534 TRACE("File type is DDS\n");
535 return get_image_info_from_dds(data, datasize, info);
538 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
540 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory);
542 if (SUCCEEDED(hr)) {
543 IWICImagingFactory_CreateStream(factory, &stream);
544 IWICStream_InitializeFromMemory(stream, (BYTE*)data, datasize);
545 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
546 IStream_Release(stream);
547 IWICImagingFactory_Release(factory);
550 if (FAILED(hr)) {
551 if ((datasize >= 2) && (!strncmp(data, "P3", 2) || !strncmp(data, "P6", 2)))
552 FIXME("File type PPM is not supported yet\n");
553 else if ((datasize >= 2) && !strncmp(data, "BM", 2))
554 FIXME("File type DIB is not supported yet\n");
555 else if ((datasize >= 10) && !strncmp(data, "#?RADIANCE", 10))
556 FIXME("File type HDR is not supported yet\n");
557 else if ((datasize >= 2) && (!strncmp(data, "PF", 2) || !strncmp(data, "Pf", 2)))
558 FIXME("File type PFM is not supported yet\n");
561 if (SUCCEEDED(hr)) {
562 GUID container_format;
563 UINT frame_count;
565 hr = IWICBitmapDecoder_GetContainerFormat(decoder, &container_format);
566 if (SUCCEEDED(hr)) {
567 if (IsEqualGUID(&container_format, &GUID_ContainerFormatBmp)) {
568 TRACE("File type is BMP\n");
569 info->ImageFileFormat = D3DXIFF_BMP;
570 } else if (IsEqualGUID(&container_format, &GUID_ContainerFormatPng)) {
571 TRACE("File type is PNG\n");
572 info->ImageFileFormat = D3DXIFF_PNG;
573 } else if(IsEqualGUID(&container_format, &GUID_ContainerFormatJpeg)) {
574 TRACE("File type is JPG\n");
575 info->ImageFileFormat = D3DXIFF_JPG;
576 } else if(IsEqualGUID(&container_format, &GUID_WineContainerFormatTga)) {
577 TRACE("File type is TGA\n");
578 info->ImageFileFormat = D3DXIFF_TGA;
579 } else {
580 WARN("Unsupported image file format %s\n", debugstr_guid(&container_format));
581 hr = D3DXERR_INVALIDDATA;
585 if (SUCCEEDED(hr))
586 hr = IWICBitmapDecoder_GetFrameCount(decoder, &frame_count);
587 if (SUCCEEDED(hr) && !frame_count)
588 hr = D3DXERR_INVALIDDATA;
590 if (SUCCEEDED(hr)) {
591 IWICBitmapFrameDecode *frame = NULL;
593 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &frame);
595 if (SUCCEEDED(hr))
596 hr = IWICBitmapFrameDecode_GetSize(frame, &info->Width, &info->Height);
598 if (SUCCEEDED(hr)) {
599 WICPixelFormatGUID pixel_format;
601 hr = IWICBitmapFrameDecode_GetPixelFormat(frame, &pixel_format);
602 if (SUCCEEDED(hr)) {
603 info->Format = wic_guid_to_d3dformat(&pixel_format);
604 if (info->Format == D3DFMT_UNKNOWN) {
605 WARN("Unsupported pixel format %s\n", debugstr_guid(&pixel_format));
606 hr = D3DXERR_INVALIDDATA;
611 if (frame)
612 IWICBitmapFrameDecode_Release(frame);
614 info->Depth = 1;
615 info->MipLevels = 1;
616 info->ResourceType = D3DRTYPE_TEXTURE;
620 if (decoder)
621 IWICBitmapDecoder_Release(decoder);
623 if (SUCCEEDED(initresult))
624 CoUninitialize();
626 if (FAILED(hr)) {
627 TRACE("Invalid or unsupported image file\n");
628 return D3DXERR_INVALIDDATA;
631 return D3D_OK;
634 /************************************************************
635 * D3DXGetImageInfoFromFile
637 * RETURNS
638 * Success: D3D_OK, if we successfully load a valid image file or
639 * if we successfully load a file which is no valid image and info is NULL
640 * Failure: D3DXERR_INVALIDDATA, if we fail to load file or
641 * if file is not a valid image file and info is not NULL
642 * D3DERR_INVALIDCALL, if file is NULL
645 HRESULT WINAPI D3DXGetImageInfoFromFileA(LPCSTR file, D3DXIMAGE_INFO *info)
647 LPWSTR widename;
648 HRESULT hr;
649 int strlength;
651 TRACE("(%s, %p): relay\n", debugstr_a(file), info);
653 if( !file ) return D3DERR_INVALIDCALL;
655 strlength = MultiByteToWideChar(CP_ACP, 0, file, -1, NULL, 0);
656 widename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
657 MultiByteToWideChar(CP_ACP, 0, file, -1, widename, strlength);
659 hr = D3DXGetImageInfoFromFileW(widename, info);
660 HeapFree(GetProcessHeap(), 0, widename);
662 return hr;
665 HRESULT WINAPI D3DXGetImageInfoFromFileW(LPCWSTR file, D3DXIMAGE_INFO *info)
667 HRESULT hr;
668 DWORD size;
669 LPVOID buffer;
671 TRACE("(%s, %p): relay\n", debugstr_w(file), info);
673 if( !file ) return D3DERR_INVALIDCALL;
675 hr = map_view_of_file(file, &buffer, &size);
676 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
678 hr = D3DXGetImageInfoFromFileInMemory(buffer, size, info);
679 UnmapViewOfFile(buffer);
681 return hr;
684 /************************************************************
685 * D3DXGetImageInfoFromResource
687 * RETURNS
688 * Success: D3D_OK, if resource is a valid image file
689 * Failure: D3DXERR_INVALIDDATA, if resource is no valid image file or NULL or
690 * if we fail to load resource
693 HRESULT WINAPI D3DXGetImageInfoFromResourceA(HMODULE module, LPCSTR resource, D3DXIMAGE_INFO *info)
695 HRSRC resinfo;
697 TRACE("(%p, %s, %p)\n", module, debugstr_a(resource), info);
699 resinfo = FindResourceA(module, resource, (LPCSTR)RT_RCDATA);
700 if(resinfo) {
701 LPVOID buffer;
702 HRESULT hr;
703 DWORD size;
705 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
706 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
707 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
710 resinfo = FindResourceA(module, resource, (LPCSTR)RT_BITMAP);
711 if(resinfo) {
712 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
713 return E_NOTIMPL;
715 return D3DXERR_INVALIDDATA;
718 HRESULT WINAPI D3DXGetImageInfoFromResourceW(HMODULE module, LPCWSTR resource, D3DXIMAGE_INFO *info)
720 HRSRC resinfo;
722 TRACE("(%p, %s, %p)\n", module, debugstr_w(resource), info);
724 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_RCDATA);
725 if(resinfo) {
726 LPVOID buffer;
727 HRESULT hr;
728 DWORD size;
730 hr = load_resource_into_memory(module, resinfo, &buffer, &size);
731 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
732 return D3DXGetImageInfoFromFileInMemory(buffer, size, info);
735 resinfo = FindResourceW(module, resource, (LPCWSTR)RT_BITMAP);
736 if(resinfo) {
737 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
738 return E_NOTIMPL;
740 return D3DXERR_INVALIDDATA;
743 /************************************************************
744 * D3DXLoadSurfaceFromFileInMemory
746 * Loads data from a given buffer into a surface and fills a given
747 * D3DXIMAGE_INFO structure with info about the source data.
749 * PARAMS
750 * pDestSurface [I] pointer to the surface
751 * pDestPalette [I] palette to use
752 * pDestRect [I] to be filled area of the surface
753 * pSrcData [I] pointer to the source data
754 * SrcDataSize [I] size of the source data in bytes
755 * pSrcRect [I] area of the source data to load
756 * dwFilter [I] filter to apply on stretching
757 * Colorkey [I] colorkey
758 * pSrcInfo [O] pointer to a D3DXIMAGE_INFO structure
760 * RETURNS
761 * Success: D3D_OK
762 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcData or SrcDataSize are NULL
763 * D3DXERR_INVALIDDATA, if pSrcData is no valid image file
766 HRESULT WINAPI D3DXLoadSurfaceFromFileInMemory(LPDIRECT3DSURFACE9 pDestSurface,
767 CONST PALETTEENTRY *pDestPalette,
768 CONST RECT *pDestRect,
769 LPCVOID pSrcData,
770 UINT SrcDataSize,
771 CONST RECT *pSrcRect,
772 DWORD dwFilter,
773 D3DCOLOR Colorkey,
774 D3DXIMAGE_INFO *pSrcInfo)
776 D3DXIMAGE_INFO imginfo;
777 HRESULT hr;
779 IWICImagingFactory *factory;
780 IWICBitmapDecoder *decoder;
781 IWICBitmapFrameDecode *bitmapframe;
782 IWICStream *stream;
784 const PixelFormatDesc *formatdesc;
785 WICRect wicrect;
786 RECT rect;
788 TRACE("(%p, %p, %p, %p, %d, %p, %d, %x, %p)\n", pDestSurface, pDestPalette, pDestRect, pSrcData,
789 SrcDataSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
791 if (!pDestSurface || !pSrcData || !SrcDataSize)
792 return D3DERR_INVALIDCALL;
794 hr = D3DXGetImageInfoFromFileInMemory(pSrcData, SrcDataSize, &imginfo);
796 if (FAILED(hr))
797 return hr;
799 if (pSrcRect)
801 wicrect.X = pSrcRect->left;
802 wicrect.Y = pSrcRect->top;
803 wicrect.Width = pSrcRect->right - pSrcRect->left;
804 wicrect.Height = pSrcRect->bottom - pSrcRect->top;
806 else
808 wicrect.X = 0;
809 wicrect.Y = 0;
810 wicrect.Width = imginfo.Width;
811 wicrect.Height = imginfo.Height;
814 SetRect(&rect, 0, 0, wicrect.Width, wicrect.Height);
816 if (imginfo.ImageFileFormat == D3DXIFF_DDS)
818 hr = load_surface_from_dds(pDestSurface, pDestPalette, pDestRect, pSrcData, &rect,
819 dwFilter, Colorkey, &imginfo);
820 if (SUCCEEDED(hr) && pSrcInfo)
821 *pSrcInfo = imginfo;
822 return hr;
825 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
827 if (FAILED(CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, &IID_IWICImagingFactory, (void**)&factory)))
828 goto cleanup_err;
830 if (FAILED(IWICImagingFactory_CreateStream(factory, &stream)))
832 IWICImagingFactory_Release(factory);
833 goto cleanup_err;
836 IWICStream_InitializeFromMemory(stream, (BYTE*)pSrcData, SrcDataSize);
838 hr = IWICImagingFactory_CreateDecoderFromStream(factory, (IStream*)stream, NULL, 0, &decoder);
840 IStream_Release(stream);
841 IWICImagingFactory_Release(factory);
843 if (FAILED(hr))
844 goto cleanup_err;
846 hr = IWICBitmapDecoder_GetFrame(decoder, 0, &bitmapframe);
848 if (FAILED(hr))
849 goto cleanup_bmp;
851 formatdesc = get_format_info(imginfo.Format);
853 if (formatdesc->format == D3DFMT_UNKNOWN)
855 FIXME("Unsupported pixel format\n");
856 hr = D3DXERR_INVALIDDATA;
858 else
860 BYTE *buffer;
861 DWORD pitch;
863 pitch = formatdesc->bytes_per_pixel * wicrect.Width;
864 buffer = HeapAlloc(GetProcessHeap(), 0, pitch * wicrect.Height);
866 hr = IWICBitmapFrameDecode_CopyPixels(bitmapframe, &wicrect, pitch,
867 pitch * wicrect.Height, buffer);
869 if (SUCCEEDED(hr))
871 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
872 buffer, imginfo.Format, pitch,
873 NULL, &rect, dwFilter, Colorkey);
876 HeapFree(GetProcessHeap(), 0, buffer);
879 IWICBitmapFrameDecode_Release(bitmapframe);
881 cleanup_bmp:
882 IWICBitmapDecoder_Release(decoder);
884 cleanup_err:
885 CoUninitialize();
887 if (FAILED(hr))
888 return D3DXERR_INVALIDDATA;
890 if (pSrcInfo)
891 *pSrcInfo = imginfo;
893 return D3D_OK;
896 /************************************************************
897 * D3DXLoadSurfaceFromFile
899 HRESULT WINAPI D3DXLoadSurfaceFromFileA(LPDIRECT3DSURFACE9 pDestSurface,
900 CONST PALETTEENTRY *pDestPalette,
901 CONST RECT *pDestRect,
902 LPCSTR pSrcFile,
903 CONST RECT *pSrcRect,
904 DWORD dwFilter,
905 D3DCOLOR Colorkey,
906 D3DXIMAGE_INFO *pSrcInfo)
908 LPWSTR pWidename;
909 HRESULT hr;
910 int strlength;
912 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_a(pSrcFile),
913 pSrcRect, dwFilter, Colorkey, pSrcInfo);
915 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
917 strlength = MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, NULL, 0);
918 pWidename = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, strlength * sizeof(WCHAR));
919 MultiByteToWideChar(CP_ACP, 0, pSrcFile, -1, pWidename, strlength);
921 hr = D3DXLoadSurfaceFromFileW(pDestSurface, pDestPalette, pDestRect, pWidename, pSrcRect, dwFilter, Colorkey, pSrcInfo);
922 HeapFree(GetProcessHeap(), 0, pWidename);
924 return hr;
927 HRESULT WINAPI D3DXLoadSurfaceFromFileW(LPDIRECT3DSURFACE9 pDestSurface,
928 CONST PALETTEENTRY *pDestPalette,
929 CONST RECT *pDestRect,
930 LPCWSTR pSrcFile,
931 CONST RECT *pSrcRect,
932 DWORD Filter,
933 D3DCOLOR Colorkey,
934 D3DXIMAGE_INFO *pSrcInfo)
936 HRESULT hr;
937 DWORD dwSize;
938 LPVOID pBuffer;
940 TRACE("(%p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, debugstr_w(pSrcFile),
941 pSrcRect, Filter, Colorkey, pSrcInfo);
943 if( !pSrcFile || !pDestSurface ) return D3DERR_INVALIDCALL;
945 hr = map_view_of_file(pSrcFile, &pBuffer, &dwSize);
946 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
948 hr = D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, Filter, Colorkey, pSrcInfo);
949 UnmapViewOfFile(pBuffer);
951 return hr;
954 /************************************************************
955 * D3DXLoadSurfaceFromResource
957 HRESULT WINAPI D3DXLoadSurfaceFromResourceA(LPDIRECT3DSURFACE9 pDestSurface,
958 CONST PALETTEENTRY *pDestPalette,
959 CONST RECT *pDestRect,
960 HMODULE hSrcModule,
961 LPCSTR pResource,
962 CONST RECT *pSrcRect,
963 DWORD dwFilter,
964 D3DCOLOR Colorkey,
965 D3DXIMAGE_INFO *pSrcInfo)
967 HRSRC hResInfo;
969 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
970 debugstr_a(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
972 if( !pDestSurface ) return D3DERR_INVALIDCALL;
974 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_RCDATA);
975 if(hResInfo) {
976 LPVOID pBuffer;
977 HRESULT hr;
978 DWORD dwSize;
980 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
981 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
982 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
985 hResInfo = FindResourceA(hSrcModule, pResource, (LPCSTR)RT_BITMAP);
986 if(hResInfo) {
987 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
988 return E_NOTIMPL;
990 return D3DXERR_INVALIDDATA;
993 HRESULT WINAPI D3DXLoadSurfaceFromResourceW(LPDIRECT3DSURFACE9 pDestSurface,
994 CONST PALETTEENTRY *pDestPalette,
995 CONST RECT *pDestRect,
996 HMODULE hSrcModule,
997 LPCWSTR pResource,
998 CONST RECT *pSrcRect,
999 DWORD dwFilter,
1000 D3DCOLOR Colorkey,
1001 D3DXIMAGE_INFO *pSrcInfo)
1003 HRSRC hResInfo;
1005 TRACE("(%p, %p, %p, %p, %s, %p, %u, %#x, %p): relay\n", pDestSurface, pDestPalette, pDestRect, hSrcModule,
1006 debugstr_w(pResource), pSrcRect, dwFilter, Colorkey, pSrcInfo);
1008 if( !pDestSurface ) return D3DERR_INVALIDCALL;
1010 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_RCDATA);
1011 if(hResInfo) {
1012 LPVOID pBuffer;
1013 HRESULT hr;
1014 DWORD dwSize;
1016 hr = load_resource_into_memory(hSrcModule, hResInfo, &pBuffer, &dwSize);
1017 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
1018 return D3DXLoadSurfaceFromFileInMemory(pDestSurface, pDestPalette, pDestRect, pBuffer, dwSize, pSrcRect, dwFilter, Colorkey, pSrcInfo);
1021 hResInfo = FindResourceW(hSrcModule, pResource, (LPCWSTR)RT_BITMAP);
1022 if(hResInfo) {
1023 FIXME("Implement loading bitmaps from resource type RT_BITMAP\n");
1024 return E_NOTIMPL;
1026 return D3DXERR_INVALIDDATA;
1030 /************************************************************
1031 * helper functions for D3DXLoadSurfaceFromMemory
1033 struct argb_conversion_info
1035 CONST PixelFormatDesc *srcformat;
1036 CONST PixelFormatDesc *destformat;
1037 DWORD srcshift[4], destshift[4];
1038 DWORD srcmask[4], destmask[4];
1039 BOOL process_channel[4];
1040 DWORD channelmask;
1043 static void init_argb_conversion_info(CONST PixelFormatDesc *srcformat, CONST PixelFormatDesc *destformat, struct argb_conversion_info *info)
1045 UINT i;
1046 ZeroMemory(info->process_channel, 4 * sizeof(BOOL));
1047 info->channelmask = 0;
1049 info->srcformat = srcformat;
1050 info->destformat = destformat;
1052 for(i = 0;i < 4;i++) {
1053 /* srcshift is used to extract the _relevant_ components */
1054 info->srcshift[i] = srcformat->shift[i] + max( srcformat->bits[i] - destformat->bits[i], 0);
1056 /* destshift is used to move the components to the correct position */
1057 info->destshift[i] = destformat->shift[i] + max(destformat->bits[i] - srcformat->bits[i], 0);
1059 info->srcmask[i] = ((1 << srcformat->bits[i]) - 1) << srcformat->shift[i];
1060 info->destmask[i] = ((1 << destformat->bits[i]) - 1) << destformat->shift[i];
1062 /* channelmask specifies bits which aren't used in the source format but in the destination one */
1063 if(destformat->bits[i]) {
1064 if(srcformat->bits[i]) info->process_channel[i] = TRUE;
1065 else info->channelmask |= info->destmask[i];
1070 static DWORD dword_from_bytes(CONST BYTE *src, UINT bytes_per_pixel)
1072 DWORD ret = 0;
1073 static BOOL fixme_once;
1075 if(bytes_per_pixel > sizeof(DWORD)) {
1076 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1077 bytes_per_pixel = sizeof(DWORD);
1080 memcpy(&ret, src, bytes_per_pixel);
1081 return ret;
1084 static void dword_to_bytes(BYTE *dst, DWORD dword, UINT bytes_per_pixel)
1086 static BOOL fixme_once;
1088 if(bytes_per_pixel > sizeof(DWORD)) {
1089 if(!fixme_once++) FIXME("Unsupported image: %u bytes per pixel\n", bytes_per_pixel);
1090 ZeroMemory(dst, bytes_per_pixel);
1091 bytes_per_pixel = sizeof(DWORD);
1094 memcpy(dst, &dword, bytes_per_pixel);
1097 /************************************************************
1098 * get_relevant_argb_components
1100 * Extracts the relevant components from the source color and
1101 * drops the less significant bits if they aren't used by the destination format.
1103 static void get_relevant_argb_components(CONST struct argb_conversion_info *info, CONST DWORD col, DWORD *out)
1105 UINT i = 0;
1106 for(;i < 4;i++)
1107 if(info->process_channel[i])
1108 out[i] = (col & info->srcmask[i]) >> info->srcshift[i];
1111 /************************************************************
1112 * make_argb_color
1114 * Recombines the output of get_relevant_argb_components and converts
1115 * it to the destination format.
1117 static DWORD make_argb_color(CONST struct argb_conversion_info *info, CONST DWORD *in)
1119 UINT i;
1120 DWORD val = 0;
1122 for(i = 0;i < 4;i++) {
1123 if(info->process_channel[i]) {
1124 /* necessary to make sure that e.g. an X4R4G4B4 white maps to an R8G8B8 white instead of 0xf0f0f0 */
1125 signed int shift;
1126 for(shift = info->destshift[i]; shift > info->destformat->shift[i]; shift -= info->srcformat->bits[i]) val |= in[i] << shift;
1127 val |= (in[i] >> (info->destformat->shift[i] - shift)) << info->destformat->shift[i];
1130 val |= info->channelmask; /* new channels are set to their maximal value */
1131 return val;
1134 static void format_to_vec4(const PixelFormatDesc *format, const DWORD *src, struct vec4 *dst)
1136 DWORD mask;
1138 if (format->bits[1])
1140 mask = (1 << format->bits[1]) - 1;
1141 dst->x = (float)((*src >> format->shift[1]) & mask) / mask;
1143 else
1144 dst->x = 1.0f;
1146 if (format->bits[2])
1148 mask = (1 << format->bits[2]) - 1;
1149 dst->y = (float)((*src >> format->shift[2]) & mask) / mask;
1151 else
1152 dst->y = 1.0f;
1154 if (format->bits[3])
1156 mask = (1 << format->bits[3]) - 1;
1157 dst->z = (float)((*src >> format->shift[3]) & mask) / mask;
1159 else
1160 dst->z = 1.0f;
1162 if (format->bits[0])
1164 mask = (1 << format->bits[0]) - 1;
1165 dst->w = (float)((*src >> format->shift[0]) & mask) / mask;
1167 else
1168 dst->w = 1.0f;
1171 static void format_from_vec4(const PixelFormatDesc *format, const struct vec4 *src, DWORD *dst)
1173 *dst = 0;
1175 if (format->bits[1])
1176 *dst |= (DWORD)(src->x * ((1 << format->bits[1]) - 1) + 0.5f) << format->shift[1];
1177 if (format->bits[2])
1178 *dst |= (DWORD)(src->y * ((1 << format->bits[2]) - 1) + 0.5f) << format->shift[2];
1179 if (format->bits[3])
1180 *dst |= (DWORD)(src->z * ((1 << format->bits[3]) - 1) + 0.5f) << format->shift[3];
1181 if (format->bits[0])
1182 *dst |= (DWORD)(src->w * ((1 << format->bits[0]) - 1) + 0.5f) << format->shift[0];
1185 /************************************************************
1186 * copy_simple_data
1188 * Copies the source buffer to the destination buffer, performing
1189 * any necessary format conversion and color keying.
1190 * Pixels outsize the source rect are blacked out.
1191 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1193 static void copy_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
1194 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
1196 struct argb_conversion_info conv_info, ck_conv_info;
1197 const PixelFormatDesc *ck_format = NULL;
1198 DWORD channels[4], pixel;
1199 UINT minwidth, minheight;
1200 UINT x, y;
1202 ZeroMemory(channels, sizeof(channels));
1203 init_argb_conversion_info(srcformat, destformat, &conv_info);
1205 minwidth = (src_size.cx < dst_size.cx) ? src_size.cx : dst_size.cx;
1206 minheight = (src_size.cy < dst_size.cy) ? src_size.cy : dst_size.cy;
1208 if (colorkey)
1210 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1211 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1212 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1215 for(y = 0;y < minheight;y++) {
1216 const BYTE *srcptr = src + y * srcpitch;
1217 BYTE *destptr = dest + y * destpitch;
1218 DWORD val;
1220 for(x = 0;x < minwidth;x++) {
1221 /* extract source color components */
1222 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1224 if (!srcformat->to_rgba && !destformat->from_rgba)
1226 get_relevant_argb_components(&conv_info, pixel, channels);
1227 val = make_argb_color(&conv_info, channels);
1229 if (colorkey)
1231 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1232 pixel = make_argb_color(&ck_conv_info, channels);
1233 if (pixel == colorkey)
1234 val &= ~conv_info.destmask[0];
1237 else
1239 struct vec4 color, tmp;
1241 format_to_vec4(srcformat, &pixel, &color);
1242 if (srcformat->to_rgba)
1243 srcformat->to_rgba(&color, &tmp);
1244 else
1245 tmp = color;
1247 if (ck_format)
1249 format_from_vec4(ck_format, &tmp, &pixel);
1250 if (pixel == colorkey)
1251 tmp.w = 0.0f;
1254 if (destformat->from_rgba)
1255 destformat->from_rgba(&tmp, &color);
1256 else
1257 color = tmp;
1259 format_from_vec4(destformat, &color, &val);
1262 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1263 srcptr += srcformat->bytes_per_pixel;
1264 destptr += destformat->bytes_per_pixel;
1267 if (src_size.cx < dst_size.cx) /* black out remaining pixels */
1268 memset(destptr, 0, destformat->bytes_per_pixel * (dst_size.cx - src_size.cx));
1270 if (src_size.cy < dst_size.cy) /* black out remaining pixels */
1271 memset(dest + src_size.cy * destpitch, 0, destpitch * (dst_size.cy - src_size.cy));
1274 /************************************************************
1275 * point_filter_simple_data
1277 * Copies the source buffer to the destination buffer, performing
1278 * any necessary format conversion, color keying and stretching
1279 * using a point filter.
1280 * Works only for ARGB formats with 1 - 4 bytes per pixel.
1282 static void point_filter_simple_data(const BYTE *src, UINT srcpitch, SIZE src_size, const PixelFormatDesc *srcformat,
1283 BYTE *dest, UINT destpitch, SIZE dst_size, const PixelFormatDesc *destformat, D3DCOLOR colorkey)
1285 struct argb_conversion_info conv_info, ck_conv_info;
1286 const PixelFormatDesc *ck_format = NULL;
1287 DWORD channels[4], pixel;
1289 UINT x, y;
1291 ZeroMemory(channels, sizeof(channels));
1292 init_argb_conversion_info(srcformat, destformat, &conv_info);
1294 if (colorkey)
1296 /* Color keys are always represented in D3DFMT_A8R8G8B8 format. */
1297 ck_format = get_format_info(D3DFMT_A8R8G8B8);
1298 init_argb_conversion_info(srcformat, ck_format, &ck_conv_info);
1301 for (y = 0; y < dst_size.cy; ++y)
1303 BYTE *destptr = dest + y * destpitch;
1304 const BYTE *bufptr = src + srcpitch * (y * src_size.cy / dst_size.cy);
1306 for (x = 0; x < dst_size.cx; ++x)
1308 const BYTE *srcptr = bufptr + (x * src_size.cx / dst_size.cx) * srcformat->bytes_per_pixel;
1309 DWORD val;
1311 /* extract source color components */
1312 pixel = dword_from_bytes(srcptr, srcformat->bytes_per_pixel);
1314 if (!srcformat->to_rgba && !destformat->from_rgba)
1316 get_relevant_argb_components(&conv_info, pixel, channels);
1317 val = make_argb_color(&conv_info, channels);
1319 if (colorkey)
1321 get_relevant_argb_components(&ck_conv_info, pixel, channels);
1322 pixel = make_argb_color(&ck_conv_info, channels);
1323 if (pixel == colorkey)
1324 val &= ~conv_info.destmask[0];
1327 else
1329 struct vec4 color, tmp;
1331 format_to_vec4(srcformat, &pixel, &color);
1332 if (srcformat->to_rgba)
1333 srcformat->to_rgba(&color, &tmp);
1334 else
1335 tmp = color;
1337 if (ck_format)
1339 format_from_vec4(ck_format, &tmp, &pixel);
1340 if (pixel == colorkey)
1341 tmp.w = 0.0f;
1344 if (destformat->from_rgba)
1345 destformat->from_rgba(&tmp, &color);
1346 else
1347 color = tmp;
1349 format_from_vec4(destformat, &color, &val);
1352 dword_to_bytes(destptr, val, destformat->bytes_per_pixel);
1353 destptr += destformat->bytes_per_pixel;
1358 /************************************************************
1359 * D3DXLoadSurfaceFromMemory
1361 * Loads data from a given memory chunk into a surface,
1362 * applying any of the specified filters.
1364 * PARAMS
1365 * pDestSurface [I] pointer to the surface
1366 * pDestPalette [I] palette to use
1367 * pDestRect [I] to be filled area of the surface
1368 * pSrcMemory [I] pointer to the source data
1369 * SrcFormat [I] format of the source pixel data
1370 * SrcPitch [I] number of bytes in a row
1371 * pSrcPalette [I] palette used in the source image
1372 * pSrcRect [I] area of the source data to load
1373 * dwFilter [I] filter to apply on stretching
1374 * Colorkey [I] colorkey
1376 * RETURNS
1377 * Success: D3D_OK, if we successfully load the pixel data into our surface or
1378 * if pSrcMemory is NULL but the other parameters are valid
1379 * Failure: D3DERR_INVALIDCALL, if pDestSurface, SrcPitch or pSrcRect are NULL or
1380 * if SrcFormat is an invalid format (other than D3DFMT_UNKNOWN) or
1381 * if DestRect is invalid
1382 * D3DXERR_INVALIDDATA, if we fail to lock pDestSurface
1383 * E_FAIL, if SrcFormat is D3DFMT_UNKNOWN or the dimensions of pSrcRect are invalid
1385 * NOTES
1386 * pSrcRect specifies the dimensions of the source data;
1387 * negative values for pSrcRect are allowed as we're only looking at the width and height anyway.
1390 HRESULT WINAPI D3DXLoadSurfaceFromMemory(IDirect3DSurface9 *dst_surface,
1391 const PALETTEENTRY *dst_palette, const RECT *dst_rect, const void *src_memory,
1392 D3DFORMAT src_format, UINT src_pitch, const PALETTEENTRY *src_palette, const RECT *src_rect,
1393 DWORD filter, D3DCOLOR color_key)
1395 CONST PixelFormatDesc *srcformatdesc, *destformatdesc;
1396 D3DSURFACE_DESC surfdesc;
1397 D3DLOCKED_RECT lockrect;
1398 SIZE src_size, dst_size;
1399 HRESULT hr;
1401 TRACE("(%p, %p, %s, %p, %#x, %u, %p, %s %#x, 0x%08x)\n",
1402 dst_surface, dst_palette, wine_dbgstr_rect(dst_rect), src_memory, src_format,
1403 src_pitch, src_palette, wine_dbgstr_rect(src_rect), filter, color_key);
1405 if (!dst_surface || !src_memory || !src_rect)
1406 return D3DERR_INVALIDCALL;
1407 if (src_format == D3DFMT_UNKNOWN
1408 || src_rect->left >= src_rect->right
1409 || src_rect->top >= src_rect->bottom)
1410 return E_FAIL;
1412 if (filter == D3DX_DEFAULT)
1413 filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
1415 IDirect3DSurface9_GetDesc(dst_surface, &surfdesc);
1417 src_size.cx = src_rect->right - src_rect->left;
1418 src_size.cy = src_rect->bottom - src_rect->top;
1419 if (!dst_rect)
1421 dst_size.cx = surfdesc.Width;
1422 dst_size.cy = surfdesc.Height;
1424 else
1426 if (dst_rect->left > dst_rect->right || dst_rect->right > surfdesc.Width)
1427 return D3DERR_INVALIDCALL;
1428 if (dst_rect->top > dst_rect->bottom || dst_rect->bottom > surfdesc.Height)
1429 return D3DERR_INVALIDCALL;
1430 if (dst_rect->left < 0 || dst_rect->top < 0)
1431 return D3DERR_INVALIDCALL;
1432 dst_size.cx = dst_rect->right - dst_rect->left;
1433 dst_size.cy = dst_rect->bottom - dst_rect->top;
1434 if (!dst_size.cx || !dst_size.cy)
1435 return D3D_OK;
1438 srcformatdesc = get_format_info(src_format);
1439 if (srcformatdesc->type == FORMAT_UNKNOWN)
1440 return E_NOTIMPL;
1442 destformatdesc = get_format_info(surfdesc.Format);
1443 if (destformatdesc->type == FORMAT_UNKNOWN)
1444 return E_NOTIMPL;
1446 if (src_format == surfdesc.Format
1447 && dst_size.cx == src_size.cx
1448 && dst_size.cy == src_size.cy) /* Simple copy. */
1450 UINT row_block_count = ((src_size.cx + srcformatdesc->block_width - 1) / srcformatdesc->block_width);
1451 UINT row_count = (src_size.cy + srcformatdesc->block_height - 1) / srcformatdesc->block_height;
1452 const BYTE *src_addr;
1453 BYTE *dst_addr;
1454 UINT row;
1456 if (src_rect->left & (srcformatdesc->block_width - 1)
1457 || src_rect->top & (srcformatdesc->block_height - 1)
1458 || (src_rect->right & (srcformatdesc->block_width - 1)
1459 && src_size.cx != surfdesc.Width)
1460 || (src_rect->bottom & (srcformatdesc->block_height - 1)
1461 && src_size.cy != surfdesc.Height))
1463 WARN("Source rect %s is misaligned.\n", wine_dbgstr_rect(src_rect));
1464 return D3DXERR_INVALIDDATA;
1467 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1468 return D3DXERR_INVALIDDATA;
1470 src_addr = src_memory;
1471 src_addr += (src_rect->top / srcformatdesc->block_height) * src_pitch;
1472 src_addr += (src_rect->left / srcformatdesc->block_width) * srcformatdesc->block_byte_count;
1473 dst_addr = lockrect.pBits;
1475 for (row = 0; row < row_count; ++row)
1477 memcpy(dst_addr, src_addr, row_block_count * srcformatdesc->block_byte_count);
1478 src_addr += src_pitch;
1479 dst_addr += lockrect.Pitch;
1482 IDirect3DSurface9_UnlockRect(dst_surface);
1484 else /* Stretching or format conversion. */
1486 if (srcformatdesc->bytes_per_pixel > 4)
1487 return E_NOTIMPL;
1488 if (destformatdesc->bytes_per_pixel > 4)
1489 return E_NOTIMPL;
1490 if (srcformatdesc->block_height != 1 || srcformatdesc->block_width != 1)
1491 return E_NOTIMPL;
1492 if (destformatdesc->block_height != 1 || destformatdesc->block_width != 1)
1493 return E_NOTIMPL;
1495 if (FAILED(hr = IDirect3DSurface9_LockRect(dst_surface, &lockrect, dst_rect, 0)))
1496 return D3DXERR_INVALIDDATA;
1498 if ((filter & 0xf) == D3DX_FILTER_NONE)
1500 copy_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1501 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1503 else /* if ((filter & 0xf) == D3DX_FILTER_POINT) */
1505 if ((filter & 0xf) != D3DX_FILTER_POINT)
1506 FIXME("Unhandled filter %#x.\n", filter);
1508 /* Always apply a point filter until D3DX_FILTER_LINEAR,
1509 * D3DX_FILTER_TRIANGLE and D3DX_FILTER_BOX are implemented. */
1510 point_filter_simple_data(src_memory, src_pitch, src_size, srcformatdesc,
1511 lockrect.pBits, lockrect.Pitch, dst_size, destformatdesc, color_key);
1514 IDirect3DSurface9_UnlockRect(dst_surface);
1517 return D3D_OK;
1520 /************************************************************
1521 * D3DXLoadSurfaceFromSurface
1523 * Copies the contents from one surface to another, performing any required
1524 * format conversion, resizing or filtering.
1526 * PARAMS
1527 * pDestSurface [I] pointer to the destination surface
1528 * pDestPalette [I] palette to use
1529 * pDestRect [I] to be filled area of the surface
1530 * pSrcSurface [I] pointer to the source surface
1531 * pSrcPalette [I] palette used for the source surface
1532 * pSrcRect [I] area of the source data to load
1533 * dwFilter [I] filter to apply on resizing
1534 * Colorkey [I] any ARGB value or 0 to disable color-keying
1536 * RETURNS
1537 * Success: D3D_OK
1538 * Failure: D3DERR_INVALIDCALL, if pDestSurface or pSrcSurface are NULL
1539 * D3DXERR_INVALIDDATA, if one of the surfaces is not lockable
1542 HRESULT WINAPI D3DXLoadSurfaceFromSurface(LPDIRECT3DSURFACE9 pDestSurface,
1543 CONST PALETTEENTRY *pDestPalette,
1544 CONST RECT *pDestRect,
1545 LPDIRECT3DSURFACE9 pSrcSurface,
1546 CONST PALETTEENTRY *pSrcPalette,
1547 CONST RECT *pSrcRect,
1548 DWORD dwFilter,
1549 D3DCOLOR Colorkey)
1551 RECT rect;
1552 D3DLOCKED_RECT lock;
1553 D3DSURFACE_DESC SrcDesc;
1554 HRESULT hr;
1556 TRACE("(%p, %p, %p, %p, %p, %p, %u, %#x): relay\n", pDestSurface, pDestPalette, pDestRect,
1557 pSrcSurface, pSrcPalette, pSrcRect, dwFilter, Colorkey);
1559 if( !pDestSurface || !pSrcSurface ) return D3DERR_INVALIDCALL;
1561 IDirect3DSurface9_GetDesc(pSrcSurface, &SrcDesc);
1563 if( !pSrcRect ) SetRect(&rect, 0, 0, SrcDesc.Width, SrcDesc.Height);
1564 else rect = *pSrcRect;
1566 hr = IDirect3DSurface9_LockRect(pSrcSurface, &lock, NULL, D3DLOCK_READONLY);
1567 if(FAILED(hr)) return D3DXERR_INVALIDDATA;
1569 hr = D3DXLoadSurfaceFromMemory(pDestSurface, pDestPalette, pDestRect,
1570 lock.pBits, SrcDesc.Format, lock.Pitch,
1571 pSrcPalette, &rect, dwFilter, Colorkey);
1573 IDirect3DSurface9_UnlockRect(pSrcSurface);
1574 return hr;
1578 HRESULT WINAPI D3DXSaveSurfaceToFileA(const char *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1579 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1581 int len;
1582 WCHAR *filename;
1583 HRESULT hr;
1585 TRACE("(%s, %#x, %p, %p, %s): relay\n",
1586 wine_dbgstr_a(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1588 if (!dst_filename) return D3DERR_INVALIDCALL;
1590 len = MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, NULL, 0);
1591 filename = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1592 if (!filename) return E_OUTOFMEMORY;
1593 MultiByteToWideChar(CP_ACP, 0, dst_filename, -1, filename, len);
1595 hr = D3DXSaveSurfaceToFileW(filename, file_format, src_surface, src_palette, src_rect);
1597 HeapFree(GetProcessHeap(), 0, filename);
1598 return hr;
1601 HRESULT WINAPI D3DXSaveSurfaceToFileW(const WCHAR *dst_filename, D3DXIMAGE_FILEFORMAT file_format,
1602 IDirect3DSurface9 *src_surface, const PALETTEENTRY *src_palette, const RECT *src_rect)
1604 IWICImagingFactory *factory;
1605 IWICBitmapEncoder *encoder = NULL;
1606 IWICBitmapFrameEncode *frame = NULL;
1607 IPropertyBag2 *encoder_options = NULL;
1608 IWICStream *stream = NULL;
1609 HRESULT hr;
1610 HRESULT initresult;
1611 const CLSID *encoder_clsid;
1612 const GUID *pixel_format_guid;
1613 WICPixelFormatGUID wic_pixel_format;
1614 D3DFORMAT d3d_pixel_format;
1615 D3DSURFACE_DESC src_surface_desc;
1616 D3DLOCKED_RECT locked_rect;
1617 int width, height;
1619 TRACE("(%s, %#x, %p, %p, %s)\n",
1620 wine_dbgstr_w(dst_filename), file_format, src_surface, src_palette, wine_dbgstr_rect(src_rect));
1622 if (!dst_filename || !src_surface) return D3DERR_INVALIDCALL;
1624 if (src_palette)
1626 FIXME("Saving surfaces with palettized pixel formats not implemented yet\n");
1627 return D3DERR_INVALIDCALL;
1630 switch (file_format)
1632 case D3DXIFF_BMP:
1633 encoder_clsid = &CLSID_WICBmpEncoder;
1634 break;
1635 case D3DXIFF_PNG:
1636 encoder_clsid = &CLSID_WICPngEncoder;
1637 break;
1638 case D3DXIFF_JPG:
1639 encoder_clsid = &CLSID_WICJpegEncoder;
1640 break;
1641 case D3DXIFF_DDS:
1642 case D3DXIFF_DIB:
1643 case D3DXIFF_HDR:
1644 case D3DXIFF_PFM:
1645 case D3DXIFF_TGA:
1646 case D3DXIFF_PPM:
1647 FIXME("File format %#x is not supported yet\n", file_format);
1648 return E_NOTIMPL;
1649 default:
1650 return D3DERR_INVALIDCALL;
1653 IDirect3DSurface9_GetDesc(src_surface, &src_surface_desc);
1654 if (src_rect)
1656 if (src_rect->left == src_rect->right || src_rect->top == src_rect->bottom)
1657 return D3D_OK;
1658 if (src_rect->left < 0 || src_rect->top < 0)
1659 return D3DERR_INVALIDCALL;
1660 if (src_rect->left > src_rect->right || src_rect->top > src_rect->bottom)
1661 return D3DERR_INVALIDCALL;
1662 if (src_rect->right > src_surface_desc.Width || src_rect->bottom > src_surface_desc.Height)
1663 return D3DERR_INVALIDCALL;
1665 width = src_rect->right - src_rect->left;
1666 height = src_rect->bottom - src_rect->top;
1668 else
1670 width = src_surface_desc.Width;
1671 height = src_surface_desc.Height;
1674 initresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1676 hr = CoCreateInstance(&CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER,
1677 &IID_IWICImagingFactory, (void **)&factory);
1678 if (FAILED(hr)) goto cleanup_err;
1680 hr = IWICImagingFactory_CreateStream(factory, &stream);
1681 IWICImagingFactory_Release(factory);
1682 if (FAILED(hr)) goto cleanup_err;
1684 hr = IWICStream_InitializeFromFilename(stream, dst_filename, GENERIC_WRITE);
1685 if (FAILED(hr)) goto cleanup_err;
1687 hr = CoCreateInstance(encoder_clsid, NULL, CLSCTX_INPROC_SERVER,
1688 &IID_IWICBitmapEncoder, (void **)&encoder);
1689 if (FAILED(hr)) goto cleanup_err;
1691 hr = IWICBitmapEncoder_Initialize(encoder, (IStream *)stream, WICBitmapEncoderNoCache);
1692 IStream_Release((IStream *)stream);
1693 stream = NULL;
1694 if (FAILED(hr)) goto cleanup_err;
1696 hr = IWICBitmapEncoder_CreateNewFrame(encoder, &frame, &encoder_options);
1697 if (FAILED(hr)) goto cleanup_err;
1699 hr = IWICBitmapFrameEncode_Initialize(frame, encoder_options);
1700 if (FAILED(hr)) goto cleanup_err;
1702 hr = IWICBitmapFrameEncode_SetSize(frame, width, height);
1703 if (FAILED(hr)) goto cleanup_err;
1705 pixel_format_guid = d3dformat_to_wic_guid(src_surface_desc.Format);
1706 if (!pixel_format_guid)
1708 FIXME("Pixel format %#x is not supported yet\n", src_surface_desc.Format);
1709 hr = E_NOTIMPL;
1710 goto cleanup;
1713 memcpy(&wic_pixel_format, pixel_format_guid, sizeof(GUID));
1714 hr = IWICBitmapFrameEncode_SetPixelFormat(frame, &wic_pixel_format);
1715 d3d_pixel_format = wic_guid_to_d3dformat(&wic_pixel_format);
1716 if (SUCCEEDED(hr) && d3d_pixel_format != D3DFMT_UNKNOWN)
1718 TRACE("Using pixel format %s %#x\n", debugstr_guid(&wic_pixel_format), d3d_pixel_format);
1720 if (src_surface_desc.Format == d3d_pixel_format) /* Simple copy */
1722 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1723 if (SUCCEEDED(hr))
1725 IWICBitmapFrameEncode_WritePixels(frame, height,
1726 locked_rect.Pitch, height * locked_rect.Pitch, locked_rect.pBits);
1727 IDirect3DSurface9_UnlockRect(src_surface);
1730 else /* Pixel format conversion */
1732 const PixelFormatDesc *src_format_desc, *dst_format_desc;
1733 SIZE size;
1734 DWORD dst_pitch;
1735 void *dst_data;
1737 src_format_desc = get_format_info(src_surface_desc.Format);
1738 dst_format_desc = get_format_info(d3d_pixel_format);
1739 if (src_format_desc->format == D3DFMT_UNKNOWN || dst_format_desc->format == D3DFMT_UNKNOWN)
1741 FIXME("Unsupported pixel format conversion %#x -> %#x\n",
1742 src_surface_desc.Format, d3d_pixel_format);
1743 hr = E_NOTIMPL;
1744 goto cleanup;
1747 if (src_format_desc->bytes_per_pixel > 4
1748 || dst_format_desc->bytes_per_pixel > 4
1749 || src_format_desc->block_height != 1 || src_format_desc->block_width != 1
1750 || dst_format_desc->block_height != 1 || dst_format_desc->block_width != 1)
1752 hr = E_NOTIMPL;
1753 goto cleanup;
1756 size.cx = width;
1757 size.cy = height;
1758 dst_pitch = width * dst_format_desc->bytes_per_pixel;
1759 dst_data = HeapAlloc(GetProcessHeap(), 0, dst_pitch * height);
1760 if (!dst_data)
1762 hr = E_OUTOFMEMORY;
1763 goto cleanup;
1766 hr = IDirect3DSurface9_LockRect(src_surface, &locked_rect, src_rect, D3DLOCK_READONLY);
1767 if (SUCCEEDED(hr))
1769 copy_simple_data(locked_rect.pBits, locked_rect.Pitch, size, src_format_desc,
1770 dst_data, dst_pitch, size, dst_format_desc, 0);
1771 IDirect3DSurface9_UnlockRect(src_surface);
1774 IWICBitmapFrameEncode_WritePixels(frame, height, dst_pitch, dst_pitch * height, dst_data);
1775 HeapFree(GetProcessHeap(), 0, dst_data);
1778 hr = IWICBitmapFrameEncode_Commit(frame);
1779 if (SUCCEEDED(hr)) hr = IWICBitmapEncoder_Commit(encoder);
1781 else WARN("Unsupported pixel format %#x\n", src_surface_desc.Format);
1783 cleanup_err:
1784 if (FAILED(hr)) hr = D3DERR_INVALIDCALL;
1786 cleanup:
1787 if (stream) IStream_Release((IStream *)stream);
1789 if (frame) IWICBitmapFrameEncode_Release(frame);
1790 if (encoder_options) IPropertyBag2_Release(encoder_options);
1792 if (encoder) IWICBitmapEncoder_Release(encoder);
1794 if (SUCCEEDED(initresult)) CoUninitialize();
1796 return hr;