/// <summary> /// Returns singletone instance /// </summary> /// <returns>Instance</returns> public static GZIP GetInstance() { if (_instance == null) { _instance = new GZIP(); } return(_instance); }
/// <summary> /// Forward application of the algorithm /// </summary> /// <param name="parOriginalImage">Original image stream</param> /// <param name="parArguments">List of an arguments (long qualityLevel, ImageFormat interimFormat)</param> /// <returns>Compressed image stream</returns> public override Stream CompressImage(Stream parOriginalImage, List <object> parArguments) { Bitmap original = new Bitmap(parOriginalImage); int originalWidth = original.Width; int originalHeight = original.Height; int moddedWidth = (originalWidth + 1) / 2; int moddedHeight = originalHeight; byte lastColumnIsOdd = (byte)(originalWidth % 2); Bitmap modded = new Bitmap(moddedWidth, moddedHeight, original.PixelFormat); BitmapData bdOriginal = original.LockBits(new Rectangle(0, 0, originalWidth, originalHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); int[] bitsOriginal = new int[originalWidth * originalHeight]; Marshal.Copy(bdOriginal.Scan0, bitsOriginal, 0, bitsOriginal.Length); // --/-- BitmapData bdModded = modded.LockBits(new Rectangle(0, 0, moddedWidth, moddedHeight), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); int[] bitsModded = new int[moddedWidth * moddedHeight]; Marshal.Copy(bdModded.Scan0, bitsModded, 0, bitsModded.Length); for (int i = 0; i < moddedHeight; i += 2) { for (int j = 0; j < moddedWidth; j++) { bitsModded[i * moddedWidth + j] = bitsOriginal[i * moddedWidth * 2 + j * 2]; if ((j * 2 + 1) < originalWidth) { bitsModded[(i + 1) * moddedWidth + j] = bitsOriginal[(i + 1) * moddedWidth * 2 + j * 2 + 1]; } } } Marshal.Copy(bitsModded, 0, bdModded.Scan0, bitsModded.Length); original.UnlockBits(bdOriginal); modded.UnlockBits(bdModded); Stream interlacedImage = new MemoryStream(); ImageFormat format = (ImageFormat)parArguments[1]; modded.Save(interlacedImage, format); interlacedImage.WriteByte(lastColumnIsOdd); GZIP gzip = GZIP.GetInstance(); int compressionLevel = (int)parArguments[0]; Stream compressedImage = gzip.CompressImage(interlacedImage, new List <object>() { compressionLevel }); return(compressedImage); }
/// <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> /// Forward application of the algorithm /// </summary> /// <param name="parOriginalImage">Original image stream</param> /// <param name="parArguments">List of an arguments (long qualityLevel, ImageFormat interimFormat)</param> /// <returns>Compressed image stream</returns> public override Stream CompressImage(Stream parOriginalImage, List <object> parArguments) { Bitmap original = new Bitmap(parOriginalImage); int levels = (int)parArguments[0]; HaarWavelet.ApplyTransform(ref original, true, levels); Stream waveletStream = new MemoryStream(); ImageFormat format = (ImageFormat)parArguments[2]; original.Save(waveletStream, format); GZIP gzip = GZIP.GetInstance(); int compressionLevel = (int)parArguments[1]; Stream compressedImage = gzip.CompressImage(waveletStream, new List <object>() { compressionLevel }); return(compressedImage); }
/// <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); }
/// <summary> /// Main form constructor /// </summary> public MainForm() { InitializeComponent(); _panels = new Hashtable(); _panels.Add(panelQuality, new List <string>() { "JPEG" }); _panels.Add(panelCompression, new List <string>() { "TIFF" }); _panels.Add(panelWaveletLevels, new List <string>() { "Wavelet+GZIP" }); _panels.Add(panelCompressionLevel, new List <string>() { "GZIP", "HInterlacing+GZIP", "VInterlacing+GZIP", "XInterlacing+GZIP", "Wavelet+GZIP" }); _panels.Add(panelInterimFormat, new List <string>() { "HInterlacing+GZIP", "VInterlacing+GZIP", "XInterlacing+GZIP", "Wavelet+GZIP" }); _panels.Add(panelFinalFormat, new List <string>() { "HInterlacing+GZIP", "VInterlacing+GZIP", "XInterlacing+GZIP", "Wavelet+GZIP" }); cmbAlgorithm.Items.Clear(); cmbAlgorithm.Items.Add(JPEG.GetInstance()); cmbAlgorithm.Items.Add(PNG.GetInstance()); cmbAlgorithm.Items.Add(TIFF.GetInstance()); cmbAlgorithm.Items.Add(GZIP.GetInstance()); cmbAlgorithm.Items.Add(HInterlacingWithGZIP.GetInstance()); cmbAlgorithm.Items.Add(VInterlacingWithGZIP.GetInstance()); cmbAlgorithm.Items.Add(XInterlacingWithGZIP.GetInstance()); cmbAlgorithm.Items.Add(WaveletWithGZIP.GetInstance()); cmbAlgorithm.SelectedIndex = 0; cmbCompression.Items.Clear(); cmbCompression.Items.Add("Default"); cmbCompression.Items.Add("None"); cmbCompression.Items.Add("CCITT3"); cmbCompression.Items.Add("CCITT4"); cmbCompression.Items.Add("LZW"); cmbCompression.Items.Add("RLE"); cmbCompression.Items.Add("ZIP"); cmbCompression.Items.Add(".NET default"); cmbCompression.SelectedIndex = 7; cmbCompressionLevel.Items.Clear(); cmbCompressionLevel.Items.Add("Optimal"); cmbCompressionLevel.Items.Add("Fast"); cmbCompressionLevel.Items.Add("None"); cmbCompressionLevel.SelectedIndex = 0; cmbInterimFormat.Items.Clear(); cmbInterimFormat.Items.Add(ImageFormat.Bmp); cmbInterimFormat.Items.Add(ImageFormat.Png); cmbInterimFormat.Items.Add(ImageFormat.Tiff); cmbInterimFormat.SelectedIndex = 0; cmbFinalFormat.Items.Clear(); cmbFinalFormat.Items.Add(ImageFormat.Bmp); cmbFinalFormat.Items.Add(ImageFormat.Png); cmbFinalFormat.Items.Add(ImageFormat.Tiff); cmbFinalFormat.SelectedIndex = 0; _resultExtension = null; }