public static IUncompressedImage Decompress(ICompressedImage dds) { using (Profiler.MeasureTime()) { return(new BlockCompressor().Decompress(dds)); } }
public IUncompressedImage Decompress(ICompressedImage image) { Logger.Default.Log("Decompressing DDS to BMP."); var bmp = new DirectBitmap(image.Width, image.Height); int numberOfVerticalBlocks = image.Height / BlockFormat.Dimension; int numberOfHorizontalBlocks = image.Width / BlockFormat.Dimension; int numberOfBlocks = numberOfVerticalBlocks * numberOfHorizontalBlocks; Parallel.For(0, numberOfBlocks, #if DEBUG RunInSingleThread, #endif (i) => { var blockIndex = PointUtility.FromRowMajor(i, numberOfHorizontalBlocks); var blockData = image.GetBlockData(blockIndex); var blockColors = image.CompressionFormat.Decompress(blockData); bmp.SetBlockColors(blockIndex, blockColors); }); Logger.Default.Log("Decompression successful."); return(bmp); }
/// <summary> /// Writes the main image data stored in the specified image into a DDS file. /// </summary> public unsafe void Write(ICompressedImage image) { WriteMagicNumber(); var header = CreateHeader(image.Width, image.Height, image.CompressionFormat.FourCC.Value); WriteHeader(header); WriteSurfaceData(image.GetBuffer()); }
private void Compress() { if (VQCompression) { VqCompress(); } else { _compressStopWath = new Stopwatch(); _fileName = _openDialog.FileName; OnPropertyChanged("SourceImage"); var bitmap = new Bitmap(_fileName); _compressStopWath.Start(); bitmap = ApplyHaarTransform(true, false, CodeBookSizePow, bitmap); var rle = new RLE(bitmap); var imgCompr = new DwtImage { Height = bitmap.Height, Width = bitmap.Width, Image = rle.CompressImage() }; _compressedData = imgCompr.ToByteArray(); _compressedData = HuffmanEncoder.Encode(_compressedData); byte[] result; using (var resultStream = new MemoryStream()) { using (var compressionStream = new DeflateStream(resultStream, CompressionLevel.Optimal)) { compressionStream.Write(_compressedData, 0, _compressedData.Length); } result = resultStream.ToArray(); } _compressedData = result; _compressStopWath.Stop(); _compressed = imgCompr; } Wind.Dispatcher.Invoke(Decompress); Wind.Dispatcher.Invoke(SetResults); }
private void VqCompress() { _compressStopWath = new Stopwatch(); _fileName = _openDialog.FileName; OnPropertyChanged("SourceImage"); var bitmap = new Bitmap(_fileName); var k = 0; var colors = new List<Color>(); for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { var color = bitmap.GetPixel(i, j); colors.Add(color); k++; } } var image = colors.GroupBy(x => x).AsParallel() .Select(x => new[] { (double)x.Key.R, (double)x.Key.B, (double)x.Key.G }) .AsParallel() .ToArray(); _vq = new VectorQuantization(image, 3, (int)Math.Pow(2, CodeBookSizePow)); _compressed = new VqImage { Height = bitmap.Height, Width = bitmap.Width, CodeBook = _vq.CodeBook.Select( x => new[] { (byte) Math.Round(x[0]), (byte) Math.Round(x[1]), (byte) Math.Round(x[2]) }).ToArray() }; k = 0; var img = new byte[bitmap.Width * bitmap.Height]; for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { var color = bitmap.GetPixel(i, j); var pos = _vq.QuantazationIndex(new[] { (double)color.R, color.B, color.G }); img[k] = (byte)pos; k++; } } var rle = new RLE(img); var imgCompr = ((VqImage) _compressed); imgCompr.Image = rle.Compress(); _compressedData = imgCompr.ToByteArray(); _compressedData = HuffmanEncoder.Encode(_compressedData); byte[] result; using (var resultStream = new MemoryStream()) { using (var compressionStream = new DeflateStream(resultStream, CompressionLevel.Optimal)) { compressionStream.Write(_compressedData, 0, _compressedData.Length); } result = resultStream.ToArray(); } _compressedData = result; _compressStopWath.Stop(); }
private void Compress() { if(VQCompression) VqCompress(); else { _compressStopWath = new Stopwatch(); _fileName = _openDialog.FileName; OnPropertyChanged("SourceImage"); var bitmap = new Bitmap(_fileName); _compressStopWath.Start(); bitmap = ApplyHaarTransform(true, false, CodeBookSizePow, bitmap); var rle = new RLE(bitmap); var imgCompr = new DwtImage {Height = bitmap.Height, Width = bitmap.Width, Image = rle.CompressImage()}; _compressedData = imgCompr.ToByteArray(); _compressedData = HuffmanEncoder.Encode(_compressedData); byte[] result; using (var resultStream = new MemoryStream()) { using (var compressionStream = new DeflateStream(resultStream, CompressionLevel.Optimal)) { compressionStream.Write(_compressedData, 0, _compressedData.Length); } result = resultStream.ToArray(); } _compressedData = result; _compressStopWath.Stop(); _compressed = imgCompr; } Wind.Dispatcher.Invoke(Decompress); Wind.Dispatcher.Invoke(SetResults); }
private void VqCompress() { _compressStopWath = new Stopwatch(); _fileName = _openDialog.FileName; OnPropertyChanged("SourceImage"); var bitmap = new Bitmap(_fileName); var k = 0; var colors = new List <Color>(); for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { var color = bitmap.GetPixel(i, j); colors.Add(color); k++; } } var image = colors.GroupBy(x => x).AsParallel() .Select(x => new[] { (double)x.Key.R, (double)x.Key.B, (double)x.Key.G }) .AsParallel() .ToArray(); _vq = new VectorQuantization(image, 3, (int)Math.Pow(2, CodeBookSizePow)); _compressed = new VqImage { Height = bitmap.Height, Width = bitmap.Width, CodeBook = _vq.CodeBook.Select( x => new[] { (byte)Math.Round(x[0]), (byte)Math.Round(x[1]), (byte)Math.Round(x[2]) }).ToArray() }; k = 0; var img = new byte[bitmap.Width * bitmap.Height]; for (int i = 0; i < bitmap.Width; i++) { for (int j = 0; j < bitmap.Height; j++) { var color = bitmap.GetPixel(i, j); var pos = _vq.QuantazationIndex(new[] { (double)color.R, color.B, color.G }); img[k] = (byte)pos; k++; } } var rle = new RLE(img); var imgCompr = ((VqImage)_compressed); imgCompr.Image = rle.Compress(); _compressedData = imgCompr.ToByteArray(); _compressedData = HuffmanEncoder.Encode(_compressedData); byte[] result; using (var resultStream = new MemoryStream()) { using (var compressionStream = new DeflateStream(resultStream, CompressionLevel.Optimal)) { compressionStream.Write(_compressedData, 0, _compressedData.Length); } result = resultStream.ToArray(); } _compressedData = result; _compressStopWath.Stop(); }