Exemplo n.º 1
0
        private static Matrix Uncompress(CompressedImage image)
        {
            var result = new Matrix(image.Height, image.Width);

            using (var allQuantizedBytes =
                       new MemoryStream(HuffmanCodec.Decode(image.CompressedBytes, image.DecodeTable, image.BitsCount)))
            {
                for (var y = 0; y < image.Height; y += DCTSize)
                {
                    for (var x = 0; x < image.Width; x += DCTSize)
                    {
                        var _y = new double[DCTSize, DCTSize];
                        var cb = new double[DCTSize, DCTSize];
                        var cr = new double[DCTSize, DCTSize];
                        foreach (var channel in new [] { _y, cb, cr })
                        {
                            var quantizedBytes = new byte[DCTSize * DCTSize];
                            allQuantizedBytes.ReadAsync(quantizedBytes, 0, quantizedBytes.Length).Wait();
                            var quantizedFreqs = ZigZagUnScan(quantizedBytes);
                            var channelFreqs   = DeQuantize(quantizedFreqs, image.Quality);
                            DCT.IDCT2D(channelFreqs, channel);
                            ShiftMatrixValues(channel, 128);
                        }
                        SetPixels(result, _y, cb, cr, PixelFormat.YCbCr, y, x);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        private static Matrix Uncompress(CompressedImage image)
        {
            var result = new Matrix(image.Height, image.Width, image.RealHeight, image.RealWidth);

            using (var allQuantizedBytes =
                       new MemoryStream(HuffmanCodec.Decode(image.CompressedBytes, image.DecodeTable, image.BitsCount)))
            {
                Parallel.ForEach(Enumerable.Range(0, image.Height / DCTSize).Select(y => y * DCTSize), y =>
                {
                    for (var x = 0; x < image.Width; x += DCTSize)
                    {
                        var _y = new double[DCTSize, DCTSize];
                        var cb = new double[DCTSize, DCTSize];
                        var cr = new double[DCTSize, DCTSize];
                        for (var i = 0; i < 3; i++)
                        {
                            var quantizedBytes         = new byte[DCTSize * DCTSize];
                            allQuantizedBytes.Position = (x * 8 + y * image.Width) * 3 + 64 * i;
                            allQuantizedBytes.ReadAsync(quantizedBytes, 0, quantizedBytes.Length).Wait();
                            var quantizedFreqs = ZigZagUnScan(quantizedBytes);
                            var channelFreqs   = DeQuantize(quantizedFreqs, image.Quality);
                            DCT.IDCT2D(channelFreqs);
                            ShiftMatrixValues(channelFreqs, 128);
                            switch (i)
                            {
                            case 0:
                                _y = channelFreqs;
                                break;

                            case 1:
                                cb = channelFreqs;
                                break;

                            default:
                                cr = channelFreqs;
                                break;
                            }
                        }

                        SetPixels(result, _y, cb, cr, PixelFormat.YCbCr, y, x);
                    }
                });
            }

            return(result);
        }
Exemplo n.º 3
0
        private static CbCrImage Uncompress(CompressedImage image)
        {
            var result                    = new CbCrImage(image.Height, image.Width);
            var DCTcoefficients           = DCT.GetCoefficientsMatrix(DCTSize, DCTSize);
            var transponseDCTcoefficients = DCT.Transpose(DCTcoefficients);
            var quantizationMatrix        = GetQuantizationMatrix(image.Quality);

            using (var allQuantizedBytes =
                       new MemoryStream(HuffmanCodec.Decode(image.CompressedBytes, image.DecodeTable, image.BitsCount)))
            {
                Parallel.For(0, image.Height / DCTSize, ky =>
                {
                    var y = ky;
                    Parallel.For(0, image.Width / DCTSize, _x =>
                    {
                        var x  = _x;
                        var _y = new double[DCTSize, DCTSize];
                        var cb = new double[DCTSize, DCTSize];
                        var cr = new double[DCTSize, DCTSize];
                        var c  = 0;
                        foreach (var channel in new[] { _y, cb, cr })
                        {
                            var quantizedBytes = new byte[DCTSize * DCTSize];
                            lock (allQuantizedBytes)
                            {
                                allQuantizedBytes.Seek((y * (image.Width / 8) + x) * 8 * 8 * 3 + 8 * 8 * c, SeekOrigin.Begin);
                                allQuantizedBytes.ReadAsync(quantizedBytes, 0, quantizedBytes.Length).Wait();
                            }
                            c++;
                            var quantizedFreqs = ZigZagUnScan(quantizedBytes);
                            var channelFreqs   = DeQuantize(quantizedFreqs, quantizationMatrix);
                            DCT.IDCT2D(channelFreqs, channel, DCTcoefficients, transponseDCTcoefficients);
                        }
                        SetPixels(result, _y, cb, cr, y * DCTSize, x * DCTSize, 128);
                    });
                });
            }

            return(result);
        }
Exemplo n.º 4
0
        private Matrix Decompress(CompressedImage image)
        {
            if (image.Quality != compressionQuality)
            {
                quantizationMatrix = GetQuantizationMatrix(image.Quality);
            }

            var matrix            = new Matrix(image.Height, image.Width, PixelFormat.YCbCr);
            var allQuantizedBytes = HuffmanCodec.Decode(image.CompressedBytes, image.DecodeTable, image.BitsCount);
            var heigth            = image.Height / DCTSize;
            var width             = image.Width / DCTSize;
            var dct = new DCT(DCTSize);

            Parallel.For(0, heigth, h =>
            {
                Parallel.For(0, width, w =>
                {
                    var y           = new double[DCTSize, DCTSize];
                    var cb          = new double[DCTSize, DCTSize];
                    var cr          = new double[DCTSize, DCTSize];
                    var channels    = new[] { y, cb, cr };
                    var blockLength = DCTSize * DCTSize * channels.Length;
                    var readPos     = h * blockLength * width + w * blockLength;
                    for (var i = 0; i < channels.Length; i++)
                    {
                        var channel        = channels[i];
                        var channelReadPos = readPos + i * DCTSize * DCTSize;
                        var quantizedBytes = allQuantizedBytes.ReadFrom(channelReadPos, DCTSize * DCTSize);
                        var quantizedFreqs = ZigZagUnScan(quantizedBytes);
                        var channelFreqs   = DeQuantize(quantizedFreqs);
                        dct.IDCT2D(channelFreqs, channel);
                        channel.ShiftValues(128);
                    }
                    matrix.SetPixels(y, cb, cr, h * DCTSize, w * DCTSize);
                });
            });
            return(matrix);
        }
Exemplo n.º 5
0
        public static Matrix Uncompress(CompressedImage image)
        {
            var result    = new Matrix(image.Height, image.Width);
            var container = new byte[image.Height * image.Width * 3];

            using (var allQuantizedBytes =
                       new MemoryStream(HuffmanCodec.Decode(image.CompressedBytes, image.DecodeTable, image.BitsCount)))
                allQuantizedBytes.Read(container);

            var qm = GetQuantizationMatrix(image.Quality);

            Parallel.For(0, image.Height / 8, i =>
            {
                var y = i * 8;
                for (var x = 0; x < image.Width; x += DCTSize)
                {
                    var channels = new[]
                    { new double[DCTSize, DCTSize], new double[DCTSize, DCTSize], new double[DCTSize, DCTSize] };
                    for (var index = 0; index < 3; index++)
                    {
                        var channel        = channels[index];
                        var quantizedBytes = new byte[DCTSize * DCTSize];
                        Array.Copy(container, (index + 3 * i * image.Width / 8 + 3 * x / 8) * 64, quantizedBytes, 0,
                                   64);
                        var quantizedFreqs = ZigZagUnScan(quantizedBytes);
                        var channelFreqs   = DeQuantize(quantizedFreqs, qm);
                        DCT.IDCT2D(channelFreqs, channel);
                        ShiftMatrixValues(channel, 128);
                    }

                    SetPixels(result, channels[0], channels[1], channels[2], PixelFormat.YCbCr, y, x);
                }
            });


            return(result);
        }
Exemplo n.º 6
0
        private static Matrix Uncompress(CompressedImage image)
        {
            var result = new Matrix(image.Height, image.Width);
//            using (var allQuantizedBytes =
//                new MemoryStream(HuffmanCodec.Decode(image.CompressedBytes, image.DecodeTable, image.BitsCount)))
//            {
//                for (var y = 0; y < image.Height; y += DCTSize)
//                for (var x = 0; x < image.Width; x += DCTSize)
//                {
//                    var _y = new double[DCTSize, DCTSize];
//                    var cb = new double[DCTSize, DCTSize];
//                    var cr = new double[DCTSize, DCTSize];
//                    foreach (var channel in new[] {_y, cb, cr})
//                    {
//                        var quantizedBytes = new byte[DCTSize * DCTSize];
//                        allQuantizedBytes.ReadAsync(quantizedBytes, 0, quantizedBytes.Length).Wait();
//                        var quantizedFreqs = ZigZagUnScan(quantizedBytes);
//                        var channelFreqs = DeQuantize(quantizedFreqs, image.Quality);
//                        DCT.IDCT2D(channelFreqs, channel);
//                        ShiftMatrixValues(channel, 128);
//                    }
//
//                    SetPixels(result, _y, cb, cr, PixelFormat.YCbCr, y, x);
//                }
//            }

            var decodedBytesArray = HuffmanCodec.Decode(image.CompressedBytes, image.DecodeTable, image.BitsCount);
            var tuples            = new List <(int, int)>();

            for (var y = 0; y < image.Height; y += DCTSize)
            {
                for (var x = 0; x < image.Width; x += DCTSize)
                {
                    tuples.Add((x, y));
                }
            }

//            var allQuantizedBytes = new List<byte>[tuples.Count];

            Parallel.For(0, tuples.Count, i =>
            {
                var(x, y) = tuples[i];
                var _y    = new double[DCTSize, DCTSize];
                var cb    = new double[DCTSize, DCTSize];
                var cr    = new double[DCTSize, DCTSize];

                var offset = 0;
                foreach (var channel in new[] { _y, cb, cr })
                {
                    var quantizedBytes = new byte[DCTSize * DCTSize];
                    Buffer.BlockCopy(decodedBytesArray, quantizedBytes.Length * (i * 3 + offset), quantizedBytes, 0, quantizedBytes.Length);
                    var quantizedFreqs = ZigZagUnScan(quantizedBytes);
                    var channelFreqs   = DeQuantize(quantizedFreqs, image.Quality);
                    DCT.IDCT2D(channelFreqs, channel);
                    ShiftMatrixValues(channel, 128);
                    offset++;
                }

                SetPixels(result, _y, cb, cr, PixelFormat.YCbCr, y, x);
            });

            return(result);
        }