Пример #1
0
int read_ppmheader(FILE *handle,int *width,int *height,int *maxcolor,int *depth) {
    int bytes;
    char buf[2];

    /* trap bad arguments */
    if (!handle || !width || !height || !maxcolor || !depth)
        exit(-1);

    *width = *height = *maxcolor = *depth = 0;

    /* PPM Header Key, 5 or 6 for now (others are possible) */
    bytes = fread(buf,1,2,handle);
    if ( (bytes < 2) || (buf[0] != 'P') || ((buf[1] != '6' ) && (buf[1] != '5')))
        return -1;

    if (buf[1]=='6')
        *depth=3;
    else
        return -1;

    *width=ppm_getint(handle);
    *height=ppm_getint(handle);
    *maxcolor=ppm_getint(handle);

    if(*width < 0 || *height < 0 || *maxcolor != 255)
        return -1;

    return 0;
}
Пример #2
0
/* Read PPM file */
unsigned char* ppm_read(FILE* file,int& cols,int& rows){
  unsigned char* pixels,*pix;
  unsigned int r,g,b;
  int format,maxval;
  int row,col;

  /* Get format */
  format=ppm_readmagicnumber(file);

  /* Get size */
  cols=ppm_getint(file);
  rows=ppm_getint(file);
  maxval=ppm_getint(file);
  if(maxval<=0 || maxval>1023){
    fprintf(stderr,"Illegal maxval value: %d.\n",maxval);
    exit(1);
    }

  /* Create memory */
  pixels=(unsigned char*)malloc(3*rows*cols);
  if(!pixels){
    fprintf(stderr,"Image too big\n");
    exit(1);
    }

  /* Load pixels */
  pix=pixels;
  switch(format){
    case PPM_FORMAT:
      for(row=0; row<rows; row++){
        for(col=0; col<cols; col++){
          r = (255*ppm_getint(file))/maxval;
          g = (255*ppm_getint(file))/maxval;
          b = (255*ppm_getint(file))/maxval;
          *pix++ = r;
          *pix++ = g;
          *pix++ = b;
          }
        }
      break;

    case RPPM_FORMAT:
      for(row=0; row<rows; row++){
        for(col=0; col<cols; col++){
          r = ppm_getrawbyte(file);
          g = ppm_getrawbyte(file);
          b = ppm_getrawbyte(file);
          *pix++ = r;
          *pix++ = g;
          *pix++ = b;
          }
        }
      break;

    default:
      fprintf(stderr,"Unknown format.\n");
      exit(1);
      break;
    }
  return pixels;
  }