-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.cpp
More file actions
218 lines (179 loc) · 6.33 KB
/
methods.cpp
File metadata and controls
218 lines (179 loc) · 6.33 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
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
#include "headers.h"
BasicImage::BasicImage(const std::string &img_path)
{
loaded_image = stbi_load(img_path.c_str(), &w, &h, &ch, 3);
if (!loaded_image)
{
std::cerr << "Error: CAN NOT LOAD IMAGE FILE." << img_path << std::endl;
throw std::runtime_error("LOAD_IMAGE_PIXELS:CAN_NOT_LOAD_IMAGE_FILE.");
}
pixels.assign(loaded_image, loaded_image + w * h * 3);
}
unsigned char *BasicImage::get_image_loader()
{
return loaded_image;
}
std::vector<int> BasicImage::get_image_params()
{
return {w, h, ch};
}
std::vector<unsigned char> BasicImage::get_pixels_range()
{
return pixels;
}
void BasicImage::save_result(const std::string &output_path, std::vector<unsigned char> new_pixels)
{
if (!stbi_write_png(output_path.c_str(), w, h, 3, new_pixels.data(), w * 3))
{
std::cerr << "Error: CAN NOT SAVE IMAGE." << std::endl;
}
stbi_image_free(loaded_image);
}
void BasicImage::free_space()
{
stbi_image_free(loaded_image);
}
void ChannelSwapping::encode(const std::string &img_path, const std::string &sens_data, const std::string &output_path)
{
BasicImage image(img_path);
int width = image.get_image_params()[0], height = image.get_image_params()[1],
channels = image.get_image_params()[2];
std::vector<unsigned char> pixels = image.get_pixels_range();
const long long int total_bits = sens_data.size() * 8;
if (total_bits > width * height)
{
throw std::runtime_error("Error: TOO_MANY_SENSETIVE_DATA_TO_ENCODE: Message too large for the image");
}
long long int bit_pos = 0;
for (long long int i = 0; i < pixels.size() && bit_pos < total_bits; i += 3)
{
char current_char = sens_data[bit_pos / 8];
bool bit = (current_char >> (7 - (bit_pos % 8))) & 1; // Get bit pos than inverse and shifts to this pos comp with 00000001
if (bit)
{
// R < G
if (pixels[i] <= pixels[i + 1])
{
std::swap(pixels[i], pixels[i + 1]);
}
}
else
{
// R > G
if (pixels[i] >= pixels[i + 1])
{
std::swap(pixels[i], pixels[i + 1]);
}
}
bit_pos++;
}
image.save_result(output_path, pixels);
last_encoded_size = sens_data.size();
}
std::string ChannelSwapping::decode(const std::string &img_path, long long int sens_data_size)
{
BasicImage image(img_path);
int width = image.get_image_params()[0], height = image.get_image_params()[1],
channels = image.get_image_params()[2];
unsigned char *img = image.get_image_loader();
std::string bit_string;
const long long int total_bits = sens_data_size * 8;
for (long long int i = 0; i < width * height * 3 && bit_string.size() < total_bits; i += 3)
{
bit_string += (img[i] > img[i + 1]) ? '1' : '0';
}
image.free_space();
std::string result;
for (long long int i = 0; i + 8 <= bit_string.size() && result.size() < sens_data_size; i += 8)
{
result += static_cast<char>(std::bitset<8>(bit_string.substr(i, 8)).to_ulong());
}
return result;
}
long long int ChannelSwapping::get_last_encoded_size() const
{
return last_encoded_size;
}
void MidBitChange::encode(const std::string &img_path, const std::string &sens_data, const std::string &output_path)
{
BasicImage image(img_path);
std::vector<unsigned char> pixels = image.get_pixels_range();
const size_t total_bits = sens_data.size() * 8;
if (total_bits > pixels.size())
{
std::cerr << "Error: Message too large for the image." << std::endl;
return;
}
size_t bit_pos = 0;
for (size_t i = 0; i < pixels.size() && bit_pos < total_bits; ++i)
{
if (i % 3 == 2)
continue;
char current_char = sens_data[bit_pos / 8];
bool bit = (current_char >> (7 - (bit_pos % 8))) & 1;
pixels[i] = (pixels[i] & 0xEF) | (bit << 4);
bit_pos++;
}
if (!stbi_write_png(output_path.c_str(), image.get_image_params()[0], image.get_image_params()[1],
image.get_image_params()[2], pixels.data(), image.get_image_params()[0] * 3))
{
std::cerr << "Error: Failed to save image." << std::endl;
}
}
std::string MidBitChange::decode(const std::string &img_path, const size_t sens_data_size)
{
BasicImage image(img_path);
std::vector<unsigned char> pixels = image.get_pixels_range();
std::string binary_str;
for (size_t i = 0; i < pixels.size() && binary_str.size() < sens_data_size * 8; ++i)
{
if (i % 3 == 2)
continue;
bool bit = (pixels[i] >> 4) & 1;
binary_str += (bit ? '1' : '0');
}
std::string result;
for (size_t i = 0; i + 8 <= binary_str.size(); i += 8)
{
result += static_cast<char>(std::bitset<8>(binary_str.substr(i, 8)).to_ulong());
}
return result;
}
void EOFHiding::encode(const std::string &img_path, const std::string &sens_data, const std::string &output_path)
{
std::ifstream in(img_path, std::ios::binary);
if (!in.is_open())
{
throw std::runtime_error("Error FILE_CAN_NOT_BE_OPEN: Failed to open input file '" + img_path + "'");
}
in.seekg(0, std::ios::end);
if (in.tellg() == 0)
{
throw std::runtime_error("Error FILE_IS_EMPTY: Input file '" + img_path + "' is empty");
}
in.seekg(0, std::ios::beg);
std::ofstream out(output_path, std::ios::binary);
if (!out.is_open())
{
throw std::runtime_error("Error FILE_CAN_NOT_BE_OPEN: Failed to open input file '" + output_path + "'");
}
out << in.rdbuf();
out.write(sens_data.c_str(), sens_data.size());
}
std::string EOFHiding::decode(const std::string &img_path, long long int sens_data_size)
{
if (sens_data_size <= 0)
{
throw std::runtime_error("Error SENS_DATA_SIZE_IS_INCCORRECT: Invalid data size (must be positive)");
}
std::ifstream in(img_path, std::ios::binary | std::ios::ate);
if (!in.is_open())
{
throw std::runtime_error("Error FILE_CAN_NOT_BE_OPEN: Failed to open input file '" + img_path + "'");
}
long long int fileSize = in.tellg();
in.seekg(fileSize - sens_data_size);
std::vector<char> hidden_data(sens_data_size);
in.read(hidden_data.data(), sens_data_size);
return std::string(hidden_data.begin(), hidden_data.end());
}