I found some C++ code here on SO for reading / writing images. I would like to improve it so I can rotate etc. images. However, at the beginning I have some problems. When I write the image, it seems that my read function read only a piece of it, since it writes to file only a piece of the original image. Please see my code and input, output images.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char **argv)
{
ifstream in;
in.open("OldImage.ppm", std::ios::binary);
ofstream out;
std::string magic_number;
int width, height, maxColVal, i, j;
in >> magic_number;
in >> width >> height >> maxColVal;
in.get();
char **image;
image = new char* [width];
for(i=0; i<width; i++)
{
image[i] = new char [height];
for(j=0; j<height; j++)
{
in >> image[i][j];
}
}
out.open("NewImage.ppm", std::ios::binary);
out << "P3" << "\n"
<< width << " "
<< height << "\n"
<< maxColVal << "\n"
;
for(i=0; i<width; i++)
{
for(j=0; j<height; j++)
{
out << image[i][j];
}
}
in.clear();
in.close();
out.clear();
out.close();
return 0;
}
Input image: https://www.dropbox.com/s/c0103eyhxzimk0j/OldImage.ppm?dl=0
Output image: https://www.dropbox.com/s/429i114c05gb8au/NewImage.ppm?dl=0
According to this doc there are 2 forms of ppm image file: raw and plain. You seem to be assuming the normal raw format but you are using magic number P3 which is for plain ppm. Try P6.
Also, your height and width loops should be the other way round, but this doesnt affect the result. Presumably it is part of your code to rotate the image.