xref: /reactos/dll/directx/wine/d3dx9_36/volume.c (revision 8540ab04)
1 /*
2  * Copyright 2010 Christian Costa
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #include "config.h"
20 #include "wine/port.h"
21 
22 #include "d3dx9_private.h"
23 
24 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
25 
26 HRESULT WINAPI D3DXLoadVolumeFromFileA(IDirect3DVolume9 *dst_volume,
27                                        const PALETTEENTRY *dst_palette,
28                                        const D3DBOX *dst_box,
29                                        const char *filename,
30                                        const D3DBOX *src_box,
31                                        DWORD filter,
32                                        D3DCOLOR color_key,
33                                        D3DXIMAGE_INFO *info)
34 {
35     HRESULT hr;
36     int length;
37     WCHAR *filenameW;
38 
39     TRACE("(%p, %p, %p, %s, %p, %#x, %#x, %p)\n",
40             dst_volume, dst_palette, dst_box, debugstr_a(filename), src_box,
41             filter, color_key, info);
42 
43     if (!dst_volume || !filename) return D3DERR_INVALIDCALL;
44 
45     length = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
46     filenameW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(*filenameW));
47     if (!filenameW) return E_OUTOFMEMORY;
48 
49     hr = D3DXLoadVolumeFromFileW(dst_volume, dst_palette, dst_box, filenameW,
50             src_box, filter, color_key, info);
51     HeapFree(GetProcessHeap(), 0, filenameW);
52 
53     return hr;
54 }
55 
56 HRESULT WINAPI D3DXLoadVolumeFromFileW(IDirect3DVolume9 *dst_volume,
57                                        const PALETTEENTRY *dst_palette,
58                                        const D3DBOX *dst_box,
59                                        const WCHAR *filename,
60                                        const D3DBOX *src_box,
61                                        DWORD filter,
62                                        D3DCOLOR color_key,
63                                        D3DXIMAGE_INFO *info)
64 {
65     HRESULT hr;
66     void *data;
67     UINT data_size;
68 
69     TRACE("(%p, %p, %p, %s, %p, %#x, %#x, %p)\n",
70             dst_volume, dst_palette, dst_box, debugstr_w(filename), src_box,
71             filter, color_key, info);
72 
73     if (!dst_volume || !filename) return D3DERR_INVALIDCALL;
74 
75     if (FAILED(map_view_of_file(filename, &data, &data_size)))
76         return D3DXERR_INVALIDDATA;
77 
78     hr = D3DXLoadVolumeFromFileInMemory(dst_volume, dst_palette, dst_box,
79             data, data_size, src_box, filter, color_key, info);
80     UnmapViewOfFile(data);
81 
82     return hr;
83 }
84 
85 HRESULT WINAPI D3DXLoadVolumeFromMemory(IDirect3DVolume9 *dst_volume,
86                                         const PALETTEENTRY *dst_palette,
87                                         const D3DBOX *dst_box,
88                                         const void *src_memory,
89                                         D3DFORMAT src_format,
90                                         UINT src_row_pitch,
91                                         UINT src_slice_pitch,
92                                         const PALETTEENTRY *src_palette,
93                                         const D3DBOX *src_box,
94                                         DWORD filter,
95                                         D3DCOLOR color_key)
96 {
97     HRESULT hr;
98     D3DVOLUME_DESC desc;
99     D3DLOCKED_BOX locked_box;
100     struct volume dst_size, src_size;
101     const struct pixel_format_desc *src_format_desc, *dst_format_desc;
102 
103     TRACE("(%p, %p, %p, %p, %#x, %u, %u, %p, %p, %x, %x)\n", dst_volume, dst_palette, dst_box,
104             src_memory, src_format, src_row_pitch, src_slice_pitch, src_palette, src_box,
105             filter, color_key);
106 
107     if (!dst_volume || !src_memory || !src_box) return D3DERR_INVALIDCALL;
108 
109     if (src_format == D3DFMT_UNKNOWN
110             || src_box->Left >= src_box->Right
111             || src_box->Top >= src_box->Bottom
112             || src_box->Front >= src_box->Back)
113         return E_FAIL;
114 
115     if (filter == D3DX_DEFAULT)
116         filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
117 
118     IDirect3DVolume9_GetDesc(dst_volume, &desc);
119 
120     src_size.width = src_box->Right - src_box->Left;
121     src_size.height = src_box->Bottom - src_box->Top;
122     src_size.depth = src_box->Back - src_box->Front;
123 
124     if (!dst_box)
125     {
126         dst_size.width = desc.Width;
127         dst_size.height = desc.Height;
128         dst_size.depth = desc.Depth;
129     }
130     else
131     {
132         if (dst_box->Left >= dst_box->Right || dst_box->Right > desc.Width)
133             return D3DERR_INVALIDCALL;
134         if (dst_box->Top >= dst_box->Bottom || dst_box->Bottom > desc.Height)
135             return D3DERR_INVALIDCALL;
136         if (dst_box->Front >= dst_box->Back || dst_box->Back > desc.Depth)
137             return D3DERR_INVALIDCALL;
138 
139         dst_size.width = dst_box->Right - dst_box->Left;
140         dst_size.height = dst_box->Bottom - dst_box->Top;
141         dst_size.depth = dst_box->Back - dst_box->Front;
142     }
143 
144     src_format_desc = get_format_info(src_format);
145     if (src_format_desc->type == FORMAT_UNKNOWN)
146         return E_NOTIMPL;
147 
148     dst_format_desc = get_format_info(desc.Format);
149     if (dst_format_desc->type == FORMAT_UNKNOWN)
150         return E_NOTIMPL;
151 
152     if (desc.Format == src_format
153             && dst_size.width == src_size.width
154             && dst_size.height == src_size.height
155             && dst_size.depth == src_size.depth
156             && color_key == 0)
157     {
158         const BYTE *src_addr;
159 
160         if (src_box->Left & (src_format_desc->block_width - 1)
161                 || src_box->Top & (src_format_desc->block_height - 1)
162                 || (src_box->Right & (src_format_desc->block_width - 1)
163                     && src_size.width != desc.Width)
164                 || (src_box->Bottom & (src_format_desc->block_height - 1)
165                     && src_size.height != desc.Height))
166         {
167             FIXME("Source box (%u, %u, %u, %u) is misaligned\n",
168                     src_box->Left, src_box->Top, src_box->Right, src_box->Bottom);
169             return E_NOTIMPL;
170         }
171 
172         src_addr = src_memory;
173         src_addr += src_box->Front * src_slice_pitch;
174         src_addr += (src_box->Top / src_format_desc->block_height) * src_row_pitch;
175         src_addr += (src_box->Left / src_format_desc->block_width) * src_format_desc->block_byte_count;
176 
177         hr = IDirect3DVolume9_LockBox(dst_volume, &locked_box, dst_box, 0);
178         if (FAILED(hr)) return hr;
179 
180         copy_pixels(src_addr, src_row_pitch, src_slice_pitch,
181                 locked_box.pBits, locked_box.RowPitch, locked_box.SlicePitch,
182                 &dst_size, dst_format_desc);
183 
184         IDirect3DVolume9_UnlockBox(dst_volume);
185     }
186     else
187     {
188         const BYTE *src_addr;
189 
190 
191         if (!is_conversion_from_supported(src_format_desc)
192                 || !is_conversion_to_supported(dst_format_desc))
193         {
194             FIXME("Pixel format conversion is not implemented %#x -> %#x\n",
195                     src_format_desc->format, dst_format_desc->format);
196             return E_NOTIMPL;
197         }
198 
199         src_addr = src_memory;
200         src_addr += src_box->Front * src_slice_pitch;
201         src_addr += src_box->Top * src_row_pitch;
202         src_addr += src_box->Left * src_format_desc->bytes_per_pixel;
203 
204         hr = IDirect3DVolume9_LockBox(dst_volume, &locked_box, dst_box, 0);
205         if (FAILED(hr)) return hr;
206 
207         if ((filter & 0xf) == D3DX_FILTER_NONE)
208         {
209             convert_argb_pixels(src_memory, src_row_pitch, src_slice_pitch, &src_size, src_format_desc,
210                     locked_box.pBits, locked_box.RowPitch, locked_box.SlicePitch, &dst_size, dst_format_desc, color_key,
211                     src_palette);
212         }
213         else
214         {
215             if ((filter & 0xf) != D3DX_FILTER_POINT)
216                 FIXME("Unhandled filter %#x.\n", filter);
217 
218             point_filter_argb_pixels(src_addr, src_row_pitch, src_slice_pitch, &src_size, src_format_desc,
219                     locked_box.pBits, locked_box.RowPitch, locked_box.SlicePitch, &dst_size, dst_format_desc, color_key,
220                     src_palette);
221         }
222 
223         IDirect3DVolume9_UnlockBox(dst_volume);
224     }
225 
226     return D3D_OK;
227 }
228 
229 HRESULT WINAPI D3DXLoadVolumeFromFileInMemory(IDirect3DVolume9 *dst_volume,
230                                               const PALETTEENTRY *dst_palette,
231                                               const D3DBOX *dst_box,
232                                               const void *src_data,
233                                               UINT src_data_size,
234                                               const D3DBOX *src_box,
235                                               DWORD filter,
236                                               D3DCOLOR color_key,
237                                               D3DXIMAGE_INFO *src_info)
238 {
239     HRESULT hr;
240     D3DBOX box;
241     D3DXIMAGE_INFO image_info;
242 
243     TRACE("dst_volume %p, dst_palette %p, dst_box %p, src_data %p, src_data_size %u, src_box %p, "
244             "filter %#x, color_key 0x%08x, src_info %p.\n",
245             dst_volume, dst_palette, dst_box, src_data, src_data_size, src_box,
246             filter, color_key, src_info);
247 
248     if (!dst_volume || !src_data)
249         return D3DERR_INVALIDCALL;
250 
251     if (FAILED(hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &image_info)))
252         return hr;
253 
254     if (src_box)
255     {
256         if (src_box->Right > image_info.Width
257                 || src_box->Bottom > image_info.Height
258                 || src_box->Back > image_info.Depth)
259             return D3DERR_INVALIDCALL;
260 
261         box = *src_box;
262     }
263     else
264     {
265         box.Left = 0;
266         box.Top = 0;
267         box.Right = image_info.Width;
268         box.Bottom = image_info.Height;
269         box.Front = 0;
270         box.Back = image_info.Depth;
271 
272     }
273 
274     if (image_info.ImageFileFormat != D3DXIFF_DDS)
275     {
276         FIXME("File format %#x is not supported yet\n", image_info.ImageFileFormat);
277         return E_NOTIMPL;
278     }
279 
280     hr = load_volume_from_dds(dst_volume, dst_palette, dst_box, src_data, &box,
281             filter, color_key, &image_info);
282     if (FAILED(hr)) return hr;
283 
284     if (src_info)
285         *src_info = image_info;
286 
287     return D3D_OK;
288 }
289 
290 HRESULT WINAPI D3DXLoadVolumeFromVolume(IDirect3DVolume9 *dst_volume,
291                                         const PALETTEENTRY *dst_palette,
292                                         const D3DBOX *dst_box,
293                                         IDirect3DVolume9 *src_volume,
294                                         const PALETTEENTRY *src_palette,
295                                         const D3DBOX *src_box,
296                                         DWORD filter,
297                                         D3DCOLOR color_key)
298 {
299     HRESULT hr;
300     D3DBOX box;
301     D3DVOLUME_DESC desc;
302     D3DLOCKED_BOX locked_box;
303 
304     TRACE("(%p, %p, %p, %p, %p, %p, %#x, %#x)\n",
305             dst_volume, dst_palette, dst_box, src_volume, src_palette, src_box,
306             filter, color_key);
307 
308     if (!dst_volume || !src_volume) return D3DERR_INVALIDCALL;
309 
310     IDirect3DVolume9_GetDesc(src_volume, &desc);
311 
312     if (!src_box)
313     {
314         box.Left = box.Top = 0;
315         box.Right = desc.Width;
316         box.Bottom = desc.Height;
317         box.Front = 0;
318         box.Back = desc.Depth;
319     }
320     else box = *src_box;
321 
322     hr = IDirect3DVolume9_LockBox(src_volume, &locked_box, NULL, D3DLOCK_READONLY);
323     if (FAILED(hr)) return hr;
324 
325     hr = D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box,
326             locked_box.pBits, desc.Format, locked_box.RowPitch, locked_box.SlicePitch,
327             src_palette, &box, filter, color_key);
328 
329     IDirect3DVolume9_UnlockBox(src_volume);
330     return hr;
331 }
332