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 using namespace firevision;
00042 
00043 void
00044 print_usage(ArgumentParser *argp)
00045 {
00046   printf("Usage: %s <-r|-v|-i> file.rectlut\n", argp->program_name());
00047   printf("You have to give at least one of -r/-v/-i and a file name\n"
00048          "  -r   retrieve rectification lut from live camera,\n"
00049          "       uses first found Bumblebee2 camera\n"
00050          "  -v   verify rectification lut, compares the identification\n"
00051          "       info stored in the file with the first currently\n"
00052          "       attached camera\n"
00053          "  -d   deep verifiction of rectification LUT, compares the identification\n"
00054          "       info stored in the file with the first currently attached camera. It\n"
00055          "       also verifies each single mapping on equality.\n"
00056          "  -i   print info about rectification LUT file\n\n"
00057          );
00058   exit(1);
00059 }
00060 
00061 
00062 int
00063 retrieve(ArgumentParser *argp)
00064 {
00065 #ifdef HAVE_BUMBLEBEE2_CAM
00066 #ifdef HAVE_TRICLOPS_SDK
00067   const char *lut_file = argp->items()[0];
00068 
00069   if ( access(lut_file, F_OK) == 0) {
00070     fprintf(stderr, "File with name %s exists, delete manually and retry. Aborting.\n", lut_file);
00071     return -1;
00072   }
00073   if ( access(lut_file, W_OK) != 0) {
00074     // ENOENT is ok, we would have access, but there is no file, yet
00075     if ( errno != ENOENT ) {
00076       fprintf(stderr, "Cannot write to file %s, permission problem?\n", lut_file);
00077       return -2;
00078     }
00079   }
00080 
00081   CameraArgumentParser *cap = new CameraArgumentParser("bumblebee2:Bumblebee2 BB2-03S2C");
00082   Bumblebee2Camera *bb2 = new Bumblebee2Camera(cap);
00083   bb2->open();
00084 
00085   TriclopsStereoProcessor *triclops = new TriclopsStereoProcessor(bb2);
00086   triclops->generate_rectification_lut(lut_file);
00087   delete triclops;
00088 
00089   bb2->close();
00090 
00091   delete bb2;
00092   delete cap;
00093 #else
00094   printf("Retrieving the rectification LUT from a camera is not supported,\n"
00095          "because the Triclops SDK was not available at compile time.\n");
00096 #endif
00097 #else
00098   printf("Retrieving the rectification LUT from a camera is not supported,\n"
00099          "because the Bumblebee2 support was not available at compile time.\n");
00100 #endif
00101 
00102   return 0;
00103 }
00104 
00105 
00106 int
00107 verify(ArgumentParser *argp)
00108 {
00109   int rv = 0;
00110 
00111 #ifdef HAVE_BUMBLEBEE2_CAM
00112   CameraArgumentParser *cap = new CameraArgumentParser("bumblebee2:Bumblebee2 BB2-03S2C");
00113   Bumblebee2Camera *bb2 = new Bumblebee2Camera(cap);
00114   bb2->open();
00115 
00116   for (unsigned int i = 0; i < argp->num_items(); ++i) {
00117 
00118     const char *lut_file = argp->items()[i];
00119 
00120     if ( access(lut_file, F_OK) != 0) {
00121       fprintf(stderr, "File with name %s does not exist. Ignoring.\n", lut_file);
00122       continue;
00123     }
00124     if ( access(lut_file, R_OK) != 0) {
00125       fprintf(stderr, "Cannot read file %s, permission problem? Ingoring.\n", lut_file);
00126       continue;
00127     }
00128 
00129     RectificationInfoFile *rif = new RectificationInfoFile();
00130     try {
00131       rif->read(lut_file);
00132 
00133       if ( bb2->verify_guid( rif->guid() ) ) {
00134         printf("Success. The rectification info file has been created for the "
00135                "connected camera\n");
00136       } else {
00137         printf("Failure. The rectification info file has *not* been created "
00138                "for the connected camera\n");
00139         rv = 5;
00140       } 
00141     } catch (Exception &e) {
00142       fprintf(stderr, "Failed to read lut file %s\n", lut_file);
00143       e.print_trace();
00144     }
00145 
00146     delete rif;
00147 
00148   }
00149 
00150   bb2->close();
00151     
00152   delete bb2;
00153   delete cap;
00154     
00155 #else
00156   printf("Verifying the rectification LUT from a camera is not supported,\n"
00157          "because the Bumblebee2 support was not available at compile time.\n");
00158 #endif
00159 
00160   return rv;
00161 }
00162 
00163 
00164 int
00165 deep_verify(ArgumentParser *argp)
00166 {
00167 #ifdef HAVE_BUMBLEBEE2_CAM
00168 #ifdef HAVE_TRICLOPS_SDK
00169   int rv = 0;
00170 
00171   CameraArgumentParser *cap = new CameraArgumentParser("bumblebee2:Bumblebee2 BB2-03S2C");
00172   Bumblebee2Camera *bb2 = new Bumblebee2Camera(cap);
00173   bb2->open();
00174 
00175   TriclopsStereoProcessor *triclops = new TriclopsStereoProcessor(bb2);
00176 
00177   for (unsigned int i = 0; i < argp->num_items(); ++i) {
00178 
00179     const char *lut_file = argp->items()[i];
00180 
00181     if ( access(lut_file, F_OK) != 0) {
00182       fprintf(stderr, "File with name %s does not exist. Ignoring.\n", lut_file);
00183       continue;
00184     }
00185     if ( access(lut_file, R_OK) != 0) {
00186       fprintf(stderr, "Cannot read file %s, permission problem? Ingoring.\n", lut_file);
00187       continue;
00188     }
00189 
00190     if ( triclops->verify_rectification_lut(lut_file) ) {
00191       printf("Success. LUT file %s contains matching configuration data.\n", lut_file);
00192     } else {
00193       printf("Failure. LUT file %s does not contain matching configuration data.\n", lut_file);
00194     }
00195 
00196   }
00197 
00198   delete triclops;
00199   bb2->close();
00200     
00201   delete bb2;
00202   delete cap;
00203     
00204   return rv;
00205 #else
00206   printf("Deep verification of the rectification LUT from a camera is not supported,\n"
00207          "because the Triclops SDK was not available at compile time.\n");
00208   return 0;
00209 #endif
00210 #else
00211   printf("Deep verification of the rectification LUT from a camera is not supported,\n"
00212          "because the Bumblebee2 support was not available at compile time.\n");
00213   return 0;
00214 #endif
00215 }
00216 
00217 
00218 void
00219 print_info(ArgumentParser *argp)
00220 {
00221   for (unsigned int i = 0; i < argp->num_items(); ++i) {
00222 
00223     const char *lut_file = argp->items()[i];
00224 
00225     if ( access(lut_file, F_OK) != 0) {
00226       fprintf(stderr, "File with name %s does not exist. Ignoring.\n", lut_file);
00227       continue;
00228     }
00229     if ( access(lut_file, R_OK) != 0) {
00230       fprintf(stderr, "Cannot read file %s, permission problem? Ingoring.\n", lut_file);
00231       continue;
00232     }
00233 
00234     RectificationInfoFile *rif = new RectificationInfoFile();
00235     try {
00236       rif->read(lut_file);
00237       RectificationInfoFile::RectInfoBlockVector *blocks = rif->rectinfo_blocks();
00238 
00239       printf("File:         %s\n"
00240              "Version:      %u\n"
00241              "Endianess:    %s\n"
00242              "Num Blocks:   %zu/%zu (header/read)\n"
00243 #if __WORDSIZE == 64
00244              "GUID:         0x%016lX\n"
00245 #else
00246              "GUID:         0x%016llX\n"
00247 #endif
00248              "Camera Model: %s\n",
00249              lut_file, rif->version(),
00250              rif->is_little_endian() ? "little endian" : "big endian",
00251              rif->num_blocks(), blocks->size(),
00252              rif->guid(), rif->model());
00253 
00254       unsigned int u = 1;
00255       RectificationInfoFile::RectInfoBlockVector::const_iterator b;
00256       for (b = blocks->begin(); b != blocks->end(); ++b) {
00257         RectificationInfoBlock *rib = *b;
00258 
00259         printf("\nRectInfo Block No. %u\n"
00260                "Type:       %s\n"
00261                "Camera:     %s\n"
00262                "Size:       %zu\n",
00263                u++,
00264                rectinfo_type_strings[rib->type()],
00265                rectinfo_camera_strings[rib->camera()],
00266                rib->block_size());
00267 
00268         switch (rib->type()) {
00269         case FIREVISION_RECTINFO_TYPE_LUT_16x16:
00270           {
00271             RectificationLutInfoBlock *rlib = dynamic_cast<RectificationLutInfoBlock *>(rib);
00272             if ( rlib == NULL ) {
00273               printf("** Failure to access LUT_16x16\n");
00274             } else {
00275               printf("LUT width:  %hu\n"
00276                      "LUT height: %hu\n",
00277                      rlib->pixel_width(), rlib->pixel_height());
00278             }
00279           }
00280           break;
00281         default:
00282           printf("** No additional information available for this info type\n");
00283           break;
00284         }
00285       }
00286 
00287       delete blocks;
00288     } catch (Exception &e) {
00289       fprintf(stderr, "Failed to read lut file %s\n", lut_file);
00290       e.print_trace();
00291     }
00292 
00293     delete rif;
00294 
00295   }
00296 }
00297 
00298 
00299 int
00300 main(int argc, char **argv)
00301 {
00302 
00303   ArgumentParser argp(argc, argv, "rvid");
00304 
00305   if (argp.num_items() == 0) {
00306     print_usage(&argp);
00307   }
00308 
00309   if ( argp.has_arg("r") ) {
00310     return retrieve(&argp);
00311   } else if ( argp.has_arg("v") ) {
00312     return verify(&argp);
00313   } else if ( argp.has_arg("d") ) {
00314     return deep_verify(&argp);
00315   } else if ( argp.has_arg("i") ) {
00316     print_info(&argp);
00317   } else {
00318     print_usage(&argp);
00319   }
00320 
00321   return 0;
00322 }

Generated on Tue Feb 22 13:32:17 2011 for Fawkes API by  doxygen 1.4.7