Celestia12
Celestia12

Reputation: 13

Write a PPM file with C++ and get distorted result

I have to read, modify and write a PPM image. I tried to check if I got all the data of the PPM image in my array right by trying to write it to a new file. But the image i got looks like a broken TV:a pattern of this:

I have no idea what I did wrong, I'd like to know at least a way to check what could be wrong. These are my codes: Main: Image instance;

string filename = "Image03.ppm";
string format = "ppm";
bool fileWritten = false;
if (instance.load(filename, format)) {
    cout << "The file has been successfully loaded" << endl;

    cout << "The dimensions of the image are:" << endl;
    cout << instance.getWidth() << "x" << instance.getHeight() << endl;

...

bool imaging::Image::load(const string & filename, const string & format){
        string extension = filename.substr(filename.find_last_of(".") + 1);
        bool flag = false; // to stop the loop when a different letter is found.
        bool result = false; // stores the return value
        unsigned int sz = extension.size();
        for (unsigned int i = 0; i < sz; ++i) {
            if (tolower(extension[i]) != tolower(format[i]) && !flag) {
                cout << "The extension of the file does not match the required format." << endl;

                flag = true;

            }
        }

        if (!flag) {
            string filename1 = filename;
            const char *cstr = filename1.c_str();
            int pw = 0;
            int ph = 0;
            float* buff = ReadPPM(cstr, &pw, &ph);
            width = pw;
            height = ph;
            bool h = WritePPM(buff, width, height, "kokoo.ppm");

...

bool WritePPM(const float * data, int w, int h, const char * filename) {
        ofstream myfile;
        myfile.open(filename, ios::out | ios::binary);

        myfile << "P6\n" << w << "\n" << h << "\n255\n";
        for (int i = 0; i < w*h; i++) {
            myfile.write((char*)&data[i], sizeof(float));
        }
        myfile.close();
        return 0;
    }

...

float * ReadPPM(const char * filename, int* w, int* h) {
        ifstream myfile;
        myfile.open(filename,  ios::binary);
        if (myfile.is_open())
        {
            int i = 0;
            string letter;
            cout << "File successfully open" << endl;

            myfile.ignore(2, ' ');
            int width;
            int height;
            myfile >> width;
            myfile >> height;
            myfile.ignore(4, ' ');

            *w = width;
            *h =  height;

            int size = width*height * 3;
            int k = myfile.peek();
            while (k == 32) {                           //32 is ascii for whitespace.
                myfile.ignore(256,' ');
                k = myfile.peek();
            }
            unsigned char * temp = new unsigned char[size];
            myfile.read((char*)temp, size);
            float * buff = new float[size];



            while (i < size) {  
                buff[i] = (float)temp[i]/255;
                i++;
                cout << "before" << buff[i] << endl;
                cin.get();
            }

            delete[] temp;
            return buff;
            myfile.close();
        }
        else
        {
            cout << "Error opening file";
            cin.get();
            return 0;
        }




    }

Even if my code looks silly, it's why I'm new to this language and except if I did something terribly wrong, I like how it looks, it's an assignment anyway.

Upvotes: 0

Views: 2503

Answers (1)

Pedro
Pedro

Reputation: 862

If you really have to do convert to a float array, here is what you are doing wrong:

Your loop in WritePPM is totally wrong. You forgot the 3, you forgot to scale back by multiplying with 255, and you can't write float values to a PPM. It should be something like:

for (int i = 0; i < 3*w*h; i++) {
    unsigned char c = (unsigned char) (255.0f * data[i] + 0.5f); 
    myfile.write((char*) &c, sizeof(c));
}

I didn't check all the other code, but you definitely still have some other bugs, such as closing the file after the return statement in ReadPPM.

Upvotes: 1

Related Questions