-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
137 lines (113 loc) · 4.02 KB
/
main.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
#include <iostream>
#include <random>
#include <chrono>
#include "opencv4/opencv2/opencv.hpp"
int main(int argc, char *argv[]) {
if (argc != 4) {
std::cout << "usage: JLVEA-cpp [encrypt|decrypt] FILENAME SEED" << std::endl;
return 3;
}
//Read parameters
int mode = 0;
if (std::strcmp(argv[1], "encrypt") != 0) {
mode = 0;
} else if (std::strcmp(argv[1], "decrypt") != 0) {
mode = 1;
} else {
std::cerr << "error: not proper encryption mode: " << argv[1] << std::endl;
return 4;
}
std::string filename = argv[2];
std::ifstream read;
read.open(filename);
if (!read) {
std::cerr << "error: no such file: " << argv[2] << std::endl;
return 5;
}
int seed = 0;
try {
seed = std::stoi(argv[3]);
} catch (std::invalid_argument& e) {
std::cerr << "error: please input proper seed number." << std::endl;
return 6;
}
//Read video frame information
cv::VideoCapture vid(filename);
int width = (int)vid.get(cv::CAP_PROP_FRAME_WIDTH);
int height = (int)vid.get(cv::CAP_PROP_FRAME_HEIGHT);
int pcount = width;
cv::VideoWriter out;
out.open("enc.mp4", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 30.0, cv::Size(width, height));
std::random_device random_device;
std::mt19937_64 rnd(seed);
std::uniform_int_distribution<int> wrange(0, width - 1);
std::uniform_int_distribution<int> hrange(0, height - 1);
std::srand(seed);
int plist[3][pcount][4];
//Generate Permutation List
for (int i = 0; i < 3; i++) {
for (int j = 0; j < pcount; j++) {
for (int k = 0; k < 4; k++) {
if (k < 2) {
plist[i][j][k] = wrange(rnd);
} else {
plist[i][j][k] = hrange(rnd);
}
}
}
}
double total_time = 0;
int frame_no = 0;
if (mode == 0) {
//Permutate Image
while(true) {
std::chrono::system_clock::time_point start_enc = std::chrono::system_clock::now();
cv::Mat frame = cv::Mat();
vid >> frame;
if (frame.empty()) {
break;
}
std::cout << "Encrypting frame no." << std::to_string(frame_no) << std::endl;
frame_no += 1;
//Channel Split
cv::Mat split[3];
cv::split(frame, split);
for (int j = 0; j < 3; j++) {
for (int k = 0; k < pcount; k++) {
int c1 = plist[j][k][0];
int c2 = plist[j][k][1];
int r1 = plist[j][k][2];
int r2 = plist[j][k][3];
cv::Mat col1 = split[j].col(c1);
cv::Mat col2 = split[j].col(c2);
cv::Mat row1 = split[j].row(r1);
cv::Mat row2 = split[j].row(r2);
col1.copyTo(split[j].col(c2));
col2.copyTo(split[j].col(c1));
row1.copyTo(split[j].row(r2));
row2.copyTo(split[j].row(r1));
for (int l = 0; l < 4; l++) {
if (l < 2) {
plist[j][k][l] = wrange(rnd);
} else {
plist[j][k][l] = hrange(rnd);
}
}
}
}
cv::Mat enc_image = cv::Mat();
cv::merge(split, 3, enc_image);
out.write(enc_image);
std::chrono::duration<double> enc_time = std::chrono::system_clock::now() - start_enc;
total_time += enc_time.count();
/*
cv::namedWindow("Test", cv::WINDOW_AUTOSIZE);
cv::imshow("Test", enc_image);
cv::waitKey(1);
*/
}
}
std::cout << "Total encryption time: " << std::to_string(total_time) << std::endl;
std::cout << "Average encryption time: " << std::to_string(total_time / 100) << std::endl;
return 0;
}