Fawkes API  Fawkes Development Version
lookuptable.cpp
1 
2 /***************************************************************************
3  * lookuptable.cpp - Implementation of a lookup table color model
4  *
5  * Generated: Wed May 18 13:59:18 2005
6  * Copyright 2005 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvmodels/color/lookuptable.h>
25 
26 #include <fvutils/color/yuv.h>
27 #include <fvutils/colormap/yuvcm.h>
28 #include <fvutils/colormap/cmfile.h>
29 #include <fvutils/ipc/shm_lut.h>
30 
31 #include <core/exceptions/software.h>
32 #include <core/exceptions/system.h>
33 
34 #include <iostream>
35 #include <sys/utsname.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <errno.h>
40 #include <cstring>
41 #include <cstdlib>
42 #include <cmath>
43 
44 using namespace std;
45 using namespace fawkes;
46 
47 namespace firevision {
48 #if 0 /* just to make Emacs auto-indent happy */
49 }
50 #endif
51 
52 /** @class ColorModelLookupTable <fvmodels/color/lookuptable.h>
53  * Color model based on a lookup table.
54  * Very fast and easy implementation of a lookup table. It ignores
55  * the luminance and determines the classification just based on the U and
56  * V chrominance values. This model is very versatile as you can generate
57  * the lookuptable with many different methods.
58  */
59 
60 /** Create a lookup table with given dimensions _not_ using shared memory.
61  * @param colormap colormap to use, the colormap is consumed, meaning that the color model
62  * takes ownership of the colormap and deletes it in its dtor.
63  */
64 ColorModelLookupTable::ColorModelLookupTable(YuvColormap *colormap)
65 {
66  __colormap = colormap;
67 }
68 
69 /** Create a lookup table with given dimensions using shared memory
70  * @param lut_id ID of the LUT in shared memory
71  * @param destroy_on_free true to destroy lookup table in shmem on delete
72  */
73 ColorModelLookupTable::ColorModelLookupTable(const char *lut_id, bool destroy_on_free)
74 {
75  __colormap = new YuvColormap(lut_id, destroy_on_free);
76 }
77 
78 
79 /** Create a lookup table with given dimensions using shared memory
80  * @param depth depth of the lookup table
81  * @param lut_id ID of the LUT in shared memory
82  * @param destroy_on_free true to destroy lookup table in shmem on delete
83  */
84 ColorModelLookupTable::ColorModelLookupTable(unsigned int depth,
85  const char *lut_id, bool destroy_on_free)
86 {
87  __colormap = new YuvColormap(lut_id, destroy_on_free, depth);
88 }
89 
90 
91 /** Create a lookup table using shared memory, load contents from file.
92  * @param file name of the file to load from
93  * @param lut_id ID of the LUT in shared memory, use a constant from utils/shm_registry.h
94  * @param destroy_on_free true to destroy lookup table in shmem on delete
95  */
96 ColorModelLookupTable::ColorModelLookupTable(const char *file,
97  const char *lut_id, bool destroy_on_free)
98 {
99  ColormapFile cmf;
100  cmf.read(file);
101  Colormap *tcm = cmf.get_colormap();
102  YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm);
103  if ( ! tycm ) {
104  delete tcm;
105  throw TypeMismatchException("File does not contain a YUV colormap");
106  }
107  __colormap = new YuvColormap(tycm, lut_id, destroy_on_free);
108  delete tcm;
109 }
110 
111 
112 /** Create a lookup table, load contents from file.
113  * @param file name of the file to load from
114  */
115 ColorModelLookupTable::ColorModelLookupTable(const char *file)
116 {
117  ColormapFile cmf;
118  cmf.read(file);
119  Colormap *tcm = cmf.get_colormap();
120  __colormap = dynamic_cast<YuvColormap *>(tcm);
121  if ( ! __colormap ) {
122  delete tcm;
123  throw TypeMismatchException("File does not contain a YUV colormap");
124  }
125 }
126 
127 
128 /** Destructor. */
129 ColorModelLookupTable::~ColorModelLookupTable()
130 {
131  delete __colormap;
132 }
133 
134 color_t
135 ColorModelLookupTable::determine(unsigned int y, unsigned int u, unsigned int v) const
136 {
137  return __colormap->determine(y, u, v);
138 }
139 
140 const char *
141 ColorModelLookupTable::get_name()
142 {
143  return "ColorModelLookupTable";
144 }
145 
146 /** Get colormap.
147  * @return a pointer to the YUV colormap used internally.
148  */
149 YuvColormap *
150 ColorModelLookupTable::get_colormap() const
151 {
152  return __colormap;
153 }
154 
155 
156 /** Set colormap.
157  * @param yuvcm colormap to assign. The content of the colormap is copied
158  * into the internal one.
159  */
160 void
161 ColorModelLookupTable::set_colormap(const YuvColormap &yuvcm)
162 {
163  *__colormap = yuvcm;
164 }
165 
166 
167 /** Load colormap from file.
168  * @param filename name of colormap file
169  */
170 void
171 ColorModelLookupTable::load(const char *filename)
172 {
173  ColormapFile cmf;
174  cmf.read(filename);
175  Colormap *tcm = cmf.get_colormap();
176  YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm);
177  if ( ! tycm ) {
178  delete tcm;
179  throw TypeMismatchException("File does not contain a YUV colormap");
180  }
181  *__colormap = *tycm;
182  delete tcm;
183 }
184 
185 
186 /** Add colormaps.
187  * This adds the colormap of the given lookuptable color model to internals colormap.
188  * @param cmlt lookup table color model to copy data from
189  * @return this
190  */
192 ColorModelLookupTable::operator+=(const ColorModelLookupTable &cmlt)
193 {
194  *__colormap += *(cmlt.__colormap);
195  return *this;
196 }
197 
198 
199 /** Reset colormap. */
200 void
201 ColorModelLookupTable::reset()
202 {
203  __colormap->reset();
204 }
205 
206 /** Compose filename.
207  * @param format format string
208  * @return composed filename
209  * @see ColormapFile::compose_filename()
210  */
211 std::string
212 ColorModelLookupTable::compose_filename(const std::string format)
213 {
214  return ColormapFile::compose_filename(format);
215 }
216 
217 } // end namespace firevision
Fawkes library namespace.
void reset()
Reset colormap.
STL namespace.
Color model based on a lookup table.
Definition: lookuptable.h:37
Colormap interface.
Definition: colormap.h:38
Colormap * get_colormap()
Get a freshly generated colormap based on current file content.
Definition: cmfile.cpp:169
YUV Colormap.
Definition: yuvcm.h:39
virtual void read(const char *file_name)
Read file.
Definition: fvfile.cpp:308
Colormap file.
Definition: cmfile.h:55