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 <models/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 /** @class ColorModelLookupTable <models/color/lookuptable.h>
00048  * Color model based on a lookup table.
00049  * Very fast and easy implementation of a lookup table. It ignores
00050  * the luminance and determines the classification just based on the U and
00051  * V chrominance values. This model is very versatile as you can generate
00052  * the lookuptable with many different methods.
00053  */
00054 
00055 /** Create a lookup table with given dimensions _not_ using shared memory.
00056  * @param colormap colormap to use, the colormap is consumed, meaning that the color model
00057  * takes ownership of the colormap and deletes it in its dtor.
00058  */
00059 ColorModelLookupTable::ColorModelLookupTable(YuvColormap *colormap)
00060 {
00061   __colormap = colormap;
00062 }
00063 
00064 /** Create a lookup table with given dimensions using shared memory
00065  * @param lut_id ID of the LUT in shared memory
00066  * @param destroy_on_free true to destroy lookup table in shmem on delete
00067  */
00068 ColorModelLookupTable::ColorModelLookupTable(const char *lut_id, bool destroy_on_free)
00069 {
00070   __colormap = new YuvColormap(lut_id, destroy_on_free);
00071 }
00072
00073 
00074 /** Create a lookup table with given dimensions using shared memory
00075  * @param depth depth of the lookup table
00076  * @param lut_id ID of the LUT in shared memory
00077  * @param destroy_on_free true to destroy lookup table in shmem on delete
00078  */
00079 ColorModelLookupTable::ColorModelLookupTable(unsigned int depth,
00080                                              const char *lut_id, bool destroy_on_free)
00081 {
00082   __colormap = new YuvColormap(lut_id, destroy_on_free, depth);
00083 }
00084
00085 
00086 /** Create a lookup table using shared memory, load contents from file.
00087  * @param file name of the file to load from
00088  * @param lut_id ID of the LUT in shared memory, use a constant from utils/shm_registry.h
00089  * @param destroy_on_free true to destroy lookup table in shmem on delete
00090  */
00091 ColorModelLookupTable::ColorModelLookupTable(const char *file,
00092                                              const char *lut_id, bool destroy_on_free)
00093 {
00094   ColormapFile cmf;
00095   cmf.read(file);
00096   Colormap *tcm = cmf.get_colormap();
00097   YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm);
00098   if ( ! tycm ) {
00099     delete tcm;
00100     throw TypeMismatchException("File does not contain a YUV colormap");
00101   }
00102   __colormap = new YuvColormap(tycm, lut_id, destroy_on_free);
00103   delete tcm;
00104 }
00105
00106 
00107 /** Create a lookup table, load contents from file.
00108  * @param file name of the file to load from
00109  */
00110 ColorModelLookupTable::ColorModelLookupTable(const char *file)
00111 {
00112   ColormapFile cmf;
00113   cmf.read(file);
00114   Colormap *tcm = cmf.get_colormap();
00115   __colormap = dynamic_cast<YuvColormap *>(tcm);
00116   if ( ! __colormap ) {
00117     delete tcm;
00118     throw TypeMismatchException("File does not contain a YUV colormap");
00119   }
00120 }
00121
00122 
00123 /** Destructor. */
00124 ColorModelLookupTable::~ColorModelLookupTable()
00125 {
00126   delete __colormap;
00127 }
00128
00129
00130 const char *
00131 ColorModelLookupTable::get_name()
00132 {
00133   return "ColorModelLookupTable";
00134 }
00135 
00136 /** Get colormap.
00137  * @return a pointer to the YUV colormap used internally.
00138  */
00139 YuvColormap *
00140 ColorModelLookupTable::get_colormap() const
00141 {
00142   return __colormap;
00143 }
00144
00145 
00146 /** Load colormap from file.
00147  * @param filename name of colormap file
00148  */
00149 void
00150 ColorModelLookupTable::load(const char *filename)
00151 {
00152   ColormapFile cmf;
00153   cmf.read(filename);
00154   Colormap *tcm = cmf.get_colormap();
00155   YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm);
00156   if ( ! tycm ) {
00157     delete tcm;
00158     throw TypeMismatchException("File does not contain a YUV colormap");
00159   }
00160   *__colormap = *tycm;
00161   delete tcm;
00162 }
00163
00164 
00165 /** Add colormaps.
00166  * This adds the colormap of the given lookuptable color model to internals colormap.
00167  * @param cmlt lookup table color model to copy data from
00168  * @return this
00169  */
00170 ColorModelLookupTable &
00171 ColorModelLookupTable::operator+=(const ColorModelLookupTable &cmlt)
00172 {
00173   *__colormap += *(cmlt.__colormap);
00174   return *this;
00175 }
00176
00177 
00178 /** Reset colormap. */
00179 void
00180 ColorModelLookupTable::reset()
00181 {
00182   __colormap->reset();
00183 }
00184 
00185 /** Compose filename.
00186  * @param format format string
00187  * @return composed filename
00188  * @see ColormapFile::compose_filename()
00189  */
00190 std::string
00191 ColorModelLookupTable::compose_filename(const std::string format)
00192 {
00193   return ColormapFile::compose_filename(format);
00194 }