d3dx9: Remove ID3DXMatrixStackImpl typedef.
[wine] / dlls / d3dx9_36 / volume.c
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 "wine/debug.h"
20 #include "d3dx9_36_private.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
23
24 HRESULT WINAPI D3DXLoadVolumeFromFileA(IDirect3DVolume9 *dst_volume,
25                                        const PALETTEENTRY *dst_palette,
26                                        const D3DBOX *dst_box,
27                                        const char *filename,
28                                        const D3DBOX *src_box,
29                                        DWORD filter,
30                                        D3DCOLOR color_key,
31                                        D3DXIMAGE_INFO *info)
32 {
33     HRESULT hr;
34     int length;
35     WCHAR *filenameW;
36
37     TRACE("(%p, %p, %p, %s, %p, %#x, %#x, %p)\n",
38             dst_volume, dst_palette, dst_box, debugstr_a(filename), src_box,
39             filter, color_key, info);
40
41     if (!dst_volume || !filename) return D3DERR_INVALIDCALL;
42
43     length = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
44     filenameW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(*filenameW));
45     if (!filenameW) return E_OUTOFMEMORY;
46
47     hr = D3DXLoadVolumeFromFileW(dst_volume, dst_palette, dst_box, filenameW,
48             src_box, filter, color_key, info);
49     HeapFree(GetProcessHeap(), 0, filenameW);
50
51     return hr;
52 }
53
54 HRESULT WINAPI D3DXLoadVolumeFromFileW(IDirect3DVolume9 *dst_volume,
55                                        const PALETTEENTRY *dst_palette,
56                                        const D3DBOX *dst_box,
57                                        const WCHAR *filename,
58                                        const D3DBOX *src_box,
59                                        DWORD filter,
60                                        D3DCOLOR color_key,
61                                        D3DXIMAGE_INFO *info)
62 {
63     HRESULT hr;
64     void *data;
65     UINT data_size;
66
67     TRACE("(%p, %p, %p, %s, %p, %#x, %#x, %p)\n",
68             dst_volume, dst_palette, dst_box, debugstr_w(filename), src_box,
69             filter, color_key, info);
70
71     if (!dst_volume || !filename) return D3DERR_INVALIDCALL;
72
73     if (FAILED(map_view_of_file(filename, &data, &data_size)))
74         return D3DXERR_INVALIDDATA;
75
76     hr = D3DXLoadVolumeFromFileInMemory(dst_volume, dst_palette, dst_box,
77             data, data_size, src_box, filter, color_key, info);
78     UnmapViewOfFile(data);
79
80     return hr;
81 }
82
83 HRESULT WINAPI D3DXLoadVolumeFromMemory(IDirect3DVolume9 *dst_volume,
84                                         const PALETTEENTRY *dst_palette,
85                                         const D3DBOX *dst_box,
86                                         const void *src_memory,
87                                         D3DFORMAT src_format,
88                                         UINT src_row_pitch,
89                                         UINT src_slice_pitch,
90                                         const PALETTEENTRY *src_palette,
91                                         const D3DBOX *src_box,
92                                         DWORD filter,
93                                         D3DCOLOR color_key)
94 {
95     HRESULT hr;
96     D3DVOLUME_DESC desc;
97     D3DLOCKED_BOX locked_box;
98     struct volume dst_size, src_size;
99     const struct pixel_format_desc *src_format_desc, *dst_format_desc;
100
101     TRACE("(%p, %p, %p, %p, %#x, %u, %u, %p, %p, %x, %x)\n", dst_volume, dst_palette, dst_box,
102             src_memory, src_format, src_row_pitch, src_slice_pitch, src_palette, src_box,
103             filter, color_key);
104
105     if (!dst_volume || !src_memory || !src_box) return D3DERR_INVALIDCALL;
106
107     if (src_format == D3DFMT_UNKNOWN
108             || src_box->Left >= src_box->Right
109             || src_box->Top >= src_box->Bottom
110             || src_box->Front >= src_box->Back)
111         return E_FAIL;
112
113     if (filter == D3DX_DEFAULT)
114         filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
115
116     IDirect3DVolume9_GetDesc(dst_volume, &desc);
117
118     src_size.width = src_box->Right - src_box->Left;
119     src_size.height = src_box->Bottom - src_box->Top;
120     src_size.depth = src_box->Back - src_box->Front;
121
122     if (!dst_box)
123     {
124         dst_size.width = desc.Width;
125         dst_size.height = desc.Height;
126         dst_size.depth = desc.Depth;
127     }
128     else
129     {
130         if (dst_box->Left >= dst_box->Right || dst_box->Right > desc.Width)
131             return D3DERR_INVALIDCALL;
132         if (dst_box->Top >= dst_box->Bottom || dst_box->Bottom > desc.Height)
133             return D3DERR_INVALIDCALL;
134         if (dst_box->Front >= dst_box->Back || dst_box->Back > desc.Depth)
135             return D3DERR_INVALIDCALL;
136
137         dst_size.width = dst_box->Right - dst_box->Left;
138         dst_size.height = dst_box->Bottom - dst_box->Top;
139         dst_size.depth = dst_box->Back - dst_box->Front;
140     }
141
142     src_format_desc = get_format_info(src_format);
143     if (src_format_desc->type == FORMAT_UNKNOWN)
144         return E_NOTIMPL;
145
146     dst_format_desc = get_format_info(desc.Format);
147     if (dst_format_desc->type == FORMAT_UNKNOWN)
148         return E_NOTIMPL;
149
150     if (desc.Format == src_format
151             && dst_size.width == src_size.width && dst_size.height == src_size.height && dst_size.depth == src_size.depth)
152     {
153         UINT row, slice;
154         BYTE *dst_addr;
155         const BYTE *src_addr;
156         UINT row_block_count = (src_size.width + src_format_desc->block_width - 1) / src_format_desc->block_width;
157         UINT row_count = (src_size.height + src_format_desc->block_height - 1) / src_format_desc->block_height;
158
159         if (src_box->Left & (src_format_desc->block_width - 1)
160                 || src_box->Top & (src_format_desc->block_height - 1)
161                 || (src_box->Right & (src_format_desc->block_width - 1)
162                     && src_size.width != desc.Width)
163                 || (src_box->Bottom & (src_format_desc->block_height - 1)
164                     && src_size.height != desc.Height))
165         {
166             FIXME("Source box (%u, %u, %u, %u) is misaligned\n",
167                     src_box->Left, src_box->Top, src_box->Right, src_box->Bottom);
168             return E_NOTIMPL;
169         }
170
171         hr = IDirect3DVolume9_LockBox(dst_volume, &locked_box, dst_box, 0);
172         if (FAILED(hr)) return hr;
173
174         for (slice = 0; slice < src_size.depth; slice++)
175         {
176             src_addr = src_memory;
177             src_addr += (src_box->Front + slice) * src_slice_pitch;
178             src_addr += (src_box->Top / src_format_desc->block_height) * src_row_pitch;
179             src_addr += (src_box->Left / src_format_desc->block_width) * src_format_desc->block_byte_count;
180
181             dst_addr = locked_box.pBits;
182             dst_addr += slice * locked_box.SlicePitch;
183
184             for (row = 0; row < row_count; row++)
185             {
186                 memcpy(dst_addr, src_addr, row_block_count * src_format_desc->block_byte_count);
187                 src_addr += src_row_pitch;
188                 dst_addr += locked_box.RowPitch;
189             }
190         }
191
192         IDirect3DVolume9_UnlockBox(dst_volume);
193     }
194     else
195     {
196         const BYTE *src_addr;
197
198         if (src_format_desc->bytes_per_pixel > 4 || dst_format_desc->bytes_per_pixel > 4
199                 || src_format_desc->block_height != 1 || src_format_desc->block_width != 1
200                 || dst_format_desc->block_height != 1 || dst_format_desc->block_width != 1)
201         {
202             FIXME("Pixel format conversion not implemented %#x -> %#x\n",
203                     src_format_desc->format, dst_format_desc->format);
204             return E_NOTIMPL;
205         }
206
207         src_addr = src_memory;
208         src_addr += src_box->Front * src_slice_pitch;
209         src_addr += src_box->Top * src_row_pitch;
210         src_addr += src_box->Left * src_format_desc->bytes_per_pixel;
211
212         hr = IDirect3DVolume9_LockBox(dst_volume, &locked_box, dst_box, 0);
213         if (FAILED(hr)) return hr;
214
215         if ((filter & 0xf) == D3DX_FILTER_NONE)
216         {
217             copy_simple_data(src_memory, src_row_pitch, src_slice_pitch, &src_size, src_format_desc,
218                     locked_box.pBits, locked_box.RowPitch, locked_box.SlicePitch, &dst_size, dst_format_desc, color_key);
219         }
220         else
221         {
222             if ((filter & 0xf) != D3DX_FILTER_POINT)
223                 FIXME("Unhandled filter %#x.\n", filter);
224
225             point_filter_simple_data(src_addr, src_row_pitch, src_slice_pitch, &src_size, src_format_desc,
226                     locked_box.pBits, locked_box.RowPitch, locked_box.SlicePitch, &dst_size, dst_format_desc, color_key);
227         }
228
229         IDirect3DVolume9_UnlockBox(dst_volume);
230     }
231
232     return D3D_OK;
233 }
234
235 HRESULT WINAPI D3DXLoadVolumeFromFileInMemory(IDirect3DVolume9 *dst_volume,
236                                               const PALETTEENTRY *dst_palette,
237                                               const D3DBOX *dst_box,
238                                               const void *src_data,
239                                               UINT src_data_size,
240                                               const D3DBOX *src_box,
241                                               DWORD filter,
242                                               D3DCOLOR color_key,
243                                               D3DXIMAGE_INFO *src_info)
244 {
245     HRESULT hr;
246     D3DBOX box;
247     D3DXIMAGE_INFO image_info;
248
249     if (!dst_volume || !src_data) return D3DERR_INVALIDCALL;
250
251     hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &image_info);
252     if (FAILED(hr)) 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 }