Commit 9b0f5e61 authored by Alexander Alekhin's avatar Alexander Alekhin

Merge pull request #5185 from mshabunin:fix-rgbe-header-parse

parents 0e4bd80b 9e4c6296
...@@ -182,6 +182,8 @@ int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info) ...@@ -182,6 +182,8 @@ int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info)
info->programtype[0] = 0; info->programtype[0] = 0;
info->gamma = info->exposure = 1.0; info->gamma = info->exposure = 1.0;
} }
// 1. read first line
if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == NULL) if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == NULL)
return rgbe_error(rgbe_read_error,NULL); return rgbe_error(rgbe_read_error,NULL);
if ((buf[0] != '#')||(buf[1] != '?')) { if ((buf[0] != '#')||(buf[1] != '?')) {
...@@ -196,14 +198,19 @@ int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info) ...@@ -196,14 +198,19 @@ int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info)
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)
return rgbe_error(rgbe_read_error,NULL);
} }
// 2. reading other header lines
bool hasFormat = false;
for(;;) { for(;;) {
if ((buf[0] == 0)||(buf[0] == '\n')) if (fgets(buf,sizeof(buf)/sizeof(buf[0]),fp) == 0)
return rgbe_error(rgbe_format_error,"no FORMAT specifier found"); return rgbe_error(rgbe_read_error,NULL);
if (buf[0] == '\n') // end of the header
break;
else if (buf[0] == '#') // commment
continue;
else if (strcmp(buf,"FORMAT=32-bit_rle_rgbe\n") == 0) else if (strcmp(buf,"FORMAT=32-bit_rle_rgbe\n") == 0)
break; /* format found so break out of loop */ hasFormat = true;
else if (info && (sscanf(buf,"GAMMA=%g",&tempf) == 1)) { else if (info && (sscanf(buf,"GAMMA=%g",&tempf) == 1)) {
info->gamma = tempf; info->gamma = tempf;
info->valid |= RGBE_VALID_GAMMA; info->valid |= RGBE_VALID_GAMMA;
...@@ -212,14 +219,14 @@ int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info) ...@@ -212,14 +219,14 @@ int RGBE_ReadHeader(FILE *fp, int *width, int *height, rgbe_header_info *info)
info->exposure = tempf; info->exposure = tempf;
info->valid |= RGBE_VALID_EXPOSURE; 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) 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");
if (!hasFormat)
return rgbe_error(rgbe_format_error, "missing FORMAT specifier");
// 3. reading resolution string
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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment