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 CompressedImage Compress(Bitmap bmp) { if (bmp.PixelFormat != PixelFormat.Format24bppRgb) { throw new Exception($"{bmp.PixelFormat} pixel format is not supported, supported rgb24"); } var pixelsMatrix = _pixelsExtractor.Extract(bmp); var converterPixelsMatrix = MatrixRgbToYCbCrConveter.Convert(pixelsMatrix); var residueYPiece = bmp.Height % (_thinIndex * _dctSize); var residueXPiece = bmp.Width % (_thinIndex * _dctSize); var countAddYPiece = residueYPiece == 0 ? 0 : _thinIndex * _dctSize - residueYPiece; var countAddXPiece = residueXPiece == 0 ? 0 : _thinIndex * _dctSize - residueXPiece; var extendedPixelsMatrix = _matrixExtender.Extend(converterPixelsMatrix, countAddYPiece, countAddXPiece); var yCbCrchannels = _channelExtractor.Extract(extendedPixelsMatrix); var thinnedCbChannel = _matrixThinner.Thin(yCbCrchannels.CbChannel, _thinIndex); var thinnedCrChannel = _matrixThinner.Thin(yCbCrchannels.CrChannel, _thinIndex); var dctYPieces = _dctCompressor.Compress(yCbCrchannels.YChannel, _dctSize, _compressionLevel, _lumiaMatrixProvider.GetMatrix()); var dctCbPieces = _dctCompressor.Compress(thinnedCbChannel, _dctSize, _compressionLevel, _colorMatrixProvider.GetMatrix()); var dctCrPieces = _dctCompressor.Compress(thinnedCrChannel, _dctSize, _compressionLevel, _colorMatrixProvider.GetMatrix()); var result = new List <byte>(); var countYBlocksPerColorBlock = _thinIndex * _thinIndex; for (int i = 0, thinI = 0; i < dctYPieces.Length; thinI++) { for (var j = 0; j < countYBlocksPerColorBlock; j++) { result.AddRange(dctYPieces[i++]); } result.AddRange(dctCbPieces[thinI]); result.AddRange(dctCrPieces[thinI]); } var rle = Rle <byte> .Encode(result).ToArray(); Dictionary <BitsWithLength, byte> decodeTable; long bitsCount; var huf = HuffmanCodec.Encode(rle, out decodeTable, out bitsCount); var compressedImage = new CompressedImage { ThinIndex = _thinIndex, CompressionLevel = _compressionLevel, DataBytes = huf, Height = extendedPixelsMatrix.GetLength(0), Width = extendedPixelsMatrix.GetLength(1), DecodeTable = decodeTable, BitsCount = bitsCount }; return(compressedImage); }
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); }