-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdct.cpp
310 lines (251 loc) · 6.3 KB
/
dct.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
#include <bits/stdc++.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
/*!
* Stores the specified input once.
*/
#define STORE_ONCE 1
/*!
* Stores the specified input and fills the rest of the available space with zeros.
*/
#define STORE_FULL 2
/*!
* Stores the specified input in a repeating manner.
*/
#define STORE_REPEAT 3
/*!
* Uses discrete cosine transformation to hide data in the coefficients of a channel of an image.
*
* \param img Input image.
* \param text Text to hide.
* \param mode Storage mode, see STORE_* constants.
* \param channel Channel to manipulate.
* \param intensity Persistence of the hidden data.
*
* \return Altered image with hidden data.
*/
using namespace std;
using namespace cv;
const string SENTINEL = "6969";
inline cv::Mat encode_dct(const cv::Mat& img, std::string text, int mode = STORE_ONCE, int channel = 0, int intensity = 100)
{
using namespace cv;
using namespace std;
//text.append(SENTINEL);
auto block_width = 8;
auto block_height = 8;
auto grid_width = img.cols / block_width;
auto grid_height = img.rows / block_height;
auto i = 0;
auto size = text.length() * 8;
Mat imgfp;
img.convertTo(imgfp, CV_32F);
vector<Mat> planes;
split(imgfp, planes);
for (int x = 1; x < grid_width; x++)
{
for (int y = 1; y < grid_height; y++)
{
auto px = (x - 1) * block_width;
auto py = (y - 1) * block_height;
Mat block(planes[channel], Rect(px, py, block_width, block_height));
Mat trans(Size(block_width, block_height), block.type());
dct(block, trans);
auto a = trans.at<float>(6, 7);
auto b = trans.at<float>(5, 1);
if (i >= size)
{
if (mode == STORE_ONCE)
{
break;
}
else if (mode == STORE_REPEAT)
{
i = 0;
}
}
auto val = 0;
if (i < size)
{
val = (text[i / 8] & 1 << i % 8) >> i % 8;
i++;
}
if (val == 0)
{
if (a > b)
{
swap(a, b);
}
}
else
{
if (a < b)
{
swap(a, b);
}
}
if (a > b)
{
auto d = (intensity - (a - b)) / 2;
a = a + d;
b = b - d;
}
else
{
auto d = (intensity - (b - a)) / 2;
a = a - d;
b = b + d;
}
trans.at<float>(6, 7) = a;
trans.at<float>(5, 1) = b;
Mat stego(Size(block_width, block_height), block.type());
idct(trans, stego);
stego.copyTo(planes[channel](Rect(px, py, block_width, block_height)));
}
if (i >= size && mode == STORE_ONCE)
{
break;
}
}
Mat mergedfp;
merge(planes, mergedfp);
Mat merged;
mergedfp.convertTo(merged, CV_8U);
return merged;
}
/*!
* Uses discrete cosine transformation to recover data hidden in the coefficients of an image.
*
* \param img Input image with hidden data.
* \param channel Channel to manipulate.
*
* \return Hidden data extracted form image.
*/
inline std::string decode_dct(const cv::Mat& img, int channel = 0)
{
using namespace cv;
using namespace std;
auto block_width = 8;
auto block_height = 8;
auto grid_width = img.cols / block_width;
auto grid_height = img.rows / block_height;
auto i = 0;
string bits(grid_width * grid_height / 8, 0);
Mat imgfp;
img.convertTo(imgfp, CV_32F);
vector<Mat> planes;
split(imgfp, planes);
for (int x = 1; x < grid_width; x++)
{
for (int y = 1; y < grid_height; y++)
{
auto px = (x - 1) * block_width;
auto py = (y - 1) * block_height;
Mat block(planes[channel], Rect(px, py, block_width, block_height));
Mat trans(Size(block_width, block_height), block.type());
dct(block, trans);
auto a = trans.at<float>(6, 7);
auto b = trans.at<float>(5, 1);
if (a > b)
{
bits[i / 8] |= 1 << i % 8;
}
i++;
}
}
return bits;
}
/*!
* Tries to recover the original string by comparing multiple extracted data
* from multiple channels or methods.
*
* \param texts List of the same string extracted from different channels/methods.
*
* \return Recovered string.
*/
inline std::string repair(const std::vector<std::string>& texts)
{
using namespace std;
auto longest = max_element(texts.begin(), texts.end(), [](auto a, auto b) { return a.size() < b.size(); })->size();
string result(longest, 0);
for (int i = 0; i < longest; i++)
{
unordered_map<char, uchar> freq;
for (int j = 0; j < texts.size(); j++)
{
if (texts[j].size() <= i)
{
continue;
}
freq[texts[j][i]]++;
}
auto frequent = max_element(freq.begin(), freq.end(), [](auto a, auto b) { return a.second < b.second; })->first;
result[i] = frequent;
}
return result;
}
/*!
* 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");
}*/
/*!
* Reads the specified file into a string.
*
* \param file Path to the file.
*
* \return Contents of the file.
*/
inline std::string read_file(const std::string& file)
{
std::ifstream fs(file);
std::string text((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
fs.close();
return text;
}
int main(int argc, char *argv[]){
if (string(argv[1]) == "encode"){
auto img = imread(argv[2]);
auto input = read_file(argv[3]);
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(argv[4], stego, vector<int> {CV_IMWRITE_JPEG_QUALITY, 80});
}
else if (string(argv[1]) == "decode"){
auto stego = imread(argv[2]);
auto output = repair(vector<string>
{
decode_dct(stego, 0),
decode_dct(stego, 1),
decode_dct(stego, 2)
});
cout << "The secret message is: " << output << endl;
imshow("Image", imread(argv[2]));
waitKey(0);
}
else{
cout << "Undefined Option" << endl;
return 0;
}
return 0;
}