13 #include <opencv2/opencv.hpp>
22 while ((
c=getc(fp)) ==
'#')
23 while (getc(fp) !=
'\n') ;
34 }
while (
c ==
'\n' ||
c ==
' ' ||
c ==
'\t' ||
c ==
'\r');
38 bool readHeader(FILE *fp,
const char *imtype,
char c1,
char c2,
39 int *width,
int *height,
int *nbands,
int thirdArg)
45 if (getc(fp) != c1 || getc(fp) != c2)
47 printf(
"ReadFilePGM: wrong magic code for %s file\n", imtype);
53 int r = fscanf(fp,
"%d", width);
59 r = fscanf(fp,
"%d", height);
66 r = fscanf(fp,
"%d", nbands);
77 if (
c ==
' ' ||
c ==
'\t' ||
c ==
'\r')
79 printf(
"newline expected in file after image height\n");
84 printf(
"whitespace expected in file after image height\n");
107 printf(
"ReadFilePFM: could not open %s\n",
filename);
111 int width, height, nBands;
112 readHeader(fp,
"PFM",
'P',
'f', &width, &height, &nBands, 0);
117 int r = fscanf(fp,
"%f", &scalef);
128 if (
c ==
' ' ||
c ==
'\t' ||
c ==
'\r')
130 printf(
"newline expected in file after scale factor\n");
135 printf(
"whitespace expected in file after scale factor\n");
141 disp = cv::Mat(height, width, CV_32FC1);
143 int littleEndianFile = (scalef < 0);
145 int needSwap = (littleEndianFile != littleEndianMachine);
149 for (
int y = height-1;
y >= 0;
y--) {
151 float* ptr = (
float *) disp.row(
y).data;
152 if ((
int)fread(ptr,
sizeof(
float),
n, fp) !=
n)
154 printf(
"ReadFilePFM(%s): file is too short\n",
filename);
163 tmp = ptr[0]; ptr[0] = ptr[3]; ptr[3] = tmp;
164 tmp = ptr[1]; ptr[1] = ptr[2]; ptr[2] = tmp;
172 printf(
"ReadFilePGM(%s): error closing file\n",
filename);