Fawkes API  Fawkes Development Version
rectfile.cpp
00001 
00002 /***************************************************************************
00003  *  rectfile.cpp - Rectification info file
00004  *
00005  *  Created: Wed Oct 31 11:48:07 2007
00006  *  Copyright  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. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <fvutils/rectification/rectinfo.h>
00025 #include <fvutils/rectification/rectfile.h>
00026 #include <fvutils/rectification/rectinfo_block.h>
00027 #include <fvutils/rectification/rectinfo_lut_block.h>
00028 
00029 #include <core/exceptions/system.h>
00030 #include <utils/misc/strndup.h>
00031 
00032 #include <cstring>
00033 #include <cstdio>
00034 #include <errno.h>
00035 #include <netinet/in.h>
00036 #include <cstdlib>
00037 
00038 namespace firevision {
00039 #if 0 /* just to make Emacs auto-indent happy */
00040 }
00041 #endif
00042 
00043 /** @class RectificationInfoFile <fvutils/rectification/rectfile.h>
00044  * Rectification Info File.
00045  * This class provides access files that contain rectification info.
00046  * Currently it supports writing and reading of such data and supports
00047  * any number of rectificatoin info blocks (although this is limited
00048  * by the file format!).
00049  *
00050  * It follows the file format as defined in rectinfo.h. Files that are written
00051  * are always of the current version. The endianess is automatically set to the
00052  * current's system endianess.
00053  *
00054  * @author Tim Niemueller
00055  */
00056 
00057 /** Constructor.
00058  * @param cam_guid Camera globally unique identifier.
00059  * @param model String with the model name of the camera
00060  */
00061 RectificationInfoFile::RectificationInfoFile(uint64_t cam_guid, const char *model)
00062   : FireVisionDataFile(FIREVISION_RECTINFO_MAGIC, FIREVISION_RECTINFO_CURVER)
00063 {
00064   _spec_header      = calloc(1, sizeof(rectinfo_header_t));
00065   _spec_header_size = sizeof(rectinfo_header_t);
00066   _header = (rectinfo_header_t *)_spec_header;
00067 
00068   _cam_guid = cam_guid;
00069   _model = strdup(model);
00070 
00071   strncpy(_header->camera_model, _model, FIREVISION_RECTINFO_CAMERA_MODEL_MAXLENGTH);
00072   _header->guid = _cam_guid;
00073 }
00074 
00075 
00076 /** Constructor.
00077  * This constructor may only be used for reading files, as the GUID of the camera
00078  * is invalid for writing.
00079  */
00080 RectificationInfoFile::RectificationInfoFile()
00081   : FireVisionDataFile(FIREVISION_RECTINFO_MAGIC, FIREVISION_RECTINFO_CURVER)
00082 {
00083   _spec_header      = calloc(1, sizeof(rectinfo_header_t));
00084   _spec_header_size = sizeof(rectinfo_header_t);
00085   _header = (rectinfo_header_t *)_spec_header;
00086 
00087   _cam_guid = 0;
00088   _model = strdup("");
00089 
00090   strncpy(_header->camera_model, _model, FIREVISION_RECTINFO_CAMERA_MODEL_MAXLENGTH);
00091   _header->guid = _cam_guid;
00092 }
00093 
00094 
00095 /** Destructor. */
00096 RectificationInfoFile::~RectificationInfoFile()
00097 {
00098   free(_model);
00099 }
00100 
00101 
00102 /** Get the GUID of camera.
00103  * @return GUID of the camera this rectification info file belongs to.
00104  */
00105 uint64_t
00106 RectificationInfoFile::guid()
00107 {
00108   return _header->guid;
00109 }
00110 
00111 
00112 /** Get the model of the camera.
00113  * @return string with the camera's model name
00114  */
00115 const char *
00116 RectificationInfoFile::model()
00117 {
00118   return _model;
00119 }
00120 
00121 
00122 /** Add a rectification info block.
00123  * This instance takes over ownership of the rectinfo block. This means that the
00124  * object is automatically deleted if this instance is deleted.
00125  * @param block block to add
00126  */
00127 void
00128 RectificationInfoFile::add_rectinfo_block(RectificationInfoBlock *block)
00129 {
00130   add_block(block);
00131 }
00132 
00133 
00134 /** Get all rectification info blocks.
00135  * @return reference to internal vector of rectinfo blocks.
00136  */
00137 RectificationInfoFile::RectInfoBlockVector *
00138 RectificationInfoFile::rectinfo_blocks()
00139 {
00140   FireVisionDataFile::BlockList &b = blocks();
00141   printf("Processing blocks: %zu\n", b.size());
00142   RectInfoBlockVector *rv = new RectInfoBlockVector();
00143   for (std::list<FireVisionDataFileBlock *>::iterator i = b.begin(); i != b.end(); ++i) {
00144     printf("Processing block\n");
00145     if ((*i)->type() == FIREVISION_RECTINFO_TYPE_LUT_16x16) {
00146       printf("Pushing lut block\n");
00147       RectificationLutInfoBlock *libl = new RectificationLutInfoBlock(*i);
00148       rv->push_back(libl);
00149     }
00150   }
00151 
00152   return rv;
00153 }
00154 
00155 
00156 void
00157 RectificationInfoFile::read(const char *filename)
00158 {
00159   FireVisionDataFile::read(filename);
00160 
00161   _header = (rectinfo_header_t *)_spec_header;
00162 
00163   if (_model) free(_model);
00164   _model    = strndup(_header->camera_model, FIREVISION_RECTINFO_CAMERA_MODEL_MAXLENGTH);
00165   _cam_guid = _header->guid;
00166 }
00167 
00168 
00169 RectificationInfoFile::RectInfoBlockVector::~RectInfoBlockVector()
00170 {
00171   for (iterator i = begin(); i != end(); ++i) {
00172     delete *i;
00173   }
00174 }
00175 
00176 } // end namespace firevision