コード例 #1
0
ファイル: Test.cs プロジェクト: Kundentelefon/JPEGEncode
        public void testLengthEncoder()
        {
            byte[]        arry     = { 57, 45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 30, 1, 0, 0, 0, 0, 0 };
            byte[]        arry2    = { 102, 63, 2, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 30, 1, 0, 0, 0, 0, 0 };
            List <byte[]> arrylist = new List <byte[]>();

            arrylist.Add(arry);
            arrylist.Add(arry2);
            RunLengthEncoder coder = new RunLengthEncoder(arrylist);
            RLEObject        test  = coder.encodeACRunLength();

            //test[0].WriteToFile("C:/Users/Maddin/Desktop/jeptest.txt");

            // Console.ReadKey();
        }
コード例 #2
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);
        }