-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
512 lines (435 loc) · 11.7 KB
/
main.cpp
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include <iostream>
#include <iomanip>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <boost/algorithm/string.hpp>
#include "format.h"
#include "helpers.hpp"
#include "lsb.hpp"
#include "lsb_alt.hpp"
#include "dct.hpp"
#include "dwt.hpp"
#include "tlv.hpp"
#if _WIN32
#include <conio.h>
#else
#define _getche getchar
#endif
using namespace cv;
using namespace std;
using namespace boost;
/*!
* Evaluates the similarity and prints the original and resulting strings.
*
* \param input Original input.
* \param output Extracted output.
*/
void print_debug(const string& input, const string& output)
{
auto original = clean(input);
auto extracted = clean(output);
auto accuracy = similarity(original, extracted);
Format::ColorCode color;
if (accuracy > 99.99)
{
color = Format::Green;
}
else if (accuracy > 75)
{
color = Format::Yellow;
}
else
{
color = Format::Red;
}
cout << endl
<< " Similarity: " << setprecision(3) << color << Format::Bold << accuracy << "%" << Format::Normal << Format::Default << endl << endl
<< " Input:" << endl << endl << Format::White << Format::Bold << original << Format::Normal << Format::Default << endl << endl
<< " Extracted:" << endl << endl << Format::White << Format::Bold << extracted << Format::Normal << Format::Default << endl;
}
/*!
* Displays the original image and pre-steganography histogram.
*/
void show_image(const Mat& img, const string& modifier = "", bool histogram = true)
{
static auto i = 0;
i++;
auto title = modifier.empty() ? "Image " + to_string(i) : modifier + " Image";
namedWindow(title, NULL);
resizeWindow(title, 512, 512);
moveWindow(title, 50 + (515 * (i - 1)), 50);
imshow(title, img);
if (histogram)
{
title = modifier.empty() ? "Histogram " + to_string(i) : modifier + " Histogram";
showHistogram(img, title.c_str());
moveWindow(title, 50 + (515 * (i - 1)), 595);
}
}
/*!
* Tests the discrete cosine transformation method.
*/
void test_dct()
{
auto img = imread("test/lena.jpg");
show_image(img, "Original");
auto input = read_file("test/test.txt");
auto stego = encode_dct(img, input);
auto output = decode_dct(stego);
print_debug(input, output);
show_image(stego, "Altered");
}
/*!
* Tests the discrete cosine transformation method with 80% JPEG compression
* and multi-channel message reconstruction.
*/
void test_dct_multi()
{
auto img = imread("test/lena.jpg");
show_image(img, "Original");
auto input = read_file("test/test.txt");
auto stego = encode_dct(img, input, STORE_FULL, 0);
stego = encode_dct(stego, input, STORE_FULL, 1);
stego = encode_dct(stego, input, STORE_FULL, 2);
imwrite("test/lena_dct.jpg", stego, vector<int> { CV_IMWRITE_JPEG_QUALITY, 80 });
stego = imread("test/lena_dct.jpg");
auto output = repair(vector<string>
{
decode_dct(stego, 0),
decode_dct(stego, 1),
decode_dct(stego, 2)
});
print_debug(input, output);
show_image(stego, "Altered");
}
/*!
* Prompts the user for a selection from the available options.
*
* \param opts Available options.
*
* \return Option selected by user.
*/
char get_selection(const string& opts)
{
cout << " Selection: " << Format::Green << Format::Bold;
char sel;
while ((sel = _getche()))
{
if (opts.find(sel) != string::npos)
{
break;
}
cout << Format::Normal << Format::Default << endl
<< " Selection: " << Format::Green << Format::Bold;
}
cout << Format::Normal << Format::Default << endl;
return sel;
}
/*!
* Translates a STORE_* constant into a string.
*
* \param store Constant value to translate.
*
* \return Translated value.
*/
string store_to_string(int store)
{
switch (store)
{
case STORE_ONCE: return "Store Once, Leave Rest Random";
case STORE_FULL: return "Store Once, Fill Rest w/ Zeros";
case STORE_REPEAT: return "Store as Indefinitely Repeating";
default: return "Unknown Mode " + to_string(store);
}
}
/*!
* Prompts the user to select a storage mode.
*
* \param store Storage variable to manipulate.
*/
void select_store(int& store)
{
switch (show_menu("Storage Mode", {
{ 'o', "Store Once, Leave Rest Random" },
{ 'z', "Store Once, Fill Rest w/ Zeros" },
{ 'r', "Store as Indefinitely Repeating" },
{ 'b', "Back to Main Menu" }
}))
{
case 'o': store = STORE_ONCE; break;
case 'z': store = STORE_FULL; break;
case 'r': store = STORE_REPEAT; break;
}
}
/*!
* Translates the channel parameter value into a string.
*
* \param channel Parameter value to translate.
*
* \return Translated value.
*/
string channel_to_string(int channel)
{
switch (channel)
{
case 0: return "Encode All Channels";
case 1: return "Encode Blue Channel";
case 2: return "Encode Green Channel";
case 3: return "Encode Red Channel";
default: return "Unknown Mode " + to_string(channel);
}
}
/*!
* Prompts the user to select a channel.
*
* \param channel Channel variable to manipulate.
*/
void select_channel(int& channel)
{
switch (show_menu("Storage Mode", {
{ 'a', "Encode All Channels" },
{ 'k', "Encode Blue Channel" },
{ 'g', "Encode Green Channel" },
{ 'r', "Encode Red Channel" },
{ 'b', "Back to Main Menu" }
}))
{
case 'a': channel = 0; break;
case 'k': channel = 1; break;
case 'g': channel = 2; break;
case 'r': channel = 3; break;
}
}
/*!
* Prompts the user to provide a string value.
*
* \param title Name of the variable.
* \param value Variable to manipulate.
* \param checkFile Check if value is an existing file.
*/
void prompt_string(const string& title, string& value, bool checkFile = false)
{
cout << endl << " " << title << ": " << Format::Green << Format::Bold;
string str;
getline(cin, str);
trim(str);
cout << Format::Normal << Format::Default;
if (str.length() > 0)
{
if (checkFile)
{
ifstream fs(str);
if (fs.good())
{
value = str;
}
else
{
cerr << " " << Format::Red << Format::Bold << "Error:" << Format::Normal << Format::Default << " Specified file '" << str << "' does not exist." << endl;
}
}
else
{
value = str;
}
}
}
/*!
* Prompts the user to provide an integer value.
*
* \param title Name of the variable.
* \param value Variable to manipulate.
* \param min Lower limit of the value.
* \param max Upper limit of the value.
*/
void prompt_int(const string& title, int& value, int min = INT_MIN, int max = INT_MAX)
{
cout << endl << " " << title << (min != INT_MIN && max != INT_MAX ? " [" + to_string(min) + "-" + to_string(max) + "]" : "") << ": " << Format::Green << Format::Bold;
string str;
getline(cin, str);
trim(str);
cout << Format::Normal << Format::Default;
if (str.length() > 0)
{
auto num = atoi(str.c_str());
if (num <= max && num >= min)
{
value = num;
}
else
{
cerr << " " << Format::Red << Format::Bold << "Error:" << Format::Normal << Format::Default << " Specified value " << num << " out of range " << to_string(min) << " - " << to_string(max) << "." << endl;
}
}
}
/*!
* Prompts the user to provide a double value.
*
* \param title Name of the variable.
* \param value Variable to manipulate.
* \param min Lower limit of the value.
* \param max Upper limit of the value.
*/
void prompt_double(const string& title, double& value, double min = DBL_MIN, double max = DBL_MAX)
{
cout << endl << " " << title << (min != DBL_MIN && max != DBL_MAX ? " [" + to_string(min) + "-" + to_string(max) + "]" : "") << ": " << Format::Green << Format::Bold;
string str;
getline(cin, str);
trim(str);
cout << Format::Normal << Format::Default;
if (str.length() > 0)
{
auto num = atof(str.c_str());
if (num <= max && num >= min)
{
value = num;
}
else
{
cerr << " " << Format::Red << Format::Bold << "Error:" << Format::Normal << Format::Default << " Specified value " << num << " out of range " << to_string(min) << " - " << to_string(max) << "." << endl;
}
}
}
/*!
* Runs the discrete cosine transformation method.
*
* \param input Path to original image.
* \param secret Path to the data to be hidden.
* \param store Storage mode.
* \param channel Channels to encode.
* \param persistence Persistence value.
* \param compression JPEG compression percentage.
*/
void do_dct(const string& input, const string& secret, int store, int channel, int persistence, int compression)
{
auto img = imread(input);
if (!img.data)
{
cerr << endl << " " << Format::Red << Format::Bold << "Error:" << Format::Normal << Format::Default << " Failed to open input image from '" << input << "'." << endl << endl;
return;
}
show_image(img, "Original");
auto data = read_file(secret);
Mat stego;
if (channel == 0)
{
stego = encode_dct(img, data, store, 0, persistence);
stego = encode_dct(stego, data, store, 1, persistence);
stego = encode_dct(stego, data, store, 2, persistence);
}
else
{
stego = encode_dct(img, data, store, channel - 1, persistence);
}
auto altered = remove_extension(input) + ".dct.jpg";
imwrite(altered, stego, vector<int> { CV_IMWRITE_JPEG_QUALITY, compression });
cout << endl << " " << Format::Green << Format::Bold << "Success:" << Format::Normal << Format::Default << " Altered image written to '" << altered << "'." << endl;
stego = imread(altered);
string output;
if (channel == 0)
{
output = repair(vector<string>
{
decode_dct(stego, 0),
decode_dct(stego, 1),
decode_dct(stego, 2)
});
}
else
{
output = decode_dct(stego, channel - 1);
}
print_debug(data, output);
show_image(stego, "Altered");
}
/*!
* Runs the discrete cosine transformation extraction method.
*
* \param altered Path to the altered image.
* \param channel Channels to decode.
*/
void read_dct(const string& altered, int channel)
{
auto stego = imread(altered);
if (!stego.data)
{
cerr << endl << " " << Format::Red << Format::Bold << "Error:" << Format::Normal << Format::Default << " Failed to open altered image from '" << altered << "'." << endl << endl;
return;
}
string output;
if (channel == 0)
{
output = repair(vector<string>
{
decode_dct(stego, 0),
decode_dct(stego, 1),
decode_dct(stego, 2)
});
}
else
{
output = decode_dct(stego, channel - 1);
}
output = clean(output);
cout << endl << " Extracted:" << endl << endl << Format::White << Format::Bold << output << Format::Normal << Format::Default << endl << endl;
}
/*!
* Entry point of the application.
*
* \param Number of arguments.
* \param Argument array pointer.
*
* \return Value indicating exit status.
*/
int main(int argc, char** argv)
{
mndct:
switch (show_menu("DCT Configuration", {
{ 'i', "Input File: " + input },
{ 'd', "Data File: " + secret },
{ 's', "Storage Mode: " + store_to_string(store) },
{ 'c', "Channel Usage: " + channel_to_string(channel) },
{ 'p', "Persistence: " + to_string(persistence) + "%" },
{ 'j', "Compression: " + to_string(compression) + "%" },
{ 'a', "Perform Steganography" },
{ 'x', "Perform Extraction" },
{ 'b', "Back to Main Menu" }
}, "ax"))
{
case 'i':
prompt_string("Input File", input, true);
goto mndct;
case 'd':
prompt_string("Data File", secret, true);
goto mndct;
case 's':
select_store(store);
goto mndct;
case 'c':
select_channel(channel);
goto mndct;
case 'p':
prompt_int("Persistence Percentage", persistence, 0, 100);
goto mndct;
case 'j':
prompt_int("JPEG Compression Percentage", compression, 0, 100);
goto mndct;
case 'a':
do_dct(input, secret, store, channel, persistence, compression);
cvWaitKey();
break;
case 'x':
read_dct(input, channel);
system("pause");
break;
case 'b':
goto main;
}
}
break;
case 'x':
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}