rgbe.c   rgbe.c 
skipping to change at line 180 skipping to change at line 180
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
if ((buf[0] != '#')||(buf[1] != '?')) { if ((buf[0] != '#')||(buf[1] != '?')) {
/* if you want to require the magic token then uncomment the next line */ /* if you want to require the magic token then uncomment the next line */
/*return rgbe_error(rgbe_format_error,"bad initial token"); */ /*return rgbe_error(rgbe_format_error,"bad initial token"); */
} }
else if (info) { else if (info) {
info->valid |= VIGRA_RGBE_VALID_PROGRAMTYPE; info->valid |= VIGRA_RGBE_VALID_PROGRAMTYPE;
for(i=0;i<sizeof(info->programtype)-1;i++) { for(i=0;i<sizeof(info->programtype)-1;i++) {
if ((buf[i+2] == 0) || isspace(buf[i+2])) if ((buf[i+2] == 0) || isspace(buf[i+2]))
break; break;
info->programtype[i] = buf[i+2]; info->programtype[i] = buf[i+2];
} }
info->programtype[i] = 0; info->programtype[i] = 0;
if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0) if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
} }
for(;;) { for(;;) {
if ((buf[0] == 0)||(buf[0] == '\n')) if ((buf[0] == 0)||(buf[0] == '\n'))
return rgbe_error(rgbe_format_error,"no FORMAT specifier found"); return rgbe_error(rgbe_format_error,"no FORMAT specifier found");
skipping to change at line 210 skipping to change at line 210
} }
if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0) if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
} }
#if 0 #if 0
if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0) if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
if (strcmp(buf,"\n") != 0) if (strcmp(buf,"\n") != 0)
return rgbe_error(rgbe_format_error, return rgbe_error(rgbe_format_error,
"missing blank line after FORMAT specifier"); "missing blank line after FORMAT specifier");
#endif #endif
for(;;) { for(;;) {
if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0) if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
if (sscanf(buf,"-Y %d +X %d",height,width) == 2) if (sscanf(buf,"-Y %d +X %d",height,width) == 2)
break; break;
} }
skipping to change at line 233 skipping to change at line 233
/* simple write routine that does not use run length encoding */ /* simple write routine that does not use run length encoding */
/* These routines can be made faster by allocating a larger buffer and /* These routines can be made faster by allocating a larger buffer and
fread-ing and fwrite-ing the data in larger chunks */ fread-ing and fwrite-ing the data in larger chunks */
int VIGRA_RGBE_WritePixels(FILE *fp, float *data, int numpixels) int VIGRA_RGBE_WritePixels(FILE *fp, float *data, int numpixels)
{ {
unsigned char rgbe[4]; unsigned char rgbe[4];
while (numpixels-- > 0) { while (numpixels-- > 0) {
VIGRA_float2rgbe(rgbe,data[RGBE_DATA_RED], VIGRA_float2rgbe(rgbe,data[RGBE_DATA_RED],
data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]); data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]);
data += RGBE_DATA_SIZE; data += RGBE_DATA_SIZE;
if (fwrite(rgbe, sizeof(rgbe), 1, fp) < 1) if (fwrite(rgbe, sizeof(rgbe), 1, fp) < 1)
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
} }
return VIGRA_RGBE_RETURN_SUCCESS; return VIGRA_RGBE_RETURN_SUCCESS;
} }
/* simple read routine. will not correctly handle run length encoding */ /* simple read routine. will not correctly handle run length encoding */
int VIGRA_RGBE_ReadPixels(FILE *fp, float *data, int numpixels) int VIGRA_RGBE_ReadPixels(FILE *fp, float *data, int numpixels)
{ {
unsigned char rgbe[4]; unsigned char rgbe[4];
while(numpixels-- > 0) { while(numpixels-- > 0) {
if (fread(rgbe, sizeof(rgbe), 1, fp) < 1) if (fread(rgbe, sizeof(rgbe), 1, fp) < 1)
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
VIGRA_rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN], VIGRA_rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN],
&data[RGBE_DATA_BLUE],rgbe); &data[RGBE_DATA_BLUE],rgbe);
data += RGBE_DATA_SIZE; data += RGBE_DATA_SIZE;
} }
return VIGRA_RGBE_RETURN_SUCCESS; return VIGRA_RGBE_RETURN_SUCCESS;
} }
int VIGRA_RGBE_ReadPixels_Raw(FILE *fp, unsigned char *data, unsigned int n umpixels) int VIGRA_RGBE_ReadPixels_Raw(FILE *fp, unsigned char *data, unsigned int n umpixels)
{ {
if (fread(data, 4, numpixels, fp) < numpixels) if (fread(data, 4, numpixels, fp) < numpixels)
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
skipping to change at line 286 skipping to change at line 286
while(cur < numbytes) { while(cur < numbytes) {
beg_run = cur; beg_run = cur;
/* find next run of length at least 4 if one exists */ /* find next run of length at least 4 if one exists */
run_count = old_run_count = 0; run_count = old_run_count = 0;
while((run_count < MINRUNLENGTH) && (beg_run < numbytes)) { while((run_count < MINRUNLENGTH) && (beg_run < numbytes)) {
beg_run += run_count; beg_run += run_count;
old_run_count = run_count; old_run_count = run_count;
run_count = 1; run_count = 1;
while( (beg_run + run_count < numbytes) && (run_count < 127) while( (beg_run + run_count < numbytes) && (run_count < 127)
&& (data[beg_run] == data[beg_run + run_count])) && (data[beg_run] == data[beg_run + run_count]))
run_count++; run_count++;
} }
/* if data before next big run is a short run then write it as such */ /* 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)) { if ((old_run_count > 1)&&(old_run_count == beg_run - cur)) {
buf[0] = 128 + old_run_count; /*write short run*/ buf[0] = 128 + old_run_count; /*write short run*/
buf[1] = data[cur]; buf[1] = data[cur];
if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1) if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1)
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
cur = beg_run; cur = beg_run;
} }
/* write out bytes until we reach the start of the next run */ /* write out bytes until we reach the start of the next run */
while(cur < beg_run) { while(cur < beg_run) {
nonrun_count = beg_run - cur; nonrun_count = beg_run - cur;
if (nonrun_count > 128) if (nonrun_count > 128)
nonrun_count = 128; nonrun_count = 128;
buf[0] = nonrun_count; buf[0] = nonrun_count;
if (fwrite(buf,sizeof(buf[0]),1,fp) < 1) if (fwrite(buf,sizeof(buf[0]),1,fp) < 1)
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
if (fwrite(&data[cur],sizeof(data[0])*nonrun_count,1,fp) < 1) if (fwrite(&data[cur],sizeof(data[0])*nonrun_count,1,fp) < 1)
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
cur += nonrun_count; cur += nonrun_count;
} }
/* write out next run if one was found */ /* write out next run if one was found */
if (run_count >= MINRUNLENGTH) { if (run_count >= MINRUNLENGTH) {
buf[0] = 128 + run_count; buf[0] = 128 + run_count;
buf[1] = data[beg_run]; buf[1] = data[beg_run];
if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1) if (fwrite(buf,sizeof(buf[0])*2,1,fp) < 1)
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
cur += run_count; cur += run_count;
} }
} }
return VIGRA_RGBE_RETURN_SUCCESS; return VIGRA_RGBE_RETURN_SUCCESS;
#undef MINRUNLENGTH #undef MINRUNLENGTH
} }
int VIGRA_RGBE_WritePixels_RLE(FILE *fp, float *data, int scanline_width, int VIGRA_RGBE_WritePixels_RLE(FILE *fp, float *data, int scanline_width,
int num_scanlines) int num_scanlines)
{ {
unsigned char rgbe[4]; unsigned char rgbe[4];
unsigned char *buffer; unsigned char *buffer;
int i, err; int i, err;
if ((scanline_width < 8)||(scanline_width > 0x7fff)) if ((scanline_width < 8)||(scanline_width > 0x7fff))
/* run length encoding is not allowed so write flat*/ /* run length encoding is not allowed so write flat*/
return VIGRA_RGBE_WritePixels(fp,data,scanline_width*num_scanlines); return VIGRA_RGBE_WritePixels(fp,data,scanline_width*num_scanlines);
buffer = (unsigned char *)malloc(sizeof(unsigned char)*4*scanline_width); buffer = (unsigned char *)malloc(sizeof(unsigned char)*4*scanline_width);
if (buffer == NULL) if (buffer == NULL)
skipping to change at line 346 skipping to change at line 346
rgbe[0] = 2; rgbe[0] = 2;
rgbe[1] = 2; rgbe[1] = 2;
rgbe[2] = scanline_width >> 8; rgbe[2] = scanline_width >> 8;
rgbe[3] = scanline_width & 0xFF; rgbe[3] = scanline_width & 0xFF;
if (fwrite(rgbe, sizeof(rgbe), 1, fp) < 1) { if (fwrite(rgbe, sizeof(rgbe), 1, fp) < 1) {
free(buffer); free(buffer);
return rgbe_error(rgbe_write_error,NULL); return rgbe_error(rgbe_write_error,NULL);
} }
for(i=0;i<scanline_width;i++) { for(i=0;i<scanline_width;i++) {
VIGRA_float2rgbe(rgbe,data[RGBE_DATA_RED], VIGRA_float2rgbe(rgbe,data[RGBE_DATA_RED],
data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]); data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]);
buffer[i] = rgbe[0]; buffer[i] = rgbe[0];
buffer[i+scanline_width] = rgbe[1]; buffer[i+scanline_width] = rgbe[1];
buffer[i+2*scanline_width] = rgbe[2]; buffer[i+2*scanline_width] = rgbe[2];
buffer[i+3*scanline_width] = rgbe[3]; buffer[i+3*scanline_width] = rgbe[3];
data += RGBE_DATA_SIZE; data += RGBE_DATA_SIZE;
} }
/* write out each of the four channels separately run length encoded */ /* write out each of the four channels separately run length encoded */
/* first red, then green, then blue, then exponent */ /* first red, then green, then blue, then exponent */
for(i=0;i<4;i++) { for(i=0;i<4;i++) {
if ((err = RGBE_WriteBytes_RLE(fp,&buffer[i*scanline_width], if ((err = RGBE_WriteBytes_RLE(fp,&buffer[i*scanline_width],
scanline_width)) != VIGRA_RGBE_RETURN_S scanline_width)) != VIGRA_RGBE_RETURN_SUCCESS) {
UCCESS) { free(buffer);
free(buffer); return err;
return err;
} }
} }
} }
free(buffer); free(buffer);
return VIGRA_RGBE_RETURN_SUCCESS; return VIGRA_RGBE_RETURN_SUCCESS;
} }
int VIGRA_RGBE_ReadPixels_RLE(FILE *fp, float *data, int scanline_width, int VIGRA_RGBE_ReadPixels_RLE(FILE *fp, float *data, int scanline_width,
int num_scanlines) int num_scanlines)
{ {
unsigned char rgbe[4], *scanline_buffer, *ptr, *ptr_end; unsigned char rgbe[4], *scanline_buffer, *ptr, *ptr_end;
int i, count; int i, count;
unsigned char buf[2]; unsigned char buf[2];
if ((scanline_width < 8)||(scanline_width > 0x7fff)) if ((scanline_width < 8)||(scanline_width > 0x7fff))
/* run length encoding is not allowed so read flat*/ /* run length encoding is not allowed so read flat*/
return VIGRA_RGBE_ReadPixels(fp,data,scanline_width*num_scanlines); return VIGRA_RGBE_ReadPixels(fp,data,scanline_width*num_scanlines);
scanline_buffer = NULL; scanline_buffer = NULL;
/* read in each successive scanline */ /* read in each successive scanline */
skipping to change at line 397 skipping to change at line 397
data += RGBE_DATA_SIZE; data += RGBE_DATA_SIZE;
free(scanline_buffer); free(scanline_buffer);
return VIGRA_RGBE_ReadPixels(fp,data,scanline_width*num_scanlines-1); return VIGRA_RGBE_ReadPixels(fp,data,scanline_width*num_scanlines-1);
} }
if ((((int)rgbe[2])<<8 | rgbe[3]) != scanline_width) { if ((((int)rgbe[2])<<8 | rgbe[3]) != scanline_width) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_format_error,"wrong scanline width"); return rgbe_error(rgbe_format_error,"wrong scanline width");
} }
if (scanline_buffer == NULL) if (scanline_buffer == NULL)
scanline_buffer = (unsigned char *) scanline_buffer = (unsigned char *)
malloc(sizeof(unsigned char)*4*scanline_width); malloc(sizeof(unsigned char)*4*scanline_width);
if (scanline_buffer == NULL) if (scanline_buffer == NULL)
return rgbe_error(rgbe_memory_error,"unable to allocate buffer space" ); return rgbe_error(rgbe_memory_error,"unable to allocate buffer space" );
ptr = &scanline_buffer[0]; ptr = &scanline_buffer[0];
/* read each of the four channels for the scanline into the buffer */ /* read each of the four channels for the scanline into the buffer */
for(i=0;i<4;i++) { for(i=0;i<4;i++) {
ptr_end = &scanline_buffer[(i+1)*scanline_width]; ptr_end = &scanline_buffer[(i+1)*scanline_width];
while(ptr < ptr_end) { while(ptr < ptr_end) {
if (fread(buf,sizeof(buf[0])*2,1,fp) < 1) { if (fread(buf,sizeof(buf[0])*2,1,fp) < 1) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
} }
if (buf[0] > 128) { if (buf[0] > 128) {
/* a run of the same value */ /* a run of the same value */
count = buf[0]-128; count = buf[0]-128;
if ((count == 0)||(count > ptr_end - ptr)) { if ((count == 0)||(count > ptr_end - ptr)) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_format_error,"bad scanline data"); return rgbe_error(rgbe_format_error,"bad scanline data");
} }
while(count-- > 0) while(count-- > 0)
*ptr++ = buf[1]; *ptr++ = buf[1];
} }
else { else {
/* a non-run */ /* a non-run */
count = buf[0]; count = buf[0];
if ((count == 0)||(count > ptr_end - ptr)) { if ((count == 0)||(count > ptr_end - ptr)) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_format_error,"bad scanline data"); return rgbe_error(rgbe_format_error,"bad scanline data");
} }
*ptr++ = buf[1]; *ptr++ = buf[1];
if (--count > 0) { if (--count > 0) {
if (fread(ptr,sizeof(*ptr)*count,1,fp) < 1) { if (fread(ptr,sizeof(*ptr)*count,1,fp) < 1) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
} }
ptr += count; ptr += count;
} }
} }
} }
} }
/* now convert data from buffer into floats */ /* now convert data from buffer into floats */
for(i=0;i<scanline_width;i++) { for(i=0;i<scanline_width;i++) {
rgbe[0] = scanline_buffer[i]; rgbe[0] = scanline_buffer[i];
rgbe[1] = scanline_buffer[i+scanline_width]; rgbe[1] = scanline_buffer[i+scanline_width];
rgbe[2] = scanline_buffer[i+2*scanline_width]; rgbe[2] = scanline_buffer[i+2*scanline_width];
rgbe[3] = scanline_buffer[i+3*scanline_width]; rgbe[3] = scanline_buffer[i+3*scanline_width];
VIGRA_rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN], VIGRA_rgbe2float(&data[RGBE_DATA_RED],&data[RGBE_DATA_GREEN],
&data[RGBE_DATA_BLUE],rgbe); &data[RGBE_DATA_BLUE],rgbe);
data += RGBE_DATA_SIZE; data += RGBE_DATA_SIZE;
} }
num_scanlines--; num_scanlines--;
} }
free(scanline_buffer); free(scanline_buffer);
return VIGRA_RGBE_RETURN_SUCCESS; return VIGRA_RGBE_RETURN_SUCCESS;
} }
int VIGRA_RGBE_ReadPixels_Raw_RLE(FILE *fp, unsigned char *data, int scanli ne_width, int VIGRA_RGBE_ReadPixels_Raw_RLE(FILE *fp, unsigned char *data, int scanli ne_width,
int num_scanlines) int num_scanlines)
{ {
unsigned char rgbe[4], *scanline_buffer, *ptr, *ptr_end; unsigned char rgbe[4], *scanline_buffer, *ptr, *ptr_end;
int i, count; int i, count;
unsigned char buf[2]; unsigned char buf[2];
if ((scanline_width < 8)||(scanline_width > 0x7fff)) if ((scanline_width < 8)||(scanline_width > 0x7fff))
/* run length encoding is not allowed so read flat*/ /* run length encoding is not allowed so read flat*/
return VIGRA_RGBE_ReadPixels_Raw(fp,data,scanline_width*num_scanlines); return VIGRA_RGBE_ReadPixels_Raw(fp,data,scanline_width*num_scanlines);
scanline_buffer = NULL; scanline_buffer = NULL;
skipping to change at line 500 skipping to change at line 500
scanline_buffer = (unsigned char *) malloc(sizeof(unsigned char)*4*sc anline_width); scanline_buffer = (unsigned char *) malloc(sizeof(unsigned char)*4*sc anline_width);
if (scanline_buffer == NULL) if (scanline_buffer == NULL)
return rgbe_error(rgbe_memory_error,"unable to allocate buffer space" ); return rgbe_error(rgbe_memory_error,"unable to allocate buffer space" );
ptr = &scanline_buffer[0]; ptr = &scanline_buffer[0];
/* read each of the four channels for the scanline into the buffer */ /* read each of the four channels for the scanline into the buffer */
for(i=0;i<4;i++) { for(i=0;i<4;i++) {
ptr_end = &scanline_buffer[(i+1)*scanline_width]; ptr_end = &scanline_buffer[(i+1)*scanline_width];
while(ptr < ptr_end) { while(ptr < ptr_end) {
if (fread(buf,sizeof(buf[0])*2,1,fp) < 1) { if (fread(buf,sizeof(buf[0])*2,1,fp) < 1) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
} }
if (buf[0] > 128) { if (buf[0] > 128) {
/* a run of the same value */ /* a run of the same value */
count = buf[0]-128; count = buf[0]-128;
if ((count == 0)||(count > ptr_end - ptr)) { if ((count == 0)||(count > ptr_end - ptr)) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_format_error,"bad scanline data"); return rgbe_error(rgbe_format_error,"bad scanline data");
} }
while(count-- > 0) while(count-- > 0)
*ptr++ = buf[1]; *ptr++ = buf[1];
} }
else { else {
/* a non-run */ /* a non-run */
count = buf[0]; count = buf[0];
if ((count == 0)||(count > ptr_end - ptr)) { if ((count == 0)||(count > ptr_end - ptr)) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_format_error,"bad scanline data"); return rgbe_error(rgbe_format_error,"bad scanline data");
} }
*ptr++ = buf[1]; *ptr++ = buf[1];
if (--count > 0) { if (--count > 0) {
if (fread(ptr,sizeof(*ptr)*count,1,fp) < 1) { if (fread(ptr,sizeof(*ptr)*count,1,fp) < 1) {
free(scanline_buffer); free(scanline_buffer);
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
} }
ptr += count; ptr += count;
} }
} }
} }
} }
/* copy byte data to output */ /* copy byte data to output */
for(i=0;i<scanline_width;i++) { for(i=0;i<scanline_width;i++) {
data[0] = scanline_buffer[i]; data[0] = scanline_buffer[i];
data[1] = scanline_buffer[i+scanline_width]; data[1] = scanline_buffer[i+scanline_width];
data[2] = scanline_buffer[i+2*scanline_width]; data[2] = scanline_buffer[i+2*scanline_width];
data[3] = scanline_buffer[i+3*scanline_width]; data[3] = scanline_buffer[i+3*scanline_width];
data += 4; data += 4;
} }
 End of changes. 19 change blocks. 
80 lines changed or deleted 79 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/