-
Notifications
You must be signed in to change notification settings - Fork 0
/
icode.cpp
359 lines (287 loc) · 4.98 KB
/
icode.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <vector>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
/* Fonction pour la barre de progression
* i = où rendu
* n = total
* w = largeur de barre
* actualize = actualiser ou non
*/
void showProgress(int i, int n, int w, int actualize)
{
float ratio = i / (float)n;
int c = ratio * w;
printf("%3d%% [", (int)(ratio*100) );
for (int i = 0; i < c; i++)
printf("=");
for (int i = c; i < w; i++)
printf(" ");
if(actualize)
printf("]\n\033[F\033[J");
else
printf("]\n");
}
/* Retourne la taille d'un fichier en bytes
* f = descripteur de fichier
*/
int whatSize(FILE* f)
{
int s;
fseek(f, 0, SEEK_END);
s = ftell(f);
fseek(f, 0, SEEK_SET);
return s;
}
/* Transforme en impair
* v = le bytes de l'image (r,g ou b)
*/
uchar odd(uchar v)
{
if(v % 2 != 0) return v;
else if(v > 0) return v-1;
else return 1;
}
/* Transforme en pair
* v = le bytes de l'image (r,g ou b)
*/
uchar even(uchar v)
{
if(v % 2 == 0) return v;
else if(v > 0) return v-1;
else return 0;
}
/* De byte à binaire
* c = byte
* bin = binaire
*/
void vBin(char c, char bin[8])
{
char j = 1;
for (int i = 0; i < 8; i++)
{
int t = (c & j);
if (t) bin[i] = 1;
else bin[i] = 0;
j*=2;
}
}
/* De int à binaire sur 32 bits
* c = int
* bin = binaire
*/
void vBin32(int c, char bin[32])
{
int j = 1;
for (int i = 0; i < 32; i++)
{
int t = (c & j);
if (t) bin[i] = 1;
else bin[i] = 0;
j*=2;
}
}
/* De binaire à byte
* bin = binaire
*/
char ivBin(char bin[8])
{
char c;
char j = 1;
for (int i = 0; i < 8; i++)
{
c = c + bin[i] * j;
j*=2;
}
return c;
}
/* De binaire sur 32 bits à int
* bin = binaire
*/
int ivBin32(char bin[32])
{
int c;
int j = 1;
for (int i = 0; i < 32; i++)
{
c = c + bin[i] * j;
j*=2;
}
return c;
}
/* Print de test
* c = byte
* bin = binaire
*/
void printT(char c, char bin[8])
{
for (int i = 0; i < 8; i++)
{
cout << (int) bin[i];
}
cout << " --> " << c << endl;
}
/* Fonction pour encoder
* i = chemin de l'image
* f = chemin du fichier
*/
void encode(char* i, char* f)
{
cout << endl;
cout << "Encoding phase begins :" << endl << endl;
Mat image;
image = imread(i, CV_LOAD_IMAGE_COLOR);
FILE* file = fopen(f, "r");
int s = whatSize(file);
char* text = (char*) malloc(s * sizeof(char));
fread(text, (size_t) s, 1, file);
fclose(file);
uchar* iPter = image.ptr();
int j = 0;
showProgress(j, s*8, 75, 1);
//encode la taille
char bin32[32];
vBin32(s, bin32);
for (int i = 0; i < 32; i++)
{
if (!bin32[i])
{
iPter[j] = even(iPter[j]);
}
else
{
iPter[j] = odd(iPter[j]);
}
j++;
}
//encode le message
for (int i = 0; i < s; i++)
{
char bin[9] = {0,0,0,0,0,0,0,0};
vBin(text[i], bin);
//printT(text[i], bin);
for (int k = 0; k < 8; k++)
{
if (!bin[k])
{
iPter[j] = even(iPter[j]);
}
else
{
iPter[j] = odd(iPter[j]);
}
j++;
showProgress(j, s*8, 75, 1);
}
}
showProgress(s*8, s*8, 75, 0);
cout << "done" << endl << endl;
free(text);
imwrite("result.png", image);
}
/* Fonction pour décoder
* i = chemin de l'image
* f = chemin du fichier
*/
void decode(char* i, char* f)
{
cout << endl;
cout << "Decoding phase begins :" << endl << endl;
Mat image;
image = imread(i, CV_LOAD_IMAGE_COLOR);
FILE* file = fopen(f, "w");
uchar* iPter = image.ptr();
int j = 0;
char c = 'a';
//décode la taille
char bin32[32];
for (int i = 0; i < 32; i++)
{
if (iPter[j] % 2)
{
bin32[i] = 1;
}
else
{
bin32[i] = 0;
}
j++;
}
int s = ivBin32(bin32);
//décode le message
for (int i = 0; i < s; i++)
{
char bin[9] = {0,0,0,0,0,0,0,0};
for (int k = 0; k < 8; k++)
{
if (iPter[j] % 2)
{
bin[k] = 1;
}
else
{
bin[k] = 0;
}
j++;
}
c = ivBin(bin);
//printT(c, bin);
fputc(c, file);
}
cout << "done" << endl << endl;
fclose(file);
}
/* Menu help
*/
void printHelp()
{
cout << endl;
cout << " ----------------------" << endl;
cout << "| Image Encoding usage |" << endl;
cout << " ----------------------" << endl << endl;
cout << "------------------ Help Menu -------------------" << endl << endl;
cout << "ic -h : To print this message" << endl;
cout << "ic -c <image> <file> : To encode" << endl;
cout << "ic -d <image> -o <file> : To decode" << endl << endl;
}
int main(int argc, char* argv[])
{
switch((argv[1])[1])
{
case 'h':
printHelp();
break;
case 'c':
if (argc < 3)
{
cout << endl;
cout << "Bad options, try this : " << endl;
printHelp();
}
encode(argv[2], argv[3]);
break;
case 'd':
if (argc < 4)
{
cout << endl;
cout << "Bad options, try this : " << endl;
printHelp();
}
decode(argv[2], argv[4]);
break;
default:
cout << endl;
cout << "Bad options, try this : " << endl;
printHelp();
break;
}
return 0;
}