我使用lodePNG库对png图像进行编码,并使用导入的txt文件更改像素的LSB。我已经编译了程序,但我不确定PNG文件是否实际上是按照我的按位操作进行编码的。
lodePNG库从PNG图像解码/编码,并将像素存储在矢量"图像",每像素4个字节,有序RGBARGBA ...,
void decodeOneStep(const char* filename)
{
unsigned width, height;
//decode
unsigned error = lodepng::decode(image, width, height, filename);
//if there's an error, display it
if (error) std::cout << "decoder error " << error << ": " <<
lodepng_error_text(error) << std::endl;
}
程序获取文本文件和PNG文件的命令行参数。我还没有为参数包含错误检查。
int const MAX_SIZE = 100;
std::vector<unsigned char> image;
int main(int argc, char *argv[])
{
const char* filename;
char* textArray = new char[MAX_SIZE];
std::ifstream textfile;
textfile.open(argv[1]);
int numCount = 0;
while (!textfile.eof() && numCount < MAX_SIZE)
{
textfile.get(textArray[numCount]); //reading single character from file to array
numCount++;
}
textfile.close();
filename = argv[2];
decodeOneStep(filename);
unsigned width = 512, height = 512;
image.resize(width * height * 4);
int pixCount = 0;
for (int i = 0; i < numCount - 1; i++) {
std::cout << textArray[i];
for (int j = 0; j < 8; j++) {
std::cout << ((textArray[i]) & 1); //used to see actual bit value.
image[pixCount++] |= ((textArray[i]) & 1);
(textArray[i]) >>= 1;
}
std::cout << std::endl;
}
encodeOneStep(filename, image, width, height);
在for循环中,我将遍历向量中的每个像素,并用char中的一些位替换LSB。由于char是8个字节,因此for循环循环8次。该程序适用于大多数PNG图像和不超过大小的文本,但我不确定按位操作实际上是在做什么。另外,我如何移位这些位以便将char位从MSB存储到LSB?我觉得我对像素值(位)如何存储在数组中的方法有所了解。
编辑:测试我已经运行了新的位操作:
for (int j = 7; j >= 0; j--) {
//These tests were written to see if the 4-bits of the pixels were actually being replaced.
//The LSB of the pixel bits are replaced with the MSB of the text character.
std::cout <<"Initial pixel 4-bits: " << std::bitset <4>(image[pixCount]) << " ";
std::cout << "MSB of char: " << ((textArray[i] >> j) & 0x01) << " ";
std::cout << "Pixel LSB replaced: " << ((image[pixCount] & mask) | ((textArray[i] >> j) & 0x01)) << " ";
image[pixCount] = (image[pixCount] & mask) | ((textArray[i] >> j) & 0x01);
pixCount++;
std::cout << std::endl;
}
测试结果:
For char 'a'
Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
Initial pixel 4-bits : 0001 MSB: 1 Pixel LSB replaced: 1
Initial pixel 4-bits : 0001 MSB: 1 Pixel LSB replaced: 1
Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
Initial pixel 4-bits : 0000 MSB: 0 Pixel LSB replaced: 0
Initial pixel 4-bits : 0001 MSB: 1 Pixel LSB replaced: 1
答案 0 :(得分:0)
首先需要在嵌入秘密位之前清除像素的lsb。
unsigned char mask = 0xfe; // in binary 11111110
// and in your loop
image[pixCount] = (image[pixCount] & mask) | (textArray[i] & 1);
pixCount++;
如果要将秘密的每个字符的位从最高位嵌入到最低位,则需要倒计时j
循环。
for (int j = 7; j >= 0; j--) {
image[pixCount] = (image[pixCount] & mask) | ((textArray[i] >> j) & 0x01);
pixCount++;
}
编辑:为了解释上面的代码,image[pixCount] & mask
是像素和所选掩码值(二进制1111110)之间的AND运算符,因此结果是lsb被清除
(textArray[i] >> j) & 0x01
将您的字符向左移动j
并仅保留lsb。如果你绘制出数学数据,这就是你得到的数据
// assume our character has the bits `abcdefgh`
j = 7
(character >> j) & 0x01 = 0000000a & 0x01 = a
j = 6
(character >> j) & 0x01 = 000000ab & 0x01 = b
j = 5
(character >> j) & 0x01 = 00000abc & 0x01 = c
// and so on
j = 0
(character >> j) & 0x01 = abcdefgh & 0x01 = h