Fawkes API
Fawkes Development Version
|
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 namespace firevision { 00048 #if 0 /* just to make Emacs auto-indent happy */ 00049 } 00050 #endif 00051 00052 /** @class ColorModelLookupTable <models/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 /** Load colormap from file. 00152 * @param filename name of colormap file 00153 */ 00154 void 00155 ColorModelLookupTable::load(const char *filename) 00156 { 00157 ColormapFile cmf; 00158 cmf.read(filename); 00159 Colormap *tcm = cmf.get_colormap(); 00160 YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm); 00161 if ( ! tycm ) { 00162 delete tcm; 00163 throw TypeMismatchException("File does not contain a YUV colormap"); 00164 } 00165 *__colormap = *tycm; 00166 delete tcm; 00167 } 00168 00169 00170 /** Add colormaps. 00171 * This adds the colormap of the given lookuptable color model to internals colormap. 00172 * @param cmlt lookup table color model to copy data from 00173 * @return this 00174 */ 00175 ColorModelLookupTable & 00176 ColorModelLookupTable::operator+=(const ColorModelLookupTable &cmlt) 00177 { 00178 *__colormap += *(cmlt.__colormap); 00179 return *this; 00180 } 00181 00182 00183 /** Reset colormap. */ 00184 void 00185 ColorModelLookupTable::reset() 00186 { 00187 __colormap->reset(); 00188 } 00189 00190 /** Compose filename. 00191 * @param format format string 00192 * @return composed filename 00193 * @see ColormapFile::compose_filename() 00194 */ 00195 std::string 00196 ColorModelLookupTable::compose_filename(const std::string format) 00197 { 00198 return ColormapFile::compose_filename(format); 00199 } 00200 00201 } // end namespace firevision