So, I'm doing an image manipulation assignment for my class using QT, I was asked to save the current Image data that I have into PPM format manually, as well as loading it back to the QDialog program.
I managed to save the image correctly (verified the output file with gimp) but loading from the file created a disaster like the following
Here is the original:
And the bad loading:
And here is my file loading code:
//... opens the file and pulling out headers & etc...
unsigned char* data = new unsigned char[width*height*3];
//Manual loading each byte into char array
for(long h = 0; h < height; h++){ //for all 600 rows
getline(readPPM,temp); //readPPM is an ifstream, temp is a string
std::stringstream oneLine(temp);
for(long w = 0; w < width*3; w++){ //to every position in that line 800*3
int readVal;
oneLine >> readVal; //string stream autofill get the int value instead of just one number
data[width*h+w] = (unsigned char)readVal; //put it into unsign char
}
}
//Method 1: create the QImage with constructor
(it blacked out 2/3 of the bottom of the image, and I'm not exactly familiar with QImage data type)
imageData = QImage(data,width,height,QImage::Format_BGR888);
//Method 2: manually setting each pixel
for(int h = 0; h < height; h++){
for(int w = 0; w < width; w++){
int r,g,b;
r = (int)data[width*h+w*3];
g = (int)data[width*h+w*3+1];
b = (int)data[width*h+w*3+2];
QColor color = qRgb(r,g,b);
imageData.setPixelColor(w,h,color);
}
}
//...set image to display...
I would like the display to look like the original image when I load from the file, and I'm not sure what went wrong to cause the corruption, please help