private void button2_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); HuffmanTree compression = new HuffmanTree(); double CRBCompression = 0; double CRACompression = 0; compression.Compression(ref ImageMatrix, ref CRBCompression, ref CRACompression, ref red_bytes_in_file, ref green_bytes_in_file, ref blue_bytes_in_file); //save Trees in file Red_Huffman = new HuffmanTree(); Green_Huffman = new HuffmanTree(); Blue_Huffman = new HuffmanTree(); Red_Huffman.PrintHuffmanTree(Red_Root); Green_Huffman.PrintHuffmanTree(Green_Root); Blue_Huffman.PrintHuffmanTree(Blue_Root); // save width and height and initial seed and tap position in file FileStream FN = new FileStream("Total_Info.dat", FileMode.Append); BinaryWriter FNR = new BinaryWriter(FN); FNR.Write(ImageOperations.GetHeight(ImageMatrix)); FNR.Write(ImageOperations.GetWidth(ImageMatrix)); FNR.Write(txt_initialseed.Text); FNR.Write(txt_tapposition.Text); FNR.Close(); FN.Close(); sw.Stop(); compression_lbl.Text = sw.Elapsed.ToString(); MessageBox.Show("Compressed !"); MessageBox.Show("Original Size = " + CRBCompression.ToString()); MessageBox.Show("Compression Ratio After Compression = " + CRACompression.ToString()); MessageBox.Show("stored bytes red" + red_bytes_in_file.ToString()); MessageBox.Show("stored bytes green" + green_bytes_in_file.ToString()); MessageBox.Show("stored bytes blue" + blue_bytes_in_file.ToString()); }
private void button1_Click(object sender, EventArgs e) { Stopwatch sp = new Stopwatch(); sp.Start(); long[] Red_array = new long[256]; long[] Green_array = new long[256]; long[] Blue_array = new long[256]; Priority_Queue Red_priority = new Priority_Queue(); Priority_Queue Green_priority = new Priority_Queue(); Priority_Queue Blue_priority = new Priority_Queue(); Red_Huffman = new HuffmanTree(); Green_Huffman = new HuffmanTree(); Blue_Huffman = new HuffmanTree(); List <HuffmanNode> Red_Nodes = new List <HuffmanNode>(); List <HuffmanNode> Green_Nodes = new List <HuffmanNode>(); List <HuffmanNode> Blue_Nodes = new List <HuffmanNode>(); Red_Root = new HuffmanNode(); Green_Root = new HuffmanNode(); Blue_Root = new HuffmanNode(); int Height = ImageOperations.GetHeight(ImageMatrix); int width = ImageOperations.GetWidth(ImageMatrix); for (int h = 0; h < Height; h++) { for (int w = 0; w < width; w++) { Red_array[ImageMatrix[h, w].red]++; Green_array[ImageMatrix[h, w].green]++; Blue_array[ImageMatrix[h, w].blue]++; } } for (int k = 0; k < 256; k++) { if (Red_array[k] > 0) { Red_Nodes.Add(new HuffmanNode { value = Convert.ToByte(k), freq = Red_array[k], CoLoR_CoDe = "" }); } if (Green_array[k] > 0) { Green_Nodes.Add(new HuffmanNode { value = Convert.ToByte(k), freq = Green_array[k], CoLoR_CoDe = "" }); } if (Blue_array[k] > 0) { Blue_Nodes.Add(new HuffmanNode { value = Convert.ToByte(k), freq = Blue_array[k], CoLoR_CoDe = "" }); } } Red_priority.Build_Min_Heap(ref Red_Nodes); Green_priority.Build_Min_Heap(ref Green_Nodes); Blue_priority.Build_Min_Heap(ref Blue_Nodes); Red_Root = Red_Huffman.BuildHuffmanTree(ref Red_Nodes); Green_Root = Green_Huffman.BuildHuffmanTree(ref Green_Nodes); Blue_Root = Blue_Huffman.BuildHuffmanTree(ref Blue_Nodes); FileStream Trunc_Red_File = new FileStream("Red_Info.txt", FileMode.Truncate); Trunc_Red_File.Close(); FileStream Red_File = new FileStream("Red_Info.txt", FileMode.Append); StreamWriter Red_Writer = new StreamWriter(Red_File); Red_Writer.WriteLine("--R--"); Red_Writer.WriteLine("Color - Frequency - Huffman Representation - Total Bits"); Red_Writer.Close(); Red_File.Close(); Red_Huffman.PrintCodes(Red_Root, "", "Red_Info.txt"); FileStream Trunc_Green_File = new FileStream("Green_Info.txt", FileMode.Truncate); Trunc_Green_File.Close(); FileStream Green_File = new FileStream("Green_Info.txt", FileMode.Append); StreamWriter Green_Writer = new StreamWriter(Green_File); Green_Writer.WriteLine("--G--"); Green_Writer.WriteLine("Color - Frequency - Huffman Representation - Total Bits"); Green_Writer.Close(); Green_File.Close(); Green_Huffman.PrintCodes(Green_Root, "", "Green_Info.txt"); FileStream Trunc_Blue_File = new FileStream("Blue_Info.txt", FileMode.Truncate); Trunc_Blue_File.Close(); FileStream Blue_File = new FileStream("Blue_Info.txt", FileMode.Append); StreamWriter Blue_Writer = new StreamWriter(Blue_File); Blue_Writer.WriteLine("--B--"); Blue_Writer.WriteLine("Color - Frequency - Huffman Representation - Total Bits"); Blue_Writer.Close(); Blue_File.Close(); Blue_Huffman.PrintCodes(Blue_Root, "", "Blue_Info.txt"); FileStream truncate_total_info = new FileStream("Total_Info.dat", FileMode.Truncate); truncate_total_info.Close(); sp.Stop(); lbl_construct_tree.Text = sp.Elapsed.ToString(); MessageBox.Show("Constructed !"); }
public static void Compress_image(RGBPixel[,] ImageMatrix, string seed, int tap) //O(N^2) { FileStream fs = new FileStream("Encrypted_image.bin", FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); int hight = GetHeight(ImageMatrix); int width = GetWidth(ImageMatrix); bw.Write(Convert.ToUInt16(hight)); bw.Write(Convert.ToUInt16(width)); bw.Write(seed); bw.Write(Convert.ToUInt16(tap)); Dictionary <int, int> Rvalues = new Dictionary <int, int>(); Dictionary <int, int> Gvalues = new Dictionary <int, int>(); Dictionary <int, int> Bvalues = new Dictionary <int, int>(); ///initiallize dictionary with 0 for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { Rvalues[ImageMatrix[i, j].red] = 0; Gvalues[ImageMatrix[i, j].green] = 0; Bvalues[ImageMatrix[i, j].blue] = 0; } } for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { //when exist Rvalues[ImageMatrix[i, j].red]++; Gvalues[ImageMatrix[i, j].green]++; Bvalues[ImageMatrix[i, j].blue]++; } } bw.Write(Convert.ToUInt16(Rvalues.Count)); foreach (KeyValuePair <int, int> K in Rvalues) { byte[] bb = BitConverter.GetBytes(K.Value); bw.Write(Convert.ToByte(K.Key)); bw.Write(Convert.ToByte(bb[0])); bw.Write(Convert.ToByte(bb[1])); bw.Write(Convert.ToByte(bb[2])); } bw.Write(Convert.ToUInt16(Gvalues.Count)); foreach (KeyValuePair <int, int> K in Gvalues) { byte[] bb = BitConverter.GetBytes(K.Value); bw.Write(Convert.ToByte(K.Key)); bw.Write(Convert.ToByte(bb[0])); bw.Write(Convert.ToByte(bb[1])); bw.Write(Convert.ToByte(bb[2])); } bw.Write(Convert.ToUInt16(Bvalues.Count)); foreach (KeyValuePair <int, int> K in Bvalues) { byte[] bb = BitConverter.GetBytes(K.Value); bw.Write(Convert.ToByte(K.Key)); bw.Write(Convert.ToByte(bb[0])); bw.Write(Convert.ToByte(bb[1])); bw.Write(Convert.ToByte(bb[2])); } HuffmanTree tree1 = new HuffmanTree(Rvalues); HuffmanTree tree2 = new HuffmanTree(Gvalues); HuffmanTree tree3 = new HuffmanTree(Bvalues); Dictionary <int, string> Redtree = tree1.CreateEncodings(); Dictionary <int, string> Greentree = tree2.CreateEncodings(); Dictionary <int, string> Bluetree = tree3.CreateEncodings(); List <string> R_binstream = new List <string>(); List <string> G_binstream = new List <string>(); List <string> B_binstream = new List <string>(); for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { R_binstream.Add(Redtree[ImageMatrix[i, j].red]); G_binstream.Add(Greentree[ImageMatrix[i, j].green]); B_binstream.Add(Bluetree[ImageMatrix[i, j].blue]); } } byte[] b = ConvertStringByte(R_binstream).ToArray(); bw.Write(Convert.ToInt32(b.Length)); bw.Write(b); b = ConvertStringByte(G_binstream).ToArray(); bw.Write(Convert.ToInt32(b.Length)); bw.Write(b); b = ConvertStringByte(B_binstream).ToArray(); bw.Write(Convert.ToInt32(b.Length)); bw.Write(b); bw.Close(); fs.Close(); }
public static RGBPixel[,] Decompress_image(string path, ref string seed, ref int tap, ref int height, ref int width) //O(N^2) { FileStream fs = new FileStream(@path, FileMode.Open); BinaryReader br = new BinaryReader(fs); height = br.ReadUInt16(); width = br.ReadUInt16(); seed = br.ReadString(); tap = br.ReadUInt16(); RGBPixel[,] ImageMatrix = new RGBPixel[height, width]; Dictionary <int, int> RedTreeList = new Dictionary <int, int>(); Dictionary <int, int> GreenTreeList = new Dictionary <int, int>(); Dictionary <int, int> BlueTreeList = new Dictionary <int, int>(); List <byte> Redstream = new List <byte>(); List <byte> Greenstream = new List <byte>(); List <byte> Bluestream = new List <byte>(); uint b = br.ReadUInt16(); while (b != 0) { int key = br.ReadByte(); byte[] bb = new byte[4]; bb[0] = br.ReadByte(); bb[1] = br.ReadByte(); bb[2] = br.ReadByte(); bb[3] = 0; int value = BitConverter.ToInt32(bb, 0); RedTreeList.Add(key, value); b--; } b = br.ReadUInt16(); while (b != 0) { int key = br.ReadByte(); byte[] bb = new byte[4]; bb[0] = br.ReadByte(); bb[1] = br.ReadByte(); bb[2] = br.ReadByte(); bb[3] = 0; int value = BitConverter.ToInt32(bb, 0); GreenTreeList.Add(key, value); b--; } b = br.ReadUInt16(); while (b != 0) { int key = br.ReadByte(); byte[] bb = new byte[4]; bb[0] = br.ReadByte(); bb[1] = br.ReadByte(); bb[2] = br.ReadByte(); bb[3] = 0; int value = BitConverter.ToInt32(bb, 0); BlueTreeList.Add(key, value); b--; } int size = br.ReadInt32(); Redstream.AddRange(br.ReadBytes(size)); size = br.ReadInt32(); Greenstream.AddRange(br.ReadBytes(size)); size = br.ReadInt32(); Bluestream.AddRange(br.ReadBytes(size)); br.Close(); fs.Close(); HuffmanTree tree1 = new HuffmanTree(RedTreeList); HuffmanTree tree2 = new HuffmanTree(GreenTreeList); HuffmanTree tree3 = new HuffmanTree(BlueTreeList); HuffmanNode Redtree = tree1.root; HuffmanNode Greentree = tree2.root; HuffmanNode Bluetree = tree3.root; int index = 0, count = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { HuffmanNode N = Redtree.Clone(); while (N.Left != null) { if (count == 8) { count = 0; index++; } if ((Redstream[index] & (1 << 7)) != 0) { N = N.Right; } else { N = N.Left; } count++; Redstream[index] <<= 1; } ImageMatrix[i, j].red = (byte)N.Value; } } count = 0; index = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { HuffmanNode N = Greentree.Clone(); while (N.Left != null) { if (count == 8) { count = 0; index++; } if ((Greenstream[index] & (1 << 7)) != 0) { N = N.Right; } else { N = N.Left; } count++; Greenstream[index] <<= 1; } ImageMatrix[i, j].green = (byte)N.Value; } } count = 0; index = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { HuffmanNode N = Bluetree.Clone(); while (N.Left != null) { if (count == 8) { count = 0; index++; } if ((Bluestream[index] & (1 << 7)) != 0) { N = N.Right; } else { N = N.Left; } count++; Bluestream[index] <<= 1; } ImageMatrix[i, j].blue = (byte)N.Value; } } return(ImageMatrix); }
public static void huffman_encoding(RGBPixel[,] ImageMatrix) { Dictionary <int, int> Rvalues = new Dictionary <int, int>(); Dictionary <int, int> Gvalues = new Dictionary <int, int>(); Dictionary <int, int> Bvalues = new Dictionary <int, int>(); int hight = GetHeight(ImageMatrix); int width = GetWidth(ImageMatrix); ///initiallize dictionary with 0 for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { Rvalues[ImageMatrix[i, j].red] = 0; Gvalues[ImageMatrix[i, j].green] = 0; Bvalues[ImageMatrix[i, j].blue] = 0; } } for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { //when exist Rvalues[ImageMatrix[i, j].red]++; Gvalues[ImageMatrix[i, j].green]++; Bvalues[ImageMatrix[i, j].blue]++; } } HuffmanTree tree1 = new HuffmanTree(Rvalues); HuffmanTree tree2 = new HuffmanTree(Gvalues); HuffmanTree tree3 = new HuffmanTree(Bvalues); Dictionary <int, string> Redtree = tree1.CreateEncodings(); Dictionary <int, string> Greentree = tree2.CreateEncodings(); Dictionary <int, string> bluetree = tree3.CreateEncodings(); long Rcount = 0; long Gcount = 0; long Bcount = 0; FileStream fs = new FileStream("huffman_output.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("--R--"); sw.WriteLine("Color - Frequency - Huffman Representation - Total Bits"); //red foreach (KeyValuePair <int, string> kvp in Redtree) { sw.Write(kvp.Key + " - " + Rvalues[kvp.Key] + " - " + kvp.Value + " - " + kvp.Value.Length * Rvalues[kvp.Key]); sw.WriteLine(); Rcount += kvp.Value.Length * Rvalues[kvp.Key]; } sw.WriteLine("*Total = " + Rcount); sw.WriteLine(); sw.WriteLine("--G--"); //green foreach (KeyValuePair <int, string> kvp in Greentree) { sw.Write(kvp.Key + " - " + Gvalues[kvp.Key] + " - " + kvp.Value + " - " + kvp.Value.Length * Gvalues[kvp.Key]); sw.WriteLine(); Gcount += kvp.Value.Length * Gvalues[kvp.Key]; } sw.WriteLine("*Total = " + Gcount); sw.WriteLine(); sw.WriteLine("--B--"); //blue foreach (KeyValuePair <int, string> kvp in bluetree) { sw.Write(kvp.Key + " - " + Bvalues[kvp.Key] + " - " + kvp.Value + " - " + kvp.Value.Length * Bvalues[kvp.Key]); sw.WriteLine(); Bcount += kvp.Value.Length * Bvalues[kvp.Key]; } double comp_output = (double)(Rcount + Gcount + Bcount) / 8; sw.WriteLine("*Total = " + Bcount); sw.WriteLine(); sw.WriteLine("**Compression Output**"); sw.WriteLine(comp_output + " bytes"); sw.WriteLine(); sw.WriteLine("**Compression Ratio**"); sw.WriteLine(Math.Round(comp_output / (hight * width * 3) * 100, 1) + "%"); sw.Close(); fs.Close(); MessageBox.Show("Huffman tree has been saved successflly !"); }