-
Notifications
You must be signed in to change notification settings - Fork 3
/
b_encoder.cpp
114 lines (96 loc) · 3.29 KB
/
b_encoder.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
// Scrambling the Signal - encoder
// Usage: program_name carrier message encoded
// Description
// This program uses user password seeded random number generator to hide
// consequtive bits of binary message image within random ordered carrier image
// bytes.
// Author: Marcin Majkowski, [email protected]
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cv.h>
#include <highgui.h>
using namespace cv;
using namespace std;
unsigned long hash_djb2(const char* str);
int main(int argc, char* argv[])
{
if (argc != 4) { // incorrect number of arguments
cout << "Usage: program_name carrier message encoded" << endl;
return -1;
}
// loading carrier image
cout << "Loading carrier image (" << argv[1] << ")... ";
auto carrier = Mat_<uchar>(imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE));
if (!carrier.data) {
cout << "Could not open or find " << argv[1] << endl;
return -1;
}
cout << "done" << endl;
// loading message image
cout << "Loading message image (" << argv[2] << ")... ";
auto message = Mat_<uchar>(imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE));
if (!message.data) {
cout << "Could not open or find " << argv[2] << endl;
return -1;
}
cout << "done" << endl;
// checking dimension agreement
cout << "Checking dimension agreement... ";
if (carrier.size == message.size)
cout << "done (agreement)" << endl;
else {
cout << "done (disagreement)" << endl;
cout << "Images have different dimension" << endl;
return -1;
}
// prompting user for a character string password
std::cout << "Input password: ";
std::string password;
getline(std::cin, password);
// transforming password string to a 64-bit integer seed (with hash
// function)
auto seed = hash_djb2(password.c_str());
// generating a random shuffled vector of increasing indexes for all of the
// points in an image
cout << "Generating shuffled vector of point indexes... ";
std::vector<int> indexes(message.cols * message.rows);
{ // I use block to limit scope of i
int i = 0;
for (auto& index : indexes)
index = i++;
}
RNG rng(seed);
random_shuffle(indexes.begin(), indexes.end(), rng);
cout << "done" << endl;
// generating encoded image
cout << "Generating encoded image... ";
auto encoded = carrier.clone();
{ // I use block to limit scope of i
int i = 0;
for (auto& pixel : message) {
auto& encoded_pixel = *(encoded.begin() + indexes[i]);
if (encoded_pixel != 255) // preventing overflow
encoded_pixel += pixel ? 0 : 1; // if pixel == 0 then add 1
++i;
}
}
cout << "done" << endl;
// saving generated image
cout << "Saving encoded image (" << argv[3] << ")... ";
vector<int> compression_params = {CV_IMWRITE_PNG_COMPRESSION, 9};
imwrite(argv[3], encoded, compression_params);
cout << "done" << endl;
// success
return 0;
}
// from http://www.cse.yorku.ca/~oz/hash.html
unsigned long hash_djb2(const char* str)
{
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}