Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C语言中正确读取PGM图像_C_P2_Pgm - Fatal编程技术网

如何在C语言中正确读取PGM图像

如何在C语言中正确读取PGM图像,c,p2,pgm,C,P2,Pgm,我是来请你帮忙的。我想编写一个C代码来读取PGM文件(P2,而不是二进制文件),我在Web上找到了很多方法。问题是,每次我试图阅读一些PGM图像,我在我的PC上作为例子,我甚至不能正确地阅读标题,因为它永远无法识别正确的P2 PGM格式。我总是会遇到诸如“无效pgm文件类型”或“不支持格式”之类的错误。以下是我正在尝试的(最后一个)代码: #include <stdio.h> #include <string.h> #include <std

我是来请你帮忙的。我想编写一个C代码来读取PGM文件(P2,而不是二进制文件),我在Web上找到了很多方法。问题是,每次我试图阅读一些PGM图像,我在我的PC上作为例子,我甚至不能正确地阅读标题,因为它永远无法识别正确的P2 PGM格式。我总是会遇到诸如“无效pgm文件类型”或“不支持格式”之类的错误。以下是我正在尝试的(最后一个)代码:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    typedef struct pgm {
      int w;
      int h;
      int max;
      int* pData;
    } pgm;


    int main(int argc, char * argv[]){

      char* filename = argv[0];
      pgm* pPgm;
      FILE* ifp;
      int word;
      int nRead = 0;
      char readChars[256];

      //open the file, check if successful
      ifp = fopen( filename, "r" );
      if (!ifp) {
        printf("Error: Unable to open file %s.\n\n", filename);
        exit(1);
      }

      pPgm = (pgm *) malloc (sizeof(pgm));

      //read headers from file
      printf ("Reading PGM file: %s...\n", filename);
      fscanf (ifp, "%s", readChars);

      if (strcmp(readChars, "P2") == 0) {
        //valid file type
        //get a word from the file
        printf("VALID TYPE.\n");
        fscanf (ifp, "%s", readChars);
        while (readChars[0] == '#') {
          //if a comment, get the rest of the line and a new word
          fgets (readChars, 255, ifp);
          fscanf (ifp, "%s", readChars);
        }

        //ok, comments are gone
        //get width, height, color depth
        sscanf (readChars, "%d", &pPgm->w);
        fscanf (ifp, "%d", &pPgm->h);
        fscanf (ifp, "%d", &pPgm->max);
        printf("WIDTH: %d, HEIGHT: %d\n", pPgm->w, pPgm->h);

        // allocate some memory, note that on the HandyBoard you want to 
        // use constant memory and NOT use calloc/malloc
        pPgm->pData = (int*)malloc(sizeof(int) * pPgm->w * pPgm->h);

        // now read in the image data itself    
        for (nRead = 0; nRead < pPgm->w * pPgm->h; nRead++) {
          fscanf(ifp, "%d" ,&word);
          pPgm->pData[nRead] = word;
          // printf("nRead = %d %d\n",nRead,pPgm->pData[nRead]);
        }

        printf ("Loaded PGM. Size: %dx%d, Greyscale: %d \n", 
        pPgm->w, pPgm->h, pPgm->max + 1);
      }
      else {
        printf ("Error: %s. Format unsupported.\n\n", readChars);
        exit(1);
      }
      fclose(ifp);

      return 0;
    }
#包括
#包括
#包括
类型定义结构pgm{
int w;
int-h;
int max;
int*pData;
}pgm;
int main(int argc,char*argv[]){
char*filename=argv[0];
pgm*pPgm;
文件*ifp;
int字;
int nRead=0;
字符读取字符[256];
//打开文件,检查是否成功
ifp=fopen(文件名,“r”);
如果(!ifp){
printf(“错误:无法打开文件%s。\n\n”,文件名);
出口(1);
}
pPgm=(pgm*)malloc(sizeof(pgm));
//从文件中读取标题
printf(“读取PGM文件:%s...n”,文件名);
fscanf(ifp,“%s”,readChars);
if(strcmp(readChars,“P2”)==0){
//有效文件类型
//从文件中获取一个单词
printf(“有效类型。\n”);
fscanf(ifp,“%s”,readChars);
while(readChars[0]='#'){
//如果是注释,则获取行的其余部分和一个新词
fgets(readChars,255,ifp);
fscanf(ifp,“%s”,readChars);
}
//好了,评论都没有了
//获取宽度、高度、颜色深度
sscanf(读字符、%d、&pPgm->w);
fscanf(ifp、%d、&pPgm->h);
fscanf(ifp、%d、&pPgm->max);
printf(“宽度:%d,高度:%d\n”,pPgm->w,pPgm->h);
//分配一些内存,请注意,在HandyBoard上,您希望
//使用常量内存,不要使用calloc/malloc
pPgm->pData=(int*)malloc(sizeof(int)*pPgm->w*pPgm->h);
//现在读入图像数据本身
对于(nRead=0;nReadw*pPgm->h;nRead++){
fscanf(ifp、%d、&word);
pPgm->pData[nRead]=字;
//printf(“nRead=%d%d\n”,nRead,pPgm->pData[nRead]);
}
printf(“加载的PGM.Size:%dx%d,灰度:%d\n”,
pPgm->w,pPgm->h,pPgm->max+1);
}
否则{
printf(“错误:%s。不支持格式。\n\n”,readChars);
出口(1);
}
fclose(ifp);
返回0;
}

似乎有一些库可以做到这一点:libnetpbm来自或。如果由于某种原因无法使用外部库,查看源代码可以帮助您了解如何读取头。顺便问一下,您已经看过这个问题的答案了吗?

似乎有一些库可以做到这一点:libnetpbm来自或。如果由于某种原因无法使用外部库,查看源代码可以帮助您了解如何读取头。顺便问一下,您是否已经看过这个问题的答案:?

也许您的PGM不正确。它应该是一个简单的ASCII文件,所以请使用所选的代码编辑器打开它并进行验证。注意,他们说。我打开了我所有的pgm图像,似乎都是正确的。也许你的pgm不正确。它应该是一个简单的ASCII文件,所以用所选的代码编辑器打开它并进行验证。注意,他们说。我打开了我所有的pgm图像,似乎都是正确的。是的,我打开了。无论如何,非常感谢你,你建议我使用的图书馆非常有用!是的。无论如何,非常感谢你,你建议我使用的图书馆非常有用!