Beispiel #1
0
        public static List <int> DecompressData(BitReader reader)
        {
            ArithmeticDecoder decoder = new ArithmeticDecoder(reader);
            var decompressedData      = new List <int>();

            // reading loop...
            for (; ;)
            {
                uint symbol = decoder.DecodeSymbol();
                if (symbol == EOF)
                {
                    break;
                }

                int reconstructSymbolFromRange = (int)symbol - 255;
                decompressedData.Add(reconstructSymbolFromRange);
                decoder.arithmeticModel.UpdateModel(symbol);
            }
            return(decompressedData);
        }
        public BmpFileObject(string filePath, ref int kReconstructError, ref int selectedPredictor)
        {
            _dataContainerEncoded = new int[256, 256];
            BitReader reader = new BitReader(filePath);

            for (int i = 0; i < _headerContainer.Length; i++)
            {
                _headerContainer[i] = (byte)reader.ReadNBits(8);
            }

            selectedPredictor = (int)reader.ReadNBits(4);
            kReconstructError = (int)reader.ReadNBits(4);
            var saveMode = (int)reader.ReadNBits(2);

            switch (saveMode)
            {
            case 0:
            {
                for (int i = 0; i < 256; i++)
                {
                    for (int j = 0; j < 256; j++)
                    {
                        _dataContainerEncoded[i, j] = (int)reader.ReadNBits(9) - 255;
                    }
                }
                break;
            }

            case 1:
            {
                for (int i = 0; i < 256; i++)
                {
                    for (int j = 0; j < 256; j++)
                    {
                        var index      = 0;
                        var lineNumber = 0;

                        while (reader.ReadNBits(1) == 1)
                        {
                            lineNumber++;
                        }

                        if (lineNumber == 0)
                        {
                            _dataContainerEncoded[i, j] = 0;
                        }
                        else
                        {
                            index = (int)reader.ReadNBits(lineNumber);
                            if (index >= (1 << (lineNumber - 1)))
                            {
                                _dataContainerEncoded[i, j] = index;
                            }
                            else
                            {
                                _dataContainerEncoded[i, j] = index - (1 << lineNumber) + 1;
                            }
                        }
                    }
                }
                break;
            }

            case 2:
            {
                var intDecodedValuesList = ArithmeticDecoder.DecompressData(reader);
                _dataContainerEncoded = Helpers.BuildMatrixFromListArithmetic(intDecodedValuesList);
                break;
            }

            default: break;
            }

            reader.Dispose();
        }