-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimgCrypt.cpp
207 lines (173 loc) · 4.27 KB
/
imgCrypt.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
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
using namespace cv;
static void help()
{
cerr
<< "\n--------------------------------------------------------------------------" << endl
<< "./imgCrypt D imgFile" << endl
<< "./imgCrypt E imgFile textFile [destImg]" << endl
<< "Put D as first arg for decrypt, E for encrypt." << endl
<< "Does not support lossy format output. Make sure to give a lossless format for encrypt output." << endl
<< "--------------------------------------------------------------------------" << endl
<< endl;
}
Mat& Encrypt(Mat& I, ifstream& file);
void Decrypt(Mat& I);
bool tock;
unsigned char whole = 255;
unsigned char r = whole & 0xF;
unsigned char l = whole & 0xF0;
string type2str(int type) {
string r;
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> CV_CN_SHIFT);
switch ( depth ) {
case CV_8U: r = "8U"; break;
case CV_8S: r = "8S"; break;
case CV_16U: r = "16U"; break;
case CV_16S: r = "16S"; break;
case CV_32S: r = "32S"; break;
case CV_32F: r = "32F"; break;
case CV_64F: r = "64F"; break;
default: r = "User"; break;
}
r += "C";
r += (chans+'0');
return r;
}
int main( int argc, char* argv[])
{
help();
tock = false;
if(!strcmp(argv[1], "D"))
{
if (argc < 3)
{
cerr << "Not enough parameters." << endl;
return -1;
}
Mat I;
I = imread(argv[2], CV_LOAD_IMAGE_COLOR);
if (!I.data)
{
cerr << "The image" << argv[2] << " could not be loaded." << endl;
return -1;
}
Decrypt(I);
}
else if(!strcmp(argv[1], "E"))
{
if (argc < 4)
{
cerr << "Not enough parameters." << endl;
return -1;
}
Mat I, J;
I = imread(argv[2], CV_LOAD_IMAGE_COLOR);
if (!I.data)
{
cerr << "The image" << argv[2] << " could not be loaded." << endl;
return -1;
}
ifstream file(argv[3], std::ios::binary);
if (!file.is_open())
{
cerr << "Unable to open file";
return -1;
}
J = Encrypt(I, file);
file.close();
if (argc >= 5)
imwrite( argv[4], J );
else
imwrite( argv[2], J );
}
else
{
cerr << "Give D or E for first arg." << endl;
}
return 0;
}
Mat& Encrypt(Mat& I, ifstream& file)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols * channels;
if (I.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
int i,j;
uchar* p;
char c;
bool stop = false;
bool stopN = false;
for( i = 0; i < nRows && !stop; ++i)
{
p = I.ptr<uchar>(i);
for ( j = 0; j < nCols && !stop; ++j)
{
if(stopN)
stop = true;
if(!tock)
{
file.get(c);
if(file.eof())
stopN = true;
}
else
c = c >> 4;
//cout << p[j] << endl;
p[j] = (l & p[j]) | (r & c);
//cout << p[j] << endl << endl;
tock = !tock;
}
}
return I;
}
void Decrypt(Mat& I)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols * channels;
if (I.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
int i,j;
uchar* p;
char c;
bool stop = false;
for( i = 0; i < nRows && !stop; ++i)
{
p = I.ptr<uchar>(i);
for ( j = 0; j < nCols && !stop; ++j)
{
char info = r & p[j];
if(!tock)
c = info;
else
{
c = info << 4 | c;
cout << c;
if(c == -1)
stop = true;
c = 0;
}
tock = !tock;
}
}
}