Exemple #1
0
        public void HuffmanDepthTest()
        {
            HuffmanNode node1 = new HuffmanNode(1, 1);
            HuffmanNode node2 = new HuffmanNode(2, 1);
            HuffmanNode node3 = new HuffmanNode(5, 5);
            HuffmanNode node4 = new HuffmanNode(7, 7);
            HuffmanNode node5 = new HuffmanNode(10, 10);
            HuffmanNode node6 = new HuffmanNode(14, 14);

            List <HuffmanNode> testList = new List <HuffmanNode> {
                node1, node5, node3, node6, node2, node4
            };
            HuffmanEncoder         encoder  = new HuffmanEncoder();
            Dictionary <byte, int> testDict = new Dictionary <byte, int>();
            //testDict = encoder.EncodeToPackageMerge(testList, 4);
        }
Exemple #2
0
        public void HuffmanMergeBaum()
        {
            HuffmanEncoder         huff  = new HuffmanEncoder();
            Dictionary <byte, int> test  = new Dictionary <byte, int>();
            Dictionary <byte, int> test2 = new Dictionary <byte, int>();

            test.Add(0, 4);
            test.Add(1, 4);
            test.Add(2, 3);
            test.Add(3, 2);
            test.Add(4, 2);
            test.Add(5, 2);
            test2.Add(0, 3);
            test2.Add(1, 3);
            test2.Add(2, 5);
            test2.Add(3, 7);
            test2.Add(4, 14);
            test2.Add(5, 14);
            huff.EncodeToPackageMergeList(test, test2);
        }
Exemple #3
0
        public RLEObject encodeACRunLength()
        {
            List <pairValues> pairList = new List <pairValues>();
            RLEObject         RLEobj   = new RLEObject();


            List <Bitstream> finalStreamList = new List <Bitstream>();

            byte[] streamArray;

            byte[]  acArray           = getACArray(input);
            short[] dcDifferenceArray = getDCDifferenceArray(input);
            var     dcCategoryArray   = dcDifferenceArray.Select(getCategory).ToArray();

            for (int g = 0; g < input.Count; g++)
            {
                byte       zeroCounter = 0;
                pairValues tempValue   = new pairValues();
                for (int i = 1; i < input[g].Length; i++)
                {
                    if (i == input[g].Length - 1 && input[g][i] == 0)
                    {
                        tempValue.zeros = 0;
                        tempValue.value = 0;
                        pairList.Add(tempValue);
                    }

                    if (zeroCounter == 16)
                    {
                        tempValue.zeros = 15;
                        tempValue.value = 0;
                        pairList.Add(tempValue);

                        zeroCounter = 0;
                    }

                    else if (input[g][i] == 0)
                    {
                        zeroCounter++;
                    }
                    else
                    {
                        tempValue.zeros = zeroCounter;
                        zeroCounter     = 0;
                        tempValue.value = input[g][i];
                        pairList.Add(tempValue);
                    }
                }


                pairValues[] pairArray = pairList.ToArray();
                streamArray = new byte[pairArray.Length];
                pairList.Clear();


                for (int i = 0; i < pairArray.Length; i++)
                {
                    pairArray[i].category = getCategory(pairArray[i].value);

                    if (pairArray[i].value < 0)
                    {
                        pairArray[i].value = (short)(~(-pairArray[i].value));
                    }
                    //nur so viele bits von rechts nehmen wie in kategorie sagt
                    pairArray[i].merged = connectBytes(pairArray[i].zeros, pairArray[i].category);
                    streamArray[i]      = pairArray[i].merged;
                }

                HuffmanEncoder coder = new HuffmanEncoder();
                SortedList <byte, List <bool> > huffmantable = new SortedList <byte, List <bool> >();
                //MemoryStream stream = new MemoryStream(acArray);
                //coder.PrepareEncodingRightsided(stream);
                huffmantable           = coder.getHuffmanTable();
                RLEobj.huffmantablesAC = huffmantable;
                List <bool> encodedList  = new List <bool>();
                Bitstream   outputStream = new Bitstream();


                pairDC pair = new pairDC();
                pair.value = dcDifferenceArray[g];
                MemoryStream   hufstream = new MemoryStream(dcCategoryArray);
                HuffmanEncoder coderDC   = new HuffmanEncoder();
                SortedList <byte, List <bool> > hufmantable = new SortedList <byte, List <bool> >();
                coderDC.PrepareEncodingRightsided(hufstream);
                hufmantable   = coderDC.getHuffmanTable();
                pair.category = getCategory(pair.value); //huffman codieren
                List <bool> encodedListDC = new List <bool>();
                encodedListDC          = hufmantable[pair.category];
                RLEobj.huffmantablesDC = (hufmantable);

                for (int z = 0; z < encodedListDC.Count; z++)
                {
                    outputStream.AddBit(encodedListDC[z]);
                }

                for (int z = 0; z < pair.category; z++)
                {
                    outputStream.AddBit(getBit(pair.value, z));
                }


                for (int i = 0; i < pairArray.Length; i++)
                {
                    encodedList = huffmantable[pairArray[i].merged];
                    for (int z = 0; z < encodedList.Count; z++)
                    {
                        outputStream.AddBit(encodedList[z]);
                    }
                    for (int z = 0; z < pairArray[i].category; z++)
                    {
                        outputStream.AddBit(getBit(pairArray[i].value, z));
                    }
                }

                finalStreamList.Add(outputStream);
            }
            RLEobj.bitstreams = finalStreamList;
            return(RLEobj);
        }
Exemple #4
0
 public void HuffmanMergeBaum()
 {
     HuffmanEncoder huff = new HuffmanEncoder();
     Dictionary<byte, int> test = new Dictionary<byte, int>();
     Dictionary<byte, int> test2 = new Dictionary<byte, int>();
     test.Add(0, 4);
     test.Add(1, 4);
     test.Add(2, 3);
     test.Add(3, 2);
     test.Add(4, 2);
     test.Add(5, 2);
     test2.Add(0, 3);
     test2.Add(1, 3);
     test2.Add(2, 5);
     test2.Add(3, 7);
     test2.Add(4, 14);
     test2.Add(5, 14);
     huff.EncodeToPackageMergeList(test, test2);
 }
Exemple #5
0
        public void HuffmanDepthTest()
        {
            HuffmanNode node1 = new HuffmanNode(1, 1);
            HuffmanNode node2 = new HuffmanNode(2, 1);
            HuffmanNode node3 = new HuffmanNode(5, 5);
            HuffmanNode node4 = new HuffmanNode(7, 7);
            HuffmanNode node5 = new HuffmanNode(10, 10);
            HuffmanNode node6 = new HuffmanNode(14, 14);

            List<HuffmanNode> testList = new List<HuffmanNode> {node1, node5, node3, node6, node2, node4};
            HuffmanEncoder encoder = new HuffmanEncoder();
            Dictionary<byte, int> testDict = new Dictionary<byte, int>();
            //testDict = encoder.EncodeToPackageMerge(testList, 4);
        }
        public RLEObject encodeACRunLength()
        {
            List<pairValues> pairList = new List<pairValues>();
            RLEObject RLEobj = new RLEObject();

            List<Bitstream> finalStreamList = new List<Bitstream>();
            byte[] streamArray;

            byte[] acArray = getACArray(input);
            short[] dcDifferenceArray = getDCDifferenceArray(input);
            var dcCategoryArray = dcDifferenceArray.Select(getCategory).ToArray();

            for (int g = 0; g < input.Count; g++)
            {
                byte zeroCounter = 0;
                pairValues tempValue = new pairValues();
                for (int i = 1; i < input[g].Length; i++)
                {
                    if (i == input[g].Length - 1 && input[g][i] == 0)
                    {
                        tempValue.zeros = 0;
                        tempValue.value = 0;
                        pairList.Add(tempValue);
                    }

                    if (zeroCounter == 16)
                    {
                        tempValue.zeros = 15;
                        tempValue.value = 0;
                        pairList.Add(tempValue);

                        zeroCounter = 0;
                    }

                    else if (input[g][i] == 0)
                    {
                        zeroCounter++;
                    }
                    else
                    {
                        tempValue.zeros = zeroCounter;
                        zeroCounter = 0;
                        tempValue.value = input[g][i];
                        pairList.Add(tempValue);

                    }
                }

                pairValues[] pairArray = pairList.ToArray();
                streamArray = new byte[pairArray.Length];
                pairList.Clear();

                for (int i = 0; i < pairArray.Length; i++)
                {
                    pairArray[i].category = getCategory(pairArray[i].value);

                    if (pairArray[i].value < 0)
                    {
                        pairArray[i].value = (short)(~(-pairArray[i].value));

                    }
                    //nur so viele bits von rechts nehmen wie in kategorie sagt
                    pairArray[i].merged = connectBytes(pairArray[i].zeros, pairArray[i].category);
                    streamArray[i] = pairArray[i].merged;
                }

                HuffmanEncoder coder = new HuffmanEncoder();
                SortedList<byte, List<bool>> huffmantable = new SortedList<byte, List<bool>>();
                //MemoryStream stream = new MemoryStream(acArray);
                //coder.PrepareEncodingRightsided(stream);
                huffmantable = coder.getHuffmanTable();
                RLEobj.huffmantablesAC = huffmantable;
                List<bool> encodedList = new List<bool>();
                Bitstream outputStream = new Bitstream();

                pairDC pair = new pairDC();
                pair.value = dcDifferenceArray[g];
                MemoryStream hufstream = new MemoryStream(dcCategoryArray);
                HuffmanEncoder coderDC = new HuffmanEncoder();
                SortedList<byte, List<bool>> hufmantable = new SortedList<byte, List<bool>>();
                coderDC.PrepareEncodingRightsided(hufstream);
                hufmantable = coderDC.getHuffmanTable();
                pair.category = getCategory(pair.value); //huffman codieren
                List<bool> encodedListDC = new List<bool>();
                encodedListDC = hufmantable[pair.category];
                RLEobj.huffmantablesDC = (hufmantable);

                for (int z = 0; z < encodedListDC.Count; z++)
                    outputStream.AddBit(encodedListDC[z]);

                for (int z = 0; z < pair.category; z++)
                {
                    outputStream.AddBit(getBit(pair.value, z));
                }

                for (int i = 0; i < pairArray.Length; i++)
                {

                    encodedList = huffmantable[pairArray[i].merged];
                    for (int z = 0; z < encodedList.Count; z++)
                        outputStream.AddBit(encodedList[z]);
                    for (int z = 0; z < pairArray[i].category; z++)
                        outputStream.AddBit(getBit(pairArray[i].value, z));

                }

                finalStreamList.Add(outputStream);
            }
            RLEobj.bitstreams = finalStreamList;
            return RLEobj;
        }