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