-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.cpp
118 lines (117 loc) · 2.61 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
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
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <bits/stdc++.h>
#include "zlib.h"
using namespace cv;
using namespace std;
char terminate_ch = '#';
char c[1000000];
char b[1000000];
int poww(int a,int r)
{
return (int)pow(a,r);
}
char generatechar(vector<int> bit)
{
int n=1;
int val=-128;
for(int i=7;i>=0;i--)
{
val+=bit[i]*n;
n=n*2;
}
return (char)(val);
}
void decompressString(char b[], uInt ret)
{
z_stream infstream;
infstream.zalloc = Z_NULL;
infstream.zfree = Z_NULL;
infstream.opaque = Z_NULL;
// setup "b" as the input and "c" as the compressed output
infstream.avail_in = (uInt)(ret); // size of input
infstream.next_in = (Bytef *)b; // input char array
infstream.avail_out = (uInt)sizeof(c); // size of output
infstream.next_out = (Bytef *)(c); // output char array
// the actual DE-compression work.
inflateInit(&infstream);
inflate(&infstream, Z_NO_FLUSH);
inflateEnd(&infstream);
}
char getch(Mat img,int &i,int &j,int &k)
{
vector<int> bits;
for(int p=0;p<8;p++)
{
if(k==9)
{
if(j==(img.cols-1))
{
j=0;
i++;
k=0;
}
else
{
k=0;
j++;
}
}
if(k<3)
bits.push_back(img.at<Vec3b>(i,j)[0]&poww(2,k)&7);
else if(k<6)
bits.push_back(img.at<Vec3b>(i,j)[1]&poww(2,k-3)&7);
else
bits.push_back(img.at<Vec3b>(i,j)[2]&poww(2,k-6)&7);
k++;
if(bits[p]>0)
bits[p]=1;
}
// for(int p=0;p<bits.size();p++)
// cout<<bits[p];
return generatechar(bits);
}
string decode(Mat img,int &i,int &j,int &k,uInt &avail)
{
char ch = 'a';
string hiddenstr = "";
char prev=ch;
while(ch!=terminate_ch || prev!=terminate_ch)
{
prev=ch;
ch = getch(img,i,j,k);
if(ch!= terminate_ch || prev!=terminate_ch)
hiddenstr += ch;
}
char ch1 = getch(img,i,j,k);
char ch2 = getch(img,i,j,k);
char ch3 = getch(img,i,j,k);
// cout<< ch<<endl;
avail = (uInt)ch1 + 100*(uInt)ch2 + 10000*(uInt)ch3;
return hiddenstr.substr(0,hiddenstr.length()-1);
}
int main( int argc, char** argv )
{
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR);
if(! image.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
strcpy(c,"");
int aa=0,bb=0,cc=0;
uInt extracted=5;
string tmp = decode(image,aa,bb,cc,extracted);
for(int i=0;i<extracted;i++)
b[i]=tmp[i];
decompressString(b,extracted);
printf("Compressed size is: %u\n", extracted);
printf("Uncompressed size is: %lu\n", strlen(c));
// for(int i=0;i<strlen(b);i++)
// {
// cout<<b[i]<<"->"<<(int)b[i]<<endl;
// }
printf("Uncompressed string is: \n%s\n", c);
return 0;
}