4

So far I can read every line and print it out to the console:

void readFile(){

   string line;
   ifstream myfile("example1.pgm");

   if (myfile.is_open()){
       while (myfile.good()){
         getline (myfile,line);
         cout << line;
       }
   }

However a pgm file apparently will always have the following at the start before the data:

P2
# test.pgm
24 7
15

How can i adapt my code so that it checks that "P2" is present, ignores any comments (#), and stores the variables and subsequent pixel data?

I'm a bit lost and new to c++ so any help is appreicated.

Thanks

4

2 回答 2

9

解析文件有很多不同的方法。对于这样的事情,您可以查看此站点上的答案。就个人而言,我会使用 getline() 循环并测试/解析每一行(存储在变量“line”中),您也可以使用 stringstream,因为它更容易与多个值一起使用:

主意

第一行:测试 P2(便携式灰度图)是否存在,可能类似于

if(line.compare("P2")) ...

第二行:什么都不做,你可以继续下一个 getline()

第三行:存储图片的大小;使用 stringstream 你可以做到这一点

int w,h;
ss >> w >> h;

以下行:存储像素数据,直到到达文件末尾

结果代码

您可以尝试此代码并根据您的需要进行调整:

#include <iostream> // cout, cerr
#include <fstream> // ifstream
#include <sstream> // stringstream
using namespace std;

int main() {
  int row = 0, col = 0, numrows = 0, numcols = 0;
  ifstream infile("file.pgm");
  stringstream ss;
  string inputLine = "";

  // First line : version
  getline(infile,inputLine);
  if(inputLine.compare("P2") != 0) cerr << "Version error" << endl;
  else cout << "Version : " << inputLine << endl;

  // Second line : comment
  getline(infile,inputLine);
  cout << "Comment : " << inputLine << endl;

  // Continue with a stringstream
  ss << infile.rdbuf();
  // Third line : size
  ss >> numcols >> numrows;
  cout << numcols << " columns and " << numrows << " rows" << endl;

  int array[numrows][numcols];

  // Following lines : data
  for(row = 0; row < numrows; ++row)
    for (col = 0; col < numcols; ++col) ss >> array[row][col];

  // Now print the array to see the result
  for(row = 0; row < numrows; ++row) {
    for(col = 0; col < numcols; ++col) {
      cout << array[row][col] << " ";
    }
    cout << endl;
  }
  infile.close();
}

编辑

这是一个关于如何使用 stringstreams 的好教程

于 2011-11-14T19:25:53.653 回答
2

一种简化PNM (PBM/PGM/PPM) 标头处理的方法是逐行构建标头字符串,直到您捕获所有必需的数据。仅使用标准 C++ 库来执行此操作不需要太多代码...

#include <string>
#include <iostream>
#include <sstream>
#include <stdexcept>
...
std::string header, magic;
int width=0, height=0, maxsample=0, samples=0, bits=0, bytes=0;

do {
   try { getline(is,magic); } catch ( const std::ios_base::failure & ) {}
   if ( !magic.empty() && magic[0] != '#' ) header += magic+" ";
   if ( !( std::stringstream(header+" 1") >> magic >> width >> height >> maxsample ).eof() ) break;
   if ( ( (magic=="P1"||magic=="P4") && maxsample==1 ) || !is.good() ) break;
   } while ( true );

samples = magic=="P1"?1:magic=="P2"?1:magic=="P3"?3:magic=="P4"?1:magic=="P5"?1:magic=="P6"?3:0;
bits = (magic=="P1"||magic=="P4")?1:maxsample<256?8:maxsample<256*256?16:0, bytes = (width*samples*bits+7)>>3;
if ( width<=0 || height<=0 || maxsample<=0 || samples<=0 || bits<=0 ) throw std::runtime_error("invalid PNM header");

这处理注释(如果存在)和 PBM 的特殊情况(无“最大样本”)——无论输入流上是否启用了异常,它都可以工作。

一旦您阅读了标题,读取图像数据通常是一件简单的事情,因为格式被定义为只是一个顺序数据转储(根据“魔术”值可能是 ASCII 或二进制)。在 16 位二进制编码样本的情况下,格式规范表明“最高有效字节在前”(大端),因此这种情况可能需要一些特定于平台的处理。

正如所写,这需要 C++11——可能是由于我使用stringstream临时的方式。

一个警告:在一种病态的情况下,这可能会在尝试读取无效标头时浪费大量时间/RAM——因为getline调用本身并没有限制。有一个相对简单的解决方案(替换getline为更强大的东西),但它需要更多的代码。

对于生产质量的应用程序,请考虑使用libnetpbm

于 2013-06-14T06:06:14.950 回答