コード例 #1
0
ファイル: rgbe.cpp プロジェクト: rttag/oiio
/* default minimal header. modify if you want more information in header */
int RGBE_WriteHeader(FILE *fp, int width, int height, rgbe_header_info *info,
                     char *errbuf)
{
    const char *programtype = "RADIANCE";
    /* N.B. from Larry Gritz:
     * Plenty of readers will refuse to read .rgbe/.hdr files if their
     * program type is not "RADIANCE".  So I changed the default
     * programtype from Bruce Walter's original "RGBE", which many readers
     * refuse to accept.  (Mac OS X's "preview" utility is one such reader!)
     */

    if (info && (info->valid & RGBE_VALID_PROGRAMTYPE))
        programtype = info->programtype;
    if (fprintf(fp,"#?%s\n",programtype) < 0)
        return rgbe_error(rgbe_write_error,NULL, errbuf);
    /* The #? is to identify file type, the programtype is optional. */
    if (info && (info->valid & RGBE_VALID_GAMMA)) {
        if (fprintf(fp,"GAMMA=%g\n",info->gamma) < 0)
            return rgbe_error(rgbe_write_error,NULL, errbuf);
    }
    if (info && (info->valid & RGBE_VALID_EXPOSURE)) {
        if (fprintf(fp,"EXPOSURE=%g\n",info->exposure) < 0)
            return rgbe_error(rgbe_write_error,NULL, errbuf);
    }
    if (fprintf(fp,"FORMAT=32-bit_rle_rgbe\n\n") < 0)
        return rgbe_error(rgbe_write_error,NULL, errbuf);
    if (fprintf(fp, "-Y %d +X %d\n", height, width) < 0)
        return rgbe_error(rgbe_write_error,NULL, errbuf);
    return RGBE_RETURN_SUCCESS;
}
コード例 #2
0
ファイル: rgbe.cpp プロジェクト: FatGarfieldjteng/cubemapgen
/* default minimal header. modify if you want more information in header */
int RGBE_WriteHeader(FILE *fp, int width, int height, rgbe_header_info *info)
{
  //char *programtype = "RGBE";
  char *programtype = "RADIANCE";

  //if (info && (info->valid & RGBE_VALID_PROGRAMTYPE))
  //  programtype = info->programtype;
  if (fprintf(fp,"#?%s%c", programtype, 10) < 0)
    return rgbe_error(rgbe_write_error,NULL);
  /* The #? is to identify file type, the programtype is optional. */

  if (fprintf(fp,"# Created with CubeMapGen%c", 10) < 0)
    return rgbe_error(rgbe_write_error,NULL);


  if (info && (info->valid & RGBE_VALID_GAMMA)) {
    if (fprintf(fp,"GAMMA=%g%c",info->gamma, 10) < 0)
      return rgbe_error(rgbe_write_error,NULL);
  }
  /*
  if (info && (info->valid & RGBE_VALID_EXPOSURE)) {
    if (fprintf(fp,"EXPOSURE=%g%c",info->exposure, 10) < 0)
      return rgbe_error(rgbe_write_error,NULL);
  }*/
//  if (fprintf(fp,"FORMAT=32-bit_rle_rgbe\n\n") < 0)
//    return rgbe_error(rgbe_write_error,NULL);
  if (fprintf(fp,"FORMAT=32-bit_rle_rgbe%c%c", 10, 10) < 0)
    return rgbe_error(rgbe_write_error,NULL);
  if (fprintf(fp, "-Y %d +X %d%c", height, width, 10) < 0)
    return rgbe_error(rgbe_write_error,NULL);
  return RGBE_RETURN_SUCCESS;
}
コード例 #3
0
ファイル: rgbe.cpp プロジェクト: rttag/oiio
static int RGBE_WriteBytes_RLE(FILE *fp, unsigned char *data, int numbytes,
                               char *errbuf)
{
#define MINRUNLENGTH 4
    int cur, beg_run, run_count, old_run_count, nonrun_count;
    unsigned char buf[2];

    cur = 0;
    while(cur < numbytes) {
        beg_run = cur;
        /* find next run of length at least 4 if one exists */
        run_count = old_run_count = 0;
        while((run_count < MINRUNLENGTH) && (beg_run < numbytes)) {
            beg_run += run_count;
            old_run_count = run_count;
            run_count = 1;
            while((data[beg_run] == data[beg_run + run_count])
                    && (beg_run + run_count < numbytes) && (run_count < 127))
                run_count++;
        }
        /* if data before next big run is a short run then write it as such */
        if ((old_run_count > 1)&&(old_run_count == beg_run - cur)) {
            buf[0] = 128 + old_run_count;   /*write short run*/
            buf[1] = data[cur];
            if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1)
                return rgbe_error(rgbe_write_error,NULL, errbuf);
            cur = beg_run;
        }
        /* write out bytes until we reach the start of the next run */
        while(cur < beg_run) {
            nonrun_count = beg_run - cur;
            if (nonrun_count > 128)
                nonrun_count = 128;
            buf[0] = nonrun_count;
            if (fwrite(buf,sizeof(buf[0]),1,fp) < 1)
                return rgbe_error(rgbe_write_error,NULL, errbuf);
            if (fwrite(&data[cur],sizeof(data[0])*nonrun_count,1,fp) < 1)
                return rgbe_error(rgbe_write_error,NULL, errbuf);
            cur += nonrun_count;
        }
        /* write out next run if one was found */
        if (run_count >= MINRUNLENGTH) {
            buf[0] = 128 + run_count;
            buf[1] = data[beg_run];
            if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1)
                return rgbe_error(rgbe_write_error,NULL, errbuf);
            cur += run_count;
        }
    }
    return RGBE_RETURN_SUCCESS;
#undef MINRUNLENGTH
}
コード例 #4
0
ファイル: rgbe.c プロジェクト: issakomi/mmt
int RGBE_ReadPixels_Raw(FILE *fp, unsigned char *data, int numpixels)
{
  if (fread(data, 4, numpixels, fp) < (size_t)numpixels)
    return rgbe_error(rgbe_read_error,NULL);

  return RGBE_RETURN_SUCCESS;
}
コード例 #5
0
ファイル: rgbe.c プロジェクト: TaiManProject/Dirks_Code_V3
int VIGRA_RGBE_ReadPixels_Raw(FILE *fp, unsigned char *data, unsigned int numpixels)
{
  if (fread(data, 4, numpixels, fp) < numpixels)
    return rgbe_error(rgbe_read_error,NULL);

  return VIGRA_RGBE_RETURN_SUCCESS;
}
コード例 #6
0
ファイル: rgbe.c プロジェクト: thegedge/StereoReconstruction
/* minimal header reading.  modify if you want to parse more information */
int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info)
{
    char buf[128];
    int found_format;
    float tempf;
    unsigned int i;

    found_format = 0;
    if (info) {
        info->valid = 0;
        info->programtype[0] = 0;
        info->gamma = info->exposure = 1.0;
    }
    if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == NULL)
        return rgbe_error(rgbe_read_error,NULL);
    if ((buf[0] != '#')||(buf[1] != '?')) {
        /* if you want to require the magic token then uncomment the next line */
        /*return rgbe_error(rgbe_format_error,"bad initial token"); */
    }
    else if (info) {
        info->valid |= RGBE_VALID_PROGRAMTYPE;
        for(i=0; i<sizeof(info->programtype)-1; i++) {
            if ((buf[i+2] == 0) || isspace(buf[i+2]))
                break;
            info->programtype[i] = buf[i+2];
        }
        info->programtype[i] = 0;
        if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
            return rgbe_error(rgbe_read_error,NULL);
    }
    for(;;) {
        if ((buf[0] == 0)||(buf[0] == '\n'))
            return rgbe_error(rgbe_format_error,"no FORMAT specifier found");
        else if (strcmp(buf,"FORMAT=32-bit_rle_rgbe\n") == 0)
            break;       /* format found so break out of loop */
        else if (info && (sscanf(buf,"GAMMA=%g",&tempf) == 1)) {
            info->gamma = tempf;
            info->valid |= RGBE_VALID_GAMMA;
        }
        else if (info && (sscanf(buf,"EXPOSURE=%g",&tempf) == 1)) {
            info->exposure = tempf;
            info->valid |= RGBE_VALID_EXPOSURE;
        }
        if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
            return rgbe_error(rgbe_read_error,NULL);
    }
    if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
        return rgbe_error(rgbe_read_error,NULL);
    if (strcmp(buf,"\n") != 0)
        return rgbe_error(rgbe_format_error,
                          "missing blank line after FORMAT specifier");
    if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
        return rgbe_error(rgbe_read_error,NULL);
    if (sscanf(buf,"-Y %d +X %d",height,width) < 2)
        return rgbe_error(rgbe_format_error,"missing image size specifier");
    return RGBE_RETURN_SUCCESS;
}
コード例 #7
0
ファイル: file_rgbe.c プロジェクト: jtsiomb/libimago
/* simple read routine.  will not correctly handle run length encoding */
static int rgbe_read_pixels(struct img_io *io, float *data, int numpixels)
{
	unsigned char rgbe[4];

	while(numpixels-- > 0) {
		if(io->read(rgbe, sizeof(rgbe), io->uptr) < 1)
			return rgbe_error(rgbe_read_error, NULL);
		rgbe2float(&data[RGBE_DATA_RED], &data[RGBE_DATA_GREEN], &data[RGBE_DATA_BLUE], rgbe);
		data += RGBE_DATA_SIZE;
	}
	return RGBE_RETURN_SUCCESS;
}
コード例 #8
0
ファイル: file_rgbe.c プロジェクト: jtsiomb/libimago
/* These routines can be made faster by allocating a larger buffer and
   fread-ing and fwrite-ing the data in larger chunks */
static int rgbe_write_pixels(struct img_io *io, float *data, int numpixels)
{
	unsigned char rgbe[4];

	while(numpixels-- > 0) {
		float2rgbe(rgbe, data[RGBE_DATA_RED], data[RGBE_DATA_GREEN], data[RGBE_DATA_BLUE]);
		data += RGBE_DATA_SIZE;
		if(io->write(rgbe, sizeof(rgbe), io->uptr) < 1)
			return rgbe_error(rgbe_write_error, NULL);
	}
	return RGBE_RETURN_SUCCESS;
}
コード例 #9
0
ファイル: rgbe.c プロジェクト: thegedge/StereoReconstruction
/* simple read routine.  will not correctly handle run length encoding */
int RGBE_ReadPixels(FILE *fp, float *data, int numpixels)
{
    unsigned char rgbe[4];

    while(numpixels-- > 0) {
        if (fread(rgbe, sizeof(rgbe), 1, fp) < 1)
            return rgbe_error(rgbe_read_error,NULL);
        rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN],
                   &data[RGBE_DATA_BLUE],rgbe);
        data += RGBE_DATA_SIZE;
    }
    return RGBE_RETURN_SUCCESS;
}
コード例 #10
0
ファイル: rgbe.c プロジェクト: thegedge/StereoReconstruction
/* These routines can be made faster by allocating a larger buffer and
   fread-ing and fwrite-ing the data in larger chunks */
int RGBE_WritePixels(FILE *fp, float *data, int numpixels)
{
    unsigned char rgbe[4];

    while (numpixels-- > 0) {
        float2rgbe(rgbe,data[RGBE_DATA_RED],
                   data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]);
        data += RGBE_DATA_SIZE;
        if (fwrite(rgbe, sizeof(rgbe), 1, fp) < 1)
            return rgbe_error(rgbe_write_error,NULL);
    }
    return RGBE_RETURN_SUCCESS;
}
コード例 #11
0
ファイル: rgbe.cpp プロジェクト: davll/DIPProject_HDR
/* default minimal header. modify if you want more information in header */
int RGBE_WriteHeader(FILE *fp, int width, int height, rgbe_header_info *info)
{
  const unsigned HDR_MAXLINE = 256;
  char buf[HDR_MAXLINE];
  int bufc;
  
  //const char *programtype = "RGBE";
  const char *programtype = "RADIANCE";
  
  if (info && (info->valid & RGBE_VALID_PROGRAMTYPE))
    programtype = info->programtype;
  
  bufc = snprintf(buf, HDR_MAXLINE, "#?%s\n", programtype);
  if(fwrite(buf, 1, bufc, fp) != bufc)
    return rgbe_error(rgbe_write_error,NULL);
  
  if (info && (info->valid & RGBE_VALID_GAMMA)) {
    bufc = snprintf(buf, HDR_MAXLINE, "GAMMA=%g\n", info->gamma);
    if (fwrite(buf, 1, bufc, fp) != bufc)
      return rgbe_error(rgbe_write_error,NULL);
  }
  
  if (info && (info->valid & RGBE_VALID_EXPOSURE)) {
    bufc = snprintf(buf, HDR_MAXLINE, "EXPOSURE=%g\n",info->exposure);
    if (fwrite(buf, 1, bufc, fp) != bufc)
      return rgbe_error(rgbe_write_error,NULL);
  }
  
  bufc = snprintf(buf, HDR_MAXLINE, "FORMAT=32-bit_rle_rgbe\n\n");
  if (fwrite(buf, 1, bufc, fp) != bufc)
    return rgbe_error(rgbe_write_error,NULL);
  
  bufc = snprintf(buf, HDR_MAXLINE, "-Y %d +X %d\n", height, width);
  if (fwrite(buf, 1, bufc, fp) != bufc)
    return rgbe_error(rgbe_write_error,NULL);
  
  return RGBE_RETURN_SUCCESS;
}
コード例 #12
0
ファイル: rgbe.c プロジェクト: thegedge/StereoReconstruction
/* default minimal header. modify if you want more information in header */
int RGBE_WriteHeader(FILE *fp, int width, int height, rgbe_header_info *info)
{
    char *programtype = "RADIANCE";

    if (info && (info->valid & RGBE_VALID_PROGRAMTYPE))
        programtype = info->programtype;
    if (fprintf(fp,"#?%s\n",programtype) < 0)
        return rgbe_error(rgbe_write_error,NULL);
    /* The #? is to identify file type, the programtype is optional. */
    if (info && (info->valid & RGBE_VALID_GAMMA)) {
        if (fprintf(fp,"GAMMA=%g\n",info->gamma) < 0)
            return rgbe_error(rgbe_write_error,NULL);
    }
    if (info && (info->valid & RGBE_VALID_EXPOSURE)) {
        if (fprintf(fp,"EXPOSURE=%g\n",info->exposure) < 0)
            return rgbe_error(rgbe_write_error,NULL);
    }
    if (fprintf(fp,"FORMAT=32-bit_rle_rgbe\n\n") < 0)
        return rgbe_error(rgbe_write_error,NULL);
    if (fprintf(fp, "-Y %d +X %d\n", height, width) < 0)
        return rgbe_error(rgbe_write_error,NULL);
    return RGBE_RETURN_SUCCESS;
}
コード例 #13
0
ファイル: rgbe.c プロジェクト: thegedge/StereoReconstruction
int RGBE_WritePixels_RLE(FILE *fp, float *data, int scanline_width,
                         int num_scanlines)
{
    unsigned char rgbe[4];
    unsigned char *buffer;
    int i, err;

    if ((scanline_width < 8)||(scanline_width > 0x7fff))
        /* run length encoding is not allowed so write flat*/
        return RGBE_WritePixels(fp,data,scanline_width*num_scanlines);
    buffer = (unsigned char *)malloc(sizeof(unsigned char)*4*scanline_width);
    if (buffer == NULL)
        /* no buffer space so write flat */
        return RGBE_WritePixels(fp,data,scanline_width*num_scanlines);
    while(num_scanlines-- > 0) {
        rgbe[0] = 2;
        rgbe[1] = 2;
        rgbe[2] = scanline_width >> 8;
        rgbe[3] = scanline_width & 0xFF;
        if (fwrite(rgbe, sizeof(rgbe), 1, fp) < 1) {
            free(buffer);
            return rgbe_error(rgbe_write_error,NULL);
        }
        for(i=0; i<scanline_width; i++) {
            float2rgbe(rgbe,data[RGBE_DATA_RED],
                       data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]);
            buffer[i] = rgbe[0];
            buffer[i+scanline_width] = rgbe[1];
            buffer[i+2*scanline_width] = rgbe[2];
            buffer[i+3*scanline_width] = rgbe[3];
            data += RGBE_DATA_SIZE;
        }
        /* write out each of the four channels separately run length encoded */
        /* first red, then green, then blue, then exponent */
        for(i=0; i<4; i++) {
            if ((err = RGBE_WriteBytes_RLE(fp,&buffer[i*scanline_width],
                                           scanline_width)) != RGBE_RETURN_SUCCESS) {
                free(buffer);
                return err;
            }
        }
    }
    free(buffer);
    return RGBE_RETURN_SUCCESS;
}
コード例 #14
0
ファイル: file_rgbe.c プロジェクト: jtsiomb/libimago
/* default minimal header. modify if you want more information in header */
static int rgbe_write_header(struct img_io *io, int width, int height, rgbe_header_info * info)
{
	char *buf;
	int ptypelen = 4;
	const char *programtype = "RGBE";

	if(info && (info->valid & RGBE_VALID_PROGRAMTYPE)) {
		programtype = info->programtype;
		ptypelen = strlen(programtype);
	}
	buf = malloc(ptypelen > 120 ? ptypelen + 8 : 128);
	sprintf(buf, "#?%s\n", programtype);
	if(io->write(buf, strlen(buf), io->uptr) < 0)
		goto err;
	/* The #? is to identify file type, the programtype is optional. */
	if(info && (info->valid & RGBE_VALID_GAMMA)) {
		sprintf(buf, "GAMMA=%g\n", info->gamma);
		if(io->write(buf, strlen(buf), io->uptr) < 0)
			goto err;
	}
	if(info && (info->valid & RGBE_VALID_EXPOSURE)) {
		sprintf(buf, "EXPOSURE=%g\n", info->exposure);
		if(io->write(buf, strlen(buf), io->uptr) < 0)
			goto err;
	}
	strcpy(buf, "FORMAT=32-bit_rle_rgbe\n\n");
	if(io->write(buf, strlen(buf), io->uptr) < 0)
		goto err;
	sprintf(buf, "-Y %d +X %d\n", height, width);
	if(io->write(buf, strlen(buf), io->uptr) < 0)
		goto err;

	free(buf);
	return RGBE_RETURN_SUCCESS;
err:
	free(buf);
	return rgbe_error(rgbe_write_error, NULL);
}
コード例 #15
0
ファイル: rgbe.cpp プロジェクト: ennioquaglia/GameEngine
//---------------------------------------------------------------------------
int RGBE_ReadPixels_RLE_Components(FILE *fp, unsigned char *data, int scanline_width,
      int num_scanlines)
{
  unsigned char rgbe[4];
  unsigned char *scanline_buffer = NULL;
  unsigned char *ptr;
  unsigned char *ptr_end;

  int i, count;
  unsigned char buf[2];

  if ((scanline_width < 8)||(scanline_width > 0x7fff))
    // run length encoding is not allowed so read flat, but we can't since we want components
    return RGBE_RETURN_FAILURE;
  
  // read in each successive scanline
  while(num_scanlines > 0)
  {
    if (fread(rgbe,sizeof(rgbe),1,fp) < 1)
    {
      free(scanline_buffer);
      return rgbe_error(rgbe_read_error,NULL);
    }
    
    if ((rgbe[0] != 2)||(rgbe[1] != 2)||(rgbe[2] & 0x80))
    {
      
      // this file is not run length encoded
      //rgbe2float(&data[0],&data[1],&data[2],rgbe);
      //data += RGBE_DATA_SIZE;
      free(scanline_buffer);
      //return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines-1);
      return RGBE_RETURN_FAILURE;
    }
    if ((((int)rgbe[2])<<8 | rgbe[3]) != scanline_width)
    {
      free(scanline_buffer);
      return rgbe_error(rgbe_format_error,"wrong scanline width");
    }
    if (scanline_buffer == NULL)
    {
      int memsize = sizeof(unsigned char)*4*scanline_width;
      scanline_buffer = (unsigned char *)malloc(memsize);
      memset( scanline_buffer, 0, memsize );
    }
    if (scanline_buffer == NULL)
    {
      return rgbe_error(rgbe_memory_error,"unable to allocate buffer space");
    }

    ptr = &scanline_buffer[0];
    
    // read each of the four channels for the scanline into the buffer
    for( i = 0; i < 4; i++ )
    {
      ptr_end = &scanline_buffer[(i+1)*scanline_width];
      while(ptr < ptr_end)
      {
        if (fread(buf,sizeof(buf[0])*2,1,fp) < 1)
        {
          free(scanline_buffer);
          return rgbe_error(rgbe_read_error,NULL);
        }
        if (buf[0] > 128)
        {
          // a run of the same value
          count = buf[0]-128;
          if ((count == 0)||(count > ptr_end - ptr))
          {
            free(scanline_buffer);
            return rgbe_error(rgbe_format_error,"bad scanline data");
          }
          while(count-- > 0)
          {
            *ptr++ = buf[1];
          }
        }
        else
        {
          // a non-run
          count = buf[0];
          if ((count == 0)||(count > ptr_end - ptr))
          {
            free(scanline_buffer);
            return rgbe_error(rgbe_format_error,"bad scanline data");
          }
          
          *ptr++ = buf[1];
          
          if (--count > 0)
          {
            if (fread(ptr,sizeof(*ptr)*count,1,fp) < 1)
            {
              free(scanline_buffer);
              return rgbe_error(rgbe_read_error,NULL);
            }
            ptr += count;
          }
        }
      }
    }

    // stores data as rgbe
    for(i=0;i<scanline_width;i++)
    {
      rgbe[0] = scanline_buffer[i];
      rgbe[1] = scanline_buffer[i+scanline_width];
      rgbe[2] = scanline_buffer[i+2*scanline_width];
      rgbe[3] = scanline_buffer[i+3*scanline_width];

      data[0] = rgbe[0];
      data[1] = rgbe[1];
      data[2] = rgbe[2];
      data[3] = rgbe[3];

      data += RGBE_COMPONENT_DATA_SIZE; // rgbe is 4 bytes
    }

    num_scanlines--;
  }
  free(scanline_buffer);
  return RGBE_RETURN_SUCCESS;
}
コード例 #16
0
ファイル: rgbe.c プロジェクト: thegedge/StereoReconstruction
int RGBE_ReadPixels_RLE(FILE *fp, float *data, int scanline_width,
                        int num_scanlines)
{
    unsigned char rgbe[4], *scanline_buffer, *ptr, *ptr_end;
    int i, count;
    unsigned char buf[2];

    if ((scanline_width < 8)||(scanline_width > 0x7fff))
        /* run length encoding is not allowed so read flat*/
        return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines);
    scanline_buffer = NULL;
    /* read in each successive scanline */
    while(num_scanlines > 0) {
        if (fread(rgbe,sizeof(rgbe),1,fp) < 1) {
            free(scanline_buffer);
            return rgbe_error(rgbe_read_error,NULL);
        }
        if ((rgbe[0] != 2)||(rgbe[1] != 2)||(rgbe[2] & 0x80)) {
            /* this file is not run length encoded */
            rgbe2float(&data[0],&data[1],&data[2],rgbe);
            data += RGBE_DATA_SIZE;
            free(scanline_buffer);
            return RGBE_ReadPixels(fp,data,scanline_width*num_scanlines-1);
        }
        if ((((int)rgbe[2])<<8 | rgbe[3]) != scanline_width) {
            free(scanline_buffer);
            return rgbe_error(rgbe_format_error,"wrong scanline width");
        }
        if (scanline_buffer == NULL)
            scanline_buffer = (unsigned char *)
                              malloc(sizeof(unsigned char)*4*scanline_width);
        if (scanline_buffer == NULL)
            return rgbe_error(rgbe_memory_error,"unable to allocate buffer space");

        ptr = &scanline_buffer[0];
        /* read each of the four channels for the scanline into the buffer */
        for(i=0; i<4; i++) {
            ptr_end = &scanline_buffer[(i+1)*scanline_width];
            while(ptr < ptr_end) {
                if (fread(buf,sizeof(buf[0])*2,1,fp) < 1) {
                    free(scanline_buffer);
                    return rgbe_error(rgbe_read_error,NULL);
                }
                if (buf[0] > 128) {
                    /* a run of the same value */
                    count = buf[0]-128;
                    if ((count == 0)||(count > ptr_end - ptr)) {
                        free(scanline_buffer);
                        return rgbe_error(rgbe_format_error,"bad scanline data");
                    }
                    while(count-- > 0)
                        *ptr++ = buf[1];
                }
                else {
                    /* a non-run */
                    count = buf[0];
                    if ((count == 0)||(count > ptr_end - ptr)) {
                        free(scanline_buffer);
                        return rgbe_error(rgbe_format_error,"bad scanline data");
                    }
                    *ptr++ = buf[1];
                    if (--count > 0) {
                        if (fread(ptr,sizeof(*ptr)*count,1,fp) < 1) {
                            free(scanline_buffer);
                            return rgbe_error(rgbe_read_error,NULL);
                        }
                        ptr += count;
                    }
                }
            }
        }
        /* now convert data from buffer into floats */
        for(i=0; i<scanline_width; i++) {
            rgbe[0] = scanline_buffer[i];
            rgbe[1] = scanline_buffer[i+scanline_width];
            rgbe[2] = scanline_buffer[i+2*scanline_width];
            rgbe[3] = scanline_buffer[i+3*scanline_width];
            rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN],
                       &data[RGBE_DATA_BLUE],rgbe);
            data += RGBE_DATA_SIZE;
        }
        num_scanlines--;
    }
    free(scanline_buffer);
    return RGBE_RETURN_SUCCESS;
}
コード例 #17
0
ファイル: rgbe.cpp プロジェクト: rttag/oiio
/* minimal header reading.  modify if you want to parse more information */
int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info,
                    char *errbuf)
{
    char buf[128];
    float tempf;
    size_t i;

    if (info) {
        info->valid = 0;
        info->programtype[0] = 0;
        info->gamma = info->exposure = 1.0;
    }
    if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == NULL)
        return rgbe_error(rgbe_read_error,NULL, errbuf);
    if ((buf[0] != '#')||(buf[1] != '?')) {
        /* if you want to require the magic token then uncomment the next line */
        /*return rgbe_error(rgbe_format_error,"bad initial token"); */
    }
    else if (info) {
        info->valid |= RGBE_VALID_PROGRAMTYPE;
        for(i=0; i<(int)sizeof(info->programtype)-1; i++) {
            if ((buf[i+2] == 0) || isspace(buf[i+2]))
                break;
            info->programtype[i] = buf[i+2];
        }
        info->programtype[i] = 0;
        if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
            return rgbe_error(rgbe_read_error,NULL, errbuf);
    }
    bool found_FORMAT_line = false;
    for(;;) {
        if ((buf[0] == 0)||(buf[0] == '\n')) {
            if (found_FORMAT_line)
                break;
            return rgbe_error(rgbe_format_error,"no FORMAT specifier found", errbuf);
        }
        else if (strcmp(buf,"FORMAT=32-bit_rle_rgbe\n") == 0) {
            found_FORMAT_line = true;
            /* LG says no:    break;       // format found so break out of loop */
        }
        else if (info && (sscanf(buf,"GAMMA=%g",&tempf) == 1)) {
            info->gamma = tempf;
            info->valid |= RGBE_VALID_GAMMA;
        }
        else if (info && (sscanf(buf,"EXPOSURE=%g",&tempf) == 1)) {
            info->exposure = tempf;
            info->valid |= RGBE_VALID_EXPOSURE;
        }
        if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
            return rgbe_error(rgbe_read_error,NULL, errbuf);
    }
    if (strcmp(buf,"\n") != 0) {
        printf ("Found '%s'\n", buf);
        return rgbe_error(rgbe_format_error,
                          "missing blank line after FORMAT specifier", errbuf);
    }
    if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
        return rgbe_error(rgbe_read_error,NULL, errbuf);

    if (sscanf(buf,"-Y %d +X %d",height,width) == 2) {
        if (info) {
            info->orientation = 1;
            info->valid |= RGBE_VALID_ORIENTATION;
        }
    } else if (sscanf(buf,"-Y %d -X %d",height,width) == 2) {
        if (info) {
            info->orientation = 2;
            info->valid |= RGBE_VALID_ORIENTATION;
        }
    } else if (sscanf(buf,"+Y %d -X %d",height,width) == 2) {
        if (info) {
            info->orientation = 3;
            info->valid |= RGBE_VALID_ORIENTATION;
        }
    } else if (sscanf(buf,"+Y %d +X %d",height,width) == 2) {
        if (info) {
            info->orientation = 4;
            info->valid |= RGBE_VALID_ORIENTATION;
        }
    } else if (sscanf(buf,"+X %d -Y %d",height,width) == 2) {
        if (info) {
            info->orientation = 5;
            info->valid |= RGBE_VALID_ORIENTATION;
        }
    } else if (sscanf(buf,"+X %d +Y %d",height,width) == 2) {
        if (info) {
            info->orientation = 6;
            info->valid |= RGBE_VALID_ORIENTATION;
        }
    } else if (sscanf(buf,"-X %d +Y %d",height,width) == 2) {
        if (info) {
            info->orientation = 7;
            info->valid |= RGBE_VALID_ORIENTATION;
        }
    } else if (sscanf(buf,"-X %d -Y %d",height,width) == 2) {
        if (info) {
            info->orientation = 8;
            info->valid |= RGBE_VALID_ORIENTATION;
        }
    } else {
        return rgbe_error(rgbe_format_error,"missing image size specifier", errbuf);
    }
    return RGBE_RETURN_SUCCESS;
}