io.h
Go to the documentation of this file.
1 /*
2  * pfmReader.h
3  *
4  * Created on: Dec 4, 2015
5  * Author: mathieu
6  */
7 
8 #ifndef PFMREADER_H_
9 #define PFMREADER_H_
10 
11 #include <iostream>
12 #include <stdio.h>
13 #include <opencv2/opencv.hpp>
14 
15 // taken from http://vision.middlebury.edu/stereo/ evaluation tool
16 
17 void skipComment(FILE *fp)
18 {
19  // skip comment lines in the headers of pnm files
20 
21  char c;
22  while ((c=getc(fp)) == '#')
23  while (getc(fp) != '\n') ;
24  ungetc(c, fp);
25 }
26 
27 void skipSpace(FILE *fp)
28 {
29  // skip white space in the headers or pnm files
30 
31  char c;
32  do {
33  c = getc(fp);
34  } while (c == '\n' || c == ' ' || c == '\t' || c == '\r');
35  ungetc(c, fp);
36 }
37 
38 bool readHeader(FILE *fp, const char *imtype, char c1, char c2,
39  int *width, int *height, int *nbands, int thirdArg)
40 {
41  // read the header of a pnmfile and initialize width and height
42 
43  char c;
44 
45  if (getc(fp) != c1 || getc(fp) != c2)
46  {
47  printf("ReadFilePGM: wrong magic code for %s file\n", imtype);
48  return false;
49  }
50  skipSpace(fp);
51  skipComment(fp);
52  skipSpace(fp);
53  int r = fscanf(fp, "%d", width);
54  if(r==0)
55  {
56  return false;
57  }
58  skipSpace(fp);
59  r = fscanf(fp, "%d", height);
60  if(r==0)
61  {
62  return false;
63  }
64  if (thirdArg) {
65  skipSpace(fp);
66  r = fscanf(fp, "%d", nbands);
67  if(r==0)
68  {
69  return false;
70  }
71  }
72  // skip SINGLE newline character after reading image height (or third arg)
73  c = getc(fp);
74  if (c == '\r') // <cr> in some files before newline
75  c = getc(fp);
76  if (c != '\n') {
77  if (c == ' ' || c == '\t' || c == '\r')
78  {
79  printf("newline expected in file after image height\n");
80  return false;
81  }
82  else
83  {
84  printf("whitespace expected in file after image height\n");
85  return false;
86  }
87  }
88 
89  return true;
90 }
91 
93 {
94  int intval = 1;
95  uchar *uval = (uchar *)&intval;
96  return uval[0] == 1;
97 }
98 
99 cv::Mat readPFM(const char* filename)
100 {
101  cv::Mat disp;
102 
103  // Open the file and read the header
104  FILE *fp = fopen(filename, "rb");
105  if (fp == 0)
106  {
107  printf("ReadFilePFM: could not open %s\n", filename);
108  return cv::Mat();
109  }
110 
111  int width, height, nBands;
112  readHeader(fp, "PFM", 'P', 'f', &width, &height, &nBands, 0);
113 
114  skipSpace(fp);
115 
116  float scalef;
117  int r = fscanf(fp, "%f", &scalef); // scale factor (if negative, little endian)
118  if(r==0)
119  {
120  return cv::Mat();
121  }
122 
123  // skip SINGLE newline character after reading third arg
124  char c = getc(fp);
125  if (c == '\r') // <cr> in some files before newline
126  c = getc(fp);
127  if (c != '\n') {
128  if (c == ' ' || c == '\t' || c == '\r')
129  {
130  printf("newline expected in file after scale factor\n");
131  return cv::Mat();
132  }
133  else
134  {
135  printf("whitespace expected in file after scale factor\n");
136  return cv::Mat();
137  }
138  }
139 
140  // Set the image shape
141  disp = cv::Mat(height, width, CV_32FC1);
142 
143  int littleEndianFile = (scalef < 0);
144  int littleEndianMachine = littleendian();
145  int needSwap = (littleEndianFile != littleEndianMachine);
146  //printf("endian file = %d, endian machine = %d, need swap = %d\n",
147  // littleEndianFile, littleEndianMachine, needSwap);
148 
149  for (int y = height-1; y >= 0; y--) { // PFM stores rows top-to-bottom!!!!
150  int n = width;
151  float* ptr = (float *) disp.row(y).data;
152  if ((int)fread(ptr, sizeof(float), n, fp) != n)
153  {
154  printf("ReadFilePFM(%s): file is too short\n", filename);
155  return cv::Mat();
156  }
157 
158  if (needSwap) { // if endianness doesn't agree, swap bytes
159  uchar* ptr = (uchar *) disp.row(y).data;
160  int x = 0;
161  uchar tmp = 0;
162  while (x < n) {
163  tmp = ptr[0]; ptr[0] = ptr[3]; ptr[3] = tmp;
164  tmp = ptr[1]; ptr[1] = ptr[2]; ptr[2] = tmp;
165  ptr += 4;
166  x++;
167  }
168  }
169  }
170  if (fclose(fp))
171  {
172  printf("ReadFilePGM(%s): error closing file\n", filename);
173  return cv::Mat();
174  }
175 
176  return disp;
177 }
178 
179 
180 #endif /* PFMREADER_H_ */
unsigned char uchar
Definition: matrix.h:41
void skipComment(FILE *fp)
Definition: io.h:17
void skipSpace(FILE *fp)
Definition: io.h:27
bool readHeader(FILE *fp, const char *imtype, char c1, char c2, int *width, int *height, int *nbands, int thirdArg)
Definition: io.h:38
const int c1
Definition: SuperPoint.cc:15
const int c2
Definition: SuperPoint.cc:16
cv::Mat readPFM(const char *filename)
Definition: io.h:99
int littleendian()
Definition: io.h:92


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:34:59