xref: /reactos/dll/directx/wine/d3dx9_36/volume.c (revision c2c66aff)
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 "d3dx9_36_private.h"
20 
21 HRESULT WINAPI D3DXLoadVolumeFromFileA(IDirect3DVolume9 *dst_volume,
22                                        const PALETTEENTRY *dst_palette,
23                                        const D3DBOX *dst_box,
24                                        const char *filename,
25                                        const D3DBOX *src_box,
26                                        DWORD filter,
27                                        D3DCOLOR color_key,
28                                        D3DXIMAGE_INFO *info)
29 {
30     HRESULT hr;
31     int length;
32     WCHAR *filenameW;
33 
34     TRACE("(%p, %p, %p, %s, %p, %#x, %#x, %p)\n",
35             dst_volume, dst_palette, dst_box, debugstr_a(filename), src_box,
36             filter, color_key, info);
37 
38     if (!dst_volume || !filename) return D3DERR_INVALIDCALL;
39 
40     length = MultiByteToWideChar(CP_ACP, 0, filename, -1, NULL, 0);
41     filenameW = HeapAlloc(GetProcessHeap(), 0, length * sizeof(*filenameW));
42     if (!filenameW) return E_OUTOFMEMORY;
43 
44     hr = D3DXLoadVolumeFromFileW(dst_volume, dst_palette, dst_box, filenameW,
45             src_box, filter, color_key, info);
46     HeapFree(GetProcessHeap(), 0, filenameW);
47 
48     return hr;
49 }
50 
51 HRESULT WINAPI D3DXLoadVolumeFromFileW(IDirect3DVolume9 *dst_volume,
52                                        const PALETTEENTRY *dst_palette,
53                                        const D3DBOX *dst_box,
54                                        const WCHAR *filename,
55                                        const D3DBOX *src_box,
56                                        DWORD filter,
57                                        D3DCOLOR color_key,
58                                        D3DXIMAGE_INFO *info)
59 {
60     HRESULT hr;
61     void *data;
62     UINT data_size;
63 
64     TRACE("(%p, %p, %p, %s, %p, %#x, %#x, %p)\n",
65             dst_volume, dst_palette, dst_box, debugstr_w(filename), src_box,
66             filter, color_key, info);
67 
68     if (!dst_volume || !filename) return D3DERR_INVALIDCALL;
69 
70     if (FAILED(map_view_of_file(filename, &data, &data_size)))
71         return D3DXERR_INVALIDDATA;
72 
73     hr = D3DXLoadVolumeFromFileInMemory(dst_volume, dst_palette, dst_box,
74             data, data_size, src_box, filter, color_key, info);
75     UnmapViewOfFile(data);
76 
77     return hr;
78 }
79 
80 HRESULT WINAPI D3DXLoadVolumeFromMemory(IDirect3DVolume9 *dst_volume,
81                                         const PALETTEENTRY *dst_palette,
82                                         const D3DBOX *dst_box,
83                                         const void *src_memory,
84                                         D3DFORMAT src_format,
85                                         UINT src_row_pitch,
86                                         UINT src_slice_pitch,
87                                         const PALETTEENTRY *src_palette,
88                                         const D3DBOX *src_box,
89                                         DWORD filter,
90                                         D3DCOLOR color_key)
91 {
92     HRESULT hr;
93     D3DVOLUME_DESC desc;
94     D3DLOCKED_BOX locked_box;
95     struct volume dst_size, src_size;
96     const struct pixel_format_desc *src_format_desc, *dst_format_desc;
97 
98     TRACE("(%p, %p, %p, %p, %#x, %u, %u, %p, %p, %x, %x)\n", dst_volume, dst_palette, dst_box,
99             src_memory, src_format, src_row_pitch, src_slice_pitch, src_palette, src_box,
100             filter, color_key);
101 
102     if (!dst_volume || !src_memory || !src_box) return D3DERR_INVALIDCALL;
103 
104     if (src_format == D3DFMT_UNKNOWN
105             || src_box->Left >= src_box->Right
106             || src_box->Top >= src_box->Bottom
107             || src_box->Front >= src_box->Back)
108         return E_FAIL;
109 
110     if (filter == D3DX_DEFAULT)
111         filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
112 
113     IDirect3DVolume9_GetDesc(dst_volume, &desc);
114 
115     src_size.width = src_box->Right - src_box->Left;
116     src_size.height = src_box->Bottom - src_box->Top;
117     src_size.depth = src_box->Back - src_box->Front;
118 
119     if (!dst_box)
120     {
121         dst_size.width = desc.Width;
122         dst_size.height = desc.Height;
123         dst_size.depth = desc.Depth;
124     }
125     else
126     {
127         if (dst_box->Left >= dst_box->Right || dst_box->Right > desc.Width)
128             return D3DERR_INVALIDCALL;
129         if (dst_box->Top >= dst_box->Bottom || dst_box->Bottom > desc.Height)
130             return D3DERR_INVALIDCALL;
131         if (dst_box->Front >= dst_box->Back || dst_box->Back > desc.Depth)
132             return D3DERR_INVALIDCALL;
133 
134         dst_size.width = dst_box->Right - dst_box->Left;
135         dst_size.height = dst_box->Bottom - dst_box->Top;
136         dst_size.depth = dst_box->Back - dst_box->Front;
137     }
138 
139     src_format_desc = get_format_info(src_format);
140     if (src_format_desc->type == FORMAT_UNKNOWN)
141         return E_NOTIMPL;
142 
143     dst_format_desc = get_format_info(desc.Format);
144     if (dst_format_desc->type == FORMAT_UNKNOWN)
145         return E_NOTIMPL;
146 
147     if (desc.Format == src_format
148             && dst_size.width == src_size.width
149             && dst_size.height == src_size.height
150             && dst_size.depth == src_size.depth
151             && color_key == 0)
152     {
153         const BYTE *src_addr;
154 
155         if (src_box->Left & (src_format_desc->block_width - 1)
156                 || src_box->Top & (src_format_desc->block_height - 1)
157                 || (src_box->Right & (src_format_desc->block_width - 1)
158                     && src_size.width != desc.Width)
159                 || (src_box->Bottom & (src_format_desc->block_height - 1)
160                     && src_size.height != desc.Height))
161         {
162             FIXME("Source box (%u, %u, %u, %u) is misaligned\n",
163                     src_box->Left, src_box->Top, src_box->Right, src_box->Bottom);
164             return E_NOTIMPL;
165         }
166 
167         src_addr = src_memory;
168         src_addr += src_box->Front * src_slice_pitch;
169         src_addr += (src_box->Top / src_format_desc->block_height) * src_row_pitch;
170         src_addr += (src_box->Left / src_format_desc->block_width) * src_format_desc->block_byte_count;
171 
172         hr = IDirect3DVolume9_LockBox(dst_volume, &locked_box, dst_box, 0);
173         if (FAILED(hr)) return hr;
174 
175         copy_pixels(src_addr, src_row_pitch, src_slice_pitch,
176                 locked_box.pBits, locked_box.RowPitch, locked_box.SlicePitch,
177                 &dst_size, dst_format_desc);
178 
179         IDirect3DVolume9_UnlockBox(dst_volume);
180     }
181     else
182     {
183         const BYTE *src_addr;
184 
185 
186         if (((src_format_desc->type != FORMAT_ARGB) && (src_format_desc->type != FORMAT_INDEX)) ||
187             (dst_format_desc->type != FORMAT_ARGB))
188         {
189             FIXME("Pixel format conversion is not implemented %#x -> %#x\n",
190                     src_format_desc->format, dst_format_desc->format);
191             return E_NOTIMPL;
192         }
193 
194         src_addr = src_memory;
195         src_addr += src_box->Front * src_slice_pitch;
196         src_addr += src_box->Top * src_row_pitch;
197         src_addr += src_box->Left * src_format_desc->bytes_per_pixel;
198 
199         hr = IDirect3DVolume9_LockBox(dst_volume, &locked_box, dst_box, 0);
200         if (FAILED(hr)) return hr;
201 
202         if ((filter & 0xf) == D3DX_FILTER_NONE)
203         {
204             convert_argb_pixels(src_memory, src_row_pitch, src_slice_pitch, &src_size, src_format_desc,
205                     locked_box.pBits, locked_box.RowPitch, locked_box.SlicePitch, &dst_size, dst_format_desc, color_key,
206                     src_palette);
207         }
208         else
209         {
210             if ((filter & 0xf) != D3DX_FILTER_POINT)
211                 FIXME("Unhandled filter %#x.\n", filter);
212 
213             point_filter_argb_pixels(src_addr, src_row_pitch, src_slice_pitch, &src_size, src_format_desc,
214                     locked_box.pBits, locked_box.RowPitch, locked_box.SlicePitch, &dst_size, dst_format_desc, color_key,
215                     src_palette);
216         }
217 
218         IDirect3DVolume9_UnlockBox(dst_volume);
219     }
220 
221     return D3D_OK;
222 }
223 
224 HRESULT WINAPI D3DXLoadVolumeFromFileInMemory(IDirect3DVolume9 *dst_volume,
225                                               const PALETTEENTRY *dst_palette,
226                                               const D3DBOX *dst_box,
227                                               const void *src_data,
228                                               UINT src_data_size,
229                                               const D3DBOX *src_box,
230                                               DWORD filter,
231                                               D3DCOLOR color_key,
232                                               D3DXIMAGE_INFO *src_info)
233 {
234     HRESULT hr;
235     D3DBOX box;
236     D3DXIMAGE_INFO image_info;
237 
238     TRACE("dst_volume %p, dst_palette %p, dst_box %p, src_data %p, src_data_size %u, src_box %p,\n",
239             dst_volume, dst_palette, dst_box, src_data, src_data_size, src_box);
240     TRACE("filter %#x, color_key %#x, src_info %p.\n", filter, color_key, src_info);
241 
242     if (!dst_volume || !src_data) return D3DERR_INVALIDCALL;
243 
244     hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &image_info);
245     if (FAILED(hr)) return hr;
246 
247     if (src_box)
248     {
249         if (src_box->Right > image_info.Width
250                 || src_box->Bottom > image_info.Height
251                 || src_box->Back > image_info.Depth)
252             return D3DERR_INVALIDCALL;
253 
254         box = *src_box;
255     }
256     else
257     {
258         box.Left = 0;
259         box.Top = 0;
260         box.Right = image_info.Width;
261         box.Bottom = image_info.Height;
262         box.Front = 0;
263         box.Back = image_info.Depth;
264 
265     }
266 
267     if (image_info.ImageFileFormat != D3DXIFF_DDS)
268     {
269         FIXME("File format %#x is not supported yet\n", image_info.ImageFileFormat);
270         return E_NOTIMPL;
271     }
272 
273     hr = load_volume_from_dds(dst_volume, dst_palette, dst_box, src_data, &box,
274             filter, color_key, &image_info);
275     if (FAILED(hr)) return hr;
276 
277     if (src_info)
278         *src_info = image_info;
279 
280     return D3D_OK;
281 }
282 
283 HRESULT WINAPI D3DXLoadVolumeFromVolume(IDirect3DVolume9 *dst_volume,
284                                         const PALETTEENTRY *dst_palette,
285                                         const D3DBOX *dst_box,
286                                         IDirect3DVolume9 *src_volume,
287                                         const PALETTEENTRY *src_palette,
288                                         const D3DBOX *src_box,
289                                         DWORD filter,
290                                         D3DCOLOR color_key)
291 {
292     HRESULT hr;
293     D3DBOX box;
294     D3DVOLUME_DESC desc;
295     D3DLOCKED_BOX locked_box;
296 
297     TRACE("(%p, %p, %p, %p, %p, %p, %#x, %#x)\n",
298             dst_volume, dst_palette, dst_box, src_volume, src_palette, src_box,
299             filter, color_key);
300 
301     if (!dst_volume || !src_volume) return D3DERR_INVALIDCALL;
302 
303     IDirect3DVolume9_GetDesc(src_volume, &desc);
304 
305     if (!src_box)
306     {
307         box.Left = box.Top = 0;
308         box.Right = desc.Width;
309         box.Bottom = desc.Height;
310         box.Front = 0;
311         box.Back = desc.Depth;
312     }
313     else box = *src_box;
314 
315     hr = IDirect3DVolume9_LockBox(src_volume, &locked_box, NULL, D3DLOCK_READONLY);
316     if (FAILED(hr)) return hr;
317 
318     hr = D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box,
319             locked_box.pBits, desc.Format, locked_box.RowPitch, locked_box.SlicePitch,
320             src_palette, &box, filter, color_key);
321 
322     IDirect3DVolume9_UnlockBox(src_volume);
323     return hr;
324 }
325