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