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 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 !"); }