I'm trying to make steganography with C++ using Magick++. Here is my code for now
#include <iostream>
#include <fstream>
#include <string>
#include <Magick++.h>
#include <bitset>
#include <vector>
int main()
{
using namespace Magick;
using namespace std;
int wd = 0, ht = 0;
InitializeMagick(nullptr);
ifstream f("FileToHide.exe", ios::binary);
char tmp;
string hiddenmsg;
vector< bitset<8> > bits;
copy(istream_iterator<char>(f),
istream_iterator<char>(),
back_inserter(bits));
int posvect = 0;
int posbits = 0;
Image src("Shifr.jpg");
src.modifyImage();
for(size_t i = 1; i <= src.size().height(); i++)
{
for(size_t j = 1; j <= src.size().width(); j++)
{
auto pixel = src.getPixels(j, i, j, i);
bitset<16> tmpbits(pixel[0].red);
tmpbits[15] = bits[posvect][posbits];
pixel[0].red = tmpbits.to_ulong();
posbits++;
if(posbits == 8)
{
posvect++;
posbits = 0;
if(posvect == bits.size())
{
src.syncPixels();
src.write("Shifr2.jpg");
return 0;
}
}
}
}
throw invalid_argument("File is too big");
}
I'm using picture with resolution 885x1080 and type .jpg. So when i=540 and j=443, src.getPixels(j, i, j, i) returns nullptr. I tried to skip it, but every next iteration returns nullptr too.
You're probably encountering undefined behaviour as you're not using
getPixels
correctly. Please see the documentationI.e. you want to use
src.getPixels(j, i, 1, 1);
for a single pixel