微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PPM 文件打不开

如何解决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 举报,一经查实,本站将立刻删除。