读取 ppm p6 图像完成时出现问题.C++
Issues reading ppm p6 image to completion. c++
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 格式本身的误解。我认为除了标题格式(即具有类型,宽度和大小以及幻数的文件(之外,只有一行,不再有""字符。因此,矩阵可以读作一个连续的字符数组。
那么,为什么这个程序会在这么奇怪的地方停止呢?
我不擅长C++但我会尝试解释这里的问题。
如果文件类型p6
则图像数据以字节格式存储,每个颜色分量(r,g,b(一个字节。从代码中的注释来看,您似乎总是期望p6
.这意味着矩阵将像您假设的那样存储为一个连续的数据块。另一个选项是p3
,它以 ASCII 格式存储颜色分量(示例 0 0 0
(。
问题是p6
类型文件中的数据块是二进制的,而您将其视为字符串。考虑一下,一个像素的 r,g,b = (65, 13, 10( 将以二进制编码,如下所示:
0x41, 0xD, 0xA
getline
将读取第一个字节,其计算结果为 ASCII 字符A
但之后将停止读取,因为0xD0xA
是 rn
,换行符(在 Windows 上(是getline
的分隔符。
而不是:
getline (myfile,line); // reach the matrix of numbers
我会做这样的事情:
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;
注意:矩阵块之前的行包含颜色分量的最大值,PPM 声明这可能大于 255,因此在这些情况下您需要小心,因为char
数组不够好,您将需要至少short
数组(2 字节(
有用的链接:
- 网络 pbm 格式
- basic_istream::阅读
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题