public static bool ConvertFromDdsAndSave(Stream ms, string outfilename, ESaveFileTypes filetype, bool vflip = false, bool hflip = false) { byte[] rentedBuffer = null; try { var offset = 0; var len = checked ((int)ms.Length); rentedBuffer = ArrayPool <byte> .Shared.Rent(len); int readBytes; while (offset < len && (readBytes = ms.Read(rentedBuffer, offset, len - offset)) > 0) { offset += readBytes; } var outDir = new FileInfo(outfilename).Directory.FullName; Directory.CreateDirectory(outDir); var fileName = Path.GetFileNameWithoutExtension(outfilename); var extension = filetype.ToString().ToLower(); var newpath = Path.Combine(outDir, $"{fileName}.{extension}"); //TexconvNative.ConvertAndSaveDdsImage(rentedBuffer, newpath, filetype, vflip, hflip); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var buffer = Array.Empty <byte>(); using (var blob = new ManagedBlob()) { var l = TexconvNative.ConvertFromDds(rentedBuffer, blob.GetBlob(), filetype, vflip, hflip); buffer = blob.GetBytes(); } File.WriteAllBytes(newpath, buffer); } else { throw new NotImplementedException(); } } finally { if (rentedBuffer is object) { ArrayPool <byte> .Shared.Return(rentedBuffer); } } return(true); }
/// <summary> /// Converts texture stream with given extension stream to dds /// </summary> public static byte[] ConvertToDds(Stream stream, EUncookExtension inExtension, DXGI_FORMAT?outFormat = null, bool vflip = false, bool hflip = false) { if (inExtension == EUncookExtension.dds) { throw new NotSupportedException("texture to convert to dds must not be dds iteslf"); } byte[] rentedBuffer = null; try { var offset = 0; var len = checked ((int)stream.Length); rentedBuffer = ArrayPool <byte> .Shared.Rent(len); int readBytes; while (offset < len && (readBytes = stream.Read(rentedBuffer, offset, len - offset)) > 0) { offset += readBytes; } var format = outFormat ?? DXGI_FORMAT.DXGI_FORMAT_UNKNOWN; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var buffer = Array.Empty <byte>(); using (var blob = new ManagedBlob()) { var l = TexconvNative.ConvertToDds(rentedBuffer, blob.GetBlob(), ToSaveFormat(inExtension), format, vflip, hflip); buffer = blob.GetBytes(); } return(buffer); } else { throw new NotImplementedException(); } } finally { if (rentedBuffer is object) { ArrayPool <byte> .Shared.Return(rentedBuffer); } } }