1

我有一个图像以原始字节的形式存储在内存中,即我有一个char*指向图像数据的内存位置。现在,我需要以某种方式验证图像数据是否合法。

我目前尝试的是简单地将字节转储到文件中。我尝试转储到 3 种类型的文件,但没有运气:

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"

有什么方法可以查看内容吗?顺便提一下,我是在 Win 平台上编写这段代码的。几年前,我记得在 MAC OS X 上做过类似的事情,结果很成功!

4

1 回答 1

1

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
于 2015-06-20T09:35:22.310 回答