-
Notifications
You must be signed in to change notification settings - Fork 23
/
decode.cpp
87 lines (69 loc) · 1.67 KB
/
decode.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
#include <iostream>
//#include <cv.h>
#include <highgui.h>
//#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
// Checks whether the bit is set or not at a particular position.
// Returns true if set
// Returns false if not set
bool isBitSet(char ch, int pos) {
// 7 6 5 4 3 2 1 0
ch = ch >> pos;
if(ch & 1)
return true;
return false;
}
int main(int argc, char** argv) {
/*
./decode output_image.png
argv[0] = ./decode
argv[1] = output_image.png
*/
// Checks if proper number of arguments are passed
if(argc != 2) {
cout << "Arguments Error" << "\n";
exit(-1);
}
// Stores original image
Mat image = imread(argv[1]);
if(image.empty()) {
cout << "Image Error\n";
exit(-1);
}
// char to work on
char ch=0;
// contains information about which bit of char to work on
int bit_count = 0;
/*
To extract the message from the image, we will iterate through the pixels and extract the LSB of
the pixel values (RGB) and this way we can get our message.
*/
for(int row=0; row < image.rows; row++) {
for(int col=0; col < image.cols; col++) {
for(int color=0; color < 3; color++) {
// stores the pixel details
Vec3b pixel = image.at<Vec3b>(Point(row,col));
// manipulate char bits according to the LSB of pixel values
if(isBitSet(pixel.val[color],0))
ch |= 1;
// increment bit_count to work on next bit
bit_count++;
// bit_count is 8, that means we got our char from the encoded image
if(bit_count == 8) {
// NULL char is encountered
if(ch == '\0')
goto OUT;
bit_count = 0;
cout << ch;
ch = 0;
}
else {
ch = ch << 1;
}
}
}
}
OUT:;
return 0;
}