简体   繁体   English

用C读取PGM图像(未正确读取文件)

[英]Reading PGM Image in C (Not Reading File Correctly)

I'm using files, pgm.c and pgm.h to read in a PGM image file, and then my program will manipulate the file (grayscale, rotation, etc). 我正在使用文件, pgm.cpgm.h读取PGM图像文件,然后我的程序将操作文件(灰度,旋转等)。

However, I can't get the file to read properly. 但是,我无法正确读取文件。 When I use: 我用的时候:

   PGMImage* img = (PGMImage*)malloc(sizeof(PGMImage));
   char iName1[256];
   printf("Enter the filename of your PGM image:\n");
   scanf("%s", iName1);
   getPGMfile(iName1, &img);

The console returns the type, width, height and maximum value of the image. 控制台返回图像的类型,宽度,高度和最大值。 However, the width is never correct. 但是,宽度永远不会正确。 It's usually 0 (and one time it was 56, way off) whether I'm using a .pgm file I found online or one I made myself. 它通常是0(并且一次是56,离开)我是否使用我在网上找到的.pgm文件或我自己制作的文件。 I've even tried hard-coding the width of the image, but even so, it causes the program the crash. 我甚至尝试过硬编码图像的宽度,但即便如此,它也会导致程序崩溃。 The width should be read in pgm.c, under the getPGMfile function: 应在pgm.c中的getPGMfile函数下读取宽度:

void getPGMfile (char filename[], PGMImage *img)
 {
   FILE *in_file;
   char ch;
   int row, col, type;
   int ch_int;

   in_file = fopen(filename, "r");
   if (in_file == NULL)
   {
     fprintf(stderr, "Error: Unable to open file %s\n\n", filename);
     exit(8);
   }

   printf("\nReading image file: %s\n", filename);

   /*determine pgm image type (only type three can be used)*/
   ch = getc(in_file);
   if(ch != 'P')
   {
      printf("ERROR(1): Not valid pgm/ppm file type\n");
      exit(1);
   }
   ch = getc(in_file);
   /*convert the one digit integer currently represented as a character to
     an integer(48 == '0')*/
   type = ch - 48;
   printf("Type: %d", type);
   if((type != 2) && (type != 3) && (type != 5) && (type != 6))
   {
      printf("ERROR(2): Not valid pgm/ppm file type\n");
      exit(1);
   }

   while(getc(in_file) != '\n');             /* skip to end of line*/

   while (getc(in_file) == '#')              /* skip comment lines */
   {
     while (getc(in_file) != '\n');          /* skip to end of comment line */
   }

   /*there seems to be a difference between color and b/w.  This line is needed
     by b/w but doesn't effect color reading...*/
   fseek(in_file, -1, SEEK_CUR);             /* backup one character*/

   fscanf(in_file,"%d", &((*img).width));
   fscanf(in_file,"%d", &((*img).height));
   fscanf(in_file,"%d", &((*img).maxVal));

   //I omitted the rest, but it can be seen in the link above

I have no idea what's causing these errors. 我不知道是什么导致了这些错误。 I'm using Eclipse and MinGW GCC on Windows 7 (64-bit). 我在Windows 7(64位)上使用Eclipse和MinGW GCC。 My instructor couldn't help me solve this, so I'm hoping you can! 我的导师无法帮我解决这个问题,所以我希望你能解决! :) :)

To summarize: The width of the PGM image is always read incorrectly, anything done with the file afterwards causes the executable to stop working. 总结:PGM图像的宽度始终读取不正确,之后使用该文件执行的任何操作都会导致可执行文件停止工作。 Hard-coding the width also causes the the executable to stop working. 硬编码宽度也会导致可执行文件停止工作。

getpgm不应该传递指针的地址,而是根据头文件传递指针:getPGMfile(iName1,img)

The PGM format goes as PGM格式如下

P5
wid ht
xxx xxx xxx xxx

where xxx are the raster values. 其中xxx是栅格值。 You need to get rid of each getc function before reading the width, or perform an ungetc when it fails to find the character you are looking for. 你需要在读取宽度之前去掉每个getc函数,或者在找不到你正在寻找的角色时执行ungetc Then, the width will be read appropriately. 然后,将适当地读取宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM