Example #1
0
  /*! read PFM file from disk */
  Ref<Image> loadPFM(const FileName& fileName)
  {
    /* open PPM file */
    FILE* file = fopen(fileName.c_str(), "rb");
    if (!file) THROW_RUNTIME_ERROR("cannot open " + fileName.str());

    /* read file type */
    char type[8];
    if (fscanf(file, "%7s", type) != 1)
      THROW_RUNTIME_ERROR("Error reading " + fileName.str());

    /* skip comment lines */
    while (readCommentLine(file)) {};

    /* read width, height, and maximal color value */
    int width, height;
    float maxColor;
    if (fscanf(file, "%i %i %f", &width, &height, &maxColor) != 3)
      THROW_RUNTIME_ERROR("Error reading " + fileName.str());

    /* Check for big endian PFM file */
    if (maxColor > 0.0f) {
      fclose(file);
      THROW_RUNTIME_ERROR("Big endian PFM files not supported");
    }
    float rcpMaxColor = -1.0f/float(maxColor);

    /* get return or space */
    fgetc(file);

    /* create image and fill with data */
    Ref<Image> img = new Image3f(width,height,fileName);

    /* image in binary format 32 bit */
    if (!strcmp(type, "PF"))
    {
      float rgb[3];
      for (ssize_t y=0; y<height; y++) {
        for (ssize_t x=0; x<width; x++) {
          if (fread(rgb,sizeof(rgb),1,file) != 1)
            THROW_RUNTIME_ERROR("Error reading " + fileName.str());
          img->set(x,y,Color4(float(rgb[0])*rcpMaxColor,float(rgb[1])*rcpMaxColor,float(rgb[2])*rcpMaxColor,1.0f));
        }
      }
    }

    /* invalid magic value */
    else {
      fclose(file);
      THROW_RUNTIME_ERROR("Invalid magic value in PFM file");
    }

    fclose(file);
    return img;
  }
Example #2
0
/*! read PPM file from disk */
Ref<Image> loadPPM(const FileName& fileName)
{
    /* open PPM file */
    FILE* file = fopen(fileName.c_str(), "rb");
    if (!file) THROW_RUNTIME_ERROR("cannot open " + fileName.str());

    /* read file type */
    char type[8];
    if (fscanf(file, "%7s", type) != 1)
        THROW_RUNTIME_ERROR("Error reading " + fileName.str());

    /* skip comment lines */
    while (readCommentLine(file)) {};

    /* read width, height, and maximal color value */
    int width, height, maxColor;
    if (fscanf(file, "%i %i %i", &width, &height, &maxColor) != 3)
        THROW_RUNTIME_ERROR("Error reading " + fileName.str());
    float rcpMaxColor = 1.0f/float(maxColor);

    /* get return or space */
    fgetc(file);

    /* create image and fill with data */
    Ref<Image> img = new Image4c(width,height,fileName);

    /* image in text format */
    if (!strcmp(type, "P3"))
    {
        int r, g, b;
        for (ssize_t y=0; y<height; y++) {
            for (ssize_t x=0; x<width; x++) {
                if (fscanf(file, "%i %i %i", &r, &g, &b) != 3)
                    THROW_RUNTIME_ERROR("Error reading " + fileName.str());
                img->set(x,y,Color4(float(r)*rcpMaxColor,float(g)*rcpMaxColor,float(b)*rcpMaxColor,1.0f));
            }
        }
    }

    /* image in binary format 8 bit */
    else if (!strcmp(type, "P6") && maxColor <= 255)
    {
        unsigned char rgb[3];
        for (ssize_t y=0; y<height; y++) {
            for (ssize_t x=0; x<width; x++) {
                if (fread(rgb,sizeof(rgb),1,file) != 1)
                    THROW_RUNTIME_ERROR("Error reading " + fileName.str());
                img->set(x,y,Color4(float(rgb[0])*rcpMaxColor,float(rgb[1])*rcpMaxColor,float(rgb[2])*rcpMaxColor,1.0f));
            }
        }
    }

    /* image in binary format 16 bit */
    else if (!strcmp(type, "P6"))
    {
        unsigned short rgb[3];
        for (ssize_t y=0; y<height; y++) {
            for (ssize_t x=0; x<width; x++) {
                if (fread(rgb,sizeof(rgb),1,file) != 1)
                    THROW_RUNTIME_ERROR("Error reading " + fileName.str());
                img->set(x,y,Color4(float(rgb[0])*rcpMaxColor,float(rgb[1])*rcpMaxColor,float(rgb[2])*rcpMaxColor,1.0f));
            }
        }
    }

    /* invalid magic value */
    else {
        fclose(file);
        THROW_RUNTIME_ERROR("Invalid magic value in PPM file");
    }

    fclose(file);
    return img;
}