public static double[,] UncompressWithDCT(this CompressedImage image, Options options) { var result = new double[image.Height, image.Width]; var DCTSize = options.DCTSize; var freqNum = 0; for (var y = 0; y < image.Height; y += DCTSize) { for (var x = 0; x < image.Width; x += DCTSize) { var channelFreqs = new double[DCTSize, DCTSize]; for (var i = 0; i < DCTSize; i++) { for (var j = 0; j < DCTSize; j++) { if (i + j < image.CompressionLevel) { channelFreqs[i, j] = image.Frequences[freqNum++]; } } } var processedSubmatrix = DCTHelper.IDCT2D(channelFreqs); processedSubmatrix.ShiftMatrixValues(128); result.SetSubmatrix(processedSubmatrix, y, x); } } return(result); }
public static double[,] ParallelUncompressWithDCT(this CompressedImage image, Options options) { var result = new double[image.Height, image.Width]; var blocksCount = image.Width * image.Height / (options.DCTSize * options.DCTSize); var freqsCount = Enumerable.Range(1, image.CompressionLevel).Sum(); Parallel.For(0, blocksCount, blockIndex => { var y = blockIndex / (image.Width / options.DCTSize); var x = blockIndex % (image.Width / options.DCTSize); var channelFreqs = new double[options.DCTSize, options.DCTSize]; var freqNum = blockIndex * freqsCount; for (var i = 0; i < options.DCTSize; i++) { for (var j = 0; j < options.DCTSize; j++) { if (i + j < image.CompressionLevel) { channelFreqs[i, j] = image.Frequences[freqNum++]; } } } var processedSubmatrix = DCTHelper.IDCT2D(channelFreqs); processedSubmatrix.ShiftMatrixValues(128); result.SetSubmatrix(processedSubmatrix, y * options.DCTSize, x * options.DCTSize); }); return(result); }
public static CompressedImage CompressWithDCT(this double[,] channelPixels, Options options, int compressionLevel = 4) { var frequencesPerBlock = -1; var DCTSize = options.DCTSize; var height = channelPixels.GetLength(0); var width = channelPixels.GetLength(1); var result = new List <double>(); for (var y = 0; y < height; y += DCTSize) { for (var x = 0; x < width; x += DCTSize) { var subMatrix = channelPixels.GetSubMatrix(y, DCTSize, x, DCTSize, DCTSize); subMatrix.ShiftMatrixValues(-128); var channelFreqs = DCTHelper.DCT2D(subMatrix); frequencesPerBlock = DCTSize * DCTSize; for (var i = 0; i < DCTSize; i++) { for (var j = 0; j < DCTSize; j++) { if (i + j < compressionLevel) { result.Add(channelFreqs[i, j]); continue; } channelFreqs[i, j] = 0; frequencesPerBlock--; } } } } return(new CompressedImage { CompressionLevel = compressionLevel, FrequencesPerBlock = frequencesPerBlock, Frequences = result, Height = height, Width = width }); }
private static List <double> GetFrequencesFromSubmatrix(double[,] channelPixels, int DCTSize, int compressionLevel, int y, int x) { var subMatrix = channelPixels.GetSubMatrix(y, DCTSize, x, DCTSize, DCTSize); subMatrix.ShiftMatrixValues(-128); var localResult = new List <double>(); var channelFreqs = DCTHelper.DCT2D(subMatrix); for (var i = 0; i < DCTSize; i++) { for (var j = 0; j < DCTSize; j++) { if (i + j < compressionLevel) { localResult.Add(channelFreqs[i, j]); continue; } channelFreqs[i, j] = 0; } } return(localResult); }