// 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; namespace SharpDX.Direct3D9 { public partial struct ImageInformation { /// /// Retrieves information about a given image file on the disk. /// /// The filename. /// A structure /// HRESULT D3DXGetImageInfoFromFileInMemory([In] const void* pSrcData,[In] unsigned int SrcDataSize,[Out] D3DXIMAGE_INFO* pSrcInfo) public static ImageInformation FromFile(string fileName) { return FromMemory(File.ReadAllBytes(fileName)); } /// /// Retrieves information about a given image file in memory. /// /// The memory. /// A structure /// HRESULT D3DXGetImageInfoFromFileInMemory([In] const void* pSrcData,[In] unsigned int SrcDataSize,[Out] D3DXIMAGE_INFO* pSrcInfo) public static ImageInformation FromMemory(byte[] memory) { unsafe { fixed (void* pMemory = memory) return D3DX9.GetImageInfoFromFileInMemory((IntPtr)pMemory, memory.Length); } } /// /// Retrieves information about a given image file from a stream. /// /// The stream. /// A structure /// This method keeps the position of the stream /// HRESULT D3DXGetImageInfoFromFileInMemory([In] const void* pSrcData,[In] unsigned int SrcDataSize,[Out] D3DXIMAGE_INFO* pSrcInfo) public static ImageInformation FromStream(Stream stream) { return FromStream(stream, true); } /// /// Retrieves information about a given image file from a stream. /// /// The stream. /// if set to true preserve the stream position; false will move the stream pointer. /// A structure /// HRESULT D3DXGetImageInfoFromFileInMemory([In] const void* pSrcData,[In] unsigned int SrcDataSize,[Out] D3DXIMAGE_INFO* pSrcInfo) public static ImageInformation FromStream(Stream stream, bool keepPosition) { long savedPosition = 0; if (keepPosition) savedPosition = stream.Position; ImageInformation result; if (stream is DataStream) { var dataStream = ((DataStream)stream); result = D3DX9.GetImageInfoFromFileInMemory(dataStream.PositionPointer, (int)(dataStream.Length - dataStream.Position)); dataStream.Position = dataStream.Length; } else { var buffer = Utilities.ReadStream(stream); result = FromMemory(buffer); } if (keepPosition) stream.Position = savedPosition; return result; } } }