Dump raw image data

2.9k views Asked by At

I have an image stored in memory in the form of raw bytes i.e. I have a char* pointing to the memory location of the image data. Now, I need to somehow validate if the image data is legitimate.

What I have currently tried is to simply dump the bytes into a file. I tried dumping into 3 types of files, but no luck:

std::ofstream ofs;
ofs.open("Image.raw", std::ofstream::out);
ofs.write((char*)imgData, imageInfo.imageLen);
ofs.close();
// Have also tried "Image.tiff" and "Image.ppm"

Is there any way to view the contents? Just to mention, I am writing this code on Win platform. A few years back, I remember doing the similar thing on MAC OS X, which yielded successful results!.

1

There are 1 answers

6
Mark Setchell On BEST ANSWER

You can write it straight out as RGB in binary like you already have - say to a file called image.rgb.

Then use ImageMagick, which is installed on most Linux distros, and available for OSX and Windows to convert it to PNG, JPEG or something more common:

convert -size 300x400 -depth 8 image.rgb result.png

or

convert -size 300x400 -depth 8 image.rgb result.jpg

You will need to tell ImageMagick the dimensions of the image as above because they are obviously not embedded within a raw file like they would be in a JPEG or PNG with a header.

If the filename you choose does not end in .rgb, you will need to prefix it with RGB: like this

convert -size ...   RGB:something.bin   result.png