public HaarChunk(Chunk chunk, int p) { y = new Complex[Chunk.BLOCKS_COUNT][, ]; cb = new Complex[Chunk.BLOCKS_COUNT][, ]; cr = new Complex[Chunk.BLOCKS_COUNT][, ]; for (int i = 0; i < Chunk.BLOCKS_COUNT; i++) { Y[i] = TransformsUtils.HaarTransform(chunk.Y[i], p); } if (MainForm.isAveragingEnabled) { cb[0] = TransformsUtils.HaarTransform(chunk.Cb[0], p); cr[0] = TransformsUtils.HaarTransform(chunk.Cr[0], p); } else { for (int i = 0; i < Chunk.BLOCKS_COUNT; i++) { cb[i] = TransformsUtils.HaarTransform(chunk.Cb[i], p); cr[i] = TransformsUtils.HaarTransform(chunk.Cr[i], p); } } }
public DCTChunk(Chunk chunk) { y = new float[Chunk.BLOCKS_COUNT][, ]; cb = new float[Chunk.BLOCKS_COUNT][, ]; cr = new float[Chunk.BLOCKS_COUNT][, ]; for (int i = 0; i < Chunk.BLOCKS_COUNT; i++) { Y[i] = TransformsUtils.DCT_1(chunk.Y[i]); } if (MainForm.isAveragingEnabled) { cb[0] = TransformsUtils.DCT_1(chunk.Cb[0]); cr[0] = TransformsUtils.DCT_1(chunk.Cr[0]); } else { for (int i = 0; i < Chunk.BLOCKS_COUNT; i++) { cb[i] = TransformsUtils.DCT_1(chunk.Cb[i]); cr[i] = TransformsUtils.DCT_1(chunk.Cr[i]); } } }
private Image BeginHaarCompress() { Timer timer = Timer.GetInstance(); timer.Clear(); int p = int.Parse(p_textBox.Text); if (!TransformsUtils.haarMatrixes.ContainsKey(p)) { TransformsUtils.BuildHaarMatrix(p); } blockSize = (int)Math.Pow(p, int.Parse(N_textBox.Text)); var chunkSize = blockSize * 2; var originalWidth = sourceImageBox.Image.Width; var originalHeight = sourceImageBox.Image.Height; var rgbImage = new RGBImage(sourceImageBox.Image); var fittedToChunksImage = ImageUtils.FitToBlockSize(rgbImage, chunkSize); var chunks = fittedToChunksImage.ToBlockArray(chunkSize, out int widthBlocks, out int heightBlocks); var haarChunks = new HaarChunk[chunks.Length]; var parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = 4 }; timer.Start(MAIN, TRANSFORM); var flag = Parallel.ForEach(chunks, parallelOptions, (elem, loopState, elementIndex) => { var haarChunk = new HaarChunk(new Chunk(chunks[elementIndex], blockSize), p); haarChunk.ZeroCoeffs(zeroPercentY, zeroPercentCb, zeroPercentCr); haarChunks[elementIndex] = haarChunk; }); while (!flag.IsCompleted) { ; } timer.End(MAIN, TRANSFORM); foreach (HaarChunk chunk in haarChunks) { coeffsCount += chunk.CoeffsCount(); zeroCoeffsCount += chunk.ZeroCoeffsCount(); } var listOfDecompressedImages = new YCbCrImage[haarChunks.Length]; var listOfDecompressedChunks = new Chunk[haarChunks.Length]; var matrix = new YCbCrImage[widthBlocks, heightBlocks]; timer.Start(MAIN, REVERSE_TRANSFORM); flag = Parallel.ForEach(haarChunks, parallelOptions, (elem, loopState, i) => { listOfDecompressedChunks[i] = new Chunk(haarChunks[i], p); listOfDecompressedImages[i] = YCbCrImage.FromChunk(listOfDecompressedChunks[i]); var j = i / widthBlocks; matrix[i - j * widthBlocks, j] = listOfDecompressedImages[i]; }); while (!flag.IsCompleted) { ; } timer.End(MAIN, REVERSE_TRANSFORM); timer.DisplayIntervals(); var decompressedImage = YCbCrImage.FromMatrix(matrix).ToRGBImage(originalWidth, originalHeight); return(decompressedImage.ToImage()); }
public Image Compress(float quantCoeffCbCrDC, float quantCoeffCbCrAC, bool yQuantEnabled, float quantCoeffYDC, float quantCoeffYAC, int blockSize, out int coeffsCount, out int zeroCoeffsCount, int percentY, int percentCb, int percentCr) { BLOCK_SIZE = blockSize; CHUNK_SIZE = blockSize * 2; coeffsCount = 0; zeroCoeffsCount = 0; Timer timer = Timer.GetInstance(); timer.Clear(); timer.Start(MAIN_ID, FULL_COMPRESS); timer.Start(SUB_ID, PREP); var originalWidth = sourceImage.Width; var originalHeight = sourceImage.Height; timer.Start(SUB_ID, TO_RGB_IMAGE); var rgbImage = new RGBImage(sourceImage); timer.End(SUB_ID, TO_RGB_IMAGE); timer.Start(SUB_ID, FITTING); var fittedToChunksImage = ImageUtils.FitToBlockSize(rgbImage, CHUNK_SIZE); timer.End(SUB_ID, FITTING); var chunks = fittedToChunksImage.ToBlockArray(CHUNK_SIZE, out int widthBlocks, out int heightBlocks); var dcCoeffs = new List <int>(); var dCTChunks = new DCTChunk[chunks.Length]; //var quantizeCbCrMatrix = MathUtils.BuildQuantizationMatrix(quantCoeffCbCrAC, quantCoeffCbCrDC, BLOCK_SIZE); //MatrixUtils<int>.ShowMatrix(quantizeCbCrMatrix); float[,] quantizeYMatrix = new float[BLOCK_SIZE, BLOCK_SIZE]; /*if (yQuantEnabled) { * quantizeYMatrix = MathUtils.BuildQuantizationMatrix(quantCoeffYAC, quantCoeffYDC, BLOCK_SIZE); * MatrixUtils<int>.ShowMatrix(quantizeYMatrix); * }*/ timer.End(SUB_ID, PREP); timer.Start(SUB_ID, DCT_OF_CHUNK); TransformsUtils.CountCosMatrixFor(BLOCK_SIZE); var parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = 4 }; var flag = Parallel.ForEach(chunks, parallelOptions, (elem, loopState, elementIndex) => { var dCTChunk = new DCTChunk(new Chunk(chunks[elementIndex], BLOCK_SIZE)); dCTChunk.ZeroCoeffs(percentY, percentCb, percentCr); dCTChunks[elementIndex] = dCTChunk; }); while (!flag.IsCompleted) { ; } timer.End(SUB_ID, DCT_OF_CHUNK); foreach (DCTChunk chunk in dCTChunks) { coeffsCount += chunk.CoeffsCount(); zeroCoeffsCount += chunk.ZeroCoeffsCount(); } var listOfDecompressedImages = new YCbCrImage[dCTChunks.Length]; var listOfDecompressedChunks = new Chunk[dCTChunks.Length]; timer.Start(SUB_ID, REVERSE_DCT); var matrix = new YCbCrImage[widthBlocks, heightBlocks]; flag = Parallel.ForEach(dCTChunks, parallelOptions, (elem, loopState, i) => { listOfDecompressedChunks[i] = new Chunk(dCTChunks[i]); listOfDecompressedImages[i] = YCbCrImage.FromChunk(listOfDecompressedChunks[i]); var j = i / widthBlocks; matrix[i - j * widthBlocks, j] = listOfDecompressedImages[i]; }); while (!flag.IsCompleted) { ; } timer.End(SUB_ID, REVERSE_DCT); var decompressedImage = YCbCrImage.FromMatrix(matrix).ToRGBImage(originalWidth, originalHeight); timer.End(MAIN_ID, FULL_COMPRESS); timer.DisplayIntervals(); return(decompressedImage.ToImage()); }