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