-
Notifications
You must be signed in to change notification settings - Fork 12
/
lsb.cpp
200 lines (182 loc) · 4.61 KB
/
lsb.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
#include "lsb.h"
LSB::LSB()
{
}
LSB::~LSB()
{
}
void LSB::LoadImg(const QString & path)
{
img.load(path);
width = img.width();
height = img.height();
max = (width * height * 3 - 12) / 4;
}
void LSB::WriteImg(const QString & path)
{
img.save(path);
}
void LSB::CoverPixel(int TYPE)
{
int len = coded.length();
int index = 0;
int red, green, blue, alpha;
QRgb pixelValue, newPixelValue;
QVector <QString> list;
QString value;
for (int i=0;i<width;i++) {
for (int j=0;j<height;j++) {
if (index <= len)
{
pixelValue = img.pixel(i, j);
red = qRed(pixelValue);
green = qGreen(pixelValue);
blue = qBlue(pixelValue);
if (TYPE == PNG)
{
alpha = qAlpha(pixelValue);
}
list.clear();
list.shrink_to_fit();
list.append(QString::number(red, 2));
list.append(QString::number(green, 2));
list.append(QString::number(blue, 2));
for (int t=0;t<3;t++) {
value = list[t];
if (index >= len)
{
value[value.length()-1] = '0';
} else {
value[value.length()-1] = coded[index];
}
list[t] = value;
index++;
}
bool n;
red = list[0].toInt(&n, 2);
green = list[1].toInt(&n, 2);
blue = list[2].toInt(&n, 2);
if (TYPE == PNG)
{
newPixelValue = qRgba(red, green, blue, alpha);
} else {
newPixelValue = qRgb(red, green, blue);
}
img.setPixel(i, j, newPixelValue);
}
}
}
}
QString LSB::SplitPixel()
{
QString head = "";
int len = 0;
bool flag = false;
QString decoded = "", value;
QRgb pixelValue;
int index = 0;
QVector <QString> list;
for (int i=0;i<width;i++) {
for (int j=0;j<height;j++) {
pixelValue = img.pixel(i, j);
int red = qRed(pixelValue);
int green = qGreen(pixelValue);
int blue = qBlue(pixelValue);
list.clear();
list.shrink_to_fit();
list.append(QString::number(red, 2));
list.append(QString::number(green, 2));
list.append(QString::number(blue, 2));
for (int t=0;t<3;t++) {
value = list[t];
if (index < 12)
{
head += value[value.length() - 1];
} else if (!flag) {
bool n;
len = head.toInt(&n, 2);
flag = true;
if (len > 0)
{
decoded += value[value.length() - 1];
}
} else if (index < 12 + len) {
decoded += value[value.length() - 1];
} else {
return decoded;
}
index++;
}
}
}
return "";
}
void LSB::SetText(const QString & string)
{
raw = string;
}
void LSB::SetCoded(const QString & string)
{
coded = string;
}
QString LSB::GetDecoded()
{
return decoded;
}
QImage LSB::GetImg()
{
return img;
}
int LSB::GetMax()
{
return max;
}
int LSB::GetWidth()
{
return width;
}
int LSB::GetHeight()
{
return height;
}
void LSB::Encode()
{
QByteArray utf8Bytes = raw.toUtf8();
QString binaryString;
for (char byte : utf8Bytes) {
binaryString += QString("%1").arg(static_cast<unsigned char>(byte), 8, 2, QLatin1Char('0'));
}
coded = binaryString;
int len = coded.length();
QString lens = QString::number(len, 2).rightJustified(12, '0');
coded = lens + coded;
}
void LSB::Decode()
{
QByteArray utf8Data;
QStringList byteStrings;
int len = coded.length();
QString temp = "";
for (int i=0;i<(len/8);i++) {
temp += coded[8*i];
temp += coded[8*i+1];
temp += coded[8*i+2];
temp += coded[8*i+3];
temp += coded[8*i+4];
temp += coded[8*i+5];
temp += coded[8*i+6];
temp += coded[8*i+7];
byteStrings.append(temp);
temp = "";
}
for (const QString& byteString : byteStrings) {
bool ok;
int byteValue = byteString.toInt(&ok, 2);
if (ok) {
utf8Data.append(static_cast<char>(byteValue));
} else {
qWarning() << "Invalid binary string:" << byteString;
}
}
decoded = QString::fromUtf8(utf8Data);
}