Fawkes API  Fawkes Development Version
lookuptable.cpp
00001 
00002 /***************************************************************************
00003  *  lookuptable.cpp - Implementation of a lookup table color model
00004  *
00005  *  Generated: Wed May 18 13:59:18 2005
00006  *  Copyright  2005  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 <fvmodels/color/lookuptable.h>
00025 
00026 #include <fvutils/color/yuv.h>
00027 #include <fvutils/colormap/yuvcm.h>
00028 #include <fvutils/colormap/cmfile.h>
00029 #include <fvutils/ipc/shm_lut.h>
00030 
00031 #include <core/exceptions/software.h>
00032 #include <core/exceptions/system.h>
00033 
00034 #include <iostream>
00035 #include <sys/utsname.h>
00036 #include <sys/stat.h>
00037 #include <unistd.h>
00038 #include <sys/types.h>
00039 #include <errno.h>
00040 #include <cstring>
00041 #include <cstdlib>
00042 #include <cmath>
00043 
00044 using namespace std;
00045 using namespace fawkes;
00046 
00047 namespace firevision {
00048 #if 0 /* just to make Emacs auto-indent happy */
00049 }
00050 #endif
00051 
00052 /** @class ColorModelLookupTable <fvmodels/color/lookuptable.h>
00053  * Color model based on a lookup table.
00054  * Very fast and easy implementation of a lookup table. It ignores
00055  * the luminance and determines the classification just based on the U and
00056  * V chrominance values. This model is very versatile as you can generate
00057  * the lookuptable with many different methods.
00058  */
00059 
00060 /** Create a lookup table with given dimensions _not_ using shared memory.
00061  * @param colormap colormap to use, the colormap is consumed, meaning that the color model
00062  * takes ownership of the colormap and deletes it in its dtor.
00063  */
00064 ColorModelLookupTable::ColorModelLookupTable(YuvColormap *colormap)
00065 {
00066   __colormap = colormap;
00067 }
00068 
00069 /** Create a lookup table with given dimensions using shared memory
00070  * @param lut_id ID of the LUT in shared memory
00071  * @param destroy_on_free true to destroy lookup table in shmem on delete
00072  */
00073 ColorModelLookupTable::ColorModelLookupTable(const char *lut_id, bool destroy_on_free)
00074 {
00075   __colormap = new YuvColormap(lut_id, destroy_on_free);
00076 }
00077 
00078 
00079 /** Create a lookup table with given dimensions using shared memory
00080  * @param depth depth of the lookup table
00081  * @param lut_id ID of the LUT in shared memory
00082  * @param destroy_on_free true to destroy lookup table in shmem on delete
00083  */
00084 ColorModelLookupTable::ColorModelLookupTable(unsigned int depth,
00085                                              const char *lut_id, bool destroy_on_free)
00086 {
00087   __colormap = new YuvColormap(lut_id, destroy_on_free, depth);
00088 }
00089 
00090 
00091 /** Create a lookup table using shared memory, load contents from file.
00092  * @param file name of the file to load from
00093  * @param lut_id ID of the LUT in shared memory, use a constant from utils/shm_registry.h
00094  * @param destroy_on_free true to destroy lookup table in shmem on delete
00095  */
00096 ColorModelLookupTable::ColorModelLookupTable(const char *file,
00097                                              const char *lut_id, bool destroy_on_free)
00098 {
00099   ColormapFile cmf;
00100   cmf.read(file);
00101   Colormap *tcm = cmf.get_colormap();
00102   YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm);
00103   if ( ! tycm ) {
00104     delete tcm;
00105     throw TypeMismatchException("File does not contain a YUV colormap");
00106   }
00107   __colormap = new YuvColormap(tycm, lut_id, destroy_on_free);
00108   delete tcm;
00109 }
00110 
00111 
00112 /** Create a lookup table, load contents from file.
00113  * @param file name of the file to load from
00114  */
00115 ColorModelLookupTable::ColorModelLookupTable(const char *file)
00116 {
00117   ColormapFile cmf;
00118   cmf.read(file);
00119   Colormap *tcm = cmf.get_colormap();
00120   __colormap = dynamic_cast<YuvColormap *>(tcm);
00121   if ( ! __colormap ) {
00122     delete tcm;
00123     throw TypeMismatchException("File does not contain a YUV colormap");
00124   }
00125 }
00126 
00127 
00128 /** Destructor. */
00129 ColorModelLookupTable::~ColorModelLookupTable()
00130 {
00131   delete __colormap;
00132 }
00133 
00134 
00135 const char *
00136 ColorModelLookupTable::get_name()
00137 {
00138   return "ColorModelLookupTable";
00139 }
00140 
00141 /** Get colormap.
00142  * @return a pointer to the YUV colormap used internally.
00143  */
00144 YuvColormap *
00145 ColorModelLookupTable::get_colormap() const
00146 {
00147   return __colormap;
00148 }
00149 
00150 
00151 /** Set colormap.
00152  * @param yuvcm colormap to assign. The content of the colormap is copied
00153  * into the internal one.
00154  */
00155 void
00156 ColorModelLookupTable::set_colormap(const YuvColormap &yuvcm)
00157 {
00158   *__colormap = yuvcm;
00159 }
00160 
00161 
00162 /** Load colormap from file.
00163  * @param filename name of colormap file
00164  */
00165 void
00166 ColorModelLookupTable::load(const char *filename)
00167 {
00168   ColormapFile cmf;
00169   cmf.read(filename);
00170   Colormap *tcm = cmf.get_colormap();
00171   YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm);
00172   if ( ! tycm ) {
00173     delete tcm;
00174     throw TypeMismatchException("File does not contain a YUV colormap");
00175   }
00176   *__colormap = *tycm;
00177   delete tcm;
00178 }
00179 
00180 
00181 /** Add colormaps.
00182  * This adds the colormap of the given lookuptable color model to internals colormap.
00183  * @param cmlt lookup table color model to copy data from
00184  * @return this
00185  */
00186 ColorModelLookupTable &
00187 ColorModelLookupTable::operator+=(const ColorModelLookupTable &cmlt)
00188 {
00189   *__colormap += *(cmlt.__colormap);
00190   return *this;
00191 }
00192 
00193 
00194 /** Reset colormap. */
00195 void
00196 ColorModelLookupTable::reset()
00197 {
00198   __colormap->reset();
00199 }
00200 
00201 /** Compose filename.
00202  * @param format format string
00203  * @return composed filename
00204  * @see ColormapFile::compose_filename()
00205  */
00206 std::string
00207 ColorModelLookupTable::compose_filename(const std::string format)
00208 {
00209   return ColormapFile::compose_filename(format);
00210 }
00211 
00212 } // end namespace firevision