PPM 文件打不开

问题描述

为了打开 PPM 文件(图像),我调整了一些下载的代码,虽然程序可以运行,但当我尝试运行它时,出现错误 cannot read image data from file。 问题与下一个代码有关。图片尺寸为320*240。我能做什么?

ImageRead(const char *filename) //Image *ImageRead(char *filename);
{
  int width,height,num,size;
  u_char *p;

  Image *image = (Image *) malloc(sizeof(Image));
  FILE  *fp    = fopen(filename,"r");

  if (!image) die("cannot allocate memory for new image");
  if (!fp)    die("cannot open file for reading");

  readPPMHeader(fp,&width,&height);

  size          = width * height * 3;
  image->data   = (u_char *) malloc(size);
  image->width  = width;
  image->height = height;

  if (!image->data) die("cannot allocate memory for new image");

  num = fread((void *) image->data,1,(size_t) size,fp);

  if (num != size) die("cannot read image data from file");

  fclose(fp);

  return image;
}

编辑:读取标题函数是下一个

static void
readPPMHeader(FILE *fp,int *width,int *height)
{
  char ch;
  int  maxval;

  if (fscanf(fp,"P%c\n",&ch) != 1 || ch != '6')
    die("file is not in ppm raw format; cannot read");

  /* skip comments */
  ch = getc(fp);
  while (ch == '#')
    {
      do {
        ch = getc(fp);
      } while (ch != '\n'); /* read to the end of the line*/
      ch = getc(fp);            /* thanks,Elliot*/
    }

  if (!isdigit(ch)) die("cannot read header information from ppm file");

  ungetc(ch,fp);       /* put that digit back*/

  /* read the width,and maximum value for a pixel */
  fscanf(fp,"%d%d%d\n",width,&maxval);

  if (maxval != 255) die("image is not true-color (24 bit); read Failed");

  checkDimension(*width);
  checkDimension(*height);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)