Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- namespace DCT
- {
- internal class DCT
- {
- public DCT(int width, int height)
- {
- Width = width;
- Height = height;
- }
- //size of all matrices
- private int Width;
- private int Height;
- //Run a DCT2D on a single matrix (Это надо)
- public double[,] DCT2D(double[,] input)
- {
- var coeffs = new double[Width, Height];
- //To initialise every [u,v] value in the coefficient table
- for (int u = 0; u < Width; u++)
- {
- for (int v = 0; v < Height; v++)
- {
- //sum the basisfunction for every [x,y] value in the bitmap input
- double sum = 0d;
- for (int x = 0; x < Width; x++)
- {
- for (int y = 0; y < Height; y++)
- {
- double a = input[x, y];
- sum += BasisFunction(a, u, v, x, y);
- }
- }
- coeffs[u, v] = (int)(sum * beta * alpha(u) * alpha(v));
- }
- }
- return coeffs;
- }
- //Run an inverse DCT2D on a single matrix (Это не надо)
- public int[,] IDCT2D(double[,] coeffs)
- {
- var output = new int[Width, Height];
- //To initialise every [x,y] value in the bitmap output
- for (int x = 0; x < Width; x++)
- {
- for (int y = 0; y < Height; y++)
- {
- //sum the basisfunction for every [u,v] value in the coefficient table
- double sum = 0d;
- for (int u = 0; u < Width; u++)
- {
- for (int v = 0; v < Height; v++)
- {
- double a = coeffs[u, v];
- sum += BasisFunction(a, u, v, x, y) * alpha(u) * alpha(v);
- }
- }
- output[x, y] = (int)Math.Round(sum * beta, 0);
- }
- }
- return output;
- }
- private double BasisFunction(double a, double u, double v, double x, double y)
- {
- double b = Math.Cos(((2d * x + 1d) * u * Math.PI) / (2 * Width));
- double c = Math.Cos(((2d * y + 1d) * v * Math.PI) / (2 * Height));
- return a * b * c;
- }
- //return 1/sqrt(2) if u is not 0
- private double alpha(int u)
- {
- if (u == 0)
- return 1 / Math.Sqrt(2);
- return 1;
- }
- //normalising value
- private double beta
- {
- get { return (1d / Width + 1d / Height); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement