Open adammaj1 opened 4 years ago
The parsing is not robust. I want to improve it. The quick fix for your case would be to add this to your program, right after calling get_pnm_type(...)
:
rewind(pFile);
as you need to rewind the file to reparse the header again and you would get:
user@userbox:~/projects/libpnmio/src$ ./issue3
Info: magic=PF, x_val=512, y_val=768, aspect_ratio=-1.000000
I have changed :
void read_pfm_header(FILE *f, int *img_xdim, int *img_ydim, int *img_type, int *endianess)
{
int flag=0; // read the line or not
int x_val, y_val;
unsigned int i;
int is_rgb=0, is_greyscale=0;
float aspect_ratio=0;
char magic[2];
char line[MAXLINE];
int count=0;
int ch;
/* Read the PFM file header. */
//fgets(line, MAXLINE, f);
//if (fscanf(fp, "P%c\n", &ch) != 1 || ch != '6')
//sscanf(line, "%s", magic);
/* read the width, height, and maximum value for a pixel */
fscanf(f, "%d %d %f\n", &x_val, &y_val, &aspect_ratio);
printf("\nx_val= %d\n y_val = %d\n aspect_ratio = %f\n", x_val, y_val, aspect_ratio);
// fprintf(stderr, "Header: x_val=%d\n, y_val=%d\n, aspect_ratio=%f\n",
// x_val, y_val, aspect_ratio);
/* FIXME: Aspect ratio different to 1.0 is not yet supported. */
if (!floatEqualComparison(aspect_ratio, -1.0, 1E-06) &&
!floatEqualComparison(aspect_ratio, 1.0, 1E-06)) {
fprintf(stderr, "Error: Aspect ratio different to -1.0 or +1.0 is unsupported!\n");
exit(1);
}
*img_xdim = x_val;
*img_ydim = y_val;
*img_type = is_rgb & ~is_greyscale;
if (aspect_ratio > 0.0) {
*endianess = 1;
} else {
*endianess = -1;
}
}
Now it not works with comments but for simple files is enough. So without rewind:
FILE * pFile;
char * fName = "memorial.pfm"; // "cornellbox_uniform_direct.pfm"; //
int pnm_type;
int img_xdim;
int img_ydim;
int img_type;
int endianess;
//
pFile = fopen (fName, "rb");
pnm_type = get_pnm_type(pFile);
if ( pnm_type ==PFM_RGB || pnm_type == PFM_GREYSCALE)
{
info(pnm_type);
read_pfm_header(pFile, &img_xdim, &img_ydim, &img_type, &endianess);
}
fclose(pFile);
return 0;
}
it works good :
PF = PFM_RGB_BINARY
x_val= 512
y_val = 768
aspect_ratio = -1.000000
Hi , can you check read_pfm_header.
it gives :
TIA