Exemple #1
0
        public static CompressedImage Load(string path)
        {
            var result = new CompressedImage();

            using (var sr = new FileStream(path, FileMode.Open))
            {
                var buffer = new byte[8];

                sr.Read(buffer, 0, 4);
                result.Width = BitConverter.ToInt32(buffer, 0);

                sr.Read(buffer, 0, 4);
                result.Height = BitConverter.ToInt32(buffer, 0);

                sr.Read(buffer, 0, 4);
                result.Quality = BitConverter.ToInt32(buffer, 0);

                sr.Read(buffer, 0, 4);
                var decodeTableSize = BitConverter.ToInt32(buffer, 0);
                result.DecodeTable = new ConcurrentDictionary <BitsWithLength, byte>(new BitsWithLength.Comparer());

                for (var i = 0; i < decodeTableSize; i++)
                {
                    sr.Read(buffer, 0, 4);
                    var bits = BitConverter.ToInt32(buffer, 0);

                    sr.Read(buffer, 0, 4);
                    var bitsCount = BitConverter.ToInt32(buffer, 0);

                    var mappedByte = (byte)sr.ReadByte();
                    result.DecodeTable[new BitsWithLength {
                                           Bits = bits, BitsCount = bitsCount
                                       }] = mappedByte;
                }

                sr.Read(buffer, 0, 8);
                result.BitsCount = BitConverter.ToInt64(buffer, 0);

                sr.Read(buffer, 0, 4);
                var compressedBytesCount = BitConverter.ToInt32(buffer, 0);

                result.CompressedBytes = new byte[compressedBytesCount];
                var totalRead = 0;
                while (totalRead < compressedBytesCount)
                {
                    totalRead += sr.Read(result.CompressedBytes, totalRead, compressedBytesCount - totalRead);
                }
            }
            return(result);
        }
Exemple #2
0
        public static void Decompress(string inputFile, int threadsCount, int compressionQuality)
        {
            try
            {
                var uncompressedFileName = inputFile + ".uncompressed." + compressionQuality + ".bmp";
                var compressedImage      = CompressedImage.Load(inputFile);
                var uncompressedImage    = Uncompress(compressedImage, threadsCount);

                var grayscaleBmp = GrayscaleMatrixToBitmap(uncompressedImage);
                grayscaleBmp.Save(uncompressedFileName, ImageFormat.Bmp);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Exemple #3
0
        private static double[,] Uncompress(CompressedImage image, int degreeOfParallelism)
        {
            var allQuantizedBytes = HuffmanCodec.HuffmanCodec.Decode(image.CompressedBytes, image.DecodeTable, image.BitsCount, degreeOfParallelism);

            var result = new double[image.Height, image.Width];

            for (var y = 0; y < image.Height; y += DCTSize)
            {
                for (var x = 0; x < image.Width; x += DCTSize)
                {
                    var quantizedBytes = new byte[DCTSize * DCTSize];
                    Array.Copy(allQuantizedBytes, y * image.Width + x * DCTSize, quantizedBytes, 0, quantizedBytes.Length);
                    var quantizedFreqs = ZigZagUnScan(quantizedBytes);
                    var channelFreqs   = DeQuantize(quantizedFreqs, image.Quality);
                    var subMatrix      = DCT.IDCT2D(channelFreqs, degreeOfParallelism);
                    subMatrix.ShiftMatrixValues(128);
                    result.SetSubmatrix(subMatrix, y, x);
                }
            }
            return(result);
        }