Exemple #1
0
        public void Compress()
        {
            if (!IsCompressed)
            {
                using var inBufferMem = MemoryPool <byte> .Shared.Rent(CompressedSize);

                var inBuffer = inBufferMem.Memory.Span.Slice(0, CompressedSize);
                mDataStream.Position = 0;
                mDataStream.Read(inBufferMem.Memory.Span.Slice(0, CompressedSize));

                using var outBufferMem = MemoryPool <byte> .Shared.Rent(UncompressedSize);

                var outBuffer      = outBufferMem.Memory.Span.Slice(0, UncompressedSize);
                int compressedSize = 0;
                if (UncompressedSize > 0)
                {
                    compressedSize = HuffmanCodec.Compress(inBuffer, outBuffer);
                }

                mDataStream = new MemoryStream(UncompressedSize);
                mDataStream.Write(outBuffer.Slice(0, compressedSize));

                IsCompressed   = true;
                CompressedSize = compressedSize;
            }
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        public void CheckHuffmanTreeBuilderWorkOnFrequencies()
        {
            var frequencies = new [] {
                0.14473691, 0.01147017, 0.00167522, 0.03831121, 0.00356579, 0.03811315,
                0.00178254, 0.00199644, 0.00183511, 0.00225716, 0.00211240, 0.00308829,
                0.00172852, 0.00186608, 0.00215921, 0.00168891, 0.00168603, 0.00218586,
                0.00284414, 0.00161833, 0.00196043, 0.00151029, 0.00173932, 0.00218370,
                0.00934121, 0.00220530, 0.00381211, 0.00185456, 0.00194675, 0.00161977,
                0.00186680, 0.00182071, 0.06421956, 0.00537786, 0.00514019, 0.00487155,
                0.00493925, 0.00503143, 0.00514019, 0.00453520, 0.00454241, 0.00485642,
                0.00422407, 0.00593387, 0.00458130, 0.00343687, 0.00342823, 0.00531592,
                0.00324890, 0.00333388, 0.00308613, 0.00293776, 0.00258918, 0.00259278,
                0.00377105, 0.00267488, 0.00227516, 0.00415997, 0.00248763, 0.00301555,
                0.00220962, 0.00206990, 0.00270369, 0.00231694, 0.00273826, 0.00450928,
                0.00384380, 0.00504728, 0.00221251, 0.00376961, 0.00232990, 0.00312574,
                0.00291688, 0.00280236, 0.00252436, 0.00229461, 0.00294353, 0.00241201,
                0.00366590, 0.00199860, 0.00257838, 0.00225860, 0.00260646, 0.00187256,
                0.00266552, 0.00242641, 0.00219450, 0.00192082, 0.00182071, 0.02185930,
                0.00157439, 0.00164353, 0.00161401, 0.00187544, 0.00186248, 0.03338637,
                0.00186968, 0.00172132, 0.00148509, 0.00177749, 0.00144620, 0.00192442,
                0.00169683, 0.00209439, 0.00209439, 0.00259062, 0.00194531, 0.00182359,
                0.00159096, 0.00145196, 0.00128199, 0.00158376, 0.00171412, 0.00243433,
                0.00345704, 0.00156359, 0.00145700, 0.00157007, 0.00232342, 0.00154198,
                0.00140730, 0.00288807, 0.00152830, 0.00151246, 0.00250203, 0.00224420,
                0.00161761, 0.00714383, 0.08188576, 0.00802537, 0.00119484, 0.00123805,
                0.05632671, 0.00305156, 0.00105584, 0.00105368, 0.00099246, 0.00090459,
                0.00109473, 0.00115379, 0.00261223, 0.00105656, 0.00124381, 0.00100326,
                0.00127550, 0.00089739, 0.00162481, 0.00100830, 0.00097229, 0.00078864,
                0.00107240, 0.00084409, 0.00265760, 0.00116891, 0.00073102, 0.00075695,
                0.00093916, 0.00106880, 0.00086786, 0.00185600, 0.00608367, 0.00133600,
                0.00075695, 0.00122077, 0.00566955, 0.00108249, 0.00259638, 0.00077063,
                0.00166586, 0.00090387, 0.00087074, 0.00084914, 0.00130935, 0.00162409,
                0.00085922, 0.00093340, 0.00093844, 0.00087722, 0.00108249, 0.00098598,
                0.00095933, 0.00427593, 0.00496661, 0.00102775, 0.00159312, 0.00118404,
                0.00114947, 0.00104936, 0.00154342, 0.00140082, 0.00115883, 0.00110769,
                0.00161112, 0.00169107, 0.00107816, 0.00142747, 0.00279804, 0.00085922,
                0.00116315, 0.00119484, 0.00128559, 0.00146204, 0.00130215, 0.00101551,
                0.00091756, 0.00161184, 0.00236375, 0.00131872, 0.00214120, 0.00088875,
                0.00138570, 0.00211960, 0.00094060, 0.00088083, 0.00094564, 0.00090243,
                0.00106160, 0.00088659, 0.00114514, 0.00095861, 0.00108753, 0.00124165,
                0.00427016, 0.00159384, 0.00170547, 0.00104431, 0.00091395, 0.00095789,
                0.00134681, 0.00095213, 0.00105944, 0.00094132, 0.00141883, 0.00102127,
                0.00101911, 0.00082105, 0.00158448, 0.00102631, 0.00087938, 0.00139290,
                0.00114658, 0.00095501, 0.00161329, 0.00126542, 0.00113218, 0.00123661,
                0.00101695, 0.00112930, 0.00317976, 0.00085346, 0.00101190, 0.00189849,
                0.00105728, 0.00186824, 0.00092908, 0.00160896,
            };


            var builder = new HuffmanTreeBuilder();
            var table   = builder.CreateTable(frequencies);
            var codec   = new HuffmanCodec(table);
        }
Exemple #4
0
        public static long DPCM(Bitmap input, string param)
        {
            // Text parameters:
            Dictionary <string, string> p = Util.ParseKeyValueList(param);
            bool predict = false;

            Util.TryParse(p, "predict", ref predict);

            int width  = input.Width;
            int height = input.Height;

            if (width < 1 || height < 1)
            {
                return(0L);
            }

            // Encoding, only dry-run in memory.
            // Backend: adaptive Huffman encoder.
            MemoryStream ms   = new MemoryStream();
            HuffmanCodec huff = new HuffmanCodec();

            huff.BinaryStream = ms;
            huff.MaxSymbol    = 510; // -255 to +255
            huff.Open(true);

            // Predictive gray-pixel encoding:
            int previous = 0;

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    Color c  = input.GetPixel(x, y);
                    int   gr = Draw.RgbToGray(c.R, c.G, c.B);
                    if (predict)
                    {
                        huff.Put(gr - previous + 255);
                        previous = gr;
                    }
                    else
                    {
                        huff.Put(gr);
                    }
                }
            }

            huff.Flush();
            long result = ms.Length;

            huff.Close();

            return(result);
        }
Exemple #5
0
        public void CheckCompressionOnRealText()
        {
            var text = File.ReadAllText("../../../poem.txt", Encoding.UTF8);
            var data = Encoding.UTF8.GetBytes(text);

            var treeBuilder = new HuffmanTreeBuilder();
            var table       = treeBuilder.CreateTable(data);
            var codec       = new HuffmanCodec(table);

            var encodedData = codec.Encode(data);

            Assert.True(encodedData.Length < data.Length);
        }
Exemple #6
0
        public static unsafe int DecompressCpk(ReadOnlySpan <byte> source, Span <byte> destination)
        {
            fixed(byte *pInput = source)
            {
                var pHeader = (CompressedCpkHeader *)pInput;

                HuffmanCodec.Decompress(
                    source.Slice(sizeof(CompressedCpkHeader), pHeader->CompressedSize),
                    destination.Slice(0, pHeader->UncompressedSize));

                return(pHeader->UncompressedSize);
            }
        }
Exemple #7
0
        public void CopyTo(Span <byte> destination, bool decompress)
        {
            if (IsCompressed && decompress)
            {
                using var inBufferMem = MemoryPool <byte> .Shared.Rent(CompressedSize);

                var inBuffer = inBufferMem.Memory.Span.Slice(0, CompressedSize);
                Open(false).Read(inBufferMem.Memory.Span.Slice(0, CompressedSize));
                HuffmanCodec.Decompress(inBuffer, destination);
            }
            else
            {
                Open(false).Read(destination);
            }
        }
Exemple #8
0
        public static unsafe int CompressCpk(ReadOnlySpan <byte> source, Span <byte> destination)
        {
            fixed(byte *pDest = destination)
            {
                var pHeader = (CompressedCpkHeader *)pDest;

                pHeader->Field00          = 0;
                pHeader->CompressedSize   = source.Length;
                pHeader->UncompressedSize = source.Length;
                pHeader->Flags            = 1;
                pHeader->CompressedSize   = HuffmanCodec.Compress(source, destination.Slice(sizeof(CompressedCpkHeader)), HuffmanCodec.DEFAULT_CHUNK_SIZE);
                Trace.Assert(pHeader->CompressedSize < pHeader->UncompressedSize);
                return(pHeader->CompressedSize + sizeof(CompressedCpkHeader));
            }
        }
Exemple #9
0
        public static void CheckCorruptionOnGuids()
        {
            const int testsAmount = 100;

            var random      = new Random();
            var treeBuilder = new HuffmanTreeBuilder();

            Parallel.For(0, testsAmount, i => {
                var data  = Guid.NewGuid().ToByteArray();
                var table = treeBuilder.CreateTable(data);
                var codec = new HuffmanCodec(table);

                var encodedData = codec.Encode(data);
                var decodedData = codec.Decode(encodedData);

                Assert.Equal(data, decodedData);
            });
        }
        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);
        }
Exemple #11
0
        public void CopyTo(Stream destination, bool decompress)
        {
            if (IsCompressed && decompress)
            {
                using var inBufferMem = MemoryPool <byte> .Shared.Rent(CompressedSize);

                var inBuffer = inBufferMem.Memory.Span.Slice(0, CompressedSize);
                Open(false).Read(inBufferMem.Memory.Span.Slice(0, CompressedSize));

                using var outBufferMem = MemoryPool <byte> .Shared.Rent(UncompressedSize);

                var outBuffer = outBufferMem.Memory.Span.Slice(0, UncompressedSize);
                HuffmanCodec.Decompress(inBuffer, outBuffer);
                destination.Write(outBuffer);
            }
            else
            {
                Open(false).CopyTo(destination);
            }
        }
Exemple #12
0
        public void Unpack(string directoryPath, Func <DwPackFileEntry, bool> callback)
        {
            var baseStreamLock = new object();

            Parallel.ForEach(Entries, (entry =>
            {
                if (callback != null && !callback(entry))
                {
                    return;
                }
                var unpackPath = Path.Combine(directoryPath, entry.Path);
                var unpackDir = Path.GetDirectoryName(unpackPath);
                Directory.CreateDirectory(unpackDir);

                using var fileStream = File.Create(unpackPath);
                if (entry.IsCompressed)
                {
                    // Copy compressed data from basestream so we can decompress in parallel
                    var compressedBufferMem = MemoryPool <byte> .Shared.Rent(entry.CompressedSize);
                    var compressedBuffer = compressedBufferMem.Memory.Span.Slice(0, entry.CompressedSize);
                    lock (baseStreamLock)
                        entry.CopyTo(compressedBuffer, decompress: false);

                    // Decompress
                    using var outBufferMem = MemoryPool <byte> .Shared.Rent(entry.UncompressedSize);
                    var outBuffer = outBufferMem.Memory.Span.Slice(0, entry.UncompressedSize);
                    HuffmanCodec.Decompress(compressedBuffer, outBuffer);
                    fileStream.Write(outBuffer);
                }
                else
                {
                    lock (baseStreamLock)
                        entry.CopyTo(fileStream, decompress: false);
                }
            }));
        }
Exemple #13
0
        static void DoTesting()
        {
            var test = new Testing
            {
                OutputWriter = Console.Out
            };

            //TestGolomb();
            //return;
            //DumpCode();
            //return;


            //test.TestUniversalCodeBattery();
            //test.TestUniversalCode(1, 10000);
            //test.TestUniversalCode(1, 1U<<20,n=>5*n/3);
            //test.TestUniversalCode(8, 1U << 28, n => 9*n/8);
            //test.TestUniversalOnFiles(Testing.Paths[0]);
            //return;

            //var bs = new Bitstream();
            ////bs.OutputWriter = Console.Out;
            //var ls1 = new List<uint> {1,4,4,9,1,34,4,0,0,76,1,34,10,34};
            //ls1 = new List<uint> { 0x00, 0x2B, 0x4F, 0x81, 0x50, 0x2B, 0x56, 0x2B, 0x2C, 0x56, 0x56, 0x24, 0x24, 0x4F, 0x56, 0x25, 0x2B, 0x7B, 0x2B, 0x2C, 0x01, 0x2B, 0x81, 0x32, 0x32, 0x07, 0x07, 0x2B, 0x56, 0x06, 0x31, 0x00, 0xA5, 0x24, 0xAC, 0xAC, 0x50, 0x81, 0x81, 0x57, 0x01, 0x81, 0x81, 0x07, 0x5D, 0x00, 0x5C, 0x4F, 0x48, 0x81, 0xD7, 0x7A, 0x7A, 0xAC, 0x7B, 0x50, 0x75, 0x56, 0x81, 0x2D, 0x02, 0x81, 0x0E, 0x32, 0x81, 0x5C, 0x0C, 0x81, 0x73, 0x2B, 0xA5, 0x75, 0xA6, 0xD7, 0xA6, 0x75, 0x7B, 0x2D, 0x82, 0xAC, 0xAD, 0xAC, 0x88, 0x5D, 0x39, 0x15, 0x5D, 0xAC, 0x37, 0x0C, 0x6C, 0x9E, 0x56, 0x9F, 0xD1, 0x81, 0x82, 0x03, 0x2D, 0xD7, 0xAC, 0x03, 0x88, 0x15, 0x39, 0x88, 0x37, 0x12, 0x37, 0x87, 0x73, 0xA5, 0x9F, 0x9A, 0xA0, 0x58, 0x58, 0x2D, 0x2E, 0x2E, 0x04, 0xAC, 0x40, 0xB3, 0xD7, 0x62, 0x12, 0xAC, 0xD7, 0x81, 0x9E, 0x90, 0x97, 0x81, 0x81, 0xCA, 0xCB, 0xD1, 0x83, 0x04, 0x2E, 0x58, 0xAD, 0xD7, 0x40, 0x1C, 0x40, 0xAC, 0x81, 0x88, 0x1C, 0x88, 0x18, 0x62, 0x87, 0x3D, 0xAC, 0x97, 0xC9, 0x56, 0xA5, 0xD1, 0xBF, 0xBF, 0xC5, 0xD7, 0xCB, 0xD7, 0x59, 0x83, 0x6B, 0x23, 0x47, 0x6B, 0x3D, 0xB2, 0xD7, 0x2B, 0x9E, 0xB4, 0xBB, 0xBB, 0xD7, 0xD1, 0xCB, 0xCB, 0xC5, 0xC5, 0xD1, 0xD1, 0x59, 0x05, 0x05, 0x59, 0x56, 0x8F, 0x47, 0x88, 0x88, 0x8D, 0x43, 0xB2, 0xAC, 0x8D, 0x1E, 0x43, 0xBB, 0xD0, 0xCB, 0xAC, 0xAD, 0x2F, 0x83, 0xB3, 0x47, 0xB2, 0x43, 0x68, 0xBB, 0xC2, 0xC2, 0xD0, 0xD7, 0x83, 0x82, 0x8F, 0x8D, 0x68, 0x2B, 0xA5, 0xC9, 0xD1, 0xAD, 0x57, 0x2B, 0xB3, 0x8F, 0xB2, 0x8D, 0xAD, 0x83, 0xD7, 0x8F, 0xB3, 0x88, 0x56, 0xA5, 0xC9, 0xB3, 0xAC, 0x2B, 0xAD, 0xB3, 0x2B, 0x2B, 0x32, 0x56, 0x4F, 0x24, 0x50, 0x25, 0x57, 0x01, 0x56, 0x2C, 0x07, 0x06, 0x31, 0x56, 0x24, 0x48, 0x25, 0x7B, 0xAC, 0x2C, 0x07, 0x32, 0x30, 0x2A, 0x5C, 0x31, 0x55, 0x73, 0x48, 0x2B, 0x7B, 0x7B, 0x56, 0x2D, 0x02, 0x2C, 0x57, 0x57, 0x5D, 0x5D, 0x32, 0x0E, 0x0E, 0x5D, 0x0E, 0x30, 0x80, 0x5C, 0x55, 0x6C, 0xA5, 0xA6, 0xA6, 0x2D, 0x82, 0x88, 0x39, 0x80, 0x30, 0x5B, 0x5B, 0xAC, 0x9E, 0x97, 0x6C, 0x9E, 0x56, 0x7B, 0x7B, 0x75, 0x03, 0x2D, 0x15, 0xD7, 0x5B, 0x36, 0x86, 0x87, 0xB2, 0xA5, 0x90, 0x56, 0xA0, 0x75, 0xA0, 0x58, 0x82, 0x82, 0x58, 0xAD, 0x39, 0x56, 0x64, 0x15, 0x88, 0xD6, 0x86, 0xAB, 0x86, 0x5A, 0xA6, 0x9A, 0xA0, 0xD1, 0xAC, 0x58, 0x04, 0x2E, 0x1C, 0x88, 0xAC, 0xAB, 0x60, 0xC2, 0xBB, 0xC2, 0x56, 0xCB, 0xCB, 0xD7, 0x82, 0x04, 0x58, 0x2E, 0x57, 0x88, 0x40, 0x8C, 0x87, 0xA5, 0xBB, 0xC9, 0x56, 0xC9, 0xD7, 0xC5, 0xCB, 0x00, 0x82, 0x2F, 0xAD, 0x83, 0x59, 0x47, 0x23, 0x47, 0x88, 0x8C, 0x8B, 0xB1, 0xC2, 0x56, 0x7A, 0xC9, 0xD0, 0x59, 0x83, 0x83, 0x2B, 0x6B, 0x56, 0x8C, 0xC2, 0xC9, 0xC9, 0xAD, 0x59, 0x83, 0x5D, 0x6B, 0x6B, 0x6B, 0x8F, 0x8F, 0x2B, 0xD7, 0xD0, 0xD0, 0xD1, 0xCB, 0xB3, 0xB3, 0x81, 0x8C, 0xAC, 0xD0, 0xD0, 0xD7, 0x89, 0x81, 0x8F, 0x2B, 0x81, 0xB1, 0xB2, 0x81, 0xD1, 0xAD, 0xB3, 0xB3, 0x88, 0x87, 0xD6, 0xB2, 0xD6, 0xD7, 0xD6, 0x00, 0xFF, 0x00};
            //UniversalCodec.BinaryAdaptiveSequentialEncode(bs,ls1);
            //bs.Position = 0;
            //Console.WriteLine();
            //var ls2 = UniversalCodec.BinaryAdaptiveSequentialDecode(bs);
            //Console.WriteLine();
            //Console.WriteLine(CodecBase.CompareInts(ls1.ToArray(),ls2.ToArray()));
            //
            //return;

            //DumpCode();
            //return;
            var codec77 = new Lz77Codec
            {
                //OutputWriter = Console.Out,
                //MaximumDistance = 85,
                //MaximumLength = 12,
                //MinimumLength = 2
            };
            //codec77.Options |= Lz77Codec.OptionFlags.DumpHeader;

            var codecCLbase = new LzclCodec {
                CodecName = "LZCL"
            };

            codecCLbase.CompressionOptions &= ~CompressionChecker.OptionFlags.UseHuffman;
            codecCLbase.CompressionOptions &= ~CompressionChecker.OptionFlags.UseArithmetic;
            codecCLbase.CompressionOptions &= ~CompressionChecker.OptionFlags.UseGolomb;

            var codecCl = new LzclCodec
            {
                CodecName = "LZCL",
                //MaximumDistance = 85,
                //MaximumLength = 12,
                //MinimumLength = 2
            };

            //codecCl.OutputWriter = Console.Out;
            //codecCl.CompressionOptions &= ~CompressionChecker.OptionFlags.UseArithmetic;
            //codecCl.CompressionOptions &= ~CompressionChecker.OptionFlags.UseGolomb;
            //codecCl.CompressionOptions &= ~CompressionChecker.OptionFlags.UseHuffman;
            //codecCl.CompressionOptions &= ~CompressionChecker.OptionFlags.UseFixed;
            //codecCl.CompressionOptions |= CompressionChecker.OptionFlags.UseStout;
            //codecCl.CompressionOptions |= CompressionChecker.OptionFlags.UseBASC;
            //codecCl.Options |= LZCLCodec.OptionFlags.ShowSavings;
            //codecCl.Options |= LZCLCodec.OptionFlags.DumpOptimize;
            codecCl.Options |= LzclCodec.OptionFlags.DumpDebug;
            //codecCl.Options |= LZCLCodec.OptionFlags.ShowTallies;
            codecCl.Options |= LzclCodec.OptionFlags.DumpCompressorSelections;

            //var ds = DataSets.HypnoLightLogo;
            //ds = File.ReadAllBytes(@"..\Corpus\Cantebury\asyoulik.txt");
            //codecCL.Optimize(ds);
            //return;

            var codecH = new HuffmanCodec();
            //codecH.OutputWriter = Console.Out;
            //codecH.Options |= HuffmanCodec.OptionFlags.DumpDecoding;
            //codecH.Options |= HuffmanCodec.OptionFlags.DumpEncoding;
            //codecH.Options |= HuffmanCodec.OptionFlags.DumpHeader;
            //codecH.Options |= HuffmanCodec.OptionFlags.DumpDictionary;
            //codecH.Options &= ~HuffmanCodec.OptionFlags.UseLowMemoryDecoding;

            var codecA = new ArithmeticCodec();
            //codecA.OutputWriter = Console.Out;
            //codecA.Options |= ArithmeticCodec.OptionFlags.DumpHeader;
            //codecA.Options |= ArithmeticCodec.OptionFlags.DumpTable;
            //codecA.Options |= ArithmeticCodec.OptionFlags.DumpState;
            //codecA.Options |= ArithmeticCodec.OptionFlags.UseLowMemoryDecoding;

            var codecF = new FixedSizeCodec();

            var codecGolomb = new GolombCodec();

            byte[] smallTest = { 1, 2, 5, 5, 7, 7, 7, 7, 8, 8, 5 };

            var request = new Testing.Request
            {
                Codecs = new List <CodecBase>
                {
                    codecH,
                    codecA,
                    codec77,
                    codecCl,
                    //codecGolomb,
                    //codecCLbase,
                    //codecF
                },
                // Codec = codec,

                //TestData = smallTest,
                //TestData = DataSets.HypnoLightLogo,
                Filename     = "*.*",
                Path         = Testing.Paths[0],
                RecurseFiles = true,

                //Decompress = false,
                UseExternalDecompressor = true,
                ShowOnlyErrors          = false,
                TrapErrors = true
            };

            test.DoTest(request);

            //OutLogo();
            // test.Optimize(DataSets.HypnoLightLogo);
        }
Exemple #14
0
        static int PerformCommand(Options opts)
        {
            if (opts.Testing)
            {
                DoTesting();
                return(1);
            }
            // create list of codecs
            var codecs = new List <CodecBase>();

            for (var i = 0; i < opts.CodecIndices.Count; ++i)
            {
                var       ci    = opts.CodecIndices[i];
                CodecBase codec = null;
                switch (CodecNames[ci])
                {
                case "Arithmetic":
                    codec = new ArithmeticCodec();
                    break;

                case "Huffman":
                    codec = new HuffmanCodec();
                    break;

                case "Lz77":
                    codec = new Lz77Codec();
                    break;

                case "Lzcl":
                    codec = new LzclCodec();
                    break;

                default:
                    Console.Error.WriteLine($"Unknown codec {CodecNames[ci]}");
                    break;
                }
                ApplyParameters(codec, opts.CodecParameters[i]);
                if (codec != null)
                {
                    codecs.Add(codec);
                }
            }
            if (!codecs.Any())
            {
                Console.Error.WriteLine("No codec(s) specified");
                return(-2);
            }
            if (opts.Verbose)
            {
                foreach (var c in codecs)
                {
                    c.OutputWriter = Console.Out;
                    // todo - flags?
                }
            }


            // if has input file and output file then do that
            if (!String.IsNullOrEmpty(opts.Inputfile) && !String.IsNullOrEmpty(opts.Outputfile))
            {
                if (!File.Exists(opts.Inputfile))
                {
                    Console.Error.WriteLine($"File {opts.Inputfile} does not exist");
                    Environment.Exit(-3);
                }
                // do first codec only
                var data = File.ReadAllBytes(opts.Inputfile);

                var watch = System.Diagnostics.Stopwatch.StartNew();

                var output    = opts.Decompress ? codecs[0].Decompress(data) : codecs[0].Compress(data);
                var headerMsg = $"File {opts.Inputfile} compressed from {data.Length} to {output.Length} for {(double)output.Length/data.Length:F3} ratio";
                if (codecs[0] is Lz77Codec)
                {
                    var lz77    = codecs[0] as Lz77Codec;
                    var bufSize = Math.Max(lz77.MaximumDistance, lz77.MaximumLength) + 1;
                    headerMsg += $" LZ77 bufsize {bufSize}";
                }
                if (codecs[0] is LzclCodec)
                {
                    var lzcl    = codecs[0] as LzclCodec;
                    var bufSize = Math.Max(lzcl.MaximumDistance, lzcl.MaximumLength) + 1;
                    headerMsg += $" LZCL bufsize {bufSize}";
                }
                OutputData(opts.Outputfile, output, OutputFormats[opts.OutputFormatIndex], headerMsg);

                watch.Stop();
                var elapsedMs  = watch.ElapsedMilliseconds;
                var actionText = opts.Decompress ? "decompressed" : "compressed";
                Console.WriteLine(
                    $"{Path.GetFileName(opts.Inputfile)} ({data.Length} bytes) {actionText} to {opts.Outputfile} ({output.Length} bytes).");
                if (!opts.Decompress)
                {
                    Console.WriteLine($"Compression ratio {100 - (100.0 * output.Length / data.Length):F1}%");
                }
                Console.WriteLine($"Elapsed time: {elapsedMs}ms");
                return(1);
            }
            // if has only input file - do some testing
            if (!String.IsNullOrEmpty(opts.Inputfile))
            {
                //public string Inputfile { get; set; } = "";
                //public bool RecurseDirectories { get; set; } = false;
                //public int OutputFormatIndex { get; set; } = -1;
                //public bool Decompress { get; set; } = false;

                var test    = new Testing();
                var request = new Testing.Request
                {
                    Codecs       = codecs,
                    Filename     = "*.*",
                    Path         = opts.Inputfile,
                    RecurseFiles = opts.RecurseDirectories,

                    // Decompress = false,
                    // UseExternalDecompressor = true,
                    ShowOnlyErrors = false,
                    TrapErrors     = true
                };
                test.DoTest(request);
                return(1);
            }
            Console.Error.WriteLine("Nothing to do");
            return(-4);
        }
Exemple #15
0
        public static long PCM(Bitmap input, string param)
        {
            // Text parameters:
            Dictionary <string, string> p = Util.ParseKeyValueList(param);
            bool BW = false;

            Util.TryParse(p, "bw", ref BW);

            int width  = input.Width;
            int height = input.Height;

            if (width < 1 || height < 1)
            {
                return(0L);
            }

            // Encoding, only dry-run in memory.
            // Backend: adaptive Huffman encoder.
            MemoryStream ms   = new MemoryStream();
            HuffmanCodec huff = new HuffmanCodec();

            huff.BinaryStream = ms;
            huff.Open(true);

            if (BW)
            {
                int buffer = 0;
                int bufLen = 0;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int gr = (input.GetPixel(x, y).GetBrightness() < 0.5f) ? 0 : 1;
                        buffer += buffer + gr;
                        if (++bufLen == 8)
                        {
                            huff.Put(buffer & 0xff);
                            buffer = 0;
                            bufLen = 0;
                        }
                    }

                    // end of scanline
                    if (bufLen > 0)
                    {
                        buffer <<= 8 - bufLen;
                        huff.Put(buffer & 0xff);
                        buffer = 0;
                        bufLen = 0;
                    }
                }
            }
            else
            {
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        Color c  = input.GetPixel(x, y);
                        int   gr = Draw.RgbToGray(c.R, c.G, c.B);
                        huff.Put(gr);
                    }
                }
            }

            huff.Flush();
            long result = ms.Length;

            huff.Close();

            return(result);
        }