int main(){
std::fstream myfile; // file object is created
myfile.open ("green.ppm");
std::string line;
unsigned red,green,blue; //to output values on 0 -255 scale.
int width, height = 0;
if (myfile.is_open())
{
std::getline (myfile,line); //type of file, skip, it will always be for this code p6
std::getline (myfile,line); // width and height of the image
std::stringstream lineStream(line); //extract the width and height;
lineStream >> width;
lineStream >> height;
// std::cout<< width << " " << height <<" \n";
getline (myfile,line); //skip magic number
getline (myfile,line); // reach the matrix of numbers
for (int i = 0; i<(width*height*3) ; i= i+3){
char num = line[i]; uint8_t number = num; red = number;
num = line[i+1]; number = num; green = number;
num = line[i+2]; number = num; blue = number;
std::cout<<"pixel " << i/3 << " is " << red << " " << green << " " << blue << std::endl;
}
//char to uint_8t to unsigned is a basic an inefficient way I found that takes the pixel rgb values in my ppm file and allows me to interpret them from a range of 0-255
}
// cout<<counter<<endl;
myfile.close();
return 0;
}
Когда я запускаю этот код по разным изображениям ppm, он действительно извлекает значения rgb правильно, но проблема в том, что он не делает это целиком. базовое изображение 800 x 800 имеет 640000 пикселей, и этот код читает около 40800, а затем заканчивается так, как будто его больше нет.Проблемы с чтением ppm p6 до завершения. C++
Я думаю, что это связано с непониманием формата ppm. Я думал, что за пределами формата заголовка, который является файлом с типом, шириной и размером, и магическим номером, была только одна строка и не более символов «\ n». Поэтому матрицу можно было бы читать как непрерывный массив символов.
Так почему же эта программа останавливается в таком нечетном месте?
Спасибо Я понимаю, что это сейчас. – DoingItForTheCorgi
Как печатаются элементы массива в пустых пространствах? printf ("% d% c% c% c \ n", i/3, красный, зеленый, синий); не работает. и ни один cout << "" красный << "" << зеленый << "" << синий; – DoingItForTheCorgi
@DoingItForTheCorgi попробовать с 'cout << static_cast (pixelData [i])' –