Advertisement
Hamikadze

Untitled

Oct 31st, 2020
1,910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace DCT
  8. {
  9.     internal class DCT
  10.     {
  11.         public DCT(int width, int height)
  12.         {
  13.             Width = width;
  14.             Height = height;
  15.         }
  16.  
  17.         //size of all matrices
  18.         private int Width;
  19.  
  20.         private int Height;
  21.  
  22.         //Run a DCT2D on a single matrix (Это надо)
  23.         public double[,] DCT2D(double[,] input)
  24.         {
  25.             var coeffs = new double[Width, Height];
  26.  
  27.             //To initialise every [u,v] value in the coefficient table
  28.             for (int u = 0; u < Width; u++)
  29.             {
  30.                 for (int v = 0; v < Height; v++)
  31.                 {
  32.                     //sum the basisfunction for every [x,y] value in the bitmap input
  33.                     double sum = 0d;
  34.  
  35.                     for (int x = 0; x < Width; x++)
  36.                     {
  37.                         for (int y = 0; y < Height; y++)
  38.                         {
  39.                             double a = input[x, y];
  40.                             sum += BasisFunction(a, u, v, x, y);
  41.                         }
  42.                     }
  43.                     coeffs[u, v] = (int)(sum * beta * alpha(u) * alpha(v));
  44.                 }
  45.             }
  46.             return coeffs;
  47.         }
  48.  
  49.  
  50.         //Run an inverse DCT2D on a single matrix (Это не надо)
  51.         public int[,] IDCT2D(double[,] coeffs)
  52.         {
  53.             var output = new int[Width, Height];
  54.  
  55.             //To initialise every [x,y] value in the bitmap output
  56.             for (int x = 0; x < Width; x++)
  57.             {
  58.                 for (int y = 0; y < Height; y++)
  59.                 {
  60.                     //sum the basisfunction for every [u,v] value in the coefficient table
  61.                     double sum = 0d;
  62.  
  63.                     for (int u = 0; u < Width; u++)
  64.                     {
  65.                         for (int v = 0; v < Height; v++)
  66.                         {
  67.                             double a = coeffs[u, v];
  68.                             sum += BasisFunction(a, u, v, x, y) * alpha(u) * alpha(v);
  69.                         }
  70.                     }
  71.  
  72.                     output[x, y] = (int)Math.Round(sum * beta, 0);
  73.                 }
  74.             }
  75.             return output;
  76.         }
  77.  
  78.         private double BasisFunction(double a, double u, double v, double x, double y)
  79.         {
  80.             double b = Math.Cos(((2d * x + 1d) * u * Math.PI) / (2 * Width));
  81.             double c = Math.Cos(((2d * y + 1d) * v * Math.PI) / (2 * Height));
  82.  
  83.             return a * b * c;
  84.         }
  85.  
  86.         //return 1/sqrt(2) if u is not 0
  87.         private double alpha(int u)
  88.         {
  89.             if (u == 0)
  90.                 return 1 / Math.Sqrt(2);
  91.             return 1;
  92.         }
  93.  
  94.         //normalising value
  95.         private double beta
  96.         {
  97.             get { return (1d / Width + 1d / Height); }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement