// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using SharpDX.Direct3D;
using SharpDX.Mathematics.Interop;
namespace SharpDX.Direct3D9
{
public partial class Volume
{
///
/// Loads a volume from memory.
///
/// Pointer to a structure, the destination palette of 256 colors or null.
/// Pointer to a structure. Specifies the destination box. Set this parameter to null to specify the entire volume.
/// Pointer to the top-left corner of the source volume in memory.
/// Member of the enumerated type, the pixel format of the source volume.
/// Pitch of source image, in bytes. For DXT formats (compressed texture formats), this number should represent the size of one row of cells, in bytes.
/// Pitch of source image, in bytes. For DXT formats (compressed texture formats), this number should represent the size of one slice of cells, in bytes.
/// Pointer to a structure, the source palette of 256 colors or null.
/// Pointer to a structure. Specifies the source box. null is not a valid value for this parameter.
/// A combination of one or more controlling how the image is filtered. Specifying D3DX_DEFAULT for this parameter is the equivalent of specifying | .
/// value to replace with transparent black, or 0 to disable the color key. This is always a 32-bit ARGB color, independent of the source image format. Alpha is significant and should usually be set to FF for opaque color keys. Thus, for opaque black, the value would be equal to 0xFF000000.
/// If the function succeeds, the return value is . If the function fails, the return value can be one of the following values: , D3DXERR_INVALIDDATA.
///
/// Writing to a non-level-zero surface of the volume texture will not cause the dirty rectangle to be updated. If is called and the texture was not already dirty (this is unlikely under normal usage scenarios), the application needs to explicitly call on the volume texture.
///
/// HRESULT D3DXLoadVolumeFromMemory([In] IDirect3DVolume9* pDestVolume,[Out, Buffer] const PALETTEENTRY* pDestPalette,[In] const void* pDestBox,[In] const void* pSrcMemory,[In] D3DFORMAT SrcFormat,[In] unsigned int SrcRowPitch,[In] unsigned int SrcSlicePitch,[In, Buffer] const PALETTEENTRY* pSrcPalette,[In] const void* pSrcBox,[In] D3DX_FILTER Filter,[In] int ColorKey)
public unsafe void LoadFromMemory(SharpDX.Direct3D9.PaletteEntry[] destPaletteRef, Box? destBox, System.IntPtr srcMemoryPointer, SharpDX.Direct3D9.Format srcFormat, int srcRowPitch, int srcSlicePitch, SharpDX.Direct3D9.PaletteEntry[] srcPaletteRef, Box srcBox, SharpDX.Direct3D9.Filter filter, RawColorBGRA colorKey)
{
Box localDestBox;
if (destBox.HasValue)
localDestBox = destBox.Value;
D3DX9.LoadVolumeFromMemory(
this, destPaletteRef, new IntPtr(&localDestBox), srcMemoryPointer, srcFormat, srcRowPitch, srcSlicePitch, srcPaletteRef, new IntPtr(&srcBox), filter, *(int*)&colorKey);
}
///
/// Loads a volume from a file on the disk.
///
/// The volume.
/// Name of the file.
/// The filter.
/// The color key.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileW([In] IDirect3DVolume9* pDestVolume,[In] const PALETTEENTRY* pDestPalette,[In] const D3DBOX* pDestBox,[In] const wchar_t* pSrcFile,[In] const D3DBOX* pSrcBox,[In] unsigned int Filter,[In] D3DCOLOR ColorKey,[In] D3DXIMAGE_INFO* pSrcInfo)
public static void FromFile(Volume volume, string fileName, Filter filter, int colorKey)
{
D3DX9.LoadVolumeFromFileW(volume, null, IntPtr.Zero, fileName, IntPtr.Zero, filter, colorKey, IntPtr.Zero);
}
///
/// Loads a volume from a file on the disk.
///
/// The volume.
/// Name of the file.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileW([In] IDirect3DVolume9* pDestVolume,[In] const PALETTEENTRY* pDestPalette,[In] const D3DBOX* pDestBox,[In] const wchar_t* pSrcFile,[In] const D3DBOX* pSrcBox,[In] unsigned int Filter,[In] D3DCOLOR ColorKey,[In] D3DXIMAGE_INFO* pSrcInfo)
public static void FromFile(Volume volume, string fileName, Filter filter, int colorKey, Box sourceBox, Box destinationBox)
{
unsafe
{
D3DX9.LoadVolumeFromFileW(volume, null, new IntPtr(&destinationBox), fileName, new IntPtr(&sourceBox), filter, colorKey, IntPtr.Zero);
}
}
///
/// Loads a volume from a file on the disk.
///
/// The volume.
/// Name of the file.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
/// The image information.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileW([In] IDirect3DVolume9* pDestVolume,[In] const PALETTEENTRY* pDestPalette,[In] const D3DBOX* pDestBox,[In] const wchar_t* pSrcFile,[In] const D3DBOX* pSrcBox,[In] unsigned int Filter,[In] D3DCOLOR ColorKey,[In] D3DXIMAGE_INFO* pSrcInfo)
public static void FromFile(Volume volume, string fileName, Filter filter, int colorKey, Box sourceBox, Box destinationBox, out ImageInformation imageInformation)
{
FromFile(volume, fileName, filter, colorKey, sourceBox, destinationBox, null, out imageInformation);
}
///
/// Loads a volume from a file on the disk.
///
/// The volume.
/// Name of the file.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
/// The palette.
/// The image information.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileW([In] IDirect3DVolume9* pDestVolume,[In] const PALETTEENTRY* pDestPalette,[In] const D3DBOX* pDestBox,[In] const wchar_t* pSrcFile,[In] const D3DBOX* pSrcBox,[In] unsigned int Filter,[In] D3DCOLOR ColorKey,[In] D3DXIMAGE_INFO* pSrcInfo)
public static void FromFile(Volume volume, string fileName, Filter filter, int colorKey, Box sourceBox, Box destinationBox, PaletteEntry[] palette, out ImageInformation imageInformation)
{
unsafe
{
fixed (void* pImageInformation = &imageInformation)
D3DX9.LoadVolumeFromFileW(volume, palette, new IntPtr(&destinationBox), fileName, new IntPtr(&sourceBox), filter, colorKey, (IntPtr)pImageInformation);
}
}
///
/// Loads a volume from a file in memory.
///
/// The volume.
/// The memory.
/// The filter.
/// The color key.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileInMemory([In] IDirect3DVolume9* pDestVolume,[Out, Buffer] const PALETTEENTRY* pDestPalette,[In] const void* pDestBox,[In] const void* pSrcData,[In] unsigned int SrcDataSize,[In] const void* pSrcBox,[In] D3DX_FILTER Filter,[In] int ColorKey,[In] void* pSrcInfo)
public static void FromFileInMemory(Volume volume, byte[] memory, Filter filter, int colorKey)
{
unsafe
{
fixed (void* pMemory = memory)
D3DX9.LoadVolumeFromFileInMemory(volume, null, IntPtr.Zero, (IntPtr)pMemory, memory.Length, IntPtr.Zero, filter, colorKey, IntPtr.Zero);
}
}
///
/// Loads a volume from a file in memory.
///
/// The volume.
/// The memory.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileInMemory([In] IDirect3DVolume9* pDestVolume,[Out, Buffer] const PALETTEENTRY* pDestPalette,[In] const void* pDestBox,[In] const void* pSrcData,[In] unsigned int SrcDataSize,[In] const void* pSrcBox,[In] D3DX_FILTER Filter,[In] int ColorKey,[In] void* pSrcInfo)
public static void FromFileInMemory(Volume volume, byte[] memory, Filter filter, int colorKey, Box sourceBox, Box destinationBox)
{
unsafe
{
fixed (void* pMemory = memory)
D3DX9.LoadVolumeFromFileInMemory(volume, null, new IntPtr(&destinationBox), (IntPtr)pMemory, memory.Length, new IntPtr(&sourceBox), filter, colorKey, IntPtr.Zero);
}
}
///
/// Loads a volume from a file in memory.
///
/// The volume.
/// The memory.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
/// The image information.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileInMemory([In] IDirect3DVolume9* pDestVolume,[Out, Buffer] const PALETTEENTRY* pDestPalette,[In] const void* pDestBox,[In] const void* pSrcData,[In] unsigned int SrcDataSize,[In] const void* pSrcBox,[In] D3DX_FILTER Filter,[In] int ColorKey,[In] void* pSrcInfo)
public static void FromFileInMemory(Volume volume, byte[] memory, Filter filter, int colorKey, Box sourceBox, Box destinationBox, out ImageInformation imageInformation)
{
FromFileInMemory(volume, memory, filter, colorKey, sourceBox, destinationBox, null, out imageInformation);
}
///
/// Loads a volume from a file in memory.
///
/// The volume.
/// The memory.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
/// The palette.
/// The image information.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileInMemory([In] IDirect3DVolume9* pDestVolume,[Out, Buffer] const PALETTEENTRY* pDestPalette,[In] const void* pDestBox,[In] const void* pSrcData,[In] unsigned int SrcDataSize,[In] const void* pSrcBox,[In] D3DX_FILTER Filter,[In] int ColorKey,[In] void* pSrcInfo)
public static void FromFileInMemory(Volume volume, byte[] memory, Filter filter, int colorKey, Box sourceBox, Box destinationBox, PaletteEntry[] palette, out ImageInformation imageInformation)
{
unsafe
{
fixed (void* pMemory = memory)
fixed (void* pImageInformation = &imageInformation)
D3DX9.LoadVolumeFromFileInMemory(volume, palette, new IntPtr(&destinationBox), (IntPtr)pMemory, memory.Length, new IntPtr(&sourceBox), filter, colorKey, (IntPtr)pImageInformation);
}
}
///
/// Loads a volume from a file in a stream.
///
/// The volume.
/// The stream.
/// The filter.
/// The color key.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileInMemory([In] IDirect3DVolume9* pDestVolume,[Out, Buffer] const PALETTEENTRY* pDestPalette,[In] const void* pDestBox,[In] const void* pSrcData,[In] unsigned int SrcDataSize,[In] const void* pSrcBox,[In] D3DX_FILTER Filter,[In] int ColorKey,[In] void* pSrcInfo)
public static void FromFileInStream(Volume volume, Stream stream, Filter filter, int colorKey)
{
CreateFromFileInStream(volume, stream, filter, colorKey, IntPtr.Zero, IntPtr.Zero, null, IntPtr.Zero);
}
///
/// Loads a volume from a file in a stream.
///
/// The volume.
/// The stream.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileInMemory([In] IDirect3DVolume9* pDestVolume,[Out, Buffer] const PALETTEENTRY* pDestPalette,[In] const void* pDestBox,[In] const void* pSrcData,[In] unsigned int SrcDataSize,[In] const void* pSrcBox,[In] D3DX_FILTER Filter,[In] int ColorKey,[In] void* pSrcInfo)
public static void FromFileInStream(Volume volume, Stream stream, Filter filter, int colorKey, Box sourceBox, Box destinationBox)
{
unsafe
{
CreateFromFileInStream(volume, stream, filter, colorKey, new IntPtr(&sourceBox), new IntPtr(&destinationBox), null, IntPtr.Zero);
}
}
///
/// Loads a volume from a file in a stream.
///
/// The volume.
/// The stream.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
/// The image information.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromFileInMemory([In] IDirect3DVolume9* pDestVolume,[Out, Buffer] const PALETTEENTRY* pDestPalette,[In] const void* pDestBox,[In] const void* pSrcData,[In] unsigned int SrcDataSize,[In] const void* pSrcBox,[In] D3DX_FILTER Filter,[In] int ColorKey,[In] void* pSrcInfo)
public static void FromFileInStream(Volume volume, Stream stream, Filter filter, int colorKey, Box sourceBox, Box destinationBox, out ImageInformation imageInformation)
{
FromFileInStream(volume, stream, filter, colorKey, sourceBox, destinationBox, null, out imageInformation);
}
///
/// Loads a volume from a file in a stream.
///
/// The volume.
/// The stream.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
/// The palette.
/// The image information.
/// A object describing the result of the operation.
/// HRESULT D3DXLoadVolumeFromFileInMemory([In] IDirect3DVolume9* pDestVolume,[Out, Buffer] const PALETTEENTRY* pDestPalette,[In] const void* pDestBox,[In] const void* pSrcData,[In] unsigned int SrcDataSize,[In] const void* pSrcBox,[In] D3DX_FILTER Filter,[In] int ColorKey,[In] void* pSrcInfo)
public static void FromFileInStream(Volume volume, Stream stream, Filter filter, int colorKey, Box sourceBox, Box destinationBox, PaletteEntry[] palette, out ImageInformation imageInformation)
{
unsafe
{
fixed (void* pImageInformation = &imageInformation)
CreateFromFileInStream(volume, stream, filter, colorKey, new IntPtr(&sourceBox), new IntPtr(&destinationBox), palette, (IntPtr)pImageInformation);
}
}
private static void CreateFromFileInStream(Volume volume, Stream stream, Filter filter, int colorKey, IntPtr sourceBox, IntPtr destinationBox, PaletteEntry[] palette, IntPtr imageInformation)
{
if (stream is DataStream)
{
D3DX9.LoadVolumeFromFileInMemory(volume, palette, destinationBox, ((DataStream)stream).PositionPointer, (int)(stream.Length - stream.Position), sourceBox, filter, colorKey, imageInformation);
}
else
{
unsafe
{
var data = Utilities.ReadStream(stream);
fixed (void* pData = data)
D3DX9.LoadVolumeFromFileInMemory(volume, palette, destinationBox, (IntPtr)pData, data.Length, sourceBox, filter, colorKey, imageInformation);
}
}
}
///
/// Loads a volume from a source volume.
///
/// The destination volume.
/// The source volume.
/// The filter.
/// The color key.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromVolume([In] IDirect3DVolume9* pDestVolume,[In] const PALETTEENTRY* pDestPalette,[In] const D3DBOX* pDestBox,[In] IDirect3DVolume9* pSrcVolume,[In] const PALETTEENTRY* pSrcPalette,[In] const D3DBOX* pSrcBox,[In] unsigned int Filter,[In] D3DCOLOR ColorKey)
public static void FromVolume(Volume destinationVolume, Volume sourceVolume, Filter filter, int colorKey)
{
D3DX9.LoadVolumeFromVolume(destinationVolume, null, IntPtr.Zero, sourceVolume, null, IntPtr.Zero, filter, colorKey);
}
///
/// Loads a volume from a source volume.
///
/// The destination volume.
/// The source volume.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXLoadVolumeFromVolume([In] IDirect3DVolume9* pDestVolume,[In] const PALETTEENTRY* pDestPalette,[In] const D3DBOX* pDestBox,[In] IDirect3DVolume9* pSrcVolume,[In] const PALETTEENTRY* pSrcPalette,[In] const D3DBOX* pSrcBox,[In] unsigned int Filter,[In] D3DCOLOR ColorKey)
public static void FromVolume(Volume destinationVolume, Volume sourceVolume, Filter filter, int colorKey, Box sourceBox, Box destinationBox)
{
FromVolume(destinationVolume, sourceVolume, filter, colorKey, sourceBox, destinationBox, null, null);
}
///
/// Loads a volume from a source volume.
///
/// The destination volume.
/// The source volume.
/// The filter.
/// The color key.
/// The source box.
/// The destination box.
/// The destination palette.
/// The source palette.
/// A object describing the result of the operation.
/// HRESULT D3DXLoadVolumeFromVolume([In] IDirect3DVolume9* pDestVolume,[In] const PALETTEENTRY* pDestPalette,[In] const D3DBOX* pDestBox,[In] IDirect3DVolume9* pSrcVolume,[In] const PALETTEENTRY* pSrcPalette,[In] const D3DBOX* pSrcBox,[In] unsigned int Filter,[In] D3DCOLOR ColorKey)
public static void FromVolume(Volume destinationVolume, Volume sourceVolume, Filter filter, int colorKey, Box sourceBox, Box destinationBox, PaletteEntry[] destinationPalette, PaletteEntry[] sourcePalette)
{
unsafe
{
D3DX9.LoadVolumeFromVolume(destinationVolume, destinationPalette, new IntPtr(&destinationBox), sourceVolume, sourcePalette, new IntPtr(&sourceBox), filter, colorKey);
}
}
///
/// Locks a box on a volume resource.
///
/// The flags.
///
/// The locked region of this resource
///
/// HRESULT IDirect3DVolume9::LockBox([Out] D3DLOCKED_BOX* pLockedVolume,[In] const void* pBox,[In] D3DLOCK Flags)
public DataBox LockBox(LockFlags flags)
{
LockedBox lockedBox;
LockBox(out lockedBox, IntPtr.Zero, flags);
return new DataBox(lockedBox.PBits, lockedBox.RowPitch, lockedBox.SlicePitch);
}
///
/// Locks a box on a volume resource.
///
/// The box.
/// The flags.
/// The locked region of this resource
/// HRESULT IDirect3DVolume9::LockBox([Out] D3DLOCKED_BOX* pLockedVolume,[In] const void* pBox,[In] D3DLOCK Flags)
public DataBox LockBox(Box box, LockFlags flags)
{
unsafe
{
LockedBox lockedBox;
LockBox(out lockedBox, new IntPtr(&box), flags);
return new DataBox(lockedBox.PBits, lockedBox.RowPitch, lockedBox.SlicePitch);
}
}
///
/// Saves a volume to a file on disk.
///
/// The volume.
/// Name of the file.
/// The format.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXSaveVolumeToFileW([In] const wchar_t* pDestFile,[In] D3DXIMAGE_FILEFORMAT DestFormat,[In] IDirect3DVolume9* pSrcVolume,[In] const PALETTEENTRY* pSrcPalette,[In] const D3DBOX* pSrcBox)
public static void ToFile(Volume volume, string fileName, ImageFileFormat format)
{
D3DX9.SaveVolumeToFileW(fileName, format, volume, null, IntPtr.Zero);
}
///
/// Saves a volume to a file on disk.
///
/// The volume.
/// Name of the file.
/// The format.
/// The box.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXSaveVolumeToFileW([In] const wchar_t* pDestFile,[In] D3DXIMAGE_FILEFORMAT DestFormat,[In] IDirect3DVolume9* pSrcVolume,[In] const PALETTEENTRY* pSrcPalette,[In] const D3DBOX* pSrcBox)
public static void ToFile(Volume volume, string fileName, ImageFileFormat format, Box box)
{
ToFile(volume, fileName, format, box, null);
}
///
/// Saves a volume to a file on disk.
///
/// The volume.
/// Name of the file.
/// The format.
/// The box.
/// The palette.
/// A object describing the result of the operation.
/// HRESULT D3DXSaveVolumeToFileW([In] const wchar_t* pDestFile,[In] D3DXIMAGE_FILEFORMAT DestFormat,[In] IDirect3DVolume9* pSrcVolume,[In] const PALETTEENTRY* pSrcPalette,[In] const D3DBOX* pSrcBox)
public static void ToFile(Volume volume, string fileName, ImageFileFormat format, Box box, PaletteEntry[] palette)
{
unsafe
{
D3DX9.SaveVolumeToFileW(fileName, format, volume, palette, new IntPtr(&box));
}
}
///
/// Saves a volume to a .
///
/// The volume.
/// The format.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXSaveVolumeToFileInMemory([In] ID3DXBuffer** ppDestBuf,[In] D3DXIMAGE_FILEFORMAT DestFormat,[In] IDirect3DVolume9* pSrcVolume,[In, Buffer] const PALETTEENTRY* pSrcPalette,[In] const void* pSrcBox)
public static DataStream ToStream(Volume volume, ImageFileFormat format)
{
Blob blob;
D3DX9.SaveVolumeToFileInMemory(out blob, format, volume, null, IntPtr.Zero);
return new DataStream(blob);
}
///
/// Saves a volume to a .
///
/// The volume.
/// The format.
/// The box.
///
/// A object describing the result of the operation.
///
/// HRESULT D3DXSaveVolumeToFileInMemory([In] ID3DXBuffer** ppDestBuf,[In] D3DXIMAGE_FILEFORMAT DestFormat,[In] IDirect3DVolume9* pSrcVolume,[In, Buffer] const PALETTEENTRY* pSrcPalette,[In] const void* pSrcBox)
public static DataStream ToStream(Volume volume, ImageFileFormat format, Box box)
{
return ToStream(volume, format, box, null);
}
///
/// Saves a volume to a .
///
/// The volume.
/// The format.
/// The box.
/// The palette.
/// A object describing the result of the operation.
/// HRESULT D3DXSaveVolumeToFileInMemory([In] ID3DXBuffer** ppDestBuf,[In] D3DXIMAGE_FILEFORMAT DestFormat,[In] IDirect3DVolume9* pSrcVolume,[In, Buffer] const PALETTEENTRY* pSrcPalette,[In] const void* pSrcBox)
public static DataStream ToStream(Volume volume, ImageFileFormat format, Box box, PaletteEntry[] palette)
{
unsafe
{
Blob blob;
D3DX9.SaveVolumeToFileInMemory(out blob, format, volume, palette, new IntPtr(&box));
return new DataStream(blob);
}
}
}
}