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×800の画像には640000ピクセルがあり、このコードは約40800を読み取り、それがもう存在しないかのように終了します。ppm p6イメージの読み取りが完了するまでの問題。 C++
これは、ppmフォーマットそのものの誤解から生じると思います。私は、タイプ、幅とサイズ、そしてマジックナンバーのファイルであるヘッダーフォーマット以外に、もう1つの行とそれ以上の '\ n'文字はないと考えました。したがって、行列は連続した文字の配列として読み取ることができます。
なぜこのプログラムがこのような奇妙な場所で停止していますか?
おかげで私は今、それを実現します。 – DoingItForTheCorgi
アレイの印刷要素が空白をどのように与えているのでしょうか? printf( "%d%c%c%c \ n"、i/3、赤、緑、青);動かない。 いずれもcout << "" red << "" << green << "" blue; – DoingItForTheCorgi
@DoingItForTheCorgiは 'cout << static_cast(pixelData [i])' –