【问题标题】:Issues reading ppm p6 image to completion. c++完成读取 ppm p6 图像的问题。 C++
【发布时间】:2016-07-11 00:09:50
【问题描述】:
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 格式本身的误解。我认为除了带有类型、宽度和大小以及幻数的文件的标题格式之外,只有一行,没有更多的 '\n' 字符。因此,矩阵可以被读取为一个连续的字符数组。

那么为什么这个程序会停在这么奇怪的地方呢?

【问题讨论】:

    标签: c++ ppm


    【解决方案1】:

    我不擅长 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 是 \r\n,一个换行符(在 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 个字节)

    有用的链接:

    1. Netpbm format
    2. basic_istream::read

    【讨论】:

    • 谢谢我现在意识到了。
    • 如何打印数组中的元素会给出空格? printf(" %d %c %c %c \n", i/3 ,red,green,blue );不管用。 cout 也不是
    • @DoingItForTheCorgi 试试cout &lt;&lt; static_cast&lt;int&gt;(pixelData[i])
    猜你喜欢
    • 2016-02-09
    • 2010-11-22
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    相关资源
    最近更新 更多