I'm trying to write a program to read ppm images, store them as objects, and write them out again. Ideally, I would like to store the pixels as int type objects, but I can only get even similar images with chars. Unfortunately even using char objects results in a grayed out version of the image. I'm not sure why changing the storage type is resulting in such a large change, or why the image colors are lost when the shape is retained.
I've tried to look through the myriad of other ppm program questions on here, but I can't make heads or tails of their answers (or if they're even relevant). I'm very unfamiliar with this language and have no idea what could be happening.
If anyone could explain what I'm doing wrong, and strategies I might use to store the data in int format instead of char, I would be immensely grateful.
Below is the file read and file write code for my ppm class, my main function just initializes a ppm object, calls readfile(), then calls writefile(). Somewhere therein it fails to preserve the image.
void PPM::readFile(std::string filename)
{
std::ifstream file;
std::string stuff;
char junk;
file.open(filename, std::ios::binary);
file >> stuff >> width >> height >> maxCol;
file >> std::noskipws >> junk;
int i = 0;
char r, g, b;
std::cout << width*height;
while (i < width*height)
{
file.read(&r, 1);
file.read(&g, 1);
file.read(&b, 1);
red.push_back(b);
grn.push_back(b);
blu.push_back(b);
i++;
std::cout << i << std::endl;
}
}
void PPM::writeFile(std::string filename) const
{
std::ofstream file;
file.open(filename, std::ios::binary);
file << "P6 " << width << " " << height << " " << maxCol << std::endl;
int i = 0;
std::cout << width << " " << height;
while (i < width*height)
{
file.write(&red[i], sizeof(red[i]));
file.write(&grn[i], sizeof(grn[i]));
file.write(&blu[i], sizeof(blu[i]));
std::cout << "iteration " << i << std::endl;
i++;
}
}
Thanks again for any help you can give
red
,green
andblu
? – Benjamin T