Exemple #1
0
        public void SixBitsMatrix()
        {
            string sixbits = @".....................X..................
.....................X..................
....................X.X.................
.....................X..................
.....................X..................
.....................X..................
.....................X..................
....................X.X.................
.....................X..................
.....................X..................
........................................
........................................
........................................
........................................
..X..X....X..X..........................
XXX..XXXXXX..XXX........................
..X..X....X..X..........................
......................XX................
.....................XX.................
.......................X................
................................X....X..
..............................XX.XXXX.XX
................................X....X..
";
            var    rle     = new Rle();

            rle.Load("Patterns/6bits.rle");
            Assert.Equal(sixbits, rle.ConwayMatrix.GetFinalResult().LimitedMatrix.ToString());
        }
        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);
        }
Exemple #3
0
        public void GliderMatrix()
        {
            var rle = new Rle();

            rle.Load(@"Patterns/glider.rle");
            Assert.Equal(glider, rle.ConwayMatrix.GetFinalResult().LimitedMatrix.ToString());
        }
Exemple #4
0
        public override string Encode(string str)
        {
            var shifts = new List <string>(Bw.GetShiftsOf(str));

            shifts.Sort();
            var position = Bw.GetIndexOf(str, shifts);

            return(Rle.Transform(shifts, position).ToString());
        }
Exemple #5
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);
        }
        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);
        }
Exemple #8
0
        public void ConwayMatrixToBoolMatrix()
        {
            string test = @"0000000000000000000000001000000000000000000000000000000000000000
0000000000000000000000101000000000000000000000000000000000000000
0000000000001100000011000000000000110000000000000000000000000000
0000000000010001000011000000000000110000000000000000000000000000
1100000000100000100011000000000000000000000000000000000000000000
1100000000100010110000101000000000000000000000000000000000000000
0000000000100000100000001000000000000000000000000000000000000000
0000000000010001000000000000000000000000000000000000000000000000
0000000000001100000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
";
            var    rle  = new Rle();

            rle.Load(@"Patterns/glider.rle");
            var matrix    = rle.GetMatrix();
            var cuadrante = Cuadrante.crear(matrix);

            Assert.Equal(test, cuadrante.ToString());
        }
Exemple #9
0
        public void ReadHeader()
        {
            var rle = new Rle();

            rle.Load(@"Patterns/glider.rle");
        }
Exemple #10
0
        public void testRle()
        {
            Compare(Rle.code(new List <uint> {
            }), new List <Rle> {
            });
            Compare(Rle.code(new List <uint> {
                4711
            }), new List <Rle> {
                new Rle(
                    4711,
                    0,
                    1,
                    true)
            });
            Compare(Rle.code(new List <uint> {
                4711, 4711
            }), new List <Rle> {
                new Rle(
                    4711,
                    0,
                    2,
                    true
                    )
            });
            Compare(Rle.code(new List <uint> {
                4711, 4711, 4811
            }), new List <Rle> {
                new Rle(
                    4711,
                    0,
                    2,
                    true
                    ),
                new Rle(
                    4811,
                    1,
                    1,
                    true
                    )
            });
            Compare(Rle.code(new List <uint> {
                4711, 4711, 4811, 4711, 4711
            }), new List <Rle> {
                new Rle(
                    4711,
                    0,
                    2,
                    true
                    ),
                new Rle(
                    4811,
                    1,
                    1,
                    true
                    ),
                new Rle(
                    4711,
                    2,
                    2,
                    true
                    )
            });
            Compare(Rle.code(new List <uint> {
                4711, 4711, 4711, 4811, 4711, 4711
            }), new List <Rle> {
                new Rle(
                    4711,
                    0,
                    3,
                    true
                    ),
                new Rle(
                    4811,
                    1,
                    1,
                    true
                    ),
                new Rle(
                    4711,
                    2,
                    2,
                    false
                    )
            }
                    );
            Compare(Rle.code(new List <uint> {
                4711, 4711, 4711, 4811, 4711, 4711, 4911, 4911, 4911
            }), new List <Rle> {
                new Rle(
                    4711,
                    0,
                    3,
                    true
                    ),
                new Rle(
                    4811,
                    1,
                    1,
                    true
                    ),
                new Rle(
                    4711,
                    2,
                    2,
                    false
                    ),
                new Rle(
                    4911,
                    3,
                    3,
                    true
                    )
            });


            Compare(Rle.code(new List <uint> {
                0x2001, 0x888, 0, 0x6630, 0, 0, 0, 0
            }), new List <Rle> {
                new Rle(0x2001, 0, 1, true),
                new Rle(0x888, 1, 1, true),
                new Rle(0, 2, 1, false),
                new Rle(0x6630, 3, 1, true),
                new Rle(0, 4, 4, true)
            });
        }
Exemple #11
0
        public static void TestWhole()
        {
            // down sample
            List <List <double> > data = new List <List <double> >();

            for (int i = 0; i < bigtestdata.GetLength(0); i++)
            {
                if (i % 2 != 0)
                {
                    continue;
                }
                List <double> row = new List <double>();
                for (int j = 0; j < bigtestdata.GetLength(1); j++)
                {
                    if (j % 2 != 0)
                    {
                        continue;
                    }
                    row.Add(bigtestdata[i, j]);
                }
                data.Add(row);
            }
            double[,] narr = new double[8, 8];
            for (int i = 0; i < data.Count; i++)
            {
                for (int j = 0; j < data[i].Count; j++)
                {
                    narr[i, j] = data[i][j];
                }
            }
            print("Down Sampled");
            narr.PrintArray();

            Dct dct     = new Dct();
            var dctdata = dct.Go(narr);

            print("dct go");
            dctdata.PrintArray();

            Quantizer.QuantizeLuminance(ref dctdata);
            print("quantized");
            dctdata.PrintArray();

            var            zagged  = ZigZag.Run(dctdata);
            var            bzagged = zagged.Select(x => (sbyte)x).ToArray();
            List <sbyte[]> zaglist = new List <sbyte[]>()
            {
                bzagged
            };

            sbyte[] rled = Rle.BetterEncode(zaglist);

            // reversing
            var unrle = Rle.DecodeRle(rled);
            var unzag = ZigZag.Inverse(unrle[0].Select(x => (int)x).ToArray());

            Quantizer.InverseQuantizeLuminance(ref unzag);
            var idct = dct.GoBack(unzag);

            idct.PrintArray();
        }