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 <core/exceptions/software.h>
25 #include <core/exceptions/system.h>
26 #include <fvmodels/color/lookuptable.h>
27 #include <fvutils/color/yuv.h>
28 #include <fvutils/colormap/cmfile.h>
29 #include <fvutils/colormap/yuvcm.h>
30 #include <fvutils/ipc/shm_lut.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <sys/utsname.h>
34 
35 #include <cmath>
36 #include <cstdlib>
37 #include <cstring>
38 #include <errno.h>
39 #include <iostream>
40 #include <unistd.h>
41 
42 using namespace std;
43 using namespace fawkes;
44 
45 namespace firevision {
46 
47 /** @class ColorModelLookupTable <fvmodels/color/lookuptable.h>
48  * Color model based on a lookup table.
49  * Very fast and easy implementation of a lookup table. It ignores
50  * the luminance and determines the classification just based on the U and
51  * V chrominance values. This model is very versatile as you can generate
52  * the lookuptable with many different methods.
53  */
54 
55 /** Create a lookup table with given dimensions _not_ using shared memory.
56  * @param colormap colormap to use, the colormap is consumed, meaning that the color model
57  * takes ownership of the colormap and deletes it in its dtor.
58  */
59 ColorModelLookupTable::ColorModelLookupTable(YuvColormap *colormap)
60 {
61  colormap_ = colormap;
62 }
63 
64 /** Create a lookup table with given dimensions using shared memory
65  * @param lut_id ID of the LUT in shared memory
66  * @param destroy_on_free true to destroy lookup table in shmem on delete
67  */
68 ColorModelLookupTable::ColorModelLookupTable(const char *lut_id, bool destroy_on_free)
69 {
70  colormap_ = new YuvColormap(lut_id, destroy_on_free);
71 }
72 
73 /** Create a lookup table with given dimensions using shared memory
74  * @param depth depth of the lookup table
75  * @param lut_id ID of the LUT in shared memory
76  * @param destroy_on_free true to destroy lookup table in shmem on delete
77  */
78 ColorModelLookupTable::ColorModelLookupTable(unsigned int depth,
79  const char * lut_id,
80  bool destroy_on_free)
81 {
82  colormap_ = new YuvColormap(lut_id, destroy_on_free, depth);
83 }
84 
85 /** Create a lookup table using shared memory, load contents from file.
86  * @param file name of the file to load from
87  * @param lut_id ID of the LUT in shared memory, use a constant from utils/shm_registry.h
88  * @param destroy_on_free true to destroy lookup table in shmem on delete
89  */
90 ColorModelLookupTable::ColorModelLookupTable(const char *file,
91  const char *lut_id,
92  bool destroy_on_free)
93 {
94  ColormapFile cmf;
95  cmf.read(file);
96  Colormap * tcm = cmf.get_colormap();
97  YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm);
98  if (!tycm) {
99  delete tcm;
100  throw TypeMismatchException("File does not contain a YUV colormap");
101  }
102  colormap_ = new YuvColormap(tycm, lut_id, destroy_on_free);
103  delete tcm;
104 }
105 
106 /** Create a lookup table, load contents from file.
107  * @param file name of the file to load from
108  */
109 ColorModelLookupTable::ColorModelLookupTable(const char *file)
110 {
111  ColormapFile cmf;
112  cmf.read(file);
113  Colormap *tcm = cmf.get_colormap();
114  colormap_ = dynamic_cast<YuvColormap *>(tcm);
115  if (!colormap_) {
116  delete tcm;
117  throw TypeMismatchException("File does not contain a YUV colormap");
118  }
119 }
120 
121 /** Destructor. */
122 ColorModelLookupTable::~ColorModelLookupTable()
123 {
124  delete colormap_;
125 }
126 
127 color_t
128 ColorModelLookupTable::determine(unsigned int y, unsigned int u, unsigned int v) const
129 {
130  return colormap_->determine(y, u, v);
131 }
132 
133 const char *
134 ColorModelLookupTable::get_name()
135 {
136  return "ColorModelLookupTable";
137 }
138 
139 /** Get colormap.
140  * @return a pointer to the YUV colormap used internally.
141  */
142 YuvColormap *
143 ColorModelLookupTable::get_colormap() const
144 {
145  return colormap_;
146 }
147 
148 /** Set colormap.
149  * @param yuvcm colormap to assign. The content of the colormap is copied
150  * into the internal one.
151  */
152 void
153 ColorModelLookupTable::set_colormap(const YuvColormap &yuvcm)
154 {
155  *colormap_ = yuvcm;
156 }
157 
158 /** Load colormap from file.
159  * @param filename name of colormap file
160  */
161 void
162 ColorModelLookupTable::load(const char *filename)
163 {
164  ColormapFile cmf;
165  cmf.read(filename);
166  Colormap * tcm = cmf.get_colormap();
167  YuvColormap *tycm = dynamic_cast<YuvColormap *>(tcm);
168  if (!tycm) {
169  delete tcm;
170  throw TypeMismatchException("File does not contain a YUV colormap");
171  }
172  *colormap_ = *tycm;
173  delete tcm;
174 }
175 
176 /** Add colormaps.
177  * This adds the colormap of the given lookuptable color model to internals colormap.
178  * @param cmlt lookup table color model to copy data from
179  * @return this
180  */
182 ColorModelLookupTable::operator+=(const ColorModelLookupTable &cmlt)
183 {
184  *colormap_ += *(cmlt.colormap_);
185  return *this;
186 }
187 
188 /** Reset colormap. */
189 void
190 ColorModelLookupTable::reset()
191 {
192  colormap_->reset();
193 }
194 
195 /** Compose filename.
196  * @param format format string
197  * @return composed filename
198  * @see ColormapFile::compose_filename()
199  */
200 std::string
201 ColorModelLookupTable::compose_filename(const std::string format)
202 {
203  return ColormapFile::compose_filename(format);
204 }
205 
206 } // end namespace firevision
firevision::ColorModelLookupTable::reset
void reset()
Reset colormap.
Definition: lookuptable.cpp:190
firevision::YuvColormap
Definition: yuvcm.h:41
firevision::FireVisionDataFile::read
virtual void read(const char *file_name)
Read file.
Definition: fvfile.cpp:290
firevision::ColormapFile::get_colormap
Colormap * get_colormap()
Get a freshly generated colormap based on current file content.
Definition: cmfile.cpp:170
fawkes::TypeMismatchException
Definition: software.h:49
firevision::ColorModelLookupTable
Definition: lookuptable.h:40
fawkes
firevision::Colormap
Definition: colormap.h:42
firevision::ColormapFile
Definition: cmfile.h:60