public void SaveCompressedClick() { Downsampling d = new Downsampling(this.model.getMainImage()); DownsampleFormat df = d.Downsample(1); Utilities.SaveCompressed(df); }
public static Bitmap DecompressHuffman(string imgSrc) { //moram da pocnem odmah otvaranjem fajla i ucitavanjem bajtova redom kako su upisivani byte[] bmpWidth; byte[] bmpHeight; byte[] stride; byte[] downsampleChannels; byte[] dictionarySize; byte[] dictionary; byte[] YDataLen; byte[] CbDataLen; byte[] CrDataLen; byte[] imageData; HuffmanTree tree = new HuffmanTree(); using (var reader = new BinaryReader(File.Open(imgSrc, FileMode.Open))) { bmpWidth = reader.ReadBytes(4); bmpHeight = reader.ReadBytes(4); stride = reader.ReadBytes(4); downsampleChannels = reader.ReadBytes(4); YDataLen = reader.ReadBytes(4); CbDataLen = reader.ReadBytes(4); CrDataLen = reader.ReadBytes(4); dictionarySize = reader.ReadBytes(4); int dictSize = BitConverter.ToInt32(dictionarySize, 0); dictionary = reader.ReadBytes(dictSize); tree.DeserializeDictionary(dictionary); List <byte> compressedData = new List <byte>(); while (reader.BaseStream.Position != reader.BaseStream.Length) { compressedData.Add(reader.ReadByte()); } imageData = tree.Decode(new BitArray(compressedData.ToArray())); } int width = BitConverter.ToInt32(bmpWidth, 0); int height = BitConverter.ToInt32(bmpHeight, 0); int strideInt = BitConverter.ToInt32(stride, 0); int downampleChannelsInt = BitConverter.ToInt32(downsampleChannels, 0); int yDataLen = BitConverter.ToInt32(YDataLen, 0); int cbDataLen = BitConverter.ToInt32(CbDataLen, 0); int crDataLen = BitConverter.ToInt32(CrDataLen, 0); DownsampleFormat format = new DownsampleFormat(strideInt, width, height, yDataLen, cbDataLen, crDataLen, downampleChannelsInt); format.data = imageData; Downsampling sampling = new Downsampling(); return(sampling.RestoreBitmap(format)); }
public static void SaveCompressed(DownsampleFormat format) { SaveFileDialog dialog = new SaveFileDialog(); string path = AppDomain.CurrentDomain.BaseDirectory; dialog.InitialDirectory = path; dialog.RestoreDirectory = true; dialog.Filter = "MM Huffman's compressed file(*.mmc) |"; if (DialogResult.OK == dialog.ShowDialog()) { CompressHuffman(format, dialog.FileName); } else { MessageBox.Show("Error saving file."); } }
public static void CompressHuffman(DownsampleFormat format, string dest) { DownsampleFormat downFormat = format; byte[] data = new byte[format.Ylen + format.Cblen + format.Crlen]; format.data.CopyTo(data, 0); HuffmanTree tree = new HuffmanTree(); tree.CreateTree(tree.GenerateListOfNodes(data)); byte[] decompressDict = tree.SerializeDictionaryToBytes(); byte[] dictionarySize = BitConverter.GetBytes(decompressDict.Length); //svaki integer ima duzinu od 4 bajta u C# byte[] bmpWidth = BitConverter.GetBytes(format.bmpWidth); //4 bajta byte[] bmpHeight = BitConverter.GetBytes(format.bmpHeight); //4 bajta byte[] stride = BitConverter.GetBytes(format.bmpStride); //4 bajta byte[] downsampleChannels = BitConverter.GetBytes(format.code); //4 bajta byte[] YDataLen = BitConverter.GetBytes(format.Ylen); //4 bajta byte[] CbDataLen = BitConverter.GetBytes(format.Cblen); //4 bajta byte[] CrDataLen = BitConverter.GetBytes(format.Crlen); //4 bajta using (var writer = new FileStream(dest, FileMode.Create)) { writer.Write(bmpWidth, 0, bmpWidth.Length); writer.Write(bmpHeight, 0, bmpHeight.Length); writer.Write(stride, 0, stride.Length); writer.Write(downsampleChannels, 0, downsampleChannels.Length); writer.Write(YDataLen, 0, YDataLen.Length); writer.Write(CbDataLen, 0, CbDataLen.Length); writer.Write(CrDataLen, 0, CrDataLen.Length); writer.Write(dictionarySize, 0, dictionarySize.Length); writer.Write(decompressDict, 0, decompressDict.Length); byte[] allData = BitArrayToByteArray(tree.Encode(data)); writer.Write(allData, 0, allData.Length); } }
public void DownsampleClick() { this.channelsOn = true; this.view.MakeChannelsVisible(); Downsampling down = new Downsampling(this.model.getMainImage()); DownsampleFormat df1 = down.Downsample(1); DownsampleFormat df2 = down.Downsample(2); DownsampleFormat df3 = down.Downsample(2); //Compression.DownsamplingNew.Downsampling down = new Compression.DownsamplingNew.Downsampling(this.model.getMainImage()); //Compression.DownsamplingNew.DownsampleFormat df1 = down.DownsampleImage("YCb"); //Compression.DownsamplingNew.DownsampleFormat df2 = down.DownsampleImage("CbCr"); //Compression.DownsamplingNew.DownsampleFormat df3 = down.DownsampleImage("YCr"); this.model.setYChannel(down.RestoreBitmap(df1)); this.model.setCbChannel(down.RestoreBitmap(df2)); this.model.setCrChannel(down.RestoreBitmap(df3)); this.view.ShowImages(model.getMainImage(), model.getYChannel(), model.getCbChannel(), model.getCrChannel(), model.getImageName()); }