/// <summary> /// Inverse application of the algorithm /// </summary> /// <param name="parCompressedImage">Compressed image stream</param> /// <param name="parArguments">List of an arguments (ImageFormat finalFormat)</param> /// <returns>Decompressed image stream</returns> public override Stream DecompressImage(Stream parCompressedImage, List <object> parArguments) { GZIP gzip = GZIP.GetInstance(); Stream decompressedWavelet = gzip.DecompressImage(parCompressedImage, new List <object>()); Bitmap wavelet = new Bitmap(decompressedWavelet); int levels = (int)parArguments[0]; HaarWavelet.ApplyTransform(ref wavelet, false, levels); Stream result = new MemoryStream(); ImageFormat format = (ImageFormat)parArguments[1]; wavelet.Save(result, format); return(result); }
/// <summary> /// Inverse application of the algorithm /// </summary> /// <param name="parCompressedImage">Compressed image stream</param> /// <param name="parArguments">List of an arguments (ImageFormat finalFormat)</param> /// <returns>Decompressed image stream</returns> public override Stream DecompressImage(Stream parCompressedImage, List <object> parArguments) { GZIP gzip = GZIP.GetInstance(); Stream interlacedImage = gzip.DecompressImage(parCompressedImage, new List <object>()); interlacedImage.Position = interlacedImage.Length - 1; bool lastColumnIsOdd = interlacedImage.ReadByte() > 0; interlacedImage.SetLength(interlacedImage.Length - 1); Bitmap interlaced = new Bitmap(interlacedImage); int interlacedWidth = interlaced.Width; int interlacedHeight = interlaced.Height; int reconstructedWidth = interlacedWidth * 2; int reconstructedHeight = interlacedHeight; if (lastColumnIsOdd) { reconstructedWidth--; } Bitmap reconstructed = new Bitmap(reconstructedWidth, reconstructedHeight, interlaced.PixelFormat); BitmapData bdInterlaced = interlaced.LockBits(new Rectangle(0, 0, interlacedWidth, interlacedHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int[] bitsInterlaced = new int[interlacedWidth * interlacedHeight]; Marshal.Copy(bdInterlaced.Scan0, bitsInterlaced, 0, bitsInterlaced.Length); // --/-- BitmapData bdReconstructed = reconstructed.LockBits(new Rectangle(0, 0, reconstructedWidth, reconstructedHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int[] bitsReconstructed = new int[reconstructedWidth * reconstructedHeight]; Marshal.Copy(bdReconstructed.Scan0, bitsReconstructed, 0, bitsReconstructed.Length); for (int i = 0; i < interlacedHeight; i += 2) { for (int j = 0; j < interlacedWidth; j++) { bitsReconstructed[i * reconstructedWidth + j * 2] = bitsInterlaced[i * interlacedWidth + j]; if ((j * 2 + 1) < reconstructedWidth) { bitsReconstructed[(i + 1) * reconstructedWidth + j * 2 + 1] = bitsInterlaced[(i + 1) * interlacedWidth + j]; } } } for (int i = 0; i < interlacedHeight; i++) { int offset = (i + 1) % 2; for (int j = offset; j < reconstructedWidth; j += 2) { List <int> pixels = new List <int>(); if (i > 0) { pixels.Add(bitsReconstructed[(i - 1) * reconstructedWidth + j]); } if ((i + 1) < interlacedHeight) { pixels.Add(bitsReconstructed[(i + 1) * reconstructedWidth + j]); } if (j > 0) { pixels.Add(bitsReconstructed[i * reconstructedWidth + j - 1]); } if ((j + 1) < reconstructedWidth) { pixels.Add(bitsReconstructed[i * reconstructedWidth + j + 1]); } bitsReconstructed[i * reconstructedWidth + j] = FindAverageColor(pixels); } } Marshal.Copy(bitsReconstructed, 0, bdReconstructed.Scan0, bitsReconstructed.Length); interlaced.UnlockBits(bdInterlaced); reconstructed.UnlockBits(bdReconstructed); Stream reconstructedImage = new MemoryStream(); ImageFormat format = (ImageFormat)parArguments[0]; reconstructed.Save(reconstructedImage, format); return(reconstructedImage); }
/// <summary> /// Inverse application of the algorithm /// </summary> /// <param name="parCompressedImage">Compressed image stream</param> /// <param name="parArguments">List of an arguments (ImageFormat finalFormat)</param> /// <returns>Decompressed image stream</returns> public override Stream DecompressImage(Stream parCompressedImage, List <object> parArguments) { GZIP gzip = GZIP.GetInstance(); Stream interlacedImage = gzip.DecompressImage(parCompressedImage, new List <object>()); interlacedImage.Position = interlacedImage.Length - 1; bool lastRowIsOdd = interlacedImage.ReadByte() > 0; interlacedImage.SetLength(interlacedImage.Length - 1); Bitmap interlaced = new Bitmap(interlacedImage); int reconstructedWidth = interlaced.Width; int reconstructedHeight = interlaced.Height * 2; if (lastRowIsOdd) { reconstructedHeight--; } Bitmap reconstructed = new Bitmap(reconstructedWidth, reconstructedHeight, interlaced.PixelFormat); BitmapData bdInterlaced = interlaced.LockBits(new Rectangle(0, 0, interlaced.Width, interlaced.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int[] bitsInterlaced = new int[bdInterlaced.Stride / 4 * bdInterlaced.Height]; Marshal.Copy(bdInterlaced.Scan0, bitsInterlaced, 0, bitsInterlaced.Length); // --/-- BitmapData bdReconstructed = reconstructed.LockBits(new Rectangle(0, 0, reconstructed.Width, reconstructed.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int[] bitsReconstructed = new int[bdReconstructed.Stride / 4 * bdReconstructed.Height]; Marshal.Copy(bdReconstructed.Scan0, bitsReconstructed, 0, bitsReconstructed.Length); int interlacedWidth = interlaced.Width; int interlacedHeight = interlaced.Height; for (int j = 0; j < interlacedWidth; j++) { for (int i = 0; i < interlacedHeight - 1; i++) { bitsReconstructed[i * interlacedWidth * 2 + j] = bitsInterlaced[i * interlacedWidth + j]; bitsReconstructed[(i * 2 + 1) * interlacedWidth + j] = this.FindAverageColor(bitsInterlaced[i * interlacedWidth + j], bitsInterlaced[(i + 1) * interlacedWidth + j]); } bitsReconstructed[(interlacedHeight - 1) * 2 * interlacedWidth + j] = bitsInterlaced[(interlacedHeight - 1) * interlacedWidth + j]; if (!lastRowIsOdd) { bitsReconstructed[(reconstructedHeight - 1) * interlacedWidth + j] = bitsInterlaced[(interlacedHeight - 1) * interlacedWidth + j]; } } Marshal.Copy(bitsReconstructed, 0, bdReconstructed.Scan0, bitsReconstructed.Length); interlaced.UnlockBits(bdInterlaced); reconstructed.UnlockBits(bdReconstructed); Stream reconstructedImage = new MemoryStream(); ImageFormat format = (ImageFormat)parArguments[0]; reconstructed.Save(reconstructedImage, format); return(reconstructedImage); }