Fawkes API  Fawkes Development Version
bb2rectlut.cpp
1 
2 /***************************************************************************
3  * bb2rectlut.cpp - BB2 Rectification LUT utility
4  *
5  * Created: Mon Oct 29 19:04:28 2007
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #ifdef HAVE_BUMBLEBEE2_CAM
24 #include <fvcams/bumblebee2.h>
25 #endif
26 #include <fvutils/system/camargp.h>
27 #include <utils/system/argparser.h>
28 #include <fvutils/rectification/rectfile.h>
29 #include <fvutils/rectification/rectinfo_block.h>
30 #include <fvutils/rectification/rectinfo_lut_block.h>
31 
32 #ifdef HAVE_TRICLOPS_SDK
33 #include <fvstereo/triclops.h>
34 #include <cerrno>
35 #endif
36 
37 #include <cstdlib>
38 #include <cstdio>
39 #include <unistd.h>
40 
41 using namespace fawkes;
42 using namespace firevision;
43 
44 void
45 print_usage(ArgumentParser *argp)
46 {
47  printf("Usage: %s <-r|-v|-i> file.rectlut\n", argp->program_name());
48  printf("You have to give at least one of -r/-v/-i and a file name\n"
49  " -r retrieve rectification lut from live camera,\n"
50  " uses first found Bumblebee2 camera\n"
51  " -v verify rectification lut, compares the identification\n"
52  " info stored in the file with the first currently\n"
53  " attached camera\n"
54  " -d deep verifiction of rectification LUT, compares the identification\n"
55  " info stored in the file with the first currently attached camera. It\n"
56  " also verifies each single mapping on equality.\n"
57  " -i print info about rectification LUT file\n\n"
58  );
59  exit(1);
60 }
61 
62 
63 int
64 retrieve(ArgumentParser *argp)
65 {
66 #ifdef HAVE_BUMBLEBEE2_CAM
67 #ifdef HAVE_TRICLOPS_SDK
68  const char *lut_file = argp->items()[0];
69 
70  if ( access(lut_file, F_OK) == 0) {
71  fprintf(stderr, "File with name %s exists, delete manually and retry. Aborting.\n", lut_file);
72  return -1;
73  }
74  if ( access(lut_file, W_OK) != 0) {
75  // ENOENT is ok, we would have access, but there is no file, yet
76  if ( errno != ENOENT ) {
77  fprintf(stderr, "Cannot write to file %s, permission problem?\n", lut_file);
78  return -2;
79  }
80  }
81 
82  CameraArgumentParser *cap = new CameraArgumentParser("bumblebee2:Bumblebee2 BB2-03S2C");
83  Bumblebee2Camera *bb2 = new Bumblebee2Camera(cap);
84  bb2->open();
85 
87  triclops->generate_rectification_lut(lut_file);
88  delete triclops;
89 
90  bb2->close();
91 
92  delete bb2;
93  delete cap;
94 #else
95  printf("Retrieving the rectification LUT from a camera is not supported,\n"
96  "because the Triclops SDK was not available at compile time.\n");
97 #endif
98 #else
99  printf("Retrieving the rectification LUT from a camera is not supported,\n"
100  "because the Bumblebee2 support was not available at compile time.\n");
101 #endif
102 
103  return 0;
104 }
105 
106 
107 int
108 verify(ArgumentParser *argp)
109 {
110  int rv = 0;
111 
112 #ifdef HAVE_BUMBLEBEE2_CAM
113  CameraArgumentParser *cap = new CameraArgumentParser("bumblebee2:Bumblebee2 BB2-03S2C");
114  Bumblebee2Camera *bb2 = new Bumblebee2Camera(cap);
115  bb2->open();
116 
117  for (unsigned int i = 0; i < argp->num_items(); ++i) {
118 
119  const char *lut_file = argp->items()[i];
120 
121  if ( access(lut_file, F_OK) != 0) {
122  fprintf(stderr, "File with name %s does not exist. Ignoring.\n", lut_file);
123  continue;
124  }
125  if ( access(lut_file, R_OK) != 0) {
126  fprintf(stderr, "Cannot read file %s, permission problem? Ingoring.\n", lut_file);
127  continue;
128  }
129 
131  try {
132  rif->read(lut_file);
133 
134  if ( bb2->verify_guid( rif->guid() ) ) {
135  printf("Success. The rectification info file has been created for the "
136  "connected camera\n");
137  } else {
138  printf("Failure. The rectification info file has *not* been created "
139  "for the connected camera\n");
140  rv = 5;
141  }
142  } catch (Exception &e) {
143  fprintf(stderr, "Failed to read lut file %s\n", lut_file);
144  e.print_trace();
145  }
146 
147  delete rif;
148 
149  }
150 
151  bb2->close();
152 
153  delete bb2;
154  delete cap;
155 
156 #else
157  printf("Verifying the rectification LUT from a camera is not supported,\n"
158  "because the Bumblebee2 support was not available at compile time.\n");
159 #endif
160 
161  return rv;
162 }
163 
164 
165 int
166 deep_verify(ArgumentParser *argp)
167 {
168 #ifdef HAVE_BUMBLEBEE2_CAM
169 #ifdef HAVE_TRICLOPS_SDK
170  int rv = 0;
171 
172  CameraArgumentParser *cap = new CameraArgumentParser("bumblebee2:Bumblebee2 BB2-03S2C");
173  Bumblebee2Camera *bb2 = new Bumblebee2Camera(cap);
174  bb2->open();
175 
177 
178  for (unsigned int i = 0; i < argp->num_items(); ++i) {
179 
180  const char *lut_file = argp->items()[i];
181 
182  if ( access(lut_file, F_OK) != 0) {
183  fprintf(stderr, "File with name %s does not exist. Ignoring.\n", lut_file);
184  continue;
185  }
186  if ( access(lut_file, R_OK) != 0) {
187  fprintf(stderr, "Cannot read file %s, permission problem? Ingoring.\n", lut_file);
188  continue;
189  }
190 
191  if ( triclops->verify_rectification_lut(lut_file) ) {
192  printf("Success. LUT file %s contains matching configuration data.\n", lut_file);
193  } else {
194  printf("Failure. LUT file %s does not contain matching configuration data.\n", lut_file);
195  }
196 
197  }
198 
199  delete triclops;
200  bb2->close();
201 
202  delete bb2;
203  delete cap;
204 
205  return rv;
206 #else
207  printf("Deep verification of the rectification LUT from a camera is not supported,\n"
208  "because the Triclops SDK was not available at compile time.\n");
209  return 0;
210 #endif
211 #else
212  printf("Deep verification of the rectification LUT from a camera is not supported,\n"
213  "because the Bumblebee2 support was not available at compile time.\n");
214  return 0;
215 #endif
216 }
217 
218 
219 void
220 print_info(ArgumentParser *argp)
221 {
222  for (unsigned int i = 0; i < argp->num_items(); ++i) {
223 
224  const char *lut_file = argp->items()[i];
225 
226  if ( access(lut_file, F_OK) != 0) {
227  fprintf(stderr, "File with name %s does not exist. Ignoring.\n", lut_file);
228  continue;
229  }
230  if ( access(lut_file, R_OK) != 0) {
231  fprintf(stderr, "Cannot read file %s, permission problem? Ingoring.\n", lut_file);
232  continue;
233  }
234 
236  try {
237  rif->read(lut_file);
239 
240  printf("File: %s\n"
241  "Version: %u\n"
242  "Endianess: %s\n"
243  "Num Blocks: %zu/%zu (header/read)\n"
244 #if __WORDSIZE == 64
245  "GUID: 0x%016lX\n"
246 #else
247  "GUID: 0x%016llX\n"
248 #endif
249  "Camera Model: %s\n",
250  lut_file, rif->version(),
251  rif->is_little_endian() ? "little endian" : "big endian",
252  rif->num_blocks(), blocks->size(),
253 #if __WORDSIZE == 64
254  (long unsigned int)rif->guid(),
255 #else
256  (long long unsigned int)rif->guid(),
257 #endif
258  rif->model());
259 
260  unsigned int u = 1;
261  RectificationInfoFile::RectInfoBlockVector::const_iterator b;
262  for (b = blocks->begin(); b != blocks->end(); ++b) {
263  RectificationInfoBlock *rib = *b;
264 
265  printf("\nRectInfo Block No. %u\n"
266  "Type: %s\n"
267  "Camera: %s\n"
268  "Size: %zu\n",
269  u++,
270  rectinfo_type_strings[rib->type()],
271  rectinfo_camera_strings[rib->camera()],
272  rib->block_size());
273 
274  switch (rib->type()) {
275  case FIREVISION_RECTINFO_TYPE_LUT_16x16:
276  {
277  RectificationLutInfoBlock *rlib = dynamic_cast<RectificationLutInfoBlock *>(rib);
278  if ( rlib == NULL ) {
279  printf("** Failure to access LUT_16x16\n");
280  } else {
281  printf("LUT width: %hu\n"
282  "LUT height: %hu\n",
283  rlib->pixel_width(), rlib->pixel_height());
284  }
285  }
286  break;
287  default:
288  printf("** No additional information available for this info type\n");
289  break;
290  }
291  }
292 
293  delete blocks;
294  } catch (Exception &e) {
295  fprintf(stderr, "Failed to read lut file %s\n", lut_file);
296  e.print_trace();
297  }
298 
299  delete rif;
300 
301  }
302 }
303 
304 
305 int
306 main(int argc, char **argv)
307 {
308 
309  ArgumentParser argp(argc, argv, "rvid");
310 
311  if (argp.num_items() == 0) {
312  print_usage(&argp);
313  }
314 
315  if ( argp.has_arg("r") ) {
316  return retrieve(&argp);
317  } else if ( argp.has_arg("v") ) {
318  return verify(&argp);
319  } else if ( argp.has_arg("d") ) {
320  return deep_verify(&argp);
321  } else if ( argp.has_arg("i") ) {
322  print_info(&argp);
323  } else {
324  print_usage(&argp);
325  }
326 
327  return 0;
328 }
const char * program_name() const
Get name of program.
Definition: argparser.cpp:502
size_t num_blocks()
Get the number of available info blocks.
Definition: fvfile.cpp:232
virtual void open()
Open the camera.
Definition: bumblebee2.cpp:353
unsigned int type() const
Get block type.
uint16_t pixel_width()
Get width of the LUT.
const char * model()
Get the model of the camera.
Definition: rectfile.cpp:116
const std::vector< const char *> & items() const
Get non-option items.
Definition: argparser.cpp:462
bool verify_rectification_lut(const char *lut_file)
Verify rectification LUT.
Definition: triclops.cpp:913
Fawkes library namespace.
Recitification Lookup Table Block.
void generate_rectification_lut(const char *lut_file)
Generate rectification LUT.
Definition: triclops.cpp:859
Parse command line arguments.
Definition: argparser.h:66
Camera argument parser.
Definition: camargp.h:38
Vector that is used for maintaining the rectification info blocks.
Definition: rectfile.h:49
bool is_little_endian()
Check if data is encoded as little endian.
Definition: fvfile.cpp:188
uint64_t guid()
Get the GUID of camera.
Definition: rectfile.cpp:106
std::vector< const char *>::size_type num_items() const
Get number of non-option items.
Definition: argparser.cpp:472
unsigned int version()
Get the version of the file.
Definition: fvfile.cpp:168
Bumblebee2 camera.
Definition: bumblebee2.h:37
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void close()
Close camera.
Definition: bumblebee2.cpp:400
uint16_t pixel_height()
Get height the LUT.
virtual void read(const char *filename)
Read file.
Definition: rectfile.cpp:157
Rectification info block.
Stereo processing using PGR Triclops SDK.
Definition: triclops.h:40
void print_trace()
Prints trace to stderr.
Definition: exception.cpp:619
size_t block_size() const
Size of blocks.
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:169
virtual bool verify_guid(uint64_t ver_guid) const
Verify GUID validity.
Definition: bumblebee2.cpp:236
Rectification Info File.
Definition: rectfile.h:38
uint8_t camera() const
Get block camera identifier.
RectInfoBlockVector * rectinfo_blocks()
Get all rectification info blocks.
Definition: rectfile.cpp:138