-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImaegHelper.cs
382 lines (326 loc) · 13.9 KB
/
ImaegHelper.cs
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
using System;
using System.Drawing;
namespace MalSocail
{
public enum Channel
{
R,
B,
G,
A,
QT
}
/*
The human eye is fairly good at seeing small differences in brightness over a relatively large area, but not so good at distinguishing the exact strength of a high frequency
(rapidly varying) brightness variation. This fact allows one to reduce the amount of information required by ignoring the high frequency components. This is done by simply
dividing each component in the frequency domain by a constant for that component, and then rounding to the nearest integer. This is the main lossy operation in the whole process.
As a result of this, it is typically the case that many of the higher frequency components are rounded to zero, and many of the rest become small positive or negative numbers.
As human vision is also more sensitive to luminance than chrominance, further compression can be obtained by working in a non-RGB color space which separates the two (e.g., YCbCr),
and quantizing the channels separately.
Wikipedia (https://en.wikipedia.org/wiki/Quantization_(image_processing)
*/
public static class ImaegHelper
{
//A quantization matrix
public static double[,] QT = new double[8, 8] {
{ 06, 04, 04, 06, 10, 16, 20, 24 },
{ 05, 05, 06, 08, 10, 23, 24, 22 },
{ 06, 05, 06, 10, 16, 23, 28, 22 },
{ 06, 07, 09, 12, 20, 35, 32, 25 },
{ 07, 09, 15, 22, 27, 44, 41, 31 },
{ 10, 14, 22, 26, 32, 42, 45, 37 },
{ 20, 26, 31, 35, 41, 48, 48, 40 },
{ 29, 37, 38, 39, 45, 40, 41, 40 }
};
//This method takes two 8x8 quantized matrices and prints a side by side comparison. Differnces are highlighted in red.
public static void PrintGridComparison(double[,] grid1, double[,] grid2, string name1, string name2, Channel channel)
{
if (grid1.Length != grid2.Length)
return;
int width = (Math.Sqrt(grid1.Length) % 1) == 0 ? (int)Math.Sqrt(grid1.Length) : 0;
int height = (Math.Sqrt(grid1.Length) % 1) == 0 ? (int)Math.Sqrt(grid1.Length) : 0;
string terminal = "\t " + new String('-', width*height + 9) + "\n";
Console.Write(terminal);
if (channel == Channel.R)
Console.ForegroundColor = ConsoleColor.Red;
else if (channel == Channel.B)
Console.ForegroundColor = ConsoleColor.Blue;
else if (channel == Channel.G)
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("\t |Channel: " + channel.ToString());
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" " + name1 + "\t |\t|Channel: " + channel.ToString()+ " " + name2 + "\t |\n");
Console.Write("\t |");
for (int y = 0; y < height; y++)
Console.Write(string.Format("{0,3} ", y));
Console.Write("|\t|");
for (int y = 0; y < height; y++)
Console.Write(string.Format("{0,3} ", y));
Console.Write("|\n");
Console.Write(terminal);
for (int y = 0; y < height; y++)
{
Console.Write("\t" + y + "|");
for (int x = 0; x < width; x++)
{
if(grid1[x,y] != grid2[x,y])
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(string.Format("{0,3} ", grid1[x, y]));
Console.ForegroundColor = ConsoleColor.White;
}
Console.Write("|\t|");
for (int x = 0; x < width; x++)
{
if (grid1[x, y] != grid2[x, y])
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(string.Format("{0,3} ", grid2[x, y]));
Console.ForegroundColor = ConsoleColor.White;
}
Console.Write("|" + y + "\n");
}
Console.Write(terminal);
}
// A reuseable method to highlight different values in red
public static void ReadString(Bitmap image)
{
Color c = image.GetPixel(0, 0);
Color c2 = image.GetPixel(1, 0);
string str = Convert.ToChar(c.R).ToString() + Convert.ToChar(c.G).ToString() + Convert.ToChar(c.B).ToString() + Convert.ToChar(c2.R).ToString() + Convert.ToChar(c2.G).ToString() + Convert.ToChar(c2.B).ToString();
Console.WriteLine(str);
}
//Extract hidden text from image
public static string ExtractString(Bitmap image, int x=0,int y = 0)
{
string extractedText = "";
int terminating = 0;
int counter = 0;
for (int i = y; i < image.Height; i++)
{
for (int j = x; j < image.Width; j++)
{
if (counter % 15 == 0)
{
Color pixel = image.GetPixel(j, i);
int RValue = pixel.R;
int GValue = pixel.G;
int BValue = pixel.B;
if (RValue + GValue + BValue == 0)
{
terminating++;
if (terminating == 8)
{
return extractedText;
}
}
else
{
char r = (char)RValue;
char g = (char)GValue;
char b = (char)BValue;
extractedText += r.ToString();
extractedText += g.ToString();
extractedText += b.ToString();
terminating = 0;
}
}
}
}
return "";
}
// Insert string into image
public static Bitmap ImplantString(Bitmap image, string str, int x=0, int y=0)
{
int length = str.Length;
int index = 0;
int terminating = 0;
int R = 0, G = 0, B = 0;
int counter = 0;
for (int i = y; i < image.Height; i++)
{
for (int j = x; j < image.Width; j++)
{
if (index < length)
{
if (counter % 15 == 0)
{
Color pixel = image.GetPixel(j, i);
R = pixel.R;
G = pixel.G;
B = pixel.B;
R = index < length ? R = str[index++] : 0;
G = index < length ? G = str[index++] : 0;
B = index < length ? B = str[index++] : 0;
image.SetPixel(j, i, Color.FromArgb(R, G, B));
}
}
else
{
if (terminating == 8)
{
return image;
}else
{
image.SetPixel(j, i, Color.FromArgb(0, 0, 0));
terminating++;
}
}
}
}
return image;
}
// Prints a single 8x8 Matrix
public static void PrintChannel(double[,] grid, Channel channel)
{
int width = (Math.Sqrt(grid.Length) % 1) == 0 ? (int)Math.Sqrt(grid.Length) : 0;
int height = (Math.Sqrt(grid.Length) % 1) == 0 ? (int)Math.Sqrt(grid.Length) : 0;
string[] lines = new string[height];
for (int y = 0; y < height; y++)
{
object[] obj = new object[width];
for (int x = 0; x < width; x++)
{
obj[x] = grid[x, y];
}
string line = String.Format("\t| [{0,3} {1,3} {2,3} {3,3} {4,3} {5,3} {6,3} {7,3}] |\n",obj);
lines[y] = line;
}
string terminal = "\t" + new String('-', lines[0].Length -2) + "\n";
Console.Write(terminal);
if(channel == Channel.R)
Console.ForegroundColor = ConsoleColor.Red;
else if (channel == Channel.B)
Console.ForegroundColor = ConsoleColor.Blue;
else if (channel == Channel.G)
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("\t| Channel: " + channel.ToString() + "\t\t\t |\n");
Console.ForegroundColor = ConsoleColor.White;
foreach (string line in lines)
Console.Write(line);
Console.Write(terminal);
}
//Quantize a 8x8 grid
public static double[,] Quantiz(double[,] grid)
{
int width = (Math.Sqrt(grid.Length) % 1) == 0 ? (int)Math.Sqrt(grid.Length) : 0;
int height = (Math.Sqrt(grid.Length) % 1) == 0 ? (int)Math.Sqrt(grid.Length) : 0;
double[,] block = new double[width, height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
block[x, y] = Math.Round(grid[x, y] / QT[x, y]);
}
}
return block;
}
// Calculate the discrete cosine transform (DCT) of a quantized 8x8 block
public static double[,] GetIDCTBlock(double[,] grid)
{
int width = (Math.Sqrt(grid.Length) % 1) == 0 ? (int) Math.Sqrt(grid.Length) : 0;
int height = (Math.Sqrt(grid.Length) % 1) == 0 ? (int)Math.Sqrt(grid.Length) : 0;
double[,] block = new double[width,height];
int cell = 0;
double[] flatten = new double[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
flatten[cell] = grid[x,y];
cell++;
}
}
double[] vector = InverseTransform(flatten);
cell = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
block[x,y] = flatten[cell];
cell++;
}
}
return block;
}
// Get 8x8 block from image
public static double[,] Get8by8(Bitmap image,Channel channel, int x = 0, int y = 0, int height = 8, int width = 8)
{
double[,] block = new double[width,height];
for (int yi = 0; yi < height; yi++)
{
for (int xi = 0; xi < width; xi++)
{
Color pixel = image.GetPixel((x + xi), (y + yi));
switch (channel)
{
case Channel.A:
block[xi, yi] = pixel.A;
break;
case Channel.B:
block[xi, yi] = pixel.B;
break;
case Channel.G:
block[xi, yi] = pixel.G;
break;
case Channel.R:
block[xi, yi] = pixel.R;
break;
}
}
}
return block;
}
// Get the image's first 8x8 block
public static void PrintFirstRowRBGValues(Bitmap image)
{
int width = 0;
int height = 0;
for (height = 0; height < 8; height++)
{
for (width = 0; width < 8; width++)
{
Color pixel = image.GetPixel(width, height);
Console.Write( String.Format("[{0,3} {1,3} {2,3} {3,3}]", pixel.R, pixel.B, pixel.G, pixel.A));
}
Console.Write("\n");
}
Console.WriteLine("\n\t"+image.PixelFormat.ToString());
Console.WriteLine("\n\tWidth = " + width);
Console.WriteLine("\tHeight = " + height);
}
// Preform and inverse transform
public static double[] InverseTransform(double[] vector)
{
if (vector == null)
throw new NullReferenceException();
double[] result = new double[vector.Length];
double factor = Math.PI / vector.Length;
for (int i = 0; i < vector.Length; i++)
{
double sum = vector[0] / 2;
for (int j = 1; j < vector.Length; j++)
sum += vector[j] * Math.Cos(j * (i + 0.5) * factor);
result[i] = sum;
}
return result;
}
public static Bitmap CreateNonIndexedImage(Image src)
{
Bitmap newBmp = new Bitmap(src.Width, src.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
using (Graphics gfx = Graphics.FromImage(newBmp))
{
gfx.DrawImage(src, 0, 0);
}
return newBmp;
}
public static int reverseBits(int n)
{
int result = 0;
for (int i = 0; i < 8; i++)
{
result = result * 2 + n % 2;
n /= 2;
}
return result;
}
}
}