Exemplo n.º 1
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;
        }
Exemplo n.º 2
0
        public byte[] getACArray(List<byte[]> input)
        {
            List<short> list = new List<short>();
            pairValues tempValue = new pairValues();
            List<pairValues> pairList = new List<pairValues>();
            byte zeroCounter = 0;
            for (int g = 0; g<input.Count; g++)
            {
                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);

                    }
                }
            }
            byte[] streamArray;
            short[] array = list.ToArray();
            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;
            }
            return streamArray;
        }