// C# program to perform discrete cosine transform
using System;
class GFG
{
// Function to find discrete cosine transform and print it
public static void dctTransform(int[,] matrix, int m, int n)
{
double pi = 3.142857;
int i, j, k, l;
// dct will store the discrete cosine transform
double[,] dct = new double[m, n];
double ci, cj, dct1, sum;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
// ci and cj depends on frequency as well as
// number of row and columns of specified matrix
if (i == 0)
ci = 1 / Math.Sqrt(m);
else
ci = Math.Sqrt(2) / Math.Sqrt(m);
if (j == 0)
cj = 1 / Math.Sqrt(n);
else
cj = Math.Sqrt(2) /Math.Sqrt(n);
// sum will temporarily store the sum of
// cosine signals
sum = 0;
for (k = 0; k < m; k++)
{
for (l = 0; l < n; l++)
{
dct1 = matrix[k, l] *
Math.Cos((2 * k + 1) * i * pi / (2 * m)) *
Math.Cos((2 * l + 1) * j * pi / (2 * n));
sum = sum + dct1;
}
}
dct[i,j] = ci * cj * sum;
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(dct[i,j]);
}
Console.WriteLine();
}
}
// Driver code
static void Main()
{
const int m = 8, n = 8;
int[,] matrix = new int[m, n] {
{ 255, 255, 255, 255, 255, 255, 255, 255 },
{ 255, 255, 255, 255, 255, 255, 255, 255 },
{ 255, 255, 255, 255, 255, 255, 255, 255 },
{ 255, 255, 255, 255, 255, 255, 255, 255 },
{ 255, 255, 255, 255, 255, 255, 255, 255 },
{ 255, 255, 255, 255, 255, 255, 255, 255 },
{ 255, 255, 255, 255, 255, 255, 255, 255 },
{ 255, 255, 255, 255, 255, 255, 255, 255 }
};
dctTransform(matrix, m, n);
}
}
// This code is contributed by SoumikMondal