public byte[] GenerateMips(STCompressionMode CompressionMode, int SurfaceLevel = 0) { Bitmap Image = BitmapExtension.GetBitmap(DecompressedData[SurfaceLevel], (int)TexWidth, (int)TexHeight); if (GammaFix) { Image = BitmapExtension.AdjustGamma(Image, 2.2f); } List <byte[]> mipmaps = new List <byte[]>(); for (int mipLevel = 0; mipLevel < MipCount; mipLevel++) { int MipWidth = Math.Max(1, (int)TexWidth >> mipLevel); int MipHeight = Math.Max(1, (int)TexHeight >> mipLevel); if (mipLevel != 0) { Image = BitmapExtension.Resize(Image, MipWidth, MipHeight); } mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image), Image.Width, Image.Height, TextureData.ConvertFormat(Format), alphaRef, CompressionMode)); } Image.Dispose(); return(Utils.CombineByteArray(mipmaps.ToArray())); }
public List <byte[]> GenerateMipList(STCompressionMode CompressionMode, bool multiThread, bool bc4Alpha, int SurfaceLevel = 0) { Bitmap Image = BitmapExtension.GetBitmap(DecompressedData[SurfaceLevel], (int)TexWidth, (int)TexHeight); if (GammaFix) { Image = BitmapExtension.AdjustGamma(Image, 2.2f); } if (bc4Alpha) { Image = BitmapExtension.SetChannel(Image, STChannelType.Alpha, STChannelType.Alpha, STChannelType.Alpha, STChannelType.Alpha); } List <byte[]> mipmaps = new List <byte[]>(); for (int mipLevel = 0; mipLevel < MipCount; mipLevel++) { int MipWidth = Math.Max(1, (int)TexWidth >> mipLevel); int MipHeight = Math.Max(1, (int)TexHeight >> mipLevel); if (mipLevel != 0) { Image = BitmapExtension.Resize(Image, MipWidth, MipHeight); } mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image), Image.Width, Image.Height, TextureData.ConvertFormat(Format), alphaRef, multiThread, CompressionMode)); } Image.Dispose(); return(mipmaps); }
public void Compress(STCompressionMode CompressionMode, bool multiThread) { DataBlockOutput.Clear(); foreach (var surface in DecompressedData) { DataBlockOutput.Add(STGenericTexture.CompressBlock(surface, (int)TexWidth, (int)TexHeight, Format, alphaRef, multiThread, CompressionMode)); } }
public void Compress(STCompressionMode CompressionMode) { DataBlockOutput.Clear(); foreach (var surface in DecompressedData) { DataBlockOutput.Add(STGenericTexture.CompressBlock(surface, (int)TexWidth, (int)TexHeight, TextureData.ConvertFormat(Format), alphaRef, CompressionMode)); } }
public List <byte[]> GenerateMipList(STCompressionMode CompressionMode, bool multiThread, int SurfaceLevel = 0) { Bitmap Image = BitmapExtension.GetBitmap(DecompressedData[SurfaceLevel], (int)TexWidth, (int)TexHeight); List <byte[]> mipmaps = new List <byte[]>(); for (int mipLevel = 0; mipLevel < MipCount; mipLevel++) { int MipWidth = Math.Max(1, (int)TexWidth >> mipLevel); int MipHeight = Math.Max(1, (int)TexHeight >> mipLevel); if (mipLevel != 0) { Image = BitmapExtension.Resize(Image, MipWidth, MipHeight); } mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image), Image.Width, Image.Height, Format, alphaRef, multiThread, CompressionMode)); } Image.Dispose(); return(mipmaps); }
public void SetupSettings(TextureImporterSettings setting) { if (setting.Format == SurfaceFormat.Invalid || SelectedIndex == -1) { return; } WidthLabel.Text = $"Width: {setting.TexWidth}"; HeightLabel.Text = $"Height: {setting.TexHeight}"; if (Thread != null && Thread.IsAlive) { Thread.Abort(); } if (formatComboBox.SelectedItem is SurfaceDim) { setting.SurfaceDim = (SurfaceDim)formatComboBox.SelectedItem; } if (formatComboBox.SelectedItem is SurfaceFormat) { setting.Format = (SurfaceFormat)formatComboBox.SelectedItem; listViewCustom1.Items[SelectedIndex].SubItems[1].Text = setting.Format.ToString(); } if (setting.Format == SurfaceFormat.BC7_UNORM || setting.Format == SurfaceFormat.BC7_SRGB) { compressionModeCB.Visible = true; compModeLbl.Visible = true; } else { compressionModeCB.Visible = false; compModeLbl.Visible = false; } Bitmap bitmap = Toolbox.Library.Imaging.GetLoadingImage(); if (compressionModeCB.SelectedIndex == 0) { CompressionMode = STCompressionMode.Fast; } else { CompressionMode = STCompressionMode.Normal; } Thread = new Thread((ThreadStart)(() => { setting.IsFinishedCompressing = false; ToggleOkButton(false); pictureBox1.Image = bitmap; var mips = setting.GenerateMipList(CompressionMode); setting.DataBlockOutput.Clear(); setting.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray())); ToggleOkButton(true); setting.IsFinishedCompressing = true; if (setting.DataBlockOutput.Count > 0) { if (setting.Format == SurfaceFormat.BC5_SNORM) { bitmap = DDSCompressor.DecompressBC5(mips[0], (int)setting.TexWidth, (int)setting.TexHeight, true); } else { bitmap = STGenericTexture.DecodeBlockGetBitmap(mips[0], setting.TexWidth, setting.TexHeight, TextureData.ConvertFormat(setting.Format), new byte[0]); } } if (pictureBox1.InvokeRequired) { pictureBox1.Invoke((MethodInvoker) delegate { pictureBox1.Image = bitmap; pictureBox1.Refresh(); int size = Utils.GetSizeInBytes(mips); dataSizeLbl.Text = $"Data Size: {STMath.GetFileSize(size, 5)}"; }); } mips.Clear(); })); Thread.Start(); }
public void SetupSettings() { if (SelectedTexSettings.Format == TEX_FORMAT.UNKNOWN || SelectedIndex == -1) { return; } WidthLabel.Text = $"Width {SelectedTexSettings.TexWidth}"; HeightLabel.Text = $"Height {SelectedTexSettings.TexHeight}"; if (Thread != null && Thread.IsAlive) { Thread.Abort(); } if (formatComboBox.SelectedItem is TEX_FORMAT) { SelectedTexSettings.Format = (TEX_FORMAT)formatComboBox.SelectedItem; listViewCustom1.Items[SelectedIndex].SubItems[1].Text = SelectedTexSettings.Format.ToString(); } if (SelectedTexSettings.Format == TEX_FORMAT.BC7_UNORM || SelectedTexSettings.Format == TEX_FORMAT.BC7_TYPELESS || SelectedTexSettings.Format == TEX_FORMAT.BC7_UNORM_SRGB) { compressionModeCB.Visible = true; } else { compressionModeCB.Visible = false; } Bitmap bitmap = Toolbox.Library.Imaging.GetLoadingImage(); if (compressionModeCB.SelectedIndex == 0) { CompressionMode = STCompressionMode.Fast; } else { CompressionMode = STCompressionMode.Normal; } Thread = new Thread((ThreadStart)(() => { SelectedTexSettings.IsFinishedCompressing = false; ToggleOkButton(false); pictureBox1.Image = bitmap; var mips = SelectedTexSettings.GenerateMipList(CompressionMode, MultiThreading); SelectedTexSettings.DataBlockOutput.Clear(); SelectedTexSettings.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray())); ToggleOkButton(true); SelectedTexSettings.IsFinishedCompressing = true; if (SelectedTexSettings.DataBlockOutput.Count > 0) { if (SelectedTexSettings.Format == TEX_FORMAT.BC5_SNORM) { bitmap = DDSCompressor.DecompressBC5(mips[0], (int)SelectedTexSettings.TexWidth, (int)SelectedTexSettings.TexHeight, true); } else { bitmap = STGenericTexture.DecodeBlockGetBitmap(mips[0], SelectedTexSettings.TexWidth, SelectedTexSettings.TexHeight, SelectedTexSettings.Format, new byte[0]); } } if (pictureBox1.InvokeRequired) { pictureBox1.Invoke((MethodInvoker) delegate { pictureBox1.Image = bitmap; pictureBox1.Refresh(); int size = Utils.GetSizeInBytes(mips); dataSizeLbl.Text = $"Data Size: {STMath.GetFileSize(size, 5)}"; }); } mips.Clear(); })); Thread.Start(); }
public static unsafe byte[] CompressBlock(Byte[] data, int width, int height, DDS.DXGI_FORMAT format, bool multiThread, float AlphaRef = 0.5f, STCompressionMode CompressionMode = STCompressionMode.Normal) { long inputRowPitch = width * 4; long inputSlicePitch = width * height * 4; if (data.Length == inputSlicePitch) { byte *buf; buf = (byte *)Marshal.AllocHGlobal((int)inputSlicePitch); Marshal.Copy(data, 0, (IntPtr)buf, (int)inputSlicePitch); DirectXTexNet.Image inputImage = new DirectXTexNet.Image( width, height, DXGI_FORMAT.R8G8B8A8_UNORM, inputRowPitch, inputSlicePitch, (IntPtr)buf, null); TexMetadata texMetadata = new TexMetadata(width, height, 1, 1, 1, 0, 0, DXGI_FORMAT.R8G8B8A8_UNORM, TEX_DIMENSION.TEXTURE2D); ScratchImage scratchImage = TexHelper.Instance.InitializeTemporary( new DirectXTexNet.Image[] { inputImage }, texMetadata, null); var compFlags = TEX_COMPRESS_FLAGS.DEFAULT; if (multiThread) { compFlags |= TEX_COMPRESS_FLAGS.PARALLEL; } if (format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM || format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM_SRGB || format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC7_TYPELESS) { if (CompressionMode == STCompressionMode.Fast) { compFlags |= TEX_COMPRESS_FLAGS.BC7_QUICK; } } if (format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM_SRGB || format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM_SRGB || format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC2_UNORM_SRGB || format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC3_UNORM_SRGB || format == DDS.DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM_SRGB) { compFlags |= TEX_COMPRESS_FLAGS.SRGB; } using (var comp = scratchImage.Compress((DXGI_FORMAT)format, compFlags, 0.5f)) { long outRowPitch; long outSlicePitch; TexHelper.Instance.ComputePitch((DXGI_FORMAT)format, width, height, out outRowPitch, out outSlicePitch, CP_FLAGS.NONE); byte[] result = new byte[outSlicePitch]; Marshal.Copy(comp.GetImage(0).Pixels, result, 0, result.Length); inputImage = null; scratchImage.Dispose(); return(result); } } return(null); }
public static byte[] CompressBlock(byte[] data, int width, int height, TEX_FORMAT format, float alphaRef, STCompressionMode CompressionMode = STCompressionMode.Fast) { if (IsCompressed(format)) { return(DDSCompressor.CompressBlock(data, width, height, (DDS.DXGI_FORMAT)format, alphaRef, CompressionMode)); } else if (IsAtscFormat(format)) { return(null); } else { return(DDSCompressor.EncodePixelBlock(data, width, height, (DDS.DXGI_FORMAT)format)); } }
public static byte[] GenerateMipsAndCompress(Bitmap bitmap, uint MipCount, TEX_FORMAT Format, float alphaRef = 0.5f, STCompressionMode CompressionMode = STCompressionMode.Fast) { byte[] DecompressedData = BitmapExtension.ImageToByte(bitmap); DecompressedData = ConvertBgraToRgba(DecompressedData); Bitmap Image = BitmapExtension.GetBitmap(DecompressedData, bitmap.Width, bitmap.Height); List <byte[]> mipmaps = new List <byte[]>(); for (int mipLevel = 0; mipLevel < MipCount; mipLevel++) { int width = Math.Max(1, bitmap.Width >> mipLevel); int height = Math.Max(1, bitmap.Height >> mipLevel); Image = BitmapExtension.Resize(Image, width, height); mipmaps.Add(STGenericTexture.CompressBlock(BitmapExtension.ImageToByte(Image), Image.Width, Image.Height, Format, alphaRef, CompressionMode)); } Image.Dispose(); return(Utils.CombineByteArray(mipmaps.ToArray())); }
public void SetupSettings() { if (SelectedTexSettings.Format == SurfaceFormat.Invalid || SelectedIndex == -1) { return; } WidthLabel.Text = $"Width {SelectedTexSettings.TexWidth}"; HeightLabel.Text = $"Height {SelectedTexSettings.TexHeight}"; if (Thread != null && Thread.IsAlive) { Thread.Abort(); } if (formatComboBox.SelectedItem is SurfaceFormat) { SelectedTexSettings.Format = (SurfaceFormat)formatComboBox.SelectedItem; listViewCustom1.Items[SelectedIndex].SubItems[1].Text = SelectedTexSettings.Format.ToString(); } if (SelectedTexSettings.Format == SurfaceFormat.BC7_UNORM || SelectedTexSettings.Format == SurfaceFormat.BC7_SRGB) { compressionModeCB.Visible = true; } else { compressionModeCB.Visible = false; } Bitmap bitmap = Switch_Toolbox.Library.Imaging.GetLoadingImage(); if (compressionModeCB.SelectedIndex == 0) { CompressionMode = STCompressionMode.Fast; } else { CompressionMode = STCompressionMode.Normal; } Thread = new Thread((ThreadStart)(() => { SelectedTexSettings.IsFinishedCompressing = false; ToggleOkButton(false); pictureBox1.Image = bitmap; var mips = SelectedTexSettings.GenerateMipList(CompressionMode); SelectedTexSettings.DataBlockOutput.Clear(); SelectedTexSettings.DataBlockOutput.Add(Utils.CombineByteArray(mips.ToArray())); ToggleOkButton(true); SelectedTexSettings.IsFinishedCompressing = true; if (SelectedTexSettings.DataBlockOutput.Count > 0) { if (SelectedTexSettings.Format == SurfaceFormat.BC5_SNORM) { bitmap = DDSCompressor.DecompressBC5(mips[0], (int)SelectedTexSettings.TexWidth, (int)SelectedTexSettings.TexHeight, true); } else { bitmap = STGenericTexture.DecodeBlockGetBitmap(mips[0], SelectedTexSettings.TexWidth, SelectedTexSettings.TexHeight, TextureData.ConvertFormat(SelectedTexSettings.Format)); } } mips.Clear(); if (pictureBox1.InvokeRequired) { pictureBox1.Invoke((MethodInvoker) delegate { pictureBox1.Image = bitmap; pictureBox1.Refresh(); }); } })); Thread.Start(); }