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 
135 const char *
136 ColorModelLookupTable::get_name()
137 {
138  return "ColorModelLookupTable";
139 }
140 
141 /** Get colormap.
142  * @return a pointer to the YUV colormap used internally.
143  */
144 YuvColormap *
145 ColorModelLookupTable::get_colormap() const
146 {
147  return __colormap;
148 }
149 
150 
151 /** Set colormap.
152  * @param yuvcm colormap to assign. The content of the colormap is copied
153  * into the internal one.
154  */
155 void
156 ColorModelLookupTable::set_colormap(const YuvColormap &yuvcm)
157 {
158  *__colormap = yuvcm;
159 }
160 
161 
162 /** Load colormap from file.
163  * @param filename name of colormap file
164  */
165 void
166 ColorModelLookupTable::load(const char *filename)
167 {
168  ColormapFile cmf;
169  cmf.read(filename);
170  Colormap *tcm = cmf.get_colormap();
171  YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm);
172  if ( ! tycm ) {
173  delete tcm;
174  throw TypeMismatchException("File does not contain a YUV colormap");
175  }
176  *__colormap = *tycm;
177  delete tcm;
178 }
179 
180 
181 /** Add colormaps.
182  * This adds the colormap of the given lookuptable color model to internals colormap.
183  * @param cmlt lookup table color model to copy data from
184  * @return this
185  */
187 ColorModelLookupTable::operator+=(const ColorModelLookupTable &cmlt)
188 {
189  *__colormap += *(cmlt.__colormap);
190  return *this;
191 }
192 
193 
194 /** Reset colormap. */
195 void
196 ColorModelLookupTable::reset()
197 {
198  __colormap->reset();
199 }
200 
201 /** Compose filename.
202  * @param format format string
203  * @return composed filename
204  * @see ColormapFile::compose_filename()
205  */
206 std::string
207 ColorModelLookupTable::compose_filename(const std::string format)
208 {
209  return ColormapFile::compose_filename(format);
210 }
211 
212 } // 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