private static Bitmap ReadDDS(string filePath) { SixLabors.ImageSharp.Image <Rgba32> image = null; using FileStream fs = File.OpenRead(filePath); image = bcDecoder.Decode(fs); return(Utility.ToBitmap(image)); }
public static float DecodeCheckPSNR(string filename, Image <Rgba32> original) { using FileStream fs = File.OpenRead(filename); var ktx = KtxFile.Load(fs); var decoder = new BcDecoder(); using var img = decoder.Decode(ktx); var pixels = original.GetPixelSpan(); var pixels2 = img.GetPixelSpan(); return(ImageQuality.PeakSignalToNoiseRatio(pixels, pixels2, true)); }
public void Bc4Decode() { using FileStream fs = File.OpenRead(@"../../../testImages/test_decompress_bc4_unorm.ktx"); KtxFile file = KtxFile.Load(fs); Assert.True(file.Header.VerifyHeader()); Assert.Equal((uint)1, file.Header.NumberOfFaces); BcDecoder decoder = new BcDecoder(); using var image = decoder.Decode(file); Assert.Equal((uint)image.Width, file.Header.PixelWidth); Assert.Equal((uint)image.Height, file.Header.PixelHeight); using FileStream outFs = File.OpenWrite("decoding_test_bc4.png"); image.SaveAsPng(outFs); }
public void ReadBc1a() { using FileStream fs = File.OpenRead(@"../../../testImages/test_decompress_bc1a.dds"); DdsFile file = DdsFile.Load(fs); Assert.Equal(DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM, file.Header.ddsPixelFormat.DxgiFormat); Assert.Equal(file.Header.dwMipMapCount, (uint)file.Faces[0].MipMaps.Length); BcDecoder decoder = new BcDecoder(); decoder.InputOptions.ddsBc1ExpectAlpha = true; var image = decoder.Decode(file); Assert.Equal((uint)image.Width, file.Header.dwWidth); Assert.Equal((uint)image.Height, file.Header.dwHeight); Assert.Contains(image.GetPixelSpan().ToArray(), x => x.A == 0); using FileStream outFs = File.OpenWrite($"decoding_test_dds_bc1a.png"); image.SaveAsPng(outFs); image.Dispose(); }
static async Task <MemoryStream> getDDS(Stream stream) { if (stream.CanSeek) { stream.Seek(0, SeekOrigin.Begin); } var exceptions = new List <Exception>(); MemoryStream ms = null; try { ms = new MemoryStream(); var file = DdsFile.Load(stream); var decoder = new BcDecoder(); var image = decoder.Decode(file); await image.SaveAsPngAsync(ms); } catch (Exception ex) { if (ms != null) { ms.Close(); await ms.DisposeAsync(); } ms = null; exceptions.Add(ex); } // fallback if (ms == null) { if (stream.CanSeek) { stream.Seek(0, SeekOrigin.Begin); } try { using var pfimImage = Dds.Create(stream, new PfimConfig()); if (pfimImage.Compressed) { pfimImage.Decompress(); } if (pfimImage.Format == ImageFormat.Rgba32) { ms = new MemoryStream(); using var image = Image.LoadPixelData <Bgra32>(tightData(pfimImage), pfimImage.Width, pfimImage.Height); await image.SaveAsPngAsync(ms); } else if (pfimImage.Format == ImageFormat.Rgb24) { ms = new MemoryStream(); using var image = Image.LoadPixelData <Bgr24>(tightData(pfimImage), pfimImage.Width, pfimImage.Height); await image.SaveAsPngAsync(ms); } } catch (Exception ex) { if (ms != null) { ms.Close(); await ms.DisposeAsync(); } ms = null; exceptions.Add(ex); } } // Fallback can result in memory stream being empty so throw aggregate exception only if both attempts failed if (ms == null && exceptions.Count == 2) { throw new AggregateException(exceptions); } return(ms); }