-
Notifications
You must be signed in to change notification settings - Fork 16
/
dct.hpp
173 lines (140 loc) · 3.26 KB
/
dct.hpp
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
#pragma once
#include <opencv2/core/core.hpp>
/*!
* 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.
*/
inline cv::Mat encode_dct(const cv::Mat& img, const std::string& text, int mode = STORE_FULL, int channel = 0, int intensity = 30)
{
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;
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;
}