Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

ifstream - Read/Write to PPM Image File C++

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;
}

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;
}

Logic Error:

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

Class public member variables:

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

Attempted Solution:

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.

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"     << "
"
        << other.width       << " "
        << other.height      << "
"
        << other.maxColVal   << "
"
       ;
    size_t size = other.width * other.height * 3;
    outputStream.write(other.m_Ptr, size);
    return outputStream;
}

m_Ptr contains only the RGB pixel values.

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;
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...