读/写PPM映像文件C ++ [英] Read/Write to PPM Image File C++

查看:275
本文介绍了读/写PPM映像文件C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试读/写PPM图片文件(.ppm)时,我知道如何:

Trying to read and write to/from a PPM Image file (.ppm) in the only way I know how:

std::istream& operator >>(std::istream &inputStream, PPMObject &other)
{
    inputStream.seekg(0, ios::end);
    int size = inputStream.tellg();
    inputStream.seekg(0, ios::beg);

    other.m_Ptr = new char[size];


    while (inputStream >> other.m_Ptr >> other.width >> other.height >> other.maxColVal)
    {
        other.magicNum = (string) other.m_Ptr;
    }

    return inputStream;
}

我的值对应于实际文件。所以我乐意尝试写数据:

My values correspond to the actual file. So I cheerfully attempt to write the data:

std::ostream& operator <<(std::ostream &outputStream, const PPMObject &other)
{
    outputStream << "P6"     << " "
        << other.width       << " "
        << other.height      << " "
        << other.maxColVal   << " "
       ;

    outputStream << other.m_Ptr;

    return outputStream;
}


$ b $ p

我确保使用std :: ios :: binary用于读写:

I am making sure to open the file using std::ios::binary for both reading and writing:

int main ()
{
    PPMObject ppmObject = PPMObject();
    std::ifstream image;
    std::ofstream outFile;

    image.open("C:\\Desktop\\PPMImage.ppm", std::ios::binary);
    image >> ppmObject;

    image.clear();
    image.close();

    outFile.open("C:\\Desktop\\NewImage.ppm", std::ios::binary);
    outFile << ppmObject;

    outFile.clear();
    outFile.close();

    return 0;
}

逻辑错误:

我只写图像的一部分。

I am only writing a portion of the image. There is no problem with the header or opening the file manually.

类公共成员变量:

m_Ptr成员变量是一个char *,高度,宽度maxColrVal都是整数。

The m_Ptr member variable is a char * and height, width maxColrVal are all integers.

尝试解决方案:

使用inputStream .read和outputStream.write读取和写入数据,但我不知道如何和我尝试了不工作。

Using inputStream.read and outputStream.write to read and write data but I don't know how and what I have tried doesn't work.

由于我的char * m_Ptr包含所有的像素数据。我可以遍历它:

Since my char * m_Ptr contains all of the pixel data. I can iterate through it:

for (int I = 0; I < other.width * other.height; I++) outputStream << other.m_Ptr[I];

但是这会导致运行时错误。

But this causes a run-time error for some reason..

推荐答案

根据 http:// fr。 wikipedia.org/wiki/Portable_pixmap ,P6是一个二进制图像。
这读取单个图像。注意,不执行检查。这需要添加。

Based on http://fr.wikipedia.org/wiki/Portable_pixmap, P6 is a binary image. This reads a single image. Note that no checking is performed. This needs to be added.

std::istream& operator >>(std::istream &inputStream, PPMObject &other)
{
    inputStream >> other.magicNum;
    inputStream >> other.width >> other.height >> other.maxColVal;
    inputStream.get(); // skip the trailing white space
    size_t size = other.width * other.height * 3;
    other.m_Ptr = new char[size];
    inputStream.read(other.m_Ptr, size);
    return inputStream;
}

此代码只能写一张图片。

This code writes a single image.

std::ostream& operator <<(std::ostream &outputStream, const PPMObject &other)
{
    outputStream << "P6"     << "\n"
        << other.width       << " "
        << other.height      << "\n"
        << other.maxColVal   << "\n"
       ;
    size_t size = other.width * other.height * 3;
    outputStream.write(other.m_Ptr, size);
    return outputStream;
}

m_Ptr只包含RGB像素值。

m_Ptr contains only the RGB pixel values.

我测试了从网上下载的图片上的代码( http://igm.univ-mlv.fr/~incerti/IMAGES/COLOR/Aerial.512.ppm )并使用以下结构PPMObject它工作。

I tested the code on an image I downloaded from the web (http://igm.univ-mlv.fr/~incerti/IMAGES/COLOR/Aerial.512.ppm) and using the following structure PPMObject it worked.

struct PPMObject
{
  std::string magicNum;
  int width, height, maxColVal;
  char * m_Ptr;
};

这篇关于读/写PPM映像文件C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆