读取ppm p6图像至完成时出现问题。c++; intmain(){ std::fstream myfile;//已创建文件对象 myfile.open(“green.ppm”); std::字符串行; 无符号的红、绿、蓝;//以0-255的比例输出值。 整数宽度,高度=0; 如果(myfile.is_open()) { std::getline(myfile,line);//文件类型,skip,它将始终用于此代码p6 std::getline(myfile,line);//图像的宽度和高度 std::stringstream lineStream(line);//提取宽度和高度; 线流>>宽度; lineStream>>高度; //STD::CUT我对C++不太好,但我会尝试解释这里的错误。
如果文件类型为读取ppm p6图像至完成时出现问题。c++; intmain(){ std::fstream myfile;//已创建文件对象 myfile.open(“green.ppm”); std::字符串行; 无符号的红、绿、蓝;//以0-255的比例输出值。 整数宽度,高度=0; 如果(myfile.is_open()) { std::getline(myfile,line);//文件类型,skip,它将始终用于此代码p6 std::getline(myfile,line);//图像的宽度和高度 std::stringstream lineStream(line);//提取宽度和高度; 线流>>宽度; lineStream>>高度; //STD::CUT我对C++不太好,但我会尝试解释这里的错误。,c++,ppm,C++,Ppm,如果文件类型为p6,则图像数据以字节格式存储,每个颜色分量(r、g、b)一个字节。从代码中的注释来看,您似乎总是期望p6。这意味着矩阵将像您假设的那样存储为一个连续的数据块。另一个选项是p3,它以ASCII格式存储颜色组件(例如0) 问题是,在 p6< /代码>类型文件中,数据块是二进制的,并且将其视为字符串。考虑到这一点,具有R、G、B=(65, 13, 10)的像素将以二进制形式编码: int main(){ std::fstream myfile; // file object is
p6
,则图像数据以字节格式存储,每个颜色分量(r、g、b)一个字节。从代码中的注释来看,您似乎总是期望p6
。这意味着矩阵将像您假设的那样存储为一个连续的数据块。另一个选项是p3
,它以ASCII格式存储颜色组件(例如0
)
问题是,在<代码> p6< /代码>类型文件中,数据块是二进制的,并且将其视为字符串。考虑到这一点,具有R、G、B=(65, 13, 10)的像素将以二进制形式编码:
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;
}
getline
将读取计算结果为ASCII字符A
的第一个字节,但将停止读取,因为0xD0xA
是\r\n
,一个换行符(在Windows上),它是getline
的分隔符
而不是:
0x41, 0xD, 0xA
我会这样做:
getline (myfile,line); // reach the matrix of numbers
int bufsz=3*宽*高;
char*pixelData=新字符[bufsz];
读取(pixelData,bufsz);
对于(int i=0;i
注意:矩阵块前的一行包含颜色分量的最大值,PPM表示该值可能大于255,因此在这些情况下需要小心,因为char
数组不够好,您至少需要short
数组(2字节)
有用链接:
谢谢,我现在意识到了。数组中的打印元素怎么会给空空格?printf(“%d%c%c%c\n”,I/3,红,绿,蓝);不起作用。cout@DoingItForTheCorgi也不起作用
int bufsz = 3*width*height;
char* pixelData = new char[bufsz];
myfile.read(pixelData,bufsz);
for ( int i = 0; i < bufsz; i+=3 ) {
unsigned char red = pixelData[i];
unsigned char green = pixelData[i+1];
unsigned char blue = pixelData[i+2];
// use unsigned char to express range [0,255],
// this may make compiler to issue warnings
// but it should be safe to do a cast
}
// don't forget to release pixelData when done with it,
// maybe use smart pointers
delete[] pixelData;