public void RleEncodeDecodeLongSequence()
        {
            byte[] bytes = new byte[] {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

                255, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
            };

            byte[] encoded = Rle.Encode(bytes);
            byte[] decoded = Rle.Decode(encoded);

            Assert.Less(encoded.Length, bytes.Length);
            Assert.AreEqual(bytes, decoded);
        }
        public void RleEncodeDecode()
        {
            byte[] bytes = new byte[] {
                0, 0, 0, 0, 0, 0, 4, 2, 0, 4, 4, 4, 4, 4, 4, 4,
                80, 80, 80, 80, 0, 2, 2, 2, 2, 255, 255, 255, 255, 255, 0, 0
            };

            byte[] encoded = Rle.Encode(bytes);
            byte[] decoded = Rle.Decode(encoded);

            Assert.Less(encoded.Length, bytes.Length);
            Assert.AreEqual(bytes, decoded);
        }
        public Bitmap Decompress(CompressedImage compressedImage)
        {
            var unHuf = HuffmanCodec.Decode(compressedImage.DataBytes, compressedImage.DecodeTable, compressedImage.BitsCount);
            var unRle = Rle <byte> .Decode(unHuf).ToArray();

            var yDct         = new List <double>();
            var cbDct        = new List <double>();
            var crDct        = new List <double>();
            var countYBlocks = compressedImage.ThinIndex * compressedImage.ThinIndex;
            var cellsToBlock = compressedImage.CompressionLevel;

            for (var i = 0; i < unRle.Length;)
            {
                for (var j = 0; j < countYBlocks * cellsToBlock; j++)
                {
                    yDct.Add(unRle[i++]);
                }
                for (var j = 0; j < cellsToBlock; j++)
                {
                    cbDct.Add(unRle[i++]);
                }
                for (var j = 0; j < cellsToBlock; j++)
                {
                    crDct.Add(unRle[i++]);
                }
            }

            var yDctBlocks       = DevideIntoPiece(yDct, compressedImage.CompressionLevel);
            var cbDctBlocks      = DevideIntoPiece(cbDct, compressedImage.CompressionLevel);
            var crDctBlocks      = DevideIntoPiece(crDct, compressedImage.CompressionLevel);
            var yChannel         = _dctDecompressor.Decompress(yDctBlocks, _dctSize, compressedImage.Height, compressedImage.Width, _lumiaMatrixProvider.GetMatrix());
            var thinnedCbChannel = _dctDecompressor.Decompress(cbDctBlocks, _dctSize, compressedImage.Height / compressedImage.ThinIndex, compressedImage.Width / compressedImage.ThinIndex, _colorMatrixProvider.GetMatrix());
            var thinnedCrChannel = _dctDecompressor.Decompress(crDctBlocks, _dctSize, compressedImage.Height / compressedImage.ThinIndex, compressedImage.Width / compressedImage.ThinIndex, _colorMatrixProvider.GetMatrix());
            var cbChannel        = _pieceMatrixExtender.Extend(thinnedCbChannel, compressedImage.ThinIndex);
            var crChannel        = _pieceMatrixExtender.Extend(thinnedCrChannel, compressedImage.ThinIndex);
            var yCbCrChannels    = new YCbCrChannels(yChannel, cbChannel, crChannel);
            var yCbCrPixels      = _iChannelsPacker.Pack(yCbCrChannels, compressedImage.Height, compressedImage.Width);
            var matrixRgbPixels  = MatrixYCbCrToRgbConverter.Convert(yCbCrPixels);
            var bmp = _bitmapBuilder.Build(matrixRgbPixels);

            return(bmp);
        }