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

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