Example #1
0
        public void ForwardTest()
        {
            double[,] input =
            {
                { 0.996078431372549, 0.996078431372549,  0.996078431372549, 0.996078431372549,  0.996078431372549,  0.996078431372549, 0.996078431372549, 0.996078431372549 },
                { 0.996078431372549, 0.996078431372549,  0.862745098039216,                 0,  0.662745098039216,  0.996078431372549, 0.996078431372549, 0.996078431372549 },
                { 0.996078431372549, 0.996078431372549,  0.529411764705882, 0.129411764705882,  0.262745098039216,  0.996078431372549, 0.996078431372549, 0.996078431372549 },
                { 0.996078431372549, 0.996078431372549, 0.0627450980392157, 0.662745098039216, 0.0627450980392157,  0.862745098039216, 0.996078431372549, 0.996078431372549 },
                { 0.996078431372549, 0.662745098039216,                  0,                 0,                  0,  0.529411764705882, 0.996078431372549, 0.996078431372549 },
                { 0.996078431372549, 0.262745098039216,  0.529411764705882, 0.996078431372549,  0.729411764705882, 0.0627450980392157, 0.996078431372549, 0.996078431372549 },
                { 0.862745098039216,                 0,  0.929411764705882, 0.996078431372549,  0.996078431372549,  0.129411764705882, 0.662745098039216, 0.996078431372549 },
                { 0.996078431372549, 0.996078431372549,  0.996078431372549, 0.996078431372549,  0.996078431372549,  0.996078431372549, 0.996078431372549, 0.996078431372549 }
            };

            CosineTransform.DCT(input);

            double[,] actual = input;

            double[,] expected =
            {
                {  6.1917, -0.3411,  1.2418,  0.1492,  0.1583,  0.2742, -0.0724,  0.0561 },
                {  0.2205,  0.0214,  0.4503,  0.3947, -0.7846, -0.4391,  0.1001, -0.2554 },
                {  1.0423,  0.2214, -1.0017, -0.2720,  0.0789, -0.1952,  0.2801,  0.4713 },
                { -0.2340, -0.0392, -0.2617, -0.2866,  0.6351,  0.3501, -0.1433,  0.3550 },
                {  0.2750,  0.0226,  0.1229,  0.2183, -0.2583, -0.0742, -0.2042, -0.5906 },
                {  0.0653,  0.0428, -0.4721, -0.2905,  0.4745,  0.2875, -0.0284, -0.1311 },
                {  0.3169,  0.0541, -0.1033, -0.0225, -0.0056,  0.1017, -0.1650, -0.1500 },
                { -0.2970, -0.0627,  0.1960,  0.0644, -0.1136, -0.1031,  0.1887,  0.1444 },
            };

            Assert.IsTrue(actual.IsEqual(expected, 0.05));
        }
Example #2
0
        /// <summary>
        /// Main Jpeg encode
        /// </summary>
        /// <param name="source">source data</param>
        /// <param name="qf">quality factor</param>
        /// <returns>encoded bytes</returns>
        public byte[] Encode(byte[] source, int qf)
        {
            // divide into 8x8
            Divide8x8Block(source);

            // do dct for every 8x8 block
            _blocks.ForEach(block =>
            {
                block     = block.Subtract(128);
                var array = block.ToArray();
                CosineTransform.DCT(array);
                var matrix = Matrix <double> .Build.DenseOfArray(array);
                _dctedBlocks.Add(matrix);
            });

            // do quantization
            _dctedBlocks.ForEach(dctedBlocks =>
            {
                Quantize(dctedBlocks, qf);
                _quantizedBlocks.Add(dctedBlocks);
            });

            // do RLE
            List <RLEBlock> rleBlocks = _rlEncoder.Encode(_quantizedBlocks);

            // construct code word by RLE
            return(Utility.BoolArrayToByteArray(CodeWordEncoder.ConstructCodeWord(rleBlocks)));
        }
Example #3
0
        public static double[] Transform(double[] functionPoints, string transformate)
        {
            var copyofFunctionsPoints = functionPoints;


            switch (transformate)
            {
            case "FFT":
                var complexArray = Array.ConvertAll(copyofFunctionsPoints, x => new Complex(x, 0));

                FourierTransform.DFT(complexArray, FourierTransform.Direction.Forward);

                copyofFunctionsPoints = Array.ConvertAll(complexArray, z => z.Real);
                break;

            case "IFFT":
                var complexArray2 = Array.ConvertAll(copyofFunctionsPoints, x => new Complex(x, 0));

                FourierTransform.DFT(complexArray2, FourierTransform.Direction.Backward);

                copyofFunctionsPoints = Array.ConvertAll(complexArray2, z => z.Real);
                break;


            case "DST":
                SineTransform.DST(copyofFunctionsPoints);
                break;

            case "IDST":
                SineTransform.IDST(copyofFunctionsPoints);
                break;

            case "DCT":
                CosineTransform.DCT(copyofFunctionsPoints);
                break;

            case "IDCT":
                CosineTransform.IDCT(copyofFunctionsPoints);
                break;

            case "DHT":
                HartleyTransform.DHT(copyofFunctionsPoints);
                break;

            case "FHT":
                HilbertTransform.FHT(copyofFunctionsPoints,
                                     FourierTransform.Direction.Forward);
                break;

            case "IFHT":
                HilbertTransform.FHT(copyofFunctionsPoints,
                                     FourierTransform.Direction.Backward);
                break;

            default:
                throw new ArgumentException("Unknown transformation!");
            }
            return(copyofFunctionsPoints); //athenia programuje//dididididi//di/kocham PaciA// JJKAKAKK  K
        }
Example #4
0
        public FrequencyData(WaveData waveData, int blockIndex)
        {
            var offset = blockIndex * BlockSize;

            Parallel.For(0, BlockSize, i =>
            {
                Spectrum[i] = waveData.Samples[offset + i];
            });

            CosineTransform.DCT(Spectrum);
            CalculatePowerSpectrum();
        }
Example #5
0
        public double[] ComputeMfcc12D(ReadOnlySpan <float> samples)
        {
            var filtered = MelSpectrum(samples);

            CosineTransform.DCT(filtered);

            const int resultDimension = 12;
            var       result          = new double[resultDimension];

            // Min は応急処置(頭が悪い)
            Array.Copy(filtered, 1, result, 0, Math.Min(resultDimension, filtered.Length - 1));

            return(result);
        }
        private void EmbedWatermark(double[,] data)
        {
            double[,] watermarkData = new double[_watermarkPixels.Width, _watermarkPixels.Height];

            //convet 32*32 watermark picture to black and white only (102->black(0);922->wthite(255))
            for (int x = 0; x < _watermarkPixels.Width; x++)
            {
                for (int y = 0; y < _watermarkPixels.Height; y++)
                {
                    watermarkData[x, y] = _watermarkPixels.R[x, y] > 125 ? 255 : 0;//convert to black or white only
                }
            }

            //test
            //ParallelHaar.FWT(data, 2);
            //var subband = LL2(data);
            var subband = data;

            Parallel.For(0, _watermarkPixels.Height, y =>        //height = 32
            {
                for (int x = 0; x < _watermarkPixels.Width; x++) //width = 32
                {
                    //BlockSize = 4
                    var block = subband.Submatrix(x * BlockSize, x * BlockSize + BlockSize - 1, y * BlockSize, y * BlockSize + BlockSize - 1);

                    CosineTransform.DCT(block);//DCT here

                    var midbandSum = Math.Max(2, Math.Abs(MidBand(block).Sum()));
                    var sigma      = (watermarkData[x, y] > 125 ? 3 : -3);

                    block[1, 2] += midbandSum * sigma;
                    block[2, 0] += midbandSum * sigma;
                    block[2, 1] += midbandSum * sigma;
                    block[2, 2] += midbandSum * sigma;

                    CosineTransform.IDCT(block);

                    for (int i = 0; i < BlockSize; i++)
                    {
                        for (int j = 0; j < BlockSize; j++)
                        {
                            subband[x * BlockSize + i, y * BlockSize + j] = block[i, j];
                        }
                    }
                }
            });

            BackApplySubBand(data, subband);
            ParallelHaar.IWT(data, 2);
        }//end EmbedWatermark
        private void EmbedWatermark(double[,] data)
        {
            double[,] watermarkData = new double[_watermarkPixels.Width, _watermarkPixels.Height];
            for (int x = 0; x < _watermarkPixels.Width; x++)
            {
                for (int y = 0; y < _watermarkPixels.Height; y++)
                {
                    watermarkData[x, y] = _watermarkPixels.R[x, y] > 125 ? 255 : 0;
                }
            }

            ParallelHaar.FWT(data, 2);
            var subband = LL2(data);

            Parallel.For(0, _watermarkPixels.Height, y =>
            {
                for (int x = 0; x < _watermarkPixels.Width; x++)
                {
                    var block = subband.Submatrix(x * BlockSize, x * BlockSize + BlockSize - 1, y * BlockSize, y * BlockSize + BlockSize - 1);

                    CosineTransform.DCT(block);

                    var midbandSum = Math.Max(2, Math.Abs(MidBand(block).Sum()));
                    var sigma      = (watermarkData[x, y] > 125 ? 3 : -3);

                    block[1, 2] += midbandSum * sigma;
                    block[2, 0] += midbandSum * sigma;
                    block[2, 1] += midbandSum * sigma;
                    block[2, 2] += midbandSum * sigma;

                    CosineTransform.IDCT(block);

                    for (int i = 0; i < BlockSize; i++)
                    {
                        for (int j = 0; j < BlockSize; j++)
                        {
                            subband[x * BlockSize + i, y * BlockSize + j] = block[i, j];
                        }
                    }
                }
            });

            BackApplySubBand(data, subband);
            ParallelHaar.IWT(data, 2);
        }
Example #8
0
        static void Main(string[] args)
        {
            const int P = 25;

            Console.WriteLine($"P - {P}");

            int[,] F1Array =
            {
                { 3, 0 },
                { 3, 1 },
                { 3, 2 },
                { 4, 0 },
                { 4, 1 },
                { 4, 2 },
                { 5, 0 },
                { 5, 1 }
            };//координати коефіцієнтів F1
            int[,] F2Array =
            {
                { 0, 3 },
                { 1, 3 },
                { 2, 3 },
                { 0, 4 },
                { 1, 4 },
                { 2, 4 },
                { 0, 5 },
                { 1, 5 }
            };                                           //координати коефіцієнтів F2

            int[] CharByte = { 0, 1, 0, 0, 0, 0, 0, 1 }; //буква в бітах

            double[,] matrix =
            {
                { 71, 61, 59, 59,  68, 70, 93,  73 },
                { 68, 67, 71, 69,  76, 76, 91,  80 },
                { 80, 77, 81, 83,  88, 84, 84,  90 },
                { 93, 83, 84, 91, 101, 91, 97,  97 },
                { 85, 80, 80, 87,  94, 91, 97, 101 },
                { 77, 76, 78, 78,  76, 76, 93,  99 },
                { 70, 73, 79, 76,  67, 65, 85,  95 },
                { 74, 80, 91, 83,  65, 56, 79, 102 },
            };

            double[,] original = new double[matrix.GetLength(0), matrix.GetLength(1)];
            double[,] embedBit = new double[matrix.GetLength(0), matrix.GetLength(1)];
            double[,] DCT      = new double[matrix.GetLength(0), matrix.GetLength(1)];

            Array.Copy(matrix, original, matrix.Length);



            if (F1Array.GetLength(0) != F2Array.GetLength(0) || F1Array.GetLength(0) != CharByte.Length)
            {
                Console.WriteLine("F1Array F2Array та CharByte мають бути однакової довжини!!!");
                Console.ReadLine();
                return;
            }



            Console.WriteLine("\n                  massage");
            WriteArrayToConsole(CharByte);


            Console.WriteLine("\n                  original");
            WriteArrayToConsole(matrix);

            CosineTransform.DCT(matrix);
            Console.WriteLine("\n                  DCT");
            WriteArrayToConsole(matrix);

            Array.Copy(matrix, DCT, DCT.Length);

            /*
             * for (int i = 0; i < CharByte.Length; i++)
             * {
             *  matrix = Koha_Zhao.dkp_bit_embed(matrix, CharByte[i], F1Array[i,0], F1Array[i, 1], F2Array[i, 0], F2Array[i, 1], P);
             * }*/

            matrix = Koha_Zhao.dkp_bit_embed(matrix, CharByte[0], 5, 4, 4, 5, P);

            Console.WriteLine("\n                  embed bit");
            WriteArrayToConsole(matrix);

            Array.Copy(matrix, embedBit, embedBit.Length);

            CosineTransform.IDCT(matrix);
            Console.WriteLine("\n                  IDCT");
            WriteArrayToConsole(matrix);

            Console.WriteLine("\n                  AverageAbsoluteDifference");
            Console.WriteLine(Statistics.AverageAbsoluteDifference(original, matrix));
            Console.WriteLine("\n                  MeanSquareError");
            Console.WriteLine(Statistics.MeanSquareError(original, matrix));
            Console.WriteLine("\n                  SignalToNoiseRatio");
            Console.WriteLine(Statistics.SignalToNoiseRatio(original, matrix));
            Console.WriteLine("\n                  ImageFidelity");
            Console.WriteLine(Statistics.ImageFidelity(original, matrix));
            Console.WriteLine("\n                  NormalizedCrossCorrelation");
            Console.WriteLine(Statistics.NormalizedCrossCorrelation(original, matrix));
            Console.WriteLine("\n                  CorrelationQuality");
            Console.WriteLine(Statistics.CorrelationQuality(original, matrix));
            Console.WriteLine("\n                  StructuralContent");
            Console.WriteLine(Statistics.StructuralContent(original, matrix));

            string data = FillInTheVariable(CharByte, original, DCT, embedBit, matrix, Statistics.AverageAbsoluteDifference(original, matrix), Statistics.MeanSquareError(original, matrix), Statistics.SignalToNoiseRatio(original, matrix), Statistics.ImageFidelity(original, matrix), Statistics.NormalizedCrossCorrelation(original, matrix), Statistics.CorrelationQuality(original, matrix), Statistics.StructuralContent(original, matrix), P);

            // создаем каталог для файла
            string        path    = @"C:\DCT";
            DirectoryInfo dirInfo = new DirectoryInfo(path);

            if (!dirInfo.Exists)
            {
                dirInfo.Create();
            }

            SaveFile(@"C:\DCT\lab6.txt", data);

            Console.Read();
        }
        private WatermarkResult RetrieveWatermark(double[,] data)
        {
            var origWidth  = data.GetUpperBound(0) + 1;
            var origHeight = data.GetUpperBound(1) + 1;

            if (DiffWidth - origWidth > 0 || DiffHeight - origHeight > 0)
            {
                var top    = (DiffHeight - origHeight) / 2;
                var left   = (DiffWidth - origWidth) / 2;
                var bottom = (DiffHeight - origHeight) - top;
                var right  = (DiffWidth - origWidth) - left;
                data = data.Pad(left, top, right, bottom);
            }

            double[,] recoveredWatermarkData = new double[_watermarkPixels.Width, _watermarkPixels.Height];

            ParallelHaar.FWT(data, 2);
            var subband = LL2(data);

            var width  = subband.GetUpperBound(0) + 1;
            var height = subband.GetUpperBound(1) + 1;

            Parallel.For(0, _watermarkPixels.Height, y =>
            {
                for (int x = 0; x < _watermarkPixels.Width; x++)
                {
                    if (x * BlockSize + BlockSize > width)
                    {
                        return;
                    }
                    if (y * BlockSize + BlockSize > height)
                    {
                        return;
                    }

                    var block = subband.Submatrix(x * BlockSize, x * BlockSize + BlockSize - 1, y * BlockSize, y * BlockSize + BlockSize - 1);

                    CosineTransform.DCT(block);
                    recoveredWatermarkData[x, y] = (MidBand(block).Sum() > 0) ? 255 : 0;
                }
            });

            double similiar = 0;
            double total    = (width / 4) * (height / 4);

            for (int x = 0; x < _watermarkPixels.Width; x++)
            {
                for (int y = 0; y < _watermarkPixels.Height; y++)
                {
                    var oldValue = _watermarkPixels.R[x, y] > 125 ? 255 : 0;
                    if (recoveredWatermarkData[x, y] == oldValue)
                    {
                        similiar++;
                    }
                }
            }

            var similarity = Math.Round((similiar / total) * 100);

            var recoveredData           = new RgbData(recoveredWatermarkData);
            var recoveredWatermarkBytes = _imageHelper.SavePixels(recoveredData);

            return(new WatermarkResult(similarity, recoveredWatermarkBytes));
        }
Example #10
0
        public static Complex[] Transform(Complex[] functionPoints, string transformate)
        {
            var copyofFunctionsPoints = functionPoints;


            var copyofFunctionsPoints2 = new Complex[functionPoints.Length];
            var multidimensialArray    = new double[functionPoints.Length, 2];

            var jaggedArray = new double[functionPoints.Length][];

            for (var i = 0; i < functionPoints.Length; i++)
            {
                jaggedArray[i] = new double[2];
            }


            for (var i = 0; i < functionPoints.Length; i++)
            {
                jaggedArray[i][0]             = multidimensialArray[i, 0] = functionPoints[i].Real;
                jaggedArray[i][1]             =
                    multidimensialArray[i, 1] = functionPoints[i].Imaginary;
                copyofFunctionsPoints2[i]     = new Complex(functionPoints[i].Real,
                                                            copyofFunctionsPoints2[i].Imaginary);
            }


            switch (transformate)
            {
            case "FFT":
                Fourier.Forward(copyofFunctionsPoints);
                break;


            case "IFFT":
                Fourier.Inverse(copyofFunctionsPoints);
                break;

            case "DST":
                SineTransform.DST(jaggedArray);
                copyofFunctionsPoints = jaggedToComplex(jaggedArray);
                break;

            case "IDST":
                SineTransform.IDST(jaggedArray);
                copyofFunctionsPoints = jaggedToComplex(jaggedArray);
                break;

            case "DCT":
                CosineTransform.DCT(multidimensialArray);
                copyofFunctionsPoints = multidimensialToComplex(multidimensialArray);
                break;

            case "IDCT":
                CosineTransform.IDCT(multidimensialArray);
                copyofFunctionsPoints = multidimensialToComplex(multidimensialArray);
                break;

            case "DHT":
                HartleyTransform.DHT(multidimensialArray);
                copyofFunctionsPoints = multidimensialToComplex(multidimensialArray);
                break;

            case "FHT":
                HilbertTransform.FHT(copyofFunctionsPoints2,
                                     FourierTransform.Direction.Forward);
                copyofFunctionsPoints = copyofFunctionsPoints2;
                break;

            case "IFHT":
                HilbertTransform.FHT(copyofFunctionsPoints2,
                                     FourierTransform.Direction.Backward);
                copyofFunctionsPoints = copyofFunctionsPoints2;
                break;

            default:
                throw new ArgumentException("Unknown transformation!");
            }
            return(copyofFunctionsPoints); //athenia programuje//dididididi//di/kocham PaciA// JJKAKAKK  K
        }
        private void MergeAndCompress_btn_Click(object sender, EventArgs e)
        {
            System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            Bitmap sceneBitmap  = new Bitmap(SceneImage);
            Bitmap spriteBitmap = new Bitmap(SpriteImage);

            resultBitmap = sceneBitmap;

            int scenemidwidth   = sceneBitmap.Width / 2;
            int scenemidtheight = sceneBitmap.Height / 2;

            int Itstartx = scenemidwidth - (spriteBitmap.Width / 2);
            int Itstarty = scenemidtheight - (spriteBitmap.Height / 2);

            //textBox1.Text = Itstartx.ToString();
            //textBox2.Text = Itstarty.ToString();

            for (int y = 0; y < spriteBitmap.Height; y++)
            {
                for (int x = 0; x < spriteBitmap.Width; x++)
                {
                    Color spritecolor = spriteBitmap.GetPixel(x, y);

                    if (spritecolor.G != 0)
                    {
                        resultBitmap.SetPixel(x + Itstartx, y + Itstarty, spritecolor);
                    }
                }
            }

            MergedPreviewBox.Image = resultBitmap.GetThumbnailImage(MergedPreviewBox.Width, MergedPreviewBox.Height, myCallback, IntPtr.Zero);



            //////////
            ///////////////MERGING    DONE
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////
///////////////Start of Compression part
////////////////////////////////////////////////////////////////////////////////////////////////////////////////


            //////////
            ///////////////Convert to YUV
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////



            //////////
            /////////////// add some pixels on both axis resulting multiple of 8
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            int zeroExtensionsX = 8 - (resultBitmap.Width % 8);
            int zeroExtensionsY = 8 - (resultBitmap.Height % 8);

            MyOwnYcbcr = new myYcBc(resultBitmap.Width + zeroExtensionsX, resultBitmap.Height + zeroExtensionsY);

            for (int y = 0; y < resultBitmap.Height; y++)
            {
                for (int x = 0; x < resultBitmap.Width; x++)
                {
                    Color resultcolor = resultBitmap.GetPixel(x, y);

                    Yuv ycrcb = new Yuv();

                    RGB rgbcolor = new RGB(resultcolor);

                    ycrcb.Yf = YCbCr.FromRGB(rgbcolor).Y * 255;

                    //4:2:0 sampling
                    if (y % 2 == 0 && x % 2 == 0)
                    {
                        ycrcb.Uf = YCbCr.FromRGB(rgbcolor).Cr * 255;
                        ycrcb.Vf = YCbCr.FromRGB(rgbcolor).Cb * 255;
                    }
                    else
                    {
                        ycrcb.Uf = 0;
                        ycrcb.Vf = 0;
                    }

                    MyOwnYcbcr.ycbcrArr[x, y] = ycrcb;
                }
            }

            //////////
            /////////////// Normalize to -128 <-> 127 range
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            for (int y = 0; y < MyOwnYcbcr.height; y++)
            {
                for (int x = 0; x < MyOwnYcbcr.width; x++)
                {
                    Yuv ycColorspaceEdit = MyOwnYcbcr.ycbcrArr[x, y];

                    ycColorspaceEdit.Ynorm = (int)((ycColorspaceEdit.Yf) - 128);

                    if (y % 2 == 0 && x % 2 == 0)
                    {
                        ycColorspaceEdit.Unorm = (int)((ycColorspaceEdit.Uf) - 128);
                        ycColorspaceEdit.Vnorm = (int)((ycColorspaceEdit.Vf) - 128);
                    }
                    else
                    {
                        ycColorspaceEdit.Unorm = (int)(ycColorspaceEdit.Uf);
                        ycColorspaceEdit.Vnorm = (int)(ycColorspaceEdit.Vf);
                    }

                    MyOwnYcbcr.ycbcrArr[x, y] = ycColorspaceEdit;
                }
            }

            //////////
            /////////////// DCT matrix Creation
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            //double[,]DCTmatrix;
            //DCTmatrix = new double[8, 8];
            //
            //for (int i = 0; i < 8; i++)
            //{
            //    for(int j = 0; j < 8; j++)
            //    {
            //        if(i == 0)
            //        {
            //            DCTmatrix[i,j] = 1 / Math.Sqrt(8);
            //        }
            //        else if(i > 0)
            //        {
            //            DCTmatrix[i, j] = Math.Sqrt( (2f/8f )) *  Math.Cos((((2 * j) + 1) * i * Math.PI) / (2 * 8)) ;
            //        }
            //    }
            //}


            //////////
            /////////////// DCT matrix Transposed
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            //double[,] DCTmatrixTransposed;
            //DCTmatrixTransposed = new double[8, 8];
            //
            //for (int i = 0; i < 8; i++)
            //{
            //    for (int j = 0; j < 8; j++)
            //    {
            //        DCTmatrixTransposed[i, j] = DCTmatrix[j, i];
            //    }
            //}

            //////////
            /////////////// 8 by 8 traversal and applying DCT to Merged picture
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            //double[,] DCTresult;
            //DCTresult = new double[8, 8];

            double[,] YtempDCT = new double[8, 8];
            double[,] UtempDCT = new double[8, 8];
            double[,] VtempDCT = new double[8, 8];


            for (int y = 0; y < MyOwnYcbcr.height; y += 8)
            {
                for (int x = 0; x < MyOwnYcbcr.width; x += 8)
                {
                    if (x <= MyOwnYcbcr.width - 8 && y <= MyOwnYcbcr.height - 8) //safeguard against out of bounds error
                    {
                        for (int ity = y; ity < y + 8; ity++)
                        {
                            for (int itx = x; itx < x + 8; itx++)
                            {
                                YtempDCT[ity - y, itx - x] = MyOwnYcbcr.ycbcrArr[itx, ity].Ynorm;
                                UtempDCT[ity - y, itx - x] = MyOwnYcbcr.ycbcrArr[itx, ity].Unorm;
                                VtempDCT[ity - y, itx - x] = MyOwnYcbcr.ycbcrArr[itx, ity].Vnorm;
                            }
                        }

                        CosineTransform.DCT(YtempDCT);
                        CosineTransform.DCT(UtempDCT);
                        CosineTransform.DCT(VtempDCT);

                        for (int ity = y; ity < y + 8; ity++)
                        {
                            for (int itx = x; itx < x + 8; itx++)
                            {
                                MyOwnYcbcr.ycbcrArr[itx, ity].Ydct = YtempDCT[ity - y, itx - x];
                                MyOwnYcbcr.ycbcrArr[itx, ity].Udct = UtempDCT[ity - y, itx - x];
                                MyOwnYcbcr.ycbcrArr[itx, ity].Vdct = VtempDCT[ity - y, itx - x];
                            }
                        }
                    }
                }
            }


            //default quality value of 50;

            int[,] QuantMatrix;
            QuantMatrix = new int[8, 8] {
                { 16, 11, 10, 16, 24, 40, 51, 61 },
                { 12, 12, 14, 19, 26, 58, 60, 55 },
                { 14, 13, 16, 24, 40, 57, 69, 56 },
                { 14, 17, 22, 29, 51, 87, 80, 62 },
                { 18, 22, 37, 56, 68, 109, 103, 77 },
                { 24, 35, 55, 64, 81, 104, 113, 92 },
                { 49, 64, 78, 87, 103, 121, 120, 101 },
                { 72, 92, 95, 98, 112, 100, 103, 99 },
            };

            // changing quality value
            if (QuantizationQuality != 50)
            {
                for (int x = 0; x < 8; x++)
                {
                    for (int y = 0; y < 8; y++)
                    {
                        if (QuantizationQuality > 50)
                        {
                            QuantMatrix[x, y] = (int)(QuantMatrix[x, y] * ((float)(100 - QuantizationQuality) / 50f));
                            if (QuantMatrix[x, y] > 255)
                            {
                                QuantMatrix[x, y] = 255;
                            }
                            if (QuantMatrix[x, y] == 0)
                            {
                                QuantMatrix[x, y] = 1;
                            }
                        }
                        else if (QuantizationQuality < 50)
                        {
                            QuantMatrix[x, y] = QuantMatrix[x, y] * (50 / QuantizationQuality);
                            if (QuantMatrix[x, y] > 255)
                            {
                                QuantMatrix[x, y] = 255;
                            }
                            else if (QuantMatrix[x, y] == 0)
                            {
                                QuantMatrix[x, y] = 1;
                            }
                        }
                    }
                }
            }


            //////////
            /////////////// 8 by 8 traversal and appyling Quantization
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////


            for (int y = 0; y < MyOwnYcbcr.height; y += 8)
            {
                for (int x = 0; x < MyOwnYcbcr.width; x += 8)
                {
                    for (int ity = y; ity < y + 8; ity++)
                    {
                        for (int itx = x; itx < x + 8; itx++)
                        {
                            if (itx < MyOwnYcbcr.width && ity < MyOwnYcbcr.height)
                            {
                                MyOwnYcbcr.ycbcrArr[itx, ity].Yquant = (int)Math.Round(MyOwnYcbcr.ycbcrArr[itx, ity].Ydct / QuantMatrix[itx - x, ity - y]);
                                MyOwnYcbcr.ycbcrArr[itx, ity].Uquant = (int)Math.Round(MyOwnYcbcr.ycbcrArr[itx, ity].Udct / QuantMatrix[itx - x, ity - y]);
                                MyOwnYcbcr.ycbcrArr[itx, ity].Vquant = (int)Math.Round(MyOwnYcbcr.ycbcrArr[itx, ity].Vdct / QuantMatrix[itx - x, ity - y]);
                            }
                        }
                    }
                }
            }

            //////////
            /////////////// 8 by 8 traversal and appyling ZIG ZAG
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////


            for (int y = 0; y < MyOwnYcbcr.height; y += 8)
            {
                for (int x = 0; x < MyOwnYcbcr.width; x += 8)
                {
                    if (x <= MyOwnYcbcr.width - 8 && y <= MyOwnYcbcr.height - 8) //safeguard against out of bounds error
                    {
                        int[,] YZbuffer = new int[8, 8];
                        int[,] UZbuffer = new int[8, 8];
                        int[,] VZbuffer = new int[8, 8];
                        // make a copy in buffer so that zigzag can copy it over to the original.

                        for (int buffery = y; buffery < y + 8; buffery++)
                        {
                            for (int bufferx = x; bufferx < x + 8; bufferx++)
                            {
                                YZbuffer[bufferx - x, buffery - y] = MyOwnYcbcr.ycbcrArr[bufferx, buffery].Yquant;
                                UZbuffer[bufferx - x, buffery - y] = MyOwnYcbcr.ycbcrArr[bufferx, buffery].Uquant;
                                VZbuffer[bufferx - x, buffery - y] = MyOwnYcbcr.ycbcrArr[bufferx, buffery].Vquant;
                            }
                        }

                        // copy zigzag wise from the buffer to the original

                        for (int ity = y; ity < y + 8; ity++)
                        {
                            for (int itx = x; itx < x + 8; itx++)
                            {
                                if (itx < MyOwnYcbcr.width && ity < MyOwnYcbcr.height)
                                {
                                    Coords Targetcoords;

                                    Targetcoords = ZZtraverseValue(itx - x, ity - y);

                                    MyOwnYcbcr.zigzagY.Add(YZbuffer[Targetcoords.xvalue, Targetcoords.yvalue]);
                                    MyOwnYcbcr.zigzagU.Add(UZbuffer[Targetcoords.xvalue, Targetcoords.yvalue]);
                                    MyOwnYcbcr.zigzagV.Add(VZbuffer[Targetcoords.xvalue, Targetcoords.yvalue]);
                                }
                            }
                        }
                    }
                }
            }



            Coords testcoords;

            testcoords.xvalue = 920;
            testcoords.yvalue = 0;

            for (int y = testcoords.yvalue; y < testcoords.yvalue + 8; y++)
            {
                for (int x = testcoords.xvalue; x < testcoords.xvalue + 8; x++)
                {
                    Matrixviewer.Rows[y - testcoords.yvalue].Cells[x - testcoords.xvalue].Value = MyOwnYcbcr.ycbcrArr[x, y].Yquant;
                    //Matrixviewer.Rows[x].Cells[y].Value = QuantMatrix[x, y];
                    //Matrixviewer.Rows[x].Cells[y].Value = testoutputmatrix[x, y];
                }
            }

            Merged_width_txtbx.Text  = MyOwnYcbcr.width.ToString();
            Merged_height_txtbx.Text = MyOwnYcbcr.height.ToString();

            textBox1.Text = MyOwnYcbcr.zigzagU.Count.ToString();
            textBox2.Text = (MyOwnYcbcr.width * MyOwnYcbcr.height).ToString();

            textBox3.Text = MyOwnYcbcr.zigzagV[7360].ToString();

            //for (int y = 0; y < 8; y++)
            //{
            //    for (int x = 0; x < 8; x++)
            //    {
            //        Matrixviewer.Rows[x].Cells[y].Value = testdata[x,y];
            //        //Matrixviewer.Rows[x].Cells[y].Value = QuantMatrix[x, y];
            //        //Matrixviewer.Rows[x].Cells[y].Value = testoutputmatrix[x, y];
            //    }
            //}


            //for (int x = 0; x < 8; x++)
            //{
            //    for (int y = 0; y < 8; y++)
            //    {
            //        Matrixviewer.Rows[y].Cells[x].Value = MyOwnYcbcr.ycbcrArr[x, y].Y;
            //    }
            //}
        }