private static unsafe void LoadDdsFile(Stream stream, ref DDSLoadInfo info) { byte[] buffer = new byte[stream.Length]; // stream.ProperRead(buffer, 0, buffer.Length); stream.Read(buffer, 0, buffer.Length); int hr; fixed (byte* pBytes = buffer) { hr = DdsIO_x86.Load(pBytes, new UIntPtr((ulong)buffer.Length), ref info); } if (FAILED(hr)) { switch (hr) { case HResult.InvalidData: throw new FormatException("The DDS file is invalid."); case HResult.NotSupported: throw new FormatException("The file is not a supported DDS format."); default: Marshal.ThrowExceptionForHR(hr); break; } } }
private static void FreeLoadInfo(ref DDSLoadInfo info) { if (IntPtr.Size == 8) { DdsIO_x64.FreeLoadInfo(ref info); } else { DdsIO_x86.FreeLoadInfo(ref info); } }
public static unsafe DdsImage Load(Stream stream) { StreamIOCallbacks streamIO = new StreamIOCallbacks(stream); IOCallbacks callbacks = new IOCallbacks { Read = streamIO.Read, Write = streamIO.Write, Seek = streamIO.Seek, GetSize = streamIO.GetSize }; int hr; DDSLoadInfo info = new DDSLoadInfo(); if (IntPtr.Size == 8) { hr = DdsIO_x64.Load(callbacks, ref info); } else { hr = DdsIO_x86.Load(callbacks, ref info); } GC.KeepAlive(streamIO); GC.KeepAlive(callbacks); if (FAILED(hr)) { if (streamIO.CallbackExceptionInfo != null) { streamIO.CallbackExceptionInfo.Throw(); } else { switch (hr) { case HResult.InvalidData: throw new FormatException("The DDS file is invalid."); case HResult.NotSupported: throw new FormatException("The file is not a supported DDS format."); default: Marshal.ThrowExceptionForHR(hr); break; } } } return(DdsImageFactory(info)); }
public static unsafe System.Drawing.Bitmap Load(Stream input) { DDSLoadInfo info = new DDSLoadInfo(); LoadDdsFile(input, ref info); if (info == default(DDSLoadInfo)) { return null; } var bitmap = new System.Drawing.Bitmap(info.width, info.height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var rect = new System.Drawing.Rectangle(0, 0, info.width, info.height); var data = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, bitmap.PixelFormat); try { for (int y = 0; y < info.height; y++) { byte* src = (byte*)info.scan0 + (y * info.stride); BGRA* dst = data.GetRowAddressUnchecked(y); for (int x = 0; x < info.width; x++) { dst->R = src[0]; dst->G = src[1]; dst->B = src[2]; dst->A = src[3]; src += 4; dst++; } } } finally { Free(ref info); } bitmap.UnlockBits(data); return bitmap; }
public static unsafe Document Load(Stream input) { DDSLoadInfo info = new DDSLoadInfo(); LoadDdsFile(input, ref info); Document doc = null; try { doc = new Document(info.width, info.height); BitmapLayer layer = Layer.CreateBackgroundLayer(info.width, info.height); Surface surface = layer.Surface; for (int y = 0; y < surface.Height; y++) { byte * src = (byte *)info.scan0 + (y * info.stride); ColorBgra *dst = surface.GetRowAddressUnchecked(y); for (int x = 0; x < surface.Width; x++) { dst->R = src[0]; dst->G = src[1]; dst->B = src[2]; dst->A = src[3]; src += 4; dst++; } } doc.Layers.Add(layer); } finally { FreeLoadInfo(ref info); } return(doc); }
private static void LoadDdsFile(Stream stream, ref DDSLoadInfo info) { StreamIOCallbacks streamIO = new StreamIOCallbacks(stream); IOCallbacks callbacks = new IOCallbacks { Read = streamIO.Read, Write = streamIO.Write, Seek = streamIO.Seek, GetSize = streamIO.GetSize }; int hr; if (IntPtr.Size == 8) { hr = DdsIO_x64.Load(callbacks, ref info); } else { hr = DdsIO_x86.Load(callbacks, ref info); } GC.KeepAlive(streamIO); GC.KeepAlive(callbacks); if (FAILED(hr)) { switch (hr) { case HResult.InvalidData: throw new FormatException("The DDS file is invalid."); case HResult.NotSupported: throw new FormatException("The file is not a supported DDS format."); default: Marshal.ThrowExceptionForHR(hr); break; } } }
public static unsafe DdsImage Load(Stream stream) { StreamIOCallbacks streamIO = new StreamIOCallbacks(stream); IOCallbacks callbacks = new IOCallbacks { Read = streamIO.Read, Write = streamIO.Write, Seek = streamIO.Seek, GetSize = streamIO.GetSize }; int hr; DDSLoadInfo info = new DDSLoadInfo(); #if NET47 if (IntPtr.Size == 8) #else if (RuntimeInformation.ProcessArchitecture == Architecture.X64) #endif { hr = DdsIO_x64.Load(callbacks, ref info); } #if NET47 else if (IntPtr.Size == 4) #else else if (RuntimeInformation.ProcessArchitecture == Architecture.X86) #endif { hr = DdsIO_x86.Load(callbacks, ref info); } else { throw new PlatformNotSupportedException(); } GC.KeepAlive(streamIO); GC.KeepAlive(callbacks); if (FAILED(hr)) { if (streamIO.CallbackExceptionInfo != null) { streamIO.CallbackExceptionInfo.Throw(); } else { switch (hr) { case HResult.InvalidData: throw new FormatException("The DDS file is invalid."); case HResult.NotSupported: throw new FormatException("The file is not a supported DDS format."); default: Marshal.ThrowExceptionForHR(hr); break; } } } return(new DdsImage(info)); }
internal static extern void Free([In, Out] ref DDSLoadInfo info);
internal static unsafe extern int Load([In] byte* input, [In] UIntPtr inputSize, [In, Out] ref DDSLoadInfo info);
private static void Free(ref DDSLoadInfo info) { DdsIO_x86.Free(ref info); }
private static DdsImage CreateImage(DDSLoadInfo info) { return(new DdsImage(info)); }
private DdsImage(DDSLoadInfo info) { this.info = info; }
internal static extern int Load([In] IOCallbacks callbacks, [In, Out] ref DDSLoadInfo info);