public void GetPreview() { var ddsReadSuccess = false; try { Preview = null; BCnEncoder.Shared.ImageFiles.DdsFile bcDds; using (var ms = new MemoryStream()) { var dds = Texture.ToDdsFile(false); dds.Write(ms, -1); ddsReadSuccess = true; ms.Seek(0, SeekOrigin.Begin); bcDds = BCnEncoder.Shared.ImageFiles.DdsFile.Load(ms); } var width = (int)bcDds.header.dwWidth; var height = (int)bcDds.header.dwHeight; var decoder = new BcDecoder(); if (decoder.IsHdrFormat(bcDds)) { var pixels = new byte[width * height * 3]; var pixelsSpan = MemoryMarshal.Cast <byte, Bgr24>(pixels); decoder.DecodeDdsToPixels(bcDds, pixelsSpan); var bmSource = BitmapSource.Create(width, height, 96.0, 96.0, PixelFormats.Bgr24, null, pixels, width * 3); Preview = bmSource; } else { var pixels = new byte[width * height * 4]; var pixelsSpan = MemoryMarshal.Cast <byte, Bgra32>(pixels); decoder.DecodeDdsToPixels(bcDds, pixelsSpan); var bmSource = BitmapSource.Create(width, height, 96.0, 96.0, PixelFormats.Bgra32, null, pixels, width * 4); Preview = bmSource; } PreviewErrorVisibility = Visibility.Collapsed; OnPropertyChanged(nameof(Width)); OnPropertyChanged(nameof(Height)); OnPropertyChanged(nameof(TextureInfo)); } catch (Exception ex) { Preview = null; if (ddsReadSuccess) { PreviewError = "Could not create preview! Export/Import may still work." + Environment.NewLine + Environment.NewLine + ex.Message; } else { PreviewError = "Could not create preview! Failed to convert pssg texture to dds." + Environment.NewLine + Environment.NewLine + ex.Message; } PreviewErrorVisibility = Visibility.Visible; } }
public static void DecodeDdsToPixels <TPixel>(this BcDecoder decoder, DdsFile dds, Span <TPixel> pixels) where TPixel : unmanaged, IPixel <TPixel> { var width = (int)dds.header.dwWidth; var height = (int)dds.header.dwHeight; if (decoder.IsHdrFormat(dds)) { var source = decoder.DecodeHdr(dds).AsSpan(); for (var r = 0; r < height; ++r) { var start = r * width; var destRow = pixels.Slice(start, width); var sorcRow = source.Slice(start, width); for (var c = 0; c < destRow.Length; ++c) { ref var destPixel = ref destRow[c]; ref var sorcPixel = ref sorcRow[c]; var rgbVal = sorcPixel.ToVector3(); destPixel.FromScaledVector4(new Vector4(rgbVal, 1)); } }