This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hider_png.cpp
363 lines (283 loc) · 7.48 KB
/
hider_png.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
#include "hider_png.h"
namespace hider_png_internal
{
#define SIGN_PX_CNT 0x4
#define DATA_TYPE_BITS_CNT 0x1
// Filling signature bits with 0x7, 0x1, 0x3, 0x5, ...
const auto signature_bits_boost =
[]() -> bdb<uint8_t>
{
bdb<uint8_t> bits(SIGN_PX_CNT * 3);
uint8_t bit = 0x7;
for (uint64_t i = 0; i < SIGN_PX_CNT; ++i)
{
bits <<= 0x3;
bits |= bdb<uint8_t>(SIGN_PX_CNT * 3, bit);
bit = (bit + 2) % 8;
}
return bits;
}();
// Functions
const uint64_t get_dss(const png::image<png::rgb_pixel> &image)
{
return ceil( std::log2( image.get_width() * image.get_height() * 3 - SIGN_PX_CNT * 3 - DATA_TYPE_BITS_CNT ) );
}
void encode(png::image<png::rgb_pixel> &image, const bdb<uint8_t> &bits, const std::string &output_filename)
{
const uint64_t bits_size = bits.size();
uint64_t x = 0, y = 0;
uint64_t bits_cnt = bits_size;
uint64_t cur_bit_i = bits_size - 1;
while ( bits_cnt )
{
png::byte rgb[] = {image[y][x].red, image[y][x].green, image[y][x].blue};
for (uint8_t k = 0; (k < 3) && bits_cnt; ++k, --bits_cnt)
{
if ( bits[cur_bit_i--] == 0 )
{
rgb[k] &= 0b11111110;
}
else
{
rgb[k] |= 0b00000001;
}
}
image[y][x] = png::rgb_pixel
(
rgb[0],
rgb[1],
rgb[2]
);
if ( ++x == image.get_width() )
{
x = 0;
++y;
}
}
image.write(output_filename);
}
// TODO... maybe later, idn...
void decode()
{
// It's still empty...
}
} // end hider_png_internal
void hider_png::encode(const std::string &filename, const std::vector<uint8_t> &data, uint8_t data_type, const std::string &output_filename)
{
png::image<png::rgb_pixel> image(filename);
const uint64_t data_bits_cnt = data.size() * 8;
const uint64_t dss = hider_png_internal::get_dss(image);
const uint64_t bits_cnt = SIGN_PX_CNT * 3 + DATA_TYPE_BITS_CNT + dss + data_bits_cnt;
const bdb<uint8_t> &signature_bits = hider_png_internal::signature_bits_boost;
const bdb<uint8_t> type_bits(DATA_TYPE_BITS_CNT, data_type);
const bdb<uint8_t> data_size_bits(dss, data_bits_cnt);
const auto bits =
[&signature_bits, &type_bits, &dss, &data_size_bits, &data_bits_cnt, &bits_cnt, &data]() -> bdb<uint8_t>
{
bdb<uint8_t> bits(bits_cnt);
for (int64_t i = SIGN_PX_CNT * 3 - 1; i >= 0; --i)
{
bits <<= 1;
bits |= bdb<uint8_t>(bits_cnt, signature_bits[i]);
}
for (int64_t i = DATA_TYPE_BITS_CNT - 1; i >= 0; --i)
{
bits <<= 1;
bits |= bdb<uint8_t>(bits_cnt, type_bits[i]);
}
for (int64_t i = dss - 1; i >= 0; --i)
{
bits <<= 1;
bits |= bdb<uint8_t>(bits_cnt, data_size_bits[i]);
}
for (int64_t i = 0; i < data.size(); ++i)
{
for (int64_t j = 0; j < 8; ++j)
{
bits <<= 1;
bits |= bdb<uint8_t>(bits_cnt, (data[i] & (0b10000000 >> j)) >> (7 - j));
}
}
return bits;
}();
#ifdef IN_IMAGE_HIDER_DEBUG
std::cout << "RECEIVED BITS FOR ENCODE - " << bits << "\n\n";
std::cout << "SIGNATURE SIZE - " << SIGN_PX_CNT * 3 << " bit.\n";
std::cout << "TYPE SIZE - " << DATA_TYPE_BITS_CNT << " bit.\n";
std::cout << "DATA SIZE - " << dss << " bit.\n";
std::cout << "DATA - " << data_bits_cnt << " bit.\n\n";
std::cout << "TOTAL BITS - " << bits_cnt << " bit.\n\n";
std::cout << "PIXELS USED - " << ceil(bits_cnt / 3) << " px.\n";
#endif // IN_IMAGE_HIDER_DEBUG
hider_png_internal::encode(image, bits, output_filename);
}
std::vector<uint8_t> hider_png::decode(const std::string &filename)
{
png::image<png::rgb_pixel> image(filename);
// GETTING SIGNATURE
const uint64_t sign_bits_cnt = SIGN_PX_CNT * 3;
const bdb<uint8_t> &signature_bits = hider_png_internal::signature_bits_boost;
bdb<uint8_t> signature_bits_received(sign_bits_cnt);
uint64_t x = 0, y = 0;
uint64_t sign_bit_i = sign_bits_cnt;
while ( sign_bit_i )
{
for (uint8_t i = 0; (i < 3) && sign_bit_i; ++i, --sign_bit_i)
{
signature_bits_received <<= 1;
if (i == 0)
{
signature_bits_received |= bdb<uint8_t>(sign_bits_cnt, image[y][x].red & 0b00000001);
}
else if (i == 1)
{
signature_bits_received |= bdb<uint8_t>(sign_bits_cnt, image[y][x].green & 0b00000001);
}
else if (i == 2)
{
signature_bits_received |= bdb<uint8_t>(sign_bits_cnt, image[y][x].blue & 0b00000001);
}
}
if ( ++x == image.get_width() )
{
x = 0;
++y;
}
}
if (signature_bits_received != signature_bits)
exit(1);
#ifdef IN_IMAGE_HIDER_DEBUG
std::cout << "\nSIGNATURE CORRECT - " << signature_bits_received << "\n";
#endif // IN_IMAGE_HIDER_DEBUG
//
typedef enum _color_i
{
RED = 0,
GREEN,
BLUE
} _color_i; // last color identifier | 0 - red, 1 - green, 2 - blue
_color_i color_i = BLUE;
// GETTING TYPE
bdb<uint8_t> type_bits_received(DATA_TYPE_BITS_CNT);
uint64_t type_bit_i = DATA_TYPE_BITS_CNT;
while ( type_bit_i )
{
for (uint8_t i = 0; (i < 3) && type_bit_i; ++i, --type_bit_i)
{
std::cout << type_bits_received << std::endl;
type_bits_received <<= 1;
if (color_i == BLUE)
{
type_bits_received |= bdb<uint8_t>(DATA_TYPE_BITS_CNT, image[y][x].red & 0b00000001);
color_i = RED;
}
else if (color_i == RED)
{
type_bits_received |= bdb<uint8_t>(DATA_TYPE_BITS_CNT, image[y][x].green & 0b00000001);
color_i = GREEN;
}
else if (color_i == GREEN)
{
type_bits_received |= bdb<uint8_t>(DATA_TYPE_BITS_CNT, image[y][x].blue & 0b00000001);
color_i = BLUE;
if ( ++x == image.get_width() )
{
x = 0;
++y;
}
}
}
}
#ifdef IN_IMAGE_HIDER_DEBUG
std::cout << "TYPE - " << type_bits_received << "\n";
#endif // IN_IMAGE_HIDER_DEBUG
//
// GETTING DATA SIZE
const uint64_t dss = hider_png_internal::get_dss(image);
bdb<uint8_t> data_size(dss);
uint64_t dss_i = dss;
while ( dss_i )
{
for (uint8_t i = 0; (i < 3) && dss_i; ++i, --dss_i)
{
data_size <<= 1;
if (color_i == BLUE)
{
data_size |= bdb<uint8_t>(dss, image[y][x].red & 0b00000001);
color_i = RED;
}
else if (color_i == RED)
{
data_size |= bdb<uint8_t>(dss, image[y][x].green & 0b00000001);
color_i = GREEN;
}
else if (color_i == GREEN)
{
data_size |= bdb<uint8_t>(dss, image[y][x].blue & 0b00000001);
color_i = BLUE;
if ( ++x == image.get_width() )
{
x = 0;
++y;
}
}
}
}
#ifdef IN_IMAGE_HIDER_DEBUG
std::cout << "DSS - " << dss << " | DATA SIZE - " << data_size << "\n";
#endif // IN_IMAGE_HIDER_DEBUG
//
// GETTING DATA SIZE
const uint64_t data_size_u64 = data_size.to_ulong();
bdb<uint8_t> data(data_size_u64);
uint64_t data_i = data_size_u64;
while ( data_i )
{
for (uint8_t i = 0; (i < 3) && data_i; ++i, --data_i)
{
data <<= 1;
if (color_i == BLUE)
{
data |= bdb<uint8_t>(data_size_u64, image[y][x].red & 0b00000001);
color_i = RED;
}
else if (color_i == RED)
{
data |= bdb<uint8_t>(data_size_u64, image[y][x].green & 0b00000001);
color_i = GREEN;
}
else if (color_i == GREEN)
{
data |= bdb<uint8_t>(data_size_u64, image[y][x].blue & 0b00000001);
color_i = BLUE;
if ( ++x == image.get_width() )
{
x = 0;
++y;
}
}
}
}
#ifdef IN_IMAGE_HIDER_DEBUG
std::cout << "DATA - " << data << "\n";
#endif
//
const std::vector<uint8_t> data_bytes =
[&data]() -> std::vector<uint8_t>
{
std::vector<uint8_t> data_v;
bdb<uint8_t> byte_bs(8);
uint8_t byte = 0;
for (int64_t i = data.size() - 1; i >= 0; --i)
{
byte_bs <<= 1;
byte_bs |= bdb<uint8_t>(8, data[i]);
if (i % 8 == 0)
{
data_v.push_back(byte_bs.to_ulong());
}
}
return data_v;
}();
return data_bytes;
}