bb2rectlut.cpp

00001
00002 /***************************************************************************
00003  *  bb2rectlut.cpp - BB2 Rectification LUT utility
00004  *
00005  *  Created: Mon Oct 29 19:04:28 2007
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  Read the full text in the LICENSE.GPL file in the doc directory.
00021  */
00022
00023 #ifdef HAVE_BUMBLEBEE2_CAM
00024 #include <cams/bumblebee2.h>
00025 #endif
00026 #include <fvutils/system/camargp.h>
00027 #include <utils/system/argparser.h>
00028 #include <fvutils/rectification/rectfile.h>
00029 #include <fvutils/rectification/rectinfo_block.h>
00030 #include <fvutils/rectification/rectinfo_lut_block.h>
00031
00032 #ifdef HAVE_TRICLOPS_SDK
00033 #include <stereo/triclops.h>
00034 #include <cerrno>
00035 #endif
00036 
00037 #include <cstdlib>
00038 #include <cstdio>
00039
00040 using namespace fawkes;
00041
00042 void
00043 print_usage(ArgumentParser *argp)
00044 {
00045   printf("Usage: %s <-r|-v|-i> file.rectlut\n", argp->program_name());
00046   printf("You have to give at least one of -r/-v/-i and a file name\n"
00047          "  -r   retrieve rectification lut from live camera,\n"
00048          "       uses first found Bumblebee2 camera\n"
00049          "  -v   verify rectification lut, compares the identification\n"
00050          "       info stored in the file with the first currently\n"
00051          "       attached camera\n"
00052          "  -d   deep verifiction of rectification LUT, compares the identification\n"
00053          "       info stored in the file with the first currently attached camera. It\n"
00054          "       also verifies each single mapping on equality.\n"
00055          "  -i   print info about rectification LUT file\n\n"
00056          );
00057   exit(1);
00058 }
00059
00060
00061 int
00062 retrieve(ArgumentParser *argp)
00063 {
00064 #ifdef HAVE_BUMBLEBEE2_CAM
00065 #ifdef HAVE_TRICLOPS_SDK
00066   const char *lut_file = argp->items()[0];
00067
00068   if ( access(lut_file, F_OK) == 0) {
00069     fprintf(stderr, "File with name %s exists, delete manually and retry. Aborting.\n", lut_file);
00070     return -1;
00071   }
00072   if ( access(lut_file, W_OK) != 0) {
00073     // ENOENT is ok, we would have access, but there is no file, yet
00074     if ( errno != ENOENT ) {
00075       fprintf(stderr, "Cannot write to file %s, permission problem?\n", lut_file);
00076       return -2;
00077     }
00078   }
00079
00080   CameraArgumentParser *cap = new CameraArgumentParser("bumblebee2:Bumblebee2 BB2-03S2C");
00081   Bumblebee2Camera *bb2 = new Bumblebee2Camera(cap);
00082   bb2->open();
00083
00084   TriclopsStereoProcessor *triclops = new TriclopsStereoProcessor(bb2);
00085   triclops->generate_rectification_lut(lut_file);
00086   delete triclops;
00087
00088   bb2->close();
00089
00090   delete bb2;
00091   delete cap;
00092 #else
00093   printf("Retrieving the rectification LUT from a camera is not supported,\n"
00094          "because the Triclops SDK was not available at compile time.\n");
00095 #endif
00096 #else
00097   printf("Retrieving the rectification LUT from a camera is not supported,\n"
00098          "because the Bumblebee2 support was not available at compile time.\n");
00099 #endif
00100 
00101   return 0;
00102 }
00103
00104
00105 int
00106 verify(ArgumentParser *argp)
00107 {
00108   int rv = 0;
00109
00110 #ifdef HAVE_BUMBLEBEE2_CAM
00111   CameraArgumentParser *cap = new CameraArgumentParser("bumblebee2:Bumblebee2 BB2-03S2C");
00112   Bumblebee2Camera *bb2 = new Bumblebee2Camera(cap);
00113   bb2->open();
00114
00115   for (unsigned int i = 0; i < argp->num_items(); ++i) {
00116
00117     const char *lut_file = argp->items()[i];
00118
00119     if ( access(lut_file, F_OK) != 0) {
00120       fprintf(stderr, "File with name %s does not exist. Ignoring.\n", lut_file);
00121       continue;
00122     }
00123     if ( access(lut_file, R_OK) != 0) {
00124       fprintf(stderr, "Cannot read file %s, permission problem? Ingoring.\n", lut_file);
00125       continue;
00126     }
00127
00128     RectificationInfoFile *rif = new RectificationInfoFile();
00129     try {
00130       rif->read(lut_file);
00131
00132       if ( bb2->verify_guid( rif->guid() ) ) {
00133         printf("Success. The rectification info file has been created for the "
00134                "connected camera\n");
00135       } else {
00136         printf("Failure. The rectification info file has *not* been created "
00137                "for the connected camera\n");
00138         rv = 5;
00139       }
00140     } catch (Exception &e) {
00141       fprintf(stderr, "Failed to read lut file %s\n", lut_file);
00142       e.print_trace();
00143     }
00144
00145     delete rif;
00146
00147   }
00148
00149   bb2->close();
00150
00151   delete bb2;
00152   delete cap;
00153
00154 #else
00155   printf("Verifying the rectification LUT from a camera is not supported,\n"
00156          "because the Bumblebee2 support was not available at compile time.\n");
00157 #endif
00158 
00159   return rv;
00160 }
00161
00162
00163 int
00164 deep_verify(ArgumentParser *argp)
00165 {
00166 #ifdef HAVE_BUMBLEBEE2_CAM
00167 #ifdef HAVE_TRICLOPS_SDK
00168   int rv = 0;
00169
00170   CameraArgumentParser *cap = new CameraArgumentParser("bumblebee2:Bumblebee2 BB2-03S2C");
00171   Bumblebee2Camera *bb2 = new Bumblebee2Camera(cap);
00172   bb2->open();
00173
00174   TriclopsStereoProcessor *triclops = new TriclopsStereoProcessor(bb2);
00175
00176   for (unsigned int i = 0; i < argp->num_items(); ++i) {
00177
00178     const char *lut_file = argp->items()[i];
00179
00180     if ( access(lut_file, F_OK) != 0) {
00181       fprintf(stderr, "File with name %s does not exist. Ignoring.\n", lut_file);
00182       continue;
00183     }
00184     if ( access(lut_file, R_OK) != 0) {
00185       fprintf(stderr, "Cannot read file %s, permission problem? Ingoring.\n", lut_file);
00186       continue;
00187     }
00188
00189     if ( triclops->verify_rectification_lut(lut_file) ) {
00190       printf("Success. LUT file %s contains matching configuration data.\n", lut_file);
00191     } else {
00192       printf("Failure. LUT file %s does not contain matching configuration data.\n", lut_file);
00193     }
00194
00195   }
00196
00197   delete triclops;
00198   bb2->close();
00199
00200   delete bb2;
00201   delete cap;
00202
00203   return rv;
00204 #else
00205   printf("Deep verification of the rectification LUT from a camera is not supported,\n"
00206          "because the Triclops SDK was not available at compile time.\n");
00207   return 0;
00208 #endif
00209 #else
00210   printf("Deep verification of the rectification LUT from a camera is not supported,\n"
00211          "because the Bumblebee2 support was not available at compile time.\n");
00212   return 0;
00213 #endif
00214 }
00215
00216
00217 void
00218 print_info(ArgumentParser *argp)
00219 {
00220   for (unsigned int i = 0; i < argp->num_items(); ++i) {
00221
00222     const char *lut_file = argp->items()[i];
00223
00224     if ( access(lut_file, F_OK) != 0) {
00225       fprintf(stderr, "File with name %s does not exist. Ignoring.\n", lut_file);
00226       continue;
00227     }
00228     if ( access(lut_file, R_OK) != 0) {
00229       fprintf(stderr, "Cannot read file %s, permission problem? Ingoring.\n", lut_file);
00230       continue;
00231     }
00232
00233     RectificationInfoFile *rif = new RectificationInfoFile();
00234     try {
00235       rif->read(lut_file);
00236       RectificationInfoFile::RectInfoBlockVector *blocks = rif->rectinfo_blocks();
00237
00238       printf("File:         %s\n"
00239              "Version:      %u\n"
00240              "Endianess:    %s\n"
00241              "Num Blocks:   %zu/%zu (header/read)\n"
00242 #if __WORDSIZE == 64
00243              "GUID:         0x%016lX\n"
00244 #else
00245              "GUID:         0x%016llX\n"
00246 #endif
00247              "Camera Model: %s\n",
00248              lut_file, rif->version(),
00249              rif->is_little_endian() ? "little endian" : "big endian",
00250              rif->num_blocks(), blocks->size(),
00251              rif->guid(), rif->model());
00252
00253       unsigned int u = 1;
00254       RectificationInfoFile::RectInfoBlockVector::const_iterator b;
00255       for (b = blocks->begin(); b != blocks->end(); ++b) {
00256         RectificationInfoBlock *rib = *b;
00257
00258         printf("\nRectInfo Block No. %u\n"
00259                "Type:       %s\n"
00260                "Camera:     %s\n"
00261                "Size:       %zu\n",
00262                u++,
00263                rectinfo_type_strings[rib->type()],
00264                rectinfo_camera_strings[rib->camera()],
00265                rib->block_size());
00266
00267         switch (rib->type()) {
00268         case FIREVISION_RECTINFO_TYPE_LUT_16x16:
00269           {
00270             RectificationLutInfoBlock *rlib = dynamic_cast<RectificationLutInfoBlock *>(rib);
00271             if ( rlib == NULL ) {
00272               printf("** Failure to access LUT_16x16\n");
00273             } else {
00274               printf("LUT width:  %hu\n"
00275                      "LUT height: %hu\n",
00276                      rlib->pixel_width(), rlib->pixel_height());
00277             }
00278           }
00279           break;
00280         default:
00281           printf("** No additional information available for this info type\n");
00282           break;
00283         }
00284       }
00285
00286       delete blocks;
00287     } catch (Exception &e) {
00288       fprintf(stderr, "Failed to read lut file %s\n", lut_file);
00289       e.print_trace();
00290     }
00291
00292     delete rif;
00293
00294   }
00295 }
00296
00297
00298 int
00299 main(int argc, char **argv)
00300 {
00301
00302   ArgumentParser argp(argc, argv, "rvid");
00303
00304   if (argp.num_items() == 0) {
00305     print_usage(&argp);
00306   }
00307
00308   if ( argp.has_arg("r") ) {
00309     return retrieve(&argp);
00310   } else if ( argp.has_arg("v") ) {
00311     return verify(&argp);
00312   } else if ( argp.has_arg("d") ) {
00313     return deep_verify(&argp);
00314   } else if ( argp.has_arg("i") ) {
00315     print_info(&argp);
00316   } else {
00317     print_usage(&argp);
00318   }
00319
00320   return 0;
00321 }