-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformation.cs
More file actions
153 lines (132 loc) · 4.19 KB
/
Copy pathTransformation.cs
File metadata and controls
153 lines (132 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//Lena Ebner - MMTB2019
using System;
using System.Drawing;
using System.IO;
public class Transformations
{
private static Transformations _instance = null;
private static readonly object instancelock = new object();
private Transformations()
{
}
public static Transformations Instance()
{
if(_instance == null)
{
lock (instancelock)
{
if(_instance == null)
{
_instance = new Transformations();
}
}
}
return _instance;
}
public RGBChannels InverseDCT(RGBChannels img, int n, int m, double radius)
{
double kx=1.0;
double ky=1.0;
double value = 0.0;
double dct = 0.0;
RGBChannels original = new RGBChannels(img.R.GetLength(0), img.R.GetLength(1));
for(int i = 0; i<m; i++)
{
for (int j = 0; j<n; j++)
{
value=0.0;
for (int x = 0; x < m; x++)
{
for (int y = 0; y < n; y++)
{
if (x==0)
kx=(1.0/Math.Sqrt(2));
else
kx=1.0;
if (y==0)
ky=(1.0/Math.Sqrt(2));
else
ky=1.0;
dct = CalcDCTValue(i,j,x,y);
if(dct > radius)
dct = 0.0;
value += (double)(kx*ky*0.25)*(img.R[y,x])*dct;
}
}
original.R[j,i] = value;
original.G[j,i] = value;
original.B[j,i] = value;
}
}
return original;
}
public RGBChannels DCT(RGBChannels img, int n, int m)
{
double kx=1.0;
double ky=1.0;
double dct=0.0;
RGBChannels channels = img;
for(int i = 0; i<m; i++)
{
for (int j = 0; j<n; j++)
{
if (i==0)
kx=(1.0/Math.Sqrt(2));
else
kx=1.0;
if (j==0)
ky=(1.0/Math.Sqrt(2));
else
ky=1.0;
dct=0.0;
for (int x = 0; x < m; x++)
{
for (int y = 0; y < n; y++)
{
dct += (img.R[y,x])*CalcDCTValue(x,y,i,j);
}
}
channels.SetValueToAllChannels((dct*((kx*ky)/4.0)),i,j);
}
}
return channels;
}
private double CalcDCTValue(int x, int y, double nx, double my)
{
return GetCosValue(x, nx)*GetCosValue(y,my);
}
private double GetCosValue(int pos, double dach)
{
return Math.Cos((((2.0*pos)+1.0)*dach*Math.PI)/16.0);
}
public Node HuffmanEncoding(Bitmap img)
{
Heap myHeap = Heap.Setup(img);
Node rootNode = CalculateHuffmanCode(myHeap);
Console.WriteLine("Traversing Tree and adding Codes to Nodes");
TraverseTreeAndAddCode(rootNode, "");
return rootNode;
}
private Node CalculateHuffmanCode(Heap minHeap)
{
Console.WriteLine("Calculating the huffman codex");
while (minHeap.Size() > 1)
{
minHeap.PairMinimas();
}
return minHeap.GetRootNode();
}
private void TraverseTreeAndAddCode(Node root, string code)
{
if (root.leftChild == null && root.rightChild == null){
Console.WriteLine("assign code : "+ code + " to "+ root.Color);
root.code = code;
}
if (root.leftChild != null)
{
TraverseTreeAndAddCode(root.leftChild, (code+"0"));
}
if (root.rightChild != null)
TraverseTreeAndAddCode(root.rightChild, (code+"1"));
}
}