d3dx9: Implement D3DXLoadVolumeFromFileInMemory.
[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 D3DXLoadVolumeFromMemory(IDirect3DVolume9 *dst_volume,
25                                         const PALETTEENTRY *dst_palette,
26                                         const D3DBOX *dst_box,
27                                         const void *src_memory,
28                                         D3DFORMAT src_format,
29                                         UINT src_row_pitch,
30                                         UINT src_slice_pitch,
31                                         const PALETTEENTRY *src_palette,
32                                         const D3DBOX *src_box,
33                                         DWORD filter,
34                                         D3DCOLOR color_key)
35 {
36     HRESULT hr;
37     D3DVOLUME_DESC desc;
38     D3DLOCKED_BOX locked_box;
39     UINT dst_width, dst_height, dst_depth;
40     UINT src_width, src_height, src_depth;
41     const PixelFormatDesc *src_format_desc, *dst_format_desc;
42
43     TRACE("(%p, %p, %p, %p, %#x, %u, %u, %p, %p, %x, %x)\n", dst_volume, dst_palette, dst_box,
44             src_memory, src_format, src_row_pitch, src_slice_pitch, src_palette, src_box,
45             filter, color_key);
46
47     if (!dst_volume || !src_memory || !src_box) return D3DERR_INVALIDCALL;
48
49     if (src_format == D3DFMT_UNKNOWN
50             || src_box->Left >= src_box->Right
51             || src_box->Top >= src_box->Bottom
52             || src_box->Front >= src_box->Back)
53         return E_FAIL;
54
55     if (filter == D3DX_DEFAULT)
56         filter = D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER;
57
58     IDirect3DVolume9_GetDesc(dst_volume, &desc);
59
60     src_width = src_box->Right - src_box->Left;
61     src_height = src_box->Bottom - src_box->Top;
62     src_depth = src_box->Back - src_box->Front;
63
64     if (!dst_box)
65     {
66         dst_width = desc.Width;
67         dst_height = desc.Height;
68         dst_depth = desc.Depth;
69     }
70     else
71     {
72         if (dst_box->Left >= dst_box->Right || dst_box->Right > desc.Width)
73             return D3DERR_INVALIDCALL;
74         if (dst_box->Top >= dst_box->Bottom || dst_box->Bottom > desc.Height)
75             return D3DERR_INVALIDCALL;
76         if (dst_box->Front >= dst_box->Back || dst_box->Back > desc.Depth)
77             return D3DERR_INVALIDCALL;
78
79         dst_width = dst_box->Right - dst_box->Left;
80         dst_height = dst_box->Bottom - dst_box->Top;
81         dst_depth = dst_box->Back - dst_box->Front;
82     }
83
84     src_format_desc = get_format_info(src_format);
85     if (src_format_desc->type == FORMAT_UNKNOWN)
86         return E_NOTIMPL;
87
88     dst_format_desc = get_format_info(desc.Format);
89     if (dst_format_desc->type == FORMAT_UNKNOWN)
90         return E_NOTIMPL;
91
92     if (desc.Format == src_format
93             && dst_width == src_width && dst_height == src_height && dst_depth == src_depth)
94     {
95         UINT row, slice;
96         BYTE *dst_addr;
97         const BYTE *src_addr;
98         UINT row_block_count = (src_width + src_format_desc->block_width - 1) / src_format_desc->block_width;
99         UINT row_count = (src_height + src_format_desc->block_height - 1) / src_format_desc->block_height;
100
101         if (src_box->Left & (src_format_desc->block_width - 1)
102                 || src_box->Top & (src_format_desc->block_height - 1)
103                 || (src_box->Right & (src_format_desc->block_width - 1)
104                     && src_width != desc.Width)
105                 || (src_box->Bottom & (src_format_desc->block_height - 1)
106                     && src_height != desc.Height))
107         {
108             FIXME("Source box (%u, %u, %u, %u) is misaligned\n",
109                     src_box->Left, src_box->Top, src_box->Right, src_box->Bottom);
110             return E_NOTIMPL;
111         }
112
113         hr = IDirect3DVolume9_LockBox(dst_volume, &locked_box, dst_box, 0);
114         if (FAILED(hr)) return hr;
115
116         for (slice = 0; slice < src_depth; slice++)
117         {
118             src_addr = src_memory;
119             src_addr += (src_box->Front + slice) * src_slice_pitch;
120             src_addr += (src_box->Top / src_format_desc->block_height) * src_row_pitch;
121             src_addr += (src_box->Left / src_format_desc->block_width) * src_format_desc->block_byte_count;
122
123             dst_addr = locked_box.pBits;
124             dst_addr += slice * locked_box.SlicePitch;
125
126             for (row = 0; row < row_count; row++)
127             {
128                 memcpy(dst_addr, src_addr, row_block_count * src_format_desc->block_byte_count);
129                 src_addr += src_row_pitch;
130                 dst_addr += locked_box.RowPitch;
131             }
132         }
133
134         IDirect3DVolume9_UnlockBox(dst_volume);
135     }
136     else
137     {
138         FIXME("Stretching or format conversion not implemented\n");
139         return E_NOTIMPL;
140     }
141
142     return D3D_OK;
143 }
144
145 HRESULT WINAPI D3DXLoadVolumeFromFileInMemory(IDirect3DVolume9 *dst_volume,
146                                               const PALETTEENTRY *dst_palette,
147                                               const D3DBOX *dst_box,
148                                               const void *src_data,
149                                               UINT src_data_size,
150                                               const D3DBOX *src_box,
151                                               DWORD filter,
152                                               D3DCOLOR color_key,
153                                               D3DXIMAGE_INFO *src_info)
154 {
155     HRESULT hr;
156     D3DBOX box;
157     D3DXIMAGE_INFO image_info;
158
159     if (!dst_volume || !src_data) return D3DERR_INVALIDCALL;
160
161     hr = D3DXGetImageInfoFromFileInMemory(src_data, src_data_size, &image_info);
162     if (FAILED(hr)) return hr;
163
164     if (src_box)
165     {
166         if (src_box->Right > image_info.Width
167                 || src_box->Bottom > image_info.Height
168                 || src_box->Back > image_info.Depth)
169             return D3DERR_INVALIDCALL;
170
171         box = *src_box;
172     }
173     else
174     {
175         box.Left = 0;
176         box.Top = 0;
177         box.Right = image_info.Width;
178         box.Bottom = image_info.Height;
179         box.Front = 0;
180         box.Back = image_info.Depth;
181
182     }
183
184     if (image_info.ImageFileFormat != D3DXIFF_DDS)
185     {
186         FIXME("File format %#x is not supported yet\n", image_info.ImageFileFormat);
187         return E_NOTIMPL;
188     }
189
190     hr = load_volume_from_dds(dst_volume, dst_palette, dst_box, src_data, &box,
191             filter, color_key, &image_info);
192     if (FAILED(hr)) return hr;
193
194     if (src_info)
195         *src_info = image_info;
196
197     return D3D_OK;
198 }
199
200 HRESULT WINAPI D3DXLoadVolumeFromVolume(IDirect3DVolume9 *dst_volume,
201                                         const PALETTEENTRY *dst_palette,
202                                         const D3DBOX *dst_box,
203                                         IDirect3DVolume9 *src_volume,
204                                         const PALETTEENTRY *src_palette,
205                                         const D3DBOX *src_box,
206                                         DWORD filter,
207                                         D3DCOLOR color_key)
208 {
209     HRESULT hr;
210     D3DBOX box;
211     D3DVOLUME_DESC desc;
212     D3DLOCKED_BOX locked_box;
213
214     TRACE("(%p, %p, %p, %p, %p, %p, %#x, %#x)\n",
215             dst_volume, dst_palette, dst_box, src_volume, src_palette, src_box,
216             filter, color_key);
217
218     if (!dst_volume || !src_volume) return D3DERR_INVALIDCALL;
219
220     IDirect3DVolume9_GetDesc(src_volume, &desc);
221
222     if (!src_box)
223     {
224         box.Left = box.Top = 0;
225         box.Right = desc.Width;
226         box.Bottom = desc.Height;
227         box.Front = 0;
228         box.Back = desc.Depth;
229     }
230     else box = *src_box;
231
232     hr = IDirect3DVolume9_LockBox(src_volume, &locked_box, NULL, D3DLOCK_READONLY);
233     if (FAILED(hr)) return hr;
234
235     hr = D3DXLoadVolumeFromMemory(dst_volume, dst_palette, dst_box,
236             locked_box.pBits, desc.Format, locked_box.RowPitch, locked_box.SlicePitch,
237             src_palette, &box, filter, color_key);
238
239     IDirect3DVolume9_UnlockBox(src_volume);
240     return hr;
241 }